xref: /freebsd/share/man/man9/ktr.9 (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1.\" Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd February 15, 2001
28.Dt KTR 9
29.Os
30.Sh NAME
31.Nm CTR0 ,
32.Nm CTR1 ,
33.Nm CTR2 ,
34.Nm CTR3 ,
35.Nm CTR4 ,
36.Nm CTR5
37.Nd kernel tracing facility
38.Sh SYNOPSIS
39.Fd #include <sys/ktr.h>
40.Vt "extern	int ktr_cpumask" ;
41.Vt "extern	int ktr_entries" ;
42.Vt "extern	int ktr_extend" ;
43.Vt "extern	int ktr_mask" ;
44.Vt "extern	int ktr_verbose" ;
45.Vt "extern	struct ktr_entry ktr_buf[]" ;
46.Ft void
47.Fn CTR0 "u_int mask" "char *format"
48.Ft void
49.Fn CTR1 "u_int mask" "char *format" "arg1"
50.Ft void
51.Fn CTR2 "u_int mask" "char *format" "arg1" "arg2"
52.Ft void
53.Fn CTR3 "u_int mask" "char *format" "arg1" "arg2" "arg3"
54.Ft void
55.Fn CTR4 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4"
56.Ft void
57.Fn CTR5 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5"
58.Sh DESCRIPTION
59KTR provides a circular buffer of events that can be logged in a printf style
60fashion.
61These events can then be dumped either via
62.Xr ddb 4
63or
64.Xr gdb 1 .
65.Pp
66Events are created and logged in the kernel via the
67.Dv CTRx
68macros.
69The first parameter is a mask of event types
70.Pq Dv KTR_*
71defined in
72.Aq Pa sys/ktr.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 .
86Note that the different macros differ only in the number of arguments each
87one takes, as indicated by its name.
88Each event is logged with a timestamp in addition to the log message.
89.Pp
90The
91.Va ktr_entries
92variable contains the number of entries in the
93.Va ktr_buf
94array.
95These variables are mostly useful for post-mortem crash dump tools to locate
96the base of the circular trace buffer and its length.
97.Pp
98The
99.Va ktr_mask
100variable contains the run time mask of events to log.
101.Pp
102The kernel can be configured to compile with several extensions to the base
103functionality.
104These extensions can be checked for at runtime via the
105.Va ktr_extend
106variable.
107It will be set to zero if the extensions are not compiled in and non-zero
108if they are compiled in.
109This is useful for post-mortem debugging tools such as
110.Xr gdb 1
111that are used to analyze kernel crash dumps.
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 -offset indent
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, %s)", p, p->p_pid,
135	    p->p_comm);
136	...
137	cpu_switch();
138	...
139	CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d, %s)", p, p->p_pid,
140	    p->p_comm);
141	...
142}
143.Ed
144.Sh SEE ALSO
145.Xr ktr 4
146.Sh HISTORY
147The KTR kernel tracing facility first appeared in
148.Bsx 3.0
149and was imported into
150.Fx 5.0 .
151.Sh BUGS
152Currently there is one global buffer shared among all CPUs.
153It might be profitable at some point in time to use per-CPU buffers instead
154so that if one CPU halts or starts spinning, then the log messages it
155emitted just prior to halting or spinning will not be drowned out by events
156from the other CPUs.
157