All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: damien.hedde@greensocs.com, luis.machado@linaro.org,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	richard.henderson@linaro.org, alan.hayward@arm.com,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: [RFC PATCH 04/11] gdbstub: move mem_buf to GDBState and use GByteArray
Date: Fri, 15 Nov 2019 17:29:53 +0000	[thread overview]
Message-ID: <20191115173000.21891-5-alex.bennee@linaro.org> (raw)
In-Reply-To: <20191115173000.21891-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>
---
 gdbstub.c | 52 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/gdbstub.c b/gdbstub.c
index 528404c1953..4c3e211890f 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -366,6 +366,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
@@ -380,6 +381,7 @@ static GDBState *gdb_allocate_state(void)
     g_assert(!gdbserver_state);
     gdbserver_state = g_new0(GDBState, 1);
     gdbserver_state->str_buf = g_string_new(NULL);
+    gdbserver_state->mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
     return gdbserver_state;
 }
 
@@ -566,12 +568,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;
     }
 }
@@ -1396,7 +1399,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);
@@ -1489,6 +1491,7 @@ static void run_cmd_parser(GDBState *s, const char *data,
     }
 
     g_string_set_size(s->str_buf, 0);
+    g_byte_array_set_size(s->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 */
@@ -1710,8 +1713,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(s->g_cpu, gdb_ctx->mem_buf,
+    hextomem(s->mem_buf, gdb_ctx->params[1].data, reg_size);
+    gdb_write_register(s->g_cpu, s->mem_buf->data,
                        gdb_ctx->params[0].val_ull);
     put_packet(s, "OK");
 }
@@ -1731,14 +1734,16 @@ static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    reg_size = gdb_read_register(s->g_cpu, gdb_ctx->mem_buf,
+    reg_size = gdb_read_register(s->g_cpu, s->mem_buf->data,
                                  gdb_ctx->params[0].val_ull);
     if (!reg_size) {
         put_packet(s, "E14");
         return;
+    } else {
+        g_byte_array_set_size(s->mem_buf, reg_size);
     }
 
-    memtohex(s->str_buf, gdb_ctx->mem_buf, reg_size);
+    memtohex(s->str_buf, s->mem_buf->data, reg_size);
     put_packet(s, s->str_buf->str);
 }
 
@@ -1757,11 +1762,11 @@ static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
+    hextomem(s->mem_buf, gdb_ctx->params[2].data,
              gdb_ctx->params[1].val_ull);
     if (target_memory_rw_debug(s->g_cpu, gdb_ctx->params[0].val_ull,
-                               gdb_ctx->mem_buf,
-                               gdb_ctx->params[1].val_ull, true)) {
+                               s->mem_buf->data,
+                               s->mem_buf->len, true)) {
         put_packet(s, "E14");
         return;
     }
@@ -1784,14 +1789,16 @@ static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
+    g_byte_array_set_size(s->mem_buf, gdb_ctx->params[1].val_ull);
+
     if (target_memory_rw_debug(s->g_cpu, gdb_ctx->params[0].val_ull,
-                               gdb_ctx->mem_buf,
-                               gdb_ctx->params[1].val_ull, false)) {
+                               s->mem_buf->data,
+                               s->mem_buf->len, false)) {
         put_packet(s, "E14");
         return;
     }
 
-    memtohex(s->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
+    memtohex(s->str_buf, s->mem_buf->data, s->mem_buf->len);
     put_packet(s, s->str_buf->str);
 }
 
@@ -1807,9 +1814,9 @@ static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 
     cpu_synchronize_state(s->g_cpu);
-    registers = gdb_ctx->mem_buf;
     len = strlen(gdb_ctx->params[0].data) / 2;
-    hextomem(registers, gdb_ctx->params[0].data, len);
+    hextomem(s->mem_buf, gdb_ctx->params[0].data, len);
+    registers = s->mem_buf->data;
     for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0;
          addr++) {
         reg_size = gdb_write_register(s->g_cpu, registers, addr);
@@ -1827,11 +1834,13 @@ static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     cpu_synchronize_state(s->g_cpu);
     len = 0;
     for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
-        len += gdb_read_register(s->g_cpu, gdb_ctx->mem_buf + len,
+        len += gdb_read_register(s->g_cpu, s->mem_buf->data + len,
                                  addr);
     }
+    /* FIXME: This is after the fact sizing */
+    g_byte_array_set_size(s->mem_buf, len);
 
-    memtohex(s->str_buf, gdb_ctx->mem_buf, len);
+    memtohex(s->str_buf, s->mem_buf->data, len);
     put_packet(s, s->str_buf->str);
 }
 
@@ -2102,6 +2111,7 @@ static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
 static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
 {
     GDBState *s = gdbserver_state;
+    const guint8 zero = 0;
     int len;
 
     if (!gdb_ctx->num_params) {
@@ -2116,11 +2126,11 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 
     len = len / 2;
-    hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
-    gdb_ctx->mem_buf[len++] = 0;
-    qemu_chr_be_write(s->mon_chr, gdb_ctx->mem_buf, len);
+    g_byte_array_set_size(s->mem_buf, len);
+    hextomem(s->mem_buf, gdb_ctx->params[0].data, len);
+    g_byte_array_append(s->mem_buf, &zero, 1);
+    qemu_chr_be_write(s->mon_chr, s->mem_buf->data, s->mem_buf->len);
     put_packet(s, "OK");
-
 }
 #endif
 
-- 
2.20.1



  parent reply	other threads:[~2019-11-15 17:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-15 17:29 [RFC PATCH 00/11] gdbstub re-factor and SVE support Alex Bennée
2019-11-15 17:29 ` [RFC PATCH 01/11] gdbstub: move allocation of GDBState to one place Alex Bennée
2019-11-18  7:37   ` Richard Henderson
2019-11-18  7:41   ` Richard Henderson
2019-11-18  9:19     ` Damien Hedde
2019-11-18 11:24       ` Richard Henderson
2019-11-18  9:50   ` Damien Hedde
2019-11-15 17:29 ` [RFC PATCH 02/11] gdbstub: stop passing GDBState * around Alex Bennée
2019-11-18  7:47   ` Richard Henderson
2019-11-18 11:52     ` Alex Bennée
2019-11-18 11:57       ` Richard Henderson
2019-11-18  9:40   ` Damien Hedde
2019-11-18 11:59     ` Alex Bennée
2019-11-15 17:29 ` [RFC PATCH 03/11] gdbstub: move str_buf to GDBState and use GString Alex Bennée
2019-11-18  8:06   ` Richard Henderson
2019-11-15 17:29 ` Alex Bennée [this message]
2019-11-18  8:10   ` [RFC PATCH 04/11] gdbstub: move mem_buf to GDBState and use GByteArray Richard Henderson
2019-11-15 17:29 ` [RFC PATCH 05/11] gdbstub: add helper for 128 bit registers Alex Bennée
2019-11-18  8:13   ` Richard Henderson
2019-11-15 17:29 ` [RFC PATCH 06/11] target/arm: use gdb_get_reg helpers Alex Bennée
2019-11-18  8:19   ` Richard Henderson
2019-11-15 17:29 ` [RFC PATCH 07/11] target/m68k: " Alex Bennée
2019-11-18  8:21   ` Richard Henderson
2019-11-15 17:29 ` [RFC PATCH 08/11] gdbstub: extend GByteArray to read register helpers Alex Bennée
2019-11-15 17:29   ` Alex Bennée
2019-11-18  8:41   ` Richard Henderson
2019-11-18  8:41     ` Richard Henderson
2019-11-15 17:29 ` [RFC PATCH 09/11] target/arm: prepare for multiple dynamic XMLs Alex Bennée
2019-11-15 17:29 ` [RFC PATCH 10/11] target/arm: explicitly encode regnum in our XML Alex Bennée
2019-11-18  8:43   ` Richard Henderson
2019-11-15 17:30 ` [RFC PATCH 11/11] target/arm: generate xml description of our SVE registers Alex Bennée
2019-11-18  8:46   ` Richard Henderson

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=20191115173000.21891-5-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=alan.hayward@arm.com \
    --cc=damien.hedde@greensocs.com \
    --cc=luis.machado@linaro.org \
    --cc=philmd@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.