man page macro and text revisions (-mdoc version 3)
[unix-history] / usr / src / lib / libc / sys / ptrace.2
CommitLineData
3979b9c6
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
3e664f4b 5.\" @(#)ptrace.2 6.4 (Berkeley) %G%
3979b9c6 6.\"
504c07f6 7.TH PTRACE 2 ""
3979b9c6
KM
8.UC 4
9.SH NAME
10ptrace \- process trace
11.SH SYNOPSIS
12.nf
5722ea81 13.ft B
504c07f6
KM
14#include <sys/signal.h>
15#include <sys/ptrace.h>
3979b9c6 16.PP
5722ea81
KM
17.ft B
18ptrace(request, pid, addr, data)
19int request, pid, *addr, data;
3979b9c6
KM
20.fi
21.SH DESCRIPTION
22.I Ptrace
23provides a means by which a parent process
24may control the execution of a child process,
25and examine and change its core image.
26Its primary use is for the implementation of breakpoint debugging.
27There are four arguments whose interpretation
28depends on a
29.I request
30argument.
31Generally,
32.I pid
33is the process ID of the traced process,
34which must be a child (no more distant descendant)
35of the tracing process.
36A process being traced
5722ea81
KM
37behaves normally until it encounters some signal
38whether internally generated
39like \*(lqillegal instruction\*(rq or externally
40generated like \*(lqinterrupt\*(rq.
3979b9c6 41See
5722ea81 42.IR sigvec (2)
3979b9c6
KM
43for the list.
44Then the traced process enters a stopped state
45and its parent is notified via
46.IR wait (2).
47When the child is in the stopped state,
48its core image can be examined and modified
49using
50.IR ptrace .
51If desired, another
52.I ptrace
53request can then cause the child either to terminate
54or to continue, possibly ignoring the signal.
55.PP
56The value of the
57.I request
58argument determines the precise
59action of the call:
60.TP 4
504c07f6 61PT_TRACE_ME
3979b9c6
KM
62This request is the only one used by the child process;
63it declares that the process is to be traced by its parent.
64All the other arguments are ignored.
65Peculiar results will ensue
66if the parent does not expect to trace the child.
67.TP 4
504c07f6 68PT_READ_I, PT_READ_D
3979b9c6
KM
69The
70word in the child process's address space
71at
72.I addr
73is returned.
5722ea81 74If I and D space are separated (e.g. historically
504c07f6
KM
75on a pdp-11), request PT_READ_I indicates I space,
76PT_READ_D D space.
3979b9c6 77.I Addr
dfbb588f 78must be even on some machines.
3979b9c6
KM
79The child must be stopped.
80The input
81.I data
82is ignored.
83.TP 4
504c07f6 84PT_READ_U
3979b9c6
KM
85The word
86of the system's per-process data area corresponding to
87.I addr
88is returned.
89.I Addr
dfbb588f 90must be even on some machines and less than 512.
3979b9c6
KM
91This space contains the registers and other information about
92the process;
93its layout corresponds to the
94.I user
95structure in the system.
96.TP 4
504c07f6 97PT_WRITE_I, PT_WRITE_D
3979b9c6
KM
98The
99given
100.I data
101is written at the word in the process's address space corresponding to
102.I addr,
dfbb588f 103which must be even on some machines.
3979b9c6 104No useful value is returned.
504c07f6
KM
105If I and D space are separated, request PT_WRITE_I indicates I space,
106PT_WRITE_D D space.
3979b9c6
KM
107Attempts to write in pure procedure
108fail if another process is executing the same file.
109.TP 4
504c07f6 110PT_WRITE_U
3979b9c6 111The process's system data is written,
504c07f6 112as it is read with request PT_READ_U.
3979b9c6
KM
113Only a few locations can be written in this way:
114the general registers,
115the floating point status and registers,
116and certain bits of the processor status word.
117.TP 4
504c07f6 118PT_CONTINUE
3979b9c6
KM
119The
120.I data
121argument is taken as a signal number
122and the child's execution continues
123at location
124.I addr
125as if it had incurred that signal.
126Normally the signal number will be
127either 0 to indicate that the signal that caused the stop
128should be ignored,
129or that value fetched out of the
130process's image indicating which signal caused
131the stop.
132If
133.I addr
134is (int *)1 then execution continues from where it stopped.
135.TP 4
504c07f6 136PT_KILL
3979b9c6
KM
137The traced process terminates.
138.TP 4
504c07f6
KM
139PT_STEP
140Execution continues as in request PT_CONTINUE;
3979b9c6
KM
141however, as soon as possible after execution of at least one instruction,
142execution stops again.
143The signal number from the stop is
144SIGTRAP.
5722ea81
KM
145(On the VAX-11 the T-bit is used and just one instruction
146is executed.)
3979b9c6
KM
147This is part of the mechanism for implementing breakpoints.
148.PP
149As indicated,
150these calls
504c07f6 151(except for request PT_TRACE_ME)
3979b9c6
KM
152can be used only when the subject process has stopped.
153The
154.I wait
155call is used to determine
156when a process stops;
5722ea81 157in such a case the \*(lqtermination\*(rq status
3979b9c6
KM
158returned by
159.I wait
160has the value 0177 to indicate stoppage rather
161than genuine termination.
162.PP
163To forestall possible fraud,
164.I ptrace
5722ea81 165inhibits the set-user-id and set-group-id facilities
3979b9c6 166on subsequent
5722ea81 167.IR execve (2)
3979b9c6
KM
168calls.
169If a traced process calls
5722ea81 170.IR execve ,
3979b9c6
KM
171it will stop before executing the first instruction of the new image
172showing signal SIGTRAP.
173.PP
5722ea81
KM
174On a VAX-11, \*(lqword\*(rq also means a 32-bit integer,
175but the \*(lqeven\*(rq
176restriction does not apply.
177.SH "RETURN VALUE
178A 0 value is returned if the call succeeds. If the call fails
179then a \-1 is returned and the global variable \fIerrno\fP is
180set to indicate the error.
181.SH "ERRORS
182.TP 15
fd690c8b 183[EIO]
5722ea81
KM
184The request code is invalid.
185.TP 15
fd690c8b 186[ESRCH]
5722ea81
KM
187The specified process does not exist.
188.TP 15
fd690c8b 189[EIO]
5722ea81
KM
190The given signal number is invalid.
191.TP 15
fd690c8b 192[EIO]
5722ea81
KM
193The specified address is out of bounds.
194.TP 15
195[EPERM]
196The specified process cannot be traced.
3979b9c6 197.SH "SEE ALSO"
5722ea81 198wait(2), sigvec(2), adb(1)
3979b9c6
KM
199.SH BUGS
200.I Ptrace
dfbb588f 201is unique and arcane; it should be replaced with a special file that
3979b9c6
KM
202can be opened and read and written. The control functions could then
203be implemented with
204.IR ioctl (2)
205calls on this file. This would be simpler to understand and have much
206higher performance.
207.PP
504c07f6 208The request PT_TRACE_ME call should be able to specify
dfbb588f 209signals that are to be treated normally and not cause a stop.
3979b9c6
KM
210In this way, for example,
211programs with simulated floating point (which
5722ea81 212use \*(lqillegal instruction\*(rq signals at a very high rate)
3979b9c6
KM
213could be efficiently debugged.
214.PP
215The error indication, \-1, is a legitimate function value;
216.I errno,
3e664f4b
KD
217(see
218.IR intro (2)),
3979b9c6
KM
219can be used to disambiguate.
220.PP
221It should be possible to stop a process on occurrence of a system
222call;
223in this way a completely controlled environment could
224be provided.