feat(libc): record traced system library calls on Linux #7411
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7411"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-18T05:11:12Z.
What
The call tracer behind
--trace-importsandAPS5_TRACE_CALLS(#4546) was built for Windows only. This makes the libc side work on Linux too, as the first half of #3815's Linux support. The relinker change that emits theTraceImportcalls into Linux executables follows in a separate PR that depends on this one.CallTrace.cppmoves fromspecifics/windowstospecifics/x86_64, since the thunk and the register-saving entry are x86-64 code rather than Windows code. It builds on Windows and Linux; macOS is left out.memfdmapped twice, once writable and once executable, matching the two views of the Windows file mapping. Threads are identified bygettid.SIGABRThandler. It prints the same two sections as the Windows report (last 64 calls with the caller asmodule+offsetfromdladdr, then the 32 most called imports) and passes the signal on to the previous handler, or re-raises it with the default action. This covers both guestabort()and hoststd::abort().SIGSEGVdo not print the trace on Linux. libc's guest write watch handlesSIGSEGVandSIGBUSitself, so a handler in front of it would report faults it recovers from. This is recorded indocs/dev/debt/libc.md.Tested
Linux x86-64, GCC 13, full Release build with
BUILD_TESTING=ON.guest_call_traceandguest_call_trace_abortare now registered on Linux as well as Windows, and both pass. They passed 20 runs in a row, including the 4-thread × 20000-call ring check.SIGABRThandler, which exits with status 3 like the Windows CRT. The test therefore also checks that the report chains to the previous handler, and CTest judges the output against the existing regular expression instead of treating the signal as a crash. The executable suffix in that expression now comes fromCMAKE_EXECUTABLE_SUFFIX.Windows and macOS were not built. The Windows code paths are unchanged apart from being moved behind
#ifdef _WIN32.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)--trace-importsin the relinker (follow-up PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T05:33:18Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/tests/GuestCallTrace.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:41:02Z.
Code review location:
core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp; line 148Follow-up to the same signal-context concern I raised on stacked #7417: this PR introduces the handler that #7417 exercises.
ReportOnAbortcallsfprintf/fflush, thenReportCallTraceusesdladdr/snprintfand reads the live trace state. These are not async-signal-safe. IfSIGABRTarrives while a thread holds a stdio, loader, or trace-related lock, the handler can deadlock before the previous/default action runs, turning the abort into a hang. POSIX restricts signal handlers to async-signal-safe functions (signal actions). Could the handler hand off to normal context or use only a signal-safe, preformatted write path? The end-to-end abort test confirms the report is printed on its normal path, but not when the signal interrupts one of these routines.Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-18T12:10:13Z.
Code review location:
core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp; line 148Cheers, good spot. You're right, and I managed to reproduce it: with another thread holding the
stderrlock, the old handler just sat there until the test timed out.Sorted in f67014106:
write(2)on a stack buffer, with no stdio;dladdr;I've also added a regression test that holds the
stderrlock from another thread during the abort. It timed out before and passes now. #7417 is rebased on top.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T00:52:32Z.
Review: COMMENTED
Linux run of
ecf408c7c, with two abort races on top of the existing tests. With one abort the report holds up under load. With two threads aborting at the same time, the report is cut off or missing in every run.Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build.Head
ecf408c7cmerged ontomaina373e85e5(clean merge).Builds.
guest_call_traceandguest_call_trace_abortpass 50/50 with--repeat until-fail:50, and 20/20 pinned to one core withtaskset -c 0. The non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out" -j7) is 747/747.mainis 745/745, and the difference is the two tests this PR registers on Linux.Probe, added to
GuestCallTrace.cppas aracemode: install aSIGABRThandler that_exit(3)s, as the abort test does, and traceIntegers. Then 4 threads call the traced import in a loop, and after 20 ms the main thread callsabort(). Each variant ran 300 times with a 10 s timeout:abort()abort()togetherOne abort while other threads keep writing the ring is fine. A few slots are skipped because they are mid-write, which is what the sequence check is for. With two aborts, the thread that loses
g_reporting.exchange(true)goes straight to the previous handler while the winner is still writing. Here that handler is_exit(3). In a real run it is usuallySIG_DFL, and thesigaction(SIG_DFL)plusraise()kills the process the same way. So the report is lost exactly when a crash spreads to a second thread. One way to fix it is to have the losing thread wait until the winner has finished writing before it chains, for example with a secondg_reportedflag andnanosleep, which is async-signal-safe. A bounded wait would avoid hanging if the winner never finishes. I tried exactly that: the winner setsg_reportedafterReportCallTrace(), and the loser polls it with a 1 msnanosleepfor at most 2 s before chaining. With that change the two-abort variant printed the full report in 300/300 runs (header and "most called" section in every run, 60–64 entries). The one-abort variant stayed at 300/300, and both call trace tests still pass.#7417 carries the same
CallTrace.cpp, so this applies there too.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-19T07:48:51Z.
@jotitadev Cheers, brilliant testing. You're spot on: the thread that lost the exchange chained straight away and took the report down with it. Sorted in 6c7bfaa29, essentially your fix. The winner sets
g_reportedonce it's written, and the loser polls it with a 1 msnanosleepfor at most 2 s before chaining. There's also a newguest_call_trace_abort_racetest that aborts from two threads at once after 100 extra traced calls, and checks the whole report. It failed 35/50 here without the wait and passes 100/100 with it. #7417 is rebased on top.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T10:28:19Z.
Review: COMMENTED
Linux rerun of
6c7bfaa29with the sameraceprobe as my earlier report onecf408c7c(4 threads calling the tracedIntegersin a loop, thenabort()after 20 ms,SIGABRThandler that_exit(3)s, 300 runs per variant).Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build. Head6c7bfaa29merged ontomain8816fd772(clean merge).ecf408c7c6c7bfaa29abort()abort()togetherNo hangs, no entries out of order or with a wrong name, exit 3 in every run. The call trace tests pass (3/3, including the new
guest_call_trace_abort_race), and the non-GPU suite is 778/778 (main775/775 plus the tests this PR registers on Linux).Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.