10 04 2013
Debug another application
Today I will show up how we could build an application that is able to can debug another. Since a while I got a problem to control an obsolete program that is productive for a while in the company I work, because it is regulary crashing and we have to know when this happens to react. For this reason I will build an application with the help of mdbg to notify when some problem with the problem occurs. First we have to download the mdbg sample from Microsoft.
Step two is to build an app that throws an exception (in my sample it will be a WinForms application with one button that will throw an exception when clicked).
Now we can come to the more interesting part.
Create a projekt (e.g. WinForms) and implement an parameterless method called “Debug”. This method has to run as a new thread. Otherwise you will block the debugged application
In this example we will use the namespace Microsoft.Samples.Debugging.MdbgEngine and the following assemblies from mdbg.
So lets fill our debug method with life.
Declare the debug engine like the following:
MDbgEngine debugEngine = new MDbgEngine();
debugEngine.Options.CreateProcessWithNewConsole = true;
debugEngine.Options.StopOnException = true;
Next we have to get our process. So lets create a process of the mentioned test app and start to watch it.
MDbgProcess debugProcess = debugEngine.CreateProcess(@"D:\temp\WTF.exe", "", DebugModeFlag.Default, null);
ExceptionThrownStopReason stopReason = debugProcess.StopReason as ExceptionThrownStopReason;
if (stopReason == null) continue;
MDbgThread activeThread = debugProcess.Threads.Active;
MDbgValue currentException = activeThread.CurrentException;
MDbgFrame currentFrame = activeThread.CurrentFrame;
catch (Exception ex)
Console.WriteLine("Error occurs (" + ex.Message + ")");
When we get a workable stop reason we can use it to inspect it more precisely like identify type of the exception etc.
This is only a small insight in mdbg, but there a more powerful functionalities.
Hope that will help you to make your first steps with mdbg and debugging outside of your develope environment.