Skip to content
Closed
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
7 changes: 4 additions & 3 deletions src/BenchmarkDotNet/Disassemblers/Arm64Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Gee.External.Capstone;
using Gee.External.Capstone.Arm64;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;

namespace BenchmarkDotNet.Disassemblers
{
Expand All @@ -19,9 +20,9 @@ private enum State
private long _value;
private int _expectedMovkShift;
private Arm64RegisterId _registerId;
private ClrRuntime _runtime;
private IClrRuntime _runtime;

public void Init(ClrRuntime runtime)
public void Init(IClrRuntime runtime)
{
_state = State.LookingForPattern;
_expectedMovkShift = 0;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void Feed(Arm64Instruction instruction)

internal class Arm64Disassembler : ClrMdDisassembler
{
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, ClrMethod currentMethod, DisassemblySyntax syntax)
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, IClrMethod currentMethod, DisassemblySyntax syntax)
{
const Arm64DisassembleMode disassembleMode = Arm64DisassembleMode.Arm;
using (CapstoneArm64Disassembler disassembler = CapstoneDisassembler.CreateArm64Disassembler(disassembleMode))
Expand Down
13 changes: 7 additions & 6 deletions src/BenchmarkDotNet/Disassemblers/ClrMdDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BenchmarkDotNet.Filters;
using BenchmarkDotNet.Portability;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;
using System.Text.RegularExpressions;

namespace BenchmarkDotNet.Disassemblers
Expand Down Expand Up @@ -157,7 +158,7 @@ private DisassembledMethod[] Disassemble(ClrMdArgs args, State state)
return result.ToArray();
}

private static bool CanBeDisassembled(ClrMethod method) => method.ILOffsetMap.Length > 0 && method.NativeCode > 0;
private static bool CanBeDisassembled(IClrMethod method) => method.ILOffsetMap.Length > 0 && method.NativeCode > 0;

private DisassembledMethod DisassembleMethod(MethodInfo methodInfo, State state, ClrMdArgs args, DisassemblySyntax syntax, SourceCodeProvider sourceCodeProvider)
{
Expand Down Expand Up @@ -206,7 +207,7 @@ private DisassembledMethod DisassembleMethod(MethodInfo methodInfo, State state,
};
}

private IEnumerable<Asm> Decode(ILToNativeMap map, State state, int depth, ClrMethod currentMethod, DisassemblySyntax syntax)
private IEnumerable<Asm> Decode(ILToNativeMap map, State state, int depth, IClrMethod currentMethod, DisassemblySyntax syntax)
{
ulong startAddress = map.StartAddress;
uint size = (uint)(map.EndAddress - map.StartAddress);
Expand All @@ -227,9 +228,9 @@ private IEnumerable<Asm> Decode(ILToNativeMap map, State state, int depth, ClrMe
return Decode(code, startAddress, state, depth, currentMethod, syntax);
}

protected abstract IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, ClrMethod currentMethod, DisassemblySyntax syntax);
protected abstract IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, IClrMethod currentMethod, DisassemblySyntax syntax);

private static ILToNativeMap[] GetCompleteNativeMap(ClrMethod method, ClrRuntime runtime)
private static ILToNativeMap[] GetCompleteNativeMap(IClrMethod method, IClrRuntime runtime)
{
// it's better to use one single map rather than few small ones
// it's simply easier to get next instruction when decoding ;)
Expand All @@ -251,10 +252,10 @@ private static ILToNativeMap[] GetCompleteNativeMap(ClrMethod method, ClrRuntime
.ToArray();
}

private static DisassembledMethod CreateEmpty(ClrMethod method, string reason)
private static DisassembledMethod CreateEmpty(IClrMethod method, string reason)
=> DisassembledMethod.Empty(method.Signature ?? "", method.NativeCode, reason);

protected void TryTranslateAddressToName(ulong address, bool isAddressPrecodeMD, State state, int depth, ClrMethod currentMethod)
protected void TryTranslateAddressToName(ulong address, bool isAddressPrecodeMD, State state, int depth, IClrMethod currentMethod)
{
if (!IsValidAddress(address) || state.AddressToNameMapping.ContainsKey(address))
return;
Expand Down
20 changes: 10 additions & 10 deletions src/BenchmarkDotNet/Disassemblers/DataContracts.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Gee.External.Capstone;
using Gee.External.Capstone.Arm64;
using Iced.Intel;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -218,18 +218,18 @@ public static class DisassemblerConstants

internal sealed class State
{
internal State(ClrRuntime runtime, string targetFrameworkMoniker)
internal State(IClrRuntime runtime, string targetFrameworkMoniker)
{
Runtime = runtime;
Todo = new Queue<MethodInfo>();
HandledMethods = new HashSet<ClrMethod>(new ClrMethodComparer());
HandledMethods = new HashSet<IClrMethod>(new IClrMethodComparer());
AddressToNameMapping = [];
RuntimeVersion = ParseVersion(targetFrameworkMoniker);
}

internal ClrRuntime Runtime { get; }
internal IClrRuntime Runtime { get; }
internal Queue<MethodInfo> Todo { get; }
internal HashSet<ClrMethod> HandledMethods { get; }
internal HashSet<IClrMethod> HandledMethods { get; }
internal Dictionary<ulong, string> AddressToNameMapping { get; }
internal Version RuntimeVersion { get; }

Expand Down Expand Up @@ -258,9 +258,9 @@ internal static Version ParseVersion(string targetFrameworkMoniker)
return Version.Parse(versionToParse);
}

private sealed class ClrMethodComparer : IEqualityComparer<ClrMethod>
private sealed class IClrMethodComparer : IEqualityComparer<IClrMethod>
{
public bool Equals(ClrMethod? x, ClrMethod? y)
public bool Equals(IClrMethod? x, IClrMethod? y)
{
if (ReferenceEquals(x, y))
return true;
Expand All @@ -271,16 +271,16 @@ public bool Equals(ClrMethod? x, ClrMethod? y)
return x.NativeCode == y.NativeCode;
}

public int GetHashCode(ClrMethod obj) => (int)obj.NativeCode;
public int GetHashCode(IClrMethod obj) => (int)obj.NativeCode;
}
}

internal readonly struct MethodInfo // I am not using ValueTuple here (would be perfect) to keep the number of dependencies as low as possible
{
internal ClrMethod Method { get; }
internal IClrMethod Method { get; }
internal int Depth { get; }

internal MethodInfo(ClrMethod method, int depth)
internal MethodInfo(IClrMethod method, int depth)
{
Method = method;
Depth = depth;
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Disassemblers/IntelDisassembler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using BenchmarkDotNet.Diagnosers;
using Iced.Intel;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;

namespace BenchmarkDotNet.Disassemblers
{
internal class IntelDisassembler : ClrMdDisassembler
{
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, ClrMethod currentMethod, DisassemblySyntax syntax)
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, IClrMethod currentMethod, DisassemblySyntax syntax)
{
var reader = new ByteArrayCodeReader(code);
var decoder = Decoder.Create(state.Runtime.DataTarget.DataReader.PointerSize * 8, reader);
Expand Down
9 changes: 5 additions & 4 deletions src/BenchmarkDotNet/Disassemblers/SourceCodeProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BenchmarkDotNet.Extensions;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;
using Microsoft.Diagnostics.Symbols;
using System.Diagnostics;

Expand All @@ -17,7 +18,7 @@ public void Dispose()
symbolReader.Dispose();
}

internal IEnumerable<Sharp> GetSource(ClrMethod method, ILToNativeMap map)
internal IEnumerable<Sharp> GetSource(IClrMethod method, ILToNativeMap map)
{
var sourceLocation = GetSourceLocation(method, map.ILOffset);
if (sourceLocation is not { LineNumber: > 0 })
Expand Down Expand Up @@ -105,7 +106,7 @@ private static string GetSmartPointer(string sourceLine, int? start, int? end)
return new string(prefix);
}

internal SourceLocation? GetSourceLocation(ClrMethod method, int ilOffset)
internal SourceLocation? GetSourceLocation(IClrMethod method, int ilOffset)
{
var reader = GetReaderForMethod(method);
if (reader == null)
Expand All @@ -114,9 +115,9 @@ private static string GetSmartPointer(string sourceLine, int? start, int? end)
return reader.SourceLocationForManagedCode((uint)method.MetadataToken, ilOffset);
}

private ManagedSymbolModule? GetReaderForMethod(ClrMethod? method)
private ManagedSymbolModule? GetReaderForMethod(IClrMethod? method)
{
ClrModule? module = method?.Type?.Module;
IClrModule? module = method?.Type?.Module;
PdbInfo? info = module?.Pdb;

ManagedSymbolModule? reader = null;
Expand Down