views:

67

answers:

4

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?

+1  A: 

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.

Patrick
+1, but not really generic - I develop cross-platform code.(I suppose I can add an o/s check)
Mawg
You could add a hidden command line option to replace the IsDebuggerPresent call. Then you need to remember to add this option if you want to debug.For the BreakPoint function you could use an assert that always goes off.
Patrick
+1  A: 

If you are on a unix OS, you can do something like:

raise(SIGTRAP); 
ergosys
RedMeat, +1, but not really generic - I develop cross-platform code. (I suppose I can add an o/s check)
Mawg
+1  A: 

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)

f4
Mawg
Please accept my apologies for taking so long to get back to you. You answer is spot on. here's my actual code... #if TESTING==UNIT_TEST #define BREAK_IF_DEBUGGING asm("int3"); #else #if TESTING==SOFTWARE_INTEGRATION_TEST #define BREAK_IF_DEBUGGING asm("int3"); #else #define BREAK_IF_DEBUGGING ; #endif #endif
Mawg
A: 

How about

define BREAK_IF_DEBUGGING assert(false);

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

rleir
The problem with assert() is that it calls abort() which ends the process.
SiegeX