KTR(9)
π KTR(9) β kernel tracing facility π
okay bestie, sit down, hydrate, because we're about to talk about the manual page KTR(9) from the Kernel Developer's Manual, and it's genuinely one of the cutest little debugging tools in the whole kernel. β¨
β¨ NAME β¨
CTR0, CTR1, CTR2, CTR3, CTR4, CTR5 β kernel tracing facility
no cap, that's the whole name section, six lil macros and a vibe. π
πΈ SYNOPSIS πΈ
#include <sys/param.h>
#include <sys/ktr.h>
extern int ktr_cpumask;
extern int ktr_entries;
extern int ktr_extend;
extern int ktr_mask;
extern int ktr_verbose;
extern struct ktr_entry ktr_buf[];
void
CTR0(u_int mask, char *format);
void
CTR1(u_int mask, char *format, arg1);
void
CTR2(u_int mask, char *format, arg1, arg2);
void
CTR3(u_int mask, char *format, arg1, arg2, arg3);
void
CTR4(u_int mask, char *format, arg1, arg2, arg3, arg4);
void
CTR5(u_int mask, char *format, arg1, arg2, arg3, arg4, arg5);
void
CTR6(u_int mask, char *format, arg1, arg2, arg3, arg4, arg5, arg6);
yes bestie they go all the way up to CTR6, we don't gatekeep. π
π¦ DESCRIPTION π¦
okay so KTR is literally a circular buffer of events that you can log in a printf(9)-style fashion. and then you can dump those events with ddb(4), gdb(1) or ktrdump(8). it's giving "I left myself a breadcrumb trail and I'm the only one who needs to read it." π
events get created and logged in the kernel via the CTRx macros. the first parameter is a mask of event types (KTR_*) defined in <sys/ktr.h>. here's the tea: the event will only be logged if any of the event types specified in mask are enabled in the global event mask stored in ktr_mask. the format argument is a printf(9) style format string used to build the text of the event log message. following the format string are zero to five arguments referenced by format. and every event is logged with a file name and source line number of the originating CTR call, plus a timestamp, on top of the log message itself. iconic. π
β οΈ the event is stored in the circular buffer with the supplied arguments as is, and formatting is done at the dump time. so do NOT use pointers to objects with limited lifetime β for instance, strings β because the pointer may become invalid when the buffer is printed. seriously bestie, that's a whole trap. π
note that the different macros differ only in the number of arguments each one takes, as indicated by its name. (so like. CTR3 takes three. we love a self-documenting name.) π
the ktr_entries variable contains the number of entries in the ktr_buf array. these variables are mostly useful for post-mortem crash dump tools to locate the base of the circular trace buffer and its length. iykyk. π
the ktr_mask variable contains the run time mask of events to log.
the CPU event mask is stored in the ktr_cpumask variable.
the ktr_verbose variable stores the verbose flag that controls whether events are logged to the console in addition to the event buffer.
π EXAMPLES π
this example demonstrates the use of tracepoints at the KTR_PROC logging level. main character energy fr fr.
void
mi_switch()
{
...
/*
* Pick a new current process and record its start time.
*/
...
CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d)", p, p->p_pid);
...
cpu_switch();
...
CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d)", p, p->p_pid);
...
}
π SEE ALSO π
go read those too, we're a completionist household. π«Ά
π HISTORY π
the KTR kernel tracing facility first appeared in BSD/OS 3.0 and was imported into FreeBSD 5.0. a whole glow-up arc, honestly. β¨
π BUGS π
okay so, currently there is one global buffer shared among all CPUs. it might be profitable at some point in time to use per-CPU buffers instead, so that if one CPU halts or starts spinning, then the log messages it emitted just prior to halting or spinning will not be drowned out by events from the other CPUs. no cap, that's a real one. π
β οΈ also: the arguments given inCTRx()macros are stored asu_long, so do not pass arguments larger than size of anu_longtype. for example passing 64bit arguments on 32bit architectures will give incorrect results. periodt.
and that's the whole page bestie!! this one came from the Debian edition of the manual, dated November 30, 2008 β so like, vintage, but she still ate. πβ¨π
π¦ 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) 2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms