Visual Studio - can be a breakpoint called from code?

20,924

Solution 1

Use the __debugbreak() intrinsic(requires the inclusion of <intrin.h>).

Using __debugbreak() is preferable to directly writing __asm { int 3 } since inline assembly is not permitted when compiling code for the x64 architecture.

And for the record, on Linux and Mac, with GCC, I'm using __builtin_trap().

Solution 2

DebugBreak(void)

From Winbase.h.

MSDN

Solution 3

You could use this in C or C++

__asm
{
    int 3
}

Solution 4

If you are using VC6 (yes, outdated but still in use in some places/projects), DebugBreak() will work but you may end up in some obscure location deep withing Windows DLLs, from which you have to walk the stack up back into your code.

That's why I'm using ASSERT() in MFC or assert() in "standard" code.

Your example would work like this:

n = UnitTest::RunAllTests();
ASSERT(n == 0);
//assert(n == 0);
return n;

If you don't need a result and want it just for debugging, you can also do

if(0 != UnitTest::RunAllTests())
{
    ASSERT(FALSE);
    //assert(false);
}
Share:
20,924
danatel
Author by

danatel

Updated on August 25, 2020

Comments

  • danatel
    danatel over 3 years

    I have a unit test project based on UnitTest++. I usually put a breakpoint to the last line of the code so that the I can inspect the console when one of the tests fails:

      n = UnitTest::RunAllTests();
      if ( n != 0 )
      {
      // place breakpoint here    
        return n;
      }
      return n;
    

    But I have to reinsert it each time I check-out the code anew from SVN. Is it possible to somewhat place the breakpoint by the compiler?:

          n = UnitTest::RunAllTests();
          if ( n != 0 )
          {
          // place breakpoint here    
    #ifdef __MSVC__
            @!!!$$$??___BREAKPOINT;
    #endif
            return n;
          }
          return n;