Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top