-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleSymbolEngine.cpp
More file actions
300 lines (252 loc) · 8.84 KB
/
SimpleSymbolEngine.cpp
File metadata and controls
300 lines (252 loc) · 8.84 KB
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
/*
NAME
SimpleSymbolEngine
DESCRIPTION
Simple symbol engine functionality.
This is demonstration code only - it is non. thread-safe and single instance.
COPYRIGHT
Copyright (C) 2004, 2021 by Roger Orr <rogero@howzatt.co.uk>
This software is distributed in the hope that it will be useful, but
without WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Permission is granted to anyone to make or distribute verbatim
copies of this software provided that the copyright notice and
this permission notice are preserved, and that the distributor
grants the recipent permission for further distribution as permitted
by this notice.
Comments and suggestions are always welcome.
Please report bugs to rogero@howzatt.co.uk.
*/
#define _CRT_SECURE_NO_WARNINGS
#include "SimpleSymbolEngine.h"
#include <windows.h>
#include <cstddef>
#include <iostream>
#include <sstream>
#include <vector>
#include <dbghelp.h>
#include <psapi.h> // GetModuleFileNameEx
#pragma comment( lib, "dbghelp" )
static char const szRCSID[] = "$Id: SimpleSymbolEngine.cpp 297 2021-07-07 21:30:58Z roger $";
namespace
{
// Helper function to read up to maxSize bytes from address in target process
// into the supplied buffer.
// Returns number of bytes actually read.
SIZE_T ReadPartialProcessMemory(HANDLE hProcess, LPCVOID address, LPVOID buffer, SIZE_T minSize, SIZE_T maxSize)
{
SIZE_T length = maxSize;
while (length >= minSize)
{
if (ReadProcessMemory(hProcess, address, buffer, length, 0))
{
return length;
}
length--;
static SYSTEM_INFO SystemInfo;
static BOOL b = (GetSystemInfo(&SystemInfo), TRUE);
SIZE_T pageOffset = ((ULONG_PTR)address + length) % SystemInfo.dwPageSize;
if (pageOffset > length)
break;
length -= pageOffset;
}
return 0;
}
// We pass 'false' as the fInvadeProcess argument to SymInitialize (for performance)
// and therefore need to call SymLoadModule64 when necessary
DWORD64 CALLBACK GetModuleBaseWrapper(HANDLE hProcess, DWORD64 address)
{
DWORD64 result = SymGetModuleBase64(hProcess, address);
if (!result)
{
// Verify a address (invalid addresses can cause AV in some dbgHelp versions)
MEMORY_BASIC_INFORMATION mbInfo;
if (::VirtualQueryEx( hProcess, (PVOID)address, &mbInfo, sizeof(mbInfo)) &&
((mbInfo.State & MEM_FREE) == 0))
{
const DWORD64 baseAddress = (DWORD64)mbInfo.AllocationBase;
const HMODULE hmod = (HMODULE)mbInfo.AllocationBase;
char szFileName[MAX_PATH + 1] = "";
if (GetModuleFileNameEx(hProcess, hmod, szFileName, MAX_PATH))
{
result = ::SymLoadModuleEx(hProcess, NULL, szFileName, NULL, baseAddress, 0, NULL, 0);
}
}
}
return result;
}
}
/////////////////////////////////////////////////////////////////////////////////////
SimpleSymbolEngine::SimpleSymbolEngine()
{
DWORD dwOpts = SymGetOptions();
dwOpts |= SYMOPT_LOAD_LINES | SYMOPT_OMAP_FIND_NEAREST;
SymSetOptions(dwOpts);
}
/////////////////////////////////////////////////////////////////////////////////////
void SimpleSymbolEngine::init(HANDLE hTargetProcess)
{
this->hProcess = hTargetProcess;
::SymInitialize(hProcess, 0, false);
}
/////////////////////////////////////////////////////////////////////////////////////
SimpleSymbolEngine::~SimpleSymbolEngine()
{
::SymCleanup(hProcess);
}
/////////////////////////////////////////////////////////////////////////////////////
std::string SimpleSymbolEngine::addressToString(PVOID address)
{
std::ostringstream oss;
// First the raw address
oss << "0x" << address;
// Then symbol, if any
struct
{
SYMBOL_INFO symInfo;
char name[ 4 * 256 ];
} SymInfo = { {sizeof(SymInfo.symInfo)}, "" };
PSYMBOL_INFO pSym = &SymInfo.symInfo;
pSym->MaxNameLen = sizeof(SymInfo.name);
DWORD64 uDisplacement(0);
if (SymFromAddr(hProcess, reinterpret_cast<ULONG_PTR>(address), &uDisplacement, pSym))
{
oss << " " << pSym->Name;
if (uDisplacement != 0)
{
LONG_PTR displacement = static_cast<LONG_PTR>(uDisplacement);
if (displacement < 0)
oss << " - " << -displacement;
else
oss << " + " << displacement;
}
}
// Finally any file/line number
IMAGEHLP_LINE64 lineInfo = {sizeof(lineInfo)};
DWORD dwDisplacement(0);
if (SymGetLineFromAddr64(hProcess, reinterpret_cast<ULONG_PTR>(address), &dwDisplacement, &lineInfo))
{
oss << " " << lineInfo.FileName << "(" << lineInfo.LineNumber << ")";
if (dwDisplacement != 0)
{
oss << " + " << dwDisplacement << " byte" << (dwDisplacement == 1 ? "" : "s");
}
}
return oss.str();
}
/////////////////////////////////////////////////////////////////////////////////////
void SimpleSymbolEngine::loadModule(HANDLE hFile, PVOID baseAddress, std::string const & fileName)
{
::SymLoadModule64(hProcess, hFile, const_cast<char*>(fileName.c_str()), 0, reinterpret_cast<ULONG_PTR>(baseAddress), 0);
}
/////////////////////////////////////////////////////////////////////////////////////
void SimpleSymbolEngine::unloadModule(PVOID baseAddress)
{
::SymUnloadModule64(hProcess, reinterpret_cast<ULONG_PTR>(baseAddress));
}
/////////////////////////////////////////////////////////////////////////////////////
// StackTrace: try to trace the stack to the given output
void SimpleSymbolEngine::stackTrace(HANDLE hThread, std::ostream & os )
{
CONTEXT context = {0};
PVOID pContext = &context;
STACKFRAME64 stackFrame = {0};
#ifdef _M_IX86
DWORD const machineType = IMAGE_FILE_MACHINE_I386;
context.ContextFlags = CONTEXT_FULL;
GetThreadContext(hThread, &context);
stackFrame.AddrPC.Offset = context.Eip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = context.Ebp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = context.Esp;
stackFrame.AddrStack.Mode = AddrModeFlat;
os << " Frame Code address\n";
#elif _M_X64
DWORD machineType;
BOOL bWow64(false);
WOW64_CONTEXT wow64_context = {0};
IsWow64Process(hProcess, &bWow64);
if (bWow64)
{
machineType = IMAGE_FILE_MACHINE_I386;
wow64_context.ContextFlags = WOW64_CONTEXT_FULL;
Wow64GetThreadContext(hThread, &wow64_context);
pContext = &wow64_context;
stackFrame.AddrPC.Offset = wow64_context.Eip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = wow64_context.Ebp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = wow64_context.Esp;
stackFrame.AddrStack.Mode = AddrModeFlat;
}
else
{
machineType = IMAGE_FILE_MACHINE_AMD64;
context.ContextFlags = CONTEXT_FULL;
GetThreadContext(hThread, &context);
stackFrame.AddrPC.Offset = context.Rip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = context.Rbp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = context.Rsp;
stackFrame.AddrStack.Mode = AddrModeFlat;
}
os << " Frame Code address\n";
#else
#error Unsupported target platform
#endif // _M_IX86
DWORD64 lastBp = 0; // Prevent loops with optimised stackframes
// Need this despite passing into StackWalk64
GetModuleBaseWrapper(hProcess, stackFrame.AddrPC.Offset);
while (::StackWalk64(machineType,
hProcess, hThread,
&stackFrame, pContext,
0, ::SymFunctionTableAccess64, GetModuleBaseWrapper, 0))
{
if (stackFrame.AddrPC.Offset == 0)
{
os << "Null address\n";
break;
}
// Need this despite passing into StackWalk64
GetModuleBaseWrapper(hProcess, stackFrame.AddrPC.Offset);
PVOID frame = reinterpret_cast<PVOID>(stackFrame.AddrFrame.Offset);
PVOID pc = reinterpret_cast<PVOID>(stackFrame.AddrPC.Offset);
os << " 0x" << frame << " " << addressToString(pc) << "\n";
if (lastBp >= stackFrame.AddrFrame.Offset)
{
os << "Stack frame out of sequence...\n";
break;
}
lastBp = stackFrame.AddrFrame.Offset;
}
os.flush();
}
/////////////////////////////////////////////////////////////////////////////////////
// Read a string from the target
std::string SimpleSymbolEngine::getString(PVOID address, BOOL unicode, DWORD maxStringLength)
{
if (unicode)
{
std::vector<wchar_t> chVector(maxStringLength + 1);
ReadPartialProcessMemory(hProcess, address, &chVector[0], sizeof(wchar_t), maxStringLength * sizeof(wchar_t));
size_t const wcLen = wcstombs(0, &chVector[0], 0);
if (wcLen == (size_t)-1)
{
return "invalid string";
}
else
{
std::vector<char> mbStr(wcLen + 1);
wcstombs(&mbStr[0], &chVector[0], wcLen);
return &mbStr[0];
}
}
else
{
std::vector<char> chVector(maxStringLength + 1);
ReadPartialProcessMemory(hProcess, address, &chVector[0], 1, maxStringLength);
return &chVector[0];
}
}