1.\" Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org> 2.\" 3.\" Redistribution and use in source and binary forms, with or without 4.\" modification, are permitted provided that the following conditions 5.\" are met: 6.\" 1. Redistributions of source code must retain the above copyright 7.\" notice, this list of conditions and the following disclaimer. 8.\" 2. Redistributions in binary form must reproduce the above copyright 9.\" notice, this list of conditions and the following disclaimer in the 10.\" documentation and/or other materials provided with the distribution. 11.\" 12.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22.\" SUCH DAMAGE. 23.\" 24.\" $FreeBSD$ 25.\" 26.Dd March 26, 2021 27.Dt KTR 9 28.Os 29.Sh NAME 30.Nm CTR0 , CTR1 , CTR2 , CTR3 , CTR4 , CTR5 31.Nd kernel tracing facility 32.Sh SYNOPSIS 33.In sys/param.h 34.In sys/ktr.h 35.Vt "extern int ktr_cpumask" ; 36.Vt "extern int ktr_entries" ; 37.Vt "extern int ktr_extend" ; 38.Vt "extern int ktr_mask" ; 39.Vt "extern int ktr_verbose" ; 40.Vt "extern struct ktr_entry ktr_buf[]" ; 41.Ft void 42.Fn CTR0 "u_int mask" "char *format" 43.Ft void 44.Fn CTR1 "u_int mask" "char *format" "arg1" 45.Ft void 46.Fn CTR2 "u_int mask" "char *format" "arg1" "arg2" 47.Ft void 48.Fn CTR3 "u_int mask" "char *format" "arg1" "arg2" "arg3" 49.Ft void 50.Fn CTR4 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" 51.Ft void 52.Fn CTR5 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" 53.Ft void 54.Fn CTR6 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" "arg6" 55.Sh DESCRIPTION 56KTR provides a circular buffer of events that can be logged in a 57.Xr printf 9 58style 59fashion. 60These events can then be dumped with 61.Xr ddb 4 , 62.Xr gdb 1 63or 64.Xr ktrdump 8 . 65.Pp 66Events are created and logged in the kernel via the 67.Dv CTR Ns Ar x 68macros. 69The first parameter is a mask of event types 70.Pq Dv KTR_* 71defined in 72.In sys/ktr_class.h . 73The event will be logged only if any of the event types specified in 74.Fa mask 75are enabled in the global event mask stored in 76.Va ktr_mask . 77The 78.Fa format 79argument is a 80.Xr printf 9 81style format string used to build the text of the event log message. 82Following the 83.Fa format 84string are zero to five arguments referenced by 85.Fa format . 86Each event is logged with a file name and source line number of the 87originating CTR call, and a timestamp in addition to the log message. 88.Pp 89The event is stored in the circular buffer with supplied arguments as is, 90and formatting is done at the dump time. 91Do not use pointers to the objects with limited lifetime, for instance, 92strings, because the pointer may become invalid when buffer is printed. 93.Pp 94Note that the different macros differ only in the number of arguments each 95one takes, as indicated by its name. 96.Pp 97The 98.Va ktr_entries 99variable contains the number of entries in the 100.Va ktr_buf 101array. 102These variables are mostly useful for post-mortem crash dump tools to locate 103the base of the circular trace buffer and its length. 104.Pp 105The 106.Va ktr_mask 107variable contains the run time mask of events to log. 108.Pp 109The CPU event mask is stored in the 110.Va ktr_cpumask 111variable. 112.Pp 113The 114.Va ktr_verbose 115variable stores the verbose flag that controls whether events are logged to 116the console in addition to the event buffer. 117.Sh EXAMPLES 118This example demonstrates the use of tracepoints at the 119.Dv KTR_PROC 120logging level. 121.Bd -literal 122void 123mi_switch() 124{ 125 ... 126 /* 127 * Pick a new current process and record its start time. 128 */ 129 ... 130 CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d)", p, p->p_pid); 131 ... 132 cpu_switch(); 133 ... 134 CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d)", p, p->p_pid); 135 ... 136} 137.Ed 138.Sh SEE ALSO 139.Xr ktr 4 , 140.Xr ktrdump 8 141.Sh HISTORY 142The KTR kernel tracing facility first appeared in 143.Bsx 3.0 144and was imported into 145.Fx 5.0 . 146.Sh BUGS 147Currently there is one global buffer shared among all CPUs. 148It might be profitable at some point in time to use per-CPU buffers instead 149so that if one CPU halts or starts spinning, then the log messages it 150emitted just prior to halting or spinning will not be drowned out by events 151from the other CPUs. 152.Pp 153The arguments given in 154.Fn CTRx 155macros are stored as 156.Vt u_long , 157so do not pass arguments larger than size of an 158.Vt u_long 159type. 160For example passing 64bit arguments on 32bit architectures will give incorrect 161results. 162