Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Debugger - any way to access compiler-generated temporary variables through the debugger?

If you examine C# code in Reflector, you can notice special compiler-generated local variables that are named with the pattern CS$X$Y. These variables were (unofficially) documented in this answer.

Is there any way to view these values via the Watch window in Visual Studio, or via the VS Debugger Extensibility API?

I'm asking specifically about the regular Visual Studio debugger, please do not answer that this is possible via windbg/sos/sosex - as that's not what I'm looking for.

like image 510
Omer Raviv Avatar asked Feb 05 '14 17:02

Omer Raviv


2 Answers

Unfortunately there is no way to do this with the C# EE. The names for these locals are indeed stored in the PDB and available. However the C# EE will filter out all temporary values during debugging to reduce clutter. This filtering is unconditional and cannot be overridden. C# is not alone here as this is the behavior in every language.

The good news though is that every language uses different naming patterns for their temporaries. This means a temporary name in C# will run right past the filtering of the VB EE. Even though it's an illegal identifier the VB EE still considers it a valid local (and vice versa). Hence you just need to temporarily switch the debugging engine for C# code to the VB EE and the locals will become visible

Here is how to do this

  • Close all instances of Visual Studio (this is really important)
  • Open up regedit
  • Navigate to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\AD7Metrics\ExpressionEvaluator\{3F5162F8-07C6-11D3-9053-00C04FA302A1}\{994B45C4-E6E9-11D2-903F-00C04FA302A1}
  • Change the value of CLSID from {60F5556F-7EBC-4992-8E83-E9B49187FDE3} to {59924502-559A-4BB1-B995-5D511BB218CD}

Now whenever you debug C# code it will use the VB EE and hence display temporaries as normal locals. Note that this is completely unsupported. But it should work fine. I was able to see raw closure values with this trick on my machine and poke around a bit without any issues.

Note that these instructions are specific to Visual Studio 2013. If you are using 2012, or 2010 it should work by changing the 12.0_Config in the registry key name to

  • 2012 use 11.0_Config
  • 2010 use 10.0_Config

It may need a few tweaks on top of that (didn't actually test older versions). If you run into any problems there let me know and I'll try and get it working locally and update the instructions

Here is a pic of the final output once you make this change

enter image description here

like image 150
JaredPar Avatar answered Oct 19 '22 03:10

JaredPar


No, they are not added to the PDB file. The debugger simply has no way to discover them, nor know anything about where they are stored. This is all entirely intentional.

like image 22
Hans Passant Avatar answered Oct 19 '22 04:10

Hans Passant