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