Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Diagnostics.Runtime;

namespace Microsoft.Diagnostics.DebugServices.Implementation
{
/// <summary>
/// Adapter exposing the host's <see cref="IModuleService"/> /
/// <see cref="IModuleSymbols"/> through ClrMD's
/// <see cref="IClrSymbolProvider"/> contract.
/// </summary>
[ServiceExport(Type = typeof(IClrSymbolProvider), Scope = ServiceScope.Target)]
public sealed class HostSymbolProvider : IClrSymbolProvider
{
private readonly IModuleService _moduleService;
private readonly ulong _signExtensionMask;

public HostSymbolProvider(IModuleService moduleService, IMemoryService memoryService)
{
_moduleService = moduleService ?? throw new ArgumentNullException(nameof(moduleService));
_signExtensionMask = memoryService?.SignExtensionMask() ?? ulong.MaxValue;
}

public bool TryGetSymbolName(ulong address, out string symbolName, out ulong displacement)
{
symbolName = null;
displacement = 0;

address &= _signExtensionMask;

IModule module;
try
{
module = _moduleService.GetModuleFromAddress(address);
}
catch (DiagnosticsException)
{
return false;
}
if (module is null)
{
return false;
}

IModuleSymbols symbols = module.Services.GetService<IModuleSymbols>();
if (symbols is null)
{
return false;
}

if (!symbols.TryGetSymbolName(address, out string bareName, out displacement)
|| string.IsNullOrEmpty(bareName))
{
return false;
}

// Strip any module! qualifier the lower-level service might have
// prepended — the new contract returns bare names only.
int bang = bareName.IndexOf('!');
symbolName = bang >= 0 && bang + 1 < bareName.Length ? bareName.Substring(bang + 1) : bareName;
return true;
}

public bool TryGetSymbolAddress(ulong moduleBase, string name, out ulong address)
{
address = 0;
if (string.IsNullOrEmpty(name))
{
return false;
}

moduleBase &= _signExtensionMask;

// moduleBase != 0 restricts the search to a single module.
if (moduleBase != 0)
{
IModule scopedModule;
try
{
scopedModule = _moduleService.GetModuleFromBaseAddress(moduleBase);
}
catch (DiagnosticsException)
{
return false;
}
if (scopedModule is null)
{
return false;
}

IModuleSymbols scopedSymbols = scopedModule.Services.GetService<IModuleSymbols>();
if (scopedSymbols is null)
{
return false;
}

if (scopedSymbols.TryGetSymbolAddress(name, out ulong scopedAddr) && scopedAddr != 0)
{
address = scopedAddr;
return true;
}
return false;
}

foreach (IModule module in _moduleService.EnumerateModules())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even want to support moduleBase == 0? To scan everything? Once we reach here this is going to grind the entire debugger to an absolute halt, and with no cancellation token possible across com, this is going to be pure pain.

{
IModuleSymbols symbols = module.Services.GetService<IModuleSymbols>();
if (symbols is null)
{
continue;
}

if (symbols.TryGetSymbolAddress(name, out ulong addr) && addr != 0)
{
address = addr;
return true;
}
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ public Runtime(IServiceProvider services, int id, ClrInfo clrInfo)
_settingsService = services.GetService<ISettingsService>() ?? throw new ArgumentException("ISettingsService required");
_symbolService = services.GetService<ISymbolService>() ?? throw new ArgumentException("ISymbolService required");

RuntimeType = RuntimeType.Unknown;
if (clrInfo.Flavor == ClrFlavor.Core)
{
RuntimeType = RuntimeType.NetCore;
}
else if (clrInfo.Flavor == ClrFlavor.Desktop)
{
RuntimeType = RuntimeType.Desktop;
}
RuntimeType = GetRuntimeType(clrInfo.Flavor);
RuntimeModule = services.GetService<IModuleService>().GetModuleFromBaseAddress(clrInfo.ModuleInfo.ImageBase);

ServiceContainerFactory containerFactory = services.GetService<IServiceManager>().CreateServiceContainerFactory(ServiceScope.Runtime, services);
Expand Down Expand Up @@ -129,6 +121,15 @@ public string GetDacFilePath(out bool verifySignature)
_verifySignature = _settingsService.DacSignatureVerificationEnabled;
}
}
if (_dacFilePath is null)
{
_cdacFilePath ??= GetLibraryPath(DebugLibraryKind.CDac);
if (_cdacFilePath is not null)
{
verifySignature = false;
return _cdacFilePath;
}
}
Comment thread
max-charlamb marked this conversation as resolved.
verifySignature = _verifySignature;
return _dacFilePath;
}
Expand Down Expand Up @@ -318,9 +319,18 @@ public override int GetHashCode()
"Desktop .NET Framework",
".NET Core",
".NET Core (single-file)",
"Native AOT",
"Other"
};

private static RuntimeType GetRuntimeType(ClrFlavor flavor) => flavor switch
{
ClrFlavor.Core => RuntimeType.NetCore,
ClrFlavor.Desktop => RuntimeType.Desktop,
ClrFlavor.NativeAOT => RuntimeType.NativeAOT,
_ => RuntimeType.Unknown,
};

public override string ToString()
{
StringBuilder sb = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public IEnumerable<IRuntime> EnumerateRuntimes(int startingRuntimeId, RuntimeEnu
DataTarget dataTarget = new(_services.GetService<IDataReader>(), new DataTargetOptions()
{
ForceCompleteRuntimeEnumeration = (flags & RuntimeEnumerationFlags.All) != 0,
VerifyDacOnWindows = settingsService?.DacSignatureVerificationEnabled ?? true
VerifyDacOnWindows = settingsService?.DacSignatureVerificationEnabled ?? true,
SymbolProvider = _services.GetService<IClrSymbolProvider>(),
});
for (int i = 0; i < dataTarget.ClrVersions.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public void RegisterExportedServices(Type serviceType)
{
throw new InvalidOperationException();
}

if (typeof(IHostExtension).IsAssignableFrom(serviceType))
{
InvokeHostExtension(serviceType);
}

for (Type currentType = serviceType; currentType is not null; currentType = currentType.BaseType)
{
if (currentType == typeof(object) || currentType == typeof(ValueType))
Expand Down Expand Up @@ -185,6 +191,27 @@ or FileLoadException
}
}

private static void InvokeHostExtension(Type type)
{
if (type.IsAbstract)
{
return;
}
if (type.GetConstructor(Type.EmptyTypes) is null)
{
throw new InvalidOperationException($"IHostExtension implementation '{type.FullName}' must have a public parameterless constructor.");
}
try
{
IHostExtension extension = (IHostExtension)Activator.CreateInstance(type);
extension.Initialize();
}
catch (Exception ex) when (ex is not DiagnosticsException)
{
throw new DiagnosticsException($"IHostExtension '{type.FullName}' initialization failed: {ex.Message}", ex);
}
}

/// <summary>
/// Add service factory for the specific scope.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions src/Microsoft.Diagnostics.DebugServices/IHostExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Diagnostics.DebugServices
{
/// <summary>
/// Implemented by extension assemblies to perform process-wide initialization
/// (for example registering an <c>IClrInfoProvider</c>) when the extension
/// is loaded.
///
/// Implementations must have a public parameterless constructor. Every
/// concrete <see cref="IHostExtension"/> implementation discovered in an
/// extension assembly is instantiated and its <see cref="Initialize"/>
/// method invoked once during <see cref="IServiceManager"/> assembly
/// registration, before any other service or provider exports from the
/// assembly are consumed. An assembly may contain multiple
/// implementations; invocation order across implementations is
/// unspecified.
/// </summary>
public interface IHostExtension
{
/// <summary>
/// Performs one-time initialization for the extension.
/// </summary>
void Initialize();
}
}
Loading
Loading