Giter Site home page Giter Site logo

backtrace's Introduction

backtrace

一个可以打印c++异常堆栈的库


支持系统

Linux

安装

1.自行安装libunwind到系统中

2.使用cmake安装backtrace:

  $ mkdir -p build
  $ cd build
  $ cmake ..
  $ make
  $ make install

使用

  • 使用backtrace::getBacktrace()获取异常堆栈信息
#include <iostream>
#include <exception>
#include <backtrace.h>

struct MyException : public std::exception
{
    virtual const char* what() const noexcept {
        return "MyException";
    }
};

void foo()
{
    throw MyException();
}

int main() {
    try {
        foo();
    } catch (std::exception& e) {
        std::cout << "Exception Info:" << e.what() << std::endl;
        std::cout << "Exception Backtrace:\n" << backtrace::getBacktrace() << std::endl;
    }
}

编译

  • 编译源码文件时,需要使用-fno-inline编译选项
  • 链接时需要链接backtrace和unwind
  $ g++ -g -std=c++11 -fno-inline -lbacktrace -lunwind test.cpp -o test

输出&定位

  • 上述demo会输出如下内容:
Exception Info:MyException
Exception Backtrace:
0x0000000000401749 <std::exception::exception()+0x11>
0x00000000004016a6 <MyException::MyException()+0x18>
0x000000000040153b <foo()+0x25>
0x000000000040155b <main+0xe>
0x00007f77b12a0830 <__libc_start_main+0xf0>
0x0000000000401449 <_start+0x29>
  • 定位到具体源码文件&行号:
  $ addr2line -Cif -e test 0x000000000040153b
  foo()
  /root/gits/backtrace/test/test.cpp:14

backtrace's People

Contributors

yyzybb537 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.