Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | #include <array> |
| 2 | #include <cstdint> |
||
| 3 | #include <exception> |
||
| 4 | #include <stdexcept> |
||
| 5 | #include <windows.h> |
||
| 6 | #include <rpc.h> |
||
| 7 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 8 | #include <dbghelp.h> |
||
| 9 | #endif |
||
| 10 | #include "vers_id.h" |
||
| 11 | |||
| 12 | using path_buffer = std::array<wchar_t, MAX_PATH>; |
||
| 13 | void d_set_exception_handler(); |
||
| 14 | |||
| 15 | namespace { |
||
| 16 | |||
| 17 | class RAII_Windows_FILE_HANDLE |
||
| 18 | { |
||
| 19 | const HANDLE m_h; |
||
| 20 | public: |
||
| 21 | RAII_Windows_FILE_HANDLE(HANDLE &&h) : |
||
| 22 | m_h(h) |
||
| 23 | { |
||
| 24 | } |
||
| 25 | ~RAII_Windows_FILE_HANDLE() |
||
| 26 | { |
||
| 27 | if (*this) |
||
| 28 | CloseHandle(m_h); |
||
| 29 | } |
||
| 30 | RAII_Windows_FILE_HANDLE(const RAII_Windows_FILE_HANDLE &) = delete; |
||
| 31 | RAII_Windows_FILE_HANDLE &operator=(const RAII_Windows_FILE_HANDLE &) = delete; |
||
| 32 | explicit operator bool() const |
||
| 33 | { |
||
| 34 | return m_h != INVALID_HANDLE_VALUE; |
||
| 35 | } |
||
| 36 | operator HANDLE() const |
||
| 37 | { |
||
| 38 | return m_h; |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | |||
| 42 | class RAII_Windows_DynamicSharedObject |
||
| 43 | { |
||
| 44 | HMODULE m_h; |
||
| 45 | RAII_Windows_DynamicSharedObject(HMODULE &&h) : |
||
| 46 | m_h(h) |
||
| 47 | { |
||
| 48 | } |
||
| 49 | static RAII_Windows_DynamicSharedObject LoadInternal(std::array<wchar_t, MAX_PATH> &pathbuf, const unsigned lws, const wchar_t *const filename) |
||
| 50 | { |
||
| 51 | wcscpy(&pathbuf[lws], filename); |
||
| 52 | return LoadLibraryW(pathbuf.data()); |
||
| 53 | } |
||
| 54 | public: |
||
| 55 | ~RAII_Windows_DynamicSharedObject() |
||
| 56 | { |
||
| 57 | if (*this) |
||
| 58 | FreeLibrary(m_h); |
||
| 59 | } |
||
| 60 | RAII_Windows_DynamicSharedObject(const RAII_Windows_DynamicSharedObject &) = delete; |
||
| 61 | RAII_Windows_DynamicSharedObject &operator=(const RAII_Windows_DynamicSharedObject &) = delete; |
||
| 62 | RAII_Windows_DynamicSharedObject(RAII_Windows_DynamicSharedObject &&) = default; |
||
| 63 | explicit operator bool() const |
||
| 64 | { |
||
| 65 | return m_h; |
||
| 66 | } |
||
| 67 | /* |
||
| 68 | * This leaks the loaded library. It is used to ensure the library |
||
| 69 | * will still be loaded when the termination handler runs, long |
||
| 70 | * after this object has gone out of scope. |
||
| 71 | */ |
||
| 72 | void release() |
||
| 73 | { |
||
| 74 | m_h = nullptr; |
||
| 75 | } |
||
| 76 | template <std::size_t N> |
||
| 77 | static RAII_Windows_DynamicSharedObject Load(std::array<wchar_t, MAX_PATH> &pathbuf, const unsigned lws, const wchar_t (&filename)[N]) |
||
| 78 | { |
||
| 79 | if (lws >= MAX_PATH - N) |
||
| 80 | return nullptr; |
||
| 81 | return LoadInternal(pathbuf, lws, filename); |
||
| 82 | } |
||
| 83 | template <typename T> |
||
| 84 | T *GetProc(const char *const proc) const |
||
| 85 | { |
||
| 86 | union { |
||
| 87 | FARPROC gpa; |
||
| 88 | T *result; |
||
| 89 | }; |
||
| 90 | gpa = GetProcAddress(m_h, proc); |
||
| 91 | return result; |
||
| 92 | } |
||
| 93 | }; |
||
| 94 | |||
| 95 | struct dxx_trap_context |
||
| 96 | { |
||
| 97 | EXCEPTION_RECORD ExceptionRecord; |
||
| 98 | CONTEXT ContextRecord; |
||
| 99 | /* Pointer to the first inaccessible byte above the stack. */ |
||
| 100 | const uint8_t *end_sp; |
||
| 101 | }; |
||
| 102 | |||
| 103 | } |
||
| 104 | |||
| 105 | #define DXX_REPORT_TEXT_FORMAT_UUID "Format UUID: 0f7deda4-1122-4be8-97d5-afc91b7803d6" |
||
| 106 | #define DXX_REPORT_TEXT_LEADER_VERSION_PREFIX "Rebirth version: x" |
||
| 107 | #define DXX_REPORT_TEXT_LEADER_BUILD_DATETIME "Rebirth built: " |
||
| 108 | #define DXX_REPORT_TEXT_LEADER_EXCEPTION_MESSAGE "Exception message: " |
||
| 109 | |||
| 110 | constexpr unsigned dump_stack_bytes = 2048 * sizeof(void *); |
||
| 111 | constexpr unsigned dump_stack_stride = 16; |
||
| 112 | |||
| 113 | /* |
||
| 114 | * This includes leading text describing the UUID, so it must be bigger |
||
| 115 | * than a bare UUID would require. |
||
| 116 | */ |
||
| 117 | static std::array<char, 54> g_strctxuuid; |
||
| 118 | /* |
||
| 119 | * These labels are defined in asm() statements to record the address at |
||
| 120 | * which special instructions are placed. The array size must match the |
||
| 121 | * size of the assembly instruction. See `vectored_exception_handler` |
||
| 122 | * for how these are used. |
||
| 123 | */ |
||
| 124 | extern const char dxx_rebirth_veh_ud2[2], dxx_rebirth_veh_sp[2]; |
||
| 125 | |||
| 126 | /* |
||
| 127 | * 64 bit registers use an R prefix; 32 bit registers use an E prefix. |
||
| 128 | * For this handler, they can be treated the same. Use a macro to |
||
| 129 | * expand to the proper name. |
||
| 130 | * |
||
| 131 | * Win32 uses a leading underscore on names, relative to the C name. |
||
| 132 | * Win64 does not. Use a macro to cover that difference. |
||
| 133 | */ |
||
| 134 | #ifdef WIN64 |
||
| 135 | #define DXX_NT_CONTEXT_REGISTER(n) R##n |
||
| 136 | #define DXX_ASM_LABEL(L) "" L |
||
| 137 | #define DXX_WINDOWS_HOST_ARCH_W L"64" |
||
| 138 | #define DXX_REPORT_TEXT_LEADER_VERSION DXX_REPORT_TEXT_LEADER_VERSION_PREFIX "64" |
||
| 139 | #else |
||
| 140 | #define DXX_NT_CONTEXT_REGISTER(n) E##n |
||
| 141 | #define DXX_ASM_LABEL(L) "_" L |
||
| 142 | #define DXX_WINDOWS_HOST_ARCH_W L"86" |
||
| 143 | #define DXX_REPORT_TEXT_LEADER_VERSION DXX_REPORT_TEXT_LEADER_VERSION_PREFIX "86" |
||
| 144 | #endif |
||
| 145 | |||
| 146 | static LONG CALLBACK vectored_exception_handler(EXCEPTION_POINTERS *const ep) |
||
| 147 | { |
||
| 148 | const auto ctx = ep->ContextRecord; |
||
| 149 | auto &ip = ctx->DXX_NT_CONTEXT_REGISTER(ip); |
||
| 150 | auto &ax = ctx->DXX_NT_CONTEXT_REGISTER(ax); |
||
| 151 | const auto t = reinterpret_cast<dxx_trap_context *>(ax); |
||
| 152 | /* |
||
| 153 | * If the fault address is dxx_rebirth_veh_ud2, this is an expected |
||
| 154 | * fault forced solely to capture register context. This fault must |
||
| 155 | * always happen. |
||
| 156 | */ |
||
| 157 | if (ip == reinterpret_cast<uintptr_t>(dxx_rebirth_veh_ud2)) |
||
| 158 | { |
||
| 159 | /* |
||
| 160 | * Copy the captured data into the faulting function's local |
||
| 161 | * variable. The address of that local was loaded into ax before |
||
| 162 | * triggering the fault. |
||
| 163 | */ |
||
| 164 | t->ExceptionRecord = *ep->ExceptionRecord; |
||
| 165 | t->ContextRecord = *ctx; |
||
| 166 | /* Step past the faulting instruction. */ |
||
| 167 | ip += sizeof(dxx_rebirth_veh_ud2); |
||
| 168 | return EXCEPTION_CONTINUE_EXECUTION; |
||
| 169 | } |
||
| 170 | /* |
||
| 171 | * If the fault address is dxx_rebirth_veh_sp, this is an expected |
||
| 172 | * fault triggered by scanning up off the top of the stack. This |
||
| 173 | * fault usually happens, but might not if the exception happened |
||
| 174 | * with a sufficiently deep stack. |
||
| 175 | */ |
||
| 176 | else if (ip == reinterpret_cast<uintptr_t>(dxx_rebirth_veh_sp)) |
||
| 177 | { |
||
| 178 | /* |
||
| 179 | * The faulting function arranged for ax to hold the terminating |
||
| 180 | * address of the search and for bx to hold the currently tested |
||
| 181 | * address. When the fault happens, bx points to an |
||
| 182 | * inaccessible byte. Set ax to point to that byte. Decrement |
||
| 183 | * bx to counter the guaranteed increment in the faulting |
||
| 184 | * function. The combination of these changes ensures that the |
||
| 185 | * (current != end) test fails, terminating the loop without |
||
| 186 | * provoking an additional fault. |
||
| 187 | */ |
||
| 188 | ax = ctx->DXX_NT_CONTEXT_REGISTER(bx)--; |
||
| 189 | /* Step past the faulting instruction. */ |
||
| 190 | ip += sizeof(dxx_rebirth_veh_sp); |
||
| 191 | return EXCEPTION_CONTINUE_EXECUTION; |
||
| 192 | } |
||
| 193 | return EXCEPTION_CONTINUE_SEARCH; |
||
| 194 | } |
||
| 195 | |||
| 196 | /* Ensure gcc does not clone the inline labels */ |
||
| 197 | __attribute__((__noinline__,__noclone__,__warn_unused_result__)) |
||
| 198 | static void *capture_exception_context(dxx_trap_context &dtc, const uint8_t *const sp) |
||
| 199 | { |
||
| 200 | const auto handler = AddVectoredExceptionHandler(1, &vectored_exception_handler); |
||
| 201 | if (handler) |
||
| 202 | { |
||
| 203 | dtc = {}; |
||
| 204 | /* |
||
| 205 | * This block guarantees at least one and at most two faults to |
||
| 206 | * occur. Run it only if an exception handler was successfully |
||
| 207 | * added to catch these faults. |
||
| 208 | */ |
||
| 209 | asm volatile( |
||
| 210 | DXX_ASM_LABEL("dxx_rebirth_veh_ud2") ":\n" |
||
| 211 | /* |
||
| 212 | * Force a fault by executing a guaranteed undefined instruction. The |
||
| 213 | * fault handler will capture register context for a minidump, then step |
||
| 214 | * past this instruction. |
||
| 215 | * |
||
| 216 | * Before the fault, store the address of the `dxx_trap_context` |
||
| 217 | * instance `dtc` into ax for easy access by the fault handler. The |
||
| 218 | * fault handler then initializes dtc from the captured data. Mark |
||
| 219 | * memory as clobbered to prevent any speculative caching of `dtc` |
||
| 220 | * across the asm block. A narrower clobber could be used, but "memory" |
||
| 221 | * is sufficient and this code is not performance critical. |
||
| 222 | */ |
||
| 223 | " ud2\n" |
||
| 224 | :: "a" (&dtc) : "memory" |
||
| 225 | ); |
||
| 226 | auto p = reinterpret_cast<uintptr_t>(sp); |
||
| 227 | for (auto e = p + dump_stack_bytes; p != e; ++p) |
||
| 228 | asm volatile( |
||
| 229 | DXX_ASM_LABEL("dxx_rebirth_veh_sp") ":\n" |
||
| 230 | /* |
||
| 231 | * Force a read of the address pointed at by [sp]. This must be done in |
||
| 232 | * assembly, not through a volatile C read, because the vectored |
||
| 233 | * exception handler must know the specific address of the read |
||
| 234 | * instruction. |
||
| 235 | * |
||
| 236 | * This may fault if the stack is not sufficiently deep. If a fault |
||
| 237 | * happens, the fault handler will adjust `e` and `p`, then step past |
||
| 238 | * the compare (although stepping past is not strictly necessary). On |
||
| 239 | * resume from the fault, the loop will find that the adjusted values |
||
| 240 | * make `++p` == `e`, causing the loop to terminate. |
||
| 241 | */ |
||
| 242 | " cmpb %%al,(%[sp])\n" |
||
| 243 | : "+a" (e), |
||
| 244 | [sp] "+b" (p) |
||
| 245 | :: "cc" |
||
| 246 | ); |
||
| 247 | /* |
||
| 248 | * Save the address of the first inaccessible byte, rounded down |
||
| 249 | * to the nearest paragraph. |
||
| 250 | */ |
||
| 251 | dtc.end_sp = reinterpret_cast<const uint8_t *>(p & -dump_stack_stride); |
||
| 252 | RemoveVectoredExceptionHandler(handler); |
||
| 253 | } |
||
| 254 | return handler; |
||
| 255 | } |
||
| 256 | #undef DXX_ASM_LABEL |
||
| 257 | |||
| 258 | /* |
||
| 259 | * Initialize `path` with a path suitable to be used to write a minidump |
||
| 260 | * for the exception. Initialize `filename` to point to the first |
||
| 261 | * character in the filename (bypassing the directories). |
||
| 262 | */ |
||
| 263 | static unsigned prepare_exception_log_path(path_buffer &path, wchar_t *&filename, const unsigned pid) |
||
| 264 | { |
||
| 265 | const auto l = GetTempPathW(path.size(), path.data()); |
||
| 266 | SYSTEMTIME st{}; |
||
| 267 | GetSystemTime(&st); |
||
| 268 | /* |
||
| 269 | * Subsecond precision is not required. The program will terminate |
||
| 270 | * after writing this dump. No user is likely to be able to restart |
||
| 271 | * the game, and provoke a second dump, and on the same PID, within |
||
| 272 | * the same second. |
||
| 273 | */ |
||
| 274 | const auto r = l + _snwprintf(filename = path.data() + l, path.size() - l, L"%.4u-%.2u-%.2u-%.2u-%.2u-%.2u x" DXX_WINDOWS_HOST_ARCH_W L" P%u %hs.elog", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, pid, g_descent_version); |
||
| 275 | const auto extension = path.data() + r - 5; |
||
| 276 | wchar_t *p; |
||
| 277 | /* |
||
| 278 | * NTFS does not allow star in filenames. The expected content of |
||
| 279 | * this part of the filename will never contain an 's', so replace |
||
| 280 | * '*' with 's'. The filename will contain at most one star, but |
||
| 281 | * possibly none if the version string reported no unstaged changes. |
||
| 282 | * The position of the star varies depending on whether the version |
||
| 283 | * string also included a plus to report staged uncommitted changes. |
||
| 284 | */ |
||
| 285 | if (*(p = &extension[-1]) == '*' || |
||
| 286 | *(p = &extension[-2]) == '*') |
||
| 287 | *p = 's'; |
||
| 288 | return r; |
||
| 289 | } |
||
| 290 | |||
| 291 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 292 | using MiniDumpWriteDump_type = decltype(MiniDumpWriteDump); |
||
| 293 | static MiniDumpWriteDump_type *g_pMiniDumpWriteDump; |
||
| 294 | |||
| 295 | /* |
||
| 296 | * Write a minidump to the open file `h`, reported as being from pid |
||
| 297 | * `pid` with context from `dtc`. If `what` is not empty, include it as |
||
| 298 | * a comment stream. The caller uses this to pass the exception text |
||
| 299 | * (as returned by `std::exception::what()`) so that it will be easy to |
||
| 300 | * find in the debugger. |
||
| 301 | */ |
||
| 302 | static void write_exception_dump(dxx_trap_context &dtc, const unsigned pid, const std::string &what, const HANDLE h, const MiniDumpWriteDump_type *const pMiniDumpWriteDump) |
||
| 303 | { |
||
| 304 | MINIDUMP_EXCEPTION_INFORMATION mei; |
||
| 305 | MINIDUMP_USER_STREAM_INFORMATION musi; |
||
| 306 | EXCEPTION_POINTERS ep; |
||
| 307 | ep.ContextRecord = &dtc.ContextRecord; |
||
| 308 | ep.ExceptionRecord = &dtc.ExceptionRecord; |
||
| 309 | mei.ThreadId = GetCurrentThreadId(); |
||
| 310 | mei.ExceptionPointers = &ep; |
||
| 311 | mei.ClientPointers = 0; |
||
| 312 | std::array<MINIDUMP_USER_STREAM, 5> mus; |
||
| 313 | std::array<char, 92> musTextVersion; |
||
| 314 | std::array<char, 44> musTextDateTime; |
||
| 315 | std::array<char, 512> musTextExceptionMessage; |
||
| 316 | unsigned UserStreamCount = 0; |
||
| 317 | { |
||
| 318 | auto &m = mus[UserStreamCount++]; |
||
| 319 | m.Type = MINIDUMP_STREAM_TYPE::CommentStreamA; |
||
| 320 | static char leader[] = DXX_REPORT_TEXT_FORMAT_UUID; |
||
| 321 | m.BufferSize = sizeof(leader); |
||
| 322 | m.Buffer = leader; |
||
| 323 | } |
||
| 324 | { |
||
| 325 | auto &m = mus[UserStreamCount++]; |
||
| 326 | m.Type = MINIDUMP_STREAM_TYPE::CommentStreamA; |
||
| 327 | m.BufferSize = g_strctxuuid.size(); |
||
| 328 | m.Buffer = g_strctxuuid.data(); |
||
| 329 | } |
||
| 330 | { |
||
| 331 | auto &m = mus[UserStreamCount++]; |
||
| 332 | m.Type = MINIDUMP_STREAM_TYPE::CommentStreamA; |
||
| 333 | m.Buffer = musTextVersion.data(); |
||
| 334 | m.BufferSize = 1 + std::snprintf(musTextVersion.data(), musTextVersion.size(), DXX_REPORT_TEXT_LEADER_VERSION " %s", g_descent_version); |
||
| 335 | } |
||
| 336 | { |
||
| 337 | auto &m = mus[UserStreamCount++]; |
||
| 338 | m.Type = MINIDUMP_STREAM_TYPE::CommentStreamA; |
||
| 339 | m.Buffer = musTextDateTime.data(); |
||
| 340 | m.BufferSize = 1 + std::snprintf(musTextDateTime.data(), musTextDateTime.size(), DXX_REPORT_TEXT_LEADER_BUILD_DATETIME "%s", g_descent_build_datetime); |
||
| 341 | } |
||
| 342 | if (!what.empty()) |
||
| 343 | { |
||
| 344 | auto &m = mus[UserStreamCount++]; |
||
| 345 | m.Type = MINIDUMP_STREAM_TYPE::CommentStreamA; |
||
| 346 | m.Buffer = musTextExceptionMessage.data(); |
||
| 347 | m.BufferSize = 1 + std::snprintf(musTextExceptionMessage.data(), musTextExceptionMessage.size(), DXX_REPORT_TEXT_LEADER_EXCEPTION_MESSAGE "%s", what.c_str()); |
||
| 348 | } |
||
| 349 | musi.UserStreamCount = UserStreamCount; |
||
| 350 | musi.UserStreamArray = mus.data(); |
||
| 351 | (*pMiniDumpWriteDump)(GetCurrentProcess(), pid, h, MiniDumpWithFullMemory, &mei, &musi, nullptr); |
||
| 352 | } |
||
| 353 | |||
| 354 | /* |
||
| 355 | * Open the file named by `path` for write. On success, call |
||
| 356 | * `write_exception_dump` above with that handle. On failure, clear the |
||
| 357 | * path so that the later UI code does not tell the user about a file |
||
| 358 | * that does not exist. |
||
| 359 | */ |
||
| 360 | static void write_exception_dump(dxx_trap_context &dtc, const unsigned pid, const std::string &what, path_buffer &path, const MiniDumpWriteDump_type *const pMiniDumpWriteDump) |
||
| 361 | { |
||
| 362 | if (RAII_Windows_FILE_HANDLE h{CreateFileW(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)}) |
||
| 363 | write_exception_dump(dtc, pid, what, h, pMiniDumpWriteDump); |
||
| 364 | else |
||
| 365 | path.front() = 0; |
||
| 366 | } |
||
| 367 | #endif |
||
| 368 | |||
| 369 | /* |
||
| 370 | * Write a plain text dump of metadata and a hex dump of the faulting |
||
| 371 | * stack. |
||
| 372 | */ |
||
| 373 | static void write_exception_stack(const wchar_t *const filename, const unsigned pid, const uint8_t *const begin_sp, const dxx_trap_context &dtc, const std::string &what, const HANDLE h) |
||
| 374 | { |
||
| 375 | std::array<char, 4096> buf; |
||
| 376 | buf = {}; |
||
| 377 | const auto len_header_text = std::snprintf(buf.data(), buf.size(), |
||
| 378 | "Rebirth exception printed context\n" |
||
| 379 | DXX_REPORT_TEXT_FORMAT_UUID "\n" |
||
| 380 | "%.50s\n" |
||
| 381 | DXX_REPORT_TEXT_LEADER_VERSION " %s\n" |
||
| 382 | DXX_REPORT_TEXT_LEADER_BUILD_DATETIME "%s\n" |
||
| 383 | "Rebirth PID: %u\n" |
||
| 384 | "Report date-time: %.19ls\n" |
||
| 385 | DXX_REPORT_TEXT_LEADER_EXCEPTION_MESSAGE "\"%s\"\n" |
||
| 386 | "UD2 IP: %p\n" |
||
| 387 | "SP: %p\n" |
||
| 388 | "\n" |
||
| 389 | , g_strctxuuid.data(), g_descent_version, g_descent_build_datetime, pid, filename, what.c_str(), reinterpret_cast<const uint8_t *>(dtc.ContextRecord.DXX_NT_CONTEXT_REGISTER(ip)), begin_sp |
||
| 390 | ); |
||
| 391 | /* |
||
| 392 | * end_sp is rounded down to a paragraph boundary. |
||
| 393 | * Round sp the same way so that there will exist an integer N such |
||
| 394 | * that (`sp` + (N * `dump_stack_stride`)) == `end_sp`. |
||
| 395 | */ |
||
| 396 | const auto sp = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(begin_sp) & -dump_stack_stride); |
||
| 397 | DWORD dwWritten; |
||
| 398 | WriteFile(h, buf.data(), len_header_text, &dwWritten, 0); |
||
| 399 | const auto end_sp = dtc.end_sp; |
||
| 400 | for (unsigned i = 0; i < dump_stack_bytes; i += dump_stack_stride) |
||
| 401 | { |
||
| 402 | char hexdump[dump_stack_stride + 1]; |
||
| 403 | const auto base_paragraph_pointer = &sp[i]; |
||
| 404 | if (base_paragraph_pointer == end_sp) |
||
| 405 | break; |
||
| 406 | hexdump[dump_stack_stride] = 0; |
||
| 407 | for (unsigned j = dump_stack_stride; j--;) |
||
| 408 | { |
||
| 409 | const uint8_t c = reinterpret_cast<const uint8_t *>(base_paragraph_pointer)[j]; |
||
| 410 | hexdump[j] = (c >= ' ' && c <= '~') ? c : '.'; |
||
| 411 | } |
||
| 412 | buf = {}; |
||
| 413 | #define FORMAT_PADDED_UINT8_4 " %.2x %.2x %.2x %.2x " |
||
| 414 | #define VAR_PADDED_UINT8_4(I) sp[I], sp[I + 1], sp[I + 2], sp[I + 3] |
||
| 415 | const auto len_line_text = std::snprintf(buf.data(), buf.size(), |
||
| 416 | "%p: " FORMAT_PADDED_UINT8_4 FORMAT_PADDED_UINT8_4 FORMAT_PADDED_UINT8_4 FORMAT_PADDED_UINT8_4 " %s\n" |
||
| 417 | , base_paragraph_pointer, VAR_PADDED_UINT8_4(i), VAR_PADDED_UINT8_4(i + 4), VAR_PADDED_UINT8_4(i + 8), VAR_PADDED_UINT8_4(i + 12), hexdump); |
||
| 418 | #undef VAR_PADDED_UINT8_4 |
||
| 419 | #undef FORMAT_PADDED_UINT8_4 |
||
| 420 | WriteFile(h, buf.data(), len_line_text, &dwWritten, 0); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | static void write_exception_stack(const wchar_t *const filename, const unsigned pid, const uint8_t *const sp, const dxx_trap_context &dtc, const std::string &what, path_buffer &path) |
||
| 425 | { |
||
| 426 | if (RAII_Windows_FILE_HANDLE h{CreateFileW(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)}) |
||
| 427 | write_exception_stack(filename, pid, sp, dtc, what, h); |
||
| 428 | else |
||
| 429 | path.front() = 0; |
||
| 430 | } |
||
| 431 | |||
| 432 | /* |
||
| 433 | * Prevent moving the large `dxx_trap_context` into the caller. |
||
| 434 | * The caller captures the stack pointer before this function begins. |
||
| 435 | * The captured value should exclude the locals of this function, so |
||
| 436 | * that the data printed from the captured value covers the stack used |
||
| 437 | * prior to the throw which triggered the terminate handler. If this |
||
| 438 | * function were inlined into the caller, the captured stack pointer |
||
| 439 | * would include the locals of this function. |
||
| 440 | */ |
||
| 441 | __attribute__((__noinline__,__noclone__)) |
||
| 442 | static void write_exception_logs(const uint8_t *const sp) |
||
| 443 | { |
||
| 444 | std::string what; |
||
| 445 | try { |
||
| 446 | /* |
||
| 447 | * Rethrow the faulting exception, then catch it to extract its |
||
| 448 | * explanatory text. |
||
| 449 | */ |
||
| 450 | std::rethrow_exception(std::current_exception()); |
||
| 451 | } catch (const std::exception &ee) { |
||
| 452 | what = ee.what(); |
||
| 453 | } catch (...) { |
||
| 454 | } |
||
| 455 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 456 | path_buffer path_dump; |
||
| 457 | #define DXX_PATH_DUMP_FORMAT_STRING L"%s%s\n" |
||
| 458 | #define DXX_PATH_DUMP_ARGUMENTS \ |
||
| 459 | , path_dump.front() ? L"If possible, make available the binary file:\n " : L"No dump file could be generated." \ |
||
| 460 | , path_dump.data() |
||
| 461 | #else |
||
| 462 | #define DXX_PATH_DUMP_FORMAT_STRING \ |
||
| 463 | L"Dump file generation is not enabled in this build.\n" |
||
| 464 | #define DXX_PATH_DUMP_ARGUMENTS |
||
| 465 | #endif |
||
| 466 | path_buffer path_stack; |
||
| 467 | dxx_trap_context dtc; |
||
| 468 | if (capture_exception_context(dtc, sp)) |
||
| 469 | { |
||
| 470 | const unsigned pid = GetCurrentProcessId(); |
||
| 471 | wchar_t *filename; |
||
| 472 | const auto pl = prepare_exception_log_path(path_stack, filename, pid); |
||
| 473 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 474 | path_dump = path_stack; |
||
| 475 | wcscpy(&path_dump[pl - 4], L"mdmp"); |
||
| 476 | if (const auto pMiniDumpWriteDump = g_pMiniDumpWriteDump) |
||
| 477 | write_exception_dump(dtc, pid, what, path_dump, pMiniDumpWriteDump); |
||
| 478 | else |
||
| 479 | path_dump.front() = 0; |
||
| 480 | #else |
||
| 481 | (void)pl; |
||
| 482 | #endif |
||
| 483 | write_exception_stack(filename, pid, sp, dtc, what, path_stack); |
||
| 484 | } |
||
| 485 | else |
||
| 486 | { |
||
| 487 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 488 | path_dump.front() = 0; |
||
| 489 | #endif |
||
| 490 | path_stack.front() = 0; |
||
| 491 | } |
||
| 492 | std::array<wchar_t, 1024> msg; |
||
| 493 | _snwprintf(msg.data(), msg.size(), |
||
| 494 | L"Rebirth encountered a fatal error. Please report this to the developers.\n" |
||
| 495 | L"\nInclude in your report:\n" |
||
| 496 | L"%s%hs%s" |
||
| 497 | L"* The level(s) played this session, including download URLs for any add-on missions\n" |
||
| 498 | L"%s%s\n\n" |
||
| 499 | DXX_PATH_DUMP_FORMAT_STRING |
||
| 500 | L"\nTo the extent possible, provide steps to reproduce, starting from the game main menu.", |
||
| 501 | what.empty() ? L"" : L"* The exception message text:\n \"", what.c_str(), what.empty() ? L"" : L"\"\n", |
||
| 502 | path_stack.front() ? L"* The contents of the text file:\n " : L"", path_stack.data() |
||
| 503 | DXX_PATH_DUMP_ARGUMENTS |
||
| 504 | ); |
||
| 505 | #undef DXX_PATH_DUMP_ARGUMENTS |
||
| 506 | #undef DXX_PATH_DUMP_FORMAT_STRING |
||
| 507 | MessageBoxW(NULL, msg.data(), L"Rebirth - Fatal Error", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); |
||
| 508 | } |
||
| 509 | |||
| 510 | [[noreturn]] |
||
| 511 | static void terminate_handler() |
||
| 512 | { |
||
| 513 | const uint8_t *sp; |
||
| 514 | asm( |
||
| 515 | #ifdef WIN64 |
||
| 516 | "movq %%rsp, %[sp]" |
||
| 517 | #else |
||
| 518 | "movl %%esp, %[sp]" |
||
| 519 | #endif |
||
| 520 | : [sp] "=rm" (sp)); |
||
| 521 | write_exception_logs(sp); |
||
| 522 | ExitProcess(0); |
||
| 523 | } |
||
| 524 | |||
| 525 | void d_set_exception_handler() |
||
| 526 | { |
||
| 527 | std::set_terminate(&terminate_handler); |
||
| 528 | std::array<wchar_t, MAX_PATH> ws; |
||
| 529 | if (const auto lws = GetSystemDirectoryW(ws.data(), ws.size())) |
||
| 530 | { |
||
| 531 | if (auto &&rpcrt4 = RAII_Windows_DynamicSharedObject::Load(ws, lws, L"\\rpcrt4.dll")) |
||
| 532 | { |
||
| 533 | if (const auto pUuidCreate = rpcrt4.GetProc<decltype(UuidCreate)>("UuidCreate")) |
||
| 534 | { |
||
| 535 | UUID ctxuuid; |
||
| 536 | /* |
||
| 537 | * Create a UUID specific to this run. |
||
| 538 | */ |
||
| 539 | if (SUCCEEDED((*pUuidCreate)(&ctxuuid))) |
||
| 540 | { |
||
| 541 | std::snprintf(g_strctxuuid.data(), g_strctxuuid.size(), "Context UUID: %08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", ctxuuid.Data1, ctxuuid.Data2, ctxuuid.Data3, ctxuuid.Data4[0], ctxuuid.Data4[1], ctxuuid.Data4[2], ctxuuid.Data4[3], ctxuuid.Data4[4], ctxuuid.Data4[5], ctxuuid.Data4[6], ctxuuid.Data4[7]); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | } |
||
| 545 | #ifdef DXX_ENABLE_WINDOWS_MINIDUMP |
||
| 546 | if (auto &&dbghelp = RAII_Windows_DynamicSharedObject::Load(ws, lws, L"\\dbghelp.dll")) |
||
| 547 | { |
||
| 548 | if ((g_pMiniDumpWriteDump = dbghelp.GetProc<decltype(MiniDumpWriteDump)>("MiniDumpWriteDump"))) |
||
| 549 | dbghelp.release(); |
||
| 550 | } |
||
| 551 | #endif |
||
| 552 | } |
||
| 553 | } |