source: opengl-game/crash-logger.cpp@ 301d0d4

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

Rename Compiler.h to compiler.hpp

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