Extend "set cwd" to work on gdbserver
authorSergio Durigan Junior <sergiodj@redhat.com>
Wed, 20 Sep 2017 23:15:40 +0000 (19:15 -0400)
committerSergio Durigan Junior <sergiodj@redhat.com>
Wed, 4 Oct 2017 06:01:45 +0000 (02:01 -0400)
This is the "natural" extension necessary for the "set cwd" command
(and the whole "set the inferior's cwd" logic) to work on gdbserver.

The idea here is to have a new remote packet, QSetWorkingDir (name
adopted from LLDB's extension to the RSP, as can be seen at
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>),
which sends an hex-encoded string representing the working directory
that the remote inferior will use.  There is a slight difference from
the packet proposed by LLDB: GDB's version will accept empty
arguments, meaning that the user wants to clear the previously set
working directory for the inferior (i.e., "set cwd" without arguments
on GDB).

For UNIX-like targets this feature is already implemented on
nat/fork-inferior.c, and all gdbserver has to do is to basically
implement "set_inferior_cwd" and call it whenever such packet arrives.
For other targets, like Windows, it is possible to use the existing
"get_inferior_cwd" function and do the necessary steps to make sure
that the inferior will use the specified working directory.

Aside from that, the patch consists basically of updates to the
testcase (making it available on remote targets) and the
documentation.

No regressions found.

gdb/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

* NEWS (Changes since GDB 8.0): Add entry about new
'set-cwd-on-gdbserver' feature.
(New remote packets): Add entry for QSetWorkingDir.
* common/common-inferior.h (set_inferior_cwd): New prototype.
* infcmd.c (set_inferior_cwd): Remove "static".
(show_cwd_command): Expand text to include remote debugging.
* remote.c: Add PACKET_QSetWorkingDir.
(remote_protocol_features) <QSetWorkingDir>: New entry for
PACKET_QSetWorkingDir.
(extended_remote_set_inferior_cwd): New function.
(extended_remote_create_inferior): Call
"extended_remote_set_inferior_cwd".
(_initialize_remote): Call "add_packet_config_cmd" for
QSetWorkingDir.

gdb/gdbserver/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

* inferiors.c (set_inferior_cwd): New function.
* server.c (handle_general_set): Handle QSetWorkingDir packet.
(handle_query): Inform that QSetWorkingDir is supported.
* win32-low.c (create_process): Pass the inferior's cwd to
CreateProcess.

gdb/testsuite/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

* gdb.base/set-cwd.exp: Make it available on
native-extended-gdbserver.

gdb/doc/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

* gdb.texinfo (Starting your Program) <The working directory.>:
Mention remote debugging.
(Working Directory) <Your Program's Working Directory>:
Likewise.
(Connecting) <Remote Packet>: Add "set-working-dir"
and "QSetWorkingDir" to the table.
(Remote Protocol) <QSetWorkingDir>: New item, explaining the
packet.

13 files changed:
gdb/ChangeLog
gdb/NEWS
gdb/common/common-inferior.h
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/gdbserver/ChangeLog
gdb/gdbserver/inferiors.c
gdb/gdbserver/server.c
gdb/gdbserver/win32-low.c
gdb/infcmd.c
gdb/remote.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/set-cwd.exp

index ea4fc621b02b0c8bc89ffbdcd622eb6a4409b5b8..e113e2ff6643d0e7a7a5b907eb96a83bb95c48f9 100644 (file)
@@ -1,3 +1,20 @@
+2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * NEWS (Changes since GDB 8.0): Add entry about new
+       'set-cwd-on-gdbserver' feature.
+       (New remote packets): Add entry for QSetWorkingDir.
+       * common/common-inferior.h (set_inferior_cwd): New prototype.
+       * infcmd.c (set_inferior_cwd): Remove "static".
+       (show_cwd_command): Expand text to include remote debugging.
+       * remote.c: Add PACKET_QSetWorkingDir.
+       (remote_protocol_features) <QSetWorkingDir>: New entry for
+       PACKET_QSetWorkingDir.
+       (extended_remote_set_inferior_cwd): New function.
+       (extended_remote_create_inferior): Call
+       "extended_remote_set_inferior_cwd".
+       (_initialize_remote): Call "add_packet_config_cmd" for
+       QSetWorkingDir.
+
 2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * NEWS (New commands): Mention "set/show cwd".
index fcf454833fa954eb8e3e1b324e13ad153d25218c..2bad096a86e4f027533854f598f425abdafa2d7d 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
 
 * New features in the GDB remote stub, GDBserver
 
+  ** GDBserver is now able to start inferior processes with a
+     specified initial working directory.
+
+     The user can set the desired working directory to be used from
+     GDB using the new "set cwd" command.
+
   ** New "--selftest" command line option runs some GDBserver self
      tests.  These self tests are disabled in releases.
 
@@ -59,6 +65,10 @@ QEnvironmentReset
 QStartupWithShell
   Indicates whether the inferior must be started with a shell or not.
 
+QSetWorkingDir
+  Tell GDBserver that the inferior to be started should use a specific
+  working directory.
+
 * The "maintenance print c-tdesc" command now takes an optional
   argument which is the file name of XML target description.
 
index 515a8c0f4e06c5a22aa4d1bd227b420f4f75fd34..3dcfda3e7b558bcf38c769bba1e2a4fbe07621d5 100644 (file)
@@ -34,4 +34,8 @@ extern char *get_exec_file (int err);
    been set, then return NULL.  */
 extern const char *get_inferior_cwd ();
 
+/* Set the inferior current working directory.  If CWD is NULL, unset
+   the directory.  */
+extern void set_inferior_cwd (const char *cwd);
+
 #endif /* ! COMMON_INFERIOR_H */
index 4ded14ae2a05f0e349de83cfe346485960ca343d..e3ea4ec614e997baac565d5f30ace804f4597ac8 100644 (file)
@@ -1,3 +1,14 @@
+2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * gdb.texinfo (Starting your Program) <The working directory.>:
+       Mention remote debugging.
+       (Working Directory) <Your Program's Working Directory>:
+       Likewise.
+       (Connecting) <Remote Packet>: Add "set-working-dir"
+       and "QSetWorkingDir" to the table.
+       (Remote Protocol) <QSetWorkingDir>: New item, explaining the
+       packet.
+
 2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * gdb.texinfo (Starting your Program) <The working directory.>:
index a68107dddb058691307f2753371b4a8a658ff14b..bfeb7a9a35d1351195961274b775344ffdbb49e7 100644 (file)
@@ -2059,8 +2059,10 @@ your program.  @xref{Environment, ,Your Program's Environment}.
 @item The @emph{working directory.}
 You can set your program's working directory with the command
 @kbd{set cwd}.  If you do not set any working directory with this
-command, your program will inherit @value{GDBN}'s working directory.
-@xref{Working Directory, ,Your Program's Working Directory}.
+command, your program will inherit @value{GDBN}'s working directory if
+native debugging, or the remote server's working directory if remote
+debugging.  @xref{Working Directory, ,Your Program's Working
+Directory}.
 
 @item The @emph{standard input and output.}
 Your program normally uses the same device for standard input and
@@ -2439,7 +2441,9 @@ Each time you start your program with @code{run}, the inferior will be
 initialized with the current working directory specified by the
 @kbd{set cwd} command.  If no directory has been specified by this
 command, then the inferior will inherit @value{GDBN}'s current working
-directory as its working directory.
+directory as its working directory if native debugging, or it will
+inherit the remote server's current working directory if remote
+debugging.
 
 @table @code
 @kindex set cwd
@@ -21003,6 +21007,10 @@ are:
 @tab @code{QEnvironmentReset}
 @tab @code{Reset the inferior environment (i.e., unset user-set variables)}
 
+@item @code{set-working-dir}
+@tab @code{QSetWorkingDir}
+@tab @code{set cwd}
+
 @item @code{conditional-breakpoints-packet}
 @tab @code{Z0 and Z1}
 @tab @code{Support for target-side breakpoint condition evaluation}
@@ -36860,6 +36868,28 @@ by supplying an appropriate @samp{qSupported} response
 actually support passing environment variables to the starting
 inferior.
 
+@item QSetWorkingDir:@r{[}@var{directory}@r{]}
+@anchor{QSetWorkingDir packet}
+@cindex set working directory, remote request
+@cindex @samp{QSetWorkingDir} packet
+This packet is used to inform the remote server of the intended
+current working directory for programs that are going to be executed.
+
+The packet is composed by @var{directory}, an hex encoded
+representation of the directory that the remote inferior will use as
+its current working directory.  If @var{directory} is an empty string,
+the remote server should reset the inferior's current working
+directory to its original, empty value.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
 @item qfThreadInfo
 @itemx qsThreadInfo
 @cindex list active threads, remote request
index 25d527b924afb57717bea65bd30f775f7f88ada2..0bdeabaa9b1a54fa96ee6a3496238384a472784f 100644 (file)
@@ -1,3 +1,11 @@
+2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * inferiors.c (set_inferior_cwd): New function.
+       * server.c (handle_general_set): Handle QSetWorkingDir packet.
+       (handle_query): Inform that QSetWorkingDir is supported.
+       * win32-low.c (create_process): Pass the inferior's cwd to
+       CreateProcess.
+
 2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * inferiors.c (current_inferior_cwd): New global variable.
index 57d9956ebb22df080c8211e171b0035413d4c411..154c167f4ca98924a93c53a1a90384b265f8e7e2 100644 (file)
@@ -456,3 +456,15 @@ get_inferior_cwd ()
 {
   return current_inferior_cwd;
 }
+
+/* See common/common-inferior.h.  */
+
+void
+set_inferior_cwd (const char *cwd)
+{
+  xfree ((void *) current_inferior_cwd);
+  if (cwd != NULL)
+    current_inferior_cwd = xstrdup (cwd);
+  else
+    current_inferior_cwd = NULL;
+}
index c4f1e8dde63c3d75f727ea9e5ae9825ab9241c5a..a95973547ce1256726dde7f0bff91989f15deb12 100644 (file)
@@ -869,6 +869,35 @@ handle_general_set (char *own_buf)
       return;
     }
 
+  if (startswith (own_buf, "QSetWorkingDir:"))
+    {
+      const char *p = own_buf + strlen ("QSetWorkingDir:");
+
+      if (*p != '\0')
+       {
+         std::string path = hex2str (p);
+
+         set_inferior_cwd (path.c_str ());
+
+         if (remote_debug)
+           debug_printf (_("[Set the inferior's current directory to %s]\n"),
+                         path.c_str ());
+       }
+      else
+       {
+         /* An empty argument means that we should clear out any
+            previously set cwd for the inferior.  */
+         set_inferior_cwd (NULL);
+
+         if (remote_debug)
+           debug_printf (_("\
+[Unset the inferior's current directory; will use gdbserver's cwd]\n"));
+       }
+      write_ok (own_buf);
+
+      return;
+    }
+
   /* Otherwise we didn't know what packet it was.  Say we didn't
      understand it.  */
   own_buf[0] = 0;
@@ -2351,7 +2380,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
       sprintf (own_buf,
               "PacketSize=%x;QPassSignals+;QProgramSignals+;"
               "QStartupWithShell+;QEnvironmentHexEncoded+;"
-              "QEnvironmentReset+;QEnvironmentUnset+",
+              "QEnvironmentReset+;QEnvironmentUnset+;"
+              "QSetWorkingDir+",
               PBUFSIZ - 1);
 
       if (target_supports_catch_syscall ())
index cc84d15c2f5af5dd3d529b3ad21668fb7a0ff42f..c11926f7c6c187c0409d531e3725465e9f8ed726 100644 (file)
@@ -32,6 +32,7 @@
 #include <tlhelp32.h>
 #include <psapi.h>
 #include <process.h>
+#include "gdb_tilde_expand.h"
 
 #ifndef USE_WIN32API
 #include <sys/cygwin.h>
@@ -562,10 +563,12 @@ static BOOL
 create_process (const char *program, char *args,
                DWORD flags, PROCESS_INFORMATION *pi)
 {
+  const char *inferior_cwd = get_inferior_cwd ();
+  std::string expanded_infcwd = gdb_tilde_expand (inferior_cwd);
   BOOL ret;
 
 #ifdef _WIN32_WCE
-  wchar_t *p, *wprogram, *wargs;
+  wchar_t *p, *wprogram, *wargs, *wcwd = NULL;
   size_t argslen;
 
   wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
@@ -579,6 +582,19 @@ create_process (const char *program, char *args,
   wargs = alloca ((argslen + 1) * sizeof (wchar_t));
   mbstowcs (wargs, args, argslen + 1);
 
+  if (inferior_cwd != NULL)
+    {
+      std::replace (expanded_infcwd.begin (), expanded_infcwd.end (),
+                   '/', '\\');
+      wcwd = alloca ((expanded_infcwd.size () + 1) * sizeof (wchar_t));
+      if (mbstowcs (wcwd, expanded_infcwd.c_str (),
+                   expanded_infcwd.size () + 1) == NULL)
+       {
+         error (_("\
+Could not convert the expanded inferior cwd to wide-char."));
+       }
+    }
+
   ret = CreateProcessW (wprogram, /* image name */
                        wargs,    /* command line */
                        NULL,     /* security, not supported */
@@ -586,7 +602,7 @@ create_process (const char *program, char *args,
                        FALSE,    /* inherit handles, not supported */
                        flags,    /* start flags */
                        NULL,     /* environment, not supported */
-                       NULL,     /* current directory, not supported */
+                       wcwd,     /* current directory */
                        NULL,     /* start info, not supported */
                        pi);      /* proc info */
 #else
@@ -599,7 +615,7 @@ create_process (const char *program, char *args,
                        TRUE,     /* inherit handles */
                        flags,    /* start flags */
                        NULL,     /* environment */
-                       NULL,     /* current directory */
+                       expanded_infcwd.c_str (), /* current directory */
                        &si,      /* start info */
                        pi);      /* proc info */
 #endif
index dbdf273ca5cb556e815b80414b103ed3dbf2e665..187c71f34461ef781c5c6fa6f4a20bc674400af7 100644 (file)
@@ -242,10 +242,9 @@ show_args_command (struct ui_file *file, int from_tty,
   deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
 }
 
-/* Set the inferior current working directory.  If CWD is NULL, unset
-   the directory.  */
+/* See common/common-inferior.h.  */
 
-static void
+void
 set_inferior_cwd (const char *cwd)
 {
   struct inferior *inf = current_inferior ();
@@ -289,7 +288,8 @@ show_cwd_command (struct ui_file *file, int from_tty,
     fprintf_filtered (gdb_stdout,
                      _("\
 You have not set the inferior's current working directory.\n\
-The inferior will inherit GDB's cwd.\n"));
+The inferior will inherit GDB's cwd if native debugging, or the remote\n\
+server's cwd if remote debugging.\n"));
   else
     fprintf_filtered (gdb_stdout,
                      _("Current working directory that will be used "
index 54d810f054325e619bfd4f5528b870adb7c7913f..8c47d0662cd282d0eba90c86f19268a20d2f3f71 100644 (file)
@@ -1430,6 +1430,7 @@ enum {
   PACKET_QPassSignals,
   PACKET_QCatchSyscalls,
   PACKET_QProgramSignals,
+  PACKET_QSetWorkingDir,
   PACKET_QStartupWithShell,
   PACKET_QEnvironmentHexEncoded,
   PACKET_QEnvironmentReset,
@@ -4662,6 +4663,8 @@ static const struct protocol_feature remote_protocol_features[] = {
     PACKET_QCatchSyscalls },
   { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
     PACKET_QProgramSignals },
+  { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
+    PACKET_QSetWorkingDir },
   { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
     PACKET_QStartupWithShell },
   { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
@@ -9641,6 +9644,45 @@ extended_remote_environment_support (struct remote_state *rs)
       send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
 }
 
+/* Helper function to set the current working directory for the
+   inferior in the remote target.  */
+
+static void
+extended_remote_set_inferior_cwd (struct remote_state *rs)
+{
+  if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
+    {
+      const char *inferior_cwd = get_inferior_cwd ();
+
+      if (inferior_cwd != NULL)
+       {
+         std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
+                                        strlen (inferior_cwd));
+
+         xsnprintf (rs->buf, get_remote_packet_size (),
+                    "QSetWorkingDir:%s", hexpath.c_str ());
+       }
+      else
+       {
+         /* An empty inferior_cwd means that the user wants us to
+            reset the remote server's inferior's cwd.  */
+         xsnprintf (rs->buf, get_remote_packet_size (),
+                    "QSetWorkingDir:");
+       }
+
+      putpkt (rs->buf);
+      getpkt (&rs->buf, &rs->buf_size, 0);
+      if (packet_ok (rs->buf,
+                    &remote_protocol_packets[PACKET_QSetWorkingDir])
+         != PACKET_OK)
+       error (_("\
+Remote replied unexpectedly while setting the inferior's working\n\
+directory: %s"),
+              rs->buf);
+
+    }
+}
+
 /* In the extended protocol we want to be able to do things like
    "run" and have them basically work as expected.  So we need
    a special create_inferior function.  We support changing the
@@ -9683,6 +9725,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
 
   extended_remote_environment_support (rs);
 
+  extended_remote_set_inferior_cwd (rs);
+
   /* Now restart the remote server.  */
   run_worked = extended_remote_run (args) != -1;
   if (!run_worked)
@@ -14190,6 +14234,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
                         "QProgramSignals", "program-signals", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
+                        "QSetWorkingDir", "set-working-dir", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
                         "QStartupWithShell", "startup-with-shell", 0);
 
index b72788ba91c75fafbd62ce6ea448ed9d3ea181ae..2a580f4cb119c50642f5f4c7d127f10fd4ac46ef 100644 (file)
@@ -1,3 +1,8 @@
+2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * gdb.base/set-cwd.exp: Make it available on
+       native-extended-gdbserver.
+
 2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * gdb.base/set-cwd.c: New file.
index 0d536488e4b15c3a121a9943bf1fe72cbc64f980..6b81fbc0b58e6bde3f165cd6c10f2511a1152217 100644 (file)
@@ -15,8 +15,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
-    untested "not implemented on remote servers"
+if { [use_gdb_stub] } {
+    untested "skipping tests due to use_gdb_stub"
     return
 }
 
This page took 0.07474 seconds and 4 git commands to generate.