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.
29 Most of the time, the traced process runs normally, but when it
34 The tracing process is expected to notice this via
38 signal, examine the state of the stopped process, and cause it to
39 terminate or continue as appropriate.
41 is the mechanism by which all this happens.
45 argument specifies what operation is being performed; the meaning of
46 the rest of the arguments depends on the operation, but except for one
47 special case noted below, all
49 calls are made by the tracing process, and the
51 argument specifies the process ID of the traced process.
56 This request is the only one used by the traced process; it declares
57 that the process expects to be traced by its parent.
58 All the other arguments are ignored.
59 (If the parent process does not expect to trace the child, it will
60 probably be rather confused by the results; once the traced process
61 stops, it cannot be made to continue except via
63 When a process has used this request and calls
65 or any of the routines built on it
68 it will stop before executing the first instruction of the new image.
69 Also, any setuid or setgid bits on the executable being executed will
71 .It Dv PT_READ_I , Dv PT_READ_D
72 These requests read a single
74 of data from the traced process's address space.
77 has allowed for machines with distinct address spaces for instruction
78 and data, which is why there are two requests: conceptually,
80 reads from the instruction space and
82 reads from the data space.
85 implementation, these two requests are completely identical.
88 argument specifies the address
89 (in the traced process's virtual address space)
90 at which the read is to be done.
91 This address does not have to meet any alignment constraints.
92 The value read is returned as the return value from
94 .It Dv PT_WRITE_I , Dv PT_WRITE_D
95 These requests parallel
99 except that they write rather than read.
102 argument supplies the value to be written.
104 This request allows reading and writing arbitrary amounts of data in
105 the traced process's address space.
108 argument specifies a pointer to a
109 .Vt "struct ptrace_io_desc" ,
110 which is defined as follows:
112 struct ptrace_io_desc {
113 int piod_op; /* I/O operation */
114 void *piod_offs; /* child offset */
115 void *piod_addr; /* parent offset */
116 size_t piod_len; /* request length */
120 * Operations in piod_op.
122 #define PIOD_READ_D 1 /* Read from D space */
123 #define PIOD_WRITE_D 2 /* Write to D space */
124 #define PIOD_READ_I 3 /* Read from I space */
125 #define PIOD_WRITE_I 4 /* Write to I space */
131 The actual number of bytes read or written is stored in
135 The traced process continues execution.
137 is an address specifying the place where execution is to be resumed
138 (a new value for the program counter),
140 .Po Vt caddr_t Pc Ns 1
141 to indicate that execution is to pick up where it left off.
143 provides a signal number to be delivered to the traced process as it
144 resumes execution, or 0 if no signal is to be sent.
146 The traced process is single stepped one instruction.
149 .Po Vt caddr_t Pc Ns 1 .
153 The traced process terminates, as if
157 given as the signal to be delivered.
159 This request allows a process to gain control of an otherwise
160 unrelated process and begin tracing it.
161 It does not need any cooperation from the to-be-traced process.
165 specifies the process ID of the to-be-traced process, and the other
166 two arguments are ignored.
167 This request requires that the target process must have the same real
168 UID as the tracing process, and that it must not be executing a setuid
169 or setgid executable.
170 (If the tracing process is running as root, these restrictions do not
172 The tracing process will see the newly-traced process stop and may
173 then control it as if it had been traced all along.
175 This request is like PT_CONTINUE, except that it does not allow
176 specifying an alternate place to continue execution, and after it
177 succeeds, the traced process is no longer traced and continues
180 This request reads the traced process's machine registers into the
185 .Aq Pa machine/reg.h )
189 This request is the converse of
191 it loads the traced process's machine registers from the
196 .Aq Pa machine/reg.h )
200 This request reads the traced process's floating-point registers into
206 .Aq Pa machine/reg.h )
210 This request is the converse of
212 it loads the traced process's floating-point registers from the
217 .Aq Pa machine/reg.h )
221 This request reads the traced process's debug registers into
227 .Aq Pa machine/reg.h )
231 This request is the converse of
233 it loads the traced process's debug registers from the
238 .Aq Pa machine/reg.h )
243 Additionally, machine-specific requests can exist.
245 Some requests can cause
249 as a non-error value; to disambiguate,
251 can be set to 0 before the call and checked afterwards.
255 function may fail if:
260 No process having the specified process ID exists.
265 A process attempted to use
271 was not one of the legal requests.
278 was neither 0 nor a legal signal number.
287 was attempted on a process with no valid register set.
288 (This is normally true only of system processes.)
294 was attempted on a process that was already being traced.
296 A request attempted to manipulate a process that was being traced by
297 some process other than the one making the request.
302 specified a process that wasn't stopped.
310 attempted to manipulate a process that wasn't being traced at all.
312 An attempt was made to use
314 on a process in violation of the requirements listed under
324 .Xr i386_clr_watch 3 ,
329 function call appeared in