Re: [PATCH 4/7] Introduce linux_pid_to_exec_file

Doug Evans
2015-04-06T16:41:10Z

Gary Benson <gbenson@redhat.com> writes:
> This commit introduces a new function, linux_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-nat.h (linux_pid_to_exec_file): New declaration.
> * nat/linux-nat.c: New file.
> * Makefile.in (common-linux-nat.o): New rule.
> * config/aarch64/linux.mh (NATDEPFILES): Add common-linux-nat.o.
> * config/alpha/alpha-linux.mh (NATDEPFILES): Likewise.
> * config/arm/linux.mh (NATDEPFILES): Likewise.
> * config/i386/linux.mh (NATDEPFILES): Likewise.
> * config/i386/linux64.mh (NATDEPFILES): Likewise.
> * config/ia64/linux.mh (NATDEPFILES): Likewise.
> * config/m32r/linux.mh (NATDEPFILES): Likewise.
> * config/m68k/linux.mh (NATDEPFILES): Likewise.
> * config/mips/linux.mh (NATDEPFILES): Likewise.
> * config/pa/linux.mh (NATDEPFILES): Likewise.
> * config/powerpc/linux.mh (NATDEPFILES): Likewise.
> * config/powerpc/ppc64-linux.mh (NATDEPFILES): Likewise.
> * config/powerpc/spu-linux.mh (NATDEPFILES): Likewise.
> * config/s390/linux.mh (NATDEPFILES): Likewise.
> * config/sparc/linux.mh (NATDEPFILES): Likewise.
> * config/sparc/linux64.mh (NATDEPFILES): Likewise.
> * config/tilegx/linux.mh (NATDEPFILES): Likewise.
> * config/xtensa/linux.mh (NATDEPFILES): Likewise.
> * linux-nat.c (linux_child_pid_to_exec_file): Factored out
> to new function linux_pid_to_exec_file in nat/linux-nat.c.
>
> ...
>
> 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 @@
> +/* Native-dependent code for GNU/Linux
> +
> + Copyright (C) 2000-2015 Free Software Foundation, Inc.
> +
> + This file is part of GDB.
> +
> + This program is free software; you can redistribute it and/or modify
> + it under the terms of the GNU General Public License as published by
> + the Free Software Foundation; either version 3 of the License, or
> + (at your option) any later version.
> +
> + This program is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + GNU General Public License for more details.
> +
> + You should have received a copy of the GNU General Public License
> + along with this program. If not, see <http://www.gnu.org/licenses/>. */
> +
> +#include "common-defs.h"
> +#include "nat/linux-nat.h"
> +
> +/* 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 enum target_stop_reason lwp_stop_reason (struct lwp_info *lwp);
>
> 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 */

Hi.

I like the idea of returning NULL if the executable file cannot be
determined, but the implementation doesn't do this.

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