24

I have just started using c++ exceptions and want to get it right. What I have in mind is to generate some sort of backtrace information when exceptions are caught. Initially I had ideas similar to Call-stack for exceptions in C++ but eventually figured out that's not quite good.

I have also read How to generate a stacktrace when my gcc C++ app crashes but do not want to add more complexity to my current project. Since, I only need the backtracing when in debug mode, I was hoping I could be using gdb for that purpose.

My strategy has been to insert breakpoint in the catch block and then go up through the call stack to exactly pinpoint why the exception was thrown in the first place (or what caused it)? Unfortunatly, I cannot seem to be able to do this since when gdb reaches the breakpoint, it clears the call stack and I can only see main (that's where I catch). Is this supposed to happen or am I doing something wrong here?

Edit: I just like to summarize the methods here for other folks:

1st Method (by paulsm4). Set a catchpoint via catch throw for catching on throw or catch catch for catching on catch! Then call backtrace

2nd Method (by aschepler) Set a breakpoint on __cxa_throw and then backtrace

3rd Method (in Qt Creator -- if you happen to use) You can easily set a breakpoint on throw or catch!

Edit_2: Using Qt Creator debugger, it seems that setting a breakpoint on __cxa_begin_catch is also an equivalent to catch catch :)

Community
  • 1
  • 1
mmirzadeh
  • 6,893
  • 8
  • 36
  • 47
  • 1
    Try checking out this question's answers: http://stackoverflow.com/questions/2443135/how-do-i-find-where-an-exception-was-thrown-in-c – MCKapur May 15 '12 at 00:03
  • A quick test for g++/Linux shows it might be useful to set a breakpoint at the internal function `__cxa_throw`. – aschepler May 15 '12 at 00:07
  • 3
    ... and now that I read @paulsm4's answer, it looks like the gdb command `catch throw` does essentially that. (If I do both the `break` and the `catch`, gdb notes to me that both use the same pc address.) – aschepler May 15 '12 at 00:10
  • @aschepler You are correct. setting a breakpoint on `__cxa_throw` also works. – mmirzadeh May 15 '12 at 00:27

2 Answers2

21

This this:

http://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html

You can use catchpoints to cause the debugger to stop for certain kinds of program events, such as C++ exceptions or the loading of a shared library. Use the catch command to set a catchpoint.

So the answer should be "yes", and it should avoid the problems with the two links you cited.

Please post back if it helped! Personally, I've never tried this GDB feature myself :)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 5
    YES :D It seem to be working. right now I can insert a catchpoint using `catch throw`. Once the exception is thrown I can use `backtrace` to get the full call stack :D :D. Thank you so much – mmirzadeh May 15 '12 at 00:24
6

Summary of answers from the comments:

1st Method (by paulsm4). Set a catchpoint via catch throw for catching on throw or catch catch for catching on catch! Then call backtrace

2nd Method (by aschepler) Set a breakpoint on __cxa_throw and then backtrace

3rd Method (in Qt Creator -- if you happen to use) You can easily set a breakpoint on throw or catch!

Using Qt Creator debugger, it seems that setting a breakpoint on __cxa_begin_catch is also an equivalent to catch catch

amo
  • 4,082
  • 5
  • 28
  • 42