This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 92f4d52ffb6904062cbd940653313a9de6a1fd51
Author: xuxingliang <[email protected]>
AuthorDate: Mon Jan 27 14:33:26 2025 +0800

    gdbstub: fix register write
    
    Writing registers did not work:
    
    1. gdb_write_registers() passed sizeof(state->registers) as the size of
       the destination.  registers is a zero-length array, so the size is 0
       and every G packet failed.
    2. gdb_read_registers() and gdb_read_register() reloaded the registers
       from the TCB on every read, so a value written with G or P was lost
       on the next read.
    
    The stub cannot change the registers of a thread in place, so keep a
    cache of the registers of the selected thread instead: it is loaded
    when GDB selects a thread with Hg/Hc, and G and P change only the
    cache.  This lets the user set $pc, $sp or $lr to unwind from another
    context without touching the thread.
    
    Signed-off-by: xuxingliang <[email protected]>
---
 libs/libc/gdbstub/lib_gdbstub.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c
index 0eda7d41699..fff985318cc 100644
--- a/libs/libc/gdbstub/lib_gdbstub.c
+++ b/libs/libc/gdbstub/lib_gdbstub.c
@@ -1002,7 +1002,7 @@ static int gdb_send_error_packet(FAR struct gdb_state_s 
*state,
  ****************************************************************************/
 
 /****************************************************************************
- * Name: gdb_get_registers
+ * Name: gdb_update_regcache
  *
  * Description:
  *   Get the registers of the specified task.
@@ -1011,7 +1011,7 @@ static int gdb_send_error_packet(FAR struct gdb_state_s 
*state,
  *   state   - The pointer to the GDB state structure.
  ****************************************************************************/
 
-static void gdb_get_registers(FAR struct gdb_state_s *state)
+static void gdb_update_regcache(FAR struct gdb_state_s *state)
 {
   FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
   FAR uint8_t *reg;
@@ -1066,7 +1066,6 @@ static int gdb_read_registers(FAR struct gdb_state_s 
*state)
 {
   int ret;
 
-  gdb_get_registers(state);
   ret = gdb_bin2hex(state->pkt_buf, sizeof(state->pkt_buf),
                     state->registers, state->size);
   if (ret < 0)
@@ -1102,7 +1101,7 @@ static int gdb_write_registers(FAR struct gdb_state_s 
*state)
 {
   int ret;
 
-  ret = gdb_hex2bin(state->registers, sizeof(state->registers),
+  ret = gdb_hex2bin(state->registers, state->size,
                     state->pkt_buf + 1, state->pkt_len - 1);
   if (ret < 0)
     {
@@ -1114,10 +1113,10 @@ static int gdb_write_registers(FAR struct gdb_state_s 
*state)
 }
 
 /****************************************************************************
- * Name: gdb_write_register
+ * Name: gdb_read_register
  *
  * Description:
- *   Write a Register Command Format: P n.
+ *   Read a Register Command Format: p n.
  *
  * Input Parameters:
  *   state   - The pointer to the GDB state structure.
@@ -1142,7 +1141,6 @@ static int gdb_read_register(FAR struct gdb_state_s 
*state)
       return ret;
     }
 
-  gdb_get_registers(state);
   if (addr >= state->size)
     {
       return 0;
@@ -1639,6 +1637,8 @@ static int gdb_thread_context(FAR struct gdb_state_s 
*state)
       state->pid = pid;
     }
 
+  gdb_update_regcache(state);
+
   gdb_send_ok_packet(state);
   return 0;
 }

Reply via email to