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 <nbenech@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1943478
Reviewed-by: svc-misra-checker <svc-misra-checker@nvidia.com>
Reviewed-by: Philip Elcan <pelcan@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Alex Waterman <alexw@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Nicolas Benech
2018-11-05 14:10:23 -05:00
committed by mobile promotions
parent 3e6779d554
commit 5b5a1cb014
4 changed files with 26 additions and 4 deletions

View File

@@ -50,6 +50,7 @@ ifneq ($(NV_BUILD_CONFIGURATION_OS_IS_QNX),1)
NV_COMPONENT_SYSTEM_SHARED_LIBRARIES += pthread NV_COMPONENT_SYSTEM_SHARED_LIBRARIES += pthread
endif endif
NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__ NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__
_NV_TOOLCHAIN_CFLAGS += -rdynamic -g
NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit
systemimage:: $(NV_COMPONENT_SYSTEMIMAGE_DIR) systemimage:: $(NV_COMPONENT_SYSTEMIMAGE_DIR)

View File

@@ -26,6 +26,11 @@
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <setjmp.h> #include <setjmp.h>
#ifndef _QNX_SOURCE
#include <execinfo.h>
#endif
#define BACKTRACE_MAXSIZE 1024
static _Thread_local bool expect_bug; static _Thread_local bool expect_bug;
static _Thread_local jmp_buf *jmp_handler; static _Thread_local jmp_buf *jmp_handler;
@@ -42,14 +47,25 @@ void bug_handler_cancel(void)
jmp_handler = NULL; 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; return;
} }
void dump_stack(void) 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. */ /* Perform a long jump to where "setjmp()" was called. */
longjmp(*jmp_handler, 1); 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!"); nvgpu_err(NULL, "BUG detected!");
dump_stack();
(void) raise(SIGSEGV); (void) raise(SIGSEGV);
pthread_exit(NULL); pthread_exit(NULL);
} }

View File

@@ -97,7 +97,7 @@ $(OUT)/nvgpu_unit: $(OUT)/libnvgpu-drv.so $(CORE_OBJS)
-o $(OUT)/nvgpu_unit $(CORE_OBJS) $(LIB_PATHS) $(LIBS) -o $(OUT)/nvgpu_unit $(CORE_OBJS) $(LIB_PATHS) $(LIBS)
$(OUT)/libnvgpu-drv.so: $(OBJS) $(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 # Default build target for all the nvgpu driver object files we want to build in
# userspace. These get bundled into libnvgpu-drv.so. # userspace. These get bundled into libnvgpu-drv.so.

View File

@@ -45,6 +45,7 @@ NV_COMPONENT_SYSTEM_SHARED_LIBRARIES += gcov
endif endif
NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__ NV_COMPONENT_CFLAGS += -D__NVGPU_POSIX__
_NV_TOOLCHAIN_CFLAGS += -rdynamic
NV_UNIT_SH=unit.sh NV_UNIT_SH=unit.sh
NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit