[Dev tip] Debugging Optimized Code in .NET


Sometime back I found myself trying to remotely debug a program thru visual studio, and while trying to access the value of one of the variables I got a message saying “Cannot evaluate expression because the code of the current method is optimized”… turns out that there are compilation flags that instruct the JIT to optimize the code (usually on release builds) and thus some variables will not be available during debugging.  You may instruct the JIT not to optimize the code by doing the following:

Let’s say that you have:

  • DLLYouWantToDebug.dll
  • DLLYouWantToDebug.pdb

Then you will create a new text file with the same name as the dll you are trying to debug but with “ini” extension, example:

  • DLLYouWantToDebug.ini

Open this file with Notepad and insert the following text in it:

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

This will override the optimization flag and then you will be able to see all the variable values

Disclaimer!

Please note that this is not intended for production usage!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s