Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value and type of the current exception in C++ using gdb?

gdb allows one to catch exceptions when they're thrown, and when they're caught. But sometimes the line an exception is thrown has no symbols, or a breakpoint is triggered during exception handling. How do I inspect the value of the current exception?

like image 548
Matt Joiner Avatar asked May 01 '13 05:05

Matt Joiner


People also ask

How do I debug an exception in GDB?

To make the debugger catch all exceptions before any stack unwinding takes place, set a breakpoint on __raise_exception (see section Breakpoints, watchpoints, and catchpoints).

What is __ Cxa_throw?

__cxa_throw. External interface to throw in the C++ support library. Takes three arguments: an exception object, a typeinfo for that object, and a pointer to the destructor to call when we are done with that object.

What is Catchpoint GDB?

GDB catchpoints are a nice feature which allows you to intercept certain interesting things that your program might do. Most noticeably when a C++ exception is thrown, asserts, syscalls and signals.

How do I list breakpoints in GDB?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.


1 Answers

Earlier answers were correct when written (in 2013), but since then gdb and libstdc++ have changed.

libstdc++ now has some hooks that let gdb interact more nicely with the exception system. In particular, there is now enough information exposed for gdb to provide a $_exception convenience variable to the user. This variable holds the exception being thrown. It is only valid at exactly the spot where the exception is being caught; which you can stop at using catch catch.

See the page from the manual for details.

like image 124
Tom Tromey Avatar answered Oct 02 '22 14:10

Tom Tromey