#include <execinfo.h>
#include <unistd.h>
#include <string.h>
void printContext(void)
{
int j, nptrs;
#define SIZE 100
#define FILE_NAME "./stack.out"
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, SIZE);
/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
would produce similar output to the following: */
FILE *fp = fopen( FILE_NAME, "a+" );
if (fp == NULL)
{
printf("[***PUBLIC***] Open stack file[%s] failure! /n",FILE_NAME);
return FALSE;
}
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
//exit(EXIT_FAILURE);
}
for (j = 0; j < nptrs; j++)
{
fprintf( fp, "[***PUBLIC***][%d][%s]/n", j, strings[j]);
}
free(strings);
}
C语言下打印调用栈
最新推荐文章于 2023-04-20 20:02:48 发布