Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178368264
D59275.1796595018.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
28 KB
Referenced Files
None
Subscribers
None
D59275.1796595018.diff
View Options
diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1251,6 +1251,8 @@
..
truncate
..
+ truss
+ ..
tsort
..
unifdef
diff --git a/usr.bin/truss/Makefile b/usr.bin/truss/Makefile
--- a/usr.bin/truss/Makefile
+++ b/usr.bin/truss/Makefile
@@ -1,9 +1,14 @@
+.include <src.opts.mk>
+
PROG= truss
-SRCS= main.c setup.c syscalls.c
+SRCS= main.c setup.c syscall_filter.c syscalls.c
LIBADD= sysdecode
#CFLAGS+= -I${.CURDIR} -I. -I${SRCTOP}/sys
CFLAGS+= -I${SRCTOP}/sys
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
.include <bsd.prog.mk>
diff --git a/usr.bin/truss/extern.h b/usr.bin/truss/extern.h
--- a/usr.bin/truss/extern.h
+++ b/usr.bin/truss/extern.h
@@ -31,6 +31,9 @@
* SUCH DAMAGE.
*/
+extern void add_syscall_filter(const char *);
+extern void list_syscall_groups(void);
+extern bool syscall_filter_match(const char *);
extern int print_line_prefix(struct trussinfo *);
extern void setup_and_wait(struct trussinfo *, char **);
extern void start_tracing(struct trussinfo *, pid_t);
diff --git a/usr.bin/truss/main.c b/usr.bin/truss/main.c
--- a/usr.bin/truss/main.c
+++ b/usr.bin/truss/main.c
@@ -56,9 +56,11 @@
static __dead2 void
usage(void)
{
- fprintf(stderr, "%s\n%s\n",
- "usage: truss [-cfaedDHS] [-o file] [-s strsize] -p pid",
- " truss [-cfaedDHS] [-o file] [-s strsize] command [args]");
+ fprintf(stderr, "%s\n%s\n%s\n",
+ "usage: truss [-cfaedDHS] [-o file] [-s strsize] [-t expr] -p pid",
+ " truss [-cfaedDHS] [-o file] [-s strsize] [-t expr] "
+ "command [args]",
+ " truss -t");
exit(1);
}
@@ -85,7 +87,13 @@
trussinfo->strsize = 32;
trussinfo->curthread = NULL;
LIST_INIT(&trussinfo->proclist);
- while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) {
+ /*
+ * The leading ':' asks getopt() to report a missing option
+ * argument as ':' rather than '?' so that a bare -t, which lists
+ * the system call groups, can be told from a malformed option.
+ * Diagnosing the other two cases then falls to us.
+ */
+ while ((c = getopt(ac, av, ":p:o:facedDs:t:SH")) != -1) {
switch (c) {
case 'p': /* specified pid */
pid = atoi(optarg);
@@ -121,13 +129,25 @@
if (errstr)
errx(1, "maximum string size is %s: %s", errstr, optarg);
break;
+ case 't': /* Select the system calls to trace */
+ add_syscall_filter(optarg);
+ break;
case 'S': /* Don't trace signals */
trussinfo->flags |= NOSIGS;
break;
case 'H':
trussinfo->flags |= DISPLAYTIDS;
break;
+ case ':':
+ if (optopt == 't') {
+ /* A bare -t lists the system call groups. */
+ list_syscall_groups();
+ return (0);
+ }
+ warnx("option requires an argument -- %c", optopt);
+ usage();
default:
+ warnx("illegal option -- %c", optopt);
usage();
}
}
diff --git a/usr.bin/truss/setup.c b/usr.bin/truss/setup.c
--- a/usr.bin/truss/setup.c
+++ b/usr.bin/truss/setup.c
@@ -472,7 +472,7 @@
}
sc = get_syscall(t, t->cs.number, narg);
- if (sc->unknown)
+ if (sc->unknown && sc->trace)
fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
t->proc->abi->type, t->cs.number);
@@ -481,6 +481,15 @@
t->cs.sc = sc;
+ /*
+ * A system call excluded by -t is never printed, so there is no
+ * point in formatting its arguments.
+ */
+ if (!sc->trace) {
+ clock_gettime(CLOCK_REALTIME, &t->before);
+ return;
+ }
+
/*
* At this point, we set up the system call arguments.
* We ignore any OUT ones, however -- those are arguments that
@@ -554,9 +563,10 @@
sc = t->cs.sc;
/*
* Here, we only look for arguments that have OUT masked in --
- * otherwise, they were handled in enter_syscall().
+ * otherwise, they were handled in enter_syscall(). A system call
+ * excluded by -t is never printed, so none of them are needed.
*/
- for (i = 0; i < sc->decode.nargs; i++) {
+ for (i = 0; i < sc->decode.nargs && sc->trace; i++) {
char *temp;
if (sc->decode.args[i].type & OUT) {
diff --git a/usr.bin/truss/syscall.h b/usr.bin/truss/syscall.h
--- a/usr.bin/truss/syscall.h
+++ b/usr.bin/truss/syscall.h
@@ -224,6 +224,7 @@
struct timespec time; /* Time spent for this call */
int ncalls; /* Number of calls */
int nerror; /* Number of calls that returned with error */
+ bool trace; /* Selected for reporting by -t */
bool unknown; /* Unknown system call */
};
diff --git a/usr.bin/truss/syscall_filter.c b/usr.bin/truss/syscall_filter.c
new file mode 100644
--- /dev/null
+++ b/usr.bin/truss/syscall_filter.c
@@ -0,0 +1,410 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
+ */
+
+/*
+ * Selection of the system calls to report, driven by -t.
+ */
+
+#include <sys/param.h>
+#include <sys/queue.h>
+
+#include <err.h>
+#include <fnmatch.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysdecode.h>
+
+#include "truss.h"
+#include "extern.h"
+
+/*
+ * Groups of related system calls, named as "@group" in a -t expression.
+ *
+ * A group is a list of fnmatch(3) patterns matched against the name of a
+ * system call. A member beginning with '@' names another group whose
+ * members are included in this one, so that a group can be built from
+ * smaller ones without repeating them.
+ *
+ * Patterns are preferred over literal names wherever a family of system
+ * calls shares a naming convention (e.g. "extattr_*_file"), so that
+ * system calls added later are picked up without further change here.
+ * Names from the ABIs truss supports beyond the native one are included
+ * where they differ, since the Linux ABI in particular renames a number
+ * of otherwise familiar system calls.
+ *
+ * Adding a group is a matter of adding a member list here and a single
+ * entry to syscall_groups[] below.
+ */
+
+static const char *const group_all[] = {
+ "*",
+ NULL
+};
+
+static const char *const group_read[] = {
+ "read", "readv", "pread*", "readahead", "readdir",
+ "recv", "recvfrom", "recvmsg", "recvmmsg*",
+ "aio_read*", "sctp_generic_recvmsg",
+ "kmq_timedreceive", "mq_timedreceive*", "msgrcv",
+ "getdents*", "getdirentries", "copy_file_range", "process_vm_readv",
+ NULL
+};
+
+static const char *const group_write[] = {
+ "write", "writev", "pwrite*",
+ "send", "sendto", "sendmsg", "sendmmsg*", "sendfile*",
+ "aio_write*", "sctp_generic_send*",
+ "kmq_timedsend", "mq_timedsend*", "msgsnd",
+ "copy_file_range", "process_vm_writev",
+ NULL
+};
+
+static const char *const group_desc[] = {
+ "@read", "@write",
+ "close", "close_range", "closefrom", "dup", "dup2", "dup3",
+ "fcntl", "fcntl64", "flock", "fsync", "fdatasync", "syncfs",
+ "sync", "sync_file_range", "ftruncate*", "lseek", "llseek", "ioctl",
+ "poll", "ppoll*", "select", "old_select", "pselect*",
+ "epoll_*", "kqueue*", "kevent*", "eventfd*", "timerfd_*",
+ "inotify_*", "signalfd*", "fanotify_*", "pipe", "pipe2",
+ "fstat", "fstat64", "newfstat", "nfstat", "fstatfs*",
+ "fchdir", "fchmod", "fchown", "fchflags", "futimes", "futimens",
+ "fpathconf", "getdtablesize", "fexecve", "fspacectl",
+ "posix_fadvise", "fadvise64*", "posix_fallocate", "fallocate",
+ "splice", "tee", "vmsplice", "memfd_create", "__specialfd",
+ "posix_openpt", "pddupfd", "aio_*", "lio_listio",
+ "pidfd_*", "io_*", "f*xattr",
+ "__acl_*_fd", "extattr_*_fd", "__mac_*_fd",
+ "cap_fcntls_*", "cap_ioctls_*", "cap_rights_limit",
+ "__cap_rights_get",
+ NULL
+};
+
+static const char *const group_file[] = {
+ "open", "openat", "openat2", "open_by_handle_at", "open_tree",
+ "creat", "stat", "stat64", "newstat", "nstat", "statx",
+ "lstat", "lstat64", "newlstat", "nlstat",
+ "fstatat", "fstatat64", "newfstatat", "statfs", "statfs64",
+ "access", "eaccess", "faccessat*",
+ "chdir", "chroot", "fchroot", "pivot_root",
+ "chmod", "lchmod", "fchmodat*",
+ "chown", "chown16", "lchown", "lchown16", "fchownat",
+ "chflags", "lchflags", "chflagsat",
+ "link", "linkat", "symlink", "symlinkat",
+ "unlink", "unlinkat", "funlinkat",
+ "rename", "renameat", "renameat2",
+ "mkdir", "mkdirat", "rmdir", "mknod", "mknodat",
+ "mkfifo", "mkfifoat", "readlink", "readlinkat",
+ "truncate", "truncate64",
+ "utime", "utimes", "lutimes", "utimensat*", "futimesat",
+ "pathconf", "lpathconf", "__getcwd", "getcwd", "__realpathat",
+ "revoke", "undelete", "acct", "quotactl*", "umask",
+ "mount", "nmount", "unmount", "umount", "oldumount", "move_mount",
+ "fh*", "getfh", "getfhat", "lgetfh", "getfsstat",
+ "name_to_handle_at", "inotify_add_watch*",
+ "execve", "execveat", "__mac_execve",
+ "swapon", "swapoff", "kldload",
+ "getxattr", "lgetxattr", "setxattr", "lsetxattr",
+ "removexattr", "lremovexattr", "listxattr", "llistxattr",
+ "__acl_*_file", "__acl_*_link", "extattr_*_file", "extattr_*_link",
+ "extattrctl", "__mac_*_file", "__mac_*_link",
+ NULL
+};
+
+static const char *const group_net[] = {
+ "socket", "socketcall", "socketpair", "bind", "bindat",
+ "connect", "connectat", "listen", "accept", "accept4",
+ "getpeername", "getsockname", "getsockopt", "setsockopt", "shutdown",
+ "send", "sendto", "sendmsg", "sendmmsg*", "sendfile*",
+ "recv", "recvfrom", "recvmsg", "recvmmsg*", "sctp_*",
+ "setfib", "gethostname", "sethostname",
+ "getdomainname", "setdomainname",
+ "nfssvc", "nlm_syscall", "rpctls_syscall",
+ NULL
+};
+
+static const char *const group_proc[] = {
+ "fork", "vfork", "rfork", "pdfork", "pdrfork", "clone", "clone3",
+ "execve", "execveat", "fexecve", "__mac_execve",
+ "_exit", "exit", "exit_group", "abort2",
+ "wait", "wait4", "wait6", "waitid", "waitpid", "pdwait",
+ "getpid", "getppid", "gettid", "getpgrp", "getpgid", "setpgid",
+ "getsid", "setsid", "getpriority", "setpriority", "nice",
+ "rtprio", "rtprio_thread", "sched_*", "cpuset*",
+ "procctl", "prctl", "arch_prctl", "ptrace",
+ "thr_*", "_umtx_*", "futex*", "sys_futex*", "membarrier",
+ "pdgetpid", "pdkill", "pdopenpid", "pidfd_open", "pidfd_getfd",
+ "jail*", "kcmp", "getcontext", "setcontext", "swapcontext", "yield",
+ "getrusage", "getrlimit", "setrlimit", "getrlimitusage",
+ "old_getrlimit", "prlimit64", "personality", "times", "vhangup",
+ "set_tid_address", "setns", "unshare", "restart_syscall", "rseq",
+ "get_robust_list", "set_robust_list",
+ NULL
+};
+
+static const char *const group_signal[] = {
+ "sig*", "rt_sig*", "rt_tgsigqueueinfo",
+ "kill", "killpg", "thr_kill*", "pdkill",
+ "tkill", "tgkill", "pidfd_send_signal",
+ "sgetmask", "ssetmask", "pause",
+ NULL
+};
+
+static const char *const group_memory[] = {
+ "mmap", "mmap2", "munmap", "mprotect", "pkey_mprotect", "mremap",
+ "madvise", "process_madvise", "mincore", "minherit",
+ "mlock", "mlock2", "munlock", "mlockall", "munlockall", "aio_mlock",
+ "msync", "break", "brk", "sbrk", "vadvise", "getpagesize",
+ "shm_open*", "shm_unlink", "shm_rename",
+ "memfd_create", "memfd_secret", "map_shadow_stack", "userfaultfd",
+ "mbind", "get_mempolicy", "set_mempolicy*",
+ "migrate_pages", "move_pages", "remap_file_pages",
+ "pkey_alloc", "pkey_free", "swapon", "swapoff",
+ NULL
+};
+
+static const char *const group_ipc[] = {
+ "msgctl", "msgget", "msgrcv", "msgsnd", "msgsys",
+ "semctl", "__semctl", "semget", "semop", "semsys", "semtimedop*",
+ "shmat", "shmctl", "shmdt", "shmget", "shmsys",
+ "ksem_*", "kmq_*", "mq_*", "ipc",
+ NULL
+};
+
+static const char *const group_creds[] = {
+ "getuid*", "geteuid*", "getgid*", "getegid*",
+ "getgroups*", "setgroups*",
+ "setuid*", "seteuid", "setgid*", "setegid",
+ "setreuid*", "setregid*", "setresuid*", "setresgid*",
+ "getresuid*", "getresgid*", "setfsuid*", "setfsgid*",
+ "issetugid", "__setugid", "setcred",
+ "getlogin", "setlogin", "getloginclass", "setloginclass",
+ "getauid", "setauid", "getaudit*", "setaudit*",
+ "audit", "auditon", "auditctl",
+ "capget", "capset", "cap_enter", "cap_getmode",
+ "seccomp", "landlock_*",
+ NULL
+};
+
+static const char *const group_time[] = {
+ "clock_*", "nanosleep", "gettimeofday", "settimeofday",
+ "adjtime", "adjtimex", "ntp_*",
+ "getitimer", "setitimer", "ktimer_*", "timer_*", "timerfd_*",
+ "ffclock_*", "time", "stime", "alarm",
+ NULL
+};
+
+struct syscall_group {
+ const char *name;
+ const char *desc;
+ const char *const *members;
+};
+
+/* Kept in alphabetical order; "truss -t list" prints it as-is. */
+static const struct syscall_group syscall_groups[] = {
+ { "all", "every system call", group_all },
+ { "creds", "get or set process credentials", group_creds },
+ { "desc", "operate on a file descriptor", group_desc },
+ { "file", "operate on a pathname", group_file },
+ { "ipc", "System V and POSIX IPC", group_ipc },
+ { "memory", "memory mapping and locking", group_memory },
+ { "net", "network and socket operations", group_net },
+ { "proc", "process and thread lifecycle", group_proc },
+ { "read", "read data from a descriptor", group_read },
+ { "signal", "signal delivery and handling", group_signal },
+ { "time", "clocks, timers and sleeping", group_time },
+ { "write", "write data to a descriptor", group_write },
+};
+
+/*
+ * One comma-separated term of a -t expression. Terms are held in the
+ * order they were given: the last one to match a system call decides
+ * whether it is reported.
+ */
+struct filter_term {
+ STAILQ_ENTRY(filter_term) entries;
+ const struct syscall_group *group; /* NULL for a pattern term */
+ char *pattern; /* NULL for a group term */
+ bool negate;
+};
+
+static STAILQ_HEAD(, filter_term) filter_terms =
+ STAILQ_HEAD_INITIALIZER(filter_terms);
+
+/*
+ * Whether a system call matched by no term at all is reported. An
+ * expression made up only of negated terms subtracts from the full set
+ * of system calls; any other expression selects from an empty one.
+ */
+static bool filter_default = true;
+
+/*
+ * A name reported by sysdecode may carry a prefix naming a compatibility
+ * layer ("compat11.stat"), a non-native ABI ("linux_open",
+ * "freebsd32_ioctl"), or both ("compat4.freebsd32_getfsstat"). Terms are
+ * matched against the name as displayed and against each shortened form,
+ * so that "-t @file" selects stat, compat11.stat and freebsd32_stat alike.
+ */
+static const char *const abi_prefixes[] = {
+ "freebsd32_",
+ "linux_",
+ "linux32_",
+};
+
+/*
+ * A group referring to itself, directly or through others, would recurse
+ * forever. The table above has no such cycle; this only keeps a future
+ * mistake in it from hanging truss.
+ */
+#define GROUP_MAX_DEPTH 8
+
+static const struct syscall_group *
+find_group(const char *name)
+{
+ size_t i;
+
+ for (i = 0; i < nitems(syscall_groups); i++) {
+ if (strcmp(name, syscall_groups[i].name) == 0)
+ return (&syscall_groups[i]);
+ }
+ return (NULL);
+}
+
+static bool
+group_match(const struct syscall_group *group, const char *name, u_int depth)
+{
+ const struct syscall_group *ref;
+ const char *const *member;
+
+ if (depth >= GROUP_MAX_DEPTH)
+ return (false);
+ for (member = group->members; *member != NULL; member++) {
+ if (**member == '@') {
+ ref = find_group(*member + 1);
+ if (ref != NULL && group_match(ref, name, depth + 1))
+ return (true);
+ } else if (fnmatch(*member, name, 0) == 0)
+ return (true);
+ }
+ return (false);
+}
+
+static const char *
+strip_abi_prefix(const char *name)
+{
+ size_t i, len;
+
+ for (i = 0; i < nitems(abi_prefixes); i++) {
+ len = strlen(abi_prefixes[i]);
+ if (strncmp(name, abi_prefixes[i], len) == 0)
+ return (name + len);
+ }
+ return (name);
+}
+
+/* Print the group table ("-t" with no expression). */
+void
+list_syscall_groups(void)
+{
+ size_t i;
+
+ printf("System call groups usable as @group in a -t expression:\n\n");
+ for (i = 0; i < nitems(syscall_groups); i++)
+ printf(" @%-9s %s\n", syscall_groups[i].name,
+ syscall_groups[i].desc);
+ printf("\n"
+ "Any other term is an fnmatch(3) pattern matched against the\n"
+ "system call name, so \"read\" selects read(2) alone and \"read*\"\n"
+ "also selects readv(2) and readlink(2). A term prefixed with '!'\n"
+ "excludes what it matches rather than including it.\n");
+}
+
+/*
+ * Add the terms of one -t expression. Repeating -t appends to the
+ * expression rather than replacing it.
+ */
+void
+add_syscall_filter(const char *expr)
+{
+ struct filter_term *term;
+ char *copy, *next, *word;
+
+ if ((copy = strdup(expr)) == NULL)
+ err(1, "strdup");
+ next = copy;
+ while ((word = strsep(&next, ",")) != NULL) {
+ if ((term = calloc(1, sizeof(*term))) == NULL)
+ err(1, "calloc");
+ if (*word == '!') {
+ term->negate = true;
+ word++;
+ }
+ if (*word == '\0')
+ errx(1, "empty term in -t %s", expr);
+ if (*word == '@') {
+ term->group = find_group(word + 1);
+ if (term->group == NULL)
+ errx(1, "unknown system call group @%s; "
+ "\"truss -t\" lists them", word + 1);
+ } else if ((term->pattern = strdup(word)) == NULL)
+ err(1, "strdup");
+ if (!term->negate)
+ filter_default = false;
+ STAILQ_INSERT_TAIL(&filter_terms, term, entries);
+ }
+ free(copy);
+}
+
+static bool
+term_match(const struct filter_term *term, const char *name)
+{
+
+ if (term->group != NULL)
+ return (group_match(term->group, name, 0));
+ return (fnmatch(term->pattern, name, 0) == 0);
+}
+
+/*
+ * Report whether a system call with the given name is to be traced.
+ * With no -t expression every system call is, as before.
+ */
+bool
+syscall_filter_match(const char *name)
+{
+ const struct filter_term *term;
+ const char *names[3];
+ const char *shorter, *stripped;
+ u_int i, nnames;
+ bool trace;
+
+ if (STAILQ_EMPTY(&filter_terms))
+ return (true);
+
+ nnames = 0;
+ names[nnames++] = name;
+ shorter = strrchr(name, '.');
+ if (shorter != NULL)
+ names[nnames++] = ++shorter;
+ else
+ shorter = name;
+ stripped = strip_abi_prefix(shorter);
+ if (stripped != shorter)
+ names[nnames++] = stripped;
+
+ trace = filter_default;
+ STAILQ_FOREACH(term, &filter_terms, entries) {
+ for (i = 0; i < nnames; i++) {
+ if (term_match(term, names[i])) {
+ trace = !term->negate;
+ break;
+ }
+ }
+ }
+ return (trace);
+}
diff --git a/usr.bin/truss/syscalls.c b/usr.bin/truss/syscalls.c
--- a/usr.bin/truss/syscalls.c
+++ b/usr.bin/truss/syscalls.c
@@ -919,6 +919,8 @@
name, strlen(procabi->compat_prefix)) == 0)
lookup_name += strlen(procabi->compat_prefix);
+ sc->trace = syscall_filter_match(name);
+
for (i = 0; i < nitems(decoded_syscalls); i++) {
if (strcmp(lookup_name, decoded_syscalls[i].name) == 0) {
sc->decode = decoded_syscalls[i];
@@ -2844,6 +2846,8 @@
t = trussinfo->curthread;
sc = t->cs.sc;
+ if (!sc->trace)
+ return;
if (trussinfo->flags & COUNTONLY) {
timespecsub(&t->after, &t->before, &timediff);
timespecadd(&sc->time, &timediff, &sc->time);
diff --git a/usr.bin/truss/tests/Makefile b/usr.bin/truss/tests/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.bin/truss/tests/Makefile
@@ -0,0 +1,8 @@
+PACKAGE= tests
+
+ATF_TESTS_SH= truss_test
+
+DIRS+= TESTSDIR
+beforeinstall: installdirs-TESTSDIR
+
+.include <bsd.test.mk>
diff --git a/usr.bin/truss/tests/truss_test.sh b/usr.bin/truss/tests/truss_test.sh
new file mode 100644
--- /dev/null
+++ b/usr.bin/truss/tests/truss_test.sh
@@ -0,0 +1,258 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
+#
+
+# The system calls a program makes vary with the machine and with the
+# run-time linker, so these tests assert which system calls -t may and
+# may not report rather than the exact sequence of them.
+
+require_truss()
+{
+ truss -o /dev/null /usr/bin/true >/dev/null 2>&1 ||
+ atf_skip "unable to trace a child process here"
+}
+
+# Write the sorted, unique names of the system calls reported in a truss
+# output file to another file.
+syscall_names()
+{
+ sed -n 's/^\([a-zA-Z_][a-zA-Z0-9_.]*\)(.*$/\1/p' "$1" | sort -u > "$2"
+}
+
+# Fail unless every name in a file matches an extended regular expression.
+only_names()
+{
+ if grep -Ev "$1" "$2" > unexpected; then
+ atf_fail "reported system calls not selected by the filter:" \
+ "$(tr '\n' ' ' < unexpected)"
+ fi
+}
+
+# Fail if any name in a file matches an extended regular expression.
+no_name()
+{
+ if grep -E "$1" "$2" > unexpected; then
+ atf_fail "system calls excluded by the filter were reported:" \
+ "$(tr '\n' ' ' < unexpected)"
+ fi
+}
+
+# Fail unless at least one name in a file matches.
+some_name()
+{
+ grep -Eq "$1" "$2" ||
+ atf_fail "no system call matching $1 was reported"
+}
+
+atf_test_case list
+list_head()
+{
+ atf_set descr "-t with no expression prints the available groups"
+}
+list_body()
+{
+ atf_check -s exit:0 -o match:'@all' -o match:'@read' \
+ -o match:'@write' -o match:'@file' -o match:'@net' \
+ truss -t
+}
+
+atf_test_case unknown_group
+unknown_group_head()
+{
+ atf_set descr "an unknown @group is rejected"
+}
+unknown_group_body()
+{
+ atf_check -s exit:1 -e match:'unknown system call group @nosuch' \
+ truss -t @nosuch /usr/bin/true
+}
+
+atf_test_case empty_term
+empty_term_head()
+{
+ atf_set descr "an empty term is rejected"
+}
+empty_term_body()
+{
+ atf_check -s exit:1 -e match:'empty term' \
+ truss -t 'read,,write' /usr/bin/true
+ atf_check -s exit:1 -e match:'empty term' truss -t '!' /usr/bin/true
+ atf_check -s exit:1 -e match:'empty term' truss -t '' /usr/bin/true
+}
+
+atf_test_case by_name
+by_name_head()
+{
+ atf_set descr "a term naming one system call selects only that one"
+}
+by_name_body()
+{
+ require_truss
+ printf 'hello\n' > input
+
+ atf_check -s exit:0 -o inline:"hello\n" truss -o out -t read cat input
+ syscall_names out names
+ only_names '^read$' names
+ some_name '^read$' names
+}
+
+atf_test_case by_pattern
+by_pattern_head()
+{
+ atf_set descr "a term may be an fnmatch(3) pattern"
+}
+by_pattern_body()
+{
+ require_truss
+ atf_check ln -s target link
+
+ atf_check -s exit:0 -o ignore truss -o out -t 'readlink*' readlink link
+ syscall_names out names
+ only_names '^readlink' names
+ some_name '^readlink' names
+}
+
+atf_test_case group
+group_head()
+{
+ atf_set descr "an @group selects the system calls it names"
+}
+group_body()
+{
+ require_truss
+ printf 'hello\n' > input
+
+ atf_check -s exit:0 -o inline:"hello\n" truss -o out -t @read cat input
+ syscall_names out names
+ some_name '^read$' names
+ no_name '^(openat|close|mmap|munmap|mprotect)$' names
+}
+
+atf_test_case group_read_excludes_readlink
+group_read_excludes_readlink_head()
+{
+ atf_set descr "@read selects I/O reads but not readlink(2)"
+}
+group_read_excludes_readlink_body()
+{
+ require_truss
+ atf_check ln -s target link
+
+ # readlink(1) calls readlink(2), which @read must not select even
+ # though "read*" does.
+ atf_check -s exit:0 -o ignore truss -o out -t @read readlink link
+ syscall_names out names
+ no_name '^readlink' names
+ some_name '^read$' names
+
+ atf_check -s exit:0 -o ignore truss -o out2 -t 'read*' readlink link
+ syscall_names out2 names2
+ some_name '^readlink' names2
+}
+
+atf_test_case group_reference
+group_reference_head()
+{
+ atf_set descr "@desc includes the members of @read and @write"
+}
+group_reference_body()
+{
+ require_truss
+ printf 'hello\n' > input
+
+ atf_check -s exit:0 -o inline:"hello\n" truss -o out -t @desc cat input
+ syscall_names out names
+ some_name '^read$' names
+ some_name '^close$' names
+}
+
+atf_test_case negation
+negation_head()
+{
+ atf_set descr "a term prefixed with ! excludes what it matches"
+}
+negation_body()
+{
+ require_truss
+
+ atf_check -s exit:0 -o ignore truss -o out -t '!@all' /usr/bin/true
+ syscall_names out names
+ atf_check -o empty cat names
+
+ atf_check -s exit:0 -o ignore truss -o out2 -t '!@memory' /usr/bin/true
+ syscall_names out2 names2
+ no_name '^(mmap|munmap|mprotect)$' names2
+ some_name '.' names2
+}
+
+atf_test_case order
+order_head()
+{
+ atf_set descr "the last term to match a system call wins"
+}
+order_body()
+{
+ require_truss
+ printf 'hello\n' > input
+
+ atf_check -s exit:0 -o ignore truss -o out -t 'read,!read' cat input
+ syscall_names out names
+ atf_check -o empty cat names
+
+ atf_check -s exit:0 -o ignore truss -o out2 -t '!read,read' cat input
+ syscall_names out2 names2
+ only_names '^read$' names2
+ some_name '^read$' names2
+}
+
+atf_test_case accumulate
+accumulate_head()
+{
+ atf_set descr "repeating -t appends to the expression"
+}
+accumulate_body()
+{
+ require_truss
+ atf_check ln -s target link
+
+ atf_check -s exit:0 -o ignore \
+ truss -o out -t read -t readlink readlink link
+ syscall_names out names
+ only_names '^(read|readlink)$' names
+ some_name '^read$' names
+ some_name '^readlink$' names
+}
+
+atf_test_case count
+count_head()
+{
+ atf_set descr "-c counts only the selected system calls"
+}
+count_body()
+{
+ require_truss
+ printf 'hello\n' > input
+
+ atf_check -s exit:0 -o inline:"hello\n" \
+ truss -c -o out -t read cat input
+ atf_check -o match:'^read ' cat out
+ atf_check -s exit:1 -o empty grep -q '^openat' out
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case list
+ atf_add_test_case unknown_group
+ atf_add_test_case empty_term
+ atf_add_test_case by_name
+ atf_add_test_case by_pattern
+ atf_add_test_case group
+ atf_add_test_case group_read_excludes_readlink
+ atf_add_test_case group_reference
+ atf_add_test_case negation
+ atf_add_test_case order
+ atf_add_test_case accumulate
+ atf_add_test_case count
+}
diff --git a/usr.bin/truss/truss.1 b/usr.bin/truss/truss.1
--- a/usr.bin/truss/truss.1
+++ b/usr.bin/truss/truss.1
@@ -1,5 +1,5 @@
.\"
-.Dd June 18, 2025
+.Dd August 30, 2026
.Dt TRUSS 1
.Os
.Sh NAME
@@ -10,12 +10,16 @@
.Op Fl facedDHS
.Op Fl o Ar file
.Op Fl s Ar strsize
+.Op Fl t Ar expr
.Fl p Ar pid
.Nm
.Op Fl facedDHS
.Op Fl o Ar file
.Op Fl s Ar strsize
+.Op Fl t Ar expr
.Ar command Op Ar args
+.Nm
+.Fl t
.Sh DESCRIPTION
The
.Nm
@@ -76,6 +80,86 @@
The default
.Ar strsize
is 32.
+.It Fl t Ar expr
+Report only the system calls selected by
+.Ar expr ,
+a comma-separated list of terms.
+Each term is either the name of a group of related system calls,
+written
+.Cm @ Ns Ar group ,
+or an
+.Xr fnmatch 3
+pattern matched against the name of a system call.
+A term prefixed with
+.Ql \&!
+excludes the system calls it matches instead of including them.
+.Pp
+An expression whose terms are all negated subtracts from the set of
+every system call;
+any other expression selects from an empty set.
+Terms are applied in the order they are given,
+and the last one to match a system call decides whether it is reported.
+Repeating
+.Fl t
+appends to the expression rather than replacing it.
+.Pp
+The groups are:
+.Bl -tag -width ".Cm @signal" -compact -offset indent
+.It Cm @all
+every system call
+.It Cm @creds
+get or set process credentials
+.It Cm @desc
+operate on a file descriptor
+.It Cm @file
+operate on a pathname
+.It Cm @ipc
+System V and POSIX IPC
+.It Cm @memory
+memory mapping and locking
+.It Cm @net
+network and socket operations
+.It Cm @proc
+process and thread lifecycle
+.It Cm @read
+read data from a descriptor
+.It Cm @signal
+signal delivery and handling
+.It Cm @time
+clocks, timers and sleeping
+.It Cm @write
+write data to a descriptor
+.El
+.Pp
+.Cm @desc
+includes
+.Cm @read
+and
+.Cm @write .
+Giving
+.Fl t
+with no
+.Ar expr
+prints the groups above and exits.
+.Pp
+A system call is matched both under the name
+.Nm
+displays for it and under that name with any prefix naming a
+compatibility layer or a non-native ABI removed,
+so that
+.Fl t Cm @file
+selects
+.Ql compat11.stat
+and
+.Ql linux_newstat
+as well as
+.Ql stat .
+.Pp
+Because the set of system calls depends on the ABI of the traced
+process,
+a term naming a system call that does not exist is not an error;
+it simply never matches.
+An unknown group is an error.
.It Fl p Ar pid
Follow the process specified by
.Ar pid
@@ -99,12 +183,35 @@
.Pp
Follow an already-running process:
.Dl $ truss -p 34
+.Pp
+List the groups that
+.Fl t
+understands:
+.Dl $ truss -t
+.Pp
+Show only the file and network activity of a command:
+.Dl $ truss -t @file,@net fetch https://www.freebsd.org/
+.Pp
+Show everything a command does except its memory management:
+.Dl $ truss -t '!@memory' make buildworld
+.Pp
+Show the descriptor activity of a running process apart from the
+data transfers themselves:
+.Dl $ truss -t '@desc,!@read,!@write' -p 34
+.Pp
+Count the
+.Xr readlink 2
+and
+.Xr readlinkat 2
+calls made while starting a program:
+.Dl $ truss -c -t 'readlink*' /bin/ls
.Sh SEE ALSO
.Xr dtrace 1 ,
.Xr kdump 1 ,
.Xr ktrace 1 ,
.Xr ptrace 2 ,
.Xr utrace 2 ,
+.Xr fnmatch 3 ,
.Xr sysdecode 3
.Sh HISTORY
The
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 6, 10:10 PM (13 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42266382
Default Alt Text
D59275.1796595018.diff (28 KB)
Attached To
Mode
D59275: truss: add -t to select which system calls are reported
Attached
Detach File
Event Timeline