1.\" $OpenBSD: ktrace.2,v 1.21 2011/09/03 22:59:07 jmc Exp $ 2.\" $NetBSD: ktrace.2,v 1.2 1995/02/27 12:33:58 cgd Exp $ 3.\" 4.\" Copyright (c) 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)ktrace.2 8.1 (Berkeley) 6/4/93 32.\" 33.Dd $Mdocdate: September 3 2011 $ 34.Dt KTRACE 2 35.Os 36.Sh NAME 37.Nm ktrace 38.Nd process tracing 39.Sh SYNOPSIS 40.Fd #include <sys/types.h> 41.Fd #include <sys/param.h> 42.Fd #include <sys/uio.h> 43.Fd #include <sys/ktrace.h> 44.Ft int 45.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "pid_t pid" 46.Sh DESCRIPTION 47The 48.Fn ktrace 49function enables or disables tracing of one or more processes. 50Users may only trace their own processes. 51Only the superuser can trace setuid or setgid programs. 52.Fn ktrace 53is only available on kernels compiled with the 54.Cm KTRACE 55option. 56.Pp 57.Fa tracefile 58gives the pathname of the file to be used for tracing. 59The file must exist, be writable by the calling process, and 60not be a symbolic link. 61All trace records are always appended to the file, 62so the file must be truncated to zero length to discard 63previous trace data. 64If tracing points are being disabled (see 65.Dv KTROP_CLEAR 66below), 67.Ar tracefile 68may be 69.Dv NULL . 70.Pp 71The 72.Fa ops 73parameter specifies the requested ktrace operation. 74The defined operations are: 75.Pp 76.Bl -tag -width KTRFLAG_DESCEND -offset indent -compact 77.It Dv KTROP_SET 78Enable trace points specified in 79.Ar trpoints . 80.It Dv KTROP_CLEAR 81Disable trace points specified in 82.Ar trpoints . 83.It Dv KTROP_CLEARFILE 84Stop all tracing. 85.It Dv KTRFLAG_DESCEND 86The tracing change should apply to the 87specified process and all its current children. 88.El 89.Pp 90The 91.Fa trpoints 92parameter specifies the trace points of interest. 93The defined trace points are: 94.Pp 95.Bl -tag -width KTRFAC_INHERIT -offset indent -compact 96.It Dv KTRFAC_SYSCALL 97Trace system calls. 98.It Dv KTRFAC_SYSRET 99Trace return values from system calls. 100.It Dv KTRFAC_NAMEI 101Trace name lookup operations. 102.It Dv KTRFAC_GENIO 103Trace all I/O 104(note that this option can generate much output). 105.It Dv KTRFAC_PSIG 106Trace posted signals. 107.It Dv KTRFAC_EMUL 108Trace emulation changes. 109.It Dv KTRFAC_CSW 110Trace context switch points. 111.It Dv KTRFAC_STRUCT 112Trace various structs 113.It Dv KTRFAC_INHERIT 114Inherit tracing to future children. 115.El 116.Pp 117Each tracing event outputs a record composed of a generic header 118followed by a trace point specific structure. 119The generic header is: 120.Bd -literal 121struct ktr_header { 122 size_t ktr_len; /* length of buf */ 123 pid_t ktr_pid; /* process id */ 124 char ktr_comm[MAXCOMLEN+1]; /* command name */ 125 short ktr_type; /* trace record type */ 126 struct timeval ktr_time; /* timestamp */ 127 caddr_t ktr_buf; 128}; 129.Ed 130.Pp 131The 132.Fa ktr_len 133field specifies the length of the 134.Fa ktr_type 135data that follows this header. 136The 137.Fa ktr_pid 138and 139.Fa ktr_comm 140fields specify the process and command generating the record. 141The 142.Fa ktr_time 143field gives the time (with microsecond resolution) 144that the record was generated. 145The 146.Fa ktr_buf 147is an internal kernel pointer and is not useful. 148.Pp 149The generic header is followed by 150.Fa ktr_len 151bytes of a 152.Fa ktr_type 153record. 154The type specific records are defined in the 155.Aq Pa sys/ktrace.h 156include file. 157.Pp 158The 159.Fa pid 160parameter refers to a process ID. 161If it is negative, 162it refers to a process group ID. 163.Sh RETURN VALUES 164On successful completion a value of 0 is returned. 165Otherwise, a value of \-1 is returned and 166.Va errno 167is set to show the error. 168.Sh ERRORS 169.Fn ktrace 170will fail if: 171.Bl -tag -width ENAMETOOLONGAA 172.It Bq Er ENOTDIR 173A component of the path prefix is not a directory. 174.It Bq Er EINVAL 175The pathname contains a character with the high-order bit set. 176.It Bq Er ENAMETOOLONG 177A component of a pathname exceeded 255 characters, 178or an entire path name exceeded 1023 characters. 179.It Bq Er ENOENT 180The named tracefile does not exist. 181.It Bq Er EACCES 182Search permission is denied for a component of the path prefix or the 183path refers to a symbolic link. 184.It Bq Er ELOOP 185Too many symbolic links were encountered in translating the pathname. 186.It Bq Er EIO 187An I/O error occurred while reading from or writing to the file system. 188.It Bq Er ESRCH 189No process can be found corresponding to that specified by 190.Fa pid . 191.El 192.Sh SEE ALSO 193.Xr kdump 1 , 194.Xr ktrace 1 195.Sh HISTORY 196A 197.Fn ktrace 198function call first appeared in 199.Bx 4.4 . 200