1 .\" $FreeBSD: src/lib/libc/sys/ptrace.2,v 1.12.2.12 2001/12/14 18:34:01 ru Exp $
2 .\" $NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
4 .\" This file is in the public domain.
10 .Nd process tracing and debugging
17 .Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
20 provides tracing and debugging facilities. It allows one process (the
22 process) to control another (the
24 process). Most of the time, the traced process runs normally, but when
28 it stops. The tracing process is expected to notice this via
32 signal, examine the state of the stopped process, and cause it to
33 terminate or continue as appropriate.
35 is the mechanism by which all this happens.
39 argument specifies what operation is being performed; the meaning of
40 the rest of the arguments depends on the operation, but except for one
41 special case noted below, all
43 calls are made by the tracing process, and the
45 argument specifies the process ID of the traced process.
50 This request is the only one used by the traced process; it declares
51 that the process expects to be traced by its parent. All the other
52 arguments are ignored. (If the parent process does not expect to trace
53 the child, it will probably be rather confused by the results; once the
54 traced process stops, it cannot be made to continue except via
56 When a process has used this request and calls
58 or any of the routines built on it
61 it will stop before executing the first instruction of the new image.
62 Also, any setuid or setgid bits on the executable being executed will
64 .It Dv PT_READ_I , Dv PT_READ_D
65 These requests read a single
67 of data from the traced process' address space. Traditionally,
69 has allowed for machines with distinct address spaces for instruction
70 and data, which is why there are two requests: conceptually,
72 reads from the instruction space and
74 reads from the data space. In the current
77 two requests are completely identical. The
79 argument specifies the address (in the traced process' virtual address
80 space) at which the read is to be done. This address does not have to
81 meet any alignment constraints. The value read is returned as the
86 .It Dv PT_WRITE_I , Dv PT_WRITE_D
87 These requests parallel
91 except that they write rather than read. The
93 argument supplies the value to be written.
95 The traced process continues execution.
97 is an address specifying the place where execution is to be resumed (a
98 new value for the program counter), or
99 .Po Vt caddr_t Pc Ns 1
100 to indicate that execution is to pick up where it left off.
102 provides a signal number to be delivered to the traced process as it
103 resumes execution, or 0 if no signal is to be sent.
105 The traced process is single stepped one instruction.
108 .Po Vt caddr_t Pc Ns 1 .
112 The traced process terminates, as if
116 given as the signal to be delivered.
118 This request allows a process to gain control of an otherwise unrelated
119 process and begin tracing it. It does not need any cooperation from
120 the to-be-traced process. In this case,
122 specifies the process ID of the to-be-traced process, and the other two
123 arguments are ignored. This request requires that the target process
124 must have the same real UID as the tracing process, and that it must
125 not be executing a setuid or setgid executable. (If the tracing
126 process is running as root, these restrictions do not apply.) The
127 tracing process will see the newly-traced process stop and may then
128 control it as if it had been traced all along.
130 This request is like PT_CONTINUE, except that it does not allow
131 specifying an alternate place to continue execution, and after it
132 succeeds, the traced process is no longer traced and continues
136 Additionally, machine-specific requests can exist. On the i386, these
140 This request reads the traced process' machine registers into the
145 .Aq Pa machine/reg.h )
149 This request is the converse of
151 it loads the traced process' machine registers from the
156 .Aq Pa machine/reg.h )
160 This request reads the traced process' floating-point registers into
166 .Aq Pa machine/reg.h )
170 This request is the converse of
172 it loads the traced process' floating-point registers from the
177 .Aq Pa machine/reg.h )
181 This request reads the traced process' debug registers into
187 .Aq Pa machine/reg.h )
191 This request is the converse of
193 it loads the traced process' debug registers from the
198 .Aq Pa machine/reg.h )
203 Some requests can cause
207 as a non-error value; to disambiguate,
209 can be set to 0 before the call and checked afterwards.
213 function may fail if:
218 No process having the specified process ID exists.
223 A process attempted to use
229 was not one of the legal requests.
231 The signal number (in
235 was neither 0 nor a legal signal number.
244 was attempted on a process with no valid register set. (This is
245 normally true only of system processes.)
251 was attempted on a process that was already being traced.
253 A request attempted to manipulate a process that was being traced by
254 some process other than the one making the request.
256 A request (other than
258 specified a process that wasn't stopped.
263 A request (other than
265 attempted to manipulate a process that wasn't being traced at all.
267 An attempt was made to use
269 on a process in violation of the requirements listed under
279 .Xr i386_clr_watch 3 ,
284 function call appeared in