简体   繁体   中英

Embedding GDB breakpoints

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.

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.

Setting this:

catch throw

should give you what you are after.

See this section of the user manual .

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