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