C++添加仅在调试时运行的调试代码

发布于 2024-10-02 10:45:23 字数 229 浏览 1 评论 0 原文

正如问题所解释的:我想添加一些调试代码,这些代码仅在程序附加到调试器时运行。我想这个flagpre-processor变量对于每个编译器来说都是不同的......

在我的例子中,我使用的是带有C++的Microsoft Visual Studio 2010。

我还在家里的另一台计算机上使用 Eclipse,运行 Ubuntu 10.4,并再次使用 C++。

As the question explains: I would like to add some debugging code that only runs when the program is attached to the debugger. I would imagine that this flag or pre-processor variable would be different for every compiler...

In my case I am using Microsoft Visual Studio 2010 with C++.

I also use Eclipse on another computer at home running Ubuntu 10.4 and again in C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

錯遇了你 2024-10-09 10:45:23

这个问题可能意味着以下两件事之一:

  1. 仅基于构建配置运行的代码(例如发布与调试)
  2. 仅在附加调试器时运行的代码

基于构建配置

这可以通过使用与您的编译器相关的预处理器宏来解决(例如 _Win32 CRT 的DEBUG)。

基于是否附加调试器

这可以通过几种不同的方式来解决。

全局布尔变量

我发现的一种方法是定义一个初始化为 false 的全局布尔变量,如下所示:

bool gDebug = false;

当我使用调试器附加到代码时,中断在代码中并通过 Watch 窗口用 true 覆盖 gDebug。然后,如果设置为 true,您可以添加有条件运行的代码:

if (gDebug)
{
    // Debugger is attached, so run this code
    // ...
}

注册表项

定义一个 DWORD 注册表值,该值初始化为 0,但是您可以通过注册表编辑器覆盖为1

然后,您可以将此注册表值设置为 1 为调试代码的条件。这可能是一个更好的替代方案,因为您可以从外部控制该值,而无需中断调试器以在适当的时间。

This question could mean 1 of 2 things:

  1. Code that only runs based on the build configuration (e.g. Release vs. Debug)
  2. Code that only runs when the debugger is attached

Based on build configuration

This can be solved by using the pre-processor macro relevant to your compiler (e.g. _DEBUG for the Win32 CRT).

Based on whether debugger is attached

This can be solved in several different ways.

Global boolean variable

One way I find is to define a global boolean variable which is initialised to false, like this:

bool gDebug = false;

And when I have attached to the code with my debugger, break in the code and override gDebug with true via the Watch window. Then you can add code that runs conditionally if this is set is true:

if (gDebug)
{
    // Debugger is attached, so run this code
    // ...
}

Registry key

Define a DWORD registry value which is initialised to 0, but you can override to 1 via the registry editor.

You then make your debug code conditional on this registry value being set to 1. This may be a better alternative as you can control this value externally without have to break in your debugger to set a global variable at the appropriate time.

幸福不弃 2024-10-09 10:45:23

如果您希望在调试/发布版本中包含或不包含某些代码,通常会为调试版本定义 _DEBUG 预处理器宏(至少在 MSVC++ CRT 中是惯例),但事实并非如此。不检测是否附加了调试器,它只是让您包含用于调试/发布版本的不同代码。

如果您想要的是附加调试器的运行时检查,则应使用 IsDebuggerPresent API,用于检测是否附加了用户模式调试器。

请注意,它不是 100% 可靠,因为通过一些不太困难的工作,调试器可以使其对您的应用程序撒谎。换句话说,这对于安全/反作弊保护和此类内容不利,更多的是为调试器提供额外的帮助(例如,正如页面本身所说,使用 OutputDeubgString 等)。此外,它不会检测内核模式调试器,内核模式调试器无论如何都可以做任何他们想做的事情。

无论如何,我建议您避免使用此函数来处理复杂的事情,因为在附加调试器时会引入不同的代码路径,这会使调试“奇怪”的错误变得相当困难。我间接使用的所有呈现这种行为的代码(例如几乎没有记录的 Windows 调试堆)总是让我头疼不已。

If you want to have some code included or not in debug/release builds, usually the _DEBUG preprocessor macro is defined for debug builds (at least, in MSVC++ CRT that is the convention), but it doesn't detect if a debugger is attached, it just let you include different code for debug/release builds.

If what you want is a runtime check for attached debuggers, you should use the IsDebuggerPresent API, which detects if a user-mode debugger is attached.

Notice that it's not 100% reliable since, with some not-so-difficult work, the debugger can make it lie to your application. In other terms, it's not good for security/anti-cheat protection and this kind of stuff, it's more to enable additional help to the debugger (e.g., as the page itself says, output more diagnostic info with OutputDeubgString, etc.). Moreover, it won't detect kernel-mode debuggers, that can do whatever they want anyway.

Anyhow, I advice you to avoid using this function for complicated stuff, since you're introducing different code paths when the debugger is attached, and this can make debugging "strange" bugs quite difficult. All the code I indirectly used which presented such behavior (e.g. the almost undocumented Windows debug heap) always gave me bad headaches.

时光与爱终年不遇 2024-10-09 10:45:23

在程序中将全局变量 in_debugger 设置为 false。将此变量设为 true 的条件是所有仅调试代码。连接调试器时,将变量设置为 true

Set a global variable in_debugger to false in your program. Condition all your debug-only code on this variable being true. Set the variable to true when you attach your debugger.

音栖息无 2024-10-09 10:45:23

对此还有一种更直接的方法。您可以在调试器会话期间手动调用您选择的函数:
http://sourceware.org/gdb/onlinedocs/gdb/Calling.html

问候,
马尔辛

There is an even more direct approach to this. You can manually call a function of your choice during the debugger session:
http://sourceware.org/gdb/onlinedocs/gdb/Calling.html

Regards,
Marcin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文