Skip to content

[cDAC] Implement ISOSDacInterface::GetFrameName #113769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2025
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
8 changes: 8 additions & 0 deletions docs/design/datacontracts/StackWalk.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ IEnumerable<IStackDataFrameHandle> CreateStackWalk(ThreadData threadData);
byte[] GetRawContext(IStackDataFrameHandle stackDataFrameHandle);
// Gets the Frame address at the given stack dataframe. Returns TargetPointer.Null if the current dataframe does not have a valid Frame.
TargetPointer GetFrameAddress(IStackDataFrameHandle stackDataFrameHandle);

// Gets the Frame name associated with the given Frame identifier. If no matching Frame name found returns an empty string.
string GetFrameName(TargetPointer frameIdentifier);
```

## Version 1
Expand Down Expand Up @@ -323,3 +326,8 @@ If the Frame is not valid, returns `TargetPointer.Null`.
TargetPointer GetFrameAddress(IStackDataFrameHandle stackDataFrameHandle);
```


`GetFrameName` gets the name associated with a FrameIdentifier (pointer sized value) from the Globals stored in the contract descriptor. If no associated Frame name is found, it returns an empty string.
```csharp
string GetFrameName(TargetPointer frameIdentifier);
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IStackWalk : IContract
public virtual IEnumerable<IStackDataFrameHandle> CreateStackWalk(ThreadData threadData) => throw new NotImplementedException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think interface members are public by default aren't they? So you can leave off the 'public', I think.

I'm also not sure whether you need the 'virtual' since these appear to be DIMs, I know interface methods are usually virtual but providing a body might make them nonvirtuals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are public by default. My understanding is that the virtual makes a small difference in allowing inherited classes to override the functions, as they are virtual, but this can also be marked in the implementing class/struct.

I took a look, and we are inconsistent across the cDAC contracts. I'll remove public virtual from these and work towards unifying on that.

public virtual byte[] GetRawContext(IStackDataFrameHandle stackDataFrameHandle) => throw new NotImplementedException();
public virtual TargetPointer GetFrameAddress(IStackDataFrameHandle stackDataFrameHandle) => throw new NotImplementedException();
public virtual string GetFrameName(TargetPointer frameIdentifier) => throw new NotImplementedException();
}

public struct StackWalk : IStackWalk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public bool Next()

public void UpdateContextFromFrame(IPlatformAgnosticContext context)
{
switch (GetFrameType(CurrentFrame))
switch (GetFrameType(target, CurrentFrame.Identifier))
{
case FrameType.InlinedCallFrame:
Data.InlinedCallFrame inlinedCallFrame = target.ProcessedData.GetOrAdd<Data.InlinedCallFrame>(CurrentFrame.Address);
Expand Down Expand Up @@ -122,24 +122,34 @@ public void UpdateContextFromFrame(IPlatformAgnosticContext context)

public bool IsInlineCallFrameWithActiveCall()
{
if (GetFrameType(CurrentFrame) != FrameType.InlinedCallFrame)
if (GetFrameType(target, CurrentFrame.Identifier) != FrameType.InlinedCallFrame)
{
return false;
}
Data.InlinedCallFrame inlinedCallFrame = target.ProcessedData.GetOrAdd<Data.InlinedCallFrame>(currentFramePointer);
return inlinedCallFrame.CallerReturnAddress != 0;
}

private FrameType GetFrameType(Data.Frame frame)
public static string GetFrameName(Target target, TargetPointer frameIdentifier)
{
FrameType frameType = GetFrameType(target, frameIdentifier);
if (frameType == FrameType.Unknown)
{
return string.Empty;
}
return frameType.ToString();
}

private static FrameType GetFrameType(Target target, TargetPointer frameIdentifier)
{
foreach (FrameType frameType in Enum.GetValues<FrameType>())
{
TargetPointer typeVptr;
TargetPointer foundFrameIdentifier;
try
{
// not all Frames are in all builds, so we need to catch the exception
typeVptr = target.ReadGlobalPointer(frameType.ToString() + "Identifier");
if (frame.Identifier == typeVptr)
foundFrameIdentifier = target.ReadGlobalPointer(frameType.ToString() + "Identifier");
if (frameIdentifier == foundFrameIdentifier)
{
return frameType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ TargetPointer IStackWalk.GetFrameAddress(IStackDataFrameHandle stackDataFrameHan
return TargetPointer.Null;
}

string IStackWalk.GetFrameName(TargetPointer frameIdentifier)
=> FrameIterator.GetFrameName(_target, frameIdentifier);

private bool IsManaged(TargetPointer ip, [NotNullWhen(true)] out CodeBlockHandle? codeBlockHandle)
{
IExecutionManager eman = _target.Contracts.ExecutionManager;
Expand Down
49 changes: 48 additions & 1 deletion src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,54 @@ int ISOSDacInterface.GetFailedAssemblyLocation(ulong assesmbly, uint count, char
int ISOSDacInterface.GetFieldDescData(ulong fieldDesc, void* data)
=> _legacyImpl is not null ? _legacyImpl.GetFieldDescData(fieldDesc, data) : HResults.E_NOTIMPL;
int ISOSDacInterface.GetFrameName(ulong vtable, uint count, char* frameName, uint* pNeeded)
=> _legacyImpl is not null ? _legacyImpl.GetFrameName(vtable, count, frameName, pNeeded) : HResults.E_NOTIMPL;
{
if (vtable == 0)
{
return HResults.E_INVALIDARG;
}

int hr = HResults.S_OK;
try
{
IStackWalk stackWalk = _target.Contracts.StackWalk;
string name = stackWalk.GetFrameName(new(vtable));

if (string.IsNullOrEmpty(name))
{
hr = HResults.E_INVALIDARG;
}
else
{
OutputBufferHelpers.CopyStringToBuffer(frameName, count, pNeeded, name);
}
}
catch (System.Exception ex)
{
hr = ex.HResult;
}

#if DEBUG
if (_legacyImpl is not null)
{
char[] nameLocal = new char[count];
uint neededLocal;
int hrLocal;
fixed (char* ptr = nameLocal)
{
hrLocal = _legacyImpl.GetFrameName(vtable, count, ptr, &neededLocal);
}
Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
if (hr == HResults.S_OK)
{
Debug.Assert(pNeeded == null || *pNeeded == neededLocal);
Debug.Assert(frameName == null || new ReadOnlySpan<char>(nameLocal, 0, (int)neededLocal).SequenceEqual(new string(frameName)),
$"cDAC: {new string(frameName)}, DAC: {new string(nameLocal, 0, (int)neededLocal)}");
}
}
#endif

return hr;
}
int ISOSDacInterface.GetGCHeapData(void* data)
=> _legacyImpl is not null ? _legacyImpl.GetGCHeapData(data) : HResults.E_NOTIMPL;
int ISOSDacInterface.GetGCHeapDetails(ulong heap, void* details)
Expand Down
Loading