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