diff mbox series

[1/2] gdbstub: Add Xfer:siginfo:read stub

Message ID 20240303192610.498490-1-gustavo.romero@linaro.org
State New
Headers show
Series [1/2] gdbstub: Add Xfer:siginfo:read stub | expand

Commit Message

Gustavo Romero March 3, 2024, 7:26 p.m. UTC
Add stub to handle Xfer:siginfo:read query that requests the machine's
siginfo data.

This is used when GDB users execute 'print $_siginfo' and when the
machine stops due to a signal, like on a SIGSEGV. The information in
siginfo allows GDB to determine further details on the signal, like the
fault address/insn when the SIGSEGV is caught. The siginfo is also used
by GDB to find out the si_code automatically and show additional info to
the user in some cases.

This is only a QEMU user mode and Linux-only feature.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
---
 gdbstub/gdbstub.c     |  9 +++++++++
 gdbstub/internals.h   |  1 +
 gdbstub/user-target.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)

Comments

Richard Henderson March 4, 2024, 5:18 p.m. UTC | #1
On 3/3/24 09:26, Gustavo Romero wrote:
> +    /* Filter out si_type from si_code. See comment in siginfo_noswap(). */ > +    tmp_siginfo = ts->sync_signal.info;
> +    tmp_siginfo.si_code = sextract32(tmp_siginfo.si_code, 0, 16);


This is incorrect, as it only handles synchronous signals.

In handle_pending_signal(), struct emulated_sigtable is passed, which has the correct 
siginfo (all of it, so no need for the adjustment).  I think you need to pass that in to 
gdb_handlesig so that a copy can be made for later xfer.


r~
Gustavo Romero March 7, 2024, 5:51 p.m. UTC | #2
On 3/4/24 2:18 PM, Richard Henderson wrote:
> On 3/3/24 09:26, Gustavo Romero wrote:
>> +    /* Filter out si_type from si_code. See comment in siginfo_noswap(). */ > +    tmp_siginfo = ts->sync_signal.info;
>> +    tmp_siginfo.si_code = sextract32(tmp_siginfo.si_code, 0, 16);
> 
> 
> This is incorrect, as it only handles synchronous signals.
> 
> In handle_pending_signal(), struct emulated_sigtable is passed, which has the correct siginfo (all of it, so no need for the adjustment).  I think you need to pass that in to gdb_handlesig so that a copy can be made for later xfer.

Thanks, I'm sending v2 that fixes it.


Cheers,
Gustavo
diff mbox series

Patch

diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c
index 2909bc8c69..54c1f6fb3c 100644
--- a/gdbstub/gdbstub.c
+++ b/gdbstub/gdbstub.c
@@ -1650,7 +1650,10 @@  static void handle_query_supported(GArray *params, void *user_ctx)
     if (gdbserver_state.c_cpu->opaque) {
         g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
     }
+
     g_string_append(gdbserver_state.str_buf, ";QCatchSyscalls+");
+
+    g_string_append(gdbserver_state.str_buf, ";qXfer:siginfo:read+");
 #endif
     g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
 #endif
@@ -1799,6 +1802,12 @@  static const GdbCmdParseEntry gdb_gen_query_table[] = {
         .cmd_startswith = 1,
         .schema = "l,l0"
     },
+    {
+        .handler = gdb_handle_query_xfer_siginfo,
+        .cmd = "Xfer:siginfo:read::",
+        .cmd_startswith = 1,
+        .schema = "l,l0"
+     },
 #endif
     {
         .handler = gdb_handle_query_xfer_exec_file,
diff --git a/gdbstub/internals.h b/gdbstub/internals.h
index 56b7c13b75..fcfe7c2d26 100644
--- a/gdbstub/internals.h
+++ b/gdbstub/internals.h
@@ -190,6 +190,7 @@  typedef union GdbCmdVariant {
 void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */
 void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
 void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
+void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx); /*user */
 void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
 void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
 void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
diff --git a/gdbstub/user-target.c b/gdbstub/user-target.c
index b7d4c37cd8..3a4cf96622 100644
--- a/gdbstub/user-target.c
+++ b/gdbstub/user-target.c
@@ -284,6 +284,37 @@  void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx)
     gdb_put_packet_binary(gdbserver_state.str_buf->str,
                       gdbserver_state.str_buf->len, true);
 }
+
+void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
+{
+    TaskState *ts;
+    unsigned long offset, len;
+    target_siginfo_t tmp_siginfo;
+    uint8_t *siginfo_offset;
+
+    offset = get_param(params, 0)->val_ul;
+    len = get_param(params, 1)->val_ul;
+
+    if (offset + len > sizeof(target_siginfo_t)) {
+        /* Invalid offset and/or requested length. */
+        gdb_put_packet("E01");
+        return;
+    }
+
+    ts = gdbserver_state.c_cpu->opaque;
+
+    /* Filter out si_type from si_code. See comment in siginfo_noswap(). */
+    tmp_siginfo = ts->sync_signal.info;
+    tmp_siginfo.si_code = sextract32(tmp_siginfo.si_code, 0, 16);
+
+    siginfo_offset = (uint8_t *)&tmp_siginfo + offset;
+
+    /* Reply */
+    g_string_assign(gdbserver_state.str_buf, "l");
+    gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
+    gdb_put_packet_binary(gdbserver_state.str_buf->str,
+                          gdbserver_state.str_buf->len, true);
+}
 #endif
 
 static const char *get_filename_param(GArray *params, int i)