美文网首页
Linux下C/C++打印栈信息

Linux下C/C++打印栈信息

作者: 王晓宇_xiaoyuwang | 来源:发表于2019-01-16 16:54 被阅读0次

http://man7.org/linux/man-pages/man3/backtrace.3.html

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BT_BUF_SIZE 100

void
myfunc3(void)
{
  int j, nptrs;
  void *buffer[BT_BUF_SIZE];
  char **strings;

  nptrs = backtrace(buffer, BT_BUF_SIZE);
  printf("backtrace() returned %d addresses\n", nptrs);

  /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
  would produce similar output to the following: */

  strings = backtrace_symbols(buffer, nptrs);
  if (strings == NULL) {
    perror("backtrace_symbols");
    exit(EXIT_FAILURE);
  }

  for (j = 0; j < nptrs; j++)
    printf("%s\n", strings[j]);

  free(strings);
}

相关文章

网友评论

      本文标题:Linux下C/C++打印栈信息

      本文链接:https://www.haomeiwen.com/subject/qugmdqtx.html