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


The following commit(s) were added to refs/heads/master by this push:
     new aa2d0c66a9a gdbstub: replace process ID 0 to a valid value
aa2d0c66a9a is described below

commit aa2d0c66a9ab256661ad9f76f43efcf513cc9703
Author: xuxingliang <[email protected]>
AuthorDate: Fri Jan 17 17:54:13 2025 +0800

    gdbstub: replace process ID 0 to a valid value
    
    The GDB remote protocol and NuttX's internal TCB lookup disagree about
    PID 0: GDB numbers threads from 1, NuttX reserves PID 0 for the IDLE
    task.  Translate the GDB-side thread number into a valid NuttX PID
    using PID0_REPLACE so that a PID that hashes to the same slot does not
    shadow the idle task during lookups.
    
    Signed-off-by: xuxingliang <[email protected]>
    Signed-off-by: zhangyuan29 <[email protected]>
---
 libs/libc/gdbstub/lib_gdbstub.c | 55 +++++++++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 16 deletions(-)

diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c
index 0cccc4a975b..675b584309a 100644
--- a/libs/libc/gdbstub/lib_gdbstub.c
+++ b/libs/libc/gdbstub/lib_gdbstub.c
@@ -62,6 +62,14 @@
 #define IS_SPECIAL_CHARACTERS(c) \
         ((c) == '#' || (c) == '$' || (c) == '}' || (c) == '*')
 
+/* Architecture can overwrite the default XCPTCONTEXT alignment */
+
+#ifndef XCPTCONTEXT_ALIGN
+#  define XCPTCONTEXT_ALIGN 16
+#endif
+
+#define PID0_REPLACE INT32_MAX
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -79,7 +87,7 @@ struct gdb_state_s
                                            */
   int last_stopreason;                    /* Last stop reason */
   FAR void *last_stopaddr;                /* Last stop address */
-  pid_t pid;                              /* Gdb current thread */
+  pid_t pid;                              /* Current NuttX thread pid */
   FAR char *pkt_next;                     /* Pointer to next byte in packet */
   char pkt_buf[BUFSIZE];                  /* Packet buffer */
   size_t pkt_len;                         /* Packet send and receive length */
@@ -1339,12 +1347,10 @@ static int gdb_write_bin_memory(FAR struct gdb_state_s 
*state)
 
 static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
 {
+  int pid = tcb->pid == 0 ? PID0_REPLACE : tcb->pid;
   FAR struct gdb_state_s *state = arg;
-  int pid = tcb->pid;
 
-  /* Gdb pid start from 1, so add it */
-
-  state->pkt_len += sprintf(&state->pkt_buf[state->pkt_len], "%x,", pid + 1);
+  state->pkt_len += sprintf(&state->pkt_buf[state->pkt_len], "%x,", pid);
 }
 
 /****************************************************************************
@@ -1419,7 +1425,12 @@ static int gdb_query(FAR struct gdb_state_s *state)
           return ret;
         }
 
-      tcb = nxsched_get_tcb(pid - 1);
+      if (pid == PID0_REPLACE)
+        {
+          pid = 0;
+        }
+
+      tcb = nxsched_get_tcb(pid);
       if (tcb == NULL)
         {
           return -EINVAL;
@@ -1501,13 +1512,18 @@ static int gdb_is_thread_active(FAR struct gdb_state_s 
*state)
       return ret;
     }
 
-  tcb = nxsched_get_tcb(pid - 1);
+  if (pid == PID0_REPLACE)
+    {
+      pid = 0;
+    }
+
+  tcb = nxsched_get_tcb(pid);
   if (tcb == NULL)
     {
       return -EINVAL;
     }
 
-  state->pid = pid - 1;
+  state->pid = pid;
   gdb_send_ok_packet(state);
   return ret;
 }
@@ -1558,13 +1574,14 @@ static int gdb_thread_context(FAR struct gdb_state_s 
*state)
 
   if (pid != 0)
     {
-      tcb = nxsched_get_tcb(pid - 1);
+      pid = pid == PID0_REPLACE ? 0 : pid;
+      tcb = nxsched_get_tcb(pid);
       if (tcb == NULL)
         {
           return -EINVAL;
         }
 
-      state->pid = pid - 1;
+      state->pid = pid;
     }
 
   gdb_send_ok_packet(state);
@@ -1595,26 +1612,32 @@ static int gdb_send_stop(FAR struct gdb_state_s *state, 
int stopreason,
                          FAR void *stopaddr)
 {
   int ret;
+  int pid;
+
+  state->pid = pid = _SCHED_GETTID();
+  if (pid == 0)
+    {
+      pid = PID0_REPLACE;
+    }
 
-  state->pid = _SCHED_GETTID();
 retry:
   switch (stopreason)
     {
       case GDB_STOPREASON_WATCHPOINT_RO:
         ret = sprintf(state->pkt_buf, "T05thread:%x;rwatch:%" PRIxPTR ";",
-                      state->pid + 1, (uintptr_t)stopaddr);
+                      pid, (uintptr_t)stopaddr);
         break;
       case GDB_STOPREASON_WATCHPOINT_WO:
         ret = sprintf(state->pkt_buf, "T05thread:%x;awatch:%" PRIxPTR ";",
-                      state->pid + 1, (uintptr_t)stopaddr);
+                      pid, (uintptr_t)stopaddr);
         break;
       case GDB_STOPREASON_WATCHPOINT_RW:
         ret = sprintf(state->pkt_buf, "T05thread:%x;watch:%" PRIxPTR ";",
-                      state->pid + 1, (uintptr_t)stopaddr);
+                      pid, (uintptr_t)stopaddr);
         break;
       case GDB_STOPREASON_BREAKPOINT:
         ret = sprintf(state->pkt_buf, "T05thread:%x;hwbreak:;",
-                      state->pid + 1);
+                      pid);
         break;
       case GDB_STOPREASON_STEPPOINT:
         if (state->last_stopreason == GDB_STOPREASON_WATCHPOINT_RW ||
@@ -1627,7 +1650,7 @@ retry:
 
       case GDB_STOPREASON_CTRLC:
       default:
-        ret = sprintf(state->pkt_buf, "T05thread:%x;", state->pid + 1);
+        ret = sprintf(state->pkt_buf, "T05thread:%x;", pid);
     }
 
   if (ret < 0)

Reply via email to