1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (c) 1993 Michael Haardt <michael@moria.de>
4 .\" Fri Apr 2 11:32:09 MET DST 1993
6 .\" and changes Copyright (C) 1999 Mike Coleman (mkc@acm.org)
7 .\" -- major revision to fully document ptrace semantics per recent Linux
8 .\" kernel (2.2.10) and glibc (2.1.2)
9 .\" Sun Nov 7 03:18:35 CST 1999
11 .\" and Copyright (c) 2011, Denys Vlasenko <vda.linux@googlemail.com>
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.
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.
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.
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,
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>
36 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Added notes on capability requirements
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.)
43 .\" 2011-09, major update by Denys Vlasenko <vda.linux@googlemail.com>
45 .\" FIXME Linux 2.6.34 adds PTRACE_GETREGSET/PTRACE_SETREGSET
46 .\" FIXME Linux 3.1 adds PTRACE_SEIZE, PTRACE_INTERRUPT,
47 .\" and PTRACE_LISTEN.
49 .TH PTRACE 2 2012-04-26 "Linux" "Linux Programmer's Manual"
51 ptrace \- process trace
54 .B #include <sys/ptrace.h>
56 .BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
57 .BI " void *" addr ", void *" data );
62 system call provides a means by which one process (the "tracer")
63 may observe and control the execution of another process (the "tracee"),
64 and examine and change the tracee's memory and registers.
65 It is primarily used to implement breakpoint debugging and system
68 A tracee first needs to be attached to the tracer.
69 Attachment and subsequent commands are per thread:
70 in a multithreaded process,
71 every thread can be individually attached to a
72 (potentially different) tracer,
73 or left not attached and thus not debugged.
74 Therefore, "tracee" always means "(one) thread",
75 never "a (possibly multithreaded) process".
76 Ptrace commands are always sent to
77 a specific tracee using a call of the form
79 ptrace(PTRACE_foo, pid, ...)
83 is the thread ID of the corresponding Linux thread.
85 (Note that in this page, a "multithreaded process"
86 means a thread group consisting of threads created using the
91 A process can initiate a trace by calling
93 and having the resulting child do a
95 followed (typically) by an
97 Alternatively, one process may commence tracing another process using
100 While being traced, the tracee will stop each time a signal is delivered,
101 even if the signal is being ignored.
104 which has its usual effect.)
105 The tracer will be notified at its next call to
107 (or one of the related "wait" system calls); that call will return a
109 value containing information that indicates
110 the cause of the stop in the tracee.
111 While the tracee is stopped,
112 the tracer can use various ptrace requests to inspect and modify the tracee.
113 The tracer then causes the tracee to continue,
114 optionally ignoring the delivered signal
115 (or even delivering a different signal instead).
118 .B PTRACE_O_TRACEEXEC
119 option is not in effect, all successful calls to
121 by the traced process will cause it to be sent a
124 giving the parent a chance to gain control before the new program
127 When the tracer is finished tracing, it can cause the tracee to continue
128 executing in a normal, untraced mode via
133 determines the action to be performed:
136 Indicate that this process is to be traced by its parent.
137 A process probably shouldn't make this request if its parent
138 isn't expecting to trace it.
147 request is used only by the tracee;
148 the remaining requests are used only by the tracer.
149 In the following requests,
151 specifies the thread ID of the tracee to be acted on.
152 For requests other than
156 the tracee must be stopped.
158 .BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
159 Read a word at the address
161 in the tracee's memory, returning the word as the result of the
164 Linux does not have separate text and data address spaces,
165 so these two requests are currently equivalent.
170 .\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
171 .\" and that is the name that seems common on other systems.
172 Read a word at offset
174 in the tracee's USER area,
175 which holds the registers and other information about the process
178 The word is returned as the result of the
181 Typically, the offset must be word-aligned, though this might vary by
187 .BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
192 in the tracee's memory.
196 .BR PTRACE_PEEKDATA ,
197 these two requests are currently equivalent.
200 .\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
201 .\" and that is the name that seems common on other systems.
206 in the tracee's USER area.
208 .BR PTRACE_PEEKUSER ,
209 the offset must typically be word-aligned.
210 In order to maintain the integrity of the kernel,
211 some modifications to the USER area are disallowed.
212 .\" FIXME In the preceding sentence, which modifications are disallowed,
213 .\" and when they are disallowed, how does userspace discover that fact?
215 .BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
216 Copy the tracee's general-purpose or floating-point registers,
217 respectively, to the address
222 for information on the format of this data.
225 Note that SPARC systems have the meaning of
231 is ignored and the registers are copied to the address
234 .BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
235 Retrieve information about the signal that caused the stop.
240 from the tracee to the address
246 .BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
247 Copy the tracee's general-purpose or floating-point registers,
248 respectively, from the address
252 .BR PTRACE_POKEUSER ,
253 some general-purpose register modifications may be disallowed.
254 .\" FIXME In the preceding sentence, which modifications are disallowed,
255 .\" and when they are disallowed, how does userspace discover that fact?
258 Note that SPARC systems have the meaning of
264 is ignored and the registers are copied from the address
267 .BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
268 Set signal information:
271 structure from the address
273 in the tracer to the tracee.
274 This will affect only signals that would normally be delivered to
275 the tracee and were caught by the tracer.
276 It may be difficult to tell
277 these normal signals from synthetic signals generated by
283 .BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
284 Set ptrace options from
289 is interpreted as a bit mask of options,
290 which are specified by the following flags:
293 .BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
294 When delivering system call traps, set bit 7 in the signal number
296 .IR "SIGTRAP|0x80" ).
297 This makes it easy for the tracer to distinguish
298 normal traps from those caused by a system call.
299 .RB ( PTRACE_O_TRACESYSGOOD
300 may not work on all architectures.)
302 .BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
303 Stop the tracee at the next
305 and automatically start tracing the newly forked process,
306 which will start with a
310 by the tracer will return a
315 status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8))
318 The PID of the new process can be retrieved with
319 .BR PTRACE_GETEVENTMSG .
321 .BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
322 Stop the tracee at the next
324 and automatically start tracing the newly vforked process,
325 which will start with a
329 by the tracer will return a
334 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK<<8))
337 The PID of the new process can be retrieved with
338 .BR PTRACE_GETEVENTMSG .
340 .BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
341 Stop the tracee at the next
343 and automatically start tracing the newly cloned process,
344 which will start with a
348 by the tracer will return a
353 status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))
356 The PID of the new process can be retrieved with
357 .BR PTRACE_GETEVENTMSG .
359 This option may not catch
367 .B PTRACE_EVENT_VFORK
368 will be delivered instead
370 .B PTRACE_O_TRACEVFORK
371 is set; otherwise if the tracee calls
373 with the exit signal set to
377 .B PTRACE_O_TRACEFORK
380 .BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
381 Stop the tracee at the next
385 by the tracer will return a
390 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
393 If the execing thread is not a thread group leader,
394 the thread ID is reset to thread group leader's ID before this stop.
395 Since Linux 3.0, the former thread ID can be retrieved with
396 .BR PTRACE_GETEVENTMSG .
398 .BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
399 Stop the tracee at the completion of the next
403 by the tracer will return a
408 status>>8 == (SIGTRAP | (PTRACE_EVENT_VFORK_DONE<<8))
411 The PID of the new process can (since Linux 2.6.18) be retrieved with
412 .BR PTRACE_GETEVENTMSG .
414 .BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
415 Stop the tracee at exit.
418 by the tracer will return a
423 status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
426 The tracee's exit status can be retrieved with
427 .BR PTRACE_GETEVENTMSG .
429 The tracee is stopped early during process exit,
430 when registers are still available,
431 allowing the tracer to see where the exit occurred,
432 whereas the normal exit notification is done after the process
434 Even though context is available,
435 the tracer cannot prevent the exit from happening at this point.
438 .BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
439 Retrieve a message (as an
440 .IR "unsigned long" )
441 about the ptrace event
442 that just happened, placing it at the address
446 .BR PTRACE_EVENT_EXIT ,
447 this is the tracee's exit status.
449 .BR PTRACE_EVENT_FORK ,
450 .BR PTRACE_EVENT_VFORK ,
451 .BR PTRACE_EVENT_VFORK_DONE ,
453 .BR PTRACE_EVENT_CLONE ,
454 this is the PID of the new process.
459 Restart the stopped tracee process.
463 it is interpreted as the number of a signal to be delivered to the tracee;
464 otherwise, no signal is delivered.
465 Thus, for example, the tracer can control
466 whether a signal sent to the tracee is delivered or not.
470 .BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
471 Restart the stopped tracee as for
473 but arrange for the tracee to be stopped at
474 the next entry to or exit from a system call,
475 or after execution of a single instruction, respectively.
476 (The tracee will also, as usual, be stopped upon receipt of a signal.)
477 From the tracer's perspective, the tracee will appear to have been
478 stopped by receipt of a
482 for example, the idea is to inspect
483 the arguments to the system call at the first stop,
486 and inspect the return value of the system call at the second stop.
489 argument is treated as for
494 .BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
497 continue and stop on entry to the next system call,
498 which will not be executed.
500 .BR PTRACE_SYSEMU_SINGLESTEP ,
501 do the same but also singlestep if not a system call.
502 This call is used by programs like
503 User Mode Linux that want to emulate all the tracee's system calls.
506 argument is treated as for
510 not supported on all architectures.)
521 .I This operation is deprecated; do not use it!
530 is that it requires the tracee to be in signal-delivery-stop,
531 otherwise it may not work
532 (i.e., may complete successfully but won't kill the tracee).
533 By contrast, sending a
535 directly has no such limitation.
536 .\" [Note from Denys Vlasenko:
537 .\" deprecation suggested by Oleg Nesterov. He prefers to deprecate it
538 .\" instead of describing (and needing to support) PTRACE_KILL's quirks.]
541 Attach to the process specified in
543 making it a tracee of the calling process.
544 .\" No longer true (removed by Denys Vlasenko, 2011, who remarks:
545 .\" "I think it isn't true in non-ancient 2.4 and in 2.6/3.x.
546 .\" Basically, it's not true for any Linux in practical use.
547 .\" ; the behavior of the tracee is as if it had done a
548 .\" .BR PTRACE_TRACEME .
549 .\" The calling process actually becomes the parent of the tracee
550 .\" process for most purposes (e.g., it will receive
551 .\" notification of tracee events and appears in
553 .\" output as the tracee's parent), but a
555 .\" by the tracee will still return the PID of the original parent.
558 but will not necessarily have stopped
559 by the completion of this call; use
561 to wait for the tracee to stop.
562 See the "Attaching and detaching" subsection for additional information.
569 Restart the stopped tracee as for
571 but first detach from it.
572 Under Linux, a tracee can be detached in this way regardless
573 of which method was used to initiate tracing.
576 .SS Death under ptrace
577 When a (possibly multithreaded) process receives a killing signal
578 (one whose disposition is set to
580 and whose default action is to kill the process),
582 Tracees report their death to their tracer(s).
583 Notification of this event is delivered via
586 Note that the killing signal will first cause signal-delivery-stop
587 (on one tracee only),
588 and only after it is injected by the tracer
589 (or after it was dispatched to a thread which isn't traced),
590 will death from the signal happen on
592 tracees within a multithreaded process.
593 (The term "signal-delivery-stop" is explained below.)
596 operates similarly, with exceptions.
597 No signal-delivery-stop is generated for
599 and therefore the tracer can't suppress it.
601 kills even within system calls
602 (syscall-exit-stop is not generated prior to death by
604 The net effect is that
606 always kills the process (all its threads),
607 even if some threads of the process are ptraced.
609 When the tracee calls
611 it reports its death to its tracer.
612 Other threads are not affected.
614 When any thread executes
616 every tracee in its thread group reports its death to its tracer.
619 .B PTRACE_O_TRACEEXIT
622 will happen before actual death.
623 This applies to exits via
626 and signal deaths (except
628 and when threads are torn down on
630 in a multithreaded process.
632 The tracer cannot assume that the ptrace-stopped tracee exists.
633 There are many scenarios when the tracee may die while stopped (such as
635 Therefore, the tracer must be prepared to handle an
637 error on any ptrace operation.
638 Unfortunately, the same error is returned if the tracee
639 exists but is not ptrace-stopped
640 (for commands which require a stopped tracee),
641 or if it is not traced by the process which issued the ptrace call.
642 The tracer needs to keep track of the stopped/running state of the tracee,
645 as "tracee died unexpectedly" only if it knows that the tracee has
646 been observed to enter ptrace-stop.
647 Note that there is no guarantee that
649 will reliably report the tracee's death status if a
650 ptrace operation returned
653 may return 0 instead.
654 In other words, the tracee may be "not yet fully dead",
655 but already refusing ptrace requests.
657 The tracer can't assume that the tracee
659 ends its life by reporting
662 .IR WIFSIGNALED(status) ;
663 there are cases where this does not occur.
664 For example, if a thread other than thread group leader does an
667 its PID will never be seen again,
668 and any subsequent ptrace stops will be reported under
669 the thread group leader's PID.
671 A tracee can be in two states: running or stopped.
673 There are many kinds of states when the tracee is stopped, and in ptrace
674 discussions they are often conflated.
675 Therefore, it is important to use precise terms.
677 In this manual page, any stopped state in which the tracee is ready
678 to accept ptrace commands from the tracer is called
681 be further subdivided into
682 .IR signal-delivery-stop ,
686 These stopped states are described in detail below.
688 When the running tracee enters ptrace-stop, it notifies its tracer using
690 (or one of the other "wait" system calls).
691 Most of this manual page assumes that the tracer waits with:
693 pid = waitpid(pid_or_minus_1, &status, __WALL);
695 Ptrace-stopped tracees are reported as returns with
698 .I WIFSTOPPED(status)
701 .\" Do we require __WALL usage, or will just using 0 be ok? (With 0,
702 .\" I am not 100% sure there aren't ugly corner cases.) Are the
703 .\" rules different if user wants to use waitid? Will waitid require
709 flag does not include the
713 flags, but implies their functionality.
719 is not recommended: the "continued" state is per-process and
720 consuming it can confuse the real parent of the tracee.
726 to return 0 ("no wait results available yet")
727 even if the tracer knows there should be a notification.
731 kill(tracee, SIGKILL);
732 waitpid(tracee, &status, __WALL | WNOHANG);
735 .\" waitid usage? WNOWAIT?
736 .\" describe how wait notifications queue (or not queue)
738 The following kinds of ptrace-stops exist: signal-delivery-stops,
741 stops, syscall-stops.
742 They all are reported by
745 .I WIFSTOPPED(status)
747 They may be differentiated by examining the value
749 and if there is ambiguity in that value, by querying
750 .BR PTRACE_GETSIGINFO .
753 macro can't be used to perform this examination,
754 because it returns the value
755 .IR "(status\>>8)\ &\ 0xff" .)
756 .SS Signal-delivery-stop
757 When a (possibly multithreaded) process receives any signal except
759 the kernel selects an arbitrary thread which handles the signal.
760 (If the signal is generated with
762 the target thread can be explicitly selected by the caller.)
763 If the selected thread is traced, it enters signal-delivery-stop.
764 At this point, the signal is not yet delivered to the process,
765 and can be suppressed by the tracer.
766 If the tracer doesn't suppress the signal,
767 it passes the signal to the tracee in the next ptrace restart request.
768 This second step of signal delivery is called
769 .I "signal injection"
771 Note that if the signal is blocked,
772 signal-delivery-stop doesn't happen until the signal is unblocked,
773 with the usual exception that
777 Signal-delivery-stop is observed by the tracer as
780 .I WIFSTOPPED(status)
781 true, with the signal returned by
782 .IR WSTOPSIG(status) .
785 this may be a different kind of ptrace-stop;
786 see the "Syscall-stops" and "execve" sections below for details.
789 returns a stopping signal, this may be a group-stop; see below.
790 .SS Signal injection and suppression
791 After signal-delivery-stop is observed by the tracer,
792 the tracer should restart the tracee with the call
794 ptrace(PTRACE_restart, pid, 0, sig)
798 is one of the restarting ptrace requests.
801 is 0, then a signal is not delivered.
802 Otherwise, the signal
805 This operation is called
806 .I "signal injection"
807 in this manual page, to distinguish it from signal-delivery-stop.
811 value may be different from the
813 value: the tracer can cause a different signal to be injected.
815 Note that a suppressed signal still causes system calls to return
817 In this case system calls will be restarted: the tracer will
818 observe the tracee to reexecute the interrupted system call (or
819 .BR restart_syscall (2)
820 system call for a few syscalls which use a different mechanism
821 for restarting) if the tracer uses
823 Even system calls (such as
825 which are not restartable after signal are restarted after
826 signal is suppressed;
827 however, kernel bugs exist which cause some syscalls to fail with
829 even though no observable signal is injected to the tracee.
831 Restarting ptrace commands issued in ptrace-stops other than
832 signal-delivery-stop are not guaranteed to inject a signal, even if
835 No error is reported; a nonzero
837 may simply be ignored.
838 Ptrace users should not try to "create a new signal" this way: use
842 The fact that signal injection requests may be ignored
843 when restarting the tracee after
844 ptrace stops that are not signal-delivery-stops
845 is a cause of confusion among ptrace users.
846 One typical scenario is that the tracer observes group-stop,
847 mistakes it for signal-delivery-stop, restarts the tracee with
849 ptrace(PTRACE_rest, pid, 0, stopsig)
851 with the intention of injecting
855 gets ignored and the tracee continues to run.
859 signal has a side effect of waking up (all threads of)
860 a group-stopped process.
861 This side effect happens before signal-delivery-stop.
862 The tracer can't suppress this side effect (it can
863 only suppress signal injection, which only causes the
865 handler to not be executed in the tracee, if such a handler is installed).
866 In fact, waking up from group-stop may be followed by
867 signal-delivery-stop for signal(s)
870 if they were pending when
875 may be not the first signal observed by the tracee after it was sent.
877 Stopping signals cause (all threads of) a process to enter group-stop.
878 This side effect happens after signal injection, and therefore can be
879 suppressed by the tracer.
881 In Linux 2.4 and earlier, the
883 signal can't be injected.
884 .\" In the Linux 2.4 sources, in arch/i386/kernel/signal.c::do_signal(),
887 .\" /* The debugger continued. Ignore SIGSTOP. */
888 .\" if (signr == SIGSTOP)
892 can be used to retrieve a
894 structure which corresponds to the delivered signal.
896 may be used to modify it.
899 has been used to alter
905 parameter in the restarting command must match,
906 otherwise the result is undefined.
908 When a (possibly multithreaded) process receives a stopping signal,
910 If some threads are traced, they enter a group-stop.
911 Note that the stopping signal will first cause signal-delivery-stop
912 (on one tracee only), and only after it is injected by the tracer
913 (or after it was dispatched to a thread which isn't traced),
914 will group-stop be initiated on
916 tracees within the multithreaded process.
917 As usual, every tracee reports its group-stop separately
918 to the corresponding tracer.
920 Group-stop is observed by the tracer as
923 .I WIFSTOPPED(status)
924 true, with the stopping signal available via
925 .IR WSTOPSIG(status) .
926 The same result is returned by some other classes of ptrace-stops,
927 therefore the recommended practice is to perform the call
929 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
931 The call can be avoided if the signal is not
937 only these four signals are stopping signals.
938 If the tracer sees something else, it can't be a group-stop.
939 Otherwise, the tracer needs to call
940 .BR PTRACE_GETSIGINFO .
945 then it is definitely a group-stop.
946 (Other failure codes are possible, such as
948 ("no such process") if a
953 after the tracer sees the tracee ptrace-stop and until it
954 restarts or kills it, the tracee will not run,
955 and will not send notifications (except
957 death) to the tracer, even if the tracer enters into another
961 The kernel behavior described in the previous paragraph
962 causes a problem with transparent handling of stopping signals.
963 If the tracer restarts the tracee after group-stop,
965 is effectively ignored\(emthe tracee doesn't remain stopped, it runs.
966 If the tracer doesn't restart the tracee before entering into the next
970 signals will not be reported to the tracer;
973 signals to have no effect on the tracee.
974 .SS PTRACE_EVENT stops
977 options, the tracee will enter ptrace-stops called
982 stops are observed by the tracer as
985 .IR WIFSTOPPED(status) ,
990 An additional bit is set in the higher byte of the status word:
995 (SIGTRAP | PTRACE_EVENT_foo << 8).
997 The following events exist:
999 .B PTRACE_EVENT_VFORK
1000 Stop before return from
1007 When the tracee is continued after this stop, it will wait for child to
1008 exit/exec before continuing its execution
1009 (in other words, the usual behavior on
1012 .B PTRACE_EVENT_FORK
1013 Stop before return from
1017 with the exit signal set to
1020 .B PTRACE_EVENT_CLONE
1021 Stop before return from
1024 .B PTRACE_EVENT_VFORK_DONE
1025 Stop before return from
1032 but after the child unblocked this tracee by exiting or execing.
1034 For all four stops described above,
1035 the stop occurs in the parent (i.e., the tracee),
1036 not in the newly created thread.
1037 .BR PTRACE_GETEVENTMSG
1038 can be used to retrieve the new thread's ID.
1040 .B PTRACE_EVENT_EXEC
1041 Stop before return from
1044 .BR PTRACE_GETEVENTMSG
1045 returns the former thread ID.
1047 .B PTRACE_EVENT_EXIT
1048 Stop before exit (including death from
1049 .BR exit_group (2)),
1050 signal death, or exit caused by
1052 in a multithreaded process.
1053 .B PTRACE_GETEVENTMSG
1054 returns the exit status.
1055 Registers can be examined
1056 (unlike when "real" exit happens).
1057 The tracee is still alive; it needs to be
1060 .BR PTRACE_DETACH ed
1063 .B PTRACE_GETSIGINFO
1073 .IR "(event<<8)\ |\ SIGTRAP" .
1075 If the tracee was restarted by
1076 .BR PTRACE_SYSCALL ,
1078 syscall-enter-stop just prior to entering any system call.
1079 If the tracer restarts the tracee with
1080 .BR PTRACE_SYSCALL ,
1081 the tracee enters syscall-exit-stop when the system call is finished,
1082 or if it is interrupted by a signal.
1083 (That is, signal-delivery-stop never happens between syscall-enter-stop
1084 and syscall-exit-stop; it happens
1088 Other possibilities are that the tracee may stop in a
1090 stop, exit (if it entered
1093 .BR exit_group (2)),
1096 or die silently (if it is a thread group leader, the
1098 happened in another thread,
1099 and that thread is not traced by the same tracer;
1100 this situation is discussed later).
1102 Syscall-enter-stop and syscall-exit-stop are observed by the tracer as
1105 .I WIFSTOPPED(status)
1111 .B PTRACE_O_TRACESYSGOOD
1112 option was set by the tracer, then
1115 .IR "(SIGTRAP\ |\ 0x80)" .
1117 Syscall-stops can be distinguished from signal-delivery-stop with
1120 .BR PTRACE_GETSIGINFO
1121 for the following cases:
1125 was delivered as a result of a userspace action,
1126 for example, a system call
1131 expiration of a POSIX timer,
1132 change of state on a POSIX message queue,
1133 or completion of an asynchronous I/O request.
1135 .IR si_code " == SI_KERNEL (0x80)"
1137 was sent by the kernel.
1139 .IR si_code " == SIGTRAP or " si_code " == (SIGTRAP|0x80)"
1140 This is a syscall-stop.
1142 However, syscall-stops happen very often (twice per system call),
1144 .B PTRACE_GETSIGINFO
1145 for every syscall-stop may be somewhat expensive.
1147 Some architectures allow the cases to be distinguished
1148 by examining registers.
1149 For example, on x86,
1153 in syscall-enter-stop.
1156 (like any other signal) always happens
1161 almost never contains
1165 looks like "syscall-stop which is not syscall-enter-stop";
1166 in other words, it looks like a
1167 "stray syscall-exit-stop" and can be detected this way.
1168 But such detection is fragile and is best avoided.
1171 .B PTRACE_O_TRACESYSGOOD
1172 option is the recommended method to distinguish syscall-stops
1173 from other kinds of ptrace-stops,
1174 since it is reliable and does not incur a performance penalty.
1176 Syscall-enter-stop and syscall-exit-stop are
1177 indistinguishable from each other by the tracer.
1178 The tracer needs to keep track of the sequence of
1179 ptrace-stops in order to not misinterpret syscall-enter-stop as
1180 syscall-exit-stop or vice versa.
1181 The rule is that syscall-enter-stop is
1182 always followed by syscall-exit-stop,
1184 stop or the tracee's death;
1185 no other kinds of ptrace-stop can occur in between.
1187 If after syscall-enter-stop,
1188 the tracer uses a restarting command other than
1189 .BR PTRACE_SYSCALL ,
1190 syscall-exit-stop is not generated.
1192 .B PTRACE_GETSIGINFO
1193 on syscall-stops returns
1202 .IR (SIGTRAP|0x80) .
1203 .SS PTRACE_SINGLESTEP, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP stops
1204 [Details of these kinds of stops are yet to be documented.]
1207 .\" document stops occurring with PTRACE_SINGLESTEP, PTRACE_SYSEMU,
1208 .\" PTRACE_SYSEMU_SINGLESTEP
1209 .SS Informational and restarting ptrace commands
1210 Most ptrace commands (all except
1212 .BR PTRACE_TRACEME ,
1215 require the tracee to be in a ptrace-stop, otherwise they fail with
1218 When the tracee is in ptrace-stop,
1219 the tracer can read and write data to
1220 the tracee using informational commands.
1221 These commands leave the tracee in ptrace-stopped state:
1224 ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
1225 ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
1226 ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
1227 ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
1228 ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
1229 ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
1230 ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
1231 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1234 Note that some errors are not reported.
1235 For example, setting signal information
1237 may have no effect in some ptrace-stops, yet the call may succeed
1238 (return 0 and not set
1241 .B PTRACE_GETEVENTMSG
1242 may succeed and return some random value if current ptrace-stop
1243 is not documented as returning a meaningful event message.
1247 ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
1250 The tracee's current flags are replaced.
1251 Flags are inherited by new tracees created and "auto-attached" via active
1252 .BR PTRACE_O_TRACEFORK ,
1253 .BR PTRACE_O_TRACEVFORK ,
1255 .BR PTRACE_O_TRACECLONE
1258 Another group of commands makes the ptrace-stopped tracee run.
1261 ptrace(cmd, pid, 0, sig);
1268 .BR PTRACE_SYSCALL ,
1269 .BR PTRACE_SINGLESTEP ,
1272 .BR PTRACE_SYSEMU_SINGLESTEP .
1273 If the tracee is in signal-delivery-stop,
1275 is the signal to be injected (if it is nonzero).
1279 (When restarting a tracee from a ptrace-stop other than signal-delivery-stop,
1280 recommended practice is to always pass 0 in
1282 .SS Attaching and detaching
1283 A thread can be attached to the tracer using the call
1285 ptrace(PTRACE_ATTACH, pid, 0, 0);
1290 If the tracer wants this
1292 to have no effect, it needs to suppress it.
1293 Note that if other signals are concurrently sent to
1294 this thread during attach,
1295 the tracer may see the tracee enter signal-delivery-stop
1296 with other signal(s) first!
1297 The usual practice is to reinject these signals until
1299 is seen, then suppress
1302 The design bug here is that a ptrace attach and a concurrently delivered
1304 may race and the concurrent
1308 .\" FIXME: Describe how to attach to a thread which is already
1311 Since attaching sends
1313 and the tracer usually suppresses it, this may cause a stray
1315 return from the currently executing system call in the tracee,
1316 as described in the "Signal injection and suppression" section.
1320 ptrace(PTRACE_TRACEME, 0, 0, 0);
1322 turns the calling thread into a tracee.
1323 The thread continues to run (doesn't enter ptrace-stop).
1324 A common practice is to follow the
1330 and allow the parent (which is our tracer now) to observe our
1331 signal-delivery-stop.
1334 .BR PTRACE_O_TRACEFORK ,
1335 .BR PTRACE_O_TRACEVFORK ,
1337 .BR PTRACE_O_TRACECLONE
1338 options are in effect, then children created by, respectively,
1348 with the exit signal set to
1352 are automatically attached to the same tracer which traced their parent.
1354 is delivered to the children, causing them to enter
1355 signal-delivery-stop after they exit the system call which created them.
1357 Detaching of the tracee is performed by:
1359 ptrace(PTRACE_DETACH, pid, 0, sig);
1362 is a restarting operation;
1363 therefore it requires the tracee to be in ptrace-stop.
1364 If the tracee is in signal-delivery-stop, a signal can be injected.
1367 parameter may be silently ignored.
1369 If the tracee is running when the tracer wants to detach it,
1370 the usual solution is to send
1374 to make sure it goes to the correct thread),
1375 wait for the tracee to stop in signal-delivery-stop for
1377 and then detach it (suppressing
1380 A design bug is that this can race with concurrent
1382 Another complication is that the tracee may enter other ptrace-stops
1383 and needs to be restarted and waited for again, until
1386 Yet another complication is to be sure that
1387 the tracee is not already ptrace-stopped,
1388 because no signal delivery happens while it is\(emnot even
1390 .\" FIXME: Describe how to detach from a group-stopped tracee so that it
1391 .\" doesn't run, but continues to wait for SIGCONT.
1393 If the tracer dies, all tracees are automatically detached and restarted,
1394 unless they were in group-stop.
1395 Handling of restart from group-stop is currently buggy,
1396 but the "as planned" behavior is to leave tracee stopped and waiting for
1398 If the tracee is restarted from signal-delivery-stop,
1399 the pending signal is injected.
1400 .SS execve(2) under ptrace
1401 .\" clone(2) THREAD_CLONE says:
1402 .\" If any of the threads in a thread group performs an execve(2),
1403 .\" then all threads other than the thread group leader are terminated,
1404 .\" and the new program is executed in the thread group leader.
1406 When one thread in a multithreaded process calls
1408 the kernel destroys all other threads in the process,
1409 .\" In kernel 3.1 sources, see fs/exec.c::de_thread()
1410 and resets the thread ID of the execing thread to the
1411 thread group ID (process ID).
1412 (Or, to put things another way, when a multithreaded process does an
1414 at completion of the call, it appears as though the
1416 occurred in the thread group leader, regardless of which thread did the
1418 This resetting of the thread ID looks very confusing to tracers:
1420 All other threads stop in
1421 .B PTRACE_EVENT_EXIT
1423 .BR PTRACE_O_TRACEEXIT
1424 option was turned on.
1425 Then all other threads except the thread group leader report
1426 death as if they exited via
1430 The execing tracee changes its thread ID while it is in the
1432 (Remember, under ptrace, the "pid" returned from
1434 or fed into ptrace calls, is the tracee's thread ID.)
1435 That is, the tracee's thread ID is reset to be the same as its process ID,
1436 which is the same as the thread group leader's thread ID.
1439 .B PTRACE_EVENT_EXEC
1440 stop happens, if the
1441 .BR PTRACE_O_TRACEEXEC
1442 option was turned on.
1444 If the thread group leader has reported its
1445 .B PTRACE_EVENT_EXIT
1447 it appears to the tracer that
1448 the dead thread leader "reappears from nowhere".
1449 (Note: the thread group leader does not report death via
1450 .I WIFEXITED(status)
1451 until there is at least one other live thread.
1452 This eliminates the possibility that the tracer will see
1453 it dying and then reappearing.)
1454 If the thread group leader was still alive,
1455 for the tracer this may look as if thread group leader
1456 returns from a different system call than it entered,
1457 or even "returned from a system call even though
1458 it was not in any system call".
1459 If the thread group leader was not traced
1460 (or was traced by a different tracer), then during
1462 it will appear as if it has become a tracee of
1463 the tracer of the execing tracee.
1465 All of the above effects are the artifacts of
1466 the thread ID change in the tracee.
1469 .B PTRACE_O_TRACEEXEC
1470 option is the recommended tool for dealing with this situation.
1472 .BR PTRACE_EVENT_EXEC
1477 In this stop, the tracer can use
1478 .B PTRACE_GETEVENTMSG
1479 to retrieve the tracee's former thread ID.
1480 (This feature was introduced in Linux 3.0).
1482 .B PTRACE_O_TRACEEXEC
1483 option disables legacy
1488 When the tracer receives
1489 .B PTRACE_EVENT_EXEC
1491 it is guaranteed that except this tracee and the thread group leader,
1492 no other threads from the process are alive.
1495 .B PTRACE_EVENT_EXEC
1497 the tracer should clean up all its internal
1498 data structures describing the threads of this process,
1499 and retain only one data structure\(emone which
1500 describes the single still running tracee, with
1502 thread ID == thread group ID == process ID.
1504 Example: two threads call
1509 *** we get syscall-enter-stop in thread 1: **
1510 PID1 execve("/bin/foo", "foo" <unfinished ...>
1511 *** we issue PTRACE_SYSCALL for thread 1 **
1512 *** we get syscall-enter-stop in thread 2: **
1513 PID2 execve("/bin/bar", "bar" <unfinished ...>
1514 *** we issue PTRACE_SYSCALL for thread 2 **
1515 *** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
1516 *** we get syscall-exit-stop for PID0: **
1517 PID0 <... execve resumed> ) = 0
1521 .B PTRACE_O_TRACEEXEC
1524 in effect for the execing tracee, the kernel delivers an extra
1529 This is an ordinary signal (similar to one which can be
1532 not a special kind of ptrace-stop.
1534 .B PTRACE_GETSIGINFO
1535 for this signal returns
1539 This signal may be blocked by signal mask,
1540 and thus may be delivered (much) later.
1542 Usually, the tracer (for example,
1544 would not want to show this extra post-execve
1546 signal to the user, and would suppress its delivery to the tracee (if
1550 it is a killing signal).
1551 However, determining
1554 to suppress is not easy.
1556 .B PTRACE_O_TRACEEXEC
1557 option and thus suppressing this extra
1559 is the recommended approach.
1561 The ptrace API (ab)uses the standard UNIX parent/child signaling over
1563 This used to cause the real parent of the process to stop receiving
1566 notifications when the child process is traced by some other process.
1568 Many of these bugs have been fixed, but as of Linux 2.6.38 several still
1569 exist; see BUGS below.
1571 As of Linux 2.6.38, the following is believed to work correctly:
1573 exit/death by signal is reported first to the tracer, then,
1574 when the tracer consumes the
1576 result, to the real parent (to the real parent only when the
1577 whole multithreaded process exits).
1578 If the tracer and the real parent are the same process,
1579 the report is sent only once.
1583 requests return the requested data,
1584 while other requests return zero.
1585 On error, all requests return \-1, and
1587 is set appropriately.
1588 Since the value returned by a successful
1590 request may be \-1, the caller must clear
1592 before the call, and then check it afterward
1593 to determine whether or not an error occurred.
1597 (i386 only) There was an error with allocating or freeing a debug register.
1600 There was an attempt to read from or write to an invalid area in
1601 the tracer's or the tracee's memory,
1602 probably because the area wasn't mapped or accessible.
1603 Unfortunately, under Linux, different variations of this fault
1608 more or less arbitrarily.
1611 An attempt was made to set an invalid option.
1615 is invalid, or an attempt was made to read from or
1616 write to an invalid area in the tracer's or the tracee's memory,
1617 or there was a word-alignment violation,
1618 or an invalid signal was specified during a restart request.
1621 The specified process cannot be traced.
1622 This could be because the
1623 tracer has insufficient privileges (the required capability is
1624 .BR CAP_SYS_PTRACE );
1625 unprivileged processes cannot trace processes that they
1626 cannot send signals to or those running
1627 set-user-ID/set-group-ID programs, for obvious reasons.
1628 Alternatively, the process may already be being traced,
1629 or (on kernels before 2.6.26) be
1634 The specified process does not exist, or is not currently being traced
1635 by the caller, or is not stopped
1636 (for requests that require a stopped tracee).
1640 Although arguments to
1642 are interpreted according to the prototype given,
1643 glibc currently declares
1645 as a variadic function with only the
1648 This means that unneeded trailing arguments may be omitted,
1649 though doing so makes use of undocumented
1653 In Linux kernels before 2.6.26,
1654 .\" See commit 00cd5c37afd5f431ac186dd131705048c0a11fdb
1656 the process with PID 1, may not be traced.
1658 The layout of the contents of memory and the USER area are
1659 quite operating-system- and architecture-specific.
1660 The offset supplied, and the data returned,
1661 might not entirely match with the definition of
1663 .\" See http://lkml.org/lkml/2008/5/8/375
1665 The size of a "word" is determined by the operating-system variant
1666 (e.g., for 32-bit Linux it is 32 bits, etc.).
1668 This page documents the way the
1670 call works currently in Linux.
1671 Its behavior differs noticeably on other flavors of UNIX.
1674 is highly specific to the operating system and architecture.
1676 On hosts with 2.6 kernel headers,
1677 .B PTRACE_SETOPTIONS
1678 is declared with a different value than the one for 2.4.
1679 This leads to applications compiled with 2.6 kernel
1680 headers failing when run on 2.4 kernels.
1681 This can be worked around by redefining
1682 .B PTRACE_SETOPTIONS
1684 .BR PTRACE_OLDSETOPTIONS ,
1687 Group-stop notifications are sent to the tracer, but not to real parent.
1688 Last confirmed on 2.6.38.6.
1690 If a thread group leader is traced and exits by calling
1692 .\" Note from Denys Vlasenko:
1693 .\" Here "exits" means any kind of death - _exit, exit_group,
1694 .\" signal death. Signal death and exit_group cases are trivial,
1695 .\" though: since signal death and exit_group kill all other threads
1696 .\" too, "until all other threads exit" thing happens rather soon
1697 .\" in these cases. Therefore, only _exit presents observably
1698 .\" puzzling behavior to ptrace users: thread leader _exit's,
1699 .\" but WIFEXITED isn't reported! We are trying to explain here
1702 .B PTRACE_EVENT_EXIT
1703 stop will happen for it (if requested), but the subsequent
1705 notification will not be delivered until all other threads exit.
1706 As explained above, if one of other threads calls
1708 the death of the thread group leader will
1711 If the execed thread is not traced by this tracer,
1712 the tracer will never know that
1715 One possible workaround is to
1717 the thread group leader instead of restarting it in this case.
1718 Last confirmed on 2.6.38.6.
1719 .\" FIXME: ^^^ need to test/verify this scenario
1723 signal may still cause a
1724 .B PTRACE_EVENT_EXIT
1725 stop before actual signal death.
1726 This may be changed in the future;
1728 is meant to always immediately kill tasks even under ptrace.
1729 Last confirmed on 2.6.38.6.
1731 Some system calls return with
1733 if a signal was sent to a tracee, but delivery was suppressed by the tracer.
1734 (This is very typical operation: it is usually
1735 done by debuggers on every attach, in order to not introduce
1738 As of Linux 3.2.9, the following system calls are affected
1739 (this list is likely incomplete):
1758 .BR capabilities (7),