]>
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 MK |
9 | .\" and Copyright (c) 2011, Denys Vlasenko <vda.linux@googlemail.com> |
10 | .\" | |
1dd72f9c | 11 | .\" %%%LICENSE_START(GPLv2+_DOC_FULL) |
fea681da MK |
12 | .\" This is free documentation; you can redistribute it and/or |
13 | .\" modify it under the terms of the GNU General Public License as | |
14 | .\" published by the Free Software Foundation; either version 2 of | |
15 | .\" the License, or (at your option) any later version. | |
16 | .\" | |
17 | .\" The GNU General Public License's references to "object code" | |
18 | .\" and "executables" are to be interpreted as the output of any | |
19 | .\" document formatting or typesetting system, including | |
20 | .\" intermediate and printed output. | |
21 | .\" | |
22 | .\" This manual is distributed in the hope that it will be useful, | |
23 | .\" but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | .\" GNU General Public License for more details. | |
26 | .\" | |
27 | .\" You should have received a copy of the GNU General Public | |
c715f741 MK |
28 | .\" License along with this manual; if not, see |
29 | .\" <http://www.gnu.org/licenses/>. | |
6a8d8745 | 30 | .\" %%%LICENSE_END |
fea681da MK |
31 | .\" |
32 | .\" Modified Fri Jul 23 23:47:18 1993 by Rik Faith <faith@cs.unc.edu> | |
33 | .\" Modified Fri Jan 31 16:46:30 1997 by Eric S. Raymond <esr@thyrsus.com> | |
34 | .\" Modified Thu Oct 7 17:28:49 1999 by Andries Brouwer <aeb@cwi.nl> | |
c11b1abf | 35 | .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
36 | .\" Added notes on capability requirements |
37 | .\" | |
44b35ee0 MK |
38 | .\" 2006-03-24, Chuck Ebbert <76306.1226@compuserve.com> |
39 | .\" Added PTRACE_SETOPTIONS, PTRACE_GETEVENTMSG, PTRACE_GETSIGINFO, | |
40 | .\" PTRACE_SETSIGINFO, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP | |
41 | .\" (Thanks to Blaisorblade, Daniel Jacobowitz and others who helped.) | |
181f997f | 42 | .\" 2011-09, major update by Denys Vlasenko <vda.linux@googlemail.com> |
44b35ee0 | 43 | .\" |
f04ba477 | 44 | .TH PTRACE 2 2013-02-16 "Linux" "Linux Programmer's Manual" |
fea681da MK |
45 | .SH NAME |
46 | ptrace \- process trace | |
47 | .SH SYNOPSIS | |
44b35ee0 | 48 | .nf |
fea681da MK |
49 | .B #include <sys/ptrace.h> |
50 | .sp | |
44b35ee0 MK |
51 | .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", " |
52 | .BI " void *" addr ", void *" data ); | |
53 | .fi | |
fea681da MK |
54 | .SH DESCRIPTION |
55 | The | |
e511ffb6 | 56 | .BR ptrace () |
181f997f MK |
57 | system call provides a means by which one process (the "tracer") |
58 | may observe and control the execution of another process (the "tracee"), | |
59 | and examine and change the tracee's memory and registers. | |
e63ad01d | 60 | It is primarily used to implement breakpoint debugging and system |
fea681da MK |
61 | call tracing. |
62 | .LP | |
8898a252 | 63 | A tracee first needs to be attached to the tracer. |
181f997f MK |
64 | Attachment and subsequent commands are per thread: |
65 | in a multithreaded process, | |
66 | every thread can be individually attached to a | |
67 | (potentially different) tracer, | |
68 | or left not attached and thus not debugged. | |
69 | Therefore, "tracee" always means "(one) thread", | |
70 | never "a (possibly multithreaded) process". | |
8b20acd1 | 71 | Ptrace commands are always sent to |
181f997f MK |
72 | a specific tracee using a call of the form |
73 | ||
74 | ptrace(PTRACE_foo, pid, ...) | |
75 | ||
76 | where | |
77 | .I pid | |
78 | is the thread ID of the corresponding Linux thread. | |
79 | .LP | |
8898a252 MK |
80 | (Note that in this page, a "multithreaded process" |
81 | means a thread group consisting of threads created using the | |
82 | .BR clone (2) | |
83 | .B CLONE_THREAD | |
84 | flag.) | |
85 | .LP | |
181f997f | 86 | A process can initiate a trace by calling |
c13182ef | 87 | .BR fork (2) |
8bd58774 MK |
88 | and having the resulting child do a |
89 | .BR PTRACE_TRACEME , | |
e63ad01d | 90 | followed (typically) by an |
4d12a715 | 91 | .BR execve (2). |
181f997f | 92 | Alternatively, one process may commence tracing another process using |
ba8f446e DV |
93 | .B PTRACE_ATTACH |
94 | or | |
95 | .BR PTRACE_SEIZE . | |
fea681da | 96 | .LP |
4d12a715 | 97 | While being traced, the tracee will stop each time a signal is delivered, |
c13182ef | 98 | even if the signal is being ignored. |
181f997f | 99 | (An exception is |
8bd58774 MK |
100 | .BR SIGKILL , |
101 | which has its usual effect.) | |
181f997f MK |
102 | The tracer will be notified at its next call to |
103 | .BR waitpid (2) | |
8898a252 MK |
104 | (or one of the related "wait" system calls); that call will return a |
105 | .I status | |
106 | value containing information that indicates | |
107 | the cause of the stop in the tracee. | |
108 | While the tracee is stopped, | |
109 | the tracer can use various ptrace requests to inspect and modify the tracee. | |
4d12a715 | 110 | The tracer then causes the tracee to continue, |
e63ad01d | 111 | optionally ignoring the delivered signal |
fea681da MK |
112 | (or even delivering a different signal instead). |
113 | .LP | |
d39a9b98 | 114 | If the |
b16ecdae DV |
115 | .B PTRACE_O_TRACEEXEC |
116 | option is not in effect, all successful calls to | |
117 | .BR execve (2) | |
d39a9b98 | 118 | by the traced process will cause it to be sent a |
b16ecdae | 119 | .B SIGTRAP |
d39a9b98 | 120 | signal, |
b16ecdae DV |
121 | giving the parent a chance to gain control before the new program |
122 | begins execution. | |
123 | .LP | |
181f997f | 124 | When the tracer is finished tracing, it can cause the tracee to continue |
4d12a715 | 125 | executing in a normal, untraced mode via |
8bd58774 | 126 | .BR PTRACE_DETACH . |
fea681da | 127 | .LP |
181f997f MK |
128 | The value of |
129 | .I request | |
130 | determines the action to be performed: | |
fea681da | 131 | .TP |
8bd58774 | 132 | .B PTRACE_TRACEME |
181f997f | 133 | Indicate that this process is to be traced by its parent. |
c13182ef MK |
134 | A process probably shouldn't make this request if its parent |
135 | isn't expecting to trace it. | |
181f997f MK |
136 | .RI ( pid , |
137 | .IR addr , | |
138 | and | |
139 | .IR data | |
140 | are ignored.) | |
fea681da | 141 | .LP |
181f997f MK |
142 | The |
143 | .B PTRACE_TRACEME | |
144 | request is used only by the tracee; | |
145 | the remaining requests are used only by the tracer. | |
146 | In the following requests, | |
147 | .I pid | |
148 | specifies the thread ID of the tracee to be acted on. | |
8bd58774 | 149 | For requests other than |
ba8f446e DV |
150 | .BR PTRACE_ATTACH , |
151 | .BR PTRACE_SEIZE , | |
152 | .B PTRACE_INTERRUPT | |
b16ecdae | 153 | and |
8bd58774 | 154 | .BR PTRACE_KILL , |
4d12a715 | 155 | the tracee must be stopped. |
fea681da | 156 | .TP |
8bd58774 | 157 | .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA |
181f997f | 158 | Read a word at the address |
0daa9e92 | 159 | .I addr |
4d12a715 | 160 | in the tracee's memory, returning the word as the result of the |
e511ffb6 | 161 | .BR ptrace () |
c13182ef | 162 | call. |
181f997f MK |
163 | Linux does not have separate text and data address spaces, |
164 | so these two requests are currently equivalent. | |
165 | .RI ( data | |
166 | is ignored.) | |
fea681da | 167 | .TP |
428d3520 | 168 | .B PTRACE_PEEKUSER |
254255af MK |
169 | .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER, |
170 | .\" and that is the name that seems common on other systems. | |
181f997f | 171 | Read a word at offset |
fea681da | 172 | .I addr |
4d12a715 | 173 | in the tracee's USER area, |
8bd58774 | 174 | which holds the registers and other information about the process |
181f997f MK |
175 | (see |
176 | .IR <sys/user.h> ). | |
e63ad01d | 177 | The word is returned as the result of the |
e511ffb6 | 178 | .BR ptrace () |
c13182ef | 179 | call. |
181f997f | 180 | Typically, the offset must be word-aligned, though this might vary by |
8660aec0 MK |
181 | architecture. |
182 | See NOTES. | |
181f997f MK |
183 | .RI ( data |
184 | is ignored.) | |
fea681da | 185 | .TP |
8bd58774 | 186 | .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA |
181f997f | 187 | Copy the word |
0daa9e92 | 188 | .I data |
181f997f | 189 | to the address |
0daa9e92 | 190 | .I addr |
4d12a715 | 191 | in the tracee's memory. |
181f997f | 192 | As for |
d6e37473 | 193 | .BR PTRACE_PEEKTEXT |
181f997f MK |
194 | and |
195 | .BR PTRACE_PEEKDATA , | |
196 | these two requests are currently equivalent. | |
fea681da | 197 | .TP |
428d3520 | 198 | .B PTRACE_POKEUSER |
254255af MK |
199 | .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER, |
200 | .\" and that is the name that seems common on other systems. | |
181f997f | 201 | Copy the word |
0daa9e92 | 202 | .I data |
fea681da MK |
203 | to offset |
204 | .I addr | |
4d12a715 | 205 | in the tracee's USER area. |
181f997f MK |
206 | As for |
207 | .BR PTRACE_PEEKUSER , | |
208 | the offset must typically be word-aligned. | |
c13182ef | 209 | In order to maintain the integrity of the kernel, |
8bd58774 | 210 | some modifications to the USER area are disallowed. |
181f997f | 211 | .\" FIXME In the preceding sentence, which modifications are disallowed, |
7fac88a9 | 212 | .\" and when they are disallowed, how does user space discover that fact? |
fea681da | 213 | .TP |
8bd58774 | 214 | .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS |
92f9c09b | 215 | Copy the tracee's general-purpose or floating-point registers, |
181f997f MK |
216 | respectively, to the address |
217 | .I data | |
218 | in the tracer. | |
219 | See | |
220 | .I <sys/user.h> | |
221 | for information on the format of this data. | |
222 | .RI ( addr | |
223 | is ignored.) | |
50fe8d53 MK |
224 | Note that SPARC systems have the meaning of |
225 | .I data | |
226 | and | |
227 | .I addr | |
228 | reversed; that is, | |
229 | .I data | |
230 | is ignored and the registers are copied to the address | |
231 | .IR addr . | |
34709982 MK |
232 | .B PTRACE_GETREGS |
233 | and | |
234 | .B PTRACE_GETFPREGS | |
235 | are not present on all architectures. | |
fea681da | 236 | .TP |
ba8f446e DV |
237 | .BR PTRACE_GETREGSET " (since Linux 2.6.34)" |
238 | Read the tracee's registers. | |
239 | .I addr | |
f04ba477 | 240 | specifies, in an architecture-dependent way, the type of registers to be read. |
ba8f446e DV |
241 | .B NT_PRSTATUS |
242 | (with numerical value 1) | |
f04ba477 MK |
243 | usually results in reading of general-purpose registers. |
244 | If the CPU has, for example, | |
ba8f446e DV |
245 | floating-point and/or vector registers, they can be retrieved by setting |
246 | .I addr | |
f04ba477 | 247 | to the corresponding |
ba8f446e DV |
248 | .B NT_foo |
249 | constant. | |
250 | .I data | |
251 | points to a | |
252 | .BR "struct iovec" , | |
f42ce0a5 | 253 | which describes the destination buffer's location and length. |
f04ba477 | 254 | On return, the kernel modifies |
ba8f446e | 255 | .B iov.len |
f04ba477 | 256 | to indicate the actual number of bytes returned. |
ba8f446e | 257 | .TP |
8bd58774 | 258 | .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)" |
44b35ee0 | 259 | Retrieve information about the signal that caused the stop. |
181f997f MK |
260 | Copy a |
261 | .I siginfo_t | |
262 | structure (see | |
44b35ee0 | 263 | .BR sigaction (2)) |
181f997f MK |
264 | from the tracee to the address |
265 | .I data | |
266 | in the tracer. | |
267 | .RI ( addr | |
268 | is ignored.) | |
44b35ee0 | 269 | .TP |
6beb1671 | 270 | .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS |
ba8f446e | 271 | Modify the tracee's general-purpose or floating-point registers, |
181f997f MK |
272 | respectively, from the address |
273 | .I data | |
274 | in the tracer. | |
8bd58774 MK |
275 | As for |
276 | .BR PTRACE_POKEUSER , | |
a42c0c5a | 277 | some general-purpose register modifications may be disallowed. |
181f997f | 278 | .\" FIXME In the preceding sentence, which modifications are disallowed, |
7fac88a9 | 279 | .\" and when they are disallowed, how does user space discover that fact? |
181f997f MK |
280 | .RI ( addr |
281 | is ignored.) | |
50fe8d53 MK |
282 | Note that SPARC systems have the meaning of |
283 | .I data | |
284 | and | |
285 | .I addr | |
286 | reversed; that is, | |
287 | .I data | |
288 | is ignored and the registers are copied from the address | |
289 | .IR addr . | |
34709982 MK |
290 | .B PTRACE_SETREGS |
291 | and | |
292 | .B PTRACE_SETFPREGS | |
293 | are not present on all architectures. | |
fea681da | 294 | .TP |
ba8f446e | 295 | .BR PTRACE_SETREGSET " (since Linux 2.6.34)" |
f04ba477 MK |
296 | Modify the tracee's registers. |
297 | The meaning of | |
ba8f446e DV |
298 | .I addr |
299 | and | |
300 | .I data | |
301 | is analogous to | |
302 | .BR PTRACE_GETREGSET . | |
303 | .TP | |
8bd58774 | 304 | .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)" |
181f997f MK |
305 | Set signal information: |
306 | copy a | |
307 | .I siginfo_t | |
308 | structure from the address | |
309 | .I data | |
310 | in the tracer to the tracee. | |
311 | This will affect only signals that would normally be delivered to | |
4d12a715 | 312 | the tracee and were caught by the tracer. |
c13182ef | 313 | It may be difficult to tell |
44b35ee0 MK |
314 | these normal signals from synthetic signals generated by |
315 | .BR ptrace () | |
8660aec0 | 316 | itself. |
181f997f MK |
317 | .RI ( addr |
318 | is ignored.) | |
44b35ee0 | 319 | .TP |
8bd58774 | 320 | .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)" |
181f997f MK |
321 | Set ptrace options from |
322 | .IR data . | |
323 | .RI ( addr | |
324 | is ignored.) | |
325 | .IR data | |
326 | is interpreted as a bit mask of options, | |
327 | which are specified by the following flags: | |
cc7d99c8 | 328 | .RS |
b89e39ef MK |
329 | .TP |
330 | .BR PTRACE_O_EXITKILL " (since Linux 3.8)" | |
331 | .\" commit 992fb6e170639b0849bace8e49bf31bd37c4123 | |
332 | If a tracer sets this flag, a | |
333 | .B SIGKILL | |
9f1b9726 MK |
334 | signal will be sent to every tracee if the tracer exits. |
335 | This option is useful for ptrace jailers that | |
c2b54496 | 336 | want to ensure that tracees can never escape the tracer's control. |
44b35ee0 | 337 | .TP |
8bd58774 | 338 | .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)" |
4d12a715 | 339 | Stop the tracee at the next |
0bfa087b | 340 | .BR clone (2) |
181f997f MK |
341 | and automatically start tracing the newly cloned process, |
342 | which will start with a | |
8bd58774 | 343 | .BR SIGSTOP . |
8898a252 MK |
344 | A |
345 | .BR waitpid (2) | |
dc85ba7c | 346 | by the tracer will return a |
8898a252 | 347 | .I status |
dc85ba7c MK |
348 | value such that |
349 | ||
350 | .nf | |
351 | status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8)) | |
352 | .fi | |
353 | ||
181f997f | 354 | The PID of the new process can be retrieved with |
8bd58774 | 355 | .BR PTRACE_GETEVENTMSG . |
181f997f | 356 | .IP |
44b35ee0 | 357 | This option may not catch |
0bfa087b | 358 | .BR clone (2) |
c13182ef | 359 | calls in all cases. |
4d12a715 | 360 | If the tracee calls |
0bfa087b | 361 | .BR clone (2) |
8bd58774 | 362 | with the |
0daa9e92 | 363 | .B CLONE_VFORK |
8bd58774 MK |
364 | flag, |
365 | .B PTRACE_EVENT_VFORK | |
366 | will be delivered instead | |
367 | if | |
368 | .B PTRACE_O_TRACEVFORK | |
4d12a715 | 369 | is set; otherwise if the tracee calls |
0bfa087b | 370 | .BR clone (2) |
8bd58774 MK |
371 | with the exit signal set to |
372 | .BR SIGCHLD , | |
373 | .B PTRACE_EVENT_FORK | |
181f997f | 374 | will be delivered if |
8bd58774 MK |
375 | .B PTRACE_O_TRACEFORK |
376 | is set. | |
44b35ee0 | 377 | .TP |
8bd58774 | 378 | .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)" |
4d12a715 | 379 | Stop the tracee at the next |
181f997f | 380 | .BR execve (2). |
8898a252 MK |
381 | A |
382 | .BR waitpid (2) | |
dc85ba7c | 383 | by the tracer will return a |
8898a252 | 384 | .I status |
dc85ba7c MK |
385 | value such that |
386 | ||
387 | .nf | |
388 | status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) | |
389 | .fi | |
390 | ||
8f318249 MK |
391 | If the execing thread is not a thread group leader, |
392 | the thread ID is reset to thread group leader's ID before this stop. | |
b16d33ef DV |
393 | Since Linux 3.0, the former thread ID can be retrieved with |
394 | .BR PTRACE_GETEVENTMSG . | |
44b35ee0 | 395 | .TP |
8bd58774 | 396 | .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)" |
181f997f | 397 | Stop the tracee at exit. |
8898a252 MK |
398 | A |
399 | .BR waitpid (2) | |
dc85ba7c | 400 | by the tracer will return a |
8898a252 | 401 | .I status |
dc85ba7c MK |
402 | value such that |
403 | ||
404 | .nf | |
405 | status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8)) | |
406 | .fi | |
407 | ||
4d12a715 | 408 | The tracee's exit status can be retrieved with |
8bd58774 | 409 | .BR PTRACE_GETEVENTMSG . |
181f997f MK |
410 | .IP |
411 | The tracee is stopped early during process exit, | |
412 | when registers are still available, | |
413 | allowing the tracer to see where the exit occurred, | |
c13182ef | 414 | whereas the normal exit notification is done after the process |
e63ad01d | 415 | is finished exiting. |
181f997f MK |
416 | Even though context is available, |
417 | the tracer cannot prevent the exit from happening at this point. | |
cc7d99c8 MK |
418 | .TP |
419 | .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)" | |
420 | Stop the tracee at the next | |
421 | .BR fork (2) | |
422 | and automatically start tracing the newly forked process, | |
423 | which will start with a | |
424 | .BR SIGSTOP . | |
425 | A | |
426 | .BR waitpid (2) | |
427 | by the tracer will return a | |
428 | .I status | |
429 | value such that | |
430 | ||
431 | .nf | |
432 | status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8)) | |
433 | .fi | |
434 | ||
435 | The PID of the new process can be retrieved with | |
436 | .BR PTRACE_GETEVENTMSG . | |
cc7d99c8 MK |
437 | .TP |
438 | .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)" | |
439 | When delivering system call traps, set bit 7 in the signal number | |
440 | (i.e., deliver | |
441 | .IR "SIGTRAP|0x80" ). | |
442 | This makes it easy for the tracer to distinguish | |
443 | normal traps from those caused by a system call. | |
444 | .RB ( PTRACE_O_TRACESYSGOOD | |
445 | may not work on all architectures.) | |
446 | .TP | |
447 | .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)" | |
448 | Stop the tracee at the next | |
449 | .BR vfork (2) | |
450 | and automatically start tracing the newly vforked process, | |
451 | which will start with a | |
452 | .BR SIGSTOP . | |
453 | A | |
454 | .BR waitpid (2) | |
455 | by the tracer will return a | |
456 | .I status | |
457 | value such that | |
458 | ||
459 | .nf | |
460 | status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8)) | |
461 | .fi | |
462 | ||
463 | The PID of the new process can be retrieved with | |
464 | .BR PTRACE_GETEVENTMSG . | |
465 | .TP | |
466 | .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)" | |
467 | Stop the tracee at the completion of the next | |
468 | .BR vfork (2). | |
469 | A | |
470 | .BR waitpid (2) | |
471 | by the tracer will return a | |
472 | .I status | |
473 | value such that | |
474 | ||
475 | .nf | |
476 | status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8)) | |
477 | .fi | |
478 | ||
479 | The PID of the new process can (since Linux 2.6.18) be retrieved with | |
480 | .BR PTRACE_GETEVENTMSG . | |
44b35ee0 MK |
481 | .RE |
482 | .TP | |
8bd58774 | 483 | .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)" |
c13182ef MK |
484 | Retrieve a message (as an |
485 | .IR "unsigned long" ) | |
44b35ee0 | 486 | about the ptrace event |
181f997f MK |
487 | that just happened, placing it at the address |
488 | .I data | |
489 | in the tracer. | |
8bd58774 | 490 | For |
181f997f | 491 | .BR PTRACE_EVENT_EXIT , |
4d12a715 | 492 | this is the tracee's exit status. |
8bd58774 MK |
493 | For |
494 | .BR PTRACE_EVENT_FORK , | |
181f997f MK |
495 | .BR PTRACE_EVENT_VFORK , |
496 | .BR PTRACE_EVENT_VFORK_DONE , | |
8bd58774 | 497 | and |
181f997f MK |
498 | .BR PTRACE_EVENT_CLONE , |
499 | this is the PID of the new process. | |
500 | .RI ( addr | |
501 | is ignored.) | |
44b35ee0 | 502 | .TP |
8bd58774 | 503 | .B PTRACE_CONT |
181f997f MK |
504 | Restart the stopped tracee process. |
505 | If | |
506 | .I data | |
507 | is nonzero, | |
508 | it is interpreted as the number of a signal to be delivered to the tracee; | |
c13182ef | 509 | otherwise, no signal is delivered. |
4d12a715 DV |
510 | Thus, for example, the tracer can control |
511 | whether a signal sent to the tracee is delivered or not. | |
181f997f MK |
512 | .RI ( addr |
513 | is ignored.) | |
fea681da | 514 | .TP |
8bd58774 | 515 | .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP |
181f997f | 516 | Restart the stopped tracee as for |
8bd58774 | 517 | .BR PTRACE_CONT , |
181f997f MK |
518 | but arrange for the tracee to be stopped at |
519 | the next entry to or exit from a system call, | |
c13182ef | 520 | or after execution of a single instruction, respectively. |
4d12a715 DV |
521 | (The tracee will also, as usual, be stopped upon receipt of a signal.) |
522 | From the tracer's perspective, the tracee will appear to have been | |
8bd58774 MK |
523 | stopped by receipt of a |
524 | .BR SIGTRAP . | |
525 | So, for | |
526 | .BR PTRACE_SYSCALL , | |
527 | for example, the idea is to inspect | |
c13182ef | 528 | the arguments to the system call at the first stop, |
8bd58774 MK |
529 | then do another |
530 | .B PTRACE_SYSCALL | |
181f997f | 531 | and inspect the return value of the system call at the second stop. |
94cffcd7 MK |
532 | The |
533 | .I data | |
534 | argument is treated as for | |
535 | .BR PTRACE_CONT . | |
a5c725cf | 536 | .RI ( addr |
181f997f | 537 | is ignored.) |
fea681da | 538 | .TP |
6beb1671 | 539 | .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)" |
8bd58774 MK |
540 | For |
541 | .BR PTRACE_SYSEMU , | |
181f997f | 542 | continue and stop on entry to the next system call, |
c13182ef | 543 | which will not be executed. |
8bd58774 MK |
544 | For |
545 | .BR PTRACE_SYSEMU_SINGLESTEP , | |
181f997f | 546 | do the same but also singlestep if not a system call. |
c13182ef | 547 | This call is used by programs like |
4d12a715 | 548 | User Mode Linux that want to emulate all the tracee's system calls. |
94cffcd7 MK |
549 | The |
550 | .I data | |
551 | argument is treated as for | |
552 | .BR PTRACE_CONT . | |
34709982 MK |
553 | The |
554 | .I addr | |
555 | argument is ignored. | |
556 | These requests are currently | |
557 | .\" As at 3.7 | |
d2ea1bd4 | 558 | supported only on x86. |
44b35ee0 | 559 | .TP |
ba8f446e DV |
560 | .BR PTRACE_LISTEN " (since Linux 3.4)" |
561 | Restart the stopped tracee, but prevent it from executing. | |
562 | The resulting state of the tracee is similar to a process which | |
f04ba477 MK |
563 | has been stopped by a |
564 | .B SIGSTOP | |
565 | (or other stopping signal). | |
ba8f446e DV |
566 | See the "group-stop" subsection for additional information. |
567 | .B PTRACE_LISTEN | |
33a0ccb2 | 568 | works only on tracees attached by |
ba8f446e DV |
569 | .BR PTRACE_SEIZE . |
570 | .TP | |
8bd58774 | 571 | .B PTRACE_KILL |
181f997f | 572 | Send the tracee a |
8bd58774 MK |
573 | .B SIGKILL |
574 | to terminate it. | |
181f997f MK |
575 | .RI ( addr |
576 | and | |
577 | .I data | |
578 | are ignored.) | |
579 | .IP | |
580 | .I This operation is deprecated; do not use it! | |
581 | Instead, send a | |
582 | .BR SIGKILL | |
583 | directly using | |
584 | .BR kill (2) | |
585 | or | |
586 | .BR tgkill (2). | |
587 | The problem with | |
588 | .B PTRACE_KILL | |
589 | is that it requires the tracee to be in signal-delivery-stop, | |
590 | otherwise it may not work | |
591 | (i.e., may complete successfully but won't kill the tracee). | |
592 | By contrast, sending a | |
593 | .B SIGKILL | |
594 | directly has no such limitation. | |
8898a252 MK |
595 | .\" [Note from Denys Vlasenko: |
596 | .\" deprecation suggested by Oleg Nesterov. He prefers to deprecate it | |
597 | .\" instead of describing (and needing to support) PTRACE_KILL's quirks.] | |
fea681da | 598 | .TP |
ba8f446e | 599 | .BR PTRACE_INTERRUPT " (since Linux 3.4)" |
f04ba477 MK |
600 | Stop a tracee. |
601 | If the tracee is running, it will stop with | |
ba8f446e | 602 | .BR PTRACE_EVENT_STOP . |
f04ba477 | 603 | If the tracee is already stopped by a signal, or receives a signal |
ba8f446e DV |
604 | in parallel with |
605 | .BR PTRACE_INTERRUPT , | |
606 | it may report a group-stop | |
607 | or a signal-delivery-stop instead of | |
608 | .BR PTRACE_EVENT_STOP . | |
609 | .B PTRACE_INTERRUPT | |
610 | only works on tracees attached by | |
611 | .BR PTRACE_SEIZE . | |
612 | .TP | |
8bd58774 | 613 | .B PTRACE_ATTACH |
181f997f | 614 | Attach to the process specified in |
fea681da | 615 | .IR pid , |
4d12a715 | 616 | making it a tracee of the calling process. |
8898a252 MK |
617 | .\" No longer true (removed by Denys Vlasenko, 2011, who remarks: |
618 | .\" "I think it isn't true in non-ancient 2.4 and in 2.6/3.x. | |
619 | .\" Basically, it's not true for any Linux in practical use. | |
4d12a715 DV |
620 | .\" ; the behavior of the tracee is as if it had done a |
621 | .\" .BR PTRACE_TRACEME . | |
622 | .\" The calling process actually becomes the parent of the tracee | |
623 | .\" process for most purposes (e.g., it will receive | |
624 | .\" notification of tracee events and appears in | |
625 | .\" .BR ps (1) | |
626 | .\" output as the tracee's parent), but a | |
627 | .\" .BR getppid (2) | |
628 | .\" by the tracee will still return the PID of the original parent. | |
629 | The tracee is sent a | |
8bd58774 MK |
630 | .BR SIGSTOP , |
631 | but will not necessarily have stopped | |
e63ad01d | 632 | by the completion of this call; use |
181f997f | 633 | .BR waitpid (2) |
8b20acd1 | 634 | to wait for the tracee to stop. |
181f997f MK |
635 | See the "Attaching and detaching" subsection for additional information. |
636 | .RI ( addr | |
637 | and | |
638 | .I data | |
639 | are ignored.) | |
fea681da | 640 | .TP |
ba8f446e DV |
641 | .BR PTRACE_SEIZE " (since Linux 3.4)" |
642 | Attach to the process specified in | |
643 | .IR pid , | |
644 | making it a tracee of the calling process. | |
645 | Unlike | |
646 | .BR PTRACE_ATTACH , | |
647 | .B PTRACE_SEIZE | |
f04ba477 MK |
648 | does not stop the process. |
649 | Only a | |
ba8f446e DV |
650 | .BR PTRACE_SEIZE d |
651 | process can accept | |
652 | .B PTRACE_INTERRUPT | |
653 | and | |
654 | .B PTRACE_LISTEN | |
655 | commands. | |
656 | .I addr | |
657 | must be zero. | |
658 | .I data | |
659 | contains a bit mask of ptrace options to activate immediately. | |
660 | .TP | |
8bd58774 | 661 | .B PTRACE_DETACH |
181f997f | 662 | Restart the stopped tracee as for |
8bd58774 | 663 | .BR PTRACE_CONT , |
181f997f MK |
664 | but first detach from it. |
665 | Under Linux, a tracee can be detached in this way regardless | |
666 | of which method was used to initiate tracing. | |
667 | .RI ( addr | |
668 | is ignored.) | |
4d12a715 | 669 | .SS Death under ptrace |
181f997f MK |
670 | When a (possibly multithreaded) process receives a killing signal |
671 | (one whose disposition is set to | |
672 | .B SIG_DFL | |
673 | and whose default action is to kill the process), | |
8b20acd1 MK |
674 | all threads exit. |
675 | Tracees report their death to their tracer(s). | |
181f997f MK |
676 | Notification of this event is delivered via |
677 | .BR waitpid (2). | |
678 | .LP | |
679 | Note that the killing signal will first cause signal-delivery-stop | |
680 | (on one tracee only), | |
681 | and only after it is injected by the tracer | |
682 | (or after it was dispatched to a thread which isn't traced), | |
683 | will death from the signal happen on | |
684 | .I all | |
685 | tracees within a multithreaded process. | |
686 | (The term "signal-delivery-stop" is explained below.) | |
4d12a715 | 687 | .LP |
181f997f | 688 | .B SIGKILL |
ca302d0e DV |
689 | does not generate signal-delivery-stop and |
690 | therefore the tracer can't suppress it. | |
181f997f MK |
691 | .B SIGKILL |
692 | kills even within system calls | |
693 | (syscall-exit-stop is not generated prior to death by | |
694 | .BR SIGKILL ). | |
695 | The net effect is that | |
696 | .B SIGKILL | |
697 | always kills the process (all its threads), | |
698 | even if some threads of the process are ptraced. | |
699 | .LP | |
700 | When the tracee calls | |
701 | .BR _exit (2), | |
702 | it reports its death to its tracer. | |
4d12a715 DV |
703 | Other threads are not affected. |
704 | .LP | |
181f997f MK |
705 | When any thread executes |
706 | .BR exit_group (2), | |
707 | every tracee in its thread group reports its death to its tracer. | |
4d12a715 | 708 | .LP |
181f997f MK |
709 | If the |
710 | .B PTRACE_O_TRACEEXIT | |
711 | option is on, | |
712 | .B PTRACE_EVENT_EXIT | |
713 | will happen before actual death. | |
714 | This applies to exits via | |
715 | .BR exit (2), | |
716 | .BR exit_group (2), | |
717 | and signal deaths (except | |
718 | .BR SIGKILL ), | |
719 | and when threads are torn down on | |
720 | .BR execve (2) | |
721 | in a multithreaded process. | |
722 | .LP | |
723 | The tracer cannot assume that the ptrace-stopped tracee exists. | |
724 | There are many scenarios when the tracee may die while stopped (such as | |
725 | .BR SIGKILL ). | |
d6e37473 | 726 | Therefore, the tracer must be prepared to handle an |
181f997f MK |
727 | .B ESRCH |
728 | error on any ptrace operation. | |
729 | Unfortunately, the same error is returned if the tracee | |
730 | exists but is not ptrace-stopped | |
731 | (for commands which require a stopped tracee), | |
732 | or if it is not traced by the process which issued the ptrace call. | |
733 | The tracer needs to keep track of the stopped/running state of the tracee, | |
734 | and interpret | |
735 | .B ESRCH | |
736 | as "tracee died unexpectedly" only if it knows that the tracee has | |
737 | been observed to enter ptrace-stop. | |
738 | Note that there is no guarantee that | |
739 | .I waitpid(WNOHANG) | |
740 | will reliably report the tracee's death status if a | |
741 | ptrace operation returned | |
742 | .BR ESRCH . | |
743 | .I waitpid(WNOHANG) | |
744 | may return 0 instead. | |
745 | In other words, the tracee may be "not yet fully dead", | |
746 | but already refusing ptrace requests. | |
747 | .LP | |
748 | The tracer can't assume that the tracee | |
749 | .I always | |
750 | ends its life by reporting | |
751 | .I WIFEXITED(status) | |
752 | or | |
8898a252 MK |
753 | .IR WIFSIGNALED(status) ; |
754 | there are cases where this does not occur. | |
755 | For example, if a thread other than thread group leader does an | |
756 | .BR execve (2), | |
757 | it disappears; | |
758 | its PID will never be seen again, | |
759 | and any subsequent ptrace stops will be reported under | |
760 | the thread group leader's PID. | |
4d12a715 DV |
761 | .SS Stopped states |
762 | A tracee can be in two states: running or stopped. | |
763 | .LP | |
181f997f | 764 | There are many kinds of states when the tracee is stopped, and in ptrace |
8b20acd1 | 765 | discussions they are often conflated. |
181f997f | 766 | Therefore, it is important to use precise terms. |
4d12a715 | 767 | .LP |
181f997f MK |
768 | In this manual page, any stopped state in which the tracee is ready |
769 | to accept ptrace commands from the tracer is called | |
770 | .IR ptrace-stop . | |
8b20acd1 | 771 | Ptrace-stops can |
181f997f MK |
772 | be further subdivided into |
773 | .IR signal-delivery-stop , | |
774 | .IR group-stop , | |
775 | .IR syscall-stop , | |
776 | and so on. | |
777 | These stopped states are described in detail below. | |
778 | .LP | |
779 | When the running tracee enters ptrace-stop, it notifies its tracer using | |
780 | .BR waitpid (2) | |
781 | (or one of the other "wait" system calls). | |
782 | Most of this manual page assumes that the tracer waits with: | |
783 | .LP | |
784 | pid = waitpid(pid_or_minus_1, &status, __WALL); | |
785 | .LP | |
786 | Ptrace-stopped tracees are reported as returns with | |
787 | .I pid | |
788 | greater than 0 and | |
789 | .I WIFSTOPPED(status) | |
790 | true. | |
8898a252 MK |
791 | .\" Denys Vlasenko: |
792 | .\" Do we require __WALL usage, or will just using 0 be ok? (With 0, | |
793 | .\" I am not 100% sure there aren't ugly corner cases.) Are the | |
181f997f MK |
794 | .\" rules different if user wants to use waitid? Will waitid require |
795 | .\" WEXITED? | |
796 | .\" | |
4d12a715 | 797 | .LP |
181f997f MK |
798 | The |
799 | .B __WALL | |
800 | flag does not include the | |
801 | .B WSTOPPED | |
802 | and | |
803 | .B WEXITED | |
804 | flags, but implies their functionality. | |
805 | .LP | |
806 | Setting the | |
807 | .B WCONTINUED | |
808 | flag when calling | |
809 | .BR waitpid (2) | |
810 | is not recommended: the "continued" state is per-process and | |
811 | consuming it can confuse the real parent of the tracee. | |
812 | .LP | |
813 | Use of the | |
814 | .B WNOHANG | |
815 | flag may cause | |
816 | .BR waitpid (2) | |
817 | to return 0 ("no wait results available yet") | |
818 | even if the tracer knows there should be a notification. | |
819 | Example: | |
820 | .nf | |
821 | ||
ca302d0e DV |
822 | errno = 0; |
823 | ptrace(PTRACE_CONT, pid, 0L, 0L); | |
824 | if (errno == ESRCH) { | |
825 | /* tracee is dead */ | |
826 | r = waitpid(tracee, &status, __WALL | WNOHANG); | |
827 | /* r can still be 0 here! */ | |
828 | } | |
181f997f | 829 | .fi |
b8d02d56 | 830 | .\" FIXME: |
181f997f MK |
831 | .\" waitid usage? WNOWAIT? |
832 | .\" describe how wait notifications queue (or not queue) | |
4d12a715 DV |
833 | .LP |
834 | The following kinds of ptrace-stops exist: signal-delivery-stops, | |
a5c725cf DP |
835 | group-stops, |
836 | .B PTRACE_EVENT | |
837 | stops, syscall-stops. | |
181f997f MK |
838 | They all are reported by |
839 | .BR waitpid (2) | |
840 | with | |
841 | .I WIFSTOPPED(status) | |
842 | true. | |
843 | They may be differentiated by examining the value | |
844 | .IR status>>8 , | |
845 | and if there is ambiguity in that value, by querying | |
846 | .BR PTRACE_GETSIGINFO . | |
181f997f MK |
847 | (Note: the |
848 | .I WSTOPSIG(status) | |
dc85ba7c | 849 | macro can't be used to perform this examination, |
8898a252 | 850 | because it returns the value |
0ce81ab5 | 851 | .IR "(status>>8)\ &\ 0xff" .) |
4d12a715 | 852 | .SS Signal-delivery-stop |
181f997f MK |
853 | When a (possibly multithreaded) process receives any signal except |
854 | .BR SIGKILL , | |
855 | the kernel selects an arbitrary thread which handles the signal. | |
856 | (If the signal is generated with | |
857 | .BR tgkill (2), | |
858 | the target thread can be explicitly selected by the caller.) | |
859 | If the selected thread is traced, it enters signal-delivery-stop. | |
860 | At this point, the signal is not yet delivered to the process, | |
861 | and can be suppressed by the tracer. | |
862 | If the tracer doesn't suppress the signal, | |
181f997f | 863 | it passes the signal to the tracee in the next ptrace restart request. |
8b20acd1 | 864 | This second step of signal delivery is called |
181f997f MK |
865 | .I "signal injection" |
866 | in this manual page. | |
867 | Note that if the signal is blocked, | |
868 | signal-delivery-stop doesn't happen until the signal is unblocked, | |
869 | with the usual exception that | |
870 | .B SIGSTOP | |
871 | can't be blocked. | |
872 | .LP | |
873 | Signal-delivery-stop is observed by the tracer as | |
874 | .BR waitpid (2) | |
875 | returning with | |
876 | .I WIFSTOPPED(status) | |
f098951d | 877 | true, with the signal returned by |
181f997f | 878 | .IR WSTOPSIG(status) . |
f098951d | 879 | If the signal is |
181f997f MK |
880 | .BR SIGTRAP , |
881 | this may be a different kind of ptrace-stop; | |
882 | see the "Syscall-stops" and "execve" sections below for details. | |
8b20acd1 | 883 | If |
181f997f MK |
884 | .I WSTOPSIG(status) |
885 | returns a stopping signal, this may be a group-stop; see below. | |
4d12a715 | 886 | .SS Signal injection and suppression |
181f997f MK |
887 | After signal-delivery-stop is observed by the tracer, |
888 | the tracer should restart the tracee with the call | |
4d12a715 | 889 | .LP |
181f997f | 890 | ptrace(PTRACE_restart, pid, 0, sig) |
4d12a715 | 891 | .LP |
181f997f MK |
892 | where |
893 | .B PTRACE_restart | |
894 | is one of the restarting ptrace requests. | |
895 | If | |
896 | .I sig | |
897 | is 0, then a signal is not delivered. | |
898 | Otherwise, the signal | |
899 | .I sig | |
900 | is delivered. | |
901 | This operation is called | |
902 | .I "signal injection" | |
903 | in this manual page, to distinguish it from signal-delivery-stop. | |
904 | .LP | |
8898a252 | 905 | The |
181f997f MK |
906 | .I sig |
907 | value may be different from the | |
908 | .I WSTOPSIG(status) | |
909 | value: the tracer can cause a different signal to be injected. | |
910 | .LP | |
911 | Note that a suppressed signal still causes system calls to return | |
8b20acd1 | 912 | prematurely. |
f098951d | 913 | In this case system calls will be restarted: the tracer will |
a17e05c5 | 914 | observe the tracee to reexecute the interrupted system call (or |
a5c725cf | 915 | .BR restart_syscall (2) |
f098951d DV |
916 | system call for a few syscalls which use a different mechanism |
917 | for restarting) if the tracer uses | |
918 | .BR PTRACE_SYSCALL . | |
919 | Even system calls (such as | |
a5c725cf | 920 | .BR poll (2)) |
f098951d | 921 | which are not restartable after signal are restarted after |
a17e05c5 MK |
922 | signal is suppressed; |
923 | however, kernel bugs exist which cause some syscalls to fail with | |
181f997f MK |
924 | .B EINTR |
925 | even though no observable signal is injected to the tracee. | |
4d12a715 | 926 | .LP |
8898a252 | 927 | Restarting ptrace commands issued in ptrace-stops other than |
181f997f MK |
928 | signal-delivery-stop are not guaranteed to inject a signal, even if |
929 | .I sig | |
8b20acd1 | 930 | is nonzero. |
181f997f MK |
931 | No error is reported; a nonzero |
932 | .I sig | |
933 | may simply be ignored. | |
934 | Ptrace users should not try to "create a new signal" this way: use | |
935 | .BR tgkill (2) | |
936 | instead. | |
4d12a715 | 937 | .LP |
8898a252 MK |
938 | The fact that signal injection requests may be ignored |
939 | when restarting the tracee after | |
940 | ptrace stops that are not signal-delivery-stops | |
941 | is a cause of confusion among ptrace users. | |
181f997f MK |
942 | One typical scenario is that the tracer observes group-stop, |
943 | mistakes it for signal-delivery-stop, restarts the tracee with | |
944 | ||
ba8f446e | 945 | ptrace(PTRACE_restart, pid, 0, stopsig) |
181f997f MK |
946 | |
947 | with the intention of injecting | |
948 | .IR stopsig , | |
949 | but | |
950 | .I stopsig | |
951 | gets ignored and the tracee continues to run. | |
952 | .LP | |
953 | The | |
954 | .B SIGCONT | |
955 | signal has a side effect of waking up (all threads of) | |
956 | a group-stopped process. | |
957 | This side effect happens before signal-delivery-stop. | |
a5c725cf | 958 | The tracer can't suppress this side effect (it can |
181f997f MK |
959 | only suppress signal injection, which only causes the |
960 | .BR SIGCONT | |
961 | handler to not be executed in the tracee, if such a handler is installed). | |
962 | In fact, waking up from group-stop may be followed by | |
963 | signal-delivery-stop for signal(s) | |
964 | .I other than | |
965 | .BR SIGCONT , | |
966 | if they were pending when | |
967 | .B SIGCONT | |
968 | was delivered. | |
969 | In other words, | |
970 | .B SIGCONT | |
971 | may be not the first signal observed by the tracee after it was sent. | |
972 | .LP | |
973 | Stopping signals cause (all threads of) a process to enter group-stop. | |
4d12a715 | 974 | This side effect happens after signal injection, and therefore can be |
181f997f MK |
975 | suppressed by the tracer. |
976 | .LP | |
dc85ba7c MK |
977 | In Linux 2.4 and earlier, the |
978 | .B SIGSTOP | |
979 | signal can't be injected. | |
980 | .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(), | |
981 | .\" there is: | |
d6e37473 | 982 | .\" |
dc85ba7c MK |
983 | .\" /* The debugger continued. Ignore SIGSTOP. */ |
984 | .\" if (signr == SIGSTOP) | |
985 | .\" continue; | |
986 | .LP | |
181f997f MK |
987 | .B PTRACE_GETSIGINFO |
988 | can be used to retrieve a | |
989 | .I siginfo_t | |
990 | structure which corresponds to the delivered signal. | |
991 | .B PTRACE_SETSIGINFO | |
992 | may be used to modify it. | |
993 | If | |
994 | .B PTRACE_SETSIGINFO | |
995 | has been used to alter | |
996 | .IR siginfo_t , | |
997 | the | |
998 | .I si_signo | |
999 | field and the | |
1000 | .I sig | |
1001 | parameter in the restarting command must match, | |
4d12a715 DV |
1002 | otherwise the result is undefined. |
1003 | .SS Group-stop | |
181f997f | 1004 | When a (possibly multithreaded) process receives a stopping signal, |
8b20acd1 MK |
1005 | all threads stop. |
1006 | If some threads are traced, they enter a group-stop. | |
181f997f MK |
1007 | Note that the stopping signal will first cause signal-delivery-stop |
1008 | (on one tracee only), and only after it is injected by the tracer | |
1009 | (or after it was dispatched to a thread which isn't traced), | |
1010 | will group-stop be initiated on | |
1011 | .I all | |
1012 | tracees within the multithreaded process. | |
1013 | As usual, every tracee reports its group-stop separately | |
1014 | to the corresponding tracer. | |
1015 | .LP | |
1016 | Group-stop is observed by the tracer as | |
1017 | .BR waitpid (2) | |
1018 | returning with | |
1019 | .I WIFSTOPPED(status) | |
1020 | true, with the stopping signal available via | |
1021 | .IR WSTOPSIG(status) . | |
1022 | The same result is returned by some other classes of ptrace-stops, | |
1023 | therefore the recommended practice is to perform the call | |
1024 | .LP | |
1025 | ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo) | |
1026 | .LP | |
1027 | The call can be avoided if the signal is not | |
1028 | .BR SIGSTOP , | |
1029 | .BR SIGTSTP , | |
1030 | .BR SIGTTIN , | |
1031 | or | |
1032 | .BR SIGTTOU ; | |
1033 | only these four signals are stopping signals. | |
1034 | If the tracer sees something else, it can't be a group-stop. | |
1035 | Otherwise, the tracer needs to call | |
1036 | .BR PTRACE_GETSIGINFO . | |
1037 | If | |
1038 | .B PTRACE_GETSIGINFO | |
1039 | fails with | |
1040 | .BR EINVAL , | |
1041 | then it is definitely a group-stop. | |
1042 | (Other failure codes are possible, such as | |
1043 | .B ESRCH | |
1044 | ("no such process") if a | |
1045 | .B SIGKILL | |
1046 | killed the tracee.) | |
4d12a715 | 1047 | .LP |
f04ba477 | 1048 | As of Linux 2.6.38, |
181f997f MK |
1049 | after the tracer sees the tracee ptrace-stop and until it |
1050 | restarts or kills it, the tracee will not run, | |
1051 | and will not send notifications (except | |
1052 | .B SIGKILL | |
1053 | death) to the tracer, even if the tracer enters into another | |
1054 | .BR waitpid (2) | |
8b20acd1 | 1055 | call. |
4d12a715 | 1056 | .LP |
b8d02d56 MK |
1057 | The kernel behavior described in the previous paragraph |
1058 | causes a problem with transparent handling of stopping signals. | |
1059 | If the tracer restarts the tracee after group-stop, | |
dc85ba7c | 1060 | the stopping signal |
8898a252 | 1061 | is effectively ignored\(emthe tracee doesn't remain stopped, it runs. |
181f997f MK |
1062 | If the tracer doesn't restart the tracee before entering into the next |
1063 | .BR waitpid (2), | |
1064 | future | |
1065 | .B SIGCONT | |
b8d02d56 MK |
1066 | signals will not be reported to the tracer; |
1067 | this would cause the | |
181f997f | 1068 | .B SIGCONT |
b8d02d56 | 1069 | signals to have no effect on the tracee. |
ba8f446e | 1070 | .LP |
f04ba477 | 1071 | Since Linux 3.4, there is a method to overcome this problem: instead of |
ba8f446e DV |
1072 | .BR PTRACE_CONT , |
1073 | a | |
1074 | .B PTRACE_LISTEN | |
1075 | command can be used to restart a tracee in a way where it does not execute, | |
f04ba477 MK |
1076 | but waits for a new event which it can report via |
1077 | .BR waitpid (2) | |
1078 | (such as when | |
ba8f446e DV |
1079 | it is restarted by a |
1080 | .BR SIGCONT ). | |
4d12a715 | 1081 | .SS PTRACE_EVENT stops |
181f997f MK |
1082 | If the tracer sets |
1083 | .B PTRACE_O_TRACE_* | |
1084 | options, the tracee will enter ptrace-stops called | |
1085 | .B PTRACE_EVENT | |
1086 | stops. | |
1087 | .LP | |
1088 | .B PTRACE_EVENT | |
1089 | stops are observed by the tracer as | |
1090 | .BR waitpid (2) | |
1091 | returning with | |
1092 | .IR WIFSTOPPED(status) , | |
1093 | and | |
1094 | .I WSTOPSIG(status) | |
1095 | returns | |
1096 | .BR SIGTRAP . | |
1097 | An additional bit is set in the higher byte of the status word: | |
1098 | the value | |
1099 | .I status>>8 | |
1100 | will be | |
1101 | ||
1102 | (SIGTRAP | PTRACE_EVENT_foo << 8). | |
1103 | ||
8b20acd1 | 1104 | The following events exist: |
181f997f MK |
1105 | .TP |
1106 | .B PTRACE_EVENT_VFORK | |
1107 | Stop before return from | |
1108 | .BR vfork (2) | |
1109 | or | |
1110 | .BR clone (2) | |
1111 | with the | |
1112 | .B CLONE_VFORK | |
1113 | flag. | |
1114 | When the tracee is continued after this stop, it will wait for child to | |
1115 | exit/exec before continuing its execution | |
1116 | (in other words, the usual behavior on | |
1117 | .BR vfork (2)). | |
1118 | .TP | |
1119 | .B PTRACE_EVENT_FORK | |
1120 | Stop before return from | |
1121 | .BR fork (2) | |
1122 | or | |
1123 | .BR clone (2) | |
1124 | with the exit signal set to | |
1125 | .BR SIGCHLD . | |
1126 | .TP | |
1127 | .B PTRACE_EVENT_CLONE | |
1128 | Stop before return from | |
a5c725cf | 1129 | .BR clone (2). |
181f997f MK |
1130 | .TP |
1131 | .B PTRACE_EVENT_VFORK_DONE | |
1132 | Stop before return from | |
1133 | .BR vfork (2) | |
1134 | or | |
1135 | .BR clone (2) | |
1136 | with the | |
1137 | .B CLONE_VFORK | |
1138 | flag, | |
1139 | but after the child unblocked this tracee by exiting or execing. | |
4d12a715 | 1140 | .LP |
181f997f MK |
1141 | For all four stops described above, |
1142 | the stop occurs in the parent (i.e., the tracee), | |
1143 | not in the newly created thread. | |
1144 | .BR PTRACE_GETEVENTMSG | |
1145 | can be used to retrieve the new thread's ID. | |
1146 | .TP | |
1147 | .B PTRACE_EVENT_EXEC | |
1148 | Stop before return from | |
1149 | .BR execve (2). | |
b16d33ef DV |
1150 | Since Linux 3.0, |
1151 | .BR PTRACE_GETEVENTMSG | |
1152 | returns the former thread ID. | |
181f997f MK |
1153 | .TP |
1154 | .B PTRACE_EVENT_EXIT | |
1155 | Stop before exit (including death from | |
1156 | .BR exit_group (2)), | |
1157 | signal death, or exit caused by | |
1158 | .BR execve (2) | |
1159 | in a multithreaded process. | |
1160 | .B PTRACE_GETEVENTMSG | |
1161 | returns the exit status. | |
8b20acd1 MK |
1162 | Registers can be examined |
1163 | (unlike when "real" exit happens). | |
181f997f MK |
1164 | The tracee is still alive; it needs to be |
1165 | .BR PTRACE_CONT ed | |
1166 | or | |
1167 | .BR PTRACE_DETACH ed | |
1168 | to finish exiting. | |
ba8f446e DV |
1169 | .TP |
1170 | .B PTRACE_EVENT_STOP | |
1171 | Stop induced by | |
1172 | .B PTRACE_INTERRUPT | |
1173 | command. | |
181f997f MK |
1174 | .LP |
1175 | .B PTRACE_GETSIGINFO | |
1176 | on | |
1177 | .B PTRACE_EVENT | |
1178 | stops returns | |
b16d33ef DV |
1179 | .B SIGTRAP |
1180 | in | |
181f997f MK |
1181 | .IR si_signo , |
1182 | with | |
1183 | .I si_code | |
1184 | set to | |
1185 | .IR "(event<<8)\ |\ SIGTRAP" . | |
4d12a715 | 1186 | .SS Syscall-stops |
181f997f MK |
1187 | If the tracee was restarted by |
1188 | .BR PTRACE_SYSCALL , | |
1189 | the tracee enters | |
1190 | syscall-enter-stop just prior to entering any system call. | |
1191 | If the tracer restarts the tracee with | |
1192 | .BR PTRACE_SYSCALL , | |
1193 | the tracee enters syscall-exit-stop when the system call is finished, | |
1194 | or if it is interrupted by a signal. | |
1195 | (That is, signal-delivery-stop never happens between syscall-enter-stop | |
1196 | and syscall-exit-stop; it happens | |
1197 | .I after | |
1198 | syscall-exit-stop.) | |
1199 | .LP | |
1200 | Other possibilities are that the tracee may stop in a | |
1201 | .B PTRACE_EVENT | |
1202 | stop, exit (if it entered | |
1203 | .BR _exit (2) | |
1204 | or | |
1205 | .BR exit_group (2)), | |
1206 | be killed by | |
1207 | .BR SIGKILL , | |
1208 | or die silently (if it is a thread group leader, the | |
1209 | .BR execve (2) | |
1210 | happened in another thread, | |
1211 | and that thread is not traced by the same tracer; | |
1212 | this situation is discussed later). | |
1213 | .LP | |
1214 | Syscall-enter-stop and syscall-exit-stop are observed by the tracer as | |
1215 | .BR waitpid (2) | |
1216 | returning with | |
1217 | .I WIFSTOPPED(status) | |
1218 | true, and | |
1219 | .I WSTOPSIG(status) | |
1220 | giving | |
1221 | .BR SIGTRAP . | |
1222 | If the | |
1223 | .B PTRACE_O_TRACESYSGOOD | |
1224 | option was set by the tracer, then | |
1225 | .I WSTOPSIG(status) | |
1226 | will give the value | |
1227 | .IR "(SIGTRAP\ |\ 0x80)" . | |
4d12a715 DV |
1228 | .LP |
1229 | Syscall-stops can be distinguished from signal-delivery-stop with | |
181f997f MK |
1230 | .B SIGTRAP |
1231 | by querying | |
1232 | .BR PTRACE_GETSIGINFO | |
1233 | for the following cases: | |
1234 | .TP | |
1235 | .IR si_code " <= 0" | |
1236 | .B SIGTRAP | |
7fac88a9 | 1237 | was delivered as a result of a user-space action, |
8898a252 | 1238 | for example, a system call |
181f997f | 1239 | .RB ( tgkill (2), |
8898a252 | 1240 | .BR kill (2), |
181f997f | 1241 | .BR sigqueue (3), |
8898a252 MK |
1242 | etc.), |
1243 | expiration of a POSIX timer, | |
1244 | change of state on a POSIX message queue, | |
1245 | or completion of an asynchronous I/O request. | |
181f997f MK |
1246 | .TP |
1247 | .IR si_code " == SI_KERNEL (0x80)" | |
1248 | .B SIGTRAP | |
1249 | was sent by the kernel. | |
1250 | .TP | |
1251 | .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)" | |
1252 | This is a syscall-stop. | |
1253 | .LP | |
1254 | However, syscall-stops happen very often (twice per system call), | |
1255 | and performing | |
1256 | .B PTRACE_GETSIGINFO | |
1257 | for every syscall-stop may be somewhat expensive. | |
1258 | .LP | |
181f997f MK |
1259 | Some architectures allow the cases to be distinguished |
1260 | by examining registers. | |
1261 | For example, on x86, | |
1262 | .I rax | |
1263 | == | |
1264 | .RB - ENOSYS | |
1265 | in syscall-enter-stop. | |
1266 | Since | |
1267 | .B SIGTRAP | |
1268 | (like any other signal) always happens | |
1269 | .I after | |
1270 | syscall-exit-stop, | |
1271 | and at this point | |
1272 | .I rax | |
1273 | almost never contains | |
1274 | .RB - ENOSYS , | |
1275 | the | |
1276 | .B SIGTRAP | |
1277 | looks like "syscall-stop which is not syscall-enter-stop"; | |
1278 | in other words, it looks like a | |
8b20acd1 | 1279 | "stray syscall-exit-stop" and can be detected this way. |
181f997f | 1280 | But such detection is fragile and is best avoided. |
4d12a715 | 1281 | .LP |
181f997f MK |
1282 | Using the |
1283 | .B PTRACE_O_TRACESYSGOOD | |
a17e05c5 | 1284 | option is the recommended method to distinguish syscall-stops |
b8d02d56 | 1285 | from other kinds of ptrace-stops, |
181f997f | 1286 | since it is reliable and does not incur a performance penalty. |
4d12a715 | 1287 | .LP |
181f997f MK |
1288 | Syscall-enter-stop and syscall-exit-stop are |
1289 | indistinguishable from each other by the tracer. | |
1290 | The tracer needs to keep track of the sequence of | |
4d12a715 | 1291 | ptrace-stops in order to not misinterpret syscall-enter-stop as |
8b20acd1 MK |
1292 | syscall-exit-stop or vice versa. |
1293 | The rule is that syscall-enter-stop is | |
181f997f MK |
1294 | always followed by syscall-exit-stop, |
1295 | .B PTRACE_EVENT | |
1296 | stop or the tracee's death; | |
1297 | no other kinds of ptrace-stop can occur in between. | |
4d12a715 | 1298 | .LP |
181f997f MK |
1299 | If after syscall-enter-stop, |
1300 | the tracer uses a restarting command other than | |
1301 | .BR PTRACE_SYSCALL , | |
1302 | syscall-exit-stop is not generated. | |
4d12a715 | 1303 | .LP |
181f997f MK |
1304 | .B PTRACE_GETSIGINFO |
1305 | on syscall-stops returns | |
1306 | .B SIGTRAP | |
1307 | in | |
1308 | .IR si_signo , | |
1309 | with | |
1310 | .I si_code | |
1311 | set to | |
1312 | .B SIGTRAP | |
1313 | or | |
1314 | .IR (SIGTRAP|0x80) . | |
1315 | .SS PTRACE_SINGLESTEP, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP stops | |
b8d02d56 | 1316 | [Details of these kinds of stops are yet to be documented.] |
181f997f | 1317 | .\" |
d6e37473 | 1318 | .\" FIXME |
b8d02d56 MK |
1319 | .\" document stops occurring with PTRACE_SINGLESTEP, PTRACE_SYSEMU, |
1320 | .\" PTRACE_SYSEMU_SINGLESTEP | |
4d12a715 | 1321 | .SS Informational and restarting ptrace commands |
181f997f MK |
1322 | Most ptrace commands (all except |
1323 | .BR PTRACE_ATTACH , | |
ba8f446e | 1324 | .BR PTRACE_SEIZE , |
181f997f | 1325 | .BR PTRACE_TRACEME , |
ba8f446e | 1326 | .BR PTRACE_INTERRUPT , |
181f997f MK |
1327 | and |
1328 | .BR PTRACE_KILL ) | |
1329 | require the tracee to be in a ptrace-stop, otherwise they fail with | |
1330 | .BR ESRCH . | |
4d12a715 | 1331 | .LP |
181f997f MK |
1332 | When the tracee is in ptrace-stop, |
1333 | the tracer can read and write data to | |
1334 | the tracee using informational commands. | |
1335 | These commands leave the tracee in ptrace-stopped state: | |
4d12a715 DV |
1336 | .LP |
1337 | .nf | |
181f997f MK |
1338 | ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0); |
1339 | ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val); | |
1340 | ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct); | |
1341 | ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct); | |
ba8f446e DV |
1342 | ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov); |
1343 | ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov); | |
181f997f MK |
1344 | ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo); |
1345 | ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo); | |
1346 | ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var); | |
1347 | ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags); | |
4d12a715 DV |
1348 | .fi |
1349 | .LP | |
8b20acd1 | 1350 | Note that some errors are not reported. |
181f997f MK |
1351 | For example, setting signal information |
1352 | .RI ( siginfo ) | |
4d12a715 | 1353 | may have no effect in some ptrace-stops, yet the call may succeed |
181f997f MK |
1354 | (return 0 and not set |
1355 | .IR errno ); | |
1356 | querying | |
1357 | .B PTRACE_GETEVENTMSG | |
1358 | may succeed and return some random value if current ptrace-stop | |
1359 | is not documented as returning a meaningful event message. | |
1360 | .LP | |
1361 | The call | |
1362 | ||
1363 | ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags); | |
d6e37473 | 1364 | |
181f997f MK |
1365 | affects one tracee. |
1366 | The tracee's current flags are replaced. | |
1367 | Flags are inherited by new tracees created and "auto-attached" via active | |
1368 | .BR PTRACE_O_TRACEFORK , | |
1369 | .BR PTRACE_O_TRACEVFORK , | |
1370 | or | |
1371 | .BR PTRACE_O_TRACECLONE | |
1372 | options. | |
1373 | .LP | |
1374 | Another group of commands makes the ptrace-stopped tracee run. | |
1375 | They have the form: | |
1376 | .LP | |
8898a252 | 1377 | ptrace(cmd, pid, 0, sig); |
181f997f MK |
1378 | .LP |
1379 | where | |
1380 | .I cmd | |
1381 | is | |
1382 | .BR PTRACE_CONT , | |
ba8f446e | 1383 | .BR PTRACE_LISTEN , |
181f997f MK |
1384 | .BR PTRACE_DETACH , |
1385 | .BR PTRACE_SYSCALL , | |
1386 | .BR PTRACE_SINGLESTEP , | |
1387 | .BR PTRACE_SYSEMU , | |
1388 | or | |
a5c725cf | 1389 | .BR PTRACE_SYSEMU_SINGLESTEP . |
181f997f MK |
1390 | If the tracee is in signal-delivery-stop, |
1391 | .I sig | |
1392 | is the signal to be injected (if it is nonzero). | |
1393 | Otherwise, | |
1394 | .I sig | |
1395 | may be ignored. | |
8898a252 MK |
1396 | (When restarting a tracee from a ptrace-stop other than signal-delivery-stop, |
1397 | recommended practice is to always pass 0 in | |
a5c725cf | 1398 | .IR sig .) |
4d12a715 | 1399 | .SS Attaching and detaching |
181f997f MK |
1400 | A thread can be attached to the tracer using the call |
1401 | ||
1402 | ptrace(PTRACE_ATTACH, pid, 0, 0); | |
1403 | ||
ba8f446e DV |
1404 | or |
1405 | ||
1406 | ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags); | |
1407 | ||
1408 | .B PTRACE_ATTACH | |
1409 | sends | |
181f997f MK |
1410 | .B SIGSTOP |
1411 | to this thread. | |
1412 | If the tracer wants this | |
1413 | .B SIGSTOP | |
1414 | to have no effect, it needs to suppress it. | |
1415 | Note that if other signals are concurrently sent to | |
1416 | this thread during attach, | |
1417 | the tracer may see the tracee enter signal-delivery-stop | |
1418 | with other signal(s) first! | |
1419 | The usual practice is to reinject these signals until | |
1420 | .B SIGSTOP | |
1421 | is seen, then suppress | |
1422 | .B SIGSTOP | |
1423 | injection. | |
181f997f MK |
1424 | The design bug here is that a ptrace attach and a concurrently delivered |
1425 | .B SIGSTOP | |
1426 | may race and the concurrent | |
1427 | .B SIGSTOP | |
1428 | may be lost. | |
1429 | .\" | |
b8d02d56 MK |
1430 | .\" FIXME: Describe how to attach to a thread which is already |
1431 | .\" group-stopped. | |
181f997f MK |
1432 | .LP |
1433 | Since attaching sends | |
1434 | .B SIGSTOP | |
1435 | and the tracer usually suppresses it, this may cause a stray | |
a5c725cf | 1436 | .B EINTR |
181f997f | 1437 | return from the currently executing system call in the tracee, |
a5c725cf | 1438 | as described in the "Signal injection and suppression" section. |
181f997f | 1439 | .LP |
f04ba477 | 1440 | Since Linux 3.4, |
ba8f446e DV |
1441 | .B PTRACE_SEIZE |
1442 | can be used instead of | |
1443 | .BR PTRACE_ATTACH . | |
1444 | .B PTRACE_SEIZE | |
e3948c69 MK |
1445 | does not stop the attached process. |
1446 | If you need to stop | |
ba8f446e DV |
1447 | it after attach (or at any other time) without sending it any signals, |
1448 | use | |
1449 | .B PTRACE_INTERRUPT | |
1450 | command. | |
1451 | .LP | |
181f997f MK |
1452 | The request |
1453 | ||
1454 | ptrace(PTRACE_TRACEME, 0, 0, 0); | |
1455 | ||
1456 | turns the calling thread into a tracee. | |
1457 | The thread continues to run (doesn't enter ptrace-stop). | |
1458 | A common practice is to follow the | |
1459 | .B PTRACE_TRACEME | |
1460 | with | |
1461 | ||
1462 | raise(SIGSTOP); | |
1463 | ||
1464 | and allow the parent (which is our tracer now) to observe our | |
4d12a715 DV |
1465 | signal-delivery-stop. |
1466 | .LP | |
d6e37473 | 1467 | If the |
181f997f MK |
1468 | .BR PTRACE_O_TRACEFORK , |
1469 | .BR PTRACE_O_TRACEVFORK , | |
1470 | or | |
1471 | .BR PTRACE_O_TRACECLONE | |
1472 | options are in effect, then children created by, respectively, | |
1473 | .BR vfork (2) | |
1474 | or | |
1475 | .BR clone (2) | |
1476 | with the | |
1477 | .B CLONE_VFORK | |
1478 | flag, | |
1479 | .BR fork (2) | |
1480 | or | |
1481 | .BR clone (2) | |
1482 | with the exit signal set to | |
1483 | .BR SIGCHLD , | |
1484 | and other kinds of | |
1485 | .BR clone (2), | |
1486 | are automatically attached to the same tracer which traced their parent. | |
1487 | .B SIGSTOP | |
1488 | is delivered to the children, causing them to enter | |
1489 | signal-delivery-stop after they exit the system call which created them. | |
1490 | .LP | |
1491 | Detaching of the tracee is performed by: | |
1492 | ||
1493 | ptrace(PTRACE_DETACH, pid, 0, sig); | |
1494 | ||
1495 | .B PTRACE_DETACH | |
1496 | is a restarting operation; | |
1497 | therefore it requires the tracee to be in ptrace-stop. | |
1498 | If the tracee is in signal-delivery-stop, a signal can be injected. | |
1499 | Otherwise, the | |
1500 | .I sig | |
1501 | parameter may be silently ignored. | |
1502 | .LP | |
1503 | If the tracee is running when the tracer wants to detach it, | |
1504 | the usual solution is to send | |
1505 | .B SIGSTOP | |
1506 | (using | |
1507 | .BR tgkill (2), | |
1508 | to make sure it goes to the correct thread), | |
1509 | wait for the tracee to stop in signal-delivery-stop for | |
1510 | .B SIGSTOP | |
1511 | and then detach it (suppressing | |
1512 | .B SIGSTOP | |
1513 | injection). | |
1514 | A design bug is that this can race with concurrent | |
1515 | .BR SIGSTOP s. | |
1516 | Another complication is that the tracee may enter other ptrace-stops | |
1517 | and needs to be restarted and waited for again, until | |
1518 | .B SIGSTOP | |
1519 | is seen. | |
1520 | Yet another complication is to be sure that | |
1521 | the tracee is not already ptrace-stopped, | |
1522 | because no signal delivery happens while it is\(emnot even | |
1523 | .BR SIGSTOP . | |
b8d02d56 MK |
1524 | .\" FIXME: Describe how to detach from a group-stopped tracee so that it |
1525 | .\" doesn't run, but continues to wait for SIGCONT. | |
181f997f MK |
1526 | .LP |
1527 | If the tracer dies, all tracees are automatically detached and restarted, | |
8b20acd1 | 1528 | unless they were in group-stop. |
b8d02d56 MK |
1529 | Handling of restart from group-stop is currently buggy, |
1530 | but the "as planned" behavior is to leave tracee stopped and waiting for | |
181f997f MK |
1531 | .BR SIGCONT . |
1532 | If the tracee is restarted from signal-delivery-stop, | |
1533 | the pending signal is injected. | |
1534 | .SS execve(2) under ptrace | |
cb729171 | 1535 | .\" clone(2) CLONE_THREAD says: |
181f997f MK |
1536 | .\" If any of the threads in a thread group performs an execve(2), |
1537 | .\" then all threads other than the thread group leader are terminated, | |
d6e37473 | 1538 | .\" and the new program is executed in the thread group leader. |
181f997f | 1539 | .\" |
8898a252 | 1540 | When one thread in a multithreaded process calls |
181f997f MK |
1541 | .BR execve (2), |
1542 | the kernel destroys all other threads in the process, | |
1543 | .\" In kernel 3.1 sources, see fs/exec.c::de_thread() | |
1544 | and resets the thread ID of the execing thread to the | |
1545 | thread group ID (process ID). | |
181f997f MK |
1546 | (Or, to put things another way, when a multithreaded process does an |
1547 | .BR execve (2), | |
8898a252 | 1548 | at completion of the call, it appears as though the |
181f997f MK |
1549 | .BR execve (2) |
1550 | occurred in the thread group leader, regardless of which thread did the | |
1551 | .BR execve (2).) | |
181f997f MK |
1552 | This resetting of the thread ID looks very confusing to tracers: |
1553 | .IP * 3 | |
1554 | All other threads stop in | |
8898a252 | 1555 | .B PTRACE_EVENT_EXIT |
b8d02d56 | 1556 | stop, if the |
8898a252 MK |
1557 | .BR PTRACE_O_TRACEEXIT |
1558 | option was turned on. | |
181f997f MK |
1559 | Then all other threads except the thread group leader report |
1560 | death as if they exited via | |
1561 | .BR _exit (2) | |
1562 | with exit code 0. | |
b8d02d56 | 1563 | .IP * |
181f997f MK |
1564 | The execing tracee changes its thread ID while it is in the |
1565 | .BR execve (2). | |
1566 | (Remember, under ptrace, the "pid" returned from | |
1567 | .BR waitpid (2), | |
1568 | or fed into ptrace calls, is the tracee's thread ID.) | |
1569 | That is, the tracee's thread ID is reset to be the same as its process ID, | |
1570 | which is the same as the thread group leader's thread ID. | |
1571 | .IP * | |
f098951d DV |
1572 | Then a |
1573 | .B PTRACE_EVENT_EXEC | |
1574 | stop happens, if the | |
1575 | .BR PTRACE_O_TRACEEXEC | |
1576 | option was turned on. | |
1577 | .IP * | |
1578 | If the thread group leader has reported its | |
1579 | .B PTRACE_EVENT_EXIT | |
1580 | stop by this time, | |
181f997f MK |
1581 | it appears to the tracer that |
1582 | the dead thread leader "reappears from nowhere". | |
a17e05c5 | 1583 | (Note: the thread group leader does not report death via |
f098951d DV |
1584 | .I WIFEXITED(status) |
1585 | until there is at least one other live thread. | |
a17e05c5 | 1586 | This eliminates the possibility that the tracer will see |
f098951d | 1587 | it dying and then reappearing.) |
181f997f MK |
1588 | If the thread group leader was still alive, |
1589 | for the tracer this may look as if thread group leader | |
1590 | returns from a different system call than it entered, | |
1591 | or even "returned from a system call even though | |
1592 | it was not in any system call". | |
1593 | If the thread group leader was not traced | |
1594 | (or was traced by a different tracer), then during | |
1595 | .BR execve (2) | |
1596 | it will appear as if it has become a tracee of | |
1597 | the tracer of the execing tracee. | |
4d12a715 | 1598 | .LP |
181f997f MK |
1599 | All of the above effects are the artifacts of |
1600 | the thread ID change in the tracee. | |
4d12a715 | 1601 | .LP |
181f997f MK |
1602 | The |
1603 | .B PTRACE_O_TRACEEXEC | |
1604 | option is the recommended tool for dealing with this situation. | |
b8d02d56 | 1605 | First, it enables |
a5c725cf DP |
1606 | .BR PTRACE_EVENT_EXEC |
1607 | stop, | |
b8d02d56 | 1608 | which occurs before |
a5c725cf | 1609 | .BR execve (2) |
b8d02d56 MK |
1610 | returns. |
1611 | In this stop, the tracer can use | |
1612 | .B PTRACE_GETEVENTMSG | |
1613 | to retrieve the tracee's former thread ID. | |
1614 | (This feature was introduced in Linux 3.0). | |
1615 | Second, the | |
1616 | .B PTRACE_O_TRACEEXEC | |
1617 | option disables legacy | |
1618 | .B SIGTRAP | |
1619 | generation on | |
1620 | .BR execve (2). | |
181f997f MK |
1621 | .LP |
1622 | When the tracer receives | |
1623 | .B PTRACE_EVENT_EXEC | |
1624 | stop notification, | |
1625 | it is guaranteed that except this tracee and the thread group leader, | |
1626 | no other threads from the process are alive. | |
1627 | .LP | |
1628 | On receiving the | |
1629 | .B PTRACE_EVENT_EXEC | |
1630 | stop notification, | |
1631 | the tracer should clean up all its internal | |
1632 | data structures describing the threads of this process, | |
1633 | and retain only one data structure\(emone which | |
1634 | describes the single still running tracee, with | |
1635 | ||
f098951d | 1636 | thread ID == thread group ID == process ID. |
181f997f MK |
1637 | .LP |
1638 | Example: two threads call | |
1639 | .BR execve (2) | |
1640 | at the same time: | |
4d12a715 DV |
1641 | .LP |
1642 | .nf | |
a5c725cf | 1643 | *** we get syscall-enter-stop in thread 1: ** |
4d12a715 DV |
1644 | PID1 execve("/bin/foo", "foo" <unfinished ...> |
1645 | *** we issue PTRACE_SYSCALL for thread 1 ** | |
a5c725cf | 1646 | *** we get syscall-enter-stop in thread 2: ** |
4d12a715 DV |
1647 | PID2 execve("/bin/bar", "bar" <unfinished ...> |
1648 | *** we issue PTRACE_SYSCALL for thread 2 ** | |
1649 | *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL ** | |
1650 | *** we get syscall-exit-stop for PID0: ** | |
1651 | PID0 <... execve resumed> ) = 0 | |
1652 | .fi | |
1653 | .LP | |
181f997f MK |
1654 | If the |
1655 | .B PTRACE_O_TRACEEXEC | |
1656 | option is | |
1657 | .I not | |
1658 | in effect for the execing tracee, the kernel delivers an extra | |
1659 | .B SIGTRAP | |
1660 | to the tracee after | |
1661 | .BR execve (2) | |
8b20acd1 MK |
1662 | returns. |
1663 | This is an ordinary signal (similar to one which can be | |
181f997f MK |
1664 | generated by |
1665 | .IR "kill -TRAP" ), | |
1666 | not a special kind of ptrace-stop. | |
1667 | Employing | |
1668 | .B PTRACE_GETSIGINFO | |
1669 | for this signal returns | |
1670 | .I si_code | |
1671 | set to 0 | |
1672 | .RI ( SI_USER ). | |
1673 | This signal may be blocked by signal mask, | |
1674 | and thus may be delivered (much) later. | |
1675 | .LP | |
1676 | Usually, the tracer (for example, | |
1677 | .BR strace (1)) | |
1678 | would not want to show this extra post-execve | |
1679 | .B SIGTRAP | |
1680 | signal to the user, and would suppress its delivery to the tracee (if | |
1681 | .B SIGTRAP | |
1682 | is set to | |
1683 | .BR SIG_DFL , | |
1684 | it is a killing signal). | |
d6e37473 | 1685 | However, determining |
181f997f MK |
1686 | .I which |
1687 | .B SIGTRAP | |
1688 | to suppress is not easy. | |
1689 | Setting the | |
1690 | .B PTRACE_O_TRACEEXEC | |
1691 | option and thus suppressing this extra | |
1692 | .B SIGTRAP | |
1693 | is the recommended approach. | |
4d12a715 | 1694 | .SS Real parent |
181f997f MK |
1695 | The ptrace API (ab)uses the standard UNIX parent/child signaling over |
1696 | .BR waitpid (2). | |
1697 | This used to cause the real parent of the process to stop receiving | |
1698 | several kinds of | |
1699 | .BR waitpid (2) | |
1700 | notifications when the child process is traced by some other process. | |
1701 | .LP | |
1702 | Many of these bugs have been fixed, but as of Linux 2.6.38 several still | |
1703 | exist; see BUGS below. | |
1704 | .LP | |
1705 | As of Linux 2.6.38, the following is believed to work correctly: | |
1706 | .IP * 3 | |
dc85ba7c MK |
1707 | exit/death by signal is reported first to the tracer, then, |
1708 | when the tracer consumes the | |
181f997f MK |
1709 | .BR waitpid (2) |
1710 | result, to the real parent (to the real parent only when the | |
1711 | whole multithreaded process exits). | |
181f997f MK |
1712 | If the tracer and the real parent are the same process, |
1713 | the report is sent only once. | |
47297adb | 1714 | .SH RETURN VALUE |
988db661 | 1715 | On success, |
0daa9e92 | 1716 | .B PTRACE_PEEK* |
78686915 DV |
1717 | requests return the requested data, while other requests return zero. |
1718 | (On Linux, this is done in the libc wrapper around ptrace system call. | |
1719 | On the system call level, | |
1720 | .B PTRACE_PEEK* | |
1721 | requests have a different API: they store the result | |
1722 | at the address specified by | |
1723 | .I data | |
1724 | parameter, and return value is the error flag.) | |
1725 | .LP | |
2b2581ee MK |
1726 | On error, all requests return \-1, and |
1727 | .I errno | |
1728 | is set appropriately. | |
8bd58774 | 1729 | Since the value returned by a successful |
0daa9e92 | 1730 | .B PTRACE_PEEK* |
181f997f | 1731 | request may be \-1, the caller must clear |
2b2581ee | 1732 | .I errno |
181f997f MK |
1733 | before the call, and then check it afterward |
1734 | to determine whether or not an error occurred. | |
2b2581ee MK |
1735 | .SH ERRORS |
1736 | .TP | |
1737 | .B EBUSY | |
181f997f | 1738 | (i386 only) There was an error with allocating or freeing a debug register. |
2b2581ee MK |
1739 | .TP |
1740 | .B EFAULT | |
1741 | There was an attempt to read from or write to an invalid area in | |
181f997f | 1742 | the tracer's or the tracee's memory, |
2b2581ee MK |
1743 | probably because the area wasn't mapped or accessible. |
1744 | Unfortunately, under Linux, different variations of this fault | |
2f0af33b MK |
1745 | will return |
1746 | .B EIO | |
1747 | or | |
1748 | .B EFAULT | |
1749 | more or less arbitrarily. | |
2b2581ee MK |
1750 | .TP |
1751 | .B EINVAL | |
1752 | An attempt was made to set an invalid option. | |
1753 | .TP | |
1754 | .B EIO | |
181f997f MK |
1755 | .I request |
1756 | is invalid, or an attempt was made to read from or | |
1757 | write to an invalid area in the tracer's or the tracee's memory, | |
2b2581ee MK |
1758 | or there was a word-alignment violation, |
1759 | or an invalid signal was specified during a restart request. | |
1760 | .TP | |
1761 | .B EPERM | |
1762 | The specified process cannot be traced. | |
1763 | This could be because the | |
4d12a715 | 1764 | tracer has insufficient privileges (the required capability is |
2b2581ee | 1765 | .BR CAP_SYS_PTRACE ); |
00b08db3 | 1766 | unprivileged processes cannot trace processes that they |
2b2581ee MK |
1767 | cannot send signals to or those running |
1768 | set-user-ID/set-group-ID programs, for obvious reasons. | |
181f997f MK |
1769 | Alternatively, the process may already be being traced, |
1770 | or (on kernels before 2.6.26) be | |
66ee0c7e | 1771 | .BR init (8) |
2b2581ee MK |
1772 | (PID 1). |
1773 | .TP | |
1774 | .B ESRCH | |
1775 | The specified process does not exist, or is not currently being traced | |
181f997f MK |
1776 | by the caller, or is not stopped |
1777 | (for requests that require a stopped tracee). | |
47297adb | 1778 | .SH CONFORMING TO |
44a2c328 | 1779 | SVr4, 4.3BSD. |
fea681da MK |
1780 | .SH NOTES |
1781 | Although arguments to | |
e511ffb6 | 1782 | .BR ptrace () |
c13182ef | 1783 | are interpreted according to the prototype given, |
5260fe08 | 1784 | glibc currently declares |
e511ffb6 | 1785 | .BR ptrace () |
181f997f MK |
1786 | as a variadic function with only the |
1787 | .I request | |
1788 | argument fixed. | |
ca302d0e DV |
1789 | It is recommended to always supply four arguments, |
1790 | even if the requested operation does not use them, | |
1791 | setting unused/ignored arguments to | |
1792 | .I 0L | |
1793 | or | |
1794 | .IR "(void\ *)\ 0". | |
181f997f MK |
1795 | .LP |
1796 | In Linux kernels before 2.6.26, | |
1797 | .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb | |
1798 | .BR init (8), | |
1799 | the process with PID 1, may not be traced. | |
1800 | .LP | |
1801 | The layout of the contents of memory and the USER area are | |
1802 | quite operating-system- and architecture-specific. | |
8660aec0 MK |
1803 | The offset supplied, and the data returned, |
1804 | might not entirely match with the definition of | |
1805 | .IR "struct user" . | |
1806 | .\" See http://lkml.org/lkml/2008/5/8/375 | |
fea681da | 1807 | .LP |
181f997f | 1808 | The size of a "word" is determined by the operating-system variant |
3e18f289 | 1809 | (e.g., for 32-bit Linux it is 32 bits). |
b8d02d56 | 1810 | .LP |
fea681da | 1811 | This page documents the way the |
e511ffb6 | 1812 | .BR ptrace () |
c13182ef | 1813 | call works currently in Linux. |
008f1ecc | 1814 | Its behavior differs noticeably on other flavors of UNIX. |
e63ad01d | 1815 | In any case, use of |
e511ffb6 | 1816 | .BR ptrace () |
181f997f | 1817 | is highly specific to the operating system and architecture. |
a1d5f77c | 1818 | .SH BUGS |
8bd58774 | 1819 | On hosts with 2.6 kernel headers, |
0daa9e92 | 1820 | .B PTRACE_SETOPTIONS |
181f997f MK |
1821 | is declared with a different value than the one for 2.4. |
1822 | This leads to applications compiled with 2.6 kernel | |
a1d5f77c | 1823 | headers failing when run on 2.4 kernels. |
8bd58774 | 1824 | This can be worked around by redefining |
0daa9e92 | 1825 | .B PTRACE_SETOPTIONS |
8bd58774 MK |
1826 | to |
1827 | .BR PTRACE_OLDSETOPTIONS , | |
1828 | if that is defined. | |
4d12a715 | 1829 | .LP |
181f997f | 1830 | Group-stop notifications are sent to the tracer, but not to real parent. |
4d12a715 DV |
1831 | Last confirmed on 2.6.38.6. |
1832 | .LP | |
181f997f MK |
1833 | If a thread group leader is traced and exits by calling |
1834 | .BR _exit (2), | |
8898a252 MK |
1835 | .\" Note from Denys Vlasenko: |
1836 | .\" Here "exits" means any kind of death - _exit, exit_group, | |
1837 | .\" signal death. Signal death and exit_group cases are trivial, | |
1838 | .\" though: since signal death and exit_group kill all other threads | |
1839 | .\" too, "until all other threads exit" thing happens rather soon | |
1840 | .\" in these cases. Therefore, only _exit presents observably | |
1841 | .\" puzzling behavior to ptrace users: thread leader _exit's, | |
1842 | .\" but WIFEXITED isn't reported! We are trying to explain here | |
1843 | .\" why it is so. | |
181f997f MK |
1844 | a |
1845 | .B PTRACE_EVENT_EXIT | |
1846 | stop will happen for it (if requested), but the subsequent | |
1847 | .B WIFEXITED | |
1848 | notification will not be delivered until all other threads exit. | |
1849 | As explained above, if one of other threads calls | |
1850 | .BR execve (2), | |
1851 | the death of the thread group leader will | |
1852 | .I never | |
1853 | be reported. | |
1854 | If the execed thread is not traced by this tracer, | |
1855 | the tracer will never know that | |
1856 | .BR execve (2) | |
4d12a715 | 1857 | happened. |
181f997f MK |
1858 | One possible workaround is to |
1859 | .B PTRACE_DETACH | |
1860 | the thread group leader instead of restarting it in this case. | |
1861 | Last confirmed on 2.6.38.6. | |
b8d02d56 | 1862 | .\" FIXME: ^^^ need to test/verify this scenario |
181f997f MK |
1863 | .LP |
1864 | A | |
1865 | .B SIGKILL | |
1866 | signal may still cause a | |
1867 | .B PTRACE_EVENT_EXIT | |
1868 | stop before actual signal death. | |
1869 | This may be changed in the future; | |
1870 | .B SIGKILL | |
1871 | is meant to always immediately kill tasks even under ptrace. | |
8b20acd1 | 1872 | Last confirmed on 2.6.38.6. |
f098951d | 1873 | .LP |
a17e05c5 | 1874 | Some system calls return with |
f098951d | 1875 | .B EINTR |
a17e05c5 MK |
1876 | if a signal was sent to a tracee, but delivery was suppressed by the tracer. |
1877 | (This is very typical operation: it is usually | |
f098951d | 1878 | done by debuggers on every attach, in order to not introduce |
a17e05c5 MK |
1879 | a bogus |
1880 | .BR SIGSTOP ). | |
1881 | As of Linux 3.2.9, the following system calls are affected | |
1882 | (this list is likely incomplete): | |
f098951d | 1883 | .BR epoll_wait (2), |
a17e05c5 | 1884 | and |
f098951d | 1885 | .BR read (2) |
a17e05c5 MK |
1886 | from an |
1887 | .BR inotify (7) | |
1888 | file descriptor. | |
ca302d0e DV |
1889 | The usual symptom of this bug is that when you attach to |
1890 | a quiescent process with the command | |
11c85ed8 | 1891 | |
ca302d0e DV |
1892 | strace -p <process-ID> |
1893 | ||
1894 | then, instead of the usual | |
1895 | and expected one-line output such as | |
1896 | .nf | |
1897 | ||
1898 | restart_syscall(<... resuming interrupted call ...>_ | |
1899 | ||
1900 | .fi | |
1901 | or | |
1902 | .nf | |
1903 | ||
1904 | select(6, [5], NULL, [5], NULL_ | |
1905 | ||
1906 | .fi | |
1907 | ('_' denotes the cursor position), you observe more than one line. | |
1908 | For example: | |
1909 | .nf | |
1910 | ||
1911 | clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0 | |
1912 | epoll_wait(4,_ | |
1913 | ||
1914 | .fi | |
1915 | What is not visible here is that the process was blocked in | |
1916 | .BR epoll_wait (2) | |
1917 | before | |
1918 | .BR strace (1) | |
1919 | has attached to it. | |
1920 | Attaching caused | |
1921 | .BR epoll_wait (2) | |
7fac88a9 | 1922 | to return to user space with the error |
ca302d0e DV |
1923 | .BR EINTR . |
1924 | In this particular case, the program reacted to | |
1925 | .B EINTR | |
b0b1d9b5 | 1926 | by checking the current time, and then executing |
ca302d0e DV |
1927 | .BR epoll_wait (2) |
1928 | again. | |
1929 | (Programs which do not expect such "stray" | |
1930 | .BR EINTR | |
1931 | errors may behave in an unintended way upon an | |
1932 | .BR strace (1) | |
1933 | attach.) | |
47297adb | 1934 | .SH SEE ALSO |
fea681da MK |
1935 | .BR gdb (1), |
1936 | .BR strace (1), | |
181f997f | 1937 | .BR clone (2), |
fea681da MK |
1938 | .BR execve (2), |
1939 | .BR fork (2), | |
181f997f MK |
1940 | .BR gettid (2), |
1941 | .BR sigaction (2), | |
1942 | .BR tgkill (2), | |
1943 | .BR vfork (2), | |
1944 | .BR waitpid (2), | |
fea681da | 1945 | .BR exec (3), |
181f997f MK |
1946 | .BR capabilities (7), |
1947 | .BR signal (7) |