From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id EE2063858416 for ; Tue, 4 Apr 2023 12:45:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE2063858416 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680612343; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+1ObYtE8Hf6MeKWMaG1NjTci7chwrss7t4Qz67NvmvI=; b=HC+XuEy1IaS6bBqkHcyniEGcH7GapEysiBgdeuwc/XQHV4v4wGZccBDXqG1Bomd1EQdHA2 TsjgKMLjWxzls0juxRcNudvSpdrMhhijkXONPLUO1L2RtSryNcU29Hv5YCaa2q8/KQSAec YDfZcpfFMVBrjQIZ/y3ki3NE1+uzJ4Y= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-628-GVtEBjLmMqK1Gn2zW6t2Cw-1; Tue, 04 Apr 2023 08:45:42 -0400 X-MC-Unique: GVtEBjLmMqK1Gn2zW6t2Cw-1 Received: by mail-qk1-f199.google.com with SMTP id p63-20020a374242000000b007468eaf866aso5153915qka.17 for ; Tue, 04 Apr 2023 05:45:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680612342; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+1ObYtE8Hf6MeKWMaG1NjTci7chwrss7t4Qz67NvmvI=; b=2lGtFMAXuAOqxAoro8aQ2WbLbpSLvJoZVSOt0l5AFTl4ICMEwu8NEIZNzuXrcUX1KK CFU/KT1e7m8SG2QGDRfi4NH+dqH2jLWkm63U87cWNt0OpBMxtY5ZsJ35+PN743iNG5LO H+PpN3rpEeUoqX5emSzfOiN9Z6kS8x/P2pWLeSF5JVfZqkY1fUNfVBFGpwc0xAxjY22/ beIYhsg3CJyF0ZZgxYcLjD4lLssCmpfsZuO/89O4n2kke37WNOZK5De1h19E2zd25lFz orBhVKGIhk70nldiikssp5aq6l6Xmx+EqwTdsTh3zYWFV0W+gwqxyqTNwUBnOECPyFkm kXWA== X-Gm-Message-State: AAQBX9dIAeaC9fdGIfJ0BsGpzfZKOhTYJLJJwkBldT0g8GHfTTnOl2gv Utwtt1/2IM6gIQJbqyQ7zBsjUUOGVyz6JWi4Ylx7y8ix5O/tode+TymYeARJwLnl9Nx+B+zCOEF jLWCJo0CoCBneFRY+Euf7SvWrrQsYp2KZpIH96IkIJoYWLbkMvOY60vfP7ODDf0VGwchT49nme4 UlB8ZMZQ== X-Received: by 2002:a05:622a:1208:b0:3dc:fa58:97fd with SMTP id y8-20020a05622a120800b003dcfa5897fdmr3102003qtx.25.1680612342048; Tue, 04 Apr 2023 05:45:42 -0700 (PDT) X-Google-Smtp-Source: AKy350bP2jfwU6IXpLTxzV6md3jtlQHHY67HqVwNdybKmMzf/gCki3MpMVXMaslF5ctsBw7gC7uZzw== X-Received: by 2002:a05:622a:1208:b0:3dc:fa58:97fd with SMTP id y8-20020a05622a120800b003dcfa5897fdmr3101954qtx.25.1680612341566; Tue, 04 Apr 2023 05:45:41 -0700 (PDT) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id y3-20020a37f603000000b007468733cd1fsm3539146qkj.58.2023.04.04.05.45.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 05:45:41 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/5] gdb: make set/show args work with $_gdb_setting_str Date: Tue, 4 Apr 2023 13:45:28 +0100 Message-Id: <14b084ae3950c36f8e631404975d3468813ef3fc.1680608960.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I noticed that $_gdb_setting_str was not working with 'args', e.g.: $ gdb -q --args /tmp/hello.x arg1 arg2 arg3 Reading symbols from /tmp/hello.x... (gdb) show args Argument list to give program being debugged when it is started is "arg1 arg2 arg3". (gdb) print $_gdb_setting_str("args") $1 = "" This is because the 'args' setting is implemented using a scratch variable ('inferior_args_scratch') which is updated when the user does 'set args ...'. There is then a function 'set_args_command' which is responsible for copying the scratch area into the current inferior. However, when the user sets the arguments via the command line the scratch variable is not updated, instead the arguments are pushed straight into the current inferior. There is a second problem, when the current inferior changes the scratch area is not updated, which means that the value returned will only ever reflect the last call to 'set args ...' regardless of which inferior is currently selected. Luckily, the fix is pretty easy, set/show variables have an alternative API which requires we provide some getter and setter functions. With this done the scratch variable can be removed and the value returned will now always reflect the current inferior. While working on set/show args I also rewrote show_args_command to remove the use of deprecated_show_value_hack. --- gdb/infcmd.c | 41 +++++----- gdb/testsuite/gdb.multi/gdb-settings.c | 22 ++++++ gdb/testsuite/gdb.multi/gdb-settings.exp | 98 ++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 21 deletions(-) create mode 100644 gdb/testsuite/gdb.multi/gdb-settings.c create mode 100644 gdb/testsuite/gdb.multi/gdb-settings.exp diff --git a/gdb/infcmd.c b/gdb/infcmd.c index dd5808b17e7..2f0d4f38c85 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -66,14 +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 string containing arguments to give to the - program will be stored by 'set args'. As soon as anything is - stored, notice_args_set will move it into per-inferior storage. - Arguments are separated by spaces. Empty string (pointer to '\0') - means no args. */ - -static std::string inferior_args_scratch; - /* Scratch area where the new cwd will be stored by 'set cwd'. */ static std::string inferior_cwd_scratch; @@ -136,26 +128,33 @@ set_inferior_args_vector (int argc, char **argv) current_inferior ()->set_args (std::move (n)); } -/* Notice when `set args' is run. */ +/* Store the new value passed to 'set args'. */ static void -set_args_command (const char *args, int from_tty, struct cmd_list_element *c) +set_args_value (const std::string &args) { - /* CLI has assigned the user-provided value to inferior_args_scratch. - Now route it to current inferior. */ - current_inferior ()->set_args (inferior_args_scratch); + current_inferior ()->set_args (args); } -/* Notice when `show args' is run. */ +/* Return the value for 'show args' to display. */ + +static const std::string & +get_args_value () +{ + return current_inferior ()->args (); +} + +/* Callback to implement 'show args' command. */ static void show_args_command (struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value) { - /* Note that we ignore the passed-in value in favor of computing it - directly. */ - deprecated_show_value_hack (file, from_tty, c, - current_inferior ()->args ().c_str ()); + /* Ignore the passed in value, pull the argument directly from the + inferior. However, these should always be the same. */ + gdb_printf (_("\ +Argument list to give program being debugged when it is started is \"%s\".\n"), + current_inferior ()->args ().c_str ()); } /* See gdbsupport/common-inferior.h. */ @@ -3154,12 +3153,12 @@ is restored."), add_alias_cmd ("tty", tty_set_show.set, class_run, 0, &cmdlist); auto args_set_show - = add_setshow_string_noescape_cmd ("args", class_run, - &inferior_args_scratch, _("\ + = add_setshow_string_noescape_cmd ("args", class_run, _("\ Set argument list to give program being debugged when it is started."), _("\ Show argument list to give program being debugged when it is started."), _("\ Follow this command with any number of args, to be passed to the program."), - set_args_command, + set_args_value, + get_args_value, show_args_command, &setlist, &showlist); set_cmd_completer (args_set_show.set, filename_completer); diff --git a/gdb/testsuite/gdb.multi/gdb-settings.c b/gdb/testsuite/gdb.multi/gdb-settings.c new file mode 100644 index 00000000000..3a264f239ed --- /dev/null +++ b/gdb/testsuite/gdb.multi/gdb-settings.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 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 . */ + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.multi/gdb-settings.exp b/gdb/testsuite/gdb.multi/gdb-settings.exp new file mode 100644 index 00000000000..d2741d16157 --- /dev/null +++ b/gdb/testsuite/gdb.multi/gdb-settings.exp @@ -0,0 +1,98 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2023 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 . + +# Test the use of $_gdb_setting_str with multiple inferiors. Mostly +# this test is interested in checking that per-inferior settings work +# correctly. + +standard_testfile + +require !use_gdb_stub + +if {[build_executable "failed to prepare" $testfile $srcfile]} { + return -1 +} + +# Return a string containing the args to pass to inferior NUM. + +proc inferior_args {num} { + return "a_${num}_1 a_${num}_2 a_${num}_3" +} + +# Start inferior NUM. + +proc start_inferior {num} { + with_test_prefix "start_inferior $num" { + global srcfile binfile + + set args [inferior_args ${num}] + + if {$num != 1} { + gdb_test "add-inferior" "Added inferior $num.*" \ + "add empty inferior" + gdb_test "inferior $num" "Switching to inferior $num.*" \ + "switch to inferior" + + gdb_test_no_output "set args $args" + } + + gdb_test {print $_gdb_setting_str("args")} \ + " = \"$args\"" \ + "check args before loading a file" + + gdb_load $binfile + + if {[gdb_start_cmd] < 0} { + fail "could not start" + return -1 + } + gdb_test "" ".*reakpoint .*, main .*${srcfile}.*" "start" + + gdb_test {print $_gdb_setting_str("args")} \ + " = \"$args\"" \ + "check after after starting" + } + + return 0 +} + +# For the first inferior we set the arguments on the command line. +save_vars { GDBFLAGS } { + set args [inferior_args 1] + append GDBFLAGS " --args ${binfile} $args" + clean_restart +} + +# An arbitrary number of inferiors to start. +set NUM_INFS 3 + +# Start each inferior, setting (and checking) its args as we do. +for {set i 1} {$i <= $NUM_INFS} {incr i} { + if {[start_inferior $i] < 0} { + return -1 + } +} + +# Switch back to each inferior and check the args are correct. +for {set i 1} {$i <= $NUM_INFS} {incr i} { + gdb_test "inferior $i" "Switching to inferior $i .*" + + set args [inferior_args $i] + gdb_test {print $_gdb_setting_str("args")} \ + " = \"$args\"" \ + "check args after after switching to inferior $i" +} -- 2.25.4