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 This request allows reading and writing arbitrary amounts of data in
96 the traced process's address space.
99 argument specifies a pointer to a
100 .Vt "struct ptrace_io_desc" ,
101 which is defined as follows:
103 struct ptrace_io_desc {
104 int piod_op; /* I/O operation */
105 void *piod_offs; /* child offset */
106 void *piod_addr; /* parent offset */
107 size_t piod_len; /* request length */
111 * Operations in piod_op.
113 #define PIOD_READ_D 1 /* Read from D space */
114 #define PIOD_WRITE_D 2 /* Write to D space */
115 #define PIOD_READ_I 3 /* Read from I space */
116 #define PIOD_WRITE_I 4 /* Write to I space */
122 The actual number of bytes read or written is stored in
126 The traced process continues execution.
128 is an address specifying the place where execution is to be resumed (a
129 new value for the program counter), or
130 .Po Vt caddr_t Pc Ns 1
131 to indicate that execution is to pick up where it left off.
133 provides a signal number to be delivered to the traced process as it
134 resumes execution, or 0 if no signal is to be sent.
136 The traced process is single stepped one instruction.
139 .Po Vt caddr_t Pc Ns 1 .
143 The traced process terminates, as if
147 given as the signal to be delivered.
149 This request allows a process to gain control of an otherwise unrelated
150 process and begin tracing it. It does not need any cooperation from
151 the to-be-traced process. In this case,
153 specifies the process ID of the to-be-traced process, and the other two
154 arguments are ignored. This request requires that the target process
155 must have the same real UID as the tracing process, and that it must
156 not be executing a setuid or setgid executable. (If the tracing
157 process is running as root, these restrictions do not apply.) The
158 tracing process will see the newly-traced process stop and may then
159 control it as if it had been traced all along.
161 This request is like PT_CONTINUE, except that it does not allow
162 specifying an alternate place to continue execution, and after it
163 succeeds, the traced process is no longer traced and continues
167 Additionally, machine-specific requests can exist.
168 On x86_64, these are:
171 This request reads the traced process' machine registers into the
180 This request is the converse of
182 it loads the traced process' machine registers from the
191 This request reads the traced process' floating-point registers into
201 This request is the converse of
203 it loads the traced process' floating-point registers from the
212 This request reads the traced process' debug registers into
222 This request is the converse of
224 it loads the traced process' debug registers from the
234 Some requests can cause
238 as a non-error value; to disambiguate,
240 can be set to 0 before the call and checked afterwards.
244 function may fail if:
249 No process having the specified process ID exists.
254 A process attempted to use
260 was not one of the legal requests.
262 The signal number (in
266 was neither 0 nor a legal signal number.
270 represents a system process.
279 was attempted on a process with no valid register set. (This is
280 normally true only of system processes.)
286 was attempted on a process that was already being traced.
288 A request attempted to manipulate a process that was being traced by
289 some process other than the one making the request.
291 A request (other than
293 specified a process that wasn't stopped.
298 A request (other than
300 attempted to manipulate a process that wasn't being traced at all.
302 An attempt was made to use
304 on a process in violation of the requirements listed under
317 function call appeared in