简体   繁体   中英

C/C++ in Eclipse, can I use a #define to set a breakpoint, but only when steping through code?

Years ago I had a little #define which I used in Borland C++ Builder. From memory, it was something approximately like

 #define BREAK_IF_DEBUGGING asm(0xA3);

or something like that. It relied on 0XA3 (or whatever it was) being the op code for the interrupt which Borland were using to trigger a breakpoint.

Can I do the same thing in Eclipse? (I'll probably wrap it in a few #idef ECLIPSE and #ifdef TESTING )

What I hope to achieve here is that
- the code compiles away to nothing in the release version, of course.
- if I run by unit tests with Ctrl-F11 then I don't want a breakpint to be triggered (which it won't because of Ctrl-F11 being "Run")
- if I "Run with Debug" using F11) then if executions hits any use of the macro then it will stop at a breakpoint.

Why? Because I want to set & forget. Just put one of these in each error leg (or embed it in my LOG_ERROR macro).

Often when I choose my initial breakpoint, it is too late, so this macro says "I think I want to run to line X, but if execution passes through one of those error branches first, I'd like to stop there & sniff around".

Whether you like the idea or not, can you tell me how to do it?

what about

#define BREAK_IF_DEBUGGING asm("int3");

(the lack of space between int and 3 is intentional : int 3 being encoded differently from other interrupts, the gnu assembler mark this difference with this special syntax)

You can use the Windows function IsDebuggerPresent (see http://msdn.microsoft.com/en-us/library/ms680345%28VS.85%29.aspx ) to check whether a debugger is attached to your process or not.

If a debugger is attached, you can use the BreakPoint function to trigger a breakpoint.

如果您使用的是Unix操作系统,则可以执行以下操作:

raise(SIGTRAP); 

How about

define BREAK_IF_DEBUGGING assert(false);

Clearly, you can make better use of assert().

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