Open tonykang22 opened 2 years ago
#include <execinfo.h> int backtrace (void **buffer, int size);
#include <execinfo.h> char **backtrace_symbols (void *const *buffer, int size);
#include <execinfo.h> void backtrace_symbols_fd (void *const *buffer, int size, int fd);
#include <execinfo.h> #include <stdio.h> #include <stdlib.h> /* Obtain a backtrace and print it to stdout. */ void print_trace (void) { void *array[10]; size_t size; char **strings; size_t i; size = backtrace (array, 10); strings = backtrace_symbols (array, size); printf ("Obtained %d stack frames.\n", size); for (i = 0; i < size; i++) printf ("%s\n", strings[i]); free (strings); } /* A dummy function to make the backtrace more interesting. */ void dummy_function (void) { print_trace (); } int main (void) { dummy_function (); return 0; }
addr2line [options] [addr addr ...]
#include <execinfo.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void segv_handler(int sig) { int j, nptrs; #define SIZE 100 void *buffer[100]; char **strings; nptrs = backtrace(buffer, SIZE); printf("backtrace() returned %d addresses\n", nptrs); fprintf(stderr, "ERROR: signal(%d)\n", sig); backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO); exit(1); } void foo() { printf("foo\n"); } void bar() { printf("bar\n"); } void first_to_space(char *str) { printf("func start\n"); str[0] = ' '; printf("func end\n"); } int main(int argc, char **argv) { char *str = NULL; signal(SIGSEGV, segv_handler); foo(); bar(); first_to_space(str); return 0; }
04. backtrace
backtrace 예제
addr2line
addr2line [options] [addr addr ...]
실전 TIP
문제
해결책
예시