Disabling Optimizations when Debugging .NET Framework Source Code

Ok, you think you’re cool when you get the capability to debug the .NET Framework source code all set up. You’re like, “I am all powerful!” Then you start noticing the oddities ….

“Wait, why I can’t get the value of that variable?!”

“Why did it step there? It should have stepped here?!”

 

The problem is that ‘you’re debugging against retail-optimized code’. Fortunately, someone at Microsoft handed out the trick to disable these optimizations. Check out this link for more info, but basically there are only a few steps:

  • Create a .cmd file that sets an environment variable and then launches Visual Studio. Name it whatever you want (e.g. DisableOptimizationsInVisualStudio.cmd). It’s contents should be:
set COMPLUS_ZapDisable=1
cd /d "%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\ide\"
start devenv.exe
exit
  • Launch Visual Studio with this .cmd file.
  • Once in Visual Studio, disable the Visual Studio hosting process:

Right click on your project and choose “Properties”.
Choose the “Debug” tab and uncheck “Enable the Visual Studio Hosting Process”.

  • Launch your application in the debugger.

 

Once again, you are all-powerful.

 

For more information (in reverse chronological order):

  • Apparently, you can now get all the source code, at once, from Microsoft and without having to resort to the .NET Mass Downloader. I have not tried this yet, but check out this link for more info.
  • The CodePlex site for the .NET Mass Downloader
  • John Robbins’ blog post about the 1.5 version of the .NET Mass Downloader
  • John Robbins’ blog post about the 1.2 version of the .NET Mass Downloader
  • John Robbins’ blog post about the 1.1 version of the .NET Mass Downloader
  • The initial release of the .NET Mass Downloader by John Robbins and Kerem Kusmezer
  • Shawn Burke’s original post on how to configure Visual Studio to debug .NET Framework source code

I should also mention that I used the .NET Mass Downloader on the following directories in order to pull down .NET 2.0, .NET 3.0, and .NET 3.5. I was not aware (prior) that most of the assemblies for .NET 3.0 and .NET 3.5 were at “C:\Program Files\Reference Assemblies\Microsoft\Framework”. That represents a departure from the original location at “C:\Windows\Microsoft.NET\Framework”.

  • C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
  • C:\WINDOWS\Microsoft.NET\Framework\v3.0
  • C:\WINDOWS\Microsoft.NET\Framework\v3.5
  • C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0
  • C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5