Skip to content

feat(libc): implement sceLibcBacktraceSelf on Linux - #2040

Closed
Aryansh1520 wants to merge 2 commits into
boykopovar:mainfrom
Aryansh1520:feat/libc-backtrace-linux
Closed

Aryansh1520 wants to merge 2 commits into
boykopovar:mainfrom
Aryansh1520:feat/libc-backtrace-linux

Conversation

@Aryansh1520

Copy link
Copy Markdown
Contributor

What

PPSA28997 (God of War Sons of Sparta, IL2CPP) calls sceLibcBacktraceGetBufferSize and sceLibcBacktraceSelf. On Linux, the host call stack is captured with backtrace, and each frame is resolved to its module and offset with dladdr. Frames are written as a linked list of records: next pointer, u32 module offset, then the module name at +0x10. GetBufferSize reports the count and the bytes Self will need. The signatures and record layout are inferred from the title and listed in TechnicalDebt, including the unknown u32 at +0xC (written as 0). Windows throws.

Tested

  • Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls.
  • tools/check_conventions.py --base origin/main and tools/pr_overlap.py --branch pass.

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
  • AI-assisted: yes

PPSA28997 (IL2CPP) calls sceLibcBacktraceGetBufferSize and
sceLibcBacktraceSelf. On Linux the host call stack is captured with
backtrace and each frame is resolved to its module and offset with
dladdr, then written as a linked list of frame records. The record
layout is inferred and listed in TechnicalDebt. Windows throws.
@MarcZX10

MarcZX10 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

The inferred record layout is documented as technical debt, but this ABI needs a regression that checks GetBufferSize against records written by Self, including a frame limit and a too-small buffer. Also, the PR describes this implementation as Linux-only, while the current non-Windows guard enables it on macOS too; please either gate the implementation to Linux and throw elsewhere, or provide macOS validation. GitHub reports no checks on this head.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Same as #2038, for sceLibcBacktraceSelf. Also, #ifndef _WIN32 turns this on for macOS too; please guard it with __linux__ and throw elsewhere.

@Aryansh1520

Copy link
Copy Markdown
Contributor Author

Updated in 75c1fa6:

  • The implementation is now behind #ifdef __linux__. macOS and Windows throw through NotImplemented_nid_no_patch, and the TechnicalDebt entry says so.
  • New Linux test guest_backtrace:
    • GetBufferSize reports the same count and byte size that Self then writes.
    • The record layout: 8-byte aligned records, next pointing to the following record and null on the last, the u32 at +0xC zero, NUL-terminated names at +0x10.
    • The first frame resolves to the calling function.
    • Frame limits 0, 1 and 2 in both functions.
    • Buffers of 0, one record minus one byte, one record, two records minus one byte and two records give 0/1/1/1/2 records, and nothing is written past the buffer size.
  • The test showed the code assumed the frame capture helper is not inlined. Force-inlining it drops the caller's frame and fails the test, so the helper is now [[gnu::noinline]].

Where PPSA28997 stops: after loading its first scene, the title calls sceLibcBacktraceGetBufferSize (sMko2YZqDNQ), imported by Il2cppUserAssemblies.prx. When it was a not-implemented stub in my local build:

[src/Open.cpp:99 sceKernelOpen] path=/app0/Media/level0 flags=0x0 nativeFlags=0x0 mode=00
[src/Open.cpp:99 sceKernelOpen] path=/app0/Media/sharedassets0.assets.resS flags=0x0 nativeFlags=0x0 mode=00
[libc] unhandled exception 'St13runtime_error' thrown from 0x7f2e1f18a7a9
terminate called after throwing an instance of 'std::runtime_error'
  what():  libcUnknown_sMko2YZqDNQ not implemented

Main exports neither function. On main (as of Oct 8) the title stops earlier, at LTov9gMEqCU (std::ios_base::_Addstd, #545).

The build workflows on this head are waiting for approval to run (fork PR).

@popcoder565

Copy link
Copy Markdown
Contributor

Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 75c1fa60, merged onto main d70b899 in one build with 38 other PRs that had no Windows run.

No title was run.

🤖 Addressed by Claude Code

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the __linux__ gate and the layout/limit/small-buffer test; build and ctest are green here (guest_backtrace passes, 524/524).

One blocker left:

  • The signatures and the record layout (next, u32 offset, u32 at +0xC, name at +0x10, 8-byte alignment) are "inferred from the title", but no evidence is in the PR, and none of KytyPS5, shadPS4, fpPS4 or prosper has a body for these exports. The test pins the PR's own layout, so it can't catch a wrong guess. Please attach static evidence from the title's call site (instruction offsets showing how the returned list is walked: the next load, the offset use, the name pointer, and which return values are checked), or a log of the title consuming the result. If it can't be confirmed, say which parts are guesses in TechnicalDebt (return value on a too-small buffer, whether count includes truncated frames).

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Progress report (51f69aa → 75c1fa6)

📈 System libraries: 84.7% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libc sceLibcBacktraceGetBufferSize
libc sceLibcBacktraceSelf

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing: the record layout is inferred and the frames are host frames, so what the title gets back isn't what the console returns. Please reopen with the exact layout, or a version that throws for what isn't known.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants