BsWin32CrashHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrerequisitesUtil.h"
  4. #include "BsDebug.h"
  5. #include "BsDynLib.h"
  6. #include "BsFileSystem.h"
  7. #include "windows.h"
  8. #include <psapi.h>
  9. // Disable warning in VS2015 that's not under my control
  10. #pragma warning(disable : 4091)
  11. #include "DbgHelp.h"
  12. #pragma warning(default : 4091)
  13. namespace BansheeEngine
  14. {
  15. /**
  16. * @brief Returns the raw stack trace using the provided context. Raw stack trace contains only
  17. * function addresses.
  18. *
  19. * @param context Processor context from which to start the stack trace.
  20. * @param stackTrace Output parameter that will contain the function addresses. First address is the deepest
  21. * called function and following address is its caller and so on.
  22. *
  23. * @returns Number of functions in the call stack.
  24. */
  25. UINT32 win32_getRawStackTrace(CONTEXT context, UINT64 stackTrace[BS_MAX_STACKTRACE_DEPTH])
  26. {
  27. HANDLE hProcess = GetCurrentProcess();
  28. HANDLE hThread = GetCurrentThread();
  29. UINT32 machineType;
  30. STACKFRAME64 stackFrame;
  31. memset(&stackFrame, 0, sizeof(stackFrame));
  32. stackFrame.AddrPC.Mode = AddrModeFlat;
  33. stackFrame.AddrStack.Mode = AddrModeFlat;
  34. stackFrame.AddrFrame.Mode = AddrModeFlat;
  35. #if BS_ARCH_TYPE == BS_ARCHITECTURE_x86_64
  36. stackFrame.AddrPC.Offset = context.Rip;
  37. stackFrame.AddrStack.Offset = context.Rsp;
  38. stackFrame.AddrFrame.Offset = context.Rbp;
  39. machineType = IMAGE_FILE_MACHINE_AMD64;
  40. #else
  41. stackFrame.AddrPC.Offset = context.Eip;
  42. stackFrame.AddrStack.Offset = context.Esp;
  43. stackFrame.AddrFrame.Offset = context.Ebp;
  44. machineType = IMAGE_FILE_MACHINE_I386;
  45. #endif
  46. UINT32 numEntries = 0;
  47. while (true)
  48. {
  49. if (!StackWalk64(machineType, hProcess, hThread, &stackFrame, &context, nullptr,
  50. SymFunctionTableAccess64, SymGetModuleBase64, nullptr))
  51. {
  52. break;
  53. }
  54. if (numEntries < BS_MAX_STACKTRACE_DEPTH)
  55. stackTrace[numEntries] = stackFrame.AddrPC.Offset;
  56. numEntries++;
  57. if (stackFrame.AddrPC.Offset == 0 || stackFrame.AddrFrame.Offset == 0)
  58. break;
  59. }
  60. return numEntries;
  61. }
  62. /**
  63. * @brief Returns a string containing a stack trace using the provided context. If function can be found in the symbol
  64. * table its readable name will be present in the stack trace, otherwise just its address.
  65. *
  66. * @param context Processor context from which to start the stack trace.
  67. * @param skip Number of bottom-most call stack entries to skip.
  68. *
  69. * @returns String containing the call stack with each function on its own line.
  70. */
  71. String win32_getStackTrace(CONTEXT context, UINT32 skip = 0)
  72. {
  73. UINT64 rawStackTrace[BS_MAX_STACKTRACE_DEPTH];
  74. UINT32 numEntries = win32_getRawStackTrace(context, rawStackTrace);
  75. numEntries = std::min((UINT32)BS_MAX_STACKTRACE_DEPTH, numEntries);
  76. UINT32 bufferSize = sizeof(PIMAGEHLP_SYMBOL64) + BS_MAX_STACKTRACE_NAME_BYTES;
  77. UINT8* buffer = (UINT8*)bs_alloc(bufferSize);
  78. PIMAGEHLP_SYMBOL64 symbol = (PIMAGEHLP_SYMBOL64)buffer;
  79. symbol->SizeOfStruct = bufferSize;
  80. symbol->MaxNameLength = BS_MAX_STACKTRACE_NAME_BYTES;
  81. HANDLE hProcess = GetCurrentProcess();
  82. StringStream outputStream;
  83. for (UINT32 i = skip; i < numEntries; i++)
  84. {
  85. if (i > skip)
  86. outputStream << std::endl;
  87. DWORD64 funcAddress = rawStackTrace[i];
  88. // Output function name
  89. DWORD64 dummy;
  90. if (SymGetSymFromAddr64(hProcess, funcAddress, &dummy, symbol))
  91. outputStream << StringUtil::format("{0}() - ", symbol->Name);
  92. // Output file name and line
  93. IMAGEHLP_LINE64 lineData;
  94. lineData.SizeOfStruct = sizeof(lineData);
  95. String addressString = toString(funcAddress, 0, ' ', std::ios::hex);
  96. DWORD column;
  97. if (SymGetLineFromAddr64(hProcess, funcAddress, &column, &lineData))
  98. {
  99. Path filePath = lineData.FileName;
  100. outputStream << StringUtil::format("0x{0} File[{1}:{2} ({3})]", addressString,
  101. filePath.getFilename(), lineData.LineNumber, column);
  102. }
  103. else
  104. {
  105. outputStream << StringUtil::format("0x{0}", addressString);
  106. }
  107. // Output module name
  108. IMAGEHLP_MODULE64 moduleData;
  109. moduleData.SizeOfStruct = sizeof(moduleData);
  110. if (SymGetModuleInfo64(hProcess, funcAddress, &moduleData))
  111. {
  112. Path filePath = moduleData.ImageName;
  113. outputStream << StringUtil::format(" Module[{0}]", filePath.getFilename());
  114. }
  115. }
  116. bs_free(buffer);
  117. return outputStream.str();
  118. }
  119. typedef bool(WINAPI *EnumProcessModulesType)(HANDLE hProcess, HMODULE* lphModule, DWORD cb, LPDWORD lpcbNeeded);
  120. typedef DWORD(WINAPI *GetModuleBaseNameType)(HANDLE hProcess, HMODULE hModule, LPSTR lpBaseName, DWORD nSize);
  121. typedef DWORD(WINAPI *GetModuleFileNameExType)(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize);
  122. typedef bool(WINAPI *GetModuleInformationType)(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb);
  123. static DynLib* gPSAPILib = nullptr;
  124. static EnumProcessModulesType gEnumProcessModules;
  125. static GetModuleBaseNameType gGetModuleBaseName;
  126. static GetModuleFileNameExType gGetModuleFileNameEx;
  127. static GetModuleInformationType gGetModuleInformation;
  128. /**
  129. * @brief Dynamically load the PSAPI.dll and the required symbols, if not already loaded.
  130. */
  131. void win32_initPSAPI()
  132. {
  133. if (gPSAPILib != nullptr)
  134. return;
  135. gPSAPILib = bs_new<DynLib>("PSAPI.dll");
  136. gEnumProcessModules = (EnumProcessModulesType)gPSAPILib->getSymbol("EnumProcessModules");
  137. gGetModuleBaseName = (GetModuleBaseNameType)gPSAPILib->getSymbol("GetModuleFileNameExA");
  138. gGetModuleFileNameEx = (GetModuleFileNameExType)gPSAPILib->getSymbol("GetModuleBaseNameA");
  139. gGetModuleInformation = (GetModuleInformationType)gPSAPILib->getSymbol("GetModuleInformation");
  140. }
  141. /**
  142. * @brief Unloads the PSAPI.dll if is loaded.
  143. */
  144. void win32_unloadPSAPI()
  145. {
  146. if (gPSAPILib == nullptr)
  147. return;
  148. gPSAPILib->unload();
  149. bs_delete(gPSAPILib);
  150. gPSAPILib = nullptr;
  151. }
  152. static bool gSymbolsLoaded = false;
  153. /**
  154. * @brief Loads symbols for all modules in the current process. Loaded symbols allow the stack walker to retrieve
  155. * human readable method, file, module names and other information.
  156. */
  157. void win32_loadSymbols()
  158. {
  159. if (gSymbolsLoaded)
  160. return;
  161. HANDLE hProcess = GetCurrentProcess();
  162. UINT32 options = SymGetOptions();
  163. options |= SYMOPT_LOAD_LINES;
  164. options |= SYMOPT_EXACT_SYMBOLS;
  165. options |= SYMOPT_UNDNAME;
  166. options |= SYMOPT_FAIL_CRITICAL_ERRORS;
  167. options |= SYMOPT_NO_PROMPTS;
  168. SymSetOptions(options);
  169. if(!SymInitialize(hProcess, nullptr, false))
  170. {
  171. LOGERR("SymInitialize failed. Error code: " + toString((UINT32)GetLastError()));
  172. return;
  173. }
  174. DWORD bufferSize;
  175. gEnumProcessModules(hProcess, nullptr, 0, &bufferSize);
  176. HMODULE* modules = (HMODULE*)bs_alloc(bufferSize);
  177. gEnumProcessModules(hProcess, modules, bufferSize, &bufferSize);
  178. UINT32 numModules = bufferSize / sizeof(HMODULE);
  179. for (UINT32 i = 0; i < numModules; i++)
  180. {
  181. MODULEINFO moduleInfo;
  182. char moduleName[BS_MAX_STACKTRACE_NAME_BYTES];
  183. char imageName[BS_MAX_STACKTRACE_NAME_BYTES];
  184. gGetModuleInformation(hProcess, modules[i], &moduleInfo, sizeof(moduleInfo));
  185. gGetModuleFileNameEx(hProcess, modules[i], imageName, BS_MAX_STACKTRACE_NAME_BYTES);
  186. gGetModuleBaseName(hProcess, modules[i], moduleName, BS_MAX_STACKTRACE_NAME_BYTES);
  187. char pdbSearchPath[BS_MAX_STACKTRACE_NAME_BYTES];
  188. char* fileName = nullptr;
  189. GetFullPathNameA(moduleName, BS_MAX_STACKTRACE_NAME_BYTES, pdbSearchPath, &fileName);
  190. *fileName = '\0';
  191. SymSetSearchPath(GetCurrentProcess(), pdbSearchPath);
  192. DWORD64 moduleAddress = SymLoadModule64(hProcess, modules[i], imageName, moduleName, (DWORD64)moduleInfo.lpBaseOfDll,
  193. (DWORD)moduleInfo.SizeOfImage);
  194. if (moduleAddress != 0)
  195. {
  196. IMAGEHLP_MODULE64 imageInfo;
  197. memset(&imageInfo, 0, sizeof(imageInfo));
  198. imageInfo.SizeOfStruct = sizeof(imageInfo);
  199. if(!SymGetModuleInfo64(GetCurrentProcess(), moduleAddress, &imageInfo))
  200. {
  201. LOGWRN("Failed retrieving module info for module: " + String(moduleName) + ". Error code: " + toString((UINT32)GetLastError()));
  202. }
  203. else
  204. {
  205. // Disabled because too much spam in the log, enable as needed
  206. #if 0
  207. if (imageInfo.SymType == SymNone)
  208. LOGWRN("Failed loading symbols for module: " + String(moduleName));
  209. #endif
  210. }
  211. }
  212. else
  213. {
  214. LOGWRN("Failed loading module " + String(moduleName) + ".Error code: " + toString((UINT32)GetLastError()) +
  215. ". Search path: " + String(pdbSearchPath) + ". Image name: " + String(imageName));
  216. }
  217. }
  218. bs_free(modules);
  219. gSymbolsLoaded = true;
  220. }
  221. /**
  222. * @brief Converts an exception record into a human readable error message.
  223. */
  224. String win32_getExceptionMessage(EXCEPTION_RECORD* record)
  225. {
  226. String exceptionAddress = toString((UINT64)record->ExceptionAddress, 0, ' ', std::ios::hex);
  227. String format;
  228. switch (record->ExceptionCode)
  229. {
  230. case EXCEPTION_ACCESS_VIOLATION:
  231. {
  232. DWORD_PTR violatedAddress = 0;
  233. if (record->NumberParameters == 2)
  234. {
  235. if (record->ExceptionInformation[0] == 0)
  236. format = "Unhandled exception at 0x{0}. Access violation reading 0x{1}.";
  237. else if (record->ExceptionInformation[0] == 8)
  238. format = "Unhandled exception at 0x{0}. Access violation DEP 0x{1}.";
  239. else
  240. format = "Unhandled exception at 0x{0}. Access violation writing 0x{1}.";
  241. violatedAddress = record->ExceptionInformation[1];
  242. }
  243. else
  244. format = "Unhandled exception at 0x{0}. Access violation.";
  245. String violatedAddressStr = toString((UINT64)violatedAddress, 0, ' ', std::ios::hex);
  246. return StringUtil::format(format, exceptionAddress, violatedAddressStr);
  247. }
  248. case EXCEPTION_IN_PAGE_ERROR:
  249. {
  250. DWORD_PTR violatedAddress = 0;
  251. DWORD_PTR code = 0;
  252. if (record->NumberParameters == 3)
  253. {
  254. if (record->ExceptionInformation[0] == 0)
  255. format = "Unhandled exception at 0x{0}. Page fault reading 0x{1} with code 0x{2}.";
  256. else if (record->ExceptionInformation[0] == 8)
  257. format = "Unhandled exception at 0x{0}. Page fault DEP 0x{1} with code 0x{2}.";
  258. else
  259. format = "Unhandled exception at 0x{0}. Page fault writing 0x{1} with code 0x{2}.";
  260. violatedAddress = record->ExceptionInformation[1];
  261. code = record->ExceptionInformation[3];
  262. }
  263. else
  264. format = "Unhandled exception at 0x{0}. Page fault.";
  265. String violatedAddressStr = toString((UINT64)violatedAddress, 0, ' ', std::ios::hex);
  266. String codeStr = toString((UINT64)code, 0, ' ', std::ios::hex);
  267. return StringUtil::format(format, exceptionAddress, violatedAddressStr, codeStr);
  268. }
  269. case STATUS_ARRAY_BOUNDS_EXCEEDED:
  270. {
  271. format = "Unhandled exception at 0x{0}. Attempting to access an out of range array element.";
  272. return StringUtil::format(format, exceptionAddress);
  273. }
  274. case EXCEPTION_DATATYPE_MISALIGNMENT:
  275. {
  276. format = "Unhandled exception at 0x{0}. Attempting to access missaligned data.";
  277. return StringUtil::format(format, exceptionAddress);
  278. }
  279. case EXCEPTION_FLT_DENORMAL_OPERAND:
  280. {
  281. format = "Unhandled exception at 0x{0}. Floating point operand too small.";
  282. return StringUtil::format(format, exceptionAddress);
  283. }
  284. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  285. {
  286. format = "Unhandled exception at 0x{0}. Floating point operation attempted to divide by zero.";
  287. return StringUtil::format(format, exceptionAddress);
  288. }
  289. case EXCEPTION_FLT_INVALID_OPERATION:
  290. {
  291. format = "Unhandled exception at 0x{0}. Floating point invalid operation.";
  292. return StringUtil::format(format, exceptionAddress);
  293. }
  294. case EXCEPTION_FLT_OVERFLOW:
  295. {
  296. format = "Unhandled exception at 0x{0}. Floating point overflow.";
  297. return StringUtil::format(format, exceptionAddress);
  298. }
  299. case EXCEPTION_FLT_UNDERFLOW:
  300. {
  301. format = "Unhandled exception at 0x{0}. Floating point underflow.";
  302. return StringUtil::format(format, exceptionAddress);
  303. }
  304. case EXCEPTION_FLT_STACK_CHECK:
  305. {
  306. format = "Unhandled exception at 0x{0}. Floating point stack overflow/underflow.";
  307. return StringUtil::format(format, exceptionAddress);
  308. }
  309. case EXCEPTION_ILLEGAL_INSTRUCTION:
  310. {
  311. format = "Unhandled exception at 0x{0}. Attempting to execute an illegal instruction.";
  312. return StringUtil::format(format, exceptionAddress);
  313. }
  314. case EXCEPTION_PRIV_INSTRUCTION:
  315. {
  316. format = "Unhandled exception at 0x{0}. Attempting to execute a private instruction.";
  317. return StringUtil::format(format, exceptionAddress);
  318. }
  319. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  320. {
  321. format = "Unhandled exception at 0x{0}. Integer operation attempted to divide by zero.";
  322. return StringUtil::format(format, exceptionAddress);
  323. }
  324. case EXCEPTION_INT_OVERFLOW:
  325. {
  326. format = "Unhandled exception at 0x{0}. Integer operation result has overflown.";
  327. return StringUtil::format(format, exceptionAddress);
  328. }
  329. case EXCEPTION_STACK_OVERFLOW:
  330. {
  331. format = "Unhandled exception at 0x{0}. Stack overflow.";
  332. return StringUtil::format(format, exceptionAddress);
  333. }
  334. default:
  335. {
  336. format = "Unhandled exception at 0x{0}. Code 0x{1}.";
  337. String exceptionCode = toString((UINT32)record->ExceptionCode, 0, ' ', std::ios::hex);
  338. return StringUtil::format(format, exceptionAddress, exceptionCode);
  339. }
  340. }
  341. }
  342. struct MiniDumpParams
  343. {
  344. Path filePath;
  345. EXCEPTION_POINTERS* exceptionData;
  346. };
  347. DWORD CALLBACK win32_writeMiniDumpWorker(void* data)
  348. {
  349. MiniDumpParams* params = (MiniDumpParams*)data;
  350. HANDLE hFile = CreateFileW(params->filePath.toWString().c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
  351. FILE_ATTRIBUTE_NORMAL, nullptr);
  352. if (hFile != INVALID_HANDLE_VALUE)
  353. {
  354. MINIDUMP_EXCEPTION_INFORMATION DumpExceptionInfo;
  355. DumpExceptionInfo.ThreadId = GetCurrentThreadId();
  356. DumpExceptionInfo.ExceptionPointers = params->exceptionData;
  357. DumpExceptionInfo.ClientPointers = false;
  358. MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal,
  359. &DumpExceptionInfo, nullptr, nullptr);
  360. CloseHandle(hFile);
  361. }
  362. return 0;
  363. }
  364. void win32_writeMiniDump(const Path& filePath, EXCEPTION_POINTERS* exceptionData)
  365. {
  366. MiniDumpParams param = { filePath, exceptionData };
  367. // Write minidump on a second thread in order to preserve the current thread's call stack
  368. DWORD threadId = 0;
  369. HANDLE hThread = CreateThread(nullptr, 0, &win32_writeMiniDumpWorker, &param, 0, &threadId);
  370. WaitForSingleObject(hThread, INFINITE);
  371. CloseHandle(hThread);
  372. }
  373. static const wchar_t* gMiniDumpName = L"minidump.dmp";
  374. const wchar_t* CrashHandler::CrashReportFolder = L"CrashReports/{0}/";
  375. const wchar_t* CrashHandler::CrashLogName = L"log.html";
  376. struct CrashHandler::Data
  377. {
  378. Mutex mutex;
  379. };
  380. CrashHandler::CrashHandler()
  381. {
  382. m = bs_new<Data>();
  383. }
  384. CrashHandler::~CrashHandler()
  385. {
  386. win32_unloadPSAPI();
  387. bs_delete(m);
  388. }
  389. void CrashHandler::reportCrash(const String& type, const String& description, const String& function,
  390. const String& file, UINT32 line) const
  391. {
  392. // Win32 debug methods are not thread safe
  393. Lock<>(m->mutex);
  394. String stackTrace = getStackTrace();
  395. StringStream errorMessageStream;
  396. errorMessageStream << "Fatal error occurred and the program has to terminate!" << std::endl;
  397. errorMessageStream << "\t\t" << type << " - " << description << std::endl;
  398. errorMessageStream << "\t\t in " << function << " [" << file << ":" << line << "]" << std::endl;
  399. errorMessageStream << std::endl;
  400. errorMessageStream << "Stack trace: " << std::endl;
  401. errorMessageStream << stackTrace;
  402. String errorMessage = errorMessageStream.str();
  403. gDebug().logError(errorMessage);
  404. Path crashFolder = getCrashFolder();
  405. FileSystem::createDir(crashFolder);
  406. gDebug().saveLog(crashFolder + WString(CrashLogName));
  407. win32_writeMiniDump(crashFolder + WString(gMiniDumpName), nullptr);
  408. WString simpleErrorMessage = L"Fatal error occurred and the program has to terminate! " \
  409. L"\n\nFor more information check the crash report located at:\n " + crashFolder.toWString();
  410. MessageBoxW(nullptr, simpleErrorMessage.c_str(), L"Banshee fatal error!", MB_OK);
  411. // Note: Potentially also log Windows Error Report and/or send crash data to server
  412. }
  413. int CrashHandler::reportCrash(void* exceptionDataPtr) const
  414. {
  415. EXCEPTION_POINTERS* exceptionData = (EXCEPTION_POINTERS*)exceptionDataPtr;
  416. // Win32 debug methods are not thread safe
  417. Lock<>(m->mutex);
  418. win32_initPSAPI();
  419. win32_loadSymbols();
  420. String stackTrace = win32_getStackTrace(*exceptionData->ContextRecord, 0);
  421. StringStream errorMessageStream;
  422. errorMessageStream << "Fatal error occurred and the program has to terminate!" << std::endl;
  423. errorMessageStream << "\t\t" << win32_getExceptionMessage(exceptionData->ExceptionRecord) << std::endl;;
  424. errorMessageStream << std::endl;
  425. errorMessageStream << "Stack trace: " << std::endl;
  426. errorMessageStream << stackTrace;
  427. String errorMessage = errorMessageStream.str();
  428. gDebug().logError(errorMessage);
  429. Path crashFolder = getCrashFolder();
  430. FileSystem::createDir(crashFolder);
  431. gDebug().saveLog(crashFolder + WString(CrashLogName));
  432. win32_writeMiniDump(crashFolder + WString(gMiniDumpName), exceptionData);
  433. WString simpleErrorMessage = L"Fatal error occurred and the program has to terminate! " \
  434. L"\n\nFor more information check the crash report located at:\n" + crashFolder.toWString();
  435. MessageBoxW(nullptr, simpleErrorMessage.c_str(), L"Banshee fatal error!", MB_OK);
  436. // Note: Potentially also log Windows Error Report and/or send crash data to server
  437. return EXCEPTION_EXECUTE_HANDLER;
  438. }
  439. Path CrashHandler::getCrashFolder() const
  440. {
  441. SYSTEMTIME systemTime;
  442. GetLocalTime(&systemTime);
  443. WString timeStamp = L"{0}{1}{2}_{3}{4}";
  444. WString strYear = toWString(systemTime.wYear, 4, '0');
  445. WString strMonth = toWString(systemTime.wMonth, 2, '0');
  446. WString strDay = toWString(systemTime.wDay, 2, '0');
  447. WString strHour = toWString(systemTime.wHour, 2, '0');
  448. WString strMinute = toWString(systemTime.wMinute, 2, '0');
  449. timeStamp = StringUtil::format(timeStamp, strYear, strMonth, strDay, strHour, strMinute);
  450. WString folderName = StringUtil::format(CrashReportFolder, timeStamp);
  451. return FileSystem::getWorkingDirectoryPath() + folderName;
  452. }
  453. String CrashHandler::getStackTrace()
  454. {
  455. CONTEXT context;
  456. RtlCaptureContext(&context);
  457. win32_initPSAPI();
  458. win32_loadSymbols();
  459. return win32_getStackTrace(context, 2);
  460. }
  461. CrashHandler& gCrashHandler()
  462. {
  463. return CrashHandler::instance();
  464. }
  465. }