Skip to content

Commit 9652933

Browse files
chrisnasandrewlock
authored andcommitted
[Profiler] Fix Windows 64 bit stack walking (#6583)
## Summary of changes Fix Windows 64 bit stack walking code ## Reason for change On Windows, 64 bit stack walking failed (maybe after a Visual Studio update). After investigating, it was due to fact that the execution context was retrieved in a function that was inlined before. Since this context is then used in the same method to iterate on different frames, then the stack should not be updated (i.e. not returning from a function call). The existing code should have always failed but a previous version of the compiler probably decided to inline it. And this is no more the case for VS 17.12. ## Implementation details Move the context retrieval code in the same function that walk the stack ## Test coverage Already covered in all providers that walk the stack ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
1 parent 76ef64b commit 9652933

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Windows/Windows64BitStackFramesCollector.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,6 @@ bool Windows64BitStackFramesCollector::TryGetThreadStackBoundaries(HANDLE thread
164164
return false;
165165
}
166166

167-
BOOL GetThreadInfo(ManagedThreadInfo* pThreadInfo, CONTEXT& context, HANDLE& handle)
168-
{
169-
if (pThreadInfo == nullptr)
170-
{
171-
::RtlCaptureContext(&context);
172-
handle = ::GetCurrentThread();
173-
return TRUE;
174-
}
175-
176-
handle = pThreadInfo->GetOsThreadHandle();
177-
return ::GetThreadContext(handle, &context);
178-
}
179-
180167
StackSnapshotResultBuffer* Windows64BitStackFramesCollector::CollectStackSampleImplementation(ManagedThreadInfo* pThreadInfo,
181168
uint32_t* pHR,
182169
bool selfCollect)
@@ -188,7 +175,18 @@ StackSnapshotResultBuffer* Windows64BitStackFramesCollector::CollectStackSampleI
188175
CONTEXT context;
189176
context.ContextFlags = CONTEXT_FULL;
190177
HANDLE handle;
191-
BOOL hasInfo = GetThreadInfo(selfCollect ? nullptr : pThreadInfo, context, handle);
178+
BOOL hasInfo = false;
179+
if (selfCollect || (pThreadInfo == nullptr)) // TODO: handle pThreadInfo == nullptr and selfCollect == false
180+
{
181+
::RtlCaptureContext(&context);
182+
handle = ::GetCurrentThread();
183+
hasInfo = TRUE;
184+
}
185+
else
186+
{
187+
handle = pThreadInfo->GetOsThreadHandle();
188+
hasInfo = ::GetThreadContext(handle, &context);
189+
}
192190

193191
if (!hasInfo)
194192
{

0 commit comments

Comments
 (0)