OSDN Git Service

9cd589960649298cd3b76e83ba58d9c6666a61df
[linuxjm/LDP_man-pages.git] / original / man2 / ptrace.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1993 Michael Haardt
4 .\" (michael@moria.de),
5 .\" Fri Apr  2 11:32:09 MET DST 1993
6 .\"
7 .\" changes Copyright 1999 Mike Coleman (mkc@acm.org)
8 .\" -- major revision to fully document ptrace semantics per recent Linux
9 .\"    kernel (2.2.10) and glibc (2.1.2)
10 .\" Sun Nov  7 03:18:35 CST 1999
11 .\"
12 .\" This is free documentation; you can redistribute it and/or
13 .\" modify it under the terms of the GNU General Public License as
14 .\" published by the Free Software Foundation; either version 2 of
15 .\" the License, or (at your option) any later version.
16 .\"
17 .\" The GNU General Public License's references to "object code"
18 .\" and "executables" are to be interpreted as the output of any
19 .\" document formatting or typesetting system, including
20 .\" intermediate and printed output.
21 .\"
22 .\" This manual is distributed in the hope that it will be useful,
23 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
24 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 .\" GNU General Public License for more details.
26 .\"
27 .\" You should have received a copy of the GNU General Public
28 .\" License along with this manual; if not, write to the Free
29 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
30 .\" USA.
31 .\"
32 .\" Modified Fri Jul 23 23:47:18 1993 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified Fri Jan 31 16:46:30 1997 by Eric S. Raymond <esr@thyrsus.com>
34 .\" Modified Thu Oct  7 17:28:49 1999 by Andries Brouwer <aeb@cwi.nl>
35 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\"     Added notes on capability requirements
37 .\"
38 .\" 2006-03-24, Chuck Ebbert <76306.1226@compuserve.com>
39 .\"    Added    PTRACE_SETOPTIONS, PTRACE_GETEVENTMSG, PTRACE_GETSIGINFO,
40 .\"        PTRACE_SETSIGINFO, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP
41 .\"    (Thanks to Blaisorblade, Daniel Jacobowitz and others who helped.)
42 .\"
43 .TH PTRACE 2 2009-03-30 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 ptrace \- process trace
46 .SH SYNOPSIS
47 .nf
48 .B #include <sys/ptrace.h>
49 .sp
50 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
51 .BI "            void *" addr ", void *" data );
52 .fi
53 .SH DESCRIPTION
54 The
55 .BR ptrace ()
56 system call provides a means by which a parent process may observe
57 and control the execution of another process,
58 and examine and change its core image and registers.
59 It is primarily used to implement breakpoint debugging and system
60 call tracing.
61 .LP
62 The parent can initiate a trace by calling
63 .BR fork (2)
64 and having the resulting child do a
65 .BR PTRACE_TRACEME ,
66 followed (typically) by an
67 .BR exec (3).
68 Alternatively, the parent may commence trace of an existing process using
69 .BR PTRACE_ATTACH .
70 .LP
71 While being traced, the child will stop each time a signal is delivered,
72 even if the signal is being ignored.
73 (The exception is
74 .BR SIGKILL ,
75 which has its usual effect.)
76 The parent will be notified at its next
77 .BR wait (2)
78 and may inspect and modify the child process while it is stopped.
79 The parent then causes the child to continue,
80 optionally ignoring the delivered signal
81 (or even delivering a different signal instead).
82 .LP
83 When the parent is finished tracing, it can terminate the child with
84 .B PTRACE_KILL
85 or cause it to continue executing in a normal, untraced mode
86 via
87 .BR PTRACE_DETACH .
88 .LP
89 The value of \fIrequest\fP determines the action to be performed:
90 .TP
91 .B PTRACE_TRACEME
92 Indicates that this process is to be traced by its parent.
93 Any signal
94 (except
95 .BR SIGKILL )
96 delivered to this process will cause it to stop and its
97 parent to be notified via
98 .BR wait (2).
99 Also, all subsequent calls to
100 .BR execve (2)
101 by this process will cause a
102 .B SIGTRAP
103 to be sent to it,
104 giving the parent a chance to gain control before the new program
105 begins execution.
106 A process probably shouldn't make this request if its parent
107 isn't expecting to trace it.
108 (\fIpid\fP, \fIaddr\fP, and \fIdata\fP are ignored.)
109 .LP
110 The above request is used only by the child process;
111 the rest are used only by the parent.
112 In the following requests, \fIpid\fP specifies the child process
113 to be acted on.
114 For requests other than
115 .BR PTRACE_KILL ,
116 the child process must
117 be stopped.
118 .TP
119 .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
120 Reads a word at the location
121 .I addr
122 in the child's memory, returning the word as the result of the
123 .BR ptrace ()
124 call.
125 Linux does not have separate text and data address spaces, so the two
126 requests are currently equivalent.
127 (The argument \fIdata\fP is ignored.)
128 .TP
129 .B PTRACE_PEEKUSER
130 .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
131 .\" and that is the name that seems common on other systems.
132 Reads a word at offset
133 .I addr
134 in the child's USER area,
135 which holds the registers and other information about the process
136 (see \fI<sys/user.h>\fP).
137 The word is returned as the result of the
138 .BR ptrace ()
139 call.
140 Typically the offset must be word-aligned, though this might vary by
141 architecture.
142 See NOTES.
143 (\fIdata\fP is ignored.)
144 .TP
145 .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
146 Copies the word
147 .I data
148 to location
149 .I addr
150 in the child's memory.
151 As above, the two requests are currently equivalent.
152 .TP
153 .B PTRACE_POKEUSER
154 .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
155 .\" and that is the name that seems common on other systems.
156 Copies the word
157 .I data
158 to offset
159 .I addr
160 in the child's USER area.
161 As above, the offset must typically be word-aligned.
162 In order to maintain the integrity of the kernel,
163 some modifications to the USER area are disallowed.
164 .TP
165 .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
166 Copies the child's general purpose or floating-point registers,
167 respectively, to location \fIdata\fP in the parent.
168 See \fI<sys/user.h>\fP for information on
169 the format of this data.
170 (\fIaddr\fP is ignored.)
171 .TP
172 .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
173 Retrieve information about the signal that caused the stop.
174 Copies a \fIsiginfo_t\fP structure (see
175 .BR sigaction (2))
176 from the child to location \fIdata\fP in the parent.
177 (\fIaddr\fP is ignored.)
178 .TP
179 .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
180 Copies the child's general purpose or floating-point registers,
181 respectively, from location \fIdata\fP in the parent.
182 As for
183 .BR PTRACE_POKEUSER ,
184 some general
185 purpose register modifications may be disallowed.
186 (\fIaddr\fP is ignored.)
187 .TP
188 .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
189 Set signal information.
190 Copies a \fIsiginfo_t\fP structure from location \fIdata\fP in the
191 parent to the child.
192 This will only affect signals that would normally be delivered to
193 the child and were caught by the tracer.
194 It may be difficult to tell
195 these normal signals from synthetic signals generated by
196 .BR ptrace ()
197 itself.
198 (\fIaddr\fP is ignored.)
199 .TP
200 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
201 Sets ptrace options from \fIdata\fP in the parent.
202 (\fIaddr\fP is ignored.)
203 \fIdata\fP is interpreted
204 as a bit mask of options, which are specified by the following flags:
205 .RS
206 .TP
207 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
208 When delivering syscall traps, set bit 7 in the signal number
209 (i.e., deliver \fI(SIGTRAP | 0x80)\fP
210 This makes it easy for the tracer to tell the difference
211 between normal traps and those caused by a syscall.
212 .RB ( PTRACE_O_TRACESYSGOOD
213 may not work on all architectures.)
214 .TP
215 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
216 Stop the child at the next
217 .BR fork (2)
218 call with \fISIGTRAP | PTRACE_EVENT_FORK\ <<\ 8\fP and automatically
219 start tracing the newly forked process,
220 which will start with a
221 .BR SIGSTOP .
222 The PID for the new process can be retrieved with
223 .BR PTRACE_GETEVENTMSG .
224 .TP
225 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
226 Stop the child at the next
227 .BR vfork (2)
228 call with \fISIGTRAP | PTRACE_EVENT_VFORK\ <<\ 8\fP and automatically start
229 tracing the newly vforked process, which will start with a
230 .BR SIGSTOP .
231 The PID for the new process can be retrieved with
232 .BR PTRACE_GETEVENTMSG .
233 .TP
234 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
235 Stop the child at the next
236 .BR clone (2)
237 call with \fISIGTRAP | PTRACE_EVENT_CLONE\ <<\ 8\fP and automatically start
238 tracing the newly cloned process, which will start with a
239 .BR SIGSTOP .
240 The PID for the new process can be retrieved with
241 .BR PTRACE_GETEVENTMSG .
242 This option may not catch
243 .BR clone (2)
244 calls in all cases.
245 If the child calls
246 .BR clone (2)
247 with the
248 .B CLONE_VFORK
249 flag,
250 .B PTRACE_EVENT_VFORK
251 will be delivered instead
252 if
253 .B PTRACE_O_TRACEVFORK
254 is set; otherwise if the child calls
255 .BR clone (2)
256 with the exit signal set to
257 .BR SIGCHLD ,
258 .B PTRACE_EVENT_FORK
259 will be delivered
260 if
261 .B PTRACE_O_TRACEFORK
262 is set.
263 .TP
264 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
265 Stop the child at the next
266 .BR execve (2)
267 call with \fISIGTRAP | PTRACE_EVENT_EXEC\ <<\ 8\fP.
268 .TP
269 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
270 Stop the child at the completion of the next
271 .BR vfork (2)
272 call with \fISIGTRAP | PTRACE_EVENT_VFORK_DONE\ <<\ 8\fP.
273 .TP
274 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
275 Stop the child at exit with \fISIGTRAP | PTRACE_EVENT_EXIT\ <<\ 8\fP.
276 The child's exit status can be retrieved with
277 .BR PTRACE_GETEVENTMSG .
278 This stop will be done early during process exit when registers
279 are still available, allowing the tracer to see where the exit occurred,
280 whereas the normal exit notification is done after the process
281 is finished exiting.
282 Even though context is available, the tracer cannot prevent the exit from
283 happening at this point.
284 .RE
285 .TP
286 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
287 Retrieve a message (as an
288 .IR "unsigned long" )
289 about the ptrace event
290 that just happened, placing it in the location \fIdata\fP in the parent.
291 For
292 .B PTRACE_EVENT_EXIT
293 this is the child's exit status.
294 For
295 .BR PTRACE_EVENT_FORK ,
296 .B PTRACE_EVENT_VFORK
297 and
298 .B PTRACE_EVENT_CLONE
299 this
300 is the PID of the new process.
301 Since Linux 2.6.18, the PID of the new process is also available
302 for
303 .BR PTRACE_EVENT_VFORK_DONE .
304 (\fIaddr\fP is ignored.)
305 .TP
306 .B PTRACE_CONT
307 Restarts the stopped child process.
308 If \fIdata\fP is nonzero and not
309 .BR SIGSTOP ,
310 it is interpreted as a signal to be delivered to the child;
311 otherwise, no signal is delivered.
312 Thus, for example, the parent can control
313 whether a signal sent to the child is delivered or not.
314 (\fIaddr\fP is ignored.)
315 .TP
316 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
317 Restarts the stopped child as for
318 .BR PTRACE_CONT ,
319 but arranges for
320 the child to be stopped at the next entry to or exit from a system call,
321 or after execution of a single instruction, respectively.
322 (The child will also, as usual, be stopped upon receipt of a signal.)
323 From the parent's perspective, the child will appear to have been
324 stopped by receipt of a
325 .BR SIGTRAP .
326 So, for
327 .BR PTRACE_SYSCALL ,
328 for example, the idea is to inspect
329 the arguments to the system call at the first stop,
330 then do another
331 .B PTRACE_SYSCALL
332 and inspect the return value of
333 the system call at the second stop.
334 The
335 .I data
336 argument is treated as for
337 .BR PTRACE_CONT .
338 (\fIaddr\fP is ignored.)
339 .TP
340 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
341 For
342 .BR PTRACE_SYSEMU ,
343 continue and stop on entry to the next syscall,
344 which will not be executed.
345 For
346 .BR PTRACE_SYSEMU_SINGLESTEP ,
347 do the same
348 but also singlestep if not a syscall.
349 This call is used by programs like
350 User Mode Linux that want to emulate all the child's system calls.
351 The
352 .I data
353 argument is treated as for
354 .BR PTRACE_CONT .
355 (\fIaddr\fP is ignored;
356 not supported on all architectures.)
357 .TP
358 .B PTRACE_KILL
359 Sends the child a
360 .B SIGKILL
361 to terminate it.
362 (\fIaddr\fP and \fIdata\fP are ignored.)
363 .TP
364 .B PTRACE_ATTACH
365 Attaches to the process specified in
366 .IR pid ,
367 making it a traced "child" of the calling process;
368 the behavior of the child is as if it had done a
369 .BR PTRACE_TRACEME .
370 The calling process actually becomes the parent of the child
371 process for most purposes (e.g., it will receive
372 notification of child events and appears in
373 .BR ps (1)
374 output as the child's parent), but a
375 .BR getppid (2)
376 by the child will still return the PID of the original parent.
377 The child is sent a
378 .BR SIGSTOP ,
379 but will not necessarily have stopped
380 by the completion of this call; use
381 .BR wait (2)
382 to wait for the child to stop.
383 (\fIaddr\fP and \fIdata\fP are ignored.)
384 .TP
385 .B PTRACE_DETACH
386 Restarts the stopped child as for
387 .BR PTRACE_CONT ,
388 but first detaches
389 from the process, undoing the reparenting effect of
390 .BR PTRACE_ATTACH ,
391 and the effects of
392 .BR PTRACE_TRACEME .
393 Although perhaps not intended, under Linux a traced child can be
394 detached in this way regardless of which method was used to initiate
395 tracing.
396 (\fIaddr\fP is ignored.)
397 .SH "RETURN VALUE"
398 On success,
399 .B PTRACE_PEEK*
400 requests return the requested data,
401 while other requests return zero.
402 On error, all requests return \-1, and
403 .I errno
404 is set appropriately.
405 Since the value returned by a successful
406 .B PTRACE_PEEK*
407 request may be \-1, the caller must check
408 .I errno
409 after such requests to determine whether or not an error occurred.
410 .SH ERRORS
411 .TP
412 .B EBUSY
413 (i386 only) There was an error with allocating or freeing a debug
414 register.
415 .TP
416 .B EFAULT
417 There was an attempt to read from or write to an invalid area in
418 the parent's or child's memory,
419 probably because the area wasn't mapped or accessible.
420 Unfortunately, under Linux, different variations of this fault
421 will return
422 .B EIO
423 or
424 .B EFAULT
425 more or less arbitrarily.
426 .TP
427 .B EINVAL
428 An attempt was made to set an invalid option.
429 .TP
430 .B EIO
431 \fIrequest\fP is invalid, or an attempt was made to read from or
432 write to an invalid area in the parent's or child's memory,
433 or there was a word-alignment violation,
434 or an invalid signal was specified during a restart request.
435 .TP
436 .B EPERM
437 The specified process cannot be traced.
438 This could be because the
439 parent has insufficient privileges (the required capability is
440 .BR CAP_SYS_PTRACE );
441 unprivileged processes cannot trace processes that they
442 cannot send signals to or those running
443 set-user-ID/set-group-ID programs, for obvious reasons.
444 Alternatively, the process may already be being traced, or be
445 .BR init (8)
446 (PID 1).
447 .TP
448 .B ESRCH
449 The specified process does not exist, or is not currently being traced
450 by the caller, or is not stopped (for requests that require that).
451 .SH "CONFORMING TO"
452 SVr4, 4.3BSD.
453 .SH NOTES
454 Although arguments to
455 .BR ptrace ()
456 are interpreted according to the prototype given,
457 glibc currently declares
458 .BR ptrace ()
459 as a variadic function with only the \fIrequest\fP argument fixed.
460 This means that unneeded trailing arguments may be omitted,
461 though doing so makes use of undocumented
462 .BR gcc (1)
463 behavior.
464 .LP
465 .BR init (8),
466 the process with PID 1, may not be traced.
467 .LP
468 The layout of the contents of memory and the USER area are quite OS- and
469 architecture-specific.
470 The offset supplied, and the data returned,
471 might not entirely match with the definition of
472 .IR "struct user" .
473 .\" See http://lkml.org/lkml/2008/5/8/375
474 .LP
475 The size of a "word" is determined by the OS variant
476 (e.g., for 32-bit Linux it is 32 bits, etc.).
477 .LP
478 Tracing causes a few subtle differences in the semantics of
479 traced processes.
480 For example, if a process is attached to with
481 .BR PTRACE_ATTACH ,
482 its original parent can no longer receive notification via
483 .BR wait (2)
484 when it stops, and there is no way for the new parent to
485 effectively simulate this notification.
486 .LP
487 When the parent receives an event with
488 .B PTRACE_EVENT_*
489 set,
490 the child is not in the normal signal delivery path.
491 This means the parent cannot do
492 .BR ptrace (PTRACE_CONT)
493 with a signal or
494 .BR ptrace (PTRACE_KILL).
495 .BR kill (2)
496 with a
497 .B SIGKILL
498 signal can be used instead to kill the child process
499 after receiving one of these messages.
500 .LP
501 This page documents the way the
502 .BR ptrace ()
503 call works currently in Linux.
504 Its behavior differs noticeably on other flavors of UNIX.
505 In any case, use of
506 .BR ptrace ()
507 is highly OS- and architecture-specific.
508 .LP
509 The SunOS man page describes
510 .BR ptrace ()
511 as "unique and arcane", which it is.
512 The proc-based debugging interface
513 present in Solaris 2 implements a superset of
514 .BR ptrace ()
515 functionality in a more powerful and uniform way.
516 .SH BUGS
517 On hosts with 2.6 kernel headers,
518 .B PTRACE_SETOPTIONS
519 is declared
520 with a different value than the one for 2.4.
521 This leads to applications compiled with such
522 headers failing when run on 2.4 kernels.
523 This can be worked around by redefining
524 .B PTRACE_SETOPTIONS
525 to
526 .BR PTRACE_OLDSETOPTIONS ,
527 if that is defined.
528 .SH "SEE ALSO"
529 .BR gdb (1),
530 .BR strace (1),
531 .BR execve (2),
532 .BR fork (2),
533 .BR signal (2),
534 .BR wait (2),
535 .BR exec (3),
536 .BR capabilities (7)