May 5, 2015

Activate the debugger in the third party dlls

All the advanced dotnet developers has already been frustrated because we are not able to debug step by step in the third party dlls. Of course you have tools like ILSpy or reflector to see the decompiled code but sometime it isn't enough to understand what really happens.
With a few manipulation you will be able to debug it and even (most of the time) have the watch values

  • Download and install dotpeek
  • In visual studio, click on tools, options 
    • In "debugging", unckeck the box "Enable just my code" 
    • In "Debugging" - "Symbols", note the value of the field "Cache symbols in the directory" for the next step 
  • In dotPeek:
    • Drag'n drop the assembly into the "Assembly Explorer"
    • Right click on the assembly and click on "generate pdb" 
    • In the field "Destination folder", past the value of the "Cache symbols in the directory" from the previous step and click on generate.
If you stop now you should be able to attach to your process, step into the decompiled dll. But you will be not able to see the values of the variables.
  • Two options to start visual studio in the correct mode:
    • To start it once type the following lines into a cmd prompt:
      set COMPLUS_ZapDisable=1
      cd /d C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE
      start devenv.exe
      exit
    • To set it permanently set this in the registry:
      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
      Add Key: COMPLUS_ZAPDISABLE
      Type: REG_SZ (String)
      Value: 1
  • You also need to disable the JIT optimization for the dll. To do that, create a .ini file with the same name as the dll on the same place (in your bin folder). Example for Sitecore.Kernel.dll, the name must be: Sitecore.Kernel.ini. Set the content of the file to:
    [.NET Framework Debugging Control] 
    GenerateTrackingInfo=1 
    AllowOptimize=0
Now you should be able to add the watch on the variables.

Enjoy!

1 comment:

  1. If it didn't work you can also try to disable the checkbox "Enable just my code" in the debugger option of visual studio

    ReplyDelete