* [PATCH 0/3] gdb/remote: improve packet buffer checks for p packet
@ 2026-11-09 15:35 Andrew Burgess
2026-11-09 15:35 ` [PATCH 1/3] gdbsupport: update comment on hex2bin Andrew Burgess
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Andrew Burgess @ 2026-11-09 15:35 UTC (permalink / raw)
To: gdb-patches; +Cc: Andrew Burgess
(1) Minor comment correctness fix.
(2) Add support for p/P packets to gdbserver.
(3) Improve GDB's handling of invalid 'p' packets.
Thanks,
Andrew
---
Andrew Burgess (3):
gdbsupport: update comment on hex2bin
gdbserver: add support for p/P packets and flag to disable g/G packets
gdb/remote: improve error checking for 'p' packet reply
gdb/remote.c | 44 ++--
gdb/testsuite/gdb.replay/p-packet.c | 22 ++
gdb/testsuite/gdb.replay/p-packet.exp | 213 ++++++++++++++++++
.../gdb.server/disable-gG-packet.exp | 143 ++++++++++++
gdbserver/server.cc | 170 +++++++++++++-
gdbserver/server.h | 1 +
gdbsupport/common-utils.h | 9 +-
7 files changed, 573 insertions(+), 29 deletions(-)
create mode 100644 gdb/testsuite/gdb.replay/p-packet.c
create mode 100644 gdb/testsuite/gdb.replay/p-packet.exp
create mode 100644 gdb/testsuite/gdb.server/disable-gG-packet.exp
base-commit: 686652a12e6d5042a11b5b2a2bf6a15fae361e80
--
2.25.4
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/3] gdbsupport: update comment on hex2bin
2026-11-09 15:35 [PATCH 0/3] gdb/remote: improve packet buffer checks for p packet Andrew Burgess
@ 2026-11-09 15:35 ` Andrew Burgess
2026-11-09 16:49 ` Tom Tromey
2026-11-09 15:35 ` [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets Andrew Burgess
2026-11-09 15:35 ` [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply Andrew Burgess
2 siblings, 1 reply; 8+ messages in thread
From: Andrew Burgess @ 2026-11-09 15:35 UTC (permalink / raw)
To: gdb-patches; +Cc: Andrew Burgess
The comment on hex2bin says that the argument COUNT is the maximum
number of characters to convert. I don't think this is correct, COUNT
is the maximum number of bytes to convert, with each byte being
represented by two characters.
The comment already talks about converting "pairs of hex digits", so
I've updated the comment to indicate that COUNT is the number of
character pairs to convert.
There is no functional change after this commit.
---
gdbsupport/common-utils.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h
index de83a715ac4..e97aed32682 100644
--- a/gdbsupport/common-utils.h
+++ b/gdbsupport/common-utils.h
@@ -223,10 +223,11 @@ extern int fromhex (int a);
/* HEX is a string of characters representing hexadecimal digits.
Convert pairs of hex digits to bytes and store sequentially into
- BIN. COUNT is the maximum number of characters to convert. This
- will convert fewer characters if the number of hex characters
- actually seen is odd, or if HEX terminates before COUNT characters.
- Returns the number of characters actually converted. */
+ BIN. COUNT is the maximum number of character pairs (bytes) to
+ convert. This will convert fewer characters if the number of hex
+ characters actually seen is odd, or if HEX terminates before COUNT
+ character pairs. Returns the number of character pairs (bytes)
+ actually converted. */
extern int hex2bin (const char *hex, gdb_byte *bin, int count);
/* Like the above, but return a gdb::byte_vector. */
--
2.25.4
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets
2026-11-09 15:35 [PATCH 0/3] gdb/remote: improve packet buffer checks for p packet Andrew Burgess
2026-11-09 15:35 ` [PATCH 1/3] gdbsupport: update comment on hex2bin Andrew Burgess
@ 2026-11-09 15:35 ` Andrew Burgess
2026-11-09 17:16 ` Tom Tromey
2026-11-09 15:35 ` [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply Andrew Burgess
2 siblings, 1 reply; 8+ messages in thread
From: Andrew Burgess @ 2026-11-09 15:35 UTC (permalink / raw)
To: gdb-patches; +Cc: Andrew Burgess
In the next commit I want to make some changes to GDB's handling of
the 'p' packet. I was looking for how I could test this packet's
handling using gdbserver and I realised that currently gdbserver
doesn't support the 'p' or 'P' packets at all. Instead all registers
are read and written using the 'g' and 'G' packets respectively.
I understand that gdbserver is a real tool, so we should not be
overloading it with "testing" features, but supporting the 'p' and 'P'
packets doesn't cost that much, and this would allow for greater
testing of GDB.
So, in this commit I have added a new gdbserver command line option:
--disable-packet=gG, which disables use of the 'g' and 'G' packets.
These now return the empty packet indicating the server doesn't
understand them. GDB will then fall back to using 'p' and 'P'
packets.
I then add support for these packets to gdbserver.
I have guarded the new code such that these packets will only work
when the g/G packets are disabled. This maintains gdbserver's current
functionality by default. But we could, if we wanted, allow the p/P
packets to be used in parallel to the g/G packets. This might allow
GDB to use p/P in cases where only a few registers need to be read or
written. However, I propose leaving such a change for the future,
it's not something I plan to work on right now.
Annoyingly there was one fix needed in GDB itself. The 'g' packet
handling in remote_target::send_g_packet (gdb/remote.c) has a rather
nasty loop which tries to "resync" with gdbserver by resending the 'g'
packet if the response doesn't look right. This code seems really
suspect to me; I don't know why this would be the correct thing to do
over fixing whatever bug causes the packet exchange to get out of
sync.
Still, this loop didn't know about empty packets, i.e. servers that
don't support the 'g' packet, so GDB would sit in an infinite loop
spamming the gdbserver with 'g' packets. I've extended this loop so
that if GDB gets back an empty packet then the 'g' packet is
considered answered.
With these fixes in place I was now able to run tests like this:
make check-gdb RUNTESTFLAGS="--target_board=native-gdbserver \
GDBSERVER='/path/to/patched/gdbserver --disable-packet=gG'"
And compare the results to those of running without the disable-packet
option. The only issues I saw were in trace related tests, the
function remote_target::get_trace_status relies on sizeof_g_packet to
define the trace register block. For a target without 'g' packet
support the sizeof_g_packet becomes 0, which means the trace file
records a register block size of 0, which breaks all the trace related
tests. This is a pre-existing issue, and not something I plan to
address in this commit.
The new --disable-packet=gG option is not added to the documentation
or the NEWS entry. This feature is not intended for users. None of
the other --disable-packet options are mentioned in either of these
sources either.
---
gdb/remote.c | 1 +
.../gdb.server/disable-gG-packet.exp | 143 +++++++++++++++
gdbserver/server.cc | 170 +++++++++++++++++-
gdbserver/server.h | 1 +
4 files changed, 312 insertions(+), 3 deletions(-)
create mode 100644 gdb/testsuite/gdb.server/disable-gG-packet.exp
diff --git a/gdb/remote.c b/gdb/remote.c
index a728d57df6e..e33e9f0181d 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9320,6 +9320,7 @@ remote_target::send_g_packet ()
while ((rs->buf[0] < '0' || rs->buf[0] > '9')
&& (rs->buf[0] < 'A' || rs->buf[0] > 'F')
&& (rs->buf[0] < 'a' || rs->buf[0] > 'f')
+ && rs->buf[0] != '\0'
&& rs->buf[0] != 'x') /* New: unavailable register value. */
{
remote_debug_printf ("Bad register packet; fetching a new packet");
diff --git a/gdb/testsuite/gdb.server/disable-gG-packet.exp b/gdb/testsuite/gdb.server/disable-gG-packet.exp
new file mode 100644
index 00000000000..1124aab2a75
--- /dev/null
+++ b/gdb/testsuite/gdb.server/disable-gG-packet.exp
@@ -0,0 +1,143 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2026 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/>.
+
+# Basic testing of the gdbserver '--disable-packet=gG' option. Connect GDB
+# to a gdbserver started with default flags. Send the 'info registers'
+# command and record the register names that are printed. Also use 'maint
+# packet' to check that the p/P packets are disabled by default.
+#
+# Connect to a new gdbserver, this one started with '--disable-packet=gG'.
+# Send 'info registers' again and check we see the same set of registers.
+# Use 'maint packet' to send some invalid p/P packets and check we get back
+# an error.
+
+load_lib gdbserver-support.exp
+
+standard_testfile normal.c
+
+require allow_gdbserver_tests
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+ return
+}
+
+set target_exec [gdbserver_download_current_prog]
+
+# Run 'info registers'. Build and return an array containing the
+# register names of each register.
+proc register_names_from_info_registers { testname } {
+ set reg_names {}
+ gdb_test_multiple "info registers" $testname -lbl {
+ -re "^info registers(?=\r\n)" {
+ exp_continue
+ }
+
+ -re "^\r\n$::gdb_prompt $" {
+ gdb_assert { [llength $reg_names] > 1 } \
+ $gdb_test_name
+ }
+
+ -re "^\r\n(\\S+)\\s+\[^\r\n\]+(?=\r\n)" {
+ lappend reg_names $expect_out(1,string)
+ exp_continue
+ }
+ }
+
+ return $reg_names
+}
+
+# Start gdbserver passing it OPTIONS. Connect to gdbserver and runto
+# the main function.
+proc start_gdbserver_connect_and_runto_main { testname {options ""} } {
+ with_test_prefix $testname {
+ save_vars { ::GDBFLAGS } {
+ # If GDB and GDBserver are both running locally, set the
+ # sysroot to avoid reading files via the remote protocol
+ # (the `is_remote target` check is already done above).
+ if { ![is_remote host] } {
+ set ::GDBFLAGS "$::GDBFLAGS -ex \"set sysroot\""
+ }
+
+ clean_restart $::testfile
+ }
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ set res [gdbserver_start $options $::target_exec]
+
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+ gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
+
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "break in main"
+ }
+}
+
+start_gdbserver_connect_and_runto_main \
+ "start gdbserver with default flags"
+
+set expected_regnames [register_names_from_info_registers \
+ "info registers using g packet"]
+
+# Check that the p/P packets are not available by default.
+gdb_test "maint packet p0" \
+ "received: \"\"" \
+ "p packet is disabled"
+gdb_test "maint packet P0=0" \
+ "received: \"\"" \
+ "P packet is disabled"
+
+start_gdbserver_connect_and_runto_main \
+ "start gdbserver without g/G packet" "--disable-packet=gG"
+
+set regnames [register_names_from_info_registers \
+ "info registers using p packet"]
+
+gdb_assert { $expected_regnames == $regnames } \
+ "saw all the expected registers"
+
+# Check we can write to a register using a 'P' packet.
+gdb_test_no_output "set \$sp = 0x1234"
+gdb_test "maint flush register-cache" \
+ "Register cache flushed\\."
+gdb_test "print/x \$sp" " = 0x0*1234" \
+ "updated stack pointer value seen"
+
+# Test 'p' packet error handling. Try with a register number that is
+# larger than an int. Then try with a register number that should fit
+# in an int, but is large enough to be outside the range of valid
+# register numbers.
+gdb_test "maint packet pffffffffffffffffffff" \
+ "received: \"E01\"" \
+ "p-packet, huge regno is an error"
+gdb_test "maint packet pfffffff" \
+ "received: \"E01\"" \
+ "p-packet, large regno is an error"
+gdb_test "maint packet Pffffffffffffffffffff=0" \
+ "received: \"E01\"" \
+ "P-packet, huge regno is an error"
+gdb_test "maint packet Pfffffff=0" \
+ "received: \"E01\"" \
+ "P-packet, large regno is an error"
+
+# Check gdbserver is still running by asking for a valid register.
+gdb_test "maint packet p0" \
+ "received: \"\[0-9a-f\]+\"" \
+ "gdbserver still active"
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 74774fcde7c..a41d53c016f 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -144,6 +144,7 @@ bool disable_packet_Tthread;
bool disable_packet_qC;
bool disable_packet_qfThreadInfo;
bool disable_packet_T;
+bool disable_packet_gG;
static unsigned char *mem_buf;
@@ -1113,6 +1114,141 @@ handle_general_set (char *own_buf)
own_buf[0] = 0;
}
+/* Handle the 'p' packet for reading registers. */
+
+static void
+handle_p_packet (char *own_buf)
+{
+ /* When the 'g' packet is in use, which is the default, all registers are
+ served via the 'g' packet. If a client tries to use the 'p' packet
+ then gdbserver replies with the empty packet; this function is not
+ called. */
+ gdb_assert (disable_packet_gG);
+
+ /* This should be checked before we get here. */
+ gdb_assert (own_buf[0] == 'p');
+
+ /* If target is not running then set an error and return. */
+ require_running_or_return (own_buf);
+
+ /* The packet format is 'p<regno>' where <regno> is a hex string. Parse
+ the hex string then check that there is nothing else left in the
+ packet buffer, and that the register number is not out of range. */
+ ULONGEST hex_value;
+ const char *p = &own_buf[1];
+ const char *end = unpack_varlen_hex (p, &hex_value);
+ if (hex_value > INT_MAX || *end != '\0' || (end - p) > (sizeof (int) * 2))
+ {
+ write_enn (own_buf);
+ return;
+ }
+ int regno = static_cast<int> (hex_value);
+ gdb_assert (regno >= 0);
+
+ if (regno >= static_cast<int> (current_target_desc ()->reg_defs.size ()))
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ client_state &cs = get_client_state ();
+ if (cs.current_traceframe >= 0)
+ {
+ regcache a_regcache (current_target_desc ());
+
+ if (fetch_traceframe_registers (cs.current_traceframe,
+ &a_regcache, -1) == 0)
+ collect_register_as_string (&a_regcache, regno, own_buf);
+ else
+ write_enn (own_buf);
+ }
+ else
+ {
+ struct regcache *regcache;
+
+ if (!set_desired_thread ())
+ write_enn (own_buf);
+ else
+ {
+ regcache = get_thread_regcache (current_thread);
+ collect_register_as_string (regcache, regno, own_buf);
+ }
+ }
+}
+
+/* Handle the 'P' packet for setting registers. */
+
+static void
+handle_P_packet (char *own_buf)
+{
+ /* When the 'g' packet is in use, which is the default, all registers are
+ served via the 'g' packet and GDB will then try setting all registers
+ via the 'G' packet. If however the 'g' packet is disabled then GDB
+ will not use the 'G' packet for setting registers, and instead relies
+ only on the 'P' packet. */
+ gdb_assert (disable_packet_gG);
+
+ /* This should be checked before we get here. */
+ gdb_assert (own_buf[0] == 'P');
+
+ /* If target is not running then set an error and return. */
+ require_running_or_return (own_buf);
+
+ /* Cannot write into a trace frame. */
+ client_state &cs = get_client_state ();
+ if (cs.current_traceframe >= 0)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* Set the thread. */
+ if (!set_desired_thread ())
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* The packet format is 'P<regno>=<value>' where <regno> and <value> are
+ hex strings. First parse <regno>, check it is in range. */
+ ULONGEST hex_value;
+ const char *p = &own_buf[1];
+ const char *end = unpack_varlen_hex (p, &hex_value);
+ if (hex_value > INT_MAX || *end != '=' || (end - p) > (sizeof (int) * 2))
+ {
+ write_enn (own_buf);
+ return;
+ }
+ int regno = static_cast<int> (hex_value);
+ gdb_assert (regno >= 0);
+
+ if (regno >= static_cast<int> (current_target_desc ()->reg_defs.size ()))
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* Set P to point to the first character of the <value> hex string. */
+ p = end + 1;
+
+ /* Parse the <value> hex string. Ensure that the string is the expected
+ length. */
+ struct regcache *regcache = get_thread_regcache (current_thread);
+ int reg_size = register_size (regcache->tdesc, regno);
+ gdb_byte *val = (gdb_byte *) alloca (reg_size);
+ int len = strlen (p);
+ int bytes_converted = hex2bin (p, val, reg_size);
+ if (bytes_converted != reg_size || (bytes_converted * 2) != len)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* Supply the new register value into the register cache. */
+ supply_register (regcache, regno, val);
+ write_ok (own_buf);
+}
+
static const char *
get_features_xml (const char *annex)
{
@@ -3927,8 +4063,9 @@ gdbserver_usage (FILE *stream)
" --disable-packet=OPT1[,OPT2,...]\n"
" Disable support for RSP packets or features.\n"
" Options:\n"
- " vCont, vConts, T, Tthread, qC, qfThreadInfo and\n"
- " threads (disable all threading packets).\n"
+ " vCont, vConts, T, Tthread, qC, qfThreadInfo,\n"
+ " threads (disable all threading packets), and\n"
+ " gG.\n"
"\n"
"For more information, consult the GDB manual (available as on-line \n"
"info or a printed manual).\n");
@@ -3946,7 +4083,8 @@ gdbserver_show_disableable (FILE *stream)
" Tthread \tPassing the thread specifier in the "
"T stop reply packet\n"
" threads \tAll of the above\n"
- " T \tAll 'T' packets\n");
+ " T \tAll 'T' packets\n"
+ " gG \tThe 'g' and 'G' packets\n");
}
/* Start up the event loop. This is the entry point to the event
@@ -4398,6 +4536,8 @@ captured_main (int argc, char *argv[])
disable_packet_qC = true;
disable_packet_qfThreadInfo = true;
}
+ else if (streq ("gG", tok))
+ disable_packet_gG = true;
else
{
fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
@@ -4899,6 +5039,12 @@ process_serial_event (void)
}
break;
case 'g':
+ if (disable_packet_gG)
+ {
+ cs.own_buf[0] = '\0';
+ break;
+ }
+
require_running_or_break (cs.own_buf);
if (cs.current_traceframe >= 0)
{
@@ -4924,6 +5070,12 @@ process_serial_event (void)
}
break;
case 'G':
+ if (disable_packet_gG)
+ {
+ cs.own_buf[0] = '\0';
+ break;
+ }
+
require_running_or_break (cs.own_buf);
if (cs.current_traceframe >= 0)
write_enn (cs.own_buf);
@@ -4941,6 +5093,18 @@ process_serial_event (void)
}
}
break;
+ case 'p':
+ if (disable_packet_gG)
+ handle_p_packet (cs.own_buf);
+ else
+ cs.own_buf[0] = '\0';
+ break;
+ case 'P':
+ if (disable_packet_gG)
+ handle_P_packet (cs.own_buf);
+ else
+ cs.own_buf[0] = '\0';
+ break;
case 'm':
{
require_running_or_break (cs.own_buf);
diff --git a/gdbserver/server.h b/gdbserver/server.h
index 1a9e6ef62f8..ba12e228f98 100644
--- a/gdbserver/server.h
+++ b/gdbserver/server.h
@@ -60,6 +60,7 @@ extern bool disable_packet_Tthread;
extern bool disable_packet_qC;
extern bool disable_packet_qfThreadInfo;
extern bool disable_packet_T;
+extern bool disable_packet_gG;
extern bool run_once;
extern bool non_stop;
--
2.25.4
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply
2026-11-09 15:35 [PATCH 0/3] gdb/remote: improve packet buffer checks for p packet Andrew Burgess
2026-11-09 15:35 ` [PATCH 1/3] gdbsupport: update comment on hex2bin Andrew Burgess
2026-11-09 15:35 ` [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets Andrew Burgess
@ 2026-11-09 15:35 ` Andrew Burgess
2026-11-09 17:27 ` Tom Tromey
2 siblings, 1 reply; 8+ messages in thread
From: Andrew Burgess @ 2026-11-09 15:35 UTC (permalink / raw)
To: gdb-patches; +Cc: Andrew Burgess
Improve the remote_target::fetch_register_using_p function (remote.c)
in a couple of ways.
When building the outgoing packet use xsnprintf; this allows for
bounds checking as the packet is being written. The chances of the
outgoing packet overflowing are pretty slim, but using a bounded write
seems better than not, so let's do that.
Next, when parsing the reply packet the result is written into a
buffer which is the size of the register being fetched. Unfortunately
the loop that reads the packet does not have any bounds checking,
which allows for buffer overflow. Convert the manual loop which calls
fromhex on each character to instead use hex2bin which allows a limit
to be applied to the number of bytes parsed; this prevents buffer
overflow. hex2bin returns the number of bytes parsed, which also
allows me to check for cases where the remote fails to supply all of
the expected bytes.
For testing I made use of the gdbserver option --disable-packet=gG
which I added in the previous commit. I ran the testsuite like this:
make check-gdb RUNTESTFLAGS="--target_board=native-gdbserver \
GDBSERVER='/path/to/patched/gdbserver --disable-packet=gG'"
I've also added a new test that makes use of the gdbreplay mechanism
to adjust the size of the returned register. This allows testing of
the two error cases: register value too short, and register value too
long.
As in the previous commit the trace related tests fail as the trace
functionality depends on the 'g' packet being supported, but otherwise
I saw no regressions after this commit.
---
gdb/remote.c | 43 +++---
gdb/testsuite/gdb.replay/p-packet.c | 22 +++
gdb/testsuite/gdb.replay/p-packet.exp | 213 ++++++++++++++++++++++++++
3 files changed, 256 insertions(+), 22 deletions(-)
create mode 100644 gdb/testsuite/gdb.replay/p-packet.c
create mode 100644 gdb/testsuite/gdb.replay/p-packet.exp
diff --git a/gdb/remote.c b/gdb/remote.c
index e33e9f0181d..86024bb7311 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9242,27 +9242,22 @@ int
remote_target::fetch_register_using_p (struct regcache *regcache,
packet_reg *reg)
{
- struct gdbarch *gdbarch = regcache->arch ();
- struct remote_state *rs = get_remote_state ();
- char *buf, *p;
- gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
- int i;
-
if (m_features.packet_support (PACKET_p) == PACKET_DISABLE)
return 0;
if (reg->pnum == -1)
return 0;
- p = rs->buf.data ();
- *p++ = 'p';
- p += hexnumstr (p, reg->pnum);
- *p++ = '\0';
+ /* Send the packet asking for the register. */
+ struct remote_state *rs = get_remote_state ();
+ xsnprintf (rs->buf.data (), get_remote_packet_size (), "p%s",
+ phex_nz (reg->pnum, 0));
putpkt (rs->buf);
+
+ /* Read the reply. */
getpkt (&rs->buf);
- buf = rs->buf.data ();
-
+ /* Check the packet status. */
packet_result result = m_features.packet_ok (rs->buf, PACKET_p);
switch (result.status ())
{
@@ -9277,23 +9272,27 @@ remote_target::fetch_register_using_p (struct regcache *regcache,
}
/* If this register is unfetchable, tell the regcache. */
+ char *buf = rs->buf.data ();
if (buf[0] == 'x')
{
regcache->raw_supply (reg->regnum, NULL);
return 1;
}
- /* Otherwise, parse and supply the value. */
- p = buf;
- i = 0;
- while (p[0] != 0)
- {
- if (p[1] == 0)
- error (_("fetch_register_using_p: early buf termination"));
+ /* Otherwise, parse the register value from the hex string. */
+ struct gdbarch *gdbarch = regcache->arch ();
+ int reg_size = register_size (gdbarch, reg->regnum);
+ gdb_byte *regp = (gdb_byte *) alloca (reg_size);
+ int bytes_converted = hex2bin (buf, regp, reg_size);
- regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
- p += 2;
- }
+ /* Check the whole packet was parsed, and that the packet length matches
+ the register size. */
+ if (buf[bytes_converted * 2] != '\0' || bytes_converted != reg_size)
+ error (_("Remote 'p' packet reply for register %s is of unexpected "
+ "length (expected %d bytes): %s"),
+ gdbarch_register_name (gdbarch, reg->regnum), reg_size, buf);
+
+ /* Supply the value to the register cache. */
regcache->raw_supply (reg->regnum, regp);
return 1;
}
diff --git a/gdb/testsuite/gdb.replay/p-packet.c b/gdb/testsuite/gdb.replay/p-packet.c
new file mode 100644
index 00000000000..6a0e311ef41
--- /dev/null
+++ b/gdb/testsuite/gdb.replay/p-packet.c
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2026 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/>. */
+
+int
+main (void)
+{
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.replay/p-packet.exp b/gdb/testsuite/gdb.replay/p-packet.exp
new file mode 100644
index 00000000000..f59fc13faa2
--- /dev/null
+++ b/gdb/testsuite/gdb.replay/p-packet.exp
@@ -0,0 +1,213 @@
+# Copyright 2026 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 error handling for the 'p' packet. Start gdbserver with the g/G
+# packets disabled. Start GDB, connect to the server, and then run 'info
+# registers', this will make use of the 'p' packet.
+#
+# Update the recorded logfile to replace the reply to the first 'p' packet,
+# the updated reply will be an invalid size.
+#
+# Replay the log and check that the 'info registers' gives a suitable error
+# message.
+
+load_lib gdbserver-support.exp
+load_lib gdbreplay-support.exp
+
+require allow_gdbserver_tests
+require has_gdbreplay
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+ return
+}
+
+# Check that remote register 0 is a suitable size. The test could be
+# updated to handle other register sizes, but for now handle 4 or 8 byte
+# registers.
+set reg_size 0
+gdb_test_multiple "maint print remote-registers" "" -lbl {
+ -re "\r\n\\S+\\s+$::decimal\\s+$::decimal\\s+$::decimal\\s+($::decimal)\\s+\[^\r\n\]+\\s+0\\s+0\\s*(?=\r\n)" {
+ set reg_size $expect_out(1,string)
+ exp_continue
+ }
+
+ -re "\r\n$gdb_prompt $" {
+ pass $gdb_test_name
+ }
+}
+
+if { $reg_size != 4 && $reg_size != 8 } {
+ unsupported "remote register 0 is an unsupported size"
+ return
+}
+
+# Connect to gdbserver and run to a breakpoint in main, then use the 'info
+# registers' command. Record the remotelogfile into the REMOTELOG log
+# file.
+proc_with_prefix record_initial_logfile { remotelog } {
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" \
+ "setting sysroot before starting gdbserver"
+
+ # Start gdbserver like:
+ # gdbserver :PORT ....
+ set res [gdbserver_start "--disable-packet=gG" $::binfile]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ gdb_test_no_output "set remotelogfile $remotelog" \
+ "setup remotelogfile"
+
+ # Connect to gdbserver.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbserver"
+ return
+ }
+
+ # If we're connecting as 'remote' then we can't use 'runto'.
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continuing to main"
+
+ gdb_test "info registers" ".*"
+}
+
+# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in
+# main and then uses the 'info registers' command.
+proc_with_prefix replay_without_error { remotelog } {
+ clean_restart $::testfile
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" "setting sysroot"
+
+ set res [gdbreplay_start $remotelog]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ # Connect to gdbreplay.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbreplay"
+ return
+ }
+
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continue to main"
+
+ gdb_test "info registers" ".*"
+}
+
+# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in
+# main and then uses the 'info registers' command. Expect an error from the
+# 'info registers' command, the error will include the VALUE string.
+proc_with_prefix replay_with_error { remotelog value } {
+ clean_restart $::testfile
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" "setting sysroot"
+
+ set res [gdbreplay_start $remotelog]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ # Connect to gdbreplay.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbreplay"
+ return
+ }
+
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continue to main"
+
+ gdb_test "info registers" \
+ "Remote 'p' packet reply for register \[^\r\n\]+ is of unexpected length \\(expected $::decimal bytes\\): $value" \
+ "info registers gives an error"
+}
+
+# Create a copy of file SRC into DST. While making the copy look for the
+# 'info registers' command. After the 'info registers' GDB will send a 'p'
+# packet, and the remote will send a reply.
+#
+# Rewrite the reply to the first 'p' packet, replacing the returned register
+# value with NEW_VALUE.
+proc create_modified_remote_logfile { src dst new_value } {
+ set dst_fd [open $dst w]
+ set src_fd [open $src r]
+
+ # State machine: 'init' -> 'info regs' -> 'p pkt' -> 'done'.
+ # 'init' -- before we find the "info registers" command.
+ # 'info regs' -- after seeing the "info registers" command, before
+ # finding the outbound 'p' packet.
+ # 'p pkt' -- after seeing the outbound 'p' packet, looking for the reply
+ # line.
+ # 'done' -- after finding and modifying the reply to the first 'p'
+ # packet.
+ set state "init"
+ while { [gets $src_fd line] != -1 } {
+ if { $state eq "init" } {
+ if { $line eq "c info registers" } {
+ set state "info regs"
+ }
+ } elseif { $state eq "info regs" } {
+ if { [regexp "^w \\\$p\[0-9a-f\]+#" $line] } {
+ set state "p pkt"
+ } else {
+ set state "done"
+ }
+ } elseif { $state eq "p pkt" } {
+ if { [regexp "^r \\\$\[0-9a-f\]+.*#" $line] } {
+ set line "r \$${new_value}"
+ }
+ set state "done"
+ }
+
+ puts $dst_fd $line
+ }
+
+ close $src_fd
+ close $dst_fd
+}
+
+# The replay log is placed in 'replay.log'.
+set remotelog [standard_output_file replay.log]
+
+record_initial_logfile $remotelog
+replay_without_error $remotelog
+
+# Create two modified logfiles. The first replaces a register with a 1 byte
+# value.
+set remotelog_short [standard_output_file replay-short.log]
+set short_value_str 00
+create_modified_remote_logfile $remotelog $remotelog_short $short_value_str
+with_test_prefix "with short register" {
+ replay_with_error $remotelog_short $short_value_str
+}
+
+# The second replaces a register with a 10 byte value.
+set remotelog_long [standard_output_file replay-long.log]
+set long_value_str 00000000000000000000
+create_modified_remote_logfile $remotelog $remotelog_long $long_value_str
+with_test_prefix "with long register" {
+ replay_with_error $remotelog_long $long_value_str
+}
--
2.25.4
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] gdbsupport: update comment on hex2bin
2026-11-09 15:35 ` [PATCH 1/3] gdbsupport: update comment on hex2bin Andrew Burgess
@ 2026-11-09 16:49 ` Tom Tromey
0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-11-09 16:49 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> The comment on hex2bin says that the argument COUNT is the maximum
Andrew> number of characters to convert. I don't think this is correct, COUNT
Andrew> is the maximum number of bytes to convert, with each byte being
Andrew> represented by two characters.
Andrew> The comment already talks about converting "pairs of hex digits", so
Andrew> I've updated the comment to indicate that COUNT is the number of
Andrew> character pairs to convert.
Andrew> There is no functional change after this commit.
Thanks.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets
2026-11-09 15:35 ` [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets Andrew Burgess
@ 2026-11-09 17:16 ` Tom Tromey
0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-11-09 17:16 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> I understand that gdbserver is a real tool, so we should not be
Andrew> overloading it with "testing" features, but supporting the 'p' and 'P'
Andrew> packets doesn't cost that much, and this would allow for greater
Andrew> testing of GDB.
I'm actually more supportive of this approach in general. While
gdbserver is a real tool, I think it's also important for testing that
gdb conforms to the protocol as documented. Features like this play a
role there.
So I think this kind of change is a positive on the whole, and the
biggest issue is to make sure that the defaults are good.
I suppose maybe if there was a very invasive proposal along these lines,
we would want to have a discussion. But I don't really anticipate it.
Andrew> Annoyingly there was one fix needed in GDB itself. The 'g' packet
Andrew> handling in remote_target::send_g_packet (gdb/remote.c) has a rather
Andrew> nasty loop which tries to "resync" with gdbserver by resending the 'g'
Andrew> packet if the response doesn't look right.
I thought I had seen a bug report about this but all I could readily
find is:
https://sourceware.org/bugzilla/show_bug.cgi?id=8318
which is like tangentially related but not really on point.
The docs do say that g/G are required, so maybe it just really hasn't
come up.
Maybe supporting p/P is an optimization and these actually should be
enabled by default? To be clear I don't think that has to be decided in
this series.
Andrew> And compare the results to those of running without the disable-packet
Andrew> option. The only issues I saw were in trace related tests, the
Andrew> function remote_target::get_trace_status relies on sizeof_g_packet to
Andrew> define the trace register block. For a target without 'g' packet
Andrew> support the sizeof_g_packet becomes 0, which means the trace file
Andrew> records a register block size of 0, which breaks all the trace related
Andrew> tests. This is a pre-existing issue, and not something I plan to
Andrew> address in this commit.
I'm not sure how much we care about this but perhaps it's worth a
follow-up bug report. My sense is that tracing isn't really used but of
course it's hard to know for sure.
I'd be comfortable saying that a server that supports tracing also has
to support the g packet.
Andrew> +bool disable_packet_gG;
I think this one can be static.
Anyway this looks good to me. Thank you.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply
2026-11-09 15:35 ` [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply Andrew Burgess
@ 2026-11-09 17:27 ` Tom Tromey
2026-11-23 16:44 ` Andrew Burgess
0 siblings, 1 reply; 8+ messages in thread
From: Tom Tromey @ 2026-11-09 17:27 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> When building the outgoing packet use xsnprintf; this allows for
Andrew> bounds checking as the packet is being written. The chances of the
Andrew> outgoing packet overflowing are pretty slim, but using a bounded write
Andrew> seems better than not, so let's do that.
I suppose for this to assert we'd have to have a packet that's smaller
than a register number and the register value. But I guess we
anticipate that this can't really happen? Or is pathological enough
that we don't care?
Andrew> + /* Otherwise, parse the register value from the hex string. */
Andrew> + struct gdbarch *gdbarch = regcache->arch ();
Andrew> + int reg_size = register_size (gdbarch, reg->regnum);
Andrew> + gdb_byte *regp = (gdb_byte *) alloca (reg_size);
I would prefer it if we didn't add new calls to alloca to the tree.
I think alloca is bad and that generally we will not notice an extra
allocation.
Tom
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply
2026-11-09 17:27 ` Tom Tromey
@ 2026-11-23 16:44 ` Andrew Burgess
0 siblings, 0 replies; 8+ messages in thread
From: Andrew Burgess @ 2026-11-23 16:44 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
Tom Tromey <tom@tromey.com> writes:
>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> When building the outgoing packet use xsnprintf; this allows for
> Andrew> bounds checking as the packet is being written. The chances of the
> Andrew> outgoing packet overflowing are pretty slim, but using a bounded write
> Andrew> seems better than not, so let's do that.
>
> I suppose for this to assert we'd have to have a packet that's smaller
> than a register number and the register value. But I guess we
> anticipate that this can't really happen? Or is pathological enough
> that we don't care?
You are correct that for the xsnprintf to fail we'd need an absolutely
tiny packet buffer. I don't think we'd make it as far as sending a p
packet in that case, things like qSupported would have gone wrong before
we got here.
But also, I'm switching to xsnprintf because it's better than just
pushing content blindly into the buffer, so we really should check the
return value, it doesn't cost much.
I initially added something like:
int res = xsnprintf (...);
if (res < 0 && res >= get_remote_packet_size ())
error (_("..."));
But then I started writing a comment, and the comment was along this
lines of: this error will never trigger because ....
And we already have a construct for use when something will never
happen, gdb_assert. So then I changed the code to:
int res = xsnprintf (...);
gdb_assert (res > 0 && res < get_remote_packet_size ());
There's also a comment explaining why I used an assert rather than an if
and error.
>
> Andrew> + /* Otherwise, parse the register value from the hex string. */
> Andrew> + struct gdbarch *gdbarch = regcache->arch ();
> Andrew> + int reg_size = register_size (gdbarch, reg->regnum);
> Andrew> + gdb_byte *regp = (gdb_byte *) alloca (reg_size);
>
> I would prefer it if we didn't add new calls to alloca to the tree.
> I think alloca is bad and that generally we will not notice an extra
> allocation.
I switched this to gdb::byte_vector.
Let me know what you think of the updated patch below.
Thanks,
Andrew
---
commit c59492b533149b5ebe20bb0953f10a6179f8c266
Author: Andrew Burgess <aburgess@redhat.com>
Date: Sun Nov 8 20:37:32 2026 +0000
gdb/remote: improve error checking for 'p' packet reply
Improve the remote_target::fetch_register_using_p function (remote.c)
in a couple of ways.
When building the outgoing packet use xsnprintf; this allows for
bounds checking as the packet is being written. The chances of the
outgoing packet overflowing are pretty slim, but using a bounded write
seems better than not, so let's do that.
Next, when parsing the reply packet the result is written into a
buffer which is the size of the register being fetched. Unfortunately
the loop that reads the packet does not have any bounds checking,
which allows for buffer overflow. Convert the manual loop which calls
fromhex on each character to instead use hex2bin which allows a limit
to be applied to the number of bytes parsed; this prevents buffer
overflow. hex2bin returns the number of bytes parsed, which also
allows me to check for cases where the remote fails to supply all of
the expected bytes.
For testing I made use of the gdbserver option --disable-packet=gG
which I added in the previous commit. I ran the testsuite like this:
make check-gdb RUNTESTFLAGS="--target_board=native-gdbserver \
GDBSERVER='/path/to/patched/gdbserver --disable-packet=gG'"
I've also added a new test that makes use of the gdbreplay mechanism
to adjust the size of the returned register. This allows testing of
the two error cases: register value too short, and register value too
long.
As in the previous commit the trace related tests fail as the trace
functionality depends on the 'g' packet being supported, but otherwise
I saw no regressions after this commit.
diff --git a/gdb/remote.c b/gdb/remote.c
index c1560c28e6b..2d264168f59 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9242,27 +9242,28 @@ int
remote_target::fetch_register_using_p (struct regcache *regcache,
packet_reg *reg)
{
- struct gdbarch *gdbarch = regcache->arch ();
- struct remote_state *rs = get_remote_state ();
- char *buf, *p;
- gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
- int i;
-
if (m_features.packet_support (PACKET_p) == PACKET_DISABLE)
return 0;
if (reg->pnum == -1)
return 0;
- p = rs->buf.data ();
- *p++ = 'p';
- p += hexnumstr (p, reg->pnum);
- *p++ = '\0';
+ /* Send the packet asking for the register. */
+ struct remote_state *rs = get_remote_state ();
+ int res = xsnprintf (rs->buf.data (), get_remote_packet_size (), "p%s",
+ phex_nz (reg->pnum, 0));
+
+ /* For writing a string into a buffer xsnprintf isn't going to return an
+ error. Nor can this overflow, a 'p' followed by an int register number
+ will always fit. */
+ gdb_assert (res > 0 && res < get_remote_packet_size ());
+
putpkt (rs->buf);
+
+ /* Read the reply. */
getpkt (&rs->buf);
- buf = rs->buf.data ();
-
+ /* Check the packet status. */
packet_result result = m_features.packet_ok (rs->buf, PACKET_p);
switch (result.status ())
{
@@ -9277,24 +9278,28 @@ remote_target::fetch_register_using_p (struct regcache *regcache,
}
/* If this register is unfetchable, tell the regcache. */
+ char *buf = rs->buf.data ();
if (buf[0] == 'x')
{
regcache->raw_supply (reg->regnum, NULL);
return 1;
}
- /* Otherwise, parse and supply the value. */
- p = buf;
- i = 0;
- while (p[0] != 0)
- {
- if (p[1] == 0)
- error (_("fetch_register_using_p: early buf termination"));
+ /* Otherwise, parse the register value from the hex string. */
+ struct gdbarch *gdbarch = regcache->arch ();
+ int reg_size = register_size (gdbarch, reg->regnum);
+ gdb::byte_vector regp (reg_size);
+ int bytes_converted = hex2bin (buf, regp.data (), reg_size);
- regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
- p += 2;
- }
- regcache->raw_supply (reg->regnum, regp);
+ /* Check the whole packet was parsed, and that the packet length matches
+ the register size. */
+ if (buf[bytes_converted * 2] != '\0' || bytes_converted != reg_size)
+ error (_("Remote 'p' packet reply for register %s is of unexpected "
+ "length (expected %d bytes): %s"),
+ gdbarch_register_name (gdbarch, reg->regnum), reg_size, buf);
+
+ /* Supply the value to the register cache. */
+ regcache->raw_supply (reg->regnum, regp.data ());
return 1;
}
diff --git a/gdb/testsuite/gdb.replay/p-packet.c b/gdb/testsuite/gdb.replay/p-packet.c
new file mode 100644
index 00000000000..6a0e311ef41
--- /dev/null
+++ b/gdb/testsuite/gdb.replay/p-packet.c
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2026 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/>. */
+
+int
+main (void)
+{
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.replay/p-packet.exp b/gdb/testsuite/gdb.replay/p-packet.exp
new file mode 100644
index 00000000000..f59fc13faa2
--- /dev/null
+++ b/gdb/testsuite/gdb.replay/p-packet.exp
@@ -0,0 +1,213 @@
+# Copyright 2026 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 error handling for the 'p' packet. Start gdbserver with the g/G
+# packets disabled. Start GDB, connect to the server, and then run 'info
+# registers', this will make use of the 'p' packet.
+#
+# Update the recorded logfile to replace the reply to the first 'p' packet,
+# the updated reply will be an invalid size.
+#
+# Replay the log and check that the 'info registers' gives a suitable error
+# message.
+
+load_lib gdbserver-support.exp
+load_lib gdbreplay-support.exp
+
+require allow_gdbserver_tests
+require has_gdbreplay
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+ return
+}
+
+# Check that remote register 0 is a suitable size. The test could be
+# updated to handle other register sizes, but for now handle 4 or 8 byte
+# registers.
+set reg_size 0
+gdb_test_multiple "maint print remote-registers" "" -lbl {
+ -re "\r\n\\S+\\s+$::decimal\\s+$::decimal\\s+$::decimal\\s+($::decimal)\\s+\[^\r\n\]+\\s+0\\s+0\\s*(?=\r\n)" {
+ set reg_size $expect_out(1,string)
+ exp_continue
+ }
+
+ -re "\r\n$gdb_prompt $" {
+ pass $gdb_test_name
+ }
+}
+
+if { $reg_size != 4 && $reg_size != 8 } {
+ unsupported "remote register 0 is an unsupported size"
+ return
+}
+
+# Connect to gdbserver and run to a breakpoint in main, then use the 'info
+# registers' command. Record the remotelogfile into the REMOTELOG log
+# file.
+proc_with_prefix record_initial_logfile { remotelog } {
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" \
+ "setting sysroot before starting gdbserver"
+
+ # Start gdbserver like:
+ # gdbserver :PORT ....
+ set res [gdbserver_start "--disable-packet=gG" $::binfile]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ gdb_test_no_output "set remotelogfile $remotelog" \
+ "setup remotelogfile"
+
+ # Connect to gdbserver.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbserver"
+ return
+ }
+
+ # If we're connecting as 'remote' then we can't use 'runto'.
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continuing to main"
+
+ gdb_test "info registers" ".*"
+}
+
+# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in
+# main and then uses the 'info registers' command.
+proc_with_prefix replay_without_error { remotelog } {
+ clean_restart $::testfile
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" "setting sysroot"
+
+ set res [gdbreplay_start $remotelog]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ # Connect to gdbreplay.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbreplay"
+ return
+ }
+
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continue to main"
+
+ gdb_test "info registers" ".*"
+}
+
+# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in
+# main and then uses the 'info registers' command. Expect an error from the
+# 'info registers' command, the error will include the VALUE string.
+proc_with_prefix replay_with_error { remotelog value } {
+ clean_restart $::testfile
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set sysroot" "setting sysroot"
+
+ set res [gdbreplay_start $remotelog]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+
+ # Connect to gdbreplay.
+ if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} {
+ unsupported "couldn't start gdbreplay"
+ return
+ }
+
+ gdb_breakpoint main
+ gdb_continue_to_breakpoint "continue to main"
+
+ gdb_test "info registers" \
+ "Remote 'p' packet reply for register \[^\r\n\]+ is of unexpected length \\(expected $::decimal bytes\\): $value" \
+ "info registers gives an error"
+}
+
+# Create a copy of file SRC into DST. While making the copy look for the
+# 'info registers' command. After the 'info registers' GDB will send a 'p'
+# packet, and the remote will send a reply.
+#
+# Rewrite the reply to the first 'p' packet, replacing the returned register
+# value with NEW_VALUE.
+proc create_modified_remote_logfile { src dst new_value } {
+ set dst_fd [open $dst w]
+ set src_fd [open $src r]
+
+ # State machine: 'init' -> 'info regs' -> 'p pkt' -> 'done'.
+ # 'init' -- before we find the "info registers" command.
+ # 'info regs' -- after seeing the "info registers" command, before
+ # finding the outbound 'p' packet.
+ # 'p pkt' -- after seeing the outbound 'p' packet, looking for the reply
+ # line.
+ # 'done' -- after finding and modifying the reply to the first 'p'
+ # packet.
+ set state "init"
+ while { [gets $src_fd line] != -1 } {
+ if { $state eq "init" } {
+ if { $line eq "c info registers" } {
+ set state "info regs"
+ }
+ } elseif { $state eq "info regs" } {
+ if { [regexp "^w \\\$p\[0-9a-f\]+#" $line] } {
+ set state "p pkt"
+ } else {
+ set state "done"
+ }
+ } elseif { $state eq "p pkt" } {
+ if { [regexp "^r \\\$\[0-9a-f\]+.*#" $line] } {
+ set line "r \$${new_value}"
+ }
+ set state "done"
+ }
+
+ puts $dst_fd $line
+ }
+
+ close $src_fd
+ close $dst_fd
+}
+
+# The replay log is placed in 'replay.log'.
+set remotelog [standard_output_file replay.log]
+
+record_initial_logfile $remotelog
+replay_without_error $remotelog
+
+# Create two modified logfiles. The first replaces a register with a 1 byte
+# value.
+set remotelog_short [standard_output_file replay-short.log]
+set short_value_str 00
+create_modified_remote_logfile $remotelog $remotelog_short $short_value_str
+with_test_prefix "with short register" {
+ replay_with_error $remotelog_short $short_value_str
+}
+
+# The second replaces a register with a 10 byte value.
+set remotelog_long [standard_output_file replay-long.log]
+set long_value_str 00000000000000000000
+create_modified_remote_logfile $remotelog $remotelog_long $long_value_str
+with_test_prefix "with long register" {
+ replay_with_error $remotelog_long $long_value_str
+}
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-23 16:45 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 15:35 [PATCH 0/3] gdb/remote: improve packet buffer checks for p packet Andrew Burgess
2026-11-09 15:35 ` [PATCH 1/3] gdbsupport: update comment on hex2bin Andrew Burgess
2026-11-09 16:49 ` Tom Tromey
2026-11-09 15:35 ` [PATCH 2/3] gdbserver: add support for p/P packets and flag to disable g/G packets Andrew Burgess
2026-11-09 17:16 ` Tom Tromey
2026-11-09 15:35 ` [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply Andrew Burgess
2026-11-09 17:27 ` Tom Tromey
2026-11-23 16:44 ` Andrew Burgess
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox