Skip to content

Commit 0b04f47

Browse files
author
Fred
committed
Instance details module: display the fieldname referencing the instance
Closes #86
1 parent 2d44ba6 commit 0b04f47

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

MemoScope/Core/ClrDump.cs

+54-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ public string GetObjectTypeName(ulong address)
251251

252252
public ClrType GetObjectType(ulong address)
253253
{
254-
var clrType = worker.Eval(() => Heap.GetObjectType(address));
254+
var clrType = worker.Eval(() => GetObjectTypeImpl(address));
255+
return clrType;
256+
}
257+
258+
public ClrType GetObjectTypeImpl(ulong address)
259+
{
260+
var clrType = Heap.GetObjectType(address);
255261
return clrType;
256262
}
257263

@@ -298,6 +304,53 @@ public ClrMethod GetMethodByHandle(ulong methodDescriptorPtr)
298304
var meth = Runtime.GetMethodByHandle(methodDescriptorPtr);
299305
return meth;
300306
}
307+
308+
// Find the field in instance at address that references refAddress
309+
public string GetFieldNameReference(ulong refAddress, ulong address)
310+
{
311+
return Eval(() => GetFieldNameReferenceImpl(refAddress, address));
312+
}
313+
public string GetFieldNameReferenceImpl(ulong refAddress, ulong address)
314+
{
315+
ClrType type = GetObjectTypeImpl(address);
316+
if( type == null)
317+
{
318+
return "Unknown";
319+
}
320+
ClrObject obj = new ClrObject(address, type);
321+
if( type.IsArray)
322+
{
323+
var length = type.GetArrayLength(address);
324+
for (int i=0; i < length; i++ )
325+
{
326+
if( obj[i].Address == refAddress)
327+
{
328+
return $"[ {i} ]";
329+
}
330+
}
331+
return "[ ? ]";
332+
}
333+
foreach (var field in type.Fields)
334+
{
335+
switch (field.ElementType)
336+
{
337+
case ClrElementType.Struct:
338+
case ClrElementType.String:
339+
case ClrElementType.Array:
340+
case ClrElementType.SZArray:
341+
case ClrElementType.Object:
342+
var fieldValue = obj[field];
343+
if ( fieldValue.Address == refAddress)
344+
{
345+
return field.Name;
346+
}
347+
break;
348+
}
349+
}
350+
return "Toto";
351+
}
352+
353+
301354
}
302355

303356
public class ThreadProperty

MemoScope/Modules/InstanceDetails/ReferenceInformation.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@ public class ReferenceInformation : ITreeNodeInformation<ReferenceInformation>,
1515
[OLVColumn(Title = "Address")]
1616
public ulong Address { get; }
1717

18+
[OLVColumn]
19+
public string FieldName { get; }
20+
1821
[OLVColumn(Title = "Type")]
1922
public string TypeName => ClrDump.GetObjectTypeName(Address);
2023

2124
public ClrType ClrType => ClrDump.GetObjectType(Address);
2225

23-
public ReferenceInformation(ClrDump dump, ulong address)
26+
public ReferenceInformation(ClrDump clrDump, ulong address, ulong refAddress) : this(clrDump, address)
2427
{
25-
ClrDump = dump;
26-
Address = address;
28+
FieldName = ClrDump.GetFieldNameReference(refAddress, address);
2729
}
2830

31+
public ReferenceInformation(ClrDump clrDump, ulong address)
32+
{
33+
ClrDump = clrDump;
34+
Address = address;
35+
}
36+
2937
public bool CanExpand => ClrDump.HasReferences(Address);
30-
public List<ReferenceInformation> Children => ClrDump.GetReferences(Address).Select(address => new ReferenceInformation(ClrDump, address)).ToList();
38+
public List<ReferenceInformation> Children => ClrDump.GetReferences(Address).Select(address => new ReferenceInformation(ClrDump, address, Address)).ToList();
3139
}
3240
}

0 commit comments

Comments
 (0)