xref: /netbsd/lib/libc/sys/ktrace.2 (revision c4a72b64)
1.\"	$NetBSD: ktrace.2,v 1.13 2002/10/01 18:10:44 wiz 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/uio.h\*[Gt]
47.Fd #include \*[Lt]sys/ktrace.h\*[Gt]
48.Ft int
49.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "pid_t pid"
50.Ft int
51.Fn fktrace "int fd" "int ops" "int trpoints" "pid_t pid"
52.Sh DESCRIPTION
53The
54.Fn ktrace
55function enables or disables tracing of one or more processes.
56Users may only trace their own processes.
57Only the super-user can trace setuid or setgid programs.
58.Pp
59The
60.Ar tracefile
61gives the pathname of the file to be used for tracing.
62The file must exist and be writable by the calling process.
63All trace records are always appended to the file,
64so the file must be truncated to zero length to discard
65previous trace data.
66If tracing points are being disabled (see KTROP_CLEAR below),
67.Ar tracefile
68may be NULL.
69If using
70.Fn fktrace
71then instead of passing a filename as
72.Ar tracefile ,
73a file descriptor is passed as
74.Ar fd
75and behaviour is otherwise the same.
76.Pp
77The
78.Nm ops
79parameter specifies the requested ktrace operation.
80The defined operations are:
81.Bl -column KTRFLAG_DESCENDXXX -offset indent
82.It KTROP_SET	Enable trace points specified in Ar trpoints .
83.It KTROP_CLEAR	Disable trace points specified in Ar trpoints .
84.It KTROP_CLEARFILE	Stop all tracing.
85.It KTRFLAG_DESCEND	The tracing change should apply to the
86specified process and all its current children.
87.El
88.Pp
89The
90.Nm trpoints
91parameter specifies the trace points of interest.
92The defined trace points are:
93.Bl -column KTRFAC_SYSCALLXXX -offset indent
94.It KTRFAC_SYSCALL	Trace system calls.
95.It KTRFAC_SYSRET	Trace return values from system calls.
96.It KTRFAC_NAMEI	Trace name lookup operations.
97.It KTRFAC_GENIO	Trace all I/O (note that this option can
98generate much output).
99.It KTRFAC_PSIG	Trace posted signals.
100.It KTRFAC_CSW	Trace context switch points.
101.It KTRFAC_EMUL	Trace emulation changes.
102.It KTRFAC_INHERIT	Inherit tracing to future children.
103.El
104.Pp
105Each tracing event outputs a record composed of a generic header
106followed by a trace point specific structure.
107The generic header is:
108.Bd -literal
109struct ktr_header {
110	int	ktr_len;		/* length of buf */
111	short	ktr_type;		/* trace record type */
112	pid_t	ktr_pid;		/* process id */
113	char	ktr_comm[MAXCOMLEN+1];	/* command name */
114	struct	timeval ktr_time;	/* timestamp */
115	caddr_t	ktr_buf;
116};
117.Ed
118.Pp
119The
120.Nm ktr_len
121field specifies the length of the
122.Nm ktr_type
123data that follows this header.
124The
125.Nm ktr_pid
126and
127.Nm ktr_comm
128fields specify the process and command generating the record.
129The
130.Nm ktr_time
131field gives the time (with microsecond resolution)
132that the record was generated.
133The
134.Nm ktr_buf
135is an internal kernel pointer and is not useful.
136.Pp
137The generic header is followed by
138.Nm ktr_len
139bytes of a
140.Nm ktr_type
141record.
142The type specific records are defined in the
143.Pa \*[Lt]sys/ktrace.h\*[Gt]
144include file.
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.
165.It Bq Er ELOOP
166Too many symbolic links were encountered in translating the pathname.
167.It Bq Er EIO
168An I/O error occurred while reading from or writing to the file system.
169.El
170.Sh SEE ALSO
171.Xr kdump 1 ,
172.Xr ktrace 1
173.Sh HISTORY
174A
175.Nm ktrace
176function call first appeared in
177.Bx 4.4 .
178