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