OSDN Git Service

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