#include <iostream>
#include <execinfo.h>
using namespace std;
void DumpTraceback(int signal) {
const int size = 200;
void *buffer[size];
char **strings;
int nptrs = backtrace(buffer, size);
printf("backtrace() returned %d address\n", nptrs);
// backtrace_symbols函数不可重入, 可以使用backtrace_symbols_fd替换
strings = backtrace_symbols(buffer, nptrs);
if (strings) {
for (int i = 0; i < nptrs; ++i) {
printf("%s\n", strings[i]);
}
free(strings);
}
}
int testAdd(int a, int b)
{
DumpTraceback(1);
return a + b;
}
int main()
{
int a ,b;
int c = testAdd(a, b);
printf("%d \n ", c);
}
g++ -g trac.cpp // 编译