Skip to content

Instantly share code, notes, and snippets.

@awreece
Last active October 8, 2018 20:35
Show Gist options
  • Save awreece/d6aaafef7d013588957e to your computer and use it in GitHub Desktop.
Save awreece/d6aaafef7d013588957e to your computer and use it in GitHub Desktop.
void print_backtrace(int skip) {
void *backtrace_array[MAX_STACK];
int stack_size = backtrace(backtrace_array, MAX_STACK);
backtrace_symbols_fd(&backtrace_array[skip+1],
stack_size - skip - 1, STDERR_FILENO);
}
void signal_backtrace(int signum) {
fprintf(stderr, "%s\n\n", strsignal(signum));
print_backtrace(2);
signal(signum, SIG_DFL);
kill(getpid(), signum);
}
diff --git a/mdriver.c b/mdriver.c
index 283c522..eb05408 100644
--- a/mdriver.c
+++ b/mdriver.c
@@ -20,6 +20,7 @@
#include <unistd.h>
+#include "backtrace.h"
#include "mm.h"
#include "memlib.h"
#include "fsecs.h"
@@ -273,6 +274,7 @@ int main(int argc, char **argv)
double util_weight = 0, perf_weight = 0;
int numcorrect;
+ signal(SIGSEGV, signal_backtrace);
setbuf(stdout, 0);
setbuf(stderr, 0);
$ ./mdriver.debug
Using default tracefiles in ./traces/
Measuring performance with a cycle counter.
Segmentation fault: 11
0 libsystem_c.dylib 0x00007fff7d221408 __sF + 152
1 libsystem_c.dylib 0x00007fff91b358dc fgets + 41
2 mdriver.debug 0x000000010b7666df mhz_full + 129
3 mdriver.debug 0x000000010b766735 mhz + 26
4 mdriver.debug 0x000000010b765f7a init_fsecs + 125
5 mdriver.debug 0x000000010b762cb3 main + 1084
6 libdyld.dylib 0x00007fff94d835fd start + 1
@Leedehai
Copy link

Leedehai commented Oct 8, 2018

Well.. GNU documentation on backtrace says backtrace() and backtrace_symbol_fd() are async-signal unsafe ("AS-Unsafe") so they should not be invoked inside a signal handler. See Linux signal safety for further clarifications. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment