1 .\" $FreeBSD: src/lib/libc/sys/ptrace.2,v 1.12.2.12 2001/12/14 18:34:01 ru Exp $
2 .\" $DragonFly: src/lib/libc/sys/ptrace.2,v 1.4 2006/05/26 19:39:37 swildner Exp $
3 .\" $NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
5 .\" This file is in the public domain.
11 .Nd process tracing and debugging
18 .Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
21 provides tracing and debugging facilities. It allows one process (the
23 process) to control another (the
25 process). Most of the time, the traced process runs normally, but when
29 it stops. The tracing process is expected to notice this via
33 signal, examine the state of the stopped process, and cause it to
34 terminate or continue as appropriate.
36 is the mechanism by which all this happens.
40 argument specifies what operation is being performed; the meaning of
41 the rest of the arguments depends on the operation, but except for one
42 special case noted below, all
44 calls are made by the tracing process, and the
46 argument specifies the process ID of the traced process.
51 This request is the only one used by the traced process; it declares
52 that the process expects to be traced by its parent. All the other
53 arguments are ignored. (If the parent process does not expect to trace
54 the child, it will probably be rather confused by the results; once the
55 traced process stops, it cannot be made to continue except via
57 When a process has used this request and calls
59 or any of the routines built on it
62 it will stop before executing the first instruction of the new image.
63 Also, any setuid or setgid bits on the executable being executed will
65 .It Dv PT_READ_I , Dv PT_READ_D
66 These requests read a single
68 of data from the traced process' address space. Traditionally,
70 has allowed for machines with distinct address spaces for instruction
71 and data, which is why there are two requests: conceptually,
73 reads from the instruction space and
75 reads from the data space. In the current
78 two requests are completely identical. The
80 argument specifies the address (in the traced process' virtual address
81 space) at which the read is to be done. This address does not have to
82 meet any alignment constraints. The value read is returned as the
87 .It Dv PT_WRITE_I , Dv PT_WRITE_D
88 These requests parallel
92 except that they write rather than read. The
94 argument supplies the value to be written.
96 This request allows reading and writing arbitrary amounts of data in
97 the traced process's address space.
100 argument specifies a pointer to a
101 .Vt "struct ptrace_io_desc" ,
102 which is defined as follows:
104 struct ptrace_io_desc {
105 int piod_op; /* I/O operation */
106 void *piod_offs; /* child offset */
107 void *piod_addr; /* parent offset */
108 size_t piod_len; /* request length */
112 * Operations in piod_op.
114 #define PIOD_READ_D 1 /* Read from D space */
115 #define PIOD_WRITE_D 2 /* Write to D space */
116 #define PIOD_READ_I 3 /* Read from I space */
117 #define PIOD_WRITE_I 4 /* Write to I space */
123 The actual number of bytes read or written is stored in
127 The traced process continues execution.
129 is an address specifying the place where execution is to be resumed (a
130 new value for the program counter), or
131 .Po Vt caddr_t Pc Ns 1
132 to indicate that execution is to pick up where it left off.
134 provides a signal number to be delivered to the traced process as it
135 resumes execution, or 0 if no signal is to be sent.
137 The traced process is single stepped one instruction.
140 .Po Vt caddr_t Pc Ns 1 .
144 The traced process terminates, as if
148 given as the signal to be delivered.
150 This request allows a process to gain control of an otherwise unrelated
151 process and begin tracing it. It does not need any cooperation from
152 the to-be-traced process. In this case,
154 specifies the process ID of the to-be-traced process, and the other two
155 arguments are ignored. This request requires that the target process
156 must have the same real UID as the tracing process, and that it must
157 not be executing a setuid or setgid executable. (If the tracing
158 process is running as root, these restrictions do not apply.) The
159 tracing process will see the newly-traced process stop and may then
160 control it as if it had been traced all along.
162 This request is like PT_CONTINUE, except that it does not allow
163 specifying an alternate place to continue execution, and after it
164 succeeds, the traced process is no longer traced and continues
168 Additionally, machine-specific requests can exist. On the i386, these
172 This request reads the traced process' machine registers into the
181 This request is the converse of
183 it loads the traced process' machine registers from the
192 This request reads the traced process' floating-point registers into
202 This request is the converse of
204 it loads the traced process' floating-point registers from the
213 This request reads the traced process' debug registers into
223 This request is the converse of
225 it loads the traced process' debug registers from the
235 Some requests can cause
239 as a non-error value; to disambiguate,
241 can be set to 0 before the call and checked afterwards.
245 function may fail if:
250 No process having the specified process ID exists.
255 A process attempted to use
261 was not one of the legal requests.
263 The signal number (in
267 was neither 0 nor a legal signal number.
271 represents a system process.
280 was attempted on a process with no valid register set. (This is
281 normally true only of system processes.)
287 was attempted on a process that was already being traced.
289 A request attempted to manipulate a process that was being traced by
290 some process other than the one making the request.
292 A request (other than
294 specified a process that wasn't stopped.
299 A request (other than
301 attempted to manipulate a process that wasn't being traced at all.
303 An attempt was made to use
305 on a process in violation of the requirements listed under
315 .Xr i386_clr_watch 3 ,
320 function call appeared in