开发者

Visual Studio - can be a breakpoint called from code?

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;


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


DebugBreak(void)

From Winbase.h.

MSDN


You could use this in C or C++

__asm
{
    int 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);
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜