[3/5] gdb: make set/show cwd work with $_gdb_setting_str

Message ID db14b6ac1bc28968fdfce4432a875d3ca275bddb.1680608960.git.aburgess@redhat.com
State New
Headers
Series Fixes for per-inferior settings and $_gdb_setting_str() |

Commit Message

Andrew Burgess April 4, 2023, 12:45 p.m. UTC
  The previous commit fixed set/show args when used with
$_gdb_setting_str, this commit fixes set/show cwd.

Instead of using a scratch variable which is then pushed into the
current inferior from a set callback, move to the API that allows for
getters and setters, and store the value directly within the current
inferior.

Update the existing test to check the cwd setting.
---
 gdb/infcmd.c                             | 15 +++++----------
 gdb/testsuite/gdb.multi/gdb-settings.exp | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 10 deletions(-)
  

Patch

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 2f0d4f38c85..2e26ae9b934 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -66,10 +66,6 @@  static void step_1 (int, int, const char *);
 #define ERROR_NO_INFERIOR \
    if (!target_has_execution ()) error (_("The program is not being run."));
 
-/* Scratch area where the new cwd will be stored by 'set cwd'.  */
-
-static std::string inferior_cwd_scratch;
-
 /* Scratch area where 'set inferior-tty' will store user-provided value.
    We'll immediate copy it into per-inferior storage.  */
 
@@ -165,12 +161,12 @@  get_inferior_cwd ()
   return current_inferior ()->cwd ();
 }
 
-/* Handle the 'set cwd' command.  */
+/* Store the new value passed to 'set cwd'.  */
 
 static void
-set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c)
+set_cwd_value (const std::string &args)
 {
-  current_inferior ()->set_cwd (inferior_cwd_scratch);
+  current_inferior ()->set_cwd (args);
 }
 
 /* Handle the 'show cwd' command.  */
@@ -3164,8 +3160,7 @@  Follow this command with any number of args, to be passed to the program."),
   set_cmd_completer (args_set_show.set, filename_completer);
 
   auto cwd_set_show =
-    add_setshow_string_noescape_cmd ("cwd", class_run,
-				     &inferior_cwd_scratch, _("\
+    add_setshow_string_noescape_cmd ("cwd", class_run, _("\
 Set the current working directory to be used when the inferior is started.\n\
 Changing this setting does not have any effect on inferiors that are\n\
 already running."),
@@ -3175,7 +3170,7 @@  Show the current working directory that is used when the inferior is started."),
 Use this command to change the current working directory that will be used\n\
 when the inferior is started.  This setting does not affect GDB's current\n\
 working directory."),
-				     set_cwd_command,
+				     set_cwd_value, get_inferior_cwd,
 				     show_cwd_command,
 				     &setlist, &showlist);
   set_cmd_completer (cwd_set_show.set, filename_completer);
diff --git a/gdb/testsuite/gdb.multi/gdb-settings.exp b/gdb/testsuite/gdb.multi/gdb-settings.exp
index d2741d16157..70e0752a5bb 100644
--- a/gdb/testsuite/gdb.multi/gdb-settings.exp
+++ b/gdb/testsuite/gdb.multi/gdb-settings.exp
@@ -33,6 +33,10 @@  proc inferior_args {num} {
     return "a_${num}_1 a_${num}_2 a_${num}_3"
 }
 
+proc inferior_cwd {num} {
+    return [standard_output_file inf_${num}_dir]
+}
+
 # Start inferior NUM.
 
 proc start_inferior {num} {
@@ -41,6 +45,9 @@  proc start_inferior {num} {
 
 	set args [inferior_args ${num}]
 
+	set dir [inferior_cwd ${num}]
+	remote_exec host "mkdir -p $dir"
+
 	if {$num != 1} {
 	    gdb_test "add-inferior" "Added inferior $num.*" \
 		"add empty inferior"
@@ -50,10 +57,17 @@  proc start_inferior {num} {
 	    gdb_test_no_output "set args $args"
 	}
 
+	gdb_test_no_output "set cwd $dir" \
+	    "set cwd for inferior ${num}"
+
 	gdb_test {print $_gdb_setting_str("args")} \
 	    " = \"$args\"" \
 	    "check args before loading a file"
 
+	gdb_test {print $_gdb_setting_str("cwd")} \
+	    " = \"$dir\"" \
+	    "check cwd before loading a file"
+
 	gdb_load $binfile
 
 	if {[gdb_start_cmd] < 0} {
@@ -65,6 +79,10 @@  proc start_inferior {num} {
 	gdb_test {print $_gdb_setting_str("args")} \
 	    " = \"$args\"" \
 	    "check after after starting"
+
+	gdb_test {print $_gdb_setting_str("cwd")} \
+	    " = \"$dir\"" \
+	    "check cwd after starting"
     }
 
     return 0
@@ -95,4 +113,9 @@  for {set i 1} {$i <= $NUM_INFS} {incr i} {
     gdb_test {print $_gdb_setting_str("args")} \
 	" = \"$args\"" \
 	"check args after after switching to inferior $i"
+
+    set dir [inferior_cwd $i]
+    gdb_test {print $_gdb_setting_str("cwd")} \
+	" = \"$dir\"" \
+	"check cwd after after switching to inferior $i"
 }