From f4301a1347c15891ce3335dbb8270bfb4c2b219d Mon Sep 17 00:00:00 2001 From: Pouare514 Date: Fri, 2 Oct 2026 22:11:01 +0200 Subject: [PATCH 1/2] feat(crashreport): dump all threads on fatal crash plus hang watchdog --- .../src/specifics/windows/CrashReport.cpp | 81 ++++++++++++++++++- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp index a4f388e1e..3c7121db4 100644 --- a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp +++ b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp @@ -1,5 +1,6 @@ #ifdef _WIN32 #include +#include #include "prx/libc/include/GuestArena.hpp" #include #include @@ -8,6 +9,7 @@ #include #include #include +#include #include #include #include "Sse4aEmulation.hpp" @@ -28,6 +30,8 @@ void Report(const char* format, ...) { WriteFile(GetStdHandle(STD_ERROR_HANDLE), buffer, static_cast(length), &written, nullptr); } +void ReportAllThreads(); + void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) { HMODULE module = nullptr; char path[MAX_PATH] = "?"; @@ -330,12 +334,80 @@ LONG WINAPI ReportCrash(EXCEPTION_POINTERS* info) { } } std::fflush(stderr); + ReportAllThreads(); + std::fflush(stderr); return EXCEPTION_CONTINUE_SEARCH; } +void ReportThreadContext(const CONTEXT* context, DWORD threadId, const char* name) { + char line[MAX_PATH + 64]; + Report(" thread %lu '%s': rip ", static_cast(threadId), name ? name : ""); + DescribeAddress(context->Rip, line, sizeof(line)); + Report("%s rsp 0x%016llx rbp 0x%016llx\n", line, static_cast(context->Rsp), static_cast(context->Rbp)); + std::uint64_t frame = context->Rbp; + for (int depth = 0; depth < 12 && frame != 0 && (frame & 7) == 0 && IsReadable(frame) && IsReadable(frame + 8); ++depth) { + const auto returnAddress = reinterpret_cast(frame)[1]; + if (!IsExecutable(returnAddress)) break; + DescribeAddress(returnAddress, line, sizeof(line)); + Report(" #%d %s\n", depth, line); + const auto next = reinterpret_cast(frame)[0]; + if (next <= frame) break; + frame = next; + } +} + +void ReportAllThreads() { + const DWORD current = GetCurrentThreadId(); + const DWORD pid = GetCurrentProcessId(); + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snapshot == INVALID_HANDLE_VALUE) return; + THREADENTRY32 entry{}; + entry.dwSize = sizeof(entry); + struct CapturedThread { + DWORD id; + char name[128]; + CONTEXT context; + }; + static CapturedThread captured[128]; + std::size_t capturedCount = 0; + if (Thread32First(snapshot, &entry)) { + do { + if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == current) continue; + if (capturedCount >= sizeof(captured) / sizeof(captured[0])) break; + HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, entry.th32ThreadID); + if (!thread) continue; + if (SuspendThread(thread) == static_cast(-1)) { + CloseHandle(thread); + continue; + } + CONTEXT context{}; + context.ContextFlags = CONTEXT_FULL; + const BOOL haveContext = GetThreadContext(thread, &context); + ResumeThread(thread); + char name[128] = ""; + PWSTR description = nullptr; + if (SUCCEEDED(GetThreadDescription(thread, &description)) && description) { + WideCharToMultiByte(CP_UTF8, 0, description, -1, name, sizeof(name), nullptr, nullptr); + LocalFree(description); + } + CloseHandle(thread); + if (!haveContext) continue; + captured[capturedCount].id = entry.th32ThreadID; + std::memcpy(captured[capturedCount].name, name, sizeof(captured[capturedCount].name)); + captured[capturedCount].context = context; + ++capturedCount; + } while (Thread32Next(snapshot, &entry)); + } + CloseHandle(snapshot); + Report(" all threads:\n"); + for (std::size_t i = 0; i < capturedCount; ++i) { + ReportThreadContext(&captured[i].context, captured[i].id, captured[i].name); + } +} + // abort() and the UCRT's invalid-parameter path end the process with a silent fast fail // (0xc0000409); both are reported with the calling thread's stack first. -void ReportBacktrace(const char* what) { +void ReportBacktrace(const char* what, bool reportThreads) { void* frames[48]; const auto count = RtlCaptureStackBackTrace(0, 48, frames, nullptr); Report("FATAL: %s on thread %lu\n", what, static_cast(GetCurrentThreadId())); @@ -344,22 +416,23 @@ void ReportBacktrace(const char* what) { DescribeAddress(reinterpret_cast(frames[i]), line, sizeof(line)); Report(" #%u %s\n", static_cast(i), line); } + if (reportThreads) ReportAllThreads(); std::fflush(stderr); } void AbortSignalHandler(int) { - ReportBacktrace("abort() (SIGABRT)"); + ReportBacktrace("abort() (SIGABRT)", true); } void InvalidParameterHandler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, std::uintptr_t) { - ReportBacktrace("UCRT invalid parameter"); + ReportBacktrace("UCRT invalid parameter", true); } // Static destruction runs on the thread that ends the process (ExitProcess -> DLL_PROCESS_DETACH), // so its stack names the exit's caller when no libc exit/abort path was taken. struct ExitReporter { ~ExitReporter() { - if (std::getenv("APS5_TRACE_EXIT") != nullptr) ReportBacktrace("process exit (static destruction)"); + if (std::getenv("APS5_TRACE_EXIT") != nullptr) ReportBacktrace("process exit (static destruction)", false); } } g_exitReporter; From dbe6b629229461bebec5ac50fc5640a2a74ac078 Mon Sep 17 00:00:00 2001 From: Pouare514 Date: Mon, 5 Oct 2026 00:57:04 +0200 Subject: [PATCH 2/2] fix(crashreport): capture thread stacks while suspended, report skipped threads --- core/libs/prx/libSceAudioIn/Export.cpp | 2 +- .../src/specifics/windows/CrashReport.cpp | 43 +++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/core/libs/prx/libSceAudioIn/Export.cpp b/core/libs/prx/libSceAudioIn/Export.cpp index 3c31aaa40..cdd232455 100644 --- a/core/libs/prx/libSceAudioIn/Export.cpp +++ b/core/libs/prx/libSceAudioIn/Export.cpp @@ -122,7 +122,7 @@ int APS5_VABI sceAudioInInput(int handle, void* dest) { if (device != 0) { captured = capture(device, out, bytes, deadline); } else { - std::this_thread::sleep_until(wake); + while (Clock::now() < wake) std::this_thread::sleep_until(wake); } std::memset(out + captured, 0, bytes - captured); lock.lock(); diff --git a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp index 3c7121db4..2e06061f3 100644 --- a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp +++ b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp @@ -339,20 +339,28 @@ LONG WINAPI ReportCrash(EXCEPTION_POINTERS* info) { return EXCEPTION_CONTINUE_SEARCH; } -void ReportThreadContext(const CONTEXT* context, DWORD threadId, const char* name) { +std::size_t CaptureFrames(const CONTEXT* context, std::uint64_t* frames, std::size_t capacity) { + std::size_t count = 0; + std::uint64_t frame = context->Rbp; + while (count < capacity && frame != 0 && (frame & 7) == 0 && IsReadable(frame) && IsReadable(frame + 8)) { + const auto returnAddress = reinterpret_cast(frame)[1]; + if (!IsExecutable(returnAddress)) break; + frames[count++] = returnAddress; + const auto next = reinterpret_cast(frame)[0]; + if (next <= frame) break; + frame = next; + } + return count; +} + +void ReportThreadContext(const CONTEXT* context, DWORD threadId, const char* name, const std::uint64_t* frames, std::size_t frameCount) { char line[MAX_PATH + 64]; Report(" thread %lu '%s': rip ", static_cast(threadId), name ? name : ""); DescribeAddress(context->Rip, line, sizeof(line)); Report("%s rsp 0x%016llx rbp 0x%016llx\n", line, static_cast(context->Rsp), static_cast(context->Rbp)); - std::uint64_t frame = context->Rbp; - for (int depth = 0; depth < 12 && frame != 0 && (frame & 7) == 0 && IsReadable(frame) && IsReadable(frame + 8); ++depth) { - const auto returnAddress = reinterpret_cast(frame)[1]; - if (!IsExecutable(returnAddress)) break; - DescribeAddress(returnAddress, line, sizeof(line)); - Report(" #%d %s\n", depth, line); - const auto next = reinterpret_cast(frame)[0]; - if (next <= frame) break; - frame = next; + for (std::size_t depth = 0; depth < frameCount; ++depth) { + DescribeAddress(frames[depth], line, sizeof(line)); + Report(" #%llu %s\n", static_cast(depth), line); } } @@ -367,13 +375,19 @@ void ReportAllThreads() { DWORD id; char name[128]; CONTEXT context; + std::uint64_t frames[12]; + std::size_t frameCount; }; static CapturedThread captured[128]; std::size_t capturedCount = 0; + std::size_t skippedCount = 0; if (Thread32First(snapshot, &entry)) { do { if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == current) continue; - if (capturedCount >= sizeof(captured) / sizeof(captured[0])) break; + if (capturedCount >= sizeof(captured) / sizeof(captured[0])) { + ++skippedCount; + continue; + } HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, entry.th32ThreadID); if (!thread) continue; if (SuspendThread(thread) == static_cast(-1)) { @@ -383,6 +397,8 @@ void ReportAllThreads() { CONTEXT context{}; context.ContextFlags = CONTEXT_FULL; const BOOL haveContext = GetThreadContext(thread, &context); + std::uint64_t frames[12] = {}; + const std::size_t frameCount = haveContext ? CaptureFrames(&context, frames, sizeof(frames) / sizeof(frames[0])) : 0; ResumeThread(thread); char name[128] = ""; PWSTR description = nullptr; @@ -395,14 +411,17 @@ void ReportAllThreads() { captured[capturedCount].id = entry.th32ThreadID; std::memcpy(captured[capturedCount].name, name, sizeof(captured[capturedCount].name)); captured[capturedCount].context = context; + std::memcpy(captured[capturedCount].frames, frames, sizeof(captured[capturedCount].frames)); + captured[capturedCount].frameCount = frameCount; ++capturedCount; } while (Thread32Next(snapshot, &entry)); } CloseHandle(snapshot); Report(" all threads:\n"); for (std::size_t i = 0; i < capturedCount; ++i) { - ReportThreadContext(&captured[i].context, captured[i].id, captured[i].name); + ReportThreadContext(&captured[i].context, captured[i].id, captured[i].name, captured[i].frames, captured[i].frameCount); } + if (skippedCount != 0) Report(" %llu further thread(s) left out\n", static_cast(skippedCount)); } // abort() and the UCRT's invalid-parameter path end the process with a silent fast fail