This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
CallStack.cpp
299 lines (255 loc) · 8.89 KB
/
CallStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//--------------------------------------------------------------------------------------
// CallStack.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "CallStack.h"
#include <mutex>
#include <SymbolResolve.h>
#include <OSLockable.h>
#include <ThreadHelpers.h>
using namespace CallStack;
size_t CallStack::CaptureBackTraceFromCurrentThread(void** addresses, HMODULE* modules, size_t framesToCapture)
{
DWORD dwStackHash = 0;
static const uint32_t FRAMES_TO_SKIP = 1;
size_t numCapturedFrames = RtlCaptureStackBackTrace(FRAMES_TO_SKIP, static_cast<DWORD>(framesToCapture), addresses, &dwStackHash);
for (uint32_t i = 0; i < numCapturedFrames; ++i)
{
uint64_t baseAddress = 0;
RUNTIME_FUNCTION *runtimeFunction = ::RtlLookupFunctionEntry(reinterpret_cast<uint64_t>(addresses[i]), &baseAddress, nullptr);
if (runtimeFunction == nullptr)
{
// If we don't have a RUNTIME_FUNCTION, then we've encountered
// a leaf function. Adjust the stack data appropriately.
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCWSTR>(addresses[i]), &modules[i]))
{
modules[i] = 0;
}
}
else
{
modules[i] = reinterpret_cast<HMODULE>(baseAddress);
}
}
return numCapturedFrames;
}
size_t CallStack::CaptureBackTraceFromContext(CONTEXT *contextRecord, void** addresses, HMODULE* modules, size_t framesToCapture)
{
if (contextRecord == nullptr)
{
return 0;
}
CONTEXT ctx = *contextRecord;
uint64_t baseAddress = 0;
KNONVOLATILE_CONTEXT_POINTERS NvContext = {};
void* handlerData = nullptr;
uint64_t establisherFrame = 0;
size_t numCapturedFrames = 0;
do
{
RUNTIME_FUNCTION *runtimeFunction = RtlLookupFunctionEntry(ctx.Rip, &baseAddress, nullptr);
modules[numCapturedFrames] = (HMODULE)baseAddress;
addresses[numCapturedFrames] = reinterpret_cast<void*>(ctx.Rip);
++numCapturedFrames;
if (runtimeFunction)
{
// unwind based on current context
RtlVirtualUnwind(
UNW_FLAG_NHANDLER,
baseAddress,
ctx.Rip,
runtimeFunction,
&ctx,
&handlerData,
&establisherFrame,
&NvContext);
}
else
{
// If we don't have a RUNTIME_FUNCTION, then we've encountered
// a leaf function. Adjust the stack appropriately.
ctx.Rip = *reinterpret_cast<uint64_t*>(ctx.Rsp);
ctx.Rsp += 8;
}
} while (numCapturedFrames < framesToCapture && ctx.Rip != 0);
return numCapturedFrames;
}
size_t CallStack::GetFrameCountFromContext(CONTEXT *contextRecord)
{
if (contextRecord == nullptr)
{
return 0;
}
CONTEXT ctx = *contextRecord;
uint64_t baseAddress = 0;
KNONVOLATILE_CONTEXT_POINTERS NvContext = {};
void* handlerData = nullptr;
uint64_t establisherFrame = 0;
size_t numCapturedFrames = 0;
do
{
RUNTIME_FUNCTION *runtimeFunction = RtlLookupFunctionEntry(ctx.Rip, &baseAddress, nullptr);
++numCapturedFrames;
if (runtimeFunction)
{
// unwind based on current context
RtlVirtualUnwind(
UNW_FLAG_NHANDLER,
baseAddress,
ctx.Rip,
runtimeFunction,
&ctx,
&handlerData,
&establisherFrame,
&NvContext);
}
else
{
// If we don't have a RUNTIME_FUNCTION, then we've encountered
// a leaf function. Adjust the stack appropriately.
ctx.Rip = *reinterpret_cast<uint64_t*>(ctx.Rsp);
ctx.Rsp += 8;
}
} while (ctx.Rip != 0);
return numCapturedFrames;
}
namespace
{
static const size_t MAX_FRAMES_TO_CAPTURE = 64;
void* g_addresses[MAX_FRAMES_TO_CAPTURE];
HMODULE g_modules[MAX_FRAMES_TO_CAPTURE];
ATG::CriticalSectionLockable g_frameDataMutex;
} // anonymous namespace
void BackTrace::Clear()
{
m_frames.clear();
memset(g_addresses, 0, sizeof(g_addresses));
memset(g_modules, 0, sizeof(g_modules));
}
size_t BackTrace::CaptureCurrentThread()
{
std::lock_guard<ATG::CriticalSectionLockable> scopedLock(g_frameDataMutex);
Clear();
size_t numCapturedFrames = CaptureBackTraceFromCurrentThread(g_addresses, g_modules, MAX_FRAMES_TO_CAPTURE);
m_frames.resize(numCapturedFrames);
for (size_t i = 0; i < numCapturedFrames; ++i)
{
frame_data &fd = m_frames[i];
fd.moduleHandle = g_modules[i];
fd.relativeVirtualAddress = reinterpret_cast<uint64_t>(g_addresses[i]) - reinterpret_cast<uint64_t>(g_modules[i]);
}
return numCapturedFrames;
}
size_t BackTrace::CaptureCrossThread(HANDLE whichThread)
{
if (IsCurrentThread(whichThread))
{
return CaptureCurrentThread();
}
{
std::lock_guard<ATG::CriticalSectionLockable> scopedLock(g_frameDataMutex);
size_t numCapturedFrames = 0;
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_FULL;
{
DX::ThreadSuspender susp(whichThread);
if (GetThreadContext(whichThread, &ctx))
{
// Only want to suspend the thread for the duration of the capture
numCapturedFrames = CaptureBackTraceFromContext(&ctx, g_addresses, g_modules, MAX_FRAMES_TO_CAPTURE);
}
}
m_frames.resize(numCapturedFrames);
for (size_t i = 0; i < numCapturedFrames; ++i)
{
frame_data &fd = m_frames[i];
fd.moduleHandle = g_modules[i];
fd.relativeVirtualAddress = reinterpret_cast<uint64_t>(g_addresses[i]) - reinterpret_cast<uint64_t>(g_modules[i]);
}
return numCapturedFrames;
}
}
HRESULT BackTrace::Resolve(unsigned flags)
{
std::vector<uint64_t> rawFrames;
size_t numFrames = m_frames.size();
if (numFrames == 0)
{
assert(numFrames > 0); // Probably forgot to capture frames (should at least have a frame for the capture method -- right?)
return E_NOT_VALID_STATE;
}
rawFrames.resize(numFrames);
for (size_t i = 0; i < numFrames; ++i)
{
rawFrames[i] = m_frames[i].relativeVirtualAddress + (uint64_t)m_frames[i].moduleHandle;
}
HRESULT hr = S_OK;
if (SUCCEEDED(hr) && (flags & CAPTURE_FLAGS_SYMBOL_INFO) == CAPTURE_FLAGS_SYMBOL_INFO)
{
hr = ResolveSymbolInfo(rawFrames.size(), &rawFrames[0]);
}
if (SUCCEEDED(hr) && (flags & CAPTURE_FLAGS_SOURCE_INFO) == CAPTURE_FLAGS_SOURCE_INFO)
{
hr = ResolveSourceInfo(rawFrames.size(), &rawFrames[0]);
}
return hr;
}
HRESULT BackTrace::ResolveSymbolInfo(size_t numFrames, ULONG_PTR *addresses)
{
ResolutionContext ctx;
ctx.whichFrame = 0;
ctx.frames = &m_frames[0];
return ::GetSymbolFromAddress(ResolveDisposition::DefaultPriority, static_cast<DWORD>(numFrames), addresses, s_SymCallback, &ctx);
}
HRESULT BackTrace::ResolveSourceInfo(size_t numFrames, ULONG_PTR *addresses)
{
ResolutionContext ctx;
ctx.whichFrame = 0;
ctx.frames = &m_frames[0];
return ::GetSourceLineFromAddress(ResolveDisposition::DefaultPriority, static_cast<DWORD>(numFrames), addresses, s_SrcCallback, &ctx);
}
BOOL BackTrace::s_SymCallback(LPVOID context, ULONG_PTR symbolAddress, HRESULT symbolResult, PCWSTR symName, ULONG offset)
{
UNREFERENCED_PARAMETER(symbolAddress);
if (context == nullptr)
{
assert(context != nullptr);
// Yikes!! (this could only be a programming error)
return FALSE;
}
ResolutionContext &ctx = *reinterpret_cast<ResolutionContext*>(context);
unsigned i = ctx.whichFrame++;
ctx.frames[i].symbolResult = symbolResult;
if (SUCCEEDED(symbolResult))
{
ctx.frames[i].symbolName = symName;
ctx.frames[i].symbolOffset = offset;
}
return TRUE;
}
BOOL BackTrace::s_SrcCallback(LPVOID context, ULONG_PTR symbolAddress, HRESULT sourceResult, PCWSTR filepath, ULONG lineNumber)
{
UNREFERENCED_PARAMETER(symbolAddress);
if (context == nullptr)
{
assert(context != nullptr);
// Yikes!! (this could only be a programming error)
return FALSE;
}
ResolutionContext &ctx = *reinterpret_cast<ResolutionContext*>(context);
unsigned i = ctx.whichFrame++;
ctx.frames[i].sourceResult = sourceResult;
if (SUCCEEDED(sourceResult))
{
ctx.frames[i].sourceFileName = filepath;
ctx.frames[i].sourceLineNumber = lineNumber;
}
return TRUE;
}
bool BackTrace::IsCurrentThread(HANDLE whichThread) const
{
return GetThreadId(whichThread) == ::GetCurrentThreadId();
}