Skip to content

fix(libc): keep host diagnostics on stderr when a guest redirects it on Linux - #4792

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
marshmll:fix/linux-host-stderr-redirect
Oct 13, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
marshmll:fix/linux-host-stderr-redirect

Conversation

@marshmll

Copy link
Copy Markdown
Contributor

What

The Linux side of #4631. On Linux a guest stderr redirect also moves the host's: guest descriptors are host descriptors, and the guest stderr stream wraps the host stderr FILE (_Stderr_nid_postfix{stderr}, and FileStream::Reopen runs std::freopen on it). After a guest dup2(fd, 2), or freopen(path, mode, stderr) followed by setvbuf with full buffering, the libc terminate report (RawWrite uses fwrite(..., stderr) on Linux), the host fprintf(stderr, ...) diagnostics and libstdc++'s terminate message go into the title's pipe or file, and abort() drops what is still buffered.

RetroArch (PPSA99169) does exactly this: its trace says stderr is the trace file (buffered, flushed every 250 ms). On main it aborts with nothing on the console and nothing in app0/trace.txt; the stop was only found with gdb. With this change the console shows:

[libc] unhandled exception 'St13runtime_error' thrown from 0x...
terminate called after throwing an instance of 'std::runtime_error'
  what():  sceSystemServiceLoadExec: executable replacement not implemented

The same happened with ps5-opengl (PPSA99005): with write watch unavailable, its FATAL: an address-based build copies 328 MiB ... landed in its own download0/ps5-opengl.log instead of the console.

  • libc (General.cpp, Linux only): KeepHostStandardError_nid_no_patch() keeps a close-on-exec duplicate of host descriptor 2 once, before the first guest redirect, and points the host stderr at an unbuffered stream on it. The guest stream keeps its own FILE and follows its redirect as on FreeBSD. If descriptor 2 is closed there is nothing to keep.
  • libc (FileStream::Reopen, Linux branch): keeps it before reopening a stream on descriptor 2.
  • libkernel (dup2, Linux branch): PreserveHostStandardError takes the same kept descriptor instead of its own dup(2), so sceKernelDebugOutText (fix(libkernel): keep sceKernelDebugOutText off a redirected guest stderr #4536) and the rest of the host output share it.
  • libc (RawWrite, Linux branch): writes the terminate report to the kept descriptor with write(), without the stream lock or buffering, as its comment already asks.
  • New guest_host_standard_error test (Unix): in forked children, a guest dup2 and a guest freopen + full buffering, each with a host fputs(stderr) and with an uncaught exception. The host output and the what(): line must reach the original stderr, and the guest output its redirect.

Windows code is unchanged; #4640 covers it there. The two touch neighbouring lines in FileStream.hpp, General.hpp, General.cpp and Stdio.cpp but no hunk overlaps; whichever lands second may need a trivial rebase.

Tested

Fedora 44, GCC 16.2.1, Release with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON, AMD RX 9060 XT (RADV) and lavapipe, on main ee49621.

  • guest_host_standard_error fails without the library changes (the host fputs after a guest dup2 never reaches the original stderr) and passes with them.
  • guest_host_standard_error, guest_filesystem, guest_dup, exception_runtime, guest_standard_streams, file_streams, guest_fiopen, guest_syslog, guest_formatting pass.
  • Full ctest on lavapipe before the rebase: 994/995. agc_compute_pipeline_replay fails on Mesa 26.2.4 lavapipe (replay did not export a pipeline cache header) and passes on RADV; it does not use stderr. guest_video_out_high_frame_rate skips because the local display has no 120 Hz mode.
  • RetroArch (PPSA99169, catalog release, sha256 checked) relinked and run in a rootless Podman container without network: the stop message above now reaches the console.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Not tested on Windows (unchanged) or macOS.

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

…on Linux

On Linux a guest stderr redirect also moves the host's: guest
descriptors are host descriptors, and the guest stderr stream wraps the
host stderr FILE. After dup2(fd, 2), or freopen(path, mode, stderr) with
full buffering, the libc terminate report, the host fprintf(stderr)
diagnostics and libstdc++'s terminate message go into the title's pipe
or file, and abort() discards what is still buffered. RetroArch
(PPSA99169) reopens stderr onto app0/trace.txt with full buffering, so
its stop on main left no message anywhere.

Before the first guest redirect of descriptor 2 (libkernel dup2, libc
freopen of a stream on descriptor 2), keep a close-on-exec duplicate of
the host descriptor 2 and point the host stderr at an unbuffered stream
on it. The guest stream keeps its own FILE and follows the redirect.
The libc terminate report writes to the kept descriptor with write()
instead of the stderr stream, and libkernel's sceKernelDebugOutText uses
the same kept descriptor.
@nicolasfalesy

Copy link
Copy Markdown
Contributor

Tested on Linux (Arch, kernel 7.2.5, GCC 16.2.1, Release), this PR (438d410) merged onto main 37dd483, with ProsperoLight (PPSA99002, catalog release, relinked with --to-intel):

  • main: the title aborts (exit 134) and the log has no what() line; the throw is only visible in gdb (File::Fcntl → Classify).
  • With this PR: the same abort now ends with
    [libc] unhandled exception 'St13runtime_error' thrown from 0x742fd1a5421d
    terminate called after throwing an instance of 'std::runtime_error'
      what():  fcntl: descriptor 35 is neither a file, a directory, a pipe nor a character device
    
    in the title's stdout/stderr log, so the stop is readable without a debugger.

Only this title was checked; no change in where it stops.

Tested with Claude Code assistance.

@oneandonlydean oneandonlydean self-assigned this Oct 13, 2026
@oneandonlydean
oneandonlydean merged commit b17fb4a into boykopovar:main Oct 13, 2026
15 checks passed
YossiYad added a commit to YossiYad/AnyPS5 that referenced this pull request Oct 15, 2026
…andard error

A guest dup2 onto descriptor 2 keeps a close-on-exec duplicate of the
host's standard error (boykopovar#4792), so the runtime's own output stays on the
console while the title's stderr goes where the title pointed it. The
duplicate did not survive LoadExec's execv, and the next executable
started with descriptor 2 still on the title's redirect: its runtime
output, terminate report included, went there.

RetroArch (PPSA99169) points stderr at app0/trace.txt and then starts
its picker. The picker's [gpu] and sceKernelDebugOutText lines landed
in trace.txt, and its abort on an unconverted es-de.bin showed up
nowhere; the message was found only in the core dump.

Before execv, the kept descriptor now goes back onto descriptor 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants