John Lam 1997 — 2026

Beware of Heisenberg effects

When you run things in a JIT system under a debugger, you aren’t always looking at what you think you’re looking at.

Let’s consider a simple piece of code:

`static int Add( int x, int y ) { return x + y; }

static int Main(string[] args) { return Add( 3, 4 ); }`

If you enabled optimizations in a Debug configuration build in VS.NET compiled and ran the program in a debugger, you can examine the generated x86 code. This is what you would see at the call site:

static void Main(string[] args) { Add( 3, 4 ); 00000000 push ebp 00000001 mov ebp,esp 00000003 push eax 00000004 push esi 00000005 mov dword ptr [ebp-4],ecx 00000008 mov edx,4 0000000d mov ecx,3 00000012 call dword ptr ds:[008F510Ch] 00000018 mov esi,eax 0000001a mov eax,esi 0000001c pop esi 0000001d mov esp,ebp 0000001f pop ebp 00000020 ret

Furthermore, if you step into the call instruction, this is what you would see at the other end:

00000000 push ebp ;setup stack frame 00000001 mov ebp,esp 00000003 sub esp,8 00000006 push edi 00000007 push esi 00000008 mov edi,ecx ;x and y are in 0000000a mov esi,edx ;ecx and edx 0000000c lea eax,[edi+esi] ;add the numbers 0000000f pop esi ;clean up stack frame 00000010 pop edi 00000011 mov esp,ebp 00000013 pop ebp 00000014 ret

Now, this hardly looks like optimized code now, does it? But we compiled using the /optimize switch, didn’t we?

As it turns out, we did compile using the /optimize switch. This causes the C# compiler to generate optimized CIL instructions. However, because we were running the code under the VS.NET debugger, which is aware of the CLR, the VS.NET debugger tells the CLR to turn off JIT optimizations [1].

If you ran your code outside of VS.NET, it would actually run using a different x86 instruction sequence. If you run it inside of VS.NET, you run using the instruction sequence shown above. Hence the Heisenberg effect: the act of observing the system causes the system to change.

Fortunately for us, we have a way around this. What we need to do is start the code outside of the scope of the VS.NET debugger, and then “break into” the code to see the generated x86 code. We can do this by taking advantage of the Just-In-Time debugging features of Windows. To do so, we need to insert a call to the Win32 DebugBreak() API in our code.

Here’s the stub that defines the signature of DebugBreak() for P/Invoke:

[ DllImport( "kernel32.dll" ) ] static extern void DebugBreak();

This is our modified Main() method:

static int Main(string[] args) { DebugBreak(); return Add( 3, 4 ); }

If you run the application from the command line, you should see the JIT debugging dialog appear.

Click on Debug, and select “New Instance of Microsoft Development Environment” from the Just-In-Time Debugging dialog.

In the Attach to Process dialog, make sure that the “Common Language Runtime” switch is NOT checked:

You will see that you are sitting in the _DbgBreakPoint@0: method. Step over the ret instruction in this method. You should now be at the tail end of the P/Invoke thunk that called the DebugBreak() API. The instructions should resemble:

003FA09F mov byte ptr [ebx+4],1 003FA0A3 cmp dword ptr ds:[793A7A54h],0 003FA0AA jne 003FA0C4 003FA0AC lea esp,[esi-18h] 003FA0AF mov dword ptr [ebx+8],edi 003FA0B2 add esp,8 003FA0B5 pop edi 003FA0B6 pop esi 003FA0B7 pop ebx 003FA0B8 pop ebp 003FA0B9 add esp,0Ch 003FA0BC ret

Step over this all of the instructions in this sequence until you hit the ret instruction. When you step over that instruction, your debugger will now be positioned at the call to Add( 3, 4 ). This is what you should see:

02E1005E mov eax,7 02E10063 ret

You’ll notice that the JIT compiler was smart enough to completely eliminate the call to the Add() method and replace it with the result of the constant calculation, 7.

As an exercise left to the reader, modify your Main method so that it looks like:

static void Main(string[] args) { Add( 3, 4 ); }

Examine the optimized x86 code using the techniques in this entry. Did you get what you expected?

[1] If you’re interested in writing a debugger, you can tell the CLR to turn off JIT optimizations by calling ICorDebugModule::EnableJITDebugging(), and passing false in the bAllowJitOpts parameter.