OSDN Git Service

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