Reputation: 40145
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?
Upvotes: 1
Views: 473
Reputation: 821
How about
define BREAK_IF_DEBUGGING assert(false);
Clearly, you can make better use of assert().
Upvotes: 0
Reputation: 3852
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)
Upvotes: 3
Reputation: 49039
If you are on a unix OS, you can do something like:
raise(SIGTRAP);
Upvotes: 1
Reputation: 23629
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.
Upvotes: 1