Created
April 25, 2014 05:07
-
-
Save mawenbao/11278165 to your computer and use it in GitHub Desktop.
This file contains 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 <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