| 53 | // --- Crash dump file support --- |
| 54 | |
| 55 | bool begin_crash_dump() { |
| 56 | CreateDirectoryA(kCrashDumpDir, NULL); |
| 57 | // Extract executable basename |
| 58 | char exePath[MAX_PATH] = {0}; |
| 59 | GetModuleFileNameA(NULL, exePath, MAX_PATH); |
| 60 | const char* baseName = strrchr(exePath, '\\'); |
| 61 | if (!baseName) baseName = strrchr(exePath, '/'); |
| 62 | baseName = baseName ? baseName + 1 : exePath; |
| 63 | // Strip .exe / .dll extension |
| 64 | char cleanName[MAX_PATH]; |
| 65 | strncpy(cleanName, baseName, MAX_PATH - 1); |
| 66 | cleanName[MAX_PATH - 1] = '\0'; |
| 67 | char* dot = strrchr(cleanName, '.'); |
| 68 | if (dot) *dot = '\0'; |
| 69 | // Use executable name + PID for uniqueness |
| 70 | snprintf(crash_dump_path, sizeof(crash_dump_path), |
| 71 | "%s/crash_%s_%lu.txt", kCrashDumpDir, cleanName, GetCurrentProcessId()); |
| 72 | // Save original stdout fd |
| 73 | saved_stdout_fd = _dup(_fileno(stdout)); |
| 74 | if (saved_stdout_fd < 0) return false; |
| 75 | // Open dump file |
| 76 | crash_dump_fd = _open(crash_dump_path, |
| 77 | _O_WRONLY | _O_CREAT | _O_TRUNC, |
| 78 | 0666); |
| 79 | if (crash_dump_fd < 0) { |
| 80 | _close(saved_stdout_fd); |
| 81 | saved_stdout_fd = -1; |
| 82 | return false; |
| 83 | } |
| 84 | // Redirect stdout to crash dump file |
| 85 | _dup2(crash_dump_fd, _fileno(stdout)); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void end_crash_dump() { |
| 90 | fflush(stdout); |
no test coverage detected