Start of man-pages-5.14: renaming .Announce and .lsm files
[man-pages.git] / man2 / ptrace.2
blobffaf8db0d84867d7262ef9c15f2de7aab4721843
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
103 .\" and others that can be found in the arch/*/include/uapi/asm/ptrace files
105 .TH PTRACE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
106 .SH NAME
107 ptrace \- process trace
108 .SH SYNOPSIS
110 .B #include <sys/ptrace.h>
112 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ,
113 .BI "            void *" addr ", void *" data );
115 .SH DESCRIPTION
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.
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
135     ptrace(PTRACE_foo, pid, ...)
137 where
138 .I pid
139 is the thread ID of the corresponding Linux thread.
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.)
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
156 .BR PTRACE_SEIZE .
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).
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.
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 .
189 The value of
190 .I request
191 determines the action to be performed:
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 ,
200 .IR data
201 are ignored.)
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 ,
215 .BR PTRACE_KILL ,
216 the tracee must be stopped.
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.)
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.)
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
256 .BR PTRACE_PEEKDATA ,
257 these two requests are currently equivalent.
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?
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.
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
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
295 .B PTRACE_GETFPREGS
296 are not present on all architectures.
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.
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
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
341 .B PTRACE_SETFPREGS
342 are not present on all architectures.
344 .BR PTRACE_SETREGSET " (since Linux 2.6.34)"
345 Modify the tracee's registers.
346 The meaning of
347 .I addr
349 .I data
350 is analogous to
351 .BR PTRACE_GETREGSET .
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.)
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.)
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,
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.
407 .in +4n
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 */
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.
425 .BR PTRACE_GETSIGMASK " (since Linux 3.11)"
426 .\" commit 29000caecbe87b6b66f144f72111f0d02fbbf0c1
427 Place a copy of the mask of blocked signals (see
428 .BR sigprocmask (2))
429 in the buffer pointed to by
430 .IR data ,
431 which should be a pointer to a buffer of type
432 .IR sigset_t .
434 .I addr
435 argument contains the size of the buffer pointed to by
436 .IR data
437 (i.e.,
438 .IR sizeof(sigset_t) ).
440 .BR PTRACE_SETSIGMASK " (since Linux 3.11)"
441 Change the mask of blocked signals (see
442 .BR sigprocmask (2))
443 to the value specified in the buffer pointed to by
444 .IR data ,
445 which should be a pointer to a buffer of type
446 .IR sigset_t .
448 .I addr
449 argument contains the size of the buffer pointed to by
450 .IR data
451 (i.e.,
452 .IR sizeof(sigset_t) ).
454 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
455 Set ptrace options from
456 .IR data .
457 .RI ( addr
458 is ignored.)
459 .IR data
460 is interpreted as a bit mask of options,
461 which are specified by the following flags:
464 .BR PTRACE_O_EXITKILL " (since Linux 3.8)"
465 .\" commit 992fb6e170639b0849bace8e49bf31bd37c4123
466 Send a
467 .B SIGKILL
468 signal to the tracee if the tracer exits.
469 This option is useful for ptrace jailers that
470 want to ensure that tracees can never escape the tracer's control.
472 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
473 Stop the tracee at the next
474 .BR clone (2)
475 and automatically start tracing the newly cloned process,
476 which will start with a
477 .BR SIGSTOP ,
479 .B PTRACE_EVENT_STOP
481 .B PTRACE_SEIZE
482 was used.
484 .BR waitpid (2)
485 by the tracer will return a
486 .I status
487 value such that
490   status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
493 The PID of the new process can be retrieved with
494 .BR PTRACE_GETEVENTMSG .
496 This option may not catch
497 .BR clone (2)
498 calls in all cases.
499 If the tracee calls
500 .BR clone (2)
501 with the
502 .B CLONE_VFORK
503 flag,
504 .B PTRACE_EVENT_VFORK
505 will be delivered instead
507 .B PTRACE_O_TRACEVFORK
508 is set; otherwise if the tracee calls
509 .BR clone (2)
510 with the exit signal set to
511 .BR SIGCHLD ,
512 .B PTRACE_EVENT_FORK
513 will be delivered if
514 .B PTRACE_O_TRACEFORK
515 is set.
517 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
518 Stop the tracee at the next
519 .BR execve (2).
521 .BR waitpid (2)
522 by the tracer will return a
523 .I status
524 value such that
527   status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
530 If the execing thread is not a thread group leader,
531 the thread ID is reset to thread group leader's ID before this stop.
532 Since Linux 3.0, the former thread ID can be retrieved with
533 .BR PTRACE_GETEVENTMSG .
535 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
536 Stop the tracee at exit.
538 .BR waitpid (2)
539 by the tracer will return a
540 .I status
541 value such that
544   status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
547 The tracee's exit status can be retrieved with
548 .BR PTRACE_GETEVENTMSG .
550 The tracee is stopped early during process exit,
551 when registers are still available,
552 allowing the tracer to see where the exit occurred,
553 whereas the normal exit notification is done after the process
554 is finished exiting.
555 Even though context is available,
556 the tracer cannot prevent the exit from happening at this point.
558 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
559 Stop the tracee at the next
560 .BR fork (2)
561 and automatically start tracing the newly forked process,
562 which will start with a
563 .BR SIGSTOP ,
565 .B PTRACE_EVENT_STOP
567 .B PTRACE_SEIZE
568 was used.
570 .BR waitpid (2)
571 by the tracer will return a
572 .I status
573 value such that
576   status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
579 The PID of the new process can be retrieved with
580 .BR PTRACE_GETEVENTMSG .
582 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
583 When delivering system call traps, set bit 7 in the signal number
584 (i.e., deliver
585 .IR "SIGTRAP|0x80" ).
586 This makes it easy for the tracer to distinguish
587 normal traps from those caused by a system call.
589 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
590 Stop the tracee at the next
591 .BR vfork (2)
592 and automatically start tracing the newly vforked process,
593 which will start with a
594 .BR SIGSTOP ,
596 .B PTRACE_EVENT_STOP
598 .B PTRACE_SEIZE
599 was used.
601 .BR waitpid (2)
602 by the tracer will return a
603 .I status
604 value such that
607   status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
610 The PID of the new process can be retrieved with
611 .BR PTRACE_GETEVENTMSG .
613 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
614 Stop the tracee at the completion of the next
615 .BR vfork (2).
617 .BR waitpid (2)
618 by the tracer will return a
619 .I status
620 value such that
623   status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
626 The PID of the new process can (since Linux 2.6.18) be retrieved with
627 .BR PTRACE_GETEVENTMSG .
629 .BR PTRACE_O_TRACESECCOMP " (since Linux 3.5)"
630 Stop the tracee when a
631 .BR seccomp (2)
632 .BR SECCOMP_RET_TRACE
633 rule is triggered.
635 .BR waitpid (2)
636 by the tracer will return a
637 .I status
638 value such that
641   status>>8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP<<8))
644 While this triggers a
645 .BR PTRACE_EVENT
646 stop, it is similar to a syscall-enter-stop.
647 For details, see the note on
648 .B PTRACE_EVENT_SECCOMP
649 below.
650 The seccomp event message data (from the
651 .BR SECCOMP_RET_DATA
652 portion of the seccomp filter rule) can be retrieved with
653 .BR PTRACE_GETEVENTMSG .
655 .BR PTRACE_O_SUSPEND_SECCOMP " (since Linux 4.3)"
656 .\" commit 13c4a90119d28cfcb6b5bdd820c233b86c2b0237
657 Suspend the tracee's seccomp protections.
658 This applies regardless of mode, and
659 can be used when the tracee has not yet installed seccomp filters.
660 That is, a valid use case is to suspend a tracee's seccomp protections
661 before they are installed by the tracee,
662 let the tracee install the filters,
663 and then clear this flag when the filters should be resumed.
664 Setting this option requires that the tracer have the
665 .BR CAP_SYS_ADMIN
666 capability,
667 not have any seccomp protections installed, and not have
668 .BR PTRACE_O_SUSPEND_SECCOMP
669 set on itself.
672 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
673 Retrieve a message (as an
674 .IR "unsigned long" )
675 about the ptrace event
676 that just happened, placing it at the address
677 .I data
678 in the tracer.
680 .BR PTRACE_EVENT_EXIT ,
681 this is the tracee's exit status.
683 .BR PTRACE_EVENT_FORK ,
684 .BR PTRACE_EVENT_VFORK ,
685 .BR PTRACE_EVENT_VFORK_DONE ,
687 .BR PTRACE_EVENT_CLONE ,
688 this is the PID of the new process.
690 .BR PTRACE_EVENT_SECCOMP ,
691 this is the
692 .BR seccomp (2)
693 filter's
694 .BR SECCOMP_RET_DATA
695 associated with the triggered rule.
696 .RI ( addr
697 is ignored.)
699 .B PTRACE_CONT
700 Restart the stopped tracee process.
702 .I data
703 is nonzero,
704 it is interpreted as the number of a signal to be delivered to the tracee;
705 otherwise, no signal is delivered.
706 Thus, for example, the tracer can control
707 whether a signal sent to the tracee is delivered or not.
708 .RI ( addr
709 is ignored.)
711 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
712 Restart the stopped tracee as for
713 .BR PTRACE_CONT ,
714 but arrange for the tracee to be stopped at
715 the next entry to or exit from a system call,
716 or after execution of a single instruction, respectively.
717 (The tracee will also, as usual, be stopped upon receipt of a signal.)
718 From the tracer's perspective, the tracee will appear to have been
719 stopped by receipt of a
720 .BR SIGTRAP .
721 So, for
722 .BR PTRACE_SYSCALL ,
723 for example, the idea is to inspect
724 the arguments to the system call at the first stop,
725 then do another
726 .B PTRACE_SYSCALL
727 and inspect the return value of the system call at the second stop.
729 .I data
730 argument is treated as for
731 .BR PTRACE_CONT .
732 .RI ( addr
733 is ignored.)
735 .BR PTRACE_SET_SYSCALL " (since Linux 2.6.16)"
736 .\" commit 3f471126ee53feb5e9b210ea2f525ed3bb9b7a7f
737 When in syscall-enter-stop,
738 change the number of the system call that is about to
739 be executed to the number specified in the
740 .I data
741 argument.
743 .I addr
744 argument is ignored.
745 This request is currently
746 .\" As of 4.19-rc2
747 supported only on arm (and arm64, though only for backwards compatibility),
748 .\" commit 27aa55c5e5123fa8b8ad0156559d34d7edff58ca
749 but most other architectures have other means of accomplishing this
750 (usually by changing the register that the userland code passed the
751 system call number in).
752 .\" see change_syscall in tools/testing/selftests/seccomp/seccomp_bpf.c
753 .\" and also strace's linux/*/set_scno.c files.
755 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
757 .BR PTRACE_SYSEMU ,
758 continue and stop on entry to the next system call,
759 which will not be executed.
760 See the documentation on syscall-stops below.
762 .BR PTRACE_SYSEMU_SINGLESTEP ,
763 do the same but also singlestep if not a system call.
764 This call is used by programs like
765 User Mode Linux that want to emulate all the tracee's system calls.
767 .I data
768 argument is treated as for
769 .BR PTRACE_CONT .
771 .I addr
772 argument is ignored.
773 These requests are currently
774 .\" As at 3.7
775 supported only on x86.
777 .BR PTRACE_LISTEN " (since Linux 3.4)"
778 Restart the stopped tracee, but prevent it from executing.
779 The resulting state of the tracee is similar to a process which
780 has been stopped by a
781 .B SIGSTOP
782 (or other stopping signal).
783 See the "group-stop" subsection for additional information.
784 .B PTRACE_LISTEN
785 works only on tracees attached by
786 .BR PTRACE_SEIZE .
788 .B PTRACE_KILL
789 Send the tracee a
790 .B SIGKILL
791 to terminate it.
792 .RI ( addr
794 .I data
795 are ignored.)
797 .I This operation is deprecated; do not use it!
798 Instead, send a
799 .BR SIGKILL
800 directly using
801 .BR kill (2)
803 .BR tgkill (2).
804 The problem with
805 .B PTRACE_KILL
806 is that it requires the tracee to be in signal-delivery-stop,
807 otherwise it may not work
808 (i.e., may complete successfully but won't kill the tracee).
809 By contrast, sending a
810 .B SIGKILL
811 directly has no such limitation.
812 .\" [Note from Denys Vlasenko:
813 .\"     deprecation suggested by Oleg Nesterov. He prefers to deprecate it
814 .\"     instead of describing (and needing to support) PTRACE_KILL's quirks.]
816 .BR PTRACE_INTERRUPT " (since Linux 3.4)"
817 Stop a tracee.
818 If the tracee is running or sleeping in kernel space and
819 .B PTRACE_SYSCALL
820 is in effect,
821 the system call is interrupted and syscall-exit-stop is reported.
822 (The interrupted system call is restarted when the tracee is restarted.)
823 If the tracee was already stopped by a signal and
824 .B PTRACE_LISTEN
825 was sent to it,
826 the tracee stops with
827 .B PTRACE_EVENT_STOP
829 .I WSTOPSIG(status)
830 returns the stop signal.
831 If any other ptrace-stop is generated at the same time (for example,
832 if a signal is sent to the tracee), this ptrace-stop happens.
833 If none of the above applies (for example, if the tracee is running in user
834 space), it stops with
835 .B PTRACE_EVENT_STOP
836 with
837 .I WSTOPSIG(status)
839 .BR SIGTRAP .
840 .B PTRACE_INTERRUPT
841 only works on tracees attached by
842 .BR PTRACE_SEIZE .
844 .B PTRACE_ATTACH
845 Attach to the process specified in
846 .IR pid ,
847 making it a tracee of the calling process.
848 .\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
849 .\"        "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
850 .\"         Basically, it's not true for any Linux in practical use.
851 .\" ; the behavior of the tracee is as if it had done a
852 .\" .BR PTRACE_TRACEME .
853 .\" The calling process actually becomes the parent of the tracee
854 .\" process for most purposes (e.g., it will receive
855 .\" notification of tracee events and appears in
856 .\" .BR ps (1)
857 .\" output as the tracee's parent), but a
858 .\" .BR getppid (2)
859 .\" by the tracee will still return the PID of the original parent.
860 The tracee is sent a
861 .BR SIGSTOP ,
862 but will not necessarily have stopped
863 by the completion of this call; use
864 .BR waitpid (2)
865 to wait for the tracee to stop.
866 See the "Attaching and detaching" subsection for additional information.
867 .RI ( addr
869 .I data
870 are ignored.)
872 Permission to perform a
873 .BR PTRACE_ATTACH
874 is governed by a ptrace access mode
875 .B PTRACE_MODE_ATTACH_REALCREDS
876 check; see below.
878 .BR PTRACE_SEIZE " (since Linux 3.4)"
880 .\" Noted by Dmitry Levin:
882 .\"     PTRACE_SEIZE was introduced by commit v3.1-rc1~308^2~28, but
883 .\"     it had to be used along with a temporary flag PTRACE_SEIZE_DEVEL,
884 .\"     which was removed later by commit v3.4-rc1~109^2~20.
886 .\"     That is, [before] v3.4 we had a test mode of PTRACE_SEIZE API,
887 .\"     which was not compatible with the current PTRACE_SEIZE API introduced
888 .\"     in Linux 3.4.
890 Attach to the process specified in
891 .IR pid ,
892 making it a tracee of the calling process.
893 Unlike
894 .BR PTRACE_ATTACH ,
895 .B PTRACE_SEIZE
896 does not stop the process.
897 Group-stops are reported as
898 .B PTRACE_EVENT_STOP
900 .I WSTOPSIG(status)
901 returns the stop signal.
902 Automatically attached children stop with
903 .B PTRACE_EVENT_STOP
905 .I WSTOPSIG(status)
906 returns
907 .B SIGTRAP
908 instead of having
909 .B SIGSTOP
910 signal delivered to them.
911 .BR execve (2)
912 does not deliver an extra
913 .BR SIGTRAP .
914 Only a
915 .BR PTRACE_SEIZE d
916 process can accept
917 .B PTRACE_INTERRUPT
919 .B PTRACE_LISTEN
920 commands.
921 The "seized" behavior just described is inherited by
922 children that are automatically attached using
923 .BR PTRACE_O_TRACEFORK ,
924 .BR PTRACE_O_TRACEVFORK ,
926 .BR PTRACE_O_TRACECLONE .
927 .I addr
928 must be zero.
929 .I data
930 contains a bit mask of ptrace options to activate immediately.
932 Permission to perform a
933 .BR PTRACE_SEIZE
934 is governed by a ptrace access mode
935 .B PTRACE_MODE_ATTACH_REALCREDS
936 check; see below.
939 .BR PTRACE_SECCOMP_GET_FILTER " (since Linux 4.4)"
940 .\" commit f8e529ed941ba2bbcbf310b575d968159ce7e895
941 This operation allows the tracer to dump the tracee's
942 classic BPF filters.
944 .I addr
945 is an integer specifying the index of the filter to be dumped.
946 The most recently installed filter has the index 0.
948 .I addr
949 is greater than the number of installed filters,
950 the operation fails with the error
951 .BR ENOENT .
953 .I data
954 is either a pointer to a
955 .IR "struct sock_filter"
956 array that is large enough to store the BPF program,
957 or NULL if the program is not to be stored.
959 Upon success,
960 the return value is the number of instructions in the BPF program.
962 .I data
963 was NULL, then this return value can be used to correctly size the
964 .IR "struct sock_filter"
965 array passed in a subsequent call.
967 This operation fails with the error
968 .B EACCES
969 if the caller does not have the
970 .B CAP_SYS_ADMIN
971 capability or if the caller is in strict or filter seccomp mode.
972 If the filter referred to by
973 .I addr
974 is not a classic BPF filter, the operation fails with the error
975 .BR EMEDIUMTYPE .
977 This operation is available if the kernel was configured with both the
978 .B CONFIG_SECCOMP_FILTER
979 and the
980 .B CONFIG_CHECKPOINT_RESTORE
981 options.
983 .B PTRACE_DETACH
984 Restart the stopped tracee as for
985 .BR PTRACE_CONT ,
986 but first detach from it.
987 Under Linux, a tracee can be detached in this way regardless
988 of which method was used to initiate tracing.
989 .RI ( addr
990 is ignored.)
993 .BR PTRACE_GET_THREAD_AREA " (since Linux 2.6.0)"
994 This operation performs a similar task to
995 .BR get_thread_area (2).
996 It reads the TLS entry in the GDT whose index is given in
997 .IR addr ,
998 placing a copy of the entry into the
999 .IR "struct user_desc"
1000 pointed to by
1001 .IR data .
1002 (By contrast with
1003 .BR get_thread_area (2),
1005 .I entry_number
1006 of the
1007 .IR "struct user_desc"
1008 is ignored.)
1010 .BR PTRACE_SET_THREAD_AREA " (since Linux 2.6.0)"
1011 This operation performs a similar task to
1012 .BR set_thread_area (2).
1013 It sets the TLS entry in the GDT whose index is given in
1014 .IR addr ,
1015 assigning it the data supplied in the
1016 .IR "struct user_desc"
1017 pointed to by
1018 .IR data .
1019 (By contrast with
1020 .BR set_thread_area (2),
1022 .I entry_number
1023 of the
1024 .IR "struct user_desc"
1025 is ignored; in other words,
1026 this ptrace operation can't be used to allocate a free TLS entry.)
1028 .BR PTRACE_GET_SYSCALL_INFO " (since Linux 5.3)"
1029 .\" commit 201766a20e30f982ccfe36bebfad9602c3ff574a
1030 Retrieve information about the system call that caused the stop.
1031 The information is placed into the buffer pointed by the
1032 .I data
1033 argument, which should be a pointer to a buffer of type
1034 .IR "struct ptrace_syscall_info" .
1036 .I addr
1037 argument contains the size of the buffer pointed to
1038 by the
1039 .I data
1040 argument (i.e.,
1041 .IR "sizeof(struct ptrace_syscall_info)" ).
1042 The return value contains the number of bytes available
1043 to be written by the kernel.
1044 If the size of the data to be written by the kernel exceeds the size
1045 specified by the
1046 .I addr
1047 argument, the output data is truncated.
1050 .I ptrace_syscall_info
1051 structure contains the following fields:
1053 .in +4n
1055 struct ptrace_syscall_info {
1056     __u8 op;        /* Type of system call stop */
1057     __u32 arch;     /* AUDIT_ARCH_* value; see seccomp(2) */
1058     __u64 instruction_pointer; /* CPU instruction pointer */
1059     __u64 stack_pointer;    /* CPU stack pointer */
1060     union {
1061         struct {    /* op == PTRACE_SYSCALL_INFO_ENTRY */
1062             __u64 nr;       /* System call number */
1063             __u64 args[6];  /* System call arguments */
1064         } entry;
1065         struct {    /* op == PTRACE_SYSCALL_INFO_EXIT */
1066             __s64 rval;     /* System call return value */
1067             __u8 is_error;  /* System call error flag;
1068                                Boolean: does rval contain
1069                                an error value (\-ERRCODE) or
1070                                a nonerror return value? */
1071         } exit;
1072         struct {    /* op == PTRACE_SYSCALL_INFO_SECCOMP */
1073             __u64 nr;       /* System call number */
1074             __u64 args[6];  /* System call arguments */
1075             __u32 ret_data; /* SECCOMP_RET_DATA portion
1076                                of SECCOMP_RET_TRACE
1077                                return value */
1078         } seccomp;
1079     };
1085 .IR op ,
1086 .IR arch ,
1087 .IR instruction_pointer ,
1089 .I stack_pointer
1090 fields are defined for all kinds of ptrace system call stops.
1091 The rest of the structure is a union; one should read only those fields
1092 that are meaningful for the kind of system call stop specified by the
1093 .IR op
1094 field.
1097 .I op
1098 field has one of the following values (defined in
1099 .IR <linux/ptrace.h>)
1100 indicating what type of stop occurred and
1101 which part of the union is filled:
1104 .BR PTRACE_SYSCALL_INFO_ENTRY
1106 .I entry
1107 component of the union contains information relating to a
1108 system call entry stop.
1110 .BR PTRACE_SYSCALL_INFO_EXIT
1112 .I exit
1113 component of the union contains information relating to a
1114 system call exit stop.
1116 .BR PTRACE_SYSCALL_INFO_SECCOMP
1118 .I seccomp
1119 component of the union contains information relating to a
1120 .B PTRACE_EVENT_SECCOMP
1121 stop.
1123 .BR PTRACE_SYSCALL_INFO_NONE
1124 No component of the union contains relevant information.
1127 .SS Death under ptrace
1128 When a (possibly multithreaded) process receives a killing signal
1129 (one whose disposition is set to
1130 .B SIG_DFL
1131 and whose default action is to kill the process),
1132 all threads exit.
1133 Tracees report their death to their tracer(s).
1134 Notification of this event is delivered via
1135 .BR waitpid (2).
1137 Note that the killing signal will first cause signal-delivery-stop
1138 (on one tracee only),
1139 and only after it is injected by the tracer
1140 (or after it was dispatched to a thread which isn't traced),
1141 will death from the signal happen on
1142 .I all
1143 tracees within a multithreaded process.
1144 (The term "signal-delivery-stop" is explained below.)
1146 .B SIGKILL
1147 does not generate signal-delivery-stop and
1148 therefore the tracer can't suppress it.
1149 .B SIGKILL
1150 kills even within system calls
1151 (syscall-exit-stop is not generated prior to death by
1152 .BR SIGKILL ).
1153 The net effect is that
1154 .B SIGKILL
1155 always kills the process (all its threads),
1156 even if some threads of the process are ptraced.
1158 When the tracee calls
1159 .BR _exit (2),
1160 it reports its death to its tracer.
1161 Other threads are not affected.
1163 When any thread executes
1164 .BR exit_group (2),
1165 every tracee in its thread group reports its death to its tracer.
1167 If the
1168 .B PTRACE_O_TRACEEXIT
1169 option is on,
1170 .B PTRACE_EVENT_EXIT
1171 will happen before actual death.
1172 This applies to exits via
1173 .BR exit (2),
1174 .BR exit_group (2),
1175 and signal deaths (except
1176 .BR SIGKILL ,
1177 depending on the kernel version; see BUGS below),
1178 and when threads are torn down on
1179 .BR execve (2)
1180 in a multithreaded process.
1182 The tracer cannot assume that the ptrace-stopped tracee exists.
1183 There are many scenarios when the tracee may die while stopped (such as
1184 .BR SIGKILL ).
1185 Therefore, the tracer must be prepared to handle an
1186 .B ESRCH
1187 error on any ptrace operation.
1188 Unfortunately, the same error is returned if the tracee
1189 exists but is not ptrace-stopped
1190 (for commands which require a stopped tracee),
1191 or if it is not traced by the process which issued the ptrace call.
1192 The tracer needs to keep track of the stopped/running state of the tracee,
1193 and interpret
1194 .B ESRCH
1195 as "tracee died unexpectedly" only if it knows that the tracee has
1196 been observed to enter ptrace-stop.
1197 Note that there is no guarantee that
1198 .I waitpid(WNOHANG)
1199 will reliably report the tracee's death status if a
1200 ptrace operation returned
1201 .BR ESRCH .
1202 .I waitpid(WNOHANG)
1203 may return 0 instead.
1204 In other words, the tracee may be "not yet fully dead",
1205 but already refusing ptrace requests.
1207 The tracer can't assume that the tracee
1208 .I always
1209 ends its life by reporting
1210 .I WIFEXITED(status)
1212 .IR WIFSIGNALED(status) ;
1213 there are cases where this does not occur.
1214 For example, if a thread other than thread group leader does an
1215 .BR execve (2),
1216 it disappears;
1217 its PID will never be seen again,
1218 and any subsequent ptrace stops will be reported under
1219 the thread group leader's PID.
1220 .SS Stopped states
1221 A tracee can be in two states: running or stopped.
1222 For the purposes of ptrace, a tracee which is blocked in a system call
1223 (such as
1224 .BR read (2),
1225 .BR pause (2),
1226 etc.)
1227 is nevertheless considered to be running, even if the tracee is blocked
1228 for a long time.
1229 The state of the tracee after
1230 .BR PTRACE_LISTEN
1231 is somewhat of a gray area: it is not in any ptrace-stop (ptrace commands
1232 won't work on it, and it will deliver
1233 .BR waitpid (2)
1234 notifications),
1235 but it also may be considered "stopped" because
1236 it is not executing instructions (is not scheduled), and if it was
1237 in group-stop before
1238 .BR PTRACE_LISTEN ,
1239 it will not respond to signals until
1240 .B SIGCONT
1241 is received.
1243 There are many kinds of states when the tracee is stopped, and in ptrace
1244 discussions they are often conflated.
1245 Therefore, it is important to use precise terms.
1247 In this manual page, any stopped state in which the tracee is ready
1248 to accept ptrace commands from the tracer is called
1249 .IR ptrace-stop .
1250 Ptrace-stops can
1251 be further subdivided into
1252 .IR signal-delivery-stop ,
1253 .IR group-stop ,
1254 .IR syscall-stop ,
1255 .IR "PTRACE_EVENT stops" ,
1256 and so on.
1257 These stopped states are described in detail below.
1259 When the running tracee enters ptrace-stop, it notifies its tracer using
1260 .BR waitpid (2)
1261 (or one of the other "wait" system calls).
1262 Most of this manual page assumes that the tracer waits with:
1264     pid = waitpid(pid_or_minus_1, &status, __WALL);
1266 Ptrace-stopped tracees are reported as returns with
1267 .I pid
1268 greater than 0 and
1269 .I WIFSTOPPED(status)
1270 true.
1271 .\" Denys Vlasenko:
1272 .\"     Do we require __WALL usage, or will just using 0 be ok? (With 0,
1273 .\"     I am not 100% sure there aren't ugly corner cases.) Are the
1274 .\"     rules different if user wants to use waitid? Will waitid require
1275 .\"     WEXITED?
1279 .B __WALL
1280 flag does not include the
1281 .B WSTOPPED
1283 .B WEXITED
1284 flags, but implies their functionality.
1286 Setting the
1287 .B WCONTINUED
1288 flag when calling
1289 .BR waitpid (2)
1290 is not recommended: the "continued" state is per-process and
1291 consuming it can confuse the real parent of the tracee.
1293 Use of the
1294 .B WNOHANG
1295 flag may cause
1296 .BR waitpid (2)
1297 to return 0 ("no wait results available yet")
1298 even if the tracer knows there should be a notification.
1299 Example:
1301 .in +4n
1303 errno = 0;
1304 ptrace(PTRACE_CONT, pid, 0L, 0L);
1305 if (errno == ESRCH) {
1306     /* tracee is dead */
1307     r = waitpid(tracee, &status, __WALL | WNOHANG);
1308     /* r can still be 0 here! */
1312 .\" FIXME .
1313 .\"     waitid usage? WNOWAIT?
1314 .\"     describe how wait notifications queue (or not queue)
1316 The following kinds of ptrace-stops exist: signal-delivery-stops,
1317 group-stops,
1318 .B PTRACE_EVENT
1319 stops, syscall-stops.
1320 They all are reported by
1321 .BR waitpid (2)
1322 with
1323 .I WIFSTOPPED(status)
1324 true.
1325 They may be differentiated by examining the value
1326 .IR status>>8 ,
1327 and if there is ambiguity in that value, by querying
1328 .BR PTRACE_GETSIGINFO .
1329 (Note: the
1330 .I WSTOPSIG(status)
1331 macro can't be used to perform this examination,
1332 because it returns the value
1333 .IR "(status>>8)\ &\ 0xff" .)
1334 .SS Signal-delivery-stop
1335 When a (possibly multithreaded) process receives any signal except
1336 .BR SIGKILL ,
1337 the kernel selects an arbitrary thread which handles the signal.
1338 (If the signal is generated with
1339 .BR tgkill (2),
1340 the target thread can be explicitly selected by the caller.)
1341 If the selected thread is traced, it enters signal-delivery-stop.
1342 At this point, the signal is not yet delivered to the process,
1343 and can be suppressed by the tracer.
1344 If the tracer doesn't suppress the signal,
1345 it passes the signal to the tracee in the next ptrace restart request.
1346 This second step of signal delivery is called
1347 .I "signal injection"
1348 in this manual page.
1349 Note that if the signal is blocked,
1350 signal-delivery-stop doesn't happen until the signal is unblocked,
1351 with the usual exception that
1352 .B SIGSTOP
1353 can't be blocked.
1355 Signal-delivery-stop is observed by the tracer as
1356 .BR waitpid (2)
1357 returning with
1358 .I WIFSTOPPED(status)
1359 true, with the signal returned by
1360 .IR WSTOPSIG(status) .
1361 If the signal is
1362 .BR SIGTRAP ,
1363 this may be a different kind of ptrace-stop;
1364 see the "Syscall-stops" and "execve" sections below for details.
1366 .I WSTOPSIG(status)
1367 returns a stopping signal, this may be a group-stop; see below.
1368 .SS Signal injection and suppression
1369 After signal-delivery-stop is observed by the tracer,
1370 the tracer should restart the tracee with the call
1372     ptrace(PTRACE_restart, pid, 0, sig)
1374 where
1375 .B PTRACE_restart
1376 is one of the restarting ptrace requests.
1378 .I sig
1379 is 0, then a signal is not delivered.
1380 Otherwise, the signal
1381 .I sig
1382 is delivered.
1383 This operation is called
1384 .I "signal injection"
1385 in this manual page, to distinguish it from signal-delivery-stop.
1388 .I sig
1389 value may be different from the
1390 .I WSTOPSIG(status)
1391 value: the tracer can cause a different signal to be injected.
1393 Note that a suppressed signal still causes system calls to return
1394 prematurely.
1395 In this case, system calls will be restarted: the tracer will
1396 observe the tracee to reexecute the interrupted system call (or
1397 .BR restart_syscall (2)
1398 system call for a few system calls which use a different mechanism
1399 for restarting) if the tracer uses
1400 .BR PTRACE_SYSCALL .
1401 Even system calls (such as
1402 .BR poll (2))
1403 which are not restartable after signal are restarted after
1404 signal is suppressed;
1405 however, kernel bugs exist which cause some system calls to fail with
1406 .B EINTR
1407 even though no observable signal is injected to the tracee.
1409 Restarting ptrace commands issued in ptrace-stops other than
1410 signal-delivery-stop are not guaranteed to inject a signal, even if
1411 .I sig
1412 is nonzero.
1413 No error is reported; a nonzero
1414 .I sig
1415 may simply be ignored.
1416 Ptrace users should not try to "create a new signal" this way: use
1417 .BR tgkill (2)
1418 instead.
1420 The fact that signal injection requests may be ignored
1421 when restarting the tracee after
1422 ptrace stops that are not signal-delivery-stops
1423 is a cause of confusion among ptrace users.
1424 One typical scenario is that the tracer observes group-stop,
1425 mistakes it for signal-delivery-stop, restarts the tracee with
1427     ptrace(PTRACE_restart, pid, 0, stopsig)
1429 with the intention of injecting
1430 .IR stopsig ,
1432 .I stopsig
1433 gets ignored and the tracee continues to run.
1436 .B SIGCONT
1437 signal has a side effect of waking up (all threads of)
1438 a group-stopped process.
1439 This side effect happens before signal-delivery-stop.
1440 The tracer can't suppress this side effect (it can
1441 only suppress signal injection, which only causes the
1442 .BR SIGCONT
1443 handler to not be executed in the tracee, if such a handler is installed).
1444 In fact, waking up from group-stop may be followed by
1445 signal-delivery-stop for signal(s)
1446 .I other than
1447 .BR SIGCONT ,
1448 if they were pending when
1449 .B SIGCONT
1450 was delivered.
1451 In other words,
1452 .B SIGCONT
1453 may be not the first signal observed by the tracee after it was sent.
1455 Stopping signals cause (all threads of) a process to enter group-stop.
1456 This side effect happens after signal injection, and therefore can be
1457 suppressed by the tracer.
1459 In Linux 2.4 and earlier, the
1460 .B SIGSTOP
1461 signal can't be injected.
1462 .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
1463 .\" there is:
1465 .\"             /* The debugger continued.  Ignore SIGSTOP.  */
1466 .\"             if (signr == SIGSTOP)
1467 .\"                     continue;
1469 .B PTRACE_GETSIGINFO
1470 can be used to retrieve a
1471 .I siginfo_t
1472 structure which corresponds to the delivered signal.
1473 .B PTRACE_SETSIGINFO
1474 may be used to modify it.
1476 .B PTRACE_SETSIGINFO
1477 has been used to alter
1478 .IR siginfo_t ,
1480 .I si_signo
1481 field and the
1482 .I sig
1483 parameter in the restarting command must match,
1484 otherwise the result is undefined.
1485 .SS Group-stop
1486 When a (possibly multithreaded) process receives a stopping signal,
1487 all threads stop.
1488 If some threads are traced, they enter a group-stop.
1489 Note that the stopping signal will first cause signal-delivery-stop
1490 (on one tracee only), and only after it is injected by the tracer
1491 (or after it was dispatched to a thread which isn't traced),
1492 will group-stop be initiated on
1493 .I all
1494 tracees within the multithreaded process.
1495 As usual, every tracee reports its group-stop separately
1496 to the corresponding tracer.
1498 Group-stop is observed by the tracer as
1499 .BR waitpid (2)
1500 returning with
1501 .I WIFSTOPPED(status)
1502 true, with the stopping signal available via
1503 .IR WSTOPSIG(status) .
1504 The same result is returned by some other classes of ptrace-stops,
1505 therefore the recommended practice is to perform the call
1507     ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
1509 The call can be avoided if the signal is not
1510 .BR SIGSTOP ,
1511 .BR SIGTSTP ,
1512 .BR SIGTTIN ,
1514 .BR SIGTTOU ;
1515 only these four signals are stopping signals.
1516 If the tracer sees something else, it can't be a group-stop.
1517 Otherwise, the tracer needs to call
1518 .BR PTRACE_GETSIGINFO .
1520 .B PTRACE_GETSIGINFO
1521 fails with
1522 .BR EINVAL ,
1523 then it is definitely a group-stop.
1524 (Other failure codes are possible, such as
1525 .B ESRCH
1526 ("no such process") if a
1527 .B SIGKILL
1528 killed the tracee.)
1530 If tracee was attached using
1531 .BR PTRACE_SEIZE ,
1532 group-stop is indicated by
1533 .BR PTRACE_EVENT_STOP :
1534 .IR "status>>16 == PTRACE_EVENT_STOP" .
1535 This allows detection of group-stops
1536 without requiring an extra
1537 .B PTRACE_GETSIGINFO
1538 call.
1540 As of Linux 2.6.38,
1541 after the tracer sees the tracee ptrace-stop and until it
1542 restarts or kills it, the tracee will not run,
1543 and will not send notifications (except
1544 .B SIGKILL
1545 death) to the tracer, even if the tracer enters into another
1546 .BR waitpid (2)
1547 call.
1549 The kernel behavior described in the previous paragraph
1550 causes a problem with transparent handling of stopping signals.
1551 If the tracer restarts the tracee after group-stop,
1552 the stopping signal
1553 is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
1554 If the tracer doesn't restart the tracee before entering into the next
1555 .BR waitpid (2),
1556 future
1557 .B SIGCONT
1558 signals will not be reported to the tracer;
1559 this would cause the
1560 .B SIGCONT
1561 signals to have no effect on the tracee.
1563 Since Linux 3.4, there is a method to overcome this problem: instead of
1564 .BR PTRACE_CONT ,
1566 .B PTRACE_LISTEN
1567 command can be used to restart a tracee in a way where it does not execute,
1568 but waits for a new event which it can report via
1569 .BR waitpid (2)
1570 (such as when
1571 it is restarted by a
1572 .BR SIGCONT ).
1573 .SS PTRACE_EVENT stops
1574 If the tracer sets
1575 .B PTRACE_O_TRACE_*
1576 options, the tracee will enter ptrace-stops called
1577 .B PTRACE_EVENT
1578 stops.
1580 .B PTRACE_EVENT
1581 stops are observed by the tracer as
1582 .BR waitpid (2)
1583 returning with
1584 .IR WIFSTOPPED(status) ,
1586 .I WSTOPSIG(status)
1587 returns
1588 .BR SIGTRAP
1589 (or for
1590 .BR PTRACE_EVENT_STOP ,
1591 returns the stopping signal if tracee is in a group-stop).
1592 An additional bit is set in the higher byte of the status word:
1593 the value
1594 .I status>>8
1595 will be
1597     ((PTRACE_EVENT_foo<<8) | SIGTRAP).
1599 The following events exist:
1601 .B PTRACE_EVENT_VFORK
1602 Stop before return from
1603 .BR vfork (2)
1605 .BR clone (2)
1606 with the
1607 .B CLONE_VFORK
1608 flag.
1609 When the tracee is continued after this stop, it will wait for child to
1610 exit/exec before continuing its execution
1611 (in other words, the usual behavior on
1612 .BR vfork (2)).
1614 .B PTRACE_EVENT_FORK
1615 Stop before return from
1616 .BR fork (2)
1618 .BR clone (2)
1619 with the exit signal set to
1620 .BR SIGCHLD .
1622 .B PTRACE_EVENT_CLONE
1623 Stop before return from
1624 .BR clone (2).
1626 .B PTRACE_EVENT_VFORK_DONE
1627 Stop before return from
1628 .BR vfork (2)
1630 .BR clone (2)
1631 with the
1632 .B CLONE_VFORK
1633 flag,
1634 but after the child unblocked this tracee by exiting or execing.
1636 For all four stops described above,
1637 the stop occurs in the parent (i.e., the tracee),
1638 not in the newly created thread.
1639 .BR PTRACE_GETEVENTMSG
1640 can be used to retrieve the new thread's ID.
1642 .B PTRACE_EVENT_EXEC
1643 Stop before return from
1644 .BR execve (2).
1645 Since Linux 3.0,
1646 .BR PTRACE_GETEVENTMSG
1647 returns the former thread ID.
1649 .B PTRACE_EVENT_EXIT
1650 Stop before exit (including death from
1651 .BR exit_group (2)),
1652 signal death, or exit caused by
1653 .BR execve (2)
1654 in a multithreaded process.
1655 .B PTRACE_GETEVENTMSG
1656 returns the exit status.
1657 Registers can be examined
1658 (unlike when "real" exit happens).
1659 The tracee is still alive; it needs to be
1660 .BR PTRACE_CONT ed
1662 .BR PTRACE_DETACH ed
1663 to finish exiting.
1665 .B PTRACE_EVENT_STOP
1666 Stop induced by
1667 .B PTRACE_INTERRUPT
1668 command, or group-stop, or initial ptrace-stop when a new child is attached
1669 (only if attached using
1670 .BR PTRACE_SEIZE ).
1672 .B PTRACE_EVENT_SECCOMP
1673 Stop triggered by a
1674 .BR seccomp (2)
1675 rule on tracee syscall entry when
1676 .BR PTRACE_O_TRACESECCOMP
1677 has been set by the tracer.
1678 The seccomp event message data (from the
1679 .BR SECCOMP_RET_DATA
1680 portion of the seccomp filter rule) can be retrieved with
1681 .BR PTRACE_GETEVENTMSG .
1682 The semantics of this stop are described in
1683 detail in a separate section below.
1685 .B PTRACE_GETSIGINFO
1687 .B PTRACE_EVENT
1688 stops returns
1689 .B SIGTRAP
1691 .IR si_signo ,
1692 with
1693 .I si_code
1694 set to
1695 .IR "(event<<8)\ |\ SIGTRAP" .
1696 .SS Syscall-stops
1697 If the tracee was restarted by
1698 .BR PTRACE_SYSCALL
1700 .BR PTRACE_SYSEMU ,
1701 the tracee enters
1702 syscall-enter-stop just prior to entering any system call (which
1703 will not be executed if the restart was using
1704 .BR PTRACE_SYSEMU ,
1705 regardless of any change made to registers at this point or how the
1706 tracee is restarted after this stop).
1707 No matter which method caused the syscall-entry-stop,
1708 if the tracer restarts the tracee with
1709 .BR PTRACE_SYSCALL ,
1710 the tracee enters syscall-exit-stop when the system call is finished,
1711 or if it is interrupted by a signal.
1712 (That is, signal-delivery-stop never happens between syscall-enter-stop
1713 and syscall-exit-stop; it happens
1714 .I after
1715 syscall-exit-stop.).
1716 If the tracee is continued using any other method (including
1717 .BR PTRACE_SYSEMU ),
1718 no syscall-exit-stop occurs.
1719 Note that all mentions
1720 .BR PTRACE_SYSEMU
1721 apply equally to
1722 .BR PTRACE_SYSEMU_SINGLESTEP .
1724 However, even if the tracee was continued using
1725 .BR PTRACE_SYSCALL ,
1726 it is not guaranteed that the next stop will be a syscall-exit-stop.
1727 Other possibilities are that the tracee may stop in a
1728 .B PTRACE_EVENT
1729 stop (including seccomp stops), exit (if it entered
1730 .BR _exit (2)
1732 .BR exit_group (2)),
1733 be killed by
1734 .BR SIGKILL ,
1735 or die silently (if it is a thread group leader, the
1736 .BR execve (2)
1737 happened in another thread,
1738 and that thread is not traced by the same tracer;
1739 this situation is discussed later).
1741 Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1742 .BR waitpid (2)
1743 returning with
1744 .I WIFSTOPPED(status)
1745 true, and
1746 .I WSTOPSIG(status)
1747 giving
1748 .BR SIGTRAP .
1749 If the
1750 .B PTRACE_O_TRACESYSGOOD
1751 option was set by the tracer, then
1752 .I WSTOPSIG(status)
1753 will give the value
1754 .IR "(SIGTRAP\ |\ 0x80)" .
1756 Syscall-stops can be distinguished from signal-delivery-stop with
1757 .B SIGTRAP
1758 by querying
1759 .BR PTRACE_GETSIGINFO
1760 for the following cases:
1762 .IR si_code " <= 0"
1763 .B SIGTRAP
1764 was delivered as a result of a user-space action,
1765 for example, a system call
1766 .RB ( tgkill (2),
1767 .BR kill (2),
1768 .BR sigqueue (3),
1769 etc.),
1770 expiration of a POSIX timer,
1771 change of state on a POSIX message queue,
1772 or completion of an asynchronous I/O request.
1774 .IR si_code " == SI_KERNEL (0x80)"
1775 .B SIGTRAP
1776 was sent by the kernel.
1778 .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1779 This is a syscall-stop.
1781 However, syscall-stops happen very often (twice per system call),
1782 and performing
1783 .B PTRACE_GETSIGINFO
1784 for every syscall-stop may be somewhat expensive.
1786 Some architectures allow the cases to be distinguished
1787 by examining registers.
1788 For example, on x86,
1789 .I rax
1791 .RB \- ENOSYS
1792 in syscall-enter-stop.
1793 Since
1794 .B SIGTRAP
1795 (like any other signal) always happens
1796 .I after
1797 syscall-exit-stop,
1798 and at this point
1799 .I rax
1800 almost never contains
1801 .RB \- ENOSYS ,
1803 .B SIGTRAP
1804 looks like "syscall-stop which is not syscall-enter-stop";
1805 in other words, it looks like a
1806 "stray syscall-exit-stop" and can be detected this way.
1807 But such detection is fragile and is best avoided.
1809 Using the
1810 .B PTRACE_O_TRACESYSGOOD
1811 option is the recommended method to distinguish syscall-stops
1812 from other kinds of ptrace-stops,
1813 since it is reliable and does not incur a performance penalty.
1815 Syscall-enter-stop and syscall-exit-stop are
1816 indistinguishable from each other by the tracer.
1817 The tracer needs to keep track of the sequence of
1818 ptrace-stops in order to not misinterpret syscall-enter-stop as
1819 syscall-exit-stop or vice versa.
1820 In general, a syscall-enter-stop is
1821 always followed by syscall-exit-stop,
1822 .B PTRACE_EVENT
1823 stop, or the tracee's death;
1824 no other kinds of ptrace-stop can occur in between.
1825 However, note that seccomp stops (see below) can cause syscall-exit-stops,
1826 without preceding syscall-entry-stops.
1827 If seccomp is in use, care needs
1828 to be taken not to misinterpret such stops as syscall-entry-stops.
1830 If after syscall-enter-stop,
1831 the tracer uses a restarting command other than
1832 .BR PTRACE_SYSCALL ,
1833 syscall-exit-stop is not generated.
1835 .B PTRACE_GETSIGINFO
1836 on syscall-stops returns
1837 .B SIGTRAP
1839 .IR si_signo ,
1840 with
1841 .I si_code
1842 set to
1843 .B SIGTRAP
1845 .IR (SIGTRAP|0x80) .
1847 .SS PTRACE_EVENT_SECCOMP stops (Linux 3.5 to 4.7)
1848 The behavior of
1849 .BR PTRACE_EVENT_SECCOMP
1850 stops and their interaction with other kinds
1851 of ptrace stops has changed between kernel versions.
1852 This documents the behavior
1853 from their introduction until Linux 4.7 (inclusive).
1854 The behavior in later kernel versions is documented in the next section.
1857 .BR PTRACE_EVENT_SECCOMP
1858 stop occurs whenever a
1859 .BR SECCOMP_RET_TRACE
1860 rule is triggered.
1861 This is independent of which methods was used to restart the system call.
1862 Notably, seccomp still runs even if the tracee was restarted using
1863 .BR PTRACE_SYSEMU
1864 and this system call is unconditionally skipped.
1866 Restarts from this stop will behave as if the stop had occurred right
1867 before the system call in question.
1868 In particular, both
1869 .BR PTRACE_SYSCALL
1871 .BR PTRACE_SYSEMU
1872 will normally cause a subsequent syscall-entry-stop.
1873 However, if after the
1874 .BR PTRACE_EVENT_SECCOMP
1875 the system call number is negative,
1876 both the syscall-entry-stop and the system call itself will be skipped.
1877 This means that if the system call number is negative after a
1878 .BR PTRACE_EVENT_SECCOMP
1879 and the tracee is restarted using
1880 .BR PTRACE_SYSCALL ,
1881 the next observed stop will be a syscall-exit-stop,
1882 rather than the syscall-entry-stop that might have been expected.
1884 .SS PTRACE_EVENT_SECCOMP stops (since Linux 4.8)
1885 Starting with Linux 4.8,
1886 .\" commit 93e35efb8de45393cf61ed07f7b407629bf698ea
1888 .BR PTRACE_EVENT_SECCOMP
1889 stop was reordered to occur between syscall-entry-stop and
1890 syscall-exit-stop.
1891 Note that seccomp no longer runs (and no
1892 .B PTRACE_EVENT_SECCOMP
1893 will be reported) if the system call is skipped due to
1894 .BR PTRACE_SYSEMU .
1896 Functionally, a
1897 .B PTRACE_EVENT_SECCOMP
1898 stop functions comparably
1899 to a syscall-entry-stop (i.e., continuations using
1900 .BR PTRACE_SYSCALL
1901 will cause syscall-exit-stops,
1902 the system call number may be changed and any other modified registers
1903 are visible to the to-be-executed system call as well).
1904 Note that there may be,
1905 but need not have been a preceding syscall-entry-stop.
1907 After a
1908 .BR PTRACE_EVENT_SECCOMP
1909 stop, seccomp will be rerun, with a
1910 .BR SECCOMP_RET_TRACE
1911 rule now functioning the same as a
1912 .BR SECCOMP_RET_ALLOW .
1913 Specifically, this means that if registers are not modified during the
1914 .BR PTRACE_EVENT_SECCOMP
1915 stop, the system call will then be allowed.
1917 .SS PTRACE_SINGLESTEP stops
1918 [Details of these kinds of stops are yet to be documented.]
1920 .\" FIXME .
1921 .\" document stops occurring with PTRACE_SINGLESTEP
1923 .SS Informational and restarting ptrace commands
1924 Most ptrace commands (all except
1925 .BR PTRACE_ATTACH ,
1926 .BR PTRACE_SEIZE ,
1927 .BR PTRACE_TRACEME ,
1928 .BR PTRACE_INTERRUPT ,
1930 .BR PTRACE_KILL )
1931 require the tracee to be in a ptrace-stop, otherwise they fail with
1932 .BR ESRCH .
1934 When the tracee is in ptrace-stop,
1935 the tracer can read and write data to
1936 the tracee using informational commands.
1937 These commands leave the tracee in ptrace-stopped state:
1939 .in +4n
1941 ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1942 ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1943 ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1944 ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1945 ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov);
1946 ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov);
1947 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1948 ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1949 ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1950 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1954 Note that some errors are not reported.
1955 For example, setting signal information
1956 .RI ( siginfo )
1957 may have no effect in some ptrace-stops, yet the call may succeed
1958 (return 0 and not set
1959 .IR errno );
1960 querying
1961 .B PTRACE_GETEVENTMSG
1962 may succeed and return some random value if current ptrace-stop
1963 is not documented as returning a meaningful event message.
1965 The call
1967     ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1969 affects one tracee.
1970 The tracee's current flags are replaced.
1971 Flags are inherited by new tracees created and "auto-attached" via active
1972 .BR PTRACE_O_TRACEFORK ,
1973 .BR PTRACE_O_TRACEVFORK ,
1975 .BR PTRACE_O_TRACECLONE
1976 options.
1978 Another group of commands makes the ptrace-stopped tracee run.
1979 They have the form:
1981     ptrace(cmd, pid, 0, sig);
1983 where
1984 .I cmd
1986 .BR PTRACE_CONT ,
1987 .BR PTRACE_LISTEN ,
1988 .BR PTRACE_DETACH ,
1989 .BR PTRACE_SYSCALL ,
1990 .BR PTRACE_SINGLESTEP ,
1991 .BR PTRACE_SYSEMU ,
1993 .BR PTRACE_SYSEMU_SINGLESTEP .
1994 If the tracee is in signal-delivery-stop,
1995 .I sig
1996 is the signal to be injected (if it is nonzero).
1997 Otherwise,
1998 .I sig
1999 may be ignored.
2000 (When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
2001 recommended practice is to always pass 0 in
2002 .IR sig .)
2003 .SS Attaching and detaching
2004 A thread can be attached to the tracer using the call
2006     ptrace(PTRACE_ATTACH, pid, 0, 0);
2010     ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags);
2012 .B PTRACE_ATTACH
2013 sends
2014 .B SIGSTOP
2015 to this thread.
2016 If the tracer wants this
2017 .B SIGSTOP
2018 to have no effect, it needs to suppress it.
2019 Note that if other signals are concurrently sent to
2020 this thread during attach,
2021 the tracer may see the tracee enter signal-delivery-stop
2022 with other signal(s) first!
2023 The usual practice is to reinject these signals until
2024 .B SIGSTOP
2025 is seen, then suppress
2026 .B SIGSTOP
2027 injection.
2028 The design bug here is that a ptrace attach and a concurrently delivered
2029 .B SIGSTOP
2030 may race and the concurrent
2031 .B SIGSTOP
2032 may be lost.
2034 .\" FIXME Describe how to attach to a thread which is already group-stopped.
2036 Since attaching sends
2037 .B SIGSTOP
2038 and the tracer usually suppresses it, this may cause a stray
2039 .B EINTR
2040 return from the currently executing system call in the tracee,
2041 as described in the "Signal injection and suppression" section.
2043 Since Linux 3.4,
2044 .B PTRACE_SEIZE
2045 can be used instead of
2046 .BR PTRACE_ATTACH .
2047 .B PTRACE_SEIZE
2048 does not stop the attached process.
2049 If you need to stop
2050 it after attach (or at any other time) without sending it any signals,
2052 .B PTRACE_INTERRUPT
2053 command.
2055 The request
2057     ptrace(PTRACE_TRACEME, 0, 0, 0);
2059 turns the calling thread into a tracee.
2060 The thread continues to run (doesn't enter ptrace-stop).
2061 A common practice is to follow the
2062 .B PTRACE_TRACEME
2063 with
2065     raise(SIGSTOP);
2067 and allow the parent (which is our tracer now) to observe our
2068 signal-delivery-stop.
2070 If the
2071 .BR PTRACE_O_TRACEFORK ,
2072 .BR PTRACE_O_TRACEVFORK ,
2074 .BR PTRACE_O_TRACECLONE
2075 options are in effect, then children created by, respectively,
2076 .BR vfork (2)
2078 .BR clone (2)
2079 with the
2080 .B CLONE_VFORK
2081 flag,
2082 .BR fork (2)
2084 .BR clone (2)
2085 with the exit signal set to
2086 .BR SIGCHLD ,
2087 and other kinds of
2088 .BR clone (2),
2089 are automatically attached to the same tracer which traced their parent.
2090 .B SIGSTOP
2091 is delivered to the children, causing them to enter
2092 signal-delivery-stop after they exit the system call which created them.
2094 Detaching of the tracee is performed by:
2096     ptrace(PTRACE_DETACH, pid, 0, sig);
2098 .B PTRACE_DETACH
2099 is a restarting operation;
2100 therefore it requires the tracee to be in ptrace-stop.
2101 If the tracee is in signal-delivery-stop, a signal can be injected.
2102 Otherwise, the
2103 .I sig
2104 parameter may be silently ignored.
2106 If the tracee is running when the tracer wants to detach it,
2107 the usual solution is to send
2108 .B SIGSTOP
2109 (using
2110 .BR tgkill (2),
2111 to make sure it goes to the correct thread),
2112 wait for the tracee to stop in signal-delivery-stop for
2113 .B SIGSTOP
2114 and then detach it (suppressing
2115 .B SIGSTOP
2116 injection).
2117 A design bug is that this can race with concurrent
2118 .BR SIGSTOP s.
2119 Another complication is that the tracee may enter other ptrace-stops
2120 and needs to be restarted and waited for again, until
2121 .B SIGSTOP
2122 is seen.
2123 Yet another complication is to be sure that
2124 the tracee is not already ptrace-stopped,
2125 because no signal delivery happens while it is\(emnot even
2126 .BR SIGSTOP .
2127 .\" FIXME Describe how to detach from a group-stopped tracee so that it
2128 .\" doesn't run, but continues to wait for SIGCONT.
2130 If the tracer dies, all tracees are automatically detached and restarted,
2131 unless they were in group-stop.
2132 Handling of restart from group-stop is currently buggy,
2133 but the "as planned" behavior is to leave tracee stopped and waiting for
2134 .BR SIGCONT .
2135 If the tracee is restarted from signal-delivery-stop,
2136 the pending signal is injected.
2137 .SS execve(2) under ptrace
2138 .\" clone(2) CLONE_THREAD says:
2139 .\"     If  any  of the threads in a thread group performs an execve(2),
2140 .\"     then all threads other than the thread group leader are terminated,
2141 .\"     and the new program is executed in the thread group leader.
2143 When one thread in a multithreaded process calls
2144 .BR execve (2),
2145 the kernel destroys all other threads in the process,
2146 .\" In kernel 3.1 sources, see fs/exec.c::de_thread()
2147 and resets the thread ID of the execing thread to the
2148 thread group ID (process ID).
2149 (Or, to put things another way, when a multithreaded process does an
2150 .BR execve (2),
2151 at completion of the call, it appears as though the
2152 .BR execve (2)
2153 occurred in the thread group leader, regardless of which thread did the
2154 .BR execve (2).)
2155 This resetting of the thread ID looks very confusing to tracers:
2156 .IP * 3
2157 All other threads stop in
2158 .B PTRACE_EVENT_EXIT
2159 stop, if the
2160 .BR PTRACE_O_TRACEEXIT
2161 option was turned on.
2162 Then all other threads except the thread group leader report
2163 death as if they exited via
2164 .BR _exit (2)
2165 with exit code 0.
2166 .IP *
2167 The execing tracee changes its thread ID while it is in the
2168 .BR execve (2).
2169 (Remember, under ptrace, the "pid" returned from
2170 .BR waitpid (2),
2171 or fed into ptrace calls, is the tracee's thread ID.)
2172 That is, the tracee's thread ID is reset to be the same as its process ID,
2173 which is the same as the thread group leader's thread ID.
2174 .IP *
2175 Then a
2176 .B PTRACE_EVENT_EXEC
2177 stop happens, if the
2178 .BR PTRACE_O_TRACEEXEC
2179 option was turned on.
2180 .IP *
2181 If the thread group leader has reported its
2182 .B PTRACE_EVENT_EXIT
2183 stop by this time,
2184 it appears to the tracer that
2185 the dead thread leader "reappears from nowhere".
2186 (Note: the thread group leader does not report death via
2187 .I WIFEXITED(status)
2188 until there is at least one other live thread.
2189 This eliminates the possibility that the tracer will see
2190 it dying and then reappearing.)
2191 If the thread group leader was still alive,
2192 for the tracer this may look as if thread group leader
2193 returns from a different system call than it entered,
2194 or even "returned from a system call even though
2195 it was not in any system call".
2196 If the thread group leader was not traced
2197 (or was traced by a different tracer), then during
2198 .BR execve (2)
2199 it will appear as if it has become a tracee of
2200 the tracer of the execing tracee.
2202 All of the above effects are the artifacts of
2203 the thread ID change in the tracee.
2206 .B PTRACE_O_TRACEEXEC
2207 option is the recommended tool for dealing with this situation.
2208 First, it enables
2209 .BR PTRACE_EVENT_EXEC
2210 stop,
2211 which occurs before
2212 .BR execve (2)
2213 returns.
2214 In this stop, the tracer can use
2215 .B PTRACE_GETEVENTMSG
2216 to retrieve the tracee's former thread ID.
2217 (This feature was introduced in Linux 3.0.)
2218 Second, the
2219 .B PTRACE_O_TRACEEXEC
2220 option disables legacy
2221 .B SIGTRAP
2222 generation on
2223 .BR execve (2).
2225 When the tracer receives
2226 .B PTRACE_EVENT_EXEC
2227 stop notification,
2228 it is guaranteed that except this tracee and the thread group leader,
2229 no other threads from the process are alive.
2231 On receiving the
2232 .B PTRACE_EVENT_EXEC
2233 stop notification,
2234 the tracer should clean up all its internal
2235 data structures describing the threads of this process,
2236 and retain only one data structure\(emone which
2237 describes the single still running tracee, with
2239     thread ID == thread group ID == process ID.
2241 Example: two threads call
2242 .BR execve (2)
2243 at the same time:
2246 *** we get syscall-enter-stop in thread 1: **
2247 PID1 execve("/bin/foo", "foo" <unfinished ...>
2248 *** we issue PTRACE_SYSCALL for thread 1 **
2249 *** we get syscall-enter-stop in thread 2: **
2250 PID2 execve("/bin/bar", "bar" <unfinished ...>
2251 *** we issue PTRACE_SYSCALL for thread 2 **
2252 *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
2253 *** we get syscall-exit-stop for PID0: **
2254 PID0 <... execve resumed> )             = 0
2257 If the
2258 .B PTRACE_O_TRACEEXEC
2259 option is
2260 .I not
2261 in effect for the execing tracee,
2262 and if the tracee was
2263 .BR PTRACE_ATTACH ed
2264 rather that
2265 .BR PTRACE_SEIZE d,
2266 the kernel delivers an extra
2267 .B SIGTRAP
2268 to the tracee after
2269 .BR execve (2)
2270 returns.
2271 This is an ordinary signal (similar to one which can be
2272 generated by
2273 .IR "kill \-TRAP" ),
2274 not a special kind of ptrace-stop.
2275 Employing
2276 .B PTRACE_GETSIGINFO
2277 for this signal returns
2278 .I si_code
2279 set to 0
2280 .RI ( SI_USER ).
2281 This signal may be blocked by signal mask,
2282 and thus may be delivered (much) later.
2284 Usually, the tracer (for example,
2285 .BR strace (1))
2286 would not want to show this extra post-execve
2287 .B SIGTRAP
2288 signal to the user, and would suppress its delivery to the tracee (if
2289 .B SIGTRAP
2290 is set to
2291 .BR SIG_DFL ,
2292 it is a killing signal).
2293 However, determining
2294 .I which
2295 .B SIGTRAP
2296 to suppress is not easy.
2297 Setting the
2298 .B PTRACE_O_TRACEEXEC
2299 option or using
2300 .B PTRACE_SEIZE
2301 and thus suppressing this extra
2302 .B SIGTRAP
2303 is the recommended approach.
2304 .SS Real parent
2305 The ptrace API (ab)uses the standard UNIX parent/child signaling over
2306 .BR waitpid (2).
2307 This used to cause the real parent of the process to stop receiving
2308 several kinds of
2309 .BR waitpid (2)
2310 notifications when the child process is traced by some other process.
2312 Many of these bugs have been fixed, but as of Linux 2.6.38 several still
2313 exist; see BUGS below.
2315 As of Linux 2.6.38, the following is believed to work correctly:
2316 .IP * 3
2317 exit/death by signal is reported first to the tracer, then,
2318 when the tracer consumes the
2319 .BR waitpid (2)
2320 result, to the real parent (to the real parent only when the
2321 whole multithreaded process exits).
2322 If the tracer and the real parent are the same process,
2323 the report is sent only once.
2324 .SH RETURN VALUE
2325 On success, the
2326 .B PTRACE_PEEK*
2327 requests return the requested data (but see NOTES),
2329 .B PTRACE_SECCOMP_GET_FILTER
2330 request returns the number of instructions in the BPF program,
2332 .B PTRACE_GET_SYSCALL_INFO
2333 request returns the number of bytes available to be written by the kernel,
2334 and other requests return zero.
2336 On error, all requests return \-1, and
2337 .I errno
2338 is set to indicate the error.
2339 Since the value returned by a successful
2340 .B PTRACE_PEEK*
2341 request may be \-1, the caller must clear
2342 .I errno
2343 before the call, and then check it afterward
2344 to determine whether or not an error occurred.
2345 .SH ERRORS
2347 .B EBUSY
2348 (i386 only) There was an error with allocating or freeing a debug register.
2350 .B EFAULT
2351 There was an attempt to read from or write to an invalid area in
2352 the tracer's or the tracee's memory,
2353 probably because the area wasn't mapped or accessible.
2354 Unfortunately, under Linux, different variations of this fault
2355 will return
2356 .B EIO
2358 .B EFAULT
2359 more or less arbitrarily.
2361 .B EINVAL
2362 An attempt was made to set an invalid option.
2364 .B EIO
2365 .I request
2366 is invalid, or an attempt was made to read from or
2367 write to an invalid area in the tracer's or the tracee's memory,
2368 or there was a word-alignment violation,
2369 or an invalid signal was specified during a restart request.
2371 .B EPERM
2372 The specified process cannot be traced.
2373 This could be because the
2374 tracer has insufficient privileges (the required capability is
2375 .BR CAP_SYS_PTRACE );
2376 unprivileged processes cannot trace processes that they
2377 cannot send signals to or those running
2378 set-user-ID/set-group-ID programs, for obvious reasons.
2379 Alternatively, the process may already be being traced,
2380 or (on kernels before 2.6.26) be
2381 .BR init (1)
2382 (PID 1).
2384 .B ESRCH
2385 The specified process does not exist, or is not currently being traced
2386 by the caller, or is not stopped
2387 (for requests that require a stopped tracee).
2388 .SH CONFORMING TO
2389 SVr4, 4.3BSD.
2390 .SH NOTES
2391 Although arguments to
2392 .BR ptrace ()
2393 are interpreted according to the prototype given,
2394 glibc currently declares
2395 .BR ptrace ()
2396 as a variadic function with only the
2397 .I request
2398 argument fixed.
2399 It is recommended to always supply four arguments,
2400 even if the requested operation does not use them,
2401 setting unused/ignored arguments to
2402 .I 0L
2404 .IR "(void\ *)\ 0".
2406 In Linux kernels before 2.6.26,
2407 .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
2408 .BR init (1),
2409 the process with PID 1, may not be traced.
2411 A tracees parent continues to be the tracer even if that tracer calls
2412 .BR execve (2).
2414 The layout of the contents of memory and the USER area are
2415 quite operating-system- and architecture-specific.
2416 The offset supplied, and the data returned,
2417 might not entirely match with the definition of
2418 .IR "struct user" .
2419 .\" See http://lkml.org/lkml/2008/5/8/375
2421 The size of a "word" is determined by the operating-system variant
2422 (e.g., for 32-bit Linux it is 32 bits).
2424 This page documents the way the
2425 .BR ptrace ()
2426 call works currently in Linux.
2427 Its behavior differs significantly on other flavors of UNIX.
2428 In any case, use of
2429 .BR ptrace ()
2430 is highly specific to the operating system and architecture.
2432 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2434 .SS Ptrace access mode checking
2435 Various parts of the kernel-user-space API (not just
2436 .BR ptrace ()
2437 operations), require so-called "ptrace access mode" checks,
2438 whose outcome determines whether an operation is permitted
2439 (or, in a few cases, causes a "read" operation to return sanitized data).
2440 These checks are performed in cases where one process can
2441 inspect sensitive information about,
2442 or in some cases modify the state of, another process.
2443 The checks are based on factors such as the credentials and capabilities
2444 of the two processes,
2445 whether or not the "target" process is dumpable,
2446 and the results of checks performed by any enabled Linux Security Module
2447 (LSM)\(emfor example, SELinux, Yama, or Smack\(emand by the commoncap LSM
2448 (which is always invoked).
2450 Prior to Linux 2.6.27, all access checks were of a single type.
2451 Since Linux 2.6.27,
2452 .\" commit 006ebb40d3d65338bd74abb03b945f8d60e362bd
2453 two access mode levels are distinguished:
2455 .BR PTRACE_MODE_READ
2456 For "read" operations or other operations that are less dangerous,
2457 such as:
2458 .BR get_robust_list (2);
2459 .BR kcmp (2);
2460 reading
2461 .IR /proc/[pid]/auxv ,
2462 .IR /proc/[pid]/environ ,
2464 .IR /proc/[pid]/stat ;
2466 .BR readlink (2)
2467 of a
2468 .IR /proc/[pid]/ns/*
2469 file.
2471 .BR PTRACE_MODE_ATTACH
2472 For "write" operations, or other operations that are more dangerous,
2473 such as: ptrace attaching
2474 .RB ( PTRACE_ATTACH )
2475 to another process
2476 or calling
2477 .BR process_vm_writev (2).
2478 .RB ( PTRACE_MODE_ATTACH
2479 was effectively the default before Linux 2.6.27.)
2481 .\" Regarding the above description of the distinction between
2482 .\" PTRACE_MODE_READ and PTRACE_MODE_ATTACH, Stephen Smalley notes:
2484 .\"     That was the intent when the distinction was introduced, but it doesn't
2485 .\"     appear to have been properly maintained, e.g. there is now a common
2486 .\"     helper lock_trace() that is used for
2487 .\"     /proc/pid/{stack,syscall,personality} but checks PTRACE_MODE_ATTACH, and
2488 .\"     PTRACE_MODE_ATTACH is also used in timerslack_ns_write/show().  Likely
2489 .\"     should review and make them consistent.  There was also some debate
2490 .\"     about proper handling of /proc/pid/fd.  Arguably that one might belong
2491 .\"     back in the _ATTACH camp.
2494 Since Linux 4.5,
2495 .\" commit caaee6234d05a58c5b4d05e7bf766131b810a657
2496 the above access mode checks are combined (ORed) with
2497 one of the following modifiers:
2499 .B PTRACE_MODE_FSCREDS
2500 Use the caller's filesystem UID and GID (see
2501 .BR credentials (7))
2502 or effective capabilities for LSM checks.
2504 .B PTRACE_MODE_REALCREDS
2505 Use the caller's real UID and GID or permitted capabilities for LSM checks.
2506 This was effectively the default before Linux 4.5.
2508 Because combining one of the credential modifiers with one of
2509 the aforementioned access modes is typical,
2510 some macros are defined in the kernel sources for the combinations:
2512 .B PTRACE_MODE_READ_FSCREDS
2513 Defined as
2514 .BR "PTRACE_MODE_READ | PTRACE_MODE_FSCREDS" .
2516 .B PTRACE_MODE_READ_REALCREDS
2517 Defined as
2518 .BR "PTRACE_MODE_READ | PTRACE_MODE_REALCREDS" .
2520 .B PTRACE_MODE_ATTACH_FSCREDS
2521 Defined as
2522 .BR "PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS" .
2524 .B PTRACE_MODE_ATTACH_REALCREDS
2525 Defined as
2526 .BR "PTRACE_MODE_ATTACH | PTRACE_MODE_REALCREDS" .
2528 One further modifier can be ORed with the access mode:
2530 .BR PTRACE_MODE_NOAUDIT " (since Linux 3.3)"
2531 .\" commit 69f594a38967f4540ce7a29b3fd214e68a8330bd
2532 .\" Just for /proc/pid/stat
2533 Don't audit this access mode check.
2534 This modifier is employed for ptrace access mode checks
2535 (such as checks when reading
2536 .IR /proc/[pid]/stat )
2537 that merely cause the output to be filtered or sanitized,
2538 rather than causing an error to be returned to the caller.
2539 In these cases, accessing the file is not a security violation and
2540 there is no reason to generate a security audit record.
2541 This modifier suppresses the generation of
2542 such an audit record for the particular access check.
2544 Note that all of the
2545 .BR PTRACE_MODE_*
2546 constants described in this subsection are kernel-internal,
2547 and not visible to user space.
2548 The constant names are mentioned here in order to label the various kinds of
2549 ptrace access mode checks that are performed for various system calls
2550 and accesses to various pseudofiles (e.g., under
2551 .IR /proc ).
2552 These names are used in other manual pages to provide a simple
2553 shorthand for labeling the different kernel checks.
2555 The algorithm employed for ptrace access mode checking determines whether
2556 the calling process is allowed to perform the corresponding action
2557 on the target process.
2558 (In the case of opening
2559 .IR /proc/[pid]
2560 files, the "calling process" is the one opening the file,
2561 and the process with the corresponding PID is the "target process".)
2562 The algorithm is as follows:
2563 .IP 1. 3
2564 If the calling thread and the target thread are in the same
2565 thread group, access is always allowed.
2566 .IP 2.
2567 If the access mode specifies
2568 .BR PTRACE_MODE_FSCREDS ,
2569 then, for the check in the next step,
2570 employ the caller's filesystem UID and GID.
2571 (As noted in
2572 .BR credentials (7),
2573 the filesystem UID and GID almost always have the same values
2574 as the corresponding effective IDs.)
2576 Otherwise, the access mode specifies
2577 .BR PTRACE_MODE_REALCREDS ,
2578 so use the caller's real UID and GID for the checks in the next step.
2579 (Most APIs that check the caller's UID and GID use the effective IDs.
2580 For historical reasons, the
2581 .BR PTRACE_MODE_REALCREDS
2582 check uses the real IDs instead.)
2583 .IP 3.
2584 Deny access if
2585 .I neither
2586 of the following is true:
2588 .IP \(bu 2
2589 The real, effective, and saved-set user IDs of the target
2590 match the caller's user ID,
2591 .IR and
2592 the real, effective, and saved-set group IDs of the target
2593 match the caller's group ID.
2594 .IP \(bu
2595 The caller has the
2596 .B CAP_SYS_PTRACE
2597 capability in the user namespace of the target.
2599 .IP 4.
2600 Deny access if the target process "dumpable" attribute has a value other than 1
2601 .RB ( SUID_DUMP_USER ;
2602 see the discussion of
2603 .BR PR_SET_DUMPABLE
2605 .BR prctl (2)),
2606 and the caller does not have the
2607 .BR CAP_SYS_PTRACE
2608 capability in the user namespace of the target process.
2609 .IP 5.
2610 The kernel LSM
2611 .IR security_ptrace_access_check ()
2612 interface is invoked to see if ptrace access is permitted.
2613 The results depend on the LSM(s).
2614 The implementation of this interface in the commoncap LSM performs
2615 the following steps:
2616 .\" (in cap_ptrace_access_check()):
2618 .IP a) 3
2619 If the access mode includes
2620 .BR PTRACE_MODE_FSCREDS ,
2621 then use the caller's
2622 .I effective
2623 capability set
2624 in the following check;
2625 otherwise (the access mode specifies
2626 .BR PTRACE_MODE_REALCREDS ,
2627 so) use the caller's
2628 .I permitted
2629 capability set.
2630 .IP b)
2631 Deny access if
2632 .I neither
2633 of the following is true:
2635 .IP \(bu 2
2636 The caller and the target process are in the same user namespace,
2637 and the caller's capabilities are a superset of the target process's
2638 .I permitted
2639 capabilities.
2640 .IP \(bu
2641 The caller has the
2642 .B CAP_SYS_PTRACE
2643 capability in the target process's user namespace.
2646 Note that the commoncap LSM does not distinguish between
2647 .B PTRACE_MODE_READ
2649 .BR PTRACE_MODE_ATTACH .
2651 .IP 6.
2652 If access has not been denied by any of the preceding steps,
2653 then access is allowed.
2655 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2657 .SS /proc/sys/kernel/yama/ptrace_scope
2658 On systems with the Yama Linux Security Module (LSM) installed
2659 (i.e., the kernel was configured with
2660 .BR CONFIG_SECURITY_YAMA ),
2662 .I /proc/sys/kernel/yama/ptrace_scope
2663 file (available since Linux 3.4)
2664 .\" commit 2d514487faf188938a4ee4fb3464eeecfbdcf8eb
2665 can be used to restrict the ability to trace a process with
2666 .BR ptrace ()
2667 (and thus also the ability to use tools such as
2668 .BR strace (1)
2670 .BR gdb (1)).
2671 The goal of such restrictions is to prevent attack escalation whereby
2672 a compromised process can ptrace-attach to other sensitive processes
2673 (e.g., a GPG agent or an SSH session) owned by the user in order
2674 to gain additional credentials that may exist in memory
2675 and thus expand the scope of the attack.
2677 More precisely, the Yama LSM limits two types of operations:
2678 .IP * 3
2679 Any operation that performs a ptrace access mode
2680 .BR PTRACE_MODE_ATTACH
2681 check\(emfor example,
2682 .BR ptrace ()
2683 .BR PTRACE_ATTACH .
2684 (See the "Ptrace access mode checking" discussion above.)
2685 .IP *
2686 .BR ptrace ()
2687 .BR PTRACE_TRACEME .
2689 A process that has the
2690 .B CAP_SYS_PTRACE
2691 capability can update the
2692 .IR /proc/sys/kernel/yama/ptrace_scope
2693 file with one of the following values:
2695 0 ("classic ptrace permissions")
2696 No additional restrictions on operations that perform
2697 .BR PTRACE_MODE_ATTACH
2698 checks (beyond those imposed by the commoncap and other LSMs).
2700 The use of
2701 .BR PTRACE_TRACEME
2702 is unchanged.
2704 1 ("restricted ptrace") [default value]
2705 When performing an operation that requires a
2706 .BR PTRACE_MODE_ATTACH
2707 check, the calling process must either have the
2708 .B CAP_SYS_PTRACE
2709 capability in the user namespace of the target process or
2710 it must have a predefined relationship with the target process.
2711 By default,
2712 the predefined relationship is that the target process
2713 must be a descendant of the caller.
2715 A target process can employ the
2716 .BR prctl (2)
2717 .B PR_SET_PTRACER
2718 operation to declare an additional PID that is allowed to perform
2719 .BR PTRACE_MODE_ATTACH
2720 operations on the target.
2721 See the kernel source file
2722 .IR Documentation/admin\-guide/LSM/Yama.rst
2723 .\" commit 90bb766440f2147486a2acc3e793d7b8348b0c22
2725 .IR Documentation/security/Yama.txt
2726 before Linux 4.13)
2727 for further details.
2729 The use of
2730 .BR PTRACE_TRACEME
2731 is unchanged.
2733 2 ("admin-only attach")
2734 Only processes with the
2735 .B CAP_SYS_PTRACE
2736 capability in the user namespace of the target process may perform
2737 .BR PTRACE_MODE_ATTACH
2738 operations or trace children that employ
2739 .BR PTRACE_TRACEME .
2741 3 ("no attach")
2742 No process may perform
2743 .BR PTRACE_MODE_ATTACH
2744 operations or trace children that employ
2745 .BR PTRACE_TRACEME .
2747 Once this value has been written to the file, it cannot be changed.
2749 With respect to values 1 and 2,
2750 note that creating a new user namespace effectively removes the
2751 protection offered by Yama.
2752 This is because a process in the parent user namespace whose effective
2753 UID matches the UID of the creator of a child namespace
2754 has all capabilities (including
2755 .BR CAP_SYS_PTRACE )
2756 when performing operations within the child user namespace
2757 (and further-removed descendants of that namespace).
2758 Consequently, when a process tries to use user namespaces to sandbox itself,
2759 it inadvertently weakens the protections offered by the Yama LSM.
2761 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2763 .SS C library/kernel differences
2764 At the system call level, the
2765 .BR PTRACE_PEEKTEXT ,
2766 .BR PTRACE_PEEKDATA ,
2768 .BR PTRACE_PEEKUSER
2769 requests have a different API: they store the result
2770 at the address specified by the
2771 .I data
2772 parameter, and the return value is the error flag.
2773 The glibc wrapper function provides the API given in DESCRIPTION above,
2774 with the result being returned via the function return value.
2775 .SH BUGS
2776 On hosts with 2.6 kernel headers,
2777 .B PTRACE_SETOPTIONS
2778 is declared with a different value than the one for 2.4.
2779 This leads to applications compiled with 2.6 kernel
2780 headers failing when run on 2.4 kernels.
2781 This can be worked around by redefining
2782 .B PTRACE_SETOPTIONS
2784 .BR PTRACE_OLDSETOPTIONS ,
2785 if that is defined.
2787 Group-stop notifications are sent to the tracer, but not to real parent.
2788 Last confirmed on 2.6.38.6.
2790 If a thread group leader is traced and exits by calling
2791 .BR _exit (2),
2792 .\" Note from Denys Vlasenko:
2793 .\"     Here "exits" means any kind of death - _exit, exit_group,
2794 .\"     signal death. Signal death and exit_group cases are trivial,
2795 .\"     though: since signal death and exit_group kill all other threads
2796 .\"     too, "until all other threads exit" thing happens rather soon
2797 .\"     in these cases. Therefore, only _exit presents observably
2798 .\"     puzzling behavior to ptrace users: thread leader _exit's,
2799 .\"     but WIFEXITED isn't reported! We are trying to explain here
2800 .\"     why it is so.
2802 .B PTRACE_EVENT_EXIT
2803 stop will happen for it (if requested), but the subsequent
2804 .B WIFEXITED
2805 notification will not be delivered until all other threads exit.
2806 As explained above, if one of other threads calls
2807 .BR execve (2),
2808 the death of the thread group leader will
2809 .I never
2810 be reported.
2811 If the execed thread is not traced by this tracer,
2812 the tracer will never know that
2813 .BR execve (2)
2814 happened.
2815 One possible workaround is to
2816 .B PTRACE_DETACH
2817 the thread group leader instead of restarting it in this case.
2818 Last confirmed on 2.6.38.6.
2819 .\"  FIXME . need to test/verify this scenario
2822 .B SIGKILL
2823 signal may still cause a
2824 .B PTRACE_EVENT_EXIT
2825 stop before actual signal death.
2826 This may be changed in the future;
2827 .B SIGKILL
2828 is meant to always immediately kill tasks even under ptrace.
2829 Last confirmed on Linux 3.13.
2831 Some system calls return with
2832 .B EINTR
2833 if a signal was sent to a tracee, but delivery was suppressed by the tracer.
2834 (This is very typical operation: it is usually
2835 done by debuggers on every attach, in order to not introduce
2836 a bogus
2837 .BR SIGSTOP ).
2838 As of Linux 3.2.9, the following system calls are affected
2839 (this list is likely incomplete):
2840 .BR epoll_wait (2),
2842 .BR read (2)
2843 from an
2844 .BR inotify (7)
2845 file descriptor.
2846 The usual symptom of this bug is that when you attach to
2847 a quiescent process with the command
2849 .in +4n
2851 strace \-p <process\-ID>
2855 then, instead of the usual
2856 and expected one-line output such as
2858 .in +4n
2860 restart_syscall(<... resuming interrupted call ...>_
2866 .in +4n
2868 select(6, [5], NULL, [5], NULL_
2872 ('_' denotes the cursor position), you observe more than one line.
2873 For example:
2875 .in +4n
2877     clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0
2878     epoll_wait(4,_
2882 What is not visible here is that the process was blocked in
2883 .BR epoll_wait (2)
2884 before
2885 .BR strace (1)
2886 has attached to it.
2887 Attaching caused
2888 .BR epoll_wait (2)
2889 to return to user space with the error
2890 .BR EINTR .
2891 In this particular case, the program reacted to
2892 .B EINTR
2893 by checking the current time, and then executing
2894 .BR epoll_wait (2)
2895 again.
2896 (Programs which do not expect such "stray"
2897 .BR EINTR
2898 errors may behave in an unintended way upon an
2899 .BR strace (1)
2900 attach.)
2902 Contrary to the normal rules, the glibc wrapper for
2903 .BR ptrace ()
2904 can set
2905 .I errno
2906 to zero.
2907 .SH SEE ALSO
2908 .BR gdb (1),
2909 .BR ltrace (1),
2910 .BR strace (1),
2911 .BR clone (2),
2912 .BR execve (2),
2913 .BR fork (2),
2914 .BR gettid (2),
2915 .BR prctl (2),
2916 .BR seccomp (2),
2917 .BR sigaction (2),
2918 .BR tgkill (2),
2919 .BR vfork (2),
2920 .BR waitpid (2),
2921 .BR exec (3),
2922 .BR capabilities (7),
2923 .BR signal (7)