[7/9] Update exec_file_attach to cope with "target:" filenames

Message ID 1426870087-32654-8-git-send-email-gbenson@redhat.com
State Superseded
Headers

Commit Message

Gary Benson March 20, 2015, 4:48 p.m. UTC
  This commit adds support for filenames prefixed	with "target:" to
exec_file_attach.  This is required to correctly follow inferior
exec* calls when a gdb_sysroot prefixed with "target:" is set.

gdb/ChangeLog:

	* exec.c (exec_file_attach): Support "target:" filenames.
---
 gdb/ChangeLog |    4 +++
 gdb/exec.c    |   71 +++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 56 insertions(+), 19 deletions(-)
  

Comments

Pedro Alves April 1, 2015, 12:14 p.m. UTC | #1
On 03/20/2015 04:48 PM, Gary Benson wrote:
> This commit adds support for filenames prefixed	with "target:" to
> exec_file_attach.  This is required to correctly follow inferior
> exec* calls when a gdb_sysroot prefixed with "target:" is set.

Hmm, I don't see how.  Isn't this only true when target_pid_to_exec_file
prepends the sysroot, which it doesn't yet?  I think this should move
to that other series.

A couple bits could use more explanation (in commit log and/or
comments):

 - Why is writing into executable files not supported with "target:" ?
 - The skipping of gdb_realpath_keepfile.

Thanks,
Pedro Alves

> 
> gdb/ChangeLog:
> 
> 	* exec.c (exec_file_attach): Support "target:" filenames.
> ---
>  gdb/ChangeLog |    4 +++
>  gdb/exec.c    |   71 +++++++++++++++++++++++++++++++++++++++++---------------
>  2 files changed, 56 insertions(+), 19 deletions(-)
> 
> diff --git a/gdb/exec.c b/gdb/exec.c
> index 124074f..113e869 100644
> --- a/gdb/exec.c
> +++ b/gdb/exec.c
> @@ -176,36 +176,66 @@ exec_file_attach (const char *filename, int from_tty)
>      }
>    else
>      {
> +      int load_via_target = 0;
>        char *scratch_pathname, *canonical_pathname;
>        int scratch_chan;
>        struct target_section *sections = NULL, *sections_end = NULL;
>        char **matching;
>  
> -      scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
> -		   write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
> -			    &scratch_pathname);
> -#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
> -      if (scratch_chan < 0)
> +      if (is_target_filename (filename))
> +	{
> +	  if (target_filesystem_is_local)
> +	    filename += strlen (TARGET_SYSROOT_PREFIX);
> +	  else
> +	    load_via_target = 1;
> +	}
> +
> +      if (load_via_target)
>  	{
> -	  char *exename = alloca (strlen (filename) + 5);
> +	  if (write_files)
> +	    warning (_("writing into executable files is "
> +		       "not supported for %s sysroots"),
> +		     TARGET_SYSROOT_PREFIX);
> +
> +	  scratch_pathname = xstrdup (filename);
> +	  make_cleanup (xfree, scratch_pathname);
>  
> -	  strcat (strcpy (exename, filename), ".exe");
> -	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
> -	     write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
> -	     &scratch_pathname);
> +	  scratch_chan = -1;
> +
> +	  canonical_pathname = scratch_pathname;
>  	}
> +      else
> +	{
> +
> +	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
> +				filename, write_files ?
> +				O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
> +				&scratch_pathname);
> +#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
> +	  if (scratch_chan < 0)
> +	    {
> +	      char *exename = alloca (strlen (filename) + 5);
> +
> +	      strcat (strcpy (exename, filename), ".exe");
> +	      scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
> +				    exename, write_files ?
> +				    O_RDWR | O_BINARY
> +				    : O_RDONLY | O_BINARY,
> +				    &scratch_pathname);
> +	    }
>  #endif
> -      if (scratch_chan < 0)
> -	perror_with_name (filename);
> +	  if (scratch_chan < 0)
> +	    perror_with_name (filename);
>  
> -      make_cleanup (xfree, scratch_pathname);
> +	  make_cleanup (xfree, scratch_pathname);
>  
> -      /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
> -	 better BFD caching.  */
> -      canonical_pathname = gdb_realpath (scratch_pathname);
> -      make_cleanup (xfree, canonical_pathname);
> +	  /* gdb_bfd_open (and its variants) prefers canonicalized
> +	     pathname for better BFD caching.  */
> +	  canonical_pathname = gdb_realpath (scratch_pathname);
> +	  make_cleanup (xfree, canonical_pathname);
> +	}
>  
> -      if (write_files)
> +      if (write_files && !load_via_target)
>  	exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
>  				  FOPEN_RUB, scratch_chan);
>        else
> @@ -218,7 +248,10 @@ exec_file_attach (const char *filename, int from_tty)
>  	}
>  
>        gdb_assert (exec_filename == NULL);
> -      exec_filename = gdb_realpath_keepfile (scratch_pathname);
> +      if (load_via_target)
> +	exec_filename = xstrdup (bfd_get_filename (exec_bfd));
> +      else
> +	exec_filename = gdb_realpath_keepfile (scratch_pathname);
>  
>        if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
>  	{
>
  

Patch

diff --git a/gdb/exec.c b/gdb/exec.c
index 124074f..113e869 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -176,36 +176,66 @@  exec_file_attach (const char *filename, int from_tty)
     }
   else
     {
+      int load_via_target = 0;
       char *scratch_pathname, *canonical_pathname;
       int scratch_chan;
       struct target_section *sections = NULL, *sections_end = NULL;
       char **matching;
 
-      scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
-		   write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
-			    &scratch_pathname);
-#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
-      if (scratch_chan < 0)
+      if (is_target_filename (filename))
+	{
+	  if (target_filesystem_is_local)
+	    filename += strlen (TARGET_SYSROOT_PREFIX);
+	  else
+	    load_via_target = 1;
+	}
+
+      if (load_via_target)
 	{
-	  char *exename = alloca (strlen (filename) + 5);
+	  if (write_files)
+	    warning (_("writing into executable files is "
+		       "not supported for %s sysroots"),
+		     TARGET_SYSROOT_PREFIX);
+
+	  scratch_pathname = xstrdup (filename);
+	  make_cleanup (xfree, scratch_pathname);
 
-	  strcat (strcpy (exename, filename), ".exe");
-	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
-	     write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
-	     &scratch_pathname);
+	  scratch_chan = -1;
+
+	  canonical_pathname = scratch_pathname;
 	}
+      else
+	{
+
+	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
+				filename, write_files ?
+				O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
+				&scratch_pathname);
+#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
+	  if (scratch_chan < 0)
+	    {
+	      char *exename = alloca (strlen (filename) + 5);
+
+	      strcat (strcpy (exename, filename), ".exe");
+	      scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
+				    exename, write_files ?
+				    O_RDWR | O_BINARY
+				    : O_RDONLY | O_BINARY,
+				    &scratch_pathname);
+	    }
 #endif
-      if (scratch_chan < 0)
-	perror_with_name (filename);
+	  if (scratch_chan < 0)
+	    perror_with_name (filename);
 
-      make_cleanup (xfree, scratch_pathname);
+	  make_cleanup (xfree, scratch_pathname);
 
-      /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
-	 better BFD caching.  */
-      canonical_pathname = gdb_realpath (scratch_pathname);
-      make_cleanup (xfree, canonical_pathname);
+	  /* gdb_bfd_open (and its variants) prefers canonicalized
+	     pathname for better BFD caching.  */
+	  canonical_pathname = gdb_realpath (scratch_pathname);
+	  make_cleanup (xfree, canonical_pathname);
+	}
 
-      if (write_files)
+      if (write_files && !load_via_target)
 	exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
 				  FOPEN_RUB, scratch_chan);
       else
@@ -218,7 +248,10 @@  exec_file_attach (const char *filename, int from_tty)
 	}
 
       gdb_assert (exec_filename == NULL);
-      exec_filename = gdb_realpath_keepfile (scratch_pathname);
+      if (load_via_target)
+	exec_filename = xstrdup (bfd_get_filename (exec_bfd));
+      else
+	exec_filename = gdb_realpath_keepfile (scratch_pathname);
 
       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
 	{