From patchwork Wed Apr 15 13:14:37 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gary Benson X-Patchwork-Id: 6230 Received: (qmail 80716 invoked by alias); 15 Apr 2015 13:14:45 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 80696 invoked by uid 89); 15 Apr 2015 13:14:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 15 Apr 2015 13:14:43 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3FDEdTK015484 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 15 Apr 2015 09:14:40 -0400 Received: from blade.nx (ovpn-116-95.ams2.redhat.com [10.36.116.95]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3FDEdq5009605; Wed, 15 Apr 2015 09:14:39 -0400 Received: from blade.nx (localhost [127.0.0.1]) by blade.nx (Postfix) with ESMTP id DFE05263FB8; Wed, 15 Apr 2015 14:14:37 +0100 (BST) From: Gary Benson To: gdb-patches@sourceware.org Cc: Pedro Alves , Doug Evans Subject: [PATCH 4/7 v2] Introduce linux_proc_pid_to_exec_file Date: Wed, 15 Apr 2015 14:14:37 +0100 Message-Id: <1429103677-8524-1-git-send-email-gbenson@redhat.com> In-Reply-To: <552E314B.9020404@redhat.com> References: <552E314B.9020404@redhat.com> X-IsSubscribed: yes 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(-) 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 */