This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH 0/1] Fix internal warning when "gdb -p xxx"
- From: Hui Zhu <hui_zhu at mentor dot com>
- To: Pedro Alves <palves at redhat dot com>
- Cc: gdb-patches ml <gdb-patches at sourceware dot org>
- Date: Thu, 20 Mar 2014 10:58:11 +0800
- Subject: Re: [PATCH 0/1] Fix internal warning when "gdb -p xxx"
- Authentication-results: sourceware.org; auth=none
- References: <53271C09 dot 5000709 at mentor dot com> <53271ED2 dot 6010707 at redhat dot com> <53272958 dot 1080605 at redhat dot com> <5327F7D0 dot 1050304 at mentor dot com> <53281C69 dot 7090703 at redhat dot com> <532915BF dot 2090608 at mentor dot com> <53296E88 dot 9040503 at redhat dot com>
On 03/19/14 18:16, Pedro Alves wrote:
On 03/19/2014 03:57 AM, Hui Zhu wrote:
Same with the nul termination. The most standard solution is:
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 != -1)
{
buf[len] = '\0';
return buf;
}
return NULL;
I make a new patch according to your comments.
Please help me review it.
The patch changes the bsd implementations's behavior, because
you made them return the /proc path when readlink fails (like
the Linux version does), instead of what the current code does
or what I suggested above.
I made a new version that change fbsd_pid_to_exec_file and nbsd_pid_to_exec_file
to your suggested above.
Please help me review it.
Thanks,
Hui
2014-03-20 Hui Zhu <hui@codesourcery.com>
Pedro Alves <palves@redhat.com>
* darwin-nat.c (darwin_pid_to_exec_file): Change xmalloc to
static buffer.
* fbsd-nat.c (fbsd_pid_to_exec_file): Ditto.
* linux-nat.c (linux_child_pid_to_exec_file): Ditto.
* nbsd-nat.c (nbsd_pid_to_exec_file): Ditto.
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1991,12 +1991,9 @@ set_enable_mach_exceptions (char *args,
static char *
darwin_pid_to_exec_file (struct target_ops *self, int pid)
{
- char *path;
+ static char path[PATH_MAX];
int res;
- path = xmalloc (PATH_MAX);
- make_cleanup (xfree, path);
-
res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, PATH_MAX);
if (res >= 0)
return path;
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -40,8 +40,8 @@ char *
fbsd_pid_to_exec_file (struct target_ops *self, int pid)
{
size_t len = PATH_MAX;
- char *buf = xcalloc (len, sizeof (char));
- char *path;
+ static char buf[PATH_MAX];
+ char name[PATH_MAX];
#ifdef KERN_PROC_PATHNAME
int mib[4];
@@ -54,15 +54,15 @@ fbsd_pid_to_exec_file (struct target_ops
return buf;
#endif
- path = xstrprintf ("/proc/%d/file", pid);
- if (readlink (path, buf, PATH_MAX - 1) == -1)
+ xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
+ len = readlink (name, buf, PATH_MAX - 1);
+ if (len != -1)
{
- xfree (buf);
- buf = NULL;
+ buf[len] = '\0';
+ return buf;
}
- xfree (path);
- return buf;
+ return NULL;
}
static int
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4011,19 +4011,15 @@ linux_nat_thread_name (struct target_ops
static char *
linux_child_pid_to_exec_file (struct target_ops *self, int pid)
{
- char *name1, *name2;
+ static char buf[PATH_MAX];
+ char name[PATH_MAX];
- name1 = xmalloc (PATH_MAX);
- name2 = xmalloc (PATH_MAX);
- make_cleanup (xfree, name1);
- make_cleanup (xfree, name2);
- memset (name2, 0, 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);
- xsnprintf (name1, PATH_MAX, "/proc/%d/exe", pid);
- if (readlink (name1, name2, PATH_MAX - 1) > 0)
- return name2;
- else
- return name1;
+ return buf;
}
/* Records the thread's register state for the corefile note
--- a/gdb/nbsd-nat.c
+++ b/gdb/nbsd-nat.c
@@ -27,17 +27,17 @@
char *
nbsd_pid_to_exec_file (struct target_ops *self, int pid)
{
- size_t len = PATH_MAX;
- char *buf = xcalloc (len, sizeof (char));
- char *path;
+ size_t len;
+ static char buf[PATH_MAX];
+ char name[PATH_MAX];
- path = xstrprintf ("/proc/%d/exe", pid);
- if (readlink (path, buf, PATH_MAX - 1) == -1)
+ xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
+ len = readlink (name, buf, PATH_MAX - 1);
+ if (len != -1)
{
- xfree (buf);
- buf = NULL;
+ buf[len] = '\0';
+ return buf;
}
- xfree (path);
- return buf;
+ return NULL;
}