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