blob: f573f223e1d88b772a4ea835f7f191900805d048 [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
Luis Machado55c5feb2008-03-12 15:56:01 +010051#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020052extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010053int
Petr Machata000e3112012-01-03 17:03:39 +010054umovelong(Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010055 return arch_umovelong (proc, addr, result, info);
56}
57#else
58/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010059int
Petr Machata000e3112012-01-03 17:03:39 +010060umovelong(Process *proc, void *addr, long *result, struct arg_type_info *info)
61{
Luis Machado55c5feb2008-03-12 15:56:01 +010062 long pointed_to;
63
64 errno = 0;
65 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
66 if (pointed_to == -1 && errno)
67 return -errno;
68
69 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050070 if (info) {
Petr Machata000e3112012-01-03 17:03:39 +010071 switch (info->type) {
Arnaud Patardf16fcff2010-01-08 08:40:19 -050072 case ARGTYPE_INT:
73 *result &= 0x00000000ffffffffUL;
74 default:
75 break;
76 };
77 }
Luis Machado55c5feb2008-03-12 15:56:01 +010078 return 0;
79}
80#endif
81
Juan Cespedesf1350522008-12-16 18:19:58 +010082void
Petr Machatacec06ec2012-04-10 13:31:55 +020083trace_fail_warning(pid_t pid)
84{
85 /* This was adapted from GDB. */
86#ifdef HAVE_LIBSELINUX
87 static int checked = 0;
88 if (checked)
89 return;
90 checked = 1;
91
92 /* -1 is returned for errors, 0 if it has no effect, 1 if
93 * PTRACE_ATTACH is forbidden. */
94 if (security_get_boolean_active("deny_ptrace") == 1)
95 fprintf(stderr,
96"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
97"tracing other processes. You can disable this process attach protection by\n"
98"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
99#endif /* HAVE_LIBSELINUX */
100}
101
102void
103trace_me(void)
104{
Petr Machata26627682011-07-08 18:15:32 +0200105 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +0200106 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200108 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100109 exit(1);
110 }
111}
112
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100113/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100114 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100115 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100116int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100117wait_for_proc(pid_t pid)
118{
Petr Machatac805c622012-03-02 00:10:37 +0100119 /* man ptrace: PTRACE_ATTACH attaches to the process specified
120 in pid. The child is sent a SIGSTOP, but will not
121 necessarily have stopped by the completion of this call;
122 use wait() to wait for the child to stop. */
123 if (waitpid(pid, NULL, __WALL) != pid) {
124 perror ("trace_pid: waitpid");
125 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100126 }
127
Petr Machatac805c622012-03-02 00:10:37 +0100128 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100129}
130
Juan Cespedesf1350522008-12-16 18:19:58 +0100131int
Petr Machatacec06ec2012-04-10 13:31:55 +0200132trace_pid(pid_t pid)
133{
Petr Machata26627682011-07-08 18:15:32 +0200134 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200135 /* This shouldn't emit error messages, as there are legitimate
136 * reasons that the PID can't be attached: like it may have
137 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200138 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100139 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100140
Petr Machatac805c622012-03-02 00:10:37 +0100141 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100142}
143
Juan Cespedesf1350522008-12-16 18:19:58 +0100144void
Petr Machata3ed2a422012-04-06 17:18:55 +0200145trace_set_options(struct Process *proc)
146{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100147 if (proc->tracesysgood & 0x80)
148 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200149
Petr Machata3ed2a422012-04-06 17:18:55 +0200150 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200151 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200152
Juan Cespedes1e583132009-04-07 18:17:11 +0200153 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
154 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
155 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200156 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
157 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100158 perror("PTRACE_SETOPTIONS");
159 return;
160 }
161 proc->tracesysgood |= 0x80;
162}
163
Juan Cespedesf1350522008-12-16 18:19:58 +0100164void
165untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200166 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200167 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100168}
169
Juan Cespedesf1350522008-12-16 18:19:58 +0100170void
Petr Machatac897cb72012-05-05 01:26:58 +0200171continue_after_signal(pid_t pid, int signum)
172{
173 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
174 pid, signum);
175 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200176}
177
178static enum ecb_status
179event_for_pid(Event * event, void * data)
180{
181 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
182 return ecb_yield;
183 return ecb_cont;
184}
185
186static int
187have_events_for(pid_t pid)
188{
189 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
190}
191
192void
193continue_process(pid_t pid)
194{
195 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200196
197 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200198 the queue for this process. Otherwise just wait for the
199 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200200 if (!have_events_for(pid))
201 /* We always trace syscalls to control fork(),
202 * clone(), execve()... */
203 ptrace(PTRACE_SYSCALL, pid, 0, 0);
204 else
205 debug(DEBUG_PROCESS,
206 "putting off the continue, events in que.");
207}
208
Petr Machata98f09922011-07-09 10:55:29 +0200209static struct pid_task *
210get_task_info(struct pid_set * pids, pid_t pid)
211{
Petr Machata750ca8c2011-10-06 14:29:34 +0200212 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200213 size_t i;
214 for (i = 0; i < pids->count; ++i)
215 if (pids->tasks[i].pid == pid)
216 return &pids->tasks[i];
217
218 return NULL;
219}
220
221static struct pid_task *
222add_task_info(struct pid_set * pids, pid_t pid)
223{
224 if (pids->count == pids->alloc) {
225 size_t ns = (2 * pids->alloc) ?: 4;
226 struct pid_task * n = realloc(pids->tasks,
227 sizeof(*pids->tasks) * ns);
228 if (n == NULL)
229 return NULL;
230 pids->tasks = n;
231 pids->alloc = ns;
232 }
233 struct pid_task * task_info = &pids->tasks[pids->count++];
234 memset(task_info, 0, sizeof(*task_info));
235 task_info->pid = pid;
236 return task_info;
237}
238
Petr Machata2b46cfc2012-02-18 11:17:29 +0100239static enum callback_status
240task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200241{
242 enum process_status st = process_status(task->pid);
243 if (data != NULL)
244 *(enum process_status *)data = st;
245
246 /* If the task is already stopped, don't worry about it.
247 * Likewise if it managed to become a zombie or terminate in
248 * the meantime. This can happen when the whole thread group
249 * is terminating. */
250 switch (st) {
251 case ps_invalid:
252 case ps_tracing_stop:
253 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200255 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200256 case ps_stop:
257 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100258 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200259 }
Petr Machata36d19822011-10-21 16:03:45 +0200260
261 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200262}
263
264/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265static enum callback_status
266task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200267{
268 struct pid_set * pids = data;
269 struct pid_task * task_info = get_task_info(pids, task->pid);
270 if (task_info != NULL
271 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100272 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200273
274 return task_stopped(task, NULL);
275}
276
Petr Machata366c2f42012-02-09 19:34:36 +0100277static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200278
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279static enum callback_status
280task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200281{
282 if (task->event_handler != NULL
283 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 return CBS_STOP;
285 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200286}
287
288static int
Petr Machata74132a42012-03-16 02:46:18 +0100289is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200290{
Petr Machata74132a42012-03-16 02:46:18 +0100291 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200292}
293
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294static enum callback_status
295send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200296{
297 Process * leader = task->leader;
298 struct pid_set * pids = data;
299
300 /* Look for pre-existing task record, or add new. */
301 struct pid_task * task_info = get_task_info(pids, task->pid);
302 if (task_info == NULL)
303 task_info = add_task_info(pids, task->pid);
304 if (task_info == NULL) {
305 perror("send_sigstop: add_task_info");
306 destroy_event_handler(leader);
307 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100308 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200309 }
310
311 /* This task still has not been attached to. It should be
312 stopped by the kernel. */
313 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100314 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200315
316 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200317 * if we sent the SIGSTOP already, which happens when we are
318 * handling "onexit" and inherited the handler from breakpoint
319 * re-enablement. */
320 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321 if (task_stopped(task, &st) == CBS_CONT)
322 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200323 if (task_info->sigstopped) {
324 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100325 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200326 task_info->delivered = 0;
327 }
328
Petr Machatacbe29c62011-09-27 02:27:58 +0200329 /* Also don't attempt to stop the process if it's a parent of
330 * vforked process. We set up event handler specially to hint
331 * us. In that case parent is in D state, which we use to
332 * weed out unnecessary looping. */
333 if (st == ps_sleeping
334 && is_vfork_parent (task)) {
335 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100336 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200337 }
338
Petr Machata98f09922011-07-09 10:55:29 +0200339 if (task_kill(task->pid, SIGSTOP) >= 0) {
340 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
341 task_info->sigstopped = 1;
342 } else
343 fprintf(stderr,
344 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
345
Petr Machata2b46cfc2012-02-18 11:17:29 +0100346 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200347}
348
Petr Machata73894bd2011-08-20 23:47:34 +0200349/* On certain kernels, detaching right after a singlestep causes the
350 tracee to be killed with a SIGTRAP (that even though the singlestep
351 was properly caught by waitpid. The ugly workaround is to put a
352 breakpoint where IP points and let the process continue. After
353 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200354static void
Petr Machata73894bd2011-08-20 23:47:34 +0200355ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200356{
357 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100358 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200359 if (sbp != NULL)
360 enable_breakpoint(proc, sbp);
361 else
Petr Machata9df15012012-02-20 12:49:46 +0100362 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200363 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200364}
365
366static void
Petr Machata98f09922011-07-09 10:55:29 +0200367process_stopping_done(struct process_stopping_handler * self, Process * leader)
368{
369 debug(DEBUG_PROCESS, "process stopping done %d",
370 self->task_enabling_breakpoint->pid);
371 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200372 if (!self->exiting) {
373 for (i = 0; i < self->pids.count; ++i)
374 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100375 && (self->pids.tasks[i].delivered
376 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200377 continue_process(self->pids.tasks[i].pid);
378 continue_process(self->task_enabling_breakpoint->pid);
379 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200380 }
381
382 if (self->exiting) {
383 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200384 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200385 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200386 } else {
387 switch ((self->ugly_workaround_p)(self)) {
388 case CBS_FAIL:
389 /* xxx handle me */
390 case CBS_STOP:
391 break;
392 case CBS_CONT:
393 goto ugly_workaround;
394 }
Petr Machata590c8082011-08-20 22:45:26 +0200395 }
396}
397
398/* Before we detach, we need to make sure that task's IP is on the
399 * edge of an instruction. So for tasks that have a breakpoint event
400 * in the queue, we adjust the instruction pointer, just like
401 * continue_after_breakpoint does. */
402static enum ecb_status
403undo_breakpoint(Event * event, void * data)
404{
405 if (event != NULL
406 && event->proc->leader == data
407 && event->type == EVENT_BREAKPOINT)
408 set_instruction_pointer(event->proc, event->e_un.brk_addr);
409 return ecb_cont;
410}
411
Petr Machata2b46cfc2012-02-18 11:17:29 +0100412static enum callback_status
413untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200414{
415 if (task != data)
416 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200418}
419
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420static enum callback_status
421remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200422{
423 /* Don't untrace leader just yet. */
424 if (task != data)
425 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200427}
428
Petr Machata86d38282012-04-24 18:09:01 +0200429static enum callback_status
430retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
431{
432 breakpoint_on_retract(bp, proc);
433 return CBS_CONT;
434}
435
Petr Machata590c8082011-08-20 22:45:26 +0200436static void
437detach_process(Process * leader)
438{
439 each_qd_event(&undo_breakpoint, leader);
440 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200441 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200442
443 /* Now untrace the process, if it was attached to by -p. */
444 struct opt_p_t * it;
445 for (it = opt_p; it != NULL; it = it->next) {
446 Process * proc = pid2proc(it->pid);
447 if (proc == NULL)
448 continue;
449 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100450 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200451 break;
452 }
453 }
Petr Machata74132a42012-03-16 02:46:18 +0100454 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200455 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200456 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200457}
458
459static void
460handle_stopping_event(struct pid_task * task_info, Event ** eventp)
461{
462 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200463 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200464 task_info->got_event = 1;
465
466 Event * event = *eventp;
467
468 /* In every state, sink SIGSTOP events for tasks that it was
469 * sent to. */
470 if (task_info != NULL
471 && event->type == EVENT_SIGNAL
472 && event->e_un.signum == SIGSTOP) {
473 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
474 if (task_info->sigstopped
475 && !task_info->delivered) {
476 task_info->delivered = 1;
477 *eventp = NULL; // sink the event
478 } else
479 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
480 "sigstopped=%d and delivered=%d\n",
481 task_info->pid, task_info->sigstopped,
482 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100483 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100484}
485
Petr Machata98f09922011-07-09 10:55:29 +0200486/* Some SIGSTOPs may have not been delivered to their respective tasks
487 * yet. They are still in the queue. If we have seen an event for
488 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200489 * caught by ltrace. We don't mind that the process is after
490 * breakpoint (and therefore potentially doesn't have aligned IP),
491 * because the signal will be delivered without the process actually
492 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200493static void
494continue_for_sigstop_delivery(struct pid_set * pids)
495{
496 size_t i;
497 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200498 if (pids->tasks[i].pid != 0
499 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200500 && !pids->tasks[i].delivered
501 && pids->tasks[i].got_event) {
502 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
503 pids->tasks[i].pid);
504 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
505 }
506 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100507}
508
Petr Machata98f09922011-07-09 10:55:29 +0200509static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200510event_exit_p(Event * event)
511{
512 return event != NULL && (event->type == EVENT_EXIT
513 || event->type == EVENT_EXIT_SIGNAL);
514}
515
516static int
Petr Machata98f09922011-07-09 10:55:29 +0200517event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200518{
Petr Machata750ca8c2011-10-06 14:29:34 +0200519 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200520 || event->type == EVENT_NONE;
521}
522
523static int
524await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
525 Event * event)
526{
527 /* If we still didn't get our SIGSTOP, continue the process
528 * and carry on. */
529 if (event != NULL && !event_exit_or_none_p(event)
530 && task_info != NULL && task_info->sigstopped) {
531 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
532 task_info->pid);
533 /* We should get the signal the first thing
534 * after this, so it should be OK to continue
535 * even if we are over a breakpoint. */
536 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
537
538 } else {
539 /* If all SIGSTOPs were delivered, uninstall the
540 * handler and continue everyone. */
541 /* XXX I suspect that we should check tasks that are
542 * still around. Is things are now, there should be a
543 * race between waiting for everyone to stop and one
544 * of the tasks exiting. */
545 int all_clear = 1;
546 size_t i;
547 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200548 if (pids->tasks[i].pid != 0
549 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200550 && !pids->tasks[i].delivered) {
551 all_clear = 0;
552 break;
553 }
554 return all_clear;
555 }
556
557 return 0;
558}
559
Petr Machata590c8082011-08-20 22:45:26 +0200560static int
561all_stops_accountable(struct pid_set * pids)
562{
563 size_t i;
564 for (i = 0; i < pids->count; ++i)
565 if (pids->tasks[i].pid != 0
566 && !pids->tasks[i].got_event
567 && !have_events_for(pids->tasks[i].pid))
568 return 0;
569 return 1;
570}
571
Petr Machataa266acb2012-04-12 23:50:23 +0200572/* The protocol is: 0 for success, negative for failure, positive if
573 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200574int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200575 int (*add_cb)(void *addr, void *data),
576 void *add_cb_data);
577
578#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
579int
Petr Machata9e1e9692012-04-19 02:29:38 +0200580arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200581 int (*add_cb)(void *addr, void *data),
582 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100583{
Petr Machataa266acb2012-04-12 23:50:23 +0200584 return 1;
585}
586#endif
587
Petr Machata42748ac2012-04-19 04:24:25 +0200588static Event *process_stopping_on_event(struct event_handler *super,
589 Event *event);
590
591static void
592remove_atomic_breakpoints(struct Process *proc)
593{
Petr Machata24a47042012-04-19 18:15:08 +0200594 struct process_stopping_handler *self
595 = (void *)proc->leader->event_handler;
596 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200597 assert(self->super.on_event == process_stopping_on_event);
598
599 int ct = sizeof(self->atomic_skip_bp_addrs)
600 / sizeof(*self->atomic_skip_bp_addrs);
601 int i;
602 for (i = 0; i < ct; ++i)
603 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200604 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200605 self->atomic_skip_bp_addrs[i] = 0;
606 }
607}
608
609static void
610atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
611{
612 remove_atomic_breakpoints(proc);
613}
614
Petr Machataa266acb2012-04-12 23:50:23 +0200615static int
616atomic_singlestep_add_bp(void *addr, void *data)
617{
618 struct process_stopping_handler *self = data;
619 struct Process *proc = self->task_enabling_breakpoint;
620
Petr Machata42748ac2012-04-19 04:24:25 +0200621 int ct = sizeof(self->atomic_skip_bp_addrs)
622 / sizeof(*self->atomic_skip_bp_addrs);
623 int i;
624 for (i = 0; i < ct; ++i)
625 if (self->atomic_skip_bp_addrs[i] == 0) {
626 self->atomic_skip_bp_addrs[i] = addr;
627 static struct bp_callbacks cbs = {
628 .on_hit = atomic_singlestep_bp_on_hit,
629 };
630 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200631 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200632 breakpoint_set_callbacks(bp, &cbs);
633 return 0;
634 }
Petr Machataa266acb2012-04-12 23:50:23 +0200635
Petr Machata42748ac2012-04-19 04:24:25 +0200636 assert(!"Too many atomic singlestep breakpoints!");
637 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200638}
639
640static int
641singlestep(struct process_stopping_handler *self)
642{
643 struct Process *proc = self->task_enabling_breakpoint;
644
645 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
646 self->breakpoint_being_enabled,
647 &atomic_singlestep_add_bp, self);
648
649 /* Propagate failure and success. */
650 if (status <= 0)
651 return status;
652
653 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100654 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200655 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100656 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200657 return -1;
658 }
659 return 0;
660}
661
662static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200663post_singlestep(struct process_stopping_handler *self,
664 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200665{
666 continue_for_sigstop_delivery(&self->pids);
667
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200668 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200669 *eventp = NULL; // handled
670
Petr Machata42748ac2012-04-19 04:24:25 +0200671 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200672
Petr Machata42748ac2012-04-19 04:24:25 +0200673 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200674 self->breakpoint_being_enabled = NULL;
675}
676
677static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200678singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200679{
680 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200681 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200682 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataa266acb2012-04-12 23:50:23 +0200683 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200684 sbp->addr, get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200685 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100686}
687
Petr Machata9e1e9692012-04-19 02:29:38 +0200688static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200689pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200690{
691 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200692 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200693 ptrace(PTRACE_CONT, teb->pid, 0, 0);
694}
695
696static void
697pt_singlestep(struct process_stopping_handler *self)
698{
699 if (singlestep(self) < 0)
700 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200701}
702
703static void
704disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200705 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200706{
707 struct Process *teb = self->task_enabling_breakpoint;
708 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200709 if (self->breakpoint_being_enabled->enabled)
710 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200711 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200712 self->state = psh_singlestep;
713}
714
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200715void
716linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
717{
Petr Machata9e1e9692012-04-19 02:29:38 +0200718 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200719}
720
721void
722linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
723{
724 disable_and(self, &pt_continue);
725}
726
Petr Machata98f09922011-07-09 10:55:29 +0200727/* This event handler is installed when we are in the process of
728 * stopping the whole thread group to do the pointer re-enablement for
729 * one of the threads. We pump all events to the queue for later
730 * processing while we wait for all the threads to stop. When this
731 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
732 * re-enable, and continue everyone. */
733static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100734process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200735{
736 struct process_stopping_handler * self = (void *)super;
737 Process * task = event->proc;
738 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200739 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200740
741 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200742 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200743 task->pid, event->type, self->state);
744
745 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
746 if (task_info == NULL)
747 fprintf(stderr, "new task??? %d\n", task->pid);
748 handle_stopping_event(task_info, &event);
749
750 int state = self->state;
751 int event_to_queue = !event_exit_or_none_p(event);
752
Petr Machata18c97072011-10-06 14:30:11 +0200753 /* Deactivate the entry if the task exits. */
754 if (event_exit_p(event) && task_info != NULL)
755 task_info->pid = 0;
756
Petr Machata43d2fe52011-11-02 13:25:49 +0100757 /* Always handle sysrets. Whether sysret occurred and what
758 * sys it rets from may need to be determined based on process
759 * stack, so we need to keep that in sync with reality. Note
760 * that we don't continue the process after the sysret is
761 * handled. See continue_after_syscall. */
762 if (event != NULL && event->type == EVENT_SYSRET) {
763 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
764 event_to_queue = 0;
765 task_info->sysret = 1;
766 }
767
Petr Machata98f09922011-07-09 10:55:29 +0200768 switch (state) {
769 case psh_stopping:
770 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100771 if (each_task(leader, NULL, &task_blocked,
772 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200773 (self->on_all_stopped)(self);
774 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200775 }
776 break;
777
Petr Machata06986d52011-11-02 13:22:46 +0100778 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200779 /* In singlestep state, breakpoint signifies that we
780 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200781 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200782
Petr Machata42748ac2012-04-19 04:24:25 +0200783 /* If this was caused by a real breakpoint, as
784 * opposed to a singlestep, assume that it's
785 * an artificial breakpoint installed for some
786 * reason for the re-enablement. In that case
787 * handle it. */
788 if (event->type == EVENT_BREAKPOINT) {
789 target_address_t ip
790 = get_instruction_pointer(task);
791 struct breakpoint *other
792 = address2bpstruct(leader, ip);
793 if (other != NULL)
794 breakpoint_on_hit(other, task);
795 }
796
Petr Machata36f40e72012-03-28 02:07:19 +0200797 /* If we got SIGNAL instead of BREAKPOINT,
798 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200799 if (event->type == EVENT_SIGNAL) {
800 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200801 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200802 singlestep_error(self);
803 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200804 goto psh_sinking;
805 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200806 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200807 } else {
808 switch ((self->keep_stepping_p)(self)) {
809 case CBS_FAIL:
810 /* XXX handle me */
811 case CBS_STOP:
812 break;
813 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200814 /* Sink singlestep event. */
815 if (event->type == EVENT_BREAKPOINT)
816 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200817 goto do_singlestep;
818 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200819 }
820
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200821 /* Re-enable the breakpoint that we are
822 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200823 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200824 if (sbp->enabled)
825 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200826
Petr Machataa266acb2012-04-12 23:50:23 +0200827 post_singlestep(self, &event);
828 goto psh_sinking;
829 }
830 break;
Petr Machata98f09922011-07-09 10:55:29 +0200831
Petr Machataa266acb2012-04-12 23:50:23 +0200832 psh_sinking:
833 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200834 case psh_sinking:
835 if (await_sigstop_delivery(&self->pids, task_info, event))
836 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200837 break;
838
839 case psh_ugly_workaround:
840 if (event == NULL)
841 break;
842 if (event->type == EVENT_BREAKPOINT) {
843 undo_breakpoint(event, leader);
844 if (task == teb)
845 self->task_enabling_breakpoint = NULL;
846 }
847 if (self->task_enabling_breakpoint == NULL
848 && all_stops_accountable(&self->pids)) {
849 undo_breakpoint(event, leader);
850 detach_process(leader);
851 event = NULL; // handled
852 }
Petr Machata98f09922011-07-09 10:55:29 +0200853 }
854
855 if (event != NULL && event_to_queue) {
856 enque_event(event);
857 event = NULL; // sink the event
858 }
859
860 return event;
861}
862
863static void
Petr Machata366c2f42012-02-09 19:34:36 +0100864process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200865{
866 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200867 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100868}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100869
Petr Machata36f40e72012-03-28 02:07:19 +0200870static enum callback_status
871no(struct process_stopping_handler *self)
872{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200873 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200874}
875
Petr Machatac1aa9582012-03-27 23:54:01 +0200876int
877process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200878 void (*as)(struct process_stopping_handler *),
879 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200880 (struct process_stopping_handler *),
881 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200882 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200883{
Petr Machata9ace5742012-04-14 02:29:57 +0200884 debug(DEBUG_FUNCTION,
885 "process_install_stopping_handler: pid=%d", proc->pid);
886
Petr Machatac1aa9582012-03-27 23:54:01 +0200887 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
888 if (handler == NULL)
889 return -1;
890
Petr Machata36f40e72012-03-28 02:07:19 +0200891 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200892 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200893 if (ks == NULL)
894 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200895 if (uw == NULL)
896 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200897
Petr Machatac1aa9582012-03-27 23:54:01 +0200898 handler->super.on_event = process_stopping_on_event;
899 handler->super.destroy = process_stopping_destroy;
900 handler->task_enabling_breakpoint = proc;
901 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200902 handler->on_all_stopped = as;
903 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200904 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200905
906 install_event_handler(proc->leader, &handler->super);
907
908 if (each_task(proc->leader, NULL, &send_sigstop,
909 &handler->pids) != NULL) {
910 destroy_event_handler(proc);
911 return -1;
912 }
913
914 /* And deliver the first fake event, in case all the
915 * conditions are already fulfilled. */
916 Event ev = {
917 .type = EVENT_NONE,
918 .proc = proc,
919 };
920 process_stopping_on_event(&handler->super, &ev);
921
922 return 0;
923}
924
Juan Cespedesf1350522008-12-16 18:19:58 +0100925void
Petr Machatabc373262012-02-07 23:31:15 +0100926continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200927{
Petr Machata9ace5742012-04-14 02:29:57 +0200928 debug(DEBUG_PROCESS,
929 "continue_after_breakpoint: pid=%d, addr=%p",
930 proc->pid, sbp->addr);
931
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200932 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200933
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100934 if (sbp->enabled == 0) {
935 continue_process(proc->pid);
936 } else {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500937#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100938 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200939 continue_process(proc->pid);
940#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200941 if (process_install_stopping_handler
942 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200943 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200944 /* Carry on not bothering to re-enable. */
945 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200946 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200947#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100948 }
949}
950
Petr Machata602330f2011-07-09 11:15:34 +0200951/**
952 * Ltrace exit. When we are about to exit, we have to go through all
953 * the processes, stop them all, remove all the breakpoints, and then
954 * detach the processes that we attached to using -p. If we left the
955 * other tasks running, they might hit stray return breakpoints and
956 * produce artifacts, so we better stop everyone, even if it's a bit
957 * of extra work.
958 */
959struct ltrace_exiting_handler
960{
Petr Machata366c2f42012-02-09 19:34:36 +0100961 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200962 struct pid_set pids;
963};
964
Petr Machata602330f2011-07-09 11:15:34 +0200965static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100966ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200967{
968 struct ltrace_exiting_handler * self = (void *)super;
969 Process * task = event->proc;
970 Process * leader = task->leader;
971
Petr Machata9ace5742012-04-14 02:29:57 +0200972 debug(DEBUG_PROCESS,
973 "ltrace_exiting_on_event: pid %d; event type %d",
974 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200975
976 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
977 handle_stopping_event(task_info, &event);
978
Petr Machata590c8082011-08-20 22:45:26 +0200979 if (event != NULL && event->type == EVENT_BREAKPOINT)
980 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200981
982 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200983 && all_stops_accountable(&self->pids))
984 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200985
986 /* Sink all non-exit events. We are about to exit, so we
987 * don't bother with queuing them. */
988 if (event_exit_or_none_p(event))
989 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200990
Petr Machata13d5df72011-08-19 23:15:15 +0200991 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200992}
993
994static void
Petr Machata366c2f42012-02-09 19:34:36 +0100995ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200996{
997 struct ltrace_exiting_handler * self = (void *)super;
998 free(self->pids.tasks);
999}
1000
1001static int
1002ltrace_exiting_install_handler(Process * proc)
1003{
1004 /* Only install to leader. */
1005 if (proc->leader != proc)
1006 return 0;
1007
1008 /* Perhaps we are already installed, if the user passed
1009 * several -p options that are tasks of one process. */
1010 if (proc->event_handler != NULL
1011 && proc->event_handler->on_event == &ltrace_exiting_on_event)
1012 return 0;
1013
Petr Machata590c8082011-08-20 22:45:26 +02001014 /* If stopping handler is already present, let it do the
1015 * work. */
1016 if (proc->event_handler != NULL) {
1017 assert(proc->event_handler->on_event
1018 == &process_stopping_on_event);
1019 struct process_stopping_handler * other
1020 = (void *)proc->event_handler;
1021 other->exiting = 1;
1022 return 0;
1023 }
1024
Petr Machata602330f2011-07-09 11:15:34 +02001025 struct ltrace_exiting_handler * handler
1026 = calloc(sizeof(*handler), 1);
1027 if (handler == NULL) {
1028 perror("malloc exiting handler");
1029 fatal:
1030 /* XXXXXXXXXXXXXXXXXXX fixme */
1031 return -1;
1032 }
1033
Petr Machata602330f2011-07-09 11:15:34 +02001034 handler->super.on_event = ltrace_exiting_on_event;
1035 handler->super.destroy = ltrace_exiting_destroy;
1036 install_event_handler(proc->leader, &handler->super);
1037
Petr Machata74132a42012-03-16 02:46:18 +01001038 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001039 &handler->pids) != NULL)
1040 goto fatal;
1041
1042 return 0;
1043}
1044
Petr Machatacbe29c62011-09-27 02:27:58 +02001045/*
1046 * When the traced process vforks, it's suspended until the child
1047 * process calls _exit or exec*. In the meantime, the two share the
1048 * address space.
1049 *
1050 * The child process should only ever call _exit or exec*, but we
1051 * can't count on that (it's not the role of ltrace to policy, but to
1052 * observe). In any case, we will _at least_ have to deal with
1053 * removal of vfork return breakpoint (which we have to smuggle back
1054 * in, so that the parent can see it, too), and introduction of exec*
1055 * return breakpoint. Since we already have both breakpoint actions
1056 * to deal with, we might as well support it all.
1057 *
1058 * The gist is that we pretend that the child is in a thread group
1059 * with its parent, and handle it as a multi-threaded case, with the
1060 * exception that we know that the parent is blocked, and don't
1061 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001062 */
1063
Petr Machata134a1082011-09-27 20:25:58 +02001064struct process_vfork_handler
1065{
Petr Machata366c2f42012-02-09 19:34:36 +01001066 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +02001067 void * bp_addr;
1068};
1069
Petr Machatacbe29c62011-09-27 02:27:58 +02001070static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001071process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001072{
Petr Machata9ace5742012-04-14 02:29:57 +02001073 debug(DEBUG_PROCESS,
1074 "process_vfork_on_event: pid %d; event type %d",
1075 event->proc->pid, event->type);
1076
Petr Machatacbe29c62011-09-27 02:27:58 +02001077 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001078 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001079 assert(self != NULL);
1080
1081 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001082 case EVENT_BREAKPOINT:
1083 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001084 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001085 self->bp_addr = event->e_un.brk_addr;
1086 break;
1087
Petr Machatacbe29c62011-09-27 02:27:58 +02001088 case EVENT_EXIT:
1089 case EVENT_EXIT_SIGNAL:
1090 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001091 /* Smuggle back in the vfork return breakpoint, so
1092 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001093 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001094 sbp = dict_find_entry(event->proc->leader->breakpoints,
1095 self->bp_addr);
1096 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001097 assert(sbp->libsym == NULL);
1098 /* We don't mind failing that, it's not a big
1099 * deal to not display one extra vfork return. */
1100 insert_breakpoint(event->proc->parent,
1101 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001102 }
1103
Petr Machataba9911f2011-09-27 21:09:47 +02001104 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001105
1106 /* Remove the leader that we artificially set up
1107 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001108 change_process_leader(event->proc, event->proc);
1109 destroy_event_handler(event->proc);
1110
Petr Machatacbe29c62011-09-27 02:27:58 +02001111 default:
1112 ;
1113 }
1114
1115 return event;
1116}
1117
1118void
1119continue_after_vfork(Process * proc)
1120{
1121 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001122 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001123 if (handler == NULL) {
1124 perror("malloc vfork handler");
1125 /* Carry on not bothering to treat the process as
1126 * necessary. */
1127 continue_process(proc->parent->pid);
1128 return;
1129 }
1130
1131 /* We must set up custom event handler, so that we see
1132 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001133 handler->super.on_event = process_vfork_on_event;
1134 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001135
1136 /* Make sure that the child is sole thread. */
1137 assert(proc->leader == proc);
1138 assert(proc->next == NULL || proc->next->leader != proc);
1139
1140 /* Make sure that the child's parent is properly set up. */
1141 assert(proc->parent != NULL);
1142 assert(proc->parent->leader != NULL);
1143
1144 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001145}
1146
Petr Machata9d29b3e2011-11-09 16:46:56 +01001147static int
1148is_mid_stopping(Process *proc)
1149{
1150 return proc != NULL
1151 && proc->event_handler != NULL
1152 && proc->event_handler->on_event == &process_stopping_on_event;
1153}
1154
Petr Machata43d2fe52011-11-02 13:25:49 +01001155void
1156continue_after_syscall(Process * proc, int sysnum, int ret_p)
1157{
1158 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001159 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1160 debug(DEBUG_PROCESS,
1161 "continue_after_syscall: don't continue %d",
1162 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001163 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001164 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001165 continue_process(proc->pid);
1166}
1167
Petr Machata602330f2011-07-09 11:15:34 +02001168/* If ltrace gets SIGINT, the processes directly or indirectly run by
1169 * ltrace get it too. We just have to wait long enough for the signal
1170 * to be delivered and the process terminated, which we notice and
1171 * exit ltrace, too. So there's not much we need to do there. We
1172 * want to keep tracing those processes as usual, in case they just
1173 * SIG_IGN the SIGINT to do their shutdown etc.
1174 *
1175 * For processes ran on the background, we want to install an exit
1176 * handler that stops all the threads, removes all breakpoints, and
1177 * detaches.
1178 */
1179void
Petr Machataffe4cd22012-04-11 18:01:44 +02001180os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001181{
1182 struct opt_p_t * it;
1183 for (it = opt_p; it != NULL; it = it->next) {
1184 Process * proc = pid2proc(it->pid);
1185 if (proc == NULL || proc->leader == NULL)
1186 continue;
1187 if (ltrace_exiting_install_handler(proc->leader) < 0)
1188 fprintf(stderr,
1189 "Couldn't install exiting handler for %d.\n",
1190 proc->pid);
1191 }
1192}
1193
Petr Machataffe4cd22012-04-11 18:01:44 +02001194int
1195os_ltrace_exiting_sighandler(void)
1196{
1197 extern int linux_in_waitpid;
1198 if (linux_in_waitpid) {
1199 os_ltrace_exiting();
1200 return 1;
1201 }
1202 return 0;
1203}
1204
Joe Damatodfa3fa32010-11-08 15:47:35 -08001205size_t
1206umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1207
1208 union {
1209 long a;
1210 char c[sizeof(long)];
1211 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001212 int started = 0;
1213 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001214
1215 while (offset < len) {
1216 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1217 if (a.a == -1 && errno) {
1218 if (started && errno == EIO)
1219 return bytes_read;
1220 else
1221 return -1;
1222 }
1223 started = 1;
1224
1225 if (len - offset >= sizeof(long)) {
1226 memcpy(laddr + offset, &a.c[0], sizeof(long));
1227 bytes_read += sizeof(long);
1228 }
1229 else {
1230 memcpy(laddr + offset, &a.c[0], len - offset);
1231 bytes_read += (len - offset);
1232 }
1233 offset += sizeof(long);
1234 }
1235
1236 return bytes_read;
1237}
1238
Steve Fink7bafff02006-08-07 04:50:42 +02001239/* Read a series of bytes starting at the process's memory address
1240 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1241 have been read.
1242*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001243int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001244umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001245 union {
1246 long a;
1247 char c[sizeof(long)];
1248 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001249 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001250 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001251
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001252 while (offset < len) {
1253 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1254 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001255 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001256 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001257 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001258 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001259 return 0;
1260 }
1261 }
1262 offset += sizeof(long);
1263 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001264 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001265 return 0;
1266}