/// <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); }
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)); }
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); }
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); }
private void InitializeDebugger() { _debuggerInterface.Initialize(); _managedCallback = new ManagedCallbackProxy(new ManagedCallback(this)); _debuggerInterface.SetManagedHandler(_managedCallback); }
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); }
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(); }
// Create a debugger that can be used to debug an URT program. public CorDebugger() { m_debugger = new CorDebug(); m_debugger.Initialize(); }
/// <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); }
/// <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"); }
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)); }