Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTarget.CreateFromDebuggerInterface Throws Exception #32

Closed
keithfink opened this issue Nov 11, 2015 · 2 comments
Closed

DataTarget.CreateFromDebuggerInterface Throws Exception #32

keithfink opened this issue Nov 11, 2015 · 2 comments

Comments

@keithfink
Copy link

When natively instantiating an instance of IDebugClient via DebugCreate, and then passing it into DataTarget.CreateFromDebuggerInterface method, the following exception is thrown:

Microsoft.Diagnostics.Runtime.ClrDiagnosticsException was unhandled
HResult (System.Exception)=-2128281598
HResult=-2128281598
Message=Failed to get proessor type, HRESULT: 8000ffff
Source=Microsoft.Diagnostics.Runtime
StackTrace:
at Microsoft.Diagnostics.Runtime.DbgEngDataReader.GetArchitecture()
at Microsoft.Diagnostics.Runtime.DataTargetImpl..ctor(IDataReader dataReader, IDebugClient client)
at Microsoft.Diagnostics.Runtime.DataTarget.CreateFromDebuggerInterface(IDebugClient client)

Using this code:

        Guid debugClientGuid = typeof(IDebugClient).GUID;
        object debugClientIUnknown;
        NativeMethods.DebugCreate(ref debugClientGuid, out debugClientIUnknown);
        DataTarget target = DataTarget.CreateFromDebuggerInterface(debugClientIUnknown as IDebugClient5);



internal static class NativeMethods
{
    [DllImport("dbgeng.dll")]
    public static extern int DebugCreate(ref Guid InterfaceId, [MarshalAs(UnmanagedType.IUnknown)] out object Interface);
}

}

@leculver
Copy link
Contributor

Apologies for the delay in responding, it's been a busy week. I suppose this should be better documented, but CreateFromDebuggerInterface expects that you have set up IDebugClient in a way such that it is ready to be used. The HRESULT is the giveaway: 0x8000fff is the "catastrophic" error code that dbgeng hands out when it's not ready for the APIs that are called. The goal with this API is to allow you to construct a DataTarget from your own DbgEng debugger session.

Here is a minimal example of it working properly:

using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interop;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        Guid debugClientGuid = typeof(IDebugClient).GUID;
        object debugClientIUnknown;
        int hr = DebugCreate(ref debugClientGuid, out debugClientIUnknown);
        Debug.Assert(hr == 0);

        IDebugClient client = (IDebugClient)debugClientIUnknown;
        IDebugControl control = (IDebugControl)debugClientIUnknown;
        int hr = client.OpenDumpFile(@"crash.dmp");
        Debug.Assert(hr == 0);
        hr = control.WaitForEvent(DEBUG_WAIT.DEFAULT, 5000);
        Debug.Assert(hr == 0);

        using (DataTarget target = DataTarget.CreateFromDebuggerInterface(client))
        {
            // TODO
        }
    }

    [DllImport("dbgeng.dll")]
    public static extern int DebugCreate(ref Guid InterfaceId, [MarshalAs(UnmanagedType.IUnknown)] out object Interface);
}

Similarly, you can look in the ClrMD source's test infrastructure to see more examples of using DbgEng (specifically https://github.com/Microsoft/clrmd/blob/master/src/Microsoft.Diagnostics.Runtime.Tests/Debugger.cs and things that call it). However, DbgEng itself (IDebugClient and related interfaces) is a Microsoft library independent from ClrMD, so you should consult its documentation on how to use it...

Does this address your concern?

@keithfink
Copy link
Author

Hi Lee,
Thank you for the response! Yes, this is certainly helpful. I actually figured this out yesterday morning and the key was the call to WaitForEvent. Once I added that, it started working.
The reason I used this approach is because in addition to all the great things ClrMd offer, I would also like to be able to execute debugger commands via IDebugControl::Execute / ExecuteWide. I didn't see an API exposed in ClrMd for actually executing commands through the engine, so that's why I went with this approach. Though I suppose I could just cast the DataTarget.DebuggerInterface to an IDebugControl. Hmm... I wonder I didn't do that to start with.
Thanks for all the great work here. I am writing some diagnostic tooling for our company and ClrMd is a fantastic help. Before this, I worked for Microsoft Developer Support for many years so am very familiar with sos, psscor, windbg, etc. (though this is my first attempt at using the dbgeng API).

Thanks,

Keith
Date: Thu, 12 Nov 2015 18:28:25 -0800
From: notifications@github.com
To: clrmd@noreply.github.com
CC: keithfink@hotmail.com
Subject: Re: [clrmd] DataTarget.CreateFromDebuggerInterface Throws Exception (#32)

Apologies for the delay in responding, it's been a busy week. I suppose this should be better documented, but CreateFromDebuggerInterface expects that you have set up IDebugClient in a way such that it is ready to be used. The HRESULT is the giveaway: 0x8000fff is the "catastrophic" error code that dbgeng hands out when it's not ready for the APIs that are called. The goal with this API is to allow you to construct a DataTarget from a DbgEng debugger session.

Here is a minimal example of it working properly:

using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interop;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
static void Main(string[] args)
{
Guid debugClientGuid = typeof(IDebugClient).GUID;
object debugClientIUnknown;
int hr = DebugCreate(ref debugClientGuid, out debugClientIUnknown);
Debug.Assert(hr == 0);

    IDebugClient client = (IDebugClient)debugClientIUnknown;
    IDebugControl control = (IDebugControl)debugClientIUnknown;
    int hr = client.OpenDumpFile(@"crash.dmp");
    Debug.Assert(hr == 0);
    control.WaitForEvent(DEBUG_WAIT.DEFAULT, 5000);
    Debug.Assert(hr == 0);

    using (DataTarget target = DataTarget.CreateFromDebuggerInterface(client))
    {
        // TODO
    }
}

[DllImport("dbgeng.dll")]
public static extern int DebugCreate(ref Guid InterfaceId, [MarshalAs(UnmanagedType.IUnknown)] out object Interface);

}

Similarly, you can look in the ClrMD source's test infrastructure to see more examples of using DbgEng (specifically https://github.com/Microsoft/clrmd/blob/master/src/Microsoft.Diagnostics.Runtime.Tests/Debugger.cs and things that call it). However, DbgEng itself (IDebugClient and related interfaces) is a Microsoft library independent from ClrMD, so you should consult its documentation on how to use it...

Does this address your concern?


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants