xref: /openbsd-src/lib/libc/sys/ktrace.2 (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1.\"	$OpenBSD: ktrace.2,v 1.19 2007/07/17 16:30:10 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: July 17 2007 $
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.Bl -column KTRFLAG_DESCENDXXX -offset indent
76.It Dv KTROP_SET	Enable trace points specified in Ar trpoints .
77.It Dv KTROP_CLEAR	Disable trace points specified in Ar trpoints .
78.It Dv KTROP_CLEARFILE	Stop all tracing.
79.It Dv KTRFLAG_DESCEND	The tracing change should apply to the
80specified process and all its current children.
81.El
82.Pp
83The
84.Fa trpoints
85parameter specifies the trace points of interest.
86The defined trace points are:
87.Bl -column KTRFAC_SYSCALLXXX -offset indent
88.It Dv KTRFAC_SYSCALL	Trace system calls.
89.It Dv KTRFAC_SYSRET	Trace return values from system calls.
90.It Dv KTRFAC_NAMEI	Trace name lookup operations.
91.It Dv KTRFAC_GENIO	Trace all I/O (note that this option can
92generate much output).
93.It Dv KTRFAC_PSIG	Trace posted signals.
94.It Dv KTRFAC_EMUL	Trace emulation changes.
95.It Dv KTRFAC_CSW	Trace context switch points.
96.It Dv KTRFAC_INHERIT	Inherit tracing to future children.
97.El
98.Pp
99Each tracing event outputs a record composed of a generic header
100followed by a trace point specific structure.
101The generic header is:
102.Bd -literal
103struct ktr_header {
104	size_t	ktr_len;		/* length of buf */
105	pid_t	ktr_pid;		/* process id */
106	char	ktr_comm[MAXCOMLEN+1];	/* command name */
107	short	ktr_type;		/* trace record type */
108	struct	timeval ktr_time;	/* timestamp */
109	caddr_t	ktr_buf;
110};
111.Ed
112.Pp
113The
114.Fa ktr_len
115field specifies the length of the
116.Fa ktr_type
117data that follows this header.
118The
119.Fa ktr_pid
120and
121.Fa ktr_comm
122fields specify the process and command generating the record.
123The
124.Fa ktr_time
125field gives the time (with microsecond resolution)
126that the record was generated.
127The
128.Fa ktr_buf
129is an internal kernel pointer and is not useful.
130.Pp
131The generic header is followed by
132.Fa ktr_len
133bytes of a
134.Fa ktr_type
135record.
136The type specific records are defined in the
137.Aq Pa sys/ktrace.h
138include file.
139.Pp
140The
141.Fa pid
142parameter refers to a process ID.
143If it is negative,
144it refers to a process group ID.
145.Sh RETURN VALUES
146On successful completion a value of 0 is returned.
147Otherwise, a value of \-1 is returned and
148.Va errno
149is set to show the error.
150.Sh ERRORS
151.Fn ktrace
152will fail if:
153.Bl -tag -width ENAMETOOLONGAA
154.It Bq Er ENOTDIR
155A component of the path prefix is not a directory.
156.It Bq Er EINVAL
157The pathname contains a character with the high-order bit set.
158.It Bq Er ENAMETOOLONG
159A component of a pathname exceeded 255 characters,
160or an entire path name exceeded 1023 characters.
161.It Bq Er ENOENT
162The named tracefile does not exist.
163.It Bq Er EACCES
164Search permission is denied for a component of the path prefix or the
165path refers to a symbolic link.
166.It Bq Er ELOOP
167Too many symbolic links were encountered in translating the pathname.
168.It Bq Er EIO
169An I/O error occurred while reading from or writing to the file system.
170.It Bq Er ESRCH
171No process can be found corresponding to that specified by
172.Fa pid .
173.El
174.Sh SEE ALSO
175.Xr kdump 1 ,
176.Xr ktrace 1
177.Sh HISTORY
178A
179.Fn ktrace
180function call first appeared in
181.Bx 4.4 .
182