KTRACE(2)
β¨ KTRACE(2) β¨
okay bestie, welcome to the ktrace manual β that's the process tracing system call from the System Calls Manual. yes we're in the big leagues now. it lives in the Standard C Library (libc, -lc), so you're not even gonna have to link anything extra. slay. π
πΈ SYNOPSIS πΈ
like, here's the vibe, literally copy-paste it:
#include <sys/param.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/ktrace.h>
int
ktrace(const char *tracefile, int ops, int trpoints, int pid);
four arguments. that's it. don't be scared. π«Ά
π¦ DESCRIPTION π¦
so the ktrace() system call enables or disables tracing of one or more processes. and the rule is: users may only trace their own processes. no snooping on other people's business, periodt. and only the superuser can trace setuid or setgid programs, because obviously. π₯
The tracefile argument gives the pathname of the file to be used for tracing. The file must exist and be a regular file writable by the calling process. All trace records are always appended to the file, so the file must be truncated to zero length to discard previous trace data. If tracing points are being disabled (see KTROP_CLEAR below), tracefile may be NULL.
β οΈ note the append thing bestie β it never rewrites, it just keeps piling on. if you want a fresh file you gotta truncate it to zero length yourself. no cap.
The ops argument specifies the requested ktrace operation. The defined operations are:
KTROP_SETβ enable trace points specified intrpoints.KTROP_CLEARβ disable trace points specified intrpoints.KTROP_CLEARFILEβ stop all tracing. like, all of it. bye. πKTRFLAG_DESCENDβ the tracing change should apply to the specified process and all its current children. it's giving generational. π
The trpoints argument specifies the trace points of interest. The defined trace points are:
KTRFAC_SYSCALLβ trace system calls.KTRFAC_SYSRETβ trace return values from system calls.KTRFAC_NAMEIβ trace name lookup operations.KTRFAC_GENIOβ trace all I/O. π«£ note that this option can generate much output. like, SO much. your trace file is gonna be huge, be warned.KTRFAC_PSIGβ trace posted signals.KTRFAC_CSWβ trace context switch points.KTRFAC_USERβ trace application-specific events.KTRFAC_STRUCTβ trace certain data structures.KTRFAC_SYSCTLβ trace sysctls.KTRFAC_PROCCTORβ trace process construction. π£KTRFAC_PROCDTORβ trace process destruction. πKTRFAC_CAPFAILβ trace capability failures.KTRFAC_FAULTβ trace page faults.KTRFAC_FAULTENDβ trace the end of page faults.KTRFAC_INHERITβ inherit tracing to future children. it's hereditary, iykyk. π§¬
Each tracing event outputs a record composed of a generic header followed by a trace point specific structure. The generic header is:
struct ktr_header {
int ktr_len; /* length of buf */
short ktr_type; /* trace record type */
pid_t ktr_pid; /* process id */
char ktr_comm[MAXCOMLEN+1]; /* command name */
struct timeval ktr_time; /* timestamp */
intptr_t ktr_tid; /* was ktr_buffer */
};
and what do all those lil fields mean, you ask? π
ktr_lenβ the length of thektr_typedata that follows this header.ktr_pidandktr_commβ the process and command generating the record.ktr_timeβ the time (with microsecond resolution) that the record was generated.ktr_tidβ holds a thread id.
The generic header is followed by ktr_len bytes of a ktr_type record. The type specific records are defined in the <sys/ktrace.h> include file. go read it, it's iconic. β
π SYSCTL TUNABLES π
these sysctl(8) tunables are the ones that influence the behaviour of ktrace():
kern.ktrace.geniosizeβ bounds the amount of data a traced I/O request will log to the trace file.kern.ktrace.request_poolβ bounds the number of trace events being logged at a time.
Sysctl tunables that control process debuggability (as determined by p_candebug(9)) also affect the operation of ktrace(). so many levers, so much power. β¨
π RETURN VALUES π
The ktrace() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic. zero means we ate, negative one means we're crying. π
π ERRORS π
The ktrace() system call will fail if:
[ENOTDIR]β a component of the path prefix is not a directory.[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.[ENOENT]β the namedtracefiledoes not exist.[EACCES]β search permission is denied for a component of the path prefix.[ELOOP]β too many symbolic links were encountered in translating the pathname.[EIO]β an I/O error occurred while reading from or writing to the file system.[EINTEGRITY]β corrupted data was detected while reading from the file system. π«[ENOSYS]β the kernel was not compiled with ktrace support. (rip, no tracing for you.)
π¨ also important, bestie: a thread may be unable to log one or more tracing events due to a temporary shortage of resources. This condition is remembered by the kernel, and the next tracing request that succeeds will have the flagKTR_DROPset in itsktr_typefield. so if you seeKTR_DROP, that's your sign that something got dropped. not dramatic, just facts.
π SEE ALSO π
go read those too, they're the supporting cast and they're all main characters. π«Ά
π HISTORY π
The ktrace() system call first appeared in 4.4BSD. a true elder icon. π
okay that's the whole page bestie, you're officially a tracing girly now. this Debian version is dated March 30, 2020, and yeah it's still living rent free in section 2 of the manual. go forth and trace, but only your own processes. ππ¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms