Wiki

Clone wiki

stack_trace / Home

stack_trace

This is a tiny library that allows you to obtain a stack trace in C++ code.

Deprecation notice: Though I will likely keep this project on bitbucket for the foreseeable future, I probably won't devote much more time to maintenance, as I consider it to be superseded by the dbg library which has no dependency on GPL or LGPL code when targeting MinGW.

A quick example

#include <algorithm>
#include <iostream>
#include <iterator>

#include <dbg/stack.hpp>

void e()
{
    dbg::stack s;
    std::copy(s.begin(), s.end(), std::ostream_iterator<dbg::stack_frame>(std::cout, "\n"));
}

void d() { e(); }
void c() { d(); }
void b() { c(); }
void a() { b(); }

int main()
{
    a();
    return 0;
}

The output:

0x4014b5: e() in P:\software\dbg\example\obj\mingw\debug\example.exe
0x4015bf: d() in P:\software\dbg\example\obj\mingw\debug\example.exe
0x4015cc: c() in P:\software\dbg\example\obj\mingw\debug\example.exe
0x4015d9: b() in P:\software\dbg\example\obj\mingw\debug\example.exe
0x4015e6: a() in P:\software\dbg\example\obj\mingw\debug\example.exe
0x401632: main in P:\software\dbg\example\obj\mingw\debug\example.exe
0x40124b: [unknown function] in P:\software\dbg\example\obj\mingw\debug\example.exe
0x401298: [unknown function] in P:\software\dbg\example\obj\mingw\debug\example.exe
0x7c817067: RegisterWaitForInputIdle in C:\WINDOWS\system32\kernel32.dll

Compiling and linking

It works on the following platforms/compilers:

  • Win32/Microsoft Visual C++ 2005
  • Win32/Microsoft Visual C++ 2008
  • Win32/MinGW 3.4.5 and above
  • Mac OS X 10.4 (Intel/PPC) and above/GCC 4.0.1 (and above?)
  • I've been told the code will work on Linux x86 and x64/g++ 4.5, though I can't confirm this.

When compiling with Visual C++, you will need to link against imagehlp.lib.

When compiling with MinGW, make sure your code is compiled with the -gstabs compiler flag. -gdb won't cut it. You'll also need to link against libbfd, libiberty and libimagehlp.

On Mac OS X, you should just be able to compile and go.

On Linux/g++ 4.5, you will need to make sure specify -ldl and -rdynamic on the link command line (assuming you're using g++ as a frontend to the linker). Information kindly supplied by Ioannis Papadopoulos.

In all cases, you will need to make sure that you compile with debugging symbols enabled.

Licensing

The code is released under the Boost Software License v1.0. However, libbfd is licensed under the GPL and libiberty is LGPL, so you will need to take those in to account if you are planning on distributing MinGW-built binaries.

Updated