#include
#include
#include
#include
#include
#include
using namespace std;
char** CallStack(int& num)
{
int nptrs;
#define SIZE 100
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, SIZE);
printf("backtrace() returned %d addressesn", nptrs);
strings = backtrace_symbols(buffer, nptrs);
num = nptrs;
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
/* for (j = 0; j < nptrs; j++)
{
printf("%sn", strings[j]);
strcpy(prt[j], strings[j]);
}
free(strings);*/
return strings;
}
void test1()
{
try
{
throw(5);
}
catch(...)
{
int num = 0;
char* *ptr = CallStack(num);
for (int j = 0; j < num; j++)
{
printf("%sn", ptr[j]);
}
free(ptr);
}
}
int main()
{
test1();
return 0;
}
上面打印出来的内容中,没有函数名,只有堆栈信息。加入-rdynamic 就可以输出函数名称了。