Re: [PATCH 4/7] Introduce linux_pid_to_exec_file

Gary Benson
2015-04-07T09:07:10Z

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;
> > +}
> > diff --git a/gdb/nat/linux-nat.h b/gdb/nat/linux-nat.h
> > index 7cdaf40..81c2edc 100644
> > --- a/gdb/nat/linux-nat.h
> > +++ b/gdb/nat/linux-nat.h
> > @@ -78,4 +78,13 @@
> >
> > extern void linux_stop_lwp (struct lwp_info *lwp);
> >
> > +/* Return the pathname of the executable file that was run to create
> > + the process PID. If the executable file cannot be determined, NULL
> > + is returned. Otherwise, a pointer to a character string containing
> > + the pathname is returned. This string should be copied into a
> > + buffer by the client if the string will not be immediately used, or
> > + if it must persist. */
> > +
> > +extern char *linux_pid_to_exec_file (int pid);
> > +
> > #endif /* LINUX_NAT_H */
>
> I like the idea of returning NULL if the executable file cannot be
> determined, but the implementation doesn't do this.

You're right, I will remove that part of the documentation comment.

> Also, while I don't have a strong opinion, it seems preferable
> to return a const char *.

The function is the implementation of target_pid_to_exec_file, via
a wrapper, and that does not return const. I briefly attempted to
constify this but it snowballed.

Thanks,
Gary

--