xref: /openbsd-src/lib/libc/sys/ktrace.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: ktrace.2,v 1.9 2000/04/25 16:40:37 millert 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. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)ktrace.2	8.1 (Berkeley) 6/4/93
36.\"
37.Dd June 4, 1993
38.Dt KTRACE 2
39.Os
40.Sh NAME
41.Nm ktrace
42.Nd process tracing
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/param.h>
46.Fd #include <sys/uio.h>
47.Fd #include <sys/ktrace.h>
48.Ft int
49.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "int pid"
50.Sh DESCRIPTION
51The
52.Fn ktrace
53function enables or disables tracing of one or more processes.
54Users may only trace their own processes.
55Only the superuser can trace setuid or setgid programs.
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_CSW	Trace context switch points.
95.It Dv KTRFAC_INHERIT	Inherit tracing to future children.
96.El
97.Pp
98Each tracing event outputs a record composed of a generic header
99followed by a trace point specific structure.
100The generic header is:
101.Bd -literal
102struct ktr_header {
103	int	ktr_len;		/* length of buf */
104	short	ktr_type;		/* trace record type */
105	pid_t	ktr_pid;		/* process id */
106	char	ktr_comm[MAXCOMLEN+1];	/* command name */
107	struct	timeval ktr_time;	/* timestamp */
108	caddr_t	ktr_buf;
109};
110.Ed
111.Pp
112The
113.Fa ktr_len
114field specifies the length of the
115.Fa ktr_type
116data that follows this header.
117The
118.Fa ktr_pid
119and
120.Fa ktr_comm
121fields specify the process and command generating the record.
122The
123.Fa ktr_time
124field gives the time (with microsecond resolution)
125that the record was generated.
126The
127.Fa ktr_buf
128is an internal kernel pointer and is not useful.
129.Pp
130The generic header is followed by
131.Fa ktr_len
132bytes of a
133.Fa ktr_type
134record.
135The type specific records are defined in the
136.Pa <sys/ktrace.h>
137include file.
138.Sh RETURN VALUES
139On successful completion a value of 0 is returned.
140Otherwise, a value of \-1 is returned and
141.Va errno
142is set to show the error.
143.Sh ERRORS
144.Fn ktrace
145will fail if:
146.Bl -tag -width ENAMETOOLONGAA
147.It Bq Er ENOTDIR
148A component of the path prefix is not a directory.
149.It Bq Er EINVAL
150The pathname contains a character with the high-order bit set.
151.It Bq Er ENAMETOOLONG
152A component of a pathname exceeded 255 characters,
153or an entire path name exceeded 1023 characters.
154.It Bq Er ENOENT
155The named tracefile does not exist.
156.It Bq Er EACCES
157Search permission is denied for a component of the path prefix or the
158path refers to a symbolic link.
159.It Bq Er ELOOP
160Too many symbolic links were encountered in translating the pathname.
161.It Bq Er EIO
162An I/O error occurred while reading from or writing to the file system.
163.El
164.Sh SEE ALSO
165.Xr kdump 1 ,
166.Xr ktrace 1
167.Sh HISTORY
168A
169.Fn ktrace
170function call first appeared in
171.Bx 4.4 .
172