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