From df0d8b4230394a04b97482a9ea546e26e46b72a8 Mon Sep 17 00:00:00 2001 From: geniusgogo <2041245+geniusgogo@users.noreply.github.com> Date: Wed, 8 Nov 2023 19:11:10 +0800 Subject: [PATCH] fix aarch64 backtrace print --- libcpu/aarch64/common/trap.c | 50 ++++++++++-------------------------- src/kservice.c | 24 +++++++++-------- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/libcpu/aarch64/common/trap.c b/libcpu/aarch64/common/trap.c index 325fe1e8ba..d408b64163 100644 --- a/libcpu/aarch64/common/trap.c +++ b/libcpu/aarch64/common/trap.c @@ -34,52 +34,29 @@ extern long list_thread(void); static void _check_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info) { - uint32_t mode = regs->cpsr; + uint32_t is_user_fault; + rt_thread_t th; - if ((mode & 0x1f) == 0x00) + is_user_fault = !(regs->cpsr & 0x1f); + if (is_user_fault) { rt_kprintf("%s! pc = 0x%x\n", info, regs->pc - pc_adj); + } - /* user stack backtrace */ - #ifdef RT_USING_LWP - { - rt_thread_t th; - - th = rt_thread_self(); - if (th && th->lwp) - { - arch_backtrace_uthread(th); - } - } - #endif + /* user stack backtrace */ + th = rt_thread_self(); + if (th && th->lwp) + { + arch_backtrace_uthread(th); + } + if (is_user_fault) + { #ifdef LWP_USING_CORE_DUMP lwp_core_dump(regs, pc_adj); #endif sys_exit_group(-1); } - else - { - /* user stack backtrace */ - #ifdef RT_USING_LWP - { - rt_thread_t th; - - th = rt_thread_self(); - if (th && th->lwp) - { - arch_backtrace_uthread(th); - } - } - #endif - - /* kernel stack backtrace */ - struct rt_hw_backtrace_frame frame = { - .fp = regs->x29, - .pc = regs->pc - }; - rt_backtrace_frame(&frame); - } } rt_inline int _get_type(unsigned long esr) @@ -383,7 +360,6 @@ void rt_hw_trap_exception(struct rt_hw_exp_stack *regs) rt_hw_cpu_shutdown(); } - void rt_hw_trap_serror(struct rt_hw_exp_stack *regs) { rt_kprintf("SError\n"); diff --git a/src/kservice.c b/src/kservice.c index 24223ff7dd..d294f218bb 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -93,13 +93,13 @@ rt_weak void rt_hw_cpu_shutdown(void) rt_weak rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame) { - LOG_W("%s: not implemented"); + LOG_W("%s is not implemented", __func__); return -RT_ENOSYS; } rt_weak rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame) { - LOG_W("%s: not implemented"); + LOG_W("%s is not implemented", __func__); return -RT_ENOSYS; } @@ -1565,7 +1565,8 @@ rt_weak rt_err_t rt_backtrace(void) #else /* otherwise not implemented */ rt_weak rt_err_t rt_backtrace(void) { - LOG_W("%s: not implemented"); + /* LOG_W cannot work under this environment */ + rt_kprintf("%s is not implemented\n", __func__); return -RT_ENOSYS; } #endif @@ -1573,18 +1574,19 @@ rt_weak rt_err_t rt_backtrace(void) rt_err_t rt_backtrace_frame(struct rt_hw_backtrace_frame *frame) { long nesting = 0; - LOG_RAW("please use: addr2line -e rtthread.elf -a -f"); + + rt_kprintf("please use: addr2line -e rtthread.elf -a -f\n"); while (nesting < RT_BACKTRACE_LEVEL_MAX_NR) { - LOG_RAW(" 0x%lx", (rt_ubase_t)frame->pc); + rt_kprintf(" 0x%lx", (rt_ubase_t)frame->pc); if (rt_hw_backtrace_frame_unwind(rt_thread_self(), frame)) { break; } nesting++; } - LOG_RAW("\n"); + rt_kprintf("\n"); return RT_EOK; } @@ -1619,7 +1621,7 @@ static void cmd_backtrace(int argc, char** argv) { if (argc == 1) { - LOG_RAW("[INFO] No thread specified\n" + rt_kprintf("[INFO] No thread specified\n" "[HELP] You can use commands like: backtrace %p\n" "Printing backtrace of calling stack...\n", rt_thread_self()); @@ -1628,7 +1630,7 @@ static void cmd_backtrace(int argc, char** argv) } else { - LOG_RAW("please use: backtrace [thread_address]\n"); + rt_kprintf("please use: backtrace [thread_address]\n"); return; } } @@ -1636,18 +1638,18 @@ static void cmd_backtrace(int argc, char** argv) pid = strtoul(argv[1], &end_ptr, 0); if (end_ptr == argv[1]) { - LOG_RAW("Invalid input: %s\n", argv[1]); + rt_kprintf("Invalid input: %s\n", argv[1]); return ; } if (pid && rt_object_get_type((void *)pid) == RT_Object_Class_Thread) { rt_thread_t target = (rt_thread_t)pid; - LOG_RAW("backtrace %s(0x%lx), from %s\n", target->parent.name, pid, argv[1]); + rt_kprintf("backtrace %s(0x%lx), from %s\n", target->parent.name, pid, argv[1]); rt_backtrace_thread(target); } else - LOG_RAW("Invalid pid: %ld\n", pid); + rt_kprintf("Invalid pid: %ld\n", pid); } MSH_CMD_EXPORT_ALIAS(cmd_backtrace, backtrace, print backtrace of a thread);