Skip to content
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

[cdac] Implement ISOSDacInterface::GetNestedExceptionData #103668

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/coreclr/debug/daccess/dacimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ class ClrDataAccess

HRESULT GetThreadDataImpl(CLRDATA_ADDRESS threadAddr, struct DacpThreadData *threadData);
HRESULT GetThreadStoreDataImpl(struct DacpThreadStoreData *data);
HRESULT GetNestedExceptionDataImpl(CLRDATA_ADDRESS exception, CLRDATA_ADDRESS *exceptionObject, CLRDATA_ADDRESS *nextNestedException);

BOOL IsExceptionFromManagedCode(EXCEPTION_RECORD * pExceptionRecord);
#ifndef TARGET_UNIX
Expand Down
47 changes: 36 additions & 11 deletions src/coreclr/debug/daccess/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3233,7 +3233,6 @@ ClrDataAccess::GetUsefulGlobals(struct DacpUsefulGlobalsData *globalsData)
return hr;
}


HRESULT
ClrDataAccess::GetNestedExceptionData(CLRDATA_ADDRESS exception, CLRDATA_ADDRESS *exceptionObject, CLRDATA_ADDRESS *nextNestedException)
{
Expand All @@ -3242,26 +3241,52 @@ ClrDataAccess::GetNestedExceptionData(CLRDATA_ADDRESS exception, CLRDATA_ADDRESS

SOSDacEnter();

#ifdef FEATURE_EH_FUNCLETS
ExceptionTrackerBase *pExData = PTR_ExceptionTrackerBase(TO_TADDR(exception));
#else
ExInfo *pExData = PTR_ExInfo(TO_TADDR(exception));
#endif // FEATURE_EH_FUNCLETS

if (!pExData)
if (m_cdacSos != NULL)
{
hr = E_INVALIDARG;
// Try the cDAC first - it will return E_NOTIMPL if it doesn't support this method yet. Fall back to the DAC.
hr = m_cdacSos->GetNestedExceptionData(exception, exceptionObject, nextNestedException);
if (FAILED(hr))
{
hr = GetNestedExceptionDataImpl(exception, exceptionObject, nextNestedException);
}
#ifdef _DEBUG
else
{
// Assert that the data is the same as what we get from the DAC.
CLRDATA_ADDRESS exceptionObjectLocal;
CLRDATA_ADDRESS nextNestedExceptionLocal;
HRESULT hrLocal = GetNestedExceptionDataImpl(exception, &exceptionObjectLocal, &nextNestedExceptionLocal);
_ASSERTE(hr == hrLocal);
_ASSERTE(*exceptionObject == exceptionObjectLocal);
_ASSERTE(*nextNestedException == nextNestedExceptionLocal);
}
#endif
}
else
{
*exceptionObject = TO_CDADDR(*PTR_TADDR(pExData->m_hThrowable));
*nextNestedException = PTR_HOST_TO_TADDR(pExData->m_pPrevNestedInfo);
hr = GetNestedExceptionDataImpl(exception, exceptionObject, nextNestedException);
}

SOSDacLeave();
return hr;
}

HRESULT
ClrDataAccess::GetNestedExceptionDataImpl(CLRDATA_ADDRESS exception, CLRDATA_ADDRESS *exceptionObject, CLRDATA_ADDRESS *nextNestedException)
{
#ifdef FEATURE_EH_FUNCLETS
ExceptionTrackerBase *pExData = PTR_ExceptionTrackerBase(TO_TADDR(exception));
#else
ExInfo *pExData = PTR_ExInfo(TO_TADDR(exception));
#endif // FEATURE_EH_FUNCLETS

if (!pExData)
return E_INVALIDARG;

*exceptionObject = TO_CDADDR(*PTR_TADDR(pExData->m_hThrowable));
*nextNestedException = PTR_HOST_TO_TADDR(pExData->m_pPrevNestedInfo);
return S_OK;
}

HRESULT
ClrDataAccess::GetDomainLocalModuleData(CLRDATA_ADDRESS addr, struct DacpDomainLocalModuleData *pLocalModuleData)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/debug/runtimeinfo/datadescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ CDAC_TYPE_END(GCAllocContext)
CDAC_TYPE_BEGIN(ExceptionInfo)
CDAC_TYPE_INDETERMINATE(ExceptionInfo)
#if FEATURE_EH_FUNCLETS
CDAC_TYPE_FIELD(ExceptionInfo, /*pointer*/, ThrownObject, offsetof(ExceptionTrackerBase, m_hThrowable))
CDAC_TYPE_FIELD(PreviousNestedInfo, /*pointer*/, PreviousNestedInfo, offsetof(ExceptionTrackerBase, m_pPrevNestedInfo))
#else
CDAC_TYPE_FIELD(ExceptionInfo, /*pointer*/, ThrownObject, offsetof(ExInfo, m_hThrowable))
CDAC_TYPE_FIELD(PreviousNestedInfo, /*pointer*/, PreviousNestedInfo, offsetof(ExInfo, m_pPrevNestedInfo))
#endif
CDAC_TYPE_END(ExceptionInfo)
Expand Down
10 changes: 9 additions & 1 deletion src/native/managed/cdacreader/src/Contracts/Thread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static IContract IContract.Create(Target target, int version)
public virtual ThreadStoreData GetThreadStoreData() => throw new NotImplementedException();
public virtual ThreadStoreCounts GetThreadCounts() => throw new NotImplementedException();
public virtual ThreadData GetThreadData(TargetPointer thread) => throw new NotImplementedException();
public virtual TargetPointer GetExceptionInfo(TargetPointer exception, out TargetPointer nextNestedException) => throw new NotImplementedException();
Copy link
Member Author

@elinor-fung elinor-fung Jun 18, 2024

Choose a reason for hiding this comment

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

This is on Thread right now, since that's kind of where the exception pointer came from. We may want to put it on an Exception contract - for handling the SOS-DAC APIs right now, it seems like it would just be this and getting info for the managed exception object (GetObjectExceptionData). But presumably we could add something to handle generating the stack trace such that SOS wouldn't directly inspect/assume things about StackTraceInfo and StackTraceElement:
https://github.com/dotnet/diagnostics/blob/d592e0da173e520ef42d4f9cdd9d7b138a9896fd/src/SOS/Strike/strike.cpp#L2454
https://github.com/dotnet/diagnostics/blob/d592e0da173e520ef42d4f9cdd9d7b138a9896fd/src/SOS/Strike/strike.cpp#L2766-L2796
https://github.com/dotnet/diagnostics/blob/d592e0da173e520ef42d4f9cdd9d7b138a9896fd/src/SOS/Strike/strike.cpp#L2610-L2617

Copy link
Member

Choose a reason for hiding this comment

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

BTW: The StackTraceElement details are changing in #103076

}

internal readonly struct Thread : IThread
Expand Down Expand Up @@ -127,10 +128,17 @@ ThreadData IThread.GetThreadData(TargetPointer threadPointer)
thread.Frame,
firstNestedException,
thread.TEB,
thread.LastThrownObject,
thread.LastThrownObject.Handle,
GetThreadFromLink(thread.LinkNext));
}

TargetPointer IThread.GetExceptionInfo(TargetPointer exception, out TargetPointer nextNestedException)
{
Data.ExceptionInfo exceptionInfo = _target.ProcessedData.GetOrAdd<Data.ExceptionInfo>(exception);
nextNestedException = exceptionInfo.PreviousNestedInfo;
return exceptionInfo.ThrownObject.Object;
}

private TargetPointer GetThreadFromLink(TargetPointer threadLink)
{
if (threadLink == TargetPointer.Null)
Expand Down
3 changes: 3 additions & 0 deletions src/native/managed/cdacreader/src/Data/ExceptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public ExceptionInfo(Target target, TargetPointer address)
Target.TypeInfo type = target.GetTypeInfo(DataType.ExceptionInfo);

PreviousNestedInfo = target.ReadPointer(address + (ulong)type.Fields[nameof(PreviousNestedInfo)].Offset);
ThrownObject = target.ProcessedData.GetOrAdd<ObjectHandle>(
target.ReadPointer(address + (ulong)type.Fields[nameof(ThrownObject)].Offset));
}

public TargetPointer PreviousNestedInfo { get; init; }
public ObjectHandle ThrownObject { get; init; }
}
20 changes: 20 additions & 0 deletions src/native/managed/cdacreader/src/Data/ObjectHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Diagnostics.DataContractReader.Data;

internal sealed class ObjectHandle : IData<ObjectHandle>
{
static ObjectHandle IData<ObjectHandle>.Create(Target target, TargetPointer address)
=> new ObjectHandle(target, address);

public ObjectHandle(Target target, TargetPointer address)
{
Handle = address;
if (address != TargetPointer.Null)
Object = target.ReadPointer(address);
}

public TargetPointer Handle { get; init; }
public TargetPointer Object { get; init; } = TargetPointer.Null;
}
5 changes: 3 additions & 2 deletions src/native/managed/cdacreader/src/Data/Thread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public Thread(Target target, TargetPointer address)
TEB = type.Fields.TryGetValue(nameof(TEB), out Target.FieldInfo fieldInfo)
? target.ReadPointer(address + (ulong)fieldInfo.Offset)
: TargetPointer.Null;
LastThrownObject = target.ReadPointer(address + (ulong)type.Fields[nameof(LastThrownObject)].Offset);
LastThrownObject = target.ProcessedData.GetOrAdd<ObjectHandle>(
target.ReadPointer(address + (ulong)type.Fields[nameof(LastThrownObject)].Offset));
LinkNext = target.ReadPointer(address + (ulong)type.Fields[nameof(LinkNext)].Offset);

// Address of the exception tracker - how it should be read depends on EH funclets feature global value
Expand All @@ -41,7 +42,7 @@ public Thread(Target target, TargetPointer address)
public GCAllocContext? AllocContext { get; init; }
public TargetPointer Frame { get; init; }
public TargetPointer TEB { get; init; }
public TargetPointer LastThrownObject { get; init; }
public ObjectHandle LastThrownObject { get; init; }
public TargetPointer LinkNext { get; init; }
public TargetPointer ExceptionTracker { get; init; }
}
19 changes: 18 additions & 1 deletion src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ public int GetBreakingChangeVersion()
public unsafe int GetMethodTableTransparencyData(ulong mt, void* data) => HResults.E_NOTIMPL;
public unsafe int GetModule(ulong addr, void** mod) => HResults.E_NOTIMPL;
public unsafe int GetModuleData(ulong moduleAddr, void* data) => HResults.E_NOTIMPL;
public unsafe int GetNestedExceptionData(ulong exception, ulong* exceptionObject, ulong* nextNestedException) => HResults.E_NOTIMPL;

public unsafe int GetNestedExceptionData(ulong exception, ulong* exceptionObject, ulong* nextNestedException)
{
try
{
Contracts.IThread contract = _target.Contracts.Thread;
TargetPointer exceptionObjectLocal = contract.GetExceptionInfo(exception, out TargetPointer nextNestedExceptionLocal);
*exceptionObject = exceptionObjectLocal;
*nextNestedException = nextNestedExceptionLocal;
}
catch (Exception ex)
{
return ex.HResult;
}

return HResults.S_OK;
}

public unsafe int GetObjectClassName(ulong obj, uint count, char* className, uint* pNeeded) => HResults.E_NOTIMPL;
public unsafe int GetObjectData(ulong objAddr, void* data) => HResults.E_NOTIMPL;
public unsafe int GetObjectStringData(ulong obj, uint count, char* stringData, uint* pNeeded) => HResults.E_NOTIMPL;
Expand Down
Loading