-
Notifications
You must be signed in to change notification settings - Fork 226
/
SymbolProxyClient.cpp
416 lines (349 loc) · 13.3 KB
/
SymbolProxyClient.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//--------------------------------------------------------------------------------------
// SymbolProxyClient.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SymbolProxyClient.h"
#include <SymbolResolve.h>
#include "ATGColors.h"
#include "ControllerFont.h"
#include "CallStack.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using Microsoft::WRL::ComPtr;
using namespace CallStack;
Sample::Sample() :
m_frame(0)
{
m_deviceResources = std::make_unique<DX::DeviceResources>();
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(IUnknown* window)
{
m_gamePad = std::make_unique<GamePad>();
m_deviceResources->SetWindow(window);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
m_backgroundThread = std::thread(&Sample::BackgroundLoop, this);
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Frame %llu", m_frame);
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
PIXEndEvent();
m_frame++;
}
// Updates the world.
void Sample::Update(DX::StepTimer const&)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
if (m_gamePadButtons.a == GamePad::ButtonStateTracker::PRESSED)
{
CaptureRenderThreadCallstack();
}
if (m_gamePadButtons.b == GamePad::ButtonStateTracker::PRESSED)
{
CaptureBackgroundThreadCallstackFromRenderThread();
}
if (m_gamePadButtons.y == GamePad::ButtonStateTracker::PRESSED)
{
LookUpSymbol();
}
if (m_gamePadButtons.x == GamePad::ButtonStateTracker::PRESSED)
{
m_console->Clear();
}
}
else
{
m_gamePadButtons.Reset();
}
PIXEndEvent();
}
#pragma endregion
#pragma region Frame Render
// Draws the scene.
void Sample::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
// Prepare the render target to render a new frame.
m_deviceResources->Prepare();
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
// Render the text console
{
m_console->Render();
}
// Render the help legend
auto size = m_deviceResources->GetOutputSize();
auto safe = SimpleMath::Viewport::ComputeTitleSafeArea(size.right, size.bottom);
m_batch->Begin();
m_smallFont->DrawString(m_batch.get(), L"Symbol Proxy Client",
XMFLOAT2(float(safe.left), float(safe.top)), ATG::Colors::LightGrey);
DX::DrawControllerString(m_batch.get(),
m_smallFont.get(), m_ctrlFont.get(),
L"[View] Exit"
L" [A] Capture Callstack on the Render thread"
L" [B] Capture Callstack on the Background thread"
L" [Y] Look up a symbol"
L" [X] Clear the console",
XMFLOAT2(float(safe.left), float(safe.bottom) - m_smallFont->GetLineSpacing()),
ATG::Colors::LightGrey);
m_batch->End();
PIXEndEvent(context);
// Show the new frame.
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit();
PIXEndEvent(context);
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto renderTarget = m_deviceResources->GetRenderTargetView();
auto depthStencil = m_deviceResources->GetDepthStencilView();
context->ClearRenderTargetView(renderTarget, ATG::Colors::Background);
context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
context->OMSetRenderTargets(1, &renderTarget, depthStencil);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Suspend(0);
}
void Sample::OnResuming()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Resume();
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto context = m_deviceResources->GetD3DDeviceContext();
auto device = m_deviceResources->GetD3DDevice();
m_graphicsMemory = std::make_unique<GraphicsMemory>(device, m_deviceResources->GetBackBufferCount());
m_console = std::make_unique<DX::TextConsoleImage>(context, L"courier_16.spritefont", L"ATGSampleBackground.DDS");
m_batch = std::make_unique<SpriteBatch>(context);
m_smallFont = std::make_unique<SpriteFont>(device, L"SegoeUI_18.spritefont");
m_ctrlFont = std::make_unique<SpriteFont>(device, L"XboxOneControllerLegendSmall.spritefont");
}
#pragma endregion
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
RECT fullscreen = m_deviceResources->GetOutputSize();
m_console->SetWindow(fullscreen, true);
}
void Sample::CaptureRenderThreadCallstack()
{
BackTrace *backtrace = new BackTrace();
size_t numFrames = backtrace->CaptureCurrentThread(); // As advertised, we are capturing this on the render thread
// Resolving symbols must make round-trip communications with xbSymbolProxy.exe which isn't particulary fast.
// You wouldn't really want to do this on the render thread and so to demonstrate a best practice we do
// the BackTrace::Resolve call (and subsequent processing) on a new thread.
std::thread([this, backtrace, numFrames]()
{
// backtrace gets cleaned up when uniqueBacktrace goes out of scope
std::unique_ptr<BackTrace> uniqueBacktrace(backtrace);
HRESULT hr = backtrace->Resolve();
if (FAILED(hr))
{
m_console->Format(L"[" __FUNCTIONW__ L"] BackTrace::Resolve failed (%0#8x)\n", hr);
DisplayFailureAdvice(hr);
}
DisplayBacktrace(numFrames, *backtrace);
}).detach();
}
void Sample::CaptureBackgroundThreadCallstackFromRenderThread()
{
HANDLE hBackgroundThread = m_backgroundThread.native_handle();
BackTrace *backtrace = new BackTrace();
// As advertised, we're capturing a callstack of another thread from this thread
size_t numFrames = backtrace->CaptureCrossThread(hBackgroundThread);
// Since Resolving symbols isn't particularly performant, we do that part (and subsequent processing)
// on a new thread
std::thread([this, backtrace, numFrames]()
{
// backtrace gets cleaned up when uniqueBacktrace goes out of scope
std::unique_ptr<BackTrace> uniqueBacktrace(backtrace);
HRESULT hr = backtrace->Resolve();
if (FAILED(hr))
{
m_console->Format(L"[" __FUNCTIONW__ L"] BackTrace::Resolve failed (%0#8x)\n", hr);
DisplayFailureAdvice(hr);
}
DisplayBacktrace(numFrames, *backtrace);
}).detach();
}
void Sample::DisplayBacktrace(size_t numCapturedFrames, const CallStack::BackTrace &backtrace)
{
if (numCapturedFrames > 0)
{
for (int i = 0; i < backtrace.size(); ++i)
{
const BackTrace::frame_data &fd = backtrace[i];
if (SUCCEEDED(fd.symbolResult) && SUCCEEDED(fd.sourceResult))
{
m_console->Format(L"%s + %#x\n", fd.symbolName.c_str(), fd.symbolOffset);
m_console->Format(L"(%s, %i)\n", fd.sourceFileName.c_str(), fd.sourceLineNumber);
}
else if (SUCCEEDED(fd.symbolResult))
{
m_console->Format(L"%s + %#x\n", fd.symbolName.c_str(), fd.symbolOffset);
}
else
{
m_console->Format(L"%0#16x\n", fd.relativeVirtualAddress + fd.moduleHandle);
}
}
}
}
void Sample::LookUpSymbol()
{
// The following two statics are to enable getting the real function address of
// a class member function and then cast it to a void*
static void(__thiscall Sample::*pMember)() = &Sample::Render;
static void *evil = (void*&)pMember; // The compiler won't let you do reinterpret_cast,
// nor will it even allow a c-style cast. Hence the name "evil".
// A variety of symbols to look up:
static ULONG_PTR symAddresses[] =
{
static_cast<ULONG_PTR>(0), // Intentional: this will not resolve to anything
reinterpret_cast<ULONG_PTR>(Sample::s_SymCallback), // A static method
reinterpret_cast<ULONG_PTR>(evil), // A non-static method
reinterpret_cast<ULONG_PTR>(symAddresses) // Not a function or method
};
// Incremental Linking Thunk (ILT). Sometimes you will see an address resolves to a symbol like
// the following:
// ILT+16355 (?s_SymCallbackSampleCAHPEAX_KJPEB_WKZ)
// The "ILT" indicates that this ia an Incremental Linking Thunk. It is actually the address of
// a jmp instruction that targets the real function address. (You would probably never see this
// in a callstack since a jmp doesn't push a return address.)
ULONG_PTR *addresses = symAddresses;
// Symbol resolution is not performant. Creating a new thread here to avoid stalling the render thread.
std::thread([=]()
{
HRESULT hr = ::GetSymbolFromAddress(ResolveDisposition::DefaultPriority, _countof(symAddresses), addresses, s_SymCallback, this);
if (FAILED(hr))
{
m_console->Format(L"]" __FUNCTIONW__ L"] GetSymbolFromAddress failed (%0#8x)\n", hr);
}
if (SUCCEEDED(hr))
{
hr = ::GetSourceLineFromAddress(ResolveDisposition::DefaultPriority, _countof(symAddresses), addresses, s_SrcCallback, this);
if (FAILED(hr))
{
m_console->Format(L"[" __FUNCTIONW__ L"] GetSourceLineFromAddress failed (%0#8x)\n", hr);
}
}
if (FAILED(hr))
{
DisplayFailureAdvice(hr);
}
}).detach();
}
void Sample::DisplayFailureAdvice(HRESULT hr)
{
if (hr == HRESULT_FROM_WIN32(ERROR_NO_DATA)
|| hr == HRESULT_FROM_WIN32(ERROR_INVALID_STATE))
{
m_console->WriteLine(L": Please start xbSymbolProxy.exe on the development PC and restart the sample.");
}
}
BOOL Sample::s_SymCallback(LPVOID context, ULONG_PTR symbolAddress, HRESULT innerHRESULT, PCWSTR symName, ULONG offset)
{
Sample *thisSample = reinterpret_cast<Sample*>(context);
return thisSample->SymCallback(symbolAddress, innerHRESULT, symName, offset);
}
BOOL Sample::SymCallback(ULONG_PTR symbolAddress, HRESULT innerHRESULT, PCWSTR symName, ULONG offset)
{
if (FAILED(innerHRESULT))
{
m_console->Format(L"[" __FUNCTIONW__ L"] Not able to resolve symbol for address, %0#16x (HRESULT: %0#8x)\n", symbolAddress, innerHRESULT);
if (innerHRESULT == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
m_console->WriteLine(L"Hint: specify the path to your .pdb folder with SymbolProxy /p <path>");
}
}
else
{
m_console->Format(L"Resolved symbol name for address %0#16x:\n Name: %s, Offset: %i\n", symbolAddress, symName, offset);
}
return TRUE;
}
BOOL Sample::s_SrcCallback(LPVOID context, ULONG_PTR symbolAddress, HRESULT innerHRESULT, PCWSTR filepath, ULONG lineNumber)
{
Sample *thisSample = reinterpret_cast<Sample*>(context);
return thisSample->SrcCallback(symbolAddress, innerHRESULT, filepath, lineNumber);
}
BOOL Sample::SrcCallback(ULONG_PTR symbolAddress, HRESULT innerHRESULT, PCWSTR filepath, ULONG lineNumber)
{
if (FAILED(innerHRESULT))
{
m_console->Format(L"[" __FUNCTIONW__ L"] Not able to resolve source information for symbol at address, %0#16x (HRESULT: %0#8x)\n", symbolAddress, innerHRESULT);
}
else
{
m_console->Format(L"Resolved symbol source information for address %0#16x:\n Filepath: %s, Line Number: %i\n", symbolAddress, filepath, lineNumber);
}
return TRUE;
}
void Sample::BackgroundLoop()
{
while (true)
{
unsigned maxDepth = 1 + std::rand() % 10;
RecursiveReporter(1, maxDepth);
}
}
void Sample::RecursiveReporter(unsigned depth, unsigned maxDepth)
{
if (depth == maxDepth)
{
return;
}
RecursiveReporter(depth + 1, maxDepth);
{
// The output tells you how many frames to expect when you press the button.
m_console->Format(L"[%s]: depth = %i\n", __FUNCTIONW__, depth);
}
// This sleep is here to slow it down for the human pressing the button.
Sleep(1000);
}