Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ adding Debugging code that only runs when Debugging

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++.

like image 371
Wallter Avatar asked Nov 16 '10 23:11

Wallter


2 Answers

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.

like image 97
LeopardSkinPillBoxHat Avatar answered Sep 18 '22 02:09

LeopardSkinPillBoxHat


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.

like image 20
Matteo Italia Avatar answered Sep 17 '22 02:09

Matteo Italia