I'm trying to attach the ClrMD in a process to itself:
private static void Main()
{
var pid = Process.GetCurrentProcess().Id;
WriteLine($"PID: {pid}");
using (var dataTarget = DataTarget.AttachToProcess(pid, 1000))
{
WriteLine($"ClrMD attached");
}
}
However, I'm getting the following exception:
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)
I can attach in passive mode, but not in Invasive or Non-Invasive mode.
You can use
DataTarget.CreateSnapshotAndAttach
. This method creates a snapshot of the process and createDataTarget
from it. Example: