|
21 | 21 |
|
22 | 22 | #include "yup_CrashHandling.h" |
23 | 23 |
|
24 | | -#if defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) || defined(__GLIBCPP__) |
25 | | -#include <cxxabi.h> |
26 | | -#include <execinfo.h> |
27 | | -#include <dlfcn.h> |
28 | | -#else |
29 | | -#include "yup_WindowsIncludes.h" |
30 | | -#endif |
31 | | - |
32 | 24 | namespace yup::Helpers |
33 | 25 | { |
34 | 26 |
|
35 | 27 | //============================================================================== |
36 | 28 |
|
37 | | -String getStackBacktrace() |
38 | | -{ |
39 | | - String result; |
40 | | - |
41 | | -#if YUP_WINDOWS |
42 | | - HANDLE process = GetCurrentProcess(); |
43 | | - SymInitialize (process, nullptr, TRUE); |
44 | | - |
45 | | - void* stack[128]; |
46 | | - int frames = static_cast<int> (CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr)); |
47 | | - |
48 | | - HeapBlock<SYMBOL_INFO> symbol; |
49 | | - symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1); |
50 | | - symbol->MaxNameLen = 255; |
51 | | - symbol->SizeOfStruct = sizeof (SYMBOL_INFO); |
52 | | - |
53 | | - for (int i = 0; i < frames; ++i) |
54 | | - { |
55 | | - DWORD64 displacement = 0; |
56 | | - |
57 | | - if (SymFromAddr (process, reinterpret_cast<DWORD64> (stack[i]), &displacement, symbol)) |
58 | | - { |
59 | | - result << i << ": "; |
60 | | - |
61 | | - IMAGEHLP_MODULE64 moduleInfo; |
62 | | - zerostruct (moduleInfo); |
63 | | - moduleInfo.SizeOfStruct = sizeof (moduleInfo); |
64 | | - |
65 | | - if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo)) |
66 | | - result << moduleInfo.ModuleName << ": "; |
67 | | - |
68 | | - result << symbol->Name << " + 0x" << String::toHexString (static_cast<int64> (displacement)) << newLine; |
69 | | - } |
70 | | - } |
71 | | - |
72 | | -#else |
73 | | - void* stack[128]; |
74 | | - auto frames = backtrace (stack, numElementsInArray (stack)); |
75 | | - char** frameStrings = backtrace_symbols (stack, frames); |
76 | | - |
77 | | - for (int i = 0; i < frames; ++i) |
78 | | - { |
79 | | - Dl_info info; |
80 | | - if (dladdr (stack[i], &info)) |
81 | | - { |
82 | | - int status = 0; |
83 | | - |
84 | | - std::unique_ptr<char, decltype (::free)*> demangled (abi::__cxa_demangle (info.dli_sname, nullptr, nullptr, &status), ::free); |
85 | | - if (status == 0) |
86 | | - { |
87 | | - result |
88 | | - << String (i).paddedRight (' ', 3) |
89 | | - << " " << File (String (info.dli_fname)).getFileName().paddedRight (' ', 35) |
90 | | - << " 0x" << String::toHexString (reinterpret_cast<size_t> (stack[i])).paddedLeft ('0', sizeof (void*) * 2) |
91 | | - << " " << demangled.get() |
92 | | - << " + " << (reinterpret_cast<char*> (stack[i]) - reinterpret_cast<char*> (info.dli_saddr)) << newLine; |
93 | | - |
94 | | - continue; |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - result << frameStrings[i] << newLine; |
99 | | - } |
100 | | - |
101 | | - ::free (frameStrings); |
102 | | -#endif |
103 | | - |
104 | | - return result; |
105 | | -} |
106 | | - |
107 | | -//============================================================================== |
108 | | - |
109 | 29 | void applicationCrashHandler ([[maybe_unused]] void* stackFrame) |
110 | 30 | { |
111 | | - Logger::getCurrentLogger()->outputDebugString (getStackBacktrace()); |
| 31 | + Logger::getCurrentLogger()->outputDebugString (SystemStats::getStackBacktrace()); |
112 | 32 | } |
113 | 33 |
|
114 | 34 | } // namespace yup::Helpers |
0 commit comments