views:

153

answers:

3

Hi!

I've found information about this feature on SO some time ago, but the topic was a duplicate of Hidden Features of Visual Studio (2005-2008)? and I can't find it anymore.

I want to use something like this:

#ifdef DEBUG
#define break_here(condition) if (condition) ... // don't remember, what must be here
#else
#define break_here(condition) if (condition) return H_FAIL;
#endif
//...
hresult = do_something(...);
break_here(hresult != H_OK);
//...
var = do_other_thing(...);
break_here(var > MAX_VAR);

It must behave like a breakpoint on error. It's something like assertion, but no dialogs, and more light-weight.

I can't use normal breakpoints here, because my module is a part of several projects and can be edited in several VS solutions. This causes breakpoints, which was set in one solution, shift somewhere in source, when code edited in other solution.

+8  A: 

Take a look at DebugBreak

Causes a breakpoint exception to occur in the current process. This allows the calling thread to signal the debugger to handle the exception.

example:

 var = do_other_thing(...);
 if (var > MAX_VAR)
      DebugBreak();
crashmstr
Thanks! I've found it too few seconds ago :) It's strange: I've search about a hour - nothing found. And after question is asked, the right answer was found in documentation in one minute :)
zxcat
+2  A: 

Maybe this on should help: http://stackoverflow.com/questions/657724/how-to-add-a-conditional-breakpoint-in-visual-c/739752

macs
Strange, but your link contains the right answer too. I didn't open that topic, because it's title sounds like something other - the real Conditional Breakpoints, not what I need.
zxcat
A: 

I forget, I need ARM builds too and one of them is compiled not in MS Visual Studio :)

Moreover, it's better for me not to link additional code in library-version of my module. The need to include "winbase.h" for DebugBreak() was one "bad thing" of it, the better is to have some intrinsic. But this is little "bad thing", because there will be no breakpoints in final release :)

With help of crashmstr's answer I've found the alternatives of DebugBreak(). And now I'm using the following construction:

#ifdef _DEBUG

    #ifdef _MSC_VER
      #ifdef _X86_
        #define myDebugBreak { __asm { int 3 } }
      #else
        #define myDebugBreak  { __debugbreak(); } // need <intrin.h>
      #endif
    #else
      #define myDebugBreak { asm { trap } } // GCC/XCode ARM11 variant
    #endif

#else

      #define myDebugBreak

#endif

#define break_here(condition) if (condition) { myDebugBreak; return H_FAIL; }
zxcat