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