xref: /aosp_15_r20/external/strace/README-linux-ptrace (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1*cf84ac9aSAndroid Build Coastguard WorkerThis document describes Linux ptrace implementation in Linux kernels
2*cf84ac9aSAndroid Build Coastguard Workerversion 3.0.0. (Update this notice if you update the document
3*cf84ac9aSAndroid Build Coastguard Workerto reflect newer kernels).
4*cf84ac9aSAndroid Build Coastguard Worker
5*cf84ac9aSAndroid Build Coastguard Worker
6*cf84ac9aSAndroid Build Coastguard Worker		Ptrace userspace API.
7*cf84ac9aSAndroid Build Coastguard Worker
8*cf84ac9aSAndroid Build Coastguard WorkerPtrace API (ab)uses standard Unix parent/child signaling over waitpid.
9*cf84ac9aSAndroid Build Coastguard WorkerAn unfortunate effect of it is that resulting API is complex and has
10*cf84ac9aSAndroid Build Coastguard Workersubtle quirks. This document aims to describe these quirks.
11*cf84ac9aSAndroid Build Coastguard Worker
12*cf84ac9aSAndroid Build Coastguard WorkerDebugged processes (tracees) first need to be attached to the debugging
13*cf84ac9aSAndroid Build Coastguard Workerprocess (tracer). Attachment and subsequent commands are per-thread: in
14*cf84ac9aSAndroid Build Coastguard Workermulti-threaded process, every thread can be individually attached to a
15*cf84ac9aSAndroid Build Coastguard Worker(potentially different) tracer, or left not attached and thus not
16*cf84ac9aSAndroid Build Coastguard Workerdebugged. Therefore, "tracee" always means "(one) thread", never "a
17*cf84ac9aSAndroid Build Coastguard Worker(possibly multi-threaded) process". Ptrace commands are always sent to
18*cf84ac9aSAndroid Build Coastguard Workera specific tracee using ptrace(PTRACE_foo, pid, ...), where pid is a
19*cf84ac9aSAndroid Build Coastguard WorkerTID of the corresponding Linux thread.
20*cf84ac9aSAndroid Build Coastguard Worker
21*cf84ac9aSAndroid Build Coastguard WorkerAfter attachment, each tracee can be in two states: running or stopped.
22*cf84ac9aSAndroid Build Coastguard Worker
23*cf84ac9aSAndroid Build Coastguard WorkerThere are many kinds of states when tracee is stopped, and in ptrace
24*cf84ac9aSAndroid Build Coastguard Workerdiscussions they are often conflated. Therefore, it is important to use
25*cf84ac9aSAndroid Build Coastguard Workerprecise terms.
26*cf84ac9aSAndroid Build Coastguard Worker
27*cf84ac9aSAndroid Build Coastguard WorkerIn this document, any stopped state in which tracee is ready to accept
28*cf84ac9aSAndroid Build Coastguard Workerptrace commands from the tracer is called ptrace-stop. Ptrace-stops can
29*cf84ac9aSAndroid Build Coastguard Workerbe further subdivided into signal-delivery-stop, group-stop,
30*cf84ac9aSAndroid Build Coastguard Workersyscall-stop and so on. They are described in detail later.
31*cf84ac9aSAndroid Build Coastguard Worker
32*cf84ac9aSAndroid Build Coastguard Worker
33*cf84ac9aSAndroid Build Coastguard Worker	1.x Death under ptrace.
34*cf84ac9aSAndroid Build Coastguard Worker
35*cf84ac9aSAndroid Build Coastguard WorkerWhen a (possibly multi-threaded) process receives a killing signal (a
36*cf84ac9aSAndroid Build Coastguard Workersignal set to SIG_DFL and whose default action is to kill the process),
37*cf84ac9aSAndroid Build Coastguard Workerall threads exit. Tracees report their death to the tracer(s). This is
38*cf84ac9aSAndroid Build Coastguard Workernot a ptrace-stop (because tracer can't query tracee status such as
39*cf84ac9aSAndroid Build Coastguard Workerregister contents, cannot restart tracee etc) but the notification
40*cf84ac9aSAndroid Build Coastguard Workerabout this event is delivered through waitpid API similarly to
41*cf84ac9aSAndroid Build Coastguard Workerptrace-stop.
42*cf84ac9aSAndroid Build Coastguard Worker
43*cf84ac9aSAndroid Build Coastguard WorkerNote that killing signal will first cause signal-delivery-stop (on one
44*cf84ac9aSAndroid Build Coastguard Workertracee only), and only after it is injected by tracer (or after it was
45*cf84ac9aSAndroid Build Coastguard Workerdispatched to a thread which isn't traced), death from signal will
46*cf84ac9aSAndroid Build Coastguard Workerhappen on ALL tracees within multi-threaded process.
47*cf84ac9aSAndroid Build Coastguard Worker
48*cf84ac9aSAndroid Build Coastguard WorkerSIGKILL operates similarly, with exceptions. No signal-delivery-stop is
49*cf84ac9aSAndroid Build Coastguard Workergenerated for SIGKILL and therefore tracer can't suppress it. SIGKILL
50*cf84ac9aSAndroid Build Coastguard Workerkills even within syscalls (syscall-exit-stop is not generated prior to
51*cf84ac9aSAndroid Build Coastguard Workerdeath by SIGKILL). The net effect is that SIGKILL always kills the
52*cf84ac9aSAndroid Build Coastguard Workerprocess (all its threads), even if some threads of the process are
53*cf84ac9aSAndroid Build Coastguard Workerptraced.
54*cf84ac9aSAndroid Build Coastguard Worker
55*cf84ac9aSAndroid Build Coastguard WorkerTracer can kill a tracee with ptrace(PTRACE_KILL, pid, 0, 0). This
56*cf84ac9aSAndroid Build Coastguard Workeroperation is deprecated, use kill/tgkill(SIGKILL) instead.
57*cf84ac9aSAndroid Build Coastguard Worker
58*cf84ac9aSAndroid Build Coastguard Worker^^^ Oleg prefers to deprecate it instead of describing (and needing to
59*cf84ac9aSAndroid Build Coastguard Workersupport) PTRACE_KILL's quirks.
60*cf84ac9aSAndroid Build Coastguard Worker
61*cf84ac9aSAndroid Build Coastguard WorkerWhen tracee executes exit syscall, it reports its death to its tracer.
62*cf84ac9aSAndroid Build Coastguard WorkerOther threads are not affected.
63*cf84ac9aSAndroid Build Coastguard Worker
64*cf84ac9aSAndroid Build Coastguard WorkerWhen any thread executes exit_group syscall, every tracee in its thread
65*cf84ac9aSAndroid Build Coastguard Workergroup reports its death to its tracer.
66*cf84ac9aSAndroid Build Coastguard Worker
67*cf84ac9aSAndroid Build Coastguard WorkerIf PTRACE_O_TRACEEXIT option is on, PTRACE_EVENT_EXIT will happen
68*cf84ac9aSAndroid Build Coastguard Workerbefore actual death. This applies to exits on exit syscall, group_exit
69*cf84ac9aSAndroid Build Coastguard Workersyscall, signal deaths (except SIGKILL), and when threads are torn down
70*cf84ac9aSAndroid Build Coastguard Workeron execve in multi-threaded process.
71*cf84ac9aSAndroid Build Coastguard Worker
72*cf84ac9aSAndroid Build Coastguard WorkerTracer cannot assume that ptrace-stopped tracee exists. There are many
73*cf84ac9aSAndroid Build Coastguard Workerscenarios when tracee may die while stopped (such as SIGKILL).
74*cf84ac9aSAndroid Build Coastguard WorkerTherefore, tracer must always be prepared to handle ESRCH error on any
75*cf84ac9aSAndroid Build Coastguard Workerptrace operation. Unfortunately, the same error is returned if tracee
76*cf84ac9aSAndroid Build Coastguard Workerexists but is not ptrace-stopped (for commands which require stopped
77*cf84ac9aSAndroid Build Coastguard Workertracee), or if it is not traced by process which issued ptrace call.
78*cf84ac9aSAndroid Build Coastguard WorkerTracer needs to keep track of stopped/running state, and interpret
79*cf84ac9aSAndroid Build Coastguard WorkerESRCH as "tracee died unexpectedly" only if it knows that tracee has
80*cf84ac9aSAndroid Build Coastguard Workerbeen observed to enter ptrace-stop. Note that there is no guarantee
81*cf84ac9aSAndroid Build Coastguard Workerthat waitpid(WNOHANG) will reliably report tracee's death status if
82*cf84ac9aSAndroid Build Coastguard Workerptrace operation returned ESRCH. waitpid(WNOHANG) may return 0 instead.
83*cf84ac9aSAndroid Build Coastguard WorkerIOW: tracee may be "not yet fully dead" but already refusing ptrace ops.
84*cf84ac9aSAndroid Build Coastguard Worker
85*cf84ac9aSAndroid Build Coastguard WorkerTracer can not assume that tracee ALWAYS ends its life by reporting
86*cf84ac9aSAndroid Build Coastguard WorkerWIFEXITED(status) or WIFSIGNALED(status).
87*cf84ac9aSAndroid Build Coastguard Worker
88*cf84ac9aSAndroid Build Coastguard Worker??? or can it? Do we include such a promise into ptrace API?
89*cf84ac9aSAndroid Build Coastguard Worker
90*cf84ac9aSAndroid Build Coastguard Worker
91*cf84ac9aSAndroid Build Coastguard Worker	1.x Stopped states.
92*cf84ac9aSAndroid Build Coastguard Worker
93*cf84ac9aSAndroid Build Coastguard WorkerWhen running tracee enters ptrace-stop, it notifies its tracer using
94*cf84ac9aSAndroid Build Coastguard Workerwaitpid API. Tracer should use waitpid family of syscalls to wait for
95*cf84ac9aSAndroid Build Coastguard Workertracee to stop. Most of this document assumes that tracer waits with:
96*cf84ac9aSAndroid Build Coastguard Worker
97*cf84ac9aSAndroid Build Coastguard Worker	pid = waitpid(pid_or_minus_1, &status, __WALL);
98*cf84ac9aSAndroid Build Coastguard Worker
99*cf84ac9aSAndroid Build Coastguard WorkerPtrace-stopped tracees are reported as returns with pid > 0 and
100*cf84ac9aSAndroid Build Coastguard WorkerWIFSTOPPED(status) == true.
101*cf84ac9aSAndroid Build Coastguard Worker
102*cf84ac9aSAndroid Build Coastguard Worker??? Do we require __WALL usage, or will just using 0 be ok? Are the
103*cf84ac9aSAndroid Build Coastguard Workerrules different if user wants to use waitid? Will waitid require
104*cf84ac9aSAndroid Build Coastguard WorkerWEXITED?
105*cf84ac9aSAndroid Build Coastguard Worker
106*cf84ac9aSAndroid Build Coastguard Worker__WALL value does not include WSTOPPED and WEXITED bits, but implies
107*cf84ac9aSAndroid Build Coastguard Workertheir functionality.
108*cf84ac9aSAndroid Build Coastguard Worker
109*cf84ac9aSAndroid Build Coastguard WorkerSetting of WCONTINUED bit in waitpid flags is not recommended: the
110*cf84ac9aSAndroid Build Coastguard Workercontinued state is per-process and consuming it can confuse real parent
111*cf84ac9aSAndroid Build Coastguard Workerof the tracee.
112*cf84ac9aSAndroid Build Coastguard Worker
113*cf84ac9aSAndroid Build Coastguard WorkerUse of WNOHANG bit in waitpid flags may cause waitpid return 0 ("no
114*cf84ac9aSAndroid Build Coastguard Workerwait results available yet") even if tracer knows there should be a
115*cf84ac9aSAndroid Build Coastguard Workernotification. Example: kill(tracee, SIGKILL); waitpid(tracee, &status,
116*cf84ac9aSAndroid Build Coastguard Worker__WALL | WNOHANG);
117*cf84ac9aSAndroid Build Coastguard Worker
118*cf84ac9aSAndroid Build Coastguard Worker??? waitid usage? WNOWAIT?
119*cf84ac9aSAndroid Build Coastguard Worker
120*cf84ac9aSAndroid Build Coastguard Worker??? describe how wait notifications queue (or not queue)
121*cf84ac9aSAndroid Build Coastguard Worker
122*cf84ac9aSAndroid Build Coastguard WorkerThe following kinds of ptrace-stops exist: signal-delivery-stops,
123*cf84ac9aSAndroid Build Coastguard Workergroup-stop, PTRACE_EVENT stops, syscall-stops [, SINGLESTEP, SYSEMU,
124*cf84ac9aSAndroid Build Coastguard WorkerSYSEMU_SINGLESTEP]. They all are reported as waitpid result with
125*cf84ac9aSAndroid Build Coastguard WorkerWIFSTOPPED(status) == true. They may be differentiated by checking
126*cf84ac9aSAndroid Build Coastguard Worker(status >> 8) value, and if looking at (status >> 8) value doesn't
127*cf84ac9aSAndroid Build Coastguard Workerresolve ambiguity, by querying PTRACE_GETSIGINFO. (Note:
128*cf84ac9aSAndroid Build Coastguard WorkerWSTOPSIG(status) macro returns ((status >> 8) & 0xff) value).
129*cf84ac9aSAndroid Build Coastguard Worker
130*cf84ac9aSAndroid Build Coastguard Worker
131*cf84ac9aSAndroid Build Coastguard Worker	1.x.x Signal-delivery-stop
132*cf84ac9aSAndroid Build Coastguard Worker
133*cf84ac9aSAndroid Build Coastguard WorkerWhen (possibly multi-threaded) process receives any signal except
134*cf84ac9aSAndroid Build Coastguard WorkerSIGKILL, kernel selects a thread which handles the signal (if signal is
135*cf84ac9aSAndroid Build Coastguard Workergenerated with t[g]kill, thread selection is done by user). If selected
136*cf84ac9aSAndroid Build Coastguard Workerthread is traced, it enters signal-delivery-stop. By this point, signal
137*cf84ac9aSAndroid Build Coastguard Workeris not yet delivered to the process, and can be suppressed by tracer.
138*cf84ac9aSAndroid Build Coastguard WorkerIf tracer doesn't suppress the signal, it passes signal to tracee in
139*cf84ac9aSAndroid Build Coastguard Workerthe next ptrace request. This second step of signal delivery is called
140*cf84ac9aSAndroid Build Coastguard Worker"signal injection" in this document. Note that if signal is blocked,
141*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop doesn't happen until signal is unblocked, with the
142*cf84ac9aSAndroid Build Coastguard Workerusual exception that SIGSTOP can't be blocked.
143*cf84ac9aSAndroid Build Coastguard Worker
144*cf84ac9aSAndroid Build Coastguard WorkerSignal-delivery-stop is observed by tracer as waitpid returning with
145*cf84ac9aSAndroid Build Coastguard WorkerWIFSTOPPED(status) == true, WSTOPSIG(status) == signal. If
146*cf84ac9aSAndroid Build Coastguard WorkerWSTOPSIG(status) == SIGTRAP, this may be a different kind of
147*cf84ac9aSAndroid Build Coastguard Workerptrace-stop - see "Syscall-stops" and "execve" sections below for
148*cf84ac9aSAndroid Build Coastguard Workerdetails. If WSTOPSIG(status) == stopping signal, this may be a
149*cf84ac9aSAndroid Build Coastguard Workergroup-stop - see below.
150*cf84ac9aSAndroid Build Coastguard Worker
151*cf84ac9aSAndroid Build Coastguard Worker
152*cf84ac9aSAndroid Build Coastguard Worker	1.x.x Signal injection and suppression.
153*cf84ac9aSAndroid Build Coastguard Worker
154*cf84ac9aSAndroid Build Coastguard WorkerAfter signal-delivery-stop is observed by tracer, tracer should restart
155*cf84ac9aSAndroid Build Coastguard Workertracee with
156*cf84ac9aSAndroid Build Coastguard Worker
157*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_rest, pid, 0, sig)
158*cf84ac9aSAndroid Build Coastguard Worker
159*cf84ac9aSAndroid Build Coastguard Workercall, where PTRACE_rest is one of the restarting ptrace ops. If sig is
160*cf84ac9aSAndroid Build Coastguard Worker0, then signal is not delivered. Otherwise, signal sig is delivered.
161*cf84ac9aSAndroid Build Coastguard WorkerThis operation is called "signal injection" in this document, to
162*cf84ac9aSAndroid Build Coastguard Workerdistinguish it from signal-delivery-stop.
163*cf84ac9aSAndroid Build Coastguard Worker
164*cf84ac9aSAndroid Build Coastguard WorkerNote that sig value may be different from WSTOPSIG(status) value -
165*cf84ac9aSAndroid Build Coastguard Workertracer can cause a different signal to be injected.
166*cf84ac9aSAndroid Build Coastguard Worker
167*cf84ac9aSAndroid Build Coastguard WorkerNote that suppressed signal still causes syscalls to return
168*cf84ac9aSAndroid Build Coastguard Workerprematurely. Kernel should always restart the syscall in this case:
169*cf84ac9aSAndroid Build Coastguard Workertracer would observe a new syscall-enter-stop for the same syscall,
170*cf84ac9aSAndroid Build Coastguard Workeror, in case of syscalls returning ERESTART_RESTARTBLOCK,
171*cf84ac9aSAndroid Build Coastguard Workertracer would observe a syscall-enter-stop for restart_syscall(2)
172*cf84ac9aSAndroid Build Coastguard Workersyscall. There may still be bugs in this area which cause some syscalls
173*cf84ac9aSAndroid Build Coastguard Workerto instead return with -EINTR even though no observable signal
174*cf84ac9aSAndroid Build Coastguard Workerwas injected to the tracee.
175*cf84ac9aSAndroid Build Coastguard Worker
176*cf84ac9aSAndroid Build Coastguard WorkerThis is a cause of confusion among ptrace users. One typical scenario
177*cf84ac9aSAndroid Build Coastguard Workeris that tracer observes group-stop, mistakes it for
178*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop, restarts tracee with ptrace(PTRACE_rest, pid, 0,
179*cf84ac9aSAndroid Build Coastguard Workerstopsig) with the intention of injecting stopsig, but stopsig gets
180*cf84ac9aSAndroid Build Coastguard Workerignored and tracee continues to run.
181*cf84ac9aSAndroid Build Coastguard Worker
182*cf84ac9aSAndroid Build Coastguard WorkerSIGCONT signal has a side effect of waking up (all threads of)
183*cf84ac9aSAndroid Build Coastguard Workergroup-stopped process. This side effect happens before
184*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop. Tracer can't suppress this side-effect (it can
185*cf84ac9aSAndroid Build Coastguard Workeronly suppress signal injection, which only causes SIGCONT handler to
186*cf84ac9aSAndroid Build Coastguard Workernot be executed in the tracee, if such handler is installed). In fact,
187*cf84ac9aSAndroid Build Coastguard Workerwaking up from group-stop may be followed by signal-delivery-stop for
188*cf84ac9aSAndroid Build Coastguard Workersignal(s) *other than* SIGCONT, if they were pending when SIGCONT was
189*cf84ac9aSAndroid Build Coastguard Workerdelivered. IOW: SIGCONT may be not the first signal observed by the
190*cf84ac9aSAndroid Build Coastguard Workertracee after it was sent.
191*cf84ac9aSAndroid Build Coastguard Worker
192*cf84ac9aSAndroid Build Coastguard WorkerStopping signals cause (all threads of) process to enter group-stop.
193*cf84ac9aSAndroid Build Coastguard WorkerThis side effect happens after signal injection, and therefore can be
194*cf84ac9aSAndroid Build Coastguard Workersuppressed by tracer.
195*cf84ac9aSAndroid Build Coastguard Worker
196*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_GETSIGINFO can be used to retrieve siginfo_t structure which
197*cf84ac9aSAndroid Build Coastguard Workercorresponds to delivered signal. PTRACE_SETSIGINFO may be used to
198*cf84ac9aSAndroid Build Coastguard Workermodify it. If PTRACE_SETSIGINFO has been used to alter siginfo_t,
199*cf84ac9aSAndroid Build Coastguard Workersi_signo field and sig parameter in restarting command must match,
200*cf84ac9aSAndroid Build Coastguard Workerotherwise the result is undefined.
201*cf84ac9aSAndroid Build Coastguard Worker
202*cf84ac9aSAndroid Build Coastguard Worker
203*cf84ac9aSAndroid Build Coastguard Worker	1.x.x Group-stop
204*cf84ac9aSAndroid Build Coastguard Worker
205*cf84ac9aSAndroid Build Coastguard WorkerWhen a (possibly multi-threaded) process receives a stopping signal,
206*cf84ac9aSAndroid Build Coastguard Workerall threads stop. If some threads are traced, they enter a group-stop.
207*cf84ac9aSAndroid Build Coastguard WorkerNote that stopping signal will first cause signal-delivery-stop (on one
208*cf84ac9aSAndroid Build Coastguard Workertracee only), and only after it is injected by tracer (or after it was
209*cf84ac9aSAndroid Build Coastguard Workerdispatched to a thread which isn't traced), group-stop will be
210*cf84ac9aSAndroid Build Coastguard Workerinitiated on ALL tracees within multi-threaded process. As usual, every
211*cf84ac9aSAndroid Build Coastguard Workertracee reports its group-stop separately to corresponding tracer.
212*cf84ac9aSAndroid Build Coastguard Worker
213*cf84ac9aSAndroid Build Coastguard WorkerGroup-stop is observed by tracer as waitpid returning with
214*cf84ac9aSAndroid Build Coastguard WorkerWIFSTOPPED(status) == true, WSTOPSIG(status) == signal. The same result
215*cf84ac9aSAndroid Build Coastguard Workeris returned by some other classes of ptrace-stops, therefore the
216*cf84ac9aSAndroid Build Coastguard Workerrecommended practice is to perform
217*cf84ac9aSAndroid Build Coastguard Worker
218*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo)
219*cf84ac9aSAndroid Build Coastguard Worker
220*cf84ac9aSAndroid Build Coastguard Workercall. The call can be avoided if signal number is not SIGSTOP, SIGTSTP,
221*cf84ac9aSAndroid Build Coastguard WorkerSIGTTIN or SIGTTOU - only these four signals are stopping signals. If
222*cf84ac9aSAndroid Build Coastguard Workertracer sees something else, it can't be group-stop. Otherwise, tracer
223*cf84ac9aSAndroid Build Coastguard Workerneeds to call PTRACE_GETSIGINFO. If PTRACE_GETSIGINFO fails with
224*cf84ac9aSAndroid Build Coastguard WorkerEINVAL, then it is definitely a group-stop. (Other failure codes are
225*cf84ac9aSAndroid Build Coastguard Workerpossible, such as ESRCH "no such process" if SIGKILL killed the tracee).
226*cf84ac9aSAndroid Build Coastguard Worker
227*cf84ac9aSAndroid Build Coastguard WorkerAs of kernel 2.6.38, after tracer sees tracee ptrace-stop and until it
228*cf84ac9aSAndroid Build Coastguard Workerrestarts or kills it, tracee will not run, and will not send
229*cf84ac9aSAndroid Build Coastguard Workernotifications (except SIGKILL death) to tracer, even if tracer enters
230*cf84ac9aSAndroid Build Coastguard Workerinto another waitpid call.
231*cf84ac9aSAndroid Build Coastguard Worker
232*cf84ac9aSAndroid Build Coastguard WorkerCurrently, it causes a problem with transparent handling of stopping
233*cf84ac9aSAndroid Build Coastguard Workersignals: if tracer restarts tracee after group-stop, SIGSTOP is
234*cf84ac9aSAndroid Build Coastguard Workereffectively ignored: tracee doesn't remain stopped, it runs. If tracer
235*cf84ac9aSAndroid Build Coastguard Workerdoesn't restart tracee before entering into next waitpid, future
236*cf84ac9aSAndroid Build Coastguard WorkerSIGCONT will not be reported to the tracer. Which would make SIGCONT to
237*cf84ac9aSAndroid Build Coastguard Workerhave no effect.
238*cf84ac9aSAndroid Build Coastguard Worker
239*cf84ac9aSAndroid Build Coastguard Worker
240*cf84ac9aSAndroid Build Coastguard Worker	1.x.x PTRACE_EVENT stops
241*cf84ac9aSAndroid Build Coastguard Worker
242*cf84ac9aSAndroid Build Coastguard WorkerIf tracer sets TRACE_O_TRACEfoo options, tracee will enter ptrace-stops
243*cf84ac9aSAndroid Build Coastguard Workercalled PTRACE_EVENT stops.
244*cf84ac9aSAndroid Build Coastguard Worker
245*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT stops are observed by tracer as waitpid returning with
246*cf84ac9aSAndroid Build Coastguard WorkerWIFSTOPPED(status) == true, WSTOPSIG(status) == SIGTRAP. Additional bit
247*cf84ac9aSAndroid Build Coastguard Workeris set in a higher byte of status word: value ((status >> 8) & 0xffff)
248*cf84ac9aSAndroid Build Coastguard Workerwill be (SIGTRAP | PTRACE_EVENT_foo << 8). The following events exist:
249*cf84ac9aSAndroid Build Coastguard Worker
250*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_VFORK - stop before return from vfork/clone+CLONE_VFORK.
251*cf84ac9aSAndroid Build Coastguard WorkerWhen tracee is continued after this, it will wait for child to
252*cf84ac9aSAndroid Build Coastguard Workerexit/exec before continuing its execution (IOW: usual behavior on
253*cf84ac9aSAndroid Build Coastguard Workervfork).
254*cf84ac9aSAndroid Build Coastguard Worker
255*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_FORK - stop before return from fork/clone+SIGCHLD
256*cf84ac9aSAndroid Build Coastguard Worker
257*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_CLONE - stop before return from clone
258*cf84ac9aSAndroid Build Coastguard Worker
259*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_VFORK_DONE - stop before return from
260*cf84ac9aSAndroid Build Coastguard Workervfork/clone+CLONE_VFORK, but after vfork child unblocked this tracee by
261*cf84ac9aSAndroid Build Coastguard Workerexiting or exec'ing.
262*cf84ac9aSAndroid Build Coastguard Worker
263*cf84ac9aSAndroid Build Coastguard WorkerFor all four stops described above: stop occurs in parent, not in newly
264*cf84ac9aSAndroid Build Coastguard Workercreated thread. PTRACE_GETEVENTMSG can be used to retrieve new thread's
265*cf84ac9aSAndroid Build Coastguard Workertid.
266*cf84ac9aSAndroid Build Coastguard Worker
267*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_EXEC - stop before return from exec.
268*cf84ac9aSAndroid Build Coastguard Worker
269*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_EXIT - stop before exit (including death from exit_group),
270*cf84ac9aSAndroid Build Coastguard Workersignal death, or exit caused by execve in multi-threaded process.
271*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_GETEVENTMSG returns exit status. Registers can be examined
272*cf84ac9aSAndroid Build Coastguard Worker(unlike when "real" exit happens). The tracee is still alive, it needs
273*cf84ac9aSAndroid Build Coastguard Workerto be PTRACE_CONTed or PTRACE_DETACHed to finish exit.
274*cf84ac9aSAndroid Build Coastguard Worker
275*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_GETSIGINFO on PTRACE_EVENT stops returns si_signo = SIGTRAP,
276*cf84ac9aSAndroid Build Coastguard Workersi_code = (event << 8) | SIGTRAP.
277*cf84ac9aSAndroid Build Coastguard Worker
278*cf84ac9aSAndroid Build Coastguard Worker
279*cf84ac9aSAndroid Build Coastguard Worker	1.x.x Syscall-stops
280*cf84ac9aSAndroid Build Coastguard Worker
281*cf84ac9aSAndroid Build Coastguard WorkerIf tracee was restarted by PTRACE_SYSCALL, tracee enters
282*cf84ac9aSAndroid Build Coastguard Workersyscall-enter-stop just prior to entering any syscall. If tracer
283*cf84ac9aSAndroid Build Coastguard Workerrestarts it with PTRACE_SYSCALL, tracee enters syscall-exit-stop when
284*cf84ac9aSAndroid Build Coastguard Workersyscall is finished, or if it is interrupted by a signal. (That is,
285*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop never happens between syscall-enter-stop and
286*cf84ac9aSAndroid Build Coastguard Workersyscall-exit-stop, it happens *after* syscall-exit-stop).
287*cf84ac9aSAndroid Build Coastguard Worker
288*cf84ac9aSAndroid Build Coastguard WorkerOther possibilities are that tracee may stop in a PTRACE_EVENT stop,
289*cf84ac9aSAndroid Build Coastguard Workerexit (if it entered exit or exit_group syscall), be killed by SIGKILL,
290*cf84ac9aSAndroid Build Coastguard Workeror die silently (if execve syscall happened in another thread).
291*cf84ac9aSAndroid Build Coastguard Worker
292*cf84ac9aSAndroid Build Coastguard WorkerSyscall-enter-stop and syscall-exit-stop are observed by tracer as
293*cf84ac9aSAndroid Build Coastguard Workerwaitpid returning with WIFSTOPPED(status) == true, WSTOPSIG(status) ==
294*cf84ac9aSAndroid Build Coastguard WorkerSIGTRAP. If PTRACE_O_TRACESYSGOOD option was set by tracer, then
295*cf84ac9aSAndroid Build Coastguard WorkerWSTOPSIG(status) == (SIGTRAP | 0x80).
296*cf84ac9aSAndroid Build Coastguard Worker
297*cf84ac9aSAndroid Build Coastguard WorkerSyscall-stops can be distinguished from signal-delivery-stop with
298*cf84ac9aSAndroid Build Coastguard WorkerSIGTRAP by querying PTRACE_GETSIGINFO: si_code <= 0 if sent by usual
299*cf84ac9aSAndroid Build Coastguard Workersuspects like [tg]kill/sigqueue/etc; or = SI_KERNEL (0x80) if sent by
300*cf84ac9aSAndroid Build Coastguard Workerkernel, whereas syscall-stops have si_code = SIGTRAP or (SIGTRAP |
301*cf84ac9aSAndroid Build Coastguard Worker0x80). However, syscall-stops happen very often (twice per syscall),
302*cf84ac9aSAndroid Build Coastguard Workerand performing PTRACE_GETSIGINFO for every syscall-stop may be somewhat
303*cf84ac9aSAndroid Build Coastguard Workerexpensive.
304*cf84ac9aSAndroid Build Coastguard Worker
305*cf84ac9aSAndroid Build Coastguard WorkerSome architectures allow to distinguish them by examining registers.
306*cf84ac9aSAndroid Build Coastguard WorkerFor example, on x86 rax = -ENOSYS in syscall-enter-stop. Since SIGTRAP
307*cf84ac9aSAndroid Build Coastguard Worker(like any other signal) always happens *after* syscall-exit-stop, and
308*cf84ac9aSAndroid Build Coastguard Workerat this point rax almost never contains -ENOSYS, SIGTRAP looks like
309*cf84ac9aSAndroid Build Coastguard Worker"syscall-stop which is not syscall-enter-stop", IOW: it looks like a
310*cf84ac9aSAndroid Build Coastguard Worker"stray syscall-exit-stop" and can be detected this way. But such
311*cf84ac9aSAndroid Build Coastguard Workerdetection is fragile and is best avoided.
312*cf84ac9aSAndroid Build Coastguard Worker
313*cf84ac9aSAndroid Build Coastguard WorkerUsing PTRACE_O_TRACESYSGOOD option is a recommended method, since it is
314*cf84ac9aSAndroid Build Coastguard Workerreliable and does not incur performance penalty.
315*cf84ac9aSAndroid Build Coastguard Worker
316*cf84ac9aSAndroid Build Coastguard WorkerSyscall-enter-stop and syscall-exit-stop are indistinguishable from
317*cf84ac9aSAndroid Build Coastguard Workereach other by tracer. Tracer needs to keep track of the sequence of
318*cf84ac9aSAndroid Build Coastguard Workerptrace-stops in order to not misinterpret syscall-enter-stop as
319*cf84ac9aSAndroid Build Coastguard Workersyscall-exit-stop or vice versa. The rule is that syscall-enter-stop is
320*cf84ac9aSAndroid Build Coastguard Workeralways followed by syscall-exit-stop, PTRACE_EVENT stop or tracee's
321*cf84ac9aSAndroid Build Coastguard Workerdeath - no other kinds of ptrace-stop can occur in between.
322*cf84ac9aSAndroid Build Coastguard Worker
323*cf84ac9aSAndroid Build Coastguard WorkerIf after syscall-enter-stop tracer uses restarting command other than
324*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_SYSCALL, syscall-exit-stop is not generated.
325*cf84ac9aSAndroid Build Coastguard Worker
326*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_GETSIGINFO on syscall-stops returns si_signo = SIGTRAP, si_code
327*cf84ac9aSAndroid Build Coastguard Worker= SIGTRAP or (SIGTRAP | 0x80).
328*cf84ac9aSAndroid Build Coastguard Worker
329*cf84ac9aSAndroid Build Coastguard Worker
330*cf84ac9aSAndroid Build Coastguard Worker	1.x.x SINGLESTEP, SYSEMU, SYSEMU_SINGLESTEP
331*cf84ac9aSAndroid Build Coastguard Worker
332*cf84ac9aSAndroid Build Coastguard Worker??? document PTRACE_SINGLESTEP, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP
333*cf84ac9aSAndroid Build Coastguard Worker
334*cf84ac9aSAndroid Build Coastguard Worker
335*cf84ac9aSAndroid Build Coastguard Worker	1.x Informational and restarting ptrace commands.
336*cf84ac9aSAndroid Build Coastguard Worker
337*cf84ac9aSAndroid Build Coastguard WorkerMost ptrace commands (all except ATTACH, TRACEME, KILL) require tracee
338*cf84ac9aSAndroid Build Coastguard Workerto be in ptrace-stop, otherwise they fail with ESRCH.
339*cf84ac9aSAndroid Build Coastguard Worker
340*cf84ac9aSAndroid Build Coastguard WorkerWhen tracee is in ptrace-stop, tracer can read and write data to tracee
341*cf84ac9aSAndroid Build Coastguard Workerusing informational commands. They leave tracee in ptrace-stopped state:
342*cf84ac9aSAndroid Build Coastguard Worker
343*cf84ac9aSAndroid Build Coastguard Workerlongv = ptrace(PTRACE_PEEKTEXT/PEEKDATA/PEEKUSER, pid, addr, 0);
344*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_POKETEXT/POKEDATA/POKEUSER, pid, addr, long_val);
345*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_GETREGS/GETFPREGS, pid, 0, &struct);
346*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_SETREGS/SETFPREGS, pid, 0, &struct);
347*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo);
348*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_SETSIGINFO, pid, 0, &siginfo);
349*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_GETEVENTMSG, pid, 0, &long_var);
350*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags);
351*cf84ac9aSAndroid Build Coastguard Worker
352*cf84ac9aSAndroid Build Coastguard WorkerNote that some errors are not reported. For example, setting siginfo
353*cf84ac9aSAndroid Build Coastguard Workermay have no effect in some ptrace-stops, yet the call may succeed
354*cf84ac9aSAndroid Build Coastguard Worker(return 0 and don't set errno).
355*cf84ac9aSAndroid Build Coastguard Worker
356*cf84ac9aSAndroid Build Coastguard Workerptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_flags) affects one tracee.
357*cf84ac9aSAndroid Build Coastguard WorkerCurrent flags are replaced. Flags are inherited by new tracees created
358*cf84ac9aSAndroid Build Coastguard Workerand "auto-attached" via active PTRACE_O_TRACE[V]FORK or
359*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_O_TRACECLONE options.
360*cf84ac9aSAndroid Build Coastguard Worker
361*cf84ac9aSAndroid Build Coastguard WorkerAnother group of commands makes ptrace-stopped tracee run. They have
362*cf84ac9aSAndroid Build Coastguard Workerthe form:
363*cf84ac9aSAndroid Build Coastguard Worker
364*cf84ac9aSAndroid Build Coastguard Worker	ptrace(PTRACE_cmd, pid, 0, sig);
365*cf84ac9aSAndroid Build Coastguard Worker
366*cf84ac9aSAndroid Build Coastguard Workerwhere cmd is CONT, DETACH, SYSCALL, SINGLESTEP, SYSEMU, or
367*cf84ac9aSAndroid Build Coastguard WorkerSYSEMU_SINGLESTEP. If tracee is in signal-delivery-stop, sig is the
368*cf84ac9aSAndroid Build Coastguard Workersignal to be injected. Otherwise, sig may be ignored.
369*cf84ac9aSAndroid Build Coastguard Worker
370*cf84ac9aSAndroid Build Coastguard Worker
371*cf84ac9aSAndroid Build Coastguard Worker	1.x Attaching and detaching
372*cf84ac9aSAndroid Build Coastguard Worker
373*cf84ac9aSAndroid Build Coastguard WorkerA thread can be attached to tracer using ptrace(PTRACE_ATTACH, pid, 0,
374*cf84ac9aSAndroid Build Coastguard Worker0) call. This also sends SIGSTOP to this thread. If tracer wants this
375*cf84ac9aSAndroid Build Coastguard WorkerSIGSTOP to have no effect, it needs to suppress it. Note that if other
376*cf84ac9aSAndroid Build Coastguard Workersignals are concurrently sent to this thread during attach, tracer may
377*cf84ac9aSAndroid Build Coastguard Workersee tracee enter signal-delivery-stop with other signal(s) first! The
378*cf84ac9aSAndroid Build Coastguard Workerusual practice is to reinject these signals until SIGSTOP is seen, then
379*cf84ac9aSAndroid Build Coastguard Workersuppress SIGSTOP injection. The design bug here is that attach and
380*cf84ac9aSAndroid Build Coastguard Workerconcurrent SIGSTOP are racing and SIGSTOP may be lost.
381*cf84ac9aSAndroid Build Coastguard Worker
382*cf84ac9aSAndroid Build Coastguard Worker??? Describe how to attach to a thread which is already group-stopped.
383*cf84ac9aSAndroid Build Coastguard Worker
384*cf84ac9aSAndroid Build Coastguard WorkerSince attaching sends SIGSTOP and tracer usually suppresses it, this
385*cf84ac9aSAndroid Build Coastguard Workermay cause stray EINTR return from the currently executing syscall in
386*cf84ac9aSAndroid Build Coastguard Workerthe tracee, as described in "signal injection and suppression" section.
387*cf84ac9aSAndroid Build Coastguard Worker
388*cf84ac9aSAndroid Build Coastguard Workerptrace(PTRACE_TRACEME, 0, 0, 0) request turns current thread into a
389*cf84ac9aSAndroid Build Coastguard Workertracee. It continues to run (doesn't enter ptrace-stop). A common
390*cf84ac9aSAndroid Build Coastguard Workerpractice is to follow ptrace(PTRACE_TRACEME) with raise(SIGSTOP) and
391*cf84ac9aSAndroid Build Coastguard Workerallow parent (which is our tracer now) to observe our
392*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop.
393*cf84ac9aSAndroid Build Coastguard Worker
394*cf84ac9aSAndroid Build Coastguard WorkerIf PTRACE_O_TRACE[V]FORK or PTRACE_O_TRACECLONE options are in effect,
395*cf84ac9aSAndroid Build Coastguard Workerthen children created by (vfork or clone(CLONE_VFORK)), (fork or
396*cf84ac9aSAndroid Build Coastguard Workerclone(SIGCHLD)) and (other kinds of clone) respectively are
397*cf84ac9aSAndroid Build Coastguard Workerautomatically attached to the same tracer which traced their parent.
398*cf84ac9aSAndroid Build Coastguard WorkerSIGSTOP is delivered to them, causing them to enter
399*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop after they exit syscall which created them.
400*cf84ac9aSAndroid Build Coastguard Worker
401*cf84ac9aSAndroid Build Coastguard WorkerDetaching of tracee is performed by ptrace(PTRACE_DETACH, pid, 0, sig).
402*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_DETACH is a restarting operation, therefore it requires tracee
403*cf84ac9aSAndroid Build Coastguard Workerto be in ptrace-stop. If tracee is in signal-delivery-stop, signal can
404*cf84ac9aSAndroid Build Coastguard Workerbe injected. Othervice, sig parameter may be silently ignored.
405*cf84ac9aSAndroid Build Coastguard Worker
406*cf84ac9aSAndroid Build Coastguard WorkerIf tracee is running when tracer wants to detach it, the usual solution
407*cf84ac9aSAndroid Build Coastguard Workeris to send SIGSTOP (using tgkill, to make sure it goes to the correct
408*cf84ac9aSAndroid Build Coastguard Workerthread), wait for tracee to stop in signal-delivery-stop for SIGSTOP
409*cf84ac9aSAndroid Build Coastguard Workerand then detach it (suppressing SIGSTOP injection). Design bug is that
410*cf84ac9aSAndroid Build Coastguard Workerthis can race with concurrent SIGSTOPs. Another complication is that
411*cf84ac9aSAndroid Build Coastguard Workertracee may enter other ptrace-stops and needs to be restarted and
412*cf84ac9aSAndroid Build Coastguard Workerwaited for again, until SIGSTOP is seen. Yet another complication is to
413*cf84ac9aSAndroid Build Coastguard Workerbe sure that tracee is not already ptrace-stopped, because no signal
414*cf84ac9aSAndroid Build Coastguard Workerdelivery happens while it is - not even SIGSTOP.
415*cf84ac9aSAndroid Build Coastguard Worker
416*cf84ac9aSAndroid Build Coastguard Worker??? Describe how to detach from a group-stopped tracee so that it
417*cf84ac9aSAndroid Build Coastguard Worker    doesn't run, but continues to wait for SIGCONT.
418*cf84ac9aSAndroid Build Coastguard Worker
419*cf84ac9aSAndroid Build Coastguard WorkerIf tracer dies, all tracees are automatically detached and restarted,
420*cf84ac9aSAndroid Build Coastguard Workerunless they were in group-stop. Handling of restart from group-stop is
421*cf84ac9aSAndroid Build Coastguard Workercurrently buggy, but "as planned" behavior is to leave tracee stopped
422*cf84ac9aSAndroid Build Coastguard Workerand waiting for SIGCONT. If tracee is restarted from
423*cf84ac9aSAndroid Build Coastguard Workersignal-delivery-stop, pending signal is injected.
424*cf84ac9aSAndroid Build Coastguard Worker
425*cf84ac9aSAndroid Build Coastguard Worker
426*cf84ac9aSAndroid Build Coastguard Worker	1.x execve under ptrace.
427*cf84ac9aSAndroid Build Coastguard Worker
428*cf84ac9aSAndroid Build Coastguard WorkerDuring execve, kernel destroys all other threads in the process, and
429*cf84ac9aSAndroid Build Coastguard Workerresets execve'ing thread tid to tgid (process id). This looks very
430*cf84ac9aSAndroid Build Coastguard Workerconfusing to tracers:
431*cf84ac9aSAndroid Build Coastguard Worker
432*cf84ac9aSAndroid Build Coastguard WorkerAll other threads stop in PTRACE_EXIT stop, if requested by active
433*cf84ac9aSAndroid Build Coastguard Workerptrace option. Then all other threads except thread group leader report
434*cf84ac9aSAndroid Build Coastguard Workerdeath as if they exited via exit syscall with exit code 0. Then
435*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_EXEC stop happens, if requested by active ptrace option
436*cf84ac9aSAndroid Build Coastguard Worker(on which tracee - leader? execve-ing one?).
437*cf84ac9aSAndroid Build Coastguard Worker
438*cf84ac9aSAndroid Build Coastguard WorkerThe execve-ing tracee changes its pid while it is in execve syscall.
439*cf84ac9aSAndroid Build Coastguard Worker(Remember, under ptrace 'pid' returned from waitpid, or fed into ptrace
440*cf84ac9aSAndroid Build Coastguard Workercalls, is tracee's tid). That is, pid is reset to process id, which
441*cf84ac9aSAndroid Build Coastguard Workercoincides with thread group leader tid.
442*cf84ac9aSAndroid Build Coastguard Worker
443*cf84ac9aSAndroid Build Coastguard WorkerIf thread group leader has reported its death by this time, for tracer
444*cf84ac9aSAndroid Build Coastguard Workerthis looks like dead thread leader "reappears from nowhere". If thread
445*cf84ac9aSAndroid Build Coastguard Workergroup leader was still alive, for tracer this may look as if thread
446*cf84ac9aSAndroid Build Coastguard Workergroup leader returns from a different syscall than it entered, or even
447*cf84ac9aSAndroid Build Coastguard Worker"returned from syscall even though it was not in any syscall". If
448*cf84ac9aSAndroid Build Coastguard Workerthread group leader was not traced (or was traced by a different
449*cf84ac9aSAndroid Build Coastguard Workertracer), during execve it will appear as if it has become a tracee of
450*cf84ac9aSAndroid Build Coastguard Workerthe tracer of execve'ing tracee. All these effects are the artifacts of
451*cf84ac9aSAndroid Build Coastguard Workerpid change.
452*cf84ac9aSAndroid Build Coastguard Worker
453*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_O_TRACEEXEC option is the recommended tool for dealing with this
454*cf84ac9aSAndroid Build Coastguard Workercase. It enables PTRACE_EVENT_EXEC stop which occurs before execve
455*cf84ac9aSAndroid Build Coastguard Workersyscall return.
456*cf84ac9aSAndroid Build Coastguard Worker
457*cf84ac9aSAndroid Build Coastguard WorkerPid change happens before PTRACE_EVENT_EXEC stop, not after.
458*cf84ac9aSAndroid Build Coastguard Worker
459*cf84ac9aSAndroid Build Coastguard WorkerWhen tracer receives PTRACE_EVENT_EXEC stop notification, it is
460*cf84ac9aSAndroid Build Coastguard Workerguaranteed that except this tracee and thread group leader, no other
461*cf84ac9aSAndroid Build Coastguard Workerthreads from the process are alive.
462*cf84ac9aSAndroid Build Coastguard Worker
463*cf84ac9aSAndroid Build Coastguard WorkerOn receiving this notification, tracer should clean up all its internal
464*cf84ac9aSAndroid Build Coastguard Workerdata structures about all threads of this process, and retain only one
465*cf84ac9aSAndroid Build Coastguard Workerdata structure, one which describes single still running tracee, with
466*cf84ac9aSAndroid Build Coastguard Workerpid = tgid = process id.
467*cf84ac9aSAndroid Build Coastguard Worker
468*cf84ac9aSAndroid Build Coastguard WorkerCurrently, there is no way to retrieve former pid of execve-ing tracee.
469*cf84ac9aSAndroid Build Coastguard WorkerIf tracer doesn't keep track of its tracees' thread group relations, it
470*cf84ac9aSAndroid Build Coastguard Workermay be unable to know which tracee  execve-ed and therefore no longer
471*cf84ac9aSAndroid Build Coastguard Workerexists under old pid due to pid change.
472*cf84ac9aSAndroid Build Coastguard Worker
473*cf84ac9aSAndroid Build Coastguard WorkerExample: two threads execve at the same time:
474*cf84ac9aSAndroid Build Coastguard Worker
475*cf84ac9aSAndroid Build Coastguard Worker  ** we get syscall-entry-stop in thread 1: **
476*cf84ac9aSAndroid Build Coastguard Worker PID1 execve("/bin/foo", "foo" <unfinished ...>
477*cf84ac9aSAndroid Build Coastguard Worker  ** we issue PTRACE_SYSCALL for thread 1 **
478*cf84ac9aSAndroid Build Coastguard Worker  ** we get syscall-entry-stop in thread 2: **
479*cf84ac9aSAndroid Build Coastguard Worker PID2 execve("/bin/bar", "bar" <unfinished ...>
480*cf84ac9aSAndroid Build Coastguard Worker  ** we issue PTRACE_SYSCALL for thread 2 **
481*cf84ac9aSAndroid Build Coastguard Worker  ** we get PTRACE_EVENT_EXEC for PID0, we issue PTRACE_SYSCALL **
482*cf84ac9aSAndroid Build Coastguard Worker  ** we get syscall-exit-stop for PID0: **
483*cf84ac9aSAndroid Build Coastguard Worker PID0 <... execve resumed> )             = 0
484*cf84ac9aSAndroid Build Coastguard Worker
485*cf84ac9aSAndroid Build Coastguard WorkerIn this situation there is no way to know which execve succeeded.
486*cf84ac9aSAndroid Build Coastguard Worker
487*cf84ac9aSAndroid Build Coastguard WorkerIf PTRACE_O_TRACEEXEC option is NOT in effect for the execve'ing
488*cf84ac9aSAndroid Build Coastguard Workertracee, kernel delivers an extra SIGTRAP to tracee after execve syscall
489*cf84ac9aSAndroid Build Coastguard Workerreturns. This is an ordinary signal (similar to one which can be
490*cf84ac9aSAndroid Build Coastguard Workergenerated by "kill -TRAP"), not a special kind of ptrace-stop.
491*cf84ac9aSAndroid Build Coastguard WorkerGETSIGINFO on it has si_code = 0 (SI_USER). It can be blocked by signal
492*cf84ac9aSAndroid Build Coastguard Workermask, and thus can happen (much) later.
493*cf84ac9aSAndroid Build Coastguard Worker
494*cf84ac9aSAndroid Build Coastguard WorkerUsually, tracer (for example, strace) would not want to show this extra
495*cf84ac9aSAndroid Build Coastguard Workerpost-execve SIGTRAP signal to the user, and would suppress its delivery
496*cf84ac9aSAndroid Build Coastguard Workerto the tracee (if SIGTRAP is set to SIG_DFL, it is a killing signal).
497*cf84ac9aSAndroid Build Coastguard WorkerHowever, determining *which* SIGTRAP to suppress is not easy. Setting
498*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_O_TRACEEXEC option and thus suppressing this extra SIGTRAP is
499*cf84ac9aSAndroid Build Coastguard Workerthe recommended approach.
500*cf84ac9aSAndroid Build Coastguard Worker
501*cf84ac9aSAndroid Build Coastguard Worker
502*cf84ac9aSAndroid Build Coastguard Worker	1.x Real parent
503*cf84ac9aSAndroid Build Coastguard Worker
504*cf84ac9aSAndroid Build Coastguard WorkerPtrace API (ab)uses standard Unix parent/child signaling over waitpid.
505*cf84ac9aSAndroid Build Coastguard WorkerThis used to cause real parent of the process to stop receiving several
506*cf84ac9aSAndroid Build Coastguard Workerkinds of waitpid notifications when child process is traced by some
507*cf84ac9aSAndroid Build Coastguard Workerother process.
508*cf84ac9aSAndroid Build Coastguard Worker
509*cf84ac9aSAndroid Build Coastguard WorkerMany of these bugs have been fixed, but as of 2.6.38 several still
510*cf84ac9aSAndroid Build Coastguard Workerexist.
511*cf84ac9aSAndroid Build Coastguard Worker
512*cf84ac9aSAndroid Build Coastguard WorkerAs of 2.6.38, the following is believed to work correctly:
513*cf84ac9aSAndroid Build Coastguard Worker
514*cf84ac9aSAndroid Build Coastguard Worker- exit/death by signal is reported first to tracer, then, when tracer
515*cf84ac9aSAndroid Build Coastguard Workerconsumes waitpid result, to real parent (to real parent only when the
516*cf84ac9aSAndroid Build Coastguard Workerwhole multi-threaded process exits). If they are the same process, the
517*cf84ac9aSAndroid Build Coastguard Workerreport is sent only once.
518*cf84ac9aSAndroid Build Coastguard Worker
519*cf84ac9aSAndroid Build Coastguard Worker
520*cf84ac9aSAndroid Build Coastguard Worker	1.x Known bugs
521*cf84ac9aSAndroid Build Coastguard Worker
522*cf84ac9aSAndroid Build Coastguard WorkerFollowing bugs still exist:
523*cf84ac9aSAndroid Build Coastguard Worker
524*cf84ac9aSAndroid Build Coastguard WorkerGroup-stop notifications are sent to tracer, but not to real parent.
525*cf84ac9aSAndroid Build Coastguard WorkerLast confirmed on 2.6.38.6.
526*cf84ac9aSAndroid Build Coastguard Worker
527*cf84ac9aSAndroid Build Coastguard WorkerIf thread group leader is traced and exits by calling exit syscall,
528*cf84ac9aSAndroid Build Coastguard WorkerPTRACE_EVENT_EXIT stop will happen for it (if requested), but subsequent
529*cf84ac9aSAndroid Build Coastguard WorkerWIFEXITED notification will not be delivered until all other threads
530*cf84ac9aSAndroid Build Coastguard Workerexit. As explained above, if one of other threads execve's, thread
531*cf84ac9aSAndroid Build Coastguard Workergroup leader death will *never* be reported. If execve-ed thread is not
532*cf84ac9aSAndroid Build Coastguard Workertraced by this tracer, tracer will never know that execve happened.
533*cf84ac9aSAndroid Build Coastguard Worker
534*cf84ac9aSAndroid Build Coastguard Worker??? need to test this scenario
535*cf84ac9aSAndroid Build Coastguard Worker
536*cf84ac9aSAndroid Build Coastguard WorkerOne possible workaround is to detach thread group leader instead of
537*cf84ac9aSAndroid Build Coastguard Workerrestarting it in this case. Last confirmed on 2.6.38.6.
538*cf84ac9aSAndroid Build Coastguard Worker
539*cf84ac9aSAndroid Build Coastguard WorkerSIGKILL signal may still cause PTRACE_EVENT_EXIT stop before actual
540*cf84ac9aSAndroid Build Coastguard Workersignal death. This may be changed in the future - SIGKILL is meant to
541*cf84ac9aSAndroid Build Coastguard Workeralways immediately kill tasks even under ptrace. Last confirmed on
542*cf84ac9aSAndroid Build Coastguard Worker2.6.38.6.
543