In part 1 we ended with an application that could start a new process or attach to the running one and then stop it. We will now find a way to make the process running and log all events coming from it. Let’s introduce a simple HandleEvent method which will be called from all other event handlers (except ICorDebugManagedCallback.ExitProcess):
void HandleEvent(ICorDebugController controller)
{
Console.WriteLine("event received");
controller.Continue(0);
}
All events handlers bodies (except ICorDebugManagedCallback.ExitProcess) will now look as follows:
{
HandleEvent(pAppDomain); // or HandleEvent(pProcess) if first parameter is pProcess
}
If we now execute our application it will print few “event received” messages and then stop. Under the debugger we will see that the debugging API throws a COM exception:
System.Runtime.InteropServices.COMException crossed a native/managed boundary
Message=Unrecoverable API error. (Exception from HRESULT: 0x80131300)
Source=mindbg
ErrorCode=-2146233600
StackTrace:
at MinDbg.NativeApi.ICorDebugController.Continue(Int32 fIsOutOfBand)
at ...
Read more: Low Level Design