[PATCH v2 2/3] gdb: Add per-remote target variables for memory read and write config

Christina Schimpe via Gdb-patches
2022-03-29T13:13:55Z

This patch adds per-remote target variables for the configuration of
memory read- and write packet size. It is a further change to commit
"gdb: Make global feature array a per-remote target array" to apply the
fixme notes described in commit 5b6d1e4 "Multi-target support".

The former global variables for that configuration are still available
to allow the command line configuration for all future remote
connections. Similar to the command line configuration of the per-
remote target feature array, the commands

- set remotewritesize (deprecated)
- set remote memory-read-packet-size
- set remote memory-write-packet-size

will configure the current target (if available) and future remote
connections. The show command will display the current remote target's
packet size configuration. If no remote target is selected the default
configuration for future connections will be shown.

It is required to adapt the test gdb.base/remote.exp which is failing
for --target_board=native-extended-gdbserver. With that board GDB
connects to gdbserver at gdb start time. Due to this patch two loggings
"The target may not be able to.." are shown if the command 'set remote
memory-write-packet-size fixed' is executed while a target is connected
for the current inferior. To fix this, the clean_restart command is
moved to a later time point of the test. It is sufficient to be
connected to the server when "runto_main" is executed. Now the
connection time is similar to a testrun with
--target_board=native-gdbserver.

To allow the user to distinguish between the packet-size configuration
for future connections and for the currently selected target, the
logging of the command 'set remote memory-write-packet-size fixed' is
adapted.
---
gdb/NEWS | 12 +++
gdb/doc/gdb.texinfo | 14 +++
gdb/remote.c | 143 +++++++++++++++++++++---------
gdb/testsuite/gdb.base/remote.exp | 45 ++++++----
4 files changed, 154 insertions(+), 60 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 7740162d3e..aa704d076d 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -16,6 +16,18 @@
the configuration of the currently selected target. If no remote target is
selected, the default configuration for future connections is shown.

+ The individual packet sizes can be configured and shown using the commands
+ ** 'set remote memory-read-packet-size (number of bytes|fixed|limit)'
+ ** 'set remote memory-write-packet-size (number of bytes|fixed|limit)'
+ ** 'show remote memory-read-packet-size'
+ ** 'show remote memory-write-packet-size'.
+
+ The configuration of the packet itself, as well as the size of a memory-read
+ or memory-write packet, applies to the currently selected target (if
+ available) and future remote connections. The "show remote" commands print
+ the configuration of the currently selected target. If no remote target is
+ selected, the default configuration for future connections is shown.
+
* GDB now supports hardware watchpoints on FreeBSD/Aarch64.

* Remove support for building against Python 2, it is now only possible to
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 2c5e4d1b1b..d9f70476d2 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -23612,6 +23612,20 @@ future connections is shown. The available settings are:

@end multitable

+@cindex Configuration of the remote packet size
+The number of bytes per memory-read or memory-write packet for a remote target
+can be configured using the commands @code{set remote memory-read-packet-size}
+and @code{set remote memory-write-packet-size}. If set to @samp{0} (zero) the
+default packet size will be used. The actual limit is further reduced depending
+on the target. Specify @samp{fixed} to disable the target-dependent restriction
+and @samp{limit} to enable it. Similar to the enabling and disabling of remote
+packets, the command applies to the currently selected target (if available) and
+all future remote connections.
+The configuration of the selected target can be displayed using the commands
+@code{show remote memory-read-packet-size} and
+@code{show remote memory-write-packet-size}. If no remote target is selected,
+the default configuration for future connections is shown.
+
@node Remote Stub
@section Implementing a Remote Stub

diff --git a/gdb/remote.c b/gdb/remote.c
index 5d9d328968..510af52c49 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -602,8 +602,31 @@ struct packet_config
enum packet_support support;
};

-/* This global array contains the default configuration for every new
- per-remote target array. */
+/* User configurable variables for the number of characters in a
+ memory read/write packet. MIN (rsa->remote_packet_size,
+ rsa->sizeof_g_packet) is the default. Some targets need smaller
+ values (fifo overruns, et.al.) and some users need larger values
+ (speed up transfers). The variables ``preferred_*'' (the user
+ request), ``current_*'' (what was actually set) and ``forced_*''
+ (Positive - a soft limit, negative - a hard limit). */
+
+struct memory_packet_config
+{
+ const char *name;
+ long size;
+ int fixed_p;
+};
+
+/* These global variables contain the default configuration for every new
+ remote_feature object. */
+static memory_packet_config memory_read_packet_config =
+{
+ "memory-read-packet-size",
+};
+static memory_packet_config memory_write_packet_config =
+{
+ "memory-write-packet-size",
+};
static packet_config remote_protocol_packets[PACKET_MAX];

/* Description of a remote target's features. It stores the configuration
@@ -613,6 +636,9 @@ struct remote_features
{
remote_features ()
{
+ m_memory_read_packet_config = memory_read_packet_config;
+ m_memory_write_packet_config = memory_write_packet_config;
+
std::copy (std::begin (remote_protocol_packets),
std::end (remote_protocol_packets),
std::begin (m_protocol_packets));
@@ -653,6 +679,11 @@ struct remote_features
new connection to a remote target. */
void reset_all_packet_configs_support ();

+ /* Configuration of a remote target's memory read packet. */
+ memory_packet_config m_memory_read_packet_config;
+ /* Configuration of a remote target's memory write packet. */
+ memory_packet_config m_memory_write_packet_config;
+
/* The per-remote target array which stores a remote's packet
configurations. */
packet_config m_protocol_packets[PACKET_MAX];
@@ -1906,21 +1937,6 @@ show_remotebreak (struct ui_file *file, int from_tty,
static unsigned int remote_address_size;

\f
-/* User configurable variables for the number of characters in a
- memory read/write packet. MIN (rsa->remote_packet_size,
- rsa->sizeof_g_packet) is the default. Some targets need smaller
- values (fifo overruns, et.al.) and some users need larger values
- (speed up transfers). The variables ``preferred_*'' (the user
- request), ``current_*'' (what was actually set) and ``forced_*''
- (Positive - a soft limit, negative - a hard limit). */
-
-struct memory_packet_config
-{
- const char *name;
- long size;
- int fixed_p;
-};
-
/* The default max memory-write-packet-size, when the setting is
"fixed". The 16k is historical. (It came from older GDB's using
alloca for buffers and the knowledge (folklore?) that some hosts
@@ -1986,7 +2002,8 @@ remote_target::get_memory_packet_size (struct memory_packet_config *config)
something really big then do a sanity check. */

static void
-set_memory_packet_size (const char *args, struct memory_packet_config *config)
+set_memory_packet_size (const char *args, struct memory_packet_config *config,
+ bool target_connected)
{
int fixed_p = config->fixed_p;
long size = config->size;
@@ -2020,9 +2037,16 @@ set_memory_packet_size (const char *args, struct memory_packet_config *config)
? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
: size);

- if (! query (_("The target may not be able to correctly handle a %s\n"
- "of %ld bytes. Change the packet size? "),
- config->name, query_size))
+ if (target_connected
+ && !query (_("The target may not be able to correctly handle a %s\n"
+ "of %ld bytes. Change the packet size? "),
+ config->name, query_size))
+ error (_("Packet size not changed."));
+ else if (!target_connected
+ && !query (_("Future targets may not be able to correctly "
+ "handle a %s\nof %ld bytes. Change the packet "
+ "size for future remote targets? "),
+ config->name, query_size))
error (_("Packet size not changed."));
}
/* Update the config. */
@@ -2031,20 +2055,23 @@ set_memory_packet_size (const char *args, struct memory_packet_config *config)
}

static void
-show_memory_packet_size (struct memory_packet_config *config)
+show_memory_packet_size (memory_packet_config *config, remote_target *remote)
{
+ const char* target_type = get_target_type_name (remote != nullptr);
+
if (config->size == 0)
- printf_filtered (_("The %s is 0 (default). "), config->name);
+ printf_filtered (_("The %s %s is 0 (default). "),
+ config->name, target_type);
else
- printf_filtered (_("The %s is %ld. "), config->name, config->size);
+ printf_filtered (_("The %s %s is %ld. "), config->name, target_type,
+ config->size);
+
if (config->fixed_p)
printf_filtered (_("Packets are fixed at %ld bytes.\n"),
get_fixed_memory_packet_size (config));
else
{
- remote_target *remote = get_current_remote_target ();
-
- if (remote != NULL)
+ if (remote != nullptr)
printf_filtered (_("Packets are limited to %ld bytes.\n"),
remote->get_memory_packet_size (config));
else
@@ -2053,22 +2080,46 @@ show_memory_packet_size (struct memory_packet_config *config)
}
}

-/* FIXME: needs to be per-remote-target. */
-static struct memory_packet_config memory_write_packet_config =
+/* Print the current configuration of memory-read or write-packet size.
+ If TARGET_CONNECTED is true the currently selected target's configuration
+ should be passed in CONFIG, if false the default configuration for future
+ remote targets. */
+
+static void
+print_set_memory_packet_size (const char *args,
+ const memory_packet_config* config,
+ bool target_connected)
{
- "memory-write-packet-size",
-};
+ if (target_connected)
+ printf_filtered (_("Use of the %s for the current and future remote "
+ "targets is set to \"%s\".\n"), config->name, args);
+ else
+ printf_filtered (_("Use of the %s for future remote targets is set to "
+ "\"%s\".\n"), config->name, args);
+}

static void
set_memory_write_packet_size (const char *args, int from_tty)
{
- set_memory_packet_size (args, &memory_write_packet_config);
+ remote_target *remote = get_current_remote_target ();
+ if (remote != nullptr)
+ set_memory_packet_size
+ (args, &remote->m_features.m_memory_write_packet_config, true);
+
+ memory_packet_config* config = &memory_write_packet_config;
+ set_memory_packet_size (args, config, false);
+ print_set_memory_packet_size (args, config, remote != nullptr);
}

static void
show_memory_write_packet_size (const char *args, int from_tty)
{
- show_memory_packet_size (&memory_write_packet_config);
+ remote_target *remote = get_current_remote_target ();
+ if (remote != nullptr)
+ show_memory_packet_size (&remote->m_features.m_memory_write_packet_config,
+ remote);
+ else
+ show_memory_packet_size (&memory_write_packet_config, nullptr);
}

/* Show the number of hardware watchpoints that can be used. */
@@ -2124,31 +2175,37 @@ show_remote_packet_max_chars (struct ui_file *file, int from_tty,
long
remote_target::get_memory_write_packet_size ()
{
- return get_memory_packet_size (&memory_write_packet_config);
+ return get_memory_packet_size (&m_features.m_memory_write_packet_config);
}

-/* FIXME: needs to be per-remote-target. */
-static struct memory_packet_config memory_read_packet_config =
-{
- "memory-read-packet-size",
-};
-
static void
set_memory_read_packet_size (const char *args, int from_tty)
{
- set_memory_packet_size (args, &memory_read_packet_config);
+ remote_target *remote = get_current_remote_target ();
+ if (remote != nullptr)
+ set_memory_packet_size
+ (args, &remote->m_features.m_memory_read_packet_config, true);
+
+ memory_packet_config* config = &memory_read_packet_config;
+ set_memory_packet_size (args, config, false);
+ print_set_memory_packet_size (args, config, remote != nullptr);
}

static void
show_memory_read_packet_size (const char *args, int from_tty)
{
- show_memory_packet_size (&memory_read_packet_config);
+ remote_target *remote = get_current_remote_target ();
+ if (remote != nullptr)
+ show_memory_packet_size (&remote->m_features.m_memory_read_packet_config,
+ remote);
+ else
+ show_memory_packet_size (&memory_read_packet_config, nullptr);
}

long
remote_target::get_memory_read_packet_size ()
{
- long size = get_memory_packet_size (&memory_read_packet_config);
+ long size = get_memory_packet_size (&m_features.m_memory_read_packet_config);

/* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
extra buffer size argument before the memory read size can be
diff --git a/gdb/testsuite/gdb.base/remote.exp b/gdb/testsuite/gdb.base/remote.exp
index 1f0869433f..935f7e5aee 100644
--- a/gdb/testsuite/gdb.base/remote.exp
+++ b/gdb/testsuite/gdb.base/remote.exp
@@ -38,31 +38,37 @@ gdb_test "disconnect" ".*"
#

gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
+ "The memory-write-packet-size on newly created remote targets is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
"write-packet default"

gdb_test "set remote memory-write-packet-size" \
"Argument required .integer, `fixed' or `limited'.\." \
"set write-packet - NULL"

-gdb_test_no_output "set remote memory-write-packet-size 20"
+gdb_test "set remote memory-write-packet-size 20" \
+ "Use of the memory-write-packet-size for future remote targets is set to \"20\"."
+
gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 20. The actual limit will be further reduced dependent on the target\." \
+ "The memory-write-packet-size on newly created remote targets is 20. The actual limit will be further reduced dependent on the target\." \
"set write-packet - small"

-gdb_test_no_output "set remote memory-write-packet-size 1"
+gdb_test "set remote memory-write-packet-size 1" \
+ "Use of the memory-write-packet-size for future remote targets is set to \"1\"."
+
gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 1. The actual limit will be further reduced dependent on the target\." \
+ "The memory-write-packet-size on newly created remote targets is 1. The actual limit will be further reduced dependent on the target\." \
"set write-packet - very-small"

-gdb_test_no_output "set remote memory-write-packet-size 0"
+gdb_test "set remote memory-write-packet-size 0" \
+ "Use of the memory-write-packet-size for future remote targets is set to \"0\"."
+
gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
+ "The memory-write-packet-size on newly created remote targets is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
"write-packet default again"

set test "set remote memory-write-packet-size fixed"
gdb_test_multiple $test $test {
- -re "Change the packet size. .y or n. " {
+ -re "Change the packet size for future remote targets. .y or n. " {
gdb_test_multiple "y" $test {
-re "$gdb_prompt $" {
pass $test
@@ -70,13 +76,16 @@ gdb_test_multiple $test $test {
}
}
}
+
gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 0 \\(default\\). Packets are fixed at 16384 bytes\." \
+ "The memory-write-packet-size on newly created remote targets is 0 \\(default\\). Packets are fixed at 16384 bytes\." \
"write-packet default fixed"

-gdb_test_no_output "set remote memory-write-packet-size limit"
+gdb_test "set remote memory-write-packet-size limit" \
+ "Use of the memory-write-packet-size for future remote targets is set to \"limit\"."
+
gdb_test "show remote memory-write-packet-size" \
- "The memory-write-packet-size is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
+ "The memory-write-packet-size on newly created remote targets is 0 \\(default\\). The actual limit will be further reduced dependent on the target\." \
"write-packet default limit again"

#
@@ -88,8 +97,9 @@ proc gdb_load_timed {executable class writesize} {
set test "timed download `[file tail $executable]' - $class, $writesize"

if {$writesize != ""} then {
- gdb_test_no_output "set remote memory-write-packet-size $writesize" \
- "$test - set packet size"
+ gdb_test "set remote memory-write-packet-size $writesize" \
+ "Use of the memory-write-packet-size for future remote targets is set to \"$writesize\"." \
+ "$test - set packet size"

send_gdb "set remote memory-write-packet-size $class\n"
gdb_expect 5 {
@@ -129,8 +139,6 @@ proc gdb_load_timed {executable class writesize} {
pass $test
}

-clean_restart $binfile
-
# These download tests won't actually download anything on !is_remote
# target boards, but we run them anyway because it's simpler, and
# harmless.
@@ -155,6 +163,8 @@ gdb_load_timed $binfile "limit" 0
# Get the size of random_data table (defaults to 48K).
set sizeof_random_data [get_sizeof "random_data" 48*1024]

+clean_restart $binfile
+
#
# Part THREE: Check the upload behavour
#
@@ -178,10 +188,11 @@ if {$sizeof_random_data > 16380 } then {

# Read a chunk just larger than the packet size (reduce the packet
# size to make life easier)
-gdb_test_no_output "set remote memory-read-packet-size 16"
+gdb_test "set remote memory-read-packet-size 16" \
+ "Use of the memory-read-packet-size for the current and future remote targets is set to \"16\"."

gdb_test "show remote memory-read-packet-size" \
- "The memory-read-packet-size is 16. Packets are limited to 20 bytes."
+ "The memory-read-packet-size on the current remote target is 16. Packets are limited to 20 bytes."
gdb_test "x/17ub random_data" \
"<random_data>:\[ \t\]+60\[ \t\]+74\[ \t\]+216\[ \t\]+38\[ \t\]+149\[ \t\]+49\[ \t\]+207\[ \t\]+44.*<random_data\\+8>:\[ \t\]+124\[ \t\]+38\[ \t\]+93\[ \t\]+125\[ \t\]+232\[ \t\]+67\[ \t\]+228\[ \t\]+56.*<random_data\\+16>:\[ \t\]+161"

--
2.25.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928