打印c++堆栈调用

#include 
#include 

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    // 编译

打印c++堆栈调用_第1张图片

c/c++ backtrace打印函数调用栈_c++ 打印堆栈_一条晓鱼的博客-CSDN博客

你可能感兴趣的:(c++,算法,开发语言)