]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/ptrace.2
bind.2, chmod.2, chown.2, chroot.2, clock_getres.2, clone.2, connect.2, dup.2, falloc...
[thirdparty/man-pages.git] / man2 / ptrace.2
1 .\" Copyright (c) 1993 Michael Haardt <michael@moria.de>
2 .\" Fri Apr 2 11:32:09 MET DST 1993
3 .\"
4 .\" and changes Copyright (C) 1999 Mike Coleman (mkc@acm.org)
5 .\" -- major revision to fully document ptrace semantics per recent Linux
6 .\" kernel (2.2.10) and glibc (2.1.2)
7 .\" Sun Nov 7 03:18:35 CST 1999
8 .\"
9 .\" and Copyright (c) 2011, Denys Vlasenko <vda.linux@googlemail.com>
10 .\" and Copyright (c) 2015, 2016, Michael Kerrisk <mtk.manpages@gmail.com>
11 .\"
12 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
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, see
30 .\" <http://www.gnu.org/licenses/>.
31 .\" %%%LICENSE_END
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 .\" 2015-01, Kees Cook <keescook@chromium.org>
45 .\" Added PTRACE_O_TRACESECCOMP, PTRACE_EVENT_SECCOMP
46 .\"
47 .\" FIXME The following are undocumented:
48 .\"
49 .\" PTRACE_GETWMMXREGS
50 .\" PTRACE_SETWMMXREGS
51 .\" ARM
52 .\" Linux 2.6.12
53 .\"
54 .\" PTRACE_SET_SYSCALL
55 .\" ARM and ARM64
56 .\" Linux 2.6.16
57 .\" commit 3f471126ee53feb5e9b210ea2f525ed3bb9b7a7f
58 .\" Author: Nicolas Pitre <nico@cam.org>
59 .\" Date: Sat Jan 14 19:30:04 2006 +0000
60 .\"
61 .\" PTRACE_GETCRUNCHREGS
62 .\" PTRACE_SETCRUNCHREGS
63 .\" ARM
64 .\" Linux 2.6.18
65 .\" commit 3bec6ded282b331552587267d67a06ed7fd95ddd
66 .\" Author: Lennert Buytenhek <buytenh@wantstofly.org>
67 .\" Date: Tue Jun 27 22:56:18 2006 +0100
68 .\"
69 .\" PTRACE_GETVFPREGS
70 .\" PTRACE_SETVFPREGS
71 .\" ARM and ARM64
72 .\" Linux 2.6.30
73 .\" commit 3d1228ead618b88e8606015cbabc49019981805d
74 .\" Author: Catalin Marinas <catalin.marinas@arm.com>
75 .\" Date: Wed Feb 11 13:12:56 2009 +0100
76 .\"
77 .\" PTRACE_GETHBPREGS
78 .\" PTRACE_SETHBPREGS
79 .\" ARM and ARM64
80 .\" Linux 2.6.37
81 .\" commit 864232fa1a2f8dfe003438ef0851a56722740f3e
82 .\" Author: Will Deacon <will.deacon@arm.com>
83 .\" Date: Fri Sep 3 10:42:55 2010 +0100
84 .\"
85 .\" PTRACE_SINGLEBLOCK
86 .\" Since at least Linux 2.4.0 on various architectures
87 .\" Since Linux 2.6.25 on x86 (and others?)
88 .\" commit 5b88abbf770a0e1975c668743100f42934f385e8
89 .\" Author: Roland McGrath <roland@redhat.com>
90 .\" Date: Wed Jan 30 13:30:53 2008 +0100
91 .\" ptrace: generic PTRACE_SINGLEBLOCK
92 .\"
93 .\" PTRACE_GETFPXREGS
94 .\" PTRACE_SETFPXREGS
95 .\" Since at least Linux 2.4.0 on various architectures
96 .\"
97 .\" PTRACE_GETFDPIC
98 .\" PTRACE_GETFDPIC_EXEC
99 .\" PTRACE_GETFDPIC_INTERP
100 .\" blackfin, c6x, frv, sh
101 .\" First appearance in Linux 2.6.11 on frv
102 .\"
103 .\" and others that can be found in the arch/*/include/uapi/asm/ptrace files
104 .\"
105 .TH PTRACE 2 2016-12-12 "Linux" "Linux Programmer's Manual"
106 .SH NAME
107 ptrace \- process trace
108 .SH SYNOPSIS
109 .nf
110 .B #include <sys/ptrace.h>
111 .sp
112 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
113 .BI " void *" addr ", void *" data );
114 .fi
115 .SH DESCRIPTION
116 The
117 .BR ptrace ()
118 system call provides a means by which one process (the "tracer")
119 may observe and control the execution of another process (the "tracee"),
120 and examine and change the tracee's memory and registers.
121 It is primarily used to implement breakpoint debugging and system
122 call tracing.
123 .LP
124 A tracee first needs to be attached to the tracer.
125 Attachment and subsequent commands are per thread:
126 in a multithreaded process,
127 every thread can be individually attached to a
128 (potentially different) tracer,
129 or left not attached and thus not debugged.
130 Therefore, "tracee" always means "(one) thread",
131 never "a (possibly multithreaded) process".
132 Ptrace commands are always sent to
133 a specific tracee using a call of the form
134
135 ptrace(PTRACE_foo, pid, ...)
136
137 where
138 .I pid
139 is the thread ID of the corresponding Linux thread.
140 .LP
141 (Note that in this page, a "multithreaded process"
142 means a thread group consisting of threads created using the
143 .BR clone (2)
144 .B CLONE_THREAD
145 flag.)
146 .LP
147 A process can initiate a trace by calling
148 .BR fork (2)
149 and having the resulting child do a
150 .BR PTRACE_TRACEME ,
151 followed (typically) by an
152 .BR execve (2).
153 Alternatively, one process may commence tracing another process using
154 .B PTRACE_ATTACH
155 or
156 .BR PTRACE_SEIZE .
157 .LP
158 While being traced, the tracee will stop each time a signal is delivered,
159 even if the signal is being ignored.
160 (An exception is
161 .BR SIGKILL ,
162 which has its usual effect.)
163 The tracer will be notified at its next call to
164 .BR waitpid (2)
165 (or one of the related "wait" system calls); that call will return a
166 .I status
167 value containing information that indicates
168 the cause of the stop in the tracee.
169 While the tracee is stopped,
170 the tracer can use various ptrace requests to inspect and modify the tracee.
171 The tracer then causes the tracee to continue,
172 optionally ignoring the delivered signal
173 (or even delivering a different signal instead).
174 .LP
175 If the
176 .B PTRACE_O_TRACEEXEC
177 option is not in effect, all successful calls to
178 .BR execve (2)
179 by the traced process will cause it to be sent a
180 .B SIGTRAP
181 signal,
182 giving the parent a chance to gain control before the new program
183 begins execution.
184 .LP
185 When the tracer is finished tracing, it can cause the tracee to continue
186 executing in a normal, untraced mode via
187 .BR PTRACE_DETACH .
188 .LP
189 The value of
190 .I request
191 determines the action to be performed:
192 .TP
193 .B PTRACE_TRACEME
194 Indicate that this process is to be traced by its parent.
195 A process probably shouldn't make this request if its parent
196 isn't expecting to trace it.
197 .RI ( pid ,
198 .IR addr ,
199 and
200 .IR data
201 are ignored.)
202 .IP
203 The
204 .B PTRACE_TRACEME
205 request is used only by the tracee;
206 the remaining requests are used only by the tracer.
207 In the following requests,
208 .I pid
209 specifies the thread ID of the tracee to be acted on.
210 For requests other than
211 .BR PTRACE_ATTACH ,
212 .BR PTRACE_SEIZE ,
213 .BR PTRACE_INTERRUPT ,
214 and
215 .BR PTRACE_KILL ,
216 the tracee must be stopped.
217 .TP
218 .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
219 Read a word at the address
220 .I addr
221 in the tracee's memory, returning the word as the result of the
222 .BR ptrace ()
223 call.
224 Linux does not have separate text and data address spaces,
225 so these two requests are currently equivalent.
226 .RI ( data
227 is ignored; but see NOTES.)
228 .TP
229 .B PTRACE_PEEKUSER
230 .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
231 .\" and that is the name that seems common on other systems.
232 Read a word at offset
233 .I addr
234 in the tracee's USER area,
235 which holds the registers and other information about the process
236 (see
237 .IR <sys/user.h> ).
238 The word is returned as the result of the
239 .BR ptrace ()
240 call.
241 Typically, the offset must be word-aligned, though this might vary by
242 architecture.
243 See NOTES.
244 .RI ( data
245 is ignored; but see NOTES.)
246 .TP
247 .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
248 Copy the word
249 .I data
250 to the address
251 .I addr
252 in the tracee's memory.
253 As for
254 .BR PTRACE_PEEKTEXT
255 and
256 .BR PTRACE_PEEKDATA ,
257 these two requests are currently equivalent.
258 .TP
259 .B PTRACE_POKEUSER
260 .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
261 .\" and that is the name that seems common on other systems.
262 Copy the word
263 .I data
264 to offset
265 .I addr
266 in the tracee's USER area.
267 As for
268 .BR PTRACE_PEEKUSER ,
269 the offset must typically be word-aligned.
270 In order to maintain the integrity of the kernel,
271 some modifications to the USER area are disallowed.
272 .\" FIXME In the preceding sentence, which modifications are disallowed,
273 .\" and when they are disallowed, how does user space discover that fact?
274 .TP
275 .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
276 Copy the tracee's general-purpose or floating-point registers,
277 respectively, to the address
278 .I data
279 in the tracer.
280 See
281 .I <sys/user.h>
282 for information on the format of this data.
283 .RI ( addr
284 is ignored.)
285 Note that SPARC systems have the meaning of
286 .I data
287 and
288 .I addr
289 reversed; that is,
290 .I data
291 is ignored and the registers are copied to the address
292 .IR addr .
293 .B PTRACE_GETREGS
294 and
295 .B PTRACE_GETFPREGS
296 are not present on all architectures.
297 .TP
298 .BR PTRACE_GETREGSET " (since Linux 2.6.34)"
299 Read the tracee's registers.
300 .I addr
301 specifies, in an architecture-dependent way, the type of registers to be read.
302 .B NT_PRSTATUS
303 (with numerical value 1)
304 usually results in reading of general-purpose registers.
305 If the CPU has, for example,
306 floating-point and/or vector registers, they can be retrieved by setting
307 .I addr
308 to the corresponding
309 .B NT_foo
310 constant.
311 .I data
312 points to a
313 .BR "struct iovec" ,
314 which describes the destination buffer's location and length.
315 On return, the kernel modifies
316 .B iov.len
317 to indicate the actual number of bytes returned.
318 .TP
319 .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
320 Modify the tracee's general-purpose or floating-point registers,
321 respectively, from the address
322 .I data
323 in the tracer.
324 As for
325 .BR PTRACE_POKEUSER ,
326 some general-purpose register modifications may be disallowed.
327 .\" FIXME . In the preceding sentence, which modifications are disallowed,
328 .\" and when they are disallowed, how does user space discover that fact?
329 .RI ( addr
330 is ignored.)
331 Note that SPARC systems have the meaning of
332 .I data
333 and
334 .I addr
335 reversed; that is,
336 .I data
337 is ignored and the registers are copied from the address
338 .IR addr .
339 .B PTRACE_SETREGS
340 and
341 .B PTRACE_SETFPREGS
342 are not present on all architectures.
343 .TP
344 .BR PTRACE_SETREGSET " (since Linux 2.6.34)"
345 Modify the tracee's registers.
346 The meaning of
347 .I addr
348 and
349 .I data
350 is analogous to
351 .BR PTRACE_GETREGSET .
352 .TP
353 .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
354 Retrieve information about the signal that caused the stop.
355 Copy a
356 .I siginfo_t
357 structure (see
358 .BR sigaction (2))
359 from the tracee to the address
360 .I data
361 in the tracer.
362 .RI ( addr
363 is ignored.)
364 .TP
365 .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
366 Set signal information:
367 copy a
368 .I siginfo_t
369 structure from the address
370 .I data
371 in the tracer to the tracee.
372 This will affect only signals that would normally be delivered to
373 the tracee and were caught by the tracer.
374 It may be difficult to tell
375 these normal signals from synthetic signals generated by
376 .BR ptrace ()
377 itself.
378 .RI ( addr
379 is ignored.)
380 .TP
381 .BR PTRACE_PEEKSIGINFO " (since Linux 3.10)"
382 .\" commit 84c751bd4aebbaae995fe32279d3dba48327bad4
383 Retrieve
384 .I siginfo_t
385 structures without removing signals from a queue.
386 .I addr
387 points to a
388 .I ptrace_peeksiginfo_args
389 structure that specifies the ordinal position from which
390 copying of signals should start,
391 and the number of signals to copy.
392 .I siginfo_t
393 structures are copied into the buffer pointed to by
394 .IR data .
395 The return value contains the number of copied signals (zero indicates
396 that there is no signal corresponding to the specified ordinal position).
397 Within the returned
398 .I siginfo
399 structures,
400 the
401 .IR si_code
402 field includes information
403 .RB ( __SI_CHLD ,
404 .BR __SI_FAULT ,
405 etc.) that are not otherwise exposed to user space.
406 .PP
407 .in +10n
408 .nf
409 struct ptrace_peeksiginfo_args {
410 u64 off; /* Ordinal position in queue at which
411 to start copying signals */
412 u32 flags; /* PTRACE_PEEKSIGINFO_SHARED or 0 */
413 s32 nr; /* Number of signals to copy */
414 };
415 .fi
416 .in
417 .IP
418 Currently, there is only one flag,
419 .BR PTRACE_PEEKSIGINFO_SHARED ,
420 for dumping signals from the process-wide signal queue.
421 If this flag is not set,
422 signals are read from the per-thread queue of the specified thread.
423 .in
424 .PP
425 .TP
426 .BR PTRACE_GETSIGMASK " (since Linux 3.11)"
427 .\" commit 29000caecbe87b6b66f144f72111f0d02fbbf0c1
428 Place a copy of the mask of blocked signals (see
429 .BR sigprocmask (2))
430 in the buffer pointed to by
431 .IR data ,
432 which should be a pointer to a buffer of type
433 .IR sigset_t .
434 The
435 .I addr
436 argument contains the size of the buffer pointed to by
437 .IR data
438 (i.e.,
439 .IR sizeof(sigset_t) ).
440 .TP
441 .BR PTRACE_SETSIGMASK " (since Linux 3.11)"
442 Change the mask of blocked signals (see
443 .BR sigprocmask (2))
444 to the value specified in the buffer pointed to by
445 .IR data ,
446 which should be a pointer to a buffer of type
447 .IR sigset_t .
448 The
449 .I addr
450 argument contains the size of the buffer pointed to by
451 .IR data
452 (i.e.,
453 .IR sizeof(sigset_t) ).
454 .TP
455 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
456 Set ptrace options from
457 .IR data .
458 .RI ( addr
459 is ignored.)
460 .IR data
461 is interpreted as a bit mask of options,
462 which are specified by the following flags:
463 .RS
464 .TP
465 .BR PTRACE_O_EXITKILL " (since Linux 3.8)"
466 .\" commit 992fb6e170639b0849bace8e49bf31bd37c4123
467 If a tracer sets this flag, a
468 .B SIGKILL
469 signal will be sent to every tracee if the tracer exits.
470 This option is useful for ptrace jailers that
471 want to ensure that tracees can never escape the tracer's control.
472 .TP
473 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
474 Stop the tracee at the next
475 .BR clone (2)
476 and automatically start tracing the newly cloned process,
477 which will start with a
478 .BR SIGSTOP ,
479 or
480 .B PTRACE_EVENT_STOP
481 if
482 .B PTRACE_SEIZE
483 was used.
484 A
485 .BR waitpid (2)
486 by the tracer will return a
487 .I status
488 value such that
489
490 .nf
491 status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
492 .fi
493
494 The PID of the new process can be retrieved with
495 .BR PTRACE_GETEVENTMSG .
496 .IP
497 This option may not catch
498 .BR clone (2)
499 calls in all cases.
500 If the tracee calls
501 .BR clone (2)
502 with the
503 .B CLONE_VFORK
504 flag,
505 .B PTRACE_EVENT_VFORK
506 will be delivered instead
507 if
508 .B PTRACE_O_TRACEVFORK
509 is set; otherwise if the tracee calls
510 .BR clone (2)
511 with the exit signal set to
512 .BR SIGCHLD ,
513 .B PTRACE_EVENT_FORK
514 will be delivered if
515 .B PTRACE_O_TRACEFORK
516 is set.
517 .TP
518 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
519 Stop the tracee at the next
520 .BR execve (2).
521 A
522 .BR waitpid (2)
523 by the tracer will return a
524 .I status
525 value such that
526
527 .nf
528 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
529 .fi
530
531 If the execing thread is not a thread group leader,
532 the thread ID is reset to thread group leader's ID before this stop.
533 Since Linux 3.0, the former thread ID can be retrieved with
534 .BR PTRACE_GETEVENTMSG .
535 .TP
536 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
537 Stop the tracee at exit.
538 A
539 .BR waitpid (2)
540 by the tracer will return a
541 .I status
542 value such that
543
544 .nf
545 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
546 .fi
547
548 The tracee's exit status can be retrieved with
549 .BR PTRACE_GETEVENTMSG .
550 .IP
551 The tracee is stopped early during process exit,
552 when registers are still available,
553 allowing the tracer to see where the exit occurred,
554 whereas the normal exit notification is done after the process
555 is finished exiting.
556 Even though context is available,
557 the tracer cannot prevent the exit from happening at this point.
558 .TP
559 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
560 Stop the tracee at the next
561 .BR fork (2)
562 and automatically start tracing the newly forked process,
563 which will start with a
564 .BR SIGSTOP ,
565 or
566 .B PTRACE_EVENT_STOP
567 if
568 .B PTRACE_SEIZE
569 was used.
570 A
571 .BR waitpid (2)
572 by the tracer will return a
573 .I status
574 value such that
575
576 .nf
577 status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
578 .fi
579
580 The PID of the new process can be retrieved with
581 .BR PTRACE_GETEVENTMSG .
582 .TP
583 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
584 When delivering system call traps, set bit 7 in the signal number
585 (i.e., deliver
586 .IR "SIGTRAP|0x80" ).
587 This makes it easy for the tracer to distinguish
588 normal traps from those caused by a system call.
589 .RB ( PTRACE_O_TRACESYSGOOD
590 may not work on all architectures.)
591 .TP
592 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
593 Stop the tracee at the next
594 .BR vfork (2)
595 and automatically start tracing the newly vforked process,
596 which will start with a
597 .BR SIGSTOP ,
598 or
599 .B PTRACE_EVENT_STOP
600 if
601 .B PTRACE_SEIZE
602 was used.
603 A
604 .BR waitpid (2)
605 by the tracer will return a
606 .I status
607 value such that
608
609 .nf
610 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
611 .fi
612
613 The PID of the new process can be retrieved with
614 .BR PTRACE_GETEVENTMSG .
615 .TP
616 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
617 Stop the tracee at the completion of the next
618 .BR vfork (2).
619 A
620 .BR waitpid (2)
621 by the tracer will return a
622 .I status
623 value such that
624
625 .nf
626 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
627 .fi
628
629 The PID of the new process can (since Linux 2.6.18) be retrieved with
630 .BR PTRACE_GETEVENTMSG .
631 .TP
632 .BR PTRACE_O_TRACESECCOMP " (since Linux 3.5)"
633 Stop the tracee when a
634 .BR seccomp (2)
635 .BR SECCOMP_RET_TRACE
636 rule is triggered.
637 A
638 .BR waitpid (2)
639 by the tracer will return a
640 .I status
641 value such that
642
643 .nf
644 status>>8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP<<8))
645 .fi
646
647 While this triggers a
648 .BR PTRACE_EVENT
649 stop, it is similar to a syscall-enter-stop.
650 For details, see the note on
651 .B PTRACE_EVENT_SECCOMP
652 below.
653 The seccomp event message data (from the
654 .BR SECCOMP_RET_DATA
655 portion of the seccomp filter rule) can be retrieved with
656 .BR PTRACE_GETEVENTMSG .
657 .TP
658 .BR PTRACE_O_SUSPEND_SECCOMP " (since Linux 4.3)"
659 .\" commit 13c4a90119d28cfcb6b5bdd820c233b86c2b0237
660 Suspend the tracee's seccomp protections.
661 This applies regardless of mode, and
662 can be used when the tracee has not yet installed seccomp filters.
663 That is, a valid use case is to suspend a tracee's seccomp protections
664 before they are installed by the tracee,
665 let the tracee install the filters,
666 and then clear this flag when the filters should be resumed.
667 Setting this option requires that the tracer have the
668 .BR CAP_SYS_ADMIN
669 capability,
670 not have any seccomp protections installed, and not have
671 .BR PTRACE_O_SUSPEND_SECCOMP
672 set on itself.
673 .RE
674 .TP
675 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
676 Retrieve a message (as an
677 .IR "unsigned long" )
678 about the ptrace event
679 that just happened, placing it at the address
680 .I data
681 in the tracer.
682 For
683 .BR PTRACE_EVENT_EXIT ,
684 this is the tracee's exit status.
685 For
686 .BR PTRACE_EVENT_FORK ,
687 .BR PTRACE_EVENT_VFORK ,
688 .BR PTRACE_EVENT_VFORK_DONE ,
689 and
690 .BR PTRACE_EVENT_CLONE ,
691 this is the PID of the new process.
692 For
693 .BR PTRACE_EVENT_SECCOMP ,
694 this is the
695 .BR seccomp (2)
696 filter's
697 .BR SECCOMP_RET_DATA
698 associated with the triggered rule.
699 .RI ( addr
700 is ignored.)
701 .TP
702 .B PTRACE_CONT
703 Restart the stopped tracee process.
704 If
705 .I data
706 is nonzero,
707 it is interpreted as the number of a signal to be delivered to the tracee;
708 otherwise, no signal is delivered.
709 Thus, for example, the tracer can control
710 whether a signal sent to the tracee is delivered or not.
711 .RI ( addr
712 is ignored.)
713 .TP
714 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
715 Restart the stopped tracee as for
716 .BR PTRACE_CONT ,
717 but arrange for the tracee to be stopped at
718 the next entry to or exit from a system call,
719 or after execution of a single instruction, respectively.
720 (The tracee will also, as usual, be stopped upon receipt of a signal.)
721 From the tracer's perspective, the tracee will appear to have been
722 stopped by receipt of a
723 .BR SIGTRAP .
724 So, for
725 .BR PTRACE_SYSCALL ,
726 for example, the idea is to inspect
727 the arguments to the system call at the first stop,
728 then do another
729 .B PTRACE_SYSCALL
730 and inspect the return value of the system call at the second stop.
731 The
732 .I data
733 argument is treated as for
734 .BR PTRACE_CONT .
735 .RI ( addr
736 is ignored.)
737 .TP
738 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
739 For
740 .BR PTRACE_SYSEMU ,
741 continue and stop on entry to the next system call,
742 which will not be executed.
743 See the documentation on syscall-stops below.
744 For
745 .BR PTRACE_SYSEMU_SINGLESTEP ,
746 do the same but also singlestep if not a system call.
747 This call is used by programs like
748 User Mode Linux that want to emulate all the tracee's system calls.
749 The
750 .I data
751 argument is treated as for
752 .BR PTRACE_CONT .
753 The
754 .I addr
755 argument is ignored.
756 These requests are currently
757 .\" As at 3.7
758 supported only on x86.
759 .TP
760 .BR PTRACE_LISTEN " (since Linux 3.4)"
761 Restart the stopped tracee, but prevent it from executing.
762 The resulting state of the tracee is similar to a process which
763 has been stopped by a
764 .B SIGSTOP
765 (or other stopping signal).
766 See the "group-stop" subsection for additional information.
767 .B PTRACE_LISTEN
768 works only on tracees attached by
769 .BR PTRACE_SEIZE .
770 .TP
771 .B PTRACE_KILL
772 Send the tracee a
773 .B SIGKILL
774 to terminate it.
775 .RI ( addr
776 and
777 .I data
778 are ignored.)
779 .IP
780 .I This operation is deprecated; do not use it!
781 Instead, send a
782 .BR SIGKILL
783 directly using
784 .BR kill (2)
785 or
786 .BR tgkill (2).
787 The problem with
788 .B PTRACE_KILL
789 is that it requires the tracee to be in signal-delivery-stop,
790 otherwise it may not work
791 (i.e., may complete successfully but won't kill the tracee).
792 By contrast, sending a
793 .B SIGKILL
794 directly has no such limitation.
795 .\" [Note from Denys Vlasenko:
796 .\" deprecation suggested by Oleg Nesterov. He prefers to deprecate it
797 .\" instead of describing (and needing to support) PTRACE_KILL's quirks.]
798 .TP
799 .BR PTRACE_INTERRUPT " (since Linux 3.4)"
800 Stop a tracee.
801 If the tracee is running or sleeping in kernel space and
802 .B PTRACE_SYSCALL
803 is in effect,
804 the system call is interrupted and syscall-exit-stop is reported.
805 (The interrupted system call is restarted when the tracee is restarted.)
806 If the tracee was already stopped by a signal and
807 .B PTRACE_LISTEN
808 was sent to it,
809 the tracee stops with
810 .B PTRACE_EVENT_STOP
811 and
812 .I WSTOPSIG(status)
813 returns the stop signal.
814 If any other ptrace-stop is generated at the same time (for example,
815 if a signal is sent to the tracee), this ptrace-stop happens.
816 If none of the above applies (for example, if the tracee is running in user
817 space), it stops with
818 .B PTRACE_EVENT_STOP
819 with
820 .I WSTOPSIG(status)
821 ==
822 .BR SIGTRAP .
823 .B PTRACE_INTERRUPT
824 only works on tracees attached by
825 .BR PTRACE_SEIZE .
826 .TP
827 .B PTRACE_ATTACH
828 Attach to the process specified in
829 .IR pid ,
830 making it a tracee of the calling process.
831 .\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
832 .\" "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
833 .\" Basically, it's not true for any Linux in practical use.
834 .\" ; the behavior of the tracee is as if it had done a
835 .\" .BR PTRACE_TRACEME .
836 .\" The calling process actually becomes the parent of the tracee
837 .\" process for most purposes (e.g., it will receive
838 .\" notification of tracee events and appears in
839 .\" .BR ps (1)
840 .\" output as the tracee's parent), but a
841 .\" .BR getppid (2)
842 .\" by the tracee will still return the PID of the original parent.
843 The tracee is sent a
844 .BR SIGSTOP ,
845 but will not necessarily have stopped
846 by the completion of this call; use
847 .BR waitpid (2)
848 to wait for the tracee to stop.
849 See the "Attaching and detaching" subsection for additional information.
850 .RI ( addr
851 and
852 .I data
853 are ignored.)
854
855 Permission to perform a
856 .BR PTRACE_ATTACH
857 is governed by a ptrace access mode
858 .B PTRACE_MODE_ATTACH_REALCREDS
859 check; see below.
860 .TP
861 .BR PTRACE_SEIZE " (since Linux 3.4)"
862 .\"
863 .\" Noted by Dmitry Levin:
864 .\"
865 .\" PTRACE_SEIZE was introduced by commit v3.1-rc1~308^2~28, but
866 .\" it had to be used along with a temporary flag PTRACE_SEIZE_DEVEL,
867 .\" which was removed later by commit v3.4-rc1~109^2~20.
868 .\"
869 .\" That is, [before] v3.4 we had a test mode of PTRACE_SEIZE API,
870 .\" which was not compatible with the current PTRACE_SEIZE API introduced
871 .\" in Linux 3.4.
872 .\"
873 Attach to the process specified in
874 .IR pid ,
875 making it a tracee of the calling process.
876 Unlike
877 .BR PTRACE_ATTACH ,
878 .B PTRACE_SEIZE
879 does not stop the process.
880 Group-stops are reported as
881 .B PTRACE_EVENT_STOP
882 and
883 .I WSTOPSIG(status)
884 returns the stop signal.
885 Automatically attached children stop with
886 .B PTRACE_EVENT_STOP
887 and
888 .I WSTOPSIG(status)
889 returns
890 .B SIGTRAP
891 instead of having
892 .B SIGSTOP
893 signal delivered to them.
894 .BR execve (2)
895 does not deliver an extra
896 .BR SIGTRAP .
897 Only a
898 .BR PTRACE_SEIZE d
899 process can accept
900 .B PTRACE_INTERRUPT
901 and
902 .B PTRACE_LISTEN
903 commands.
904 The "seized" behavior just described is inherited by
905 children that are automatically attached using
906 .BR PTRACE_O_TRACEFORK ,
907 .BR PTRACE_O_TRACEVFORK ,
908 and
909 .BR PTRACE_O_TRACECLONE .
910 .I addr
911 must be zero.
912 .I data
913 contains a bit mask of ptrace options to activate immediately.
914
915 Permission to perform a
916 .BR PTRACE_SEIZE
917 is governed by a ptrace access mode
918 .B PTRACE_MODE_ATTACH_REALCREDS
919 check; see below.
920 .\"
921 .TP
922 .BR PTRACE_SECCOMP_GET_FILTER " (since Linux 4.4)"
923 .\" commit f8e529ed941ba2bbcbf310b575d968159ce7e895
924 This operation allows the tracer to dump the tracee's
925 classic BPF filters.
926
927 .I addr
928 is an integer specifying the index of the filter to be dumped.
929 The most recently installed filter has the index 0.
930 If
931 .I addr
932 is greater than the number of installed filters,
933 the operation fails with the error
934 .BR ENOENT .
935
936 .I data
937 is either a pointer to a
938 .IR "struct sock_filter"
939 array that is large enough to store the BPF program,
940 or NULL if the program is not to be stored.
941
942 Upon success,
943 the return value is the number of instructions in the BPF program.
944 If
945 .I data
946 was NULL, then this return value can be used to correctly size the
947 .IR "struct sock_filter"
948 array passed in a subsequent call.
949
950 This operation fails with the error
951 .B EACCESS
952 if the caller does not have the
953 .B CAP_SYS_ADMIN
954 capability or if the caller is in strict or filter seccomp mode.
955 If the filter referred to by
956 .I addr
957 is not a classic BPF filter, the operation fails with the error
958 .BR EMEDIUMTYPE .
959
960 This operation is available if the kernel was configured with both the
961 .B CONFIG_SECCOMP_FILTER
962 and the
963 .B CONFIG_CHECKPOINT_RESTORE
964 options.
965 .TP
966 .B PTRACE_DETACH
967 Restart the stopped tracee as for
968 .BR PTRACE_CONT ,
969 but first detach from it.
970 Under Linux, a tracee can be detached in this way regardless
971 of which method was used to initiate tracing.
972 .RI ( addr
973 is ignored.)
974 .\"
975 .TP
976 .BR PTRACE_GET_THREAD_AREA " (since Linux 2.6.0)"
977 This operation performs a similar task to
978 .BR get_thread_area (2).
979 It reads the TLS entry in the GDT whose index is given in
980 .IR addr ,
981 placing a copy of the entry into the
982 .IR "struct user_desc"
983 pointed to by
984 .IR data .
985 (By contrast with
986 .BR get_thread_area (2),
987 the
988 .I entry_number
989 of the
990 .IR "struct user_desc"
991 is ignored.)
992 .TP
993 .BR PTRACE_SET_THREAD_AREA " (since Linux 2.6.0)"
994 This operation performs a similar task to
995 .BR set_thread_area (2).
996 It sets the TLS entry in the GDT whose index is given in
997 .IR addr ,
998 assigning it the data supplied in the
999 .IR "struct user_desc"
1000 pointed to by
1001 .IR data .
1002 (By contrast with
1003 .BR set_thread_area (2),
1004 the
1005 .I entry_number
1006 of the
1007 .IR "struct user_desc"
1008 is ignored; in other words,
1009 this ptrace operation can't be used to allocate a free TLS entry.)
1010 .\"
1011 .SS Death under ptrace
1012 When a (possibly multithreaded) process receives a killing signal
1013 (one whose disposition is set to
1014 .B SIG_DFL
1015 and whose default action is to kill the process),
1016 all threads exit.
1017 Tracees report their death to their tracer(s).
1018 Notification of this event is delivered via
1019 .BR waitpid (2).
1020 .LP
1021 Note that the killing signal will first cause signal-delivery-stop
1022 (on one tracee only),
1023 and only after it is injected by the tracer
1024 (or after it was dispatched to a thread which isn't traced),
1025 will death from the signal happen on
1026 .I all
1027 tracees within a multithreaded process.
1028 (The term "signal-delivery-stop" is explained below.)
1029 .LP
1030 .B SIGKILL
1031 does not generate signal-delivery-stop and
1032 therefore the tracer can't suppress it.
1033 .B SIGKILL
1034 kills even within system calls
1035 (syscall-exit-stop is not generated prior to death by
1036 .BR SIGKILL ).
1037 The net effect is that
1038 .B SIGKILL
1039 always kills the process (all its threads),
1040 even if some threads of the process are ptraced.
1041 .LP
1042 When the tracee calls
1043 .BR _exit (2),
1044 it reports its death to its tracer.
1045 Other threads are not affected.
1046 .LP
1047 When any thread executes
1048 .BR exit_group (2),
1049 every tracee in its thread group reports its death to its tracer.
1050 .LP
1051 If the
1052 .B PTRACE_O_TRACEEXIT
1053 option is on,
1054 .B PTRACE_EVENT_EXIT
1055 will happen before actual death.
1056 This applies to exits via
1057 .BR exit (2),
1058 .BR exit_group (2),
1059 and signal deaths (except
1060 .BR SIGKILL ,
1061 depending on the kernel version; see BUGS below),
1062 and when threads are torn down on
1063 .BR execve (2)
1064 in a multithreaded process.
1065 .LP
1066 The tracer cannot assume that the ptrace-stopped tracee exists.
1067 There are many scenarios when the tracee may die while stopped (such as
1068 .BR SIGKILL ).
1069 Therefore, the tracer must be prepared to handle an
1070 .B ESRCH
1071 error on any ptrace operation.
1072 Unfortunately, the same error is returned if the tracee
1073 exists but is not ptrace-stopped
1074 (for commands which require a stopped tracee),
1075 or if it is not traced by the process which issued the ptrace call.
1076 The tracer needs to keep track of the stopped/running state of the tracee,
1077 and interpret
1078 .B ESRCH
1079 as "tracee died unexpectedly" only if it knows that the tracee has
1080 been observed to enter ptrace-stop.
1081 Note that there is no guarantee that
1082 .I waitpid(WNOHANG)
1083 will reliably report the tracee's death status if a
1084 ptrace operation returned
1085 .BR ESRCH .
1086 .I waitpid(WNOHANG)
1087 may return 0 instead.
1088 In other words, the tracee may be "not yet fully dead",
1089 but already refusing ptrace requests.
1090 .LP
1091 The tracer can't assume that the tracee
1092 .I always
1093 ends its life by reporting
1094 .I WIFEXITED(status)
1095 or
1096 .IR WIFSIGNALED(status) ;
1097 there are cases where this does not occur.
1098 For example, if a thread other than thread group leader does an
1099 .BR execve (2),
1100 it disappears;
1101 its PID will never be seen again,
1102 and any subsequent ptrace stops will be reported under
1103 the thread group leader's PID.
1104 .SS Stopped states
1105 A tracee can be in two states: running or stopped.
1106 For the purposes of ptrace, a tracee which is blocked in a system call
1107 (such as
1108 .BR read (2),
1109 .BR pause (2),
1110 etc.)
1111 is nevertheless considered to be running, even if the tracee is blocked
1112 for a long time.
1113 The state of the tracee after
1114 .BR PTRACE_LISTEN
1115 is somewhat of a gray area: it is not in any ptrace-stop (ptrace commands
1116 won't work on it, and it will deliver
1117 .BR waitpid (2)
1118 notifications),
1119 but it also may be considered "stopped" because
1120 it is not executing instructions (is not scheduled), and if it was
1121 in group-stop before
1122 .BR PTRACE_LISTEN ,
1123 it will not respond to signals until
1124 .B SIGCONT
1125 is received.
1126 .LP
1127 There are many kinds of states when the tracee is stopped, and in ptrace
1128 discussions they are often conflated.
1129 Therefore, it is important to use precise terms.
1130 .LP
1131 In this manual page, any stopped state in which the tracee is ready
1132 to accept ptrace commands from the tracer is called
1133 .IR ptrace-stop .
1134 Ptrace-stops can
1135 be further subdivided into
1136 .IR signal-delivery-stop ,
1137 .IR group-stop ,
1138 .IR syscall-stop ,
1139 .IR PTRACE_EVENT stops,
1140 and so on.
1141 These stopped states are described in detail below.
1142 .LP
1143 When the running tracee enters ptrace-stop, it notifies its tracer using
1144 .BR waitpid (2)
1145 (or one of the other "wait" system calls).
1146 Most of this manual page assumes that the tracer waits with:
1147 .LP
1148 pid = waitpid(pid_or_minus_1, &status, __WALL);
1149 .LP
1150 Ptrace-stopped tracees are reported as returns with
1151 .I pid
1152 greater than 0 and
1153 .I WIFSTOPPED(status)
1154 true.
1155 .\" Denys Vlasenko:
1156 .\" Do we require __WALL usage, or will just using 0 be ok? (With 0,
1157 .\" I am not 100% sure there aren't ugly corner cases.) Are the
1158 .\" rules different if user wants to use waitid? Will waitid require
1159 .\" WEXITED?
1160 .\"
1161 .LP
1162 The
1163 .B __WALL
1164 flag does not include the
1165 .B WSTOPPED
1166 and
1167 .B WEXITED
1168 flags, but implies their functionality.
1169 .LP
1170 Setting the
1171 .B WCONTINUED
1172 flag when calling
1173 .BR waitpid (2)
1174 is not recommended: the "continued" state is per-process and
1175 consuming it can confuse the real parent of the tracee.
1176 .LP
1177 Use of the
1178 .B WNOHANG
1179 flag may cause
1180 .BR waitpid (2)
1181 to return 0 ("no wait results available yet")
1182 even if the tracer knows there should be a notification.
1183 Example:
1184 .nf
1185
1186 errno = 0;
1187 ptrace(PTRACE_CONT, pid, 0L, 0L);
1188 if (errno == ESRCH) {
1189 /* tracee is dead */
1190 r = waitpid(tracee, &status, __WALL | WNOHANG);
1191 /* r can still be 0 here! */
1192 }
1193 .fi
1194 .\" FIXME .
1195 .\" waitid usage? WNOWAIT?
1196 .\" describe how wait notifications queue (or not queue)
1197 .LP
1198 The following kinds of ptrace-stops exist: signal-delivery-stops,
1199 group-stops,
1200 .B PTRACE_EVENT
1201 stops, syscall-stops.
1202 They all are reported by
1203 .BR waitpid (2)
1204 with
1205 .I WIFSTOPPED(status)
1206 true.
1207 They may be differentiated by examining the value
1208 .IR status>>8 ,
1209 and if there is ambiguity in that value, by querying
1210 .BR PTRACE_GETSIGINFO .
1211 (Note: the
1212 .I WSTOPSIG(status)
1213 macro can't be used to perform this examination,
1214 because it returns the value
1215 .IR "(status>>8)\ &\ 0xff" .)
1216 .SS Signal-delivery-stop
1217 When a (possibly multithreaded) process receives any signal except
1218 .BR SIGKILL ,
1219 the kernel selects an arbitrary thread which handles the signal.
1220 (If the signal is generated with
1221 .BR tgkill (2),
1222 the target thread can be explicitly selected by the caller.)
1223 If the selected thread is traced, it enters signal-delivery-stop.
1224 At this point, the signal is not yet delivered to the process,
1225 and can be suppressed by the tracer.
1226 If the tracer doesn't suppress the signal,
1227 it passes the signal to the tracee in the next ptrace restart request.
1228 This second step of signal delivery is called
1229 .I "signal injection"
1230 in this manual page.
1231 Note that if the signal is blocked,
1232 signal-delivery-stop doesn't happen until the signal is unblocked,
1233 with the usual exception that
1234 .B SIGSTOP
1235 can't be blocked.
1236 .LP
1237 Signal-delivery-stop is observed by the tracer as
1238 .BR waitpid (2)
1239 returning with
1240 .I WIFSTOPPED(status)
1241 true, with the signal returned by
1242 .IR WSTOPSIG(status) .
1243 If the signal is
1244 .BR SIGTRAP ,
1245 this may be a different kind of ptrace-stop;
1246 see the "Syscall-stops" and "execve" sections below for details.
1247 If
1248 .I WSTOPSIG(status)
1249 returns a stopping signal, this may be a group-stop; see below.
1250 .SS Signal injection and suppression
1251 After signal-delivery-stop is observed by the tracer,
1252 the tracer should restart the tracee with the call
1253 .LP
1254 ptrace(PTRACE_restart, pid, 0, sig)
1255 .LP
1256 where
1257 .B PTRACE_restart
1258 is one of the restarting ptrace requests.
1259 If
1260 .I sig
1261 is 0, then a signal is not delivered.
1262 Otherwise, the signal
1263 .I sig
1264 is delivered.
1265 This operation is called
1266 .I "signal injection"
1267 in this manual page, to distinguish it from signal-delivery-stop.
1268 .LP
1269 The
1270 .I sig
1271 value may be different from the
1272 .I WSTOPSIG(status)
1273 value: the tracer can cause a different signal to be injected.
1274 .LP
1275 Note that a suppressed signal still causes system calls to return
1276 prematurely.
1277 In this case, system calls will be restarted: the tracer will
1278 observe the tracee to reexecute the interrupted system call (or
1279 .BR restart_syscall (2)
1280 system call for a few system calls which use a different mechanism
1281 for restarting) if the tracer uses
1282 .BR PTRACE_SYSCALL .
1283 Even system calls (such as
1284 .BR poll (2))
1285 which are not restartable after signal are restarted after
1286 signal is suppressed;
1287 however, kernel bugs exist which cause some system calls to fail with
1288 .B EINTR
1289 even though no observable signal is injected to the tracee.
1290 .LP
1291 Restarting ptrace commands issued in ptrace-stops other than
1292 signal-delivery-stop are not guaranteed to inject a signal, even if
1293 .I sig
1294 is nonzero.
1295 No error is reported; a nonzero
1296 .I sig
1297 may simply be ignored.
1298 Ptrace users should not try to "create a new signal" this way: use
1299 .BR tgkill (2)
1300 instead.
1301 .LP
1302 The fact that signal injection requests may be ignored
1303 when restarting the tracee after
1304 ptrace stops that are not signal-delivery-stops
1305 is a cause of confusion among ptrace users.
1306 One typical scenario is that the tracer observes group-stop,
1307 mistakes it for signal-delivery-stop, restarts the tracee with
1308
1309 ptrace(PTRACE_restart, pid, 0, stopsig)
1310
1311 with the intention of injecting
1312 .IR stopsig ,
1313 but
1314 .I stopsig
1315 gets ignored and the tracee continues to run.
1316 .LP
1317 The
1318 .B SIGCONT
1319 signal has a side effect of waking up (all threads of)
1320 a group-stopped process.
1321 This side effect happens before signal-delivery-stop.
1322 The tracer can't suppress this side effect (it can
1323 only suppress signal injection, which only causes the
1324 .BR SIGCONT
1325 handler to not be executed in the tracee, if such a handler is installed).
1326 In fact, waking up from group-stop may be followed by
1327 signal-delivery-stop for signal(s)
1328 .I other than
1329 .BR SIGCONT ,
1330 if they were pending when
1331 .B SIGCONT
1332 was delivered.
1333 In other words,
1334 .B SIGCONT
1335 may be not the first signal observed by the tracee after it was sent.
1336 .LP
1337 Stopping signals cause (all threads of) a process to enter group-stop.
1338 This side effect happens after signal injection, and therefore can be
1339 suppressed by the tracer.
1340 .LP
1341 In Linux 2.4 and earlier, the
1342 .B SIGSTOP
1343 signal can't be injected.
1344 .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
1345 .\" there is:
1346 .\"
1347 .\" /* The debugger continued. Ignore SIGSTOP. */
1348 .\" if (signr == SIGSTOP)
1349 .\" continue;
1350 .LP
1351 .B PTRACE_GETSIGINFO
1352 can be used to retrieve a
1353 .I siginfo_t
1354 structure which corresponds to the delivered signal.
1355 .B PTRACE_SETSIGINFO
1356 may be used to modify it.
1357 If
1358 .B PTRACE_SETSIGINFO
1359 has been used to alter
1360 .IR siginfo_t ,
1361 the
1362 .I si_signo
1363 field and the
1364 .I sig
1365 parameter in the restarting command must match,
1366 otherwise the result is undefined.
1367 .SS Group-stop
1368 When a (possibly multithreaded) process receives a stopping signal,
1369 all threads stop.
1370 If some threads are traced, they enter a group-stop.
1371 Note that the stopping signal will first cause signal-delivery-stop
1372 (on one tracee only), and only after it is injected by the tracer
1373 (or after it was dispatched to a thread which isn't traced),
1374 will group-stop be initiated on
1375 .I all
1376 tracees within the multithreaded process.
1377 As usual, every tracee reports its group-stop separately
1378 to the corresponding tracer.
1379 .LP
1380 Group-stop is observed by the tracer as
1381 .BR waitpid (2)
1382 returning with
1383 .I WIFSTOPPED(status)
1384 true, with the stopping signal available via
1385 .IR WSTOPSIG(status) .
1386 The same result is returned by some other classes of ptrace-stops,
1387 therefore the recommended practice is to perform the call
1388 .LP
1389 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
1390 .LP
1391 The call can be avoided if the signal is not
1392 .BR SIGSTOP ,
1393 .BR SIGTSTP ,
1394 .BR SIGTTIN ,
1395 or
1396 .BR SIGTTOU ;
1397 only these four signals are stopping signals.
1398 If the tracer sees something else, it can't be a group-stop.
1399 Otherwise, the tracer needs to call
1400 .BR PTRACE_GETSIGINFO .
1401 If
1402 .B PTRACE_GETSIGINFO
1403 fails with
1404 .BR EINVAL ,
1405 then it is definitely a group-stop.
1406 (Other failure codes are possible, such as
1407 .B ESRCH
1408 ("no such process") if a
1409 .B SIGKILL
1410 killed the tracee.)
1411 .LP
1412 If tracee was attached using
1413 .BR PTRACE_SEIZE ,
1414 group-stop is indicated by
1415 .BR PTRACE_EVENT_STOP :
1416 .IR "status>>16 == PTRACE_EVENT_STOP" .
1417 This allows detection of group-stops
1418 without requiring an extra
1419 .B PTRACE_GETSIGINFO
1420 call.
1421 .LP
1422 As of Linux 2.6.38,
1423 after the tracer sees the tracee ptrace-stop and until it
1424 restarts or kills it, the tracee will not run,
1425 and will not send notifications (except
1426 .B SIGKILL
1427 death) to the tracer, even if the tracer enters into another
1428 .BR waitpid (2)
1429 call.
1430 .LP
1431 The kernel behavior described in the previous paragraph
1432 causes a problem with transparent handling of stopping signals.
1433 If the tracer restarts the tracee after group-stop,
1434 the stopping signal
1435 is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
1436 If the tracer doesn't restart the tracee before entering into the next
1437 .BR waitpid (2),
1438 future
1439 .B SIGCONT
1440 signals will not be reported to the tracer;
1441 this would cause the
1442 .B SIGCONT
1443 signals to have no effect on the tracee.
1444 .LP
1445 Since Linux 3.4, there is a method to overcome this problem: instead of
1446 .BR PTRACE_CONT ,
1447 a
1448 .B PTRACE_LISTEN
1449 command can be used to restart a tracee in a way where it does not execute,
1450 but waits for a new event which it can report via
1451 .BR waitpid (2)
1452 (such as when
1453 it is restarted by a
1454 .BR SIGCONT ).
1455 .SS PTRACE_EVENT stops
1456 If the tracer sets
1457 .B PTRACE_O_TRACE_*
1458 options, the tracee will enter ptrace-stops called
1459 .B PTRACE_EVENT
1460 stops.
1461 .LP
1462 .B PTRACE_EVENT
1463 stops are observed by the tracer as
1464 .BR waitpid (2)
1465 returning with
1466 .IR WIFSTOPPED(status) ,
1467 and
1468 .I WSTOPSIG(status)
1469 returns
1470 .BR SIGTRAP .
1471 An additional bit is set in the higher byte of the status word:
1472 the value
1473 .I status>>8
1474 will be
1475
1476 (SIGTRAP | PTRACE_EVENT_foo << 8).
1477
1478 The following events exist:
1479 .TP
1480 .B PTRACE_EVENT_VFORK
1481 Stop before return from
1482 .BR vfork (2)
1483 or
1484 .BR clone (2)
1485 with the
1486 .B CLONE_VFORK
1487 flag.
1488 When the tracee is continued after this stop, it will wait for child to
1489 exit/exec before continuing its execution
1490 (in other words, the usual behavior on
1491 .BR vfork (2)).
1492 .TP
1493 .B PTRACE_EVENT_FORK
1494 Stop before return from
1495 .BR fork (2)
1496 or
1497 .BR clone (2)
1498 with the exit signal set to
1499 .BR SIGCHLD .
1500 .TP
1501 .B PTRACE_EVENT_CLONE
1502 Stop before return from
1503 .BR clone (2).
1504 .TP
1505 .B PTRACE_EVENT_VFORK_DONE
1506 Stop before return from
1507 .BR vfork (2)
1508 or
1509 .BR clone (2)
1510 with the
1511 .B CLONE_VFORK
1512 flag,
1513 but after the child unblocked this tracee by exiting or execing.
1514 .LP
1515 For all four stops described above,
1516 the stop occurs in the parent (i.e., the tracee),
1517 not in the newly created thread.
1518 .BR PTRACE_GETEVENTMSG
1519 can be used to retrieve the new thread's ID.
1520 .TP
1521 .B PTRACE_EVENT_EXEC
1522 Stop before return from
1523 .BR execve (2).
1524 Since Linux 3.0,
1525 .BR PTRACE_GETEVENTMSG
1526 returns the former thread ID.
1527 .TP
1528 .B PTRACE_EVENT_EXIT
1529 Stop before exit (including death from
1530 .BR exit_group (2)),
1531 signal death, or exit caused by
1532 .BR execve (2)
1533 in a multithreaded process.
1534 .B PTRACE_GETEVENTMSG
1535 returns the exit status.
1536 Registers can be examined
1537 (unlike when "real" exit happens).
1538 The tracee is still alive; it needs to be
1539 .BR PTRACE_CONT ed
1540 or
1541 .BR PTRACE_DETACH ed
1542 to finish exiting.
1543 .TP
1544 .B PTRACE_EVENT_STOP
1545 Stop induced by
1546 .B PTRACE_INTERRUPT
1547 command, or group-stop, or initial ptrace-stop when a new child is attached
1548 (only if attached using
1549 .BR PTRACE_SEIZE ).
1550 .TP
1551 .B PTRACE_EVENT_SECCOMP
1552 Stop triggered by a
1553 .BR seccomp (2)
1554 rule on tracee syscall entry when
1555 .BR PTRACE_O_TRACESECCOMP
1556 has been set by the tracer.
1557 The seccomp event message data (from the
1558 .BR SECCOMP_RET_DATA
1559 portion of the seccomp filter rule) can be retrieved with
1560 .BR PTRACE_GETEVENTMSG .
1561 The semantics of this stop are described in
1562 detail in a separate section below.
1563 .LP
1564 .B PTRACE_GETSIGINFO
1565 on
1566 .B PTRACE_EVENT
1567 stops returns
1568 .B SIGTRAP
1569 in
1570 .IR si_signo ,
1571 with
1572 .I si_code
1573 set to
1574 .IR "(event<<8)\ |\ SIGTRAP" .
1575 .SS Syscall-stops
1576 If the tracee was restarted by
1577 .BR PTRACE_SYSCALL
1578 or
1579 .BR PTRACE_SYSEMU ,
1580 the tracee enters
1581 syscall-enter-stop just prior to entering any system call (which
1582 will not be executed if the restart was using
1583 .BR PTRACE_SYSEMU ,
1584 regardless of any change made to registers at this point or how the
1585 tracee is restarted after this stop).
1586 No matter which method caused the syscall-entry-stop,
1587 if the tracer restarts the tracee with
1588 .BR PTRACE_SYSCALL ,
1589 the tracee enters syscall-exit-stop when the system call is finished,
1590 or if it is interrupted by a signal.
1591 (That is, signal-delivery-stop never happens between syscall-enter-stop
1592 and syscall-exit-stop; it happens
1593 .I after
1594 syscall-exit-stop.).
1595 If the tracee is continued using any other method (including
1596 .BR PTRACE_SYSEMU ),
1597 no syscall-exit-stop occurs.
1598 Note that all mentions
1599 .BR PTRACE_SYSEMU
1600 apply equally to
1601 .BR PTRACE_SYSEMU_SINGLESTEP.
1602 .LP
1603 However, even if the tracee was continued using
1604 .BR PTRACE_SYSCALL
1605 , it is not guaranteed that the next stop will be a syscall-exit-stop.
1606 Other possibilities are that the tracee may stop in a
1607 .B PTRACE_EVENT
1608 stop (including seccomp stops), exit (if it entered
1609 .BR _exit (2)
1610 or
1611 .BR exit_group (2)),
1612 be killed by
1613 .BR SIGKILL ,
1614 or die silently (if it is a thread group leader, the
1615 .BR execve (2)
1616 happened in another thread,
1617 and that thread is not traced by the same tracer;
1618 this situation is discussed later).
1619 .LP
1620 Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1621 .BR waitpid (2)
1622 returning with
1623 .I WIFSTOPPED(status)
1624 true, and
1625 .I WSTOPSIG(status)
1626 giving
1627 .BR SIGTRAP .
1628 If the
1629 .B PTRACE_O_TRACESYSGOOD
1630 option was set by the tracer, then
1631 .I WSTOPSIG(status)
1632 will give the value
1633 .IR "(SIGTRAP\ |\ 0x80)" .
1634 .LP
1635 Syscall-stops can be distinguished from signal-delivery-stop with
1636 .B SIGTRAP
1637 by querying
1638 .BR PTRACE_GETSIGINFO
1639 for the following cases:
1640 .TP
1641 .IR si_code " <= 0"
1642 .B SIGTRAP
1643 was delivered as a result of a user-space action,
1644 for example, a system call
1645 .RB ( tgkill (2),
1646 .BR kill (2),
1647 .BR sigqueue (3),
1648 etc.),
1649 expiration of a POSIX timer,
1650 change of state on a POSIX message queue,
1651 or completion of an asynchronous I/O request.
1652 .TP
1653 .IR si_code " == SI_KERNEL (0x80)"
1654 .B SIGTRAP
1655 was sent by the kernel.
1656 .TP
1657 .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1658 This is a syscall-stop.
1659 .LP
1660 However, syscall-stops happen very often (twice per system call),
1661 and performing
1662 .B PTRACE_GETSIGINFO
1663 for every syscall-stop may be somewhat expensive.
1664 .LP
1665 Some architectures allow the cases to be distinguished
1666 by examining registers.
1667 For example, on x86,
1668 .I rax
1669 ==
1670 .RB - ENOSYS
1671 in syscall-enter-stop.
1672 Since
1673 .B SIGTRAP
1674 (like any other signal) always happens
1675 .I after
1676 syscall-exit-stop,
1677 and at this point
1678 .I rax
1679 almost never contains
1680 .RB - ENOSYS ,
1681 the
1682 .B SIGTRAP
1683 looks like "syscall-stop which is not syscall-enter-stop";
1684 in other words, it looks like a
1685 "stray syscall-exit-stop" and can be detected this way.
1686 But such detection is fragile and is best avoided.
1687 .LP
1688 Using the
1689 .B PTRACE_O_TRACESYSGOOD
1690 option is the recommended method to distinguish syscall-stops
1691 from other kinds of ptrace-stops,
1692 since it is reliable and does not incur a performance penalty.
1693 .LP
1694 Syscall-enter-stop and syscall-exit-stop are
1695 indistinguishable from each other by the tracer.
1696 The tracer needs to keep track of the sequence of
1697 ptrace-stops in order to not misinterpret syscall-enter-stop as
1698 syscall-exit-stop or vice versa.
1699 In general, a syscall-enter-stop is
1700 always followed by syscall-exit-stop,
1701 .B PTRACE_EVENT
1702 stop, or the tracee's death;
1703 no other kinds of ptrace-stop can occur in between.
1704 However, note that seccomp stops (see below) can cause syscall-exit-stops,
1705 without preceding syscall-entry-stops.
1706 If seccomp is in use, care needs
1707 to be taken not to misinterpret such stops as syscall-entry-stops.
1708 .LP
1709 If after syscall-enter-stop,
1710 the tracer uses a restarting command other than
1711 .BR PTRACE_SYSCALL ,
1712 syscall-exit-stop is not generated.
1713 .LP
1714 .B PTRACE_GETSIGINFO
1715 on syscall-stops returns
1716 .B SIGTRAP
1717 in
1718 .IR si_signo ,
1719 with
1720 .I si_code
1721 set to
1722 .B SIGTRAP
1723 or
1724 .IR (SIGTRAP|0x80) .
1725 .\"
1726 .SS PTRACE_EVENT_SECCOMP stops (Linux 3.5 to 4.7)
1727 The behavior of
1728 .BR PTRACE_EVENT_SECCOMP
1729 stops and their interaction with other kinds
1730 of ptrace stops has changed between kernel versions.
1731 This documents the behavior
1732 from their introduction until Linux 4.7 (inclusive).
1733 The behavior in later kernel versions is documented in the next section.
1734
1735 A
1736 .BR PTRACE_EVENT_SECCOMP
1737 stop occurs whenever a
1738 .BR SECCOMP_RET_TRACE
1739 rule is triggered.
1740 This is independent of which methods was used to restart the system call.
1741 Notably, seccomp still runs even if the tracee was restarted using
1742 .BR PTRACE_SYSEMU
1743 and this system call is unconditionally skipped.
1744
1745 Restarts from this stop will behave as if the stop had occurred right
1746 before the system call in question.
1747 In particular, both
1748 .BR PTRACE_SYSCALL
1749 and
1750 .BR PTRACE_SYSEMU
1751 will normally cause a subsequent syscall-entry-stop.
1752 However, if after the
1753 .BR PTRACE_EVENT_SECCOMP
1754 the system call number is negative,
1755 both the syscall-entry-stop and the system call itself will be skipped.
1756 This means that if the system call number is negative after a
1757 .BR PTRACE_EVENT_SECCOMP
1758 and the tracee is restarted using
1759 .BR PTRACE_SYSCALL,
1760 the next observed stop will be a syscall-exit-stop,
1761 rather than the syscall-entry-stop that might have been expected.
1762 .\"
1763 .SS PTRACE_EVENT_SECCOMP stops (since Linux 4.8)
1764 Starting with Linux 4.8,
1765 .\" commit 93e35efb8de45393cf61ed07f7b407629bf698ea
1766 the
1767 .BR PTRACE_EVENT_SECCOMP
1768 stop was reordered to occur between syscall-entry-stop and
1769 syscall-exit-stop.
1770 Note that seccomp no longer runs (and no
1771 .B PTRACE_EVENT_SECCOMP
1772 will be reported) if the system call is skipped due to
1773 .BR PTRACE_SYSEMU .
1774
1775 Functionally, a
1776 .B PTRACE_EVENT_SECCOMP
1777 stop functions comparably
1778 to a syscall-entry-stop (i.e., continuations using
1779 .BR PTRACE_SYSCALL
1780 will cause syscall-exit-stops,
1781 the system call number may be changed and any other modified registers
1782 are visible to the to-be-executed system call as well).
1783 Note that there may be,
1784 but need not have been a preceding syscall-entry-stop.
1785
1786 After a
1787 .BR PTRACE_EVENT_SECCOMP
1788 stop, seccomp will be rerun, with a
1789 .BR SECCOMP_RET_TRACE
1790 rule now functioning the same as a
1791 .BR SECCOMP_RET_ALLOW .
1792 Specifically, this means that if registers are not modified during the
1793 .BR PTRACE_EVENT_SECCOMP
1794 stop, the system call will then be allowed.
1795 .\"
1796 .SS PTRACE_SINGLESTEP stops
1797 [Details of these kinds of stops are yet to be documented.]
1798 .\"
1799 .\" FIXME .
1800 .\" document stops occurring with PTRACE_SINGLESTEP
1801 .\"
1802 .SS Informational and restarting ptrace commands
1803 Most ptrace commands (all except
1804 .BR PTRACE_ATTACH ,
1805 .BR PTRACE_SEIZE ,
1806 .BR PTRACE_TRACEME ,
1807 .BR PTRACE_INTERRUPT ,
1808 and
1809 .BR PTRACE_KILL )
1810 require the tracee to be in a ptrace-stop, otherwise they fail with
1811 .BR ESRCH .
1812 .LP
1813 When the tracee is in ptrace-stop,
1814 the tracer can read and write data to
1815 the tracee using informational commands.
1816 These commands leave the tracee in ptrace-stopped state:
1817 .LP
1818 .nf
1819 ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1820 ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1821 ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1822 ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1823 ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov);
1824 ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov);
1825 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1826 ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1827 ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1828 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1829 .fi
1830 .LP
1831 Note that some errors are not reported.
1832 For example, setting signal information
1833 .RI ( siginfo )
1834 may have no effect in some ptrace-stops, yet the call may succeed
1835 (return 0 and not set
1836 .IR errno );
1837 querying
1838 .B PTRACE_GETEVENTMSG
1839 may succeed and return some random value if current ptrace-stop
1840 is not documented as returning a meaningful event message.
1841 .LP
1842 The call
1843
1844 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1845
1846 affects one tracee.
1847 The tracee's current flags are replaced.
1848 Flags are inherited by new tracees created and "auto-attached" via active
1849 .BR PTRACE_O_TRACEFORK ,
1850 .BR PTRACE_O_TRACEVFORK ,
1851 or
1852 .BR PTRACE_O_TRACECLONE
1853 options.
1854 .LP
1855 Another group of commands makes the ptrace-stopped tracee run.
1856 They have the form:
1857 .LP
1858 ptrace(cmd, pid, 0, sig);
1859 .LP
1860 where
1861 .I cmd
1862 is
1863 .BR PTRACE_CONT ,
1864 .BR PTRACE_LISTEN ,
1865 .BR PTRACE_DETACH ,
1866 .BR PTRACE_SYSCALL ,
1867 .BR PTRACE_SINGLESTEP ,
1868 .BR PTRACE_SYSEMU ,
1869 or
1870 .BR PTRACE_SYSEMU_SINGLESTEP .
1871 If the tracee is in signal-delivery-stop,
1872 .I sig
1873 is the signal to be injected (if it is nonzero).
1874 Otherwise,
1875 .I sig
1876 may be ignored.
1877 (When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
1878 recommended practice is to always pass 0 in
1879 .IR sig .)
1880 .SS Attaching and detaching
1881 A thread can be attached to the tracer using the call
1882
1883 ptrace(PTRACE_ATTACH, pid, 0, 0);
1884
1885 or
1886
1887 ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags);
1888
1889 .B PTRACE_ATTACH
1890 sends
1891 .B SIGSTOP
1892 to this thread.
1893 If the tracer wants this
1894 .B SIGSTOP
1895 to have no effect, it needs to suppress it.
1896 Note that if other signals are concurrently sent to
1897 this thread during attach,
1898 the tracer may see the tracee enter signal-delivery-stop
1899 with other signal(s) first!
1900 The usual practice is to reinject these signals until
1901 .B SIGSTOP
1902 is seen, then suppress
1903 .B SIGSTOP
1904 injection.
1905 The design bug here is that a ptrace attach and a concurrently delivered
1906 .B SIGSTOP
1907 may race and the concurrent
1908 .B SIGSTOP
1909 may be lost.
1910 .\"
1911 .\" FIXME Describe how to attach to a thread which is already group-stopped.
1912 .LP
1913 Since attaching sends
1914 .B SIGSTOP
1915 and the tracer usually suppresses it, this may cause a stray
1916 .B EINTR
1917 return from the currently executing system call in the tracee,
1918 as described in the "Signal injection and suppression" section.
1919 .LP
1920 Since Linux 3.4,
1921 .B PTRACE_SEIZE
1922 can be used instead of
1923 .BR PTRACE_ATTACH .
1924 .B PTRACE_SEIZE
1925 does not stop the attached process.
1926 If you need to stop
1927 it after attach (or at any other time) without sending it any signals,
1928 use
1929 .B PTRACE_INTERRUPT
1930 command.
1931 .LP
1932 The request
1933
1934 ptrace(PTRACE_TRACEME, 0, 0, 0);
1935
1936 turns the calling thread into a tracee.
1937 The thread continues to run (doesn't enter ptrace-stop).
1938 A common practice is to follow the
1939 .B PTRACE_TRACEME
1940 with
1941
1942 raise(SIGSTOP);
1943
1944 and allow the parent (which is our tracer now) to observe our
1945 signal-delivery-stop.
1946 .LP
1947 If the
1948 .BR PTRACE_O_TRACEFORK ,
1949 .BR PTRACE_O_TRACEVFORK ,
1950 or
1951 .BR PTRACE_O_TRACECLONE
1952 options are in effect, then children created by, respectively,
1953 .BR vfork (2)
1954 or
1955 .BR clone (2)
1956 with the
1957 .B CLONE_VFORK
1958 flag,
1959 .BR fork (2)
1960 or
1961 .BR clone (2)
1962 with the exit signal set to
1963 .BR SIGCHLD ,
1964 and other kinds of
1965 .BR clone (2),
1966 are automatically attached to the same tracer which traced their parent.
1967 .B SIGSTOP
1968 is delivered to the children, causing them to enter
1969 signal-delivery-stop after they exit the system call which created them.
1970 .LP
1971 Detaching of the tracee is performed by:
1972
1973 ptrace(PTRACE_DETACH, pid, 0, sig);
1974
1975 .B PTRACE_DETACH
1976 is a restarting operation;
1977 therefore it requires the tracee to be in ptrace-stop.
1978 If the tracee is in signal-delivery-stop, a signal can be injected.
1979 Otherwise, the
1980 .I sig
1981 parameter may be silently ignored.
1982 .LP
1983 If the tracee is running when the tracer wants to detach it,
1984 the usual solution is to send
1985 .B SIGSTOP
1986 (using
1987 .BR tgkill (2),
1988 to make sure it goes to the correct thread),
1989 wait for the tracee to stop in signal-delivery-stop for
1990 .B SIGSTOP
1991 and then detach it (suppressing
1992 .B SIGSTOP
1993 injection).
1994 A design bug is that this can race with concurrent
1995 .BR SIGSTOP s.
1996 Another complication is that the tracee may enter other ptrace-stops
1997 and needs to be restarted and waited for again, until
1998 .B SIGSTOP
1999 is seen.
2000 Yet another complication is to be sure that
2001 the tracee is not already ptrace-stopped,
2002 because no signal delivery happens while it is\(emnot even
2003 .BR SIGSTOP .
2004 .\" FIXME Describe how to detach from a group-stopped tracee so that it
2005 .\" doesn't run, but continues to wait for SIGCONT.
2006 .LP
2007 If the tracer dies, all tracees are automatically detached and restarted,
2008 unless they were in group-stop.
2009 Handling of restart from group-stop is currently buggy,
2010 but the "as planned" behavior is to leave tracee stopped and waiting for
2011 .BR SIGCONT .
2012 If the tracee is restarted from signal-delivery-stop,
2013 the pending signal is injected.
2014 .SS execve(2) under ptrace
2015 .\" clone(2) CLONE_THREAD says:
2016 .\" If any of the threads in a thread group performs an execve(2),
2017 .\" then all threads other than the thread group leader are terminated,
2018 .\" and the new program is executed in the thread group leader.
2019 .\"
2020 When one thread in a multithreaded process calls
2021 .BR execve (2),
2022 the kernel destroys all other threads in the process,
2023 .\" In kernel 3.1 sources, see fs/exec.c::de_thread()
2024 and resets the thread ID of the execing thread to the
2025 thread group ID (process ID).
2026 (Or, to put things another way, when a multithreaded process does an
2027 .BR execve (2),
2028 at completion of the call, it appears as though the
2029 .BR execve (2)
2030 occurred in the thread group leader, regardless of which thread did the
2031 .BR execve (2).)
2032 This resetting of the thread ID looks very confusing to tracers:
2033 .IP * 3
2034 All other threads stop in
2035 .B PTRACE_EVENT_EXIT
2036 stop, if the
2037 .BR PTRACE_O_TRACEEXIT
2038 option was turned on.
2039 Then all other threads except the thread group leader report
2040 death as if they exited via
2041 .BR _exit (2)
2042 with exit code 0.
2043 .IP *
2044 The execing tracee changes its thread ID while it is in the
2045 .BR execve (2).
2046 (Remember, under ptrace, the "pid" returned from
2047 .BR waitpid (2),
2048 or fed into ptrace calls, is the tracee's thread ID.)
2049 That is, the tracee's thread ID is reset to be the same as its process ID,
2050 which is the same as the thread group leader's thread ID.
2051 .IP *
2052 Then a
2053 .B PTRACE_EVENT_EXEC
2054 stop happens, if the
2055 .BR PTRACE_O_TRACEEXEC
2056 option was turned on.
2057 .IP *
2058 If the thread group leader has reported its
2059 .B PTRACE_EVENT_EXIT
2060 stop by this time,
2061 it appears to the tracer that
2062 the dead thread leader "reappears from nowhere".
2063 (Note: the thread group leader does not report death via
2064 .I WIFEXITED(status)
2065 until there is at least one other live thread.
2066 This eliminates the possibility that the tracer will see
2067 it dying and then reappearing.)
2068 If the thread group leader was still alive,
2069 for the tracer this may look as if thread group leader
2070 returns from a different system call than it entered,
2071 or even "returned from a system call even though
2072 it was not in any system call".
2073 If the thread group leader was not traced
2074 (or was traced by a different tracer), then during
2075 .BR execve (2)
2076 it will appear as if it has become a tracee of
2077 the tracer of the execing tracee.
2078 .LP
2079 All of the above effects are the artifacts of
2080 the thread ID change in the tracee.
2081 .LP
2082 The
2083 .B PTRACE_O_TRACEEXEC
2084 option is the recommended tool for dealing with this situation.
2085 First, it enables
2086 .BR PTRACE_EVENT_EXEC
2087 stop,
2088 which occurs before
2089 .BR execve (2)
2090 returns.
2091 In this stop, the tracer can use
2092 .B PTRACE_GETEVENTMSG
2093 to retrieve the tracee's former thread ID.
2094 (This feature was introduced in Linux 3.0.)
2095 Second, the
2096 .B PTRACE_O_TRACEEXEC
2097 option disables legacy
2098 .B SIGTRAP
2099 generation on
2100 .BR execve (2).
2101 .LP
2102 When the tracer receives
2103 .B PTRACE_EVENT_EXEC
2104 stop notification,
2105 it is guaranteed that except this tracee and the thread group leader,
2106 no other threads from the process are alive.
2107 .LP
2108 On receiving the
2109 .B PTRACE_EVENT_EXEC
2110 stop notification,
2111 the tracer should clean up all its internal
2112 data structures describing the threads of this process,
2113 and retain only one data structure\(emone which
2114 describes the single still running tracee, with
2115
2116 thread ID == thread group ID == process ID.
2117 .LP
2118 Example: two threads call
2119 .BR execve (2)
2120 at the same time:
2121 .LP
2122 .nf
2123 *** we get syscall-enter-stop in thread 1: **
2124 PID1 execve("/bin/foo", "foo" <unfinished ...>
2125 *** we issue PTRACE_SYSCALL for thread 1 **
2126 *** we get syscall-enter-stop in thread 2: **
2127 PID2 execve("/bin/bar", "bar" <unfinished ...>
2128 *** we issue PTRACE_SYSCALL for thread 2 **
2129 *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
2130 *** we get syscall-exit-stop for PID0: **
2131 PID0 <... execve resumed> ) = 0
2132 .fi
2133 .LP
2134 If the
2135 .B PTRACE_O_TRACEEXEC
2136 option is
2137 .I not
2138 in effect for the execing tracee,
2139 and if the tracee was
2140 .BR PTRACE_ATTACH ed
2141 rather that
2142 .BR PTRACE_SEIZE d,
2143 the kernel delivers an extra
2144 .B SIGTRAP
2145 to the tracee after
2146 .BR execve (2)
2147 returns.
2148 This is an ordinary signal (similar to one which can be
2149 generated by
2150 .IR "kill -TRAP" ),
2151 not a special kind of ptrace-stop.
2152 Employing
2153 .B PTRACE_GETSIGINFO
2154 for this signal returns
2155 .I si_code
2156 set to 0
2157 .RI ( SI_USER ).
2158 This signal may be blocked by signal mask,
2159 and thus may be delivered (much) later.
2160 .LP
2161 Usually, the tracer (for example,
2162 .BR strace (1))
2163 would not want to show this extra post-execve
2164 .B SIGTRAP
2165 signal to the user, and would suppress its delivery to the tracee (if
2166 .B SIGTRAP
2167 is set to
2168 .BR SIG_DFL ,
2169 it is a killing signal).
2170 However, determining
2171 .I which
2172 .B SIGTRAP
2173 to suppress is not easy.
2174 Setting the
2175 .B PTRACE_O_TRACEEXEC
2176 option or using
2177 .B PTRACE_SEIZE
2178 and thus suppressing this extra
2179 .B SIGTRAP
2180 is the recommended approach.
2181 .SS Real parent
2182 The ptrace API (ab)uses the standard UNIX parent/child signaling over
2183 .BR waitpid (2).
2184 This used to cause the real parent of the process to stop receiving
2185 several kinds of
2186 .BR waitpid (2)
2187 notifications when the child process is traced by some other process.
2188 .LP
2189 Many of these bugs have been fixed, but as of Linux 2.6.38 several still
2190 exist; see BUGS below.
2191 .LP
2192 As of Linux 2.6.38, the following is believed to work correctly:
2193 .IP * 3
2194 exit/death by signal is reported first to the tracer, then,
2195 when the tracer consumes the
2196 .BR waitpid (2)
2197 result, to the real parent (to the real parent only when the
2198 whole multithreaded process exits).
2199 If the tracer and the real parent are the same process,
2200 the report is sent only once.
2201 .SH RETURN VALUE
2202 On success, the
2203 .B PTRACE_PEEK*
2204 requests return the requested data (but see NOTES),
2205 while other requests return zero.
2206 .LP
2207 On error, all requests return \-1, and
2208 .I errno
2209 is set appropriately.
2210 Since the value returned by a successful
2211 .B PTRACE_PEEK*
2212 request may be \-1, the caller must clear
2213 .I errno
2214 before the call, and then check it afterward
2215 to determine whether or not an error occurred.
2216 .SH ERRORS
2217 .TP
2218 .B EBUSY
2219 (i386 only) There was an error with allocating or freeing a debug register.
2220 .TP
2221 .B EFAULT
2222 There was an attempt to read from or write to an invalid area in
2223 the tracer's or the tracee's memory,
2224 probably because the area wasn't mapped or accessible.
2225 Unfortunately, under Linux, different variations of this fault
2226 will return
2227 .B EIO
2228 or
2229 .B EFAULT
2230 more or less arbitrarily.
2231 .TP
2232 .B EINVAL
2233 An attempt was made to set an invalid option.
2234 .TP
2235 .B EIO
2236 .I request
2237 is invalid, or an attempt was made to read from or
2238 write to an invalid area in the tracer's or the tracee's memory,
2239 or there was a word-alignment violation,
2240 or an invalid signal was specified during a restart request.
2241 .TP
2242 .B EPERM
2243 The specified process cannot be traced.
2244 This could be because the
2245 tracer has insufficient privileges (the required capability is
2246 .BR CAP_SYS_PTRACE );
2247 unprivileged processes cannot trace processes that they
2248 cannot send signals to or those running
2249 set-user-ID/set-group-ID programs, for obvious reasons.
2250 Alternatively, the process may already be being traced,
2251 or (on kernels before 2.6.26) be
2252 .BR init (1)
2253 (PID 1).
2254 .TP
2255 .B ESRCH
2256 The specified process does not exist, or is not currently being traced
2257 by the caller, or is not stopped
2258 (for requests that require a stopped tracee).
2259 .SH CONFORMING TO
2260 SVr4, 4.3BSD.
2261 .SH NOTES
2262 Although arguments to
2263 .BR ptrace ()
2264 are interpreted according to the prototype given,
2265 glibc currently declares
2266 .BR ptrace ()
2267 as a variadic function with only the
2268 .I request
2269 argument fixed.
2270 It is recommended to always supply four arguments,
2271 even if the requested operation does not use them,
2272 setting unused/ignored arguments to
2273 .I 0L
2274 or
2275 .IR "(void\ *)\ 0".
2276 .LP
2277 In Linux kernels before 2.6.26,
2278 .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
2279 .BR init (1),
2280 the process with PID 1, may not be traced.
2281 .LP
2282 A tracees parent continues to be the tracer even if that tracer calls
2283 .BR execve (2).
2284 .LP
2285 The layout of the contents of memory and the USER area are
2286 quite operating-system- and architecture-specific.
2287 The offset supplied, and the data returned,
2288 might not entirely match with the definition of
2289 .IR "struct user" .
2290 .\" See http://lkml.org/lkml/2008/5/8/375
2291 .LP
2292 The size of a "word" is determined by the operating-system variant
2293 (e.g., for 32-bit Linux it is 32 bits).
2294 .LP
2295 This page documents the way the
2296 .BR ptrace ()
2297 call works currently in Linux.
2298 Its behavior differs significantly on other flavors of UNIX.
2299 In any case, use of
2300 .BR ptrace ()
2301 is highly specific to the operating system and architecture.
2302 .\"
2303 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2304 .\"
2305 .SS Ptrace access mode checking
2306 Various parts of the kernel-user-space API (not just
2307 .BR ptrace ()
2308 operations), require so-called "ptrace access mode" checks,
2309 whose outcome determines whether an operation is permitted
2310 (or, in a few cases, causes a "read" operation to return sanitized data).
2311 These checks are performed in cases where one process can
2312 inspect sensitive information about,
2313 or in some cases modify the state of, another process.
2314 The checks are based on factors such as the credentials and capabilities
2315 of the two processes,
2316 whether or not the "target" process is dumpable,
2317 and the results of checks performed by any enabled Linux Security Module
2318 (LSM)\(emfor example, SELinux, Yama, or Smack\(emand by the commoncap LSM
2319 (which is always invoked).
2320
2321 Prior to Linux 2.6.27, all access checks were of a single type.
2322 Since Linux 2.6.27,
2323 .\" commit 006ebb40d3d65338bd74abb03b945f8d60e362bd
2324 two access mode levels are distinguished:
2325 .TP
2326 .BR PTRACE_MODE_READ
2327 For "read" operations or other operations that are less dangerous,
2328 such as:
2329 .BR get_robust_list (2);
2330 .BR kcmp (2);
2331 reading
2332 .IR /proc/[pid]/auxv ,
2333 .IR /proc/[pid]/environ ,
2334 or
2335 .IR /proc/[pid]/stat ;
2336 or
2337 .BR readlink (2)
2338 of a
2339 .IR /proc/[pid]/ns/*
2340 file.
2341 .TP
2342 .BR PTRACE_MODE_ATTACH
2343 For "write" operations, or other operations that are more dangerous,
2344 such as: ptrace attaching
2345 .RB ( PTRACE_ATTACH )
2346 to another process
2347 or calling
2348 .BR process_vm_writev (2).
2349 .RB ( PTRACE_MODE_ATTACH
2350 was effectively the default before Linux 2.6.27.)
2351 .\"
2352 .\" Regarding the above description of the distinction between
2353 .\" PTRACE_MODE_READ and PTRACE_MODE_ATTACH, Stephen Smalley notes:
2354 .\"
2355 .\" That was the intent when the distinction was introduced, but it doesn't
2356 .\" appear to have been properly maintained, e.g. there is now a common
2357 .\" helper lock_trace() that is used for
2358 .\" /proc/pid/{stack,syscall,personality} but checks PTRACE_MODE_ATTACH, and
2359 .\" PTRACE_MODE_ATTACH is also used in timerslack_ns_write/show(). Likely
2360 .\" should review and make them consistent. There was also some debate
2361 .\" about proper handling of /proc/pid/fd. Arguably that one might belong
2362 .\" back in the _ATTACH camp.
2363 .\"
2364 .PP
2365 Since Linux 4.5,
2366 .\" commit caaee6234d05a58c5b4d05e7bf766131b810a657
2367 the above access mode checks are combined (ORed) with
2368 one of the following modifiers:
2369 .TP
2370 .B PTRACE_MODE_FSCREDS
2371 Use the caller's filesystem UID and GID (see
2372 .BR credentials (7))
2373 or effective capabilities for LSM checks.
2374 .TP
2375 .B PTRACE_MODE_REALCREDS
2376 Use the caller's real UID and GID or permitted capabilities for LSM checks.
2377 This was effectively the default before Linux 4.5.
2378 .PP
2379 Because combining one of the credential modifiers with one of
2380 the aforementioned access modes is typical,
2381 some macros are defined in the kernel sources for the combinations:
2382 .TP
2383 .B PTRACE_MODE_READ_FSCREDS
2384 Defined as
2385 .BR "PTRACE_MODE_READ | PTRACE_MODE_FSCREDS" .
2386 .TP
2387 .B PTRACE_MODE_READ_REALCREDS
2388 Defined as
2389 .BR "PTRACE_MODE_READ | PTRACE_MODE_REALCREDS" .
2390 .TP
2391 .B PTRACE_MODE_ATTACH_FSCREDS
2392 Defined as
2393 .BR "PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS" .
2394 .TP
2395 .B PTRACE_MODE_ATTACH_REALCREDS
2396 Defined as
2397 .BR "PTRACE_MODE_ATTACH | PTRACE_MODE_REALCREDS" .
2398 .PP
2399 One further modifier can be ORed with the access mode:
2400 .TP
2401 .BR PTRACE_MODE_NOAUDIT " (since Linux 3.3)"
2402 .\" commit 69f594a38967f4540ce7a29b3fd214e68a8330bd
2403 .\" Just for /proc/pid/stat
2404 Don't audit this access mode check.
2405 This modifier is employed for ptrace access mode checks
2406 (such as checks when reading
2407 .IR /proc/[pid]/stat )
2408 that merely cause the output to be filtered or sanitized,
2409 rather than causing an error to be returned to the caller.
2410 In these cases, accessing the file is not a security violation and
2411 there is no reason to generate a security audit record.
2412 This modifier suppresses the generation of
2413 such an audit record for the particular access check.
2414 .PP
2415 Note that all of the
2416 .BR PTRACE_MODE_*
2417 constants described in this subsection are kernel-internal,
2418 and not visible to user space.
2419 The constant names are mentioned here in order to label the various kinds of
2420 ptrace access mode checks that are performed for various system calls
2421 and accesses to various pseudofiles (e.g., under
2422 .IR /proc ).
2423 These names are used in other manual pages to provide a simple
2424 shorthand for labeling the different kernel checks.
2425
2426 The algorithm employed for ptrace access mode checking determines whether
2427 the calling process is allowed to perform the corresponding action
2428 on the target process.
2429 (In the case of opening
2430 .IR /proc/[pid]
2431 files, the "calling process" is the one opening the file,
2432 and the process with the corresponding PID is the "target process".)
2433 The algorithm is as follows:
2434 .IP 1. 4
2435 If the calling thread and the target thread are in the same
2436 thread group, access is always allowed.
2437 .IP 2.
2438 If the access mode specifies
2439 .BR PTRACE_MODE_FSCREDS ,
2440 then, for the check in the next step,
2441 employ the caller's filesystem UID and GID.
2442 (As noted in
2443 .BR credentials (7),
2444 the filesystem UID and GID almost always have the same values
2445 as the corresponding effective IDs.)
2446
2447 Otherwise, the access mode specifies
2448 .BR PTRACE_MODE_REALCREDS ,
2449 so use the caller's real UID and GID for the checks in the next step.
2450 (Most APIs that check the caller's UID and GID use the effective IDs.
2451 For historical reasons, the
2452 .BR PTRACE_MODE_REALCREDS
2453 check uses the real IDs instead.)
2454 .IP 3.
2455 Deny access if
2456 .I neither
2457 of the following is true:
2458 .RS
2459 .IP \(bu 2
2460 The real, effective, and saved-set user IDs of the target
2461 match the caller's user ID,
2462 .IR and
2463 the real, effective, and saved-set group IDs of the target
2464 match the caller's group ID.
2465 .IP \(bu
2466 The caller has the
2467 .B CAP_SYS_PTRACE
2468 capability in the user namespace of the target.
2469 .RE
2470 .IP 4.
2471 Deny access if the target process "dumpable" attribute has a value other than 1
2472 .RB ( SUID_DUMP_USER ;
2473 see the discussion of
2474 .BR PR_SET_DUMPABLE
2475 in
2476 .BR prctl (2)),
2477 and the caller does not have the
2478 .BR CAP_SYS_PTRACE
2479 capability in the user namespace of the target process.
2480 .IP 5.
2481 The kernel LSM
2482 .IR security_ptrace_access_check ()
2483 interface is invoked to see if ptrace access is permitted.
2484 The results depend on the LSM(s).
2485 The implementation of this interface in the commoncap LSM performs
2486 the following steps:
2487 .\" (in cap_ptrace_access_check()):
2488 .RS
2489 .IP a) 3
2490 If the access mode includes
2491 .BR PTRACE_MODE_FSCREDS ,
2492 then use the caller's
2493 .I effective
2494 capability set
2495 in the following check;
2496 otherwise (the access mode specifies
2497 .BR PTRACE_MODE_REALCREDS ,
2498 so) use the caller's
2499 .I permitted
2500 capability set.
2501 .IP b)
2502 Deny access if
2503 .I neither
2504 of the following is true:
2505 .RS
2506 .IP \(bu 2
2507 The caller and the target process are in the same user namespace,
2508 and the caller's capabilities are a proper superset of the target process's
2509 .I permitted
2510 capabilities.
2511 .IP \(bu
2512 The caller has the
2513 .B CAP_SYS_PTRACE
2514 capability in the target process's user namespace.
2515 .RE
2516 .IP
2517 Note that the commoncap LSM does not distinguish between
2518 .B PTRACE_MODE_READ
2519 and
2520 .BR PTRACE_MODE_ATTACH .
2521 .RE
2522 .IP 6.
2523 If access has not been denied by any of the preceding steps,
2524 then access is allowed.
2525 .\"
2526 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2527 .\"
2528 .SS /proc/sys/kernel/yama/ptrace_scope
2529 On systems with the Yama Linux Security Module (LSM) installed
2530 (i.e., the kernel was configured with
2531 .BR CONFIG_SECURITY_YAMA ),
2532 the
2533 .I /proc/sys/kernel/yama/ptrace_scope
2534 file (available since Linux 3.4)
2535 .\" commit 2d514487faf188938a4ee4fb3464eeecfbdcf8eb
2536 can be used to restrict the ability to trace a process with
2537 .BR ptrace ()
2538 (and thus also the ability to use tools such as
2539 .BR strace (1)
2540 and
2541 .BR gdb (1)).
2542 The goal of such restrictions is to prevent attack escalation whereby
2543 a compromised process can ptrace-attach to other sensitive processes
2544 (e.g., a GPG agent or an SSH session) owned by the user in order
2545 to gain additional credentials that may exist in memory
2546 and thus expand the scope of the attack.
2547
2548 More precisely, the Yama LSM limits two types of operations:
2549 .IP * 3
2550 Any operation that performs a ptrace access mode
2551 .BR PTRACE_MODE_ATTACH
2552 check\(emfor example,
2553 .BR ptrace ()
2554 .BR PTRACE_ATTACH .
2555 (See the "Ptrace access mode checking" discussion above.)
2556
2557 .IP *
2558 .BR ptrace ()
2559 .BR PTRACE_TRACEME .
2560 .PP
2561 A process that has the
2562 .B CAP_SYS_PTRACE
2563 capability can update the
2564 .IR /proc/sys/kernel/yama/ptrace_scope
2565 file with one of the following values:
2566 .TP
2567 0 ("classic ptrace permissions")
2568 No additional restrictions on operations that perform
2569 .BR PTRACE_MODE_ATTACH
2570 checks (beyond those imposed by the commoncap and other LSMs).
2571
2572 The use of
2573 .BR PTRACE_TRACEME
2574 is unchanged.
2575 .TP
2576 1 ("restricted ptrace") [default value]
2577 When performing an operation that requires a
2578 .BR PTRACE_MODE_ATTACH
2579 check, the calling process must either have the
2580 .B CAP_SYS_PTRACE
2581 capability in the user namespace of the target process or
2582 it must have a predefined relationship with the target process.
2583 By default,
2584 the predefined relationship is that the target process
2585 must be a descendant of the caller.
2586
2587 A target process can employ the
2588 .BR prctl (2)
2589 .B PR_SET_PTRACER
2590 operation to declare an additional PID that is allowed to perform
2591 .BR PTRACE_MODE_ATTACH
2592 operations on the target.
2593 See the kernel source file
2594 .IR Documentation/security/Yama.txt
2595 for further details.
2596
2597 The use of
2598 .BR PTRACE_TRACEME
2599 is unchanged.
2600 .TP
2601 2 ("admin-only attach")
2602 Only processes with the
2603 .B CAP_SYS_PTRACE
2604 capability in the user namespace of the target process may perform
2605 .BR PTRACE_MODE_ATTACH
2606 operations or trace children that employ
2607 .BR PTRACE_TRACEME .
2608 .TP
2609 3 ("no attach")
2610 No process may perform
2611 .BR PTRACE_MODE_ATTACH
2612 operations or trace children that employ
2613 .BR PTRACE_TRACEME .
2614
2615 Once this value has been written to the file, it cannot be changed.
2616 .PP
2617 With respect to values 1 and 2,
2618 note that creating a new user namespace effectively removes the
2619 protection offered by Yama.
2620 This is because a process in the parent user namespace whose effective
2621 UID matches the UID of the creator of a child namespace
2622 has all capabilities (including
2623 .BR CAP_SYS_PTRACE )
2624 when performing operations within the child user namespace
2625 (and further-removed descendants of that namespace).
2626 Consequently, when a process tries to use user namespaces to sandbox itself,
2627 it inadvertently weakens the protections offered by the Yama LSM.
2628 .\"
2629 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2630 .\"
2631 .SS C library/kernel differences
2632 At the system call level, the
2633 .BR PTRACE_PEEKTEXT ,
2634 .BR PTRACE_PEEKDATA ,
2635 and
2636 .BR PTRACE_PEEKUSER
2637 requests have a different API: they store the result
2638 at the address specified by the
2639 .I data
2640 parameter, and the return value is the error flag.
2641 The glibc wrapper function provides the API given in DESCRIPTION above,
2642 with the result being returned via the function return value.
2643 .SH BUGS
2644 On hosts with 2.6 kernel headers,
2645 .B PTRACE_SETOPTIONS
2646 is declared with a different value than the one for 2.4.
2647 This leads to applications compiled with 2.6 kernel
2648 headers failing when run on 2.4 kernels.
2649 This can be worked around by redefining
2650 .B PTRACE_SETOPTIONS
2651 to
2652 .BR PTRACE_OLDSETOPTIONS ,
2653 if that is defined.
2654 .LP
2655 Group-stop notifications are sent to the tracer, but not to real parent.
2656 Last confirmed on 2.6.38.6.
2657 .LP
2658 If a thread group leader is traced and exits by calling
2659 .BR _exit (2),
2660 .\" Note from Denys Vlasenko:
2661 .\" Here "exits" means any kind of death - _exit, exit_group,
2662 .\" signal death. Signal death and exit_group cases are trivial,
2663 .\" though: since signal death and exit_group kill all other threads
2664 .\" too, "until all other threads exit" thing happens rather soon
2665 .\" in these cases. Therefore, only _exit presents observably
2666 .\" puzzling behavior to ptrace users: thread leader _exit's,
2667 .\" but WIFEXITED isn't reported! We are trying to explain here
2668 .\" why it is so.
2669 a
2670 .B PTRACE_EVENT_EXIT
2671 stop will happen for it (if requested), but the subsequent
2672 .B WIFEXITED
2673 notification will not be delivered until all other threads exit.
2674 As explained above, if one of other threads calls
2675 .BR execve (2),
2676 the death of the thread group leader will
2677 .I never
2678 be reported.
2679 If the execed thread is not traced by this tracer,
2680 the tracer will never know that
2681 .BR execve (2)
2682 happened.
2683 One possible workaround is to
2684 .B PTRACE_DETACH
2685 the thread group leader instead of restarting it in this case.
2686 Last confirmed on 2.6.38.6.
2687 .\" FIXME . need to test/verify this scenario
2688 .LP
2689 A
2690 .B SIGKILL
2691 signal may still cause a
2692 .B PTRACE_EVENT_EXIT
2693 stop before actual signal death.
2694 This may be changed in the future;
2695 .B SIGKILL
2696 is meant to always immediately kill tasks even under ptrace.
2697 Last confirmed on Linux 3.13.
2698 .LP
2699 Some system calls return with
2700 .B EINTR
2701 if a signal was sent to a tracee, but delivery was suppressed by the tracer.
2702 (This is very typical operation: it is usually
2703 done by debuggers on every attach, in order to not introduce
2704 a bogus
2705 .BR SIGSTOP ).
2706 As of Linux 3.2.9, the following system calls are affected
2707 (this list is likely incomplete):
2708 .BR epoll_wait (2),
2709 and
2710 .BR read (2)
2711 from an
2712 .BR inotify (7)
2713 file descriptor.
2714 The usual symptom of this bug is that when you attach to
2715 a quiescent process with the command
2716
2717 strace \-p <process-ID>
2718
2719 then, instead of the usual
2720 and expected one-line output such as
2721 .nf
2722
2723 restart_syscall(<... resuming interrupted call ...>_
2724
2725 .fi
2726 or
2727 .nf
2728
2729 select(6, [5], NULL, [5], NULL_
2730
2731 .fi
2732 ('_' denotes the cursor position), you observe more than one line.
2733 For example:
2734 .nf
2735
2736 clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0
2737 epoll_wait(4,_
2738
2739 .fi
2740 What is not visible here is that the process was blocked in
2741 .BR epoll_wait (2)
2742 before
2743 .BR strace (1)
2744 has attached to it.
2745 Attaching caused
2746 .BR epoll_wait (2)
2747 to return to user space with the error
2748 .BR EINTR .
2749 In this particular case, the program reacted to
2750 .B EINTR
2751 by checking the current time, and then executing
2752 .BR epoll_wait (2)
2753 again.
2754 (Programs which do not expect such "stray"
2755 .BR EINTR
2756 errors may behave in an unintended way upon an
2757 .BR strace (1)
2758 attach.)
2759 .SH SEE ALSO
2760 .BR gdb (1),
2761 .BR strace (1),
2762 .BR clone (2),
2763 .BR execve (2),
2764 .BR fork (2),
2765 .BR gettid (2),
2766 .BR prctl (2),
2767 .BR seccomp (2),
2768 .BR sigaction (2),
2769 .BR tgkill (2),
2770 .BR vfork (2),
2771 .BR waitpid (2),
2772 .BR exec (3),
2773 .BR capabilities (7),
2774 .BR signal (7)