// Retrieve information about runtimes that are currently loaded into the target process. public IEnumerable <CLRRuntimeInfo> EnumerateLoadedRuntimes(Int32 processId) { List <CLRRuntimeInfo> runtimes = new List <CLRRuntimeInfo>(); IEnumUnknown enumRuntimes; using (ProcessSafeHandle hProcess = NativeMethods.OpenProcess( /* * (int)(NativeMethods.ProcessAccessOptions.ProcessVMRead | * NativeMethods.ProcessAccessOptions.ProcessQueryInformation | * NativeMethods.ProcessAccessOptions.ProcessDupHandle | * NativeMethods.ProcessAccessOptions.Synchronize), **/ // TODO FIX NOW for debugging. 0x1FFFFF, // PROCESS_ALL_ACCESS false, // inherit handle processId)) { if (hProcess.IsInvalid) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } enumRuntimes = m_metaHost.EnumerateLoadedRuntimes(hProcess); } // Since we're only getting one at a time, we can pass NULL for count. // S_OK also means we got the single element we asked for. for (object oIUnknown; enumRuntimes.Next(1, out oIUnknown, IntPtr.Zero) == 0; /* empty */) { runtimes.Add(new CLRRuntimeInfo(oIUnknown)); } return(runtimes); }
private static ICorDebug GetDebuggerInterface(ICLRMetaHost metaHost, Process process) { var runtimes = metaHost.EnumerateLoadedRuntimes(process.Handle); var runtime = GetRuntime(runtimes, GetRequestedRuntimeVersion(process.MainModule.FileName)); object rawDebuggerInterface; runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface); return((ICorDebug)rawDebuggerInterface); }
/// <summary> /// Attaches debugger to the running process. /// </summary> /// <param name="pid">Process id</param> /// <param name="desiredVersion">the desired version of the runtime - you don't need to /// provide the whole version string as only the first n letters /// are compared, for example version string: "v2.0" will match /// runtimes versioned "v2.0.1234" or "v2.0.50727". If <code>null</code> /// is given, the first found runtime will be returned.</param> /// <param name="options">The options.</param> /// <returns></returns> public static CorDebugger CreateDebuggerForProcess(Int32 pid, String desiredVersion = null, CorDebuggerOptions options = null) { ICLRMetaHost metahost = NativeMethods.CLRCreateInstance(ref CLSID_ICLRMetahost, ref IID_ICLRMetahost); Process proc = Process.GetProcessById(pid); IEnumUnknown runtimes = metahost.EnumerateLoadedRuntimes(proc.Handle); ICLRRuntimeInfo runtime = GetRuntime(runtimes, desiredVersion); if (runtime == null) { throw new RuntimeNotFoundException(); } return(CreateDebugger(runtime, options ?? new CorDebuggerOptions())); }
public static void AttachToProcess(Int32 pid) { Process proc = Process.GetProcessById(pid); ICLRMetaHost metahost = NativeMethods.CLRCreateInstance(ref metahost_clsid, ref metahost_riid); IEnumUnknown runtimes = metahost.EnumerateLoadedRuntimes(proc.Handle); ICLRRuntimeInfo runtime = RTHelper.GetRuntime(runtimes, "v4.0"); ICorDebug codebugger = CreateDebugger(runtime); codebugger.Initialize(); codebugger.SetManagedHandler(new ManagedCallback()); ICorDebugProcess coproc; codebugger.DebugActiveProcess(Convert.ToUInt32(pid), 0, out coproc); Console.ReadKey(); }
public static IEnumerable <string> GetProcessRuntimes(ICLRMetaHost host, SafeHandle hProcess) { if (host != null) { var buffer = new StringBuilder(1024); IEnumUnknown ppEnumerator = host.EnumerateLoadedRuntimes(hProcess.DangerousGetHandle()); return(ppEnumerator.Cast <ICLRRuntimeInfo>().Select(rti => { var bufferLength = (uint)buffer.Capacity; rti.GetVersionString(buffer, ref bufferLength); return buffer.ToString(0, (int)bufferLength - 1); }).ToList()); } else { string buffer = GetVersionFromProcess(hProcess); if (buffer != null) { return(new[] { buffer }); } } return(Enumerable.Empty <string>()); }
public static IEnumerable<string> GetProcessRuntimes(ICLRMetaHost host, SafeHandle hProcess) { if (host != null) { var buffer = new StringBuilder(1024); IEnumUnknown ppEnumerator = host.EnumerateLoadedRuntimes(hProcess.DangerousGetHandle()); return ppEnumerator.Cast<ICLRRuntimeInfo>().Select(rti => { var bufferLength = (uint)buffer.Capacity; rti.GetVersionString(buffer, ref bufferLength); return buffer.ToString(0, (int)bufferLength - 1); }).ToList(); } else { string buffer = GetVersionFromProcess(hProcess); if (buffer != null) { return new[] { buffer }; } } return Enumerable.Empty<string>(); }
private static ICorDebug GetDebuggerInterface(ICLRMetaHost metaHost, Process process) { var runtimes = metaHost.EnumerateLoadedRuntimes(process.Handle); var runtime = GetRuntime(runtimes, GetRequestedRuntimeVersion(process.MainModule.FileName)); object rawDebuggerInterface; runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface); return (ICorDebug)rawDebuggerInterface; }