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