[4/7,v2] Introduce linux_proc_pid_to_exec_file

Message ID 1429103677-8524-1-git-send-email-gbenson@redhat.com
State Superseded
Headers

Commit Message

Gary Benson April 15, 2015, 1:14 p.m. UTC
  This commit introduces a new function linux_proc_pid_to_exec_file
that shared Linux code can use to discover the filename of the
executable that was run to create a process on the system.

gdb/ChangeLog:

	* nat/linux-procfs.h (linux_proc_pid_to_exec_file):
	New declaration.
	* nat/linux-procfs.c (linux_proc_pid_to_exec_file):
	New function, factored out from...
	* linux-nat.c (linux_child_pid_to_exec_file): ...here.
---
 gdb/ChangeLog          |    8 ++++++++
 gdb/linux-nat.c        |   10 +---------
 gdb/nat/linux-procfs.c |   19 +++++++++++++++++++
 gdb/nat/linux-procfs.h |    6 ++++++
 4 files changed, 34 insertions(+), 9 deletions(-)
  

Comments

Pedro Alves April 15, 2015, 4:01 p.m. UTC | #1
On 04/15/2015 02:14 PM, Gary Benson wrote:
> This commit introduces a new function linux_proc_pid_to_exec_file
> that shared Linux code can use to discover the filename of the
> executable that was run to create a process on the system.

I'd have the preferred the readlink change to be a separate change,
but, it's not worth it to split now.

> 
> gdb/ChangeLog:
> 
> 	* nat/linux-procfs.h (linux_proc_pid_to_exec_file):
> 	New declaration.
> 	* nat/linux-procfs.c (linux_proc_pid_to_exec_file):
> 	New function, factored out from...
> 	* linux-nat.c (linux_child_pid_to_exec_file): ...here.


OK.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 6c198cf..b04aa68 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4106,15 +4106,7 @@  linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
 static char *
 linux_child_pid_to_exec_file (struct target_ops *self, int pid)
 {
-  static char buf[PATH_MAX];
-  char name[PATH_MAX];
-
-  xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
-  memset (buf, 0, PATH_MAX);
-  if (readlink (name, buf, PATH_MAX - 1) <= 0)
-    strcpy (buf, name);
-
-  return buf;
+  return linux_proc_pid_to_exec_file (pid);
 }
 
 /* Implement the to_xfer_partial interface for memory reads using the /proc
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 7599b32..44364c5 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -273,3 +273,22 @@  linux_proc_task_list_dir_exists (pid_t pid)
   xsnprintf (pathname, sizeof (pathname), "/proc/%ld/task", (long) pid);
   return (stat (pathname, &buf) == 0);
 }
+
+/* See linux-procfs.h.  */
+
+char *
+linux_proc_pid_to_exec_file (int pid)
+{
+  static char buf[PATH_MAX];
+  char name[PATH_MAX];
+  ssize_t len;
+
+  xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
+  len = readlink (name, buf, PATH_MAX - 1);
+  if (len <= 0)
+    strcpy (buf, name);
+  else
+    buf[len] = '\0';
+
+  return buf;
+}
diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h
index c4f5788..fdbf383 100644
--- a/gdb/nat/linux-procfs.h
+++ b/gdb/nat/linux-procfs.h
@@ -73,4 +73,10 @@  extern void linux_proc_attach_tgid_threads (pid_t pid,
 /* Return true if the /proc/PID/task/ directory exists.  */
 extern int linux_proc_task_list_dir_exists (pid_t pid);
 
+/* Return the full absolute name of the executable file that was run
+   to create the process PID.  The returned value persists until this
+   function is next called.  */
+
+extern char *linux_proc_pid_to_exec_file (int pid);
+
 #endif /* COMMON_LINUX_PROCFS_H */