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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup bli
*/
#include <Windows.h>
#include <stdio.h>
#include <dbghelp.h>
#include <shlwapi.h>
#include <tlhelp32.h>
#include "BLI_string.h"
#include "MEM_guardedalloc.h"
static EXCEPTION_POINTERS *current_exception = NULL;
static const char *bli_windows_get_exception_description(const DWORD exceptioncode)
{
switch (exceptioncode) {
case EXCEPTION_ACCESS_VIOLATION:
return "EXCEPTION_ACCESS_VIOLATION";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
return "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
case EXCEPTION_BREAKPOINT:
return "EXCEPTION_BREAKPOINT";
case EXCEPTION_DATATYPE_MISALIGNMENT:
return "EXCEPTION_DATATYPE_MISALIGNMENT";
case EXCEPTION_FLT_DENORMAL_OPERAND:
return "EXCEPTION_FLT_DENORMAL_OPERAND";
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
return "EXCEPTION_FLT_DIVIDE_BY_ZERO";
case EXCEPTION_FLT_INEXACT_RESULT:
return "EXCEPTION_FLT_INEXACT_RESULT";
case EXCEPTION_FLT_INVALID_OPERATION:
return "EXCEPTION_FLT_INVALID_OPERATION";
case EXCEPTION_FLT_OVERFLOW:
return "EXCEPTION_FLT_OVERFLOW";
case EXCEPTION_FLT_STACK_CHECK:
return "EXCEPTION_FLT_STACK_CHECK";
case EXCEPTION_FLT_UNDERFLOW:
return "EXCEPTION_FLT_UNDERFLOW";
case EXCEPTION_ILLEGAL_INSTRUCTION:
return "EXCEPTION_ILLEGAL_INSTRUCTION";
case EXCEPTION_IN_PAGE_ERROR:
return "EXCEPTION_IN_PAGE_ERROR";
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return "EXCEPTION_INT_DIVIDE_BY_ZERO";
case EXCEPTION_INT_OVERFLOW:
return "EXCEPTION_INT_OVERFLOW";
case EXCEPTION_INVALID_DISPOSITION:
return "EXCEPTION_INVALID_DISPOSITION";
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
return "EXCEPTION_NONCONTINUABLE_EXCEPTION";
case EXCEPTION_PRIV_INSTRUCTION:
return "EXCEPTION_PRIV_INSTRUCTION";
case EXCEPTION_SINGLE_STEP:
return "EXCEPTION_SINGLE_STEP";
case EXCEPTION_STACK_OVERFLOW:
return "EXCEPTION_STACK_OVERFLOW";
default:
return "UNKNOWN EXCEPTION";
}
}
static void bli_windows_get_module_name(LPVOID address, PCHAR buffer, size_t size)
{
HMODULE mod;
buffer[0] = 0;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, address, &mod)) {
if (GetModuleFileName(mod, buffer, size)) {
PathStripPath(buffer);
}
}
}
static void bli_windows_get_module_version(const char *file, char *buffer, size_t buffersize)
{
buffer[0] = 0;
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSize(file, &verHandle);
if (verSize != 0) {
LPSTR verData = (LPSTR)MEM_callocN(verSize, "crash module version");
if (GetFileVersionInfo(file, verHandle, verSize, verData)) {
if (VerQueryValue(verData, "\\", (VOID FAR * FAR *)&lpBuffer, &size)) {
if (size) {
VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
/* Magic value from
* https://docs.microsoft.com/en-us/windows/win32/api/verrsrc/ns-verrsrc-vs_fixedfileinfo
*/
if (verInfo->dwSignature == 0xfeef04bd) {
BLI_snprintf(buffer,
buffersize,
"%d.%d.%d.%d",
(verInfo->dwFileVersionMS >> 16) & 0xffff,
(verInfo->dwFileVersionMS >> 0) & 0xffff,
(verInfo->dwFileVersionLS >> 16) & 0xffff,
(verInfo->dwFileVersionLS >> 0) & 0xffff);
}
}
}
}
MEM_freeN(verData);
}
}
static void bli_windows_system_backtrace_exception_record(FILE *fp, PEXCEPTION_RECORD record)
{
char module[MAX_PATH];
fprintf(fp, "Exception Record:\n\n");
fprintf(fp,
"ExceptionCode : %s\n",
bli_windows_get_exception_description(record->ExceptionCode));
fprintf(fp, "Exception Address : 0x%p\n", record->ExceptionAddress);
bli_windows_get_module_name(record->ExceptionAddress, module, sizeof(module));
fprintf(fp, "Exception Module : %s\n", module);
fprintf(fp, "Exception Flags : 0x%.8x\n", record->ExceptionFlags);
fprintf(fp, "Exception Parameters : 0x%x\n", record->NumberParameters);
for (DWORD idx = 0; idx < record->NumberParameters; idx++) {
fprintf(fp, "\tParameters[%d] : 0x%p\n", idx, (LPVOID *)record->ExceptionInformation[idx]);
}
if (record->ExceptionRecord) {
fprintf(fp, "Nested ");
bli_windows_system_backtrace_exception_record(fp, record->ExceptionRecord);
}
fprintf(fp, "\n\n");
}
static bool BLI_windows_system_backtrace_run_trace(FILE *fp, HANDLE hThread, PCONTEXT context)
{
const int max_symbol_length = 100;
bool result = true;
PSYMBOL_INFO symbolinfo = MEM_callocN(sizeof(SYMBOL_INFO) + max_symbol_length * sizeof(char),
"crash Symbol table");
symbolinfo->MaxNameLen = max_symbol_length - 1;
symbolinfo->SizeOfStruct = sizeof(SYMBOL_INFO);
STACKFRAME frame = {0};
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rsp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
while (true) {
if (StackWalk64(IMAGE_FILE_MACHINE_AMD64,
GetCurrentProcess(),
hThread,
&frame,
context,
NULL,
SymFunctionTableAccess64,
SymGetModuleBase64,
0)) {
if (frame.AddrPC.Offset) {
char module[MAX_PATH];
bli_windows_get_module_name((LPVOID)frame.AddrPC.Offset, module, sizeof(module));
if (SymFromAddr(GetCurrentProcess(), (DWORD64)(frame.AddrPC.Offset), 0, symbolinfo)) {
fprintf(fp, "%-20s:0x%p %s", module, (LPVOID)symbolinfo->Address, symbolinfo->Name);
IMAGEHLP_LINE lineinfo;
lineinfo.SizeOfStruct = sizeof(lineinfo);
DWORD displacement = 0;
if (SymGetLineFromAddr(
GetCurrentProcess(), (DWORD64)(frame.AddrPC.Offset), &displacement, &lineinfo)) {
fprintf(fp, " %s:%d", lineinfo.FileName, lineinfo.LineNumber);
}
fprintf(fp, "\n");
}
else {
fprintf(fp,
"%-20s:0x%p %s\n",
module,
(LPVOID)frame.AddrPC.Offset,
"Symbols not available");
result = false;
break;
}
}
else {
break;
}
}
else {
break;
}
}
MEM_freeN(symbolinfo);
fprintf(fp, "\n\n");
return result;
}
static bool bli_windows_system_backtrace_stack_thread(FILE *fp, HANDLE hThread)
{
CONTEXT context = {0};
context.ContextFlags = CONTEXT_ALL;
/* GetThreadContext requires the thread to be in a suspended state, which is problematic for the
* currently running thread, RtlCaptureContext is used as an alternative to sidestep this */
if (hThread != GetCurrentThread()) {
SuspendThread(hThread);
bool success = GetThreadContext(hThread, &context);
ResumeThread(hThread);
if (!success) {
fprintf(fp, "Cannot get thread context : 0x0%.8x\n", GetLastError());
return false;
}
}
else {
RtlCaptureContext(&context);
}
return BLI_windows_system_backtrace_run_trace(fp, hThread, &context);
}
static void bli_windows_system_backtrace_modules(FILE *fp)
{
fprintf(fp, "Loaded Modules :\n");
HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
if (hModuleSnap == INVALID_HANDLE_VALUE)
return;
MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hModuleSnap, &me32)) {
CloseHandle(hModuleSnap); // Must clean up the snapshot object!
fprintf(fp, " Error getting module list.\n");
return;
}
do {
if (me32.th32ProcessID == GetCurrentProcessId()) {
char version[MAX_PATH];
bli_windows_get_module_version(me32.szExePath, version, sizeof(version));
fprintf(fp, "0x%p %-20s %s\n", me32.modBaseAddr, version, me32.szModule);
}
} while (Module32Next(hModuleSnap, &me32));
}
static void bli_windows_system_backtrace_threads(FILE *fp)
{
fprintf(fp, "Threads:\n");
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
THREADENTRY32 te32;
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE) {
fprintf(fp, "Unable to retrieve threads list.\n");
return;
}
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &te32)) {
CloseHandle(hThreadSnap);
return;
}
do {
if (te32.th32OwnerProcessID == GetCurrentProcessId()) {
if (GetCurrentThreadId() != te32.th32ThreadID) {
fprintf(fp, "Thread : %.8x\n", te32.th32ThreadID);
HANDLE ht = OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
bli_windows_system_backtrace_stack_thread(fp, ht);
CloseHandle(ht);
}
}
} while (Thread32Next(hThreadSnap, &te32));
CloseHandle(hThreadSnap);
}
static bool BLI_windows_system_backtrace_stack(FILE *fp)
{
fprintf(fp, "Stack trace:\n");
/* If we are handling an exception use the context record from that. */
if (current_exception) {
/* The back trace code will write to the context record, to protect the original record from
* modifications give the backtrace a copy to work on. */
CONTEXT TempContext = *current_exception->ContextRecord;
return BLI_windows_system_backtrace_run_trace(fp, GetCurrentThread(), &TempContext);
}
else {
return bli_windows_system_backtrace_stack_thread(fp, GetCurrentThread());
}
}
static bool bli_private_symbols_loaded()
{
IMAGEHLP_MODULE64 m64;
m64.SizeOfStruct = sizeof(m64);
if (SymGetModuleInfo64(GetCurrentProcess(), (DWORD64)GetModuleHandle(NULL), &m64)) {
return m64.GlobalSymbols;
}
return false;
}
static void bli_load_symbols()
{
/* If this is a developer station and the private pdb is already loaded leave it be. */
if (bli_private_symbols_loaded()) {
return;
}
char pdb_file[MAX_PATH] = {0};
/* get the currently executing image */
if (GetModuleFileNameA(NULL, pdb_file, sizeof(pdb_file))) {
/* remove the filename */
PathRemoveFileSpecA(pdb_file);
/* append blender.pdb */
PathAppendA(pdb_file, "blender.pdb");
if (PathFileExistsA(pdb_file)) {
HMODULE mod = GetModuleHandle(NULL);
if (mod) {
WIN32_FILE_ATTRIBUTE_DATA file_data;
if (GetFileAttributesExA(pdb_file, GetFileExInfoStandard, &file_data)) {
/* SymInitialize will try to load symbols on its own, so we first must unload whatever it
* did trying to help */
SymUnloadModule64(GetCurrentProcess(), (DWORD64)mod);
DWORD64 module_base = SymLoadModule(GetCurrentProcess(),
NULL,
pdb_file,
NULL,
(DWORD64)mod,
(DWORD)file_data.nFileSizeLow);
if (module_base == 0) {
fprintf(stderr,
"Error loading symbols %s\n\terror:0x%.8x\n\tsize = %d\n\tbase=0x%p\n",
pdb_file,
GetLastError(),
file_data.nFileSizeLow,
(LPVOID)mod);
}
}
}
}
}
}
void BLI_system_backtrace(FILE *fp)
{
SymInitialize(GetCurrentProcess(), NULL, TRUE);
bli_load_symbols();
if (current_exception) {
bli_windows_system_backtrace_exception_record(fp, current_exception->ExceptionRecord);
}
if (BLI_windows_system_backtrace_stack(fp)) {
/* When the blender symbols are missing the stack traces will be unreliable
* so only run if the previous step completed successfully. */
bli_windows_system_backtrace_threads(fp);
}
bli_windows_system_backtrace_modules(fp);
fputc(0, fp); /* Give our selves a nice zero terminator for later on */
}
void BLI_windows_handle_exception(EXCEPTION_POINTERS *exception)
{
current_exception = exception;
if (current_exception) {
fprintf(stderr,
"Error : %s\n",
bli_windows_get_exception_description(exception->ExceptionRecord->ExceptionCode));
fflush(stderr);
LPVOID address = exception->ExceptionRecord->ExceptionAddress;
fprintf(stderr, "Address : 0x%p\n", address);
CHAR modulename[MAX_PATH];
bli_windows_get_module_name(address, modulename, sizeof(modulename));
fprintf(stderr, "Module : %s\n", modulename);
}
fflush(stderr);
}
|