Created
August 19, 2022 17:14
-
-
Save Frank-Buss/60d65db7ec325230514c53e8da582f66 to your computer and use it in GitHub Desktop.
printing call stack on assert and segfaults
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <execinfo.h> | |
/* | |
compile this program like this: | |
gcc -Wall -O0 -g -fsanitize=address -fno-omit-frame-pointer -finstrument-functions test.c -o test | |
on the assert error, you will see this output: | |
test: test.c:58: bar: Assertion `0' failed. | |
#0 0x7fb58685ef75 in __sanitizer_print_stack_trace ../../../../src/libsanitizer/asan/asan_stack.cpp:86 | |
#1 0x55ad780281e7 in on_abort /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:52 | |
#2 0x7fb586620d5f (/lib/x86_64-linux-gnu/libc.so.6+0x3bd5f) | |
#3 0x7fb586620ce0 in __libc_signal_restore_set ../sysdeps/unix/sysv/linux/internal-signals.h:86 | |
#4 0x7fb586620ce0 in __GI_raise ../sysdeps/unix/sysv/linux/raise.c:48 | |
#5 0x7fb58660a536 in __GI_abort stdlib/abort.c:79 | |
#6 0x7fb58660a40e in __assert_fail_base assert/assert.c:92 | |
#7 0x7fb586619661 in __GI___assert_fail assert/assert.c:101 | |
#8 0x55ad78028247 in bar /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:58 | |
#9 0x55ad78028268 in foo /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:62 | |
#10 0x55ad780282bc in main /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:68 | |
#11 0x7fb58660bd09 in __libc_start_main ../csu/libc-start.c:308 | |
#12 0x55ad78028109 in _start (/home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test+0x1109) | |
and on the segfault (line 57), you will see this output: | |
AddressSanitizer:DEADLYSIGNAL | |
================================================================= | |
==603575==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5602c663026d bp 0x7ffcec0c75b0 sp 0x7ffcec0c75b0 T0) | |
==603575==The signal is caused by a WRITE memory access. | |
==603575==Hint: address points to the zero page. | |
#0 0x5602c663026d in bar /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:57 | |
#1 0x5602c66302b4 in foo /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:62 | |
#2 0x5602c6630308 in main /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:68 | |
#3 0x7f2968670d09 in __libc_start_main ../csu/libc-start.c:308 | |
#4 0x5602c6630119 in _start (/home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test+0x1119) | |
AddressSanitizer can not provide additional info. | |
SUMMARY: AddressSanitizer: SEGV /home/frank/data/projects/vouch/github/digital-key-device-sdk/core/test.c:57 in bar | |
==603575==ABORTING | |
*/ | |
extern void __sanitizer_print_stack_trace(void); | |
void on_abort(int signum) | |
{ | |
__sanitizer_print_stack_trace(); | |
signal(signum, SIG_DFL); | |
} | |
void bar() { | |
// *((char*) NULL) = 0; | |
assert(0); | |
} | |
void foo() { | |
bar(); | |
} | |
int main(int argc, char** argv) { | |
signal(SIGABRT, &on_abort); | |
foo(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment