blob: e108ea98a37c17d1464d69e39c4d3d46d3c13642 [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:
726 goto do_singlestep;
727 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200728 }
729
Petr Machata98f09922011-07-09 10:55:29 +0200730 /* Essentially we don't care what event caused
731 * the thread to stop. We can do the
732 * re-enablement now. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200733 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200734 if (sbp->enabled)
735 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200736
Petr Machataa266acb2012-04-12 23:50:23 +0200737 post_singlestep(self, &event);
738 goto psh_sinking;
739 }
740 break;
Petr Machata98f09922011-07-09 10:55:29 +0200741
Petr Machataa266acb2012-04-12 23:50:23 +0200742 psh_sinking:
743 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200744 case psh_sinking:
745 if (await_sigstop_delivery(&self->pids, task_info, event))
746 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200747 break;
748
749 case psh_ugly_workaround:
750 if (event == NULL)
751 break;
752 if (event->type == EVENT_BREAKPOINT) {
753 undo_breakpoint(event, leader);
754 if (task == teb)
755 self->task_enabling_breakpoint = NULL;
756 }
757 if (self->task_enabling_breakpoint == NULL
758 && all_stops_accountable(&self->pids)) {
759 undo_breakpoint(event, leader);
760 detach_process(leader);
761 event = NULL; // handled
762 }
Petr Machata98f09922011-07-09 10:55:29 +0200763 }
764
765 if (event != NULL && event_to_queue) {
766 enque_event(event);
767 event = NULL; // sink the event
768 }
769
770 return event;
771}
772
773static void
Petr Machata366c2f42012-02-09 19:34:36 +0100774process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200775{
776 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200777 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100778}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100779
Petr Machata36f40e72012-03-28 02:07:19 +0200780static enum callback_status
781no(struct process_stopping_handler *self)
782{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200783 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200784}
785
Petr Machatac1aa9582012-03-27 23:54:01 +0200786int
787process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200788 void (*as)(struct process_stopping_handler *),
789 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200790 (struct process_stopping_handler *),
791 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200792 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200793{
794 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
795 if (handler == NULL)
796 return -1;
797
Petr Machata36f40e72012-03-28 02:07:19 +0200798 if (as == NULL)
799 as = &disable_and_singlestep;
800 if (ks == NULL)
801 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200802 if (uw == NULL)
803 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200804
Petr Machatac1aa9582012-03-27 23:54:01 +0200805 handler->super.on_event = process_stopping_on_event;
806 handler->super.destroy = process_stopping_destroy;
807 handler->task_enabling_breakpoint = proc;
808 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200809 handler->on_all_stopped = as;
810 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200811 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200812
813 install_event_handler(proc->leader, &handler->super);
814
815 if (each_task(proc->leader, NULL, &send_sigstop,
816 &handler->pids) != NULL) {
817 destroy_event_handler(proc);
818 return -1;
819 }
820
821 /* And deliver the first fake event, in case all the
822 * conditions are already fulfilled. */
823 Event ev = {
824 .type = EVENT_NONE,
825 .proc = proc,
826 };
827 process_stopping_on_event(&handler->super, &ev);
828
829 return 0;
830}
831
Juan Cespedesf1350522008-12-16 18:19:58 +0100832void
Petr Machatabc373262012-02-07 23:31:15 +0100833continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200834{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200835 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200836
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100837 if (sbp->enabled == 0) {
838 continue_process(proc->pid);
839 } else {
Petr Machata26627682011-07-08 18:15:32 +0200840 debug(DEBUG_PROCESS,
841 "continue_after_breakpoint: pid=%d, addr=%p",
842 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500843#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100844 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200845 continue_process(proc->pid);
846#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200847 if (process_install_stopping_handler
848 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200849 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200850 /* Carry on not bothering to re-enable. */
851 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200852 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200853#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100854 }
855}
856
Petr Machata602330f2011-07-09 11:15:34 +0200857/**
858 * Ltrace exit. When we are about to exit, we have to go through all
859 * the processes, stop them all, remove all the breakpoints, and then
860 * detach the processes that we attached to using -p. If we left the
861 * other tasks running, they might hit stray return breakpoints and
862 * produce artifacts, so we better stop everyone, even if it's a bit
863 * of extra work.
864 */
865struct ltrace_exiting_handler
866{
Petr Machata366c2f42012-02-09 19:34:36 +0100867 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200868 struct pid_set pids;
869};
870
Petr Machata602330f2011-07-09 11:15:34 +0200871static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100872ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200873{
874 struct ltrace_exiting_handler * self = (void *)super;
875 Process * task = event->proc;
876 Process * leader = task->leader;
877
878 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
879
880 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
881 handle_stopping_event(task_info, &event);
882
Petr Machata590c8082011-08-20 22:45:26 +0200883 if (event != NULL && event->type == EVENT_BREAKPOINT)
884 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200885
886 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200887 && all_stops_accountable(&self->pids))
888 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200889
890 /* Sink all non-exit events. We are about to exit, so we
891 * don't bother with queuing them. */
892 if (event_exit_or_none_p(event))
893 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200894
Petr Machata13d5df72011-08-19 23:15:15 +0200895 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200896}
897
898static void
Petr Machata366c2f42012-02-09 19:34:36 +0100899ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200900{
901 struct ltrace_exiting_handler * self = (void *)super;
902 free(self->pids.tasks);
903}
904
905static int
906ltrace_exiting_install_handler(Process * proc)
907{
908 /* Only install to leader. */
909 if (proc->leader != proc)
910 return 0;
911
912 /* Perhaps we are already installed, if the user passed
913 * several -p options that are tasks of one process. */
914 if (proc->event_handler != NULL
915 && proc->event_handler->on_event == &ltrace_exiting_on_event)
916 return 0;
917
Petr Machata590c8082011-08-20 22:45:26 +0200918 /* If stopping handler is already present, let it do the
919 * work. */
920 if (proc->event_handler != NULL) {
921 assert(proc->event_handler->on_event
922 == &process_stopping_on_event);
923 struct process_stopping_handler * other
924 = (void *)proc->event_handler;
925 other->exiting = 1;
926 return 0;
927 }
928
Petr Machata602330f2011-07-09 11:15:34 +0200929 struct ltrace_exiting_handler * handler
930 = calloc(sizeof(*handler), 1);
931 if (handler == NULL) {
932 perror("malloc exiting handler");
933 fatal:
934 /* XXXXXXXXXXXXXXXXXXX fixme */
935 return -1;
936 }
937
Petr Machata602330f2011-07-09 11:15:34 +0200938 handler->super.on_event = ltrace_exiting_on_event;
939 handler->super.destroy = ltrace_exiting_destroy;
940 install_event_handler(proc->leader, &handler->super);
941
Petr Machata74132a42012-03-16 02:46:18 +0100942 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +0200943 &handler->pids) != NULL)
944 goto fatal;
945
946 return 0;
947}
948
Petr Machatacbe29c62011-09-27 02:27:58 +0200949/*
950 * When the traced process vforks, it's suspended until the child
951 * process calls _exit or exec*. In the meantime, the two share the
952 * address space.
953 *
954 * The child process should only ever call _exit or exec*, but we
955 * can't count on that (it's not the role of ltrace to policy, but to
956 * observe). In any case, we will _at least_ have to deal with
957 * removal of vfork return breakpoint (which we have to smuggle back
958 * in, so that the parent can see it, too), and introduction of exec*
959 * return breakpoint. Since we already have both breakpoint actions
960 * to deal with, we might as well support it all.
961 *
962 * The gist is that we pretend that the child is in a thread group
963 * with its parent, and handle it as a multi-threaded case, with the
964 * exception that we know that the parent is blocked, and don't
965 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200966 */
967
Petr Machata134a1082011-09-27 20:25:58 +0200968struct process_vfork_handler
969{
Petr Machata366c2f42012-02-09 19:34:36 +0100970 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +0200971 void * bp_addr;
972};
973
Petr Machatacbe29c62011-09-27 02:27:58 +0200974static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100975process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +0200976{
977 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +0100978 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200979 assert(self != NULL);
980
981 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200982 case EVENT_BREAKPOINT:
983 /* Remember the vfork return breakpoint. */
984 if (self->bp_addr == NULL)
985 self->bp_addr = event->e_un.brk_addr;
986 break;
987
Petr Machatacbe29c62011-09-27 02:27:58 +0200988 case EVENT_EXIT:
989 case EVENT_EXIT_SIGNAL:
990 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200991 /* Smuggle back in the vfork return breakpoint, so
992 * that our parent can trip over it once again. */
993 if (self->bp_addr != NULL) {
994 sbp = dict_find_entry(event->proc->leader->breakpoints,
995 self->bp_addr);
996 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +0200997 insert_breakpoint(event->proc->parent,
Petr Machata9df15012012-02-20 12:49:46 +0100998 self->bp_addr, sbp->libsym);
Petr Machata134a1082011-09-27 20:25:58 +0200999 }
1000
Petr Machataba9911f2011-09-27 21:09:47 +02001001 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001002
1003 /* Remove the leader that we artificially set up
1004 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001005 change_process_leader(event->proc, event->proc);
1006 destroy_event_handler(event->proc);
1007
Petr Machatacbe29c62011-09-27 02:27:58 +02001008 default:
1009 ;
1010 }
1011
1012 return event;
1013}
1014
1015void
1016continue_after_vfork(Process * proc)
1017{
1018 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001019 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001020 if (handler == NULL) {
1021 perror("malloc vfork handler");
1022 /* Carry on not bothering to treat the process as
1023 * necessary. */
1024 continue_process(proc->parent->pid);
1025 return;
1026 }
1027
1028 /* We must set up custom event handler, so that we see
1029 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001030 handler->super.on_event = process_vfork_on_event;
1031 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001032
1033 /* Make sure that the child is sole thread. */
1034 assert(proc->leader == proc);
1035 assert(proc->next == NULL || proc->next->leader != proc);
1036
1037 /* Make sure that the child's parent is properly set up. */
1038 assert(proc->parent != NULL);
1039 assert(proc->parent->leader != NULL);
1040
1041 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001042}
1043
Petr Machata9d29b3e2011-11-09 16:46:56 +01001044static int
1045is_mid_stopping(Process *proc)
1046{
1047 return proc != NULL
1048 && proc->event_handler != NULL
1049 && proc->event_handler->on_event == &process_stopping_on_event;
1050}
1051
Petr Machata43d2fe52011-11-02 13:25:49 +01001052void
1053continue_after_syscall(Process * proc, int sysnum, int ret_p)
1054{
1055 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001056 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1057 debug(DEBUG_PROCESS,
1058 "continue_after_syscall: don't continue %d",
1059 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001060 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001061 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001062 continue_process(proc->pid);
1063}
1064
Petr Machata602330f2011-07-09 11:15:34 +02001065/* If ltrace gets SIGINT, the processes directly or indirectly run by
1066 * ltrace get it too. We just have to wait long enough for the signal
1067 * to be delivered and the process terminated, which we notice and
1068 * exit ltrace, too. So there's not much we need to do there. We
1069 * want to keep tracing those processes as usual, in case they just
1070 * SIG_IGN the SIGINT to do their shutdown etc.
1071 *
1072 * For processes ran on the background, we want to install an exit
1073 * handler that stops all the threads, removes all breakpoints, and
1074 * detaches.
1075 */
1076void
Petr Machataffe4cd22012-04-11 18:01:44 +02001077os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001078{
1079 struct opt_p_t * it;
1080 for (it = opt_p; it != NULL; it = it->next) {
1081 Process * proc = pid2proc(it->pid);
1082 if (proc == NULL || proc->leader == NULL)
1083 continue;
1084 if (ltrace_exiting_install_handler(proc->leader) < 0)
1085 fprintf(stderr,
1086 "Couldn't install exiting handler for %d.\n",
1087 proc->pid);
1088 }
1089}
1090
Petr Machataffe4cd22012-04-11 18:01:44 +02001091int
1092os_ltrace_exiting_sighandler(void)
1093{
1094 extern int linux_in_waitpid;
1095 if (linux_in_waitpid) {
1096 os_ltrace_exiting();
1097 return 1;
1098 }
1099 return 0;
1100}
1101
Joe Damatodfa3fa32010-11-08 15:47:35 -08001102size_t
1103umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1104
1105 union {
1106 long a;
1107 char c[sizeof(long)];
1108 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001109 int started = 0;
1110 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001111
1112 while (offset < len) {
1113 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1114 if (a.a == -1 && errno) {
1115 if (started && errno == EIO)
1116 return bytes_read;
1117 else
1118 return -1;
1119 }
1120 started = 1;
1121
1122 if (len - offset >= sizeof(long)) {
1123 memcpy(laddr + offset, &a.c[0], sizeof(long));
1124 bytes_read += sizeof(long);
1125 }
1126 else {
1127 memcpy(laddr + offset, &a.c[0], len - offset);
1128 bytes_read += (len - offset);
1129 }
1130 offset += sizeof(long);
1131 }
1132
1133 return bytes_read;
1134}
1135
Steve Fink7bafff02006-08-07 04:50:42 +02001136/* Read a series of bytes starting at the process's memory address
1137 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1138 have been read.
1139*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001140int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001141umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001142 union {
1143 long a;
1144 char c[sizeof(long)];
1145 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001146 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001147 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001148
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001149 while (offset < len) {
1150 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1151 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001152 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001153 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001154 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001155 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001156 return 0;
1157 }
1158 }
1159 offset += sizeof(long);
1160 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001161 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001162 return 0;
1163}