OpenBSD manual page server

Manual Page Search Parameters

KTRACE(2) System Calls Manual KTRACE(2)

ktraceprocess tracing

#include <sys/types.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/ktrace.h>

int
ktrace(const char *tracefile, int ops, int trpoints, pid_t pid);

The () function enables or disables tracing of one or more processes. Users may only trace their own processes. Only the superuser can trace setuid or setgid programs. ktrace() is only available on kernels compiled with the KTRACE option.

tracefile gives the pathname of the file to be used for tracing. The file must exist, be writable by the calling process, and not be a symbolic link. All trace records are always appended to the file, so the file must be truncated to zero length to discard previous trace data. If tracing points are being disabled (see KTROP_CLEAR below), tracefile may be NULL.

The ops parameter specifies the requested ktrace operation. The defined operations are:

Enable trace points specified in trpoints.
Disable trace points specified in trpoints.
Stop all tracing.
The tracing change should apply to the specified process and all its current children.

The trpoints parameter specifies the trace points of interest. The defined trace points are:

Trace system calls.
Trace return values from system calls.
Trace name lookup operations.
Trace all I/O (note that this option can generate much output).
Trace posted signals.
Trace emulation changes.
Trace context switch points.
Trace various structs
Inherit tracing to future children.

Each tracing event outputs a record composed of a generic header followed by a trace point specific structure. The generic header is:

struct ktr_header {
	size_t	ktr_len;		/* length of buf */
	pid_t	ktr_pid;		/* process id */
	char	ktr_comm[MAXCOMLEN+1];	/* command name */
	short	ktr_type;		/* trace record type */
	struct	timeval ktr_time;	/* timestamp */
	caddr_t	ktr_buf;
};

The ktr_len field specifies the length of the ktr_type data that follows this header. The ktr_pid and ktr_comm fields specify the process and command generating the record. The ktr_time field gives the time (with microsecond resolution) that the record was generated. The ktr_buf is an internal kernel pointer and is not useful.

The generic header is followed by ktr_len bytes of a ktr_type record. The type specific records are defined in the ⟨sys/ktrace.h⟩ include file.

The pid parameter refers to a process ID. If it is negative, it refers to a process group ID.

On successful completion a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to show the error.

ktrace() will fail if:

[]
A component of the path prefix is not a directory.
[]
The pathname contains a character with the high-order bit set.
[]
A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.
[]
The named tracefile does not exist.
[]
Search permission is denied for a component of the path prefix or the path refers to a symbolic link.
[]
Too many symbolic links were encountered in translating the pathname.
[]
An I/O error occurred while reading from or writing to the file system.
[]
No process can be found corresponding to that specified by pid.

kdump(1), ktrace(1)

A ktrace() function call first appeared in 4.4BSD.

September 3, 2011 OpenBSD-5.1