Stop my program and go to Debugger without setting breakpoints (Visual Studio / GCC and C ++)

I found information about this feature on SO some time ago, but was the topic a duplicate of the Hidden Features of Visual Studio (2005-2008)? and I can’t find him 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 should behave like a breakpoint on error. This is a bit of a statement, but no dialogue and no more easy.

I cannot use the usual breakpoints here because my module is part of several projects and can be edited in several VS solutions. This causes breakpoints that were set in one solution to shift somewhere in the source when the code is edited in another solution.

+3
3

DebugBreak

. .

:

 var = do_other_thing(...);
 if (var > MAX_VAR)
      DebugBreak();
+9
+2

, ARM, MS Visual Studio:)

Also, I better not link the extra code in the library version of my module. The need to include "winbase.h" for DebugBreak () was one "bad" of it, the better to have some internal ones. But this is a little "bad", because in the final version there will be no breakpoints :)

Using crashmstr's answer , I found alternatives to DebugBreak () . And now I use 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; }
+2
source

Source: https://habr.com/ru/post/1717742/


All Articles