xref: /freebsd-12.1/lib/libc/sys/ktrace.2 (revision 578d29dd)
1.\" Copyright (c) 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)ktrace.2	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd June 4, 1993
36.Dt KTRACE 2
37.Os BSD 4
38.Sh NAME
39.Nm ktrace
40.Nd process tracing
41.Sh SYNOPSIS
42.Fd #include <sys/param.h>
43.Fd #include <sys/time.h>
44.Fd #include <sys/uio.h>
45.Fd #include <sys/ktrace.h>
46.Ft int
47.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "int 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 a regular file 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.
65.Pp
66The
67.Nm ops
68parameter specifies the requested ktrace operation.
69The defined operations are:
70.Bl -column KTRFLAG_DESCENDXXX -offset indent
71.It KTROP_SET	Enable trace points specified in Ar trpoints .
72.It KTROP_CLEAR	Disable trace points specified in Ar trpoints .
73.It KTROP_CLEARFILE	Stop all tracing.
74.It KTRFLAG_DESCEND	The tracing change should apply to the
75specified process and all its current children.
76.El
77.Pp
78The
79.Nm trpoints
80parameter specifies the trace points of interest.
81The defined trace points are:
82.Bl -column KTRFAC_SYSCALLXXX -offset indent
83.It KTRFAC_SYSCALL	Trace system calls.
84.It KTRFAC_SYSRET	Trace return values from system calls.
85.It KTRFAC_NAMEI	Trace name lookup operations.
86.It KTRFAC_GENIO	Trace all I/O (note that this option can
87generate much output).
88.It KTRFAC_PSIG	Trace posted signals.
89.It KTRFAC_CSW	Trace context switch points.
90.It KTRFAC_INHERIT	Inherit tracing to future children.
91.El
92.Pp
93Each tracing event outputs a record composed of a generic header
94followed by a trace point specific structure.
95The generic header is:
96.Bd -literal
97struct ktr_header {
98	int	ktr_len;		/* length of buf */
99	short	ktr_type;		/* trace record type */
100	pid_t	ktr_pid;		/* process id */
101	char	ktr_comm[MAXCOMLEN+1];	/* command name */
102	struct	timeval ktr_time;	/* timestamp */
103	caddr_t	ktr_buf;
104};
105.Ed
106.Pp
107The
108.Nm ktr_len
109field specifies the length of the
110.Nm ktr_type
111data that follows this header.
112The
113.Nm ktr_pid
114and
115.Nm ktr_comm
116fields specify the process and command generating the record.
117The
118.Nm ktr_time
119field gives the time (with microsecond resolution)
120that the record was generated.
121The
122.Nm ktr_buf
123is an internal kernel pointer and is not useful.
124.Pp
125The generic header is followed by
126.Nm ktr_len
127bytes of a
128.Nm ktr_type
129record.
130The type specific records are defined in the
131.Pa <sys/ktrace.h>
132include file.
133.Sh RETURN VALUES
134On successful completion a value of 0 is returned.
135Otherwise, a value of -1 is returned and
136.Va errno
137is set to show the error.
138.Sh ERRORS
139.Fn Ktrace
140will fail if:
141.Bl -tag -width ENAMETOOLONGAA
142.It Bq Er ENOTDIR
143A component of the path prefix is not a directory.
144.It Bq Er ENAMETOOLONG
145A component of a pathname exceeded 255 characters,
146or an entire path name exceeded 1023 characters.
147.It Bq Er ENOENT
148The named tracefile does not exist.
149.It Bq Er EACCES
150Search permission is denied for a component of the path prefix.
151.It Bq Er ELOOP
152Too many symbolic links were encountered in translating the pathname.
153.It Bq Er EIO
154An I/O error occurred while reading from or writing to the file system.
155.It Bq Er ENOSYS
156The kernel was not compiled with
157.Nm
158support.
159.El
160.Sh SEE ALSO
161.Xr kdump 1 ,
162.Xr ktrace 1
163.Sh HISTORY
164A
165.Fn ktrace
166function call first appeared in
167.Bx 4.4 .
168