Skip to content

Commit 30d5f17

Browse files
author
Mike McLaughlin
authored
Changes needed for clrmd 2.1 (#3094)
* Changes needed for clrmd 2.1 Remove VersionData and convert to System.Version. Not sure why I didn't use System.Version before. Add DbgEng Interop classes from Microsoft.Diagnostics.Runtime.Utilities. CLRMD 2.1 has removed that assembly * Update CLRMD version to latest * Update CLRMD version * Disable DebugServices.UnitTests TestDbgEng tests because of problems loading dbgeng.dll * Update comment with issue
1 parent 9807fb4 commit 30d5f17

243 files changed

Lines changed: 18179 additions & 615 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eng/CodeAnalysis.ruleset

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<RuleSet Name="Diagnostics Ruleset"
2-
Description="Diagnostics Ruleset"
3-
ToolsVersion="14.0">
4-
5-
<!-- Define all the analyzer rule actions for this repo. -->
6-
<Include Path="CodeAnalysis.Repository.ruleset" Action="Default" />
7-
8-
<!-- This will override or define all rules needed values to be SDL compliant. -->
9-
<Include Path="CodeAnalysis.Security.ruleset" Action="Default" />
10-
</RuleSet>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Diagnostics Ruleset" Description="Diagnostics Ruleset" ToolsVersion="14.0">
3+
<!-- Define all the analyzer rule actions for this repo. -->
4+
<Include Path="CodeAnalysis.Repository.ruleset" Action="Default" />
5+
<!-- This will override or define all rules needed values to be SDL compliant. -->
6+
<Include Path="CodeAnalysis.Security.ruleset" Action="Default" />
7+
<Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
8+
<Rule Id="CA1069" Action="None" />
9+
</Rules>
10+
</RuleSet>

eng/Version.Details.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<Uri>https://github.com/dotnet/symstore</Uri>
55
<Sha>f3993563fcc13b10ac0e5c4384f346aa88758468</Sha>
66
</Dependency>
7-
<Dependency Name="Microsoft.Diagnostics.Runtime" Version="2.0.325901">
7+
<Dependency Name="Microsoft.Diagnostics.Runtime" Version="2.0.327704">
88
<Uri>https://github.com/microsoft/clrmd</Uri>
9-
<Sha>a64d9ac11086f28fbd4b2b2337c19be7826fbfa9</Sha>
9+
<Sha>be891ed2cc2c8b98d9e0b531b513b75a4d4bfd88</Sha>
1010
</Dependency>
1111
<Dependency Name="Microsoft.Diagnostics.Runtime.Utilities" Version="2.0.325901">
1212
<Uri>https://github.com/microsoft/clrmd</Uri>

eng/Versions.props

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
<MicrosoftWin32PrimitivesVersion>4.3.0</MicrosoftWin32PrimitivesVersion>
4545
<!-- Other libs -->
4646
<MicrosoftBclAsyncInterfacesVersion>1.1.0</MicrosoftBclAsyncInterfacesVersion>
47-
<MicrosoftDiagnosticsRuntimeVersion>2.0.325901</MicrosoftDiagnosticsRuntimeVersion>
48-
<MicrosoftDiagnosticsRuntimeUtilitiesVersion>2.0.325901</MicrosoftDiagnosticsRuntimeUtilitiesVersion>
47+
<MicrosoftDiagnosticsRuntimeVersion>2.1.330601</MicrosoftDiagnosticsRuntimeVersion>
4948
<MicrosoftDiaSymReaderNativePackageVersion>16.9.0-beta1.21055.5</MicrosoftDiaSymReaderNativePackageVersion>
5049
<MicrosoftDiagnosticsTracingTraceEventVersion>2.0.64</MicrosoftDiagnosticsTracingTraceEventVersion>
5150
<MicrosoftExtensionsLoggingVersion>2.1.1</MicrosoftExtensionsLoggingVersion>
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Diagnostics.Runtime;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Collections.Immutable;
9+
using System.Diagnostics;
10+
using System.Linq;
11+
using System.Runtime.CompilerServices;
12+
using System.Runtime.InteropServices;
13+
14+
namespace Microsoft.Diagnostics.DebugServices.Implementation
15+
{
16+
/// <summary>
17+
/// ClrMD runtime service implementation
18+
/// </summary>
19+
internal class DataReader : IDataReader
20+
{
21+
private readonly ITarget _target;
22+
private IEnumerable<ModuleInfo> _modules;
23+
private IModuleService _moduleService;
24+
private IThreadService _threadService;
25+
private IMemoryService _memoryService;
26+
27+
public DataReader(ITarget target)
28+
{
29+
_target = target;
30+
target.OnFlushEvent.Register(() => _modules = null);
31+
}
32+
33+
#region IDataReader
34+
35+
string IDataReader.DisplayName => "";
36+
37+
bool IDataReader.IsThreadSafe => false;
38+
39+
OSPlatform IDataReader.TargetPlatform => _target.OperatingSystem;
40+
41+
Architecture IDataReader.Architecture => _target.Architecture;
42+
43+
int IDataReader.ProcessId => unchecked((int)_target.ProcessId.GetValueOrDefault());
44+
45+
IEnumerable<ModuleInfo> IDataReader.EnumerateModules() => _modules ??= ModuleService.EnumerateModules().Select((module) => new DataReaderModule(module)).ToList();
46+
47+
bool IDataReader.GetThreadContext(uint threadId, uint contextFlags, Span<byte> context)
48+
{
49+
try
50+
{
51+
byte[] registerContext = ThreadService.GetThreadFromId(threadId).GetThreadContext();
52+
context = new Span<byte>(registerContext);
53+
return true;
54+
}
55+
catch (DiagnosticsException ex)
56+
{
57+
Trace.TraceError($"GetThreadContext: {threadId} exception {ex.Message}");
58+
}
59+
return false;
60+
}
61+
62+
void IDataReader.FlushCachedData()
63+
{
64+
}
65+
66+
#endregion
67+
68+
#region IMemoryReader
69+
70+
int IMemoryReader.PointerSize => MemoryService.PointerSize;
71+
72+
int IMemoryReader.Read(ulong address, Span<byte> buffer)
73+
{
74+
MemoryService.ReadMemory(address, buffer, out int bytesRead);
75+
return bytesRead;
76+
}
77+
78+
bool IMemoryReader.Read<T>(ulong address, out T value)
79+
{
80+
Span<byte> buffer = stackalloc byte[Marshal.SizeOf<T>()];
81+
if (((IMemoryReader)this).Read(address, buffer) == buffer.Length)
82+
{
83+
value = Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(buffer));
84+
return true;
85+
}
86+
value = default;
87+
return false;
88+
}
89+
90+
T IMemoryReader.Read<T>(ulong address)
91+
{
92+
((IMemoryReader)this).Read(address, out T result);
93+
return result;
94+
}
95+
96+
bool IMemoryReader.ReadPointer(ulong address, out ulong value)
97+
{
98+
return MemoryService.ReadPointer(address, out value);
99+
}
100+
101+
ulong IMemoryReader.ReadPointer(ulong address)
102+
{
103+
MemoryService.ReadPointer(address, out ulong value);
104+
return value;
105+
}
106+
107+
#endregion
108+
109+
private IModuleService ModuleService => _moduleService ??= _target.Services.GetService<IModuleService>();
110+
111+
private IMemoryService MemoryService => _memoryService ??= _target.Services.GetService<IMemoryService>();
112+
113+
private IThreadService ThreadService => _threadService ??= _target.Services.GetService<IThreadService>();
114+
115+
private class DataReaderModule : ModuleInfo
116+
{
117+
private readonly IModule _module;
118+
119+
public DataReaderModule(IModule module)
120+
: base(module.ImageBase, module.FileName)
121+
{
122+
_module = module;
123+
}
124+
125+
public override long ImageSize => unchecked((long)_module.ImageSize);
126+
127+
public override int IndexFileSize => unchecked((int)_module.IndexFileSize.GetValueOrDefault(0));
128+
129+
public override int IndexTimeStamp => unchecked((int)_module.IndexTimeStamp.GetValueOrDefault(0));
130+
131+
public override Version Version
132+
{
133+
get
134+
{
135+
try
136+
{
137+
return _module.GetVersionData() ?? Utilities.EmptyVersion;
138+
}
139+
catch (DiagnosticsException ex)
140+
{
141+
Trace.TraceError($"ModuleInfo.Version: {_module.ImageBase:X16} exception {ex.Message}");
142+
}
143+
return Utilities.EmptyVersion;
144+
}
145+
}
146+
147+
public override ImmutableArray<byte> BuildId
148+
{
149+
get
150+
{
151+
try
152+
{
153+
return _module.BuildId;
154+
}
155+
catch (DiagnosticsException ex)
156+
{
157+
Trace.TraceError($"ModuleInfo.BuildId: {_module.ImageBase:X16} exception {ex.Message}");
158+
}
159+
return ImmutableArray<byte>.Empty;
160+
}
161+
}
162+
163+
public override PdbInfo Pdb
164+
{
165+
get
166+
{
167+
try
168+
{
169+
PdbFileInfo pdbFileInfo = _module.GetPdbFileInfos().Where((pdbFileInfo) => pdbFileInfo.IsPortable).LastOrDefault();
170+
if (pdbFileInfo is null)
171+
{
172+
pdbFileInfo = _module.GetPdbFileInfos().LastOrDefault();
173+
if (pdbFileInfo is null)
174+
{
175+
return default;
176+
}
177+
}
178+
return new PdbInfo(pdbFileInfo.Path, pdbFileInfo.Guid, pdbFileInfo.Revision);
179+
}
180+
catch (DiagnosticsException ex)
181+
{
182+
Trace.TraceError($"ModuleInfo.Pdb: {_module.ImageBase:X16} exception {ex.Message}");
183+
}
184+
return default;
185+
}
186+
}
187+
188+
public override bool IsManaged => _module.IsManaged;
189+
190+
public override ulong GetExportSymbolAddress(string symbol)
191+
{
192+
var exportSymbols = _module.Services.GetService<IExportSymbols>();
193+
if (exportSymbols is not null)
194+
{
195+
if (exportSymbols.TryGetSymbolAddress(symbol, out ulong offset))
196+
{
197+
return offset;
198+
}
199+
}
200+
return 0;
201+
}
202+
203+
public override IResourceNode ResourceRoot => base.ResourceRoot;
204+
}
205+
}
206+
}

src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using Microsoft.Diagnostics.Runtime.Utilities;
65
using Microsoft.FileFormats;
7-
using Microsoft.FileFormats.ELF;
8-
using Microsoft.FileFormats.MachO;
96
using System;
107
using System.Collections.Generic;
118
using System.Diagnostics;
129
using System.IO;
13-
using System.Linq;
1410
using System.Reflection.Metadata;
1511
using System.Reflection.PortableExecutable;
1612

src/Microsoft.Diagnostics.DebugServices.Implementation/MetadataMappingMemoryService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.Diagnostics.Runtime;
6-
using Microsoft.Diagnostics.Runtime.Utilities;
76
using Microsoft.FileFormats;
87
using Microsoft.FileFormats.PE;
98
using System;

src/Microsoft.Diagnostics.DebugServices.Implementation/Module.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using Microsoft.Diagnostics.Runtime.Utilities;
5+
using Microsoft.Diagnostics.Runtime;
66
using Microsoft.FileFormats;
77
using Microsoft.FileFormats.ELF;
88
using Microsoft.FileFormats.MachO;
@@ -197,7 +197,7 @@ public string GetSymbolFileName()
197197
{
198198
if (InitializeValue(Flags.InitializeSymbolFileName))
199199
{
200-
if (Target.OperatingSystem == OSPlatform.Linux)
200+
if (ImageSize > 0 && Target.OperatingSystem == OSPlatform.Linux)
201201
{
202202
try
203203
{
@@ -216,8 +216,8 @@ public string GetSymbolFileName()
216216
(ex is InvalidVirtualAddressException ||
217217
ex is ArgumentOutOfRangeException ||
218218
ex is IndexOutOfRangeException ||
219+
ex is OverflowException ||
219220
ex is BadInputFormatException)
220-
221221
{
222222
Trace.TraceWarning("ELF .gnu_debuglink section in {0}: {1}", this, ex.Message);
223223
}
@@ -226,7 +226,7 @@ ex is IndexOutOfRangeException ||
226226
return _symbolFileName;
227227
}
228228

229-
public abstract VersionData GetVersionData();
229+
public abstract Version GetVersionData();
230230

231231
public abstract string GetVersionString();
232232

@@ -254,24 +254,15 @@ bool IExportSymbols.TryGetSymbolAddress(string name, out ulong address)
254254
}
255255
else if (Target.OperatingSystem == OSPlatform.Linux)
256256
{
257-
try
257+
if (ImageSize > 0)
258258
{
259-
Stream stream = ModuleService.MemoryService.CreateMemoryStream(ImageBase, ImageSize);
260-
ElfFile elfFile = new(stream, position: ImageBase, leaveOpen: false, isVirtual: true);
261-
if (elfFile.Header.IsValid)
259+
ModuleInfo module = ModuleInfo.TryCreate(Target.Services.GetService<DataReader>(), ImageBase, FileName);
260+
if (module is not null)
262261
{
263-
if (elfFile.TryGetExportSymbol(name, out ulong offset))
264-
{
265-
address = ImageBase + offset;
266-
return true;
267-
}
268-
address = 0;
269-
return false;
262+
address = module.GetExportSymbolAddress(name);
263+
return address != 0;
270264
}
271265
}
272-
catch (InvalidDataException)
273-
{
274-
}
275266
}
276267
return TryGetSymbolAddressInner(name, out address);
277268
}
@@ -284,9 +275,9 @@ protected virtual bool TryGetSymbolAddressInner(string name, out ulong address)
284275

285276
#endregion
286277

287-
protected VersionData GetVersion()
278+
protected Version GetVersionInner()
288279
{
289-
VersionData versionData = null;
280+
Version version = null;
290281

291282
PEFile peFile = GetPEInfo();
292283
if (peFile != null)
@@ -296,7 +287,7 @@ protected VersionData GetVersion()
296287
VsFixedFileInfo fileInfo = peFile.VersionInfo;
297288
if (fileInfo != null)
298289
{
299-
versionData = fileInfo.ToVersionData();
290+
version = fileInfo.ToVersion();
300291
}
301292
}
302293
catch (Exception ex) when (ex is InvalidVirtualAddressException || ex is BadInputFormatException)
@@ -325,8 +316,7 @@ protected VersionData GetVersion()
325316
string versionToParse = versionString.Substring(0, spaceIndex);
326317
try
327318
{
328-
Version version = System.Version.Parse(versionToParse);
329-
versionData = new VersionData(version.Major, version.Minor, version.Build, version.Revision);
319+
version = Version.Parse(versionToParse);
330320
}
331321
catch (ArgumentException ex)
332322
{
@@ -336,7 +326,7 @@ protected VersionData GetVersion()
336326
}
337327
}
338328

339-
return versionData;
329+
return version;
340330
}
341331

342332
protected PEFile GetPEInfo()

src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ internal PEFile GetPEInfo(ulong address, ulong size, out IEnumerable<PdbFileInfo
212212
moduleFlags &= ~(Module.Flags.IsPEImage | Module.Flags.IsManaged | Module.Flags.IsLoadedLayout | Module.Flags.IsFileLayout);
213213

214214
// None of the modules that lldb (on either Linux/MacOS) provides are PEs
215-
if (Target.Host.HostType != HostType.Lldb)
215+
if (size > 0 && Target.Host.HostType != HostType.Lldb)
216216
{
217217
// First try getting the PE info as loaded layout (native Windows DLLs and most managed PEs).
218218
peFile = GetPEInfo(isVirtual: true, address, size, out List<PdbFileInfo> pdbs, out Module.Flags flags);
@@ -367,7 +367,7 @@ protected string GetVersionString(IModule module)
367367
}
368368
else
369369
{
370-
Trace.TraceError($"GetVersionString: unsupported module {module} or platform {Target.OperatingSystem}");
370+
Trace.TraceError($"GetVersionString: unsupported module {module} on platform {Target.OperatingSystem}");
371371
}
372372
}
373373
}

0 commit comments

Comments
 (0)