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