private IServiceProvider GetServices() { // If there is no target, then provide just the global services ServiceProvider services = _serviceProvider; if (_target != null) { // Create a per command invocation service provider. These services may change between each command invocation. services = new ServiceProvider(_target.Services); // Add the current thread if any services.AddServiceFactory <IThread>(() => { IThreadService threadService = _target.Services.GetService <IThreadService>(); if (threadService != null && threadService.CurrentThreadId.HasValue) { return(threadService.GetThreadFromId(threadService.CurrentThreadId.Value)); } return(null); }); // Add the current runtime and related services var runtimeService = _target.Services.GetService <IRuntimeService>(); if (runtimeService != null) { services.AddServiceFactory <IRuntime>(() => runtimeService.CurrentRuntime); services.AddServiceFactory <ClrRuntime>(() => services.GetService <IRuntime>()?.Services.GetService <ClrRuntime>()); services.AddServiceFactory <ClrMDHelper>(() => new ClrMDHelper(services.GetService <ClrRuntime>())); } } return(services); }
/// <summary> /// Add all the services needed by commands /// </summary> private void AddServices(DataTarget target) { _serviceProvider.AddService(target); _serviceProvider.AddService <IConsoleService>(_consoleProvider); _serviceProvider.AddService(_commandProcessor); _serviceProvider.AddServiceFactory(typeof(IHelpBuilder), _commandProcessor.CreateHelpBuilder); // Create common analyze context for commands var analyzeContext = new AnalyzeContext() { CurrentThreadId = unchecked ((int)target.DataReader.EnumerateAllThreads().FirstOrDefault()) }; _serviceProvider.AddService(analyzeContext); // Add the register, memory, SOSHost and ClrRuntime services var registerService = new RegisterService(target); _serviceProvider.AddService(registerService); var memoryService = new MemoryService(target.DataReader); _serviceProvider.AddService(memoryService); _serviceProvider.AddServiceFactory(typeof(ClrRuntime), () => CreateRuntime(target)); _serviceProvider.AddServiceFactory(typeof(SOSHost), () => { var sosHost = new SOSHost(_serviceProvider); sosHost.InitializeSOSHost(SymbolReader.TempDirectory, _isDesktop, _dacFilePath, dbiFilePath: null); return(sosHost); }); }
public Analyzer() { LoggingCommand.Initialize(); _serviceProvider = new ServiceProvider(); _consoleProvider = new ConsoleService(); _commandService = new CommandService(); _symbolService = new SymbolService(this); _contextService = new ContextService(this); _serviceProvider.AddService <IHost>(this); _serviceProvider.AddService <IConsoleService>(_consoleProvider); _serviceProvider.AddService <ICommandService>(_commandService); _serviceProvider.AddService <ISymbolService>(_symbolService); _serviceProvider.AddService <IContextService>(_contextService); _serviceProvider.AddServiceFactory <SOSLibrary>(() => SOSLibrary.Create(this)); _contextService.ServiceProvider.AddServiceFactory <ClrMDHelper>(() => { ClrRuntime clrRuntime = _contextService.Services.GetService <ClrRuntime>(); return(clrRuntime != null ? new ClrMDHelper(clrRuntime) : null); }); _commandService.AddCommands(new Assembly[] { typeof(Analyzer).Assembly }); _commandService.AddCommands(new Assembly[] { typeof(ClrMDHelper).Assembly }); _commandService.AddCommands(new Assembly[] { typeof(SOSHost).Assembly }); _commandService.AddCommands(typeof(HelpCommand), (services) => new HelpCommand(_commandService, services)); _commandService.AddCommands(typeof(ExitCommand), (services) => new ExitCommand(_consoleProvider.Stop)); }
/// <summary> /// Create a target instance from IDataReader /// </summary> internal TargetFromDebuggerServices(DebuggerServices debuggerServices, IHost host) : base(host, dumpPath: null) { Debug.Assert(debuggerServices != null); HResult hr = debuggerServices.GetOperatingSystem(out DebuggerServices.OperatingSystem operatingSystem); Debug.Assert(hr == HResult.S_OK); OperatingSystem = operatingSystem switch { DebuggerServices.OperatingSystem.Windows => OSPlatform.Windows, DebuggerServices.OperatingSystem.Linux => OSPlatform.Linux, DebuggerServices.OperatingSystem.OSX => OSPlatform.OSX, _ => throw new PlatformNotSupportedException($"Operating system not supported: {operatingSystem}"), }; hr = debuggerServices.GetDebuggeeType(out DEBUG_CLASS debugClass, out DEBUG_CLASS_QUALIFIER qualifier); Debug.Assert(hr == HResult.S_OK); if (qualifier >= DEBUG_CLASS_QUALIFIER.USER_WINDOWS_SMALL_DUMP) { IsDump = true; } hr = debuggerServices.GetExecutingProcessorType(out IMAGE_FILE_MACHINE type); if (hr == HResult.S_OK) { Architecture = type switch { IMAGE_FILE_MACHINE.I386 => Architecture.X86, IMAGE_FILE_MACHINE.ARM => Architecture.Arm, IMAGE_FILE_MACHINE.THUMB => Architecture.Arm, IMAGE_FILE_MACHINE.THUMB2 => Architecture.Arm, IMAGE_FILE_MACHINE.AMD64 => Architecture.X64, IMAGE_FILE_MACHINE.ARM64 => Architecture.Arm64, _ => throw new PlatformNotSupportedException($"Machine type not supported: {type}"), }; } else { throw new PlatformNotSupportedException($"GetExecutingProcessorType() FAILED {hr:X8}"); } hr = debuggerServices.GetCurrentProcessId(out uint processId); if (hr == HResult.S_OK) { ProcessId = processId; } else { Trace.TraceError("GetCurrentThreadId() FAILED {0:X8}", hr); } // Add the thread, memory, and module services ServiceProvider.AddServiceFactory <IModuleService>(() => new ModuleServiceFromDebuggerServices(this, debuggerServices)); ServiceProvider.AddServiceFactory <IThreadService>(() => new ThreadServiceFromDebuggerServices(this, debuggerServices)); ServiceProvider.AddServiceFactory <IMemoryService>(() => new MemoryServiceFromDebuggerServices(this, debuggerServices)); }
/// <summary> /// Add all the services needed by commands /// </summary> private void AddServices(DataTarget target) { _serviceProvider.AddService(target); _serviceProvider.AddService(target.DataReader); _serviceProvider.AddService <IConsoleService>(_consoleProvider); _serviceProvider.AddService(_commandProcessor); _serviceProvider.AddServiceFactory(typeof(IHelpBuilder), _commandProcessor.CreateHelpBuilder); if (!(target.DataReader is IThreadReader threadReader)) { throw new InvalidOperationException("IThreadReader not implemented"); } // Create common analyze context for commands var analyzeContext = new AnalyzeContext() { CurrentThreadId = threadReader.EnumerateOSThreadIds().FirstOrDefault() }; _serviceProvider.AddService(analyzeContext); // Add the thread, memory, SOSHost and ClrRuntime services var threadService = new ThreadService(target.DataReader); _serviceProvider.AddService <IThreadService>(threadService); var memoryService = new MemoryService(target.DataReader); _serviceProvider.AddService(memoryService); _serviceProvider.AddServiceFactory(typeof(ClrRuntime), () => CreateRuntime(target)); _serviceProvider.AddServiceFactory(typeof(SOSHost), () => { var sosHost = new SOSHost(_serviceProvider); sosHost.InitializeSOSHost(SymbolReader.TempDirectory, _isDesktop, _dacFilePath, dbiFilePath: null); return(sosHost); }); // ClrMD helper for extended commands _serviceProvider.AddServiceFactory(typeof(ClrMDHelper), () => new ClrMDHelper(_serviceProvider) ); }
private HResult RegisterDebuggerServices( IntPtr self, IntPtr iunk) { if (iunk == IntPtr.Zero || DebuggerServices != null) { return(HResult.E_FAIL); } // Create the wrapper for the host debugger services try { DebuggerServices = new DebuggerServices(iunk, HostType); } catch (InvalidCastException ex) { Trace.TraceError(ex.Message); return(HResult.E_NOINTERFACE); } try { var remoteMemoryService = new RemoteMemoryService(iunk); _serviceProvider.AddService <IRemoteMemoryService>(remoteMemoryService); } catch (InvalidCastException) { } HResult hr; try { var consoleService = new ConsoleServiceFromDebuggerServices(DebuggerServices); _serviceProvider.AddService <IConsoleService>(consoleService); _serviceProvider.AddServiceFactory <IThreadUnwindService>(() => new ThreadUnwindServiceFromDebuggerServices(DebuggerServices)); Trace.TraceInformation("HostServices.RegisterDebuggerServices"); // Add each extension command to the native debugger foreach ((string name, string help, IEnumerable <string> aliases) in _commandProcessor.Commands) { hr = DebuggerServices.AddCommand(name, help, aliases); if (hr != HResult.S_OK) { Trace.TraceWarning($"Cannot add extension command {hr:X8} {name} - {help}"); } } } catch (Exception ex) { Trace.TraceError(ex.ToString()); return(HResult.E_FAIL); } hr = DebuggerServices.GetSymbolPath(out string symbolPath); if (hr == HResult.S_OK) { if (!_symbolService.ParseSymbolPath(symbolPath)) { Trace.TraceError("ParseSymbolPath FAILED: {0}", symbolPath); } } else { Trace.TraceError("DebuggerServices.GetSymbolPath FAILED: {0:X8}", hr); } return(HResult.S_OK); }
/// <summary> /// Create a target instance from IDataReader /// </summary> internal TargetFromDebuggerServices(DebuggerServices debuggerServices, IHost host, int id) : base(host, id, dumpPath: null) { Debug.Assert(debuggerServices != null); HResult hr = debuggerServices.GetOperatingSystem(out DebuggerServices.OperatingSystem operatingSystem); Debug.Assert(hr == HResult.S_OK); OperatingSystem = operatingSystem switch { DebuggerServices.OperatingSystem.Windows => OSPlatform.Windows, DebuggerServices.OperatingSystem.Linux => OSPlatform.Linux, DebuggerServices.OperatingSystem.OSX => OSPlatform.OSX, _ => throw new PlatformNotSupportedException($"Operating system not supported: {operatingSystem}"), }; hr = debuggerServices.GetDebuggeeType(out DEBUG_CLASS debugClass, out DEBUG_CLASS_QUALIFIER qualifier); Debug.Assert(hr == HResult.S_OK); if (qualifier >= DEBUG_CLASS_QUALIFIER.USER_WINDOWS_SMALL_DUMP) { IsDump = true; } hr = debuggerServices.GetExecutingProcessorType(out IMAGE_FILE_MACHINE type); if (hr == HResult.S_OK) { Architecture = type switch { IMAGE_FILE_MACHINE.I386 => Architecture.X86, IMAGE_FILE_MACHINE.ARM => Architecture.Arm, IMAGE_FILE_MACHINE.THUMB => Architecture.Arm, IMAGE_FILE_MACHINE.THUMB2 => Architecture.Arm, IMAGE_FILE_MACHINE.AMD64 => Architecture.X64, IMAGE_FILE_MACHINE.ARM64 => Architecture.Arm64, _ => throw new PlatformNotSupportedException($"Machine type not supported: {type}"), }; } else { throw new PlatformNotSupportedException($"GetExecutingProcessorType() FAILED {hr:X8}"); } hr = debuggerServices.GetCurrentProcessId(out uint processId); if (hr == HResult.S_OK) { ProcessId = processId; } else { Trace.TraceError("GetCurrentThreadId() FAILED {0:X8}", hr); } // Add the thread, memory, and module services IMemoryService rawMemoryService = new MemoryServiceFromDebuggerServices(this, debuggerServices); ServiceProvider.AddServiceFactory <IModuleService>(() => new ModuleServiceFromDebuggerServices(this, rawMemoryService, debuggerServices)); ServiceProvider.AddServiceFactory <IThreadService>(() => new ThreadServiceFromDebuggerServices(this, debuggerServices)); ServiceProvider.AddServiceFactory <IMemoryService>(() => { Debug.Assert(Host.HostType != HostType.DotnetDump); IMemoryService memoryService = rawMemoryService; if (IsDump && Host.HostType == HostType.Lldb) { // This is a special memory service that maps the managed assemblies' metadata into the address // space. The lldb debugger returns zero's (instead of failing the memory read) for missing pages // in core dumps that older (< 5.0) createdumps generate so it needs this special metadata mapping // memory service. dotnet-dump needs this logic for clrstack -i (uses ICorDebug data targets). return(new MetadataMappingMemoryService(this, memoryService)); } return(memoryService); }); }