|
45 | 45 | # define WIN32_LEAN_AND_MEAN |
46 | 46 | # include <windows.h> |
47 | 47 | # include <dbghelp.h> |
| 48 | +# include <psapi.h> |
48 | 49 | # include <shlobj.h> |
49 | 50 | # pragma comment(lib, "dbghelp.lib") |
| 51 | +# pragma comment(lib, "psapi.lib") |
50 | 52 | # pragma comment(lib, "shell32.lib") |
51 | 53 | #else |
52 | 54 | # include <fcntl.h> |
@@ -75,6 +77,9 @@ constexpr std::size_t kRotateBytes = 10 * 1024 * 1024; |
75 | 77 | constexpr std::size_t kFrameReserve = 1024; |
76 | 78 | #ifdef _WIN32 |
77 | 79 | constexpr DWORD64 kSymDispMax = 256 * 1024; |
| 80 | +constexpr int kMaxModules = 64; |
| 81 | +constexpr std::size_t kRawStackBytes = 512; |
| 82 | +constexpr int kInsnBytes = 16; |
78 | 83 | #endif |
79 | 84 |
|
80 | 85 | #ifndef _WIN32 |
@@ -381,6 +386,8 @@ void writeHeader(CrashFile *cf, const char *kind) |
381 | 386 | appendStr(b, kSectionBufSize, &p, "\nPID : "); |
382 | 387 | #ifdef _WIN32 |
383 | 388 | appendU64(b, kSectionBufSize, &p, GetCurrentProcessId()); |
| 389 | + appendStr(b, kSectionBufSize, &p, "\nTID : "); |
| 390 | + appendU64(b, kSectionBufSize, &p, GetCurrentThreadId()); |
384 | 391 | #else |
385 | 392 | appendU64(b, kSectionBufSize, &p, static_cast<std::uint64_t>(getpid())); |
386 | 393 | #endif |
@@ -475,11 +482,175 @@ bool isFatalSehCode(DWORD code) |
475 | 482 | } |
476 | 483 | } |
477 | 484 |
|
| 485 | +void writeRegisters(CrashFile *cf, CONTEXT *ctx) |
| 486 | +{ |
| 487 | + std::size_t p = 0; |
| 488 | + char *b = g_section_buf; |
| 489 | + appendStr(b, kSectionBufSize, &p, "Registers:\n"); |
| 490 | + |
| 491 | +#if defined(_M_X64) || defined(__x86_64__) |
| 492 | + auto reg = [&](const char *name, DWORD64 val) { |
| 493 | + appendStr(b, kSectionBufSize, &p, " "); |
| 494 | + appendStr(b, kSectionBufSize, &p, name); |
| 495 | + appendStr(b, kSectionBufSize, &p, ": 0x"); |
| 496 | + appendHex64(b, kSectionBufSize, &p, val, 16); |
| 497 | + appendChar(b, kSectionBufSize, &p, '\n'); |
| 498 | + }; |
| 499 | + reg("RAX", ctx->Rax); reg("RBX", ctx->Rbx); |
| 500 | + reg("RCX", ctx->Rcx); reg("RDX", ctx->Rdx); |
| 501 | + reg("RSI", ctx->Rsi); reg("RDI", ctx->Rdi); |
| 502 | + reg("RBP", ctx->Rbp); reg("RSP", ctx->Rsp); |
| 503 | + reg("R8 ", ctx->R8); reg("R9 ", ctx->R9); |
| 504 | + reg("R10", ctx->R10); reg("R11", ctx->R11); |
| 505 | + reg("R12", ctx->R12); reg("R13", ctx->R13); |
| 506 | + reg("R14", ctx->R14); reg("R15", ctx->R15); |
| 507 | + reg("RIP", ctx->Rip); |
| 508 | +#elif defined(_M_ARM64) || defined(__aarch64__) |
| 509 | + appendStr(b, kSectionBufSize, &p, " (ARM64 register dump not implemented)\n"); |
| 510 | +#else |
| 511 | + appendStr(b, kSectionBufSize, &p, " (unsupported arch)\n"); |
| 512 | +#endif |
| 513 | + |
| 514 | + writeSection(cf, b, &p); |
| 515 | +} |
| 516 | + |
| 517 | +bool isMemoryReadable(const void *ptr, std::size_t len) |
| 518 | +{ |
| 519 | + MEMORY_BASIC_INFORMATION mbi{}; |
| 520 | + if (VirtualQuery(ptr, &mbi, sizeof(mbi)) == 0) return false; |
| 521 | + if (mbi.State != MEM_COMMIT) return false; |
| 522 | + const DWORD prot = mbi.Protect; |
| 523 | + if (prot & (PAGE_NOACCESS | PAGE_GUARD)) return false; |
| 524 | + const auto start = reinterpret_cast<std::uintptr_t>(ptr); |
| 525 | + const auto regionEnd = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress) + mbi.RegionSize; |
| 526 | + return (start + len) <= regionEnd; |
| 527 | +} |
| 528 | + |
| 529 | +void writeInsnBytes(CrashFile *cf, void *addr) |
| 530 | +{ |
| 531 | + std::size_t p = 0; |
| 532 | + char *b = g_section_buf; |
| 533 | + unsigned char insnBuf[kInsnBytes] = {0}; |
| 534 | + bool insnOk = false; |
| 535 | + |
| 536 | + if (addr && isMemoryReadable(addr, kInsnBytes)) { |
| 537 | + memcpy(insnBuf, addr, kInsnBytes); |
| 538 | + insnOk = true; |
| 539 | + } |
| 540 | + |
| 541 | + appendStr(b, kSectionBufSize, &p, "Insn : "); |
| 542 | + if (insnOk) { |
| 543 | + for (int i = 0; i < kInsnBytes; ++i) { |
| 544 | + if (i > 0) appendChar(b, kSectionBufSize, &p, ' '); |
| 545 | + appendHex64(b, kSectionBufSize, &p, insnBuf[i], 2); |
| 546 | + } |
| 547 | + } else { |
| 548 | + appendStr(b, kSectionBufSize, &p, "(unreadable)"); |
| 549 | + } |
| 550 | + appendChar(b, kSectionBufSize, &p, '\n'); |
| 551 | + writeSection(cf, b, &p); |
| 552 | +} |
| 553 | + |
| 554 | +void writeModules(CrashFile *cf) |
| 555 | +{ |
| 556 | + std::size_t p = 0; |
| 557 | + char *b = g_section_buf; |
| 558 | + appendStr(b, kSectionBufSize, &p, "Modules:\n"); |
| 559 | + writeSection(cf, b, &p); |
| 560 | + |
| 561 | + HANDLE process = GetCurrentProcess(); |
| 562 | + HMODULE mods[kMaxModules]; |
| 563 | + DWORD needed = 0; |
| 564 | + if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) |
| 565 | + return; |
| 566 | + |
| 567 | + const int count = static_cast<int>(needed / sizeof(HMODULE)); |
| 568 | + const int limit = count < kMaxModules ? count : kMaxModules; |
| 569 | + |
| 570 | + for (int i = 0; i < limit; ++i) { |
| 571 | + MODULEINFO mi{}; |
| 572 | + GetModuleInformation(process, mods[i], &mi, sizeof(mi)); |
| 573 | + |
| 574 | + char name[MAX_PATH] = {0}; |
| 575 | + GetModuleFileNameA(mods[i], name, MAX_PATH); |
| 576 | + |
| 577 | + // Extract just the filename from the full path |
| 578 | + const char *basename = name; |
| 579 | + for (const char *c = name; *c; ++c) { |
| 580 | + if (*c == '\\' || *c == '/') basename = c + 1; |
| 581 | + } |
| 582 | + |
| 583 | + p = 0; |
| 584 | + appendStr(b, kSectionBufSize, &p, " 0x"); |
| 585 | + appendHex64(b, kSectionBufSize, &p, |
| 586 | + reinterpret_cast<std::uint64_t>(mi.lpBaseOfDll), 16); |
| 587 | + appendChar(b, kSectionBufSize, &p, ' '); |
| 588 | + appendHex64(b, kSectionBufSize, &p, |
| 589 | + static_cast<std::uint64_t>(mi.SizeOfImage), 8); |
| 590 | + appendChar(b, kSectionBufSize, &p, ' '); |
| 591 | + appendStr(b, kSectionBufSize, &p, basename); |
| 592 | + appendChar(b, kSectionBufSize, &p, '\n'); |
| 593 | + |
| 594 | + if (kSectionBufSize - p < kFrameReserve) { |
| 595 | + writeSection(cf, b, &p); |
| 596 | + } |
| 597 | + } |
| 598 | + writeSection(cf, b, &p); |
| 599 | +} |
| 600 | + |
| 601 | +void writeRawStack(CrashFile *cf, CONTEXT *ctx) |
| 602 | +{ |
| 603 | +#if defined(_M_X64) || defined(__x86_64__) |
| 604 | + std::size_t p = 0; |
| 605 | + char *b = g_section_buf; |
| 606 | + appendStr(b, kSectionBufSize, &p, "Raw stack (512 bytes from RSP):\n"); |
| 607 | + writeSection(cf, b, &p); |
| 608 | + |
| 609 | + unsigned char stackBuf[kRawStackBytes] = {0}; |
| 610 | + bool ok = false; |
| 611 | + void *rspPtr = reinterpret_cast<void *>(ctx->Rsp); |
| 612 | + if (rspPtr && isMemoryReadable(rspPtr, kRawStackBytes)) { |
| 613 | + memcpy(stackBuf, rspPtr, kRawStackBytes); |
| 614 | + ok = true; |
| 615 | + } |
| 616 | + |
| 617 | + if (!ok) { |
| 618 | + p = 0; |
| 619 | + appendStr(b, kSectionBufSize, &p, " (unreadable)\n"); |
| 620 | + writeSection(cf, b, &p); |
| 621 | + return; |
| 622 | + } |
| 623 | + |
| 624 | + // Dump as 64-bit qwords, 8 per line |
| 625 | + const std::uint64_t *qwords = reinterpret_cast<const std::uint64_t *>(stackBuf); |
| 626 | + const int totalQwords = static_cast<int>(kRawStackBytes / 8); |
| 627 | + p = 0; |
| 628 | + for (int i = 0; i < totalQwords; ++i) { |
| 629 | + if (i % 8 == 0) appendStr(b, kSectionBufSize, &p, " "); |
| 630 | + appendStr(b, kSectionBufSize, &p, "0x"); |
| 631 | + appendHex64(b, kSectionBufSize, &p, qwords[i], 16); |
| 632 | + if (i % 8 == 7 || i == totalQwords - 1) { |
| 633 | + appendChar(b, kSectionBufSize, &p, '\n'); |
| 634 | + if (kSectionBufSize - p < kFrameReserve) { |
| 635 | + writeSection(cf, b, &p); |
| 636 | + } |
| 637 | + } else { |
| 638 | + appendChar(b, kSectionBufSize, &p, ' '); |
| 639 | + } |
| 640 | + } |
| 641 | + writeSection(cf, b, &p); |
| 642 | +#else |
| 643 | + (void)cf; (void)ctx; |
| 644 | +#endif |
| 645 | +} |
| 646 | + |
478 | 647 | void writeStackTraceWindows(CrashFile *cf, CONTEXT *ctx) |
479 | 648 | { |
480 | 649 | HANDLE process = GetCurrentProcess(); |
481 | 650 | HANDLE thread = GetCurrentThread(); |
482 | 651 |
|
| 652 | + SymRefreshModuleList(process); |
| 653 | + |
483 | 654 | STACKFRAME64 frame{}; |
484 | 655 | DWORD machine = 0; |
485 | 656 | #if defined(_M_X64) || defined(__x86_64__) |
@@ -633,8 +804,12 @@ bool writeReportSeh(EXCEPTION_POINTERS *info, const char *origin) |
633 | 804 | writeSection(&cf, b, &p); |
634 | 805 | } |
635 | 806 |
|
| 807 | + writeRegisters(&cf, info->ContextRecord); |
| 808 | + writeInsnBytes(&cf, info->ExceptionRecord->ExceptionAddress); |
636 | 809 | writeContextSection(&cf); |
| 810 | + writeModules(&cf); |
637 | 811 | writeStackTraceWindows(&cf, info->ContextRecord); |
| 812 | + writeRawStack(&cf, info->ContextRecord); |
638 | 813 | writeFooter(&cf); |
639 | 814 | cf.close(); |
640 | 815 | return true; |
|
0 commit comments