Welcome to MSDN Blogs Sign in | Join | Help

How to disable optimizations when debugging Reference Source

When you debug code in the .NET Framework using the newly available Reference Source functionality in VS 2008, you may notice that many variables are not available for inspection.

image

This is because you're debugging against retail-optimized code.  In many cases, since you can still step through, this is something that's manageable.

But what if you really need to get a better idea of what's going on?  Fortunately, there is a way.

What you need to do is to tell the CLR not to load the pre-JIT (aka NGEN) images.  Here is how to do it. 

First, create a CMD file that sets an environment variable, then launches Visual Studio.  I called mine "NoOptDevEnv.cmd", and it's contents are as follows:

set COMPLUS_ZapDisable=1
cd /d "%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\ide\"
start devenv.exe
exit

I put this cmd file on my desktop.   When I want to disable optimizations, and only when I want to do this, I launch VS from this command. 

Once in my Visual Studio project, do the following steps:

1) Right click on your project file and choose "Properties"

2) Choose the "Debug" tab and uncheck "Enable the Visual Studio Hosting Process"

3) Launch your application in the debugger.

Now, you'll see full local and member variable information:

image

Finally, you may be asking yourself "What is the Visual Studio Hosting Process (aka VSHost), and what happens if I turn it off?"  And this is a good question.

For the most part, disabling VSHost won't have any major impacts, but it will disable two features that you may be using.

First, you will not be able to do "Debug In Zone", which allows you to debug a process in the context of a security zone such as "Internet" or "Intranet".  That won't work without VSHost.

Second, Design Time Expression Evaluation for class libraries will also not work.  What that means, for example, is that if you are developing a Class Library, you won't be able to execute code from it in the Immediate Window while under the debugger.

In general, I recommend re-enabling VSHost (undo step 2 above) when you are finished with your debugging.

Hope that helps!

Published Tuesday, January 29, 2008 7:35 PM by sburke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: How to disable optimizations when debugging Reference Source

Wow, it's amazing!!, very good job, i love Microsoft.

Tuesday, January 29, 2008 3:28 PM by GuerreroTook

# Reference Source デバッグ時に最適化を殺すには

Reference Source デバッグ時に最適化を殺すには

Tuesday, January 29, 2008 8:42 PM by 囚人のジレンマな日々

# How to disable optimizations when debugging Reference Source

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, January 31, 2008 3:18 AM by DotNetKicks.com

# Episode 1 of our Weekly Recap Show is up

Kudos to Brian for editing everything last night, I think he's sleeping on my couch right now, but our

Friday, February 01, 2008 11:32 AM by Dan Fernandez's Blog

# Episode 1 of our Weekly Recap Show is up

Kudos to Brian for editing everything last night, I think he's sleeping on my couch right now, but

Friday, February 01, 2008 12:04 PM by Noticias externas

# Brian Keller and Dan Fernandez: Name this Show!

This is Episode #1 of <insert.name.here>, a weekly recap show of our favorite things for developers

Friday, February 01, 2008 12:04 PM by Noticias externas

# re: How to disable optimizations when debugging Reference Source

BTW, what does the "ZAP" acronym stand for?

Thursday, February 21, 2008 10:22 AM by ondrejsv

# re: How to disable optimizations when debugging Reference Source

ZAP is an old name we had for the JIT'd assemblies.  It's no longer in use - just like the name COMPLUS in reference to the CLR - but gives you an idea of how long this switch has been around!

Monday, March 03, 2008 4:01 PM by sburke

# Debugging Optimized Code

At one point or another, you run in the situation where you need to debug an application that makes use

Monday, January 12, 2009 4:36 PM by WPF Client Development

# How to disable optimizations during debugging

Sooner or later you may run into a situation where you need to evaluate a local variable under debugger

Tuesday, January 27, 2009 5:51 PM by Kirill Osenkov

# re: How to disable optimizations when debugging Reference Source

I cannot see the debug tab under project properties. I am using VS 2008, any ideas?

Sunday, November 22, 2009 9:52 PM by ashtek

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker