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