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