Finding the Source Code for an Assembly

Sometimes, especially when working on large projects (such as, I don't know, say ... the CLR), you find yourself debugging a problem where you don't know where a component is built from.  Depending on the problem, it might be useful to get to the sources of the assembly to help diagnose what the issue is (or to look up in the source control system who the owner of the code is so you can contact them).  I ran into this problem recently, and using the IronPython MDbg extension was able to whip up a quick method that will dump the path to the source files that make up an assembly that you have symbols for:

def ShowAssemblySources(assembly):
    for module in Shell.Debugger.Processes.Active.Modules:
        if module.CorModule.Assembly.Name.Contains(assembly):
            print module.CorModule.Name
            if module.SymReader != None:
                for document in module.SymReader.GetDocuments():
                    print "  %s" % document.URL
            else:
                print "  module does not have symbols, skipping"

Using this method is pretty straight forward:

[p#:0, t#:0] mdbg> ShowAssemblySources("caspol")
c:\WINDOWS\Microsoft.NET\Framework64\v2.0.AMD64chk\caspol.exe
  d:\vbl\lab21s\ndp\clr\src\ToolBox\caspol\obj1c\amd64\CommonResStrings.cspp
  d:\vbl\lab21s\ndp\clr\src\ToolBox\caspol\obj1c\amd64\CommonResStrings.cs
  d:\vbl\lab21s\ndp\clr\src\ToolBox\caspol\caspol.cs

It's definately not an every-day debugging tool, which again makes it nice that IronPython makes extending the MDbg command set so easy since I wouldn't have wanted to spend more than a few minutes working this code out.