1 .\" Copyright (c) 1993 Michael Haardt <michael@moria.de>
2 .\" Fri Apr 2 11:32:09 MET DST 1993
4 .\" and changes Copyright (C) 1999 Mike Coleman (mkc@acm.org)
5 .\" -- major revision to fully document ptrace semantics per recent Linux
6 .\" kernel (2.2.10) and glibc (2.1.2)
7 .\" Sun Nov 7 03:18:35 CST 1999
9 .\" and Copyright (c) 2011, Denys Vlasenko <vda.linux@googlemail.com>
11 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
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.
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.
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.
27 .\" You should have received a copy of the GNU General Public
28 .\" License along with this manual; if not, see
29 .\" <http://www.gnu.org/licenses/>.
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>
35 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Added notes on capability requirements
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.)
42 .\" 2011-09, major update by Denys Vlasenko <vda.linux@googlemail.com>
44 .TH PTRACE 2 2013-02-16 "Linux" "Linux Programmer's Manual"
46 ptrace \- process trace
49 .B #include <sys/ptrace.h>
51 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
52 .BI " void *" addr ", void *" data );
57 system call provides a means by which one process (the "tracer")
58 may observe and control the execution of another process (the "tracee"),
59 and examine and change the tracee's memory and registers.
60 It is primarily used to implement breakpoint debugging and system
63 A tracee first needs to be attached to the tracer.
64 Attachment and subsequent commands are per thread:
65 in a multithreaded process,
66 every thread can be individually attached to a
67 (potentially different) tracer,
68 or left not attached and thus not debugged.
69 Therefore, "tracee" always means "(one) thread",
70 never "a (possibly multithreaded) process".
71 Ptrace commands are always sent to
72 a specific tracee using a call of the form
74 ptrace(PTRACE_foo, pid, ...)
78 is the thread ID of the corresponding Linux thread.
80 (Note that in this page, a "multithreaded process"
81 means a thread group consisting of threads created using the
86 A process can initiate a trace by calling
88 and having the resulting child do a
90 followed (typically) by an
92 Alternatively, one process may commence tracing another process using
97 While being traced, the tracee will stop each time a signal is delivered,
98 even if the signal is being ignored.
101 which has its usual effect.)
102 The tracer will be notified at its next call to
104 (or one of the related "wait" system calls); that call will return a
106 value containing information that indicates
107 the cause of the stop in the tracee.
108 While the tracee is stopped,
109 the tracer can use various ptrace requests to inspect and modify the tracee.
110 The tracer then causes the tracee to continue,
111 optionally ignoring the delivered signal
112 (or even delivering a different signal instead).
115 .B PTRACE_O_TRACEEXEC
116 option is not in effect, all successful calls to
118 by the traced process will cause it to be sent a
121 giving the parent a chance to gain control before the new program
124 When the tracer is finished tracing, it can cause the tracee to continue
125 executing in a normal, untraced mode via
130 determines the action to be performed:
133 Indicate that this process is to be traced by its parent.
134 A process probably shouldn't make this request if its parent
135 isn't expecting to trace it.
144 request is used only by the tracee;
145 the remaining requests are used only by the tracer.
146 In the following requests,
148 specifies the thread ID of the tracee to be acted on.
149 For requests other than
155 the tracee must be stopped.
157 .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
158 Read a word at the address
160 in the tracee's memory, returning the word as the result of the
163 Linux does not have separate text and data address spaces,
164 so these two requests are currently equivalent.
169 .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
170 .\" and that is the name that seems common on other systems.
171 Read a word at offset
173 in the tracee's USER area,
174 which holds the registers and other information about the process
177 The word is returned as the result of the
180 Typically, the offset must be word-aligned, though this might vary by
186 .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
191 in the tracee's memory.
195 .BR PTRACE_PEEKDATA ,
196 these two requests are currently equivalent.
199 .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
200 .\" and that is the name that seems common on other systems.
205 in the tracee's USER area.
207 .BR PTRACE_PEEKUSER ,
208 the offset must typically be word-aligned.
209 In order to maintain the integrity of the kernel,
210 some modifications to the USER area are disallowed.
211 .\" FIXME In the preceding sentence, which modifications are disallowed,
212 .\" and when they are disallowed, how does user space discover that fact?
214 .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
215 Copy the tracee's general-purpose or floating-point registers,
216 respectively, to the address
221 for information on the format of this data.
224 Note that SPARC systems have the meaning of
230 is ignored and the registers are copied to the address
235 are not present on all architectures.
237 .BR PTRACE_GETREGSET " (since Linux 2.6.34)"
238 Read the tracee's registers.
240 specifies, in an architecture-dependent way, the type of registers to be read.
242 (with numerical value 1)
243 usually results in reading of general-purpose registers.
244 If the CPU has, for example,
245 floating-point and/or vector registers, they can be retrieved by setting
253 which describes the destination buffer's location and length.
254 On return, the kernel modifies
256 to indicate the actual number of bytes returned.
258 .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
259 Retrieve information about the signal that caused the stop.
264 from the tracee to the address
270 .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
271 Modify the tracee's general-purpose or floating-point registers,
272 respectively, from the address
276 .BR PTRACE_POKEUSER ,
277 some general-purpose register modifications may be disallowed.
278 .\" FIXME In the preceding sentence, which modifications are disallowed,
279 .\" and when they are disallowed, how does user space discover that fact?
282 Note that SPARC systems have the meaning of
288 is ignored and the registers are copied from the address
293 are not present on all architectures.
295 .BR PTRACE_SETREGSET " (since Linux 2.6.34)"
296 Modify the tracee's registers.
302 .BR PTRACE_GETREGSET .
304 .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
305 Set signal information:
308 structure from the address
310 in the tracer to the tracee.
311 This will affect only signals that would normally be delivered to
312 the tracee and were caught by the tracer.
313 It may be difficult to tell
314 these normal signals from synthetic signals generated by
320 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
321 Set ptrace options from
326 is interpreted as a bit mask of options,
327 which are specified by the following flags:
330 .BR PTRACE_O_EXITKILL " (since Linux 3.8)"
331 .\" commit 992fb6e170639b0849bace8e49bf31bd37c4123
332 If a tracer sets this flag, a
334 signal will be sent to every tracee if the tracer exits.
335 This option is useful for ptrace jailers that
336 want to ensure that tracees can never escape the tracer's control.
338 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
339 Stop the tracee at the next
341 and automatically start tracing the newly cloned process,
342 which will start with a
346 by the tracer will return a
351 status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
354 The PID of the new process can be retrieved with
355 .BR PTRACE_GETEVENTMSG .
357 This option may not catch
365 .B PTRACE_EVENT_VFORK
366 will be delivered instead
368 .B PTRACE_O_TRACEVFORK
369 is set; otherwise if the tracee calls
371 with the exit signal set to
375 .B PTRACE_O_TRACEFORK
378 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
379 Stop the tracee at the next
383 by the tracer will return a
388 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
391 If the execing thread is not a thread group leader,
392 the thread ID is reset to thread group leader's ID before this stop.
393 Since Linux 3.0, the former thread ID can be retrieved with
394 .BR PTRACE_GETEVENTMSG .
396 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
397 Stop the tracee at exit.
400 by the tracer will return a
405 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
408 The tracee's exit status can be retrieved with
409 .BR PTRACE_GETEVENTMSG .
411 The tracee is stopped early during process exit,
412 when registers are still available,
413 allowing the tracer to see where the exit occurred,
414 whereas the normal exit notification is done after the process
416 Even though context is available,
417 the tracer cannot prevent the exit from happening at this point.
419 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
420 Stop the tracee at the next
422 and automatically start tracing the newly forked process,
423 which will start with a
427 by the tracer will return a
432 status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
435 The PID of the new process can be retrieved with
436 .BR PTRACE_GETEVENTMSG .
438 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
439 When delivering system call traps, set bit 7 in the signal number
441 .IR "SIGTRAP|0x80" ).
442 This makes it easy for the tracer to distinguish
443 normal traps from those caused by a system call.
444 .RB ( PTRACE_O_TRACESYSGOOD
445 may not work on all architectures.)
447 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
448 Stop the tracee at the next
450 and automatically start tracing the newly vforked process,
451 which will start with a
455 by the tracer will return a
460 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
463 The PID of the new process can be retrieved with
464 .BR PTRACE_GETEVENTMSG .
466 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
467 Stop the tracee at the completion of the next
471 by the tracer will return a
476 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
479 The PID of the new process can (since Linux 2.6.18) be retrieved with
480 .BR PTRACE_GETEVENTMSG .
483 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
484 Retrieve a message (as an
485 .IR "unsigned long" )
486 about the ptrace event
487 that just happened, placing it at the address
491 .BR PTRACE_EVENT_EXIT ,
492 this is the tracee's exit status.
494 .BR PTRACE_EVENT_FORK ,
495 .BR PTRACE_EVENT_VFORK ,
496 .BR PTRACE_EVENT_VFORK_DONE ,
498 .BR PTRACE_EVENT_CLONE ,
499 this is the PID of the new process.
504 Restart the stopped tracee process.
508 it is interpreted as the number of a signal to be delivered to the tracee;
509 otherwise, no signal is delivered.
510 Thus, for example, the tracer can control
511 whether a signal sent to the tracee is delivered or not.
515 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
516 Restart the stopped tracee as for
518 but arrange for the tracee to be stopped at
519 the next entry to or exit from a system call,
520 or after execution of a single instruction, respectively.
521 (The tracee will also, as usual, be stopped upon receipt of a signal.)
522 From the tracer's perspective, the tracee will appear to have been
523 stopped by receipt of a
527 for example, the idea is to inspect
528 the arguments to the system call at the first stop,
531 and inspect the return value of the system call at the second stop.
534 argument is treated as for
539 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
542 continue and stop on entry to the next system call,
543 which will not be executed.
545 .BR PTRACE_SYSEMU_SINGLESTEP ,
546 do the same but also singlestep if not a system call.
547 This call is used by programs like
548 User Mode Linux that want to emulate all the tracee's system calls.
551 argument is treated as for
556 These requests are currently
558 supported only on x86.
560 .BR PTRACE_LISTEN " (since Linux 3.4)"
561 Restart the stopped tracee, but prevent it from executing.
562 The resulting state of the tracee is similar to a process which
563 has been stopped by a
565 (or other stopping signal).
566 See the "group-stop" subsection for additional information.
568 works only on tracees attached by
580 .I This operation is deprecated; do not use it!
589 is that it requires the tracee to be in signal-delivery-stop,
590 otherwise it may not work
591 (i.e., may complete successfully but won't kill the tracee).
592 By contrast, sending a
594 directly has no such limitation.
595 .\" [Note from Denys Vlasenko:
596 .\" deprecation suggested by Oleg Nesterov. He prefers to deprecate it
597 .\" instead of describing (and needing to support) PTRACE_KILL's quirks.]
599 .BR PTRACE_INTERRUPT " (since Linux 3.4)"
601 If the tracee is running, it will stop with
602 .BR PTRACE_EVENT_STOP .
603 If the tracee is already stopped by a signal, or receives a signal
605 .BR PTRACE_INTERRUPT ,
606 it may report a group-stop
607 or a signal-delivery-stop instead of
608 .BR PTRACE_EVENT_STOP .
610 only works on tracees attached by
614 Attach to the process specified in
616 making it a tracee of the calling process.
617 .\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
618 .\" "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
619 .\" Basically, it's not true for any Linux in practical use.
620 .\" ; the behavior of the tracee is as if it had done a
621 .\" .BR PTRACE_TRACEME .
622 .\" The calling process actually becomes the parent of the tracee
623 .\" process for most purposes (e.g., it will receive
624 .\" notification of tracee events and appears in
626 .\" output as the tracee's parent), but a
628 .\" by the tracee will still return the PID of the original parent.
631 but will not necessarily have stopped
632 by the completion of this call; use
634 to wait for the tracee to stop.
635 See the "Attaching and detaching" subsection for additional information.
641 .BR PTRACE_SEIZE " (since Linux 3.4)"
642 Attach to the process specified in
644 making it a tracee of the calling process.
648 does not stop the process.
659 contains a bit mask of ptrace options to activate immediately.
662 Restart the stopped tracee as for
664 but first detach from it.
665 Under Linux, a tracee can be detached in this way regardless
666 of which method was used to initiate tracing.
669 .SS Death under ptrace
670 When a (possibly multithreaded) process receives a killing signal
671 (one whose disposition is set to
673 and whose default action is to kill the process),
675 Tracees report their death to their tracer(s).
676 Notification of this event is delivered via
679 Note that the killing signal will first cause signal-delivery-stop
680 (on one tracee only),
681 and only after it is injected by the tracer
682 (or after it was dispatched to a thread which isn't traced),
683 will death from the signal happen on
685 tracees within a multithreaded process.
686 (The term "signal-delivery-stop" is explained below.)
689 does not generate signal-delivery-stop and
690 therefore the tracer can't suppress it.
692 kills even within system calls
693 (syscall-exit-stop is not generated prior to death by
695 The net effect is that
697 always kills the process (all its threads),
698 even if some threads of the process are ptraced.
700 When the tracee calls
702 it reports its death to its tracer.
703 Other threads are not affected.
705 When any thread executes
707 every tracee in its thread group reports its death to its tracer.
710 .B PTRACE_O_TRACEEXIT
713 will happen before actual death.
714 This applies to exits via
717 and signal deaths (except
719 and when threads are torn down on
721 in a multithreaded process.
723 The tracer cannot assume that the ptrace-stopped tracee exists.
724 There are many scenarios when the tracee may die while stopped (such as
726 Therefore, the tracer must be prepared to handle an
728 error on any ptrace operation.
729 Unfortunately, the same error is returned if the tracee
730 exists but is not ptrace-stopped
731 (for commands which require a stopped tracee),
732 or if it is not traced by the process which issued the ptrace call.
733 The tracer needs to keep track of the stopped/running state of the tracee,
736 as "tracee died unexpectedly" only if it knows that the tracee has
737 been observed to enter ptrace-stop.
738 Note that there is no guarantee that
740 will reliably report the tracee's death status if a
741 ptrace operation returned
744 may return 0 instead.
745 In other words, the tracee may be "not yet fully dead",
746 but already refusing ptrace requests.
748 The tracer can't assume that the tracee
750 ends its life by reporting
753 .IR WIFSIGNALED(status) ;
754 there are cases where this does not occur.
755 For example, if a thread other than thread group leader does an
758 its PID will never be seen again,
759 and any subsequent ptrace stops will be reported under
760 the thread group leader's PID.
762 A tracee can be in two states: running or stopped.
764 There are many kinds of states when the tracee is stopped, and in ptrace
765 discussions they are often conflated.
766 Therefore, it is important to use precise terms.
768 In this manual page, any stopped state in which the tracee is ready
769 to accept ptrace commands from the tracer is called
772 be further subdivided into
773 .IR signal-delivery-stop ,
777 These stopped states are described in detail below.
779 When the running tracee enters ptrace-stop, it notifies its tracer using
781 (or one of the other "wait" system calls).
782 Most of this manual page assumes that the tracer waits with:
784 pid = waitpid(pid_or_minus_1, &status, __WALL);
786 Ptrace-stopped tracees are reported as returns with
789 .I WIFSTOPPED(status)
792 .\" Do we require __WALL usage, or will just using 0 be ok? (With 0,
793 .\" I am not 100% sure there aren't ugly corner cases.) Are the
794 .\" rules different if user wants to use waitid? Will waitid require
800 flag does not include the
804 flags, but implies their functionality.
810 is not recommended: the "continued" state is per-process and
811 consuming it can confuse the real parent of the tracee.
817 to return 0 ("no wait results available yet")
818 even if the tracer knows there should be a notification.
823 ptrace(PTRACE_CONT, pid, 0L, 0L);
824 if (errno == ESRCH) {
826 r = waitpid(tracee, &status, __WALL | WNOHANG);
827 /* r can still be 0 here! */
831 .\" waitid usage? WNOWAIT?
832 .\" describe how wait notifications queue (or not queue)
834 The following kinds of ptrace-stops exist: signal-delivery-stops,
837 stops, syscall-stops.
838 They all are reported by
841 .I WIFSTOPPED(status)
843 They may be differentiated by examining the value
845 and if there is ambiguity in that value, by querying
846 .BR PTRACE_GETSIGINFO .
849 macro can't be used to perform this examination,
850 because it returns the value
851 .IR "(status>>8)\ &\ 0xff" .)
852 .SS Signal-delivery-stop
853 When a (possibly multithreaded) process receives any signal except
855 the kernel selects an arbitrary thread which handles the signal.
856 (If the signal is generated with
858 the target thread can be explicitly selected by the caller.)
859 If the selected thread is traced, it enters signal-delivery-stop.
860 At this point, the signal is not yet delivered to the process,
861 and can be suppressed by the tracer.
862 If the tracer doesn't suppress the signal,
863 it passes the signal to the tracee in the next ptrace restart request.
864 This second step of signal delivery is called
865 .I "signal injection"
867 Note that if the signal is blocked,
868 signal-delivery-stop doesn't happen until the signal is unblocked,
869 with the usual exception that
873 Signal-delivery-stop is observed by the tracer as
876 .I WIFSTOPPED(status)
877 true, with the signal returned by
878 .IR WSTOPSIG(status) .
881 this may be a different kind of ptrace-stop;
882 see the "Syscall-stops" and "execve" sections below for details.
885 returns a stopping signal, this may be a group-stop; see below.
886 .SS Signal injection and suppression
887 After signal-delivery-stop is observed by the tracer,
888 the tracer should restart the tracee with the call
890 ptrace(PTRACE_restart, pid, 0, sig)
894 is one of the restarting ptrace requests.
897 is 0, then a signal is not delivered.
898 Otherwise, the signal
901 This operation is called
902 .I "signal injection"
903 in this manual page, to distinguish it from signal-delivery-stop.
907 value may be different from the
909 value: the tracer can cause a different signal to be injected.
911 Note that a suppressed signal still causes system calls to return
913 In this case system calls will be restarted: the tracer will
914 observe the tracee to reexecute the interrupted system call (or
915 .BR restart_syscall (2)
916 system call for a few syscalls which use a different mechanism
917 for restarting) if the tracer uses
919 Even system calls (such as
921 which are not restartable after signal are restarted after
922 signal is suppressed;
923 however, kernel bugs exist which cause some syscalls to fail with
925 even though no observable signal is injected to the tracee.
927 Restarting ptrace commands issued in ptrace-stops other than
928 signal-delivery-stop are not guaranteed to inject a signal, even if
931 No error is reported; a nonzero
933 may simply be ignored.
934 Ptrace users should not try to "create a new signal" this way: use
938 The fact that signal injection requests may be ignored
939 when restarting the tracee after
940 ptrace stops that are not signal-delivery-stops
941 is a cause of confusion among ptrace users.
942 One typical scenario is that the tracer observes group-stop,
943 mistakes it for signal-delivery-stop, restarts the tracee with
945 ptrace(PTRACE_restart, pid, 0, stopsig)
947 with the intention of injecting
951 gets ignored and the tracee continues to run.
955 signal has a side effect of waking up (all threads of)
956 a group-stopped process.
957 This side effect happens before signal-delivery-stop.
958 The tracer can't suppress this side effect (it can
959 only suppress signal injection, which only causes the
961 handler to not be executed in the tracee, if such a handler is installed).
962 In fact, waking up from group-stop may be followed by
963 signal-delivery-stop for signal(s)
966 if they were pending when
971 may be not the first signal observed by the tracee after it was sent.
973 Stopping signals cause (all threads of) a process to enter group-stop.
974 This side effect happens after signal injection, and therefore can be
975 suppressed by the tracer.
977 In Linux 2.4 and earlier, the
979 signal can't be injected.
980 .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
983 .\" /* The debugger continued. Ignore SIGSTOP. */
984 .\" if (signr == SIGSTOP)
988 can be used to retrieve a
990 structure which corresponds to the delivered signal.
992 may be used to modify it.
995 has been used to alter
1001 parameter in the restarting command must match,
1002 otherwise the result is undefined.
1004 When a (possibly multithreaded) process receives a stopping signal,
1006 If some threads are traced, they enter a group-stop.
1007 Note that the stopping signal will first cause signal-delivery-stop
1008 (on one tracee only), and only after it is injected by the tracer
1009 (or after it was dispatched to a thread which isn't traced),
1010 will group-stop be initiated on
1012 tracees within the multithreaded process.
1013 As usual, every tracee reports its group-stop separately
1014 to the corresponding tracer.
1016 Group-stop is observed by the tracer as
1019 .I WIFSTOPPED(status)
1020 true, with the stopping signal available via
1021 .IR WSTOPSIG(status) .
1022 The same result is returned by some other classes of ptrace-stops,
1023 therefore the recommended practice is to perform the call
1025 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
1027 The call can be avoided if the signal is not
1033 only these four signals are stopping signals.
1034 If the tracer sees something else, it can't be a group-stop.
1035 Otherwise, the tracer needs to call
1036 .BR PTRACE_GETSIGINFO .
1038 .B PTRACE_GETSIGINFO
1041 then it is definitely a group-stop.
1042 (Other failure codes are possible, such as
1044 ("no such process") if a
1049 after the tracer sees the tracee ptrace-stop and until it
1050 restarts or kills it, the tracee will not run,
1051 and will not send notifications (except
1053 death) to the tracer, even if the tracer enters into another
1057 The kernel behavior described in the previous paragraph
1058 causes a problem with transparent handling of stopping signals.
1059 If the tracer restarts the tracee after group-stop,
1061 is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
1062 If the tracer doesn't restart the tracee before entering into the next
1066 signals will not be reported to the tracer;
1067 this would cause the
1069 signals to have no effect on the tracee.
1071 Since Linux 3.4, there is a method to overcome this problem: instead of
1075 command can be used to restart a tracee in a way where it does not execute,
1076 but waits for a new event which it can report via
1079 it is restarted by a
1081 .SS PTRACE_EVENT stops
1084 options, the tracee will enter ptrace-stops called
1089 stops are observed by the tracer as
1092 .IR WIFSTOPPED(status) ,
1097 An additional bit is set in the higher byte of the status word:
1102 (SIGTRAP | PTRACE_EVENT_foo << 8).
1104 The following events exist:
1106 .B PTRACE_EVENT_VFORK
1107 Stop before return from
1114 When the tracee is continued after this stop, it will wait for child to
1115 exit/exec before continuing its execution
1116 (in other words, the usual behavior on
1119 .B PTRACE_EVENT_FORK
1120 Stop before return from
1124 with the exit signal set to
1127 .B PTRACE_EVENT_CLONE
1128 Stop before return from
1131 .B PTRACE_EVENT_VFORK_DONE
1132 Stop before return from
1139 but after the child unblocked this tracee by exiting or execing.
1141 For all four stops described above,
1142 the stop occurs in the parent (i.e., the tracee),
1143 not in the newly created thread.
1144 .BR PTRACE_GETEVENTMSG
1145 can be used to retrieve the new thread's ID.
1147 .B PTRACE_EVENT_EXEC
1148 Stop before return from
1151 .BR PTRACE_GETEVENTMSG
1152 returns the former thread ID.
1154 .B PTRACE_EVENT_EXIT
1155 Stop before exit (including death from
1156 .BR exit_group (2)),
1157 signal death, or exit caused by
1159 in a multithreaded process.
1160 .B PTRACE_GETEVENTMSG
1161 returns the exit status.
1162 Registers can be examined
1163 (unlike when "real" exit happens).
1164 The tracee is still alive; it needs to be
1167 .BR PTRACE_DETACH ed
1170 .B PTRACE_EVENT_STOP
1175 .B PTRACE_GETSIGINFO
1185 .IR "(event<<8)\ |\ SIGTRAP" .
1187 If the tracee was restarted by
1188 .BR PTRACE_SYSCALL ,
1190 syscall-enter-stop just prior to entering any system call.
1191 If the tracer restarts the tracee with
1192 .BR PTRACE_SYSCALL ,
1193 the tracee enters syscall-exit-stop when the system call is finished,
1194 or if it is interrupted by a signal.
1195 (That is, signal-delivery-stop never happens between syscall-enter-stop
1196 and syscall-exit-stop; it happens
1200 Other possibilities are that the tracee may stop in a
1202 stop, exit (if it entered
1205 .BR exit_group (2)),
1208 or die silently (if it is a thread group leader, the
1210 happened in another thread,
1211 and that thread is not traced by the same tracer;
1212 this situation is discussed later).
1214 Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1217 .I WIFSTOPPED(status)
1223 .B PTRACE_O_TRACESYSGOOD
1224 option was set by the tracer, then
1227 .IR "(SIGTRAP\ |\ 0x80)" .
1229 Syscall-stops can be distinguished from signal-delivery-stop with
1232 .BR PTRACE_GETSIGINFO
1233 for the following cases:
1237 was delivered as a result of a user-space action,
1238 for example, a system call
1243 expiration of a POSIX timer,
1244 change of state on a POSIX message queue,
1245 or completion of an asynchronous I/O request.
1247 .IR si_code " == SI_KERNEL (0x80)"
1249 was sent by the kernel.
1251 .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1252 This is a syscall-stop.
1254 However, syscall-stops happen very often (twice per system call),
1256 .B PTRACE_GETSIGINFO
1257 for every syscall-stop may be somewhat expensive.
1259 Some architectures allow the cases to be distinguished
1260 by examining registers.
1261 For example, on x86,
1265 in syscall-enter-stop.
1268 (like any other signal) always happens
1273 almost never contains
1277 looks like "syscall-stop which is not syscall-enter-stop";
1278 in other words, it looks like a
1279 "stray syscall-exit-stop" and can be detected this way.
1280 But such detection is fragile and is best avoided.
1283 .B PTRACE_O_TRACESYSGOOD
1284 option is the recommended method to distinguish syscall-stops
1285 from other kinds of ptrace-stops,
1286 since it is reliable and does not incur a performance penalty.
1288 Syscall-enter-stop and syscall-exit-stop are
1289 indistinguishable from each other by the tracer.
1290 The tracer needs to keep track of the sequence of
1291 ptrace-stops in order to not misinterpret syscall-enter-stop as
1292 syscall-exit-stop or vice versa.
1293 The rule is that syscall-enter-stop is
1294 always followed by syscall-exit-stop,
1296 stop or the tracee's death;
1297 no other kinds of ptrace-stop can occur in between.
1299 If after syscall-enter-stop,
1300 the tracer uses a restarting command other than
1301 .BR PTRACE_SYSCALL ,
1302 syscall-exit-stop is not generated.
1304 .B PTRACE_GETSIGINFO
1305 on syscall-stops returns
1314 .IR (SIGTRAP|0x80) .
1315 .SS PTRACE_SINGLESTEP, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP stops
1316 [Details of these kinds of stops are yet to be documented.]
1319 .\" document stops occurring with PTRACE_SINGLESTEP, PTRACE_SYSEMU,
1320 .\" PTRACE_SYSEMU_SINGLESTEP
1321 .SS Informational and restarting ptrace commands
1322 Most ptrace commands (all except
1325 .BR PTRACE_TRACEME ,
1326 .BR PTRACE_INTERRUPT ,
1329 require the tracee to be in a ptrace-stop, otherwise they fail with
1332 When the tracee is in ptrace-stop,
1333 the tracer can read and write data to
1334 the tracee using informational commands.
1335 These commands leave the tracee in ptrace-stopped state:
1338 ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1339 ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1340 ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1341 ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1342 ptrace(PTRACE_GETREGSET, pid, NT_foo, &iov);
1343 ptrace(PTRACE_SETREGSET, pid, NT_foo, &iov);
1344 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1345 ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1346 ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1347 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1350 Note that some errors are not reported.
1351 For example, setting signal information
1353 may have no effect in some ptrace-stops, yet the call may succeed
1354 (return 0 and not set
1357 .B PTRACE_GETEVENTMSG
1358 may succeed and return some random value if current ptrace-stop
1359 is not documented as returning a meaningful event message.
1363 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1366 The tracee's current flags are replaced.
1367 Flags are inherited by new tracees created and "auto-attached" via active
1368 .BR PTRACE_O_TRACEFORK ,
1369 .BR PTRACE_O_TRACEVFORK ,
1371 .BR PTRACE_O_TRACECLONE
1374 Another group of commands makes the ptrace-stopped tracee run.
1377 ptrace(cmd, pid, 0, sig);
1385 .BR PTRACE_SYSCALL ,
1386 .BR PTRACE_SINGLESTEP ,
1389 .BR PTRACE_SYSEMU_SINGLESTEP .
1390 If the tracee is in signal-delivery-stop,
1392 is the signal to be injected (if it is nonzero).
1396 (When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
1397 recommended practice is to always pass 0 in
1399 .SS Attaching and detaching
1400 A thread can be attached to the tracer using the call
1402 ptrace(PTRACE_ATTACH, pid, 0, 0);
1406 ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_flags);
1412 If the tracer wants this
1414 to have no effect, it needs to suppress it.
1415 Note that if other signals are concurrently sent to
1416 this thread during attach,
1417 the tracer may see the tracee enter signal-delivery-stop
1418 with other signal(s) first!
1419 The usual practice is to reinject these signals until
1421 is seen, then suppress
1424 The design bug here is that a ptrace attach and a concurrently delivered
1426 may race and the concurrent
1430 .\" FIXME: Describe how to attach to a thread which is already
1433 Since attaching sends
1435 and the tracer usually suppresses it, this may cause a stray
1437 return from the currently executing system call in the tracee,
1438 as described in the "Signal injection and suppression" section.
1442 can be used instead of
1445 does not stop the attached process.
1447 it after attach (or at any other time) without sending it any signals,
1454 ptrace(PTRACE_TRACEME, 0, 0, 0);
1456 turns the calling thread into a tracee.
1457 The thread continues to run (doesn't enter ptrace-stop).
1458 A common practice is to follow the
1464 and allow the parent (which is our tracer now) to observe our
1465 signal-delivery-stop.
1468 .BR PTRACE_O_TRACEFORK ,
1469 .BR PTRACE_O_TRACEVFORK ,
1471 .BR PTRACE_O_TRACECLONE
1472 options are in effect, then children created by, respectively,
1482 with the exit signal set to
1486 are automatically attached to the same tracer which traced their parent.
1488 is delivered to the children, causing them to enter
1489 signal-delivery-stop after they exit the system call which created them.
1491 Detaching of the tracee is performed by:
1493 ptrace(PTRACE_DETACH, pid, 0, sig);
1496 is a restarting operation;
1497 therefore it requires the tracee to be in ptrace-stop.
1498 If the tracee is in signal-delivery-stop, a signal can be injected.
1501 parameter may be silently ignored.
1503 If the tracee is running when the tracer wants to detach it,
1504 the usual solution is to send
1508 to make sure it goes to the correct thread),
1509 wait for the tracee to stop in signal-delivery-stop for
1511 and then detach it (suppressing
1514 A design bug is that this can race with concurrent
1516 Another complication is that the tracee may enter other ptrace-stops
1517 and needs to be restarted and waited for again, until
1520 Yet another complication is to be sure that
1521 the tracee is not already ptrace-stopped,
1522 because no signal delivery happens while it is\(emnot even
1524 .\" FIXME: Describe how to detach from a group-stopped tracee so that it
1525 .\" doesn't run, but continues to wait for SIGCONT.
1527 If the tracer dies, all tracees are automatically detached and restarted,
1528 unless they were in group-stop.
1529 Handling of restart from group-stop is currently buggy,
1530 but the "as planned" behavior is to leave tracee stopped and waiting for
1532 If the tracee is restarted from signal-delivery-stop,
1533 the pending signal is injected.
1534 .SS execve(2) under ptrace
1535 .\" clone(2) CLONE_THREAD says:
1536 .\" If any of the threads in a thread group performs an execve(2),
1537 .\" then all threads other than the thread group leader are terminated,
1538 .\" and the new program is executed in the thread group leader.
1540 When one thread in a multithreaded process calls
1542 the kernel destroys all other threads in the process,
1543 .\" In kernel 3.1 sources, see fs/exec.c::de_thread()
1544 and resets the thread ID of the execing thread to the
1545 thread group ID (process ID).
1546 (Or, to put things another way, when a multithreaded process does an
1548 at completion of the call, it appears as though the
1550 occurred in the thread group leader, regardless of which thread did the
1552 This resetting of the thread ID looks very confusing to tracers:
1554 All other threads stop in
1555 .B PTRACE_EVENT_EXIT
1557 .BR PTRACE_O_TRACEEXIT
1558 option was turned on.
1559 Then all other threads except the thread group leader report
1560 death as if they exited via
1564 The execing tracee changes its thread ID while it is in the
1566 (Remember, under ptrace, the "pid" returned from
1568 or fed into ptrace calls, is the tracee's thread ID.)
1569 That is, the tracee's thread ID is reset to be the same as its process ID,
1570 which is the same as the thread group leader's thread ID.
1573 .B PTRACE_EVENT_EXEC
1574 stop happens, if the
1575 .BR PTRACE_O_TRACEEXEC
1576 option was turned on.
1578 If the thread group leader has reported its
1579 .B PTRACE_EVENT_EXIT
1581 it appears to the tracer that
1582 the dead thread leader "reappears from nowhere".
1583 (Note: the thread group leader does not report death via
1584 .I WIFEXITED(status)
1585 until there is at least one other live thread.
1586 This eliminates the possibility that the tracer will see
1587 it dying and then reappearing.)
1588 If the thread group leader was still alive,
1589 for the tracer this may look as if thread group leader
1590 returns from a different system call than it entered,
1591 or even "returned from a system call even though
1592 it was not in any system call".
1593 If the thread group leader was not traced
1594 (or was traced by a different tracer), then during
1596 it will appear as if it has become a tracee of
1597 the tracer of the execing tracee.
1599 All of the above effects are the artifacts of
1600 the thread ID change in the tracee.
1603 .B PTRACE_O_TRACEEXEC
1604 option is the recommended tool for dealing with this situation.
1606 .BR PTRACE_EVENT_EXEC
1611 In this stop, the tracer can use
1612 .B PTRACE_GETEVENTMSG
1613 to retrieve the tracee's former thread ID.
1614 (This feature was introduced in Linux 3.0).
1616 .B PTRACE_O_TRACEEXEC
1617 option disables legacy
1622 When the tracer receives
1623 .B PTRACE_EVENT_EXEC
1625 it is guaranteed that except this tracee and the thread group leader,
1626 no other threads from the process are alive.
1629 .B PTRACE_EVENT_EXEC
1631 the tracer should clean up all its internal
1632 data structures describing the threads of this process,
1633 and retain only one data structure\(emone which
1634 describes the single still running tracee, with
1636 thread ID == thread group ID == process ID.
1638 Example: two threads call
1643 *** we get syscall-enter-stop in thread 1: **
1644 PID1 execve("/bin/foo", "foo" <unfinished ...>
1645 *** we issue PTRACE_SYSCALL for thread 1 **
1646 *** we get syscall-enter-stop in thread 2: **
1647 PID2 execve("/bin/bar", "bar" <unfinished ...>
1648 *** we issue PTRACE_SYSCALL for thread 2 **
1649 *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
1650 *** we get syscall-exit-stop for PID0: **
1651 PID0 <... execve resumed> ) = 0
1655 .B PTRACE_O_TRACEEXEC
1658 in effect for the execing tracee, the kernel delivers an extra
1663 This is an ordinary signal (similar to one which can be
1666 not a special kind of ptrace-stop.
1668 .B PTRACE_GETSIGINFO
1669 for this signal returns
1673 This signal may be blocked by signal mask,
1674 and thus may be delivered (much) later.
1676 Usually, the tracer (for example,
1678 would not want to show this extra post-execve
1680 signal to the user, and would suppress its delivery to the tracee (if
1684 it is a killing signal).
1685 However, determining
1688 to suppress is not easy.
1690 .B PTRACE_O_TRACEEXEC
1691 option and thus suppressing this extra
1693 is the recommended approach.
1695 The ptrace API (ab)uses the standard UNIX parent/child signaling over
1697 This used to cause the real parent of the process to stop receiving
1700 notifications when the child process is traced by some other process.
1702 Many of these bugs have been fixed, but as of Linux 2.6.38 several still
1703 exist; see BUGS below.
1705 As of Linux 2.6.38, the following is believed to work correctly:
1707 exit/death by signal is reported first to the tracer, then,
1708 when the tracer consumes the
1710 result, to the real parent (to the real parent only when the
1711 whole multithreaded process exits).
1712 If the tracer and the real parent are the same process,
1713 the report is sent only once.
1717 requests return the requested data,
1718 while other requests return zero.
1719 On error, all requests return \-1, and
1721 is set appropriately.
1722 Since the value returned by a successful
1724 request may be \-1, the caller must clear
1726 before the call, and then check it afterward
1727 to determine whether or not an error occurred.
1731 (i386 only) There was an error with allocating or freeing a debug register.
1734 There was an attempt to read from or write to an invalid area in
1735 the tracer's or the tracee's memory,
1736 probably because the area wasn't mapped or accessible.
1737 Unfortunately, under Linux, different variations of this fault
1742 more or less arbitrarily.
1745 An attempt was made to set an invalid option.
1749 is invalid, or an attempt was made to read from or
1750 write to an invalid area in the tracer's or the tracee's memory,
1751 or there was a word-alignment violation,
1752 or an invalid signal was specified during a restart request.
1755 The specified process cannot be traced.
1756 This could be because the
1757 tracer has insufficient privileges (the required capability is
1758 .BR CAP_SYS_PTRACE );
1759 unprivileged processes cannot trace processes that they
1760 cannot send signals to or those running
1761 set-user-ID/set-group-ID programs, for obvious reasons.
1762 Alternatively, the process may already be being traced,
1763 or (on kernels before 2.6.26) be
1768 The specified process does not exist, or is not currently being traced
1769 by the caller, or is not stopped
1770 (for requests that require a stopped tracee).
1774 Although arguments to
1776 are interpreted according to the prototype given,
1777 glibc currently declares
1779 as a variadic function with only the
1782 It is recommended to always supply four arguments,
1783 even if the requested operation does not use them,
1784 setting unused/ignored arguments to
1789 In Linux kernels before 2.6.26,
1790 .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
1792 the process with PID 1, may not be traced.
1794 The layout of the contents of memory and the USER area are
1795 quite operating-system- and architecture-specific.
1796 The offset supplied, and the data returned,
1797 might not entirely match with the definition of
1799 .\" See http://lkml.org/lkml/2008/5/8/375
1801 The size of a "word" is determined by the operating-system variant
1802 (e.g., for 32-bit Linux it is 32 bits).
1804 This page documents the way the
1806 call works currently in Linux.
1807 Its behavior differs noticeably on other flavors of UNIX.
1810 is highly specific to the operating system and architecture.
1812 On hosts with 2.6 kernel headers,
1813 .B PTRACE_SETOPTIONS
1814 is declared with a different value than the one for 2.4.
1815 This leads to applications compiled with 2.6 kernel
1816 headers failing when run on 2.4 kernels.
1817 This can be worked around by redefining
1818 .B PTRACE_SETOPTIONS
1820 .BR PTRACE_OLDSETOPTIONS ,
1823 Group-stop notifications are sent to the tracer, but not to real parent.
1824 Last confirmed on 2.6.38.6.
1826 If a thread group leader is traced and exits by calling
1828 .\" Note from Denys Vlasenko:
1829 .\" Here "exits" means any kind of death - _exit, exit_group,
1830 .\" signal death. Signal death and exit_group cases are trivial,
1831 .\" though: since signal death and exit_group kill all other threads
1832 .\" too, "until all other threads exit" thing happens rather soon
1833 .\" in these cases. Therefore, only _exit presents observably
1834 .\" puzzling behavior to ptrace users: thread leader _exit's,
1835 .\" but WIFEXITED isn't reported! We are trying to explain here
1838 .B PTRACE_EVENT_EXIT
1839 stop will happen for it (if requested), but the subsequent
1841 notification will not be delivered until all other threads exit.
1842 As explained above, if one of other threads calls
1844 the death of the thread group leader will
1847 If the execed thread is not traced by this tracer,
1848 the tracer will never know that
1851 One possible workaround is to
1853 the thread group leader instead of restarting it in this case.
1854 Last confirmed on 2.6.38.6.
1855 .\" FIXME: ^^^ need to test/verify this scenario
1859 signal may still cause a
1860 .B PTRACE_EVENT_EXIT
1861 stop before actual signal death.
1862 This may be changed in the future;
1864 is meant to always immediately kill tasks even under ptrace.
1865 Last confirmed on 2.6.38.6.
1867 Some system calls return with
1869 if a signal was sent to a tracee, but delivery was suppressed by the tracer.
1870 (This is very typical operation: it is usually
1871 done by debuggers on every attach, in order to not introduce
1874 As of Linux 3.2.9, the following system calls are affected
1875 (this list is likely incomplete):
1882 The usual symptom of this bug is that when you attach to
1883 a quiescent process with the command
1885 strace -p <process-ID>
1887 then, instead of the usual
1888 and expected one-line output such as
1891 restart_syscall(<... resuming interrupted call ...>_
1897 select(6, [5], NULL, [5], NULL_
1900 ('_' denotes the cursor position), you observe more than one line.
1904 clock_gettime(CLOCK_MONOTONIC, {15370, 690928118}) = 0
1908 What is not visible here is that the process was blocked in
1915 to return to user space with the error
1917 In this particular case, the program reacted to
1919 by checking the current time, and then executing
1922 (Programs which do not expect such "stray"
1924 errors may behave in an unintended way upon an
1939 .BR capabilities (7),