Constify get_exec_file

Message ID 20191213221108.8948-1-tromey@adacore.com
State New, archived
Headers

Commit Message

Tom Tromey Dec. 13, 2019, 10:11 p.m. UTC
  I noticed that get_exec_file could return a "const char *".  This
patch implements this change.

I couldn't build all the code -- but I did build Linux native and a
mingw cross.

Consequently, the NTO code has a hack, where it casts away const.  I
think this can be removed, but that required more work there, and
since I couldn't compile it, I felt it best not to try.

Let me know what you think.

gdb/ChangeLog
2019-12-13  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (windows_nat_target::attach): Update.
	* remote.c (extended_remote_target::attach): Update.
	* procfs.c (procfs_target::attach): Update.
	* nto-procfs.c (nto_procfs_target::attach): Update.
	(nto_procfs_target::create_inferior): Update.
	* inf-ptrace.c (inf_ptrace_target::attach): Update.
	* gnu-nat.c (gnu_nat_target::attach): Update.
	(gnu_nat_target::detach): Update.
	* darwin-nat.c (darwin_nat_target::attach): Update.
	* corefile.c (get_exec_file): Constify result.  Remove extraneous
	return.
	* bsd-kvm.c (bsd_kvm_target_open): Update.
	* gdbsupport/common-inferior.h (get_exec_file): Constify result.

gdb/gdbserver/ChangeLog
2019-12-13  Tom Tromey  <tromey@adacore.com>

	* server.c (get_exec_file): Constify result.

Change-Id: I29c60f7313a7def0dcb290ff0c2a4c1dea4f981f
---
 gdb/ChangeLog                    | 16 ++++++++++++++++
 gdb/bsd-kvm.c                    |  2 +-
 gdb/corefile.c                   |  3 +--
 gdb/darwin-nat.c                 |  2 +-
 gdb/gdbserver/ChangeLog          |  4 ++++
 gdb/gdbserver/server.c           |  2 +-
 gdb/gdbsupport/common-inferior.h |  2 +-
 gdb/gnu-nat.c                    |  5 ++---
 gdb/inf-ptrace.c                 |  3 +--
 gdb/nto-procfs.c                 |  5 ++---
 gdb/procfs.c                     |  3 +--
 gdb/remote.c                     |  2 +-
 gdb/windows-nat.c                |  2 +-
 13 files changed, 33 insertions(+), 18 deletions(-)
  

Comments

Terekhov, Mikhail via Gdb-patches Dec. 13, 2019, 10:13 p.m. UTC | #1
On Fri, Dec 13, 2019 at 5:11 PM Tom Tromey <tromey@adacore.com> wrote:
>
> I noticed that get_exec_file could return a "const char *".  This
> patch implements this change.
>
> I couldn't build all the code -- but I did build Linux native and a
> mingw cross.
>
> Consequently, the NTO code has a hack, where it casts away const.  I
> think this can be removed, but that required more work there, and
> since I couldn't compile it, I felt it best not to try.

Maybe use const_cast<char*> for those to make the intent clearer?

>
> Let me know what you think.
>
> gdb/ChangeLog
> 2019-12-13  Tom Tromey  <tromey@adacore.com>
>
>         * windows-nat.c (windows_nat_target::attach): Update.
>         * remote.c (extended_remote_target::attach): Update.
>         * procfs.c (procfs_target::attach): Update.
>         * nto-procfs.c (nto_procfs_target::attach): Update.
>         (nto_procfs_target::create_inferior): Update.
>         * inf-ptrace.c (inf_ptrace_target::attach): Update.
>         * gnu-nat.c (gnu_nat_target::attach): Update.
>         (gnu_nat_target::detach): Update.
>         * darwin-nat.c (darwin_nat_target::attach): Update.
>         * corefile.c (get_exec_file): Constify result.  Remove extraneous
>         return.
>         * bsd-kvm.c (bsd_kvm_target_open): Update.
>         * gdbsupport/common-inferior.h (get_exec_file): Constify result.
>
> gdb/gdbserver/ChangeLog
> 2019-12-13  Tom Tromey  <tromey@adacore.com>
>
>         * server.c (get_exec_file): Constify result.
>
> Change-Id: I29c60f7313a7def0dcb290ff0c2a4c1dea4f981f
> ---
>  gdb/ChangeLog                    | 16 ++++++++++++++++
>  gdb/bsd-kvm.c                    |  2 +-
>  gdb/corefile.c                   |  3 +--
>  gdb/darwin-nat.c                 |  2 +-
>  gdb/gdbserver/ChangeLog          |  4 ++++
>  gdb/gdbserver/server.c           |  2 +-
>  gdb/gdbsupport/common-inferior.h |  2 +-
>  gdb/gnu-nat.c                    |  5 ++---
>  gdb/inf-ptrace.c                 |  3 +--
>  gdb/nto-procfs.c                 |  5 ++---
>  gdb/procfs.c                     |  3 +--
>  gdb/remote.c                     |  2 +-
>  gdb/windows-nat.c                |  2 +-
>  13 files changed, 33 insertions(+), 18 deletions(-)
>
> diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
> index 21f978728da..56fae69174f 100644
> --- a/gdb/bsd-kvm.c
> +++ b/gdb/bsd-kvm.c
> @@ -106,7 +106,7 @@ static void
>  bsd_kvm_target_open (const char *arg, int from_tty)
>  {
>    char errbuf[_POSIX2_LINE_MAX];
> -  char *execfile = NULL;
> +  const char *execfile = NULL;
>    kvm_t *temp_kd;
>    char *filename = NULL;
>
> diff --git a/gdb/corefile.c b/gdb/corefile.c
> index f22c1540cb1..c4735ada00a 100644
> --- a/gdb/corefile.c
> +++ b/gdb/corefile.c
> @@ -141,7 +141,7 @@ validate_files (void)
>
>  /* See gdbsupport/common-inferior.h.  */
>
> -char *
> +const char *
>  get_exec_file (int err)
>  {
>    if (exec_filename)
> @@ -151,7 +151,6 @@ get_exec_file (int err)
>
>    error (_("No executable file specified.\n\
>  Use the \"file\" or \"exec-file\" command."));
> -  return NULL;
>  }
>
>
> diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
> index ecc7fc07f69..c3905d1a30e 100644
> --- a/gdb/darwin-nat.c
> +++ b/gdb/darwin-nat.c
> @@ -2045,7 +2045,7 @@ darwin_nat_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      char *exec_file = get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
> index c5f7176cff8..a425fbaa013 100644
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -265,7 +265,7 @@ get_exec_wrapper ()
>
>  /* See gdbsupport/common-inferior.h.  */
>
> -char *
> +const char *
>  get_exec_file (int err)
>  {
>    if (err && program_path.get () == NULL)
> diff --git a/gdb/gdbsupport/common-inferior.h b/gdb/gdbsupport/common-inferior.h
> index 77d4ad93d35..0ee554e7ad0 100644
> --- a/gdb/gdbsupport/common-inferior.h
> +++ b/gdb/gdbsupport/common-inferior.h
> @@ -28,7 +28,7 @@ extern const char *get_exec_wrapper ();
>  /* Return the name of the executable file as a string.
>     ERR nonzero means get error if there is none specified;
>     otherwise return 0 in that case.  */
> -extern char *get_exec_file (int err);
> +extern const char *get_exec_file (int err);
>
>  /* Return the inferior's current working directory.  If nothing has
>     been set, then return NULL.  */
> diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
> index 93a50becceb..19db53f3933 100644
> --- a/gdb/gnu-nat.c
> +++ b/gdb/gnu-nat.c
> @@ -2195,7 +2195,6 @@ void
>  gnu_nat_target::attach (const char *args, int from_tty)
>  {
>    int pid;
> -  char *exec_file;
>    struct inf *inf = cur_inf ();
>    struct inferior *inferior;
>
> @@ -2206,7 +2205,7 @@ gnu_nat_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      exec_file = (char *) get_exec_file (0);
> +      const char *exec_file = (char *) get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered ("Attaching to program `%s', pid %d\n",
> @@ -2260,7 +2259,7 @@ gnu_nat_target::detach (inferior *inf, int from_tty)
>
>    if (from_tty)
>      {
> -      char *exec_file = get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered ("Detaching from program `%s' pid %d\n",
> diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
> index 4a8e7323736..fd18146efe1 100644
> --- a/gdb/inf-ptrace.c
> +++ b/gdb/inf-ptrace.c
> @@ -186,7 +186,6 @@ inf_ptrace_target::mourn_inferior ()
>  void
>  inf_ptrace_target::attach (const char *args, int from_tty)
>  {
> -  char *exec_file;
>    pid_t pid;
>    struct inferior *inf;
>
> @@ -210,7 +209,7 @@ inf_ptrace_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      exec_file = get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
> diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
> index dcb0494e9c9..97ae68d6bf9 100644
> --- a/gdb/nto-procfs.c
> +++ b/gdb/nto-procfs.c
> @@ -693,7 +693,6 @@ nto_procfs_target::pid_to_exec_file (const int pid)
>  void
>  nto_procfs_target::attach (const char *args, int from_tty)
>  {
> -  char *exec_file;
>    int pid;
>    struct inferior *inf;
>
> @@ -704,7 +703,7 @@ nto_procfs_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      exec_file = (char *) get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
> @@ -1215,7 +1214,7 @@ nto_procfs_target::create_inferior (const char *exec_file,
>
>    argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
>                   sizeof (*argv));
> -  argv[0] = get_exec_file (1);
> +  argv[0] = (char *) get_exec_file (1);
>    if (!argv[0])
>      {
>        if (exec_file)
> diff --git a/gdb/procfs.c b/gdb/procfs.c
> index 57d4b8f0bfb..ccc5e26064b 100644
> --- a/gdb/procfs.c
> +++ b/gdb/procfs.c
> @@ -1870,7 +1870,6 @@ procfs_debug_inferior (procinfo *pi)
>  void
>  procfs_target::attach (const char *args, int from_tty)
>  {
> -  char *exec_file;
>    int   pid;
>
>    pid = parse_pid_to_attach (args);
> @@ -1880,7 +1879,7 @@ procfs_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      exec_file = get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_filtered (_("Attaching to program `%s', %s\n"),
> diff --git a/gdb/remote.c b/gdb/remote.c
> index e7a8bb61860..3c544502a85 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -5792,7 +5792,7 @@ extended_remote_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      char *exec_file = get_exec_file (0);
> +      const char *exec_file = get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
> diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
> index d77828291c5..1642043ca61 100644
> --- a/gdb/windows-nat.c
> +++ b/gdb/windows-nat.c
> @@ -1990,7 +1990,7 @@ windows_nat_target::attach (const char *args, int from_tty)
>
>    if (from_tty)
>      {
> -      char *exec_file = (char *) get_exec_file (0);
> +      const char *exec_file = (char *) get_exec_file (0);
>
>        if (exec_file)
>         printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
> --
> 2.21.0
>
  
Tom Tromey Dec. 16, 2019, 9:09 p.m. UTC | #2
>>>>> "Christian" == Christian Biesinger <cbiesinger@google.com> writes:

Christian> Maybe use const_cast<char*> for those to make the intent clearer?

Good idea.  I'm did this and I'm going to check it in.

I also found that I had forgotten to remove a couple of casts to "char *".
I've removed those as well.

Tom
  

Patch

diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
index 21f978728da..56fae69174f 100644
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -106,7 +106,7 @@  static void
 bsd_kvm_target_open (const char *arg, int from_tty)
 {
   char errbuf[_POSIX2_LINE_MAX];
-  char *execfile = NULL;
+  const char *execfile = NULL;
   kvm_t *temp_kd;
   char *filename = NULL;
 
diff --git a/gdb/corefile.c b/gdb/corefile.c
index f22c1540cb1..c4735ada00a 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -141,7 +141,7 @@  validate_files (void)
 
 /* See gdbsupport/common-inferior.h.  */
 
-char *
+const char *
 get_exec_file (int err)
 {
   if (exec_filename)
@@ -151,7 +151,6 @@  get_exec_file (int err)
 
   error (_("No executable file specified.\n\
 Use the \"file\" or \"exec-file\" command."));
-  return NULL;
 }
 
 
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index ecc7fc07f69..c3905d1a30e 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -2045,7 +2045,7 @@  darwin_nat_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      char *exec_file = get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index c5f7176cff8..a425fbaa013 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -265,7 +265,7 @@  get_exec_wrapper ()
 
 /* See gdbsupport/common-inferior.h.  */
 
-char *
+const char *
 get_exec_file (int err)
 {
   if (err && program_path.get () == NULL)
diff --git a/gdb/gdbsupport/common-inferior.h b/gdb/gdbsupport/common-inferior.h
index 77d4ad93d35..0ee554e7ad0 100644
--- a/gdb/gdbsupport/common-inferior.h
+++ b/gdb/gdbsupport/common-inferior.h
@@ -28,7 +28,7 @@  extern const char *get_exec_wrapper ();
 /* Return the name of the executable file as a string.
    ERR nonzero means get error if there is none specified;
    otherwise return 0 in that case.  */
-extern char *get_exec_file (int err);
+extern const char *get_exec_file (int err);
 
 /* Return the inferior's current working directory.  If nothing has
    been set, then return NULL.  */
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 93a50becceb..19db53f3933 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2195,7 +2195,6 @@  void
 gnu_nat_target::attach (const char *args, int from_tty)
 {
   int pid;
-  char *exec_file;
   struct inf *inf = cur_inf ();
   struct inferior *inferior;
 
@@ -2206,7 +2205,7 @@  gnu_nat_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      exec_file = (char *) get_exec_file (0);
+      const char *exec_file = (char *) get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered ("Attaching to program `%s', pid %d\n",
@@ -2260,7 +2259,7 @@  gnu_nat_target::detach (inferior *inf, int from_tty)
 
   if (from_tty)
     {
-      char *exec_file = get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered ("Detaching from program `%s' pid %d\n",
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index 4a8e7323736..fd18146efe1 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -186,7 +186,6 @@  inf_ptrace_target::mourn_inferior ()
 void
 inf_ptrace_target::attach (const char *args, int from_tty)
 {
-  char *exec_file;
   pid_t pid;
   struct inferior *inf;
 
@@ -210,7 +209,7 @@  inf_ptrace_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      exec_file = get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index dcb0494e9c9..97ae68d6bf9 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -693,7 +693,6 @@  nto_procfs_target::pid_to_exec_file (const int pid)
 void
 nto_procfs_target::attach (const char *args, int from_tty)
 {
-  char *exec_file;
   int pid;
   struct inferior *inf;
 
@@ -704,7 +703,7 @@  nto_procfs_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      exec_file = (char *) get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
@@ -1215,7 +1214,7 @@  nto_procfs_target::create_inferior (const char *exec_file,
 
   argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
 		  sizeof (*argv));
-  argv[0] = get_exec_file (1);
+  argv[0] = (char *) get_exec_file (1);
   if (!argv[0])
     {
       if (exec_file)
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 57d4b8f0bfb..ccc5e26064b 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -1870,7 +1870,6 @@  procfs_debug_inferior (procinfo *pi)
 void
 procfs_target::attach (const char *args, int from_tty)
 {
-  char *exec_file;
   int   pid;
 
   pid = parse_pid_to_attach (args);
@@ -1880,7 +1879,7 @@  procfs_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      exec_file = get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_filtered (_("Attaching to program `%s', %s\n"),
diff --git a/gdb/remote.c b/gdb/remote.c
index e7a8bb61860..3c544502a85 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5792,7 +5792,7 @@  extended_remote_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      char *exec_file = get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index d77828291c5..1642043ca61 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1990,7 +1990,7 @@  windows_nat_target::attach (const char *args, int from_tty)
 
   if (from_tty)
     {
-      char *exec_file = (char *) get_exec_file (0);
+      const char *exec_file = (char *) get_exec_file (0);
 
       if (exec_file)
 	printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,