[binutils-gdb] gdb: improve how 'remote exec-file' is stored and accessed
Andrew Burgess
aburgess@sourceware.org
Sun Sep 14 14:57:47 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a108d9c1136459cd03ce294100932bcce870a21f
commit a108d9c1136459cd03ce294100932bcce870a21f
Author: Andrew Burgess <aburgess@redhat.com>
Date: Thu Jul 20 10:25:43 2023 +0100
gdb: improve how 'remote exec-file' is stored and accessed
This commit makes two related changes. The goal of the commit is to
update the 'remote exec-file' setting to work correctly in a
multi-inferior setup. To do this I have switched from the older
style add_setshow_* function, which uses a single backing variable, to
the newer style add_setshow_* functions that uses a get/set callback.
The get/set callbacks now directly access the state held in the
progspace which ensures that the correct value is always returned.
However, the new get/set API requires that the get callback return a
reference to the setting's value, which in this case needs to be a
std::string.
Currently the 'remote exec-file' setting is stored as a 'char *'
string, which isn't going to work.
And so, this commit also changes 'remote exec-file' to be stored as a
std::string within the progspace.
Now, when switching between multiple inferiors, GDB can correctly
inform the user about the value of the 'remote exec-file' setting.
Approved-By: Tom Tromey <tom@tromey.com>
Diff:
---
gdb/remote.c | 62 ++++++++++++++------------------
gdb/testsuite/gdb.multi/gdb-settings.exp | 13 +++++++
2 files changed, 39 insertions(+), 36 deletions(-)
diff --git a/gdb/remote.c b/gdb/remote.c
index e108e982902..5535afbc100 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1396,7 +1396,7 @@ public: /* Remote specific methods. */
void remote_kill_k ();
void extended_remote_disable_randomization (int val);
- int extended_remote_run (const char *remote_exec_file,
+ int extended_remote_run (const std::string &remote_exec_file,
const std::string &args);
void send_environment_packet (const char *action,
@@ -1525,14 +1525,7 @@ remote_register_is_expedited (int regnum)
}
/* Per-program-space data key. */
-static const registry<program_space>::key<char, gdb::xfree_deleter<char>>
- remote_pspace_data;
-
-/* The variable registered as the control variable used by the
- remote exec-file commands. While the remote exec-file setting is
- per-program-space, the set/show machinery uses this as the
- location of the remote exec-file value. */
-static std::string remote_exec_file_var;
+static const registry<program_space>::key<std::string> remote_pspace_data;
/* The size to align memory write packets, when practical. The protocol
does not guarantee any alignment, and gdb will generate short
@@ -1862,47 +1855,42 @@ remote_target::get_remote_state ()
/* Fetch the remote exec-file from the current program space. */
-static const char *
+static const std::string &
get_remote_exec_file (void)
{
- char *remote_exec_file;
-
- remote_exec_file = remote_pspace_data.get (current_program_space);
- if (remote_exec_file == NULL)
- return "";
-
- return remote_exec_file;
+ const std::string *remote_exec_file
+ = remote_pspace_data.get (current_program_space);
+ if (remote_exec_file == nullptr)
+ remote_exec_file = remote_pspace_data.emplace (current_program_space);
+ return *remote_exec_file;
}
/* Set the remote exec file for PSPACE. */
static void
set_pspace_remote_exec_file (struct program_space *pspace,
- const char *remote_exec_file)
+ const std::string &filename)
{
- char *old_file = remote_pspace_data.get (pspace);
-
- xfree (old_file);
- remote_pspace_data.set (pspace, xstrdup (remote_exec_file));
+ remote_pspace_data.clear (pspace);
+ remote_pspace_data.emplace (pspace, filename);
}
-/* The "set/show remote exec-file" set command hook. */
+/* The "set remote exec-file" callback. */
static void
-set_remote_exec_file (const char *ignored, int from_tty,
- struct cmd_list_element *c)
+set_remote_exec_file_cb (const std::string &filename)
{
set_pspace_remote_exec_file (current_program_space,
- remote_exec_file_var.c_str ());
+ filename);
}
-/* The "set/show remote exec-file" show command hook. */
+/* Implement the "show remote exec-file" command. */
static void
show_remote_exec_file (struct ui_file *file, int from_tty,
struct cmd_list_element *cmd, const char *value)
{
- gdb_printf (file, "%s\n", get_remote_exec_file ());
+ gdb_printf (file, "%s\n", get_remote_exec_file ().c_str ());
}
static int
@@ -10863,7 +10851,7 @@ remote_target::extended_remote_disable_randomization (int val)
}
int
-remote_target::extended_remote_run (const char *remote_exec_file,
+remote_target::extended_remote_run (const std::string &remote_exec_file,
const std::string &args)
{
struct remote_state *rs = get_remote_state ();
@@ -10877,10 +10865,11 @@ remote_target::extended_remote_run (const char *remote_exec_file,
strcpy (rs->buf.data (), "vRun;");
len = strlen (rs->buf.data ());
- if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
+ if (remote_exec_file.size () * 2 + len >= get_remote_packet_size ())
error (_("Remote file name too long for run packet"));
- len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf.data () + len,
- strlen (remote_exec_file));
+ len += 2 * bin2hex ((gdb_byte *) remote_exec_file.data (),
+ rs->buf.data () + len,
+ remote_exec_file.size ());
if (!args.empty ())
{
@@ -10924,7 +10913,7 @@ remote_target::extended_remote_run (const char *remote_exec_file,
"try \"set remote exec-file\"?"));
else
error (_("Running \"%s\" on the remote target failed"),
- remote_exec_file);
+ remote_exec_file.c_str ());
default:
gdb_assert_not_reached ("bad switch");
}
@@ -11044,7 +11033,7 @@ extended_remote_target::create_inferior (const char *exec_file,
int run_worked;
char *stop_reply;
struct remote_state *rs = get_remote_state ();
- const char *remote_exec_file = get_remote_exec_file ();
+ const std::string &remote_exec_file = get_remote_exec_file ();
/* If running asynchronously, register the target file descriptor
with the event loop. */
@@ -16664,10 +16653,11 @@ Transfer files to and from the remote target system."),
&remote_cmdlist);
add_setshow_string_noescape_cmd ("exec-file", class_files,
- &remote_exec_file_var, _("\
+ _("\
Set the remote pathname for \"run\"."), _("\
Show the remote pathname for \"run\"."), NULL,
- set_remote_exec_file,
+ set_remote_exec_file_cb,
+ get_remote_exec_file,
show_remote_exec_file,
&remote_set_cmdlist,
&remote_show_cmdlist);
diff --git a/gdb/testsuite/gdb.multi/gdb-settings.exp b/gdb/testsuite/gdb.multi/gdb-settings.exp
index 368eddecb7c..026a10ba68a 100644
--- a/gdb/testsuite/gdb.multi/gdb-settings.exp
+++ b/gdb/testsuite/gdb.multi/gdb-settings.exp
@@ -71,6 +71,7 @@ foreach_with_prefix inf $inferiors {
gdb_test_no_output "set args inf${inf}-args"
gdb_test_no_output "set cwd /inf${inf}-cwd"
gdb_test_no_output "set inferior-tty /inf${inf}-tty"
+ gdb_test_no_output "set remote exec-file /inf${inf}-remote-exec"
}
# Check settings are still correct for each inferior.
@@ -88,6 +89,9 @@ foreach_with_prefix inf $inferiors {
gdb_test "with inferior-tty tmp-value -- print 1" " = 1"
gdb_test "show inferior-tty" "/inf${inf}-tty.*"
+ gdb_test "with remote exec-file tmp-value -- print 1" " = 1"
+ gdb_test "show remote exec-file" "/inf${inf}-remote-exec"
+
# If the inferiors are running check $_gdb_setting_str and
# $_gdb_setting return the correct values.
if { $run } {
@@ -101,6 +105,11 @@ foreach_with_prefix inf $inferiors {
"\"/inf${inf}-tty\""
gdb_test {print $_gdb_setting("inferior-tty")} \
"\"/inf${inf}-tty\""
+
+ gdb_test {print $_gdb_setting_str("remote exec-file")} \
+ "\"/inf${inf}-remote-exec\""
+ gdb_test {print $_gdb_setting("remote exec-file")} \
+ "\"/inf${inf}-remote-exec\""
}
# Check the settings can be read from Python.
@@ -109,6 +118,8 @@ foreach_with_prefix inf $inferiors {
gdb_test "python print(gdb.parameter('cwd'))" "/inf${inf}-cwd"
gdb_test "python print(gdb.parameter('inferior-tty'))" \
"/inf${inf}-tty"
+ gdb_test "python print(gdb.parameter('remote exec-file'))" \
+ "/inf${inf}-remote-exec"
}
# Check the settings can be read from Guile.
@@ -119,5 +130,7 @@ foreach_with_prefix inf $inferiors {
"/inf${inf}-cwd"
gdb_test "guile (print (parameter-value \"inferior-tty\"))" \
"/inf${inf}-tty"
+ gdb_test "guile (print (parameter-value \"remote exec-file\"))" \
+ "/inf${inf}-remote-exec"
}
}
More information about the Gdb-cvs
mailing list