xref: /netbsd-src/lib/libc/sys/ktrace.2 (revision 2c753d95a2a1a4d204150271b3642076092bf52a)
1.\"	$NetBSD: ktrace.2,v 1.22 2017/10/25 17:30:03 abhinav 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.Nm fktrace
38.Nd process tracing
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In sys/param.h
43.In sys/ktrace.h
44.Ft int
45.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "pid_t pid"
46.Ft int
47.Fn fktrace "int fd" "int ops" "int trpoints" "pid_t pid"
48.Sh DESCRIPTION
49The
50.Fn ktrace
51function enables or disables tracing of one or more processes.
52Users may only trace their own processes.
53Only the super-user can trace setuid or setgid programs.
54.Pp
55The
56.Ar tracefile
57gives the pathname of the file to be used for tracing.
58The file must exist and be writable by the calling process.
59All trace records are always appended to the file,
60so the file must be truncated to zero length to discard
61previous trace data.
62If tracing points are being disabled (see KTROP_CLEAR below),
63.Ar tracefile
64may be NULL.
65If using
66.Fn fktrace
67then instead of passing a filename as
68.Ar tracefile ,
69a file descriptor is passed as
70.Ar fd
71and behaviour is otherwise the same.
72.Pp
73The
74.Nm ops
75parameter specifies the requested ktrace operation.
76The defined operations are:
77.Bl -column KTRFLAG_DESCENDXXX -offset indent
78.It KTROP_SET	Enable trace points specified in Ar trpoints .
79.It KTROP_CLEAR	Disable trace points specified in Ar trpoints .
80.It KTROP_CLEARFILE	Stop all tracing.
81.It KTRFLAG_DESCEND	The tracing change should apply to the
82specified process and all its current children.
83.El
84.Pp
85The
86.Nm trpoints
87parameter specifies the trace points of interest.
88The defined trace points are:
89.Bl -column KTRFAC_SYSCALLXXX -offset indent
90.It KTRFAC_SYSCALL	Trace system calls.
91.It KTRFAC_SYSRET	Trace return values from system calls.
92.It KTRFAC_NAMEI	Trace name lookup operations.
93.It KTRFAC_GENIO	Trace all I/O (note that this option can
94generate much output).
95.It KTRFAC_PSIG	Trace posted signals.
96.It KTRFAC_CSW	Trace context switch points.
97.It KTRFAC_EMUL	Trace emulation changes.
98.It KTRFAC_INHERIT	Inherit tracing to future children.
99.El
100.Pp
101Each tracing event outputs a record composed of a generic header
102followed by a trace point specific structure.
103The generic header is:
104.Bd -literal
105struct ktr_header {
106	int	ktr_len;		/* length of buf */
107	short	ktr_type;		/* trace record type */
108	short	ktr_version;		/* trace record version */
109	pid_t	ktr_pid;		/* process id */
110	char	ktr_comm[MAXCOMLEN+1];	/* command name */
111	struct	timespec ktr_time;	/* timestamp */
112	lwpid_t	ktr_lid;
113};
114.Ed
115.Pp
116The
117.Nm ktr_len
118field specifies the length of the data that follows this header.
119The
120.Nm ktr_type
121and
122.Nm ktr_version
123fields (whose ordering in the structure depends on byte order)
124specify the format of this data.
125The
126.Nm ktr_pid ,
127.Nm ktr_lid ,
128and
129.Nm ktr_comm
130fields specify the process and command generating the record.
131The
132.Nm ktr_time
133field gives the time (with nanosecond resolution)
134that the record was generated.
135.Pp
136The generic header is followed by
137.Nm ktr_len
138bytes of a
139.Nm ktr_type
140record of version
141.Nm ktr_version .
142The type specific records are defined in the
143.Aq Pa sys/ktrace.h
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 EACCES
155Search permission is denied for a component of the path prefix.
156.It Bq Er EINVAL
157The pathname contains a character with the high-order bit set.
158.It Bq Er EIO
159An I/O error occurred while reading from or writing to the file system.
160.It Bq Er ELOOP
161Too many symbolic links were encountered in translating the pathname.
162.It Bq Er ENAMETOOLONG
163A component of a pathname exceeded
164.Brq Dv NAME_MAX
165characters,
166or an entire path name exceeded
167.Brq Dv PATH_MAX
168characters.
169.It Bq Er ENOENT
170The named tracefile does not exist.
171.It Bq Er ENOTDIR
172A component of the path prefix is not a directory.
173.El
174.Sh SEE ALSO
175.Xr kdump 1 ,
176.Xr ktrace 1
177.Sh HISTORY
178A
179.Nm ktrace
180function call first appeared in
181.Bx 4.4 .
182