- C++ 11 and above. Should work fine on most linux distributions.
<execinfo.h>: usebacktrace()to get call stacks.<cxxabi.h>: useabi::__cxa_demangle()to demangle symbol names.- For symbol resolution, compile with
-rdynamicor haveaddr2lineinstalled. - [opt] For relative address resolution, must be compiled with
-ldlflag (usedl_addr()from<dlfcn.h>). - [opt] For source code resolution, must be compiled with
-gflag and installaddr2linefrom GNU Binutils, also usepopenandfork(from<unistd.h>) to call. Tested on binutils version 2.31.1. - If compiled with
-O1or above, please checkFrame::inlined_byto get inlined frames.
Only need 1 header file bttrack.h and 1 source file bttrack.cpp. Compile like:
# directly add to your code
g++ -o prog -O0 -g -rdynamic -ldl bttrack.cpp prog.cpp
# build as shared library
g++ -o libbttrack.so -O3 -g -ldl -shared -fPIC bttrack.cpp
g++ -o prog -O0 -g -rdynamic -L. -lbttrack prog.cpp./runtest.sh test_001.cpp- Basic usage (see
test_001.cpp):- Record backtrace:
Record(id, score=1) - Get recorded stack frames:
Dump(id, output) - To human readable:
StackFramesToString(records, print_symbol=true) - To JSON:
StackFramesToJson(records, indent=2) - Example:
- Record backtrace:
#include "bttrack.h"
#include <iostream>
int main() {
// backtrace can be recorded in multiple channels
const uint8_t id = 0;
// record
bttrack::Record(id);
// dump result
std::vector<bttrack::StackFrames> records;
bttrack::Dump(id, records);
// parse & print result
std::string readable = bttrack::StackFramesToString(records);
std::string json = bttrack::StackFramesToJson(records, 2);
std::cout << readable << std::endl << json << std::endl;
return 0;
}- Advanced usage (see
test_002.cpp):- Get backtrace:
GetBacktrace(stack) - Manually record at anytime:
Record(id, stack, score=1)
- Get backtrace:
MIT License. All rights reserved.