git: 6189085715db - main - [ku]trace(2): add kern_[ku]trace

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 11:54:22 UTC
The branch main has been updated by brooks:

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

commit 6189085715dbd71ba6aff1b965950f3331ba1128
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2025-11-21 17:42:02 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-11-09 11:28:40 +0000

    [ku]trace(2): add kern_[ku]trace
    
    Effort:         CHERI upstreaming
    Sponsored by:   Innovate UK
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2284
---
 sys/kern/kern_ktrace.c | 34 +++++++++++++++++++++++-----------
 sys/sys/syscallsubr.h  |  3 +++
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c
index f3ee1c53fafd..d6fcbe5cbe56 100644
--- a/sys/kern/kern_ktrace.c
+++ b/sys/kern/kern_ktrace.c
@@ -50,6 +50,7 @@
 #include <sys/proc.h>
 #include <sys/resourcevar.h>
 #include <sys/socket.h>
+#include <sys/syscallsubr.h>
 #include <sys/stat.h>
 #include <sys/sx.h>
 #include <sys/sysctl.h>
@@ -1097,14 +1098,20 @@ struct ktrace_args {
 /* ARGSUSED */
 int
 sys_ktrace(struct thread *td, struct ktrace_args *uap)
+{
+	return (kern_ktrace(td, uap->fname, uap->ops, uap->facs, uap->pid));
+}
+
+int
+kern_ktrace(struct thread *td, const char *fname, int uops, int ufacs, int pid)
 {
 #ifdef KTRACE
 	struct vnode *vp = NULL;
 	struct proc *p;
 	struct pgrp *pg;
-	int facs = uap->facs & ~KTRFAC_ROOT;
-	int ops = KTROP(uap->ops);
-	int descend = uap->ops & KTRFLAG_DESCEND;
+	int facs = ufacs & ~KTRFAC_ROOT;
+	int ops = KTROP(uops);
+	int descend = uops & KTRFLAG_DESCEND;
 	int ret = 0;
 	int flags, error = 0;
 	struct nameidata nd;
@@ -1121,7 +1128,7 @@ sys_ktrace(struct thread *td, struct ktrace_args *uap)
 		/*
 		 * an operation which requires a file argument.
 		 */
-		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
+		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, fname);
 		flags = FREAD | FWRITE | O_NOFOLLOW;
 		error = vn_open(&nd, &flags, 0, NULL);
 		if (error)
@@ -1169,11 +1176,11 @@ restart:
 	 * do it
 	 */
 	sx_slock(&proctree_lock);
-	if (uap->pid < 0) {
+	if (pid < 0) {
 		/*
 		 * by process group
 		 */
-		pg = pgfind(-uap->pid);
+		pg = pgfind(-pid);
 		if (pg == NULL) {
 			sx_sunlock(&proctree_lock);
 			error = ESRCH;
@@ -1201,7 +1208,7 @@ restart:
 		/*
 		 * by pid
 		 */
-		p = pfind(uap->pid);
+		p = pfind(pid);
 		if (p == NULL) {
 			error = ESRCH;
 			sx_sunlock(&proctree_lock);
@@ -1233,7 +1240,12 @@ done:
 int
 sys_utrace(struct thread *td, struct utrace_args *uap)
 {
+	return (kern_utrace(td, uap->addr, uap->len));
+}
 
+int
+kern_utrace(struct thread *td, const void *addr, size_t len)
+{
 #ifdef KTRACE
 	struct ktr_request *req;
 	void *cp;
@@ -1241,10 +1253,10 @@ sys_utrace(struct thread *td, struct utrace_args *uap)
 
 	if (!KTRPOINT(td, KTR_USER))
 		return (0);
-	if (uap->len > KTR_USER_MAXLEN)
+	if (len > KTR_USER_MAXLEN)
 		return (EINVAL);
-	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
-	error = copyin(uap->addr, cp, uap->len);
+	cp = malloc(len, M_KTRACE, M_WAITOK);
+	error = copyin(addr, cp, len);
 	if (error) {
 		free(cp, M_KTRACE);
 		return (error);
@@ -1255,7 +1267,7 @@ sys_utrace(struct thread *td, struct utrace_args *uap)
 		return (ENOMEM);
 	}
 	req->ktr_buffer = cp;
-	req->ktr_header.ktr_len = uap->len;
+	req->ktr_header.ktr_len = len;
 	ktr_submitrequest(td, req);
 	return (0);
 #else /* !KTRACE */
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
index fc452fe88647..b81d8129c746 100644
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -296,6 +296,8 @@ int	kern_kmq_timedsend(struct thread *td, int, const char *,
 	    size_t, unsigned int, const struct timespec *);
 int	kern_kqueue(struct thread *td, int flags, bool cponfork,
 	    struct filecaps *fcaps);
+int	kern_ktrace(struct thread *td, const char *fname, int uops, int ufacs,
+	    int pid);
 int	kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
 	    const char *path2, enum uio_seg segflg, int flag);
 int	kern_listen(struct thread *td, int s, int backlog);
@@ -478,6 +480,7 @@ int	kern_utimesat(struct thread *td, int fd, const char *path,
 int	kern_utimensat(struct thread *td, int fd, const char *path,
 	    enum uio_seg pathseg, const struct timespec *tptr,
 	    enum uio_seg tptrseg, int flag);
+int	kern_utrace(struct thread *td, const void *addr, size_t len);
 int	kern_wait(struct thread *td, pid_t pid, int *status, int options,
 	    struct rusage *rup);
 int	kern_wait4(struct thread *td, int pid, int *statusp, int options,