Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break when a specific exception type is thrown in GDB?

Tags:

c++

gdb

According to the documentation I can break on specific exception type by using conditional breakpoints. However the syntax for the condition isn't very clear to me:

condition bnum <expression> 

Looking at the expression syntax I think this is the pattern I need:

{type} addr

However, I don't know what I should pass for the addr argument. I tried the following:

(gdb) catch throw (gdb) condition 1 boost::bad_function_call * 

But it doesn't work (gdb breaks on all exception types).

Can anyone help?


Update

I also tried @Adam's suggestion, but it results in an error message:
(gdb) catch throw boost::bad_function_call Junk at end of arguments. 

Without boost:: namespace:

(gdb) catch throw bad_function_call Junk at end of arguments. 


Workaround

Breaking in the constructor of bad_function_call works.
like image 690
StackedCrooked Avatar asked Jul 26 '11 19:07

StackedCrooked


People also ask

How do you set a breakpoint in GDB at a specific line?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

What does breakpoint do in GDB?

A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops.

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.

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.


2 Answers

EDIT

The documentation suggests that catch throw <exceptname> can be used to break whenever an exception of type <exceptname> is thrown; however, that doesn't seem to work in practice.

(gdb) help catch Set catchpoints to catch events. Raised signals may be caught:         catch signal              - all signals         catch signal <signame>    - a particular signal Raised exceptions may be caught:         catch throw               - all exceptions, when thrown         catch throw <exceptname>  - a particular exception, when thrown         catch catch               - all exceptions, when caught         catch catch <exceptname>  - a particular exception, when caught Thread or process events may be caught:         catch thread_start        - any threads, just after creation         catch thread_exit         - any threads, just before expiration         catch thread_join         - any threads, just after joins Process events may be caught:         catch start               - any processes, just after creation         catch exit                - any processes, just before expiration         catch fork                - calls to fork()         catch vfork               - calls to vfork()         catch exec                - calls to exec() Dynamically-linked library events may be caught:         catch load                - loads of any library         catch load <libname>      - loads of a particular library         catch unload              - unloads of any library         catch unload <libname>    - unloads of a particular library The act of your program's execution stopping may also be caught:         catch stop  C++ exceptions may be caught:         catch throw               - all exceptions, when thrown         catch catch               - all exceptions, when caught Ada exceptions may be caught:         catch exception           - all exceptions, when raised         catch exception <name>    - a particular exception, when raised         catch exception unhandled - all unhandled exceptions, when raised         catch assert              - all failed assertions, when raised  Do "help set follow-fork-mode" for info on debugging your program after a fork or vfork is caught.  Do "help breakpoints" for info on other commands dealing with breakpoints. 
like image 91
Adam Rosenfield Avatar answered Sep 18 '22 01:09

Adam Rosenfield


When gdb command 'catch throw' fails, try this workaround :
(tested with Linux g++ 4.4.5/gdb 6.6)
1/ Add this code anywhere in the program to debug :

#include <stdexcept> #include <exception> #include <typeinfo>  struct __cxa_exception {     std::type_info *inf; }; struct __cxa_eh_globals {     __cxa_exception *exc; }; extern "C" __cxa_eh_globals* __cxa_get_globals(); const char* what_exc() {     __cxa_eh_globals* eh = __cxa_get_globals();     if (eh && eh->exc && eh->exc->inf)         return eh->exc->inf->name();     return NULL; } 

2/ In gdb you will then be able to filter exceptions with :

(gdb) break __cxa_begin_catch   (gdb) cond N (what_exc()?strstr(what_exc(),"exception_name"):0!=0)   

where N is the breakpoint number, and exception_name is the name of exception for which we wish to break.

like image 32
Francois Avatar answered Sep 18 '22 01:09

Francois