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