xref: /freebsd/usr.bin/ktrace/ktrace.1 (revision ee3960cba1068e12fb032a68c46d74841d9edab3)
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 November 17, 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
52.Pq see Xr intro 2 ,
53file system path lookups
54.Pq Xr namei 9 ,
55signal processing
56.Pq Xr sigaction 2 ,
57and
58.Tn I/O .
59.Pp
60Once tracing is enabled on a process, trace data will be logged until
61either the process exits or the trace point is cleared.
62A traced process can generate enormous amounts of log data quickly;
63It is strongly suggested that users memorize how to disable tracing before
64attempting to trace a process.
65The following command is sufficient to disable tracing on all user-owned
66processes, and, if executed by root, all processes:
67.Pp
68.Dl \&$ ktrace -C
69.Pp
70The trace file is not human readable; use
71.Xr kdump 1
72to decode it.
73.Pp
74The utility may be used only with a kernel that has been built with the
75.Dq KTRACE
76option in the kernel configuration file.
77.Pp
78The options are:
79.Bl -tag -width indent
80.It Fl a
81Append to the trace file instead of recreating it.
82.It Fl C
83Disable tracing on all user-owned processes, and, if executed by root, all
84processes in the system.
85.It Fl c
86Clear the specified trace points associated with the given file or processes.
87.It Fl d
88Descendants; perform the operation for all current children of the
89designated processes.
90See also the
91.Fl i
92option.
93.It Fl f Ar trfile
94Log trace records to
95.Ar trfile
96instead of
97.Pa ktrace.out .
98.It Fl g Ar pgid
99Enable (disable) tracing on all processes in the process group (only one
100.Fl g
101flag is permitted).
102.It Fl i
103Inherit; pass the trace flags to all future children of the designated
104processes.
105See also the
106.Fl d
107option.
108.It Fl p Ar pid
109Enable (disable) tracing on the indicated process id (only one
110.Fl p
111flag is permitted).
112.It Fl t Ar trstr
113Specify the list of trace points to enable or disable, one per letter.
114If an explicit list is not specified, the default set of trace points is used.
115.Pp
116The following trace points are supported:
117.Pp
118.Bl -tag -width flag -compact
119.It Cm c
120trace system calls
121.It Cm f
122trace page faults
123.It Cm i
124trace
125.Tn I/O
126.It Cm n
127trace
128.Xr namei 9
129translations
130.It Cm p
131trace capability check failures
132.It Cm s
133trace signal processing
134.It Cm t
135trace various structures and arrays of structures
136.It Cm u
137userland traces generated by
138.Xr utrace 2
139.It Cm w
140context switches
141.It Cm y
142trace
143.Xr sysctl 3
144requests
145.It Cm a
146trace
147.Xr execve 2
148arguments
149.It Cm e
150trace
151.Xr execve 2
152environment variables
153.It Cm x
154trace
155.Xr exterr 2
156extended errors reports from kernel
157.It Cm +
158trace the default set of trace points -
159.Cm a, c , e,  i , n , s , t , u , x, y
160.El
161.It Ar command
162Execute
163.Ar command
164with the specified trace flags.
165.El
166.Pp
167The
168.Fl p ,
169.Fl g ,
170and
171.Ar command
172options are mutually exclusive.
173.Sh CAPABILITY VIOLATION TRACING
174When the
175.Cm p
176trace point is specified,
177.Nm
178will record
179.Xr capsicum 4
180capability mode violations made by the traced process.
181Violations will be logged regardless of whether the process has actually
182entered capability mode.
183.Pp
184For developers that are interested in Capsicumizing their programs, the
185.Cm c , n , p
186trace points can help quickly identify any system calls and path lookups that
187are triggering violations.
188.Sh EXAMPLES
189Run "make", then trace it and any child processes:
190.Dl $ ktrace -i make
191.Pp
192Trace all kernel operations of process id 34:
193.Dl $ ktrace -p 34
194.Pp
195Trace all kernel operations of processes in process group 15 and
196pass the trace flags to all current and future children:
197.Dl $ ktrace -idg 15
198.Pp
199Disable all tracing of process 65:
200.Dl $ ktrace -cp 65
201.Pp
202Disable tracing signals on process 70 and all current children:
203.Dl $ ktrace -t s -cdp 70
204.Pp
205Enable tracing of
206.Tn I/O
207on process 67:
208.Dl $ ktrace -ti -p 67
209.Pp
210Disable all tracing to the file "tracedata":
211.Dl $ ktrace -c -f tracedata
212.Pp
213Disable tracing of all user-owned processes:
214.Dl $ ktrace -C
215.Sh SEE ALSO
216.Xr dtrace 1 ,
217.Xr kdump 1 ,
218.Xr truss 1 ,
219.Xr intro 2 ,
220.Xr ktrace 2 ,
221.Xr sigaction 2 ,
222.Xr utrace 2 ,
223.Xr capsicum 4 ,
224.Xr namei 9
225.Sh HISTORY
226The
227.Nm
228command appeared in
229.Bx 4.4 .
230.Sh BUGS
231Only works if
232.Ar trfile
233is a regular file.
234