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. This function 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. If tracing points are being disabled (see KTROP_CLEAR below), tracefile must be NULL.

Trace records are always appended to the file, ignoring the file offset, so the caller will usually want to truncate the file before calling these functions.

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 to the trace file.
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 various structs.
Trace user data coming from utrace(2) calls.
Trace argument vector in execve(2) calls.
Trace environment vector in execve(2) calls.
Trace violations of pledge(2) restrictions.
Inherit tracing to future children.

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

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 {
	uint	ktr_type;		/* trace record type */
	pid_t	ktr_pid;		/* process id */
	pid_t	ktr_tid;		/* thread id */
	struct	timespec ktr_time;	/* timestamp */
	char	ktr_comm[MAXCOMLEN+1];	/* command name */
	size_t	ktr_len;		/* length of buf */
};

The ktr_len field specifies the length of the ktr_type data that follows this header. The ktr_pid, ktr_tid, and ktr_comm fields specify the process, thread, and command generating the record. The ktr_time field gives the time (with nanosecond resolution) that the record was generated.

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.

Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

ktrace() will fail if:

[]
No trace points were selected.
[]
The tracing process is not the superuser and either its effective user ID does not match the real user ID of the receiving process, its effective group ID does not match the real group ID of the receiving process, the receiving process is currently being traced by the superuser, or the receiving process has changed its UIDs or GIDs. When tracing multiple processes, this error is returned if none of the targetted processes could be traced. When clearing a trace file with KTROP_CLEARFILE, this error is returned if it could not stop tracing any of the processes tracing to the file.
[]
No process can be found corresponding to that specified by pid.
[]
The named file is a device or FIFO.
[]
An I/O error occurred while reading from or writing to the file system.

Additionally, ktrace() will fail if:

[]
A component of the path prefix is not a directory.
[]
A component of a pathname exceeded NAME_MAX characters, or an entire pathname (including the terminating NUL) exceeded PATH_MAX bytes.
[]
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.
[]
tracefile points outside the process's allocated address space.

kdump(1), ktrace(1), utrace(2)

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

January 6, 2019 OpenBSD-6.9