blob: f25fabccd7207489640e6995acebf62c88d79ecb [file] [log] [blame]
Petr Machatacec06ec2012-04-10 13:31:55 +02001#include "config.h"
2
3#include <asm/unistd.h>
4#include <sys/types.h>
5#include <sys/wait.h>
6#include <assert.h>
7#include <errno.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01008#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01009#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010010#include <string.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010011#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
Petr Machatacec06ec2012-04-10 13:31:55 +020013#ifdef HAVE_LIBSELINUX
14# include <selinux/selinux.h>
15#endif
16
17#include "ptrace.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020018#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010019#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010020#include "proc.h"
Petr Machata92822542012-03-28 00:31:14 +020021#include "linux-gnu/trace.h"
Petr Machata000e3112012-01-03 17:03:39 +010022#include "type.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020023
24/* If the system headers did not provide the constants, hard-code the normal
25 values. */
26#ifndef PTRACE_EVENT_FORK
27
28#define PTRACE_OLDSETOPTIONS 21
29#define PTRACE_SETOPTIONS 0x4200
30#define PTRACE_GETEVENTMSG 0x4201
31
32/* options set using PTRACE_SETOPTIONS */
33#define PTRACE_O_TRACESYSGOOD 0x00000001
34#define PTRACE_O_TRACEFORK 0x00000002
35#define PTRACE_O_TRACEVFORK 0x00000004
36#define PTRACE_O_TRACECLONE 0x00000008
37#define PTRACE_O_TRACEEXEC 0x00000010
38#define PTRACE_O_TRACEVFORKDONE 0x00000020
39#define PTRACE_O_TRACEEXIT 0x00000040
40
41/* Wait extended result codes for the above trace options. */
42#define PTRACE_EVENT_FORK 1
43#define PTRACE_EVENT_VFORK 2
44#define PTRACE_EVENT_CLONE 3
45#define PTRACE_EVENT_EXEC 4
46#define PTRACE_EVENT_VFORK_DONE 5
47#define PTRACE_EVENT_EXIT 6
48
49#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010050
Juan Cespedesf1350522008-12-16 18:19:58 +010051void
Petr Machatacec06ec2012-04-10 13:31:55 +020052trace_fail_warning(pid_t pid)
53{
54 /* This was adapted from GDB. */
55#ifdef HAVE_LIBSELINUX
56 static int checked = 0;
57 if (checked)
58 return;
59 checked = 1;
60
61 /* -1 is returned for errors, 0 if it has no effect, 1 if
62 * PTRACE_ATTACH is forbidden. */
63 if (security_get_boolean_active("deny_ptrace") == 1)
64 fprintf(stderr,
65"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
66"tracing other processes. You can disable this process attach protection by\n"
67"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
68#endif /* HAVE_LIBSELINUX */
69}
70
71void
72trace_me(void)
73{
Petr Machata26627682011-07-08 18:15:32 +020074 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020075 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010076 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +020077 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +010078 exit(1);
79 }
80}
81
Petr Machatab4f9e0c2012-02-07 01:57:59 +010082/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +010083 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +010084 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +010085int
Petr Machatab4f9e0c2012-02-07 01:57:59 +010086wait_for_proc(pid_t pid)
87{
Petr Machatac805c622012-03-02 00:10:37 +010088 /* man ptrace: PTRACE_ATTACH attaches to the process specified
89 in pid. The child is sent a SIGSTOP, but will not
90 necessarily have stopped by the completion of this call;
91 use wait() to wait for the child to stop. */
92 if (waitpid(pid, NULL, __WALL) != pid) {
93 perror ("trace_pid: waitpid");
94 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010095 }
96
Petr Machatac805c622012-03-02 00:10:37 +010097 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010098}
99
Juan Cespedesf1350522008-12-16 18:19:58 +0100100int
Petr Machatacec06ec2012-04-10 13:31:55 +0200101trace_pid(pid_t pid)
102{
Petr Machata26627682011-07-08 18:15:32 +0200103 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200104 /* This shouldn't emit error messages, as there are legitimate
105 * reasons that the PID can't be attached: like it may have
106 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200107 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100108 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100109
Petr Machatac805c622012-03-02 00:10:37 +0100110 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100111}
112
Juan Cespedesf1350522008-12-16 18:19:58 +0100113void
Petr Machata3ed2a422012-04-06 17:18:55 +0200114trace_set_options(struct Process *proc)
115{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100116 if (proc->tracesysgood & 0x80)
117 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200118
Petr Machata3ed2a422012-04-06 17:18:55 +0200119 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200120 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200121
Juan Cespedes1e583132009-04-07 18:17:11 +0200122 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
123 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
124 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200125 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
126 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100127 perror("PTRACE_SETOPTIONS");
128 return;
129 }
130 proc->tracesysgood |= 0x80;
131}
132
Juan Cespedesf1350522008-12-16 18:19:58 +0100133void
134untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200135 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200136 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139void
Petr Machatac897cb72012-05-05 01:26:58 +0200140continue_after_signal(pid_t pid, int signum)
141{
142 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
143 pid, signum);
144 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200145}
146
147static enum ecb_status
148event_for_pid(Event * event, void * data)
149{
150 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
151 return ecb_yield;
152 return ecb_cont;
153}
154
155static int
156have_events_for(pid_t pid)
157{
158 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
159}
160
161void
162continue_process(pid_t pid)
163{
164 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200165
166 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200167 the queue for this process. Otherwise just wait for the
168 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200169 if (!have_events_for(pid))
170 /* We always trace syscalls to control fork(),
171 * clone(), execve()... */
172 ptrace(PTRACE_SYSCALL, pid, 0, 0);
173 else
174 debug(DEBUG_PROCESS,
175 "putting off the continue, events in que.");
176}
177
Petr Machata98f09922011-07-09 10:55:29 +0200178static struct pid_task *
179get_task_info(struct pid_set * pids, pid_t pid)
180{
Petr Machata750ca8c2011-10-06 14:29:34 +0200181 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200182 size_t i;
183 for (i = 0; i < pids->count; ++i)
184 if (pids->tasks[i].pid == pid)
185 return &pids->tasks[i];
186
187 return NULL;
188}
189
190static struct pid_task *
191add_task_info(struct pid_set * pids, pid_t pid)
192{
193 if (pids->count == pids->alloc) {
194 size_t ns = (2 * pids->alloc) ?: 4;
195 struct pid_task * n = realloc(pids->tasks,
196 sizeof(*pids->tasks) * ns);
197 if (n == NULL)
198 return NULL;
199 pids->tasks = n;
200 pids->alloc = ns;
201 }
202 struct pid_task * task_info = &pids->tasks[pids->count++];
203 memset(task_info, 0, sizeof(*task_info));
204 task_info->pid = pid;
205 return task_info;
206}
207
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208static enum callback_status
209task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200210{
211 enum process_status st = process_status(task->pid);
212 if (data != NULL)
213 *(enum process_status *)data = st;
214
215 /* If the task is already stopped, don't worry about it.
216 * Likewise if it managed to become a zombie or terminate in
217 * the meantime. This can happen when the whole thread group
218 * is terminating. */
219 switch (st) {
220 case ps_invalid:
221 case ps_tracing_stop:
222 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200224 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200225 case ps_stop:
226 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200228 }
Petr Machata36d19822011-10-21 16:03:45 +0200229
230 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200231}
232
233/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234static enum callback_status
235task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200236{
237 struct pid_set * pids = data;
238 struct pid_task * task_info = get_task_info(pids, task->pid);
239 if (task_info != NULL
240 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200242
243 return task_stopped(task, NULL);
244}
245
Petr Machata366c2f42012-02-09 19:34:36 +0100246static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200247
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248static enum callback_status
249task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200250{
251 if (task->event_handler != NULL
252 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 return CBS_STOP;
254 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200255}
256
257static int
Petr Machata74132a42012-03-16 02:46:18 +0100258is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200259{
Petr Machata74132a42012-03-16 02:46:18 +0100260 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200261}
262
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263static enum callback_status
264send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200265{
266 Process * leader = task->leader;
267 struct pid_set * pids = data;
268
269 /* Look for pre-existing task record, or add new. */
270 struct pid_task * task_info = get_task_info(pids, task->pid);
271 if (task_info == NULL)
272 task_info = add_task_info(pids, task->pid);
273 if (task_info == NULL) {
274 perror("send_sigstop: add_task_info");
275 destroy_event_handler(leader);
276 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200278 }
279
280 /* This task still has not been attached to. It should be
281 stopped by the kernel. */
282 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100283 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200284
285 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200286 * if we sent the SIGSTOP already, which happens when we are
287 * handling "onexit" and inherited the handler from breakpoint
288 * re-enablement. */
289 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 if (task_stopped(task, &st) == CBS_CONT)
291 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200292 if (task_info->sigstopped) {
293 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200295 task_info->delivered = 0;
296 }
297
Petr Machatacbe29c62011-09-27 02:27:58 +0200298 /* Also don't attempt to stop the process if it's a parent of
299 * vforked process. We set up event handler specially to hint
300 * us. In that case parent is in D state, which we use to
301 * weed out unnecessary looping. */
302 if (st == ps_sleeping
303 && is_vfork_parent (task)) {
304 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200306 }
307
Petr Machata98f09922011-07-09 10:55:29 +0200308 if (task_kill(task->pid, SIGSTOP) >= 0) {
309 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
310 task_info->sigstopped = 1;
311 } else
312 fprintf(stderr,
313 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
314
Petr Machata2b46cfc2012-02-18 11:17:29 +0100315 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200316}
317
Petr Machata73894bd2011-08-20 23:47:34 +0200318/* On certain kernels, detaching right after a singlestep causes the
319 tracee to be killed with a SIGTRAP (that even though the singlestep
320 was properly caught by waitpid. The ugly workaround is to put a
321 breakpoint where IP points and let the process continue. After
322 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200323static void
Petr Machata73894bd2011-08-20 23:47:34 +0200324ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200325{
326 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100327 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200328 if (sbp != NULL)
329 enable_breakpoint(proc, sbp);
330 else
Petr Machata9df15012012-02-20 12:49:46 +0100331 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200332 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200333}
334
335static void
Petr Machata98f09922011-07-09 10:55:29 +0200336process_stopping_done(struct process_stopping_handler * self, Process * leader)
337{
338 debug(DEBUG_PROCESS, "process stopping done %d",
339 self->task_enabling_breakpoint->pid);
340 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200341 if (!self->exiting) {
342 for (i = 0; i < self->pids.count; ++i)
343 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100344 && (self->pids.tasks[i].delivered
345 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200346 continue_process(self->pids.tasks[i].pid);
347 continue_process(self->task_enabling_breakpoint->pid);
348 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200349 }
350
351 if (self->exiting) {
352 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200353 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200354 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200355 } else {
356 switch ((self->ugly_workaround_p)(self)) {
357 case CBS_FAIL:
358 /* xxx handle me */
359 case CBS_STOP:
360 break;
361 case CBS_CONT:
362 goto ugly_workaround;
363 }
Petr Machata590c8082011-08-20 22:45:26 +0200364 }
365}
366
367/* Before we detach, we need to make sure that task's IP is on the
368 * edge of an instruction. So for tasks that have a breakpoint event
369 * in the queue, we adjust the instruction pointer, just like
370 * continue_after_breakpoint does. */
371static enum ecb_status
372undo_breakpoint(Event * event, void * data)
373{
374 if (event != NULL
375 && event->proc->leader == data
376 && event->type == EVENT_BREAKPOINT)
377 set_instruction_pointer(event->proc, event->e_un.brk_addr);
378 return ecb_cont;
379}
380
Petr Machata2b46cfc2012-02-18 11:17:29 +0100381static enum callback_status
382untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200383{
384 if (task != data)
385 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100386 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200387}
388
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389static enum callback_status
390remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200391{
392 /* Don't untrace leader just yet. */
393 if (task != data)
394 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100395 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200396}
397
Petr Machata86d38282012-04-24 18:09:01 +0200398static enum callback_status
399retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
400{
401 breakpoint_on_retract(bp, proc);
402 return CBS_CONT;
403}
404
Petr Machata590c8082011-08-20 22:45:26 +0200405static void
406detach_process(Process * leader)
407{
408 each_qd_event(&undo_breakpoint, leader);
409 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200410 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200411
412 /* Now untrace the process, if it was attached to by -p. */
413 struct opt_p_t * it;
414 for (it = opt_p; it != NULL; it = it->next) {
415 Process * proc = pid2proc(it->pid);
416 if (proc == NULL)
417 continue;
418 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100419 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200420 break;
421 }
422 }
Petr Machata74132a42012-03-16 02:46:18 +0100423 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200424 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200425 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200426}
427
428static void
429handle_stopping_event(struct pid_task * task_info, Event ** eventp)
430{
431 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200432 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200433 task_info->got_event = 1;
434
435 Event * event = *eventp;
436
437 /* In every state, sink SIGSTOP events for tasks that it was
438 * sent to. */
439 if (task_info != NULL
440 && event->type == EVENT_SIGNAL
441 && event->e_un.signum == SIGSTOP) {
442 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
443 if (task_info->sigstopped
444 && !task_info->delivered) {
445 task_info->delivered = 1;
446 *eventp = NULL; // sink the event
447 } else
448 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
449 "sigstopped=%d and delivered=%d\n",
450 task_info->pid, task_info->sigstopped,
451 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100452 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100453}
454
Petr Machata98f09922011-07-09 10:55:29 +0200455/* Some SIGSTOPs may have not been delivered to their respective tasks
456 * yet. They are still in the queue. If we have seen an event for
457 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200458 * caught by ltrace. We don't mind that the process is after
459 * breakpoint (and therefore potentially doesn't have aligned IP),
460 * because the signal will be delivered without the process actually
461 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200462static void
463continue_for_sigstop_delivery(struct pid_set * pids)
464{
465 size_t i;
466 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200467 if (pids->tasks[i].pid != 0
468 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200469 && !pids->tasks[i].delivered
470 && pids->tasks[i].got_event) {
471 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
472 pids->tasks[i].pid);
473 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
474 }
475 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100476}
477
Petr Machata98f09922011-07-09 10:55:29 +0200478static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200479event_exit_p(Event * event)
480{
481 return event != NULL && (event->type == EVENT_EXIT
482 || event->type == EVENT_EXIT_SIGNAL);
483}
484
485static int
Petr Machata98f09922011-07-09 10:55:29 +0200486event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200487{
Petr Machata750ca8c2011-10-06 14:29:34 +0200488 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200489 || event->type == EVENT_NONE;
490}
491
492static int
493await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
494 Event * event)
495{
496 /* If we still didn't get our SIGSTOP, continue the process
497 * and carry on. */
498 if (event != NULL && !event_exit_or_none_p(event)
499 && task_info != NULL && task_info->sigstopped) {
500 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
501 task_info->pid);
502 /* We should get the signal the first thing
503 * after this, so it should be OK to continue
504 * even if we are over a breakpoint. */
505 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
506
507 } else {
508 /* If all SIGSTOPs were delivered, uninstall the
509 * handler and continue everyone. */
510 /* XXX I suspect that we should check tasks that are
511 * still around. Is things are now, there should be a
512 * race between waiting for everyone to stop and one
513 * of the tasks exiting. */
514 int all_clear = 1;
515 size_t i;
516 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200517 if (pids->tasks[i].pid != 0
518 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200519 && !pids->tasks[i].delivered) {
520 all_clear = 0;
521 break;
522 }
523 return all_clear;
524 }
525
526 return 0;
527}
528
Petr Machata590c8082011-08-20 22:45:26 +0200529static int
530all_stops_accountable(struct pid_set * pids)
531{
532 size_t i;
533 for (i = 0; i < pids->count; ++i)
534 if (pids->tasks[i].pid != 0
535 && !pids->tasks[i].got_event
536 && !have_events_for(pids->tasks[i].pid))
537 return 0;
538 return 1;
539}
540
Petr Machataa266acb2012-04-12 23:50:23 +0200541/* The protocol is: 0 for success, negative for failure, positive if
542 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200543int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200544 int (*add_cb)(void *addr, void *data),
545 void *add_cb_data);
546
547#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
548int
Petr Machata9e1e9692012-04-19 02:29:38 +0200549arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200550 int (*add_cb)(void *addr, void *data),
551 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100552{
Petr Machataa266acb2012-04-12 23:50:23 +0200553 return 1;
554}
555#endif
556
Petr Machata42748ac2012-04-19 04:24:25 +0200557static Event *process_stopping_on_event(struct event_handler *super,
558 Event *event);
559
560static void
561remove_atomic_breakpoints(struct Process *proc)
562{
Petr Machata24a47042012-04-19 18:15:08 +0200563 struct process_stopping_handler *self
564 = (void *)proc->leader->event_handler;
565 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200566 assert(self->super.on_event == process_stopping_on_event);
567
568 int ct = sizeof(self->atomic_skip_bp_addrs)
569 / sizeof(*self->atomic_skip_bp_addrs);
570 int i;
571 for (i = 0; i < ct; ++i)
572 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200573 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200574 self->atomic_skip_bp_addrs[i] = 0;
575 }
576}
577
578static void
579atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
580{
581 remove_atomic_breakpoints(proc);
582}
583
Petr Machataa266acb2012-04-12 23:50:23 +0200584static int
585atomic_singlestep_add_bp(void *addr, void *data)
586{
587 struct process_stopping_handler *self = data;
588 struct Process *proc = self->task_enabling_breakpoint;
589
Petr Machata42748ac2012-04-19 04:24:25 +0200590 int ct = sizeof(self->atomic_skip_bp_addrs)
591 / sizeof(*self->atomic_skip_bp_addrs);
592 int i;
593 for (i = 0; i < ct; ++i)
594 if (self->atomic_skip_bp_addrs[i] == 0) {
595 self->atomic_skip_bp_addrs[i] = addr;
596 static struct bp_callbacks cbs = {
597 .on_hit = atomic_singlestep_bp_on_hit,
598 };
599 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200600 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200601 breakpoint_set_callbacks(bp, &cbs);
602 return 0;
603 }
Petr Machataa266acb2012-04-12 23:50:23 +0200604
Petr Machata42748ac2012-04-19 04:24:25 +0200605 assert(!"Too many atomic singlestep breakpoints!");
606 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200607}
608
609static int
610singlestep(struct process_stopping_handler *self)
611{
612 struct Process *proc = self->task_enabling_breakpoint;
613
614 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
615 self->breakpoint_being_enabled,
616 &atomic_singlestep_add_bp, self);
617
618 /* Propagate failure and success. */
619 if (status <= 0)
620 return status;
621
622 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100623 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200624 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100625 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200626 return -1;
627 }
628 return 0;
629}
630
631static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200632post_singlestep(struct process_stopping_handler *self,
633 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200634{
635 continue_for_sigstop_delivery(&self->pids);
636
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200637 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200638 *eventp = NULL; // handled
639
Petr Machata42748ac2012-04-19 04:24:25 +0200640 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200641
Petr Machata42748ac2012-04-19 04:24:25 +0200642 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200643 self->breakpoint_being_enabled = NULL;
644}
645
646static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200647singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200648{
649 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200650 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200651 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataa266acb2012-04-12 23:50:23 +0200652 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200653 sbp->addr, get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200654 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100655}
656
Petr Machata9e1e9692012-04-19 02:29:38 +0200657static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200658pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200659{
660 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200661 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200662 ptrace(PTRACE_CONT, teb->pid, 0, 0);
663}
664
665static void
666pt_singlestep(struct process_stopping_handler *self)
667{
668 if (singlestep(self) < 0)
669 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200670}
671
672static void
673disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200674 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200675{
676 struct Process *teb = self->task_enabling_breakpoint;
677 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200678 if (self->breakpoint_being_enabled->enabled)
679 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200680 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200681 self->state = psh_singlestep;
682}
683
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200684void
685linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
686{
Petr Machata9e1e9692012-04-19 02:29:38 +0200687 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200688}
689
690void
691linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
692{
693 disable_and(self, &pt_continue);
694}
695
Petr Machata98f09922011-07-09 10:55:29 +0200696/* This event handler is installed when we are in the process of
697 * stopping the whole thread group to do the pointer re-enablement for
698 * one of the threads. We pump all events to the queue for later
699 * processing while we wait for all the threads to stop. When this
700 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
701 * re-enable, and continue everyone. */
702static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100703process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200704{
705 struct process_stopping_handler * self = (void *)super;
706 Process * task = event->proc;
707 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200708 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200709
710 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200711 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200712 task->pid, event->type, self->state);
713
714 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
715 if (task_info == NULL)
716 fprintf(stderr, "new task??? %d\n", task->pid);
717 handle_stopping_event(task_info, &event);
718
719 int state = self->state;
720 int event_to_queue = !event_exit_or_none_p(event);
721
Petr Machata18c97072011-10-06 14:30:11 +0200722 /* Deactivate the entry if the task exits. */
723 if (event_exit_p(event) && task_info != NULL)
724 task_info->pid = 0;
725
Petr Machata43d2fe52011-11-02 13:25:49 +0100726 /* Always handle sysrets. Whether sysret occurred and what
727 * sys it rets from may need to be determined based on process
728 * stack, so we need to keep that in sync with reality. Note
729 * that we don't continue the process after the sysret is
730 * handled. See continue_after_syscall. */
731 if (event != NULL && event->type == EVENT_SYSRET) {
732 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
733 event_to_queue = 0;
734 task_info->sysret = 1;
735 }
736
Petr Machata98f09922011-07-09 10:55:29 +0200737 switch (state) {
738 case psh_stopping:
739 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100740 if (each_task(leader, NULL, &task_blocked,
741 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200742 (self->on_all_stopped)(self);
743 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200744 }
745 break;
746
Petr Machata06986d52011-11-02 13:22:46 +0100747 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200748 /* In singlestep state, breakpoint signifies that we
749 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200750 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200751
Petr Machata42748ac2012-04-19 04:24:25 +0200752 /* If this was caused by a real breakpoint, as
753 * opposed to a singlestep, assume that it's
754 * an artificial breakpoint installed for some
755 * reason for the re-enablement. In that case
756 * handle it. */
757 if (event->type == EVENT_BREAKPOINT) {
758 target_address_t ip
759 = get_instruction_pointer(task);
760 struct breakpoint *other
761 = address2bpstruct(leader, ip);
762 if (other != NULL)
763 breakpoint_on_hit(other, task);
764 }
765
Petr Machata36f40e72012-03-28 02:07:19 +0200766 /* If we got SIGNAL instead of BREAKPOINT,
767 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200768 if (event->type == EVENT_SIGNAL) {
769 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200770 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200771 singlestep_error(self);
772 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200773 goto psh_sinking;
774 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200775 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200776 } else {
777 switch ((self->keep_stepping_p)(self)) {
778 case CBS_FAIL:
779 /* XXX handle me */
780 case CBS_STOP:
781 break;
782 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200783 /* Sink singlestep event. */
784 if (event->type == EVENT_BREAKPOINT)
785 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200786 goto do_singlestep;
787 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200788 }
789
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200790 /* Re-enable the breakpoint that we are
791 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200792 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200793 if (sbp->enabled)
794 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200795
Petr Machataa266acb2012-04-12 23:50:23 +0200796 post_singlestep(self, &event);
797 goto psh_sinking;
798 }
799 break;
Petr Machata98f09922011-07-09 10:55:29 +0200800
Petr Machataa266acb2012-04-12 23:50:23 +0200801 psh_sinking:
802 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200803 case psh_sinking:
804 if (await_sigstop_delivery(&self->pids, task_info, event))
805 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200806 break;
807
808 case psh_ugly_workaround:
809 if (event == NULL)
810 break;
811 if (event->type == EVENT_BREAKPOINT) {
812 undo_breakpoint(event, leader);
813 if (task == teb)
814 self->task_enabling_breakpoint = NULL;
815 }
816 if (self->task_enabling_breakpoint == NULL
817 && all_stops_accountable(&self->pids)) {
818 undo_breakpoint(event, leader);
819 detach_process(leader);
820 event = NULL; // handled
821 }
Petr Machata98f09922011-07-09 10:55:29 +0200822 }
823
824 if (event != NULL && event_to_queue) {
825 enque_event(event);
826 event = NULL; // sink the event
827 }
828
829 return event;
830}
831
832static void
Petr Machata366c2f42012-02-09 19:34:36 +0100833process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200834{
835 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200836 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100837}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100838
Petr Machata36f40e72012-03-28 02:07:19 +0200839static enum callback_status
840no(struct process_stopping_handler *self)
841{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200842 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200843}
844
Petr Machatac1aa9582012-03-27 23:54:01 +0200845int
846process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200847 void (*as)(struct process_stopping_handler *),
848 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200849 (struct process_stopping_handler *),
850 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200851 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200852{
Petr Machata9ace5742012-04-14 02:29:57 +0200853 debug(DEBUG_FUNCTION,
854 "process_install_stopping_handler: pid=%d", proc->pid);
855
Petr Machatac1aa9582012-03-27 23:54:01 +0200856 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
857 if (handler == NULL)
858 return -1;
859
Petr Machata36f40e72012-03-28 02:07:19 +0200860 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200861 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200862 if (ks == NULL)
863 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200864 if (uw == NULL)
865 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200866
Petr Machatac1aa9582012-03-27 23:54:01 +0200867 handler->super.on_event = process_stopping_on_event;
868 handler->super.destroy = process_stopping_destroy;
869 handler->task_enabling_breakpoint = proc;
870 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200871 handler->on_all_stopped = as;
872 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200873 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200874
875 install_event_handler(proc->leader, &handler->super);
876
877 if (each_task(proc->leader, NULL, &send_sigstop,
878 &handler->pids) != NULL) {
879 destroy_event_handler(proc);
880 return -1;
881 }
882
883 /* And deliver the first fake event, in case all the
884 * conditions are already fulfilled. */
885 Event ev = {
886 .type = EVENT_NONE,
887 .proc = proc,
888 };
889 process_stopping_on_event(&handler->super, &ev);
890
891 return 0;
892}
893
Juan Cespedesf1350522008-12-16 18:19:58 +0100894void
Petr Machatabc373262012-02-07 23:31:15 +0100895continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200896{
Petr Machata9ace5742012-04-14 02:29:57 +0200897 debug(DEBUG_PROCESS,
898 "continue_after_breakpoint: pid=%d, addr=%p",
899 proc->pid, sbp->addr);
900
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200901 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200902
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100903 if (sbp->enabled == 0) {
904 continue_process(proc->pid);
905 } else {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500906#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100907 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200908 continue_process(proc->pid);
909#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200910 if (process_install_stopping_handler
911 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200912 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200913 /* Carry on not bothering to re-enable. */
914 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200915 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200916#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100917 }
918}
919
Petr Machata602330f2011-07-09 11:15:34 +0200920/**
921 * Ltrace exit. When we are about to exit, we have to go through all
922 * the processes, stop them all, remove all the breakpoints, and then
923 * detach the processes that we attached to using -p. If we left the
924 * other tasks running, they might hit stray return breakpoints and
925 * produce artifacts, so we better stop everyone, even if it's a bit
926 * of extra work.
927 */
928struct ltrace_exiting_handler
929{
Petr Machata366c2f42012-02-09 19:34:36 +0100930 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200931 struct pid_set pids;
932};
933
Petr Machata602330f2011-07-09 11:15:34 +0200934static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100935ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200936{
937 struct ltrace_exiting_handler * self = (void *)super;
938 Process * task = event->proc;
939 Process * leader = task->leader;
940
Petr Machata9ace5742012-04-14 02:29:57 +0200941 debug(DEBUG_PROCESS,
942 "ltrace_exiting_on_event: pid %d; event type %d",
943 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200944
945 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
946 handle_stopping_event(task_info, &event);
947
Petr Machata590c8082011-08-20 22:45:26 +0200948 if (event != NULL && event->type == EVENT_BREAKPOINT)
949 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200950
951 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200952 && all_stops_accountable(&self->pids))
953 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200954
955 /* Sink all non-exit events. We are about to exit, so we
956 * don't bother with queuing them. */
957 if (event_exit_or_none_p(event))
958 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200959
Petr Machata13d5df72011-08-19 23:15:15 +0200960 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200961}
962
963static void
Petr Machata366c2f42012-02-09 19:34:36 +0100964ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200965{
966 struct ltrace_exiting_handler * self = (void *)super;
967 free(self->pids.tasks);
968}
969
970static int
971ltrace_exiting_install_handler(Process * proc)
972{
973 /* Only install to leader. */
974 if (proc->leader != proc)
975 return 0;
976
977 /* Perhaps we are already installed, if the user passed
978 * several -p options that are tasks of one process. */
979 if (proc->event_handler != NULL
980 && proc->event_handler->on_event == &ltrace_exiting_on_event)
981 return 0;
982
Petr Machata590c8082011-08-20 22:45:26 +0200983 /* If stopping handler is already present, let it do the
984 * work. */
985 if (proc->event_handler != NULL) {
986 assert(proc->event_handler->on_event
987 == &process_stopping_on_event);
988 struct process_stopping_handler * other
989 = (void *)proc->event_handler;
990 other->exiting = 1;
991 return 0;
992 }
993
Petr Machata602330f2011-07-09 11:15:34 +0200994 struct ltrace_exiting_handler * handler
995 = calloc(sizeof(*handler), 1);
996 if (handler == NULL) {
997 perror("malloc exiting handler");
998 fatal:
999 /* XXXXXXXXXXXXXXXXXXX fixme */
1000 return -1;
1001 }
1002
Petr Machata602330f2011-07-09 11:15:34 +02001003 handler->super.on_event = ltrace_exiting_on_event;
1004 handler->super.destroy = ltrace_exiting_destroy;
1005 install_event_handler(proc->leader, &handler->super);
1006
Petr Machata74132a42012-03-16 02:46:18 +01001007 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001008 &handler->pids) != NULL)
1009 goto fatal;
1010
1011 return 0;
1012}
1013
Petr Machatacbe29c62011-09-27 02:27:58 +02001014/*
1015 * When the traced process vforks, it's suspended until the child
1016 * process calls _exit or exec*. In the meantime, the two share the
1017 * address space.
1018 *
1019 * The child process should only ever call _exit or exec*, but we
1020 * can't count on that (it's not the role of ltrace to policy, but to
1021 * observe). In any case, we will _at least_ have to deal with
1022 * removal of vfork return breakpoint (which we have to smuggle back
1023 * in, so that the parent can see it, too), and introduction of exec*
1024 * return breakpoint. Since we already have both breakpoint actions
1025 * to deal with, we might as well support it all.
1026 *
1027 * The gist is that we pretend that the child is in a thread group
1028 * with its parent, and handle it as a multi-threaded case, with the
1029 * exception that we know that the parent is blocked, and don't
1030 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001031 */
1032
Petr Machata134a1082011-09-27 20:25:58 +02001033struct process_vfork_handler
1034{
Petr Machata366c2f42012-02-09 19:34:36 +01001035 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +02001036 void * bp_addr;
1037};
1038
Petr Machatacbe29c62011-09-27 02:27:58 +02001039static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001040process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001041{
Petr Machata9ace5742012-04-14 02:29:57 +02001042 debug(DEBUG_PROCESS,
1043 "process_vfork_on_event: pid %d; event type %d",
1044 event->proc->pid, event->type);
1045
Petr Machatacbe29c62011-09-27 02:27:58 +02001046 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001047 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001048 assert(self != NULL);
1049
1050 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001051 case EVENT_BREAKPOINT:
1052 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001053 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001054 self->bp_addr = event->e_un.brk_addr;
1055 break;
1056
Petr Machatacbe29c62011-09-27 02:27:58 +02001057 case EVENT_EXIT:
1058 case EVENT_EXIT_SIGNAL:
1059 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001060 /* Smuggle back in the vfork return breakpoint, so
1061 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001062 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001063 sbp = dict_find_entry(event->proc->leader->breakpoints,
1064 self->bp_addr);
1065 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001066 assert(sbp->libsym == NULL);
1067 /* We don't mind failing that, it's not a big
1068 * deal to not display one extra vfork return. */
1069 insert_breakpoint(event->proc->parent,
1070 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001071 }
1072
Petr Machataba9911f2011-09-27 21:09:47 +02001073 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001074
1075 /* Remove the leader that we artificially set up
1076 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001077 change_process_leader(event->proc, event->proc);
1078 destroy_event_handler(event->proc);
1079
Petr Machatacbe29c62011-09-27 02:27:58 +02001080 default:
1081 ;
1082 }
1083
1084 return event;
1085}
1086
1087void
1088continue_after_vfork(Process * proc)
1089{
1090 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001091 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001092 if (handler == NULL) {
1093 perror("malloc vfork handler");
1094 /* Carry on not bothering to treat the process as
1095 * necessary. */
1096 continue_process(proc->parent->pid);
1097 return;
1098 }
1099
1100 /* We must set up custom event handler, so that we see
1101 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001102 handler->super.on_event = process_vfork_on_event;
1103 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001104
1105 /* Make sure that the child is sole thread. */
1106 assert(proc->leader == proc);
1107 assert(proc->next == NULL || proc->next->leader != proc);
1108
1109 /* Make sure that the child's parent is properly set up. */
1110 assert(proc->parent != NULL);
1111 assert(proc->parent->leader != NULL);
1112
1113 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001114}
1115
Petr Machata9d29b3e2011-11-09 16:46:56 +01001116static int
1117is_mid_stopping(Process *proc)
1118{
1119 return proc != NULL
1120 && proc->event_handler != NULL
1121 && proc->event_handler->on_event == &process_stopping_on_event;
1122}
1123
Petr Machata43d2fe52011-11-02 13:25:49 +01001124void
1125continue_after_syscall(Process * proc, int sysnum, int ret_p)
1126{
1127 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001128 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1129 debug(DEBUG_PROCESS,
1130 "continue_after_syscall: don't continue %d",
1131 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001132 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001133 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001134 continue_process(proc->pid);
1135}
1136
Petr Machata602330f2011-07-09 11:15:34 +02001137/* If ltrace gets SIGINT, the processes directly or indirectly run by
1138 * ltrace get it too. We just have to wait long enough for the signal
1139 * to be delivered and the process terminated, which we notice and
1140 * exit ltrace, too. So there's not much we need to do there. We
1141 * want to keep tracing those processes as usual, in case they just
1142 * SIG_IGN the SIGINT to do their shutdown etc.
1143 *
1144 * For processes ran on the background, we want to install an exit
1145 * handler that stops all the threads, removes all breakpoints, and
1146 * detaches.
1147 */
1148void
Petr Machataffe4cd22012-04-11 18:01:44 +02001149os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001150{
1151 struct opt_p_t * it;
1152 for (it = opt_p; it != NULL; it = it->next) {
1153 Process * proc = pid2proc(it->pid);
1154 if (proc == NULL || proc->leader == NULL)
1155 continue;
1156 if (ltrace_exiting_install_handler(proc->leader) < 0)
1157 fprintf(stderr,
1158 "Couldn't install exiting handler for %d.\n",
1159 proc->pid);
1160 }
1161}
1162
Petr Machataffe4cd22012-04-11 18:01:44 +02001163int
1164os_ltrace_exiting_sighandler(void)
1165{
1166 extern int linux_in_waitpid;
1167 if (linux_in_waitpid) {
1168 os_ltrace_exiting();
1169 return 1;
1170 }
1171 return 0;
1172}
1173
Joe Damatodfa3fa32010-11-08 15:47:35 -08001174size_t
1175umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1176
1177 union {
1178 long a;
1179 char c[sizeof(long)];
1180 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001181 int started = 0;
1182 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001183
1184 while (offset < len) {
1185 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1186 if (a.a == -1 && errno) {
1187 if (started && errno == EIO)
1188 return bytes_read;
1189 else
1190 return -1;
1191 }
1192 started = 1;
1193
1194 if (len - offset >= sizeof(long)) {
1195 memcpy(laddr + offset, &a.c[0], sizeof(long));
1196 bytes_read += sizeof(long);
1197 }
1198 else {
1199 memcpy(laddr + offset, &a.c[0], len - offset);
1200 bytes_read += (len - offset);
1201 }
1202 offset += sizeof(long);
1203 }
1204
1205 return bytes_read;
1206}