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
RevLine 
[5529ab5]1#include "crash-logger.hpp"
[d9b6a1c]2
3#include <cstdlib>
4#include <cstdio>
5#include <csignal>
[6abfd07]6#include <cstring>
[d9b6a1c]7
8#include <fcntl.h>
9
[301d0d4]10#include "compiler.hpp"
[9546928]11#include "consts.hpp"
[d9b6a1c]12
[6abfd07]13// TODO: Double-check which includes are necessary
[d9b6a1c]14
[6abfd07]15#ifdef WINDOWS
[9d4456b]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
[d9b6a1c]20 #include <windows.h>
21 #include <io.h>
22 #include <sys/stat.h>
23
[6abfd07]24 #include "FileStackWalker.h"
25
[d9b6a1c]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
[6abfd07]33
34 bool handleException(unsigned int expCode, EXCEPTION_POINTERS* pExp, HANDLE thread);
[d9b6a1c]35#else
36 #include <unistd.h>
37 #include <execinfo.h>
38 #include <cxxabi.h>
[9d4456b]39
40 // CHeck if these are needed in Linux
41 //#include <errno.h>
42 //#include <cstring>
[6abfd07]43
44 void abortHandler(int signum);
45 static inline void printStackTrace(int fd_out = STDERR_FILENO);
[d9b6a1c]46#endif
47
[6abfd07]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
[d9b6a1c]69
[6abfd07]70 signal(SIGABRT, abortHandler);
71 signal(SIGSEGV, abortHandler);
72 signal(SIGILL, abortHandler);
73 signal(SIGFPE, abortHandler);
[d9b6a1c]74
[6abfd07]75 write(STDERR_FILENO, "Handlers attached\n", 18);
76
77 mainFunc(argc, argv);
78 #endif
[d9b6a1c]79}
80
81CrashLogger::~CrashLogger() {
82}
83
[6abfd07]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) {
[d9b6a1c]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
[4762301]141 unsigned int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
[d9b6a1c]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;
[17f28a1]152 char* funcname = (char*)malloc(sizeof(char) * funcnamesize);
[d9b6a1c]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
[4762301]157 for (unsigned int i = 0; i < addrlen; i++) {
[d9b6a1c]158 char* begin_name = NULL;
159 char* begin_offset = NULL;
[6abfd07]160
161#ifdef MAC
162 for (char *p = symbollist[i]; *p; p++) {
163 if ((*p == '_') && (*(p-1) == ' ')) {
164 begin_name = p-1;
[4762301]165 } else if (*p == '+') {
[6abfd07]166 begin_offset = p-1;
[d9b6a1c]167 }
[6abfd07]168 }
169
[17f28a1]170 write(fd_out, " ", 2);
171 if (begin_name && begin_offset && (begin_name < begin_offset)) {
[6abfd07]172 *begin_name++ = '\0';
173 *begin_offset++ = '\0';
[d9b6a1c]174
[6abfd07]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);
[d9b6a1c]187 } else {
[6abfd07]188 // demangling failed. Output function name as a C function with no arguments.
[d9b6a1c]189 write(fd_out, symbollist[i], strlen(symbollist[i]));
[6abfd07]190 write(fd_out, " ", 1);
191 write(fd_out, begin_name, strlen(begin_name));
192 write(fd_out, "() ", 3);
[4762301]193 }
194 write(fd_out, begin_offset, strlen(begin_offset));
[17f28a1]195 } else {
[4762301]196 // couldn't parse the line? print the whole line.
197 write(fd_out, symbollist[i], strlen(symbollist[i]));
198 }
[17f28a1]199 write(fd_out, "\n", 1);
[4762301]200#else
[87c8f1a]201 char* end_offset = NULL;
202
[4762301]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
[17f28a1]213 write(fd_out, " ", 2);
[4762301]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));
[17f28a1]233 }
234
[4762301]235 if (begin_offset) {
236 write(fd_out, " + ", 3);
[6abfd07]237 write(fd_out, begin_offset, strlen(begin_offset));
[4762301]238 } else {
239 write(fd_out, " ", 9);
[d9b6a1c]240 }
[4762301]241 write(fd_out, ") ", 1);
242 write(fd_out, end_offset, strlen(end_offset));
[6abfd07]243 } else {
244 // couldn't parse the line? print the whole line.
[d9b6a1c]245 write(fd_out, symbollist[i], strlen(symbollist[i]));
[6abfd07]246 }
[17f28a1]247 write(fd_out, "\n", 1);
[6abfd07]248#endif
[d9b6a1c]249 }
250
[17f28a1]251 free(funcname);
[d9b6a1c]252 free(symbollist);
253
254 write(fd_out, "End of stack trace\n", 19);
255}
256
[6abfd07]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);
[d9b6a1c]271 #else
[6abfd07]272 write(fd_out, "Unknown", 7);
[d9b6a1c]273 #endif
[6abfd07]274 write(fd_out, "\n", 1);
[d9b6a1c]275
[6abfd07]276 write(fd_out, "\n", 1);
[d9b6a1c]277}
Note: See TracBrowser for help on using the repository browser.