source: opengl-game/crash-logger.cpp

feature/imgui-sdl
Last change on this file was 87c8f1a, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In vaulkangame, define vertex buffer and index buffer data and transfer it to the gpu

  • 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
161#ifdef MAC
162 for (char *p = symbollist[i]; *p; p++) {
163 if ((*p == '_') && (*(p-1) == ' ')) {
164 begin_name = p-1;
165 } else if (*p == '+') {
166 begin_offset = p-1;
167 }
168 }
169
170 write(fd_out, " ", 2);
171 if (begin_name && begin_offset && (begin_name < begin_offset)) {
172 *begin_name++ = '\0';
173 *begin_offset++ = '\0';
174
175 // mangled name is now in [begin_name, begin_offset) and caller
176 // offset in [begin_offset, end_offset). now apply
177 // __cxa_demangle():
178 int status;
179 char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
180
181 if (status == 0) {
182 funcname = ret; // use possibly realloc()-ed string
183 write(fd_out, symbollist[i], strlen(symbollist[i]));
184 write(fd_out, " ", 1);
185 write(fd_out, funcname, strlen(funcname));
186 write(fd_out, " ", 1);
187 } else {
188 // demangling failed. Output function name as a C function with no arguments.
189 write(fd_out, symbollist[i], strlen(symbollist[i]));
190 write(fd_out, " ", 1);
191 write(fd_out, begin_name, strlen(begin_name));
192 write(fd_out, "() ", 3);
193 }
194 write(fd_out, begin_offset, strlen(begin_offset));
195 } else {
196 // couldn't parse the line? print the whole line.
197 write(fd_out, symbollist[i], strlen(symbollist[i]));
198 }
199 write(fd_out, "\n", 1);
200#else
201 char* end_offset = NULL;
202
203 for (char *p = symbollist[i]; *p; p++) {
204 if (*p == '(') {
205 begin_name = p;
206 } else if (*p == '+') {
207 begin_offset = p;
208 } else if (*p == ')' && (begin_offset || begin_name)) {
209 end_offset = p;
210 }
211 }
212
213 write(fd_out, " ", 2);
214 if (begin_name && end_offset && (begin_name < end_offset)) {
215 *begin_name++ = '\0';
216 *end_offset++ = '\0';
217 if (begin_offset) {
218 *begin_offset++ = '\0';
219 }
220
221 // mangled name is now in [begin_name, begin_offset) and caller
222 // offset in [begin_offset, end_offset). now apply
223 // __cxa_demangle():
224 int status;
225 char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
226
227 write(fd_out, symbollist[i], strlen(symbollist[i]));
228 write(fd_out, " ( ", 3);
229 if (status == 0) {
230 write(fd_out, ret, strlen(ret));
231 } else {
232 write(fd_out, begin_name, strlen(begin_name));
233 }
234
235 if (begin_offset) {
236 write(fd_out, " + ", 3);
237 write(fd_out, begin_offset, strlen(begin_offset));
238 } else {
239 write(fd_out, " ", 9);
240 }
241 write(fd_out, ") ", 1);
242 write(fd_out, end_offset, strlen(end_offset));
243 } else {
244 // couldn't parse the line? print the whole line.
245 write(fd_out, symbollist[i], strlen(symbollist[i]));
246 }
247 write(fd_out, "\n", 1);
248#endif
249 }
250
251 free(funcname);
252 free(symbollist);
253
254 write(fd_out, "End of stack trace\n", 19);
255}
256
257#endif
258
259void printInfo(int fd_out) {
260 write(fd_out, "Game Version: ", 14);
261 write(fd_out, GAME_VERSION, strlen(GAME_VERSION));
262 write(fd_out, "\n", 1);
263
264 write(fd_out, "OS: ", 4);
265 #if defined WINDOWS
266 write(fd_out, "Windows", 7);
267 #elif defined LINUX
268 write(fd_out, "Linux", 5);
269 #elif defined MAC
270 write(fd_out, "Mac", 3);
271 #else
272 write(fd_out, "Unknown", 7);
273 #endif
274 write(fd_out, "\n", 1);
275
276 write(fd_out, "\n", 1);
277}
Note: See TracBrowser for help on using the repository browser.