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 eed43407a5c9980cdae455f95960f6a229f283ce
Author: xuxingliang <[email protected]>
AuthorDate: Mon Jan 27 14:39:50 2025 +0800

    gdbstub: support P packet to write single register
    
    GDB writes one register with the P packet ("Pn...=r...") and falls
    back to G, which rewrites the whole register set, only when the stub
    does not support P.  Handle P by storing the value in the register
    buffer of the stub.
    
    The register number is used as an index into the buffer, so this only
    works on architectures whose GDB register numbers follow the order of
    g_tcbinfo.
    
    Signed-off-by: xuxingliang <[email protected]>
---
 libs/libc/gdbstub/lib_gdbstub.c | 58 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c
index 675b584309a..58289fe5c8a 100644
--- a/libs/libc/gdbstub/lib_gdbstub.c
+++ b/libs/libc/gdbstub/lib_gdbstub.c
@@ -1160,6 +1160,61 @@ static int gdb_read_register(FAR struct gdb_state_s 
*state)
   return 0;
 }
 
+/****************************************************************************
+ * Name: gdb_write_register
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *   Note that this won't work for all architectures because the GDB remote
+ *   register number is not always the same as the NuttX register number.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ * Note: Comand Format: Pn.
+ *       Response Format: OK or negative value for error number.
+ ****************************************************************************/
+
+static int gdb_write_register(FAR struct gdb_state_s *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->pkt_next++;
+
+  ret = gdb_expect_integer(state, &addr);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_expect_separator(state, '=');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (addr > state->size / sizeof(uintptr_t) - 1)
+    {
+      return -EINVAL;
+    }
+
+  ret = gdb_hex2bin(&state->registers[addr], sizeof(uintptr_t),
+                    state->pkt_next, gdb_remaining_len(state));
+
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
 /****************************************************************************
  * Name: gdb_read_memory
  *
@@ -2094,6 +2149,9 @@ int gdb_process(FAR struct gdb_state_s *state, int 
stopreason,
           case 'p': /* Read one register */
             ret = gdb_read_register(state);
             break;
+          case 'P': /* Write one register */
+            ret = gdb_write_register(state);
+            break;
           case 'm': /* Read memory */
             ret = gdb_read_memory(state);
             break;

Reply via email to