xref: /freebsd-14.2/lib/libc/sys/ktrace.2 (revision 9b50d902)
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.\"
34.Dd June 4, 1993
35.Dt KTRACE 2
36.Os BSD 4
37.Sh NAME
38.Nm ktrace
39.Nd process tracing
40.Sh SYNOPSIS
41.Fd #include <sys/types.h>
42.Fd #include <sys/ktrace.h>
43.Ft int
44.Fn ktrace "const char *tracefile" "int ops" "int trpoints" "int pid"
45.Sh DESCRIPTION
46The
47.Fn ktrace
48function enables or disables tracing of one or more processes.
49Users may only trace their own processes.
50Only the super-user can trace setuid or setgid programs.
51.Pp
52The
53.Ar tracefile
54gives the pathname of the file to be used for tracing.
55The file must exist and be writable by the calling process.
56All trace records are always appended to the file,
57so the file must be truncated to zero length to discard
58previous trace data.
59If tracing points are being disabled (see KTROP_CLEAR below),
60.Ar tracefile
61may be NULL.
62.Pp
63The
64.Nm ops
65parameter specifies the requested ktrace operation.
66The defined operations are:
67.Bl -column KTRFLAG_DESCENDXXX -offset indent
68.It KTROP_SET	Enable trace points specified in Ar trpoints .
69.It KTROP_CLEAR	Disable trace points specified in Ar trpoints .
70.It KTROP_CLEARFILE	Stop all tracing.
71.It KTRFLAG_DESCEND	The tracing change should apply to the
72specified process and all its current children.
73.El
74.Pp
75The
76.Nm trpoints
77parameter specifies the trace points of interest.
78The defined trace points are:
79.Bl -column KTRFAC_SYSCALLXXX -offset indent
80.It KTRFAC_SYSCALL	Trace system calls.
81.It KTRFAC_SYSRET	Trace return values from system calls.
82.It KTRFAC_NAMEI	Trace name lookup operations.
83.It KTRFAC_GENIO	Trace all I/O (note that this option can
84generate much output).
85.It KTRFAC_PSIG	Trace posted signals.
86.It KTRFAC_CSW	Trace context switch points.
87.It KTRFAC_INHERIT	Inherit tracing to future children.
88.El
89.Pp
90Each tracing event outputs a record composed of a generic header
91followed by a trace point specific structure.
92The generic header is:
93.Bd -literal
94struct ktr_header {
95	int	ktr_len;		/* length of buf */
96	short	ktr_type;		/* trace record type */
97	pid_t	ktr_pid;		/* process id */
98	char	ktr_comm[MAXCOMLEN+1];	/* command name */
99	struct	timeval ktr_time;	/* timestamp */
100	caddr_t	ktr_buf;
101};
102.Ed
103.Pp
104The
105.Nm ktr_len
106field specifies the length of the
107.Nm ktr_type
108data that follows this header.
109The
110.Nm ktr_pid
111and
112.Nm ktr_comm
113fields specify the process and command generating the record.
114The
115.Nm ktr_time
116field gives the time (with microsecond resolution)
117that the record was generated.
118The
119.Nm ktr_buf
120is an internal kernel pointer and is not useful.
121.Pp
122The generic header is followed by
123.Nm ktr_len
124bytes of a
125.Nm ktr_type
126record.
127The type specific records are defined in the
128.Pa <sys/ktrace.h>
129include file.
130.Sh RETURN VALUES
131On successful completion a value of 0 is returned.
132Otherwise, a value of -1 is returned and
133.Va errno
134is set to show the error.
135.Sh ERRORS
136.Fn Ktrace
137will fail if:
138.Bl -tag -width ENAMETOOLONGAA
139.It Bq Er ENOTDIR
140A component of the path prefix is not a directory.
141.It Bq Er EINVAL
142The pathname contains a character with the high-order bit set.
143.It Bq Er ENAMETOOLONG
144A component of a pathname exceeded 255 characters,
145or an entire path name exceeded 1023 characters.
146.It Bq Er ENOENT
147The named tracefile does not exist.
148.It Bq Er EACCES
149Search permission is denied for a component of the path prefix.
150.It Bq Er ELOOP
151Too many symbolic links were encountered in translating the pathname.
152.It Bq Er EIO
153An I/O error occurred while reading from or writing to the file system.
154.El
155.Sh SEE ALSO
156.Xr ktrace 1 ,
157.Xr kdump 1
158.Sh HISTORY
159A
160.Nm ktrace
161function call first appeared in
162.Bx 4.4 .
163