blob: af9423c7f9dee162ab62af2057cc3afb8d8b9010 [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 Machata55ed83b2007-05-17 16:24:15 +020022
23/* If the system headers did not provide the constants, hard-code the normal
24 values. */
25#ifndef PTRACE_EVENT_FORK
26
27#define PTRACE_OLDSETOPTIONS 21
28#define PTRACE_SETOPTIONS 0x4200
29#define PTRACE_GETEVENTMSG 0x4201
30
31/* options set using PTRACE_SETOPTIONS */
32#define PTRACE_O_TRACESYSGOOD 0x00000001
33#define PTRACE_O_TRACEFORK 0x00000002
34#define PTRACE_O_TRACEVFORK 0x00000004
35#define PTRACE_O_TRACECLONE 0x00000008
36#define PTRACE_O_TRACEEXEC 0x00000010
37#define PTRACE_O_TRACEVFORKDONE 0x00000020
38#define PTRACE_O_TRACEEXIT 0x00000040
39
40/* Wait extended result codes for the above trace options. */
41#define PTRACE_EVENT_FORK 1
42#define PTRACE_EVENT_VFORK 2
43#define PTRACE_EVENT_CLONE 3
44#define PTRACE_EVENT_EXEC 4
45#define PTRACE_EVENT_VFORK_DONE 5
46#define PTRACE_EVENT_EXIT 6
47
48#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010049
Luis Machado55c5feb2008-03-12 15:56:01 +010050#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020051extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010052int
Juan Cespedesa8909f72009-04-28 20:02:41 +020053umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010054 return arch_umovelong (proc, addr, result, info);
55}
56#else
57/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010058int
Juan Cespedesa8909f72009-04-28 20:02:41 +020059umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010060 long pointed_to;
61
62 errno = 0;
63 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
64 if (pointed_to == -1 && errno)
65 return -errno;
66
67 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050068 if (info) {
69 switch(info->type) {
70 case ARGTYPE_INT:
71 *result &= 0x00000000ffffffffUL;
72 default:
73 break;
74 };
75 }
Luis Machado55c5feb2008-03-12 15:56:01 +010076 return 0;
77}
78#endif
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080void
Petr Machatacec06ec2012-04-10 13:31:55 +020081trace_fail_warning(pid_t pid)
82{
83 /* This was adapted from GDB. */
84#ifdef HAVE_LIBSELINUX
85 static int checked = 0;
86 if (checked)
87 return;
88 checked = 1;
89
90 /* -1 is returned for errors, 0 if it has no effect, 1 if
91 * PTRACE_ATTACH is forbidden. */
92 if (security_get_boolean_active("deny_ptrace") == 1)
93 fprintf(stderr,
94"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
95"tracing other processes. You can disable this process attach protection by\n"
96"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
97#endif /* HAVE_LIBSELINUX */
98}
99
100void
101trace_me(void)
102{
Petr Machata26627682011-07-08 18:15:32 +0200103 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100105 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200106 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107 exit(1);
108 }
109}
110
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100111/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100112 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100113 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100114int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100115wait_for_proc(pid_t pid)
116{
Petr Machatac805c622012-03-02 00:10:37 +0100117 /* man ptrace: PTRACE_ATTACH attaches to the process specified
118 in pid. The child is sent a SIGSTOP, but will not
119 necessarily have stopped by the completion of this call;
120 use wait() to wait for the child to stop. */
121 if (waitpid(pid, NULL, __WALL) != pid) {
122 perror ("trace_pid: waitpid");
123 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100124 }
125
Petr Machatac805c622012-03-02 00:10:37 +0100126 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100127}
128
Juan Cespedesf1350522008-12-16 18:19:58 +0100129int
Petr Machatacec06ec2012-04-10 13:31:55 +0200130trace_pid(pid_t pid)
131{
Petr Machata26627682011-07-08 18:15:32 +0200132 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200133 /* This shouldn't emit error messages, as there are legitimate
134 * reasons that the PID can't be attached: like it may have
135 * already ended. */
136 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100137 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100138
Petr Machatac805c622012-03-02 00:10:37 +0100139 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100140}
141
Juan Cespedesf1350522008-12-16 18:19:58 +0100142void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200143trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100144 if (proc->tracesysgood & 0x80)
145 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200146
Petr Machata26627682011-07-08 18:15:32 +0200147 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200148
Juan Cespedes1e583132009-04-07 18:17:11 +0200149 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
150 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
151 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200152 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
153 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100154 perror("PTRACE_SETOPTIONS");
155 return;
156 }
157 proc->tracesysgood |= 0x80;
158}
159
Juan Cespedesf1350522008-12-16 18:19:58 +0100160void
161untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200162 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100163 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100164}
165
Juan Cespedesf1350522008-12-16 18:19:58 +0100166void
167continue_after_signal(pid_t pid, int signum) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200168 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
Petr Machata98f09922011-07-09 10:55:29 +0200169 ptrace(PTRACE_SYSCALL, pid, 0, signum);
170}
171
172static enum ecb_status
173event_for_pid(Event * event, void * data)
174{
175 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
176 return ecb_yield;
177 return ecb_cont;
178}
179
180static int
181have_events_for(pid_t pid)
182{
183 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
184}
185
186void
187continue_process(pid_t pid)
188{
189 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200190
191 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200192 the queue for this process. Otherwise just wait for the
193 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200194 if (!have_events_for(pid))
195 /* We always trace syscalls to control fork(),
196 * clone(), execve()... */
197 ptrace(PTRACE_SYSCALL, pid, 0, 0);
198 else
199 debug(DEBUG_PROCESS,
200 "putting off the continue, events in que.");
201}
202
203/**
204 * This is used for bookkeeping related to PIDs that the event
Petr Machata750ca8c2011-10-06 14:29:34 +0200205 * handlers work with.
206 */
Petr Machata98f09922011-07-09 10:55:29 +0200207struct pid_task {
Petr Machata750ca8c2011-10-06 14:29:34 +0200208 pid_t pid; /* This may be 0 for tasks that exited
209 * mid-handling. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200210 int sigstopped : 1;
211 int got_event : 1;
212 int delivered : 1;
213 int vforked : 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100214 int sysret : 1;
Petr Machata98f09922011-07-09 10:55:29 +0200215} * pids;
216
217struct pid_set {
218 struct pid_task * tasks;
219 size_t count;
220 size_t alloc;
221};
222
223/**
224 * Breakpoint re-enablement. When we hit a breakpoint, we must
225 * disable it, single-step, and re-enable it. That single-step can be
226 * done only by one task in a task group, while others are stopped,
227 * otherwise the processes would race for who sees the breakpoint
228 * disabled and who doesn't. The following is to keep track of it
229 * all.
230 */
231struct process_stopping_handler
232{
Petr Machata366c2f42012-02-09 19:34:36 +0100233 struct event_handler super;
Petr Machata98f09922011-07-09 10:55:29 +0200234
235 /* The task that is doing the re-enablement. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200236 Process *task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200237
238 /* The pointer being re-enabled. */
Petr Machatabc373262012-02-07 23:31:15 +0100239 struct breakpoint *breakpoint_being_enabled;
Petr Machata98f09922011-07-09 10:55:29 +0200240
Petr Machataa266acb2012-04-12 23:50:23 +0200241 /* Artificial atomic skip breakpoint, if any needed. */
242 void *atomic_skip_bp_addr;
243
Petr Machatac1aa9582012-03-27 23:54:01 +0200244 /* When all tasks are stopped, this callback gets called. */
245 void (*on_all_stopped)(struct process_stopping_handler *);
246
Petr Machata98f09922011-07-09 10:55:29 +0200247 enum {
248 /* We are waiting for everyone to land in t/T. */
249 psh_stopping = 0,
250
251 /* We are doing the PTRACE_SINGLESTEP. */
252 psh_singlestep,
253
254 /* We are waiting for all the SIGSTOPs to arrive so
255 * that we can sink them. */
256 psh_sinking,
Petr Machata46d66ab2011-08-20 05:29:25 +0200257
258 /* This is for tracking the ugly workaround. */
259 psh_ugly_workaround,
Petr Machata98f09922011-07-09 10:55:29 +0200260 } state;
261
Petr Machata590c8082011-08-20 22:45:26 +0200262 int exiting;
263
Petr Machata98f09922011-07-09 10:55:29 +0200264 struct pid_set pids;
265};
266
Petr Machata98f09922011-07-09 10:55:29 +0200267static struct pid_task *
268get_task_info(struct pid_set * pids, pid_t pid)
269{
Petr Machata750ca8c2011-10-06 14:29:34 +0200270 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200271 size_t i;
272 for (i = 0; i < pids->count; ++i)
273 if (pids->tasks[i].pid == pid)
274 return &pids->tasks[i];
275
276 return NULL;
277}
278
279static struct pid_task *
280add_task_info(struct pid_set * pids, pid_t pid)
281{
282 if (pids->count == pids->alloc) {
283 size_t ns = (2 * pids->alloc) ?: 4;
284 struct pid_task * n = realloc(pids->tasks,
285 sizeof(*pids->tasks) * ns);
286 if (n == NULL)
287 return NULL;
288 pids->tasks = n;
289 pids->alloc = ns;
290 }
291 struct pid_task * task_info = &pids->tasks[pids->count++];
292 memset(task_info, 0, sizeof(*task_info));
293 task_info->pid = pid;
294 return task_info;
295}
296
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297static enum callback_status
298task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200299{
300 enum process_status st = process_status(task->pid);
301 if (data != NULL)
302 *(enum process_status *)data = st;
303
304 /* If the task is already stopped, don't worry about it.
305 * Likewise if it managed to become a zombie or terminate in
306 * the meantime. This can happen when the whole thread group
307 * is terminating. */
308 switch (st) {
309 case ps_invalid:
310 case ps_tracing_stop:
311 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200313 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200314 case ps_stop:
315 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100316 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200317 }
Petr Machata36d19822011-10-21 16:03:45 +0200318
319 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200320}
321
322/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100323static enum callback_status
324task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200325{
326 struct pid_set * pids = data;
327 struct pid_task * task_info = get_task_info(pids, task->pid);
328 if (task_info != NULL
329 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200331
332 return task_stopped(task, NULL);
333}
334
Petr Machata366c2f42012-02-09 19:34:36 +0100335static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200336
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337static enum callback_status
338task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200339{
340 if (task->event_handler != NULL
341 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100342 return CBS_STOP;
343 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200344}
345
346static int
Petr Machata74132a42012-03-16 02:46:18 +0100347is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200348{
Petr Machata74132a42012-03-16 02:46:18 +0100349 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200350}
351
Petr Machata2b46cfc2012-02-18 11:17:29 +0100352static enum callback_status
353send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200354{
355 Process * leader = task->leader;
356 struct pid_set * pids = data;
357
358 /* Look for pre-existing task record, or add new. */
359 struct pid_task * task_info = get_task_info(pids, task->pid);
360 if (task_info == NULL)
361 task_info = add_task_info(pids, task->pid);
362 if (task_info == NULL) {
363 perror("send_sigstop: add_task_info");
364 destroy_event_handler(leader);
365 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200367 }
368
369 /* This task still has not been attached to. It should be
370 stopped by the kernel. */
371 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100372 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200373
374 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200375 * if we sent the SIGSTOP already, which happens when we are
376 * handling "onexit" and inherited the handler from breakpoint
377 * re-enablement. */
378 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100379 if (task_stopped(task, &st) == CBS_CONT)
380 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200381 if (task_info->sigstopped) {
382 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100383 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200384 task_info->delivered = 0;
385 }
386
Petr Machatacbe29c62011-09-27 02:27:58 +0200387 /* Also don't attempt to stop the process if it's a parent of
388 * vforked process. We set up event handler specially to hint
389 * us. In that case parent is in D state, which we use to
390 * weed out unnecessary looping. */
391 if (st == ps_sleeping
392 && is_vfork_parent (task)) {
393 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200395 }
396
Petr Machata98f09922011-07-09 10:55:29 +0200397 if (task_kill(task->pid, SIGSTOP) >= 0) {
398 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
399 task_info->sigstopped = 1;
400 } else
401 fprintf(stderr,
402 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
403
Petr Machata2b46cfc2012-02-18 11:17:29 +0100404 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200405}
406
Petr Machata73894bd2011-08-20 23:47:34 +0200407/* On certain kernels, detaching right after a singlestep causes the
408 tracee to be killed with a SIGTRAP (that even though the singlestep
409 was properly caught by waitpid. The ugly workaround is to put a
410 breakpoint where IP points and let the process continue. After
411 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200412static void
Petr Machata73894bd2011-08-20 23:47:34 +0200413ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200414{
415 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100416 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200417 if (sbp != NULL)
418 enable_breakpoint(proc, sbp);
419 else
420 insert_breakpoint(proc, ip, NULL, 1);
Petr Machata73894bd2011-08-20 23:47:34 +0200421 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200422}
423
424static void
Petr Machata98f09922011-07-09 10:55:29 +0200425process_stopping_done(struct process_stopping_handler * self, Process * leader)
426{
427 debug(DEBUG_PROCESS, "process stopping done %d",
428 self->task_enabling_breakpoint->pid);
429 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200430 if (!self->exiting) {
431 for (i = 0; i < self->pids.count; ++i)
432 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100433 && (self->pids.tasks[i].delivered
434 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200435 continue_process(self->pids.tasks[i].pid);
436 continue_process(self->task_enabling_breakpoint->pid);
437 destroy_event_handler(leader);
438 } else {
439 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200440 ugly_workaround(self->task_enabling_breakpoint);
Petr Machata590c8082011-08-20 22:45:26 +0200441 }
442}
443
444/* Before we detach, we need to make sure that task's IP is on the
445 * edge of an instruction. So for tasks that have a breakpoint event
446 * in the queue, we adjust the instruction pointer, just like
447 * continue_after_breakpoint does. */
448static enum ecb_status
449undo_breakpoint(Event * event, void * data)
450{
451 if (event != NULL
452 && event->proc->leader == data
453 && event->type == EVENT_BREAKPOINT)
454 set_instruction_pointer(event->proc, event->e_un.brk_addr);
455 return ecb_cont;
456}
457
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458static enum callback_status
459untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200460{
461 if (task != data)
462 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100463 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200464}
465
Petr Machata2b46cfc2012-02-18 11:17:29 +0100466static enum callback_status
467remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200468{
469 /* Don't untrace leader just yet. */
470 if (task != data)
471 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100472 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200473}
474
475static void
476detach_process(Process * leader)
477{
478 each_qd_event(&undo_breakpoint, leader);
479 disable_all_breakpoints(leader);
480
481 /* Now untrace the process, if it was attached to by -p. */
482 struct opt_p_t * it;
483 for (it = opt_p; it != NULL; it = it->next) {
484 Process * proc = pid2proc(it->pid);
485 if (proc == NULL)
486 continue;
487 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100488 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200489 break;
490 }
491 }
Petr Machata74132a42012-03-16 02:46:18 +0100492 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200493 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200494 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200495}
496
497static void
498handle_stopping_event(struct pid_task * task_info, Event ** eventp)
499{
500 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200501 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200502 task_info->got_event = 1;
503
504 Event * event = *eventp;
505
506 /* In every state, sink SIGSTOP events for tasks that it was
507 * sent to. */
508 if (task_info != NULL
509 && event->type == EVENT_SIGNAL
510 && event->e_un.signum == SIGSTOP) {
511 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
512 if (task_info->sigstopped
513 && !task_info->delivered) {
514 task_info->delivered = 1;
515 *eventp = NULL; // sink the event
516 } else
517 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
518 "sigstopped=%d and delivered=%d\n",
519 task_info->pid, task_info->sigstopped,
520 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100521 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100522}
523
Petr Machata98f09922011-07-09 10:55:29 +0200524/* Some SIGSTOPs may have not been delivered to their respective tasks
525 * yet. They are still in the queue. If we have seen an event for
526 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200527 * caught by ltrace. We don't mind that the process is after
528 * breakpoint (and therefore potentially doesn't have aligned IP),
529 * because the signal will be delivered without the process actually
530 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200531static void
532continue_for_sigstop_delivery(struct pid_set * pids)
533{
534 size_t i;
535 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200536 if (pids->tasks[i].pid != 0
537 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200538 && !pids->tasks[i].delivered
539 && pids->tasks[i].got_event) {
540 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
541 pids->tasks[i].pid);
542 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
543 }
544 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100545}
546
Petr Machata98f09922011-07-09 10:55:29 +0200547static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200548event_exit_p(Event * event)
549{
550 return event != NULL && (event->type == EVENT_EXIT
551 || event->type == EVENT_EXIT_SIGNAL);
552}
553
554static int
Petr Machata98f09922011-07-09 10:55:29 +0200555event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200556{
Petr Machata750ca8c2011-10-06 14:29:34 +0200557 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200558 || event->type == EVENT_NONE;
559}
560
561static int
562await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
563 Event * event)
564{
565 /* If we still didn't get our SIGSTOP, continue the process
566 * and carry on. */
567 if (event != NULL && !event_exit_or_none_p(event)
568 && task_info != NULL && task_info->sigstopped) {
569 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
570 task_info->pid);
571 /* We should get the signal the first thing
572 * after this, so it should be OK to continue
573 * even if we are over a breakpoint. */
574 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
575
576 } else {
577 /* If all SIGSTOPs were delivered, uninstall the
578 * handler and continue everyone. */
579 /* XXX I suspect that we should check tasks that are
580 * still around. Is things are now, there should be a
581 * race between waiting for everyone to stop and one
582 * of the tasks exiting. */
583 int all_clear = 1;
584 size_t i;
585 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200586 if (pids->tasks[i].pid != 0
587 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200588 && !pids->tasks[i].delivered) {
589 all_clear = 0;
590 break;
591 }
592 return all_clear;
593 }
594
595 return 0;
596}
597
Petr Machata590c8082011-08-20 22:45:26 +0200598static int
599all_stops_accountable(struct pid_set * pids)
600{
601 size_t i;
602 for (i = 0; i < pids->count; ++i)
603 if (pids->tasks[i].pid != 0
604 && !pids->tasks[i].got_event
605 && !have_events_for(pids->tasks[i].pid))
606 return 0;
607 return 1;
608}
609
Petr Machataa266acb2012-04-12 23:50:23 +0200610/* The protocol is: 0 for success, negative for failure, positive if
611 * default singlestep is to be used. */
612int arch_atomic_singlestep(struct Process *proc, Breakpoint *sbp,
613 int (*add_cb)(void *addr, void *data),
614 void *add_cb_data);
615
616#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
617int
618arch_atomic_singlestep(struct Process *proc, Breakpoint *sbp,
619 int (*add_cb)(void *addr, void *data),
620 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100621{
Petr Machataa266acb2012-04-12 23:50:23 +0200622 return 1;
623}
624#endif
625
626static int
627atomic_singlestep_add_bp(void *addr, void *data)
628{
629 struct process_stopping_handler *self = data;
630 struct Process *proc = self->task_enabling_breakpoint;
631
632 /* Only support single address as of now. */
633 assert(self->atomic_skip_bp_addr == NULL);
634
635 self->atomic_skip_bp_addr = addr + 4;
636 insert_breakpoint(proc->leader, self->atomic_skip_bp_addr, NULL, 1);
637
638 return 0;
639}
640
641static int
642singlestep(struct process_stopping_handler *self)
643{
644 struct Process *proc = self->task_enabling_breakpoint;
645
646 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
647 self->breakpoint_being_enabled,
648 &atomic_singlestep_add_bp, self);
649
650 /* Propagate failure and success. */
651 if (status <= 0)
652 return status;
653
654 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100655 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200656 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100657 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200658 return -1;
659 }
660 return 0;
661}
662
663static void
664post_singlestep(struct process_stopping_handler *self, Event **eventp)
665{
666 continue_for_sigstop_delivery(&self->pids);
667
668 if ((*eventp)->type == EVENT_BREAKPOINT)
669 *eventp = NULL; // handled
670
671 if (self->atomic_skip_bp_addr != 0)
672 delete_breakpoint(self->task_enabling_breakpoint->leader,
673 self->atomic_skip_bp_addr);
674
675 self->breakpoint_being_enabled = NULL;
676}
677
678static void
679singlestep_error(struct process_stopping_handler *self, Event **eventp)
680{
681 struct Process *teb = self->task_enabling_breakpoint;
682 Breakpoint *sbp = self->breakpoint_being_enabled;
683 fprintf(stderr, "%d couldn't singlestep over %s (%p)\n",
684 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
685 sbp->addr);
686 delete_breakpoint(teb->leader, sbp->addr);
687 post_singlestep(self, eventp);
Petr Machata06986d52011-11-02 13:22:46 +0100688}
689
Petr Machatac1aa9582012-03-27 23:54:01 +0200690static void
691disable_and_singlestep(struct process_stopping_handler *self)
692{
693 struct Process *teb = self->task_enabling_breakpoint;
694 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d", teb->pid);
695 if (self->breakpoint_being_enabled->enabled)
696 disable_breakpoint(teb, self->breakpoint_being_enabled);
697 if (singlestep(self) < 0) {
698 singlestep_error(self, &event);
699 //goto psh_sinking; /* XXX FIME */
700 abort();
701 }
702 self->state = psh_singlestep;
703}
704
Petr Machata98f09922011-07-09 10:55:29 +0200705/* This event handler is installed when we are in the process of
706 * stopping the whole thread group to do the pointer re-enablement for
707 * one of the threads. We pump all events to the queue for later
708 * processing while we wait for all the threads to stop. When this
709 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
710 * re-enable, and continue everyone. */
711static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100712process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200713{
714 struct process_stopping_handler * self = (void *)super;
715 Process * task = event->proc;
716 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200717 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200718
719 debug(DEBUG_PROCESS,
720 "pid %d; event type %d; state %d",
721 task->pid, event->type, self->state);
722
723 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
724 if (task_info == NULL)
725 fprintf(stderr, "new task??? %d\n", task->pid);
726 handle_stopping_event(task_info, &event);
727
728 int state = self->state;
729 int event_to_queue = !event_exit_or_none_p(event);
730
Petr Machata18c97072011-10-06 14:30:11 +0200731 /* Deactivate the entry if the task exits. */
732 if (event_exit_p(event) && task_info != NULL)
733 task_info->pid = 0;
734
Petr Machata43d2fe52011-11-02 13:25:49 +0100735 /* Always handle sysrets. Whether sysret occurred and what
736 * sys it rets from may need to be determined based on process
737 * stack, so we need to keep that in sync with reality. Note
738 * that we don't continue the process after the sysret is
739 * handled. See continue_after_syscall. */
740 if (event != NULL && event->type == EVENT_SYSRET) {
741 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
742 event_to_queue = 0;
743 task_info->sysret = 1;
744 }
745
Petr Machata98f09922011-07-09 10:55:29 +0200746 switch (state) {
747 case psh_stopping:
748 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100749 if (each_task(leader, NULL, &task_blocked,
750 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200751 (self->on_all_stopped)(self);
752 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200753 }
754 break;
755
Petr Machata06986d52011-11-02 13:22:46 +0100756 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200757 /* In singlestep state, breakpoint signifies that we
758 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200759 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200760
Petr Machatac1aa9582012-03-27 23:54:01 +0200761 /* This is not the singlestep that we are
762 * waiting for. */
Petr Machatad5d93c42011-10-21 16:41:10 +0200763 if (event->type == EVENT_SIGNAL) {
Petr Machataa266acb2012-04-12 23:50:23 +0200764 if (singlestep(self) < 0) {
765 singlestep_error(self, &event);
766 goto psh_sinking;
767 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200768 break;
769 }
770
Petr Machata98f09922011-07-09 10:55:29 +0200771 /* Essentially we don't care what event caused
772 * the thread to stop. We can do the
773 * re-enablement now. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200774 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200775 if (sbp->enabled)
776 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200777
Petr Machataa266acb2012-04-12 23:50:23 +0200778 post_singlestep(self, &event);
779 goto psh_sinking;
780 }
781 break;
Petr Machata98f09922011-07-09 10:55:29 +0200782
Petr Machataa266acb2012-04-12 23:50:23 +0200783 psh_sinking:
784 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200785 case psh_sinking:
786 if (await_sigstop_delivery(&self->pids, task_info, event))
787 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200788 break;
789
790 case psh_ugly_workaround:
791 if (event == NULL)
792 break;
793 if (event->type == EVENT_BREAKPOINT) {
794 undo_breakpoint(event, leader);
795 if (task == teb)
796 self->task_enabling_breakpoint = NULL;
797 }
798 if (self->task_enabling_breakpoint == NULL
799 && all_stops_accountable(&self->pids)) {
800 undo_breakpoint(event, leader);
801 detach_process(leader);
802 event = NULL; // handled
803 }
Petr Machata98f09922011-07-09 10:55:29 +0200804 }
805
806 if (event != NULL && event_to_queue) {
807 enque_event(event);
808 event = NULL; // sink the event
809 }
810
811 return event;
812}
813
814static void
Petr Machata366c2f42012-02-09 19:34:36 +0100815process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200816{
817 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200818 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100819}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100820
Petr Machatac1aa9582012-03-27 23:54:01 +0200821int
822process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
823 void (*cb)(struct process_stopping_handler *))
824{
825 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
826 if (handler == NULL)
827 return -1;
828
829 handler->super.on_event = process_stopping_on_event;
830 handler->super.destroy = process_stopping_destroy;
831 handler->task_enabling_breakpoint = proc;
832 handler->breakpoint_being_enabled = sbp;
833 handler->on_all_stopped = cb;
834
835 install_event_handler(proc->leader, &handler->super);
836
837 if (each_task(proc->leader, NULL, &send_sigstop,
838 &handler->pids) != NULL) {
839 destroy_event_handler(proc);
840 return -1;
841 }
842
843 /* And deliver the first fake event, in case all the
844 * conditions are already fulfilled. */
845 Event ev = {
846 .type = EVENT_NONE,
847 .proc = proc,
848 };
849 process_stopping_on_event(&handler->super, &ev);
850
851 return 0;
852}
853
Juan Cespedesf1350522008-12-16 18:19:58 +0100854void
Petr Machatabc373262012-02-07 23:31:15 +0100855continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200856{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200857 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200858
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100859 if (sbp->enabled == 0) {
860 continue_process(proc->pid);
861 } else {
Petr Machata26627682011-07-08 18:15:32 +0200862 debug(DEBUG_PROCESS,
863 "continue_after_breakpoint: pid=%d, addr=%p",
864 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500865#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100866 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200867 continue_process(proc->pid);
868#else
Petr Machatac1aa9582012-03-27 23:54:01 +0200869 if (process_install_stopping_handler
870 (proc, sbp, &disable_and_singlestep) < 0) {
871 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200872 /* Carry on not bothering to re-enable. */
873 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200874 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200875#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100876 }
877}
878
Petr Machata602330f2011-07-09 11:15:34 +0200879/**
880 * Ltrace exit. When we are about to exit, we have to go through all
881 * the processes, stop them all, remove all the breakpoints, and then
882 * detach the processes that we attached to using -p. If we left the
883 * other tasks running, they might hit stray return breakpoints and
884 * produce artifacts, so we better stop everyone, even if it's a bit
885 * of extra work.
886 */
887struct ltrace_exiting_handler
888{
Petr Machata366c2f42012-02-09 19:34:36 +0100889 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200890 struct pid_set pids;
891};
892
Petr Machata602330f2011-07-09 11:15:34 +0200893static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100894ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200895{
896 struct ltrace_exiting_handler * self = (void *)super;
897 Process * task = event->proc;
898 Process * leader = task->leader;
899
900 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
901
902 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
903 handle_stopping_event(task_info, &event);
904
Petr Machata590c8082011-08-20 22:45:26 +0200905 if (event != NULL && event->type == EVENT_BREAKPOINT)
906 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200907
908 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200909 && all_stops_accountable(&self->pids))
910 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200911
912 /* Sink all non-exit events. We are about to exit, so we
913 * don't bother with queuing them. */
914 if (event_exit_or_none_p(event))
915 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200916
Petr Machata13d5df72011-08-19 23:15:15 +0200917 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200918}
919
920static void
Petr Machata366c2f42012-02-09 19:34:36 +0100921ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200922{
923 struct ltrace_exiting_handler * self = (void *)super;
924 free(self->pids.tasks);
925}
926
927static int
928ltrace_exiting_install_handler(Process * proc)
929{
930 /* Only install to leader. */
931 if (proc->leader != proc)
932 return 0;
933
934 /* Perhaps we are already installed, if the user passed
935 * several -p options that are tasks of one process. */
936 if (proc->event_handler != NULL
937 && proc->event_handler->on_event == &ltrace_exiting_on_event)
938 return 0;
939
Petr Machata590c8082011-08-20 22:45:26 +0200940 /* If stopping handler is already present, let it do the
941 * work. */
942 if (proc->event_handler != NULL) {
943 assert(proc->event_handler->on_event
944 == &process_stopping_on_event);
945 struct process_stopping_handler * other
946 = (void *)proc->event_handler;
947 other->exiting = 1;
948 return 0;
949 }
950
Petr Machata602330f2011-07-09 11:15:34 +0200951 struct ltrace_exiting_handler * handler
952 = calloc(sizeof(*handler), 1);
953 if (handler == NULL) {
954 perror("malloc exiting handler");
955 fatal:
956 /* XXXXXXXXXXXXXXXXXXX fixme */
957 return -1;
958 }
959
Petr Machata602330f2011-07-09 11:15:34 +0200960 handler->super.on_event = ltrace_exiting_on_event;
961 handler->super.destroy = ltrace_exiting_destroy;
962 install_event_handler(proc->leader, &handler->super);
963
Petr Machata74132a42012-03-16 02:46:18 +0100964 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +0200965 &handler->pids) != NULL)
966 goto fatal;
967
968 return 0;
969}
970
Petr Machatacbe29c62011-09-27 02:27:58 +0200971/*
972 * When the traced process vforks, it's suspended until the child
973 * process calls _exit or exec*. In the meantime, the two share the
974 * address space.
975 *
976 * The child process should only ever call _exit or exec*, but we
977 * can't count on that (it's not the role of ltrace to policy, but to
978 * observe). In any case, we will _at least_ have to deal with
979 * removal of vfork return breakpoint (which we have to smuggle back
980 * in, so that the parent can see it, too), and introduction of exec*
981 * return breakpoint. Since we already have both breakpoint actions
982 * to deal with, we might as well support it all.
983 *
984 * The gist is that we pretend that the child is in a thread group
985 * with its parent, and handle it as a multi-threaded case, with the
986 * exception that we know that the parent is blocked, and don't
987 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200988 */
989
Petr Machata134a1082011-09-27 20:25:58 +0200990struct process_vfork_handler
991{
Petr Machata366c2f42012-02-09 19:34:36 +0100992 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +0200993 void * bp_addr;
994};
995
Petr Machatacbe29c62011-09-27 02:27:58 +0200996static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100997process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +0200998{
999 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001000 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001001 assert(self != NULL);
1002
1003 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001004 case EVENT_BREAKPOINT:
1005 /* Remember the vfork return breakpoint. */
1006 if (self->bp_addr == NULL)
1007 self->bp_addr = event->e_un.brk_addr;
1008 break;
1009
Petr Machatacbe29c62011-09-27 02:27:58 +02001010 case EVENT_EXIT:
1011 case EVENT_EXIT_SIGNAL:
1012 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001013 /* Smuggle back in the vfork return breakpoint, so
1014 * that our parent can trip over it once again. */
1015 if (self->bp_addr != NULL) {
1016 sbp = dict_find_entry(event->proc->leader->breakpoints,
1017 self->bp_addr);
1018 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +02001019 insert_breakpoint(event->proc->parent,
1020 self->bp_addr,
1021 sbp->libsym, 1);
Petr Machata134a1082011-09-27 20:25:58 +02001022 }
1023
Petr Machataba9911f2011-09-27 21:09:47 +02001024 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001025
1026 /* Remove the leader that we artificially set up
1027 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001028 change_process_leader(event->proc, event->proc);
1029 destroy_event_handler(event->proc);
1030
Petr Machatacbe29c62011-09-27 02:27:58 +02001031 default:
1032 ;
1033 }
1034
1035 return event;
1036}
1037
1038void
1039continue_after_vfork(Process * proc)
1040{
1041 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001042 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001043 if (handler == NULL) {
1044 perror("malloc vfork handler");
1045 /* Carry on not bothering to treat the process as
1046 * necessary. */
1047 continue_process(proc->parent->pid);
1048 return;
1049 }
1050
1051 /* We must set up custom event handler, so that we see
1052 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001053 handler->super.on_event = process_vfork_on_event;
1054 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001055
1056 /* Make sure that the child is sole thread. */
1057 assert(proc->leader == proc);
1058 assert(proc->next == NULL || proc->next->leader != proc);
1059
1060 /* Make sure that the child's parent is properly set up. */
1061 assert(proc->parent != NULL);
1062 assert(proc->parent->leader != NULL);
1063
1064 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001065}
1066
Petr Machata9d29b3e2011-11-09 16:46:56 +01001067static int
1068is_mid_stopping(Process *proc)
1069{
1070 return proc != NULL
1071 && proc->event_handler != NULL
1072 && proc->event_handler->on_event == &process_stopping_on_event;
1073}
1074
Petr Machata43d2fe52011-11-02 13:25:49 +01001075void
1076continue_after_syscall(Process * proc, int sysnum, int ret_p)
1077{
1078 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001079 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1080 debug(DEBUG_PROCESS,
1081 "continue_after_syscall: don't continue %d",
1082 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001083 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001084 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001085 continue_process(proc->pid);
1086}
1087
Petr Machata602330f2011-07-09 11:15:34 +02001088/* If ltrace gets SIGINT, the processes directly or indirectly run by
1089 * ltrace get it too. We just have to wait long enough for the signal
1090 * to be delivered and the process terminated, which we notice and
1091 * exit ltrace, too. So there's not much we need to do there. We
1092 * want to keep tracing those processes as usual, in case they just
1093 * SIG_IGN the SIGINT to do their shutdown etc.
1094 *
1095 * For processes ran on the background, we want to install an exit
1096 * handler that stops all the threads, removes all breakpoints, and
1097 * detaches.
1098 */
1099void
Petr Machataffe4cd22012-04-11 18:01:44 +02001100os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001101{
1102 struct opt_p_t * it;
1103 for (it = opt_p; it != NULL; it = it->next) {
1104 Process * proc = pid2proc(it->pid);
1105 if (proc == NULL || proc->leader == NULL)
1106 continue;
1107 if (ltrace_exiting_install_handler(proc->leader) < 0)
1108 fprintf(stderr,
1109 "Couldn't install exiting handler for %d.\n",
1110 proc->pid);
1111 }
1112}
1113
Petr Machataffe4cd22012-04-11 18:01:44 +02001114int
1115os_ltrace_exiting_sighandler(void)
1116{
1117 extern int linux_in_waitpid;
1118 if (linux_in_waitpid) {
1119 os_ltrace_exiting();
1120 return 1;
1121 }
1122 return 0;
1123}
1124
Joe Damatodfa3fa32010-11-08 15:47:35 -08001125size_t
1126umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1127
1128 union {
1129 long a;
1130 char c[sizeof(long)];
1131 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001132 int started = 0;
1133 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001134
1135 while (offset < len) {
1136 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1137 if (a.a == -1 && errno) {
1138 if (started && errno == EIO)
1139 return bytes_read;
1140 else
1141 return -1;
1142 }
1143 started = 1;
1144
1145 if (len - offset >= sizeof(long)) {
1146 memcpy(laddr + offset, &a.c[0], sizeof(long));
1147 bytes_read += sizeof(long);
1148 }
1149 else {
1150 memcpy(laddr + offset, &a.c[0], len - offset);
1151 bytes_read += (len - offset);
1152 }
1153 offset += sizeof(long);
1154 }
1155
1156 return bytes_read;
1157}
1158
Steve Fink7bafff02006-08-07 04:50:42 +02001159/* Read a series of bytes starting at the process's memory address
1160 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1161 have been read.
1162*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001163int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001164umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001165 union {
1166 long a;
1167 char c[sizeof(long)];
1168 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001169 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001170 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001171
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001172 while (offset < len) {
1173 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1174 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001175 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001176 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001177 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001178 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001179 return 0;
1180 }
1181 }
1182 offset += sizeof(long);
1183 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001184 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001185 return 0;
1186}