Skip to content

Instantly share code, notes, and snippets.

@mawenbao
Created April 25, 2014 05:07
Show Gist options
  • Save mawenbao/11278165 to your computer and use it in GitHub Desktop.
Save mawenbao/11278165 to your computer and use it in GitHub Desktop.
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void print_stack_frames(int signum) {
int j, nptrs;
#define SIZE 100
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, SIZE);
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (j = 0; j < nptrs; j++)
printf("%s\n", strings[j]);
free(strings);
exit(signum);
}
void myfunc2(int a) {
int *b;
*b = a;
}
void myfunc() {
int a = 1;
myfunc2(a);
}
int main(int argc, char *argv[]) {
signal(SIGSEGV, print_stack_frames);
myfunc();
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment