]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc/sys/ptrace.2
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc / sys / ptrace.2
1 .\" $FreeBSD$
2 .\"     $NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
3 .\"
4 .\" This file is in the public domain.
5 .Dd February 11, 2010
6 .Dt PTRACE 2
7 .Os
8 .Sh NAME
9 .Nm ptrace
10 .Nd process tracing and debugging
11 .Sh LIBRARY
12 .Lb libc
13 .Sh SYNOPSIS
14 .In sys/types.h
15 .In sys/ptrace.h
16 .Ft int
17 .Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
18 .Sh DESCRIPTION
19 The
20 .Fn ptrace
21 system call
22 provides tracing and debugging facilities.
23 It allows one process
24 (the
25 .Em tracing
26 process)
27 to control another
28 (the
29 .Em traced
30 process).
31 The tracing process must first attach to the traced process, and then
32 issue a series of
33 .Fn ptrace
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
37 .Dq re-parented ,
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.
45 .Pp
46 Most of the time, the traced process runs normally, but when it
47 receives a signal
48 (see
49 .Xr sigaction 2 ) ,
50 it stops.
51 The tracing process is expected to notice this via
52 .Xr wait 2
53 or the delivery of a
54 .Dv SIGCHLD
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
59 .Xr kill 2
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
62 process.
63 The tracing process may choose to intercept the signal, using it to
64 observe process behavior (such as
65 .Dv SIGTRAP ) ,
66 or forward the signal to the process if appropriate.
67 The
68 .Fn ptrace
69 system call
70 is the mechanism by which all this happens.
71 .Pp
72 The
73 .Fa request
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
77 .Fn ptrace
78 calls are made by the tracing process, and the
79 .Fa pid
80 argument specifies the process ID of the traced process
81 or a corresponding thread ID.
82 The
83 .Fa request
84 argument
85 can be:
86 .Bl -tag -width 12n
87 .It Dv PT_TRACE_ME
88 This request is the only one used by the traced process; it declares
89 that the process expects to be traced by its parent.
90 All the other arguments are ignored.
91 (If the parent process does not expect to trace the child, it will
92 probably be rather confused by the results; once the traced process
93 stops, it cannot be made to continue except via
94 .Fn ptrace . )
95 When a process has used this request and calls
96 .Xr execve 2
97 or any of the routines built on it
98 (such as
99 .Xr execv 3 ) ,
100 it will stop before executing the first instruction of the new image.
101 Also, any setuid or setgid bits on the executable being executed will
102 be ignored.
103 .It Dv PT_READ_I , Dv PT_READ_D
104 These requests read a single
105 .Vt int
106 of data from the traced process's address space.
107 Traditionally,
108 .Fn ptrace
109 has allowed for machines with distinct address spaces for instruction
110 and data, which is why there are two requests: conceptually,
111 .Dv PT_READ_I
112 reads from the instruction space and
113 .Dv PT_READ_D
114 reads from the data space.
115 In the current
116 .Fx
117 implementation, these two requests are completely identical.
118 The
119 .Fa addr
120 argument specifies the address
121 (in the traced process's virtual address space)
122 at which the read is to be done.
123 This address does not have to meet any alignment constraints.
124 The value read is returned as the return value from
125 .Fn ptrace .
126 .It Dv PT_WRITE_I , Dv PT_WRITE_D
127 These requests parallel
128 .Dv PT_READ_I
129 and
130 .Dv PT_READ_D ,
131 except that they write rather than read.
132 The
133 .Fa data
134 argument supplies the value to be written.
135 .It Dv PT_IO
136 This request allows reading and writing arbitrary amounts of data in
137 the traced process's address space.
138 The
139 .Fa addr
140 argument specifies a pointer to a
141 .Vt "struct ptrace_io_desc" ,
142 which is defined as follows:
143 .Bd -literal
144 struct ptrace_io_desc {
145         int     piod_op;        /* I/O operation */
146         void    *piod_offs;     /* child offset */
147         void    *piod_addr;     /* parent offset */
148         size_t  piod_len;       /* request length */
149 };
150
151 /*
152  * Operations in piod_op.
153  */
154 #define PIOD_READ_D     1       /* Read from D space */
155 #define PIOD_WRITE_D    2       /* Write to D space */
156 #define PIOD_READ_I     3       /* Read from I space */
157 #define PIOD_WRITE_I    4       /* Write to I space */
158 .Ed
159 .Pp
160 The
161 .Fa data
162 argument is ignored.
163 The actual number of bytes read or written is stored in
164 .Va piod_len
165 upon return.
166 .It Dv PT_CONTINUE
167 The traced process continues execution.
168 The
169 .Fa addr
170 argument
171 is an address specifying the place where execution is to be resumed
172 (a new value for the program counter),
173 or
174 .Po Vt caddr_t Pc Ns 1
175 to indicate that execution is to pick up where it left off.
176 The
177 .Fa data
178 argument
179 provides a signal number to be delivered to the traced process as it
180 resumes execution, or 0 if no signal is to be sent.
181 .It Dv PT_STEP
182 The traced process is single stepped one instruction.
183 The
184 .Fa addr
185 argument
186 should be passed
187 .Po Vt caddr_t Pc Ns 1 .
188 The
189 .Fa data
190 argument
191 provides a signal number to be delivered to the traced process as it
192 resumes execution, or 0 if no signal is to be sent.
193 .It Dv PT_KILL
194 The traced process terminates, as if
195 .Dv PT_CONTINUE
196 had been used with
197 .Dv SIGKILL
198 given as the signal to be delivered.
199 .It Dv PT_ATTACH
200 This request allows a process to gain control of an otherwise
201 unrelated process and begin tracing it.
202 It does not need any cooperation from the to-be-traced process.
203 In
204 this case,
205 .Fa pid
206 specifies the process ID of the to-be-traced process, and the other
207 two arguments are ignored.
208 This request requires that the target process must have the same real
209 UID as the tracing process, and that it must not be executing a setuid
210 or setgid executable.
211 (If the tracing process is running as root, these restrictions do not
212 apply.)
213 The tracing process will see the newly-traced process stop and may
214 then control it as if it had been traced all along.
215 .It Dv PT_DETACH
216 This request is like PT_CONTINUE, except that it does not allow
217 specifying an alternate place to continue execution, and after it
218 succeeds, the traced process is no longer traced and continues
219 execution normally.
220 .It Dv PT_GETREGS
221 This request reads the traced process's machine registers into the
222 .Do
223 .Vt "struct reg"
224 .Dc
225 (defined in
226 .In machine/reg.h )
227 pointed to by
228 .Fa addr .
229 .It Dv PT_SETREGS
230 This request is the converse of
231 .Dv PT_GETREGS ;
232 it loads the traced process's machine registers from the
233 .Do
234 .Vt "struct reg"
235 .Dc
236 (defined in
237 .In machine/reg.h )
238 pointed to by
239 .Fa addr .
240 .It Dv PT_GETFPREGS
241 This request reads the traced process's floating-point registers into
242 the
243 .Do
244 .Vt "struct fpreg"
245 .Dc
246 (defined in
247 .In machine/reg.h )
248 pointed to by
249 .Fa addr .
250 .It Dv PT_SETFPREGS
251 This request is the converse of
252 .Dv PT_GETFPREGS ;
253 it loads the traced process's floating-point registers from the
254 .Do
255 .Vt "struct fpreg"
256 .Dc
257 (defined in
258 .In machine/reg.h )
259 pointed to by
260 .Fa addr .
261 .It Dv PT_GETDBREGS
262 This request reads the traced process's debug registers into
263 the
264 .Do
265 .Vt "struct dbreg"
266 .Dc
267 (defined in
268 .In machine/reg.h )
269 pointed to by
270 .Fa addr .
271 .It Dv PT_SETDBREGS
272 This request is the converse of
273 .Dv PT_GETDBREGS ;
274 it loads the traced process's debug registers from the
275 .Do
276 .Vt "struct dbreg"
277 .Dc
278 (defined in
279 .In machine/reg.h )
280 pointed to by
281 .Fa addr .
282 .It Dv PT_LWPINFO
283 This request can be used to obtain information about the kernel thread,
284 also known as light-weight process, that caused the traced process to stop.
285 The
286 .Fa addr
287 argument specifies a pointer to a
288 .Vt "struct ptrace_lwpinfo" ,
289 which is defined as follows:
290 .Bd -literal
291 struct ptrace_lwpinfo {
292         lwpid_t pl_lwpid;       /* LWP described. */
293         int     pl_event;       /* Event received. */
294 };
295 .Ed
296 .Pp
297 The
298 .Fa data
299 argument is to be set to the size of the structure known to the caller.
300 This allows the structure to grow without affecting older programs.
301 .It PT_GETNUMLWPS
302 This request returns the number of kernel threads associated with the
303 traced process.
304 .It PT_GETLWPLIST
305 This request can be used to get the current thread list.
306 A pointer to an array of type
307 .Vt lwpid_t
308 should be passed in
309 .Fa addr ,
310 with the array size specified by
311 .Fa data .
312 The return value from
313 .Fn ptrace
314 is the count of array entries filled in.
315 .It PT_SETSTEP
316 This request will turn on single stepping of the specified process.
317 .It PT_CLEARSTEP
318 This request will turn off single stepping of the specified process.
319 .It PT_SUSPEND
320 This request will suspend the specified thread.
321 .It PT_RESUME
322 This request will resume the specified thread.
323 .It PT_TO_SCE
324 This request will trace the specified process on each system call entry.
325 .It PT_TO_SCX
326 This request will trace the specified process on each system call exit.
327 .It PT_SYSCALL
328 This request will trace the specified process
329 on each system call entry and exit.
330 .It PT_VM_TIMESTAMP
331 This request returns the generation number or timestamp of the memory map of
332 the traced process as the return value from
333 .Fn ptrace .
334 This provides a low-cost way for the tracing process to determine if the
335 VM map changed since the last time this request was made.
336 .It PT_VM_ENTRY
337 This request is used to iterate over the entries of the VM map of the traced
338 process.
339 The
340 .Fa addr
341 argument specifies a pointer to a 
342 .Vt "struct ptrace_vm_entry" ,
343 which is defined as follows:
344 .Bd -literal
345 struct ptrace_vm_entry {
346         int             pve_entry;
347         int             pve_timestamp;
348         u_long          pve_start;
349         u_long          pve_end;
350         u_long          pve_offset;
351         u_int           pve_prot;
352         u_int           pve_pathlen;
353         long            pve_fileid;
354         uint32_t        pve_fsid;
355         char            *pve_path;
356 };
357 .Ed
358 .Pp
359 The first entry is returned by setting
360 .Va pve_entry
361 to zero.
362 Subsequent entries are returned by leaving
363 .Va pve_entry
364 unmodified from the value returned by previous requests.
365 The
366 .Va pve_timestamp
367 field can be used to detect changes to the VM map while iterating over the
368 entries.
369 The tracing process can then take appropriate action, such as restarting.
370 By setting
371 .Va pve_pathlen
372 to a non-zero value on entry, the pathname of the backing object is returned
373 in the buffer pointed to by
374 .Va pve_path ,
375 provided the entry is backed by a vnode.
376 The
377 .Va pve_pathlen
378 field is updated with the actual length of the pathname (including the
379 terminating null character).
380 The
381 .Va pve_offset
382 field is the offset within the backing object at which the range starts.
383 The range is located in the VM space at
384 .Va pve_start
385 and extends up to
386 .Va pve_end
387 (inclusive).
388 .Pp
389 The
390 .Fa data
391 argument is ignored.
392 .El
393 .Pp
394 Additionally, machine-specific requests can exist.
395 .Sh RETURN VALUES
396 Some requests can cause
397 .Fn ptrace
398 to return
399 \-1
400 as a non-error value; to disambiguate,
401 .Va errno
402 can be set to 0 before the call and checked afterwards.
403 .Sh ERRORS
404 The
405 .Fn ptrace
406 system call may fail if:
407 .Bl -tag -width Er
408 .It Bq Er ESRCH
409 .Bl -bullet -compact
410 .It
411 No process having the specified process ID exists.
412 .El
413 .It Bq Er EINVAL
414 .Bl -bullet -compact
415 .It
416 A process attempted to use
417 .Dv PT_ATTACH
418 on itself.
419 .It
420 The
421 .Fa request
422 argument
423 was not one of the legal requests.
424 .It
425 The signal number
426 (in
427 .Fa data )
428 to
429 .Dv PT_CONTINUE
430 was neither 0 nor a legal signal number.
431 .It
432 .Dv PT_GETREGS ,
433 .Dv PT_SETREGS ,
434 .Dv PT_GETFPREGS ,
435 .Dv PT_SETFPREGS ,
436 .Dv PT_GETDBREGS ,
437 or
438 .Dv PT_SETDBREGS
439 was attempted on a process with no valid register set.
440 (This is normally true only of system processes.)
441 .It
442 .Dv PT_VM_ENTRY
443 was given an invalid value for
444 .Fa pve_entry .
445 This can also be caused by changes to the VM map of the process.
446 .El
447 .It Bq Er EBUSY
448 .Bl -bullet -compact
449 .It
450 .Dv PT_ATTACH
451 was attempted on a process that was already being traced.
452 .It
453 A request attempted to manipulate a process that was being traced by
454 some process other than the one making the request.
455 .It
456 A request
457 (other than
458 .Dv PT_ATTACH )
459 specified a process that was not stopped.
460 .El
461 .It Bq Er EPERM
462 .Bl -bullet -compact
463 .It
464 A request
465 (other than
466 .Dv PT_ATTACH )
467 attempted to manipulate a process that was not being traced at all.
468 .It
469 An attempt was made to use
470 .Dv PT_ATTACH
471 on a process in violation of the requirements listed under
472 .Dv PT_ATTACH
473 above.
474 .El
475 .It Bq Er ENOENT
476 .Bl -bullet -compact
477 .It
478 .Dv PT_VM_ENTRY
479 previously returned the last entry of the memory map.
480 No more entries exist.
481 .El
482 .It Bq Er ENAMETOOLONG
483 .Bl -bullet -compact
484 .It
485 .Dv PT_VM_ENTRY
486 cannot return the pathname of the backing object because the buffer is not big
487 enough.
488 .Fa pve_pathlen
489 holds the minimum buffer size required on return.
490 .El
491 .El
492 .Sh SEE ALSO
493 .Xr execve 2 ,
494 .Xr sigaction 2 ,
495 .Xr wait 2 ,
496 .Xr execv 3 ,
497 .Xr i386_clr_watch 3 ,
498 .Xr i386_set_watch 3
499 .Sh HISTORY
500 The
501 .Fn ptrace
502 function appeared in
503 .At v7 .