Skip to content
Merged
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
4 changes: 2 additions & 2 deletions eng/Version.Details.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ This file should be imported by eng/Versions.props
<MicrosoftNETCoreAppRefPackageVersion>11.0.0</MicrosoftNETCoreAppRefPackageVersion>
<MicrosoftNETCorePlatformsPackageVersion>11.0.0-rtm.26514.118</MicrosoftNETCorePlatformsPackageVersion>
<!-- microsoft-clrmd dependencies -->
<MicrosoftDiagnosticsRuntimePackageVersion>4.2.752201</MicrosoftDiagnosticsRuntimePackageVersion>
<MicrosoftDiagnosticsRuntimeUtilitiesPackageVersion>4.2.752201</MicrosoftDiagnosticsRuntimeUtilitiesPackageVersion>
<MicrosoftDiagnosticsRuntimePackageVersion>4.2.755301</MicrosoftDiagnosticsRuntimePackageVersion>
<MicrosoftDiagnosticsRuntimeUtilitiesPackageVersion>4.2.755301</MicrosoftDiagnosticsRuntimeUtilitiesPackageVersion>
</PropertyGroup>
<!--Property group for alternate package version names-->
<PropertyGroup>
Expand Down
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Dependencies>
<Source Uri="https://github.com/dotnet/dotnet" Mapping="diagnostics" Sha="a586537b3872e4e2f69c0b1ce7017a97bc3bcd55" BarId="336755" />
<ProductDependencies>
<Dependency Name="Microsoft.Diagnostics.Runtime" Version="4.2.752201">
<Dependency Name="Microsoft.Diagnostics.Runtime" Version="4.2.755301">
<Uri>https://github.com/microsoft/clrmd</Uri>
<Sha>a16ead09f45f816fc43a8c69ccc814cf4291ad66</Sha>
<Sha>cba085b49b816c88ea5ca80327c0226ed4141a74</Sha>
</Dependency>
<Dependency Name="Microsoft.Diagnostics.Runtime.Utilities" Version="4.2.752201">
<Dependency Name="Microsoft.Diagnostics.Runtime.Utilities" Version="4.2.755301">
<Uri>https://github.com/microsoft/clrmd</Uri>
<Sha>a16ead09f45f816fc43a8c69ccc814cf4291ad66</Sha>
<Sha>cba085b49b816c88ea5ca80327c0226ed4141a74</Sha>
</Dependency>
<Dependency Name="System.CommandLine" Version="2.0.0-beta5.25210.1">
<Uri>https://github.com/dotnet/command-line-api</Uri>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
<ProjectReference Include="$(MSBuildThisFileDirectory)..\Microsoft.Diagnostics.DebugServices\Microsoft.Diagnostics.DebugServices.csproj" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\Microsoft.SymbolStore\Microsoft.SymbolStore.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.Diagnostics.DebugServices.UnitTests" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Utilities;
using Microsoft.SymbolStore;
Expand All @@ -30,14 +31,21 @@ public class Runtime : IRuntime, IDisposable
private string _dacFilePath;
private bool _verifySignature;
private string _dbiFilePath;
private Action _releaseDataTarget;

protected readonly ServiceContainer _serviceContainer;

public Runtime(IServiceProvider services, int id, ClrInfo clrInfo)
: this(services, id, clrInfo, clrInfo is null ? null : clrInfo.DataTarget.Dispose)
{
}

internal Runtime(IServiceProvider services, int id, ClrInfo clrInfo, Action releaseDataTarget)
{
Target = services.GetService<ITarget>() ?? throw new DiagnosticsException("Dump or live session target required");
Id = id;
_clrInfo = clrInfo ?? throw new ArgumentNullException(nameof(clrInfo));
_releaseDataTarget = releaseDataTarget ?? throw new ArgumentNullException(nameof(releaseDataTarget));
_settingsService = services.GetService<ISettingsService>() ?? throw new ArgumentException("ISettingsService required");
_symbolService = services.GetService<ISymbolService>() ?? throw new ArgumentException("ISymbolService required");

Expand All @@ -55,15 +63,19 @@ public Runtime(IServiceProvider services, int id, ClrInfo clrInfo)

void IDisposable.Dispose()
{
// The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime
// instance is disposed below in DisposeServices().
_clrRuntime?.DataTarget.Dispose();
_clrRuntime = null;
_serviceContainer.RemoveService(typeof(IRuntime));
_serviceContainer.DisposeServices();
_clrDataProcess?.Dispose();
_clrDataProcess = null;
_cdacActivationResult = null;
try
{
_clrRuntime = null;
_serviceContainer.RemoveService(typeof(IRuntime));
_serviceContainer.DisposeServices();
_clrDataProcess?.Dispose();
_clrDataProcess = null;
_cdacActivationResult = null;
}
finally
{
Interlocked.Exchange(ref _releaseDataTarget, null)?.Invoke();
}
}

#region IRuntime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Diagnostics.Runtime;

namespace Microsoft.Diagnostics.DebugServices.Implementation
Expand All @@ -15,6 +17,9 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
[ProviderExport(Type = typeof(IRuntimeProvider))]
public class RuntimeProvider : IRuntimeProvider
{
private const string RuntimeInfoExport = "DotNetRuntimeInfo";
private const string ContractDescriptorExport = "DotNetRuntimeContractDescriptor";

private readonly IServiceProvider _services;

public RuntimeProvider(IServiceProvider services)
Expand All @@ -31,11 +36,17 @@ public RuntimeProvider(IServiceProvider services)
/// <param name="flags">Enumeration control flags</param>
public IEnumerable<IRuntime> EnumerateRuntimes(int startingRuntimeId, RuntimeEnumerationFlags flags)
{
// The ClrInfo and DataTarget instances are disposed when Runtime instance is disposed. Runtime instances are
// not flushed when the Target/RuntimeService is flushed; they are all disposed and the list cleared. They are
// all re-created the next time the IRuntime or ClrRuntime instance is queried.
ISettingsService settingsService = _services.GetService<ISettingsService>();
bool verifyDac = settingsService?.DacSignatureVerificationEnabled ?? true;
IDataReader dataReader = _services.GetService<IDataReader>();
IModule entryPoint = dataReader.TargetPlatform == OSPlatform.Windows
? _services.GetService<IModuleService>()?.EntryPointModule
: null;
IReadOnlyList<ModuleInfo> runtimeModules = EnumerateRuntimeModules(
dataReader,
flags,
entryPoint?.ImageBase,
entryPoint?.FileName);

// The cDAC (mscordaccore_universal) ships inside the (signed) diagnostics tool package and
// carries no individual DAC signature, so it cannot satisfy ClrMD's signature check. Trust it
Expand All @@ -47,13 +58,13 @@ public IEnumerable<IRuntime> EnumerateRuntimes(int startingRuntimeId, RuntimeEnu
string normalizedTrustedCDacPath = string.IsNullOrEmpty(trustedCDacPath) ? null : Path.GetFullPath(trustedCDacPath);
StringComparison pathComparison = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

DataTarget dataTarget = new(_services.GetService<IDataReader>(), new DataTargetOptions()
DataTargetOptions CreateDataTargetOptions(bool skipRuntimeEnumeration) => new()
{
ForceCompleteRuntimeEnumeration = (flags & RuntimeEnumerationFlags.All) != 0,
ForceCompleteRuntimeEnumeration = !skipRuntimeEnumeration,
SkipRuntimeEnumeration = skipRuntimeEnumeration,
VerifyDacOnWindows = verifyDac,
// Takes priority over VerifyDacOnWindows: skip verification only for the exact bundled cDAC.
DacSignatureVerificationOverride = (dacFilePath) =>
{
DacSignatureVerificationOverride = (dacFilePath) => {
if (normalizedTrustedCDacPath is not null
&& !string.IsNullOrEmpty(dacFilePath)
&& string.Equals(Path.GetFullPath(dacFilePath), normalizedTrustedCDacPath, pathComparison))
Expand All @@ -63,13 +74,185 @@ public IEnumerable<IRuntime> EnumerateRuntimes(int startingRuntimeId, RuntimeEnu
return verifyDac;
},
SymbolProvider = _services.GetService<IClrSymbolProvider>(),
});
for (int i = 0; i < dataTarget.ClrVersions.Length; i++)
};

DataTarget dataTarget = new(dataReader, CreateDataTargetOptions(skipRuntimeEnumeration: true));
// The enumerator and each yielded runtime own a reference to the shared, complete target.
int referenceCount = 1;
Action releaseDataTarget = () => {
if (Interlocked.Decrement(ref referenceCount) == 0)
{
dataTarget.Dispose();
}
};

try
{
using (DataTarget discoveryTarget = new(
new RuntimeModuleDataReader(dataReader, runtimeModules),
CreateDataTargetOptions(skipRuntimeEnumeration: false)))
{
foreach (ClrInfo clrInfo in discoveryTarget.ClrVersions)
{
dataTarget.AddRuntime(CloneClrInfo(clrInfo, dataTarget));
}
}

int runtimeId = startingRuntimeId;
foreach (ClrInfo clrInfo in dataTarget.ClrVersions)
{
Runtime runtime = new(_services, runtimeId++, clrInfo, releaseDataTarget);
Interlocked.Increment(ref referenceCount);
yield return runtime;
}
}
finally
{
yield return new Runtime(_services, startingRuntimeId + i, dataTarget.ClrVersions[i]);
releaseDataTarget();
}
}

internal static IReadOnlyList<ModuleInfo> EnumerateRuntimeModules(
IDataReader dataReader,
RuntimeEnumerationFlags flags,
ulong? entryPointBase,
string entryPointFileName)
{
if (dataReader is null)
{
throw new ArgumentNullException(nameof(dataReader));
}

List<ModuleInfo> modules = dataReader.EnumerateModules().ToList();
if ((flags & RuntimeEnumerationFlags.All) != 0)
{
return modules;
}

HashSet<ulong> candidates = new();
foreach (ModuleInfo module in modules)
{
if (IsNamedRuntimeModule(module.FileName))
{
candidates.Add(module.ImageBase);
}
}

if (dataReader.TargetPlatform != OSPlatform.Windows)
{
// Unix module ordering does not identify the executable reliably. Follow ClrMD:
// use createdump's runtime hint when supported, otherwise probe every module.
bool hasDiagnosticRuntime = SpecialDiagInfo.TryGetRuntimeBaseAddress(dataReader, out ulong runtimeBase) &&
modules.Any(module => module.ImageBase == runtimeBase && HasRuntimeExport(module));
foreach (ModuleInfo module in modules)
{
if (hasDiagnosticRuntime ? module.ImageBase == runtimeBase : HasRuntimeExport(module))
{
candidates.Add(module.ImageBase);
}
}
}
else if (entryPointBase.HasValue && !string.IsNullOrEmpty(entryPointFileName))
{
string entryPointName = GetFileNameWithoutExtension(entryPointFileName);
foreach (ModuleInfo module in modules)
{
bool isEntryPoint = module.ImageBase == entryPointBase.Value;
bool isEntryPointDll =
dataReader.TargetPlatform == OSPlatform.Windows &&
GetExtension(module.FileName).Equals(".dll", StringComparison.OrdinalIgnoreCase) &&
GetFileNameWithoutExtension(module.FileName).Equals(entryPointName, StringComparison.OrdinalIgnoreCase);
if ((isEntryPoint || isEntryPointDll) && HasRuntimeExport(module))
{
candidates.Add(module.ImageBase);
}
}
}

return modules.Where(module => candidates.Contains(module.ImageBase)).ToList();
}

private static bool IsNamedRuntimeModule(string fileName)
{
string moduleName = GetFileName(fileName);
return moduleName.Equals("clr.dll", StringComparison.OrdinalIgnoreCase)
|| moduleName.Equals("coreclr.dll", StringComparison.OrdinalIgnoreCase)
|| moduleName.Equals("libcoreclr.so", StringComparison.Ordinal)
|| moduleName.Equals("libcoreclr.dylib", StringComparison.OrdinalIgnoreCase);
}

private static bool HasRuntimeExport(ModuleInfo module)
{
return GetExportAddress(module, RuntimeInfoExport) != 0
|| GetExportAddress(module, ContractDescriptorExport) != 0;
}

private static ulong GetExportAddress(ModuleInfo module, string export)
{
try
{
return module.GetExportSymbolAddress(export);
}
catch (Exception ex) when
(ex is IOException or
InvalidDataException or
ArgumentException or
OverflowException or
DiagnosticsException)
{
return 0;
}
}

private static ClrInfo CloneClrInfo(ClrInfo source, DataTarget dataTarget) =>
new(dataTarget, source.ModuleInfo, source.Version)
{
BuildId = source.BuildId,
ContractDescriptorAddress = source.ContractDescriptorAddress,
DebuggingLibraries = source.DebuggingLibraries,
Flavor = source.Flavor,
IndexFileSize = source.IndexFileSize,
IndexTimeStamp = source.IndexTimeStamp,
IsSingleFile = source.IsSingleFile,
};

private static string GetFileName(string path) =>
Path.GetFileName(path.Replace('\\', '/'));

private static string GetFileNameWithoutExtension(string path) =>
Path.GetFileNameWithoutExtension(path.Replace('\\', '/'));

private static string GetExtension(string path) =>
Path.GetExtension(path.Replace('\\', '/'));

private sealed class RuntimeModuleDataReader : IDataReader
{
private readonly IDataReader _reader;
private readonly IReadOnlyList<ModuleInfo> _modules;

public RuntimeModuleDataReader(IDataReader reader, IReadOnlyList<ModuleInfo> modules)
{
_reader = reader ?? throw new ArgumentNullException(nameof(reader));
_modules = modules ?? throw new ArgumentNullException(nameof(modules));
}

public string DisplayName => _reader.DisplayName;
public bool IsThreadSafe => _reader.IsThreadSafe;
public OSPlatform TargetPlatform => _reader.TargetPlatform;
public Architecture Architecture => _reader.Architecture;
public int ProcessId => _reader.ProcessId;
public int PointerSize => _reader.PointerSize;
public IEnumerable<ModuleInfo> EnumerateModules() => _modules;
public bool GetThreadContext(uint threadID, uint contextFlags, Span<byte> context) =>
_reader.GetThreadContext(threadID, contextFlags, context);
public void FlushCachedData() => _reader.FlushCachedData();
public int Read(ulong address, Span<byte> buffer) => _reader.Read(address, buffer);
public bool Read<T>(ulong address, out T value) where T : unmanaged => _reader.Read(address, out value);
public T Read<T>(ulong address) where T : unmanaged => _reader.Read<T>(address);
public bool ReadPointer(ulong address, out ulong value) => _reader.ReadPointer(address, out value);
public ulong ReadPointer(ulong address) => _reader.ReadPointer(address);
}

#endregion
}
}
Loading