This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH 4/7] Introduce linux_pid_to_exec_file
- From: Doug Evans <xdje42 at gmail dot com>
- To: Gary Benson <gbenson at redhat dot com>
- Cc: "gdb-patches at sourceware dot org" <gdb-patches at sourceware dot org>
- Date: Tue, 7 Apr 2015 20:15:30 -0700
- Subject: Re: [PATCH 4/7] Introduce linux_pid_to_exec_file
- Authentication-results: sourceware.org; auth=none
- References: <1427887341-31819-1-git-send-email-gbenson at redhat dot com> <1427887341-31819-5-git-send-email-gbenson at redhat dot com> <m3twwti51n dot fsf at sspiff dot org> <20150407090705 dot GA22271 at blade dot nx>
On Tue, Apr 7, 2015 at 2:07 AM, Gary Benson <gbenson@redhat.com> wrote:
> Doug Evans wrote:
>> Gary Benson <gbenson@redhat.com> writes:
>> > diff --git a/gdb/nat/linux-nat.c b/gdb/nat/linux-nat.c
>> > new file mode 100644
>> > index 0000000..b9deae3
>> > --- /dev/null
>> > +++ b/gdb/nat/linux-nat.c
>> > @@ -0,0 +1,37 @@
> ...
>> > +/* See nat/linux-nat.h. */
>> > +
>> > +char *
>> > +linux_pid_to_exec_file (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;
>> > +}
Hi. Another nit.
Since readlink does not nul-terminate the string, I think it would be
clearer to explicitly nul-terminate the result instead of doing
the initial memset. I realize the current linux-nat.c version
doesn't do this, but we can still improve things here.
[One might also want the code to protect itself in the case
where the buf is not large enough, but one can leave that
for now since we assume PATH_MAX is big enough
throughout.]