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