A mail daemon on a FreeBSD host hangs for a few seconds once an hour, at no particular minute, and has done so since Tuesday. The log has nothing to say about it. Restarting the daemon would make the problem disappear for the afternoon, along with any chance of finding it. The administrator types one line:
ktrace -i -t cs -f /var/tmp/mail.kt -p 4211
The command returns at once. There is no tracer sitting in the foreground and no session that dies with the laptop lid, so the administrator goes home. When the next hang comes, the kernel writes down every system call the daemon makes and every signal it receives, and the same goes for any child it starts from now on. On Monday, kdump -f /var/tmp/mail.kt turns the file into readable lines, and the culprit turns out to be a DNS lookup that waits on a resolver which no longer exists. The daemon never noticed it was being watched, which is rather the point.
ktrace came from Berkeley. The manual page says it "appeared in 4.4BSD", and the kernel file that does the work, kern_ktrace.c, carries a copyright of the Regents of the University of California dated 1989. It entered the FreeBSD tree on 24 May 1994 with the import of 4.4BSD-Lite, and has received 205 commits since. In 2005 Robert N. M. Watson put a second copyright line on the file, beneath Berkeley's, for his own work on it. Nobody has let it stand still: the newest change to kdump went in on 1 December 2026.
The kernel writes its own notes
The design fits in a paragraph, and the comment at the top of kern_ktrace.c supplies it. The ktrace(2) system call ties a file to a process. From then on, whenever that process makes a traced call, the kernel writes a record to the file, and "records can be written directly from the context that generates the event". The traced process does the writing itself, as part of the call it was making anyway. Only one moment is awkward: during a context switch the kernel may not sleep, so the record waits in a small queue and goes to disk "at a convenient moment -- either immediately, the next traceable event, at system call return, or at process exit".
What follows from this is the part that makes administrators fond of it. The tracer is a flag on the process and a file on a disk, and the ktrace command is only the switch. I checked this on the server behind this blog: I started a small loop in the measurement jail, ran ktrace -t c -p against it, and the command exited with status 0. No ktrace process remained. ps -o ktrace showed the trace flags still set on the loop (80000c06, for the record), and three seconds later the file held 25 calls that nobody had been present for. The trace runs until the process exits or ktrace -C clears it.
Two options carry the idea to whole families of processes. -d traces the children that already exist, and -i passes the trace on to every child yet to be born, which is how one follows a build, a shell script or a daemon that forks a worker per connection. Both have been there since the manual page of 1993.
The price of stopping
There is another way to trace a process, and FreeBSD ships that one as well. truss uses ptrace(2): a second process attaches to the first, the kernel stops the traced process at the entry to every system call and again at its exit, wakes the tracer, lets it read the registers and format a line, and only then lets the traced process carry on. The Linux tool everyone knows, strace, works the same way, through PTRACE_SYSCALL, which in the words of its manual page arranges for the tracee "to be stopped at the next entry to or exit from a system call".
Having both designs in one base system makes for an unusually clean experiment. On 4 December 2026 I ran the same task in the measurement jail on vga, a FreeBSD 15.1-RELEASE-p3 host with an Intel Core i5-13500: dd copying 200,000 single bytes from /dev/zero to /dev/null, which comes to 400,107 system calls. Five runs each, timed with /usr/bin/time -p:
without a trace 0.04 s
ktrace -t c 0.92 s
truss 3.94 s
Same machine, same kernel, same 400,107 calls, and the tracer that stops the process takes a little over four times as long. The difference is the round trip: two stops per call, two wake-ups of another process, and the register reading in between, all of which ktrace never does. The trace file came to 70,419,288 bytes, 176 bytes per call and its return, in a binary format that kdump decodes later, on this machine or any other.
strace has learnt a trick to stop less often. With --seccomp-bpf it asks the kernel to halt the tracee "only for the system calls that are being traced". Its manual page adds the conditions: the option "has no effect unless -f" is given, and it is "not applicable to processes attached using -p". The mail daemon of the opening, attached to while running, would be stopped at every call.
What the file holds
ktrace records far more than system calls, one letter per kind. The manual of 1993 knew four letters. FreeBSD 15.1 knows thirteen, among them page faults, context switches, sysctl requests, the arguments and environment of each execve, records a program writes about itself with utrace(2), and, the most recent arrival, the kernel's extended error reports.
One letter deserves a paragraph of its own. -t p records Capsicum capability violations, and the manual page notes that it does so "regardless of whether the process has actually entered capability mode". A developer can trace an unmodified program and get the list of every call that would fail inside the sandbox, before writing a line of sandboxing code. The base system uses that discipline widely enough that the trace from my measurement shows dd itself calling cap_enter before it reads its first byte.
kdump prints the file as one line per record, with process ID, command name and the call, its arguments and its return value, decoded with the same libsysdecode library that truss uses. -H adds the thread, -T the time, and -p picks one process out of a family. A trace taken on a server without a compiler can be copied to a laptop and read there at leisure, which is a small comfort at three in the morning and a large one the morning after.
The sheet
Efficiency. FreeBSD 15.1 (releng/15.1), lines without comments and blank lines: kern_ktrace.c 1,166 and sys/ktrace.h 222 in the kernel, the ktrace command 265, kdump 2,806, and the decoding library libsysdecode 1,747, which truss shares. The whole chain comes to 6,206 lines. strace 7.3 of 22 October 2026 holds 60,616 lines in the C and header files of its src directory alone, before its generated tables. The ktrace binary is 10,008 bytes and needs nothing beyond libc and libsys; kdump is 61,856 bytes. Measured overhead on 400,107 calls: 0.92 seconds against 3.94 for truss, median of five runs.
Security. Of the four questions: ktrace needs no privilege to trace one's own processes and root for anyone else's; it takes no data from the network; it parses nothing, since the kernel writes fixed binary records and kdump reads them in a separate process at a separate time; and it hands no rights to a tracer, because there is none. Two FreeBSD security advisories in thirty-two years name it, SA-14:12 and SA-25:04, both disclosures of a few bytes of kernel memory into the trace, neither allowing code to run.
Longevity. In 4.4BSD, in FreeBSD since 24 May 1994, 205 commits on the kernel file and changes as recent as this week. It is in the base system of FreeBSD, OpenBSD, NetBSD and DragonFly BSD, and the format and the tools have stayed in the family since Berkeley.
Stability. The manual page of 6 June 1993 lists nine options, -a, -C, -c, -d, -f, -g, -i, -p and -t, and four trace points, c, n, i and s. All nine and all four stand in the page for 15.1 with the same meaning. A command line from a 1993 handbook runs unchanged on a 2026 server.
Measured on 4 December 2026: lines counted with a script that strips C comments and blank lines; times with /usr/bin/time -p, five runs each; records counted with kdump; commits counted on the history of sys/kern/kern_ktrace.c; advisories on the FreeBSD advisory index. The script and the raw output are in the essay's notes.
The limit
The file grows quickly. The manual page warns that a traced process "can generate enormous amounts of log data quickly" and recommends learning ktrace -C before the first trace. Choosing the letters keeps it in hand: -t c alone produced the 70 MB above, and a trace of the default set on a busy daemon fills a disk faster.
The writing is not free. Single-byte dd is the worst case on purpose, and there the trace made the task twenty-odd times slower than without one, because every record is a write to a file. A process making a few thousand calls a second barely notices; for counting millions of events in aggregate, FreeBSD has DTrace in the base system.
Threads interleave. The kernel comment says plainly that "ordering guarantees are weak" when several threads write to one file. Each record carries process and thread ID, and kdump -H sorts them out.
The output wants kdump. The file is binary, and reading it takes kdump or a program written against the record format in sys/ktrace.h.
ktrace is the fly on the wall that keeps taking notes after everyone has gone home, and in thirty-two years it has never once asked the room to stand still.