Skip to content

Commit

Permalink
Improve dumping GC info in R2RDump (#44857)
Browse files Browse the repository at this point in the history
The --raw command-line option used to dump bytes prefixed with their RVAs for all structures except GC blobs, which were dumped with file offsets instead. This change fixes that inconsistency. It also fixes the size reported by x86.GcInfo, which determines how many bytes to dump. In addition I simplified ReadyToRunMethod's code to store just the GC blob's RVA and avoid the delegate allocation.
  • Loading branch information
AntonLapounov authored Nov 18, 2020
1 parent 7e3b310 commit 514079d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ public abstract class BaseGcInfo
/// <summary>
/// A runtime function corresponds to a contiguous fragment of code that implements a method.
/// </summary>
/// <remarks>
/// Based on <a href="https://github.com/dotnet/runtime/blob/master/src/coreclr/src/pal/inc/pal.h">src/pal/inc/pal.h</a> _RUNTIME_FUNCTION
/// </remarks>
public class RuntimeFunction
{
// based on <a href= "https://github.com/dotnet/runtime/blob/master/src/coreclr/src/pal/inc/pal.h" > src / pal / inc / pal.h </ a > _RUNTIME_FUNCTION
private ReadyToRunReader _readyToRunReader;
private EHInfo _ehInfo;
private DebugInfo _debugInfo;
Expand Down Expand Up @@ -184,8 +186,7 @@ public RuntimeFunction(
int unwindRva,
int codeOffset,
ReadyToRunMethod method,
BaseUnwindInfo unwindInfo,
Func<BaseGcInfo> gcInfo)
BaseUnwindInfo unwindInfo)
{
_readyToRunReader = readyToRunReader;

Expand All @@ -196,7 +197,6 @@ public RuntimeFunction(
Method = method;
UnwindInfo = unwindInfo;
CodeOffset = codeOffset;
method.GetGcInfo = gcInfo;
}

private void EnsureInitialized()
Expand All @@ -213,17 +213,17 @@ private int GetSize()
{
return EndAddress - StartAddress;
}
else if (UnwindInfo is x86.UnwindInfo)
else if (UnwindInfo is x86.UnwindInfo x86Info)
{
return (int)((x86.UnwindInfo)UnwindInfo).FunctionLength;
return (int)x86Info.FunctionLength;
}
else if (UnwindInfo is Arm.UnwindInfo)
else if (UnwindInfo is Arm.UnwindInfo armInfo)
{
return (int)((Arm.UnwindInfo)UnwindInfo).FunctionLength;
return (int)armInfo.FunctionLength;
}
else if (UnwindInfo is Arm64.UnwindInfo)
else if (UnwindInfo is Arm64.UnwindInfo arm64Info)
{
return (int)((Arm64.UnwindInfo)UnwindInfo).FunctionLength;
return (int)arm64Info.FunctionLength;
}
else if (Method.GcInfo != null)
{
Expand Down Expand Up @@ -300,7 +300,7 @@ private void EnsureRuntimeFunctions()
/// </summary>
public int EntryPointRuntimeFunctionId { get; set; }

public Func<BaseGcInfo> GetGcInfo { get; set; }
public int GcInfoRva { get; set; }

public BaseGcInfo GcInfo
{
Expand Down Expand Up @@ -445,9 +445,18 @@ public ReadyToRunMethod(

private void EnsureInitialized()
{
if (_gcInfo == null && GetGcInfo != null)
if (_gcInfo == null && GcInfoRva != 0)
{
_gcInfo = GetGcInfo();
int gcInfoOffset = _readyToRunReader.CompositeReader.GetOffset(GcInfoRva);
if (_readyToRunReader.Machine == Machine.I386)
{
_gcInfo = new x86.GcInfo(_readyToRunReader.Image, gcInfoOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion);
}
else
{
// Arm and Arm64 use the same GcInfo format as Amd64
_gcInfo = new Amd64.GcInfo(_readyToRunReader.Image, gcInfoOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion);
}
}
}

Expand Down Expand Up @@ -507,8 +516,8 @@ private void ParseRuntimeFunctions()
int runtimeFunctionSize = _readyToRunReader.CalculateRuntimeFunctionSize();
int runtimeFunctionOffset = _readyToRunReader.CompositeReader.GetOffset(_readyToRunReader.ReadyToRunHeader.Sections[ReadyToRunSectionType.RuntimeFunctions].RelativeVirtualAddress);
int curOffset = runtimeFunctionOffset + runtimeFunctionId * runtimeFunctionSize;
Func<BaseGcInfo> gcInfo = default(Func<BaseGcInfo>);
int codeOffset = 0;

for (int i = 0; i < RuntimeFunctionCount; i++)
{
int startRva = NativeReader.ReadInt32(_readyToRunReader.Image, ref curOffset);
Expand All @@ -527,36 +536,32 @@ private void ParseRuntimeFunctions()
int unwindOffset = _readyToRunReader.CompositeReader.GetOffset(unwindRva);

BaseUnwindInfo unwindInfo = null;
if (_readyToRunReader.Machine == Machine.Amd64)
if (_readyToRunReader.Machine == Machine.I386)
{
unwindInfo = new Amd64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
if (i == 0)
{
gcInfo = new Func<BaseGcInfo>(() => new Amd64.GcInfo(_readyToRunReader.Image, unwindOffset + unwindInfo.Size, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion));
}
unwindInfo = new x86.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.I386)
else if (_readyToRunReader.Machine == Machine.Amd64)
{
unwindInfo = new x86.UnwindInfo(_readyToRunReader.Image, unwindOffset);
if (i == 0)
{
gcInfo = new Func<BaseGcInfo>(() => new x86.GcInfo(_readyToRunReader.Image, unwindOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion));
}
unwindInfo = new Amd64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.ArmThumb2)
{
unwindInfo = new Arm.UnwindInfo(_readyToRunReader.Image, unwindOffset);
if (i == 0)
{
gcInfo = new Func<BaseGcInfo>(() => new Amd64.GcInfo(_readyToRunReader.Image, unwindOffset + unwindInfo.Size, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion)); // Arm and Arm64 use the same GcInfo format as x6
}
}
else if (_readyToRunReader.Machine == Machine.Arm64)
{
unwindInfo = new Arm64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
if (i == 0)
}

if (i == 0 && unwindInfo != null)
{
if (_readyToRunReader.Machine == Machine.I386)
{
GcInfoRva = unwindRva;
}
else
{
gcInfo = new Func<BaseGcInfo>(() => new Amd64.GcInfo(_readyToRunReader.Image, unwindOffset + unwindInfo.Size, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion));
GcInfoRva = unwindRva + unwindInfo.Size;
}
}

Expand All @@ -568,8 +573,7 @@ private void ParseRuntimeFunctions()
unwindRva,
codeOffset,
this,
unwindInfo,
gcInfo);
unwindInfo);

_runtimeFunctions.Add(rtf);
runtimeFunctionId++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public UnwindInfo() { }

public UnwindInfo(byte[] image, int offset)
{
int startOffset = offset;
FunctionLength = NativeReader.DecodeUnsignedGc(image, ref offset);
Size = sizeof(int);
Size = offset - startOffset;
}

public override string ToString()
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/src/tools/r2rdump/TextDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ internal override void DumpMethod(ReadyToRunMethod method)

if (_options.Raw)
{
// TODO: Output RVAs for consistency with other DumpBytes calls
DumpBytes(gcInfo.Offset, (uint)gcInfo.Size, "", false);
DumpBytes(method.GcInfoRva, (uint)gcInfo.Size);
}
}
SkipLine();
Expand Down

0 comments on commit 514079d

Please sign in to comment.