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,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);
}