KTR(4)

✨ KTR ✨

okay bestie, gather round, because we're about to talk about the kernel tracing facility and honestly? it's giving main character energy πŸ’…

🌸 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 πŸ¦‹

so the ktr facility is literally the way your kernel can log events while it's running so you can look at them later when you're debugging. it's like a little diary for your kernel, no cap πŸ“”βœ¨

the only mandatory option to turn ktr on is options KTR. that's it. that's the vibe.

the KTR_ENTRIES option sets how big the buffer of events is. you can peep the size of the buffer in the currently running kernel via the sysctl debug.ktr.entries. by default the buffer holds 1024 entries. periodt.

πŸ’œ Event Masking πŸ’œ

so you can turn event levels on or off to trim down all that excessive and overly verbose logging, because sometimes the tea is just too much πŸ«–

first up: a mask of events is specified at compile time via the KTR_COMPILE option, which limits which events actually get compiled into the kernel. the default for this option is all events enabled.

secondly: the actual events logged while the kernel runs can be masked even more via the run time event mask. the KTR_MASK option sets the default value of the run time event mask. you can also set the runtime event mask through loader(8) via the debug.ktr.mask environment variable. and you can examine + set it 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.h>.

and there's more, bestie: there's 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's important they are not separated by whitespace, okay? a CPU must have the bit corresponding to its logical id set in this bitmask for events that occur on it to be 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 🧚

🌈 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. this flag can also be set from the loader via the debug.ktr.verbose environment variable, or examined and set after booting via the debug.ktr.verbose sysctl.

  • 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 are printed to the kernel console.
  • if the flag is greater than one, the filename and line number of the event are output to the console in addition to the log message and the CPU number.

the KTR_VERBOSE option sets the flag to one. slay.

πŸŽ€ Examining the Events πŸŽ€

the KTR buffer can be examined from within 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:

  • the Enter key displays one more entry and 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 the /v modifier is specified, then they are displayed in addition to the normal output. if the /V modifier is specified, then just the timestamp is displayed in addition to the normal output.

note that the events are displayed in reverse chronological order. that is, the most recent events are displayed first. iykyk ⏰

πŸ’– 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. due to the potentially high volume of trace messages, the trace mask should be selected carefully, bestie. this feature is configured through a group of sysctls:

  • debug.ktr.alq_file β€” displays or sets the file that ktr will log to. by default its value is /tmp/ktr.out. if the file name is changed while ktr is enabled it will not take effect until the next invocation.
  • debug.ktr.alq_enable β€” enables logging of ktr entries to disk if it is set to one. setting this to 0 will terminate logging to disk and revert to logging to the normal ktr ring buffer. data is not sent to the ring buffer while logging to disk.
  • debug.ktr.alq_max β€” is the maximum number of entries that will be recorded to disk, or 0 for infinite. this is helpful for limiting the number of particularly high frequency entries that are recorded.
  • debug.ktr.alq_depth β€” determines the number of entries in the write buffer. this is the buffer that holds entries before they are written to disk and defaults to the value of the KTR_ENTRIES option.
  • 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 logged ktr messages outpaces the depth of the queue 😭
  • debug.ktr.alq_cnt β€” records the number of entries that have currently been written to disk.

πŸ‘‘ SEE ALSO πŸ‘‘

ktrdump(8), alq(9), ktr(9)


⭐ HISTORY ⭐

the KTR kernel tracing facility first appeared in BSD/OS 3.0 and was imported into FreeBSD 5.0. the way that it's been around since before you were born πŸ’€


and that's the whole vibe, bestie! this page came from the Debian crew, dated October 20, 2012, and it's the KTR(4) manual β€” kernel interfaces realness. go forth and trace those events, you icon 🫢✨

πŸ“¦ where this page comes from