From 5b5a1cb014de31d344ae90b308b72e708f7b082f Mon Sep 17 00:00:00 2001 From: Nicolas Benech Date: Mon, 5 Nov 2018 14:10:23 -0500 Subject: [PATCH] nvgpu: posix: Add support for backtrace Add support for backtrace prints when BUG() is being called. To support backtrace calls, the -rdynamic compiler flag is now being used. On host, it fully works as expected, on target only exported symbols (in libnvgpu-drv.export) are visible. JIRA NVGPU-1246 Change-Id: Ia075ac99228e8377bdb3142fef2e56c2fea2d967 Signed-off-by: Nicolas Benech Reviewed-on: https://git-master.nvidia.com/r/1943478 Reviewed-by: svc-misra-checker Reviewed-by: Philip Elcan GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/Makefile.tmk | 1 + drivers/gpu/nvgpu/os/posix/bug.c | 26 +++++++++++++++++++++++--- userspace/Makefile | 2 +- userspace/Makefile.tmk | 1 + 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/nvgpu/Makefile.tmk b/drivers/gpu/nvgpu/Makefile.tmk index 3c73b1089..5a601c6bc 100644 --- a/drivers/gpu/nvgpu/Makefile.tmk +++ b/drivers/gpu/nvgpu/Makefile.tmk @@ -50,6 +50,7 @@ ifneq ($(NV_BUILD_CONFIGURATION_OS_IS_QNX),1) NV_COMPONENT_SYSTEM_SHARED_LIBRARIES += pthread endif NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__ +_NV_TOOLCHAIN_CFLAGS += -rdynamic -g NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit systemimage:: $(NV_COMPONENT_SYSTEMIMAGE_DIR) diff --git a/drivers/gpu/nvgpu/os/posix/bug.c b/drivers/gpu/nvgpu/os/posix/bug.c index 62a9c25b5..04351a2d9 100644 --- a/drivers/gpu/nvgpu/os/posix/bug.c +++ b/drivers/gpu/nvgpu/os/posix/bug.c @@ -26,6 +26,11 @@ #include #include #include +#ifndef _QNX_SOURCE +#include +#endif + +#define BACKTRACE_MAXSIZE 1024 static _Thread_local bool expect_bug; static _Thread_local jmp_buf *jmp_handler; @@ -42,14 +47,25 @@ void bug_handler_cancel(void) jmp_handler = NULL; } -static void __dump_stack(unsigned int skip_frames) +static void __dump_stack(int skip_frames) { +#ifndef _QNX_SOURCE + void *trace[BACKTRACE_MAXSIZE]; + int i; + int trace_size = backtrace(trace, BACKTRACE_MAXSIZE); + char **trace_syms = backtrace_symbols(trace, trace_size); + + for (i = skip_frames; i < trace_size; i++) { + nvgpu_err(NULL, "[%d] %s", i - skip_frames, trace_syms[i]); + } +#endif return; } void dump_stack(void) { - __dump_stack(0); + /* Skip this function and __dump_stack() */ + __dump_stack(2); } /* @@ -63,8 +79,12 @@ void __bug(const char *fmt, ...) /* Perform a long jump to where "setjmp()" was called. */ longjmp(*jmp_handler, 1); } - /* If BUG is unexpected, raise a SIGSEGV signal and kill the thread */ + /* + * If BUG was unexpected, raise a SIGSEGV signal, dump the stack and + * kill the thread. + */ nvgpu_err(NULL, "BUG detected!"); + dump_stack(); (void) raise(SIGSEGV); pthread_exit(NULL); } diff --git a/userspace/Makefile b/userspace/Makefile index fec34aa60..2dd8afc2a 100644 --- a/userspace/Makefile +++ b/userspace/Makefile @@ -97,7 +97,7 @@ $(OUT)/nvgpu_unit: $(OUT)/libnvgpu-drv.so $(CORE_OBJS) -o $(OUT)/nvgpu_unit $(CORE_OBJS) $(LIB_PATHS) $(LIBS) $(OUT)/libnvgpu-drv.so: $(OBJS) - $(CC) -shared -o $(OUT)/libnvgpu-drv.so $(OBJS) -lgcov + $(CC) -shared -o $(OUT)/libnvgpu-drv.so $(OBJS) -lgcov -rdynamic # Default build target for all the nvgpu driver object files we want to build in # userspace. These get bundled into libnvgpu-drv.so. diff --git a/userspace/Makefile.tmk b/userspace/Makefile.tmk index 672ec1ef9..1a47d48b6 100644 --- a/userspace/Makefile.tmk +++ b/userspace/Makefile.tmk @@ -45,6 +45,7 @@ NV_COMPONENT_SYSTEM_SHARED_LIBRARIES += gcov endif NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__ +_NV_TOOLCHAIN_CFLAGS += -rdynamic NV_UNIT_SH=unit.sh NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit