]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: Copy inferior properties in clone-inferior
authorLancelot SIX <lancelot.six@amd.com>
Mon, 6 Dec 2021 10:23:42 +0000 (05:23 -0500)
committerLancelot SIX <lancelot.six@amd.com>
Wed, 29 Dec 2021 08:27:38 +0000 (03:27 -0500)
This commit ensures that the following settings are cloned from one
inferior to the new one when processing the clone-inferior command:
  - inferior-tty
  - environment variables
  - cwd
  - args

Some of those parameters can be passed as command line arguments to GDB
(-args and -tty), so one could expect the clone-inferior to respect
those flags.  The following debugging session illustrates that:

    gdb -nx -quiet -batch \
         -ex "show args" \
         -ex "show inferior-tty" \
         -ex "clone-inferior" \
         -ex "inferior 2" \
         -ex "show args" \
         -ex "show inferior-tty" \
         -tty=/some/tty \
         -args echo foo bar
    Argument list to give program being debugged when it is started is "foo bar".
    Terminal for future runs of program being debugged is "/some/tty".
    [New inferior 2]
    Added inferior 2.
    [Switching to inferior 2 [<null>] (/bin/echo)]
    Argument list to give program being debugged when it is started is "".
    Terminal for future runs of program being debugged is "".

The other properties this commit copies on clone (i.e. CWD and the
environment variables) are included since they are related (in the sense
that they influence the runtime behavior of the program) even if they
cannot be directly set using command line switches.

There is a chance that this patch changes existing user workflow.  I
think that this change is mostly harmless.  If users want to start a new
inferior based on an existing one, they probably already propagate those
settings to the new inferior in some way.

Tested on x86_64-linux.

Change-Id: I3b1f28b662f246228b37bb24c2ea1481567b363d

gdb/NEWS
gdb/doc/gdb.texinfo
gdb/inferior.c
gdb/testsuite/gdb.base/inferior-clone.exp [new file with mode: 0644]

index 1e4afa91bc5931446e7d4bc45c45b485b0884749..c26e15b530a0c64c436fe8d0b506b1f0da44bfe5 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -72,6 +72,13 @@ maint packet
   as escaped hex, e.g. \x?? where '??' is replaces with the value of
   the non-printable character.
 
+clone-inferior
+  The clone-inferior command now ensures that the TTY, CMD and ARGS
+  settings are copied from the original inferior to the new one.
+  All modifications to the environment variables done using the 'set
+  environment' or 'unset environment' commands are also copied to the new
+  inferior.
+
 * Python API
 
   ** New function gdb.add_history(), which takes a gdb.Value object
index 7da6805f804307398d872d1bd9f1123545fca75f..77cd184c47bf7ae989d5bc71824f7a8a7b3e8fa8 100644 (file)
@@ -3331,8 +3331,12 @@ use @code{run} to spawn a local program, etc.
 @item clone-inferior [ -copies @var{n} ] [ @var{infno} ]
 Adds @var{n} inferiors ready to execute the same program as inferior
 @var{infno}; @var{n} defaults to 1, and @var{infno} defaults to the
-number of the current inferior.  This is a convenient command when you
-want to run another instance of the inferior you are debugging.
+number of the current inferior.  This command copies the values of the
+@var{args}, @w{@var{inferior-tty}} and @var{cwd} properties from the
+current inferior to the new one.  It also propagates changes the user
+made to environment variables using the @w{@code{set environment}} and
+@w{@code{unset environment}} commands.  This is a convenient command
+when you want to run another instance of the inferior you are debugging.
 
 @smallexample
 (@value{GDBP}) info inferiors
index 72b7c6181ad14fdbb2a35b4026cb4b84164a454f..52aa6fb955c138b3bd27acc98c1dea7fab971e2a 100644 (file)
@@ -934,6 +934,23 @@ clone_inferior_command (const char *args, int from_tty)
        copy_inferior_target_desc_info (inf, orginf);
 
       clone_program_space (pspace, orginf->pspace);
+
+      /* Copy properties from the original inferior to the new one.  */
+      inf->set_args (orginf->args ());
+      inf->set_cwd (orginf->cwd ());
+      inf->set_tty (orginf->tty ());
+      for (const std::string &set_var : orginf->environment.user_set_env ())
+       {
+         /* set_var has the form NAME=value.  Split on the first '='.  */
+         const std::string::size_type pos = set_var.find ('=');
+         gdb_assert (pos != std::string::npos);
+         const std::string varname = set_var.substr (0, pos);
+         inf->environment.set
+           (varname.c_str (), orginf->environment.get (varname.c_str ()));
+       }
+      for (const std::string &unset_var
+          : orginf->environment.user_unset_env ())
+       inf->environment.unset (unset_var.c_str ());
     }
 }
 
diff --git a/gdb/testsuite/gdb.base/inferior-clone.exp b/gdb/testsuite/gdb.base/inferior-clone.exp
new file mode 100644 (file)
index 0000000..bb43be6
--- /dev/null
@@ -0,0 +1,93 @@
+# Copyright (C) 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This testcase checks that the clone-inferior command copies important
+# properties from the original to the new inferior such CWD, args and env.
+
+# Add an environment variable that can be modified later for each inferior
+# in GDB.
+set env(ENVVAR) var
+
+# This test does not need a binfile.
+clean_restart
+
+# Set custom properties for Inferior 1
+with_test_prefix "setup inferior 1" {
+    gdb_test_no_output "set args foo bar 'b a z'"
+    gdb_test_no_output "set cwd /any/where"
+    gdb_test_no_output "set environment FOO foo"
+    gdb_test_no_output "set environment BAR bar"
+    gdb_test_no_output "set inferior-tty some_tty"
+}
+
+# Check that properties of inferior 1 have been copied
+with_test_prefix "inferior 2" {
+    gdb_test "clone-inferior" "Added inferior 2"
+    gdb_test "inferior 2" "\[Switching to inferior 2 \[<null>\] (.*)\]" "change inferior"
+    gdb_test "show args" \
+       "Argument list to give program being debugged when it is started is \"foo bar 'b a z'\"\."
+    gdb_test "show cwd" \
+       "Current working directory that will be used when starting the inferior is \"/any/where\"\."
+    gdb_test "show environment FOO" "FOO = foo"
+    gdb_test "show environment BAR" "BAR = bar"
+    gdb_test "show environment ENVVAR" "ENVVAR = var"
+    gdb_test "show inferior-tty" \
+       "Terminal for future runs of program being debugged is \"some_tty\"\."
+}
+
+# Change this second inferior, to check that the next clone-inferior
+# clones from the active inferior, and changing the values for inferior 2
+# does not interfere with values on inferior 1.
+with_test_prefix "update inferior 2" {
+    gdb_test_no_output "set args foo"
+    gdb_test_no_output "set cwd /somewhere/else"
+    gdb_test_no_output "set environment FOO oof"
+}
+
+with_test_prefix "inferior 1" {
+    gdb_test "inferior 1" "\[Switching to inferior 1 \[<null>\] (.*)\]" "change inferior"
+    gdb_test "show args" \
+       "Argument list to give program being debugged when it is started is \"foo bar 'b a z'\"\."
+    gdb_test "show cwd" \
+       "Current working directory that will be used when starting the inferior is \"/any/where\"\."
+    gdb_test "show environment FOO" "FOO = foo"
+    gdb_test "show environment BAR" "BAR = bar"
+    gdb_test "show environment ENVVAR" "ENVVAR = var"
+    gdb_test "show inferior-tty" \
+       "Terminal for future runs of program being debugged is \"some_tty\"\."
+}
+
+# Tweak inferior 1 a bit more.
+with_test_prefix "update inferior 1" {
+    gdb_test_no_output "unset environment FOO"
+    gdb_test_no_output "unset environment ENVVAR"
+}
+
+
+# Check that the values observed on inferior 3 are those copied from
+# inferior 1.
+with_test_prefix "inferior 3" {
+    gdb_test "clone-inferior" "Added inferior 3"
+    gdb_test "inferior 3" "\[Switching to inferior 3 \[<null>\] (.*)\]" "change inferior"
+    gdb_test "show args" \
+       "Argument list to give program being debugged when it is started is \"foo bar 'b a z'\"\."
+    gdb_test "show cwd" \
+       "Current working directory that will be used when starting the inferior is \"/any/where\"\."
+    gdb_test "show environment FOO" "Environment variable \"FOO\" not defined\."
+    gdb_test "show environment BAR" "BAR = bar"
+    gdb_test "show environment ENVVAR" "Environment variable \"ENVVAR\" not defined\."
+    gdb_test "show inferior-tty" \
+       "Terminal for future runs of program being debugged is \"some_tty\"\."
+}