This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
RE: [PATCH][PR threads/15824] when get threads name failed from info threads with linux kernel version earlier than 2.6.33
- From: "Will Huang" <will dot huang at aliyun-inc dot com>
- To: "'Yao Qi'" <yao at codesourcery dot com>
- Cc: <palves at redhat dot com>, <gdb-patches at sourceware dot org>
- Date: Mon, 19 Aug 2013 13:01:00 +0800
- Subject: RE: [PATCH][PR threads/15824] when get threads name failed from info threads with linux kernel version earlier than 2.6.33
- References: <00b201ce98a6$7cf69150$76e3b3f0$ at huang@aliyun-inc.com> <520D8D9A dot 3050003 at codesourcery dot com> <016701ce9a41$f39086d0$dab19470$ at huang@aliyun-inc.com> <520DF2E2 dot 3020407 at codesourcery dot com> <01a201ce9a6a$74436db0$5cca4910$ at huang@aliyun-inc.com> <520E0CFB dot 9010203 at codesourcery dot com> <000301ce9c87$19927630$4cb76290$ at huang@aliyun-inc.com> <52118C73 dot 1090604 at codesourcery dot com>
Yes, inux_proc_parse_status is more straightforward than linux_proc_get_string.
Changes updated.
diff -ruN ./gdb.org/ChangeLog ./gdb/ChangeLog
--- ./gdb.org/ChangeLog 2013-08-16 12:30:22.000000000 +0800
+++ ./gdb/ChangeLog 2013-08-19 11:45:35.000000000 +0800
@@ -1,3 +1,11 @@
+2013-08-14 Will Huang <will.huang@aliyun-inc.com>
+
+ PR threads/15824
+ * linux-nat.c (linux_nat_thread_name): Add get thread name from
+ procfs "/proc/$pid/task/$tid/status".
+ * commo/linux-procfs.c (linux_proc_get_thread_name): New.
+ (linux_proc_parse_status): New.
+
2013-08-15 Kevin Buettner <kevinb@redhat.com>
* rl78-tdep.c (RL78_RAW_PC_REGNUM): New enum.
diff -ruN ./gdb.org/common/linux-procfs.c ./gdb/common/linux-procfs.c
--- ./gdb.org/common/linux-procfs.c 2013-04-23 00:46:15.000000000 +0800
+++ ./gdb/common/linux-procfs.c 2013-08-19 11:46:36.000000000 +0800
@@ -56,6 +56,58 @@
return retval;
}
+/* Parse file /proc/LWPID/task/TID/status, save the value of FIELD
+ in buffer TARGET, whose length is T_SIZE, and return the length
+ of the value. If TID is zero, parse /proc/LWPID/status instead.
+ Return -1 if not found. */
+
+static int
+linux_proc_parse_status (pid_t lwpid, pid_t tid, char *target,
+ size_t t_size, const char *field)
+{
+ size_t field_len = strlen (field);
+ FILE *status_file;
+ char buf[100];
+ int retval = -1;
+
+ if (tid > 0)
+ xsnprintf (buf, sizeof (buf), "/proc/%d/task/%d/status",
+ (int) lwpid, (int) tid);
+ else
+ {
+ /*if TID is zero, ingnore it */
+ xsnprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
+ }
+
+ status_file = gdb_fopen_cloexec (buf, "r");
+ if (status_file == NULL)
+ {
+ warning (_("unable to open /proc file '%s'"), buf);
+ return -1;
+ }
+
+ while (fgets (buf, sizeof (buf), status_file))
+ if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':')
+ {
+ size_t pos = field_len + 1;
+
+ while ((buf[pos] == ' ' || buf[pos] == '\t')
+ && (pos < sizeof (buf) - 1))
+ pos++;
+
+ strncpy (target, &buf[pos], t_size);
+ target[t_size - 1] = '\0';
+ retval = strlen (target);
+ if (retval > 0 && target[retval - 1] == '\n')
+ target[retval - 1] = '\0';
+
+ break;
+ }
+
+ fclose (status_file);
+ return retval;
+}
+
/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
found. */
@@ -119,3 +171,13 @@
{
return linux_proc_pid_has_state (pid, "Z (zombie)");
}
+
+/* Save the name of thread PID/TID into buffer NAME. SIZE is the size
+ of buffer NAME. Return the length of the name if success,
+ otherwise return -1. */
+
+int
+linux_proc_get_thread_name (pid_t pid, pid_t tid, char *name, size_t size)
+{
+ return linux_proc_parse_status (pid, tid, name, size, "Name");
+}
diff -ruN ./gdb.org/common/linux-procfs.h ./gdb/common/linux-procfs.h
--- ./gdb.org/common/linux-procfs.h 2013-01-01 14:32:54.000000000 +0800
+++ ./gdb/common/linux-procfs.h 2013-08-19 10:32:57.000000000 +0800
@@ -40,4 +40,10 @@
extern int linux_proc_pid_is_zombie (pid_t pid);
+/* Save the name of thread PID/TID into buffer NAME. SIZE is the size
+ of buffer NAME. Return the length of the name if success,
+ otherwise return -1. */
+
+extern int linux_proc_get_thread_name (pid_t pid, pid_t tid,
+ char *name, size_t size);
#endif /* COMMON_LINUX_PROCFS_H */
diff -ruN ./gdb.org/linux-nat.c ./gdb/linux-nat.c
--- ./gdb.org/linux-nat.c 2013-07-01 19:28:30.000000000 +0800
+++ ./gdb/linux-nat.c 2013-08-19 09:45:34.000000000 +0800
@@ -4291,6 +4291,17 @@
fclose (comm_file);
}
+ else
+ {
+ static char comm[COMM_LEN + 1];
+ int size = linux_proc_get_thread_name ((pid_t) pid, (pid_t) lwp,
+ comm, COMM_LEN + 1);
+
+ if (size > 0)
+ result = comm;
+
+ }
+
#undef COMM_LEN
#undef FORMAT
diff -ruN ./gdb.org/testsuite/ChangeLog ./gdb/testsuite/ChangeLog
--- ./gdb.org/testsuite/ChangeLog 2013-08-17 01:44:21.000000000 +0800
+++ ./gdb/testsuite/ChangeLog 2013-08-19 09:46:08.000000000 +0800
@@ -1,3 +1,9 @@
+2013-08-14 Will Huang <will.huang@aliyun-inc.com>
+
+ PR threads/15824
+ * gdb.threads/manythreads.exp: Add test for get thread
+ original name.
+
2013-08-16 Doug Evans <dje@google.com>
* lib/prelink-support.exp (prelink_yes): Flag test as unsupported if
diff -ruN ./gdb.org/testsuite/gdb.threads/manythreads.exp ./gdb/testsuite/gdb.threads/manythreads.exp
--- ./gdb.org/testsuite/gdb.threads/manythreads.exp 2013-01-01 14:41:27.000000000 +0800
+++ ./gdb/testsuite/gdb.threads/manythreads.exp 2013-08-19 09:47:10.000000000 +0800
@@ -93,6 +93,7 @@
}
}
+gdb_test "info threads" ".*manythreads.*" "check thread original name"
gdb_test_no_output "thread name zardoz" "give a name to the thread"
gdb_test "info threads" ".*zardoz.*" "check thread name"
Thanks,
Will Huang
> -----Original Message-----
> From: Yao Qi [mailto:yao@codesourcery.com]
> Sent: Monday, August 19, 2013 11:10 AM
> To: Will Huang
> Cc: palves@redhat.com; gdb-patches@sourceware.org
> Subject: Re: [PATCH][PR threads/15824] when get threads name failed from info
> threads with linux kernel version earlier than 2.6.33
>
> On 08/19/2013 10:52 AM, Will Huang wrote:
> > +
> > +/* Return the string length of FILED /proc/LWPID/task/TID/status.
> > + Return -1 if not found. */
> > +
> > +static int
> > +linux_proc_get_string (pid_t lwpid, pid_t tid, char *target,
> > + size_t t_size, const char *field)
>
>
> I suggest renaming this function to "linux_proc_parse_status" or something else,
> and adjust the comment.
>
> /* Parse file /proc/LWPID/task/TID/status, save the value of FIELD
> in buffer TARGET, whose length is T_SIZE, and return the length
> of the value. If TID is zero, parse /proc/LWPID/status instead.
> Return -1 if not found. */
>
> static int
> linux_proc_parse_status (pid_t lwpid, pid_t tid, char *target,
> size_t t_size, const char *field)
> --
> Yao (éå)