/// <summary> /// Creates a new ICorDebug instance, initializes it /// and sets the managed callback listener. /// </summary> /// <param name="codebugger">ICorDebug COM object.</param> /// <param name="options">The options.</param> internal CorDebugger(ICorDebug codebugger, CorDebuggerOptions options) { this.codebugger = codebugger; this.options = options; this.codebugger.Initialize(); this.codebugger.SetManagedHandler(new ManagedCallback(options)); }
static private MDbgProcess GetProcessFromCordb(ICorDebug cordbg) { Debugger.Processes.FreeStaleUnmanagedResources(); CorDebugger cordebugger = new CorDebugger(cordbg); Debugger.Processes.RegisterDebuggerForCleanup(cordebugger); return(new MDbgProcess(Debugger, cordebugger)); }
private ICorDebug CreateOrGetDebuggerInterface(Process process) { if (_debuggerInterface == null) { _debuggerInterface = GetDebuggerInterface(GetMetaHost(), process); InitializeDebugger(); } return(_debuggerInterface); }
private ICorDebug CreateOrGetDebuggerInterface(string filePath) { if (_debuggerInterface == null) { _debuggerInterface = CreateDebuggerInterface(GetMetaHost(), filePath); InitializeDebugger(); } return(_debuggerInterface); }
private DebugContext(ICLRRuntimeInfo runtime) { var debuggingClassId = Constants.ClrDebuggingLegacyGuid; var debugInterfaceId = typeof(ICorDebug).GUID; this.debug = (ICorDebug)runtime.GetInterface(ref debuggingClassId, ref debugInterfaceId); this.debug.Initialize(); this.debug.SetManagedHandler(this); }
/// <summary> /// Prepares the debugger /// </summary> /// <param name="debuggeeVersion">Version of the program to debug - eg. "v1.1.4322" /// If null, the version of the executing process will be used</param> internal void InitDebugger(string debuggeeVersion) { /*if (debuggeeVersion != null && debuggeeVersion.Length > 1) { * this.debuggeeVersion = debuggeeVersion; * } else { * this.debuggeeVersion = GetDebuggerVersion(); * } * int debug_ver = 3; * if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { * debug_ver = 3; // 2.0 CLR * TraceMessage("Debugger interface version: v2.0"); * } else { * debug_ver = 4; // 4.0 CLR * TraceMessage("Debugger interface version: v4.0"); * }*/ if (string.IsNullOrEmpty(debuggeeVersion)) { debuggeeVersion = GetDebuggerVersion(); TraceMessage("Debuggee version: Unknown (assuming " + debuggeeVersion + ")"); } else { TraceMessage("Debuggee version: " + debuggeeVersion); } this.debuggeeVersion = debuggeeVersion; int debuggerVersion; // The CLR does not provide 4.0 debugger interface for older versions if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { debuggerVersion = 3; // 2.0 CLR TraceMessage("Debugger interface version: v2.0"); } else { debuggerVersion = 4; // 4.0 CLR TraceMessage("Debugger interface version: v4.0"); } corDebug = new ICorDebug(NativeMethods.CreateDebuggingInterfaceFromVersion(debuggerVersion, debuggeeVersion)); managedCallbackSwitch = new ManagedCallbackSwitch(this); managedCallbackProxy = new ManagedCallbackProxy(this, managedCallbackSwitch); corDebug.Initialize(); corDebug.SetManagedHandler(new ICorDebugManagedCallback(managedCallbackProxy)); TraceMessage("ICorDebug initialized, debugee version " + debuggeeVersion); }
public static ICorDebug GetDebugger(string debuggerVersion) { CLRMetaHost mh = new CLRMetaHost(); CLRRuntimeInfo rti = mh.GetRuntime(debuggerVersion); ICorDebug rawDebuggingAPI = rti.GetLegacyICorDebugInterface(); if (rawDebuggingAPI == null) { throw new ArgumentException("Cannot be null.", "rawDebugggingAPI"); } rawDebuggingAPI.Initialize(); rawDebuggingAPI.SetManagedHandler(new MyHandler()); return(rawDebuggingAPI); }
DnDebugger(ICorDebug corDebug, DebugOptions debugOptions, IDebugMessageDispatcher debugMessageDispatcher, string debuggeeVersion) { if (debugMessageDispatcher == null) throw new ArgumentNullException("debugMessageDispatcher"); this.debuggerManagedThreadId = Thread.CurrentThread.ManagedThreadId; this.processes = new DebuggerCollection<ICorDebugProcess, DnProcess>(CreateDnProcess); this.debugMessageDispatcher = debugMessageDispatcher; this.corDebug = corDebug; this.debugOptions = debugOptions ?? new DebugOptions(); this.debuggeeVersion = debuggeeVersion ?? string.Empty; corDebug.Initialize(); corDebug.SetManagedHandler(new CorDebugManagedCallback(this)); }
// Closes the debugger. After this method is called, it is an error // to call any other methods on this object. public void Terminate() { if (m_debugger != null) { try { m_debugger.Terminate(); } catch (System.Exception e) { Console.WriteLine("termination error: " + e.Message); } m_debugger = null; } }
private static unsafe ICorDebug CreateICorDebugImpl(DbgShimInterop dbgShimInterop, uint processId, TimeSpan runtimeLoadTimeout, void *resumeHandle) { var waiter = new ManualResetEvent(false); ICorDebug corDebug = null; Exception callbackException = null; void * token; DbgShimInterop.RuntimeStartupCallback callback = delegate(void *pCordb, void *parameter, int hr) { try { if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } var debugger = SharpDX.ComObject.FromPointer <CorApi.Portable.LocalDebugger>((IntPtr)pCordb); var unknown = Marshal.GetObjectForIUnknown((IntPtr)pCordb); corDebug = (ICorDebug)unknown; } catch (Exception e) { callbackException = e; } waiter.Set(); }; var callbackPtr = Marshal.GetFunctionPointerForDelegate(callback); var hret = (HResults)dbgShimInterop.RegisterForRuntimeStartup(processId, callbackPtr, null, &token); if (hret != HResults.S_OK) { throw new COMException(string.Format("Failed call RegisterForRuntimeStartup: {0}", hret), (int)hret); } if (resumeHandle != null) { dbgShimInterop.ResumeProcess(resumeHandle); } if (!waiter.WaitOne(runtimeLoadTimeout)) { throw new TimeoutException(string.Format(".NET core load awaiting timed out for {0}", runtimeLoadTimeout)); } GC.KeepAlive(callback); if (callbackException != null) { throw callbackException; } return(corDebug); }
internal static DesktopMethod Create(DesktopRuntimeBase runtime, ICorDebug.IMetadataImport metadata, IMethodDescData mdData) { if (mdData == null) return null; MethodAttributes attrs = (MethodAttributes)0; if (metadata != null) { int pClass, methodLength; uint blobLen, codeRva, implFlags; IntPtr blob; if (metadata.GetMethodProps(mdData.MDToken, out pClass, null, 0, out methodLength, out attrs, out blob, out blobLen, out codeRva, out implFlags) < 0) attrs = (MethodAttributes)0; } return new DesktopMethod(runtime, mdData.MethodDesc, mdData, attrs); }
private void DebuggerThread() { Console.WriteLine($"Attaching to {_process.ProcessName}, pid: {_process.Id}"); var metahost = CorDebugHelper.GetClrMetaHost(); var runtimes = metahost.EnumerateLoadedRuntimes(_process.Handle); string version = null; ICorDebug corDebug = null; while (runtimes.Next(1, out var rgelt, IntPtr.Zero) == 0) { var runtimeInfo = (ICLRRuntimeInfo)rgelt; var pwzBuffer = new StringBuilder(30); int capacity = pwzBuffer.Capacity; runtimeInfo.GetVersionString(pwzBuffer, ref capacity); version = pwzBuffer.ToString(); var riid = typeof(ICorDebug).GUID; var rclsid = typeof(ClrDebuggingLegacy).GUID; corDebug = (ICorDebug)runtimeInfo.GetInterface(ref rclsid, ref riid); } if (corDebug == null) { throw new Exception("error: cannot take corDebug"); } Console.WriteLine($"info: runtime: {version}"); corDebug.Initialize(); corDebug.SetManagedHandler(_debuggerCallbacks); corDebug.SetUnmanagedHandler(_debuggerCallbacks); corDebug.DebugActiveProcess((uint)_process.Id, 0, out _debugger); while (_debuggingActive) { Thread.Sleep(WAIT_INTERVAL); } if (!_process.HasExited) { _debugger.Stop(WAIT_INTERVAL); _debugger.Detach(); } }
/// <summary> /// Prepares the debugger /// </summary> /// <param name="debuggeeVersion">Version of the program to debug - eg. "v1.1.4322" /// If null, the version of the executing process will be used</param> internal void InitDebugger(string debuggeeVersion) { if (IsKernelDebuggerEnabled) { throw new DebuggerException("Can not debug because kernel debugger is enabled"); } if (string.IsNullOrEmpty(debuggeeVersion)) { debuggeeVersion = GetDebuggerVersion(); TraceMessage("Debuggee version: Unknown (assuming " + debuggeeVersion + ")"); } else { TraceMessage("Debuggee version: " + debuggeeVersion); } this.debuggeeVersion = debuggeeVersion; int debuggerVersion; // The CLR does not provide 4.0 debugger interface for older versions if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { debuggerVersion = 3; // 2.0 CLR TraceMessage("Debugger interface version: v2.0"); } else { debuggerVersion = 4; // 4.0 CLR TraceMessage("Debugger interface version: v4.0"); } corDebug = NativeMethods.CreateDebuggingInterfaceFromVersion(debuggerVersion, debuggeeVersion); TrackedComObjects.Track(corDebug); managedCallbackSwitch = new ManagedCallbackSwitch(this); managedCallbackProxy = new ManagedCallbackProxy(this, managedCallbackSwitch); corDebug.Initialize(); corDebug.SetManagedHandler(managedCallbackProxy); TraceMessage("ICorDebug initialized"); }
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 ICorDebug GetDebuggerForProcess(int processID, string minimumVersion, DebuggerCallBacks callBacks = null) { CLRMetaHost mh = new CLRMetaHost(); CLRRuntimeInfo highestLoadedRuntime = null; foreach (var runtime in mh.EnumerateLoadedRuntimes(processID)) { if (highestLoadedRuntime == null || string.Compare(highestLoadedRuntime.GetVersionString(), runtime.GetVersionString(), StringComparison.OrdinalIgnoreCase) < 0) { highestLoadedRuntime = runtime; } } if (highestLoadedRuntime == null) { throw new ApplicationException("Could not enumerate .NET runtimes on the system."); } var runtimeVersion = highestLoadedRuntime.GetVersionString(); if (string.Compare(runtimeVersion, minimumVersion, StringComparison.OrdinalIgnoreCase) < 0) { throw new ApplicationException("Runtime in process " + runtimeVersion + " below the minimum of " + minimumVersion); } ICorDebug rawDebuggingAPI = highestLoadedRuntime.GetLegacyICorDebugInterface(); if (rawDebuggingAPI == null) { throw new ArgumentException("Cannot be null.", "rawDebugggingAPI"); } rawDebuggingAPI.Initialize(); if (callBacks == null) { callBacks = new DebuggerCallBacks(); } rawDebuggingAPI.SetManagedHandler(callBacks); return(rawDebuggingAPI); }
/// <summary> /// Prepares the debugger /// </summary> /// <param name="debuggeeVersion">Version of the program to debug - eg. "v1.1.4322" /// If null, the version of the executing process will be used</param> internal void InitDebugger(string debuggeeVersion) { if (IsKernelDebuggerEnabled) { throw new DebuggerException("Can not debug because kernel debugger is enabled"); } if (string.IsNullOrEmpty(debuggeeVersion)) { debuggeeVersion = GetDebuggerVersion(); } this.debuggeeVersion = debuggeeVersion; corDebug = new ICorDebug(NativeMethods.CreateDebuggingInterfaceFromVersion(3, debuggeeVersion)); managedCallbackSwitch = new ManagedCallbackSwitch(this); managedCallbackProxy = new ManagedCallbackProxy(this, managedCallbackSwitch); corDebug.Initialize(); corDebug.SetManagedHandler(new ICorDebugManagedCallback(managedCallbackProxy)); TraceMessage("ICorDebug initialized, debugee version " + debuggeeVersion); }
/// <summary> /// Prepares the debugger /// </summary> /// <param name="debuggeeVersion">Version of the program to debug - eg. "v1.1.4322" /// If null, the version of the executing process will be used</param> internal void InitDebugger(string debuggeeVersion) { if (IsKernelDebuggerEnabled) { throw new DebuggerException("Can not debug because kernel debugger is enabled"); } if (string.IsNullOrEmpty(debuggeeVersion)) { debuggeeVersion = GetDebuggerVersion(); TraceMessage("Debuggee version: Unknown (assuming " + debuggeeVersion + ")"); } else { TraceMessage("Debuggee version: " + debuggeeVersion); } this.debuggeeVersion = debuggeeVersion; int debuggerVersion; // The CLR does not provide 4.0 debugger interface for older versions if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { debuggerVersion = 3; // 2.0 CLR TraceMessage("Debugger interface version: v2.0"); } else { debuggerVersion = 4; // 4.0 CLR TraceMessage("Debugger interface version: v4.0"); } corDebug = NativeMethods.CreateDebuggingInterfaceFromVersion(debuggerVersion, debuggeeVersion); managedCallbackSwitch = new ManagedCallbackSwitch(this); managedCallbackProxy = new ManagedCallbackProxy(this, managedCallbackSwitch); corDebug.Initialize(); corDebug.SetManagedHandler(managedCallbackProxy); TraceMessage("ICorDebug initialized"); }
/// <summary> /// Prepares the debugger /// </summary> /// <param name="debuggeeVersion">Version of the program to debug - eg. "v1.1.4322" /// If null, the version of the executing process will be used</param> internal void InitDebugger(string debuggeeVersion) { /*if (debuggeeVersion != null && debuggeeVersion.Length > 1) { this.debuggeeVersion = debuggeeVersion; } else { this.debuggeeVersion = GetDebuggerVersion(); } int debug_ver = 3; if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { debug_ver = 3; // 2.0 CLR TraceMessage("Debugger interface version: v2.0"); } else { debug_ver = 4; // 4.0 CLR TraceMessage("Debugger interface version: v4.0"); }*/ if (string.IsNullOrEmpty(debuggeeVersion)) { debuggeeVersion = GetDebuggerVersion(); TraceMessage("Debuggee version: Unknown (assuming " + debuggeeVersion + ")"); } else { TraceMessage("Debuggee version: " + debuggeeVersion); } this.debuggeeVersion = debuggeeVersion; int debuggerVersion; // The CLR does not provide 4.0 debugger interface for older versions if (debuggeeVersion.StartsWith("v1") || debuggeeVersion.StartsWith("v2")) { debuggerVersion = 3; // 2.0 CLR TraceMessage("Debugger interface version: v2.0"); } else { debuggerVersion = 4; // 4.0 CLR TraceMessage("Debugger interface version: v4.0"); } corDebug = new ICorDebug(NativeMethods.CreateDebuggingInterfaceFromVersion(debuggerVersion, debuggeeVersion)); managedCallbackSwitch = new ManagedCallbackSwitch(this); managedCallbackProxy = new ManagedCallbackProxy(this, managedCallbackSwitch); corDebug.Initialize(); corDebug.SetManagedHandler(new ICorDebugManagedCallback(managedCallbackProxy)); TraceMessage("ICorDebug initialized, debugee version " + debuggeeVersion); }
static private MDbgProcess GetProcessFromCordb(ICorDebug cordbg) { Debugger.Processes.FreeStaleUnmanagedResources(); CorDebugger cordebugger = new CorDebugger(cordbg); Debugger.Processes.RegisterDebuggerForCleanup(cordebugger); return new MDbgProcess(Debugger, cordebugger); }
// Create a debugger that can be used to debug an URT program. public CorDebugger() { m_debugger = new CorDebug(); m_debugger.Initialize(); }
private ICorDebug CreateOrGetDebuggerInterface(string filePath) { if (_debuggerInterface == null) { _debuggerInterface = CreateDebuggerInterface(GetMetaHost(), filePath); InitializeDebugger(); } return _debuggerInterface; }
private ICorDebug CreateOrGetDebuggerInterface(Process process) { if (_debuggerInterface == null) { _debuggerInterface = GetDebuggerInterface(GetMetaHost(), process); InitializeDebugger(); } return _debuggerInterface; }
private void InitializeDebugger() { int foundClrs; DebugMethods.EnumerateCLRs(_pid, out _clrHandles, out _clrNames, out foundClrs); var version = new StringBuilder(256); int allocated; DebugMethods.CreateVersionStringFromModule(_pid, _clrNames[0], version, version.Capacity, out allocated); _corDebugInterface = DebugMethods.CreateDebuggingInterfaceFromVersion(version.ToString()); _corDebugInterface.Initialize(); var callback = CreateCallback(); _corDebugInterface.SetManagedHandler(callback); _debugProcess = new DebugProcess(this, _pid); _debugProcess.Attach(); }
/** * Closes the debugger. After this method is called, it is an error * to call any other methods on this object. */ public void Terminate() { Debug.Assert(m_debugger != null); ICorDebug d = m_debugger; m_debugger = null; d.Terminate(); }
DnDebugger(ICorDebug corDebug, DebugOptions debugOptions, IDebugMessageDispatcher debugMessageDispatcher, string debuggeeVersion) { if (debugMessageDispatcher == null) throw new ArgumentNullException(nameof(debugMessageDispatcher)); debuggerManagedThreadId = Thread.CurrentThread.ManagedThreadId; processes = new DebuggerCollection<ICorDebugProcess, DnProcess>(CreateDnProcess); this.debugMessageDispatcher = debugMessageDispatcher; this.corDebug = corDebug; this.debugOptions = debugOptions ?? new DebugOptions(); DebuggeeVersion = debuggeeVersion ?? string.Empty; // I have not tested debugging with CLR 1.x. It's too old to support it so this is a won't fix if (DebuggeeVersion.StartsWith("1.")) throw new NotImplementedException("Can't debug .NET 1.x assemblies. Add an App.config file to force using .NET 2.0 or later"); corDebug.Initialize(); corDebug.SetManagedHandler(new CorDebugManagedCallback(this)); }
protected void Init() { _debug = new CorDebugClass(); _debug.Initialize(); _debug.SetManagedHandler(this); }
public CorDebugger(ICorDebug rawDebuggingAPI) { InitFromICorDebug(rawDebuggingAPI); }
private ClrInterface GetInterface(ICorDebug.IMetadataImport import, int mdIFace) { StringBuilder builder = new StringBuilder(1024); int extends, cnt; System.Reflection.TypeAttributes attr; int res = import.GetTypeDefProps(mdIFace, builder, builder.Capacity, out cnt, out attr, out extends); int scope; string name = null; ClrInterface result = null; if (res == 0) { name = builder.ToString(); } else if (res == 1) { res = import.GetTypeRefProps(mdIFace, out scope, builder, builder.Capacity, out cnt); if (res == 0) { name = builder.ToString(); } else if (res == 1) { } } // TODO: Handle typespec case. if (name != null && !DesktopHeap.Interfaces.TryGetValue(name, out result)) { ClrInterface type = null; if (extends != 0 && extends != 0x01000000) type = GetInterface(import, extends); result = new DesktopHeapInterface(name, type); DesktopHeap.Interfaces[name] = result; } return result; }
public static extern void CoCreateInstance(ref Guid rclsid, IntPtr pUnkOuter, Int32 dwClsContext, ref Guid riid, // must be "ref NativeMethods.IIDICorDebug" [MarshalAs(UnmanagedType.Interface)] out ICorDebug debuggingInterface );
public static extern void CoCreateInstance(ref Guid rclsid, IntPtr pUnkOuter, Int32 dwClsContext, ref Guid riid, // must use "typeof(ICorDebug).GUID" [MarshalAs(UnmanagedType.Interface)] out ICorDebug debuggingInterface );
private static bool DebugActiveSilverlightProcess(int processId, DebugModeFlag debugMode) { MDbgProcess p = null; string[] fullPaths; EventWaitHandle[] continueStartupEvents; bool bMatchFound = false; // some pre-condition checks if (processId <= 0) { throw new MDbgShellException("Invalid arguments passed in"); } // Get all funcs exported by the coreclr's dbg shim. Silverlight.InitSLApi(); // Enumerate all coreclr instances in the process Silverlight.EnumerateCLRs((uint)processId, out fullPaths, out continueStartupEvents); int nSilverlight = fullPaths.Length; if (fullPaths == null || nSilverlight == 0) { throw new MDbgShellException("Could not enumerate any CLRs in specifed Silverlight process"); } // for each coreclr instance found..... for (int i = 0; i < nSilverlight && !bMatchFound; i++) { // Attach to the first one WriteOutput("FOUND: " + fullPaths[i]); string slVersion = Silverlight.CreateVersionStringFromModule((uint)processId, fullPaths[i]); sVersionString = slVersion; // we'll get the required ICorDebug interface from dbgshim.dll ICorDebug cordbg = null; try { cordbg = Silverlight.CreateDebuggingInterfaceFromVersionEx(CorDebugInterfaceVersion.CorDebugLatestVersion, slVersion); } catch (COMException ce) { Console.WriteLine("CDIFVEx failed, will retry with CDIFV.\n" + ce.ToString()); } if (cordbg == null) { cordbg = Silverlight.CreateDebuggingInterfaceFromVersion(slVersion); } p = GetProcessFromCordb(cordbg); // specify JIT flages here p.DebugMode = debugMode; p.Attach((int)processId); bMatchFound = true; // signal the continue event if (!continueStartupEvents[i].SafeWaitHandle.IsInvalid) { continueStartupEvents[i].Set(); } if (null != p) { p.Go().WaitOne(); } } return(bMatchFound); }
private void InitFromICorDebug(ICorDebug rawDebuggingAPI) { Debug.Assert(rawDebuggingAPI!=null); if( rawDebuggingAPI==null ) throw new ArgumentException("Cannot be null.","rawDebugggingAPI"); m_debugger = rawDebuggingAPI; m_debugger.Initialize (); m_debugger.SetManagedHandler (new ManagedCallback(this)); }