feat(relinker): support --trace-imports for Linux output #7417

Merged
IshmamDC217 merged 5 commits from upstream-pr/7417 into main 2026-10-20 20:03:30 +00:00
IshmamDC217 commented 2026-10-18 05:23:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-18T05:23:24Z.


What

--trace-imports (#4547) only worked for Windows output. This adds it for Linux output, as the second half of #3815's Linux support, on top of the libc runtime in #7411.

With the flag, LinuxElfPatcher now:

  • adds an undefined TraceImport_nid_no_patch symbol and a GLOB_DAT relocation for it, bound eagerly like every other import, so a libc.prx without it fails at load with ld.so's undefined symbol error;
  • writes a table of the traced import slots with their NID and library, as offsets so it works for PIE and --linux-fixed-base output alike;
  • puts a prologue in front of the existing entry stub. For each entry it calls TraceImport_nid_no_patch(*slot, name, library) and stores the result back in the slot, then falls through to the usual stub. rdx (ld.so's fini pointer) and the stack are preserved.

The same imports are traced as on Windows: functions bound through a PLT slot, or through a GOT slot whose symbol is a function, with no addend, and not weak.

Before storing, the prologue makes each slot's page readable, writable and executable with mprotect. Today the Linux output drops PT_GNU_RELRO, but #6406 keeps it, and real eboots have their JUMP_SLOT targets inside it. Without this, the store would fault once both are merged. I merged #6406 locally with a RELRO range over the slot's page and the traced output still ran correctly. The trade-off (those pages stay writable, and data pages become executable while tracing) is recorded in docs/dev/debt/relinker.md.

Other changes:

  • --trace-imports no longer requires --windows. It is refused with --macos, and it still requires libc.prx among the libraries and conflicts with --lazy-binding.
  • Usage, TRACING.md and the debt entries now cover Linux.

Tested

Linux x86-64, GCC 13. Relinker-only build: the full suite of 195 tests passes.

  • New linux_trace_imports test. It builds a fake a.prx and a fake libc.prx with cc, like linux_executable_exports, and runs the relinked fixture. Its hook replaces a.prx's shared with a function that exits with 77.
    • Untraced: exits with 11.
    • Traced: exits with 77, both PIE and with --linux-fixed-base.
    • Data and weak imports are left alone: exits with 11.
    • A libc.prx without the hook fails at load naming the symbol.
    • The relinker refuses no libc.prx dependency, --lazy-binding and --macos.
    • I checked that the test fails when the expected exit code is wrong.
  • windows_trace_imports loses only its "requires --windows" assertion.
  • End to end with the real libc.prx from #7411: APS5_TRACE_CALLS=a ./out.elf on a fixture whose import calls abort() prints:
[libc] SIGABRT
  last traced system library calls (APS5_TRACE_CALLS):
    #1 thread 8574 a.prx shared from 0x000055903df90006 out.elf+0x6
  most called traced imports (1 calls in total):
               1 a.prx shared

Not tested on a real title, Windows or macOS.

python3 tools/check_conventions.py --base origin/main passes.

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 under Silent stubs in their library's technical debt file
  • 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: #7411
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4906629193 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7417). Originally posted by [IshmamDC217](https://github.com/IshmamDC217) on 2026-10-18T05:23:24Z. --- ### What `--trace-imports` (#4547) only worked for Windows output. This adds it for Linux output, as the second half of #3815's Linux support, on top of the libc runtime in #7411. With the flag, `LinuxElfPatcher` now: - adds an undefined `TraceImport_nid_no_patch` symbol and a `GLOB_DAT` relocation for it, bound eagerly like every other import, so a `libc.prx` without it fails at load with ld.so's undefined symbol error; - writes a table of the traced import slots with their NID and library, as offsets so it works for PIE and `--linux-fixed-base` output alike; - puts a prologue in front of the existing entry stub. For each entry it calls `TraceImport_nid_no_patch(*slot, name, library)` and stores the result back in the slot, then falls through to the usual stub. `rdx` (ld.so's fini pointer) and the stack are preserved. The same imports are traced as on Windows: functions bound through a PLT slot, or through a GOT slot whose symbol is a function, with no addend, and not weak. Before storing, the prologue makes each slot's page readable, writable and executable with `mprotect`. Today the Linux output drops `PT_GNU_RELRO`, but #6406 keeps it, and real eboots have their `JUMP_SLOT` targets inside it. Without this, the store would fault once both are merged. I merged #6406 locally with a RELRO range over the slot's page and the traced output still ran correctly. The trade-off (those pages stay writable, and data pages become executable while tracing) is recorded in `docs/dev/debt/relinker.md`. Other changes: - `--trace-imports` no longer requires `--windows`. It is refused with `--macos`, and it still requires `libc.prx` among the libraries and conflicts with `--lazy-binding`. - Usage, `TRACING.md` and the debt entries now cover Linux. ### Tested Linux x86-64, GCC 13. Relinker-only build: the full suite of 195 tests passes. - New `linux_trace_imports` test. It builds a fake `a.prx` and a fake `libc.prx` with `cc`, like `linux_executable_exports`, and runs the relinked fixture. Its hook replaces `a.prx`'s `shared` with a function that exits with 77. - Untraced: exits with 11. - Traced: exits with 77, both PIE and with `--linux-fixed-base`. - Data and weak imports are left alone: exits with 11. - A `libc.prx` without the hook fails at load naming the symbol. - The relinker refuses no `libc.prx` dependency, `--lazy-binding` and `--macos`. - I checked that the test fails when the expected exit code is wrong. - `windows_trace_imports` loses only its "requires `--windows`" assertion. - End to end with the real `libc.prx` from #7411: `APS5_TRACE_CALLS=a ./out.elf` on a fixture whose import calls `abort()` prints: ``` [libc] SIGABRT last traced system library calls (APS5_TRACE_CALLS): #1 thread 8574 a.prx shared from 0x000055903df90006 out.elf+0x6 most called traced imports (1 calls in total): 1 a.prx shared ``` Not tested on a real title, Windows or macOS. `python3 tools/check_conventions.py --base origin/main` passes. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [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: #7411 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-18 05:26:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:26:48Z.

Code review location: core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp; line 148

@@ -71,6 +89,86 @@ void MapThunkArea() {
     CloseHandle(section);
     if (g_writable == nullptr || g_executable == nullptr) throw std::system_error(static_cast<int>(code), std::system_category(), "call trace: MapViewOfFile");
 }
+#else
+void MapThunkArea() {
+    const int file = memfd_create("aps5-call-trace", MFD_CLOEXEC);
+    if (file < 0) throw std::system_error(errno, std::generic_category(), "call trace: memfd_create");
+    void* writable = MAP_FAILED;
+    void* executable = MAP_FAILED;
+    if (ftruncate(file, ThunkAreaBytes) == 0) {
+        writable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
+        if (writable != MAP_FAILED) executable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_EXEC, MAP_SHARED, file, 0);
+    }
+    const int code = errno;
+    close(file);
+    if (writable == MAP_FAILED || executable == MAP_FAILED) {
+        if (writable != MAP_FAILED) munmap(writable, ThunkAreaBytes);
+        throw std::system_error(code, std::generic_category(), "call trace: mmap");
+    }
+    g_writable = static_cast<std::uint8_t*>(writable);
+    g_executable = static_cast<std::uint8_t*>(executable);
+}
+
+struct sigaction g_previousAbort{};
+
+void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) {
+    Dl_info info{};
+    if (dladdr(reinterpret_cast<const void*>(address), &info) != 0 && info.dli_fname != nullptr && info.dli_fbase != nullptr) {
+        const char* slash = std::strrchr(info.dli_fname, '/');
+        const char* name = slash == nullptr ? info.dli_fname : slash + 1;
+        std::snprintf(buffer, size, "0x%016llx %s+0x%llx", static_cast<unsigned long long>(address), *name == '\0' ? "?" : name,
+            static_cast<unsigned long long>(address - reinterpret_cast<std::uint64_t>(info.dli_fbase)));
+        return;
+    }
+    std::snprintf(buffer, size, "0x%016llx", static_cast<unsigned long long>(address));
+}
+
+void ReportCallTrace() {
+    static CallTraceEntry entries[64];
+    const std::size_t count = CopyCallTrace_nid_no_patch(entries, std::size(entries));
+    if (count == 0) return;
+    char line[4096 + 64];
+    std::fprintf(stderr, "  last traced system library calls (APS5_TRACE_CALLS):\n");
+    for (std::size_t index = 0; index < count; ++index) {
+        DescribeAddress(entries[index].returnAddress, line, sizeof(line));
+        std::fprintf(stderr, "    #%llu thread %llu %s %s from %s\n", static_cast<unsigned long long>(entries[index].sequence),
+            static_cast<unsigned long long>(entries[index].thread), entries[index].library, entries[index].name, line);
+    }
+    static CallTraceCount counts[32];
+    std::uint64_t total = 0;
+    const std::size_t counted = CopyCallCounts_nid_no_patch(counts, std::size(counts), &total);
+    if (counted == 0) return;
+    std::fprintf(stderr, "  most called traced imports (%llu calls in total):\n", static_cast<unsigned long long>(total));
+    for (std::size_t index = 0; index < counted; ++index) {
+        std::fprintf(stderr, "    %12llu %s %s\n", static_cast<unsigned long long>(counts[index].calls), counts[index].library, counts[index].name);
+    }
+}
+
+void ReportOnAbort(int number, siginfo_t* info, void* context) {
+    std::fprintf(stderr, "[libc] SIGABRT\n");

The SIGABRT handler calls fprintf here, then ReportCallTrace() calls dladdr, stdio, and the trace-copy routines. These are not async-signal-safe; if abort() is raised while the thread holds a stdio/loader/trace lock, this handler can deadlock before the prior handler runs or the process terminates. Could the trace be emitted from a non-signal-context abort path (or otherwise use a signal-safe handoff) so the diagnostic cannot turn an abort into a hang?

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4305863743 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7417#discussion_r4305863743). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T05:26:48Z. **Code review location:** `core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp`; line 148 ~~~~diff @@ -71,6 +89,86 @@ void MapThunkArea() { CloseHandle(section); if (g_writable == nullptr || g_executable == nullptr) throw std::system_error(static_cast<int>(code), std::system_category(), "call trace: MapViewOfFile"); } +#else +void MapThunkArea() { + const int file = memfd_create("aps5-call-trace", MFD_CLOEXEC); + if (file < 0) throw std::system_error(errno, std::generic_category(), "call trace: memfd_create"); + void* writable = MAP_FAILED; + void* executable = MAP_FAILED; + if (ftruncate(file, ThunkAreaBytes) == 0) { + writable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0); + if (writable != MAP_FAILED) executable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_EXEC, MAP_SHARED, file, 0); + } + const int code = errno; + close(file); + if (writable == MAP_FAILED || executable == MAP_FAILED) { + if (writable != MAP_FAILED) munmap(writable, ThunkAreaBytes); + throw std::system_error(code, std::generic_category(), "call trace: mmap"); + } + g_writable = static_cast<std::uint8_t*>(writable); + g_executable = static_cast<std::uint8_t*>(executable); +} + +struct sigaction g_previousAbort{}; + +void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) { + Dl_info info{}; + if (dladdr(reinterpret_cast<const void*>(address), &info) != 0 && info.dli_fname != nullptr && info.dli_fbase != nullptr) { + const char* slash = std::strrchr(info.dli_fname, '/'); + const char* name = slash == nullptr ? info.dli_fname : slash + 1; + std::snprintf(buffer, size, "0x%016llx %s+0x%llx", static_cast<unsigned long long>(address), *name == '\0' ? "?" : name, + static_cast<unsigned long long>(address - reinterpret_cast<std::uint64_t>(info.dli_fbase))); + return; + } + std::snprintf(buffer, size, "0x%016llx", static_cast<unsigned long long>(address)); +} + +void ReportCallTrace() { + static CallTraceEntry entries[64]; + const std::size_t count = CopyCallTrace_nid_no_patch(entries, std::size(entries)); + if (count == 0) return; + char line[4096 + 64]; + std::fprintf(stderr, " last traced system library calls (APS5_TRACE_CALLS):\n"); + for (std::size_t index = 0; index < count; ++index) { + DescribeAddress(entries[index].returnAddress, line, sizeof(line)); + std::fprintf(stderr, " #%llu thread %llu %s %s from %s\n", static_cast<unsigned long long>(entries[index].sequence), + static_cast<unsigned long long>(entries[index].thread), entries[index].library, entries[index].name, line); + } + static CallTraceCount counts[32]; + std::uint64_t total = 0; + const std::size_t counted = CopyCallCounts_nid_no_patch(counts, std::size(counts), &total); + if (counted == 0) return; + std::fprintf(stderr, " most called traced imports (%llu calls in total):\n", static_cast<unsigned long long>(total)); + for (std::size_t index = 0; index < counted; ++index) { + std::fprintf(stderr, " %12llu %s %s\n", static_cast<unsigned long long>(counts[index].calls), counts[index].library, counts[index].name); + } +} + +void ReportOnAbort(int number, siginfo_t* info, void* context) { + std::fprintf(stderr, "[libc] SIGABRT\n"); ~~~~ --- The SIGABRT handler calls `fprintf` here, then `ReportCallTrace()` calls `dladdr`, stdio, and the trace-copy routines. These are not async-signal-safe; if `abort()` is raised while the thread holds a stdio/loader/trace lock, this handler can deadlock before the prior handler runs or the process terminates. Could the trace be emitted from a non-signal-context abort path (or otherwise use a signal-safe handoff) so the diagnostic cannot turn an abort into a hang?
github-actions[bot] commented 2026-10-18 05:36:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T05:36:20Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7532 core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp L280, L286, L295, L324, L326, L347-348, L398
PR 7918 docs/dev/debt/relinker.md L30-31

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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/CMakeLists.txt
core/relinker/CMakeLists.txt
docs/dev/debt/libc.md
docs/dev/debt/relinker.md
docs/user/USAGE.md
PR 5431 docs/dev/ENVIRONMENT.md
PR 5925 docs/user/USAGE.md
PR 6044 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/GuestCallTrace.cpp
core/relinker/CMakeLists.txt
PR 6226 docs/dev/debt/libc.md
PR 6469 docs/dev/debt/libc.md
PR 6618 core/relinker/CMakeLists.txt
PR 6636 docs/dev/ENVIRONMENT.md
PR 6788 docs/user/USAGE.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6879 core/relinker/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 6924 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 6946 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 6947 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 6977 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/CMakeLists.txt
PR 7464 docs/dev/ENVIRONMENT.md
PR 7473 docs/dev/debt/libc.md
PR 7484 docs/dev/ENVIRONMENT.md
PR 7604 docs/dev/ENVIRONMENT.md
docs/dev/TRACING.md
PR 7642 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 7648 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7800 docs/user/USAGE.md
PR 7922 docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8076 docs/dev/debt/relinker.md
PR 8077 docs/dev/ENVIRONMENT.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8206 docs/dev/ENVIRONMENT.md
PR 8352 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8542 docs/dev/ENVIRONMENT.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246160688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7417#issuecomment-6246160688). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T05:36:20Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 7532](https://www.github.com/boykopovar/AnyPS5/pull/7532) | `core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp` [L280](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L280), [L286](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L286), [L295](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L295), [L324](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L324), [L326](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L326), [L347-348](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L347-L348), [L398](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L398) | | | | [PR 7918](https://www.github.com/boykopovar/AnyPS5/pull/7918) | `docs/dev/debt/relinker.md` [L30-31](https://github.com/boykopovar/AnyPS5/blob/2185dbf92e8d9013a1620d2dfc95de50494e19f8/docs/dev/debt/relinker.md#L30-L31) | | | 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. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/relinker/CMakeLists.txt`<br>`docs/dev/debt/libc.md`<br>`docs/dev/debt/relinker.md`<br>`docs/user/USAGE.md` | | [PR 5431](https://www.github.com/boykopovar/AnyPS5/pull/5431) | `docs/dev/ENVIRONMENT.md` | | [PR 5925](https://www.github.com/boykopovar/AnyPS5/pull/5925) | `docs/user/USAGE.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/GuestCallTrace.cpp`<br>`core/relinker/CMakeLists.txt` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `docs/dev/debt/libc.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libc.md` | | [PR 6618](https://www.github.com/boykopovar/AnyPS5/pull/6618) | `core/relinker/CMakeLists.txt` | | [PR 6636](https://www.github.com/boykopovar/AnyPS5/pull/6636) | `docs/dev/ENVIRONMENT.md` | | [PR 6788](https://www.github.com/boykopovar/AnyPS5/pull/6788) | `docs/user/USAGE.md` | | [PR 6819](https://www.github.com/boykopovar/AnyPS5/pull/6819) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt`<br>`docs/dev/ENVIRONMENT.md` | | [PR 6924](https://www.github.com/boykopovar/AnyPS5/pull/6924) | `docs/dev/ENVIRONMENT.md`<br>`docs/user/USAGE.md` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `docs/dev/ENVIRONMENT.md`<br>`docs/user/USAGE.md` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `docs/dev/ENVIRONMENT.md`<br>`docs/user/USAGE.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/CMakeLists.txt` | | [PR 7464](https://www.github.com/boykopovar/AnyPS5/pull/7464) | `docs/dev/ENVIRONMENT.md` | | [PR 7473](https://www.github.com/boykopovar/AnyPS5/pull/7473) | `docs/dev/debt/libc.md` | | [PR 7484](https://www.github.com/boykopovar/AnyPS5/pull/7484) | `docs/dev/ENVIRONMENT.md` | | [PR 7604](https://www.github.com/boykopovar/AnyPS5/pull/7604) | `docs/dev/ENVIRONMENT.md`<br>`docs/dev/TRACING.md` | | [PR 7642](https://www.github.com/boykopovar/AnyPS5/pull/7642) | `docs/dev/ENVIRONMENT.md`<br>`docs/user/USAGE.md` | | [PR 7648](https://www.github.com/boykopovar/AnyPS5/pull/7648) | `docs/dev/debt/libc.md` | | [PR 7715](https://www.github.com/boykopovar/AnyPS5/pull/7715) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 7800](https://www.github.com/boykopovar/AnyPS5/pull/7800) | `docs/user/USAGE.md` | | [PR 7922](https://www.github.com/boykopovar/AnyPS5/pull/7922) | `docs/dev/ENVIRONMENT.md` | | [PR 8004](https://www.github.com/boykopovar/AnyPS5/pull/8004) | `docs/dev/ENVIRONMENT.md` | | [PR 8076](https://www.github.com/boykopovar/AnyPS5/pull/8076) | `docs/dev/debt/relinker.md` | | [PR 8077](https://www.github.com/boykopovar/AnyPS5/pull/8077) | `docs/dev/ENVIRONMENT.md` | | [PR 8150](https://www.github.com/boykopovar/AnyPS5/pull/8150) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8206](https://www.github.com/boykopovar/AnyPS5/pull/8206) | `docs/dev/ENVIRONMENT.md` | | [PR 8352](https://www.github.com/boykopovar/AnyPS5/pull/8352) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8542](https://www.github.com/boykopovar/AnyPS5/pull/8542) | `docs/dev/ENVIRONMENT.md` |
IshmamDC217 commented 2026-10-18 12:10:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-18T12:10:25Z.

Code review location: core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp; line 148

@@ -71,6 +89,86 @@ void MapThunkArea() {
     CloseHandle(section);
     if (g_writable == nullptr || g_executable == nullptr) throw std::system_error(static_cast<int>(code), std::system_category(), "call trace: MapViewOfFile");
 }
+#else
+void MapThunkArea() {
+    const int file = memfd_create("aps5-call-trace", MFD_CLOEXEC);
+    if (file < 0) throw std::system_error(errno, std::generic_category(), "call trace: memfd_create");
+    void* writable = MAP_FAILED;
+    void* executable = MAP_FAILED;
+    if (ftruncate(file, ThunkAreaBytes) == 0) {
+        writable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
+        if (writable != MAP_FAILED) executable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_EXEC, MAP_SHARED, file, 0);
+    }
+    const int code = errno;
+    close(file);
+    if (writable == MAP_FAILED || executable == MAP_FAILED) {
+        if (writable != MAP_FAILED) munmap(writable, ThunkAreaBytes);
+        throw std::system_error(code, std::generic_category(), "call trace: mmap");
+    }
+    g_writable = static_cast<std::uint8_t*>(writable);
+    g_executable = static_cast<std::uint8_t*>(executable);
+}
+
+struct sigaction g_previousAbort{};
+
+void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) {
+    Dl_info info{};
+    if (dladdr(reinterpret_cast<const void*>(address), &info) != 0 && info.dli_fname != nullptr && info.dli_fbase != nullptr) {
+        const char* slash = std::strrchr(info.dli_fname, '/');
+        const char* name = slash == nullptr ? info.dli_fname : slash + 1;
+        std::snprintf(buffer, size, "0x%016llx %s+0x%llx", static_cast<unsigned long long>(address), *name == '\0' ? "?" : name,
+            static_cast<unsigned long long>(address - reinterpret_cast<std::uint64_t>(info.dli_fbase)));
+        return;
+    }
+    std::snprintf(buffer, size, "0x%016llx", static_cast<unsigned long long>(address));
+}
+
+void ReportCallTrace() {
+    static CallTraceEntry entries[64];
+    const std::size_t count = CopyCallTrace_nid_no_patch(entries, std::size(entries));
+    if (count == 0) return;
+    char line[4096 + 64];
+    std::fprintf(stderr, "  last traced system library calls (APS5_TRACE_CALLS):\n");
+    for (std::size_t index = 0; index < count; ++index) {
+        DescribeAddress(entries[index].returnAddress, line, sizeof(line));
+        std::fprintf(stderr, "    #%llu thread %llu %s %s from %s\n", static_cast<unsigned long long>(entries[index].sequence),
+            static_cast<unsigned long long>(entries[index].thread), entries[index].library, entries[index].name, line);
+    }
+    static CallTraceCount counts[32];
+    std::uint64_t total = 0;
+    const std::size_t counted = CopyCallCounts_nid_no_patch(counts, std::size(counts), &total);
+    if (counted == 0) return;
+    std::fprintf(stderr, "  most called traced imports (%llu calls in total):\n", static_cast<unsigned long long>(total));
+    for (std::size_t index = 0; index < counted; ++index) {
+        std::fprintf(stderr, "    %12llu %s %s\n", static_cast<unsigned long long>(counts[index].calls), counts[index].library, counts[index].name);
+    }
+}
+
+void ReportOnAbort(int number, siginfo_t* info, void* context) {
+    std::fprintf(stderr, "[libc] SIGABRT\n");

Cheers, sorted in #7411 (f67014106), and this one's now rebased on it. The handler only uses write(2) now, with no stdio, dladdr or locks. There's a regression test for the held stderr lock case too. Details are over on #7411.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4306958787 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7417#discussion_r4306958787). Originally posted by [IshmamDC217](https://github.com/IshmamDC217) on 2026-10-18T12:10:25Z. **Code review location:** `core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp`; line 148 ~~~~diff @@ -71,6 +89,86 @@ void MapThunkArea() { CloseHandle(section); if (g_writable == nullptr || g_executable == nullptr) throw std::system_error(static_cast<int>(code), std::system_category(), "call trace: MapViewOfFile"); } +#else +void MapThunkArea() { + const int file = memfd_create("aps5-call-trace", MFD_CLOEXEC); + if (file < 0) throw std::system_error(errno, std::generic_category(), "call trace: memfd_create"); + void* writable = MAP_FAILED; + void* executable = MAP_FAILED; + if (ftruncate(file, ThunkAreaBytes) == 0) { + writable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0); + if (writable != MAP_FAILED) executable = mmap(nullptr, ThunkAreaBytes, PROT_READ | PROT_EXEC, MAP_SHARED, file, 0); + } + const int code = errno; + close(file); + if (writable == MAP_FAILED || executable == MAP_FAILED) { + if (writable != MAP_FAILED) munmap(writable, ThunkAreaBytes); + throw std::system_error(code, std::generic_category(), "call trace: mmap"); + } + g_writable = static_cast<std::uint8_t*>(writable); + g_executable = static_cast<std::uint8_t*>(executable); +} + +struct sigaction g_previousAbort{}; + +void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) { + Dl_info info{}; + if (dladdr(reinterpret_cast<const void*>(address), &info) != 0 && info.dli_fname != nullptr && info.dli_fbase != nullptr) { + const char* slash = std::strrchr(info.dli_fname, '/'); + const char* name = slash == nullptr ? info.dli_fname : slash + 1; + std::snprintf(buffer, size, "0x%016llx %s+0x%llx", static_cast<unsigned long long>(address), *name == '\0' ? "?" : name, + static_cast<unsigned long long>(address - reinterpret_cast<std::uint64_t>(info.dli_fbase))); + return; + } + std::snprintf(buffer, size, "0x%016llx", static_cast<unsigned long long>(address)); +} + +void ReportCallTrace() { + static CallTraceEntry entries[64]; + const std::size_t count = CopyCallTrace_nid_no_patch(entries, std::size(entries)); + if (count == 0) return; + char line[4096 + 64]; + std::fprintf(stderr, " last traced system library calls (APS5_TRACE_CALLS):\n"); + for (std::size_t index = 0; index < count; ++index) { + DescribeAddress(entries[index].returnAddress, line, sizeof(line)); + std::fprintf(stderr, " #%llu thread %llu %s %s from %s\n", static_cast<unsigned long long>(entries[index].sequence), + static_cast<unsigned long long>(entries[index].thread), entries[index].library, entries[index].name, line); + } + static CallTraceCount counts[32]; + std::uint64_t total = 0; + const std::size_t counted = CopyCallCounts_nid_no_patch(counts, std::size(counts), &total); + if (counted == 0) return; + std::fprintf(stderr, " most called traced imports (%llu calls in total):\n", static_cast<unsigned long long>(total)); + for (std::size_t index = 0; index < counted; ++index) { + std::fprintf(stderr, " %12llu %s %s\n", static_cast<unsigned long long>(counts[index].calls), counts[index].library, counts[index].name); + } +} + +void ReportOnAbort(int number, siginfo_t* info, void* context) { + std::fprintf(stderr, "[libc] SIGABRT\n"); ~~~~ --- Cheers, sorted in #7411 (f67014106), and this one's now rebased on it. The handler only uses `write(2)` now, with no stdio, `dladdr` or locks. There's a regression test for the held `stderr` lock case too. Details are over on #7411.
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!7417
No description provided.