]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add tests for per-inferior settings
authorSébastien Darche <sdarche@efficios.com>
Mon, 29 Sep 2025 15:01:03 +0000 (11:01 -0400)
committerSébastien Darche <sdarche@efficios.com>
Wed, 8 Oct 2025 20:00:33 +0000 (16:00 -0400)
I am currently upstreaming some patches from ROCgdb that were made a few
years ago. This series cleans up how the CLI and MI access parameters
for multiple inferiors.

A preceding patch (702991711a91 ("gdb: Have setter and getter callbacks
for settings")) allowed for per-inferior getter/setter functions of some
inferior-specific settings. Over the years a few changes were accepted
upstream (cc09d372f664 ("gdb: make set/show args work with
$_gdb_setting_str")) that implemented those capabilities.

This patch adds a few tests to verify that those settings are properly
set and accessed from the CLI, MI as well as the python interface.

Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I7a5eab210c84d6a6782a32125f68bde34d9a2339
Approved-By: Andrew Burgess <aburgess@redhat.com>
gdb/testsuite/gdb.base/setshow.exp
gdb/testsuite/gdb.base/with.exp
gdb/testsuite/gdb.mi/set-show.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-parameter.exp

index de62692cd2ae7850c5545b6dc06b460a71df889a..2d5b62d477645a683fe3cab41366078e97ecba42 100644 (file)
@@ -515,6 +515,70 @@ proc_with_prefix test_argument_preceded_by_space {} {
     }
 }
 
+# Some settings are per-inferior instead of global.  Verify that we can set
+# and show them correctly, including with the $_gdb_setting and
+# $_gdb_setting_str convenience functions.
+
+proc_with_prefix test_per_inferior_settings {} {
+    clean_restart $::testfile
+
+    # The $_gdb_setting/$_gdb_setting_str tests require running inferiors,
+    # because they allocate memory in the inferiors for the produced values.
+    # Since we need two inferiors for this test, we can't run them with stub
+    # boards (e.g. gdbserver with non-extended remote protocol), since they
+    # can only run one inferior at a time.  We can still run the other tests
+    # with multiple inferiors, they just won't be running inferiors.
+    set run [expr {![use_gdb_stub]}]
+
+    # Add a second inferior.
+    gdb_test "add-inferior -exec $::binfile" "Added inferior 2.*" \
+       "add second inferior"
+
+    # Set some settings on each inferior.
+    foreach_with_prefix inf {1 2} {
+       gdb_test "inferior ${inf}" "Switching to inferior ${inf}.*" \
+           "switch to inferior ${inf} before set"
+
+       if { $run } {
+           if {![runto_main]} {
+               fail "could not run to main"
+               return
+           }
+       }
+
+       gdb_test_no_output "set inferior-tty /inf${inf}-tty"
+       gdb_test_no_output "set cwd /inf${inf}-cwd"
+       gdb_test_no_output "set args /inf${inf}-args"
+       gdb_test_no_output "set remote exec-file /inf${inf}-ref"
+       # Outputs a warning, ignore it.
+       gdb_test "set tdesc filename /inf${inf}-tf"
+    }
+
+    foreach_with_prefix inf {1 2} {
+       gdb_test "inferior ${inf}" "Switching to inferior ${inf}.*" \
+           "switch to inferior ${inf} before show"
+
+       gdb_test "show inferior-tty" "Terminal for future runs of program being debugged is \"/inf${inf}-tty\"."
+       gdb_test "show cwd" "Current working directory that will be used when starting the inferior is \"/inf${inf}-cwd\"."
+       gdb_test "show args" "Argument list to give program being debugged when it is started is \"/inf${inf}-args\"."
+       gdb_test "show remote exec-file" "The remote exec-file is \"/inf${inf}-ref\"."
+       gdb_test "show tdesc filename" "The target description will be read from \"/inf${inf}-tf\"."
+
+       if { $run } {
+           gdb_test "print \$_gdb_setting(\"inferior-tty\")" " = \"/inf${inf}-tty\""
+           gdb_test "print \$_gdb_setting_str(\"inferior-tty\")" " = \"/inf${inf}-tty\""
+           gdb_test "print \$_gdb_setting(\"cwd\")" " = \"/inf${inf}-cwd\""
+           gdb_test "print \$_gdb_setting_str(\"cwd\")" " = \"/inf${inf}-cwd\""
+           gdb_test "print \$_gdb_setting(\"args\")" " = \"/inf${inf}-args\""
+           gdb_test "print \$_gdb_setting_str(\"args\")" " = \"/inf${inf}-args\""
+           gdb_test "print \$_gdb_setting(\"remote exec-file\")" " = \"/inf${inf}-ref\""
+           gdb_test "print \$_gdb_setting_str(\"remote exec-file\")" " = \"/inf${inf}-ref\""
+           gdb_test "print \$_gdb_setting(\"tdesc filename\")" " = \"/inf${inf}-tf\""
+           gdb_test "print \$_gdb_setting_str(\"tdesc filename\")" " = \"/inf${inf}-tf\""
+       }
+    }
+}
+
 test_setshow_annotate
 test_setshow_args
 test_setshow_check
@@ -535,3 +599,4 @@ test_setshow_write
 test_show_user
 test_setshow_verbose
 test_argument_preceded_by_space
+test_per_inferior_settings
index eb1c85a5160e0f49e1b7a67d3266ad02f3889ca5..cb69e331afe742294e14e50445835cdca5318984 100644 (file)
@@ -223,6 +223,49 @@ with_test_prefix "run control" {
        "return 0;.*=>.*"
 }
 
+# Some parameters are not global, but per-inferior.  Make sure we save
+# and restore the correct value, based on the current inferior.  If a
+# per-inferior parameter does not provide a getter for the value
+# (depending on the current inferior), the `with` environment will not
+# save (and later restore) the proper value if the last modified
+# inferior is not the current one.
+with_test_prefix "per-inferior parameters" {
+    clean_restart
+
+    # Add a second inferior.
+    gdb_test "add-inferior" "Added inferior 2"
+
+    # Set some parameters on each inferior.
+    foreach_with_prefix inf {1 2} {
+       gdb_test "inferior ${inf}" "Switching to inferior ${inf}.*" \
+           "switch to inferior ${inf} before set"
+       gdb_test_no_output "set inferior-tty /inf${inf}-tty"
+       gdb_test_no_output "set cwd /inf${inf}-cwd"
+       gdb_test_no_output "set args /inf${inf}-args"
+       gdb_test_no_output "set remote exec-file /inf${inf}-ref"
+       # Outputs a warning, ignore it.
+       gdb_test "set tdesc filename /inf${inf}-tf"
+    }
+
+    gdb_test "inferior 1" "Switching to inferior 1.*" "switch back to inferior 1"
+
+    gdb_test "with inferior-tty tmp-value -- print 1" " = 1"
+    gdb_test "show inferior-tty" "/inf1-tty.*"
+
+    gdb_test "with cwd tmp-value -- print 1" " = 1"
+    gdb_test "show cwd" "/inf1-cwd.*"
+
+    gdb_test "with args tmp-value -- print 1" " = 1"
+    gdb_test "show args" "/inf1-args.*"
+
+    gdb_test "with remote exec-file tmp-value -- print 1" " = 1"
+    gdb_test "show remote exec-file" "/inf1-ref.*"
+
+    # Again, this prints some warnings around the " = 1", ignore them.
+    gdb_test "with tdesc filename tmp-value -- print 1" " = 1.*"
+    gdb_test "show tdesc filename" "/inf1-tf.*"
+}
+
 # Check errors.
 with_test_prefix "errors" {
     gdb_test "with" "Missing arguments\\."
diff --git a/gdb/testsuite/gdb.mi/set-show.exp b/gdb/testsuite/gdb.mi/set-show.exp
new file mode 100644 (file)
index 0000000..0c28a5f
--- /dev/null
@@ -0,0 +1,51 @@
+# Copyright 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/>.
+
+# Test the -gdb-set and -gdb-show commands.
+
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+standard_testfile
+
+# Some parameters are per inferior.  The value returned by -gdb-show for them
+# should be dependent on which is the current inferior.
+proc_with_prefix test_per_inferior_parameters { } {
+    mi_clean_restart
+
+    # Add a second inferior right away.
+    mi_gdb_test "-add-inferior" ".*\\^done,inferior=\"i2\"" "add inferior"
+
+    # Set distinct values on each inferior.
+    foreach_with_prefix inf {1 2} {
+       mi_gdb_test "-gdb-set --thread-group i${inf} inferior-tty /inf${inf}-tty" "\\^done" "set inferior-tty"
+       mi_gdb_test "-gdb-set --thread-group i${inf} cwd /inf${inf}-cwd" "\\^done" "set cwd"
+       mi_gdb_test "-gdb-set --thread-group i${inf} args /inf${inf}-args" "\\^done" "set args"
+       mi_gdb_test "-gdb-set --thread-group i${inf} remote exec-file /inf${inf}-ref" "\\^done" "set remote exec-file"
+       # Outputs a warning, ignore it.
+       mi_gdb_test "-gdb-set --thread-group i${inf} tdesc filename /inf${inf}-tf" ".*\\^done" "set tdesc filename"
+    }
+
+    # Check values on each inferior.
+    foreach_with_prefix inf {1 2} {
+       mi_gdb_test "-gdb-show --thread-group i${inf} inferior-tty" "\\^done,value=\"/inf${inf}-tty\"" "show inferior-tty"
+       mi_gdb_test "-gdb-show --thread-group i${inf} cwd" "\\^done,value=\"/inf${inf}-cwd\"" "show cwd"
+       mi_gdb_test "-gdb-show --thread-group i${inf} args" "\\^done,value=\"/inf${inf}-args\"" "show args"
+       mi_gdb_test "-gdb-show --thread-group i${inf} remote exec-file" "\\^done,value=\"/inf${inf}-ref\"" "show remote exec-file"
+       mi_gdb_test "-gdb-show --thread-group i${inf} tdesc filename" "\\^done,value=\"/inf${inf}-tf\"" "show tdesc filename"
+    }
+}
+
+test_per_inferior_parameters
index 1d7f22b7b800f3e9d8e820b012f01f9d669d9fbe..a80fa19e5842385cc8a8202f28cb85bf17592283 100644 (file)
@@ -718,6 +718,38 @@ proc_with_prefix test_throwing_parameter { } {
        "gdb.GdbError does not show Python stack"
 }
 
+# Some parameters are per inferior.  The value returned by gdb.parameter for
+# them should be dependent on which is the current inferior.
+proc_with_prefix test_per_inferior_parameters { } {
+    clean_restart
+
+    # Add a second inferior.
+    gdb_test "add-inferior" "Added inferior 2"
+
+    # Set some parameters on each inferior.
+    foreach_with_prefix inf {1 2} {
+       gdb_test "inferior ${inf}" "Switching to inferior ${inf}.*" \
+           "switch to inferior ${inf} before show"
+       gdb_test_no_output "set inferior-tty /inf${inf}-tty"
+       gdb_test_no_output "set cwd /inf${inf}-cwd"
+       gdb_test_no_output "set args /inf${inf}-args"
+       gdb_test_no_output "set remote exec-file /inf${inf}-ref"
+       # Outputs a warning, ignore it.
+       gdb_test "set tdesc filename /inf${inf}-tf"
+    }
+
+    # Check the values on each inferior.
+    foreach_with_prefix inf {1 2} {
+       gdb_test "inferior ${inf}" "Switching to inferior ${inf}.*" \
+           "switch to inferior ${inf} before set"
+       gdb_test "python print(gdb.parameter('inferior-tty'))" "/inf${inf}-tty"
+       gdb_test "python print(gdb.parameter('cwd'))" "/inf${inf}-cwd"
+       gdb_test "python print(gdb.parameter('args'))" "/inf${inf}-args"
+       gdb_test "python print(gdb.parameter('remote exec-file'))" "/inf${inf}-ref"
+       gdb_test "python print(gdb.parameter('tdesc filename'))" "/inf${inf}-tf"
+    }
+}
+
 proc_with_prefix test_language {} {
     gdb_test "python print(gdb.parameter('language'))" "auto" \
        "print language parameter"
@@ -874,6 +906,7 @@ test_deprecated_api_parameter
 test_gdb_parameter
 test_integer_parameter
 test_throwing_parameter
+test_per_inferior_parameters
 test_language
 test_ambiguous_parameter
 test_unknown_prefix