#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>
#include <string.h>
void backtrace()
{
const int maxLevel = 200;
void* buffer[maxLevel];
int level = backtrace(buffer, maxLevel);
const int SIZE = 1024;
char cmd[SIZE] = "addr2line -C -f -e ";
char* prog = cmd + strlen(cmd);
int r = readlink("/proc/self/exe", prog, sizeof(cmd) - (prog-cmd)-1);
FILE* fp = popen(cmd, "w");
if (!fp)
{
perror("popen");
return;
}
for (int i = 0; i < level; ++i)
{
fprintf(fp, "%p\n", buffer[i]);
}
fclose(fp);
}
void foo(int, char*)
{
backtrace();
}
void bar(double)
{
foo(0, NULL);
}
int main()
{
bar(0.0);
return 0;
}
backtrace获取堆栈
最新推荐文章于 2024-12-24 15:54:21 发布