private void Initialize() { _debugger = CreateDebuggerClient(); _control = _debugger as IDebugControl4; _symbols = _debugger as IDebugSymbols4; _systemObjects = _debugger as IDebugSystemObjects3; _advanced = _debugger as IDebugAdvanced3; _spaces = _debugger as IDebugDataSpaces4; // in case previous debugging session hasn't finished correctly // some leftover breakpoints may exist (even if debugging target has changed) _control.ClearBreakpoints(); _requestHelper = new RequestHelper(_advanced, _spaces, _symbols); _commandExecutor = new CommandExecutor(_control); _output = new OutputCallbacks(); _callbacks = new EventCallbacks(_control); _callbacks.BreakpointHit += OnBreakpoint; _callbacks.ExceptionHit += OnException; _callbacks.BreakHappened += OnBreak; _callbacks.ThreadStarted += OnThreadStarted; _callbacks.ThreadFinished += OnThreadFinished; _callbacks.ProcessExited += OnProcessExited; _debugger.SetEventCallbacks(_callbacks); _debugger.SetOutputCallbacks(_output); _debugger.SetInputCallbacks(new InputCallbacks()); _visualizers = new VisualizerRegistry(new DefaultVisualizer(_requestHelper, _symbols, _output)); InitializeHandlers(); }
private void CreateClient(IDebugClient client) { DebuggerInterface = client; _spaces = (IDebugDataSpaces)DebuggerInterface; _spacesPtr = (IDebugDataSpacesPtr)DebuggerInterface; _symbols = (IDebugSymbols)DebuggerInterface; _control = (IDebugControl2)DebuggerInterface; // These interfaces may not be present in older DbgEng dlls. _spaces2 = DebuggerInterface as IDebugDataSpaces2; _symbols3 = DebuggerInterface as IDebugSymbols3; _advanced = DebuggerInterface as IDebugAdvanced; _systemObjects = DebuggerInterface as IDebugSystemObjects; _systemObjects3 = DebuggerInterface as IDebugSystemObjects3; Interlocked.Increment(ref s_totalInstanceCount); if (_systemObjects3 == null && s_totalInstanceCount > 1) { throw new ClrDiagnosticsException("This version of DbgEng is too old to create multiple instances of DataTarget.", ClrDiagnosticsExceptionKind.DebuggerError); } if (_systemObjects3 != null) { _systemObjects3.GetCurrentSystemId(out _instance); } }
private void Dispose(bool disposing) { if (!_isDisposed) { if (disposing) { _cancel.Cancel(); if (_debuggerThread != null) _debuggerThread.Join(TimeSpan.FromMilliseconds(100)); if (_callbacks != null) { _callbacks.BreakpointHit -= OnBreakpoint; _callbacks.ExceptionHit -= OnException; _callbacks.BreakHappened -= OnBreak; _callbacks.ThreadFinished -= OnThreadFinished; _callbacks.ThreadStarted -= OnThreadStarted; _callbacks.ProcessExited -= OnProcessExited; } if (_debugger != null) { _debugger.EndSession(DEBUG_END.ACTIVE_TERMINATE); _debugger.SetEventCallbacks(null); _debugger.SetOutputCallbacks(null); _debugger.SetInputCallbacks(null); } _callbacks = null; _messages.Dispose(); } if (_debugger != null) { while (Marshal.ReleaseComObject(_debugger) > 0) { } } _debugger = null; _control = null; _symbols = null; _spaces = null; _systemObjects = null; _advanced = null; _requestHelper = null; _isDisposed = true; } }
/// <summary> /// Initializes a new instance of the <see cref="DebugEngineProxy" /> class. /// </summary> /// <param name="control">The control.</param> /// <param name="client">The client.</param> /// <param name="registers">The registers.</param> /// <param name="systemObjects">The system objects.</param> /// <param name="debugDataSpaces">The debug data spaces.</param> public DebugEngineProxy(IDebugControl6 control, IDebugClient5 client, IDebugRegisters2 registers, IDebugSystemObjects systemObjects, IDebugDataSpaces debugDataSpaces, IExecuteWrapper executeWrapper) { Control = control; Client = client; Registers = registers; Dataspaces = debugDataSpaces; ExecuteWrapper = executeWrapper; RegisterEngine = new RegisterEngine(); // todo: inject MemoryEngine = new MemoryEngine(); SystemObjects = systemObjects; Is32Bit = Regex.Match(ExecuteWrapper.Execute("!peb"), @"PEB at (?<peb>[a-fA-F0-9]+)").Groups["peb"].Value .Length == 8; }
/// <summary> /// Reads virtual memory from the trace file /// </summary> /// <param name="low">The low memory address of the range</param> /// <param name="high">The high memory address of the range</param> /// <param name="dataSpaces">The data spaces COM interface allowing access to the memory</param> /// <returns>System.Byte[].</returns> /// <exception cref="ArgumentOutOfRangeException">When low >= high</exception> /// <exception cref="ApplicationException">If the debugging engine cannot perform the request</exception> public byte[] ReadMemory(ulong low, ulong high, IDebugDataSpaces dataSpaces) { if (low >= high) { throw new ArgumentOutOfRangeException($"Low must be less than high, but {low} >= {high}"); } var length = high - low; var buffer = new byte[length]; var hr = dataSpaces.ReadVirtual(low, buffer, buffer.Length.ToUInt(), out var bytesRead); if (hr != 0) { throw new ApplicationException($"Unable to read virtual memory at {low:X16}, error code: {hr}"); } return(buffer.Take(bytesRead.ToInt()).ToArray()); // todo: bounds checking }
public UserDebugger() { Guid guid = new Guid("27fe5639-8407-4f47-8364-ee118fb08ac8"); object obj = null; int hr = DebugCreate(ref guid, out obj); if (hr < 0) { Console.WriteLine("SourceFix: Unable to acquire client interface"); return; } _client = obj as IDebugClient5; _control = _client as IDebugControl4; _debugDataSpace = _client as IDebugDataSpaces; _client.SetOutputCallbacks(this); _client.SetEventCallbacksWide(this); }
public void Dispose() { if (_debugDataSpace != null) { Marshal.ReleaseComObject(_debugDataSpace); _debugDataSpace = null; } if (_control != null) { Marshal.ReleaseComObject(_control); _control = null; } if (_client != null) { Marshal.ReleaseComObject(_client); _client = null; } }
public RequestHelper(IDebugAdvanced3 advanced, IDebugDataSpaces spaces, IDebugSymbols4 symbols) { if (advanced == null) { throw new ArgumentNullException(nameof(advanced)); } if (spaces == null) { throw new ArgumentNullException(nameof(spaces)); } if (symbols == null) { throw new ArgumentNullException(nameof(symbols)); } _advanced = advanced; _spaces = spaces; _symbols = symbols; }
/// <summary> /// Initializes the API. /// </summary> /// <param name="log">The log.</param> internal static void InitApi(ILog log = null) { LastHR = HRESULT.S_OK; if (client != null) { return; } try { log?.Debug("Client did not exist. Creating a new client and associated interfaces."); client = (IDebugClient5)CreateIDebugClient(); control = (IDebugControl6)client; registers = (IDebugRegisters2)client; symbols = (IDebugSymbols5)client; systemObjects = (IDebugSystemObjects)client; debugDataSpaces = (IDebugDataSpaces)client; } catch (Exception e) { log?.Fatal("Unable to create debug client. Are you missing DLLs?"); log?.Fatal(e); LastHR = HRESULT.E_UNEXPECTED; } }
public MemoryEngineBuilder WithReadMemory(ulong start, ulong end, IDebugDataSpaces spaces, bool is32Bit, byte[] bytes) { Mock.Setup(engine => engine.ReadMemory(start, end, spaces)).Returns(bytes); return(this); }
private void CreateClient(IDebugClient client) { _client = client; _spaces = (IDebugDataSpaces)_client; _spacesPtr = (IDebugDataSpacesPtr)_client; _symbols = (IDebugSymbols)_client; _control = (IDebugControl2)_client; // These interfaces may not be present in older DbgEng dlls. _spaces2 = _client as IDebugDataSpaces2; _symbols3 = _client as IDebugSymbols3; _advanced = _client as IDebugAdvanced; _systemObjects = _client as IDebugSystemObjects; _systemObjects3 = _client as IDebugSystemObjects3; Interlocked.Increment(ref s_totalInstanceCount); if (_systemObjects3 == null && s_totalInstanceCount > 1) throw new ClrDiagnosticsException("This version of DbgEng is too old to create multiple instances of DataTarget.", ClrDiagnosticsException.HR.DebuggerError); if (_systemObjects3 != null) _systemObjects3.GetCurrentSystemId(out _instance); }