gpu: nvgpu: posix: Add BUG() exception handling

For unit testing, this new feature allows to "catch" calls
to BUG() when they are expected.

JIRA NVGPU-1287

Change-Id: I29fc9cd7fc28f8697a865c173b6991e2a48a3b4d
Signed-off-by: Nicolas Benech <nbenech@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1930974
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Nicolas Benech
2018-10-19 17:37:50 -04:00
committed by mobile promotions
parent f00d9ca1aa
commit ee282de11b
3 changed files with 52 additions and 2 deletions

View File

@@ -24,6 +24,7 @@
#define __NVGPU_POSIX_BUG_H__
#include <nvgpu/types.h>
#include <setjmp.h>
/*
* TODO: make these actually useful!
@@ -54,4 +55,29 @@ void dump_stack(void);
void __bug(const char *fmt, ...) __attribute__ ((noreturn));
bool __warn(bool cond, const char *fmt, ...);
/* Provide a simple API for BUG() handling */
void bug_handler_register(jmp_buf *handler);
void bug_handler_cancel(void);
/*
* Macro to indicate that a BUG() call is expected when executing
* the "code_to_run" block of code. The macro uses a statement expression
* and the setjmp API to set a long jump point that gets called by the BUG()
* function if enabled. This allows the macro to simply expand as true if
* BUG() was called, and false otherwise.
*/
#define EXPECT_BUG(code_to_run) \
({ \
jmp_buf handler; \
bool bug_result = true; \
if (!setjmp(handler)) { \
bug_handler_register(&handler); \
code_to_run; \
bug_handler_cancel(); \
bug_result = false; \
} \
bug_result; \
})
#endif

View File

@@ -10,6 +10,8 @@ __nvgpu_set_enabled
bitmap_clear
bitmap_find_next_zero_area_off
bitmap_set
bug_handler_cancel
bug_handler_register
clear_bit
find_first_bit
find_first_zero_bit

View File

@@ -24,6 +24,23 @@
#include <nvgpu/posix/bug.h>
#include <signal.h>
#include <pthread.h>
#include <stdbool.h>
#include <setjmp.h>
static _Thread_local bool expect_bug;
static _Thread_local jmp_buf *jmp_handler;
void bug_handler_register(jmp_buf *handler)
{
expect_bug = true;
jmp_handler = handler;
}
void bug_handler_cancel(void)
{
expect_bug = false;
jmp_handler = NULL;
}
static void __dump_stack(unsigned int skip_frames)
{
@@ -40,9 +57,14 @@ void dump_stack(void)
*/
void __bug(const char *fmt, ...)
{
if (expect_bug) {
nvgpu_info(NULL, "Expected BUG detected!");
expect_bug = false;
/* 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 */
nvgpu_err(NULL, "BUG detected!");
/* Raise a bad system call signal and kill the thread */
(void) raise(SIGSEGV);
pthread_exit(NULL);
}