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

xiaoxiang 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 29114efb41b sched/misc: Optimize thread saving order in coredump for 
easier gdb debug
29114efb41b is described below

commit 29114efb41bc959374e2c8ee84d689aeed1042fb
Author: zhanxiaoqi <[email protected]>
AuthorDate: Tue Apr 14 15:06:49 2026 +0800

    sched/misc: Optimize thread saving order in coredump for easier gdb debug
    
    When GDB opens a coredump file, it first selects a "current thread",
    and the bt command by default only performs backtracing on this current
    thread. When there is no dedicated "crash thread marker" for this current
    thread in NuttX's coredump.elf, it usually degenerates to
    "the first thread parsed in the core file".
    
    Signed-off-by: zhanxiaoqi <[email protected]>
---
 sched/misc/coredump.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/sched/misc/coredump.c b/sched/misc/coredump.c
index 1d6d5afd719..1cb41ab5df1 100644
--- a/sched/misc/coredump.c
+++ b/sched/misc/coredump.c
@@ -368,9 +368,21 @@ static void elf_emit_note(FAR struct elf_dumpinfo_s *cinfo)
 
   if (cinfo->pid == INVALID_PROCESS_ID)
     {
+     FAR struct tcb_s *rtcb = running_task();
+
+      /* Emit the current (typically crashing) task first so that GDB's
+       * default thread selection shows the crashing backtrace on the initial
+       * `bt`.
+       */
+
+      if (rtcb != NULL)
+        {
+          elf_emit_tcb_note(cinfo, rtcb);
+        }
+
       for (i = 0; i < g_npidhash; i++)
         {
-          if (g_pidhash[i] != NULL)
+          if (g_pidhash[i] != NULL && g_pidhash[i] != rtcb)
             {
               elf_emit_tcb_note(cinfo, g_pidhash[i]);
             }
@@ -457,9 +469,16 @@ static void elf_emit_stack(FAR struct elf_dumpinfo_s 
*cinfo)
 
   if (cinfo->pid == INVALID_PROCESS_ID)
     {
+      FAR struct tcb_s *rtcb = running_task();
+
+      if (rtcb != NULL)
+        {
+          elf_emit_tcb_stack(cinfo, rtcb);
+        }
+
       for (i = 0; i < g_npidhash; i++)
         {
-          if (g_pidhash[i] != NULL)
+          if (g_pidhash[i] != NULL && g_pidhash[i] != rtcb)
             {
               elf_emit_tcb_stack(cinfo, g_pidhash[i]);
             }
@@ -640,9 +659,16 @@ static void elf_emit_phdr(FAR struct elf_dumpinfo_s *cinfo,
   phdr.p_align  = ELF_PAGESIZE;
   if (cinfo->pid == INVALID_PROCESS_ID)
     {
+      FAR struct tcb_s *rtcb = running_task();
+
+      if (rtcb != NULL)
+        {
+          elf_emit_tcb_phdr(cinfo, rtcb, &phdr, &offset);
+        }
+
       for (i = 0; i < g_npidhash; i++)
         {
-          if (g_pidhash[i] != NULL)
+          if (g_pidhash[i] != NULL && g_pidhash[i] != rtcb)
             {
               elf_emit_tcb_phdr(cinfo, g_pidhash[i], &phdr, &offset);
             }

Reply via email to