You Don’t Need to Copy PDB Files to Debug in the GAC!

Following up on my last post, many developers choose to put assemblies into bin because they find them easier to debug. Usually, they do not create detailed CAS policies. Instead they simply set the trust level to full so they can ‘just make it work’. While this is a sentiment I can fully understand having wrestled with it myself, it’s not the best idea. Generally, I think most will agree that the closer the development environment’s configuration matches the production environment the better. So developing with assemblies in BIN with the web config set to full trust and then deploying them at the last minute to the GAC just makes me feel unclean.

Fortunately, it’s just as easy to debug in the GAC as it is in BIN if you configure the development environment correctly. Unfortunately, few know how to do this because the internet is polluted with pages full of bad information that is a held over from previous versions of .Net. You can spot these easily because they will say you need to copy the debug symbols (.pdb file) to the GAC. In and of itself, that will not work. These days it is also completely unnecessary.

To configure VS 2005 to debug the assemblies properly, do the following:

Open Tools | Options.

Check Show all settings if needed and locate Enable Just My Code (Managed only)

Uncheck it and click OK

Attach to the w3wp.exe process for the instance you want to debug.

There may be more than one. You can attach to all of them, but only the first one will be debugged. To verify that you are attached to the correct instance of w3wp.exe, open the Modules window by pressing CTRL-ALT-U.

If your assembly is listed, you picked the right one. If not, choose stop and attach to the next one until you find it.

In this example, the assembly is loaded. Note that the symbol file is loaded and that it was not copied to the GAC.

My breakpoints are hit! Yay!

 

P.S. Don’t forget the set debug="true" in the web.config or it won’t matter where you put the assembly! This can really trip you up if you forget to do it because the breakpoints will turn all nice and red, but it won’t stop.

Note: I renamed this post because I want people to read it and I decided that some might miss the message based on the earlier title.

Author: Doug Ware

Atlanta based entrepreneur, author of many SharePoint books and videos, leader of Atlanta .NET user group, founder of InstantQuick, and SharePoint MVP.

27 Comments

  • Dan Attis

    November 16, 2007, 4:24 pm

    You can also configure a symbol server or search paths, and alter your build process to publish the PDB files out to the symbol server or cache directory. Then you can always have symbol information available, no matter what version of the assembly happens to be loaded (very useful for troubleshooting a production or QA environment problem where you otherwise might not have retained the pdb files). It’s easily configured through the Symbols section under Debugging in the VS Options dialog.

    This can also be very handy for team development scenarios where one developer is building assemblies that are used by others who do not necessarily have the proper PDB files available to them without a central repository.

  • Doug Ware

    November 16, 2007, 8:13 pm

    Lol! There goes my follow up post. :)
    Just kidding. Those are great points Dan!

  • http://drewmace.blogspot.com

    November 21, 2007, 11:55 am

    Good article.

  • Thomas Carpe

    December 11, 2007, 2:47 pm

    This is a Godsend in SharePoint, where GAC deployment is generally the way to go. I’ll have to come up with a good topic for follow-up with respect to feature/solution development, as supportability is a *HUGE* concern for my clients. http://www.thomascarpe.com/ Cheers!

  • Anon

    May 14, 2008, 4:57 am

    Hi,
    I tried your methods for debugging a remote MOSS server but when no symbol was loaded for the DLL i was trying to debug.

    I tried to load the symbol by pointing to a PDB file on my local machine, but VS2005 keep saying the “The symbol file .pdb does not match the module”.

    Only when i copied the PDB to the c:windowsassembly on the remote server did i finally can get the symbol loaded.

    How can I debug remotely without copying the PDB to GAC?

    Thanks.

  • Jeremiah

    August 17, 2008, 9:34 pm

    Thanks for this information! I have linked back and suggested anyone who reads my blog to check this out.

  • dataCore

    September 23, 2008, 11:31 am

    for remote debugging i manualy put the pdb files into “c:windowsdll”

    pro:
    - u don’t need to now the full GAC Name with version number/pkey a.s.o.
    - it workx :)

    btw: to copy the dll remotly into the gac with “Post-Build Event”, use remcom.exe -> http://talhatariq.wordpress.com/projects/remote-command-executor-xrce/

  • Mark DiGiovanni

    September 24, 2008, 11:17 pm

    I’ve been using http://www.codeplex.com/STSDev (great tool, BTW) and trying to get debugging to work with MOSS Web Parts. I’ve been chasing my tail in circles. I’ve tried everything that I’ve found and nothing worked until now.

    I tried this and it worked right away!

    You get a Gold Star! Actually, you get two Gold Stars!

    Cheers!

  • Anon

    October 3, 2008, 2:50 pm

    Worked great. This was a big help.

  • Joan

    January 30, 2009, 4:35 am

    I have been trying for two days now to get this to work. Your blog made my day! I will make sure to bookmark it carefully.

  • ashraf

    February 10, 2009, 2:44 pm

    Thanks man
    it works with VS08

    but the thing u need to add the assembly manully
    and restart the iis
    then attach the process

  • Prabin

    February 12, 2009, 7:57 pm

    I was trying like hell for 2 hrs to find debugging techniques for the assembly in gac. It worked out .

  • sundar

    February 17, 2009, 2:38 am

    any reason behind this

  • Anon

    April 21, 2009, 4:14 am

    This was truely gr8 hidden information unrevealed :)

  • Neha

    April 21, 2009, 4:15 am

    Was truely gr8!!

  • khovanskiiªn

    April 22, 2009, 5:45 pm

    Thank you dude. This is really something useful for humanity… Hehehehee… BIG THANKS AGAIN!

  • Rayyan

    September 4, 2009, 2:39 am

    thanks alot that really helps! keep it up! thanks again :)

  • Darryl Dalton

    October 22, 2009, 3:57 pm

    Awesome info, thanks a bunch! I just wanted pass along you can also get the correct w3wp.exe process id by running iisapp from a command prompt. It should give you the name of the web site and it’s process id.

  • Sreedhar

    January 8, 2010, 12:29 pm

    Hi,

    I am using Visual Studio 2008. I have a .dll and .pdb files.

    1. I opened Visual Studio.
    2. Created a new project.
    3. Unchecked Enable Just My Code in Tools->Options.
    4. Pressed CTRL + ALT +U. Modules windows opened.
    5. I ran my application (which I want to debug and for which i have dll and pdb files). Now I attached w3wp.exe and I can see all my dlls and pdb files are in the list.
    6. What next ? How can I see the code? And how I can put the breakpoint?

    Something am I missing here.

    Please provide more details.

  • Dan

    March 9, 2010, 6:46 am

    It seems everything in Sharepoint is just painful or like butchering instead of programming, got stuck on debugging using WSPBuilder but this is the way to go for sure to ease some of that pain. Thanks! http://www.ifunky.net

  • Anon

    March 31, 2010, 8:31 pm

    You R Gr888888888888!!!!

  • Anon

    April 19, 2010, 6:29 am

    need i say more

  • BSG

    July 13, 2010, 7:44 pm

    The “Just My Code” option claims to optimize assembly loading. I’m working on a *very* large project and am paranoid about disabling any optimization features.

    In my case, to solve this problem I copy the .pdb and the .DLL generated in the bin folder to this folder:
    C:UsersxxxxxAppDataLocalTempTemporary ASP.NET Filesxxxxxassemblydl3xxxxx

    This seemed to be a temporary fix. It worked for days, but when I came in the next week, the folder had the singular .DLL and no .pdb (again). Is there a way to force VS to output these correctly, or is the optimization so insignificant that I can deal with it?

  • Arthur

    November 22, 2010, 1:08 am

    It’s awesome Doug! You discovered how to debug assemblies in the GAC without copying the pdb symbols file to the MSIL folder. Simply attaching to the correct w3wp worker process will work.
    I had no idea about this, great work Dough. Keep posting!

    artist website

  • WillyF

    March 29, 2011, 12:05 pm

    As Sreedhar asked, how to see the code and set breakpoints ??
    Thanks for your answers.

  • Anon

    April 11, 2011, 4:30 am

    Wow, great article

  • Mt

    August 9, 2011, 3:03 am

    Hi,

    Is this only for dev env? How to enable line number for errors in integration and prod env’s

Comments are closed