From d092c5a2465ece3435131ae6fef1ccb6e70986cb Mon Sep 17 00:00:00 2001 From: Sergio Durigan Junior Date: Mon, 11 Sep 2017 01:13:50 -0400 Subject: Implement "set cwd" command on GDB This commit adds new "set/show cwd" commands, which are used to set/show the current working directory of the inferior that will be started. The idea here is that "set cwd" will become the de facto way of setting the inferior's cwd. Currently, the user can use "cd" for that, but there are side effects: with "cd", GDB also switches to another directory, and that can impact the loading of scripts and other files. With "set cwd", we separate the logic into a new command. To maintain backward compatibility, if the user issues a "cd" command but doesn't use "set cwd", then the inferior's cwd will still be changed according to what the user specified. However, "set cwd" has precedence over "cd", so it can always be used to override it. "set cwd" works in the following way: - If the user sets the inferior's cwd by using "set cwd", then this directory is saved into current_inferior ()->cwd and is used when the inferior is started (see below). - If the user doesn't set the inferior's cwd by using "set cwd", but rather use the "cd" command as before, then this directory is inherited by the inferior because GDB will have chdir'd into it. On Unix-like hosts, the way the directory is changed before the inferior execution is by expanding the user set directory before the fork, and then "chdir" after the call to fork/vfork on "fork_inferior", but before the actual execution. On Windows, the inferior cwd set by the user is passed directly to the CreateProcess call, which takes care of the actual chdir for us. This way, we'll make sure that GDB's cwd is not affected by the user set cwd. gdb/ChangeLog: 2017-10-04 Sergio Durigan Junior * NEWS (New commands): Mention "set/show cwd". * cli/cli-cmds.c (_initialize_cli_cmds): Mention "set cwd" on "cd" command's help text. * common/common-inferior.h (get_inferior_cwd): New prototype. * infcmd.c (inferior_cwd_scratch): New global variable. (set_inferior_cwd): New function. (get_inferior_cwd): Likewise. (set_cwd_command): Likewise. (show_cwd_command): Likewise. (_initialize_infcmd): Add "set/show cwd" commands. * inferior.h (class inferior) : New field. * nat/fork-inferior.c: Include "gdb_tilde_expand.h". (fork_inferior): Change inferior's cwd before its execution. * windows-nat.c (windows_create_inferior): Pass inferior's cwd to CreateProcess. gdb/gdbserver/ChangeLog: 2017-10-04 Sergio Durigan Junior * inferiors.c (current_inferior_cwd): New global variable. (get_inferior_cwd): New function. * inferiors.h (struct process_info) : New field. gdb/doc/ChangeLog: 2017-10-04 Sergio Durigan Junior * gdb.texinfo (Starting your Program) : Mention new "set cwd" command. (Working Directory) : Rephrase to explain that "set cwd" exists and is the default way to change the inferior's cwd. gdb/testsuite/ChangeLog: 2017-10-04 Sergio Durigan Junior * gdb.base/set-cwd.c: New file. * gdb.base/set-cwd.exp: Likewise. --- gdb/nat/fork-inferior.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'gdb/nat') diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c index 6ff1197..ee1340b 100644 --- a/gdb/nat/fork-inferior.c +++ b/gdb/nat/fork-inferior.c @@ -25,6 +25,7 @@ #include "common-inferior.h" #include "common-gdbthread.h" #include "signals-state-save-restore.h" +#include "gdb_tilde_expand.h" #include extern char **environ; @@ -298,6 +299,8 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, char **save_our_env; int i; int save_errno; + const char *inferior_cwd; + std::string expanded_inferior_cwd; /* If no exec file handed to us, get it from the exec-file command -- with a good, common error message if none is specified. */ @@ -339,6 +342,18 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, the parent and child flushing the same data after the fork. */ gdb_flush_out_err (); + /* Check if the user wants to set a different working directory for + the inferior. */ + inferior_cwd = get_inferior_cwd (); + + if (inferior_cwd != NULL) + { + /* Expand before forking because between fork and exec, the child + process may only execute async-signal-safe operations. */ + expanded_inferior_cwd = gdb_tilde_expand (inferior_cwd); + inferior_cwd = expanded_inferior_cwd.c_str (); + } + /* If there's any initialization of the target layers that must happen to prepare to handle the child we're about fork, do it now... */ @@ -374,6 +389,14 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, UIs. */ close_most_fds (); + /* Change to the requested working directory if the user + requested it. */ + if (inferior_cwd != NULL) + { + if (chdir (inferior_cwd) < 0) + trace_start_error_with_name (inferior_cwd); + } + if (debug_fork) sleep (debug_fork); -- cgit v1.1