[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] | 43 | void printInfo(int fd_out);
|
---|
| 44 |
|
---|
| 45 | CrashLogger::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 |
|
---|
| 76 | CrashLogger::~CrashLogger() {
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[6abfd07] | 79 | #ifdef WINDOWS
|
---|
| 80 |
|
---|
| 81 | bool 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 |
|
---|
| 110 | void 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 |
|
---|
| 129 | static 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;
|
---|
[4762301] | 147 | char funcname[1024];
|
---|
[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 |
|
---|
| 166 | if (begin_name && begin_offset && (begin_name < begin_offset )) {
|
---|
| 167 | *begin_name++ = '\0';
|
---|
| 168 | *begin_offset++ = '\0';
|
---|
[d9b6a1c] | 169 |
|
---|
[6abfd07] | 170 | // mangled name is now in [begin_name, begin_offset) and caller
|
---|
| 171 | // offset in [begin_offset, end_offset). now apply
|
---|
| 172 | // __cxa_demangle():
|
---|
| 173 | int status;
|
---|
| 174 | char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
|
---|
| 175 |
|
---|
| 176 | if (status == 0) {
|
---|
| 177 | funcname = ret; // use possibly realloc()-ed string
|
---|
[4762301] | 178 | write(fd_out, " ", 2);
|
---|
[6abfd07] | 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.
|
---|
[4762301] | 185 | write(fd_out, " ", 2);
|
---|
[d9b6a1c] | 186 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
[6abfd07] | 187 | write(fd_out, " ", 1);
|
---|
| 188 | write(fd_out, begin_name, strlen(begin_name));
|
---|
| 189 | write(fd_out, "() ", 3);
|
---|
[4762301] | 190 | }
|
---|
| 191 | write(fd_out, begin_offset, strlen(begin_offset));
|
---|
| 192 | write(fd_out, "\n", 1);
|
---|
| 193 | } else {`
|
---|
| 194 | // couldn't parse the line? print the whole line.
|
---|
| 195 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
| 196 | }
|
---|
| 197 | #else
|
---|
| 198 | for (char *p = symbollist[i]; *p; p++) {
|
---|
| 199 | if (*p == '(') {
|
---|
| 200 | begin_name = p;
|
---|
| 201 | } else if (*p == '+') {
|
---|
| 202 | begin_offset = p;
|
---|
| 203 | } else if (*p == ')' && (begin_offset || begin_name)) {
|
---|
| 204 | end_offset = p;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 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, " ", 2);
|
---|
| 222 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
| 223 | write(fd_out, " ( ", 3);
|
---|
| 224 | if (status == 0) {
|
---|
| 225 | write(fd_out, ret, strlen(ret));
|
---|
| 226 | } else {
|
---|
| 227 | write(fd_out, begin_name, strlen(begin_name));
|
---|
| 228 | }
|
---|
| 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));
|
---|
| 237 | write(fd_out, "\n", 1);
|
---|
[6abfd07] | 238 | } else {
|
---|
| 239 | // couldn't parse the line? print the whole line.
|
---|
[d9b6a1c] | 240 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
[6abfd07] | 241 | }
|
---|
| 242 | #endif
|
---|
[d9b6a1c] | 243 | }
|
---|
| 244 |
|
---|
| 245 | free(symbollist);
|
---|
| 246 |
|
---|
| 247 | write(fd_out, "End of stack trace\n", 19);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[6abfd07] | 250 | #endif
|
---|
| 251 |
|
---|
| 252 | void printInfo(int fd_out) {
|
---|
| 253 | write(fd_out, "Game Version: ", 14);
|
---|
| 254 | write(fd_out, GAME_VERSION, strlen(GAME_VERSION));
|
---|
| 255 | write(fd_out, "\n", 1);
|
---|
| 256 |
|
---|
| 257 | write(fd_out, "OS: ", 4);
|
---|
| 258 | #if defined WINDOWS
|
---|
| 259 | write(fd_out, "Windows", 7);
|
---|
| 260 | #elif defined LINUX
|
---|
| 261 | write(fd_out, "Linux", 5);
|
---|
| 262 | #elif defined MAC
|
---|
| 263 | write(fd_out, "Mac", 3);
|
---|
[d9b6a1c] | 264 | #else
|
---|
[6abfd07] | 265 | write(fd_out, "Unknown", 7);
|
---|
[d9b6a1c] | 266 | #endif
|
---|
[6abfd07] | 267 | write(fd_out, "\n", 1);
|
---|
[d9b6a1c] | 268 |
|
---|
[6abfd07] | 269 | write(fd_out, "\n", 1);
|
---|
[d9b6a1c] | 270 | }
|
---|