source: opengl-game/CrashLogger.cpp@ 972aca1

feature/imgui-sdl points-test
Last change on this file since 972aca1 was 17f28a1, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Improve the formatting for the crash log on OSX

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[d9b6a1c]1#include "CrashLogger.h"
2
3#include <cstdlib>
4#include <cstdio>
5#include <csignal>
[6abfd07]6#include <cstring>
[4762301]7#include <cstdint> // Check if this lets me remove any windows includes
[d9b6a1c]8
9#include <fcntl.h>
10
11#include "Compiler.h"
[6abfd07]12#include "Consts.h"
[d9b6a1c]13
[6abfd07]14// TODO: Double-check which includes are necessary
[d9b6a1c]15
[6abfd07]16#ifdef WINDOWS
[d9b6a1c]17 #include <windows.h>
18 #include <io.h>
19 #include <sys/stat.h>
20
[6abfd07]21 #include "FileStackWalker.h"
22
[d9b6a1c]23 // The windows analogues to the unix open, close, and write functions
24 // are _open, _close, and _write. These #defines let me use the same name in all cases.
25 #define open _open
26 #define close _close
27 #define write _write
28
29 #define STDERR_FILENO 2
[6abfd07]30
31 bool handleException(unsigned int expCode, EXCEPTION_POINTERS* pExp, HANDLE thread);
[d9b6a1c]32#else
33 #include <unistd.h>
34 #include <execinfo.h>
35 #include <errno.h>
36 #include <cxxabi.h>
37 #include <cstring>
[6abfd07]38
39 void abortHandler(int signum);
40 static inline void printStackTrace(int fd_out = STDERR_FILENO);
[d9b6a1c]41#endif
42
[6abfd07]43void printInfo(int fd_out);
44
45CrashLogger::CrashLogger(int(*mainFunc)(int, char*[]), int argc, char* argv[]) {
46 write(STDERR_FILENO, "Calling main\n", 13);
47
48 #ifdef WINDOWS
49 __try {
50 mainFunc(argc, argv);
51 // maybe do this and call a function inside CrashLogger
52 // In that case, also pass GetCurrentThread() as a parameter to then pass to handleException
53 // I could also move almost all of this into CrashLogger by creating a function in CrashLogger that takes a reference
54 // to the effective main function and, for Windows, wraps it in all this error-handling stuff
55 } __except (handleException(
56 GetExceptionCode(),
57 GetExceptionInformation(),
58 GetCurrentThread())
59 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_EXECUTE_HANDLER) {
60 }
61 #else
62 // Apparently, sigaction should be used instead for Linux
63 // It gives more info. Check if I should bother switching
[d9b6a1c]64
[6abfd07]65 signal(SIGABRT, abortHandler);
66 signal(SIGSEGV, abortHandler);
67 signal(SIGILL, abortHandler);
68 signal(SIGFPE, abortHandler);
[d9b6a1c]69
[6abfd07]70 write(STDERR_FILENO, "Handlers attached\n", 18);
71
72 mainFunc(argc, argv);
73 #endif
[d9b6a1c]74}
75
76CrashLogger::~CrashLogger() {
77}
78
[6abfd07]79#ifdef WINDOWS
80
81bool handleException(unsigned int expCode, EXCEPTION_POINTERS* pExp, HANDLE thread) {
82 int crash_log = open(CRASH_LOG_FILE, O_RDWR | O_CREAT | O_APPEND, _S_IREAD | _S_IWRITE);
83
84 if (crash_log == -1) {
85 // TODO: Figure out exactly what perror does and if I should use it
86 perror("opening crash.log"); // TODO: Figure out exactly what perror does and if I should use it
87 }
88
89 printInfo(crash_log);
90
91 FileStackWalker sw(crash_log == -1 ? STDERR_FILENO : crash_log);
92
93 if (pExp != NULL) {
94 sw.ShowCallstack(thread, pExp->ContextRecord);
95 } else {
96 write(crash_log, "Could not get the stack trace\n", 30);
97 }
98
99 close(crash_log);
100
101 if (expCode == EXCEPTION_ACCESS_VIOLATION) {
102 write(STDERR_FILENO, "ACCESS VIOLATION\n", 17);
103 }
104
105 return true;
106}
107
108#else
109
110void abortHandler(int signum) {
111 int crash_log = open(CRASH_LOG_FILE, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
112
113 if (crash_log == -1) {
114 // TODO: Figure out exactly what perror does and if I should use it
115 perror("opening crash.log");
116 }
117
118 printInfo(crash_log);
119
120 printStackTrace(crash_log);
121
122 close(crash_log);
123
124 write(STDERR_FILENO, "The game has crashed. Check crash.log for more info\n", 52);
125
126 exit(signum);
127}
128
129static inline void printStackTrace(int fd_out) {
[d9b6a1c]130 write(fd_out, "stack trace:\n", 13);
131
132 // storage array for stack trace address data
133 void* addrlist[64];
134
135 // retrieve current stack addresses
[4762301]136 unsigned int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
[d9b6a1c]137
138 if (addrlen == 0) {
139 write(fd_out, " \n", 3);
140 return;
141 }
142
143 // create readable strings to each frame.
144 char** symbollist = backtrace_symbols(addrlist, addrlen);
145
146 size_t funcnamesize = 1024;
[17f28a1]147 char* funcname = (char*)malloc(sizeof(char) * funcnamesize);
[d9b6a1c]148
149 // iterate over the returned symbol lines
150 // skip the first few, since those are printStackTrace, abortHandler,
151 // and a couple others called after the crash
[4762301]152 for (unsigned int i = 0; i < addrlen; i++) {
[d9b6a1c]153 char* begin_name = NULL;
154 char* begin_offset = NULL;
[4762301]155 char* end_offset = NULL;
[6abfd07]156
157#ifdef MAC
158 for (char *p = symbollist[i]; *p; p++) {
159 if ((*p == '_') && (*(p-1) == ' ')) {
160 begin_name = p-1;
[4762301]161 } else if (*p == '+') {
[6abfd07]162 begin_offset = p-1;
[d9b6a1c]163 }
[6abfd07]164 }
165
[17f28a1]166 write(fd_out, " ", 2);
167 if (begin_name && begin_offset && (begin_name < begin_offset)) {
[6abfd07]168 *begin_name++ = '\0';
169 *begin_offset++ = '\0';
[d9b6a1c]170
[6abfd07]171 // mangled name is now in [begin_name, begin_offset) and caller
172 // offset in [begin_offset, end_offset). now apply
173 // __cxa_demangle():
174 int status;
175 char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
176
177 if (status == 0) {
178 funcname = ret; // use possibly realloc()-ed string
179 write(fd_out, symbollist[i], strlen(symbollist[i]));
180 write(fd_out, " ", 1);
181 write(fd_out, funcname, strlen(funcname));
182 write(fd_out, " ", 1);
[d9b6a1c]183 } else {
[6abfd07]184 // demangling failed. Output function name as a C function with no arguments.
[d9b6a1c]185 write(fd_out, symbollist[i], strlen(symbollist[i]));
[6abfd07]186 write(fd_out, " ", 1);
187 write(fd_out, begin_name, strlen(begin_name));
188 write(fd_out, "() ", 3);
[4762301]189 }
190 write(fd_out, begin_offset, strlen(begin_offset));
[17f28a1]191 } else {
[4762301]192 // couldn't parse the line? print the whole line.
193 write(fd_out, symbollist[i], strlen(symbollist[i]));
194 }
[17f28a1]195 write(fd_out, "\n", 1);
[4762301]196#else
197 for (char *p = symbollist[i]; *p; p++) {
198 if (*p == '(') {
199 begin_name = p;
200 } else if (*p == '+') {
201 begin_offset = p;
202 } else if (*p == ')' && (begin_offset || begin_name)) {
203 end_offset = p;
204 }
205 }
206
[17f28a1]207 write(fd_out, " ", 2);
[4762301]208 if (begin_name && end_offset && (begin_name < end_offset)) {
209 *begin_name++ = '\0';
210 *end_offset++ = '\0';
211 if (begin_offset) {
212 *begin_offset++ = '\0';
213 }
214
215 // mangled name is now in [begin_name, begin_offset) and caller
216 // offset in [begin_offset, end_offset). now apply
217 // __cxa_demangle():
218 int status;
219 char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
220
221 write(fd_out, symbollist[i], strlen(symbollist[i]));
222 write(fd_out, " ( ", 3);
223 if (status == 0) {
224 write(fd_out, ret, strlen(ret));
225 } else {
226 write(fd_out, begin_name, strlen(begin_name));
[17f28a1]227 }
228
[4762301]229 if (begin_offset) {
230 write(fd_out, " + ", 3);
[6abfd07]231 write(fd_out, begin_offset, strlen(begin_offset));
[4762301]232 } else {
233 write(fd_out, " ", 9);
[d9b6a1c]234 }
[4762301]235 write(fd_out, ") ", 1);
236 write(fd_out, end_offset, strlen(end_offset));
[6abfd07]237 } else {
238 // couldn't parse the line? print the whole line.
[d9b6a1c]239 write(fd_out, symbollist[i], strlen(symbollist[i]));
[6abfd07]240 }
[17f28a1]241 write(fd_out, "\n", 1);
[6abfd07]242#endif
[d9b6a1c]243 }
244
[17f28a1]245 free(funcname);
[d9b6a1c]246 free(symbollist);
247
248 write(fd_out, "End of stack trace\n", 19);
249}
250
[6abfd07]251#endif
252
253void printInfo(int fd_out) {
254 write(fd_out, "Game Version: ", 14);
255 write(fd_out, GAME_VERSION, strlen(GAME_VERSION));
256 write(fd_out, "\n", 1);
257
258 write(fd_out, "OS: ", 4);
259 #if defined WINDOWS
260 write(fd_out, "Windows", 7);
261 #elif defined LINUX
262 write(fd_out, "Linux", 5);
263 #elif defined MAC
264 write(fd_out, "Mac", 3);
[d9b6a1c]265 #else
[6abfd07]266 write(fd_out, "Unknown", 7);
[d9b6a1c]267 #endif
[6abfd07]268 write(fd_out, "\n", 1);
[d9b6a1c]269
[6abfd07]270 write(fd_out, "\n", 1);
[d9b6a1c]271}
Note: See TracBrowser for help on using the repository browser.