Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding GDB breakpoints

Tags:

c++

debugging

gdb

I am writing an application that recovers from an error by throwing an exception.

However when debugging I would like my debugger to stop at the point of my error before the exception is thrown. Is there anyway to add some code that will cause GDB to interrupt the execution with some piece of code.

I am interested in both ARM and x86 architectures some something cross platform will be useful.

Edit: I am looking for some breakpoint instruction or a macros that wraps one.

like image 292
doron Avatar asked Dec 02 '25 06:12

doron


2 Answers

You could add a "breakpoint instruction" to your code. Most OS's that I know of ignores breakpoints if no debugger is attached.

It is compiler and processor dependent what instruction you use for this, but in most compilers there is an "intrinsic" or "builtin" function, such as DebugBreak(); for MS compilers and __builtin_trap(); for gcc and related.

Edit: __builtin_trap(); is a bad idea, as it will stop the code from running further at all. Instead, using inline assembler:

inline void DebugBreak()
{
#if defined(X86)
    __asm__ __volatile__("int 3");
#elif defined(ARM)
    __asm__ __volatile__("bkpt"); 
#else
  #error DebugBreak not defined for this architecture or no architecture defined.
#endif
}

should work for x86 & arm - you may want to have "better" names for the architecture choice macros - I don't do much work with varying architectures, so I haven't memorised exactly what the official names are.

like image 54
Mats Petersson Avatar answered Dec 03 '25 19:12

Mats Petersson


Setting this:

catch throw

should give you what you are after.

See this section of the user manual.

like image 35
trojanfoe Avatar answered Dec 03 '25 21:12

trojanfoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!