]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/ptrace.2
_exit.2, capget.2, fcntl.2, futex.2, listen.2, memfd_create.2, modify_ldt.2, move_pag...
[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.\"
6b621d05 105.TH PTRACE 2 2020-02-09 "Linux" "Linux Programmer's Manual"
fea681da
MK
106.SH NAME
107ptrace \- process trace
108.SH SYNOPSIS
44b35ee0 109.nf
fea681da 110.B #include <sys/ptrace.h>
68e4db0a 111.PP
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 122call tracing.
dd3568a1 123.PP
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 133a specific tracee using a call of the form
efeece04 134.PP
181f997f 135 ptrace(PTRACE_foo, pid, ...)
efeece04 136.PP
181f997f
MK
137where
138.I pid
139is the thread ID of the corresponding Linux thread.
dd3568a1 140.PP
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.)
dd3568a1 146.PP
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 .
dd3568a1 157.PP
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 173(or even delivering a different signal instead).
dd3568a1 174.PP
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.
dd3568a1 184.PP
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 .
dd3568a1 188.PP
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 406.PP
408731d4
MK
407.in +4n
408.EX
7a535f54 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 414};
b8302363 415.EE
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
14d6e62f 467Send a
b89e39ef 468.B SIGKILL
14d6e62f 469signal to the tracee if the tracer exits.
9f1b9726 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 488value such that
efeece04 489.IP
dc85ba7c
MK
490.nf
491 status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
492.fi
efeece04 493.IP
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 525value such that
efeece04 526.IP
dc85ba7c
MK
527.nf
528 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
529.fi
efeece04 530.IP
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 542value such that
efeece04 543.IP
dc85ba7c
MK
544.nf
545 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
546.fi
efeece04 547.IP
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
efeece04 575.IP
cc7d99c8
MK
576.nf
577 status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
578.fi
efeece04 579.IP
cc7d99c8
MK
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.
cc7d99c8
MK
589.TP
590.BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
591Stop the tracee at the next
592.BR vfork (2)
593and automatically start tracing the newly vforked process,
594which will start with a
29f9b8fb
DV
595.BR SIGSTOP ,
596or
597.B PTRACE_EVENT_STOP
598if
599.B PTRACE_SEIZE
600was used.
cc7d99c8
MK
601A
602.BR waitpid (2)
603by the tracer will return a
604.I status
605value such that
efeece04 606.IP
cc7d99c8
MK
607.nf
608 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
609.fi
efeece04 610.IP
cc7d99c8
MK
611The PID of the new process can be retrieved with
612.BR PTRACE_GETEVENTMSG .
613.TP
614.BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
615Stop the tracee at the completion of the next
616.BR vfork (2).
617A
618.BR waitpid (2)
619by the tracer will return a
620.I status
621value such that
efeece04 622.IP
cc7d99c8
MK
623.nf
624 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
625.fi
efeece04 626.IP
cc7d99c8
MK
627The PID of the new process can (since Linux 2.6.18) be retrieved with
628.BR PTRACE_GETEVENTMSG .
3b4a59c4
KC
629.TP
630.BR PTRACE_O_TRACESECCOMP " (since Linux 3.5)"
631Stop the tracee when a
632.BR seccomp (2)
633.BR SECCOMP_RET_TRACE
81c5080b
MK
634rule is triggered.
635A
3b4a59c4
KC
636.BR waitpid (2)
637by the tracer will return a
638.I status
639value such that
efeece04 640.IP
3b4a59c4
KC
641.nf
642 status>>8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP<<8))
643.fi
efeece04 644.IP
3b4a59c4
KC
645While this triggers a
646.BR PTRACE_EVENT
ff20e9ca
MK
647stop, it is similar to a syscall-enter-stop.
648For details, see the note on
649.B PTRACE_EVENT_SECCOMP
650below.
81c5080b 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,
ff20e9ca
MK
740which will not be executed.
741See the documentation on syscall-stops below.
8bd58774
MK
742For
743.BR PTRACE_SYSEMU_SINGLESTEP ,
181f997f 744do the same but also singlestep if not a system call.
c13182ef 745This call is used by programs like
4d12a715 746User Mode Linux that want to emulate all the tracee's system calls.
94cffcd7
MK
747The
748.I data
749argument is treated as for
750.BR PTRACE_CONT .
34709982
MK
751The
752.I addr
753argument is ignored.
754These requests are currently
755.\" As at 3.7
d2ea1bd4 756supported only on x86.
44b35ee0 757.TP
ba8f446e
DV
758.BR PTRACE_LISTEN " (since Linux 3.4)"
759Restart the stopped tracee, but prevent it from executing.
760The resulting state of the tracee is similar to a process which
f04ba477
MK
761has been stopped by a
762.B SIGSTOP
763(or other stopping signal).
ba8f446e
DV
764See the "group-stop" subsection for additional information.
765.B PTRACE_LISTEN
33a0ccb2 766works only on tracees attached by
ba8f446e
DV
767.BR PTRACE_SEIZE .
768.TP
8bd58774 769.B PTRACE_KILL
181f997f 770Send the tracee a
8bd58774
MK
771.B SIGKILL
772to terminate it.
181f997f
MK
773.RI ( addr
774and
775.I data
776are ignored.)
777.IP
778.I This operation is deprecated; do not use it!
779Instead, send a
780.BR SIGKILL
781directly using
782.BR kill (2)
783or
784.BR tgkill (2).
785The problem with
786.B PTRACE_KILL
787is that it requires the tracee to be in signal-delivery-stop,
788otherwise it may not work
789(i.e., may complete successfully but won't kill the tracee).
790By contrast, sending a
791.B SIGKILL
792directly has no such limitation.
8898a252
MK
793.\" [Note from Denys Vlasenko:
794.\" deprecation suggested by Oleg Nesterov. He prefers to deprecate it
795.\" instead of describing (and needing to support) PTRACE_KILL's quirks.]
fea681da 796.TP
ba8f446e 797.BR PTRACE_INTERRUPT " (since Linux 3.4)"
f04ba477 798Stop a tracee.
8da59274
DV
799If the tracee is running or sleeping in kernel space and
800.B PTRACE_SYSCALL
801is in effect,
802the system call is interrupted and syscall-exit-stop is reported.
803(The interrupted system call is restarted when the tracee is restarted.)
804If the tracee was already stopped by a signal and
805.B PTRACE_LISTEN
806was sent to it,
807the tracee stops with
808.B PTRACE_EVENT_STOP
ad84c543 809and
8da59274 810.I WSTOPSIG(status)
ad84c543 811returns the stop signal.
8da59274
DV
812If any other ptrace-stop is generated at the same time (for example,
813if a signal is sent to the tracee), this ptrace-stop happens.
a9deb5e0
MF
814If none of the above applies (for example, if the tracee is running in user
815space), it stops with
8da59274
DV
816.B PTRACE_EVENT_STOP
817with
818.I WSTOPSIG(status)
819==
820.BR SIGTRAP .
ba8f446e
DV
821.B PTRACE_INTERRUPT
822only works on tracees attached by
823.BR PTRACE_SEIZE .
824.TP
8bd58774 825.B PTRACE_ATTACH
181f997f 826Attach to the process specified in
fea681da 827.IR pid ,
4d12a715 828making it a tracee of the calling process.
8898a252
MK
829.\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
830.\" "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
831.\" Basically, it's not true for any Linux in practical use.
4d12a715
DV
832.\" ; the behavior of the tracee is as if it had done a
833.\" .BR PTRACE_TRACEME .
834.\" The calling process actually becomes the parent of the tracee
835.\" process for most purposes (e.g., it will receive
836.\" notification of tracee events and appears in
837.\" .BR ps (1)
838.\" output as the tracee's parent), but a
839.\" .BR getppid (2)
840.\" by the tracee will still return the PID of the original parent.
841The tracee is sent a
8bd58774
MK
842.BR SIGSTOP ,
843but will not necessarily have stopped
e63ad01d 844by the completion of this call; use
181f997f 845.BR waitpid (2)
8b20acd1 846to wait for the tracee to stop.
181f997f
MK
847See the "Attaching and detaching" subsection for additional information.
848.RI ( addr
849and
850.I data
851are ignored.)
efeece04 852.IP
d4c976d8
MK
853Permission to perform a
854.BR PTRACE_ATTACH
855is governed by a ptrace access mode
856.B PTRACE_MODE_ATTACH_REALCREDS
857check; see below.
fea681da 858.TP
ba8f446e 859.BR PTRACE_SEIZE " (since Linux 3.4)"
fec74bb1
MK
860.\"
861.\" Noted by Dmitry Levin:
862.\"
863.\" PTRACE_SEIZE was introduced by commit v3.1-rc1~308^2~28, but
864.\" it had to be used along with a temporary flag PTRACE_SEIZE_DEVEL,
865.\" which was removed later by commit v3.4-rc1~109^2~20.
866.\"
867.\" That is, [before] v3.4 we had a test mode of PTRACE_SEIZE API,
868.\" which was not compatible with the current PTRACE_SEIZE API introduced
869.\" in Linux 3.4.
870.\"
ba8f446e
DV
871Attach to the process specified in
872.IR pid ,
873making it a tracee of the calling process.
874Unlike
875.BR PTRACE_ATTACH ,
876.B PTRACE_SEIZE
f04ba477 877does not stop the process.
28e2ca57
DV
878Group-stops are reported as
879.B PTRACE_EVENT_STOP
53cdec41 880and
28e2ca57 881.I WSTOPSIG(status)
53cdec41 882returns the stop signal.
28e2ca57
DV
883Automatically attached children stop with
884.B PTRACE_EVENT_STOP
53cdec41 885and
28e2ca57 886.I WSTOPSIG(status)
53cdec41 887returns
28e2ca57
DV
888.B SIGTRAP
889instead of having
890.B SIGSTOP
891signal delivered to them.
cc3407d1 892.BR execve (2)
28e2ca57 893does not deliver an extra
53cdec41 894.BR SIGTRAP .
f04ba477 895Only a
ba8f446e
DV
896.BR PTRACE_SEIZE d
897process can accept
898.B PTRACE_INTERRUPT
899and
900.B PTRACE_LISTEN
901commands.
28e2ca57
DV
902The "seized" behavior just described is inherited by
903children that are automatically attached using
904.BR PTRACE_O_TRACEFORK ,
905.BR PTRACE_O_TRACEVFORK ,
906and
907.BR PTRACE_O_TRACECLONE .
ba8f446e
DV
908.I addr
909must be zero.
910.I data
911contains a bit mask of ptrace options to activate immediately.
efeece04 912.IP
c33e8aff
MK
913Permission to perform a
914.BR PTRACE_SEIZE
915is governed by a ptrace access mode
916.B PTRACE_MODE_ATTACH_REALCREDS
917check; see below.
baf11d5c
MK
918.\"
919.TP
920.BR PTRACE_SECCOMP_GET_FILTER " (since Linux 4.4)"
921.\" commit f8e529ed941ba2bbcbf310b575d968159ce7e895
922This operation allows the tracer to dump the tracee's
923classic BPF filters.
efeece04 924.IP
baf11d5c
MK
925.I addr
926is an integer specifying the index of the filter to be dumped.
927The most recently installed filter has the index 0.
928If
929.I addr
930is greater than the number of installed filters,
931the operation fails with the error
932.BR ENOENT .
efeece04 933.IP
baf11d5c
MK
934.I data
935is either a pointer to a
936.IR "struct sock_filter"
937array that is large enough to store the BPF program,
938or NULL if the program is not to be stored.
efeece04 939.IP
baf11d5c
MK
940Upon success,
941the return value is the number of instructions in the BPF program.
942If
943.I data
944was NULL, then this return value can be used to correctly size the
945.IR "struct sock_filter"
946array passed in a subsequent call.
efeece04 947.IP
baf11d5c 948This operation fails with the error
7b10f505 949.B EACCES
baf11d5c
MK
950if the caller does not have the
951.B CAP_SYS_ADMIN
952capability or if the caller is in strict or filter seccomp mode.
953If the filter referred to by
954.I addr
955is not a classic BPF filter, the operation fails with the error
956.BR EMEDIUMTYPE .
efeece04 957.IP
baf11d5c
MK
958This operation is available if the kernel was configured with both the
959.B CONFIG_SECCOMP_FILTER
960and the
961.B CONFIG_CHECKPOINT_RESTORE
962options.
ba8f446e 963.TP
8bd58774 964.B PTRACE_DETACH
181f997f 965Restart the stopped tracee as for
8bd58774 966.BR PTRACE_CONT ,
181f997f
MK
967but first detach from it.
968Under Linux, a tracee can be detached in this way regardless
969of which method was used to initiate tracing.
970.RI ( addr
971is ignored.)
baf11d5c 972.\"
bc8bfd8a
MK
973.TP
974.BR PTRACE_GET_THREAD_AREA " (since Linux 2.6.0)"
975This operation performs a similar task to
976.BR get_thread_area (2).
977It reads the TLS entry in the GDT whose index is given in
978.IR addr ,
979placing a copy of the entry into the
980.IR "struct user_desc"
981pointed to by
982.IR data .
983(By contrast with
984.BR get_thread_area (2),
985the
986.I entry_number
987of the
988.IR "struct user_desc"
989is ignored.)
990.TP
991.BR PTRACE_SET_THREAD_AREA " (since Linux 2.6.0)"
992This operation performs a similar task to
993.BR set_thread_area (2).
994It sets the TLS entry in the GDT whose index is given in
995.IR addr ,
996assigning it the data supplied in the
997.IR "struct user_desc"
998pointed to by
999.IR data .
1000(By contrast with
1001.BR set_thread_area (2),
1002the
1003.I entry_number
1004of the
1005.IR "struct user_desc"
1006is ignored; in other words,
1007this ptrace operation can't be used to allocate a free TLS entry.)
fc91449c
DL
1008.TP
1009.BR PTRACE_GET_SYSCALL_INFO " (since Linux 5.3)"
1010.\" commit 201766a20e30f982ccfe36bebfad9602c3ff574a
c3543fab
MK
1011Retrieve information about the system call that caused the stop.
1012The information is placed into the buffer pointed by the
fc91449c
DL
1013.I data
1014argument, which should be a pointer to a buffer of type
1015.IR "struct ptrace_syscall_info" .
1016The
1017.I addr
1018argument contains the size of the buffer pointed to
c3543fab 1019by the
fc91449c
DL
1020.I data
1021argument (i.e.,
1022.IR "sizeof(struct ptrace_syscall_info)" ).
1023The return value contains the number of bytes available
1024to be written by the kernel.
c3543fab
MK
1025If the size of the data to be written by the kernel exceeds the size
1026specified by the
fc91449c 1027.I addr
c3543fab 1028argument, the output data is truncated.
a60e8f1b
DL
1029.IP
1030The
1031.I ptrace_syscall_info
1032structure contains the following fields:
1033.IP
9d8f542d 1034.in +2n
a60e8f1b
DL
1035.EX
1036struct ptrace_syscall_info {
9d8f542d
MK
1037 __u8 op; /* Type of system call stop */
1038 __u32 arch; /* AUDIT_ARCH_* value; see seccomp(2) */
f04534d2 1039 __u64 instruction_pointer; /* CPU instruction pointer */
9d8f542d 1040 __u64 stack_pointer; /* CPU stack pointer */
a60e8f1b 1041 union {
9d8f542d
MK
1042 struct { /* op == PTRACE_SYSCALL_INFO_ENTRY */
1043 __u64 nr; /* System call number */
1044 __u64 args[6]; /* System call arguments */
f04534d2 1045 } entry;
9d8f542d
MK
1046 struct { /* op == PTRACE_SYSCALL_INFO_EXIT */
1047 __s64 rval; /* System call return value */
227a3682 1048 __u8 is_error; /* System call error flag;
9914d8bd
MK
1049 Boolean: does rval contain
1050 an error value (\-ERRCODE) or
1051 a nonerror return value? */
f04534d2 1052 } exit;
9d8f542d
MK
1053 struct { /* op == PTRACE_SYSCALL_INFO_SECCOMP */
1054 __u64 nr; /* System call number */
1055 __u64 args[6]; /* System call arguments */
1056 __u32 ret_data; /* SECCOMP_RET_DATA portion
1057 of SECCOMP_RET_TRACE
1058 return value */
f04534d2 1059 } seccomp;
a60e8f1b
DL
1060 };
1061};
1062.EE
1063.in
1064.IP
1c0955b1 1065The
a60e8f1b
DL
1066.IR op ,
1067.IR arch ,
1068.IR instruction_pointer ,
1069and
1070.I stack_pointer
1071fields are defined for all kinds of ptrace system call stops.
1c0955b1 1072The rest of the structure is a union; one should read only those fields
a60e8f1b
DL
1073that are meaningful for the kind of system call stop specified by the
1074.IR op
1075field.
f04534d2
MK
1076.IP
1077The
1078.I op
1079field has one of the following values (defined in
1080.IR <linux/ptrace.h>)
1081indicating what type of stop occurred and
1082which part of the union is filled:
1083.RS
1084.TP
1085.BR PTRACE_SYSCALL_INFO_ENTRY
1086The
1087.I entry
1088component of the union contains information relating to a
1089system call entry stop.
1090.TP
1091.BR PTRACE_SYSCALL_INFO_EXIT
1092The
1093.I exit
1094component of the union contains information relating to a
1095system call exit stop.
1096.TP
1097.BR PTRACE_SYSCALL_INFO_SECCOMP
1098The
302c512c 1099.I seccomp
f04534d2
MK
1100component of the union contains information relating to a
1101.B PTRACE_EVENT_SECCOMP
1102stop.
1103.TP
1104.BR PTRACE_SYSCALL_INFO_NONE
1105No component of the union contains relevant information.
1106.RE
bc8bfd8a 1107.\"
4d12a715 1108.SS Death under ptrace
181f997f
MK
1109When a (possibly multithreaded) process receives a killing signal
1110(one whose disposition is set to
1111.B SIG_DFL
1112and whose default action is to kill the process),
8b20acd1
MK
1113all threads exit.
1114Tracees report their death to their tracer(s).
181f997f
MK
1115Notification of this event is delivered via
1116.BR waitpid (2).
dd3568a1 1117.PP
181f997f
MK
1118Note that the killing signal will first cause signal-delivery-stop
1119(on one tracee only),
1120and only after it is injected by the tracer
1121(or after it was dispatched to a thread which isn't traced),
1122will death from the signal happen on
1123.I all
1124tracees within a multithreaded process.
1125(The term "signal-delivery-stop" is explained below.)
dd3568a1 1126.PP
181f997f 1127.B SIGKILL
ca302d0e
DV
1128does not generate signal-delivery-stop and
1129therefore the tracer can't suppress it.
181f997f
MK
1130.B SIGKILL
1131kills even within system calls
1132(syscall-exit-stop is not generated prior to death by
1133.BR SIGKILL ).
1134The net effect is that
1135.B SIGKILL
1136always kills the process (all its threads),
1137even if some threads of the process are ptraced.
dd3568a1 1138.PP
181f997f
MK
1139When the tracee calls
1140.BR _exit (2),
1141it reports its death to its tracer.
4d12a715 1142Other threads are not affected.
dd3568a1 1143.PP
181f997f
MK
1144When any thread executes
1145.BR exit_group (2),
1146every tracee in its thread group reports its death to its tracer.
dd3568a1 1147.PP
181f997f
MK
1148If the
1149.B PTRACE_O_TRACEEXIT
1150option is on,
1151.B PTRACE_EVENT_EXIT
1152will happen before actual death.
1153This applies to exits via
1154.BR exit (2),
1155.BR exit_group (2),
1156and signal deaths (except
55bd9495
MK
1157.BR SIGKILL ,
1158depending on the kernel version; see BUGS below),
181f997f
MK
1159and when threads are torn down on
1160.BR execve (2)
1161in a multithreaded process.
dd3568a1 1162.PP
181f997f
MK
1163The tracer cannot assume that the ptrace-stopped tracee exists.
1164There are many scenarios when the tracee may die while stopped (such as
1165.BR SIGKILL ).
d6e37473 1166Therefore, the tracer must be prepared to handle an
181f997f
MK
1167.B ESRCH
1168error on any ptrace operation.
1169Unfortunately, the same error is returned if the tracee
1170exists but is not ptrace-stopped
1171(for commands which require a stopped tracee),
1172or if it is not traced by the process which issued the ptrace call.
1173The tracer needs to keep track of the stopped/running state of the tracee,
1174and interpret
1175.B ESRCH
1176as "tracee died unexpectedly" only if it knows that the tracee has
1177been observed to enter ptrace-stop.
1178Note that there is no guarantee that
1179.I waitpid(WNOHANG)
1180will reliably report the tracee's death status if a
1181ptrace operation returned
1182.BR ESRCH .
1183.I waitpid(WNOHANG)
1184may return 0 instead.
1185In other words, the tracee may be "not yet fully dead",
1186but already refusing ptrace requests.
dd3568a1 1187.PP
181f997f
MK
1188The tracer can't assume that the tracee
1189.I always
1190ends its life by reporting
1191.I WIFEXITED(status)
1192or
8898a252
MK
1193.IR WIFSIGNALED(status) ;
1194there are cases where this does not occur.
1195For example, if a thread other than thread group leader does an
1196.BR execve (2),
1197it disappears;
1198its PID will never be seen again,
1199and any subsequent ptrace stops will be reported under
1200the thread group leader's PID.
4d12a715
DV
1201.SS Stopped states
1202A tracee can be in two states: running or stopped.
ad84c543 1203For the purposes of ptrace, a tracee which is blocked in a system call
8da59274
DV
1204(such as
1205.BR read (2),
ad84c543
MK
1206.BR pause (2),
1207etc.)
1208is nevertheless considered to be running, even if the tracee is blocked
8da59274
DV
1209for a long time.
1210The state of the tracee after
1211.BR PTRACE_LISTEN
1212is somewhat of a gray area: it is not in any ptrace-stop (ptrace commands
ad84c543
MK
1213won't work on it, and it will deliver
1214.BR waitpid (2)
1215notifications),
8da59274
DV
1216but it also may be considered "stopped" because
1217it is not executing instructions (is not scheduled), and if it was
1218in group-stop before
1219.BR PTRACE_LISTEN ,
ad84c543
MK
1220it will not respond to signals until
1221.B SIGCONT
1222is received.
dd3568a1 1223.PP
181f997f 1224There are many kinds of states when the tracee is stopped, and in ptrace
8b20acd1 1225discussions they are often conflated.
181f997f 1226Therefore, it is important to use precise terms.
dd3568a1 1227.PP
181f997f
MK
1228In this manual page, any stopped state in which the tracee is ready
1229to accept ptrace commands from the tracer is called
1230.IR ptrace-stop .
8b20acd1 1231Ptrace-stops can
181f997f
MK
1232be further subdivided into
1233.IR signal-delivery-stop ,
1234.IR group-stop ,
1235.IR syscall-stop ,
20e64af8 1236.IR "PTRACE_EVENT stops" ,
181f997f
MK
1237and so on.
1238These stopped states are described in detail below.
dd3568a1 1239.PP
181f997f
MK
1240When the running tracee enters ptrace-stop, it notifies its tracer using
1241.BR waitpid (2)
1242(or one of the other "wait" system calls).
1243Most of this manual page assumes that the tracer waits with:
dd3568a1 1244.PP
181f997f 1245 pid = waitpid(pid_or_minus_1, &status, __WALL);
dd3568a1 1246.PP
181f997f
MK
1247Ptrace-stopped tracees are reported as returns with
1248.I pid
1249greater than 0 and
1250.I WIFSTOPPED(status)
1251true.
8898a252
MK
1252.\" Denys Vlasenko:
1253.\" Do we require __WALL usage, or will just using 0 be ok? (With 0,
1254.\" I am not 100% sure there aren't ugly corner cases.) Are the
181f997f
MK
1255.\" rules different if user wants to use waitid? Will waitid require
1256.\" WEXITED?
1257.\"
dd3568a1 1258.PP
181f997f
MK
1259The
1260.B __WALL
1261flag does not include the
1262.B WSTOPPED
1263and
1264.B WEXITED
1265flags, but implies their functionality.
dd3568a1 1266.PP
181f997f
MK
1267Setting the
1268.B WCONTINUED
1269flag when calling
1270.BR waitpid (2)
1271is not recommended: the "continued" state is per-process and
1272consuming it can confuse the real parent of the tracee.
dd3568a1 1273.PP
181f997f
MK
1274Use of the
1275.B WNOHANG
1276flag may cause
1277.BR waitpid (2)
1278to return 0 ("no wait results available yet")
1279even if the tracer knows there should be a notification.
1280Example:
408731d4
MK
1281.PP
1282.in +4n
1283.EX
1284errno = 0;
1285ptrace(PTRACE_CONT, pid, 0L, 0L);
1286if (errno == ESRCH) {
1287 /* tracee is dead */
1288 r = waitpid(tracee, &status, __WALL | WNOHANG);
1289 /* r can still be 0 here! */
1290}
1291.EE
1292.in
bea08fec 1293.\" FIXME .
181f997f
MK
1294.\" waitid usage? WNOWAIT?
1295.\" describe how wait notifications queue (or not queue)
dd3568a1 1296.PP
4d12a715 1297The following kinds of ptrace-stops exist: signal-delivery-stops,
a5c725cf
DP
1298group-stops,
1299.B PTRACE_EVENT
1300stops, syscall-stops.
181f997f
MK
1301They all are reported by
1302.BR waitpid (2)
1303with
1304.I WIFSTOPPED(status)
1305true.
1306They may be differentiated by examining the value
1307.IR status>>8 ,
1308and if there is ambiguity in that value, by querying
1309.BR PTRACE_GETSIGINFO .
181f997f
MK
1310(Note: the
1311.I WSTOPSIG(status)
dc85ba7c 1312macro can't be used to perform this examination,
8898a252 1313because it returns the value
0ce81ab5 1314.IR "(status>>8)\ &\ 0xff" .)
4d12a715 1315.SS Signal-delivery-stop
181f997f
MK
1316When a (possibly multithreaded) process receives any signal except
1317.BR SIGKILL ,
1318the kernel selects an arbitrary thread which handles the signal.
1319(If the signal is generated with
1320.BR tgkill (2),
1321the target thread can be explicitly selected by the caller.)
1322If the selected thread is traced, it enters signal-delivery-stop.
1323At this point, the signal is not yet delivered to the process,
1324and can be suppressed by the tracer.
1325If the tracer doesn't suppress the signal,
181f997f 1326it passes the signal to the tracee in the next ptrace restart request.
8b20acd1 1327This second step of signal delivery is called
181f997f
MK
1328.I "signal injection"
1329in this manual page.
1330Note that if the signal is blocked,
1331signal-delivery-stop doesn't happen until the signal is unblocked,
1332with the usual exception that
1333.B SIGSTOP
1334can't be blocked.
dd3568a1 1335.PP
181f997f
MK
1336Signal-delivery-stop is observed by the tracer as
1337.BR waitpid (2)
1338returning with
1339.I WIFSTOPPED(status)
f098951d 1340true, with the signal returned by
181f997f 1341.IR WSTOPSIG(status) .
f098951d 1342If the signal is
181f997f
MK
1343.BR SIGTRAP ,
1344this may be a different kind of ptrace-stop;
1345see the "Syscall-stops" and "execve" sections below for details.
8b20acd1 1346If
181f997f
MK
1347.I WSTOPSIG(status)
1348returns a stopping signal, this may be a group-stop; see below.
4d12a715 1349.SS Signal injection and suppression
181f997f
MK
1350After signal-delivery-stop is observed by the tracer,
1351the tracer should restart the tracee with the call
dd3568a1 1352.PP
181f997f 1353 ptrace(PTRACE_restart, pid, 0, sig)
dd3568a1 1354.PP
181f997f
MK
1355where
1356.B PTRACE_restart
1357is one of the restarting ptrace requests.
1358If
1359.I sig
1360is 0, then a signal is not delivered.
1361Otherwise, the signal
1362.I sig
1363is delivered.
1364This operation is called
1365.I "signal injection"
1366in this manual page, to distinguish it from signal-delivery-stop.
dd3568a1 1367.PP
8898a252 1368The
181f997f
MK
1369.I sig
1370value may be different from the
1371.I WSTOPSIG(status)
1372value: the tracer can cause a different signal to be injected.
dd3568a1 1373.PP
181f997f 1374Note that a suppressed signal still causes system calls to return
8b20acd1 1375prematurely.
15d33661 1376In this case, system calls will be restarted: the tracer will
a17e05c5 1377observe the tracee to reexecute the interrupted system call (or
a5c725cf 1378.BR restart_syscall (2)
177660fa 1379system call for a few system calls which use a different mechanism
f098951d
DV
1380for restarting) if the tracer uses
1381.BR PTRACE_SYSCALL .
1382Even system calls (such as
a5c725cf 1383.BR poll (2))
f098951d 1384which are not restartable after signal are restarted after
a17e05c5 1385signal is suppressed;
177660fa 1386however, kernel bugs exist which cause some system calls to fail with
181f997f
MK
1387.B EINTR
1388even though no observable signal is injected to the tracee.
dd3568a1 1389.PP
8898a252 1390Restarting ptrace commands issued in ptrace-stops other than
181f997f
MK
1391signal-delivery-stop are not guaranteed to inject a signal, even if
1392.I sig
8b20acd1 1393is nonzero.
181f997f
MK
1394No error is reported; a nonzero
1395.I sig
1396may simply be ignored.
1397Ptrace users should not try to "create a new signal" this way: use
1398.BR tgkill (2)
1399instead.
dd3568a1 1400.PP
8898a252
MK
1401The fact that signal injection requests may be ignored
1402when restarting the tracee after
1403ptrace stops that are not signal-delivery-stops
1404is a cause of confusion among ptrace users.
181f997f
MK
1405One typical scenario is that the tracer observes group-stop,
1406mistakes it for signal-delivery-stop, restarts the tracee with
efeece04 1407.PP
ba8f446e 1408 ptrace(PTRACE_restart, pid, 0, stopsig)
efeece04 1409.PP
181f997f
MK
1410with the intention of injecting
1411.IR stopsig ,
1412but
1413.I stopsig
1414gets ignored and the tracee continues to run.
dd3568a1 1415.PP
181f997f
MK
1416The
1417.B SIGCONT
1418signal has a side effect of waking up (all threads of)
1419a group-stopped process.
1420This side effect happens before signal-delivery-stop.
a5c725cf 1421The tracer can't suppress this side effect (it can
181f997f
MK
1422only suppress signal injection, which only causes the
1423.BR SIGCONT
1424handler to not be executed in the tracee, if such a handler is installed).
1425In fact, waking up from group-stop may be followed by
1426signal-delivery-stop for signal(s)
1427.I other than
1428.BR SIGCONT ,
1429if they were pending when
1430.B SIGCONT
1431was delivered.
1432In other words,
1433.B SIGCONT
1434may be not the first signal observed by the tracee after it was sent.
dd3568a1 1435.PP
181f997f 1436Stopping signals cause (all threads of) a process to enter group-stop.
4d12a715 1437This side effect happens after signal injection, and therefore can be
181f997f 1438suppressed by the tracer.
dd3568a1 1439.PP
dc85ba7c
MK
1440In Linux 2.4 and earlier, the
1441.B SIGSTOP
1442signal can't be injected.
1443.\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
1444.\" there is:
d6e37473 1445.\"
dc85ba7c
MK
1446.\" /* The debugger continued. Ignore SIGSTOP. */
1447.\" if (signr == SIGSTOP)
1448.\" continue;
dd3568a1 1449.PP
181f997f
MK
1450.B PTRACE_GETSIGINFO
1451can be used to retrieve a
1452.I siginfo_t
1453structure which corresponds to the delivered signal.
1454.B PTRACE_SETSIGINFO
1455may be used to modify it.
1456If
1457.B PTRACE_SETSIGINFO
1458has been used to alter
1459.IR siginfo_t ,
1460the
1461.I si_signo
1462field and the
1463.I sig
1464parameter in the restarting command must match,
4d12a715
DV
1465otherwise the result is undefined.
1466.SS Group-stop
181f997f 1467When a (possibly multithreaded) process receives a stopping signal,
8b20acd1
MK
1468all threads stop.
1469If some threads are traced, they enter a group-stop.
181f997f
MK
1470Note that the stopping signal will first cause signal-delivery-stop
1471(on one tracee only), and only after it is injected by the tracer
1472(or after it was dispatched to a thread which isn't traced),
1473will group-stop be initiated on
1474.I all
1475tracees within the multithreaded process.
1476As usual, every tracee reports its group-stop separately
1477to the corresponding tracer.
dd3568a1 1478.PP
181f997f
MK
1479Group-stop is observed by the tracer as
1480.BR waitpid (2)
1481returning with
1482.I WIFSTOPPED(status)
1483true, with the stopping signal available via
1484.IR WSTOPSIG(status) .
1485The same result is returned by some other classes of ptrace-stops,
1486therefore the recommended practice is to perform the call
dd3568a1 1487.PP
181f997f 1488 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
dd3568a1 1489.PP
181f997f
MK
1490The call can be avoided if the signal is not
1491.BR SIGSTOP ,
1492.BR SIGTSTP ,
1493.BR SIGTTIN ,
1494or
1495.BR SIGTTOU ;
1496only these four signals are stopping signals.
1497If the tracer sees something else, it can't be a group-stop.
1498Otherwise, the tracer needs to call
1499.BR PTRACE_GETSIGINFO .
1500If
1501.B PTRACE_GETSIGINFO
1502fails with
1503.BR EINVAL ,
1504then it is definitely a group-stop.
1505(Other failure codes are possible, such as
1506.B ESRCH
1507("no such process") if a
1508.B SIGKILL
1509killed the tracee.)
dd3568a1 1510.PP
ad84c543 1511If tracee was attached using
72906215 1512.BR PTRACE_SEIZE ,
ad84c543 1513group-stop is indicated by
8da59274 1514.BR PTRACE_EVENT_STOP :
ad84c543
MK
1515.IR "status>>16 == PTRACE_EVENT_STOP" .
1516This allows detection of group-stops
1517without requiring an extra
8da59274
DV
1518.B PTRACE_GETSIGINFO
1519call.
dd3568a1 1520.PP
f04ba477 1521As of Linux 2.6.38,
181f997f
MK
1522after the tracer sees the tracee ptrace-stop and until it
1523restarts or kills it, the tracee will not run,
1524and will not send notifications (except
1525.B SIGKILL
1526death) to the tracer, even if the tracer enters into another
1527.BR waitpid (2)
8b20acd1 1528call.
dd3568a1 1529.PP
b8d02d56
MK
1530The kernel behavior described in the previous paragraph
1531causes a problem with transparent handling of stopping signals.
1532If the tracer restarts the tracee after group-stop,
dc85ba7c 1533the stopping signal
8898a252 1534is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
181f997f
MK
1535If the tracer doesn't restart the tracee before entering into the next
1536.BR waitpid (2),
1537future
1538.B SIGCONT
b8d02d56
MK
1539signals will not be reported to the tracer;
1540this would cause the
181f997f 1541.B SIGCONT
b8d02d56 1542signals to have no effect on the tracee.
dd3568a1 1543.PP
f04ba477 1544Since Linux 3.4, there is a method to overcome this problem: instead of
ba8f446e
DV
1545.BR PTRACE_CONT ,
1546a
1547.B PTRACE_LISTEN
1548command can be used to restart a tracee in a way where it does not execute,
f04ba477
MK
1549but waits for a new event which it can report via
1550.BR waitpid (2)
1551(such as when
ba8f446e
DV
1552it is restarted by a
1553.BR SIGCONT ).
4d12a715 1554.SS PTRACE_EVENT stops
181f997f
MK
1555If the tracer sets
1556.B PTRACE_O_TRACE_*
1557options, the tracee will enter ptrace-stops called
1558.B PTRACE_EVENT
1559stops.
dd3568a1 1560.PP
181f997f
MK
1561.B PTRACE_EVENT
1562stops are observed by the tracer as
1563.BR waitpid (2)
1564returning with
1565.IR WIFSTOPPED(status) ,
1566and
1567.I WSTOPSIG(status)
1568returns
302c512c
DV
1569.BR SIGTRAP
1570(or for
1571.BR PTRACE_EVENT_STOP ,
1572returns the stopping signal if tracee is in a group-stop).
181f997f
MK
1573An additional bit is set in the higher byte of the status word:
1574the value
1575.I status>>8
1576will be
efeece04 1577.PP
302c512c 1578 ((PTRACE_EVENT_foo<<8) | SIGTRAP).
efeece04 1579.PP
8b20acd1 1580The following events exist:
181f997f
MK
1581.TP
1582.B PTRACE_EVENT_VFORK
1583Stop before return from
1584.BR vfork (2)
1585or
1586.BR clone (2)
1587with the
1588.B CLONE_VFORK
1589flag.
1590When the tracee is continued after this stop, it will wait for child to
1591exit/exec before continuing its execution
1592(in other words, the usual behavior on
1593.BR vfork (2)).
1594.TP
1595.B PTRACE_EVENT_FORK
1596Stop before return from
1597.BR fork (2)
1598or
1599.BR clone (2)
1600with the exit signal set to
1601.BR SIGCHLD .
1602.TP
1603.B PTRACE_EVENT_CLONE
1604Stop before return from
a5c725cf 1605.BR clone (2).
181f997f
MK
1606.TP
1607.B PTRACE_EVENT_VFORK_DONE
1608Stop before return from
1609.BR vfork (2)
1610or
1611.BR clone (2)
1612with the
1613.B CLONE_VFORK
1614flag,
1615but after the child unblocked this tracee by exiting or execing.
dd3568a1 1616.PP
181f997f
MK
1617For all four stops described above,
1618the stop occurs in the parent (i.e., the tracee),
1619not in the newly created thread.
1620.BR PTRACE_GETEVENTMSG
1621can be used to retrieve the new thread's ID.
1622.TP
1623.B PTRACE_EVENT_EXEC
1624Stop before return from
1625.BR execve (2).
b16d33ef
DV
1626Since Linux 3.0,
1627.BR PTRACE_GETEVENTMSG
1628returns the former thread ID.
181f997f
MK
1629.TP
1630.B PTRACE_EVENT_EXIT
1631Stop before exit (including death from
1632.BR exit_group (2)),
1633signal death, or exit caused by
1634.BR execve (2)
1635in a multithreaded process.
1636.B PTRACE_GETEVENTMSG
1637returns the exit status.
8b20acd1
MK
1638Registers can be examined
1639(unlike when "real" exit happens).
181f997f
MK
1640The tracee is still alive; it needs to be
1641.BR PTRACE_CONT ed
1642or
1643.BR PTRACE_DETACH ed
1644to finish exiting.
ba8f446e
DV
1645.TP
1646.B PTRACE_EVENT_STOP
1647Stop induced by
1648.B PTRACE_INTERRUPT
29f9b8fb
DV
1649command, or group-stop, or initial ptrace-stop when a new child is attached
1650(only if attached using
28e2ca57 1651.BR PTRACE_SEIZE ).
3b4a59c4
KC
1652.TP
1653.B PTRACE_EVENT_SECCOMP
1654Stop triggered by a
1655.BR seccomp (2)
1656rule on tracee syscall entry when
1657.BR PTRACE_O_TRACESECCOMP
81c5080b
MK
1658has been set by the tracer.
1659The seccomp event message data (from the
3b4a59c4 1660.BR SECCOMP_RET_DATA
81c5080b 1661portion of the seccomp filter rule) can be retrieved with
ff20e9ca
MK
1662.BR PTRACE_GETEVENTMSG .
1663The semantics of this stop are described in
5419141e 1664detail in a separate section below.
dd3568a1 1665.PP
181f997f
MK
1666.B PTRACE_GETSIGINFO
1667on
1668.B PTRACE_EVENT
1669stops returns
b16d33ef
DV
1670.B SIGTRAP
1671in
181f997f
MK
1672.IR si_signo ,
1673with
1674.I si_code
1675set to
1676.IR "(event<<8)\ |\ SIGTRAP" .
4d12a715 1677.SS Syscall-stops
181f997f 1678If the tracee was restarted by
131bcd7a
KF
1679.BR PTRACE_SYSCALL
1680or
1681.BR PTRACE_SYSEMU ,
181f997f 1682the tracee enters
131bcd7a
KF
1683syscall-enter-stop just prior to entering any system call (which
1684will not be executed if the restart was using
ff20e9ca 1685.BR PTRACE_SYSEMU ,
131bcd7a
KF
1686regardless of any change made to registers at this point or how the
1687tracee is restarted after this stop).
1688No matter which method caused the syscall-entry-stop,
1689if the tracer restarts the tracee with
181f997f
MK
1690.BR PTRACE_SYSCALL ,
1691the tracee enters syscall-exit-stop when the system call is finished,
1692or if it is interrupted by a signal.
1693(That is, signal-delivery-stop never happens between syscall-enter-stop
1694and syscall-exit-stop; it happens
1695.I after
ff20e9ca
MK
1696syscall-exit-stop.).
1697If the tracee is continued using any other method (including
1698.BR PTRACE_SYSEMU ),
1699no syscall-exit-stop occurs.
1700Note that all mentions
131bcd7a
KF
1701.BR PTRACE_SYSEMU
1702apply equally to
1703.BR PTRACE_SYSEMU_SINGLESTEP.
dd3568a1 1704.PP
0d5b85ca 1705However, even if the tracee was continued using
2bb165ec
MK
1706.BR PTRACE_SYSCALL ,
1707it is not guaranteed that the next stop will be a syscall-exit-stop.
181f997f
MK
1708Other possibilities are that the tracee may stop in a
1709.B PTRACE_EVENT
5419141e 1710stop (including seccomp stops), exit (if it entered
181f997f
MK
1711.BR _exit (2)
1712or
1713.BR exit_group (2)),
1714be killed by
1715.BR SIGKILL ,
1716or die silently (if it is a thread group leader, the
1717.BR execve (2)
1718happened in another thread,
1719and that thread is not traced by the same tracer;
1720this situation is discussed later).
dd3568a1 1721.PP
181f997f
MK
1722Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1723.BR waitpid (2)
1724returning with
1725.I WIFSTOPPED(status)
1726true, and
1727.I WSTOPSIG(status)
1728giving
1729.BR SIGTRAP .
1730If the
1731.B PTRACE_O_TRACESYSGOOD
1732option was set by the tracer, then
1733.I WSTOPSIG(status)
1734will give the value
1735.IR "(SIGTRAP\ |\ 0x80)" .
dd3568a1 1736.PP
4d12a715 1737Syscall-stops can be distinguished from signal-delivery-stop with
181f997f
MK
1738.B SIGTRAP
1739by querying
1740.BR PTRACE_GETSIGINFO
1741for the following cases:
1742.TP
1743.IR si_code " <= 0"
1744.B SIGTRAP
7fac88a9 1745was delivered as a result of a user-space action,
8898a252 1746for example, a system call
181f997f 1747.RB ( tgkill (2),
8898a252 1748.BR kill (2),
181f997f 1749.BR sigqueue (3),
8898a252
MK
1750etc.),
1751expiration of a POSIX timer,
1752change of state on a POSIX message queue,
1753or completion of an asynchronous I/O request.
181f997f
MK
1754.TP
1755.IR si_code " == SI_KERNEL (0x80)"
1756.B SIGTRAP
1757was sent by the kernel.
1758.TP
1759.IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1760This is a syscall-stop.
dd3568a1 1761.PP
181f997f
MK
1762However, syscall-stops happen very often (twice per system call),
1763and performing
1764.B PTRACE_GETSIGINFO
1765for every syscall-stop may be somewhat expensive.
dd3568a1 1766.PP
181f997f
MK
1767Some architectures allow the cases to be distinguished
1768by examining registers.
1769For example, on x86,
1770.I rax
1771==
1772.RB - ENOSYS
1773in syscall-enter-stop.
1774Since
1775.B SIGTRAP
1776(like any other signal) always happens
1777.I after
1778syscall-exit-stop,
1779and at this point
1780.I rax
1781almost never contains
1782.RB - ENOSYS ,
1783the
1784.B SIGTRAP
1785looks like "syscall-stop which is not syscall-enter-stop";
1786in other words, it looks like a
8b20acd1 1787"stray syscall-exit-stop" and can be detected this way.
181f997f 1788But such detection is fragile and is best avoided.
dd3568a1 1789.PP
181f997f
MK
1790Using the
1791.B PTRACE_O_TRACESYSGOOD
a17e05c5 1792option is the recommended method to distinguish syscall-stops
b8d02d56 1793from other kinds of ptrace-stops,
181f997f 1794since it is reliable and does not incur a performance penalty.
dd3568a1 1795.PP
181f997f
MK
1796Syscall-enter-stop and syscall-exit-stop are
1797indistinguishable from each other by the tracer.
1798The tracer needs to keep track of the sequence of
4d12a715 1799ptrace-stops in order to not misinterpret syscall-enter-stop as
8b20acd1 1800syscall-exit-stop or vice versa.
ff20e9ca 1801In general, a syscall-enter-stop is
181f997f
MK
1802always followed by syscall-exit-stop,
1803.B PTRACE_EVENT
ff20e9ca 1804stop, or the tracee's death;
181f997f 1805no other kinds of ptrace-stop can occur in between.
5419141e 1806However, note that seccomp stops (see below) can cause syscall-exit-stops,
0d5b85ca 1807without preceding syscall-entry-stops.
ff20e9ca
MK
1808If seccomp is in use, care needs
1809to be taken not to misinterpret such stops as syscall-entry-stops.
dd3568a1 1810.PP
181f997f
MK
1811If after syscall-enter-stop,
1812the tracer uses a restarting command other than
1813.BR PTRACE_SYSCALL ,
1814syscall-exit-stop is not generated.
dd3568a1 1815.PP
181f997f
MK
1816.B PTRACE_GETSIGINFO
1817on syscall-stops returns
1818.B SIGTRAP
1819in
1820.IR si_signo ,
1821with
1822.I si_code
1823set to
1824.B SIGTRAP
1825or
1826.IR (SIGTRAP|0x80) .
ff20e9ca
MK
1827.\"
1828.SS PTRACE_EVENT_SECCOMP stops (Linux 3.5 to 4.7)
5419141e
KF
1829The behavior of
1830.BR PTRACE_EVENT_SECCOMP
1831stops and their interaction with other kinds
ff20e9ca
MK
1832of ptrace stops has changed between kernel versions.
1833This documents the behavior
1834from their introduction until Linux 4.7 (inclusive).
1835The behavior in later kernel versions is documented in the next section.
efeece04 1836.PP
5419141e
KF
1837A
1838.BR PTRACE_EVENT_SECCOMP
1839stop occurs whenever a
1840.BR SECCOMP_RET_TRACE
ff20e9ca
MK
1841rule is triggered.
1842This is independent of which methods was used to restart the system call.
1843Notably, seccomp still runs even if the tracee was restarted using
5419141e
KF
1844.BR PTRACE_SYSEMU
1845and this system call is unconditionally skipped.
efeece04 1846.PP
5419141e 1847Restarts from this stop will behave as if the stop had occurred right
ff20e9ca
MK
1848before the system call in question.
1849In particular, both
5419141e
KF
1850.BR PTRACE_SYSCALL
1851and
1852.BR PTRACE_SYSEMU
ff20e9ca
MK
1853will normally cause a subsequent syscall-entry-stop.
1854However, if after the
5419141e 1855.BR PTRACE_EVENT_SECCOMP
ff20e9ca
MK
1856the system call number is negative,
1857both the syscall-entry-stop and the system call itself will be skipped.
1858This means that if the system call number is negative after a
5419141e
KF
1859.BR PTRACE_EVENT_SECCOMP
1860and the tracee is restarted using
f7111396 1861.BR PTRACE_SYSCALL ,
5419141e 1862the next observed stop will be a syscall-exit-stop,
ff20e9ca
MK
1863rather than the syscall-entry-stop that might have been expected.
1864.\"
1865.SS PTRACE_EVENT_SECCOMP stops (since Linux 4.8)
1866Starting with Linux 4.8,
1867.\" commit 93e35efb8de45393cf61ed07f7b407629bf698ea
1868the
5419141e 1869.BR PTRACE_EVENT_SECCOMP
ff20e9ca
MK
1870stop was reordered to occur between syscall-entry-stop and
1871syscall-exit-stop.
1872Note that seccomp no longer runs (and no
1873.B PTRACE_EVENT_SECCOMP
1874will be reported) if the system call is skipped due to
1875.BR PTRACE_SYSEMU .
efeece04 1876.PP
ff20e9ca
MK
1877Functionally, a
1878.B PTRACE_EVENT_SECCOMP
1879stop functions comparably
1880to a syscall-entry-stop (i.e., continuations using
5419141e 1881.BR PTRACE_SYSCALL
ff20e9ca
MK
1882will cause syscall-exit-stops,
1883the system call number may be changed and any other modified registers
1884are visible to the to-be-executed system call as well).
1885Note that there may be,
0d5b85ca 1886but need not have been a preceding syscall-entry-stop.
efeece04 1887.PP
5419141e
KF
1888After a
1889.BR PTRACE_EVENT_SECCOMP
ff20e9ca 1890stop, seccomp will be rerun, with a
5419141e
KF
1891.BR SECCOMP_RET_TRACE
1892rule now functioning the same as a
ff20e9ca
MK
1893.BR SECCOMP_RET_ALLOW .
1894Specifically, this means that if registers are not modified during the
5419141e
KF
1895.BR PTRACE_EVENT_SECCOMP
1896stop, the system call will then be allowed.
ff20e9ca 1897.\"
131bcd7a 1898.SS PTRACE_SINGLESTEP stops
b8d02d56 1899[Details of these kinds of stops are yet to be documented.]
181f997f 1900.\"
bea08fec 1901.\" FIXME .
131bcd7a 1902.\" document stops occurring with PTRACE_SINGLESTEP
ff20e9ca 1903.\"
4d12a715 1904.SS Informational and restarting ptrace commands
181f997f
MK
1905Most ptrace commands (all except
1906.BR PTRACE_ATTACH ,
ba8f446e 1907.BR PTRACE_SEIZE ,
181f997f 1908.BR PTRACE_TRACEME ,
ba8f446e 1909.BR PTRACE_INTERRUPT ,
181f997f
MK
1910and
1911.BR PTRACE_KILL )
1912require the tracee to be in a ptrace-stop, otherwise they fail with
1913.BR ESRCH .
dd3568a1 1914.PP
181f997f
MK
1915When the tracee is in ptrace-stop,
1916the tracer can read and write data to
1917the tracee using informational commands.
1918These commands leave the tracee in ptrace-stopped state:
dd3568a1 1919.PP
408731d4
MK
1920.in +4n
1921.EX
1922ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1923ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1924ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1925ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1926ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov);
1927ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov);
1928ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1929ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1930ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1931ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1932.EE
1933.in
dd3568a1 1934.PP
8b20acd1 1935Note that some errors are not reported.
181f997f
MK
1936For example, setting signal information
1937.RI ( siginfo )
4d12a715 1938may have no effect in some ptrace-stops, yet the call may succeed
181f997f
MK
1939(return 0 and not set
1940.IR errno );
1941querying
1942.B PTRACE_GETEVENTMSG
1943may succeed and return some random value if current ptrace-stop
1944is not documented as returning a meaningful event message.
dd3568a1 1945.PP
181f997f 1946The call
efeece04 1947.PP
181f997f 1948 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
efeece04 1949.PP
181f997f
MK
1950affects one tracee.
1951The tracee's current flags are replaced.
1952Flags are inherited by new tracees created and "auto-attached" via active
1953.BR PTRACE_O_TRACEFORK ,
1954.BR PTRACE_O_TRACEVFORK ,
1955or
1956.BR PTRACE_O_TRACECLONE
1957options.
dd3568a1 1958.PP
181f997f
MK
1959Another group of commands makes the ptrace-stopped tracee run.
1960They have the form:
dd3568a1 1961.PP
8898a252 1962 ptrace(cmd, pid, 0, sig);
dd3568a1 1963.PP
181f997f
MK
1964where
1965.I cmd
1966is
1967.BR PTRACE_CONT ,
ba8f446e 1968.BR PTRACE_LISTEN ,
181f997f
MK
1969.BR PTRACE_DETACH ,
1970.BR PTRACE_SYSCALL ,
1971.BR PTRACE_SINGLESTEP ,
1972.BR PTRACE_SYSEMU ,
1973or
a5c725cf 1974.BR PTRACE_SYSEMU_SINGLESTEP .
181f997f
MK
1975If the tracee is in signal-delivery-stop,
1976.I sig
1977is the signal to be injected (if it is nonzero).
1978Otherwise,
1979.I sig
1980may be ignored.
8898a252
MK
1981(When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
1982recommended practice is to always pass 0 in
a5c725cf 1983.IR sig .)
4d12a715 1984.SS Attaching and detaching
181f997f 1985A thread can be attached to the tracer using the call
efeece04 1986.PP
181f997f 1987 ptrace(PTRACE_ATTACH, pid, 0, 0);
efeece04 1988.PP
ba8f446e 1989or
efeece04 1990.PP
ba8f446e 1991 ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags);
efeece04 1992.PP
ba8f446e
DV
1993.B PTRACE_ATTACH
1994sends
181f997f
MK
1995.B SIGSTOP
1996to this thread.
1997If the tracer wants this
1998.B SIGSTOP
1999to have no effect, it needs to suppress it.
2000Note that if other signals are concurrently sent to
2001this thread during attach,
2002the tracer may see the tracee enter signal-delivery-stop
2003with other signal(s) first!
2004The usual practice is to reinject these signals until
2005.B SIGSTOP
2006is seen, then suppress
2007.B SIGSTOP
2008injection.
181f997f
MK
2009The design bug here is that a ptrace attach and a concurrently delivered
2010.B SIGSTOP
2011may race and the concurrent
2012.B SIGSTOP
2013may be lost.
2014.\"
3b1fdaf3 2015.\" FIXME Describe how to attach to a thread which is already group-stopped.
dd3568a1 2016.PP
181f997f
MK
2017Since attaching sends
2018.B SIGSTOP
2019and the tracer usually suppresses it, this may cause a stray
a5c725cf 2020.B EINTR
181f997f 2021return from the currently executing system call in the tracee,
a5c725cf 2022as described in the "Signal injection and suppression" section.
dd3568a1 2023.PP
f04ba477 2024Since Linux 3.4,
ba8f446e
DV
2025.B PTRACE_SEIZE
2026can be used instead of
2027.BR PTRACE_ATTACH .
2028.B PTRACE_SEIZE
e3948c69
MK
2029does not stop the attached process.
2030If you need to stop
ba8f446e
DV
2031it after attach (or at any other time) without sending it any signals,
2032use
2033.B PTRACE_INTERRUPT
2034command.
dd3568a1 2035.PP
181f997f 2036The request
efeece04 2037.PP
181f997f 2038 ptrace(PTRACE_TRACEME, 0, 0, 0);
efeece04 2039.PP
181f997f
MK
2040turns the calling thread into a tracee.
2041The thread continues to run (doesn't enter ptrace-stop).
2042A common practice is to follow the
2043.B PTRACE_TRACEME
2044with
efeece04 2045.PP
181f997f 2046 raise(SIGSTOP);
efeece04 2047.PP
181f997f 2048and allow the parent (which is our tracer now) to observe our
4d12a715 2049signal-delivery-stop.
dd3568a1 2050.PP
d6e37473 2051If the
181f997f
MK
2052.BR PTRACE_O_TRACEFORK ,
2053.BR PTRACE_O_TRACEVFORK ,
2054or
2055.BR PTRACE_O_TRACECLONE
2056options are in effect, then children created by, respectively,
2057.BR vfork (2)
2058or
2059.BR clone (2)
2060with the
2061.B CLONE_VFORK
2062flag,
2063.BR fork (2)
2064or
2065.BR clone (2)
2066with the exit signal set to
2067.BR SIGCHLD ,
2068and other kinds of
2069.BR clone (2),
2070are automatically attached to the same tracer which traced their parent.
2071.B SIGSTOP
2072is delivered to the children, causing them to enter
2073signal-delivery-stop after they exit the system call which created them.
dd3568a1 2074.PP
181f997f 2075Detaching of the tracee is performed by:
efeece04 2076.PP
181f997f 2077 ptrace(PTRACE_DETACH, pid, 0, sig);
efeece04 2078.PP
181f997f
MK
2079.B PTRACE_DETACH
2080is a restarting operation;
2081therefore it requires the tracee to be in ptrace-stop.
2082If the tracee is in signal-delivery-stop, a signal can be injected.
2083Otherwise, the
2084.I sig
2085parameter may be silently ignored.
dd3568a1 2086.PP
181f997f
MK
2087If the tracee is running when the tracer wants to detach it,
2088the usual solution is to send
2089.B SIGSTOP
2090(using
2091.BR tgkill (2),
2092to make sure it goes to the correct thread),
2093wait for the tracee to stop in signal-delivery-stop for
2094.B SIGSTOP
2095and then detach it (suppressing
2096.B SIGSTOP
2097injection).
2098A design bug is that this can race with concurrent
2099.BR SIGSTOP s.
2100Another complication is that the tracee may enter other ptrace-stops
2101and needs to be restarted and waited for again, until
2102.B SIGSTOP
2103is seen.
2104Yet another complication is to be sure that
2105the tracee is not already ptrace-stopped,
2106because no signal delivery happens while it is\(emnot even
2107.BR SIGSTOP .
3b1fdaf3
MK
2108.\" FIXME Describe how to detach from a group-stopped tracee so that it
2109.\" doesn't run, but continues to wait for SIGCONT.
dd3568a1 2110.PP
181f997f 2111If the tracer dies, all tracees are automatically detached and restarted,
8b20acd1 2112unless they were in group-stop.
b8d02d56
MK
2113Handling of restart from group-stop is currently buggy,
2114but the "as planned" behavior is to leave tracee stopped and waiting for
181f997f
MK
2115.BR SIGCONT .
2116If the tracee is restarted from signal-delivery-stop,
2117the pending signal is injected.
2118.SS execve(2) under ptrace
cb729171 2119.\" clone(2) CLONE_THREAD says:
181f997f
MK
2120.\" If any of the threads in a thread group performs an execve(2),
2121.\" then all threads other than the thread group leader are terminated,
d6e37473 2122.\" and the new program is executed in the thread group leader.
181f997f 2123.\"
8898a252 2124When one thread in a multithreaded process calls
181f997f
MK
2125.BR execve (2),
2126the kernel destroys all other threads in the process,
2127.\" In kernel 3.1 sources, see fs/exec.c::de_thread()
2128and resets the thread ID of the execing thread to the
2129thread group ID (process ID).
181f997f
MK
2130(Or, to put things another way, when a multithreaded process does an
2131.BR execve (2),
8898a252 2132at completion of the call, it appears as though the
181f997f
MK
2133.BR execve (2)
2134occurred in the thread group leader, regardless of which thread did the
2135.BR execve (2).)
181f997f
MK
2136This resetting of the thread ID looks very confusing to tracers:
2137.IP * 3
2138All other threads stop in
8898a252 2139.B PTRACE_EVENT_EXIT
b8d02d56 2140stop, if the
8898a252
MK
2141.BR PTRACE_O_TRACEEXIT
2142option was turned on.
181f997f
MK
2143Then all other threads except the thread group leader report
2144death as if they exited via
2145.BR _exit (2)
2146with exit code 0.
b8d02d56 2147.IP *
181f997f
MK
2148The execing tracee changes its thread ID while it is in the
2149.BR execve (2).
2150(Remember, under ptrace, the "pid" returned from
2151.BR waitpid (2),
2152or fed into ptrace calls, is the tracee's thread ID.)
2153That is, the tracee's thread ID is reset to be the same as its process ID,
2154which is the same as the thread group leader's thread ID.
2155.IP *
f098951d
DV
2156Then a
2157.B PTRACE_EVENT_EXEC
2158stop happens, if the
2159.BR PTRACE_O_TRACEEXEC
2160option was turned on.
2161.IP *
2162If the thread group leader has reported its
2163.B PTRACE_EVENT_EXIT
2164stop by this time,
181f997f
MK
2165it appears to the tracer that
2166the dead thread leader "reappears from nowhere".
a17e05c5 2167(Note: the thread group leader does not report death via
f098951d
DV
2168.I WIFEXITED(status)
2169until there is at least one other live thread.
a17e05c5 2170This eliminates the possibility that the tracer will see
f098951d 2171it dying and then reappearing.)
181f997f
MK
2172If the thread group leader was still alive,
2173for the tracer this may look as if thread group leader
2174returns from a different system call than it entered,
2175or even "returned from a system call even though
2176it was not in any system call".
2177If the thread group leader was not traced
2178(or was traced by a different tracer), then during
2179.BR execve (2)
2180it will appear as if it has become a tracee of
2181the tracer of the execing tracee.
dd3568a1 2182.PP
181f997f
MK
2183All of the above effects are the artifacts of
2184the thread ID change in the tracee.
dd3568a1 2185.PP
181f997f
MK
2186The
2187.B PTRACE_O_TRACEEXEC
2188option is the recommended tool for dealing with this situation.
b8d02d56 2189First, it enables
a5c725cf
DP
2190.BR PTRACE_EVENT_EXEC
2191stop,
b8d02d56 2192which occurs before
a5c725cf 2193.BR execve (2)
b8d02d56
MK
2194returns.
2195In this stop, the tracer can use
2196.B PTRACE_GETEVENTMSG
2197to retrieve the tracee's former thread ID.
94e66ffd 2198(This feature was introduced in Linux 3.0.)
b8d02d56
MK
2199Second, the
2200.B PTRACE_O_TRACEEXEC
2201option disables legacy
2202.B SIGTRAP
2203generation on
2204.BR execve (2).
dd3568a1 2205.PP
181f997f
MK
2206When the tracer receives
2207.B PTRACE_EVENT_EXEC
2208stop notification,
2209it is guaranteed that except this tracee and the thread group leader,
2210no other threads from the process are alive.
dd3568a1 2211.PP
181f997f
MK
2212On receiving the
2213.B PTRACE_EVENT_EXEC
2214stop notification,
2215the tracer should clean up all its internal
2216data structures describing the threads of this process,
2217and retain only one data structure\(emone which
2218describes the single still running tracee, with
efeece04 2219.PP
f098951d 2220 thread ID == thread group ID == process ID.
dd3568a1 2221.PP
181f997f
MK
2222Example: two threads call
2223.BR execve (2)
2224at the same time:
dd3568a1 2225.PP
4d12a715 2226.nf
a5c725cf 2227*** we get syscall-enter-stop in thread 1: **
4d12a715
DV
2228PID1 execve("/bin/foo", "foo" <unfinished ...>
2229*** we issue PTRACE_SYSCALL for thread 1 **
a5c725cf 2230*** we get syscall-enter-stop in thread 2: **
4d12a715
DV
2231PID2 execve("/bin/bar", "bar" <unfinished ...>
2232*** we issue PTRACE_SYSCALL for thread 2 **
2233*** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
2234*** we get syscall-exit-stop for PID0: **
2235PID0 <... execve resumed> ) = 0
2236.fi
dd3568a1 2237.PP
181f997f
MK
2238If the
2239.B PTRACE_O_TRACEEXEC
2240option is
2241.I not
28e2ca57 2242in effect for the execing tracee,
53cdec41 2243and if the tracee was
28e2ca57
DV
2244.BR PTRACE_ATTACH ed
2245rather that
2246.BR PTRACE_SEIZE d,
2247the kernel delivers an extra
181f997f
MK
2248.B SIGTRAP
2249to the tracee after
2250.BR execve (2)
8b20acd1
MK
2251returns.
2252This is an ordinary signal (similar to one which can be
181f997f
MK
2253generated by
2254.IR "kill -TRAP" ),
2255not a special kind of ptrace-stop.
2256Employing
2257.B PTRACE_GETSIGINFO
2258for this signal returns
2259.I si_code
2260set to 0
2261.RI ( SI_USER ).
2262This signal may be blocked by signal mask,
2263and thus may be delivered (much) later.
dd3568a1 2264.PP
181f997f
MK
2265Usually, the tracer (for example,
2266.BR strace (1))
2267would not want to show this extra post-execve
2268.B SIGTRAP
2269signal to the user, and would suppress its delivery to the tracee (if
2270.B SIGTRAP
2271is set to
2272.BR SIG_DFL ,
2273it is a killing signal).
d6e37473 2274However, determining
181f997f
MK
2275.I which
2276.B SIGTRAP
2277to suppress is not easy.
2278Setting the
2279.B PTRACE_O_TRACEEXEC
28e2ca57
DV
2280option or using
2281.B PTRACE_SEIZE
2282and thus suppressing this extra
181f997f
MK
2283.B SIGTRAP
2284is the recommended approach.
4d12a715 2285.SS Real parent
181f997f
MK
2286The ptrace API (ab)uses the standard UNIX parent/child signaling over
2287.BR waitpid (2).
2288This used to cause the real parent of the process to stop receiving
2289several kinds of
2290.BR waitpid (2)
2291notifications when the child process is traced by some other process.
dd3568a1 2292.PP
181f997f
MK
2293Many of these bugs have been fixed, but as of Linux 2.6.38 several still
2294exist; see BUGS below.
dd3568a1 2295.PP
181f997f
MK
2296As of Linux 2.6.38, the following is believed to work correctly:
2297.IP * 3
dc85ba7c
MK
2298exit/death by signal is reported first to the tracer, then,
2299when the tracer consumes the
181f997f
MK
2300.BR waitpid (2)
2301result, to the real parent (to the real parent only when the
2302whole multithreaded process exits).
181f997f
MK
2303If the tracer and the real parent are the same process,
2304the report is sent only once.
47297adb 2305.SH RETURN VALUE
051ec121 2306On success, the
78686915 2307.B PTRACE_PEEK*
051ec121 2308requests return the requested data (but see NOTES),
e7a758e3
JH
2309the
2310.B PTRACE_SECCOMP_GET_FILTER
2311request returns the number of instructions in the BPF program, and
2312other requests return zero.
dd3568a1 2313.PP
2b2581ee
MK
2314On error, all requests return \-1, and
2315.I errno
2316is set appropriately.
8bd58774 2317Since the value returned by a successful
0daa9e92 2318.B PTRACE_PEEK*
181f997f 2319request may be \-1, the caller must clear
2b2581ee 2320.I errno
181f997f
MK
2321before the call, and then check it afterward
2322to determine whether or not an error occurred.
2b2581ee
MK
2323.SH ERRORS
2324.TP
2325.B EBUSY
181f997f 2326(i386 only) There was an error with allocating or freeing a debug register.
2b2581ee
MK
2327.TP
2328.B EFAULT
2329There was an attempt to read from or write to an invalid area in
181f997f 2330the tracer's or the tracee's memory,
2b2581ee
MK
2331probably because the area wasn't mapped or accessible.
2332Unfortunately, under Linux, different variations of this fault
2f0af33b
MK
2333will return
2334.B EIO
2335or
2336.B EFAULT
2337more or less arbitrarily.
2b2581ee
MK
2338.TP
2339.B EINVAL
2340An attempt was made to set an invalid option.
2341.TP
2342.B EIO
181f997f
MK
2343.I request
2344is invalid, or an attempt was made to read from or
2345write to an invalid area in the tracer's or the tracee's memory,
2b2581ee
MK
2346or there was a word-alignment violation,
2347or an invalid signal was specified during a restart request.
2348.TP
2349.B EPERM
2350The specified process cannot be traced.
2351This could be because the
4d12a715 2352tracer has insufficient privileges (the required capability is
2b2581ee 2353.BR CAP_SYS_PTRACE );
00b08db3 2354unprivileged processes cannot trace processes that they
2b2581ee
MK
2355cannot send signals to or those running
2356set-user-ID/set-group-ID programs, for obvious reasons.
181f997f
MK
2357Alternatively, the process may already be being traced,
2358or (on kernels before 2.6.26) be
e8906093 2359.BR init (1)
2b2581ee
MK
2360(PID 1).
2361.TP
2362.B ESRCH
2363The specified process does not exist, or is not currently being traced
181f997f
MK
2364by the caller, or is not stopped
2365(for requests that require a stopped tracee).
47297adb 2366.SH CONFORMING TO
44a2c328 2367SVr4, 4.3BSD.
fea681da
MK
2368.SH NOTES
2369Although arguments to
e511ffb6 2370.BR ptrace ()
c13182ef 2371are interpreted according to the prototype given,
5260fe08 2372glibc currently declares
e511ffb6 2373.BR ptrace ()
181f997f
MK
2374as a variadic function with only the
2375.I request
2376argument fixed.
ca302d0e
DV
2377It is recommended to always supply four arguments,
2378even if the requested operation does not use them,
2379setting unused/ignored arguments to
2380.I 0L
2381or
2382.IR "(void\ *)\ 0".
dd3568a1 2383.PP
181f997f
MK
2384In Linux kernels before 2.6.26,
2385.\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
e8906093 2386.BR init (1),
181f997f 2387the process with PID 1, may not be traced.
dd3568a1 2388.PP
674f11ec
JH
2389A tracees parent continues to be the tracer even if that tracer calls
2390.BR execve (2).
dd3568a1 2391.PP
181f997f
MK
2392The layout of the contents of memory and the USER area are
2393quite operating-system- and architecture-specific.
8660aec0
MK
2394The offset supplied, and the data returned,
2395might not entirely match with the definition of
2396.IR "struct user" .
2397.\" See http://lkml.org/lkml/2008/5/8/375
dd3568a1 2398.PP
181f997f 2399The size of a "word" is determined by the operating-system variant
3e18f289 2400(e.g., for 32-bit Linux it is 32 bits).
dd3568a1 2401.PP
fea681da 2402This page documents the way the
e511ffb6 2403.BR ptrace ()
c13182ef 2404call works currently in Linux.
07318a59 2405Its behavior differs significantly on other flavors of UNIX.
e63ad01d 2406In any case, use of
e511ffb6 2407.BR ptrace ()
181f997f 2408is highly specific to the operating system and architecture.
4978c606 2409.\"
ace93363
MK
2410.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2411.\"
2412.SS Ptrace access mode checking
2413Various parts of the kernel-user-space API (not just
bf7bc8b8 2414.BR ptrace ()
00172d8d
MK
2415operations), require so-called "ptrace access mode" checks,
2416whose outcome determines whether an operation is permitted
2417(or, in a few cases, causes a "read" operation to return sanitized data).
2418These checks are performed in cases where one process can
2419inspect sensitive information about,
2420or in some cases modify the state of, another process.
2421The checks are based on factors such as the credentials and capabilities
2422of the two processes,
2423whether or not the "target" process is dumpable,
2424and the results of checks performed by any enabled Linux Security Module
2425(LSM)\(emfor example, SELinux, Yama, or Smack\(emand by the commoncap LSM
611d3ac4 2426(which is always invoked).
efeece04 2427.PP
be26fa86 2428Prior to Linux 2.6.27, all access checks were of a single type.
ace93363
MK
2429Since Linux 2.6.27,
2430.\" commit 006ebb40d3d65338bd74abb03b945f8d60e362bd
2431two access mode levels are distinguished:
2432.TP
2433.BR PTRACE_MODE_READ
2434For "read" operations or other operations that are less dangerous,
2435such as:
2436.BR get_robust_list (2);
2437.BR kcmp (2);
2438reading
2439.IR /proc/[pid]/auxv ,
2440.IR /proc/[pid]/environ ,
2441or
2442.IR /proc/[pid]/stat ;
2443or
2444.BR readlink (2)
2445of a
2446.IR /proc/[pid]/ns/*
2447file.
2448.TP
2449.BR PTRACE_MODE_ATTACH
2450For "write" operations, or other operations that are more dangerous,
2451such as: ptrace attaching
2452.RB ( PTRACE_ATTACH )
2453to another process
2454or calling
2455.BR process_vm_writev (2).
2456.RB ( PTRACE_MODE_ATTACH
2457was effectively the default before Linux 2.6.27.)
bcd0d82d
MK
2458.\"
2459.\" Regarding the above description of the distinction between
2460.\" PTRACE_MODE_READ and PTRACE_MODE_ATTACH, Stephen Smalley notes:
2461.\"
2462.\" That was the intent when the distinction was introduced, but it doesn't
2463.\" appear to have been properly maintained, e.g. there is now a common
2464.\" helper lock_trace() that is used for
2465.\" /proc/pid/{stack,syscall,personality} but checks PTRACE_MODE_ATTACH, and
2466.\" PTRACE_MODE_ATTACH is also used in timerslack_ns_write/show(). Likely
2467.\" should review and make them consistent. There was also some debate
2468.\" about proper handling of /proc/pid/fd. Arguably that one might belong
2469.\" back in the _ATTACH camp.
2470.\"
ace93363
MK
2471.PP
2472Since Linux 4.5,
2473.\" commit caaee6234d05a58c5b4d05e7bf766131b810a657
611d3ac4 2474the above access mode checks are combined (ORed) with
ace93363
MK
2475one of the following modifiers:
2476.TP
2477.B PTRACE_MODE_FSCREDS
2478Use the caller's filesystem UID and GID (see
2479.BR credentials (7))
2480or effective capabilities for LSM checks.
2481.TP
2482.B PTRACE_MODE_REALCREDS
2483Use the caller's real UID and GID or permitted capabilities for LSM checks.
2484This was effectively the default before Linux 4.5.
2485.PP
2486Because combining one of the credential modifiers with one of
2487the aforementioned access modes is typical,
2488some macros are defined in the kernel sources for the combinations:
2489.TP
2490.B PTRACE_MODE_READ_FSCREDS
2491Defined as
2492.BR "PTRACE_MODE_READ | PTRACE_MODE_FSCREDS" .
2493.TP
2494.B PTRACE_MODE_READ_REALCREDS
2495Defined as
2496.BR "PTRACE_MODE_READ | PTRACE_MODE_REALCREDS" .
2497.TP
2498.B PTRACE_MODE_ATTACH_FSCREDS
2499Defined as
2500.BR "PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS" .
2501.TP
2502.B PTRACE_MODE_ATTACH_REALCREDS
2503Defined as
2504.BR "PTRACE_MODE_ATTACH | PTRACE_MODE_REALCREDS" .
ace93363
MK
2505.PP
2506One further modifier can be ORed with the access mode:
2507.TP
2508.BR PTRACE_MODE_NOAUDIT " (since Linux 3.3)"
2509.\" commit 69f594a38967f4540ce7a29b3fd214e68a8330bd
2510.\" Just for /proc/pid/stat
2511Don't audit this access mode check.
3cd161fe
SS
2512This modifier is employed for ptrace access mode checks
2513(such as checks when reading
2514.IR /proc/[pid]/stat )
2515that merely cause the output to be filtered or sanitized,
2516rather than causing an error to be returned to the caller.
2517In these cases, accessing the file is not a security violation and
2518there is no reason to generate a security audit record.
2519This modifier suppresses the generation of
2520such an audit record for the particular access check.
ace93363 2521.PP
edb73684
MK
2522Note that all of the
2523.BR PTRACE_MODE_*
2524constants described in this subsection are kernel-internal,
2525and not visible to user space.
2526The constant names are mentioned here in order to label the various kinds of
2527ptrace access mode checks that are performed for various system calls
2528and accesses to various pseudofiles (e.g., under
2529.IR /proc ).
32245813 2530These names are used in other manual pages to provide a simple
edb73684 2531shorthand for labeling the different kernel checks.
efeece04 2532.PP
ace93363
MK
2533The algorithm employed for ptrace access mode checking determines whether
2534the calling process is allowed to perform the corresponding action
a330bffa
MK
2535on the target process.
2536(In the case of opening
2537.IR /proc/[pid]
2538files, the "calling process" is the one opening the file,
2539and the process with the corresponding PID is the "target process".)
2540The algorithm is as follows:
9edaeefb 2541.IP 1. 3
ace93363
MK
2542If the calling thread and the target thread are in the same
2543thread group, access is always allowed.
2544.IP 2.
2545If the access mode specifies
2546.BR PTRACE_MODE_FSCREDS ,
78f07865
MK
2547then, for the check in the next step,
2548employ the caller's filesystem UID and GID.
2549(As noted in
2550.BR credentials (7),
2551the filesystem UID and GID almost always have the same values
2552as the corresponding effective IDs.)
efeece04 2553.IP
78f07865 2554Otherwise, the access mode specifies
ace93363 2555.BR PTRACE_MODE_REALCREDS ,
78f07865
MK
2556so use the caller's real UID and GID for the checks in the next step.
2557(Most APIs that check the caller's UID and GID use the effective IDs.
2558For historical reasons, the
2559.BR PTRACE_MODE_REALCREDS
2560check uses the real IDs instead.)
ace93363
MK
2561.IP 3.
2562Deny access if
2563.I neither
2564of the following is true:
2565.RS
2566.IP \(bu 2
2567The real, effective, and saved-set user IDs of the target
2568match the caller's user ID,
2569.IR and
2570the real, effective, and saved-set group IDs of the target
2571match the caller's group ID.
2572.IP \(bu
2573The caller has the
2574.B CAP_SYS_PTRACE
0647331a 2575capability in the user namespace of the target.
ace93363
MK
2576.RE
2577.IP 4.
2578Deny access if the target process "dumpable" attribute has a value other than 1
2579.RB ( SUID_DUMP_USER ;
2580see the discussion of
2581.BR PR_SET_DUMPABLE
2582in
2583.BR prctl (2)),
2584and the caller does not have the
2585.BR CAP_SYS_PTRACE
2586capability in the user namespace of the target process.
2587.IP 5.
2588The kernel LSM
2589.IR security_ptrace_access_check ()
2590interface is invoked to see if ptrace access is permitted.
b0459842 2591The results depend on the LSM(s).
611d3ac4 2592The implementation of this interface in the commoncap LSM performs
ace93363
MK
2593the following steps:
2594.\" (in cap_ptrace_access_check()):
2595.RS
2596.IP a) 3
2597If the access mode includes
2598.BR PTRACE_MODE_FSCREDS ,
2599then use the caller's
2600.I effective
2601capability set
2602in the following check;
2603otherwise (the access mode specifies
2604.BR PTRACE_MODE_REALCREDS ,
2605so) use the caller's
2606.I permitted
2607capability set.
2608.IP b)
2609Deny access if
2610.I neither
2611of the following is true:
2612.RS
2613.IP \(bu 2
0647331a 2614The caller and the target process are in the same user namespace,
173eb06c 2615and the caller's capabilities are a superset of the target process's
ace93363
MK
2616.I permitted
2617capabilities.
2618.IP \(bu
2619The caller has the
2620.B CAP_SYS_PTRACE
2621capability in the target process's user namespace.
2622.RE
2623.IP
611d3ac4 2624Note that the commoncap LSM does not distinguish between
ace93363
MK
2625.B PTRACE_MODE_READ
2626and
2627.BR PTRACE_MODE_ATTACH .
2628.RE
2629.IP 6.
2630If access has not been denied by any of the preceding steps,
2631then access is allowed.
2632.\"
2633.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2634.\"
4978c606 2635.SS /proc/sys/kernel/yama/ptrace_scope
e5323616
MK
2636On systems with the Yama Linux Security Module (LSM) installed
2637(i.e., the kernel was configured with
2638.BR CONFIG_SECURITY_YAMA ),
2639the
4978c606 2640.I /proc/sys/kernel/yama/ptrace_scope
94b0464c 2641file (available since Linux 3.4)
4978c606
MK
2642.\" commit 2d514487faf188938a4ee4fb3464eeecfbdcf8eb
2643can be used to restrict the ability to trace a process with
bf7bc8b8 2644.BR ptrace ()
4978c606
MK
2645(and thus also the ability to use tools such as
2646.BR strace (1)
2647and
2648.BR gdb (1)).
2649The goal of such restrictions is to prevent attack escalation whereby
2650a compromised process can ptrace-attach to other sensitive processes
2651(e.g., a GPG agent or an SSH session) owned by the user in order
028b5760
MK
2652to gain additional credentials that may exist in memory
2653and thus expand the scope of the attack.
efeece04 2654.PP
e5323616
MK
2655More precisely, the Yama LSM limits two types of operations:
2656.IP * 3
2657Any operation that performs a ptrace access mode
2658.BR PTRACE_MODE_ATTACH
2659check\(emfor example,
2660.BR ptrace ()
2661.BR PTRACE_ATTACH .
2662(See the "Ptrace access mode checking" discussion above.)
efeece04 2663.IP
e5323616
MK
2664.IP *
2665.BR ptrace ()
2666.BR PTRACE_TRACEME .
2667.PP
2668A process that has the
4978c606 2669.B CAP_SYS_PTRACE
e5323616
MK
2670capability can update the
2671.IR /proc/sys/kernel/yama/ptrace_scope
2672file with one of the following values:
4978c606
MK
2673.TP
26740 ("classic ptrace permissions")
e5323616
MK
2675No additional restrictions on operations that perform
2676.BR PTRACE_MODE_ATTACH
2677checks (beyond those imposed by the commoncap and other LSMs).
efeece04 2678.IP
4978c606
MK
2679The use of
2680.BR PTRACE_TRACEME
2681is unchanged.
2682.TP
e5323616
MK
26831 ("restricted ptrace") [default value]
2684When performing an operation that requires a
2685.BR PTRACE_MODE_ATTACH
d5765e27
MK
2686check, the calling process must either have the
2687.B CAP_SYS_PTRACE
2688capability in the user namespace of the target process or
e48ed83a 2689it must have a predefined relationship with the target process.
4978c606 2690By default,
e5323616 2691the predefined relationship is that the target process
028b5760 2692must be a descendant of the caller.
efeece04 2693.IP
e5323616 2694A target process can employ the
4978c606
MK
2695.BR prctl (2)
2696.B PR_SET_PTRACER
028b5760 2697operation to declare an additional PID that is allowed to perform
e5323616
MK
2698.BR PTRACE_MODE_ATTACH
2699operations on the target.
2700See the kernel source file
6744a500
ES
2701.IR Documentation/admin\-guide/LSM/Yama.rst
2702.\" commit 90bb766440f2147486a2acc3e793d7b8348b0c22
2703(or
4978c606 2704.IR Documentation/security/Yama.txt
6744a500 2705before Linux 4.13)
e5323616 2706for further details.
efeece04 2707.IP
4978c606
MK
2708The use of
2709.BR PTRACE_TRACEME
2710is unchanged.
2711.TP
27122 ("admin-only attach")
2713Only processes with the
2714.B CAP_SYS_PTRACE
d5765e27 2715capability in the user namespace of the target process may perform
e5323616
MK
2716.BR PTRACE_MODE_ATTACH
2717operations or trace children that employ
4978c606
MK
2718.BR PTRACE_TRACEME .
2719.TP
27203 ("no attach")
e5323616
MK
2721No process may perform
2722.BR PTRACE_MODE_ATTACH
2723operations or trace children that employ
4978c606 2724.BR PTRACE_TRACEME .
efeece04 2725.IP
4978c606 2726Once this value has been written to the file, it cannot be changed.
d5765e27
MK
2727.PP
2728With respect to values 1 and 2,
028b5760
MK
2729note that creating a new user namespace effectively removes the
2730protection offered by Yama.
2731This is because a process in the parent user namespace whose effective
2732UID matches the UID of the creator of a child namespace
2733has all capabilities (including
2734.BR CAP_SYS_PTRACE )
2735when performing operations within the child user namespace
2736(and further-removed descendants of that namespace).
2737Consequently, when a process tries to use user namespaces to sandbox itself,
2738it inadvertently weakens the protections offered by the Yama LSM.
4978c606 2739.\"
e5323616
MK
2740.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2741.\"
0722a578 2742.SS C library/kernel differences
53a99749
MK
2743At the system call level, the
2744.BR PTRACE_PEEKTEXT ,
2745.BR PTRACE_PEEKDATA ,
2746and
2747.BR PTRACE_PEEKUSER
2748requests have a different API: they store the result
2749at the address specified by the
2750.I data
2751parameter, and the return value is the error flag.
2752The glibc wrapper function provides the API given in DESCRIPTION above,
2753with the result being returned via the function return value.
a1d5f77c 2754.SH BUGS
8bd58774 2755On hosts with 2.6 kernel headers,
0daa9e92 2756.B PTRACE_SETOPTIONS
181f997f
MK
2757is declared with a different value than the one for 2.4.
2758This leads to applications compiled with 2.6 kernel
a1d5f77c 2759headers failing when run on 2.4 kernels.
8bd58774 2760This can be worked around by redefining
0daa9e92 2761.B PTRACE_SETOPTIONS
8bd58774
MK
2762to
2763.BR PTRACE_OLDSETOPTIONS ,
2764if that is defined.
dd3568a1 2765.PP
181f997f 2766Group-stop notifications are sent to the tracer, but not to real parent.
4d12a715 2767Last confirmed on 2.6.38.6.
dd3568a1 2768.PP
181f997f
MK
2769If a thread group leader is traced and exits by calling
2770.BR _exit (2),
8898a252
MK
2771.\" Note from Denys Vlasenko:
2772.\" Here "exits" means any kind of death - _exit, exit_group,
2773.\" signal death. Signal death and exit_group cases are trivial,
2774.\" though: since signal death and exit_group kill all other threads
2775.\" too, "until all other threads exit" thing happens rather soon
2776.\" in these cases. Therefore, only _exit presents observably
2777.\" puzzling behavior to ptrace users: thread leader _exit's,
2778.\" but WIFEXITED isn't reported! We are trying to explain here
2779.\" why it is so.
181f997f
MK
2780a
2781.B PTRACE_EVENT_EXIT
2782stop will happen for it (if requested), but the subsequent
2783.B WIFEXITED
2784notification will not be delivered until all other threads exit.
2785As explained above, if one of other threads calls
2786.BR execve (2),
2787the death of the thread group leader will
2788.I never
2789be reported.
2790If the execed thread is not traced by this tracer,
2791the tracer will never know that
2792.BR execve (2)
4d12a715 2793happened.
181f997f
MK
2794One possible workaround is to
2795.B PTRACE_DETACH
2796the thread group leader instead of restarting it in this case.
2797Last confirmed on 2.6.38.6.
bea08fec 2798.\" FIXME . need to test/verify this scenario
dd3568a1 2799.PP
181f997f
MK
2800A
2801.B SIGKILL
2802signal may still cause a
2803.B PTRACE_EVENT_EXIT
2804stop before actual signal death.
2805This may be changed in the future;
2806.B SIGKILL
2807is meant to always immediately kill tasks even under ptrace.
55bd9495 2808Last confirmed on Linux 3.13.
dd3568a1 2809.PP
a17e05c5 2810Some system calls return with
f098951d 2811.B EINTR
a17e05c5
MK
2812if a signal was sent to a tracee, but delivery was suppressed by the tracer.
2813(This is very typical operation: it is usually
f098951d 2814done by debuggers on every attach, in order to not introduce
a17e05c5
MK
2815a bogus
2816.BR SIGSTOP ).
2817As of Linux 3.2.9, the following system calls are affected
2818(this list is likely incomplete):
f098951d 2819.BR epoll_wait (2),
a17e05c5 2820and
f098951d 2821.BR read (2)
a17e05c5
MK
2822from an
2823.BR inotify (7)
2824file descriptor.
ca302d0e
DV
2825The usual symptom of this bug is that when you attach to
2826a quiescent process with the command
efeece04 2827.PP
408731d4
MK
2828.in +4n
2829.EX
2830strace \-p <process-ID>
2831.EE
2832.in
efeece04 2833.PP
ca302d0e
DV
2834then, instead of the usual
2835and expected one-line output such as
408731d4
MK
2836.PP
2837.in +4n
2838.EX
2839restart_syscall(<... resuming interrupted call ...>_
2840.EE
2841.in
2842.PP
ca302d0e 2843or
408731d4
MK
2844.PP
2845.in +4n
2846.EX
2847select(6, [5], NULL, [5], NULL_
2848.EE
2849.in
2850.PP
ca302d0e
DV
2851('_' denotes the cursor position), you observe more than one line.
2852For example:
408731d4
MK
2853.PP
2854.in +4n
2855.EX
ca302d0e
DV
2856 clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0
2857 epoll_wait(4,_
408731d4
MK
2858.EE
2859.in
2860.PP
ca302d0e
DV
2861What is not visible here is that the process was blocked in
2862.BR epoll_wait (2)
2863before
2864.BR strace (1)
2865has attached to it.
2866Attaching caused
2867.BR epoll_wait (2)
7fac88a9 2868to return to user space with the error
ca302d0e
DV
2869.BR EINTR .
2870In this particular case, the program reacted to
2871.B EINTR
b0b1d9b5 2872by checking the current time, and then executing
ca302d0e
DV
2873.BR epoll_wait (2)
2874again.
2875(Programs which do not expect such "stray"
2876.BR EINTR
2877errors may behave in an unintended way upon an
2878.BR strace (1)
2879attach.)
c62b9453
JH
2880.PP
2881Contrary to the normal rules, the glibc wrapper for
2882.BR ptrace ()
2883can set
2884.I errno
2885to zero.
47297adb 2886.SH SEE ALSO
fea681da 2887.BR gdb (1),
53506ea9 2888.BR ltrace (1),
fea681da 2889.BR strace (1),
181f997f 2890.BR clone (2),
fea681da
MK
2891.BR execve (2),
2892.BR fork (2),
181f997f 2893.BR gettid (2),
d901e325 2894.BR prctl (2),
3b4a59c4 2895.BR seccomp (2),
181f997f
MK
2896.BR sigaction (2),
2897.BR tgkill (2),
2898.BR vfork (2),
2899.BR waitpid (2),
fea681da 2900.BR exec (3),
181f997f
MK
2901.BR capabilities (7),
2902.BR signal (7)