使用 ClrMD 附加到自己?结果:0x80070057

Ban*_*San 5 .net c# debugging clrmd

我正在尝试将进程中的 ClrMD 附加到自身:

private static void Main()
{
    var pid = Process.GetCurrentProcess().Id;

    WriteLine($"PID: {pid}");
    using (var dataTarget = DataTarget.AttachToProcess(pid, 1000))
    {
        WriteLine($"ClrMD attached");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下异常:

PID: 7416

Unhandled Exception: Microsoft.Diagnostics.Runtime.ClrDiagnosticsException: Could not attach to pid 1CF8, HRESULT: 0x80070057
   at Microsoft.Diagnostics.Runtime.DbgEngDataReader..ctor(Int32 pid, AttachFlag flags, UInt32 msecTimeout)
   at Microsoft.Diagnostics.Runtime.DataTarget.AttachToProcess(Int32 pid, UInt32 msecTimeout, AttachFlag attachFlag)
   at Microsoft.Diagnostics.Runtime.DataTarget.AttachToProcess(Int32 pid, UInt32 msecTimeout)
   at BanksySan.Scratch.Console.Program.Main(String[] args)
Run Code Online (Sandbox Code Playgroud)

可以在被动模式下连接,但不能在入侵或非入侵模式下连接。

ita*_*aiy 5

您可以使用DataTarget.CreateSnapshotAndAttach。此方法创建进程的快照并DataTarget从中创建。例子:

var processId = Process.GetCurrentProcess().Id;

using (var dataTarget = DataTarget.CreateSnapshotAndAttach(processId))
{
}
Run Code Online (Sandbox Code Playgroud)


小智 1

Invasiveflag 允许该 API 的使用者通过正常的 IDebug 函数调用来控制目标进程。该进程将因此暂停(在附加期间),以便获取数据并控制目标进程

NonInvasive调试器附加中,进程将因此暂停(在附加期间),并且能够获取数据,但调用者无法控制目标进程。当进程已经附加了调试器时,这非常有用。

执行Passive附加意味着没有调试器实际上附加到目标进程。该进程不会暂停,因此对快速变化的数据(例如GC堆或调用堆栈的内容)的查询将高度不一致,除非用户通过其他方式暂停该进程。当使用 ICorDebug(托管调试器)附加时,它非常有用,因为您无法使用 ICorDebug 的非侵入式附加。