Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux中打印函数堆栈

Linux中打印函数堆栈mongo源码目录下的util/stacktrace.cpp文件:// Copyright 2009. 10gen, Inc.
#include "mongo/util/stacktrace.h"#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <execinfo.h>namespace mongo {
  static const int maxBackTraceFrames = 20;  void printStackTrace(std::ostream& os) {
    void *b[maxBackTraceFrames];    int size = ::backtrace(b, maxBackTraceFrames);
    for (int i = 0; i < size; ++i) {
      os << std::hex << b[i] << std::dec << " ";
    } 
    os << std::endl;    char** strings;
    strings = ::backtrace_symbols(b, size);
    for (int i = 0; i < size; ++i) {
      os << " " << strings[i] << " ";
    } 
    os.flush();
    ::free(strings);
  }
}