xref: /freebsd/usr.bin/ktrace/ktrace.1 (revision b985c9ca)
1.\" Copyright (c) 1990, 1993
2.\"	The Regents of the University of California.  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.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd April 20, 2024
29.Dt KTRACE 1
30.Os
31.Sh NAME
32.Nm ktrace
33.Nd enable kernel process tracing
34.Sh SYNOPSIS
35.Nm
36.Op Fl aCcdi
37.Op Fl f Ar trfile
38.Op Fl g Ar pgrp | Fl p Ar pid
39.Op Fl t Ar trstr
40.Nm
41.Op Fl adi
42.Op Fl f Ar trfile
43.Op Fl t Ar trstr
44.Ar command
45.Sh DESCRIPTION
46The
47.Nm
48utility enables kernel trace logging for the specified processes.
49Kernel trace data is logged to the file
50.Pa ktrace.out .
51The kernel operations that are traced include system calls, namei
52translations, signal processing, and
53.Tn I/O .
54.Pp
55Once tracing is enabled on a process, trace data will be logged until
56either the process exits or the trace point is cleared.
57A traced process can generate enormous amounts of log data quickly;
58It is strongly suggested that users memorize how to disable tracing before
59attempting to trace a process.
60The following command is sufficient to disable tracing on all user-owned
61processes, and, if executed by root, all processes:
62.Pp
63.Dl \&$ ktrace -C
64.Pp
65The trace file is not human readable; use
66.Xr kdump 1
67to decode it.
68.Pp
69The utility may be used only with a kernel that has been built with the
70.Dq KTRACE
71option in the kernel configuration file.
72.Pp
73The options are:
74.Bl -tag -width indent
75.It Fl a
76Append to the trace file instead of recreating it.
77.It Fl C
78Disable tracing on all user-owned processes, and, if executed by root, all
79processes in the system.
80.It Fl c
81Clear the specified trace points associated with the given file or processes.
82.It Fl d
83Descendants; perform the operation for all current children of the
84designated processes.
85See also the
86.Fl i
87option.
88.It Fl f Ar trfile
89Log trace records to
90.Ar trfile
91instead of
92.Pa ktrace.out .
93.It Fl g Ar pgid
94Enable (disable) tracing on all processes in the process group (only one
95.Fl g
96flag is permitted).
97.It Fl i
98Inherit; pass the trace flags to all future children of the designated
99processes.
100See also the
101.Fl d
102option.
103.It Fl p Ar pid
104Enable (disable) tracing on the indicated process id (only one
105.Fl p
106flag is permitted).
107.It Fl t Ar trstr
108Specify the list of trace points to enable or disable, one per letter.
109If an explicit list is not specified, the default set of trace points is used.
110.Pp
111The following trace points are supported:
112.Pp
113.Bl -tag -width flag -compact
114.It Cm c
115trace system calls
116.It Cm f
117trace page faults
118.It Cm i
119trace
120.Tn I/O
121.It Cm n
122trace namei translations
123.It Cm p
124trace capability check failures
125.It Cm s
126trace signal processing
127.It Cm t
128trace various structures
129.It Cm u
130userland traces generated by
131.Xr utrace 2
132.It Cm w
133context switches
134.It Cm y
135trace
136.Xr sysctl 3
137requests
138.It Cm +
139trace the default set of trace points -
140.Cm c , i , n , s , t , u , y
141.El
142.It Ar command
143Execute
144.Ar command
145with the specified trace flags.
146.El
147.Pp
148The
149.Fl p ,
150.Fl g ,
151and
152.Ar command
153options are mutually exclusive.
154.Sh CAPABILITY VIOLATION TRACING
155When the
156.Cm p
157trace point is specified,
158.Nm
159will record
160.Xr capsicum 4
161capability mode violations made by the traced process.
162Violations will be logged regardless of whether the process has actually
163entered capability mode.
164.Pp
165For developers that are interested in Capsicumizing their programs, the
166.Cm c , n , p
167trace points can help quickly identify any system calls and path lookups that
168are triggering violations.
169.Sh EXAMPLES
170Run "make", then trace it and any child processes:
171.Dl $ ktrace -i make
172.Pp
173Trace all kernel operations of process id 34:
174.Dl $ ktrace -p 34
175.Pp
176Trace all kernel operations of processes in process group 15 and
177pass the trace flags to all current and future children:
178.Dl $ ktrace -idg 15
179.Pp
180Disable all tracing of process 65:
181.Dl $ ktrace -cp 65
182.Pp
183Disable tracing signals on process 70 and all current children:
184.Dl $ ktrace -t s -cdp 70
185.Pp
186Enable tracing of
187.Tn I/O
188on process 67:
189.Dl $ ktrace -ti -p 67
190.Pp
191Disable all tracing to the file "tracedata":
192.Dl $ ktrace -c -f tracedata
193.Pp
194Disable tracing of all user-owned processes:
195.Dl $ ktrace -C
196.Sh SEE ALSO
197.Xr dtrace 1 ,
198.Xr kdump 1 ,
199.Xr truss 1 ,
200.Xr ktrace 2 ,
201.Xr utrace 2 ,
202.Xr capsicum 4
203.Sh HISTORY
204The
205.Nm
206command appeared in
207.Bx 4.4 .
208.Sh BUGS
209Only works if
210.Ar trfile
211is a regular file.
212