From 29114efb41bc959374e2c8ee84d689aeed1042fb Mon Sep 17 00:00:00 2001 From: zhanxiaoqi Date: Tue, 14 Apr 2026 15:06:49 +0800 Subject: [PATCH] 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 --- 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); }