git: fd5bc306ff3d - main - ptrace(2): expand ability to fetch syscall parameters

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 31 Mar 2025 02:46:05 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=fd5bc306ff3d8f908f36703d6ab714322f9f3c75

commit fd5bc306ff3d8f908f36703d6ab714322f9f3c75
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-03-20 01:52:29 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-03-31 02:43:22 +0000

    ptrace(2): expand ability to fetch syscall parameters
    
    Do not limit lwpinfo reporting of syscall number and args to SCE/SCX
    events. When td_sa holds the values, we can report them. Clear
    td_sa.code in TDA_SIG ast handler: this handler is run when the process
    is traced, and it is run with the last ptracestop() points before the
    return to userspace.
    
    This allows debugger to infer the interrupted syscall immediately after
    PT_ATTACH without the need to loose control to the debuggee' thread. It
    should work even when the debuggee is stopped in AST.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D49430
---
 sys/kern/kern_sig.c    | 8 ++++++++
 sys/kern/sys_process.c | 8 +++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index d7aa932aa7e3..0ee58af7fbe4 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -343,6 +343,14 @@ ast_sig(struct thread *td, int tda)
 	 * the postsig() loop was performed.
 	 */
 	sigfastblock_setpend(td, resched_sigs);
+
+	/*
+	 * Clear td_sa.code: signal to ptrace that syscall arguments
+	 * are unavailable after this point. This AST handler is the
+	 * last chance for ptracestop() to signal the tracer before
+	 * the tracee returns to userspace.
+	 */
+	td->td_sa.code = 0;
 }
 
 static void
diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index 581f8f65ec56..c7dd505d97ca 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -1168,7 +1168,8 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 
 	case PT_GET_SC_ARGS:
 		CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
-		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
+		if (((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 &&
+		     td2->td_sa.code == 0)
 #ifdef COMPAT_FREEBSD32
 		    || (wrap32 && !safe)
 #endif
@@ -1511,12 +1512,9 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 		pl->pl_sigmask = td2->td_sigmask;
 		pl->pl_siglist = td2->td_siglist;
 		strcpy(pl->pl_tdname, td2->td_name);
-		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
+		if (td2->td_sa.code != 0) {
 			pl->pl_syscall_code = td2->td_sa.code;
 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
-		} else {
-			pl->pl_syscall_code = 0;
-			pl->pl_syscall_narg = 0;
 		}
 		CTR6(KTR_PTRACE,
     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",