简体   繁体   中英

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?

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 (eg 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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM