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