@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
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 ());
}
}
--- /dev/null
+# 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\"\."
+}