C++ display stack trace on exception

0 votes
asked Mar 27, 2009 by rlbond

I want to have a way to report the stack trace to the user if an exception is thrown. What is the best way to do this? Does it take huge amounts of extra code?

To answer questions:

I'd like it to be portable if possible. I want information to pop up, so the user can copy the stack trace and email it to me if an error comes up.

12 Answers

0 votes
answered Mar 27, 2009 by andrew-grant

It depends which platform.

On GCC it's pretty trivial, see this post for more details.

On MSVC then you can use the StackWalker library that handles all of the underlying API calls needed for Windows.

You'll have to figure out the best way to integrate this functionality into your app, but the amount of code you need to write should be minimal.

0 votes
answered Mar 6, 2010 by hplbsh

On Windows, check out BugTrap. Its not longer at the original link, but its still available on CodeProject.

0 votes
answered Mar 19, 2012 by nico

AFAIK libunwind is quite portable and so far I haven't found anything easier to use.

0 votes
answered Mar 16, 2013 by boris

Cpp-tool ex_diag - easyweight, multiplatform, minimal resource using, simple and flexible at trace.

0 votes
answered Mar 23, 2013 by tasos-parisinos

on linux with g++ check out this lib


it does all the work for you

0 votes
answered Mar 30, 2013 by bobobobo

Unix: backtrace

Mac: backtrace

Windows: CaptureBackTrace

0 votes
answered Mar 12, 2014 by thomas-tempelmann

Andrew Grant's answer does not help getting a stack trace of the throwing function, at least not with GCC, because a throw statement does not save the current stack trace on its own, and the catch handler won't have access to the stack trace at that point any more.

The only way - using GCC - to solve this is to make sure to generate a stack trace at the point of the throw instruction, and save that with the exception object.

This method requires, of course, that every code that throws an exception uses that particular Exception class.

Update 11 July 2017: For some helpful code, take a look at cahit beyaz's answer, which points to http://stacktrace.sourceforge.net - I haven't used it yet but it looks promising.

0 votes
answered Mar 23, 2014 by orlin-georgiev

Poppy can gather not only the stack trace, but also parameter values, local variables, etc. - everything leading to the crash.

0 votes
answered Mar 11, 2015 by marcos-fuentes

The following code stops the execution right after an exception is thrown. You need to set a windows_exception_handler along with a termination handler. I tested this in MinGW 32bits.

void beforeCrash(void);

static const bool SET_TERMINATE = std::set_terminate(beforeCrash);

void beforeCrash() {

int main(int argc, char *argv[])

Check the following code for the windows_exception_handler function: http://www.codedisqus.com/0ziVPgVPUk/exception-handling-and-stacktrace-under-windows-mingwgcc.html

0 votes
answered Mar 9, 2016 by mattfarley

I have a similar problem, and though I like portability, I only need gcc support. In gcc, execinfo.h and the backtrace calls are available. To demangle the function names, Mr. Bingmann has a nice piece of code. To dump a backtrace on an exception, I create an exception that prints the backtrace in the constructor. If I were expecting this to work with an exception thrown in a library, it might require rebuilding/linking so that the backtracing exception is used.

#Makefile with flags for printing backtrace with function names
# compile with symbols for backtrace
# add symbols to dynamic symbol table for backtrace
turducken: turducken.cc

#include <cstdio>
#include <stdexcept>
#include <execinfo.h>
#include "stacktrace.h" /* https://panthema.net/2008/0901-stacktrace-demangled/ */

// simple exception that prints backtrace when constructed
class btoverflow_error: public std::overflow_error
    btoverflow_error( const std::string& arg ) :
        std::overflow_error( arg )

void chicken(void)
    throw btoverflow_error( "too big" );

void duck(void)

void turkey(void)

int main( int argc, char *argv[])
    catch( btoverflow_error e)
        printf( "caught exception: %s\n", e.what() );

Compiling and running this with gcc 4.8.4 yields a backtrace with nicely unmangled C++ function names:

stack trace: ./turducken : btoverflow_error::btoverflow_error(std::string const&)+0x43 ./turducken : chicken()+0x48 ./turducken : duck()+0x9 ./turducken : turkey()+0x9 ./turducken : main()+0x15 /lib/x86_64-linux-gnu/libc.so.6 : __libc_start_main()+0xf5 ./turducken() [0x401629]

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter