[1/3] Move linux thread name retrieval code to linux-procfs.c
Commit Message
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
gdb/linux-nat.c | 33 +--------------------------------
gdb/nat/linux-procfs.c | 39 +++++++++++++++++++++++++++++++++++++++
gdb/nat/linux-procfs.h | 5 +++++
3 files changed, 45 insertions(+), 32 deletions(-)
@@ -4083,38 +4083,7 @@ linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
static char *
linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
{
- int pid = ptid_get_pid (thr->ptid);
- long lwp = ptid_get_lwp (thr->ptid);
-#define FORMAT "/proc/%d/task/%ld/comm"
- char buf[sizeof (FORMAT) + 30];
- FILE *comm_file;
- char *result = NULL;
-
- snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
- comm_file = gdb_fopen_cloexec (buf, "r");
- if (comm_file)
- {
- /* Not exported by the kernel, so we define it here. */
-#define COMM_LEN 16
- static char line[COMM_LEN + 1];
-
- if (fgets (line, sizeof (line), comm_file))
- {
- char *nl = strchr (line, '\n');
-
- if (nl)
- *nl = '\0';
- if (*line != '\0')
- result = line;
- }
-
- fclose (comm_file);
- }
-
-#undef COMM_LEN
-#undef FORMAT
-
- return result;
+ return linux_proc_thread_name(thr->ptid);
}
/* Accepts an integer PID; Returns a string representing a file that
@@ -273,3 +273,42 @@ linux_proc_pid_to_exec_file (int pid)
return buf;
}
+
+/* See linux-procfs.h. */
+
+char *
+linux_proc_thread_name (ptid_t ptid)
+{
+ int pid = ptid_get_pid (ptid);
+ long lwp = ptid_get_lwp (ptid);
+#define FORMAT "/proc/%d/task/%ld/comm"
+ char buf[sizeof (FORMAT) + 30];
+ FILE *comm_file;
+ char *result = NULL;
+
+ snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
+ comm_file = gdb_fopen_cloexec (buf, "r");
+ if (comm_file)
+ {
+ /* Not exported by the kernel, so we define it here. */
+#define COMM_LEN 16
+ static char line[COMM_LEN + 1];
+
+ if (fgets (line, sizeof (line), comm_file))
+ {
+ char *nl = strchr (line, '\n');
+
+ if (nl)
+ *nl = '\0';
+ if (*line != '\0')
+ result = line;
+ }
+
+ fclose (comm_file);
+ }
+
+#undef COMM_LEN
+#undef FORMAT
+
+ return result;
+}
@@ -73,4 +73,9 @@ extern int linux_proc_task_list_dir_exists (pid_t pid);
extern char *linux_proc_pid_to_exec_file (int pid);
+/* Return thread name in /proc/PID/task/TID/comm. The returned value
+ persists until this function is next called. */
+
+extern char *linux_proc_thread_name (ptid_t ptid);
+
#endif /* COMMON_LINUX_PROCFS_H */