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