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