Using DbgHost.exe COM objects from C#

1

My company has an application that has some responsiveness/stability/memory issues. I am trying to write a little helper application that can help us identify these issues. I would like this application to be able to generate dumps on command and to do some basic automated debugging, first off I am trying to get all the stack traces for all the threads in the target application.

So I have been researching this for a week or so, I have found several ways of adding the ability to do debugging. Some options I have found were to use ICorDebug, DbgEng.dll and DbgHost.exe. I really would like to use DbgHost.exe since it gives me the ability to inject the LeakTrack.dll into the target process to track the memory allocations.

My problem is that I can't seem to get it to work and I can't find any really good information on the web concerning the two objects, DbgControl and DbgObj. I have found the following links:

How to control a debugger engine?

Scripting DbgHostLib

The first article explains how to open a dump file and do stuff with it, the second explains how to attach to a process and do stuff with it. The second is using some web automation software. The second is exactly what I would like to do.

So here is my code so far, I am only adding the important code, the rest of the code is merely glue to for the UI.

    private void OnAttach(uint? targetProcessId)
    {
        if (targetProcessId == null || targetProcessId.Value == default(uint))
        {
            return;
        }

        Process targetProcess = Process.GetProcessById((int)targetProcessId.Value);
        if (targetProcess.HasExited)
        {
            return;
        }

        DbgControl dbgControl = new DbgControl();
        dbgControl.AttachToProcess((int)targetProcessId.Value, @"C:\scripts", @"C:\symcache", null);
        try
        {
            DbgObj dbgObj = new DbgObj();
            Debug.WriteLine(dbgObj.ThreadInfo.Count);
            foreach (ProcessThread processThread in targetProcess.Threads)
            {
                IDbgThread dbgThread = dbgObj.GetThreadBySystemID(processThread.Id);
                foreach (IDbgStackFrame dbgStackFrame in dbgThread.StackFrames)
                {
                    Debug.WriteLine(dbgStackFrame.InstructionAddress);
                }
            }
        }
        finally
        {
            dbgControl.DetachFromProcess();
        }
    }

    private void OnOpen(string dumpFilePath)
    {
        if (!File.Exists(dumpFilePath))
        {
            return;
        }

        DbgControl dbgControl = new DbgControl();
        DbgObj dbgObj = dbgControl.OpenDump(dumpFilePath, @"C:\symcache", @"C:\symcache", null);

        Debug.WriteLine(dbgObj.ThreadInfo.Count);
    }

So the OnOpen stuff works, the OnAttach stuff doesn't. The OnAttach code successfully attaches to the process, I an create a DbgObj and even dump the thread count, but when I try to get the thread object it fails. I get:

The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

When I check the EventVwr.exe, I get these entries:

Faulting application name: Dbghost.exe, version: 1.2.0.52, time stamp: 0x4e164226
Faulting module name: Dbghost.exe, version: 1.2.0.52, time stamp: 0x4e164226
Exception code: 0xc0000005
Fault offset: 0x0001907d
Faulting process id: 0x2300
Faulting application start time: 0x01cdcaa0cdffbb1a
Faulting application path: C:\Program Files\DebugDiag\x86Support\Dbghost.exe
Faulting module path: C:\Program Files\DebugDiag\x86Support\Dbghost.exe
Report Id: 0bb5082a-3694-11e2-a454-d4bed9031bdf

So DbgHost.exe is getting an access violation. I am about to give up on this and move on to the more common methods, but I am hoping someone has some guidance that can help me get over this bump in the road.

c#
debugging
com
windbg
asked on Stack Overflow Nov 25, 2012 by taylorjonl • edited Nov 25, 2012 by taylorjonl

1 Answer

0

I found the same issue. I just understand this link. I used DebugDiag 1.2 with C# for automate dump. Note, please ignore "test.dmp", current i can't specify file name and it's not the main function that we really need it.

This is our code that we are looking for. This is a example for C#.

C# code:

int pid = 1234; //process id that you need to dump
string dumpPath;
dumpPath = @"C:\Programs\test.dmp";
DbgControl dbgControl = new DbgControl();
dbgControl.AttachToProcess(pid, @"C:\Programs\InjectLeakTrack.vbs", @"C:\", dumpPath);
DbgObj dbgObj = new DbgObj();
dbgObj.DumpPath = @"C:\";
dbgObj.CreateDumpForProcessID(pid, "No reason", false, true);
dbgControl.DetachFromProcess();

For InjectLeakTrack.vbs, we need only this.

Sub Debugger_OnInitialBreakpoint()
   Debugger.InjectLeakTrack
End Sub

We also use C# for automate analyst. C# code:

DbgControl dbgControl = new DbgControl();
DbgObj g_Debugger = dbgControl.OpenDump(dumpPath, @"c:\symbols", @"c:\symbols", null);
dynamic g_UtilExt = g_Debugger.GetExtensionObject("CrashHangExt", "Utils");
dynamic g_VMInfo = g_Debugger.GetExtensionObject("MemoryExt", "VMInfo");
dynamic g_LeakTrackInfo = g_VMInfo.LeakTrackInfo;
Console.Write(g_LeakTrackInfo.IsLeakTrackLoaded());
answered on Stack Overflow Jun 18, 2013 by NineWoranop • edited Jun 21, 2013 by NineWoranop

User contributions licensed under CC BY-SA 3.0