blob: bd5d8261794aab3a12310b16d4ec0b35e71458bf [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 Machata55ed83b2007-05-17 16:24:15 +020022
23/* If the system headers did not provide the constants, hard-code the normal
24 values. */
25#ifndef PTRACE_EVENT_FORK
26
27#define PTRACE_OLDSETOPTIONS 21
28#define PTRACE_SETOPTIONS 0x4200
29#define PTRACE_GETEVENTMSG 0x4201
30
31/* options set using PTRACE_SETOPTIONS */
32#define PTRACE_O_TRACESYSGOOD 0x00000001
33#define PTRACE_O_TRACEFORK 0x00000002
34#define PTRACE_O_TRACEVFORK 0x00000004
35#define PTRACE_O_TRACECLONE 0x00000008
36#define PTRACE_O_TRACEEXEC 0x00000010
37#define PTRACE_O_TRACEVFORKDONE 0x00000020
38#define PTRACE_O_TRACEEXIT 0x00000040
39
40/* Wait extended result codes for the above trace options. */
41#define PTRACE_EVENT_FORK 1
42#define PTRACE_EVENT_VFORK 2
43#define PTRACE_EVENT_CLONE 3
44#define PTRACE_EVENT_EXEC 4
45#define PTRACE_EVENT_VFORK_DONE 5
46#define PTRACE_EVENT_EXIT 6
47
48#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010049
Luis Machado55c5feb2008-03-12 15:56:01 +010050#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020051extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010052int
Juan Cespedesa8909f72009-04-28 20:02:41 +020053umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010054 return arch_umovelong (proc, addr, result, info);
55}
56#else
57/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010058int
Juan Cespedesa8909f72009-04-28 20:02:41 +020059umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010060 long pointed_to;
61
62 errno = 0;
63 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
64 if (pointed_to == -1 && errno)
65 return -errno;
66
67 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050068 if (info) {
69 switch(info->type) {
70 case ARGTYPE_INT:
71 *result &= 0x00000000ffffffffUL;
72 default:
73 break;
74 };
75 }
Luis Machado55c5feb2008-03-12 15:56:01 +010076 return 0;
77}
78#endif
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080void
Petr Machatacec06ec2012-04-10 13:31:55 +020081trace_fail_warning(pid_t pid)
82{
83 /* This was adapted from GDB. */
84#ifdef HAVE_LIBSELINUX
85 static int checked = 0;
86 if (checked)
87 return;
88 checked = 1;
89
90 /* -1 is returned for errors, 0 if it has no effect, 1 if
91 * PTRACE_ATTACH is forbidden. */
92 if (security_get_boolean_active("deny_ptrace") == 1)
93 fprintf(stderr,
94"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
95"tracing other processes. You can disable this process attach protection by\n"
96"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
97#endif /* HAVE_LIBSELINUX */
98}
99
100void
101trace_me(void)
102{
Petr Machata26627682011-07-08 18:15:32 +0200103 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +0200104 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100105 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200106 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107 exit(1);
108 }
109}
110
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100111/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100112 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100113 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100114int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100115wait_for_proc(pid_t pid)
116{
Petr Machatac805c622012-03-02 00:10:37 +0100117 /* man ptrace: PTRACE_ATTACH attaches to the process specified
118 in pid. The child is sent a SIGSTOP, but will not
119 necessarily have stopped by the completion of this call;
120 use wait() to wait for the child to stop. */
121 if (waitpid(pid, NULL, __WALL) != pid) {
122 perror ("trace_pid: waitpid");
123 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100124 }
125
Petr Machatac805c622012-03-02 00:10:37 +0100126 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100127}
128
Juan Cespedesf1350522008-12-16 18:19:58 +0100129int
Petr Machatacec06ec2012-04-10 13:31:55 +0200130trace_pid(pid_t pid)
131{
Petr Machata26627682011-07-08 18:15:32 +0200132 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200133 /* This shouldn't emit error messages, as there are legitimate
134 * reasons that the PID can't be attached: like it may have
135 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200136 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100137 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100138
Petr Machatac805c622012-03-02 00:10:37 +0100139 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100140}
141
Juan Cespedesf1350522008-12-16 18:19:58 +0100142void
Petr Machata3ed2a422012-04-06 17:18:55 +0200143trace_set_options(struct Process *proc)
144{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100145 if (proc->tracesysgood & 0x80)
146 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200147
Petr Machata3ed2a422012-04-06 17:18:55 +0200148 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200149 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200150
Juan Cespedes1e583132009-04-07 18:17:11 +0200151 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
152 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
153 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200154 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
155 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100156 perror("PTRACE_SETOPTIONS");
157 return;
158 }
159 proc->tracesysgood |= 0x80;
160}
161
Juan Cespedesf1350522008-12-16 18:19:58 +0100162void
163untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200164 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200165 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100166}
167
Juan Cespedesf1350522008-12-16 18:19:58 +0100168void
Petr Machatac897cb72012-05-05 01:26:58 +0200169continue_after_signal(pid_t pid, int signum)
170{
171 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
172 pid, signum);
173 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200174}
175
176static enum ecb_status
177event_for_pid(Event * event, void * data)
178{
179 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
180 return ecb_yield;
181 return ecb_cont;
182}
183
184static int
185have_events_for(pid_t pid)
186{
187 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
188}
189
190void
191continue_process(pid_t pid)
192{
193 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200194
195 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200196 the queue for this process. Otherwise just wait for the
197 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200198 if (!have_events_for(pid))
199 /* We always trace syscalls to control fork(),
200 * clone(), execve()... */
201 ptrace(PTRACE_SYSCALL, pid, 0, 0);
202 else
203 debug(DEBUG_PROCESS,
204 "putting off the continue, events in que.");
205}
206
Petr Machata98f09922011-07-09 10:55:29 +0200207static struct pid_task *
208get_task_info(struct pid_set * pids, pid_t pid)
209{
Petr Machata750ca8c2011-10-06 14:29:34 +0200210 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200211 size_t i;
212 for (i = 0; i < pids->count; ++i)
213 if (pids->tasks[i].pid == pid)
214 return &pids->tasks[i];
215
216 return NULL;
217}
218
219static struct pid_task *
220add_task_info(struct pid_set * pids, pid_t pid)
221{
222 if (pids->count == pids->alloc) {
223 size_t ns = (2 * pids->alloc) ?: 4;
224 struct pid_task * n = realloc(pids->tasks,
225 sizeof(*pids->tasks) * ns);
226 if (n == NULL)
227 return NULL;
228 pids->tasks = n;
229 pids->alloc = ns;
230 }
231 struct pid_task * task_info = &pids->tasks[pids->count++];
232 memset(task_info, 0, sizeof(*task_info));
233 task_info->pid = pid;
234 return task_info;
235}
236
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237static enum callback_status
238task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200239{
240 enum process_status st = process_status(task->pid);
241 if (data != NULL)
242 *(enum process_status *)data = st;
243
244 /* If the task is already stopped, don't worry about it.
245 * Likewise if it managed to become a zombie or terminate in
246 * the meantime. This can happen when the whole thread group
247 * is terminating. */
248 switch (st) {
249 case ps_invalid:
250 case ps_tracing_stop:
251 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200253 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200254 case ps_stop:
255 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200257 }
Petr Machata36d19822011-10-21 16:03:45 +0200258
259 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200260}
261
262/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263static enum callback_status
264task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200265{
266 struct pid_set * pids = data;
267 struct pid_task * task_info = get_task_info(pids, task->pid);
268 if (task_info != NULL
269 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200271
272 return task_stopped(task, NULL);
273}
274
Petr Machata366c2f42012-02-09 19:34:36 +0100275static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200276
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277static enum callback_status
278task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200279{
280 if (task->event_handler != NULL
281 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100282 return CBS_STOP;
283 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200284}
285
286static int
Petr Machata74132a42012-03-16 02:46:18 +0100287is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200288{
Petr Machata74132a42012-03-16 02:46:18 +0100289 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200290}
291
Petr Machata2b46cfc2012-02-18 11:17:29 +0100292static enum callback_status
293send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200294{
295 Process * leader = task->leader;
296 struct pid_set * pids = data;
297
298 /* Look for pre-existing task record, or add new. */
299 struct pid_task * task_info = get_task_info(pids, task->pid);
300 if (task_info == NULL)
301 task_info = add_task_info(pids, task->pid);
302 if (task_info == NULL) {
303 perror("send_sigstop: add_task_info");
304 destroy_event_handler(leader);
305 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100306 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200307 }
308
309 /* This task still has not been attached to. It should be
310 stopped by the kernel. */
311 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200313
314 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200315 * if we sent the SIGSTOP already, which happens when we are
316 * handling "onexit" and inherited the handler from breakpoint
317 * re-enablement. */
318 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319 if (task_stopped(task, &st) == CBS_CONT)
320 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200321 if (task_info->sigstopped) {
322 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100323 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200324 task_info->delivered = 0;
325 }
326
Petr Machatacbe29c62011-09-27 02:27:58 +0200327 /* Also don't attempt to stop the process if it's a parent of
328 * vforked process. We set up event handler specially to hint
329 * us. In that case parent is in D state, which we use to
330 * weed out unnecessary looping. */
331 if (st == ps_sleeping
332 && is_vfork_parent (task)) {
333 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200335 }
336
Petr Machata98f09922011-07-09 10:55:29 +0200337 if (task_kill(task->pid, SIGSTOP) >= 0) {
338 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
339 task_info->sigstopped = 1;
340 } else
341 fprintf(stderr,
342 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
343
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200345}
346
Petr Machata73894bd2011-08-20 23:47:34 +0200347/* On certain kernels, detaching right after a singlestep causes the
348 tracee to be killed with a SIGTRAP (that even though the singlestep
349 was properly caught by waitpid. The ugly workaround is to put a
350 breakpoint where IP points and let the process continue. After
351 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200352static void
Petr Machata73894bd2011-08-20 23:47:34 +0200353ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200354{
355 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100356 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200357 if (sbp != NULL)
358 enable_breakpoint(proc, sbp);
359 else
Petr Machata9df15012012-02-20 12:49:46 +0100360 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200361 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200362}
363
364static void
Petr Machata98f09922011-07-09 10:55:29 +0200365process_stopping_done(struct process_stopping_handler * self, Process * leader)
366{
367 debug(DEBUG_PROCESS, "process stopping done %d",
368 self->task_enabling_breakpoint->pid);
369 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200370 if (!self->exiting) {
371 for (i = 0; i < self->pids.count; ++i)
372 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100373 && (self->pids.tasks[i].delivered
374 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200375 continue_process(self->pids.tasks[i].pid);
376 continue_process(self->task_enabling_breakpoint->pid);
377 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200378 }
379
380 if (self->exiting) {
381 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200382 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200383 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200384 } else {
385 switch ((self->ugly_workaround_p)(self)) {
386 case CBS_FAIL:
387 /* xxx handle me */
388 case CBS_STOP:
389 break;
390 case CBS_CONT:
391 goto ugly_workaround;
392 }
Petr Machata590c8082011-08-20 22:45:26 +0200393 }
394}
395
396/* Before we detach, we need to make sure that task's IP is on the
397 * edge of an instruction. So for tasks that have a breakpoint event
398 * in the queue, we adjust the instruction pointer, just like
399 * continue_after_breakpoint does. */
400static enum ecb_status
401undo_breakpoint(Event * event, void * data)
402{
403 if (event != NULL
404 && event->proc->leader == data
405 && event->type == EVENT_BREAKPOINT)
406 set_instruction_pointer(event->proc, event->e_un.brk_addr);
407 return ecb_cont;
408}
409
Petr Machata2b46cfc2012-02-18 11:17:29 +0100410static enum callback_status
411untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200412{
413 if (task != data)
414 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100415 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200416}
417
Petr Machata2b46cfc2012-02-18 11:17:29 +0100418static enum callback_status
419remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200420{
421 /* Don't untrace leader just yet. */
422 if (task != data)
423 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100424 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200425}
426
Petr Machata86d38282012-04-24 18:09:01 +0200427static enum callback_status
428retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
429{
430 breakpoint_on_retract(bp, proc);
431 return CBS_CONT;
432}
433
Petr Machata590c8082011-08-20 22:45:26 +0200434static void
435detach_process(Process * leader)
436{
437 each_qd_event(&undo_breakpoint, leader);
438 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200439 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200440
441 /* Now untrace the process, if it was attached to by -p. */
442 struct opt_p_t * it;
443 for (it = opt_p; it != NULL; it = it->next) {
444 Process * proc = pid2proc(it->pid);
445 if (proc == NULL)
446 continue;
447 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100448 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200449 break;
450 }
451 }
Petr Machata74132a42012-03-16 02:46:18 +0100452 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200453 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200454 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200455}
456
457static void
458handle_stopping_event(struct pid_task * task_info, Event ** eventp)
459{
460 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200461 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200462 task_info->got_event = 1;
463
464 Event * event = *eventp;
465
466 /* In every state, sink SIGSTOP events for tasks that it was
467 * sent to. */
468 if (task_info != NULL
469 && event->type == EVENT_SIGNAL
470 && event->e_un.signum == SIGSTOP) {
471 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
472 if (task_info->sigstopped
473 && !task_info->delivered) {
474 task_info->delivered = 1;
475 *eventp = NULL; // sink the event
476 } else
477 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
478 "sigstopped=%d and delivered=%d\n",
479 task_info->pid, task_info->sigstopped,
480 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100481 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100482}
483
Petr Machata98f09922011-07-09 10:55:29 +0200484/* Some SIGSTOPs may have not been delivered to their respective tasks
485 * yet. They are still in the queue. If we have seen an event for
486 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200487 * caught by ltrace. We don't mind that the process is after
488 * breakpoint (and therefore potentially doesn't have aligned IP),
489 * because the signal will be delivered without the process actually
490 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200491static void
492continue_for_sigstop_delivery(struct pid_set * pids)
493{
494 size_t i;
495 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200496 if (pids->tasks[i].pid != 0
497 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200498 && !pids->tasks[i].delivered
499 && pids->tasks[i].got_event) {
500 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
501 pids->tasks[i].pid);
502 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
503 }
504 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100505}
506
Petr Machata98f09922011-07-09 10:55:29 +0200507static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200508event_exit_p(Event * event)
509{
510 return event != NULL && (event->type == EVENT_EXIT
511 || event->type == EVENT_EXIT_SIGNAL);
512}
513
514static int
Petr Machata98f09922011-07-09 10:55:29 +0200515event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200516{
Petr Machata750ca8c2011-10-06 14:29:34 +0200517 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200518 || event->type == EVENT_NONE;
519}
520
521static int
522await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
523 Event * event)
524{
525 /* If we still didn't get our SIGSTOP, continue the process
526 * and carry on. */
527 if (event != NULL && !event_exit_or_none_p(event)
528 && task_info != NULL && task_info->sigstopped) {
529 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
530 task_info->pid);
531 /* We should get the signal the first thing
532 * after this, so it should be OK to continue
533 * even if we are over a breakpoint. */
534 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
535
536 } else {
537 /* If all SIGSTOPs were delivered, uninstall the
538 * handler and continue everyone. */
539 /* XXX I suspect that we should check tasks that are
540 * still around. Is things are now, there should be a
541 * race between waiting for everyone to stop and one
542 * of the tasks exiting. */
543 int all_clear = 1;
544 size_t i;
545 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200546 if (pids->tasks[i].pid != 0
547 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200548 && !pids->tasks[i].delivered) {
549 all_clear = 0;
550 break;
551 }
552 return all_clear;
553 }
554
555 return 0;
556}
557
Petr Machata590c8082011-08-20 22:45:26 +0200558static int
559all_stops_accountable(struct pid_set * pids)
560{
561 size_t i;
562 for (i = 0; i < pids->count; ++i)
563 if (pids->tasks[i].pid != 0
564 && !pids->tasks[i].got_event
565 && !have_events_for(pids->tasks[i].pid))
566 return 0;
567 return 1;
568}
569
Petr Machataa266acb2012-04-12 23:50:23 +0200570/* The protocol is: 0 for success, negative for failure, positive if
571 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200572int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200573 int (*add_cb)(void *addr, void *data),
574 void *add_cb_data);
575
576#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
577int
Petr Machata9e1e9692012-04-19 02:29:38 +0200578arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200579 int (*add_cb)(void *addr, void *data),
580 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100581{
Petr Machataa266acb2012-04-12 23:50:23 +0200582 return 1;
583}
584#endif
585
Petr Machata42748ac2012-04-19 04:24:25 +0200586static Event *process_stopping_on_event(struct event_handler *super,
587 Event *event);
588
589static void
590remove_atomic_breakpoints(struct Process *proc)
591{
Petr Machata24a47042012-04-19 18:15:08 +0200592 struct process_stopping_handler *self
593 = (void *)proc->leader->event_handler;
594 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200595 assert(self->super.on_event == process_stopping_on_event);
596
597 int ct = sizeof(self->atomic_skip_bp_addrs)
598 / sizeof(*self->atomic_skip_bp_addrs);
599 int i;
600 for (i = 0; i < ct; ++i)
601 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200602 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200603 self->atomic_skip_bp_addrs[i] = 0;
604 }
605}
606
607static void
608atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
609{
610 remove_atomic_breakpoints(proc);
611}
612
Petr Machataa266acb2012-04-12 23:50:23 +0200613static int
614atomic_singlestep_add_bp(void *addr, void *data)
615{
616 struct process_stopping_handler *self = data;
617 struct Process *proc = self->task_enabling_breakpoint;
618
Petr Machata42748ac2012-04-19 04:24:25 +0200619 int ct = sizeof(self->atomic_skip_bp_addrs)
620 / sizeof(*self->atomic_skip_bp_addrs);
621 int i;
622 for (i = 0; i < ct; ++i)
623 if (self->atomic_skip_bp_addrs[i] == 0) {
624 self->atomic_skip_bp_addrs[i] = addr;
625 static struct bp_callbacks cbs = {
626 .on_hit = atomic_singlestep_bp_on_hit,
627 };
628 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200629 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200630 breakpoint_set_callbacks(bp, &cbs);
631 return 0;
632 }
Petr Machataa266acb2012-04-12 23:50:23 +0200633
Petr Machata42748ac2012-04-19 04:24:25 +0200634 assert(!"Too many atomic singlestep breakpoints!");
635 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200636}
637
638static int
639singlestep(struct process_stopping_handler *self)
640{
641 struct Process *proc = self->task_enabling_breakpoint;
642
643 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
644 self->breakpoint_being_enabled,
645 &atomic_singlestep_add_bp, self);
646
647 /* Propagate failure and success. */
648 if (status <= 0)
649 return status;
650
651 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100652 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200653 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100654 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200655 return -1;
656 }
657 return 0;
658}
659
660static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200661post_singlestep(struct process_stopping_handler *self,
662 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200663{
664 continue_for_sigstop_delivery(&self->pids);
665
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200666 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200667 *eventp = NULL; // handled
668
Petr Machata42748ac2012-04-19 04:24:25 +0200669 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200670
Petr Machata42748ac2012-04-19 04:24:25 +0200671 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200672 self->breakpoint_being_enabled = NULL;
673}
674
675static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200676singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200677{
678 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200679 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200680 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataa266acb2012-04-12 23:50:23 +0200681 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200682 sbp->addr, get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200683 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100684}
685
Petr Machata9e1e9692012-04-19 02:29:38 +0200686static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200687pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200688{
689 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200690 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200691 ptrace(PTRACE_CONT, teb->pid, 0, 0);
692}
693
694static void
695pt_singlestep(struct process_stopping_handler *self)
696{
697 if (singlestep(self) < 0)
698 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200699}
700
701static void
702disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200703 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200704{
705 struct Process *teb = self->task_enabling_breakpoint;
706 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200707 if (self->breakpoint_being_enabled->enabled)
708 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200709 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200710 self->state = psh_singlestep;
711}
712
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200713void
714linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
715{
Petr Machata9e1e9692012-04-19 02:29:38 +0200716 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200717}
718
719void
720linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
721{
722 disable_and(self, &pt_continue);
723}
724
Petr Machata98f09922011-07-09 10:55:29 +0200725/* This event handler is installed when we are in the process of
726 * stopping the whole thread group to do the pointer re-enablement for
727 * one of the threads. We pump all events to the queue for later
728 * processing while we wait for all the threads to stop. When this
729 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
730 * re-enable, and continue everyone. */
731static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100732process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200733{
734 struct process_stopping_handler * self = (void *)super;
735 Process * task = event->proc;
736 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200737 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200738
739 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200740 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200741 task->pid, event->type, self->state);
742
743 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
744 if (task_info == NULL)
745 fprintf(stderr, "new task??? %d\n", task->pid);
746 handle_stopping_event(task_info, &event);
747
748 int state = self->state;
749 int event_to_queue = !event_exit_or_none_p(event);
750
Petr Machata18c97072011-10-06 14:30:11 +0200751 /* Deactivate the entry if the task exits. */
752 if (event_exit_p(event) && task_info != NULL)
753 task_info->pid = 0;
754
Petr Machata43d2fe52011-11-02 13:25:49 +0100755 /* Always handle sysrets. Whether sysret occurred and what
756 * sys it rets from may need to be determined based on process
757 * stack, so we need to keep that in sync with reality. Note
758 * that we don't continue the process after the sysret is
759 * handled. See continue_after_syscall. */
760 if (event != NULL && event->type == EVENT_SYSRET) {
761 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
762 event_to_queue = 0;
763 task_info->sysret = 1;
764 }
765
Petr Machata98f09922011-07-09 10:55:29 +0200766 switch (state) {
767 case psh_stopping:
768 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100769 if (each_task(leader, NULL, &task_blocked,
770 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200771 (self->on_all_stopped)(self);
772 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200773 }
774 break;
775
Petr Machata06986d52011-11-02 13:22:46 +0100776 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200777 /* In singlestep state, breakpoint signifies that we
778 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200779 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200780
Petr Machata42748ac2012-04-19 04:24:25 +0200781 /* If this was caused by a real breakpoint, as
782 * opposed to a singlestep, assume that it's
783 * an artificial breakpoint installed for some
784 * reason for the re-enablement. In that case
785 * handle it. */
786 if (event->type == EVENT_BREAKPOINT) {
787 target_address_t ip
788 = get_instruction_pointer(task);
789 struct breakpoint *other
790 = address2bpstruct(leader, ip);
791 if (other != NULL)
792 breakpoint_on_hit(other, task);
793 }
794
Petr Machata36f40e72012-03-28 02:07:19 +0200795 /* If we got SIGNAL instead of BREAKPOINT,
796 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200797 if (event->type == EVENT_SIGNAL) {
798 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200799 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200800 singlestep_error(self);
801 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200802 goto psh_sinking;
803 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200804 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200805 } else {
806 switch ((self->keep_stepping_p)(self)) {
807 case CBS_FAIL:
808 /* XXX handle me */
809 case CBS_STOP:
810 break;
811 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200812 /* Sink singlestep event. */
813 if (event->type == EVENT_BREAKPOINT)
814 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200815 goto do_singlestep;
816 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200817 }
818
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200819 /* Re-enable the breakpoint that we are
820 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200821 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200822 if (sbp->enabled)
823 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200824
Petr Machataa266acb2012-04-12 23:50:23 +0200825 post_singlestep(self, &event);
826 goto psh_sinking;
827 }
828 break;
Petr Machata98f09922011-07-09 10:55:29 +0200829
Petr Machataa266acb2012-04-12 23:50:23 +0200830 psh_sinking:
831 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200832 case psh_sinking:
833 if (await_sigstop_delivery(&self->pids, task_info, event))
834 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200835 break;
836
837 case psh_ugly_workaround:
838 if (event == NULL)
839 break;
840 if (event->type == EVENT_BREAKPOINT) {
841 undo_breakpoint(event, leader);
842 if (task == teb)
843 self->task_enabling_breakpoint = NULL;
844 }
845 if (self->task_enabling_breakpoint == NULL
846 && all_stops_accountable(&self->pids)) {
847 undo_breakpoint(event, leader);
848 detach_process(leader);
849 event = NULL; // handled
850 }
Petr Machata98f09922011-07-09 10:55:29 +0200851 }
852
853 if (event != NULL && event_to_queue) {
854 enque_event(event);
855 event = NULL; // sink the event
856 }
857
858 return event;
859}
860
861static void
Petr Machata366c2f42012-02-09 19:34:36 +0100862process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200863{
864 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200865 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100866}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100867
Petr Machata36f40e72012-03-28 02:07:19 +0200868static enum callback_status
869no(struct process_stopping_handler *self)
870{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200871 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200872}
873
Petr Machatac1aa9582012-03-27 23:54:01 +0200874int
875process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200876 void (*as)(struct process_stopping_handler *),
877 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200878 (struct process_stopping_handler *),
879 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200880 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200881{
Petr Machata9ace5742012-04-14 02:29:57 +0200882 debug(DEBUG_FUNCTION,
883 "process_install_stopping_handler: pid=%d", proc->pid);
884
Petr Machatac1aa9582012-03-27 23:54:01 +0200885 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
886 if (handler == NULL)
887 return -1;
888
Petr Machata36f40e72012-03-28 02:07:19 +0200889 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200890 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200891 if (ks == NULL)
892 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200893 if (uw == NULL)
894 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200895
Petr Machatac1aa9582012-03-27 23:54:01 +0200896 handler->super.on_event = process_stopping_on_event;
897 handler->super.destroy = process_stopping_destroy;
898 handler->task_enabling_breakpoint = proc;
899 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200900 handler->on_all_stopped = as;
901 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200902 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200903
904 install_event_handler(proc->leader, &handler->super);
905
906 if (each_task(proc->leader, NULL, &send_sigstop,
907 &handler->pids) != NULL) {
908 destroy_event_handler(proc);
909 return -1;
910 }
911
912 /* And deliver the first fake event, in case all the
913 * conditions are already fulfilled. */
914 Event ev = {
915 .type = EVENT_NONE,
916 .proc = proc,
917 };
918 process_stopping_on_event(&handler->super, &ev);
919
920 return 0;
921}
922
Juan Cespedesf1350522008-12-16 18:19:58 +0100923void
Petr Machatabc373262012-02-07 23:31:15 +0100924continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200925{
Petr Machata9ace5742012-04-14 02:29:57 +0200926 debug(DEBUG_PROCESS,
927 "continue_after_breakpoint: pid=%d, addr=%p",
928 proc->pid, sbp->addr);
929
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200930 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200931
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100932 if (sbp->enabled == 0) {
933 continue_process(proc->pid);
934 } else {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500935#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100936 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200937 continue_process(proc->pid);
938#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200939 if (process_install_stopping_handler
940 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200941 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200942 /* Carry on not bothering to re-enable. */
943 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200944 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200945#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100946 }
947}
948
Petr Machata602330f2011-07-09 11:15:34 +0200949/**
950 * Ltrace exit. When we are about to exit, we have to go through all
951 * the processes, stop them all, remove all the breakpoints, and then
952 * detach the processes that we attached to using -p. If we left the
953 * other tasks running, they might hit stray return breakpoints and
954 * produce artifacts, so we better stop everyone, even if it's a bit
955 * of extra work.
956 */
957struct ltrace_exiting_handler
958{
Petr Machata366c2f42012-02-09 19:34:36 +0100959 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200960 struct pid_set pids;
961};
962
Petr Machata602330f2011-07-09 11:15:34 +0200963static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100964ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200965{
966 struct ltrace_exiting_handler * self = (void *)super;
967 Process * task = event->proc;
968 Process * leader = task->leader;
969
Petr Machata9ace5742012-04-14 02:29:57 +0200970 debug(DEBUG_PROCESS,
971 "ltrace_exiting_on_event: pid %d; event type %d",
972 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200973
974 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
975 handle_stopping_event(task_info, &event);
976
Petr Machata590c8082011-08-20 22:45:26 +0200977 if (event != NULL && event->type == EVENT_BREAKPOINT)
978 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200979
980 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200981 && all_stops_accountable(&self->pids))
982 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200983
984 /* Sink all non-exit events. We are about to exit, so we
985 * don't bother with queuing them. */
986 if (event_exit_or_none_p(event))
987 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200988
Petr Machata13d5df72011-08-19 23:15:15 +0200989 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200990}
991
992static void
Petr Machata366c2f42012-02-09 19:34:36 +0100993ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200994{
995 struct ltrace_exiting_handler * self = (void *)super;
996 free(self->pids.tasks);
997}
998
999static int
1000ltrace_exiting_install_handler(Process * proc)
1001{
1002 /* Only install to leader. */
1003 if (proc->leader != proc)
1004 return 0;
1005
1006 /* Perhaps we are already installed, if the user passed
1007 * several -p options that are tasks of one process. */
1008 if (proc->event_handler != NULL
1009 && proc->event_handler->on_event == &ltrace_exiting_on_event)
1010 return 0;
1011
Petr Machata590c8082011-08-20 22:45:26 +02001012 /* If stopping handler is already present, let it do the
1013 * work. */
1014 if (proc->event_handler != NULL) {
1015 assert(proc->event_handler->on_event
1016 == &process_stopping_on_event);
1017 struct process_stopping_handler * other
1018 = (void *)proc->event_handler;
1019 other->exiting = 1;
1020 return 0;
1021 }
1022
Petr Machata602330f2011-07-09 11:15:34 +02001023 struct ltrace_exiting_handler * handler
1024 = calloc(sizeof(*handler), 1);
1025 if (handler == NULL) {
1026 perror("malloc exiting handler");
1027 fatal:
1028 /* XXXXXXXXXXXXXXXXXXX fixme */
1029 return -1;
1030 }
1031
Petr Machata602330f2011-07-09 11:15:34 +02001032 handler->super.on_event = ltrace_exiting_on_event;
1033 handler->super.destroy = ltrace_exiting_destroy;
1034 install_event_handler(proc->leader, &handler->super);
1035
Petr Machata74132a42012-03-16 02:46:18 +01001036 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001037 &handler->pids) != NULL)
1038 goto fatal;
1039
1040 return 0;
1041}
1042
Petr Machatacbe29c62011-09-27 02:27:58 +02001043/*
1044 * When the traced process vforks, it's suspended until the child
1045 * process calls _exit or exec*. In the meantime, the two share the
1046 * address space.
1047 *
1048 * The child process should only ever call _exit or exec*, but we
1049 * can't count on that (it's not the role of ltrace to policy, but to
1050 * observe). In any case, we will _at least_ have to deal with
1051 * removal of vfork return breakpoint (which we have to smuggle back
1052 * in, so that the parent can see it, too), and introduction of exec*
1053 * return breakpoint. Since we already have both breakpoint actions
1054 * to deal with, we might as well support it all.
1055 *
1056 * The gist is that we pretend that the child is in a thread group
1057 * with its parent, and handle it as a multi-threaded case, with the
1058 * exception that we know that the parent is blocked, and don't
1059 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001060 */
1061
Petr Machata134a1082011-09-27 20:25:58 +02001062struct process_vfork_handler
1063{
Petr Machata366c2f42012-02-09 19:34:36 +01001064 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +02001065 void * bp_addr;
1066};
1067
Petr Machatacbe29c62011-09-27 02:27:58 +02001068static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001069process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001070{
Petr Machata9ace5742012-04-14 02:29:57 +02001071 debug(DEBUG_PROCESS,
1072 "process_vfork_on_event: pid %d; event type %d",
1073 event->proc->pid, event->type);
1074
Petr Machatacbe29c62011-09-27 02:27:58 +02001075 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001076 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001077 assert(self != NULL);
1078
1079 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001080 case EVENT_BREAKPOINT:
1081 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001082 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001083 self->bp_addr = event->e_un.brk_addr;
1084 break;
1085
Petr Machatacbe29c62011-09-27 02:27:58 +02001086 case EVENT_EXIT:
1087 case EVENT_EXIT_SIGNAL:
1088 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001089 /* Smuggle back in the vfork return breakpoint, so
1090 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001091 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001092 sbp = dict_find_entry(event->proc->leader->breakpoints,
1093 self->bp_addr);
1094 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001095 assert(sbp->libsym == NULL);
1096 /* We don't mind failing that, it's not a big
1097 * deal to not display one extra vfork return. */
1098 insert_breakpoint(event->proc->parent,
1099 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001100 }
1101
Petr Machataba9911f2011-09-27 21:09:47 +02001102 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001103
1104 /* Remove the leader that we artificially set up
1105 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001106 change_process_leader(event->proc, event->proc);
1107 destroy_event_handler(event->proc);
1108
Petr Machatacbe29c62011-09-27 02:27:58 +02001109 default:
1110 ;
1111 }
1112
1113 return event;
1114}
1115
1116void
1117continue_after_vfork(Process * proc)
1118{
1119 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001120 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001121 if (handler == NULL) {
1122 perror("malloc vfork handler");
1123 /* Carry on not bothering to treat the process as
1124 * necessary. */
1125 continue_process(proc->parent->pid);
1126 return;
1127 }
1128
1129 /* We must set up custom event handler, so that we see
1130 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001131 handler->super.on_event = process_vfork_on_event;
1132 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001133
1134 /* Make sure that the child is sole thread. */
1135 assert(proc->leader == proc);
1136 assert(proc->next == NULL || proc->next->leader != proc);
1137
1138 /* Make sure that the child's parent is properly set up. */
1139 assert(proc->parent != NULL);
1140 assert(proc->parent->leader != NULL);
1141
1142 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001143}
1144
Petr Machata9d29b3e2011-11-09 16:46:56 +01001145static int
1146is_mid_stopping(Process *proc)
1147{
1148 return proc != NULL
1149 && proc->event_handler != NULL
1150 && proc->event_handler->on_event == &process_stopping_on_event;
1151}
1152
Petr Machata43d2fe52011-11-02 13:25:49 +01001153void
1154continue_after_syscall(Process * proc, int sysnum, int ret_p)
1155{
1156 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001157 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1158 debug(DEBUG_PROCESS,
1159 "continue_after_syscall: don't continue %d",
1160 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001161 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001162 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001163 continue_process(proc->pid);
1164}
1165
Petr Machata602330f2011-07-09 11:15:34 +02001166/* If ltrace gets SIGINT, the processes directly or indirectly run by
1167 * ltrace get it too. We just have to wait long enough for the signal
1168 * to be delivered and the process terminated, which we notice and
1169 * exit ltrace, too. So there's not much we need to do there. We
1170 * want to keep tracing those processes as usual, in case they just
1171 * SIG_IGN the SIGINT to do their shutdown etc.
1172 *
1173 * For processes ran on the background, we want to install an exit
1174 * handler that stops all the threads, removes all breakpoints, and
1175 * detaches.
1176 */
1177void
Petr Machataffe4cd22012-04-11 18:01:44 +02001178os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001179{
1180 struct opt_p_t * it;
1181 for (it = opt_p; it != NULL; it = it->next) {
1182 Process * proc = pid2proc(it->pid);
1183 if (proc == NULL || proc->leader == NULL)
1184 continue;
1185 if (ltrace_exiting_install_handler(proc->leader) < 0)
1186 fprintf(stderr,
1187 "Couldn't install exiting handler for %d.\n",
1188 proc->pid);
1189 }
1190}
1191
Petr Machataffe4cd22012-04-11 18:01:44 +02001192int
1193os_ltrace_exiting_sighandler(void)
1194{
1195 extern int linux_in_waitpid;
1196 if (linux_in_waitpid) {
1197 os_ltrace_exiting();
1198 return 1;
1199 }
1200 return 0;
1201}
1202
Joe Damatodfa3fa32010-11-08 15:47:35 -08001203size_t
1204umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1205
1206 union {
1207 long a;
1208 char c[sizeof(long)];
1209 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001210 int started = 0;
1211 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001212
1213 while (offset < len) {
1214 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1215 if (a.a == -1 && errno) {
1216 if (started && errno == EIO)
1217 return bytes_read;
1218 else
1219 return -1;
1220 }
1221 started = 1;
1222
1223 if (len - offset >= sizeof(long)) {
1224 memcpy(laddr + offset, &a.c[0], sizeof(long));
1225 bytes_read += sizeof(long);
1226 }
1227 else {
1228 memcpy(laddr + offset, &a.c[0], len - offset);
1229 bytes_read += (len - offset);
1230 }
1231 offset += sizeof(long);
1232 }
1233
1234 return bytes_read;
1235}
1236
Steve Fink7bafff02006-08-07 04:50:42 +02001237/* Read a series of bytes starting at the process's memory address
1238 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1239 have been read.
1240*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001241int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001242umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001243 union {
1244 long a;
1245 char c[sizeof(long)];
1246 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001247 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001248 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001249
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001250 while (offset < len) {
1251 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1252 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001253 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001254 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001255 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001256 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001257 return 0;
1258 }
1259 }
1260 offset += sizeof(long);
1261 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001262 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001263 return 0;
1264}