feat(libkernel): list the calling process's threads for kern.proc.pid_td #16401

Merged
NevesHenry merged 4 commits from upstream-pr/16401 into main 2026-11-11 22:14:52 +00:00
NevesHenry commented 2026-11-06 16:11:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NevesHenry on 2026-11-06T16:11:32Z.


What

ProsperoWeb (PRWB00001) 0.2.1 lists its own threads every 30 s with sysctl {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, pid} (ps5_cpu_monitor.cpp#L65-L100). On main, with #16270 so that the title gets past the loader, the first call throws about 6 s in:

terminate called after throwing an instance of 'std::runtime_error'
  what():  sysctl_nid_postfix: unsupported MIB 1.14.17.24

kern.proc.pid without threads (1.14.1) already answers for the calling process. This adds its pid_td sibling, 1.14.17, as FreeBSD 11.4's kern_proc_out answers it without KERN_PROC_NOTHREADS: one 1,088-byte kinfo_proc per thread.

  • SystemConfiguration.cpp: each record is the kern.proc.pid record (ki_structsize, ki_pid, ki_ppid, ki_comm) plus, for one live guest thread, ki_tid (offset 600, the pthread_getthreadid_np value), ki_numthreads (596, the number of records) and the thread name split as fill_kinfo_thread splits it: up to 16 bytes in ki_tdname (394), the next 3 in ki_moretdname (502). The offsets are those of FreeBSD 11.4 amd64 sys/user.h, checked with static_assert. A null buffer reports the size of all records, a short buffer gets ENOMEM, a write EPERM, any other pid (including -1) ESRCH and a name that is not four elements long EINVAL, the same checks as 1.14.1.
  • Pthread: LiveGuestThreadNames() copies the tid and name of every registered guest thread that has not finished, under the registry lock, after scePthreadSelf() so the calling thread is always listed.
  • The title's own comment says the console may have no such sysctl ("ENOENT on the PS5"), and it then falls back to the threads it created through pthread_create. I have no console measurement either way, so this follows FreeBSD, as 1.14.1 and 1.14.8 do, and the debt entry says so. If a measurement shows ENOENT, the node becomes an ENOENT answer like the other measured-absent MIBs, and the title takes its fallback.
  • A debt entry records what is unverified: whether the console answers this MIB at all, and every per-thread field left 0 (run time, CPU, state, priority).
  • guest_system_configuration gets CheckProcessThreadsSysctl: it creates a thread named kproc_thread_name, reads the records, checks the common fields and ki_numthreads in each, distinct ki_tids, that both the calling thread and the new one are listed, the name split (kproc_thread_nam + e), the error cases, and that the joined thread is no longer listed.

Tested

Linux: Ubuntu 24.04 container on WSL2, Ryzen 9 7900, GCC 13.3, Release, llvmpipe (the only Vulkan device here).

Title: ProsperoWeb 0.2.1, PRWB00001.zip from kvaps/ProsperoWeb, SHA-256 687652cbc05c01db60410c357bc4322f05170de4829ace174d1d5628ba63b0b0, the same as in the homebrew catalog. I ran it with tools/title_run.py (relinker default flags) for 40 s under Xvfb in a container with no network, without input, 3 runs each. The title also needs #16270, so both columns have it merged on main 492ed5c5fe; the only difference is this branch's libkernel.prx.

main + #16270 main + #16270 + this PR
Stop what(): sysctl_nid_postfix: unsupported MIB 1.14.17.<pid>, SIGABRT 6 to 7 s in (3/3) none; still running at 40 s (3/3)
Title log its self-tests, main: starting the UI, then the abort cpu: affinity 0-12 ... threads 2 (kern.proc; ...) at start and threads 11 (kern.proc; ...) 30 s later, splash: drawn and flipped, WebKit: context and data store created, then startup: showing the startup-error message

The title now waits for its broker process, which it starts through an ELF loader (elfldr on 127.0.0.1:9021 or etaHEN's websrv on 127.0.0.1:8080) that only a jailbroken console runs, so its WebContent and Networking processes fail to launch. That is outside this PR.

  • ctest -R "^guest_system_configuration$": passes. With core/libs/prx/libkernel from main and the test kept, it fails: what(): sysctl_nid_postfix: unsupported MIB 1.14.17.9 (Subprocess aborted).
  • Full build (all targets) passes; ctest -R "kernel|pthread|sched|cpuset|system_configuration|thread|sysctl" -E "^agc_": 162/162 pass.
  • tools/check_conventions.py --base origin/main passes.
  • Update 2026-11-09: merged main 3e089775b9 (no conflicts). main now checks GuestThreadEnded instead of _finished everywhere it walks the thread registry (on Windows it also counts an adopted thread whose host thread has ended), so LiveGuestThreadNames() uses it too: b0b07fc8f0, one line, no change on Linux. On the merged head, guest_system_configuration passes and still fails with main's SystemConfiguration.cpp (unsupported MIB 1.14.17.<pid>), the same ctest set passes 184/184 (it has grown since), and check_conventions.py --base origin/main passes.
  • Windows and macOS: not built.
  • Overlap: no open PR or issue mentions KERN_PROC_INC_THREAD, kinfo_proc, pid_td or 1.14.17 except #16270's description, which names this stop. tools/pr_overlap.py needs git 2.38 and this host has 2.34, so I merged the heads of the 124 open PRs that change these files into this branch and into main: all merge or conflict the same way with both, except #15040, which adds #include <utility> on the line where this branch adds #include <vector> in Pthread.hpp.

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: none (the title also needs #16270 to get this far; the code is independent)
  • Fixes: none
  • Unblocks: PRWB00001 #16268
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

AI disclosure: this change and description were written with Claude Code.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5190866369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16401). Originally posted by [NevesHenry](https://github.com/NevesHenry) on 2026-11-06T16:11:32Z. --- ### What ProsperoWeb (PRWB00001) 0.2.1 lists its own threads every 30 s with `sysctl` {`CTL_KERN`, `KERN_PROC`, `KERN_PROC_PID | KERN_PROC_INC_THREAD`, pid} ([`ps5_cpu_monitor.cpp#L65-L100`](https://github.com/kvaps/ProsperoWeb/blob/8cfe29c3cbd763c8c832f414f902b68b7d9a09bb/src/frontend/ps5/ps5_cpu_monitor.cpp#L65-L100)). On `main`, with #16270 so that the title gets past the loader, the first call throws about 6 s in: ``` terminate called after throwing an instance of 'std::runtime_error' what(): sysctl_nid_postfix: unsupported MIB 1.14.17.24 ``` `kern.proc.pid` without threads (1.14.1) already answers for the calling process. This adds its `pid_td` sibling, 1.14.17, as FreeBSD 11.4's [`kern_proc_out`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_proc.c#L1329-L1373) answers it without `KERN_PROC_NOTHREADS`: one 1,088-byte `kinfo_proc` per thread. - `SystemConfiguration.cpp`: each record is the `kern.proc.pid` record (`ki_structsize`, `ki_pid`, `ki_ppid`, `ki_comm`) plus, for one live guest thread, `ki_tid` (offset 600, the `pthread_getthreadid_np` value), `ki_numthreads` (596, the number of records) and the thread name split as [`fill_kinfo_thread`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_proc.c#L1042-L1146) splits it: up to 16 bytes in `ki_tdname` (394), the next 3 in `ki_moretdname` (502). The offsets are those of FreeBSD 11.4 amd64 [`sys/user.h`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/user.h#L119-L217), checked with `static_assert`. A null buffer reports the size of all records, a short buffer gets `ENOMEM`, a write `EPERM`, any other pid (including -1) `ESRCH` and a name that is not four elements long `EINVAL`, the same checks as 1.14.1. - `Pthread`: `LiveGuestThreadNames()` copies the tid and name of every registered guest thread that has not finished, under the registry lock, after `scePthreadSelf()` so the calling thread is always listed. - The title's own comment says the console may have no such sysctl ("ENOENT on the PS5"), and it then falls back to the threads it created through `pthread_create`. I have no console measurement either way, so this follows FreeBSD, as 1.14.1 and 1.14.8 do, and the debt entry says so. If a measurement shows `ENOENT`, the node becomes an `ENOENT` answer like the other measured-absent MIBs, and the title takes its fallback. - A debt entry records what is unverified: whether the console answers this MIB at all, and every per-thread field left 0 (run time, CPU, state, priority). - `guest_system_configuration` gets `CheckProcessThreadsSysctl`: it creates a thread named `kproc_thread_name`, reads the records, checks the common fields and `ki_numthreads` in each, distinct `ki_tid`s, that both the calling thread and the new one are listed, the name split (`kproc_thread_nam` + `e`), the error cases, and that the joined thread is no longer listed. ### Tested Linux: Ubuntu 24.04 container on WSL2, Ryzen 9 7900, GCC 13.3, Release, llvmpipe (the only Vulkan device here). Title: ProsperoWeb 0.2.1, `PRWB00001.zip` from [kvaps/ProsperoWeb](https://github.com/kvaps/ProsperoWeb/releases/tag/v0.2.1), SHA-256 `687652cbc05c01db60410c357bc4322f05170de4829ace174d1d5628ba63b0b0`, the same as in the homebrew catalog. I ran it with `tools/title_run.py` (relinker default flags) for 40 s under Xvfb in a container with no network, without input, 3 runs each. The title also needs #16270, so both columns have it merged on `main` 492ed5c5fe; the only difference is this branch's `libkernel.prx`. | | `main` + #16270 | `main` + #16270 + this PR | |---|---|---| | Stop | `what(): sysctl_nid_postfix: unsupported MIB 1.14.17.<pid>`, SIGABRT 6 to 7 s in (3/3) | none; still running at 40 s (3/3) | | Title log | its self-tests, `main: starting the UI`, then the abort | `cpu: affinity 0-12 ... threads 2 (kern.proc; ...)` at start and `threads 11 (kern.proc; ...)` 30 s later, `splash: drawn and flipped`, `WebKit: context and data store created`, then `startup: showing the startup-error message` | The title now waits for its broker process, which it starts through an ELF loader (`elfldr` on 127.0.0.1:9021 or etaHEN's `websrv` on 127.0.0.1:8080) that only a jailbroken console runs, so its WebContent and Networking processes fail to launch. That is outside this PR. - `ctest -R "^guest_system_configuration$"`: passes. With `core/libs/prx/libkernel` from `main` and the test kept, it fails: `what(): sysctl_nid_postfix: unsupported MIB 1.14.17.9` (Subprocess aborted). - Full build (all targets) passes; `ctest -R "kernel|pthread|sched|cpuset|system_configuration|thread|sysctl" -E "^agc_"`: 162/162 pass. - `tools/check_conventions.py --base origin/main` passes. - Update 2026-11-09: merged `main` 3e089775b9 (no conflicts). `main` now checks `GuestThreadEnded` instead of `_finished` everywhere it walks the thread registry (on Windows it also counts an adopted thread whose host thread has ended), so `LiveGuestThreadNames()` uses it too: b0b07fc8f0, one line, no change on Linux. On the merged head, `guest_system_configuration` passes and still fails with `main`'s `SystemConfiguration.cpp` (`unsupported MIB 1.14.17.<pid>`), the same ctest set passes 184/184 (it has grown since), and `check_conventions.py --base origin/main` passes. - Windows and macOS: not built. - Overlap: no open PR or issue mentions `KERN_PROC_INC_THREAD`, `kinfo_proc`, `pid_td` or 1.14.17 except #16270's description, which names this stop. `tools/pr_overlap.py` needs git 2.38 and this host has 2.34, so I merged the heads of the 124 open PRs that change these files into this branch and into `main`: all merge or conflict the same way with both, except #15040, which adds `#include <utility>` on the line where this branch adds `#include <vector>` in `Pthread.hpp`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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: none (the title also needs #16270 to get this far; the code is independent) - [x] Fixes: none - [x] Unblocks: PRWB00001 #16268 - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) AI disclosure: this change and description were written with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
milescoviello commented 2026-11-09 07:53:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-09T07:53:19Z.

Review: APPROVED


Read bff6aa8315 against main b66ca0e85a (the merge preview is clean).

Layout. I checked the new GuestKinfoProc fields against FreeBSD's sys/sys/user.h at release/11.4.0, and every offset matches.

  • ki_tdname[TDNAMLEN+1] sits right before ki_wmesg, ki_login and ki_lockname, which puts it at 394, below the existing ki_comm at 447.
  • After ki_comm, ki_emul[17] and ki_loginclass[18] put ki_moretdname[4] at 502.
  • ki_sparestrings[46], then ki_spareints[KI_NSPARE_INT] with KI_NSPARE_INT 4 in 11.4, then seven ints (ki_oncpu to ki_jid) put ki_numthreads at 596 and ki_tid at 600.
  • The new static_asserts and the 1088-byte total agree with that.

Name split. Up to TDNAMLEN (16) characters go into ki_tdname, and the rest, up to 3 more, into ki_moretdname. Both stay NUL-terminated because the record starts zeroed. That is the split FreeBSD's fill_kinfo_thread makes for names longer than TDNAMLEN, and the test pins it with a 17-character name ("kproc_thread_nam" + "e").

Locks. LiveGuestThreadNames takes the registry lock, then each thread's nameLock. scePthreadRename and scePthreadGetname take them in the same order, and nothing in Thread.cpp takes nameLock first, so there is no inversion.

Errors. EPERM for a write, EINVAL for a MIB that isn't 4 long, ESRCH for another PID, and ENOMEM with a partial copy for a short buffer (through the shared CopyOut). These match the existing 1.14.1 handler and FreeBSD's sysctl_kern_proc.

One behaviour note, not a request. As in FreeBSD, a caller that sizes its buffer from the oldValue == nullptr query can get ENOMEM if a thread starts before the second call. ProsperoWeb would need slack or a retry for that; FreeBSD's own ps adds slack for the same reason.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5822966594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16401#pullrequestreview-5822966594). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-09T07:53:19Z. **Review:** APPROVED --- Read `bff6aa8315` against `main` `b66ca0e85a` (the merge preview is clean). **Layout.** I checked the new `GuestKinfoProc` fields against FreeBSD's `sys/sys/user.h` at `release/11.4.0`, and every offset matches. - `ki_tdname[TDNAMLEN+1]` sits right before `ki_wmesg`, `ki_login` and `ki_lockname`, which puts it at 394, below the existing `ki_comm` at 447. - After `ki_comm`, `ki_emul[17]` and `ki_loginclass[18]` put `ki_moretdname[4]` at 502. - `ki_sparestrings[46]`, then `ki_spareints[KI_NSPARE_INT]` with `KI_NSPARE_INT` 4 in 11.4, then seven ints (`ki_oncpu` to `ki_jid`) put `ki_numthreads` at 596 and `ki_tid` at 600. - The new `static_assert`s and the 1088-byte total agree with that. **Name split.** Up to `TDNAMLEN` (16) characters go into `ki_tdname`, and the rest, up to 3 more, into `ki_moretdname`. Both stay NUL-terminated because the record starts zeroed. That is the split FreeBSD's `fill_kinfo_thread` makes for names longer than `TDNAMLEN`, and the test pins it with a 17-character name (`"kproc_thread_nam"` + `"e"`). **Locks.** `LiveGuestThreadNames` takes the registry lock, then each thread's `nameLock`. `scePthreadRename` and `scePthreadGetname` take them in the same order, and nothing in `Thread.cpp` takes `nameLock` first, so there is no inversion. **Errors.** EPERM for a write, EINVAL for a MIB that isn't 4 long, ESRCH for another PID, and ENOMEM with a partial copy for a short buffer (through the shared `CopyOut`). These match the existing 1.14.1 handler and FreeBSD's `sysctl_kern_proc`. One behaviour note, not a request. As in FreeBSD, a caller that sizes its buffer from the `oldValue == nullptr` query can get ENOMEM if a thread starts before the second call. ProsperoWeb would need slack or a retry for that; FreeBSD's own `ps` adds slack for the same reason. AI-assisted: yes (Claude Code)
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!16401
No description provided.