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@…>, 6 years ago

Improve the formatting for the crash log on OSX

  • Property mode set to 100644
File size: 7.8 KB
Line 
1#include "CrashLogger.h"
2
3#include <cstdlib>
4#include <cstdio>
5#include <csignal>
6#include <cstring>
7#include <cstdint> // Check if this lets me remove any windows includes
8
9#include <fcntl.h>
10
11#include "Compiler.h"
12#include "Consts.h"
13
14// TODO: Double-check which includes are necessary
15
16#ifdef WINDOWS
17 #include <windows.h>
18 #include <io.h>
19 #include <sys/stat.h>
20
21 #include "FileStackWalker.h"
22
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
30
31 bool handleException(unsigned int expCode, EXCEPTION_POINTERS* pExp, HANDLE thread);
32#else
33 #include <unistd.h>
34 #include <execinfo.h>
35 #include <errno.h>
36 #include <cxxabi.h>
37 #include <cstring>
38
39 void abortHandler(int signum);
40 static inline void printStackTrace(int fd_out = STDERR_FILENO);
41#endif
42
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
64
65 signal(SIGABRT, abortHandler);
66 signal(SIGSEGV, abortHandler);
67 signal(SIGILL, abortHandler);
68 signal(SIGFPE, abortHandler);
69
70 write(STDERR_FILENO, "Handlers attached\n", 18);
71
72 mainFunc(argc, argv);
73 #endif
74}
75
76CrashLogger::~CrashLogger() {
77}
78
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) {
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
136 unsigned int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
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;
147 char* funcname = (char*)malloc(sizeof(char) * funcnamesize);
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
152 for (unsigned int i = 0; i < addrlen; i++) {
153 char* begin_name = NULL;
154 char* begin_offset = NULL;
155 char* end_offset = NULL;
156
157#ifdef MAC
158 for (char *p = symbollist[i]; *p; p++) {
159 if ((*p == '_') && (*(p-1) == ' ')) {
160 begin_name = p-1;
161 } else if (*p == '+') {
162 begin_offset = p-1;
163 }
164 }
165
166 write(fd_out, " ", 2);
167 if (begin_name && begin_offset && (begin_name < begin_offset)) {
168 *begin_name++ = '\0';
169 *begin_offset++ = '\0';
170
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);
183 } else {
184 // demangling failed. Output function name as a C function with no arguments.
185 write(fd_out, symbollist[i], strlen(symbollist[i]));
186 write(fd_out, " ", 1);
187 write(fd_out, begin_name, strlen(begin_name));
188 write(fd_out, "() ", 3);
189 }
190 write(fd_out, begin_offset, strlen(begin_offset));
191 } else {
192 // couldn't parse the line? print the whole line.
193 write(fd_out, symbollist[i], strlen(symbollist[i]));
194 }
195 write(fd_out, "\n", 1);
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
207 write(fd_out, " ", 2);
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));
227 }
228
229 if (begin_offset) {
230 write(fd_out, " + ", 3);
231 write(fd_out, begin_offset, strlen(begin_offset));
232 } else {
233 write(fd_out, " ", 9);
234 }
235 write(fd_out, ") ", 1);
236 write(fd_out, end_offset, strlen(end_offset));
237 } else {
238 // couldn't parse the line? print the whole line.
239 write(fd_out, symbollist[i], strlen(symbollist[i]));
240 }
241 write(fd_out, "\n", 1);
242#endif
243 }
244
245 free(funcname);
246 free(symbollist);
247
248 write(fd_out, "End of stack trace\n", 19);
249}
250
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);
265 #else
266 write(fd_out, "Unknown", 7);
267 #endif
268 write(fd_out, "\n", 1);
269
270 write(fd_out, "\n", 1);
271}
Note: See TracBrowser for help on using the repository browser.