OSDN Git Service

(split) LDP: Update original to LDP v3.52.
[linuxjm/LDP_man-pages.git] / original / man2 / ptrace.2
1 .\" Copyright (c) 1993 Michael Haardt <michael@moria.de>
2 .\" Fri Apr  2 11:32:09 MET DST 1993
3 .\"
4 .\" and changes Copyright (C) 1999 Mike Coleman (mkc@acm.org)
5 .\" -- major revision to fully document ptrace semantics per recent Linux
6 .\"    kernel (2.2.10) and glibc (2.1.2)
7 .\" Sun Nov  7 03:18:35 CST 1999
8 .\"
9 .\" and Copyright (c) 2011, Denys Vlasenko <vda.linux@googlemail.com>
10 .\"
11 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
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, see
29 .\" <http://www.gnu.org/licenses/>.
30 .\" %%%LICENSE_END
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 .\" 2011-09, major update by Denys Vlasenko <vda.linux@googlemail.com>
43 .\"
44 .TH PTRACE 2 2013-02-16 "Linux" "Linux Programmer's Manual"
45 .SH NAME
46 ptrace \- process trace
47 .SH SYNOPSIS
48 .nf
49 .B #include <sys/ptrace.h>
50 .sp
51 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
52 .BI "            void *" addr ", void *" data );
53 .fi
54 .SH DESCRIPTION
55 The
56 .BR ptrace ()
57 system call provides a means by which one process (the "tracer")
58 may observe and control the execution of another process (the "tracee"),
59 and examine and change the tracee's memory and registers.
60 It is primarily used to implement breakpoint debugging and system
61 call tracing.
62 .LP
63 A tracee first needs to be attached to the tracer.
64 Attachment and subsequent commands are per thread:
65 in a multithreaded process,
66 every thread can be individually attached to a
67 (potentially different) tracer,
68 or left not attached and thus not debugged.
69 Therefore, "tracee" always means "(one) thread",
70 never "a (possibly multithreaded) process".
71 Ptrace commands are always sent to
72 a specific tracee using a call of the form
73
74     ptrace(PTRACE_foo, pid, ...)
75
76 where
77 .I pid
78 is the thread ID of the corresponding Linux thread.
79 .LP
80 (Note that in this page, a "multithreaded process"
81 means a thread group consisting of threads created using the
82 .BR clone (2)
83 .B CLONE_THREAD
84 flag.)
85 .LP
86 A process can initiate a trace by calling
87 .BR fork (2)
88 and having the resulting child do a
89 .BR PTRACE_TRACEME ,
90 followed (typically) by an
91 .BR execve (2).
92 Alternatively, one process may commence tracing another process using
93 .B PTRACE_ATTACH
94 or
95 .BR PTRACE_SEIZE .
96 .LP
97 While being traced, the tracee will stop each time a signal is delivered,
98 even if the signal is being ignored.
99 (An exception is
100 .BR SIGKILL ,
101 which has its usual effect.)
102 The tracer will be notified at its next call to
103 .BR waitpid (2)
104 (or one of the related "wait" system calls); that call will return a
105 .I status
106 value containing information that indicates
107 the cause of the stop in the tracee.
108 While the tracee is stopped,
109 the tracer can use various ptrace requests to inspect and modify the tracee.
110 The tracer then causes the tracee to continue,
111 optionally ignoring the delivered signal
112 (or even delivering a different signal instead).
113 .LP
114 If the
115 .B PTRACE_O_TRACEEXEC
116 option is not in effect, all successful calls to
117 .BR execve (2)
118 by the traced process will cause it to be sent a
119 .B SIGTRAP
120 signal,
121 giving the parent a chance to gain control before the new program
122 begins execution.
123 .LP
124 When the tracer is finished tracing, it can cause the tracee to continue
125 executing in a normal, untraced mode via
126 .BR PTRACE_DETACH .
127 .LP
128 The value of
129 .I request
130 determines the action to be performed:
131 .TP
132 .B PTRACE_TRACEME
133 Indicate that this process is to be traced by its parent.
134 A process probably shouldn't make this request if its parent
135 isn't expecting to trace it.
136 .RI ( pid ,
137 .IR addr ,
138 and
139 .IR data
140 are ignored.)
141 .LP
142 The
143 .B PTRACE_TRACEME
144 request is used only by the tracee;
145 the remaining requests are used only by the tracer.
146 In the following requests,
147 .I pid
148 specifies the thread ID of the tracee to be acted on.
149 For requests other than
150 .BR PTRACE_ATTACH ,
151 .BR PTRACE_SEIZE ,
152 .B PTRACE_INTERRUPT
153 and
154 .BR PTRACE_KILL ,
155 the tracee must be stopped.
156 .TP
157 .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
158 Read a word at the address
159 .I addr
160 in the tracee's memory, returning the word as the result of the
161 .BR ptrace ()
162 call.
163 Linux does not have separate text and data address spaces,
164 so these two requests are currently equivalent.
165 .RI ( data
166 is ignored.)
167 .TP
168 .B PTRACE_PEEKUSER
169 .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
170 .\" and that is the name that seems common on other systems.
171 Read a word at offset
172 .I addr
173 in the tracee's USER area,
174 which holds the registers and other information about the process
175 (see
176 .IR <sys/user.h> ).
177 The word is returned as the result of the
178 .BR ptrace ()
179 call.
180 Typically, the offset must be word-aligned, though this might vary by
181 architecture.
182 See NOTES.
183 .RI ( data
184 is ignored.)
185 .TP
186 .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
187 Copy the word
188 .I data
189 to the address
190 .I addr
191 in the tracee's memory.
192 As for
193 .BR PTRACE_PEEKTEXT
194 and
195 .BR PTRACE_PEEKDATA ,
196 these two requests are currently equivalent.
197 .TP
198 .B PTRACE_POKEUSER
199 .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
200 .\" and that is the name that seems common on other systems.
201 Copy the word
202 .I data
203 to offset
204 .I addr
205 in the tracee's USER area.
206 As for
207 .BR PTRACE_PEEKUSER ,
208 the offset must typically be word-aligned.
209 In order to maintain the integrity of the kernel,
210 some modifications to the USER area are disallowed.
211 .\" FIXME In the preceding sentence, which modifications are disallowed,
212 .\" and when they are disallowed, how does user space discover that fact?
213 .TP
214 .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
215 Copy the tracee's general-purpose or floating-point registers,
216 respectively, to the address
217 .I data
218 in the tracer.
219 See
220 .I <sys/user.h>
221 for information on the format of this data.
222 .RI ( addr
223 is ignored.)
224 Note that SPARC systems have the meaning of
225 .I data
226 and
227 .I addr
228 reversed; that is,
229 .I data
230 is ignored and the registers are copied to the address
231 .IR addr .
232 .B PTRACE_GETREGS
233 and
234 .B PTRACE_GETFPREGS
235 are not present on all architectures.
236 .TP
237 .BR PTRACE_GETREGSET " (since Linux 2.6.34)"
238 Read the tracee's registers.
239 .I addr
240 specifies, in an architecture-dependent way, the type of registers to be read.
241 .B NT_PRSTATUS
242 (with numerical value 1)
243 usually results in reading of general-purpose registers.
244 If the CPU has, for example,
245 floating-point and/or vector registers, they can be retrieved by setting
246 .I addr
247 to the corresponding
248 .B NT_foo
249 constant.
250 .I data
251 points to a
252 .BR "struct iovec" ,
253 which describes the destination buffer's location and length.
254 On return, the kernel modifies
255 .B iov.len
256 to indicate the actual number of bytes returned.
257 .TP
258 .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
259 Retrieve information about the signal that caused the stop.
260 Copy a
261 .I siginfo_t
262 structure (see
263 .BR sigaction (2))
264 from the tracee to the address
265 .I data
266 in the tracer.
267 .RI ( addr
268 is ignored.)
269 .TP
270 .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
271 Modify the tracee's general-purpose or floating-point registers,
272 respectively, from the address
273 .I data
274 in the tracer.
275 As for
276 .BR PTRACE_POKEUSER ,
277 some general-purpose register modifications may be disallowed.
278 .\" FIXME In the preceding sentence, which modifications are disallowed,
279 .\" and when they are disallowed, how does user space discover that fact?
280 .RI ( addr
281 is ignored.)
282 Note that SPARC systems have the meaning of
283 .I data
284 and
285 .I addr
286 reversed; that is,
287 .I data
288 is ignored and the registers are copied from the address
289 .IR addr .
290 .B PTRACE_SETREGS
291 and
292 .B PTRACE_SETFPREGS
293 are not present on all architectures.
294 .TP
295 .BR PTRACE_SETREGSET " (since Linux 2.6.34)"
296 Modify the tracee's registers.
297 The meaning of
298 .I addr
299 and
300 .I data
301 is analogous to
302 .BR PTRACE_GETREGSET .
303 .TP
304 .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
305 Set signal information:
306 copy a
307 .I siginfo_t
308 structure from the address
309 .I data
310 in the tracer to the tracee.
311 This will affect only signals that would normally be delivered to
312 the tracee and were caught by the tracer.
313 It may be difficult to tell
314 these normal signals from synthetic signals generated by
315 .BR ptrace ()
316 itself.
317 .RI ( addr
318 is ignored.)
319 .TP
320 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
321 Set ptrace options from
322 .IR data .
323 .RI ( addr
324 is ignored.)
325 .IR data
326 is interpreted as a bit mask of options,
327 which are specified by the following flags:
328 .RS
329 .TP
330 .BR PTRACE_O_EXITKILL " (since Linux 3.8)"
331 .\" commit 992fb6e170639b0849bace8e49bf31bd37c4123
332 If a tracer sets this flag, a
333 .B SIGKILL
334 signal will be sent to every tracee if the tracer exits.
335 This option is useful for ptrace jailers that
336 want to ensure that tracees can never escape the tracer's control.
337 .TP
338 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
339 Stop the tracee at the next
340 .BR clone (2)
341 and automatically start tracing the newly cloned process,
342 which will start with a
343 .BR SIGSTOP .
344 A
345 .BR waitpid (2)
346 by the tracer will return a
347 .I status
348 value such that
349
350 .nf
351   status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
352 .fi
353
354 The PID of the new process can be retrieved with
355 .BR PTRACE_GETEVENTMSG .
356 .IP
357 This option may not catch
358 .BR clone (2)
359 calls in all cases.
360 If the tracee calls
361 .BR clone (2)
362 with the
363 .B CLONE_VFORK
364 flag,
365 .B PTRACE_EVENT_VFORK
366 will be delivered instead
367 if
368 .B PTRACE_O_TRACEVFORK
369 is set; otherwise if the tracee calls
370 .BR clone (2)
371 with the exit signal set to
372 .BR SIGCHLD ,
373 .B PTRACE_EVENT_FORK
374 will be delivered if
375 .B PTRACE_O_TRACEFORK
376 is set.
377 .TP
378 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
379 Stop the tracee at the next
380 .BR execve (2).
381 A
382 .BR waitpid (2)
383 by the tracer will return a
384 .I status
385 value such that
386
387 .nf
388   status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
389 .fi
390
391 If the execing thread is not a thread group leader,
392 the thread ID is reset to thread group leader's ID before this stop.
393 Since Linux 3.0, the former thread ID can be retrieved with
394 .BR PTRACE_GETEVENTMSG .
395 .TP
396 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
397 Stop the tracee at exit.
398 A
399 .BR waitpid (2)
400 by the tracer will return a
401 .I status
402 value such that
403
404 .nf
405   status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
406 .fi
407
408 The tracee's exit status can be retrieved with
409 .BR PTRACE_GETEVENTMSG .
410 .IP
411 The tracee is stopped early during process exit,
412 when registers are still available,
413 allowing the tracer to see where the exit occurred,
414 whereas the normal exit notification is done after the process
415 is finished exiting.
416 Even though context is available,
417 the tracer cannot prevent the exit from happening at this point.
418 .TP
419 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
420 Stop the tracee at the next
421 .BR fork (2)
422 and automatically start tracing the newly forked process,
423 which will start with a
424 .BR SIGSTOP .
425 A
426 .BR waitpid (2)
427 by the tracer will return a
428 .I status
429 value such that
430
431 .nf
432   status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
433 .fi
434
435 The PID of the new process can be retrieved with
436 .BR PTRACE_GETEVENTMSG .
437 .TP
438 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
439 When delivering system call traps, set bit 7 in the signal number
440 (i.e., deliver
441 .IR "SIGTRAP|0x80" ).
442 This makes it easy for the tracer to distinguish
443 normal traps from those caused by a system call.
444 .RB ( PTRACE_O_TRACESYSGOOD
445 may not work on all architectures.)
446 .TP
447 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
448 Stop the tracee at the next
449 .BR vfork (2)
450 and automatically start tracing the newly vforked process,
451 which will start with a
452 .BR SIGSTOP .
453 A
454 .BR waitpid (2)
455 by the tracer will return a
456 .I status
457 value such that
458
459 .nf
460   status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
461 .fi
462
463 The PID of the new process can be retrieved with
464 .BR PTRACE_GETEVENTMSG .
465 .TP
466 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
467 Stop the tracee at the completion of the next
468 .BR vfork (2).
469 A
470 .BR waitpid (2)
471 by the tracer will return a
472 .I status
473 value such that
474
475 .nf
476   status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
477 .fi
478
479 The PID of the new process can (since Linux 2.6.18) be retrieved with
480 .BR PTRACE_GETEVENTMSG .
481 .RE
482 .TP
483 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
484 Retrieve a message (as an
485 .IR "unsigned long" )
486 about the ptrace event
487 that just happened, placing it at the address
488 .I data
489 in the tracer.
490 For
491 .BR PTRACE_EVENT_EXIT ,
492 this is the tracee's exit status.
493 For
494 .BR PTRACE_EVENT_FORK ,
495 .BR PTRACE_EVENT_VFORK ,
496 .BR PTRACE_EVENT_VFORK_DONE ,
497 and
498 .BR PTRACE_EVENT_CLONE ,
499 this is the PID of the new process.
500 .RI (  addr
501 is ignored.)
502 .TP
503 .B PTRACE_CONT
504 Restart the stopped tracee process.
505 If
506 .I data
507 is nonzero,
508 it is interpreted as the number of a signal to be delivered to the tracee;
509 otherwise, no signal is delivered.
510 Thus, for example, the tracer can control
511 whether a signal sent to the tracee is delivered or not.
512 .RI ( addr
513 is ignored.)
514 .TP
515 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
516 Restart the stopped tracee as for
517 .BR PTRACE_CONT ,
518 but arrange for the tracee to be stopped at
519 the next entry to or exit from a system call,
520 or after execution of a single instruction, respectively.
521 (The tracee will also, as usual, be stopped upon receipt of a signal.)
522 From the tracer's perspective, the tracee will appear to have been
523 stopped by receipt of a
524 .BR SIGTRAP .
525 So, for
526 .BR PTRACE_SYSCALL ,
527 for example, the idea is to inspect
528 the arguments to the system call at the first stop,
529 then do another
530 .B PTRACE_SYSCALL
531 and inspect the return value of the system call at the second stop.
532 The
533 .I data
534 argument is treated as for
535 .BR PTRACE_CONT .
536 .RI ( addr
537 is ignored.)
538 .TP
539 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
540 For
541 .BR PTRACE_SYSEMU ,
542 continue and stop on entry to the next system call,
543 which will not be executed.
544 For
545 .BR PTRACE_SYSEMU_SINGLESTEP ,
546 do the same but also singlestep if not a system call.
547 This call is used by programs like
548 User Mode Linux that want to emulate all the tracee's system calls.
549 The
550 .I data
551 argument is treated as for
552 .BR PTRACE_CONT .
553 The
554 .I addr
555 argument is ignored.
556 These requests are currently
557 .\" As at 3.7
558 supported only on x86.
559 .TP
560 .BR PTRACE_LISTEN " (since Linux 3.4)"
561 Restart the stopped tracee, but prevent it from executing.
562 The resulting state of the tracee is similar to a process which
563 has been stopped by a
564 .B SIGSTOP
565 (or other stopping signal).
566 See the "group-stop" subsection for additional information.
567 .B PTRACE_LISTEN
568 works only on tracees attached by
569 .BR PTRACE_SEIZE .
570 .TP
571 .B PTRACE_KILL
572 Send the tracee a
573 .B SIGKILL
574 to terminate it.
575 .RI ( addr
576 and
577 .I data
578 are ignored.)
579 .IP
580 .I This operation is deprecated; do not use it!
581 Instead, send a
582 .BR SIGKILL
583 directly using
584 .BR kill (2)
585 or
586 .BR tgkill (2).
587 The problem with
588 .B PTRACE_KILL
589 is that it requires the tracee to be in signal-delivery-stop,
590 otherwise it may not work
591 (i.e., may complete successfully but won't kill the tracee).
592 By contrast, sending a
593 .B SIGKILL
594 directly has no such limitation.
595 .\" [Note from Denys Vlasenko:
596 .\"     deprecation suggested by Oleg Nesterov. He prefers to deprecate it
597 .\"     instead of describing (and needing to support) PTRACE_KILL's quirks.]
598 .TP
599 .BR PTRACE_INTERRUPT " (since Linux 3.4)"
600 Stop a tracee.
601 If the tracee is running, it will stop with
602 .BR PTRACE_EVENT_STOP .
603 If the tracee is already stopped by a signal, or receives a signal
604 in parallel with
605 .BR PTRACE_INTERRUPT ,
606 it may report a group-stop
607 or a signal-delivery-stop instead of
608 .BR PTRACE_EVENT_STOP .
609 .B PTRACE_INTERRUPT
610 only works on tracees attached by
611 .BR PTRACE_SEIZE .
612 .TP
613 .B PTRACE_ATTACH
614 Attach to the process specified in
615 .IR pid ,
616 making it a tracee of the calling process.
617 .\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
618 .\"        "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
619 .\"         Basically, it's not true for any Linux in practical use.
620 .\" ; the behavior of the tracee is as if it had done a
621 .\" .BR PTRACE_TRACEME .
622 .\" The calling process actually becomes the parent of the tracee
623 .\" process for most purposes (e.g., it will receive
624 .\" notification of tracee events and appears in
625 .\" .BR ps (1)
626 .\" output as the tracee's parent), but a
627 .\" .BR getppid (2)
628 .\" by the tracee will still return the PID of the original parent.
629 The tracee is sent a
630 .BR SIGSTOP ,
631 but will not necessarily have stopped
632 by the completion of this call; use
633 .BR waitpid (2)
634 to wait for the tracee to stop.
635 See the "Attaching and detaching" subsection for additional information.
636 .RI ( addr
637 and
638 .I data
639 are ignored.)
640 .TP
641 .BR PTRACE_SEIZE " (since Linux 3.4)"
642 Attach to the process specified in
643 .IR pid ,
644 making it a tracee of the calling process.
645 Unlike
646 .BR PTRACE_ATTACH ,
647 .B PTRACE_SEIZE
648 does not stop the process.
649 Only a
650 .BR PTRACE_SEIZE d
651 process can accept
652 .B PTRACE_INTERRUPT
653 and
654 .B PTRACE_LISTEN
655 commands.
656 .I addr
657 must be zero.
658 .I data
659 contains a bit mask of ptrace options to activate immediately.
660 .TP
661 .B PTRACE_DETACH
662 Restart the stopped tracee as for
663 .BR PTRACE_CONT ,
664 but first detach from it.
665 Under Linux, a tracee can be detached in this way regardless
666 of which method was used to initiate tracing.
667 .RI ( addr
668 is ignored.)
669 .SS Death under ptrace
670 When a (possibly multithreaded) process receives a killing signal
671 (one whose disposition is set to
672 .B SIG_DFL
673 and whose default action is to kill the process),
674 all threads exit.
675 Tracees report their death to their tracer(s).
676 Notification of this event is delivered via
677 .BR waitpid (2).
678 .LP
679 Note that the killing signal will first cause signal-delivery-stop
680 (on one tracee only),
681 and only after it is injected by the tracer
682 (or after it was dispatched to a thread which isn't traced),
683 will death from the signal happen on
684 .I all
685 tracees within a multithreaded process.
686 (The term "signal-delivery-stop" is explained below.)
687 .LP
688 .B SIGKILL
689 does not generate signal-delivery-stop and
690 therefore the tracer can't suppress it.
691 .B SIGKILL
692 kills even within system calls
693 (syscall-exit-stop is not generated prior to death by
694 .BR SIGKILL ).
695 The net effect is that
696 .B SIGKILL
697 always kills the process (all its threads),
698 even if some threads of the process are ptraced.
699 .LP
700 When the tracee calls
701 .BR _exit (2),
702 it reports its death to its tracer.
703 Other threads are not affected.
704 .LP
705 When any thread executes
706 .BR exit_group (2),
707 every tracee in its thread group reports its death to its tracer.
708 .LP
709 If the
710 .B PTRACE_O_TRACEEXIT
711 option is on,
712 .B PTRACE_EVENT_EXIT
713 will happen before actual death.
714 This applies to exits via
715 .BR exit (2),
716 .BR exit_group (2),
717 and signal deaths (except
718 .BR SIGKILL ),
719 and when threads are torn down on
720 .BR execve (2)
721 in a multithreaded process.
722 .LP
723 The tracer cannot assume that the ptrace-stopped tracee exists.
724 There are many scenarios when the tracee may die while stopped (such as
725 .BR SIGKILL ).
726 Therefore, the tracer must be prepared to handle an
727 .B ESRCH
728 error on any ptrace operation.
729 Unfortunately, the same error is returned if the tracee
730 exists but is not ptrace-stopped
731 (for commands which require a stopped tracee),
732 or if it is not traced by the process which issued the ptrace call.
733 The tracer needs to keep track of the stopped/running state of the tracee,
734 and interpret
735 .B ESRCH
736 as "tracee died unexpectedly" only if it knows that the tracee has
737 been observed to enter ptrace-stop.
738 Note that there is no guarantee that
739 .I waitpid(WNOHANG)
740 will reliably report the tracee's death status if a
741 ptrace operation returned
742 .BR ESRCH .
743 .I waitpid(WNOHANG)
744 may return 0 instead.
745 In other words, the tracee may be "not yet fully dead",
746 but already refusing ptrace requests.
747 .LP
748 The tracer can't assume that the tracee
749 .I always
750 ends its life by reporting
751 .I WIFEXITED(status)
752 or
753 .IR WIFSIGNALED(status) ;
754 there are cases where this does not occur.
755 For example, if a thread other than thread group leader does an
756 .BR execve (2),
757 it disappears;
758 its PID will never be seen again,
759 and any subsequent ptrace stops will be reported under
760 the thread group leader's PID.
761 .SS Stopped states
762 A tracee can be in two states: running or stopped.
763 .LP
764 There are many kinds of states when the tracee is stopped, and in ptrace
765 discussions they are often conflated.
766 Therefore, it is important to use precise terms.
767 .LP
768 In this manual page, any stopped state in which the tracee is ready
769 to accept ptrace commands from the tracer is called
770 .IR ptrace-stop .
771 Ptrace-stops can
772 be further subdivided into
773 .IR signal-delivery-stop ,
774 .IR group-stop ,
775 .IR syscall-stop ,
776 and so on.
777 These stopped states are described in detail below.
778 .LP
779 When the running tracee enters ptrace-stop, it notifies its tracer using
780 .BR waitpid (2)
781 (or one of the other "wait" system calls).
782 Most of this manual page assumes that the tracer waits with:
783 .LP
784     pid = waitpid(pid_or_minus_1, &status, __WALL);
785 .LP
786 Ptrace-stopped tracees are reported as returns with
787 .I pid
788 greater than 0 and
789 .I WIFSTOPPED(status)
790 true.
791 .\" Denys Vlasenko:
792 .\"     Do we require __WALL usage, or will just using 0 be ok? (With 0,
793 .\"     I am not 100% sure there aren't ugly corner cases.) Are the
794 .\"     rules different if user wants to use waitid? Will waitid require
795 .\"     WEXITED?
796 .\"
797 .LP
798 The
799 .B __WALL
800 flag does not include the
801 .B WSTOPPED
802 and
803 .B WEXITED
804 flags, but implies their functionality.
805 .LP
806 Setting the
807 .B WCONTINUED
808 flag when calling
809 .BR waitpid (2)
810 is not recommended: the "continued" state is per-process and
811 consuming it can confuse the real parent of the tracee.
812 .LP
813 Use of the
814 .B WNOHANG
815 flag may cause
816 .BR waitpid (2)
817 to return 0 ("no wait results available yet")
818 even if the tracer knows there should be a notification.
819 Example:
820 .nf
821
822     errno = 0;
823     ptrace(PTRACE_CONT, pid, 0L, 0L);
824     if (errno == ESRCH) {
825         /* tracee is dead */
826         r = waitpid(tracee, &status, __WALL | WNOHANG);
827         /* r can still be 0 here! */
828     }
829 .fi
830 .\" FIXME:
831 .\"     waitid usage? WNOWAIT?
832 .\"     describe how wait notifications queue (or not queue)
833 .LP
834 The following kinds of ptrace-stops exist: signal-delivery-stops,
835 group-stops,
836 .B PTRACE_EVENT
837 stops, syscall-stops.
838 They all are reported by
839 .BR waitpid (2)
840 with
841 .I WIFSTOPPED(status)
842 true.
843 They may be differentiated by examining the value
844 .IR status>>8 ,
845 and if there is ambiguity in that value, by querying
846 .BR PTRACE_GETSIGINFO .
847 (Note: the
848 .I WSTOPSIG(status)
849 macro can't be used to perform this examination,
850 because it returns the value
851 .IR "(status>>8)\ &\ 0xff" .)
852 .SS Signal-delivery-stop
853 When a (possibly multithreaded) process receives any signal except
854 .BR SIGKILL ,
855 the kernel selects an arbitrary thread which handles the signal.
856 (If the signal is generated with
857 .BR tgkill (2),
858 the target thread can be explicitly selected by the caller.)
859 If the selected thread is traced, it enters signal-delivery-stop.
860 At this point, the signal is not yet delivered to the process,
861 and can be suppressed by the tracer.
862 If the tracer doesn't suppress the signal,
863 it passes the signal to the tracee in the next ptrace restart request.
864 This second step of signal delivery is called
865 .I "signal injection"
866 in this manual page.
867 Note that if the signal is blocked,
868 signal-delivery-stop doesn't happen until the signal is unblocked,
869 with the usual exception that
870 .B SIGSTOP
871 can't be blocked.
872 .LP
873 Signal-delivery-stop is observed by the tracer as
874 .BR waitpid (2)
875 returning with
876 .I WIFSTOPPED(status)
877 true, with the signal returned by
878 .IR WSTOPSIG(status) .
879 If the signal is
880 .BR SIGTRAP ,
881 this may be a different kind of ptrace-stop;
882 see the "Syscall-stops" and "execve" sections below for details.
883 If
884 .I WSTOPSIG(status)
885 returns a stopping signal, this may be a group-stop; see below.
886 .SS Signal injection and suppression
887 After signal-delivery-stop is observed by the tracer,
888 the tracer should restart the tracee with the call
889 .LP
890     ptrace(PTRACE_restart, pid, 0, sig)
891 .LP
892 where
893 .B PTRACE_restart
894 is one of the restarting ptrace requests.
895 If
896 .I sig
897 is 0, then a signal is not delivered.
898 Otherwise, the signal
899 .I sig
900 is delivered.
901 This operation is called
902 .I "signal injection"
903 in this manual page, to distinguish it from signal-delivery-stop.
904 .LP
905 The
906 .I sig
907 value may be different from the
908 .I WSTOPSIG(status)
909 value: the tracer can cause a different signal to be injected.
910 .LP
911 Note that a suppressed signal still causes system calls to return
912 prematurely.
913 In this case system calls will be restarted: the tracer will
914 observe the tracee to reexecute the interrupted system call (or
915 .BR restart_syscall (2)
916 system call for a few syscalls which use a different mechanism
917 for restarting) if the tracer uses
918 .BR PTRACE_SYSCALL .
919 Even system calls (such as
920 .BR poll (2))
921 which are not restartable after signal are restarted after
922 signal is suppressed;
923 however, kernel bugs exist which cause some syscalls to fail with
924 .B EINTR
925 even though no observable signal is injected to the tracee.
926 .LP
927 Restarting ptrace commands issued in ptrace-stops other than
928 signal-delivery-stop are not guaranteed to inject a signal, even if
929 .I sig
930 is nonzero.
931 No error is reported; a nonzero
932 .I sig
933 may simply be ignored.
934 Ptrace users should not try to "create a new signal" this way: use
935 .BR tgkill (2)
936 instead.
937 .LP
938 The fact that signal injection requests may be ignored
939 when restarting the tracee after
940 ptrace stops that are not signal-delivery-stops
941 is a cause of confusion among ptrace users.
942 One typical scenario is that the tracer observes group-stop,
943 mistakes it for signal-delivery-stop, restarts the tracee with
944
945     ptrace(PTRACE_restart, pid, 0, stopsig)
946
947 with the intention of injecting
948 .IR stopsig ,
949 but
950 .I stopsig
951 gets ignored and the tracee continues to run.
952 .LP
953 The
954 .B SIGCONT
955 signal has a side effect of waking up (all threads of)
956 a group-stopped process.
957 This side effect happens before signal-delivery-stop.
958 The tracer can't suppress this side effect (it can
959 only suppress signal injection, which only causes the
960 .BR SIGCONT
961 handler to not be executed in the tracee, if such a handler is installed).
962 In fact, waking up from group-stop may be followed by
963 signal-delivery-stop for signal(s)
964 .I other than
965 .BR SIGCONT ,
966 if they were pending when
967 .B SIGCONT
968 was delivered.
969 In other words,
970 .B SIGCONT
971 may be not the first signal observed by the tracee after it was sent.
972 .LP
973 Stopping signals cause (all threads of) a process to enter group-stop.
974 This side effect happens after signal injection, and therefore can be
975 suppressed by the tracer.
976 .LP
977 In Linux 2.4 and earlier, the
978 .B SIGSTOP
979 signal can't be injected.
980 .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
981 .\" there is:
982 .\"
983 .\"             /* The debugger continued.  Ignore SIGSTOP.  */
984 .\"             if (signr == SIGSTOP)
985 .\"                     continue;
986 .LP
987 .B PTRACE_GETSIGINFO
988 can be used to retrieve a
989 .I siginfo_t
990 structure which corresponds to the delivered signal.
991 .B PTRACE_SETSIGINFO
992 may be used to modify it.
993 If
994 .B PTRACE_SETSIGINFO
995 has been used to alter
996 .IR siginfo_t ,
997 the
998 .I si_signo
999 field and the
1000 .I sig
1001 parameter in the restarting command must match,
1002 otherwise the result is undefined.
1003 .SS Group-stop
1004 When a (possibly multithreaded) process receives a stopping signal,
1005 all threads stop.
1006 If some threads are traced, they enter a group-stop.
1007 Note that the stopping signal will first cause signal-delivery-stop
1008 (on one tracee only), and only after it is injected by the tracer
1009 (or after it was dispatched to a thread which isn't traced),
1010 will group-stop be initiated on
1011 .I all
1012 tracees within the multithreaded process.
1013 As usual, every tracee reports its group-stop separately
1014 to the corresponding tracer.
1015 .LP
1016 Group-stop is observed by the tracer as
1017 .BR waitpid (2)
1018 returning with
1019 .I WIFSTOPPED(status)
1020 true, with the stopping signal available via
1021 .IR WSTOPSIG(status) .
1022 The same result is returned by some other classes of ptrace-stops,
1023 therefore the recommended practice is to perform the call
1024 .LP
1025     ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
1026 .LP
1027 The call can be avoided if the signal is not
1028 .BR SIGSTOP ,
1029 .BR SIGTSTP ,
1030 .BR SIGTTIN ,
1031 or
1032 .BR SIGTTOU ;
1033 only these four signals are stopping signals.
1034 If the tracer sees something else, it can't be a group-stop.
1035 Otherwise, the tracer needs to call
1036 .BR PTRACE_GETSIGINFO .
1037 If
1038 .B PTRACE_GETSIGINFO
1039 fails with
1040 .BR EINVAL ,
1041 then it is definitely a group-stop.
1042 (Other failure codes are possible, such as
1043 .B ESRCH
1044 ("no such process") if a
1045 .B SIGKILL
1046 killed the tracee.)
1047 .LP
1048 As of Linux 2.6.38,
1049 after the tracer sees the tracee ptrace-stop and until it
1050 restarts or kills it, the tracee will not run,
1051 and will not send notifications (except
1052 .B SIGKILL
1053 death) to the tracer, even if the tracer enters into another
1054 .BR waitpid (2)
1055 call.
1056 .LP
1057 The kernel behavior described in the previous paragraph
1058 causes a problem with transparent handling of stopping signals.
1059 If the tracer restarts the tracee after group-stop,
1060 the stopping signal
1061 is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
1062 If the tracer doesn't restart the tracee before entering into the next
1063 .BR waitpid (2),
1064 future
1065 .B SIGCONT
1066 signals will not be reported to the tracer;
1067 this would cause the
1068 .B SIGCONT
1069 signals to have no effect on the tracee.
1070 .LP
1071 Since Linux 3.4, there is a method to overcome this problem: instead of
1072 .BR PTRACE_CONT ,
1073 a
1074 .B PTRACE_LISTEN
1075 command can be used to restart a tracee in a way where it does not execute,
1076 but waits for a new event which it can report via
1077 .BR waitpid (2)
1078 (such as when
1079 it is restarted by a
1080 .BR SIGCONT ).
1081 .SS PTRACE_EVENT stops
1082 If the tracer sets
1083 .B PTRACE_O_TRACE_*
1084 options, the tracee will enter ptrace-stops called
1085 .B PTRACE_EVENT
1086 stops.
1087 .LP
1088 .B PTRACE_EVENT
1089 stops are observed by the tracer as
1090 .BR waitpid (2)
1091 returning with
1092 .IR WIFSTOPPED(status) ,
1093 and
1094 .I WSTOPSIG(status)
1095 returns
1096 .BR SIGTRAP .
1097 An additional bit is set in the higher byte of the status word:
1098 the value
1099 .I status>>8
1100 will be
1101
1102     (SIGTRAP | PTRACE_EVENT_foo << 8).
1103
1104 The following events exist:
1105 .TP
1106 .B PTRACE_EVENT_VFORK
1107 Stop before return from
1108 .BR vfork (2)
1109 or
1110 .BR clone (2)
1111 with the
1112 .B CLONE_VFORK
1113 flag.
1114 When the tracee is continued after this stop, it will wait for child to
1115 exit/exec before continuing its execution
1116 (in other words, the usual behavior on
1117 .BR vfork (2)).
1118 .TP
1119 .B PTRACE_EVENT_FORK
1120 Stop before return from
1121 .BR fork (2)
1122 or
1123 .BR clone (2)
1124 with the exit signal set to
1125 .BR SIGCHLD .
1126 .TP
1127 .B PTRACE_EVENT_CLONE
1128 Stop before return from
1129 .BR clone (2).
1130 .TP
1131 .B PTRACE_EVENT_VFORK_DONE
1132 Stop before return from
1133 .BR vfork (2)
1134 or
1135 .BR clone (2)
1136 with the
1137 .B CLONE_VFORK
1138 flag,
1139 but after the child unblocked this tracee by exiting or execing.
1140 .LP
1141 For all four stops described above,
1142 the stop occurs in the parent (i.e., the tracee),
1143 not in the newly created thread.
1144 .BR PTRACE_GETEVENTMSG
1145 can be used to retrieve the new thread's ID.
1146 .TP
1147 .B PTRACE_EVENT_EXEC
1148 Stop before return from
1149 .BR execve (2).
1150 Since Linux 3.0,
1151 .BR PTRACE_GETEVENTMSG
1152 returns the former thread ID.
1153 .TP
1154 .B PTRACE_EVENT_EXIT
1155 Stop before exit (including death from
1156 .BR exit_group (2)),
1157 signal death, or exit caused by
1158 .BR execve (2)
1159 in a multithreaded process.
1160 .B PTRACE_GETEVENTMSG
1161 returns the exit status.
1162 Registers can be examined
1163 (unlike when "real" exit happens).
1164 The tracee is still alive; it needs to be
1165 .BR PTRACE_CONT ed
1166 or
1167 .BR PTRACE_DETACH ed
1168 to finish exiting.
1169 .TP
1170 .B PTRACE_EVENT_STOP
1171 Stop induced by
1172 .B PTRACE_INTERRUPT
1173 command.
1174 .LP
1175 .B PTRACE_GETSIGINFO
1176 on
1177 .B PTRACE_EVENT
1178 stops returns
1179 .B SIGTRAP
1180 in
1181 .IR si_signo ,
1182 with
1183 .I si_code
1184 set to
1185 .IR "(event<<8)\ |\ SIGTRAP" .
1186 .SS Syscall-stops
1187 If the tracee was restarted by
1188 .BR PTRACE_SYSCALL ,
1189 the tracee enters
1190 syscall-enter-stop just prior to entering any system call.
1191 If the tracer restarts the tracee with
1192 .BR PTRACE_SYSCALL ,
1193 the tracee enters syscall-exit-stop when the system call is finished,
1194 or if it is interrupted by a signal.
1195 (That is, signal-delivery-stop never happens between syscall-enter-stop
1196 and syscall-exit-stop; it happens
1197 .I after
1198 syscall-exit-stop.)
1199 .LP
1200 Other possibilities are that the tracee may stop in a
1201 .B PTRACE_EVENT
1202 stop, exit (if it entered
1203 .BR _exit (2)
1204 or
1205 .BR exit_group (2)),
1206 be killed by
1207 .BR SIGKILL ,
1208 or die silently (if it is a thread group leader, the
1209 .BR execve (2)
1210 happened in another thread,
1211 and that thread is not traced by the same tracer;
1212 this situation is discussed later).
1213 .LP
1214 Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1215 .BR waitpid (2)
1216 returning with
1217 .I WIFSTOPPED(status)
1218 true, and
1219 .I WSTOPSIG(status)
1220 giving
1221 .BR SIGTRAP .
1222 If the
1223 .B PTRACE_O_TRACESYSGOOD
1224 option was set by the tracer, then
1225 .I WSTOPSIG(status)
1226 will give the value
1227 .IR "(SIGTRAP\ |\ 0x80)" .
1228 .LP
1229 Syscall-stops can be distinguished from signal-delivery-stop with
1230 .B SIGTRAP
1231 by querying
1232 .BR PTRACE_GETSIGINFO
1233 for the following cases:
1234 .TP
1235 .IR si_code " <= 0"
1236 .B SIGTRAP
1237 was delivered as a result of a user-space action,
1238 for example, a system call
1239 .RB ( tgkill (2),
1240 .BR kill (2),
1241 .BR sigqueue (3),
1242 etc.),
1243 expiration of a POSIX timer,
1244 change of state on a POSIX message queue,
1245 or completion of an asynchronous I/O request.
1246 .TP
1247 .IR si_code " == SI_KERNEL (0x80)"
1248 .B SIGTRAP
1249 was sent by the kernel.
1250 .TP
1251 .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1252 This is a syscall-stop.
1253 .LP
1254 However, syscall-stops happen very often (twice per system call),
1255 and performing
1256 .B PTRACE_GETSIGINFO
1257 for every syscall-stop may be somewhat expensive.
1258 .LP
1259 Some architectures allow the cases to be distinguished
1260 by examining registers.
1261 For example, on x86,
1262 .I rax
1263 ==
1264 .RB - ENOSYS
1265 in syscall-enter-stop.
1266 Since
1267 .B SIGTRAP
1268 (like any other signal) always happens
1269 .I after
1270 syscall-exit-stop,
1271 and at this point
1272 .I rax
1273 almost never contains
1274 .RB - ENOSYS ,
1275 the
1276 .B SIGTRAP
1277 looks like "syscall-stop which is not syscall-enter-stop";
1278 in other words, it looks like a
1279 "stray syscall-exit-stop" and can be detected this way.
1280 But such detection is fragile and is best avoided.
1281 .LP
1282 Using the
1283 .B PTRACE_O_TRACESYSGOOD
1284 option is the recommended method to distinguish syscall-stops
1285 from other kinds of ptrace-stops,
1286 since it is reliable and does not incur a performance penalty.
1287 .LP
1288 Syscall-enter-stop and syscall-exit-stop are
1289 indistinguishable from each other by the tracer.
1290 The tracer needs to keep track of the sequence of
1291 ptrace-stops in order to not misinterpret syscall-enter-stop as
1292 syscall-exit-stop or vice versa.
1293 The rule is that syscall-enter-stop is
1294 always followed by syscall-exit-stop,
1295 .B PTRACE_EVENT
1296 stop or the tracee's death;
1297 no other kinds of ptrace-stop can occur in between.
1298 .LP
1299 If after syscall-enter-stop,
1300 the tracer uses a restarting command other than
1301 .BR PTRACE_SYSCALL ,
1302 syscall-exit-stop is not generated.
1303 .LP
1304 .B PTRACE_GETSIGINFO
1305 on syscall-stops returns
1306 .B SIGTRAP
1307 in
1308 .IR si_signo ,
1309 with
1310 .I si_code
1311 set to
1312 .B SIGTRAP
1313 or
1314 .IR (SIGTRAP|0x80) .
1315 .SS PTRACE_SINGLESTEP, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP stops
1316 [Details of these kinds of stops are yet to be documented.]
1317 .\"
1318 .\" FIXME
1319 .\" document stops occurring with PTRACE_SINGLESTEP, PTRACE_SYSEMU,
1320 .\" PTRACE_SYSEMU_SINGLESTEP
1321 .SS Informational and restarting ptrace commands
1322 Most ptrace commands (all except
1323 .BR PTRACE_ATTACH ,
1324 .BR PTRACE_SEIZE ,
1325 .BR PTRACE_TRACEME ,
1326 .BR PTRACE_INTERRUPT ,
1327 and
1328 .BR PTRACE_KILL )
1329 require the tracee to be in a ptrace-stop, otherwise they fail with
1330 .BR ESRCH .
1331 .LP
1332 When the tracee is in ptrace-stop,
1333 the tracer can read and write data to
1334 the tracee using informational commands.
1335 These commands leave the tracee in ptrace-stopped state:
1336 .LP
1337 .nf
1338     ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1339     ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1340     ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1341     ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1342     ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov);
1343     ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov);
1344     ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1345     ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1346     ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1347     ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1348 .fi
1349 .LP
1350 Note that some errors are not reported.
1351 For example, setting signal information
1352 .RI ( siginfo )
1353 may have no effect in some ptrace-stops, yet the call may succeed
1354 (return 0 and not set
1355 .IR errno );
1356 querying
1357 .B PTRACE_GETEVENTMSG
1358 may succeed and return some random value if current ptrace-stop
1359 is not documented as returning a meaningful event message.
1360 .LP
1361 The call
1362
1363     ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1364
1365 affects one tracee.
1366 The tracee's current flags are replaced.
1367 Flags are inherited by new tracees created and "auto-attached" via active
1368 .BR PTRACE_O_TRACEFORK ,
1369 .BR PTRACE_O_TRACEVFORK ,
1370 or
1371 .BR PTRACE_O_TRACECLONE
1372 options.
1373 .LP
1374 Another group of commands makes the ptrace-stopped tracee run.
1375 They have the form:
1376 .LP
1377     ptrace(cmd, pid, 0, sig);
1378 .LP
1379 where
1380 .I cmd
1381 is
1382 .BR PTRACE_CONT ,
1383 .BR PTRACE_LISTEN ,
1384 .BR PTRACE_DETACH ,
1385 .BR PTRACE_SYSCALL ,
1386 .BR PTRACE_SINGLESTEP ,
1387 .BR PTRACE_SYSEMU ,
1388 or
1389 .BR PTRACE_SYSEMU_SINGLESTEP .
1390 If the tracee is in signal-delivery-stop,
1391 .I sig
1392 is the signal to be injected (if it is nonzero).
1393 Otherwise,
1394 .I sig
1395 may be ignored.
1396 (When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
1397 recommended practice is to always pass 0 in
1398 .IR sig .)
1399 .SS Attaching and detaching
1400 A thread can be attached to the tracer using the call
1401
1402     ptrace(PTRACE_ATTACH, pid, 0, 0);
1403
1404 or
1405
1406     ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags);
1407
1408 .B PTRACE_ATTACH
1409 sends
1410 .B SIGSTOP
1411 to this thread.
1412 If the tracer wants this
1413 .B SIGSTOP
1414 to have no effect, it needs to suppress it.
1415 Note that if other signals are concurrently sent to
1416 this thread during attach,
1417 the tracer may see the tracee enter signal-delivery-stop
1418 with other signal(s) first!
1419 The usual practice is to reinject these signals until
1420 .B SIGSTOP
1421 is seen, then suppress
1422 .B SIGSTOP
1423 injection.
1424 The design bug here is that a ptrace attach and a concurrently delivered
1425 .B SIGSTOP
1426 may race and the concurrent
1427 .B SIGSTOP
1428 may be lost.
1429 .\"
1430 .\" FIXME: Describe how to attach to a thread which is already
1431 .\"        group-stopped.
1432 .LP
1433 Since attaching sends
1434 .B SIGSTOP
1435 and the tracer usually suppresses it, this may cause a stray
1436 .B EINTR
1437 return from the currently executing system call in the tracee,
1438 as described in the "Signal injection and suppression" section.
1439 .LP
1440 Since Linux 3.4,
1441 .B PTRACE_SEIZE
1442 can be used instead of
1443 .BR PTRACE_ATTACH .
1444 .B PTRACE_SEIZE
1445 does not stop the attached process.
1446 If you need to stop
1447 it after attach (or at any other time) without sending it any signals,
1448 use
1449 .B PTRACE_INTERRUPT
1450 command.
1451 .LP
1452 The request
1453
1454     ptrace(PTRACE_TRACEME, 0, 0, 0);
1455
1456 turns the calling thread into a tracee.
1457 The thread continues to run (doesn't enter ptrace-stop).
1458 A common practice is to follow the
1459 .B PTRACE_TRACEME
1460 with
1461
1462     raise(SIGSTOP);
1463
1464 and allow the parent (which is our tracer now) to observe our
1465 signal-delivery-stop.
1466 .LP
1467 If the
1468 .BR PTRACE_O_TRACEFORK ,
1469 .BR PTRACE_O_TRACEVFORK ,
1470 or
1471 .BR PTRACE_O_TRACECLONE
1472 options are in effect, then children created by, respectively,
1473 .BR vfork (2)
1474 or
1475 .BR clone (2)
1476 with the
1477 .B CLONE_VFORK
1478 flag,
1479 .BR fork (2)
1480 or
1481 .BR clone (2)
1482 with the exit signal set to
1483 .BR SIGCHLD ,
1484 and other kinds of
1485 .BR clone (2),
1486 are automatically attached to the same tracer which traced their parent.
1487 .B SIGSTOP
1488 is delivered to the children, causing them to enter
1489 signal-delivery-stop after they exit the system call which created them.
1490 .LP
1491 Detaching of the tracee is performed by:
1492
1493     ptrace(PTRACE_DETACH, pid, 0, sig);
1494
1495 .B PTRACE_DETACH
1496 is a restarting operation;
1497 therefore it requires the tracee to be in ptrace-stop.
1498 If the tracee is in signal-delivery-stop, a signal can be injected.
1499 Otherwise, the
1500 .I sig
1501 parameter may be silently ignored.
1502 .LP
1503 If the tracee is running when the tracer wants to detach it,
1504 the usual solution is to send
1505 .B SIGSTOP
1506 (using
1507 .BR tgkill (2),
1508 to make sure it goes to the correct thread),
1509 wait for the tracee to stop in signal-delivery-stop for
1510 .B SIGSTOP
1511 and then detach it (suppressing
1512 .B SIGSTOP
1513 injection).
1514 A design bug is that this can race with concurrent
1515 .BR SIGSTOP s.
1516 Another complication is that the tracee may enter other ptrace-stops
1517 and needs to be restarted and waited for again, until
1518 .B SIGSTOP
1519 is seen.
1520 Yet another complication is to be sure that
1521 the tracee is not already ptrace-stopped,
1522 because no signal delivery happens while it is\(emnot even
1523 .BR SIGSTOP .
1524 .\" FIXME: Describe how to detach from a group-stopped tracee so that it
1525 .\"        doesn't run, but continues to wait for SIGCONT.
1526 .LP
1527 If the tracer dies, all tracees are automatically detached and restarted,
1528 unless they were in group-stop.
1529 Handling of restart from group-stop is currently buggy,
1530 but the "as planned" behavior is to leave tracee stopped and waiting for
1531 .BR SIGCONT .
1532 If the tracee is restarted from signal-delivery-stop,
1533 the pending signal is injected.
1534 .SS execve(2) under ptrace
1535 .\" clone(2) CLONE_THREAD says:
1536 .\"     If  any  of the threads in a thread group performs an execve(2),
1537 .\"     then all threads other than the thread group leader are terminated,
1538 .\"     and the new program is executed in the thread group leader.
1539 .\"
1540 When one thread in a multithreaded process calls
1541 .BR execve (2),
1542 the kernel destroys all other threads in the process,
1543 .\" In kernel 3.1 sources, see fs/exec.c::de_thread()
1544 and resets the thread ID of the execing thread to the
1545 thread group ID (process ID).
1546 (Or, to put things another way, when a multithreaded process does an
1547 .BR execve (2),
1548 at completion of the call, it appears as though the
1549 .BR execve (2)
1550 occurred in the thread group leader, regardless of which thread did the
1551 .BR execve (2).)
1552 This resetting of the thread ID looks very confusing to tracers:
1553 .IP * 3
1554 All other threads stop in
1555 .B PTRACE_EVENT_EXIT
1556 stop, if the
1557 .BR PTRACE_O_TRACEEXIT
1558 option was turned on.
1559 Then all other threads except the thread group leader report
1560 death as if they exited via
1561 .BR _exit (2)
1562 with exit code 0.
1563 .IP *
1564 The execing tracee changes its thread ID while it is in the
1565 .BR execve (2).
1566 (Remember, under ptrace, the "pid" returned from
1567 .BR waitpid (2),
1568 or fed into ptrace calls, is the tracee's thread ID.)
1569 That is, the tracee's thread ID is reset to be the same as its process ID,
1570 which is the same as the thread group leader's thread ID.
1571 .IP *
1572 Then a
1573 .B PTRACE_EVENT_EXEC
1574 stop happens, if the
1575 .BR PTRACE_O_TRACEEXEC
1576 option was turned on.
1577 .IP *
1578 If the thread group leader has reported its
1579 .B PTRACE_EVENT_EXIT
1580 stop by this time,
1581 it appears to the tracer that
1582 the dead thread leader "reappears from nowhere".
1583 (Note: the thread group leader does not report death via
1584 .I WIFEXITED(status)
1585 until there is at least one other live thread.
1586 This eliminates the possibility that the tracer will see
1587 it dying and then reappearing.)
1588 If the thread group leader was still alive,
1589 for the tracer this may look as if thread group leader
1590 returns from a different system call than it entered,
1591 or even "returned from a system call even though
1592 it was not in any system call".
1593 If the thread group leader was not traced
1594 (or was traced by a different tracer), then during
1595 .BR execve (2)
1596 it will appear as if it has become a tracee of
1597 the tracer of the execing tracee.
1598 .LP
1599 All of the above effects are the artifacts of
1600 the thread ID change in the tracee.
1601 .LP
1602 The
1603 .B PTRACE_O_TRACEEXEC
1604 option is the recommended tool for dealing with this situation.
1605 First, it enables
1606 .BR PTRACE_EVENT_EXEC
1607 stop,
1608 which occurs before
1609 .BR execve (2)
1610 returns.
1611 In this stop, the tracer can use
1612 .B PTRACE_GETEVENTMSG
1613 to retrieve the tracee's former thread ID.
1614 (This feature was introduced in Linux 3.0).
1615 Second, the
1616 .B PTRACE_O_TRACEEXEC
1617 option disables legacy
1618 .B SIGTRAP
1619 generation on
1620 .BR execve (2).
1621 .LP
1622 When the tracer receives
1623 .B PTRACE_EVENT_EXEC
1624 stop notification,
1625 it is guaranteed that except this tracee and the thread group leader,
1626 no other threads from the process are alive.
1627 .LP
1628 On receiving the
1629 .B PTRACE_EVENT_EXEC
1630 stop notification,
1631 the tracer should clean up all its internal
1632 data structures describing the threads of this process,
1633 and retain only one data structure\(emone which
1634 describes the single still running tracee, with
1635
1636     thread ID == thread group ID == process ID.
1637 .LP
1638 Example: two threads call
1639 .BR execve (2)
1640 at the same time:
1641 .LP
1642 .nf
1643 *** we get syscall-enter-stop in thread 1: **
1644 PID1 execve("/bin/foo", "foo" <unfinished ...>
1645 *** we issue PTRACE_SYSCALL for thread 1 **
1646 *** we get syscall-enter-stop in thread 2: **
1647 PID2 execve("/bin/bar", "bar" <unfinished ...>
1648 *** we issue PTRACE_SYSCALL for thread 2 **
1649 *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
1650 *** we get syscall-exit-stop for PID0: **
1651 PID0 <... execve resumed> )             = 0
1652 .fi
1653 .LP
1654 If the
1655 .B PTRACE_O_TRACEEXEC
1656 option is
1657 .I not
1658 in effect for the execing tracee, the kernel delivers an extra
1659 .B SIGTRAP
1660 to the tracee after
1661 .BR execve (2)
1662 returns.
1663 This is an ordinary signal (similar to one which can be
1664 generated by
1665 .IR "kill -TRAP" ),
1666 not a special kind of ptrace-stop.
1667 Employing
1668 .B PTRACE_GETSIGINFO
1669 for this signal returns
1670 .I si_code
1671 set to 0
1672 .RI ( SI_USER ).
1673 This signal may be blocked by signal mask,
1674 and thus may be delivered (much) later.
1675 .LP
1676 Usually, the tracer (for example,
1677 .BR strace (1))
1678 would not want to show this extra post-execve
1679 .B SIGTRAP
1680 signal to the user, and would suppress its delivery to the tracee (if
1681 .B SIGTRAP
1682 is set to
1683 .BR SIG_DFL ,
1684 it is a killing signal).
1685 However, determining
1686 .I which
1687 .B SIGTRAP
1688 to suppress is not easy.
1689 Setting the
1690 .B PTRACE_O_TRACEEXEC
1691 option and thus suppressing this extra
1692 .B SIGTRAP
1693 is the recommended approach.
1694 .SS Real parent
1695 The ptrace API (ab)uses the standard UNIX parent/child signaling over
1696 .BR waitpid (2).
1697 This used to cause the real parent of the process to stop receiving
1698 several kinds of
1699 .BR waitpid (2)
1700 notifications when the child process is traced by some other process.
1701 .LP
1702 Many of these bugs have been fixed, but as of Linux 2.6.38 several still
1703 exist; see BUGS below.
1704 .LP
1705 As of Linux 2.6.38, the following is believed to work correctly:
1706 .IP * 3
1707 exit/death by signal is reported first to the tracer, then,
1708 when the tracer consumes the
1709 .BR waitpid (2)
1710 result, to the real parent (to the real parent only when the
1711 whole multithreaded process exits).
1712 If the tracer and the real parent are the same process,
1713 the report is sent only once.
1714 .SH RETURN VALUE
1715 On success,
1716 .B PTRACE_PEEK*
1717 requests return the requested data,
1718 while other requests return zero.
1719 On error, all requests return \-1, and
1720 .I errno
1721 is set appropriately.
1722 Since the value returned by a successful
1723 .B PTRACE_PEEK*
1724 request may be \-1, the caller must clear
1725 .I errno
1726 before the call, and then check it afterward
1727 to determine whether or not an error occurred.
1728 .SH ERRORS
1729 .TP
1730 .B EBUSY
1731 (i386 only) There was an error with allocating or freeing a debug register.
1732 .TP
1733 .B EFAULT
1734 There was an attempt to read from or write to an invalid area in
1735 the tracer's or the tracee's memory,
1736 probably because the area wasn't mapped or accessible.
1737 Unfortunately, under Linux, different variations of this fault
1738 will return
1739 .B EIO
1740 or
1741 .B EFAULT
1742 more or less arbitrarily.
1743 .TP
1744 .B EINVAL
1745 An attempt was made to set an invalid option.
1746 .TP
1747 .B EIO
1748 .I request
1749 is invalid, or an attempt was made to read from or
1750 write to an invalid area in the tracer's or the tracee's memory,
1751 or there was a word-alignment violation,
1752 or an invalid signal was specified during a restart request.
1753 .TP
1754 .B EPERM
1755 The specified process cannot be traced.
1756 This could be because the
1757 tracer has insufficient privileges (the required capability is
1758 .BR CAP_SYS_PTRACE );
1759 unprivileged processes cannot trace processes that they
1760 cannot send signals to or those running
1761 set-user-ID/set-group-ID programs, for obvious reasons.
1762 Alternatively, the process may already be being traced,
1763 or (on kernels before 2.6.26) be
1764 .BR init (8)
1765 (PID 1).
1766 .TP
1767 .B ESRCH
1768 The specified process does not exist, or is not currently being traced
1769 by the caller, or is not stopped
1770 (for requests that require a stopped tracee).
1771 .SH CONFORMING TO
1772 SVr4, 4.3BSD.
1773 .SH NOTES
1774 Although arguments to
1775 .BR ptrace ()
1776 are interpreted according to the prototype given,
1777 glibc currently declares
1778 .BR ptrace ()
1779 as a variadic function with only the
1780 .I request
1781 argument fixed.
1782 It is recommended to always supply four arguments,
1783 even if the requested operation does not use them,
1784 setting unused/ignored arguments to
1785 .I 0L
1786 or
1787 .IR "(void\ *)\ 0".
1788 .LP
1789 In Linux kernels before 2.6.26,
1790 .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
1791 .BR init (8),
1792 the process with PID 1, may not be traced.
1793 .LP
1794 The layout of the contents of memory and the USER area are
1795 quite operating-system- and architecture-specific.
1796 The offset supplied, and the data returned,
1797 might not entirely match with the definition of
1798 .IR "struct user" .
1799 .\" See http://lkml.org/lkml/2008/5/8/375
1800 .LP
1801 The size of a "word" is determined by the operating-system variant
1802 (e.g., for 32-bit Linux it is 32 bits).
1803 .LP
1804 This page documents the way the
1805 .BR ptrace ()
1806 call works currently in Linux.
1807 Its behavior differs noticeably on other flavors of UNIX.
1808 In any case, use of
1809 .BR ptrace ()
1810 is highly specific to the operating system and architecture.
1811 .SH BUGS
1812 On hosts with 2.6 kernel headers,
1813 .B PTRACE_SETOPTIONS
1814 is declared with a different value than the one for 2.4.
1815 This leads to applications compiled with 2.6 kernel
1816 headers failing when run on 2.4 kernels.
1817 This can be worked around by redefining
1818 .B PTRACE_SETOPTIONS
1819 to
1820 .BR PTRACE_OLDSETOPTIONS ,
1821 if that is defined.
1822 .LP
1823 Group-stop notifications are sent to the tracer, but not to real parent.
1824 Last confirmed on 2.6.38.6.
1825 .LP
1826 If a thread group leader is traced and exits by calling
1827 .BR _exit (2),
1828 .\" Note from Denys Vlasenko:
1829 .\"     Here "exits" means any kind of death - _exit, exit_group,
1830 .\"     signal death. Signal death and exit_group cases are trivial,
1831 .\"     though: since signal death and exit_group kill all other threads
1832 .\"     too, "until all other threads exit" thing happens rather soon
1833 .\"     in these cases. Therefore, only _exit presents observably
1834 .\"     puzzling behavior to ptrace users: thread leader _exit's,
1835 .\"     but WIFEXITED isn't reported! We are trying to explain here
1836 .\"     why it is so.
1837 a
1838 .B PTRACE_EVENT_EXIT
1839 stop will happen for it (if requested), but the subsequent
1840 .B WIFEXITED
1841 notification will not be delivered until all other threads exit.
1842 As explained above, if one of other threads calls
1843 .BR execve (2),
1844 the death of the thread group leader will
1845 .I never
1846 be reported.
1847 If the execed thread is not traced by this tracer,
1848 the tracer will never know that
1849 .BR execve (2)
1850 happened.
1851 One possible workaround is to
1852 .B PTRACE_DETACH
1853 the thread group leader instead of restarting it in this case.
1854 Last confirmed on 2.6.38.6.
1855 .\"  FIXME: ^^^ need to test/verify this scenario
1856 .LP
1857 A
1858 .B SIGKILL
1859 signal may still cause a
1860 .B PTRACE_EVENT_EXIT
1861 stop before actual signal death.
1862 This may be changed in the future;
1863 .B SIGKILL
1864 is meant to always immediately kill tasks even under ptrace.
1865 Last confirmed on 2.6.38.6.
1866 .LP
1867 Some system calls return with
1868 .B EINTR
1869 if a signal was sent to a tracee, but delivery was suppressed by the tracer.
1870 (This is very typical operation: it is usually
1871 done by debuggers on every attach, in order to not introduce
1872 a bogus
1873 .BR SIGSTOP ).
1874 As of Linux 3.2.9, the following system calls are affected
1875 (this list is likely incomplete):
1876 .BR epoll_wait (2),
1877 and
1878 .BR read (2)
1879 from an
1880 .BR inotify (7)
1881 file descriptor.
1882 The usual symptom of this bug is that when you attach to
1883 a quiescent process with the command
1884
1885     strace -p <process-ID>
1886
1887 then, instead of the usual
1888 and expected one-line output such as
1889 .nf
1890
1891     restart_syscall(<... resuming interrupted call ...>_
1892
1893 .fi
1894 or
1895 .nf
1896
1897     select(6, [5], NULL, [5], NULL_
1898
1899 .fi
1900 ('_' denotes the cursor position), you observe more than one line.
1901 For example:
1902 .nf
1903
1904     clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0
1905     epoll_wait(4,_
1906
1907 .fi
1908 What is not visible here is that the process was blocked in
1909 .BR epoll_wait (2)
1910 before
1911 .BR strace (1)
1912 has attached to it.
1913 Attaching caused
1914 .BR epoll_wait (2)
1915 to return to user space with the error
1916 .BR EINTR .
1917 In this particular case, the program reacted to
1918 .B EINTR
1919 by checking the current time, and then executing
1920 .BR epoll_wait (2)
1921 again.
1922 (Programs which do not expect such "stray"
1923 .BR EINTR
1924 errors may behave in an unintended way upon an
1925 .BR strace (1)
1926 attach.)
1927 .SH SEE ALSO
1928 .BR gdb (1),
1929 .BR strace (1),
1930 .BR clone (2),
1931 .BR execve (2),
1932 .BR fork (2),
1933 .BR gettid (2),
1934 .BR sigaction (2),
1935 .BR tgkill (2),
1936 .BR vfork (2),
1937 .BR waitpid (2),
1938 .BR exec (3),
1939 .BR capabilities (7),
1940 .BR signal (7)