[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] | 48 | void printInfo(int fd_out);
|
---|
| 49 |
|
---|
| 50 | CrashLogger::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 |
|
---|
| 81 | CrashLogger::~CrashLogger() {
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[6abfd07] | 84 | #ifdef WINDOWS
|
---|
| 85 |
|
---|
| 86 | bool 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 |
|
---|
| 115 | void 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 |
|
---|
| 134 | static 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;
|
---|
[4762301] | 160 | char* end_offset = NULL;
|
---|
[6abfd07] | 161 |
|
---|
| 162 | #ifdef MAC
|
---|
| 163 | for (char *p = symbollist[i]; *p; p++) {
|
---|
| 164 | if ((*p == '_') && (*(p-1) == ' ')) {
|
---|
| 165 | begin_name = p-1;
|
---|
[4762301] | 166 | } else if (*p == '+') {
|
---|
[6abfd07] | 167 | begin_offset = p-1;
|
---|
[d9b6a1c] | 168 | }
|
---|
[6abfd07] | 169 | }
|
---|
| 170 |
|
---|
[17f28a1] | 171 | write(fd_out, " ", 2);
|
---|
| 172 | if (begin_name && begin_offset && (begin_name < begin_offset)) {
|
---|
[6abfd07] | 173 | *begin_name++ = '\0';
|
---|
| 174 | *begin_offset++ = '\0';
|
---|
[d9b6a1c] | 175 |
|
---|
[6abfd07] | 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);
|
---|
[d9b6a1c] | 188 | } else {
|
---|
[6abfd07] | 189 | // demangling failed. Output function name as a C function with no arguments.
|
---|
[d9b6a1c] | 190 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
[6abfd07] | 191 | write(fd_out, " ", 1);
|
---|
| 192 | write(fd_out, begin_name, strlen(begin_name));
|
---|
| 193 | write(fd_out, "() ", 3);
|
---|
[4762301] | 194 | }
|
---|
| 195 | write(fd_out, begin_offset, strlen(begin_offset));
|
---|
[17f28a1] | 196 | } else {
|
---|
[4762301] | 197 | // couldn't parse the line? print the whole line.
|
---|
| 198 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
| 199 | }
|
---|
[17f28a1] | 200 | write(fd_out, "\n", 1);
|
---|
[4762301] | 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 |
|
---|
[17f28a1] | 212 | write(fd_out, " ", 2);
|
---|
[4762301] | 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));
|
---|
[17f28a1] | 232 | }
|
---|
| 233 |
|
---|
[4762301] | 234 | if (begin_offset) {
|
---|
| 235 | write(fd_out, " + ", 3);
|
---|
[6abfd07] | 236 | write(fd_out, begin_offset, strlen(begin_offset));
|
---|
[4762301] | 237 | } else {
|
---|
| 238 | write(fd_out, " ", 9);
|
---|
[d9b6a1c] | 239 | }
|
---|
[4762301] | 240 | write(fd_out, ") ", 1);
|
---|
| 241 | write(fd_out, end_offset, strlen(end_offset));
|
---|
[6abfd07] | 242 | } else {
|
---|
| 243 | // couldn't parse the line? print the whole line.
|
---|
[d9b6a1c] | 244 | write(fd_out, symbollist[i], strlen(symbollist[i]));
|
---|
[6abfd07] | 245 | }
|
---|
[17f28a1] | 246 | write(fd_out, "\n", 1);
|
---|
[6abfd07] | 247 | #endif
|
---|
[d9b6a1c] | 248 | }
|
---|
| 249 |
|
---|
[17f28a1] | 250 | free(funcname);
|
---|
[d9b6a1c] | 251 | free(symbollist);
|
---|
| 252 |
|
---|
| 253 | write(fd_out, "End of stack trace\n", 19);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[6abfd07] | 256 | #endif
|
---|
| 257 |
|
---|
| 258 | void 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);
|
---|
[d9b6a1c] | 270 | #else
|
---|
[6abfd07] | 271 | write(fd_out, "Unknown", 7);
|
---|
[d9b6a1c] | 272 | #endif
|
---|
[6abfd07] | 273 | write(fd_out, "\n", 1);
|
---|
[d9b6a1c] | 274 |
|
---|
[6abfd07] | 275 | write(fd_out, "\n", 1);
|
---|
[d9b6a1c] | 276 | }
|
---|