C++添加仅在调试时运行的调试代码
正如问题所解释的:我想添加一些调试代码,这些代码仅在程序附加到调试器时运行。我想这个flag
或pre-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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题可能意味着以下两件事之一:
基于构建配置
这可以通过使用与您的编译器相关的预处理器宏来解决(例如 _Win32 CRT 的DEBUG)。
基于是否附加调试器
这可以通过几种不同的方式来解决。
全局布尔变量
我发现的一种方法是定义一个初始化为
false
的全局布尔变量,如下所示:当我使用调试器附加到代码时,中断在代码中并通过 Watch 窗口用
true
覆盖gDebug
。然后,如果设置为 true,您可以添加有条件运行的代码:注册表项
定义一个
DWORD
注册表值,该值初始化为0
,但是您可以通过注册表编辑器覆盖为1
。然后,您可以将此注册表值设置为
1
为调试代码的条件。这可能是一个更好的替代方案,因为您可以从外部控制该值,而无需中断调试器以在适当的时间。This question could mean 1 of 2 things:
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:And when I have attached to the code with my debugger, break in the code and override
gDebug
withtrue
via the Watch window. Then you can add code that runs conditionally if this is set is true:Registry key
Define a
DWORD
registry value which is initialised to0
, but you can override to1
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.如果您希望在调试/发布版本中包含或不包含某些代码,通常会为调试版本定义
_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.
在程序中将全局变量
in_debugger
设置为false
。将此变量设为true
的条件是所有仅调试代码。连接调试器时,将变量设置为true
。Set a global variable
in_debugger
tofalse
in your program. Condition all your debug-only code on this variable beingtrue
. Set the variable totrue
when you attach your debugger.对此还有一种更直接的方法。您可以在调试器会话期间手动调用您选择的函数:
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