views:

169

answers:

6

While I am aware of the differences between debug and release builds, I am curious if attaching the debugger to a process (built release or debug) changes that processes behaviour?

For reference, I'm developing on HP 11.31 Itanium but still am curious for the general case.

+1  A: 

It certainly can, depending on the platform and the method of debugging. For example, when debugging on Windows, there is actually the IsDebuggerPresent function. As noted that function can be circumvented, but then there are other means. So basically, it's complicated.

Matthew Flaschen
Waht you are saying is that it's possible for an application to find out, but I doubt that's what the questioner wanted to know. It's much more useful to explain many ways in which an application can act differently *without intending to*, since that's the case that most often comes up in debugging.
PanCrit
The question wasn't specific regarding intentional/unintentional changes of behavior. As you note, there can be both. Other answerers have delve a little into the mechanisms of unintentional changes in debug mode. As noted, it is relatively common that the slower diagnostic memory allocator is also a little more forgiving of certain errors.
Matthew Flaschen
A: 

Yes, I've often found that attaching a debugger to a process instantly makes bugs disappear, only to have them reappear when I compile my app in release mode. Unfortunately I usually can't really ask all my users to open a debugger just to run my app, so it can be quite frustrating.

davr
Have you attached the debugger to a release version or did you compile with debug flags?
ojblass
Compilation with debug flags often makes memory behave a little different. I am curious about the act of attaching the debugger. I am not really after the effect of debug vs. release builds.
ojblass
+5  A: 

http://en.wikipedia.org/wiki/Heisenbug#Heisenbug

Of course, attaching a debugger will change the timing (which can change e.g. thread race conditions), and also some system calls can detect if a debugger is attached.

Brian
+1  A: 

Yep, lots of things inside the Windows data structures change when a debugger is attached. It changes how memory is allocated/freed, it adds additional housekeeping code and "markers" on the stack (Ever noticed the F00D values in newly allocated memory) in fact many of the changes are used by anti-debuggers to detect if an application is being debugged.

In interpreted languages (Java, .NET) the runtime will often generate different machine instructions when running under a debugger to help it trap and display exceptions, show the original code, etc. It will usually generate unoptimized code as well when a debugger is attached.

Some of these changes affect the way the software behaves and can result complicate transient bugs that are caused by optimizations or extremely fine timinig dependencies.

Paul Alexander
A: 

Another thing to keep in mind is that for multithreaded apps attaching the debugger definitely can yield very different results. These are the kind of things referred to as "Heisenbugs."

BobbyShaftoe
A: 

Sure, in multithreaded apps, attaching a debugger can yield different result. However, how about the codes which are not related to threads?

I also have this problem. Strangely, release build which a debugger is attached to doesn't have problems, but when a debugger is not attached, it has problems. If it is launched first and a debugger is attached to it, it also shows the same problems.

JongAm Park