38

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;
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
danatel
  • 4,844
  • 11
  • 48
  • 62
  • Could you please change the Question-Title? This is not just a Visual Studio issue but is interesting for other compilers too. – MaBe Nov 10 '16 at 09:18

6 Answers6

66

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().

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • 1
    Maybe I'm missing something but it's `__debugbreak()` - without underline in the middle. – NPS Aug 24 '13 at 19:20
19
DebugBreak(void)

From Winbase.h.

MSDN

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90
  • 1
    I'd like to add this [quote from MSDN](https://msdn.microsoft.com/en-us/library/ea9yy3ey.aspx) regarding the difference between `DebugBreak` and `__debugbreak`: _Because DebugBreak is a call to a system function, system debug symbols must be installed to ensure the correct call stack information is displayed after breaking. Otherwise, the call stack information displayed by the debugger may be off by one frame. If you use __debugbreak, symbols are not required._ – sunny moon Jun 22 '16 at 21:13
9

You could use this in C or C++

__asm
{
    int 3
}
Indy9000
  • 8,651
  • 2
  • 32
  • 37
3

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);
}
foraidt
  • 5,519
  • 5
  • 52
  • 80
0

How about using a Debug or Trace method to output the console info. That may be a better approach than relying on breakpoints.

Cody C
  • 4,757
  • 3
  • 29
  • 36
  • The UnitTest++ framework does the output. I insert statements such as CHECK(pi < 4); and if pi is 31 the framework prints the error. There are like 500 tests and the number is growing. They usually do not fail. – danatel Mar 05 '10 at 19:18
0

How often do you check out the project from SVN? This is typically something I only do once per project, or when I rebuild my PC.

If you also checkin the project files, the breakpoints should be stored in the project files.

I think it is in the .suo file. You could also put that under SVN control if you wanted, though I prefer not to.

codenheim
  • 20,467
  • 1
  • 59
  • 80
  • As often as possible to be sure that all new files have been added before commit and no dependecies are missing. It is also nice to keep old build directories on the disc to have quick access to the old versions of binaries. – danatel Mar 05 '10 at 19:21
  • Good point. We also do smoke test builds and that didn't occur to me. – codenheim Mar 05 '10 at 19:40