Getting MDBG running on .net 4.0 framework

MDBG is, briefly, a sample managed library for writing debuggers which uses COM interop and the CLR hosting debugger APIs (ICorDebug interface etc.). As far as I can find, the most recent version of the MDB sample is still the .net 2.0 version, which doesn’t work for hosting .net 4.0 applications.

When I say doesn’t work, I mean in a somewhat subtle, non-obvious way, as in fact everything appears to work fine at first, you can indeed fire up a debuggee process, and you can indeed receive the ‘process exited’ event.

However, once you start trying to do anything useful you will realize that the debugger is not getting called back for all of the other interesting and important debugging events. Like ‘process created’, and ‘unhandled exception’. Much binging and googling eventually found me a few resources to work through the problems, and I’ll list them here:

In the category of General Background Info:

‘Upgrading debugger to support v4’ – Q&A in the .net development forums, which explains that ‘Yes, we broke compatibility and didn’t document it properly.’
‘ICorDebug architecture in v4’ – blog post in case you are interested in reasons for the above.

In the category of Really Useful Sample Code of How to Fix it:

‘Updating the managed debugging API for .NET v4’ – blog post by Brian Donahue, which has the exact function you need to replace in MDBG, minus some definitions – thank you, thank you!

And finally, in the category of Useful MSPL Open Source Project With all those Definitions you Need (and some nice wrapper stuff as well)

The ‘CLR Activation’ Codeplex project – which is a nice managed wrapper for the COM CLR hosting APIS, and can be easily adapted to fill in the final missing holes. In the interests of speed I broke encapsulation and make some private/internal stuff public, maybe some day I’ll have something nicer than that which deserves a code post, but who knows, this is still prototype stuff.