12/07/2011

Debugging .net applications in production with mdbg

Today we had an issue at work where one of our ASP.net applications was installed on a production server and some part of the application (a WCF service hosted within the web application) didn't work. The reason why it was so hard to find why it didn't work is because we use a library for nhibernate session management that swallows the actual exception and throws a new one without adding the exception as an innerexception.
Without really knowing what the underlying exception is, it is very hard to make it work.

So how do we find out what the underlying exception is without modifying the code, without access to pdb debug files and without using Visual Studio remote debugging for a web application on a production server?

Enter Mdbg
The NET Framework Command-Line Debugger helps tools vendors and application developers find and fix bugs in programs that target the .NET Framework common language runtime. This tool uses the runtime debugging API to provide debugging services. You can use MDbg.exe to debug only managed code; there is no support for debugging unmanaged code.
The command line interface looks daunting at first sight because there's a whole bunch of commands.
But for what we wanted to do, we could narrow it down to just a few.
Mdbg.exe is part of either the Windows SDK or Visual Studio. None of which was installed on the production machine and none of which we wanted to install.
But fortunately all you need are just 3 files that can be copy-pasted to a folder on the production machine and you're ready to go.
  • Mdbg.exe
  • MdbgCore.dll
  • MdbgDis.dll
I copied them from C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
Double click and there's the mdbg CLI



We will now attach the debugger to a running application. In our case this was the correct w3p.exe process. As an example I will use a .net application I have installed on my computer, the totally awesome music player MusicBee.
In mdbg you can attach to a process with the process id. To find out the process id of the running musicbee application fire up your task manager, select the process id column in view > Select columns, and remember the correct process id (in this case 11820).



To attach a process in mdbg type: a 11820

We wanted to catch all exceptions. We can do this by typing: ca ex
And then we want the debugger to continue until we encounter an exception by typing: g



And there we have one:



If you want to continue (just like pressing F5 in Visual Studio) just press: g

I think this is just the top of the iceberg of what Mdbg can do, but we managed to solve our problem (the user of the application pool didn't have rights to write in c:\windows\temp) quite fast.

1 comment:

  1. Hi,
    I'm using Visual Studio 2015 and I can't find Mdbg.exe, can you share yours?
    Thank you.

    ReplyDelete