1、示例程序
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <unistd.h>
int func1()
{
printf("1234");
#define SIZE 100
void *buffer[100];
int nptrs = backtrace(buffer, SIZE);
printf("backtrace() returned %d addresses\n", nptrs);
backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO);
return 1;
}
int func2()
{
func1();
return 1;
}
int func3()
{
func2();
return 1;
}
int main()
{
func3();
return 1;
}
2、编译
[root@localhost demo]# gcc -g -rdynamic -o stack stack.c
3、运行
[root@localhost demo]# ./stack
1234backtrace() returned 6 addresses
./stack(func1+0x2e)[0x401188]
./stack(func2+0xe)[0x4011cd]
./stack(func3+0xe)[0x4011e2]
./stack(main+0xe)[0x4011f7]
/lib64/libc.so.6(__libc_start_main+0xe7)[0x7f9fd06d8b67]
./stack(_start+0x2a)[0x40108a]