In my previous post, I showed how MDbgEngine (available on NuGet) can be used to stop a process when an exception is thrown, and how a minidump can then be created to enable post-mortem debugging.
Using MDbgEngine, you can do much, much more – and all with a very high-level API.Have a look at how you can print a call stack (with argument values!):
process.PostDebugEvent += (sender, e) => { log.Log(e.CallbackType.ToString()); if (e.CallbackType == ManagedCallbackType.OnException2) { var ce = (CorException2EventArgs)e.CallbackArgs; if (ce.EventType == CorDebugExceptionCallbackType.DEBUG_EXCEPTION_FIRST_CHANCE) { var thread = process.Threads.Lookup(ce.Thread); foreach (var frame in thread.Frames) { if (!frame.IsManaged || frame.Function.FullName.StartsWith("System.")) break; log.Log("{0}({1})", frame.Function.FullName, string.Join(", ", frame.Function.GetArguments(frame).Select( arg => string.Format("{0} = {1}", arg.Name, arg.GetStringValue(false))))); } } } };
This code, when attached to an example process, printed logged the following information:
OnException2 AspNet.Default.B(i = 3, t = "test 2") AspNet.Default.A(i = 1, t = "test")
PADRE – Pluggable Automatic Debugging and Reporting Engine
In the previous blog post, and on Twitter, I’ve already mentioned the idea of turning this into a project. The above sample code is now available on GitHub, along with some boilerplate code in MEF, to make it truly pluggable and a sample ASP.NET application throwing exceptions.
Just open the solution, run it and go to the ASP.NET application to click Button 2. You should get a stack trace in the console window like above.
I’d love to hear some feedback!