14

I can tell the gdb debugger to stop as soon as any C++ exception is thrown by setting a catchpoint with the gdb command

catch throw

However, is there any way to only stop at uncaught C++ exceptions (like in C# or Java)? This would make it much easier to find bugs in some situations.

Thanks!

emkey08
  • 5,059
  • 3
  • 33
  • 34
  • related - [How do I find where an exception was thrown in C++?](http://stackoverflow.com/q/2443135/203667) – jschmier Apr 19 '11 at 15:38
  • I don't think I understand - how do you determine if an exception is uncaught other than by letting it unroll until it breaks through the toplevel? – Mat Apr 19 '11 at 15:39
  • Well, I guess gdb would have to check whether the exception is caught or not *before* actually unwinding the stack. I don't know how C# or Java solve this, but I'm just curious if it can be done in C++ anyhow. – emkey08 Apr 19 '11 at 15:43
  • 1
    @Mat: depends on the C++ implementation, but it's perfectly feasible that the exception-handling code can look up the stack twice: once to find out whether there's a catch point, and then again to unwind the stack. Note that it is implementation-defined whether an uncaught exception results in destructors being called all the way up the stack, or an immediate call to `terminate` without stack unwinding. – Steve Jessop Apr 19 '11 at 15:48
  • The manual way (if exceptions are exceptional) is to `catch throw` and `catch catch`. That will stop whenever an exception is thrown (annotate where) and then when it is caught, if after `continue` the program calls terminate with an uncaught exception (i.e. does not stop in the `catch catch` you know where the last throw occurred) – David Rodríguez - dribeas Apr 19 '11 at 15:50
  • @Steve Jessop: thanks for the information, that makes sens. – Mat Apr 19 '11 at 15:52
  • 1
    The GNU C++ exception implementation actually _does_ walk the whole stack to see if there is a handler before it pops any frames. There are some languages where the handler must run _on top_ of the frame that threw the exception (Common Lisp?), and the folks designing the "new" GNU C++ exception handling system wanted both C++-like and CL-like languages to be able to share stacks and exception machinery. So the information is there. (Unfortunately, I don't know the answer to the actual question --- whether GDB can catch these.) – Jim Blandy Apr 19 '11 at 16:34
  • (I never used GDB's features in this area because they were always broken. And I was a GDB developer. :( ) – Jim Blandy Apr 19 '11 at 16:38

1 Answers1

15

If an exception is uncaught, the special library function terminate() is automatically called. Terminate is actually a pointer to a function and default value is the Standard C library function abort(). You may be able to set a breakpoint on the call to the abort() function and identify the location of the uncaught exception from there.

break abort
...
run
...
bt

You can install your own terminate() function by using std::set_terminate(). You should be able to set a breakpoint on your terminate function in gdb. You may be able to generate a stack backtrace from your terminate() function and this backtrace may help in identifying the location of the exception. Additional details are provided here.

Community
  • 1
  • 1
jschmier
  • 15,458
  • 6
  • 54
  • 72