visual-studio Visual Studio - 可以是从代码调用的断点吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2389270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:37:47  来源:igfitidea点击:

Visual Studio - can be a breakpoint called from code?

c++visual-studiodebuggingvisual-c++breakpoints

提问by danatel

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:

我有一个基于 UnitTest++ 的单元测试项目。我通常在代码的最后一行放置一个断点,以便在其中一个测试失败时检查控制台:

  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?:

但是每次我从 SVN 重新检出代码时,我都必须重新插入它。编译器是否可以在某种程度上放置断点?:

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

回答by Gregory Pakosz

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

使用__debugbreak()内在的(需要包含<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.

使用__debugbreak()比直接编写更可取,__asm { int 3 }因为在为 x64 架构编译代码时不允许内联汇编。

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

为了记录,在 Linux 和 Mac 上,使用 GCC,我使用__builtin_trap().

回答by Clinton Pierce

DebugBreak(void)

From Winbase.h.

来自 Winbase.h。

MSDN

MSDN

回答by Indy9000

You could use this in C or C++

你可以在 C 或 C++ 中使用它

__asm
{
    int 3
}

回答by foraidt

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.

如果您使用的是 VC6(是的,已经过时但仍在某些地方/项目中使用),DebugBreak()则可以使用,但您最终可能会在 Windows DLL 深处的某个模糊位置,您必须从该位置将堆栈向上推回您的代码。

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

这就是我ASSERT()在 MFC 或assert()“标准”代码中使用的原因。

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);
}

回答by Cody C

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

如何使用 Debug 或 Trace 方法输出控制台信息。这可能是比依赖断点更好的方法。

回答by codenheim

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.

你多久检查一次 SVN 的项目?这通常是我每个项目只做一次的事情,或者当我重建我的 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.

我认为它在 .suo 文件中。如果您愿意,您也可以将其置于 SVN 控制之下,尽管我不想这样做。