| 1 |
/* |
| 2 |
Copyright (c) 2004 Andrei Polushin |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to deal |
| 6 |
in the Software without restriction, including without limitation the rights |
| 7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in |
| 12 |
all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#if !defined(_M_ARM) && !defined(_M_ARM64) \ |
| 24 |
&& !defined(_M_X64) && defined(_MSC_VER) |
| 25 |
|
| 26 |
/* TODO: arm[64], x64 currently miss some machine-dependent code below. */ |
| 27 |
/* See also GC_HAVE_BUILTIN_BACKTRACE in gc_config_macros.h. */ |
| 28 |
|
| 29 |
#define GC_BUILD |
| 30 |
#include "private/msvc_dbg.h" |
| 31 |
#include "gc.h" |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
#ifndef WIN32_LEAN_AND_MEAN |
| 36 |
# define WIN32_LEAN_AND_MEAN 1 |
| 37 |
#endif |
| 38 |
#define NOSERVICE |
| 39 |
#include <windows.h> |
| 40 |
|
| 41 |
#pragma pack(push, 8) |
| 42 |
#include <imagehlp.h> |
| 43 |
#pragma pack(pop) |
| 44 |
|
| 45 |
#pragma comment(lib, "dbghelp.lib") |
| 46 |
#pragma optimize("gy", off) |
| 47 |
|
| 48 |
/* Disable a warning that /GS can not protect parameters and local */ |
| 49 |
/* variables from local buffer overrun because optimizations are off. */ |
| 50 |
#pragma warning(disable:4748) |
| 51 |
|
| 52 |
typedef GC_word word; |
| 53 |
#define GC_ULONG_PTR word |
| 54 |
|
| 55 |
#ifdef _WIN64 |
| 56 |
typedef GC_ULONG_PTR ULONG_ADDR; |
| 57 |
#else |
| 58 |
typedef ULONG ULONG_ADDR; |
| 59 |
#endif |
| 60 |
|
| 61 |
static HANDLE GetSymHandle(void) |
| 62 |
{ |
| 63 |
static HANDLE symHandle = NULL; |
| 64 |
if (!symHandle) { |
| 65 |
BOOL bRet = SymInitialize(symHandle = GetCurrentProcess(), NULL, FALSE); |
| 66 |
if (bRet) { |
| 67 |
DWORD dwOptions = SymGetOptions(); |
| 68 |
dwOptions &= ~SYMOPT_UNDNAME; |
| 69 |
dwOptions |= SYMOPT_LOAD_LINES; |
| 70 |
SymSetOptions(dwOptions); |
| 71 |
} |
| 72 |
} |
| 73 |
return symHandle; |
| 74 |
} |
| 75 |
|
| 76 |
static void* CALLBACK FunctionTableAccess(HANDLE hProcess, |
| 77 |
ULONG_ADDR dwAddrBase) |
| 78 |
{ |
| 79 |
return SymFunctionTableAccess(hProcess, dwAddrBase); |
| 80 |
} |
| 81 |
|
| 82 |
static ULONG_ADDR CALLBACK GetModuleBase(HANDLE hProcess, ULONG_ADDR dwAddress) |
| 83 |
{ |
| 84 |
MEMORY_BASIC_INFORMATION memoryInfo; |
| 85 |
ULONG_ADDR dwAddrBase = SymGetModuleBase(hProcess, dwAddress); |
| 86 |
if (dwAddrBase != 0) { |
| 87 |
return dwAddrBase; |
| 88 |
} |
| 89 |
if (VirtualQueryEx(hProcess, (void*)(GC_ULONG_PTR)dwAddress, &memoryInfo, |
| 90 |
sizeof(memoryInfo))) { |
| 91 |
char filePath[_MAX_PATH]; |
| 92 |
char curDir[_MAX_PATH]; |
| 93 |
char exePath[_MAX_PATH]; |
| 94 |
DWORD size = GetModuleFileNameA((HINSTANCE)memoryInfo.AllocationBase, |
| 95 |
filePath, sizeof(filePath)); |
| 96 |
|
| 97 |
/* Save and restore current directory around SymLoadModule, see KB */ |
| 98 |
/* article Q189780. */ |
| 99 |
GetCurrentDirectoryA(sizeof(curDir), curDir); |
| 100 |
GetModuleFileNameA(NULL, exePath, sizeof(exePath)); |
| 101 |
#if _MSC_VER > 1200 |
| 102 |
strcat_s(exePath, sizeof(exePath), "\\.."); |
| 103 |
#else /* VC6 or earlier */ |
| 104 |
strcat(exePath, "\\.."); |
| 105 |
#endif |
| 106 |
SetCurrentDirectoryA(exePath); |
| 107 |
#ifdef _DEBUG |
| 108 |
GetCurrentDirectoryA(sizeof(exePath), exePath); |
| 109 |
#endif |
| 110 |
SymLoadModule(hProcess, NULL, size ? filePath : NULL, NULL, |
| 111 |
(ULONG_ADDR)(GC_ULONG_PTR)memoryInfo.AllocationBase, 0); |
| 112 |
SetCurrentDirectoryA(curDir); |
| 113 |
} |
| 114 |
return (ULONG_ADDR)(GC_ULONG_PTR)memoryInfo.AllocationBase; |
| 115 |
} |
| 116 |
|
| 117 |
static ULONG_ADDR CheckAddress(void* address) |
| 118 |
{ |
| 119 |
ULONG_ADDR dwAddress = (ULONG_ADDR)(GC_ULONG_PTR)address; |
| 120 |
GetModuleBase(GetSymHandle(), dwAddress); |
| 121 |
return dwAddress; |
| 122 |
} |
| 123 |
|
| 124 |
size_t GetStackFrames(size_t skip, void* frames[], size_t maxFrames) |
| 125 |
{ |
| 126 |
HANDLE hProcess = GetSymHandle(); |
| 127 |
HANDLE hThread = GetCurrentThread(); |
| 128 |
CONTEXT context; |
| 129 |
context.ContextFlags = CONTEXT_FULL; |
| 130 |
if (!GetThreadContext(hThread, &context)) { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
/* GetThreadContext might return invalid context for the current thread. */ |
| 134 |
#if defined(_M_IX86) |
| 135 |
__asm mov context.Ebp, ebp |
| 136 |
#endif |
| 137 |
return GetStackFramesFromContext(hProcess, hThread, &context, skip + 1, |
| 138 |
frames, maxFrames); |
| 139 |
} |
| 140 |
|
| 141 |
size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread, |
| 142 |
CONTEXT* context, size_t skip, |
| 143 |
void* frames[], size_t maxFrames) |
| 144 |
{ |
| 145 |
size_t frameIndex; |
| 146 |
DWORD machineType; |
| 147 |
STACKFRAME stackFrame = { 0 }; |
| 148 |
stackFrame.AddrPC.Mode = AddrModeFlat; |
| 149 |
#if defined(_M_IX86) |
| 150 |
machineType = IMAGE_FILE_MACHINE_I386; |
| 151 |
stackFrame.AddrPC.Offset = context->Eip; |
| 152 |
stackFrame.AddrStack.Mode = AddrModeFlat; |
| 153 |
stackFrame.AddrStack.Offset = context->Esp; |
| 154 |
stackFrame.AddrFrame.Mode = AddrModeFlat; |
| 155 |
stackFrame.AddrFrame.Offset = context->Ebp; |
| 156 |
#elif defined(_M_MRX000) |
| 157 |
machineType = IMAGE_FILE_MACHINE_R4000; |
| 158 |
stackFrame.AddrPC.Offset = context->Fir; |
| 159 |
#elif defined(_M_ALPHA) |
| 160 |
machineType = IMAGE_FILE_MACHINE_ALPHA; |
| 161 |
stackFrame.AddrPC.Offset = (unsigned long)context->Fir; |
| 162 |
#elif defined(_M_PPC) |
| 163 |
machineType = IMAGE_FILE_MACHINE_POWERPC; |
| 164 |
stackFrame.AddrPC.Offset = context->Iar; |
| 165 |
#elif defined(_M_IA64) |
| 166 |
machineType = IMAGE_FILE_MACHINE_IA64; |
| 167 |
stackFrame.AddrPC.Offset = context->StIIP; |
| 168 |
#elif defined(_M_ALPHA64) |
| 169 |
machineType = IMAGE_FILE_MACHINE_ALPHA64; |
| 170 |
stackFrame.AddrPC.Offset = context->Fir; |
| 171 |
#elif !defined(CPPCHECK) |
| 172 |
# error Unknown CPU |
| 173 |
#endif |
| 174 |
for (frameIndex = 0; frameIndex < maxFrames; ) { |
| 175 |
BOOL bRet = StackWalk(machineType, hProcess, hThread, &stackFrame, |
| 176 |
&context, NULL, FunctionTableAccess, GetModuleBase, NULL); |
| 177 |
if (!bRet) { |
| 178 |
break; |
| 179 |
} |
| 180 |
if (skip) { |
| 181 |
skip--; |
| 182 |
} else { |
| 183 |
frames[frameIndex++] = (void*)(GC_ULONG_PTR)stackFrame.AddrPC.Offset; |
| 184 |
} |
| 185 |
} |
| 186 |
return frameIndex; |
| 187 |
} |
| 188 |
|
| 189 |
size_t GetModuleNameFromAddress(void* address, char* moduleName, size_t size) |
| 190 |
{ |
| 191 |
if (size) *moduleName = 0; |
| 192 |
{ |
| 193 |
const char* sourceName; |
| 194 |
IMAGEHLP_MODULE moduleInfo = { sizeof (moduleInfo) }; |
| 195 |
if (!SymGetModuleInfo(GetSymHandle(), CheckAddress(address), |
| 196 |
&moduleInfo)) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
sourceName = strrchr(moduleInfo.ImageName, '\\'); |
| 200 |
if (sourceName) { |
| 201 |
sourceName++; |
| 202 |
} else { |
| 203 |
sourceName = moduleInfo.ImageName; |
| 204 |
} |
| 205 |
if (size) { |
| 206 |
strncpy(moduleName, sourceName, size)[size - 1] = 0; |
| 207 |
} |
| 208 |
return strlen(sourceName); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
size_t GetModuleNameFromStack(size_t skip, char* moduleName, size_t size) |
| 213 |
{ |
| 214 |
void* address = NULL; |
| 215 |
GetStackFrames(skip + 1, &address, 1); |
| 216 |
if (address) { |
| 217 |
return GetModuleNameFromAddress(address, moduleName, size); |
| 218 |
} |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
union sym_namebuf_u { |
| 223 |
IMAGEHLP_SYMBOL sym; |
| 224 |
char symNameBuffer[sizeof(IMAGEHLP_SYMBOL) + MAX_SYM_NAME]; |
| 225 |
}; |
| 226 |
|
| 227 |
size_t GetSymbolNameFromAddress(void* address, char* symbolName, size_t size, |
| 228 |
size_t* offsetBytes) |
| 229 |
{ |
| 230 |
if (size) *symbolName = 0; |
| 231 |
if (offsetBytes) *offsetBytes = 0; |
| 232 |
__try { |
| 233 |
ULONG_ADDR dwOffset = 0; |
| 234 |
union sym_namebuf_u u; |
| 235 |
|
| 236 |
u.sym.SizeOfStruct = sizeof(u.sym); |
| 237 |
u.sym.MaxNameLength = sizeof(u.symNameBuffer) - sizeof(u.sym); |
| 238 |
|
| 239 |
if (!SymGetSymFromAddr(GetSymHandle(), CheckAddress(address), &dwOffset, |
| 240 |
&u.sym)) { |
| 241 |
return 0; |
| 242 |
} else { |
| 243 |
const char* sourceName = u.sym.Name; |
| 244 |
char undName[1024]; |
| 245 |
if (UnDecorateSymbolName(u.sym.Name, undName, sizeof(undName), |
| 246 |
UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ACCESS_SPECIFIERS)) { |
| 247 |
sourceName = undName; |
| 248 |
} else if (SymUnDName(&u.sym, undName, sizeof(undName))) { |
| 249 |
sourceName = undName; |
| 250 |
} |
| 251 |
if (offsetBytes) { |
| 252 |
*offsetBytes = dwOffset; |
| 253 |
} |
| 254 |
if (size) { |
| 255 |
strncpy(symbolName, sourceName, size)[size - 1] = 0; |
| 256 |
} |
| 257 |
return strlen(sourceName); |
| 258 |
} |
| 259 |
} __except (EXCEPTION_EXECUTE_HANDLER) { |
| 260 |
SetLastError(GetExceptionCode()); |
| 261 |
} |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
size_t GetSymbolNameFromStack(size_t skip, char* symbolName, size_t size, |
| 266 |
size_t* offsetBytes) |
| 267 |
{ |
| 268 |
void* address = NULL; |
| 269 |
GetStackFrames(skip + 1, &address, 1); |
| 270 |
if (address) { |
| 271 |
return GetSymbolNameFromAddress(address, symbolName, size, offsetBytes); |
| 272 |
} |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
size_t GetFileLineFromAddress(void* address, char* fileName, size_t size, |
| 277 |
size_t* lineNumber, size_t* offsetBytes) |
| 278 |
{ |
| 279 |
if (size) *fileName = 0; |
| 280 |
if (lineNumber) *lineNumber = 0; |
| 281 |
if (offsetBytes) *offsetBytes = 0; |
| 282 |
{ |
| 283 |
char* sourceName; |
| 284 |
IMAGEHLP_LINE line = { sizeof (line) }; |
| 285 |
GC_ULONG_PTR dwOffset = 0; |
| 286 |
if (!SymGetLineFromAddr(GetSymHandle(), CheckAddress(address), &dwOffset, |
| 287 |
&line)) { |
| 288 |
return 0; |
| 289 |
} |
| 290 |
if (lineNumber) { |
| 291 |
*lineNumber = line.LineNumber; |
| 292 |
} |
| 293 |
if (offsetBytes) { |
| 294 |
*offsetBytes = dwOffset; |
| 295 |
} |
| 296 |
sourceName = line.FileName; |
| 297 |
/* TODO: resolve relative filenames, found in 'source directories' */ |
| 298 |
/* registered with MSVC IDE. */ |
| 299 |
if (size) { |
| 300 |
strncpy(fileName, sourceName, size)[size - 1] = 0; |
| 301 |
} |
| 302 |
return strlen(sourceName); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
size_t GetFileLineFromStack(size_t skip, char* fileName, size_t size, |
| 307 |
size_t* lineNumber, size_t* offsetBytes) |
| 308 |
{ |
| 309 |
void* address = NULL; |
| 310 |
GetStackFrames(skip + 1, &address, 1); |
| 311 |
if (address) { |
| 312 |
return GetFileLineFromAddress(address, fileName, size, lineNumber, |
| 313 |
offsetBytes); |
| 314 |
} |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
#define GC_SNPRINTF _snprintf |
| 319 |
|
| 320 |
size_t GetDescriptionFromAddress(void* address, const char* format, |
| 321 |
char* buffer, size_t size) |
| 322 |
{ |
| 323 |
char* const begin = buffer; |
| 324 |
char* const end = buffer + size; |
| 325 |
size_t line_number = 0; |
| 326 |
|
| 327 |
(void)format; |
| 328 |
if (size) { |
| 329 |
*buffer = 0; |
| 330 |
} |
| 331 |
buffer += GetFileLineFromAddress(address, buffer, size, &line_number, NULL); |
| 332 |
size = (GC_ULONG_PTR)end < (GC_ULONG_PTR)buffer ? 0 : end - buffer; |
| 333 |
|
| 334 |
if (line_number) { |
| 335 |
char str[20]; |
| 336 |
|
| 337 |
(void)GC_SNPRINTF(str, sizeof(str), "(%d) : ", (int)line_number); |
| 338 |
str[sizeof(str) - 1] = '\0'; |
| 339 |
if (size) { |
| 340 |
strncpy(buffer, str, size)[size - 1] = 0; |
| 341 |
} |
| 342 |
buffer += strlen(str); |
| 343 |
size = (GC_ULONG_PTR)end < (GC_ULONG_PTR)buffer ? 0 : end - buffer; |
| 344 |
} |
| 345 |
|
| 346 |
if (size) { |
| 347 |
strncpy(buffer, "at ", size)[size - 1] = 0; |
| 348 |
} |
| 349 |
buffer += sizeof("at ") - 1; |
| 350 |
size = (GC_ULONG_PTR)end < (GC_ULONG_PTR)buffer ? 0 : end - buffer; |
| 351 |
|
| 352 |
buffer += GetSymbolNameFromAddress(address, buffer, size, NULL); |
| 353 |
size = (GC_ULONG_PTR)end < (GC_ULONG_PTR)buffer ? 0 : end - buffer; |
| 354 |
|
| 355 |
if (size) { |
| 356 |
strncpy(buffer, " in ", size)[size - 1] = 0; |
| 357 |
} |
| 358 |
buffer += sizeof(" in ") - 1; |
| 359 |
size = (GC_ULONG_PTR)end < (GC_ULONG_PTR)buffer ? 0 : end - buffer; |
| 360 |
|
| 361 |
buffer += GetModuleNameFromAddress(address, buffer, size); |
| 362 |
return buffer - begin; |
| 363 |
} |
| 364 |
|
| 365 |
size_t GetDescriptionFromStack(void* const frames[], size_t count, |
| 366 |
const char* format, char* description[], |
| 367 |
size_t size) |
| 368 |
{ |
| 369 |
const GC_ULONG_PTR begin = (GC_ULONG_PTR)description; |
| 370 |
const GC_ULONG_PTR end = begin + size; |
| 371 |
GC_ULONG_PTR buffer = begin + (count + 1) * sizeof(char*); |
| 372 |
size_t i; |
| 373 |
|
| 374 |
for (i = 0; i < count; ++i) { |
| 375 |
if (description) |
| 376 |
description[i] = (char*)buffer; |
| 377 |
buffer += 1 + GetDescriptionFromAddress(frames[i], format, (char*)buffer, |
| 378 |
end < buffer ? 0 : end - buffer); |
| 379 |
} |
| 380 |
if (description) |
| 381 |
description[count] = NULL; |
| 382 |
return buffer - begin; |
| 383 |
} |
| 384 |
|
| 385 |
/* Compatibility with execinfo.h: */ |
| 386 |
|
| 387 |
int backtrace(void* addresses[], int count) |
| 388 |
{ |
| 389 |
return GetStackFrames(1, addresses, count); |
| 390 |
} |
| 391 |
|
| 392 |
char** backtrace_symbols(void*const* addresses, int count) |
| 393 |
{ |
| 394 |
size_t size = GetDescriptionFromStack(addresses, count, NULL, NULL, 0); |
| 395 |
char** symbols = (char**)malloc(size); |
| 396 |
if (symbols != NULL) |
| 397 |
GetDescriptionFromStack(addresses, count, NULL, symbols, size); |
| 398 |
return symbols; |
| 399 |
} |
| 400 |
|
| 401 |
#else |
| 402 |
|
| 403 |
extern int GC_quiet; |
| 404 |
/* ANSI C does not allow translation units to be empty. */ |
| 405 |
|
| 406 |
#endif |