qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Damien Hedde" <damien.hedde@greensocs.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	qemu-arm@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>
Subject: [PATCH v5 04/22] gdbstub: move mem_buf to GDBState and use GByteArray
Date: Tue, 14 Jan 2020 15:09:35 +0000	[thread overview]
Message-ID: <20200114150953.27659-5-alex.bennee@linaro.org> (raw)
In-Reply-To: <20200114150953.27659-1-alex.bennee@linaro.org>

This is in preparation for further re-factoring of the register API
with the rest of the code. Theoretically the read register function
could overwrite the MAX_PACKET_LENGTH buffer although currently all
registers are well within the size range.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Damien Hedde <damien.hedde@greensocs.com>
Tested-by: Damien Hedde <damien.hedde@greensocs.com>

---
v4
  - don't set_set to len on rcmd (it's zeroed before we get here)
v3
  - fixed up email on Damien's tags
---
 gdbstub.c | 56 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 21 deletions(-)

diff --git a/gdbstub.c b/gdbstub.c
index b6dfeac5d1..49abd2bdc7 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -367,6 +367,7 @@ typedef struct GDBState {
     char syscall_buf[256];
     gdb_syscall_complete_cb current_syscall_cb;
     GString *str_buf;
+    GByteArray *mem_buf;
 } GDBState;
 
 /* By default use no IRQs and no timers while single stepping so as to
@@ -382,6 +383,7 @@ static void init_gdbserver_state(void)
     memset(&gdbserver_state, 0, sizeof(GDBState));
     gdbserver_state.init = true;
     gdbserver_state.str_buf = g_string_new(NULL);
+    gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
 }
 
 #ifndef CONFIG_USER_ONLY
@@ -576,12 +578,13 @@ static void memtohex(GString *buf, const uint8_t *mem, int len)
     g_string_append_c(buf, '\0');
 }
 
-static void hextomem(uint8_t *mem, const char *buf, int len)
+static void hextomem(GByteArray *mem, const char *buf, int len)
 {
     int i;
 
     for(i = 0; i < len; i++) {
-        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
+        guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
+        g_byte_array_append(mem, &byte, 1);
         buf += 2;
     }
 }
@@ -1412,7 +1415,6 @@ static int cmd_parse_params(const char *data, const char *schema,
 typedef struct GdbCmdContext {
     GdbCmdVariant *params;
     int num_params;
-    uint8_t mem_buf[MAX_PACKET_LENGTH];
 } GdbCmdContext;
 
 typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
@@ -1503,6 +1505,7 @@ static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
     }
 
     g_string_set_size(gdbserver_state.str_buf, 0);
+    g_byte_array_set_size(gdbserver_state.mem_buf, 0);
 
     /* In case there was an error during the command parsing we must
     * send a NULL packet to indicate the command is not supported */
@@ -1715,8 +1718,8 @@ static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 
     reg_size = strlen(gdb_ctx->params[1].data) / 2;
-    hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
-    gdb_write_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
+    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[1].data, reg_size);
+    gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
                        gdb_ctx->params[0].val_ull);
     put_packet("OK");
 }
@@ -1735,14 +1738,17 @@ static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    reg_size = gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
+    reg_size = gdb_read_register(gdbserver_state.g_cpu,
+                                 gdbserver_state.mem_buf->data,
                                  gdb_ctx->params[0].val_ull);
     if (!reg_size) {
         put_packet("E14");
         return;
+    } else {
+        g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
     }
 
-    memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, reg_size);
+    memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
     put_strbuf();
 }
 
@@ -1759,11 +1765,11 @@ static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
+    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[2].data,
              gdb_ctx->params[1].val_ull);
     if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
-                               gdb_ctx->mem_buf,
-                               gdb_ctx->params[1].val_ull, true)) {
+                               gdbserver_state.mem_buf->data,
+                               gdbserver_state.mem_buf->len, true)) {
         put_packet("E14");
         return;
     }
@@ -1784,14 +1790,17 @@ static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
+    g_byte_array_set_size(gdbserver_state.mem_buf, gdb_ctx->params[1].val_ull);
+
     if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
-                               gdb_ctx->mem_buf,
-                               gdb_ctx->params[1].val_ull, false)) {
+                               gdbserver_state.mem_buf->data,
+                               gdbserver_state.mem_buf->len, false)) {
         put_packet("E14");
         return;
     }
 
-    memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
+    memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
+             gdbserver_state.mem_buf->len);
     put_strbuf();
 }
 
@@ -1806,9 +1815,9 @@ static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 
     cpu_synchronize_state(gdbserver_state.g_cpu);
-    registers = gdb_ctx->mem_buf;
     len = strlen(gdb_ctx->params[0].data) / 2;
-    hextomem(registers, gdb_ctx->params[0].data, len);
+    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
+    registers = gdbserver_state.mem_buf->data;
     for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
          addr++) {
         reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
@@ -1825,11 +1834,14 @@ static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     cpu_synchronize_state(gdbserver_state.g_cpu);
     len = 0;
     for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
-        len += gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf + len,
+        len += gdb_read_register(gdbserver_state.g_cpu,
+                                 gdbserver_state.mem_buf->data + len,
                                  addr);
     }
+    /* FIXME: This is after the fact sizing */
+    g_byte_array_set_size(gdbserver_state.mem_buf, len);
 
-    memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, len);
+    memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
     put_strbuf();
 }
 
@@ -2078,6 +2090,7 @@ static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
 #else
 static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
 {
+    const guint8 zero = 0;
     int len;
 
     if (!gdb_ctx->num_params) {
@@ -2091,12 +2104,13 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
+    g_assert(gdbserver_state.mem_buf->len == 0);
     len = len / 2;
-    hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
-    gdb_ctx->mem_buf[len++] = 0;
-    qemu_chr_be_write(gdbserver_state.mon_chr, gdb_ctx->mem_buf, len);
+    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
+    g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
+    qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
+                      gdbserver_state.mem_buf->len);
     put_packet("OK");
-
 }
 #endif
 
-- 
2.20.1



  parent reply	other threads:[~2020-01-14 15:15 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-14 15:09 [PATCH v5 00/22] gdbstub refactor and SVE support (+check-tcg tweaks) Alex Bennée
2020-01-14 15:09 ` [PATCH v5 01/22] gdbstub: make GDBState static and have common init function Alex Bennée
2020-01-14 15:09 ` [PATCH v5 02/22] gdbstub: stop passing GDBState * around and use global Alex Bennée
2020-01-16 10:05   ` Damien Hedde
2020-01-16 15:07   ` Philippe Mathieu-Daudé
2020-01-14 15:09 ` [PATCH v5 03/22] gdbstub: move str_buf to GDBState and use GString Alex Bennée
2020-01-14 15:09 ` Alex Bennée [this message]
2020-01-14 15:09 ` [PATCH v5 05/22] gdbstub: add helper for 128 bit registers Alex Bennée
2020-01-14 15:09 ` [PATCH v5 06/22] target/arm: use gdb_get_reg helpers Alex Bennée
2020-01-14 15:09 ` [PATCH v5 07/22] target/m68k: " Alex Bennée
2020-01-14 15:09 ` [PATCH v5 08/22] gdbstub: extend GByteArray to read register helpers Alex Bennée
2020-01-15  5:53   ` David Gibson
2020-01-16  9:58   ` Damien Hedde
2020-01-14 15:09 ` [PATCH v5 09/22] target/arm: prepare for multiple dynamic XMLs Alex Bennée
2020-01-15 22:21   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 10/22] target/arm: explicitly encode regnum in our XML Alex Bennée
2020-01-14 15:09 ` [PATCH v5 11/22] target/arm: default SVE length to 64 bytes for linux-user Alex Bennée
2020-01-14 15:09 ` [PATCH v5 12/22] target/arm: generate xml description of our SVE registers Alex Bennée
2020-01-15 22:16   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 13/22] tests/tcg: add a configure compiler check for ARMv8.1 and SVE Alex Bennée
2020-01-15 22:24   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 14/22] target/arm: don't bother with id_aa64pfr0_read for USER_ONLY Alex Bennée
2020-01-14 15:09 ` [PATCH v5 15/22] tests/tcg/aarch64: userspace system register test Alex Bennée
2020-01-15 22:30   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 16/22] configure: allow user to specify what gdb to use Alex Bennée
2020-01-15 22:31   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 17/22] tests/guest-debug: add a simple test runner Alex Bennée
2020-01-15 22:40   ` Richard Henderson
2020-01-15 22:43   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 18/22] tests/tcg/aarch64: add a gdbstub testcase for SVE registers Alex Bennée
2020-01-15 22:54   ` Richard Henderson
2020-01-16  7:36     ` Alex Bennée
2020-01-14 15:09 ` [PATCH v5 19/22] tests/tcg/aarch64: add SVE iotcl test Alex Bennée
2020-01-15 22:46   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 20/22] tests/tcg/aarch64: add test-sve-ioctl guest-debug test Alex Bennée
2020-01-15 23:07   ` Richard Henderson
2020-02-04 21:45     ` Alex Bennée
2020-02-05 10:19       ` Richard Henderson
2020-02-05 11:49         ` Andrew Jones
2020-01-14 15:09 ` [PATCH v5 21/22] gdbstub: change GDBState.last_packet to GByteArray Alex Bennée
2020-01-15 23:10   ` Richard Henderson
2020-01-14 15:09 ` [PATCH v5 22/22] gdbstub: do not split gdb_monitor_write payload Alex Bennée
2020-01-15 23:11   ` Richard Henderson
2020-01-14 18:57 ` [PATCH v5 00/22] gdbstub refactor and SVE support (+check-tcg tweaks) no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200114150953.27659-5-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=damien.hedde@greensocs.com \
    --cc=philmd@redhat.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).