KTR(4)
β¨ NAME β¨
ktr β the kernel tracing facility, and yeah bestie, this whole page is living in the Kernel Interfaces Manual π
β¨ SYNOPSIS β¨
options KTR
options ALQ
options KTR_ALQ
options KTR_COMPILE=(KTR_LOCK|KTR_INTR|KTR_PROC)
options KTR_CPUMASK=0x3
options KTR_ENTRIES=8192
options KTR_MASK=(KTR_INTR|KTR_PROC)
options KTR_VERBOSE
πΈ DESCRIPTION πΈ
okay so picture this: the ktr facility lets kernel events get logged while the kernel is out here doing its thing, so you can go back and look at them later when you're debugging. that's the whole vibe π
. the only mandatory option to turn ktr on is options KTR β like, that's the bare minimum, the main character requirement, no cap.
KTR_ENTRIES sets the size of the buffer of events. you can check the size of the buffer in the kernel that's currently running via the sysctl debug.ktr.entries. by default the buffer holds 1024 entries π«Ά.
π¦ Event Masking π¦
event levels can be turned on or off to trim down excessive and overly verbose logging β because girl, some of these events are a lot π. first up, you specify a mask of events at compile time via the KTR_COMPILE option, which limits which events actually get compiled into the kernel. the default for this option? all events enabled β¨.
secondly, the actual events logged while the kernel runs can be masked even further via the run time event mask. KTR_MASK sets the default value of the run time event mask. the runtime event mask can also be set by loader(8) through the debug.ktr.mask environment variable. it can also be examined and set after booting via the debug.ktr.mask sysctl. by default the run time mask is set to block any tracing π. the definitions of the event mask bits live in <sys/ktr_class.h>.
and there's more β there's also a CPU event mask, whose default value you can change via the KTR_CPUMASK option. when you use two or more parameters to KTR_CPUMASK, it is important they are not separated by whitespace (she means it π). a CPU has to have the bit corresponding to its logical id set in this bitmask for events happening on it to get logged. this mask can be set by loader(8) via the debug.ktr.cpumask environment variable. it can also be examined and set after booting via the debug.ktr.cpumask sysctl. by default, only CPUs specified in KTR_CPUMASK will log events. see sys/conf/NOTES for more info, iykyk π.
β Verbose Mode β
by default, events are only logged to the internal buffer so you can examine them later. but if the verbose flag is set, they get dumped to the kernel console too β extra, but in a useful way π«Ά. this flag can also be set from the loader via the debug.ktr.verbose environment variable, or it can be examined and set after booting via the debug.ktr.verbose sysctl. here's the tea:
- if the flag is set to zero, which is the default, verbose output is disabled.
- if the flag is set to one, the contents of the log message and the CPU number get printed to the kernel console.
- if the flag is greater than one, the filename and line number of the event get output to the console in addition to the log message and the CPU number π
and KTR_VERBOSE sets the flag to one.
π Examining the Events π
you can examine the KTR buffer from inside ddb(4) via the show ktr [/vV] command. this command displays the contents of the trace buffer one page at a time. at the --more-- prompt:
- Enter displays one more entry and then prompts again
- the spacebar displays another page of entries
- any other key quits
by default the timestamp, filename, and line number are not displayed with each log entry. if you specify the /v modifier, then they're displayed in addition to the normal output. if you specify the /V modifier, then just the timestamp is displayed in addition to the normal output. and heads up π«Ά β the events are displayed in reverse chronological order, meaning the most recent events show up first. iconic behavior honestly.
π₯ Logging ktr to Disk π₯
the KTR_ALQ option can be used to log ktr entries to disk for post analysis using the ktrdump(8) utility. this option depends on the ALQ option. because of the potentially high volume of trace messages, the trace mask should be selected carefully π. this feature is configured through a group of sysctls:
debug.ktr.alq_fileβ displays or sets the file thatktrwill log to. by default its value is/tmp/ktr.out. if the file name is changed whilektris enabled, it will not take effect until the next invocation.debug.ktr.alq_enableβ enables logging ofktrentries to disk if it's set to one. setting this to0will terminate logging to disk and revert to logging to the normalktrring buffer. data is not sent to the ring buffer while logging to disk.debug.ktr.alq_maxβ the maximum number of entries that will be recorded to disk, or0for infinite. this is helpful for limiting the number of particularly high frequency entries that get recorded.debug.ktr.alq_depthβ determines the number of entries in the write buffer. this is the buffer that holds entries before they're written to disk, and it defaults to the value of theKTR_ENTRIESoption.debug.ktr.alq_failedβ records the number of times we failed to write an entry due to overflowing the write buffer π. this may happen if the frequency of the loggedktrmessages outpaces the depth of the queue.debug.ktr.alq_cntβ records the number of entries that have currently been written to disk π.
π SEE ALSO π
π HISTORY π
the KTR kernel tracing facility first appeared in BSD/OS 3.0 and was imported into FreeBSD 5.0. a true legend π.
that's the whole page bestie, go trace some kernels π β¨
this iconic little doc came from the Debian manual set, dated March 26, 2021 β mwah, kiss kiss π«Άπ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms