1. 6
  1. 1

    (Reposting the comment I left on the blog post).

    Personally, I like using gdb’s breakpoint commands to automatically print the backtrace (with arguments and/or locals!) and continue.

    (gdb) b file.cpp:1234
    (gdb) commands
    > bt 10
    > cont
    > end
    (gdb) run

    You could probably find some way to save the output to a file so its greppable or whatever, too.

    1. 1

      SystemTap can also be pretty useful for getting stacktraces (and without requiring any modifications to your source code):

      $ stap --ldd -e 'probe process("a.out").function("foo") { print_ubacktrace(); }' -c ./a.out 
       0x4004b6 : foo+0x0/0x7 [/tmp/a.out]
       0x4004cb : bar+0xe/0x10 [/tmp/a.out]
       0x4004e6 : main+0x19/0x23 [/tmp/a.out]
       0x7f40a3a91b45 : __libc_start_main+0xf5/0x1c0 [/lib/x86_64-linux-gnu/libc-2.19.so]
       0x4003e9 : _start+0x29/0x30 [/tmp/a.out]

      (Downside: does require some special privileges, though not necessarily root.)