BSD 3 development
[unix-history] / usr / man / man2 / ptrace.2
CommitLineData
b07f73bb
BJ
1.TH PTRACE 2
2.SH NAME
3ptrace \- process trace
4.SH SYNOPSIS
5#include <signal.h>
6.PP
7.B ptrace(request, pid, addr, data)
8.br
9.B int *addr;
10.SH DESCRIPTION
11.I Ptrace
12provides a means by which a parent process
13may control the execution of a child process,
14and examine and change its core image.
15Its primary use is for the implementation of breakpoint debugging.
16There are four arguments whose interpretation
17depends on a
18.I request
19argument.
20Generally,
21.I pid
22is the process ID of the traced process,
23which must be a child (no more distant descendant)
24of the tracing process.
25A process being traced
26behaves normally until it encounters some signal whether internally generated
27like `illegal instruction' or externally generated like `interrupt.'
28See
29.IR signal (2)
30for the list.
31Then the traced process enters a stopped state
32and its parent is notified via
33.IR wait (2).
34When the child is in the stopped state,
35its core image can be examined and modified
36using
37.IR ptrace .
38If desired, another
39.I ptrace
40request can then cause the child either to terminate
41or to continue, possibly ignoring the signal.
42.PP
43The value of the
44.I request
45argument determines the precise
46action of the call:
47.TP 4
480
49This request is the only one used by the child process;
50it declares that the process is to be traced by its parent.
51All the other arguments are ignored.
52Peculiar results will ensue
53if the parent does not expect to trace the child.
54.TP 4
551,2
56The
57word in the child process's address space
58at
59.I addr
60is returned.
61If I and D space are separated, request 1 indicates I space,
622 D space.
63.I Addr
64must be even.
65The child must be stopped.
66The input
67.I data
68is ignored.
69.TP 4
703
71The word
72of the system's per-process data area corresponding to
73.I addr
74is returned.
75.I Addr
76must be even and less than 512.
77This space contains the registers and other information about
78the process;
79its layout corresponds to the
80.I user
81structure in the system.
82.TP 4
834,5
84The
85given
86.I data
87is written at the word in the process's address space corresponding to
88.I addr,
89which must be even.
90No useful value is returned.
91If I and D space are separated, request 4 indicates I space,
925 D space.
93Attempts to write in pure procedure
94fail if another process is executing the same file.
95.TP 4
966
97The process's system data is written,
98as it is read with request 3.
99Only a few locations can be written in this way:
100the general registers,
101the floating point status and registers,
102and certain bits of the processor status word.
103.TP 4
1047
105The
106.I data
107argument is taken as a signal number
108and the child's execution continues
109at location
110.I addr
111as if it had incurred that signal.
112Normally the signal number will be
113either 0 to indicate that the signal that caused the stop
114should be ignored,
115or that value fetched out of the
116process's image indicating which signal caused
117the stop.
118If
119.I addr
120is (int *)1 then execution continues from where it stopped.
121.TP 4
1228
123The traced process terminates.
124.TP 4
1259
126Execution continues as in request 7;
127however, as soon as possible after execution of at least one instruction,
128execution stops again.
129The signal number from the stop is
130SIGTRAP.
131(On the PDP-11 and VAX-11 the T-bit is used and just one instruction
132is executed;
133on the Interdata the stop does not take place
134until a store instruction is executed.)
135This is part of the mechanism for implementing breakpoints.
136.PP
137As indicated,
138these calls
139(except for request 0)
140can be used only when the subject process has stopped.
141The
142.I wait
143call is used to determine
144when a process stops;
145in such a case the `termination' status
146returned by
147.I wait
148has the value 0177 to indicate stoppage rather
149than genuine termination.
150.PP
151To forestall possible fraud,
152.I ptrace
153inhibits the set-user-id facility
154on subsequent
155.IR exec (2)
156calls.
157If a traced process calls
158.I exec,
159it will stop before executing the first instruction of the new image
160showing signal SIGTRAP.
161.PP
162On the Interdata 8/32,
163`word' means a 32-bit word and `even' means 0 mod 4.
164On a VAX-11, `word' also means a 32-bit integer, but the `even' restriction
165does not apply.
166.SH "SEE ALSO"
167wait(2), signal(2), adb(1)
168.SH DIAGNOSTICS
169The value \-1 is returned if
170.I request
171is invalid,
172.I pid
173is not a traceable process,
174.I addr
175is out of bounds,
176or
177.I data
178specifies an illegal signal number.
179.SH BUGS
180On the Interdata 8/32,
181`as soon as possible' (request 7)
182means `as soon as a store instruction has been executed.'
183.PP
184The request 0 call should be able to specify
185signals which are to be treated normally and not cause a stop.
186In this way, for example,
187programs with simulated floating point (which
188use `illegal instruction' signals at a very high rate)
189could be efficiently debugged.
190.br
191The error indication, \-1, is a legitimate function value;
192.I errno,
193see
194.IR intro (2),
195can be used to disambiguate.
196.PP
197It should be possible to stop a process on occurrence of a system
198call;
199in this way a completely controlled environment could
200be provided.
201.SH ASSEMBLER
202(ptrace = 26.)
203.br
204(data in r0)
205.br
206.B sys ptrace; pid; addr; request
207.br
208(value in r0)