Skip to content

Commit

Permalink
Fix Thread::SetProfilerFilterContext usage (#52607)
Browse files Browse the repository at this point in the history
Add holder to catch exceptions and clear context.

Issue: #47895
  • Loading branch information
mikem8361 committed May 11, 2021
1 parent b306268 commit acdc536
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/coreclr/debug/daccess/dacimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,6 @@ class DacStackReferenceWalker : public DefaultCOMImpl<ISOSStackRefEnum, IID_ISOS
return &pData[mCurr->count++];
}


template <class IntType, class StructType>
IntType WalkStack(IntType count, StructType refs[], promote_func promote, GCEnumCallback enumFunc)
{
Expand All @@ -2001,15 +2000,36 @@ class DacStackReferenceWalker : public DefaultCOMImpl<ISOSStackRefEnum, IID_ISOS
_ASSERTE(mCurr == NULL);
_ASSERTE(mHead.next == NULL);

bool clearProfilerFilterContext = false;
class ProfilerFilterContextHolder
{
Thread* m_pThread;

public:
ProfilerFilterContextHolder() : m_pThread(NULL)
{
}

void Activate(Thread* pThread)
{
m_pThread = pThread;
}

~ProfilerFilterContextHolder()
{
if (m_pThread != NULL)
m_pThread->SetProfilerFilterContext(NULL);
}
};

ProfilerFilterContextHolder contextHolder;
T_CONTEXT ctx;

// Get the current thread's context and set that as the filter context
if (mThread->GetFilterContext() == NULL && mThread->GetProfilerFilterContext() == NULL)
{
mDac->m_pTarget->GetThreadContext(mThread->GetOSThreadId(), CONTEXT_FULL, sizeof(ctx), (BYTE*)&ctx);
mThread->SetProfilerFilterContext(&ctx);
clearProfilerFilterContext = true;
contextHolder.Activate(mThread);
}

// Setup GCCONTEXT structs for the stackwalk.
Expand All @@ -2036,11 +2056,6 @@ class DacStackReferenceWalker : public DefaultCOMImpl<ISOSStackRefEnum, IID_ISOS
mEnumerated = true;
mThread->StackWalkFrames(DacStackReferenceWalker::Callback, &gcctx, flagsStackWalk);

if (clearProfilerFilterContext)
{
mThread->SetProfilerFilterContext(NULL);
}

// We have filled the user's array as much as we could. If there's more data than
// could fit, mHead.Next will contain a linked list of refs to enumerate.
mCurr = mHead.next;
Expand Down

0 comments on commit acdc536

Please sign in to comment.