Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/coreclr/vm/profilinghelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,15 @@ class SetCallbackStateFlagsHolder
DWORD m_dwOriginalFullState;
};

class ProfilerELTContextHolder
{
public:
ProfilerELTContextHolder(T_CONTEXT *pContext);
~ProfilerELTContextHolder();

private:
Thread * m_pThread;
T_CONTEXT *m_pOriginalContext;
};

#endif //__PROFILING_HELPER_H__
30 changes: 30 additions & 0 deletions src/coreclr/vm/profilinghelper.inl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ FORCEINLINE SetCallbackStateFlagsHolder::~SetCallbackStateFlagsHolder()
}
}

FORCEINLINE ProfilerELTContextHolder::ProfilerELTContextHolder(T_CONTEXT *pContext)
{
m_pThread = (pContext != nullptr) ? GetThreadNULLOk() : nullptr;
if (m_pThread != nullptr)
{
m_pOriginalContext = m_pThread->GetProfilerELTContext();
m_pThread->SetProfilerELTContext(pContext);
}
else
{
m_pOriginalContext = nullptr;
}
}

FORCEINLINE ProfilerELTContextHolder::~ProfilerELTContextHolder()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;

if (m_pThread != nullptr)
{
m_pThread->SetProfilerELTContext(m_pOriginalContext);
}
}

#ifdef ENABLE_CONTRACTS
//---------------------------------------------------------------------------------------
//
Expand Down
53 changes: 53 additions & 0 deletions src/coreclr/vm/proftoeeinterfaceimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
#include "generics.h"
#include "gcinfo.h"
#include "safemath.h"
#include "stacktrace.h"
#include "threadsuspend.h"
#include "inlinetracking.h"
#include "frozenobjectheap.h"
Expand Down Expand Up @@ -8578,6 +8579,8 @@ HRESULT ProfToEEInterfaceImpl::DoStackSnapshot(ThreadID thread,
CONTEXT ctxCurrent;
memset(&ctxCurrent, 0, sizeof(ctxCurrent));

CONTEXT ctxELT;

REGDISPLAY rd;

PROFILER_STACK_WALK_DATA data;
Expand Down Expand Up @@ -8716,6 +8719,17 @@ HRESULT ProfToEEInterfaceImpl::DoStackSnapshot(ThreadID thread,
#endif // !PLATFORM_SUPPORTS_SAFE_THREADSUSPEND
}

if ((pctxSeed == nullptr) && (pThreadToSnapshot == pCurrentThread))
{
T_CONTEXT *pELTContext = pCurrentThread->GetProfilerELTContext();
if (pELTContext != nullptr)
{
CopyOSContext(&ctxELT, pELTContext);
Thread::VirtualUnwindToFirstManagedCallFrame(&ctxELT);

@jkotas jkotas Aug 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is indirectly introducing new libunwind dependency. It is factory for problems. For example, this won't work on Windows x86 at all.

I think if we wanted to fix this, it would be better to fix the codegen for these hooks so that there is proper managed->unmanaged transition.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure DoStackSnapshot works within ELT hooks on Windows x86, as it returned CORPROF_E_STACKSNAPSHOT_UNSAFE on Windows x64 both in .NET 9 and .NET 10.
It seemed like the scenario only worked on Linux x64 in .NET 9.

Thanks, I'll look into fixing the codegen if this overhead is going in the wrong direction.

@jkotas jkotas Aug 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this never worked on Windows x64, was it actually working reliably on Linux? If it never worked well, can we block it everywhere instead like it is blocked on Windows?

I do not think it is worth it to try to make these callbacks worth better. They are slow, and I believe most profilers moved away from them.

@mdh1418 mdh1418 Aug 14, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is there a particular reason for DoStackSnapshot to not be supported within these callbacks?
I don't fully understand what DoStackSnapshot within these callbacks would achieve, but for the user of the original issue, what would an alternative be?

It sounds like the scenario was working on Linux until #107152, from the issue author it worked on .NET 8 and .NET 9. I confirmed it worked on .NET 9, didn't try .NET 8 yet.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/icorprofilerinfo2-dostacksnapshot-method documentation says that the synchronous DoStackSnapshot stackwalk only works from ICorProfilerCallback callbacks. ETL hook is not ICorProfilerCallback callback. I do not think this was ever meant to work.

pctxSeed = &ctxELT;
}
}
Comment on lines +8722 to +8731

// If target thread is in pre-emptive mode, the profiler's seed context is unnecessary
// because our frame chain is good enough: it will give us at least as accurate a
// starting point as the profiler could. Also, since profiler contexts cannot be
Expand Down Expand Up @@ -10694,6 +10708,21 @@ void __stdcall ProfilerUnmanagedToManagedTransitionMD(MethodDesc *pMD,
// These do a lot of work for us, setting up Frames, gathering arg info and resolving generics.
//*******************************************************************************************

#ifdef PROFILING_SUPPORTED
static void CaptureProfilerELTContext(T_CONTEXT *pContext)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
}
CONTRACTL_END;

ClrCaptureContext(pContext);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is very expensive. What is the performance impact of this change when the ETL hooks are enabled? I suspect that it will extremely slow to the point of being unusuable.

@jkotas jkotas Aug 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see you have some benchmark results in the PR description. What was the benchmark source?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I used an adhoc BenchmarkDotNet microbenchmark in my local repo in conjunction with a profiler with a barebones EnterCallback. Since the context saving and restoration surround the callback in ProfilerEnter, I ran the benchmark against the baseline repo and changed corerun builds to measure the overhead introduced in this PR.

}
#endif // PROFILING_SUPPORTED

HCIMPL2(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecificHandle)
{
FCALL_CONTRACT;
Expand Down Expand Up @@ -10741,6 +10770,14 @@ HCIMPL2(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecifi
SetCallbackStateFlagsHolder csf(
COR_PRF_CALLBACKSTATE_INCALLBACK);

T_CONTEXT *pProfilerELTContext = nullptr;
if (CORProfilerStackSnapshotEnabled())
{
pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT)));
CaptureProfilerELTContext(pProfilerELTContext);
}
ProfilerELTContextHolder contextHolder(pProfilerELTContext);

COR_PRF_ELT_INFO_INTERNAL eltInfo;
eltInfo.platformSpecificHandle = platformSpecificHandle;

Expand Down Expand Up @@ -10908,6 +10945,14 @@ HCIMPL2(EXTERN_C void, ProfileLeave, UINT_PTR clientData, void * platformSpecifi
SetCallbackStateFlagsHolder csf(
COR_PRF_CALLBACKSTATE_INCALLBACK);

T_CONTEXT *pProfilerELTContext = nullptr;
if (CORProfilerStackSnapshotEnabled())
{
pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT)));
CaptureProfilerELTContext(pProfilerELTContext);
}
ProfilerELTContextHolder contextHolder(pProfilerELTContext);

COR_PRF_ELT_INFO_INTERNAL eltInfo;
eltInfo.platformSpecificHandle = platformSpecificHandle;

Expand Down Expand Up @@ -11033,6 +11078,14 @@ HCIMPL2(EXTERN_C void, ProfileTailcall, UINT_PTR clientData, void * platformSpec
SetCallbackStateFlagsHolder csf(
COR_PRF_CALLBACKSTATE_INCALLBACK);

T_CONTEXT *pProfilerELTContext = nullptr;
if (CORProfilerStackSnapshotEnabled())
{
pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT)));
CaptureProfilerELTContext(pProfilerELTContext);
}
ProfilerELTContextHolder contextHolder(pProfilerELTContext);

COR_PRF_ELT_INFO_INTERNAL eltInfo;
eltInfo.platformSpecificHandle = platformSpecificHandle;

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ Thread::Thread()
}

m_pProfilerFilterContext = NULL;
m_pProfilerELTContext = nullptr;
#endif // PROFILING_SUPPORTED

m_CacheStackBase = 0;
Expand Down
20 changes: 20 additions & 0 deletions src/coreclr/vm/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,12 @@ class Thread
//---------------------------------------------------------------
T_CONTEXT *m_pProfilerFilterContext;

//---------------------------------------------------------------
// Native context captured before invoking an ELT profiler callback.
// DoStackSnapshot may unwind it to seed a same-thread stack walk.
//---------------------------------------------------------------
T_CONTEXT *m_pProfilerELTContext;

//---------------------------------------------------------------
// Bitmask to remember per-thread state useful for the profiler API. See
// COR_PRF_CALLBACKSTATE_* flags in clr\src\inc\ProfilePriv.h for bit values.
Expand Down Expand Up @@ -2836,6 +2842,20 @@ class Thread
m_pProfilerFilterContext = pContext;
}

void SetProfilerELTContext(T_CONTEXT *pContext)
{
LIMITED_METHOD_CONTRACT;

m_pProfilerELTContext = pContext;
}

T_CONTEXT *GetProfilerELTContext()
{
LIMITED_METHOD_CONTRACT;

return m_pProfilerELTContext;
}

FORCEINLINE DWORD GetProfilerEvacuationCounter(size_t slot)
{
LIMITED_METHOD_CONTRACT;
Expand Down
52 changes: 51 additions & 1 deletion src/tests/profiler/native/eltprofiler/slowpatheltprofiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ HRESULT SlowPathELTProfiler::Initialize(IUnknown* pICorProfilerInfoUnk)
if (FAILED(hr = pCorProfilerInfo->SetEventMask2(COR_PRF_MONITOR_ENTERLEAVE
| COR_PRF_ENABLE_FUNCTION_ARGS
| COR_PRF_ENABLE_FUNCTION_RETVAL
| COR_PRF_ENABLE_FRAME_INFO,
| COR_PRF_ENABLE_FRAME_INFO
| COR_PRF_ENABLE_STACK_SNAPSHOT,
0)))
{
wcout << L"FAIL: IpCorProfilerInfo::SetEventMask2() failed hr=0x" << std::hex << hr << endl;
Expand Down Expand Up @@ -164,6 +165,8 @@ HRESULT STDMETHODCALLTYPE SlowPathELTProfiler::EnterCallback(FunctionIDOrClientI
return S_OK;
}

TestHookRestrictions();

COR_PRF_FRAME_INFO frameInfo;
ULONG pcbArgumentInfo = 0;
NewArrayHolder<BYTE> pArgumentInfoBytes;
Expand Down Expand Up @@ -422,13 +425,60 @@ HRESULT STDMETHODCALLTYPE SlowPathELTProfiler::EnterCallback(FunctionIDOrClientI
return hr;
}

HRESULT STDMETHODCALLTYPE SlowPathELTProfiler::StackSnapshotCallback(
FunctionID functionId,
UINT_PTR instructionPointer,
COR_PRF_FRAME_INFO frameInfo,
ULONG32 contextSize,
BYTE context[],
void *clientData)
{
bool *sawManagedFrame = reinterpret_cast<bool *>(clientData);
*sawManagedFrame |= functionId != 0;
return S_OK;
}

void SlowPathELTProfiler::TestHookRestrictions()
{
if (_testedHookRestrictions.exchange(true))
{
return;
}

#ifndef WIN32
bool sawManagedFrame = false;
HRESULT hr = pCorProfilerInfo->DoStackSnapshot(
0,
StackSnapshotCallback,
COR_PRF_SNAPSHOT_DEFAULT,
&sawManagedFrame,
nullptr,
0);
if (hr != S_OK || !sawManagedFrame)
{
wcout << L"DoStackSnapshot from ELT hook failed hr=0x" << std::hex << hr
<< L" sawManagedFrame=" << sawManagedFrame << endl;
_failures++;
}
#endif // WIN32

HRESULT forceGCHr = pCorProfilerInfo->ForceGC();
if (forceGCHr != CORPROF_E_UNSUPPORTED_CALL_SEQUENCE)
{
wcout << L"ForceGC from ELT hook returned unexpected hr=0x" << std::hex << forceGCHr << endl;
_failures++;
}
}

HRESULT STDMETHODCALLTYPE SlowPathELTProfiler::LeaveCallback(FunctionIDOrClientID functionIdOrClientID, COR_PRF_ELT_INFO eltInfo)
{
if (_testType != TestType::LeaveHooks)
{
return S_OK;
}

TestHookRestrictions();

COR_PRF_FRAME_INFO frameInfo;
COR_PRF_FUNCTION_ARGUMENT_RANGE * pRetvalRange = new COR_PRF_FUNCTION_ARGUMENT_RANGE;
HRESULT hr = pCorProfilerInfo->GetFunctionLeave3Info(functionIdOrClientID.functionID, eltInfo, &frameInfo, pRetvalRange);
Expand Down
12 changes: 11 additions & 1 deletion src/tests/profiler/native/eltprofiler/slowpatheltprofiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SlowPathELTProfiler : public Profiler
public:
static std::shared_ptr<SlowPathELTProfiler> s_profiler;

SlowPathELTProfiler() : Profiler(), _failures(0), _testType(TestType::Unknown)
SlowPathELTProfiler() : Profiler(), _failures(0), _testedHookRestrictions(false), _testType(TestType::Unknown)
{
_sawFuncEnter[L"SimpleArgsFunc"] = false;
_sawFuncEnter[L"MixedStructFunc"] = false;
Expand Down Expand Up @@ -185,11 +185,21 @@ class SlowPathELTProfiler : public Profiler
};

std::atomic<int> _failures;
std::atomic<bool> _testedHookRestrictions;
std::unordered_map<std::wstring, bool> _sawFuncEnter;
std::unordered_map<std::wstring, bool> _sawFuncLeave;

TestType _testType;

static HRESULT STDMETHODCALLTYPE StackSnapshotCallback(
FunctionID functionId,
UINT_PTR instructionPointer,
COR_PRF_FRAME_INFO frameInfo,
ULONG32 contextSize,
BYTE context[],
void *clientData);

void TestHookRestrictions();
void PrintBytes(const BYTE *bytes, size_t length);

bool ValidateInt(UINT_PTR ptr, int expected);
Expand Down
Loading