From f302e8fd401d9ff987aab96a91821cb24c250ed8 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sat, 25 Dec 2021 23:28:16 +0800 Subject: [PATCH] arch/sim: Implement up_backtrace Signed-off-by: Xiang Xiao --- arch/sim/src/Makefile | 6 +- arch/sim/src/nuttx-names.in | 1 + arch/sim/src/sim/up_assert.c | 16 ++++-- arch/sim/src/sim/up_backtrace.c | 55 +++++++++++++++++++ .../sim/{up_host_abort.c => up_hostmisc.c} | 9 ++- arch/sim/src/sim/up_internal.h | 3 +- 6 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 arch/sim/src/sim/up_backtrace.c rename arch/sim/src/sim/{up_host_abort.c => up_hostmisc.c} (91%) diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index f24045c9ec..67d2ae198a 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -61,6 +61,10 @@ CSRCS += up_heap.c up_uart.c up_assert.c up_puts.c CSRCS += up_copyfullstate.c CSRCS += up_sigdeliver.c +ifeq ($(CONFIG_SCHED_BACKTRACE),y) +CSRCS += up_backtrace.c +endif + ifeq ($(CONFIG_ARCH_HAVE_VFORK),y) ifeq ($(CONFIG_SCHED_WAITPID),y) CSRCS += up_vfork.c @@ -75,7 +79,7 @@ ifeq ($(CONFIG_HOST_MACOS),y) HOSTCFLAGS += -Wno-deprecated-declarations endif -HOSTSRCS = up_hostirq.c up_hostmemory.c up_hosttime.c up_simuart.c up_host_abort.c +HOSTSRCS = up_hostirq.c up_hostmemory.c up_hosttime.c up_simuart.c up_hostmisc.c STDLIBS += -lpthread ifeq ($(CONFIG_HOST_MACOS),y) ifeq ($(CONFIG_LIBCXX),y) diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index 3b3629a9e9..24c6725ab0 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -35,6 +35,7 @@ NXSYMBOLS(abort) NXSYMBOLS(accept) NXSYMBOLS(access) NXSYMBOLS(atexit) +NXSYMBOLS(backtrace) NXSYMBOLS(bind) NXSYMBOLS(calloc) NXSYMBOLS(chmod) diff --git a/arch/sim/src/sim/up_assert.c b/arch/sim/src/sim/up_assert.c index 51526fa285..ed96ace282 100644 --- a/arch/sim/src/sim/up_assert.c +++ b/arch/sim/src/sim/up_assert.c @@ -73,6 +73,8 @@ void up_assert(const char *filename, int lineno) { + FAR struct tcb_s *rtcb = running_task(); + /* Flush any buffered SYSLOG data (prior to the assertion) */ syslog_flush(); @@ -82,7 +84,7 @@ void up_assert(const char *filename, int lineno) #ifdef CONFIG_SMP #if CONFIG_TASK_NAME_SIZE > 0 _alert("Assertion failed CPU%d at file:%s line: %d task: %s\n", - up_cpu_index(), filename, lineno, running_task()->name); + up_cpu_index(), filename, lineno, rtcb->name); #else _alert("Assertion failed CPU%d at file:%s line: %d\n", up_cpu_index(), filename, lineno); @@ -90,13 +92,19 @@ void up_assert(const char *filename, int lineno) #else #if CONFIG_TASK_NAME_SIZE > 0 _alert("Assertion failed at file:%s line: %d task: %s\n", - filename, lineno, running_task()->name); + filename, lineno, rtcb->name); #else _alert("Assertion failed at file:%s line: %d\n", filename, lineno); #endif #endif + /* Show back trace */ + +#ifdef CONFIG_SCHED_BACKTRACE + sched_dumpstack(rtcb->pid); +#endif + /* Flush any buffered SYSLOG data (from the above) */ syslog_flush(); @@ -104,14 +112,14 @@ void up_assert(const char *filename, int lineno) /* Allow for any board/configuration specific crash information */ #ifdef CONFIG_BOARD_CRASHDUMP - board_crashdump(up_getsp(), running_task(), filename, lineno); + board_crashdump(up_getsp(), rtcb, filename, lineno); #endif /* Flush any buffered SYSLOG data */ syslog_flush(); - if (CURRENT_REGS || (running_task())->flink == NULL) + if (CURRENT_REGS || rtcb->flink == NULL) { /* Exit the simulation */ diff --git a/arch/sim/src/sim/up_backtrace.c b/arch/sim/src/sim/up_backtrace.c new file mode 100644 index 0000000000..fed2dde247 --- /dev/null +++ b/arch/sim/src/sim/up_backtrace.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * arch/sim/src/sim/up_backtrace.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +#include "up_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int up_backtrace(struct tcb_s *tcb, void **buffer, int size, int skip) +{ + void *buf[skip + size]; + int ret = 0; + + if (tcb == running_task()) + { + ret = host_backtrace(buf, skip + size); + } + + if (ret <= skip) + { + return ret < 0 ? ret : 0; + } + + ret -= skip; + memcpy(buffer, &buf[skip], ret * sizeof(void *)); + + return ret; +} diff --git a/arch/sim/src/sim/up_host_abort.c b/arch/sim/src/sim/up_hostmisc.c similarity index 91% rename from arch/sim/src/sim/up_host_abort.c rename to arch/sim/src/sim/up_hostmisc.c index db288d388d..b7747eea69 100644 --- a/arch/sim/src/sim/up_host_abort.c +++ b/arch/sim/src/sim/up_hostmisc.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/sim/src/sim/up_host_abort.c + * arch/sim/src/sim/up_hostmisc.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -22,6 +22,7 @@ * Included Files ****************************************************************************/ +#include #include #include "up_internal.h" @@ -47,3 +48,9 @@ void host_abort(int status) exit(status); } +int host_backtrace(void** array, int size) +{ + /* exit the simulation */ + + return backtrace(array, size); +} diff --git a/arch/sim/src/sim/up_internal.h b/arch/sim/src/sim/up_internal.h index c2ae3e2510..29da23744f 100644 --- a/arch/sim/src/sim/up_internal.h +++ b/arch/sim/src/sim/up_internal.h @@ -142,9 +142,10 @@ void up_copyfullstate(uint32_t *dest, uint32_t *src); void *up_doirq(int irq, void *regs); -/* up_head.c ****************************************************************/ +/* up_hostmisc.c ************************************************************/ void host_abort(int status); +int host_backtrace(void** array, int size); /* up_hostmemory.c **********************************************************/