Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do debuggers manage to break on any throw?

In GDB and other debuggers it's possible to ask the debugger (using catch throw) to stop anytime an exception is thrown before the process passes said exception to the respective exception handler.

By what mechanism is this possible? Is there an OS signal that can be used to hook in? Is there a function pointer to monkey patch to allow this? Does it single step to make this happen?

Is this mechanism otherwise available outside the debugger?

like image 842
Catskul Avatar asked Mar 05 '19 22:03

Catskul


1 Answers

GDB sets a breakpoint on the library function which does the stack unwinding (__cxa_throw() for x86_64) to implement catch throw. It will use the same mechanism to set this breakpoint it uses to set any other type of code breakpoint.

By what mechanism is this possible? Is there an OS signal that can be used to hook in? Is there a function pointer to monkey patch to allow this? Does it single step to make this happen?

None of these. It is just a normal breakpoint on __cxa_throw(). GDB uses knowledge of the implementation of the C++ runtime, with all the disadvantages that brings. C++ exceptions are below the radar of the operating system, so the operating system would not know about them. The confusion stems from the fact that certain signals (e.g. segfaults) are called exceptions on Windows and can be handled in a very similar way to C++ exceptions on Windows when debugging. But this is making OS-signals (e.g. segfault) behaving like C++ exceptions, not the other way round.

See also ftp://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_30.html

like image 160
Johannes Overmann Avatar answered Sep 19 '22 08:09

Johannes Overmann