Tuesday, June 14, 2016

Debugging and Creating PDBs with ReSharper

Being able to debug Sitecore code is an important skill for supporting Sitecore solutions.  There have been a number of excellent articles on how to do this, but they typically describe using JetBrains DotPeek product as a "symbol server". (See http://bilyukov.com/debugging-sitecore-dotpeek/, and https://jammykam.wordpress.com/2015/01/11/how-to-debug-sitecore-kernel-in-visual/). An alternative, which I find somewhat simpler, is to use ReSharper to generate PDB files, and place those in your solution bin directory. I will walk you through that approach in this article.



This article assumes you have JetBrain's ReSharper installed.  It may be possible to these steps purely with the free DotPeek, but I'm not sure how the Debugging experience will work without access to decompiled sources in Visual Studio. (I may come back to that in a later post.)

Let's try to set a breakpoint in the BeginRequest pipeline.  This example uses a Visual Studio solution set up outside the web root (as described in this video), but the process should work similarly if the project is inside the web root.

First, you need to enable decompiling of third party sources.  This can be done by checking the following box in the ReSharper options:

With this enabled, you will be able to navigate to internal methods in Sitecore DLLs that you reference in your project. Let's assume you have a reference to Sitecore.Kernell.dll, you will be able to type Control-T to find methods.  Note that your shortcut might be different: You can find yours at ReSharper/Navigate/Go To Everything:


This will pull up this navigation tool.



Click "Include Library Sources", and type "ItemResolver".  If you are the first time doing this, you will need to acknowledge a legal warning from ReSharper that you are viewing proprietary code.  (Standard disclaimer: I'm not a lawyer and I don't work for Sitecore, but I've long viewed reading Sitecore code to be a key part of a Sitecore developer's toolset.  If you have concerns, ask your Sitecore Regional Representative whether your license permits you to do this.)

Okay, at this point you will see the ItemResolver class.  Go down to the Process method, and put a breakpoint on the first line.


Now attach to the w3wp.exe worker process. (See http://stackoverflow.com/a/36203091/402949) Now, you will see the breakpoint will have a white center and error icon, and if you mouse over it you will see this tooltip warning, indicating that symbols have not been loaded for this location:


To add the symbols, go to ReSharper/Windows/Assembly Explorer, and click the Open icon to add the Sitecore.Kernell.dll to the window.


Now right click and select Generate PDBs.



You can select any location for them; the key thing is that you need to copy the finished PDB and put it next to the corresponding DLL that is being used to serve the website (i.e not the local copy in your Libraries folder or project bin if that is separate from your web root.)


Inside the webroot bin

Note that ReSharper will create a folder called Sitecore.Kernel.pdb. You don't want that; inside of it is a folder with a Guid name, and inside that is the PDB. Copy that to your webroot bin directory:

Now retry the debug session.  This time your break point will get hit. The screen shot below shows you can mouse over and inspect the "args" parameter:

A couple of additional notes:

  • The first time I reissued the request, the browser tab hung.  I closed the tab and submitted the request again, and it worked fine.
  • There is a Visual Studio Debug option "Enable Just My Code".  I usually have this unchecked, but in my experimenting the above breakpoint got hit either way.
  • I have not had do any of the steps described by Jammy Kam to get meaningful local variables to appear (such as the args parameter above). However a few local variables do get optimized away, and the steps described in Jammy Kam's article did not work for me on my most recent attempts.  This has not been a blocker for me, as most variables are visible.. If you want to dig deeper here, read Jammy's post (link at top of article) and this one (http://blog.paulgeorge.co.uk/2016/02/05/disabling-optimizations-debugging-third-party-dlls-with-reflector-pro/).
  • ReSharper really gives you the keys to the kingdom of how Sitecore is put together. I blogged awhile ago about things you can do with it to. (http://www.dansolovay.com/2013/01/resharper-shortcuts-every-sitecore.html)
Happy spelunking!

No comments:

Post a Comment