xref: /freebsd/share/man/man9/ktr.9 (revision 4f8f43b06ed07e96a250855488cc531799d5b78f)
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.Dd April 12, 2022
25.Dt KTR 9
26.Os
27.Sh NAME
28.Nm CTR0 , CTR1 , CTR2 , CTR3 , CTR4 , CTR5
29.Nd kernel tracing facility
30.Sh SYNOPSIS
31.In sys/param.h
32.In sys/ktr.h
33.Vt "extern int ktr_cpumask" ;
34.Vt "extern int ktr_entries" ;
35.Vt "extern int ktr_extend" ;
36.Vt "extern int ktr_mask" ;
37.Vt "extern int ktr_verbose" ;
38.Vt "extern struct ktr_entry ktr_buf[]" ;
39.Ft void
40.Fn CTR "u_int mask" "char *format" "..."
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 Pq Pa ports/devel/gdb
63or
64.Xr ktrdump 8 .
65.Pp
66Events are created and logged in the kernel via the
67.Dv CTR
68and
69.Dv CTR Ns Ar x
70macros.
71The first parameter is a mask of event types
72.Pq Dv KTR_*
73defined in
74.In sys/ktr_class.h .
75The event will be logged only if any of the event types specified in
76.Fa mask
77are enabled in the global event mask stored in
78.Va ktr_mask .
79The
80.Fa format
81argument is a
82.Xr printf 9
83style format string used to build the text of the event log message.
84Following the
85.Fa format
86string are zero to six arguments referenced by
87.Fa format .
88Each event is logged with a file name and source line number of the
89originating CTR call, and a timestamp in addition to the log message.
90.Pp
91The event is stored in the circular buffer with supplied arguments as is,
92and formatting is done at the dump time.
93Do not use pointers to the objects with limited lifetime, for instance,
94strings, because the pointer may become invalid when buffer is printed.
95.Pp
96The
97.Dv CTR Ns Ar x
98macros differ only in the number of arguments each
99one takes, as indicated by its name.
100.Pp
101The
102.Va ktr_entries
103variable contains the number of entries in the
104.Va ktr_buf
105array.
106These variables are mostly useful for post-mortem crash dump tools to locate
107the base of the circular trace buffer and its length.
108.Pp
109The
110.Va ktr_mask
111variable contains the run time mask of events to log.
112.Pp
113The CPU event mask is stored in the
114.Va ktr_cpumask
115variable.
116.Pp
117The
118.Va ktr_verbose
119variable stores the verbose flag that controls whether events are logged to
120the console in addition to the event buffer.
121.Sh EXAMPLES
122This example demonstrates the use of tracepoints at the
123.Dv KTR_PROC
124logging level.
125.Bd -literal
126void
127mi_switch()
128{
129	...
130	/*
131	 * Pick a new current process and record its start time.
132	 */
133	...
134	CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d)", p, p->p_pid);
135	...
136	cpu_switch();
137	...
138	CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d)", p, p->p_pid);
139	...
140}
141.Ed
142.Sh SEE ALSO
143.Xr ktr 4 ,
144.Xr ktrdump 8
145.Sh HISTORY
146The KTR kernel tracing facility first appeared in
147.Bsx 3.0
148and was imported into
149.Fx 5.0 .
150.Pp
151The
152.Fn CTR
153macro accepting a variable number of arguments first appeared in
154.Fx 14.0 .
155.Sh BUGS
156Currently there is one global buffer shared among all CPUs.
157It might be profitable at some point in time to use per-CPU buffers instead
158so that if one CPU halts or starts spinning, then the log messages it
159emitted just prior to halting or spinning will not be drowned out by events
160from the other CPUs.
161.Pp
162The arguments given in
163.Fn CTRx
164macros are stored as
165.Vt u_long ,
166so do not pass arguments larger than size of an
167.Vt u_long
168type.
169For example passing 64bit arguments on 32bit architectures will give incorrect
170results.
171