feat(libc): record traced import calls on Windows #4546

Merged
codingreapermaps merged 1 commit from upstream-pr/4546 into main 2026-10-13 14:51:55 +00:00
codingreapermaps commented 2026-10-12 15:22:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by codingreapermaps on 2026-10-12T15:22:23Z.


What

Adds the runtime half of an opt-in call trace on Windows. It covers part of #3815: the entry of a call, not its arguments or its result.

TraceImport_nid_no_patch(target, name, library) returns target unchanged unless library is named in APS5_TRACE_CALLS (a comma-separated list, or all). For a selected library it returns a thunk that records the import, the thread and the caller's address in a ring of the last 65536 calls, counts the call, and jumps to the function. The thunk saves the integer argument registers and xmm0 to xmm7, so stack arguments, varargs and struct returns pass through. One target keeps one thunk, so function pointers compare equal.

The crash and abort reports end with the last 64 calls and the 32 most called imports:

last traced system library calls (APS5_TRACE_CALLS):
  #1 thread 15168 libkernel.prx k+AXqu2-eBc from 0x0000000140010006 output.exe+0x10006
most called traced imports (1 calls in total):
               1 libkernel.prx k+AXqu2-eBc

Nothing calls TraceImport_nid_no_patch in this PR, so no title is affected. The relinker option that binds imports through it is a separate PR that depends on this one. Limits (Windows only, entry only, one process-wide ring, wider vector registers untested) are in docs/dev/debt/libc.md.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main 43e3a70f8.

  • guest_call_trace (new): integer, floating-point, mixed, variadic and struct-return calls through a thunk return the same result as the direct call; the ring holds the calls in order with the right thread, names and a caller address inside the test image; 4 threads of 20000 calls leave the last 65536 calls with consecutive sequence numbers; the counters match.
  • The same test fails when the thunk is changed to clobber rax, r9 or xmm3 before the jump.
  • guest_call_trace_abort (new): the abort report lists the two traced calls.
  • windows_backtrace_report still passes.
  • tools/check_conventions.py and tools/check_test_registration.py pass.

With the relinker option from the follow-up PR, DOOM Shareware (PPSA99666, lowbit/ps5-doom v1.1.1, Intel i7-1165G7, before the rebase onto current main) ran with every library traced to the same point as untraced: 901,307,599 calls recorded, normal process exit. Not built on Linux or macOS; the source file is compiled only on Windows.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4831468407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4546). Originally posted by [codingreapermaps](https://github.com/codingreapermaps) on 2026-10-12T15:22:23Z. --- ### What Adds the runtime half of an opt-in call trace on Windows. It covers part of #3815: the entry of a call, not its arguments or its result. `TraceImport_nid_no_patch(target, name, library)` returns `target` unchanged unless `library` is named in `APS5_TRACE_CALLS` (a comma-separated list, or `all`). For a selected library it returns a thunk that records the import, the thread and the caller's address in a ring of the last 65536 calls, counts the call, and jumps to the function. The thunk saves the integer argument registers and `xmm0` to `xmm7`, so stack arguments, varargs and struct returns pass through. One target keeps one thunk, so function pointers compare equal. The crash and abort reports end with the last 64 calls and the 32 most called imports: last traced system library calls (APS5_TRACE_CALLS): #1 thread 15168 libkernel.prx k+AXqu2-eBc from 0x0000000140010006 output.exe+0x10006 most called traced imports (1 calls in total): 1 libkernel.prx k+AXqu2-eBc Nothing calls `TraceImport_nid_no_patch` in this PR, so no title is affected. The relinker option that binds imports through it is a separate PR that depends on this one. Limits (Windows only, entry only, one process-wide ring, wider vector registers untested) are in `docs/dev/debt/libc.md`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main 43e3a70f8. - `guest_call_trace` (new): integer, floating-point, mixed, variadic and struct-return calls through a thunk return the same result as the direct call; the ring holds the calls in order with the right thread, names and a caller address inside the test image; 4 threads of 20000 calls leave the last 65536 calls with consecutive sequence numbers; the counters match. - The same test fails when the thunk is changed to clobber `rax`, `r9` or `xmm3` before the jump. - `guest_call_trace_abort` (new): the abort report lists the two traced calls. - `windows_backtrace_report` still passes. - `tools/check_conventions.py` and `tools/check_test_registration.py` pass. With the relinker option from the follow-up PR, DOOM Shareware (PPSA99666, lowbit/ps5-doom v1.1.1, Intel i7-1165G7, before the rebase onto current main) ran with every library traced to the same point as untraced: 901,307,599 calls recorded, normal process exit. Not built on Linux or macOS; the source file is compiled only on Windows. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-13 14:19:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:19:08Z.


Reviewed the thunk (register save/restore, 16-byte stack alignment, return address read at the right offset, stack arguments and rax for varargs preserved) and the ring/count logic; no findings. The only conflict with main was a docs/dev/debt/libc.md adjacency, so I rebased and moved the debt line a few lines up. The Windows-only code and its tests are verified by the full Build run before merging; rebasing and merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147664859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4546#issuecomment-6147664859). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:19:08Z. --- Reviewed the thunk (register save/restore, 16-byte stack alignment, return address read at the right offset, stack arguments and `rax` for varargs preserved) and the ring/count logic; no findings. The only conflict with `main` was a `docs/dev/debt/libc.md` adjacency, so I rebased and moved the debt line a few lines up. The Windows-only code and its tests are verified by the full Build run before merging; rebasing and merging.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4546
No description provided.