From ea51f1ffdacd5d2c2956e352a5f66224c587d432 Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Mon, 19 Feb 2024 09:07:53 +0100 Subject: [PATCH] monitor: limit the 'm' command response size Fixes #5119 --- repos/os/src/monitor/gdb_stub.h | 29 +++++++++++++---------------- repos/os/src/test/monitor/main.cc | 9 +++++---- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/repos/os/src/monitor/gdb_stub.h b/repos/os/src/monitor/gdb_stub.h index 99f831f6b0..425b174c5c 100644 --- a/repos/os/src/monitor/gdb_stub.h +++ b/repos/os/src/monitor/gdb_stub.h @@ -642,25 +642,22 @@ struct m : Command_without_separator gdb_response(out, [&] (Output &out) { - for (size_t pos = 0; pos < len; ) { + /* + * The terminal_crosslink component uses a buffer of 4 KiB and + * some space is needed for asynchronous notifications and + * protocol overhead. GDB's 'm' command encodes memory as hex, + * two characters per byte. Hence, a dump of max. 1 KiB is + * currently possible. + */ + char buf[1024] { }; - char chunk[16*1024] { }; + Byte_range_ptr const dst { buf, min(sizeof(buf), len) }; - size_t const remain_len = len - pos; - size_t const num_bytes = min(sizeof(chunk), remain_len); + size_t const read_len = + state.read_memory(Memory_accessor::Virt_addr { addr }, dst); - size_t const read_len = - state.read_memory(Memory_accessor::Virt_addr { addr + pos }, - Byte_range_ptr(chunk, num_bytes)); - - for (unsigned i = 0; i < read_len; i++) - print(out, Gdb_hex(chunk[i])); - - pos += read_len; - - if (read_len < num_bytes) - break; - } + for (unsigned i = 0; i < read_len; i++) + print(out, Gdb_hex(buf[i])); }); } }; diff --git a/repos/os/src/test/monitor/main.cc b/repos/os/src/test/monitor/main.cc index ee88b57d58..ad4b550f31 100644 --- a/repos/os/src/test/monitor/main.cc +++ b/repos/os/src/test/monitor/main.cc @@ -124,16 +124,17 @@ struct Test::Main /* * Dimensioning of the buffer for one round trip: * - * The terminal_crosslink component uses a buffer of 4 KiB. + * The terminal_crosslink component uses a buffer of 4 KiB and + * the debug monitor limits the 'm' command response to 2 KiB to leave + * enough space for asynchronous notifications and protocol overhead. * GDB's 'm' command encodes memory as hex, two characters per byte. - * Hence, a dump of max. 2 KiB fits into the terminal-crosslink buffer. - * The GDB command, packet header, and checksum also take a few bytes. + * Hence, a dump of max. 1 KiB is currently possible. * * The most effective way to optimize the throughput would be to * increase the terminal-crosslink's buffer size, reducing the number * of round trips. */ - char buffer[2*1024 - 16] { }; + char buffer[1024] { }; uint64_t const start_us = timer.elapsed_us(); uint64_t now_us = start_us;