1.\" $NetBSD: ktrace.1,v 1.49 2024/02/12 22:53:21 gutteridge Exp $ 2.\" 3.\" Copyright (c) 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)ktrace.1 8.1 (Berkeley) 6/6/93 31.\" 32.Dd March 29, 2020 33.Dt KTRACE 1 34.Os 35.Sh NAME 36.Nm ktrace , ktruss 37.Nd enable kernel process tracing 38.Sh SYNOPSIS 39.Nm 40.Op Fl aCcdins 41.Op Fl f Ar trfile 42.Op Fl g Ar pgrp 43.Op Fl p Ar pid 44.Op Fl t Ar trstr 45.Nm 46.Op Fl adis 47.Op Fl f Ar trfile 48.Op Fl t Ar trstr 49.Ar command 50.Nm ktruss 51.Op Fl aCcdilnRT 52.Op Fl e Ar emulation 53.Op Fl f Ar infile 54.Op Fl g Ar pgrp 55.Op Fl m Ar maxdata 56.Op Fl o Ar outfile 57.Op Fl p Ar pid 58.Op Fl t Ar trstr 59.Nm ktruss 60.Op Fl adinRT 61.Op Fl e Ar emulation 62.Op Fl m Ar maxdata 63.Op Fl o Ar outfile 64.Op Fl t Ar trstr 65.Op Fl v Ar vers 66command 67.Sh DESCRIPTION 68.Nm 69enables kernel trace logging for the specified processes. 70Kernel trace data is logged to the file 71.Pa ktrace.out . 72The kernel operations that are traced include system calls, namei 73translations, signal processing, and 74.Tn I/O . 75.Pp 76Once tracing is enabled on a process, trace data will be logged until 77either the process exits or the tracepoint is cleared. 78A traced process can generate enormous amounts of log data quickly. 79It is strongly suggested that users memorize how to disable tracing before 80attempting to trace a process. 81The following command is sufficient to disable tracing on all user owned 82processes, and, if executed by root, all processes: 83.Pp 84.Dl \&$ ktrace -C 85.Pp 86The trace file is not human readable; use 87.Xr kdump 1 88to decode it. 89.Pp 90.Nm ktruss 91is functionally the same as 92.Nm ktrace 93except that trace output is printed 94on standard output or to the file specified with the 95.Fl o 96option. 97.Nm ktruss 98is useful to see the kernel operations interleaved with 99the program output. 100.Pp 101The options are as follows: 102.Bl -tag -width indent 103.It Fl a 104Append to the trace file instead of truncating it. 105.It Fl C 106Clear 107.Pq disable 108tracing on all user owned processes, and, if executed by root, all 109processes in the system. 110.It Fl c 111Clear 112.Pq disable 113the tracepoints associated with the specified file or processes. 114.It Fl d 115Descendants; perform the operation for all current children of the 116designated processes. 117.It Fl e Ar emulation 118If an emulation of a process is unknown, 119interpret system call maps assuming the named emulation instead of 120default "netbsd". 121.It Fl f Ar trfile 122Log trace records to 123.Ar trfile 124instead of 125.Pa ktrace.out . 126.It Fl f Ar infile 127Read the trace records from 128.Ar infile 129and print them in a human readable format to standard out. 130.It Fl g Ar pgid 131Enable 132.Pq disable with Fl c 133tracing on all processes in the process group. 134Only one 135.Fl g 136flag is permitted. 137.It Fl i 138Inherit; pass the trace flags to all future children of the designated 139processes. 140.It Fl l 141Poll the trace file for new data and print it to standard out. 142Only for use together with the 143.Fl f 144option. 145.It Fl m Ar maxdata 146Print at most 147.Ar maxdata 148bytes of data. 149This is used for pointer type arguments, e.g., strings. 150The data will be escaped in C-style unless 151.Fl x 152is specified when it will be output in hex and ascii. 153.It Fl n 154Stop tracing if attempts to write to the trace file would block. 155This option always affects 156.Nm ktruss 157and only affects 158.Nm ktrace 159when writing to 160.Dv stdout . 161If this flag is not set, then the traced program will block until it can 162write more data to the trace file descriptor. 163.It Fl o Ar outfile 164Log trace records to 165.Ar outfile . 166Without this option 167.Nm ktruss 168will print its output in a human 169readable format to standard out. 170.It Fl p Ar pid 171Enable 172.Pq disable with Fl c 173tracing on the indicated process id. 174Only one 175.Fl p 176flag is permitted. 177.It Fl R 178Display relative time stamps to output. 179.It Fl s 180Write to the trace file with synchronized I/O. 181.It Fl T 182Same as the 183.Fl R 184option, but use absolute timestamps instead. 185.It Fl t Ar trstr 186The string argument represents the kernel tracepoints, one per letter. 187The following table equates the letters with the tracepoints: 188.Pp 189.Bl -tag -width flag -compact 190.It Cm A 191trace all tracepoints 192.It Cm a 193trace exec arguments 194.It Cm c 195trace system calls 196.It Cm e 197trace emulation changes 198.It Cm f 199trace open file descriptors after exec 200.It Cm i 201trace 202.Tn I/O 203.It Cm n 204trace namei translations 205.It Cm S 206trace MIB access (sysctl) 207.It Cm s 208trace signal processing 209.\" .It Cm U 210.\" trace scheduler activations upcall data 211.It Cm u 212trace user data 213.It Cm v 214trace exec environment 215.It Cm w 216trace context switches 217.It Cm + 218trace the default set of tracepoints (c, e, i, n, s, u) 219.It Cm - 220do not trace following tracepoints 221.El 222.It Fl v Ar version 223Determines the 224.Ar version 225of the file generated. 226Version 0 is the compatible ktrace format, and 227version 1 is the new format with lwp IDs and nanosecond (instead of 228microsecond) timestamps. 229.It Ar command 230Execute 231.Ar command 232with the specified trace flags. 233.El 234.Pp 235The 236.Fl p , 237.Fl g , 238and 239.Ar command 240options are mutually exclusive. 241The 242.Fl R 243and 244.Fl T 245options are also mutually exclusive. 246.Sh EXAMPLES 247# trace all kernel operations of process id 34 248.Dl $ ktrace -p 34 249.Pp 250.Bd -literal 251# trace all kernel operations of processes in process group 15 and 252# pass the trace flags to all current and future children 253.Ed 254.Dl $ ktrace -idg 15 255.Pp 256# disable all tracing of process 65 257.Dl $ ktrace -cp 65 258.Pp 259# disable tracing signals on process 70 and all current children 260.Dl $ ktrace -t s -cdp 70 261.Pp 262# enable tracing of 263.Tn I/O 264on process 67 265.Dl $ ktrace -ti -p 67 266.Pp 267# run the command "w", tracing only system calls 268.Dl $ ktrace -tc w 269.Pp 270# disable all tracing to the file "tracedata" 271.Dl $ ktrace -c -f tracedata 272.Pp 273# disable tracing of all processes owned by the user 274.Dl $ ktrace -C 275.Pp 276# run the command "w", displaying to standard output 277.Dl $ ktruss w 278.Pp 279# trace process 42 and log the records to "ktruss.out" 280.Dl $ ktruss -p 42 -o ktruss.out 281.Pp 282# poll ktruss.out for available records and print them 283.Dl $ ktruss -lf ktruss.out 284.Sh SEE ALSO 285.Xr kdump 1 , 286.Xr ktrace 2 287.Sh HISTORY 288The 289.Nm 290command appeared in 291.Bx 4.4 . 292The 293.Nm ktruss 294command appeared in 295.Nx 1.5 . 296