backtrace(3) Library Functions Manual backtrace(3) backtrace, backtrace_symbols, backtrace_symbols_fd - (libc -lc) #include int backtrace(int size; void *buffer[size], int size); char **backtrace_symbols(int size; void *const buffer[size], int size); void backtrace_symbols_fd(int size; void *const buffer[size], int size, int fd); backtrace() buffer. . buffer void * . size buffer. size size buffer size . backtrace() buffer backtrace_symbols() . size buffer. ( ) ( ). backtrace_symbols(). malloc(3) backtrace_symbols() . ( .) backtrace_symbols_fd() buffer size backtrace_symbols() fd. backtrace_symbols_fd() malloc(3) . backtrace() buffer size. size size . backtrace_symbols() malloc(3) NULL. attributes(7). +------------+------------------------------------+--------------------+ || | | +------------+------------------------------------+--------------------+ |backtrace()| | MT-Safe | |backtrace_symbols()| | | |backtrace_symbols_fd()| | | +------------+------------------------------------+--------------------+ GNU. glibc 2.1. . : o ( gcc(1)) . o . o . o backtrace() backtrace_symbols_fd() malloc() libgcc . malloc(3). ( ) libgcc . . GNU -rdynamic. "" . backtrace() backtrace_symbols(). : $ cc -rdynamic prog.c -o prog $ ./prog 3 backtrace() returned 8 addresses ./prog(myfunc3+0x5c) [0x80487f0] ./prog [0x8048871] ./prog(myfunc+0x21) [0x8048894] ./prog(myfunc+0x1a) [0x804888d] ./prog(myfunc+0x1a) [0x804888d] ./prog(main+0x65) [0x80488fb] /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c] ./prog [0x8048711] #include #include #include #include #define BT_BUF_SIZE 100 void myfunc3(void) { int nptrs; void *buffer[BT_BUF_SIZE]; char **strings; nptrs = backtrace(buffer, BT_BUF_SIZE); printf("backtrace() returned %d addresses\n", nptrs); /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) would produce similar output to the following: */ strings = backtrace_symbols(buffer, nptrs); if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } for (size_t j = 0; j < nptrs; j++) printf("%s\n", strings[j]); free(strings); } static void /* "static" means don't export the symbol. */ myfunc2(void) { myfunc3(); } void myfunc(int ncalls) { if (ncalls > 1) myfunc(ncalls - 1); else myfunc2(); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "%s num-calls\n", argv[0]); exit(EXIT_FAILURE); } myfunc(atoi(argv[1])); exit(EXIT_SUCCESS); } addr2line(1) gcc(1) gdb(1) ld(1) dlopen(3) malloc(3) 3 . . : . 6.18 8 2026 backtrace(3)