From 3312ab2087410fcc00d8ae17a68fa7563e0eec6e Mon Sep 17 00:00:00 2001 From: anjiahao Date: Mon, 25 Mar 2024 11:27:38 +0800 Subject: [PATCH] gdbstub:fix get thread rsp command crash riscv-gdb/gdb/thread.c:1309: internal-error: void switch_to_thread(thread_info*): Assertion `thr != NULL' failed. Signed-off-by: anjiahao --- libs/libc/gdbstub/lib_gdbstub.c | 47 ++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c index 433017f60c..4fd5c6a1a1 100644 --- a/libs/libc/gdbstub/lib_gdbstub.c +++ b/libs/libc/gdbstub/lib_gdbstub.c @@ -781,6 +781,39 @@ static int gdb_send_ok_packet(FAR struct gdb_state_s *state) return gdb_send_packet(state); } +/**************************************************************************** + * Name: gdb_send_signal_packet + * + * Description: + * Send a signal packet (S AA). + * + * Input Parameters: + * state - The pointer to the GDB state structure. + * signal - The signal to send. + * + * Returned Value: + * Zero on success. + * Negative value on error. + * + ****************************************************************************/ + +static int gdb_send_signal_packet(FAR struct gdb_state_s *state, + unsigned char signal) +{ + int ret; + + state->pkt_buf[0] = 'S'; + ret = gdb_bin2hex(&state->pkt_buf[1], sizeof(state->pkt_buf) - 1, + &signal, 1); + if (ret < 0) + { + return ret; + } + + state->pkt_len = 1 + ret; + return gdb_send_packet(state); +} + /**************************************************************************** * Name: gdb_send_error_packet * @@ -1316,6 +1349,7 @@ static int gdb_is_thread_active(FAR struct gdb_state_s *state) * Negative value on error. * * Note : Comand Format: Hg + * Hc- * Rsponse Format: OK ****************************************************************************/ @@ -1325,12 +1359,19 @@ static int gdb_thread_context(FAR struct gdb_state_s *state) uintptr_t pid; int ret; - if (state->pkt_buf[1] != 'g') + if (state->pkt_buf[1] == 'g') + { + state->pkt_next += 2; + } + else if (state->pkt_buf[1] == 'c') + { + state->pkt_next += 3; + } + else { return -EINVAL; } - state->pkt_next += 2; ret = gdb_expect_integer(state, &pid); if (ret < 0) { @@ -1756,7 +1797,7 @@ int gdb_process(FAR struct gdb_state_s *state, int stopreason, switch (state->pkt_buf[0]) { case '?': /* gdbserial status */ - ret = gdb_send_stop(state, stopreason, stopaddr); + ret = gdb_send_signal_packet(state, 0x00); break; case 'g': /* Read registers */ ret = gdb_read_registers(state);