blob: 90cd4de4d28563efdb892952ac82e33ab83c2224 [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>
Petr Machatad3dc17b2012-03-21 03:23:25 +01008#include <error.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010010#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010011#include <string.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010012#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013
Petr Machatacec06ec2012-04-10 13:31:55 +020014#ifdef HAVE_LIBSELINUX
15# include <selinux/selinux.h>
16#endif
17
18#include "ptrace.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020019#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010020#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010021#include "proc.h"
Petr Machata92822542012-03-28 00:31:14 +020022#include "linux-gnu/trace.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
Juan Cespedesa8909f72009-04-28 20:02:41 +020054umovelong (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
Juan Cespedesa8909f72009-04-28 20:02:41 +020060umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010061 long pointed_to;
62
63 errno = 0;
64 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
65 if (pointed_to == -1 && errno)
66 return -errno;
67
68 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050069 if (info) {
70 switch(info->type) {
71 case ARGTYPE_INT:
72 *result &= 0x00000000ffffffffUL;
73 default:
74 break;
75 };
76 }
Luis Machado55c5feb2008-03-12 15:56:01 +010077 return 0;
78}
79#endif
80
Juan Cespedesf1350522008-12-16 18:19:58 +010081void
Petr Machatacec06ec2012-04-10 13:31:55 +020082trace_fail_warning(pid_t pid)
83{
84 /* This was adapted from GDB. */
85#ifdef HAVE_LIBSELINUX
86 static int checked = 0;
87 if (checked)
88 return;
89 checked = 1;
90
91 /* -1 is returned for errors, 0 if it has no effect, 1 if
92 * PTRACE_ATTACH is forbidden. */
93 if (security_get_boolean_active("deny_ptrace") == 1)
94 fprintf(stderr,
95"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
96"tracing other processes. You can disable this process attach protection by\n"
97"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
98#endif /* HAVE_LIBSELINUX */
99}
100
101void
102trace_me(void)
103{
Petr Machata26627682011-07-08 18:15:32 +0200104 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100106 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200107 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100108 exit(1);
109 }
110}
111
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100112/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100113 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100114 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100115int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100116wait_for_proc(pid_t pid)
117{
Petr Machatac805c622012-03-02 00:10:37 +0100118 /* man ptrace: PTRACE_ATTACH attaches to the process specified
119 in pid. The child is sent a SIGSTOP, but will not
120 necessarily have stopped by the completion of this call;
121 use wait() to wait for the child to stop. */
122 if (waitpid(pid, NULL, __WALL) != pid) {
123 perror ("trace_pid: waitpid");
124 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100125 }
126
Petr Machatac805c622012-03-02 00:10:37 +0100127 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100128}
129
Juan Cespedesf1350522008-12-16 18:19:58 +0100130int
Petr Machatacec06ec2012-04-10 13:31:55 +0200131trace_pid(pid_t pid)
132{
Petr Machata26627682011-07-08 18:15:32 +0200133 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200134 /* This shouldn't emit error messages, as there are legitimate
135 * reasons that the PID can't be attached: like it may have
136 * already ended. */
137 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100138 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100139
Petr Machatac805c622012-03-02 00:10:37 +0100140 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100141}
142
Juan Cespedesf1350522008-12-16 18:19:58 +0100143void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200144trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100145 if (proc->tracesysgood & 0x80)
146 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200147
Petr Machata26627682011-07-08 18:15:32 +0200148 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200149
Juan Cespedes1e583132009-04-07 18:17:11 +0200150 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
151 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
152 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200153 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
154 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100155 perror("PTRACE_SETOPTIONS");
156 return;
157 }
158 proc->tracesysgood |= 0x80;
159}
160
Juan Cespedesf1350522008-12-16 18:19:58 +0100161void
162untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200163 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100164 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100165}
166
Juan Cespedesf1350522008-12-16 18:19:58 +0100167void
168continue_after_signal(pid_t pid, int signum) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200169 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
Petr Machata98f09922011-07-09 10:55:29 +0200170 ptrace(PTRACE_SYSCALL, pid, 0, signum);
171}
172
173static enum ecb_status
174event_for_pid(Event * event, void * data)
175{
176 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
177 return ecb_yield;
178 return ecb_cont;
179}
180
181static int
182have_events_for(pid_t pid)
183{
184 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
185}
186
187void
188continue_process(pid_t pid)
189{
190 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200191
192 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200193 the queue for this process. Otherwise just wait for the
194 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200195 if (!have_events_for(pid))
196 /* We always trace syscalls to control fork(),
197 * clone(), execve()... */
198 ptrace(PTRACE_SYSCALL, pid, 0, 0);
199 else
200 debug(DEBUG_PROCESS,
201 "putting off the continue, events in que.");
202}
203
Petr Machata98f09922011-07-09 10:55:29 +0200204static struct pid_task *
205get_task_info(struct pid_set * pids, pid_t pid)
206{
Petr Machata750ca8c2011-10-06 14:29:34 +0200207 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200208 size_t i;
209 for (i = 0; i < pids->count; ++i)
210 if (pids->tasks[i].pid == pid)
211 return &pids->tasks[i];
212
213 return NULL;
214}
215
216static struct pid_task *
217add_task_info(struct pid_set * pids, pid_t pid)
218{
219 if (pids->count == pids->alloc) {
220 size_t ns = (2 * pids->alloc) ?: 4;
221 struct pid_task * n = realloc(pids->tasks,
222 sizeof(*pids->tasks) * ns);
223 if (n == NULL)
224 return NULL;
225 pids->tasks = n;
226 pids->alloc = ns;
227 }
228 struct pid_task * task_info = &pids->tasks[pids->count++];
229 memset(task_info, 0, sizeof(*task_info));
230 task_info->pid = pid;
231 return task_info;
232}
233
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234static enum callback_status
235task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200236{
237 enum process_status st = process_status(task->pid);
238 if (data != NULL)
239 *(enum process_status *)data = st;
240
241 /* If the task is already stopped, don't worry about it.
242 * Likewise if it managed to become a zombie or terminate in
243 * the meantime. This can happen when the whole thread group
244 * is terminating. */
245 switch (st) {
246 case ps_invalid:
247 case ps_tracing_stop:
248 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200250 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200251 case ps_stop:
252 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200254 }
Petr Machata36d19822011-10-21 16:03:45 +0200255
256 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200257}
258
259/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260static enum callback_status
261task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200262{
263 struct pid_set * pids = data;
264 struct pid_task * task_info = get_task_info(pids, task->pid);
265 if (task_info != NULL
266 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100267 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200268
269 return task_stopped(task, NULL);
270}
271
Petr Machata366c2f42012-02-09 19:34:36 +0100272static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200273
Petr Machata2b46cfc2012-02-18 11:17:29 +0100274static enum callback_status
275task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200276{
277 if (task->event_handler != NULL
278 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279 return CBS_STOP;
280 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200281}
282
283static int
Petr Machata74132a42012-03-16 02:46:18 +0100284is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200285{
Petr Machata74132a42012-03-16 02:46:18 +0100286 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200287}
288
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289static enum callback_status
290send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200291{
292 Process * leader = task->leader;
293 struct pid_set * pids = data;
294
295 /* Look for pre-existing task record, or add new. */
296 struct pid_task * task_info = get_task_info(pids, task->pid);
297 if (task_info == NULL)
298 task_info = add_task_info(pids, task->pid);
299 if (task_info == NULL) {
300 perror("send_sigstop: add_task_info");
301 destroy_event_handler(leader);
302 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100303 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200304 }
305
306 /* This task still has not been attached to. It should be
307 stopped by the kernel. */
308 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200310
311 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200312 * if we sent the SIGSTOP already, which happens when we are
313 * handling "onexit" and inherited the handler from breakpoint
314 * re-enablement. */
315 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100316 if (task_stopped(task, &st) == CBS_CONT)
317 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200318 if (task_info->sigstopped) {
319 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100320 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200321 task_info->delivered = 0;
322 }
323
Petr Machatacbe29c62011-09-27 02:27:58 +0200324 /* Also don't attempt to stop the process if it's a parent of
325 * vforked process. We set up event handler specially to hint
326 * us. In that case parent is in D state, which we use to
327 * weed out unnecessary looping. */
328 if (st == ps_sleeping
329 && is_vfork_parent (task)) {
330 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200332 }
333
Petr Machata98f09922011-07-09 10:55:29 +0200334 if (task_kill(task->pid, SIGSTOP) >= 0) {
335 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
336 task_info->sigstopped = 1;
337 } else
338 fprintf(stderr,
339 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
340
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200342}
343
Petr Machata73894bd2011-08-20 23:47:34 +0200344/* On certain kernels, detaching right after a singlestep causes the
345 tracee to be killed with a SIGTRAP (that even though the singlestep
346 was properly caught by waitpid. The ugly workaround is to put a
347 breakpoint where IP points and let the process continue. After
348 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200349static void
Petr Machata73894bd2011-08-20 23:47:34 +0200350ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200351{
352 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100353 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200354 if (sbp != NULL)
355 enable_breakpoint(proc, sbp);
356 else
Petr Machata9df15012012-02-20 12:49:46 +0100357 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200358 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200359}
360
361static void
Petr Machata98f09922011-07-09 10:55:29 +0200362process_stopping_done(struct process_stopping_handler * self, Process * leader)
363{
364 debug(DEBUG_PROCESS, "process stopping done %d",
365 self->task_enabling_breakpoint->pid);
366 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200367 if (!self->exiting) {
368 for (i = 0; i < self->pids.count; ++i)
369 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100370 && (self->pids.tasks[i].delivered
371 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200372 continue_process(self->pids.tasks[i].pid);
373 continue_process(self->task_enabling_breakpoint->pid);
374 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200375 }
376
377 if (self->exiting) {
378 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200379 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200380 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200381 } else {
382 switch ((self->ugly_workaround_p)(self)) {
383 case CBS_FAIL:
384 /* xxx handle me */
385 case CBS_STOP:
386 break;
387 case CBS_CONT:
388 goto ugly_workaround;
389 }
Petr Machata590c8082011-08-20 22:45:26 +0200390 }
391}
392
393/* Before we detach, we need to make sure that task's IP is on the
394 * edge of an instruction. So for tasks that have a breakpoint event
395 * in the queue, we adjust the instruction pointer, just like
396 * continue_after_breakpoint does. */
397static enum ecb_status
398undo_breakpoint(Event * event, void * data)
399{
400 if (event != NULL
401 && event->proc->leader == data
402 && event->type == EVENT_BREAKPOINT)
403 set_instruction_pointer(event->proc, event->e_un.brk_addr);
404 return ecb_cont;
405}
406
Petr Machata2b46cfc2012-02-18 11:17:29 +0100407static enum callback_status
408untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200409{
410 if (task != data)
411 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100412 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200413}
414
Petr Machata2b46cfc2012-02-18 11:17:29 +0100415static enum callback_status
416remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200417{
418 /* Don't untrace leader just yet. */
419 if (task != data)
420 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200422}
423
424static void
425detach_process(Process * leader)
426{
427 each_qd_event(&undo_breakpoint, leader);
428 disable_all_breakpoints(leader);
429
430 /* Now untrace the process, if it was attached to by -p. */
431 struct opt_p_t * it;
432 for (it = opt_p; it != NULL; it = it->next) {
433 Process * proc = pid2proc(it->pid);
434 if (proc == NULL)
435 continue;
436 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100437 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200438 break;
439 }
440 }
Petr Machata74132a42012-03-16 02:46:18 +0100441 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200442 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200443 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200444}
445
446static void
447handle_stopping_event(struct pid_task * task_info, Event ** eventp)
448{
449 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200450 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200451 task_info->got_event = 1;
452
453 Event * event = *eventp;
454
455 /* In every state, sink SIGSTOP events for tasks that it was
456 * sent to. */
457 if (task_info != NULL
458 && event->type == EVENT_SIGNAL
459 && event->e_un.signum == SIGSTOP) {
460 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
461 if (task_info->sigstopped
462 && !task_info->delivered) {
463 task_info->delivered = 1;
464 *eventp = NULL; // sink the event
465 } else
466 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
467 "sigstopped=%d and delivered=%d\n",
468 task_info->pid, task_info->sigstopped,
469 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100470 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100471}
472
Petr Machata98f09922011-07-09 10:55:29 +0200473/* Some SIGSTOPs may have not been delivered to their respective tasks
474 * yet. They are still in the queue. If we have seen an event for
475 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200476 * caught by ltrace. We don't mind that the process is after
477 * breakpoint (and therefore potentially doesn't have aligned IP),
478 * because the signal will be delivered without the process actually
479 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200480static void
481continue_for_sigstop_delivery(struct pid_set * pids)
482{
483 size_t i;
484 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200485 if (pids->tasks[i].pid != 0
486 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200487 && !pids->tasks[i].delivered
488 && pids->tasks[i].got_event) {
489 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
490 pids->tasks[i].pid);
491 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
492 }
493 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100494}
495
Petr Machata98f09922011-07-09 10:55:29 +0200496static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200497event_exit_p(Event * event)
498{
499 return event != NULL && (event->type == EVENT_EXIT
500 || event->type == EVENT_EXIT_SIGNAL);
501}
502
503static int
Petr Machata98f09922011-07-09 10:55:29 +0200504event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200505{
Petr Machata750ca8c2011-10-06 14:29:34 +0200506 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200507 || event->type == EVENT_NONE;
508}
509
510static int
511await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
512 Event * event)
513{
514 /* If we still didn't get our SIGSTOP, continue the process
515 * and carry on. */
516 if (event != NULL && !event_exit_or_none_p(event)
517 && task_info != NULL && task_info->sigstopped) {
518 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
519 task_info->pid);
520 /* We should get the signal the first thing
521 * after this, so it should be OK to continue
522 * even if we are over a breakpoint. */
523 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
524
525 } else {
526 /* If all SIGSTOPs were delivered, uninstall the
527 * handler and continue everyone. */
528 /* XXX I suspect that we should check tasks that are
529 * still around. Is things are now, there should be a
530 * race between waiting for everyone to stop and one
531 * of the tasks exiting. */
532 int all_clear = 1;
533 size_t i;
534 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200535 if (pids->tasks[i].pid != 0
536 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200537 && !pids->tasks[i].delivered) {
538 all_clear = 0;
539 break;
540 }
541 return all_clear;
542 }
543
544 return 0;
545}
546
Petr Machata590c8082011-08-20 22:45:26 +0200547static int
548all_stops_accountable(struct pid_set * pids)
549{
550 size_t i;
551 for (i = 0; i < pids->count; ++i)
552 if (pids->tasks[i].pid != 0
553 && !pids->tasks[i].got_event
554 && !have_events_for(pids->tasks[i].pid))
555 return 0;
556 return 1;
557}
558
Petr Machataa266acb2012-04-12 23:50:23 +0200559/* The protocol is: 0 for success, negative for failure, positive if
560 * default singlestep is to be used. */
561int arch_atomic_singlestep(struct Process *proc, Breakpoint *sbp,
562 int (*add_cb)(void *addr, void *data),
563 void *add_cb_data);
564
565#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
566int
567arch_atomic_singlestep(struct Process *proc, Breakpoint *sbp,
568 int (*add_cb)(void *addr, void *data),
569 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100570{
Petr Machataa266acb2012-04-12 23:50:23 +0200571 return 1;
572}
573#endif
574
575static int
576atomic_singlestep_add_bp(void *addr, void *data)
577{
578 struct process_stopping_handler *self = data;
579 struct Process *proc = self->task_enabling_breakpoint;
580
581 /* Only support single address as of now. */
582 assert(self->atomic_skip_bp_addr == NULL);
583
584 self->atomic_skip_bp_addr = addr + 4;
585 insert_breakpoint(proc->leader, self->atomic_skip_bp_addr, NULL, 1);
586
587 return 0;
588}
589
590static int
591singlestep(struct process_stopping_handler *self)
592{
593 struct Process *proc = self->task_enabling_breakpoint;
594
595 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
596 self->breakpoint_being_enabled,
597 &atomic_singlestep_add_bp, self);
598
599 /* Propagate failure and success. */
600 if (status <= 0)
601 return status;
602
603 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100604 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200605 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100606 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200607 return -1;
608 }
609 return 0;
610}
611
612static void
613post_singlestep(struct process_stopping_handler *self, Event **eventp)
614{
615 continue_for_sigstop_delivery(&self->pids);
616
617 if ((*eventp)->type == EVENT_BREAKPOINT)
618 *eventp = NULL; // handled
619
620 if (self->atomic_skip_bp_addr != 0)
621 delete_breakpoint(self->task_enabling_breakpoint->leader,
622 self->atomic_skip_bp_addr);
623
624 self->breakpoint_being_enabled = NULL;
625}
626
627static void
628singlestep_error(struct process_stopping_handler *self, Event **eventp)
629{
630 struct Process *teb = self->task_enabling_breakpoint;
631 Breakpoint *sbp = self->breakpoint_being_enabled;
632 fprintf(stderr, "%d couldn't singlestep over %s (%p)\n",
633 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
634 sbp->addr);
635 delete_breakpoint(teb->leader, sbp->addr);
636 post_singlestep(self, eventp);
Petr Machata06986d52011-11-02 13:22:46 +0100637}
638
Petr Machatac1aa9582012-03-27 23:54:01 +0200639static void
640disable_and_singlestep(struct process_stopping_handler *self)
641{
642 struct Process *teb = self->task_enabling_breakpoint;
643 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d", teb->pid);
644 if (self->breakpoint_being_enabled->enabled)
645 disable_breakpoint(teb, self->breakpoint_being_enabled);
646 if (singlestep(self) < 0) {
647 singlestep_error(self, &event);
648 //goto psh_sinking; /* XXX FIME */
649 abort();
650 }
651 self->state = psh_singlestep;
652}
653
Petr Machata98f09922011-07-09 10:55:29 +0200654/* This event handler is installed when we are in the process of
655 * stopping the whole thread group to do the pointer re-enablement for
656 * one of the threads. We pump all events to the queue for later
657 * processing while we wait for all the threads to stop. When this
658 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
659 * re-enable, and continue everyone. */
660static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100661process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200662{
663 struct process_stopping_handler * self = (void *)super;
664 Process * task = event->proc;
665 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200666 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200667
668 debug(DEBUG_PROCESS,
669 "pid %d; event type %d; state %d",
670 task->pid, event->type, self->state);
671
672 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
673 if (task_info == NULL)
674 fprintf(stderr, "new task??? %d\n", task->pid);
675 handle_stopping_event(task_info, &event);
676
677 int state = self->state;
678 int event_to_queue = !event_exit_or_none_p(event);
679
Petr Machata18c97072011-10-06 14:30:11 +0200680 /* Deactivate the entry if the task exits. */
681 if (event_exit_p(event) && task_info != NULL)
682 task_info->pid = 0;
683
Petr Machata43d2fe52011-11-02 13:25:49 +0100684 /* Always handle sysrets. Whether sysret occurred and what
685 * sys it rets from may need to be determined based on process
686 * stack, so we need to keep that in sync with reality. Note
687 * that we don't continue the process after the sysret is
688 * handled. See continue_after_syscall. */
689 if (event != NULL && event->type == EVENT_SYSRET) {
690 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
691 event_to_queue = 0;
692 task_info->sysret = 1;
693 }
694
Petr Machata98f09922011-07-09 10:55:29 +0200695 switch (state) {
696 case psh_stopping:
697 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100698 if (each_task(leader, NULL, &task_blocked,
699 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200700 (self->on_all_stopped)(self);
701 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200702 }
703 break;
704
Petr Machata06986d52011-11-02 13:22:46 +0100705 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200706 /* In singlestep state, breakpoint signifies that we
707 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200708 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200709
Petr Machata36f40e72012-03-28 02:07:19 +0200710 /* If we got SIGNAL instead of BREAKPOINT,
711 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200712 if (event->type == EVENT_SIGNAL) {
713 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200714 if (singlestep(self) < 0) {
715 singlestep_error(self, &event);
716 goto psh_sinking;
717 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200718 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200719 } else {
720 switch ((self->keep_stepping_p)(self)) {
721 case CBS_FAIL:
722 /* XXX handle me */
723 case CBS_STOP:
724 break;
725 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200726 /* Sink singlestep event. */
727 if (event->type == EVENT_BREAKPOINT)
728 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200729 goto do_singlestep;
730 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200731 }
732
Petr Machata98f09922011-07-09 10:55:29 +0200733 /* Essentially we don't care what event caused
734 * the thread to stop. We can do the
735 * re-enablement now. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200736 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200737 if (sbp->enabled)
738 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200739
Petr Machataa266acb2012-04-12 23:50:23 +0200740 post_singlestep(self, &event);
741 goto psh_sinking;
742 }
743 break;
Petr Machata98f09922011-07-09 10:55:29 +0200744
Petr Machataa266acb2012-04-12 23:50:23 +0200745 psh_sinking:
746 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200747 case psh_sinking:
748 if (await_sigstop_delivery(&self->pids, task_info, event))
749 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200750 break;
751
752 case psh_ugly_workaround:
753 if (event == NULL)
754 break;
755 if (event->type == EVENT_BREAKPOINT) {
756 undo_breakpoint(event, leader);
757 if (task == teb)
758 self->task_enabling_breakpoint = NULL;
759 }
760 if (self->task_enabling_breakpoint == NULL
761 && all_stops_accountable(&self->pids)) {
762 undo_breakpoint(event, leader);
763 detach_process(leader);
764 event = NULL; // handled
765 }
Petr Machata98f09922011-07-09 10:55:29 +0200766 }
767
768 if (event != NULL && event_to_queue) {
769 enque_event(event);
770 event = NULL; // sink the event
771 }
772
773 return event;
774}
775
776static void
Petr Machata366c2f42012-02-09 19:34:36 +0100777process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200778{
779 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200780 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100781}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100782
Petr Machata36f40e72012-03-28 02:07:19 +0200783static enum callback_status
784no(struct process_stopping_handler *self)
785{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200786 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200787}
788
Petr Machatac1aa9582012-03-27 23:54:01 +0200789int
790process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200791 void (*as)(struct process_stopping_handler *),
792 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200793 (struct process_stopping_handler *),
794 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200795 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200796{
797 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
798 if (handler == NULL)
799 return -1;
800
Petr Machata36f40e72012-03-28 02:07:19 +0200801 if (as == NULL)
802 as = &disable_and_singlestep;
803 if (ks == NULL)
804 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200805 if (uw == NULL)
806 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200807
Petr Machatac1aa9582012-03-27 23:54:01 +0200808 handler->super.on_event = process_stopping_on_event;
809 handler->super.destroy = process_stopping_destroy;
810 handler->task_enabling_breakpoint = proc;
811 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200812 handler->on_all_stopped = as;
813 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200814 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200815
816 install_event_handler(proc->leader, &handler->super);
817
818 if (each_task(proc->leader, NULL, &send_sigstop,
819 &handler->pids) != NULL) {
820 destroy_event_handler(proc);
821 return -1;
822 }
823
824 /* And deliver the first fake event, in case all the
825 * conditions are already fulfilled. */
826 Event ev = {
827 .type = EVENT_NONE,
828 .proc = proc,
829 };
830 process_stopping_on_event(&handler->super, &ev);
831
832 return 0;
833}
834
Juan Cespedesf1350522008-12-16 18:19:58 +0100835void
Petr Machatabc373262012-02-07 23:31:15 +0100836continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200837{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200838 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200839
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100840 if (sbp->enabled == 0) {
841 continue_process(proc->pid);
842 } else {
Petr Machata26627682011-07-08 18:15:32 +0200843 debug(DEBUG_PROCESS,
844 "continue_after_breakpoint: pid=%d, addr=%p",
845 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500846#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100847 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200848 continue_process(proc->pid);
849#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200850 if (process_install_stopping_handler
851 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200852 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200853 /* Carry on not bothering to re-enable. */
854 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200855 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200856#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100857 }
858}
859
Petr Machata602330f2011-07-09 11:15:34 +0200860/**
861 * Ltrace exit. When we are about to exit, we have to go through all
862 * the processes, stop them all, remove all the breakpoints, and then
863 * detach the processes that we attached to using -p. If we left the
864 * other tasks running, they might hit stray return breakpoints and
865 * produce artifacts, so we better stop everyone, even if it's a bit
866 * of extra work.
867 */
868struct ltrace_exiting_handler
869{
Petr Machata366c2f42012-02-09 19:34:36 +0100870 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200871 struct pid_set pids;
872};
873
Petr Machata602330f2011-07-09 11:15:34 +0200874static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100875ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200876{
877 struct ltrace_exiting_handler * self = (void *)super;
878 Process * task = event->proc;
879 Process * leader = task->leader;
880
881 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
882
883 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
884 handle_stopping_event(task_info, &event);
885
Petr Machata590c8082011-08-20 22:45:26 +0200886 if (event != NULL && event->type == EVENT_BREAKPOINT)
887 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200888
889 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200890 && all_stops_accountable(&self->pids))
891 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200892
893 /* Sink all non-exit events. We are about to exit, so we
894 * don't bother with queuing them. */
895 if (event_exit_or_none_p(event))
896 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200897
Petr Machata13d5df72011-08-19 23:15:15 +0200898 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200899}
900
901static void
Petr Machata366c2f42012-02-09 19:34:36 +0100902ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200903{
904 struct ltrace_exiting_handler * self = (void *)super;
905 free(self->pids.tasks);
906}
907
908static int
909ltrace_exiting_install_handler(Process * proc)
910{
911 /* Only install to leader. */
912 if (proc->leader != proc)
913 return 0;
914
915 /* Perhaps we are already installed, if the user passed
916 * several -p options that are tasks of one process. */
917 if (proc->event_handler != NULL
918 && proc->event_handler->on_event == &ltrace_exiting_on_event)
919 return 0;
920
Petr Machata590c8082011-08-20 22:45:26 +0200921 /* If stopping handler is already present, let it do the
922 * work. */
923 if (proc->event_handler != NULL) {
924 assert(proc->event_handler->on_event
925 == &process_stopping_on_event);
926 struct process_stopping_handler * other
927 = (void *)proc->event_handler;
928 other->exiting = 1;
929 return 0;
930 }
931
Petr Machata602330f2011-07-09 11:15:34 +0200932 struct ltrace_exiting_handler * handler
933 = calloc(sizeof(*handler), 1);
934 if (handler == NULL) {
935 perror("malloc exiting handler");
936 fatal:
937 /* XXXXXXXXXXXXXXXXXXX fixme */
938 return -1;
939 }
940
Petr Machata602330f2011-07-09 11:15:34 +0200941 handler->super.on_event = ltrace_exiting_on_event;
942 handler->super.destroy = ltrace_exiting_destroy;
943 install_event_handler(proc->leader, &handler->super);
944
Petr Machata74132a42012-03-16 02:46:18 +0100945 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +0200946 &handler->pids) != NULL)
947 goto fatal;
948
949 return 0;
950}
951
Petr Machatacbe29c62011-09-27 02:27:58 +0200952/*
953 * When the traced process vforks, it's suspended until the child
954 * process calls _exit or exec*. In the meantime, the two share the
955 * address space.
956 *
957 * The child process should only ever call _exit or exec*, but we
958 * can't count on that (it's not the role of ltrace to policy, but to
959 * observe). In any case, we will _at least_ have to deal with
960 * removal of vfork return breakpoint (which we have to smuggle back
961 * in, so that the parent can see it, too), and introduction of exec*
962 * return breakpoint. Since we already have both breakpoint actions
963 * to deal with, we might as well support it all.
964 *
965 * The gist is that we pretend that the child is in a thread group
966 * with its parent, and handle it as a multi-threaded case, with the
967 * exception that we know that the parent is blocked, and don't
968 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200969 */
970
Petr Machata134a1082011-09-27 20:25:58 +0200971struct process_vfork_handler
972{
Petr Machata366c2f42012-02-09 19:34:36 +0100973 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +0200974 void * bp_addr;
975};
976
Petr Machatacbe29c62011-09-27 02:27:58 +0200977static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100978process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +0200979{
980 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +0100981 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200982 assert(self != NULL);
983
984 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200985 case EVENT_BREAKPOINT:
986 /* Remember the vfork return breakpoint. */
987 if (self->bp_addr == NULL)
988 self->bp_addr = event->e_un.brk_addr;
989 break;
990
Petr Machatacbe29c62011-09-27 02:27:58 +0200991 case EVENT_EXIT:
992 case EVENT_EXIT_SIGNAL:
993 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200994 /* Smuggle back in the vfork return breakpoint, so
995 * that our parent can trip over it once again. */
996 if (self->bp_addr != NULL) {
997 sbp = dict_find_entry(event->proc->leader->breakpoints,
998 self->bp_addr);
999 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +02001000 insert_breakpoint(event->proc->parent,
Petr Machata9df15012012-02-20 12:49:46 +01001001 self->bp_addr, sbp->libsym);
Petr Machata134a1082011-09-27 20:25:58 +02001002 }
1003
Petr Machataba9911f2011-09-27 21:09:47 +02001004 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001005
1006 /* Remove the leader that we artificially set up
1007 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001008 change_process_leader(event->proc, event->proc);
1009 destroy_event_handler(event->proc);
1010
Petr Machatacbe29c62011-09-27 02:27:58 +02001011 default:
1012 ;
1013 }
1014
1015 return event;
1016}
1017
1018void
1019continue_after_vfork(Process * proc)
1020{
1021 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001022 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001023 if (handler == NULL) {
1024 perror("malloc vfork handler");
1025 /* Carry on not bothering to treat the process as
1026 * necessary. */
1027 continue_process(proc->parent->pid);
1028 return;
1029 }
1030
1031 /* We must set up custom event handler, so that we see
1032 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001033 handler->super.on_event = process_vfork_on_event;
1034 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001035
1036 /* Make sure that the child is sole thread. */
1037 assert(proc->leader == proc);
1038 assert(proc->next == NULL || proc->next->leader != proc);
1039
1040 /* Make sure that the child's parent is properly set up. */
1041 assert(proc->parent != NULL);
1042 assert(proc->parent->leader != NULL);
1043
1044 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001045}
1046
Petr Machata9d29b3e2011-11-09 16:46:56 +01001047static int
1048is_mid_stopping(Process *proc)
1049{
1050 return proc != NULL
1051 && proc->event_handler != NULL
1052 && proc->event_handler->on_event == &process_stopping_on_event;
1053}
1054
Petr Machata43d2fe52011-11-02 13:25:49 +01001055void
1056continue_after_syscall(Process * proc, int sysnum, int ret_p)
1057{
1058 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001059 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1060 debug(DEBUG_PROCESS,
1061 "continue_after_syscall: don't continue %d",
1062 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001063 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001064 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001065 continue_process(proc->pid);
1066}
1067
Petr Machata602330f2011-07-09 11:15:34 +02001068/* If ltrace gets SIGINT, the processes directly or indirectly run by
1069 * ltrace get it too. We just have to wait long enough for the signal
1070 * to be delivered and the process terminated, which we notice and
1071 * exit ltrace, too. So there's not much we need to do there. We
1072 * want to keep tracing those processes as usual, in case they just
1073 * SIG_IGN the SIGINT to do their shutdown etc.
1074 *
1075 * For processes ran on the background, we want to install an exit
1076 * handler that stops all the threads, removes all breakpoints, and
1077 * detaches.
1078 */
1079void
Petr Machataffe4cd22012-04-11 18:01:44 +02001080os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001081{
1082 struct opt_p_t * it;
1083 for (it = opt_p; it != NULL; it = it->next) {
1084 Process * proc = pid2proc(it->pid);
1085 if (proc == NULL || proc->leader == NULL)
1086 continue;
1087 if (ltrace_exiting_install_handler(proc->leader) < 0)
1088 fprintf(stderr,
1089 "Couldn't install exiting handler for %d.\n",
1090 proc->pid);
1091 }
1092}
1093
Petr Machataffe4cd22012-04-11 18:01:44 +02001094int
1095os_ltrace_exiting_sighandler(void)
1096{
1097 extern int linux_in_waitpid;
1098 if (linux_in_waitpid) {
1099 os_ltrace_exiting();
1100 return 1;
1101 }
1102 return 0;
1103}
1104
Joe Damatodfa3fa32010-11-08 15:47:35 -08001105size_t
1106umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1107
1108 union {
1109 long a;
1110 char c[sizeof(long)];
1111 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001112 int started = 0;
1113 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001114
1115 while (offset < len) {
1116 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1117 if (a.a == -1 && errno) {
1118 if (started && errno == EIO)
1119 return bytes_read;
1120 else
1121 return -1;
1122 }
1123 started = 1;
1124
1125 if (len - offset >= sizeof(long)) {
1126 memcpy(laddr + offset, &a.c[0], sizeof(long));
1127 bytes_read += sizeof(long);
1128 }
1129 else {
1130 memcpy(laddr + offset, &a.c[0], len - offset);
1131 bytes_read += (len - offset);
1132 }
1133 offset += sizeof(long);
1134 }
1135
1136 return bytes_read;
1137}
1138
Steve Fink7bafff02006-08-07 04:50:42 +02001139/* Read a series of bytes starting at the process's memory address
1140 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1141 have been read.
1142*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001143int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001144umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001145 union {
1146 long a;
1147 char c[sizeof(long)];
1148 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001149 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001150 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001151
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001152 while (offset < len) {
1153 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1154 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001155 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001156 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001157 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001158 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001159 return 0;
1160 }
1161 }
1162 offset += sizeof(long);
1163 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001164 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001165 return 0;
1166}