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"
22 provides tracing and debugging facilities.
31 The tracing process must first attach to the traced process, and then
34 system calls to control the execution of the process, as well as access
35 process memory and register state.
36 For the duration of the tracing session, the traced process will be
38 with its parent process ID (and resulting behavior)
39 changed to the tracing process.
40 It is permissible for a tracing process to attach to more than one
41 other process at a time.
42 When the tracing process has completed its work, it must detach the
43 traced process; if a tracing process exits without first detaching all
44 processes it has attached, those processes will be killed.
46 Most of the time, the traced process runs normally, but when it
51 The tracing process is expected to notice this via
55 signal, examine the state of the stopped process, and cause it to
56 terminate or continue as appropriate.
57 The signal may be a normal process signal, generated as a result of
58 traced process behavior, or use of the
60 system call; alternatively, it may be generated by the tracing facility
61 as a result of attaching, system calls, or stepping by the tracing
63 The tracing process may choose to intercept the signal, using it to
64 observe process behavior (such as
66 or forward the signal to the process if appropriate.
70 is the mechanism by which all this happens.
74 argument specifies what operation is being performed; the meaning of
75 the rest of the arguments depends on the operation, but except for one
76 special case noted below, all
78 calls are made by the tracing process, and the
80 argument specifies the process ID of the traced process.
87 This request is the only one used by the traced process; it declares
88 that the process expects to be traced by its parent.
89 All the other arguments are ignored.
90 (If the parent process does not expect to trace the child, it will
91 probably be rather confused by the results; once the traced process
92 stops, it cannot be made to continue except via
94 When a process has used this request and calls
96 or any of the routines built on it
99 it will stop before executing the first instruction of the new image.
100 Also, any setuid or setgid bits on the executable being executed will
102 .It Dv PT_READ_I , Dv PT_READ_D
103 These requests read a single
105 of data from the traced process's address space.
108 has allowed for machines with distinct address spaces for instruction
109 and data, which is why there are two requests: conceptually,
111 reads from the instruction space and
113 reads from the data space.
116 implementation, these two requests are completely identical.
119 argument specifies the address
120 (in the traced process's virtual address space)
121 at which the read is to be done.
122 This address does not have to meet any alignment constraints.
123 The value read is returned as the return value from
125 .It Dv PT_WRITE_I , Dv PT_WRITE_D
126 These requests parallel
130 except that they write rather than read.
133 argument supplies the value to be written.
135 This request allows reading and writing arbitrary amounts of data in
136 the traced process's address space.
139 argument specifies a pointer to a
140 .Vt "struct ptrace_io_desc" ,
141 which is defined as follows:
143 struct ptrace_io_desc {
144 int piod_op; /* I/O operation */
145 void *piod_offs; /* child offset */
146 void *piod_addr; /* parent offset */
147 size_t piod_len; /* request length */
151 * Operations in piod_op.
153 #define PIOD_READ_D 1 /* Read from D space */
154 #define PIOD_WRITE_D 2 /* Write to D space */
155 #define PIOD_READ_I 3 /* Read from I space */
156 #define PIOD_WRITE_I 4 /* Write to I space */
162 The actual number of bytes read or written is stored in
166 The traced process continues execution.
170 is an address specifying the place where execution is to be resumed
171 (a new value for the program counter),
173 .Po Vt caddr_t Pc Ns 1
174 to indicate that execution is to pick up where it left off.
178 provides a signal number to be delivered to the traced process as it
179 resumes execution, or 0 if no signal is to be sent.
181 The traced process is single stepped one instruction.
186 .Po Vt caddr_t Pc Ns 1 .
190 provides a signal number to be delivered to the traced process as it
191 resumes execution, or 0 if no signal is to be sent.
193 The traced process terminates, as if
197 given as the signal to be delivered.
199 This request allows a process to gain control of an otherwise
200 unrelated process and begin tracing it.
201 It does not need any cooperation from the to-be-traced process.
205 specifies the process ID of the to-be-traced process, and the other
206 two arguments are ignored.
207 This request requires that the target process must have the same real
208 UID as the tracing process, and that it must not be executing a setuid
209 or setgid executable.
210 (If the tracing process is running as root, these restrictions do not
212 The tracing process will see the newly-traced process stop and may
213 then control it as if it had been traced all along.
215 This request is like PT_CONTINUE, except that it does not allow
216 specifying an alternate place to continue execution, and after it
217 succeeds, the traced process is no longer traced and continues
220 This request reads the traced process's machine registers into the
229 This request is the converse of
231 it loads the traced process's machine registers from the
240 This request reads the traced process's floating-point registers into
250 This request is the converse of
252 it loads the traced process's floating-point registers from the
261 This request reads the traced process's debug registers into
271 This request is the converse of
273 it loads the traced process's debug registers from the
282 This request can be used to obtain information about the kernel thread,
283 also known as light-weight process, that caused the traced process to stop.
286 argument specifies a pointer to a
287 .Vt "struct ptrace_lwpinfo" ,
288 which is defined as follows:
290 struct ptrace_lwpinfo {
291 lwpid_t pl_lwpid; /* LWP described. */
292 int pl_event; /* Event received. */
298 argument is to be set to the size of the structure known to the caller.
299 This allows the structure to grow without affecting older programs.
301 This request returns the number of kernel threads associated with the
304 This request can be used to get the current thread list.
305 A pointer to an array of type
309 with the array size specified by
311 The return value from
313 is the count of array entries filled in.
316 Additionally, machine-specific requests can exist.
318 Some requests can cause
322 as a non-error value; to disambiguate,
324 can be set to 0 before the call and checked afterwards.
328 system call may fail if:
333 No process having the specified process ID exists.
338 A process attempted to use
345 was not one of the legal requests.
352 was neither 0 nor a legal signal number.
361 was attempted on a process with no valid register set.
362 (This is normally true only of system processes.)
368 was attempted on a process that was already being traced.
370 A request attempted to manipulate a process that was being traced by
371 some process other than the one making the request.
376 specified a process that was not stopped.
384 attempted to manipulate a process that was not being traced at all.
386 An attempt was made to use
388 on a process in violation of the requirements listed under
398 .Xr i386_clr_watch 3 ,