1、概述
我们知道Linux程序如果使用-g编译,若程序发生崩溃(coredump),是可以使用gdb调试生成的dump文件,找到崩溃的位置的。
然后C++有一些组件是提供崩溃堆栈打印的,本文给出如下几种方法。
2 、使用boost::stacktrace
boost::stacktrace是boost1.65版本以后支持的堆栈打印功能,使用前请确保安装了libboost-stacktrace;
如果没有安装,对于ubuntu系统,可使用如下方法安装
sudo apt-get install libboost-stacktrace-dev
测试代码
#include
#include
#include
#include
#include
#include
#include
#define BOOST_STACKTRACE_USE_ADDR2LINE
#include
char *str = (char *)"test";
void core_test()
{
str[1] = 'T';
}
// This is definitely not async-signal-safe. Let's hope it doesn't crash or hang.
void handler(int signo)
{
if (std::ofstream file_stream("my_stacktrace.log", std::ios::trunc); file_stream.is_open())
{
std::stringstream ss;
ss |