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