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 52afbda5292d8847dddc2049c561fc1c5d9b80a2
Author: xuxingliang <[email protected]>
AuthorDate: Fri Jan 17 18:00:44 2025 +0800

    gdbstub: fix Hg/Hc packet parsing
    
    GDB puts the thread ID of the H packet right after "Hg" or "Hc", and
    the ID can be -1 (all threads) or 0 (any thread), e.g. "Hc-1".  The
    parser skipped three characters for Hc as if every Hc packet were
    "Hc-<id>", so "Hc<id>" lost its first digit, and an ID of -1 was looked
    up as a thread and failed.
    
    Skip two characters for both, keep the selected thread for -1 and 0,
    and reload the register cache for the selected thread.
    
    Signed-off-by: xuxingliang <[email protected]>
---
 libs/libc/gdbstub/lib_gdbstub.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c
index b6aed62b89f..5e2ce3d2a96 100644
--- a/libs/libc/gdbstub/lib_gdbstub.c
+++ b/libs/libc/gdbstub/lib_gdbstub.c
@@ -1587,6 +1587,8 @@ static int gdb_is_thread_active(FAR struct gdb_state_s 
*state)
  * Description:
  *  The thread context packet is used by GDB to request information from
  *  the stub.
+ *  Set thread for subsequent operations (m, M, g, G, etc)
+ *  Thread ID of -1 means all all thread, 0 means any thread.
  *
  * Input Parameters:
  *   state   - The pointer to the GDB state structure.
@@ -1596,7 +1598,7 @@ static int gdb_is_thread_active(FAR struct gdb_state_s 
*state)
  *   Negative value on error.
  *
  * Note : Command Format: Hg<id>
- *                       Hc-<id>
+ *                       Hc<id>
  *        Response Format: OK
  ****************************************************************************/
 
@@ -1605,37 +1607,39 @@ static int gdb_thread_context(FAR struct gdb_state_s 
*state)
   FAR struct tcb_s *tcb;
   uintptr_t pid;
   int ret;
+  char c = state->pkt_buf[1];
 
-  if (state->pkt_buf[1] == 'g')
-    {
-      state->pkt_next += 2;
-    }
-  else if  (state->pkt_buf[1] == 'c')
-    {
-      state->pkt_next += 3;
-    }
-  else
+  if (c != 'g' && c != 'c')
     {
       return -EINVAL;
     }
 
+  state->pkt_next += 2;   /* Skip Hg/Hc */
+
   ret = gdb_expect_integer(state, &pid);
   if (ret < 0)
     {
       return ret;
     }
 
-  if (pid != 0)
+  if (pid == 0 || pid == -1)
     {
-      pid = pid == PID0_REPLACE ? 0 : pid;
-      tcb = nxsched_get_tcb(pid);
-      if (tcb == NULL)
-        {
-          return -EINVAL;
-        }
+      /* ‘-1’ to indicate all threads, or ‘0’ to pick any thread. */
 
-      state->pid = pid;
+      pid = state->pid;   /* Keep unchanged */
     }
+  else if (pid == PID0_REPLACE)
+    {
+      pid = 0;
+    }
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
 
   gdb_update_regcache(state);
 

Reply via email to