[1/3] Move linux thread name retrieval code to linux-procfs.c

Message ID 1443700496-2936-2-git-send-email-pclouds@gmail.com
State New, archived
Headers

Commit Message

Nguyễn Thái Ngọc Duy Oct. 1, 2015, 11:54 a.m. UTC
  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(-)
  

Patch

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index eb9f5bb..5d677de 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -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
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 24bcb01..7aa327b 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -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;
+}
diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h
index f9cad39..108f7a5 100644
--- a/gdb/nat/linux-procfs.h
+++ b/gdb/nat/linux-procfs.h
@@ -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 */