blob: 67e1f93843f9b81bc5ace961b2ad7d6f034658fc [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01002#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01003#include <string.h>
4#include <errno.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +01005#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01006#include <sys/types.h>
Petr Machata89a53602007-01-25 18:05:44 +01007#include <sys/wait.h>
Juan Cespedes5c3fe062004-06-14 18:08:37 +02008#include "ptrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <asm/unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020010#include <assert.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010011
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020013
14/* If the system headers did not provide the constants, hard-code the normal
15 values. */
16#ifndef PTRACE_EVENT_FORK
17
18#define PTRACE_OLDSETOPTIONS 21
19#define PTRACE_SETOPTIONS 0x4200
20#define PTRACE_GETEVENTMSG 0x4201
21
22/* options set using PTRACE_SETOPTIONS */
23#define PTRACE_O_TRACESYSGOOD 0x00000001
24#define PTRACE_O_TRACEFORK 0x00000002
25#define PTRACE_O_TRACEVFORK 0x00000004
26#define PTRACE_O_TRACECLONE 0x00000008
27#define PTRACE_O_TRACEEXEC 0x00000010
28#define PTRACE_O_TRACEVFORKDONE 0x00000020
29#define PTRACE_O_TRACEEXIT 0x00000040
30
31/* Wait extended result codes for the above trace options. */
32#define PTRACE_EVENT_FORK 1
33#define PTRACE_EVENT_VFORK 2
34#define PTRACE_EVENT_CLONE 3
35#define PTRACE_EVENT_EXEC 4
36#define PTRACE_EVENT_VFORK_DONE 5
37#define PTRACE_EVENT_EXIT 6
38
39#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010040
Luis Machado55c5feb2008-03-12 15:56:01 +010041#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020042extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010043int
Juan Cespedesa8909f72009-04-28 20:02:41 +020044umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010045 return arch_umovelong (proc, addr, result, info);
46}
47#else
48/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010049int
Juan Cespedesa8909f72009-04-28 20:02:41 +020050umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010051 long pointed_to;
52
53 errno = 0;
54 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
55 if (pointed_to == -1 && errno)
56 return -errno;
57
58 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050059 if (info) {
60 switch(info->type) {
61 case ARGTYPE_INT:
62 *result &= 0x00000000ffffffffUL;
63 default:
64 break;
65 };
66 }
Luis Machado55c5feb2008-03-12 15:56:01 +010067 return 0;
68}
69#endif
70
Juan Cespedesf1350522008-12-16 18:19:58 +010071void
72trace_me(void) {
Petr Machata26627682011-07-08 18:15:32 +020073 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 perror("PTRACE_TRACEME");
76 exit(1);
77 }
78}
79
Petr Machatab4f9e0c2012-02-07 01:57:59 +010080/* There's a (hopefully) brief period of time after the child process
81 * exec's when we can't trace it yet. Here we wait for kernel to
82 * prepare the process. */
83void
84wait_for_proc(pid_t pid)
85{
86 size_t i;
87 for (i = 0; i < 100; ++i) {
88 /* We read from memory address 0, but that shouldn't
89 * be a problem: the reading will just fail. We are
90 * looking for a particular reason of failure. */
91 if (ptrace(PTRACE_PEEKTEXT, pid, 0, 0) != -1
92 || errno != ESRCH)
93 return;
94
95 usleep(1000);
96 }
97
98 fprintf(stderr, "\
99I consistently fail to read a word from the freshly launched process.\n\
100I'll now try to proceed with tracing, but this shouldn't be happening.\n");
101}
102
Juan Cespedesf1350522008-12-16 18:19:58 +0100103int
104trace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200105 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100106 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100107 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100108 }
Petr Machata89a53602007-01-25 18:05:44 +0100109
Juan Cespedes714ee9d2009-04-07 13:28:54 +0200110 /* man ptrace: PTRACE_ATTACH attaches to the process specified
111 in pid. The child is sent a SIGSTOP, but will not
112 necessarily have stopped by the completion of this call;
113 use wait() to wait for the child to stop. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200114 if (waitpid (pid, NULL, __WALL) != pid) {
Juan Cespedes714ee9d2009-04-07 13:28:54 +0200115 perror ("trace_pid: waitpid");
Petr Machata9a5420c2011-07-09 11:21:23 +0200116 return -1;
Juan Cespedes714ee9d2009-04-07 13:28:54 +0200117 }
118
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100119 return 0;
120}
121
Juan Cespedesf1350522008-12-16 18:19:58 +0100122void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200123trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100124 if (proc->tracesysgood & 0x80)
125 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200126
Petr Machata26627682011-07-08 18:15:32 +0200127 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200128
Juan Cespedes1e583132009-04-07 18:17:11 +0200129 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
130 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
131 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200132 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
133 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100134 perror("PTRACE_SETOPTIONS");
135 return;
136 }
137 proc->tracesysgood |= 0x80;
138}
139
Juan Cespedesf1350522008-12-16 18:19:58 +0100140void
141untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200142 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100143 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100144}
145
Juan Cespedesf1350522008-12-16 18:19:58 +0100146void
147continue_after_signal(pid_t pid, int signum) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200148 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
Petr Machata98f09922011-07-09 10:55:29 +0200149 ptrace(PTRACE_SYSCALL, pid, 0, signum);
150}
151
152static enum ecb_status
153event_for_pid(Event * event, void * data)
154{
155 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
156 return ecb_yield;
157 return ecb_cont;
158}
159
160static int
161have_events_for(pid_t pid)
162{
163 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
164}
165
166void
167continue_process(pid_t pid)
168{
169 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200170
171 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200172 the queue for this process. Otherwise just wait for the
173 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200174 if (!have_events_for(pid))
175 /* We always trace syscalls to control fork(),
176 * clone(), execve()... */
177 ptrace(PTRACE_SYSCALL, pid, 0, 0);
178 else
179 debug(DEBUG_PROCESS,
180 "putting off the continue, events in que.");
181}
182
183/**
184 * This is used for bookkeeping related to PIDs that the event
Petr Machata750ca8c2011-10-06 14:29:34 +0200185 * handlers work with.
186 */
Petr Machata98f09922011-07-09 10:55:29 +0200187struct pid_task {
Petr Machata750ca8c2011-10-06 14:29:34 +0200188 pid_t pid; /* This may be 0 for tasks that exited
189 * mid-handling. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200190 int sigstopped : 1;
191 int got_event : 1;
192 int delivered : 1;
193 int vforked : 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100194 int sysret : 1;
Petr Machata98f09922011-07-09 10:55:29 +0200195} * pids;
196
197struct pid_set {
198 struct pid_task * tasks;
199 size_t count;
200 size_t alloc;
201};
202
203/**
204 * Breakpoint re-enablement. When we hit a breakpoint, we must
205 * disable it, single-step, and re-enable it. That single-step can be
206 * done only by one task in a task group, while others are stopped,
207 * otherwise the processes would race for who sees the breakpoint
208 * disabled and who doesn't. The following is to keep track of it
209 * all.
210 */
211struct process_stopping_handler
212{
213 Event_Handler super;
214
215 /* The task that is doing the re-enablement. */
216 Process * task_enabling_breakpoint;
217
218 /* The pointer being re-enabled. */
219 Breakpoint * breakpoint_being_enabled;
220
221 enum {
222 /* We are waiting for everyone to land in t/T. */
223 psh_stopping = 0,
224
225 /* We are doing the PTRACE_SINGLESTEP. */
226 psh_singlestep,
227
228 /* We are waiting for all the SIGSTOPs to arrive so
229 * that we can sink them. */
230 psh_sinking,
Petr Machata46d66ab2011-08-20 05:29:25 +0200231
232 /* This is for tracking the ugly workaround. */
233 psh_ugly_workaround,
Petr Machata98f09922011-07-09 10:55:29 +0200234 } state;
235
Petr Machata590c8082011-08-20 22:45:26 +0200236 int exiting;
237
Petr Machata98f09922011-07-09 10:55:29 +0200238 struct pid_set pids;
239};
240
Petr Machata98f09922011-07-09 10:55:29 +0200241static struct pid_task *
242get_task_info(struct pid_set * pids, pid_t pid)
243{
Petr Machata750ca8c2011-10-06 14:29:34 +0200244 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200245 size_t i;
246 for (i = 0; i < pids->count; ++i)
247 if (pids->tasks[i].pid == pid)
248 return &pids->tasks[i];
249
250 return NULL;
251}
252
253static struct pid_task *
254add_task_info(struct pid_set * pids, pid_t pid)
255{
256 if (pids->count == pids->alloc) {
257 size_t ns = (2 * pids->alloc) ?: 4;
258 struct pid_task * n = realloc(pids->tasks,
259 sizeof(*pids->tasks) * ns);
260 if (n == NULL)
261 return NULL;
262 pids->tasks = n;
263 pids->alloc = ns;
264 }
265 struct pid_task * task_info = &pids->tasks[pids->count++];
266 memset(task_info, 0, sizeof(*task_info));
267 task_info->pid = pid;
268 return task_info;
269}
270
271static enum pcb_status
Petr Machatacbe29c62011-09-27 02:27:58 +0200272task_stopped(Process * task, void * data)
273{
274 enum process_status st = process_status(task->pid);
275 if (data != NULL)
276 *(enum process_status *)data = st;
277
278 /* If the task is already stopped, don't worry about it.
279 * Likewise if it managed to become a zombie or terminate in
280 * the meantime. This can happen when the whole thread group
281 * is terminating. */
282 switch (st) {
283 case ps_invalid:
284 case ps_tracing_stop:
285 case ps_zombie:
Petr Machata36d19822011-10-21 16:03:45 +0200286 case ps_sleeping:
Petr Machatacbe29c62011-09-27 02:27:58 +0200287 return pcb_cont;
Petr Machata36d19822011-10-21 16:03:45 +0200288 case ps_stop:
289 case ps_other:
Petr Machatacbe29c62011-09-27 02:27:58 +0200290 return pcb_stop;
291 }
Petr Machata36d19822011-10-21 16:03:45 +0200292
293 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200294}
295
296/* Task is blocked if it's stopped, or if it's a vfork parent. */
297static enum pcb_status
298task_blocked(Process * task, void * data)
299{
300 struct pid_set * pids = data;
301 struct pid_task * task_info = get_task_info(pids, task->pid);
302 if (task_info != NULL
303 && task_info->vforked)
304 return pcb_cont;
305
306 return task_stopped(task, NULL);
307}
308
309static Event * process_vfork_on_event(Event_Handler * super, Event * event);
310
311static enum pcb_status
312task_vforked(Process * task, void * data)
313{
314 if (task->event_handler != NULL
315 && task->event_handler->on_event == &process_vfork_on_event)
316 return pcb_stop;
317 return pcb_cont;
318}
319
320static int
321is_vfork_parent(Process * task)
322{
323 return each_task(task->leader, &task_vforked, NULL) != NULL;
324}
325
326static enum pcb_status
Petr Machata98f09922011-07-09 10:55:29 +0200327send_sigstop(Process * task, void * data)
328{
329 Process * leader = task->leader;
330 struct pid_set * pids = data;
331
332 /* Look for pre-existing task record, or add new. */
333 struct pid_task * task_info = get_task_info(pids, task->pid);
334 if (task_info == NULL)
335 task_info = add_task_info(pids, task->pid);
336 if (task_info == NULL) {
337 perror("send_sigstop: add_task_info");
338 destroy_event_handler(leader);
339 /* Signal failure upwards. */
340 return pcb_stop;
341 }
342
343 /* This task still has not been attached to. It should be
344 stopped by the kernel. */
345 if (task->state == STATE_BEING_CREATED)
346 return pcb_cont;
347
348 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200349 * if we sent the SIGSTOP already, which happens when we are
350 * handling "onexit" and inherited the handler from breakpoint
351 * re-enablement. */
352 enum process_status st;
353 if (task_stopped(task, &st) == pcb_cont)
Petr Machata98f09922011-07-09 10:55:29 +0200354 return pcb_cont;
355 if (task_info->sigstopped) {
356 if (!task_info->delivered)
357 return pcb_cont;
358 task_info->delivered = 0;
359 }
360
Petr Machatacbe29c62011-09-27 02:27:58 +0200361 /* Also don't attempt to stop the process if it's a parent of
362 * vforked process. We set up event handler specially to hint
363 * us. In that case parent is in D state, which we use to
364 * weed out unnecessary looping. */
365 if (st == ps_sleeping
366 && is_vfork_parent (task)) {
367 task_info->vforked = 1;
368 return pcb_cont;
369 }
370
Petr Machata98f09922011-07-09 10:55:29 +0200371 if (task_kill(task->pid, SIGSTOP) >= 0) {
372 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
373 task_info->sigstopped = 1;
374 } else
375 fprintf(stderr,
376 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
377
378 return pcb_cont;
379}
380
Petr Machata73894bd2011-08-20 23:47:34 +0200381/* On certain kernels, detaching right after a singlestep causes the
382 tracee to be killed with a SIGTRAP (that even though the singlestep
383 was properly caught by waitpid. The ugly workaround is to put a
384 breakpoint where IP points and let the process continue. After
385 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200386static void
Petr Machata73894bd2011-08-20 23:47:34 +0200387ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200388{
389 void * ip = get_instruction_pointer(proc);
390 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
391 if (sbp != NULL)
392 enable_breakpoint(proc, sbp);
393 else
394 insert_breakpoint(proc, ip, NULL, 1);
Petr Machata73894bd2011-08-20 23:47:34 +0200395 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200396}
397
398static void
Petr Machata98f09922011-07-09 10:55:29 +0200399process_stopping_done(struct process_stopping_handler * self, Process * leader)
400{
401 debug(DEBUG_PROCESS, "process stopping done %d",
402 self->task_enabling_breakpoint->pid);
403 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200404 if (!self->exiting) {
405 for (i = 0; i < self->pids.count; ++i)
406 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100407 && (self->pids.tasks[i].delivered
408 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200409 continue_process(self->pids.tasks[i].pid);
410 continue_process(self->task_enabling_breakpoint->pid);
411 destroy_event_handler(leader);
412 } else {
413 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200414 ugly_workaround(self->task_enabling_breakpoint);
Petr Machata590c8082011-08-20 22:45:26 +0200415 }
416}
417
418/* Before we detach, we need to make sure that task's IP is on the
419 * edge of an instruction. So for tasks that have a breakpoint event
420 * in the queue, we adjust the instruction pointer, just like
421 * continue_after_breakpoint does. */
422static enum ecb_status
423undo_breakpoint(Event * event, void * data)
424{
425 if (event != NULL
426 && event->proc->leader == data
427 && event->type == EVENT_BREAKPOINT)
428 set_instruction_pointer(event->proc, event->e_un.brk_addr);
429 return ecb_cont;
430}
431
432static enum pcb_status
433untrace_task(Process * task, void * data)
434{
435 if (task != data)
436 untrace_pid(task->pid);
437 return pcb_cont;
438}
439
440static enum pcb_status
441remove_task(Process * task, void * data)
442{
443 /* Don't untrace leader just yet. */
444 if (task != data)
445 remove_process(task);
446 return pcb_cont;
447}
448
449static void
450detach_process(Process * leader)
451{
452 each_qd_event(&undo_breakpoint, leader);
453 disable_all_breakpoints(leader);
454
455 /* Now untrace the process, if it was attached to by -p. */
456 struct opt_p_t * it;
457 for (it = opt_p; it != NULL; it = it->next) {
458 Process * proc = pid2proc(it->pid);
459 if (proc == NULL)
460 continue;
461 if (proc->leader == leader) {
462 each_task(leader, &untrace_task, NULL);
463 break;
464 }
465 }
466 each_task(leader, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200467 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200468 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200469}
470
471static void
472handle_stopping_event(struct pid_task * task_info, Event ** eventp)
473{
474 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200475 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200476 task_info->got_event = 1;
477
478 Event * event = *eventp;
479
480 /* In every state, sink SIGSTOP events for tasks that it was
481 * sent to. */
482 if (task_info != NULL
483 && event->type == EVENT_SIGNAL
484 && event->e_un.signum == SIGSTOP) {
485 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
486 if (task_info->sigstopped
487 && !task_info->delivered) {
488 task_info->delivered = 1;
489 *eventp = NULL; // sink the event
490 } else
491 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
492 "sigstopped=%d and delivered=%d\n",
493 task_info->pid, task_info->sigstopped,
494 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100495 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100496}
497
Petr Machata98f09922011-07-09 10:55:29 +0200498/* Some SIGSTOPs may have not been delivered to their respective tasks
499 * yet. They are still in the queue. If we have seen an event for
500 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200501 * caught by ltrace. We don't mind that the process is after
502 * breakpoint (and therefore potentially doesn't have aligned IP),
503 * because the signal will be delivered without the process actually
504 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200505static void
506continue_for_sigstop_delivery(struct pid_set * pids)
507{
508 size_t i;
509 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200510 if (pids->tasks[i].pid != 0
511 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200512 && !pids->tasks[i].delivered
513 && pids->tasks[i].got_event) {
514 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
515 pids->tasks[i].pid);
516 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
517 }
518 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100519}
520
Petr Machata98f09922011-07-09 10:55:29 +0200521static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200522event_exit_p(Event * event)
523{
524 return event != NULL && (event->type == EVENT_EXIT
525 || event->type == EVENT_EXIT_SIGNAL);
526}
527
528static int
Petr Machata98f09922011-07-09 10:55:29 +0200529event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200530{
Petr Machata750ca8c2011-10-06 14:29:34 +0200531 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200532 || event->type == EVENT_NONE;
533}
534
535static int
536await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
537 Event * event)
538{
539 /* If we still didn't get our SIGSTOP, continue the process
540 * and carry on. */
541 if (event != NULL && !event_exit_or_none_p(event)
542 && task_info != NULL && task_info->sigstopped) {
543 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
544 task_info->pid);
545 /* We should get the signal the first thing
546 * after this, so it should be OK to continue
547 * even if we are over a breakpoint. */
548 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
549
550 } else {
551 /* If all SIGSTOPs were delivered, uninstall the
552 * handler and continue everyone. */
553 /* XXX I suspect that we should check tasks that are
554 * still around. Is things are now, there should be a
555 * race between waiting for everyone to stop and one
556 * of the tasks exiting. */
557 int all_clear = 1;
558 size_t i;
559 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200560 if (pids->tasks[i].pid != 0
561 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200562 && !pids->tasks[i].delivered) {
563 all_clear = 0;
564 break;
565 }
566 return all_clear;
567 }
568
569 return 0;
570}
571
Petr Machata590c8082011-08-20 22:45:26 +0200572static int
573all_stops_accountable(struct pid_set * pids)
574{
575 size_t i;
576 for (i = 0; i < pids->count; ++i)
577 if (pids->tasks[i].pid != 0
578 && !pids->tasks[i].got_event
579 && !have_events_for(pids->tasks[i].pid))
580 return 0;
581 return 1;
582}
583
Petr Machata06986d52011-11-02 13:22:46 +0100584static void
585singlestep(Process * proc)
586{
587 debug(1, "PTRACE_SINGLESTEP");
588 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0))
589 perror("PTRACE_SINGLESTEP");
590}
591
Petr Machata98f09922011-07-09 10:55:29 +0200592/* This event handler is installed when we are in the process of
593 * stopping the whole thread group to do the pointer re-enablement for
594 * one of the threads. We pump all events to the queue for later
595 * processing while we wait for all the threads to stop. When this
596 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
597 * re-enable, and continue everyone. */
598static Event *
599process_stopping_on_event(Event_Handler * super, Event * event)
600{
601 struct process_stopping_handler * self = (void *)super;
602 Process * task = event->proc;
603 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200604 Breakpoint * sbp = self->breakpoint_being_enabled;
605 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200606
607 debug(DEBUG_PROCESS,
608 "pid %d; event type %d; state %d",
609 task->pid, event->type, self->state);
610
611 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
612 if (task_info == NULL)
613 fprintf(stderr, "new task??? %d\n", task->pid);
614 handle_stopping_event(task_info, &event);
615
616 int state = self->state;
617 int event_to_queue = !event_exit_or_none_p(event);
618
Petr Machata18c97072011-10-06 14:30:11 +0200619 /* Deactivate the entry if the task exits. */
620 if (event_exit_p(event) && task_info != NULL)
621 task_info->pid = 0;
622
Petr Machata43d2fe52011-11-02 13:25:49 +0100623 /* Always handle sysrets. Whether sysret occurred and what
624 * sys it rets from may need to be determined based on process
625 * stack, so we need to keep that in sync with reality. Note
626 * that we don't continue the process after the sysret is
627 * handled. See continue_after_syscall. */
628 if (event != NULL && event->type == EVENT_SYSRET) {
629 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
630 event_to_queue = 0;
631 task_info->sysret = 1;
632 }
633
Petr Machata98f09922011-07-09 10:55:29 +0200634 switch (state) {
635 case psh_stopping:
636 /* If everyone is stopped, singlestep. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200637 if (each_task(leader, &task_blocked, &self->pids) == NULL) {
Petr Machata98f09922011-07-09 10:55:29 +0200638 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200639 teb->pid);
640 if (sbp->enabled)
641 disable_breakpoint(teb, sbp);
Petr Machata06986d52011-11-02 13:22:46 +0100642 singlestep(teb);
Petr Machata98f09922011-07-09 10:55:29 +0200643 self->state = state = psh_singlestep;
644 }
645 break;
646
Petr Machata06986d52011-11-02 13:22:46 +0100647 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200648 /* In singlestep state, breakpoint signifies that we
649 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200650 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200651
Petr Machata06986d52011-11-02 13:22:46 +0100652 /* This is not the singlestep that we are waiting for. */
Petr Machatad5d93c42011-10-21 16:41:10 +0200653 if (event->type == EVENT_SIGNAL) {
Petr Machata06986d52011-11-02 13:22:46 +0100654 singlestep(task);
Petr Machatad5d93c42011-10-21 16:41:10 +0200655 break;
656 }
657
Petr Machata98f09922011-07-09 10:55:29 +0200658 /* Essentially we don't care what event caused
659 * the thread to stop. We can do the
660 * re-enablement now. */
Petr Machata590c8082011-08-20 22:45:26 +0200661 if (sbp->enabled)
662 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200663
664 continue_for_sigstop_delivery(&self->pids);
665
666 self->breakpoint_being_enabled = NULL;
667 self->state = state = psh_sinking;
668
669 if (event->type == EVENT_BREAKPOINT)
670 event = NULL; // handled
671 } else
672 break;
Petr Machata98f09922011-07-09 10:55:29 +0200673
674 /* fall-through */
675
676 case psh_sinking:
677 if (await_sigstop_delivery(&self->pids, task_info, event))
678 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200679 break;
680
681 case psh_ugly_workaround:
682 if (event == NULL)
683 break;
684 if (event->type == EVENT_BREAKPOINT) {
685 undo_breakpoint(event, leader);
686 if (task == teb)
687 self->task_enabling_breakpoint = NULL;
688 }
689 if (self->task_enabling_breakpoint == NULL
690 && all_stops_accountable(&self->pids)) {
691 undo_breakpoint(event, leader);
692 detach_process(leader);
693 event = NULL; // handled
694 }
Petr Machata98f09922011-07-09 10:55:29 +0200695 }
696
697 if (event != NULL && event_to_queue) {
698 enque_event(event);
699 event = NULL; // sink the event
700 }
701
702 return event;
703}
704
705static void
706process_stopping_destroy(Event_Handler * super)
707{
708 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200709 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100710}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100711
Juan Cespedesf1350522008-12-16 18:19:58 +0100712void
Petr Machata26627682011-07-08 18:15:32 +0200713continue_after_breakpoint(Process *proc, Breakpoint *sbp)
714{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200715 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100716 if (sbp->enabled == 0) {
717 continue_process(proc->pid);
718 } else {
Petr Machata26627682011-07-08 18:15:32 +0200719 debug(DEBUG_PROCESS,
720 "continue_after_breakpoint: pid=%d, addr=%p",
721 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500722#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100723 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200724 continue_process(proc->pid);
725#else
Petr Machata98f09922011-07-09 10:55:29 +0200726 struct process_stopping_handler * handler
727 = calloc(sizeof(*handler), 1);
728 if (handler == NULL) {
729 perror("malloc breakpoint disable handler");
730 fatal:
731 /* Carry on not bothering to re-enable. */
732 continue_process(proc->pid);
733 return;
734 }
735
736 handler->super.on_event = process_stopping_on_event;
737 handler->super.destroy = process_stopping_destroy;
738 handler->task_enabling_breakpoint = proc;
739 handler->breakpoint_being_enabled = sbp;
740 install_event_handler(proc->leader, &handler->super);
741
742 if (each_task(proc->leader, &send_sigstop,
743 &handler->pids) != NULL)
744 goto fatal;
745
746 /* And deliver the first fake event, in case all the
747 * conditions are already fulfilled. */
748 Event ev;
749 ev.type = EVENT_NONE;
750 ev.proc = proc;
751 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200752#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100753 }
754}
755
Petr Machata602330f2011-07-09 11:15:34 +0200756/**
757 * Ltrace exit. When we are about to exit, we have to go through all
758 * the processes, stop them all, remove all the breakpoints, and then
759 * detach the processes that we attached to using -p. If we left the
760 * other tasks running, they might hit stray return breakpoints and
761 * produce artifacts, so we better stop everyone, even if it's a bit
762 * of extra work.
763 */
764struct ltrace_exiting_handler
765{
766 Event_Handler super;
767 struct pid_set pids;
768};
769
Petr Machata602330f2011-07-09 11:15:34 +0200770static Event *
771ltrace_exiting_on_event(Event_Handler * super, Event * event)
772{
773 struct ltrace_exiting_handler * self = (void *)super;
774 Process * task = event->proc;
775 Process * leader = task->leader;
776
777 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
778
779 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
780 handle_stopping_event(task_info, &event);
781
Petr Machata590c8082011-08-20 22:45:26 +0200782 if (event != NULL && event->type == EVENT_BREAKPOINT)
783 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200784
785 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200786 && all_stops_accountable(&self->pids))
787 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200788
789 /* Sink all non-exit events. We are about to exit, so we
790 * don't bother with queuing them. */
791 if (event_exit_or_none_p(event))
792 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200793
Petr Machata13d5df72011-08-19 23:15:15 +0200794 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200795}
796
797static void
798ltrace_exiting_destroy(Event_Handler * super)
799{
800 struct ltrace_exiting_handler * self = (void *)super;
801 free(self->pids.tasks);
802}
803
804static int
805ltrace_exiting_install_handler(Process * proc)
806{
807 /* Only install to leader. */
808 if (proc->leader != proc)
809 return 0;
810
811 /* Perhaps we are already installed, if the user passed
812 * several -p options that are tasks of one process. */
813 if (proc->event_handler != NULL
814 && proc->event_handler->on_event == &ltrace_exiting_on_event)
815 return 0;
816
Petr Machata590c8082011-08-20 22:45:26 +0200817 /* If stopping handler is already present, let it do the
818 * work. */
819 if (proc->event_handler != NULL) {
820 assert(proc->event_handler->on_event
821 == &process_stopping_on_event);
822 struct process_stopping_handler * other
823 = (void *)proc->event_handler;
824 other->exiting = 1;
825 return 0;
826 }
827
Petr Machata602330f2011-07-09 11:15:34 +0200828 struct ltrace_exiting_handler * handler
829 = calloc(sizeof(*handler), 1);
830 if (handler == NULL) {
831 perror("malloc exiting handler");
832 fatal:
833 /* XXXXXXXXXXXXXXXXXXX fixme */
834 return -1;
835 }
836
Petr Machata602330f2011-07-09 11:15:34 +0200837 handler->super.on_event = ltrace_exiting_on_event;
838 handler->super.destroy = ltrace_exiting_destroy;
839 install_event_handler(proc->leader, &handler->super);
840
841 if (each_task(proc->leader, &send_sigstop,
842 &handler->pids) != NULL)
843 goto fatal;
844
845 return 0;
846}
847
Petr Machatacbe29c62011-09-27 02:27:58 +0200848/*
849 * When the traced process vforks, it's suspended until the child
850 * process calls _exit or exec*. In the meantime, the two share the
851 * address space.
852 *
853 * The child process should only ever call _exit or exec*, but we
854 * can't count on that (it's not the role of ltrace to policy, but to
855 * observe). In any case, we will _at least_ have to deal with
856 * removal of vfork return breakpoint (which we have to smuggle back
857 * in, so that the parent can see it, too), and introduction of exec*
858 * return breakpoint. Since we already have both breakpoint actions
859 * to deal with, we might as well support it all.
860 *
861 * The gist is that we pretend that the child is in a thread group
862 * with its parent, and handle it as a multi-threaded case, with the
863 * exception that we know that the parent is blocked, and don't
864 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200865 */
866
Petr Machata134a1082011-09-27 20:25:58 +0200867struct process_vfork_handler
868{
869 Event_Handler super;
870 void * bp_addr;
871};
872
Petr Machatacbe29c62011-09-27 02:27:58 +0200873static Event *
874process_vfork_on_event(Event_Handler * super, Event * event)
875{
876 struct process_vfork_handler * self = (void *)super;
Petr Machata134a1082011-09-27 20:25:58 +0200877 Breakpoint * sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200878 assert(self != NULL);
879
880 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200881 case EVENT_BREAKPOINT:
882 /* Remember the vfork return breakpoint. */
883 if (self->bp_addr == NULL)
884 self->bp_addr = event->e_un.brk_addr;
885 break;
886
Petr Machatacbe29c62011-09-27 02:27:58 +0200887 case EVENT_EXIT:
888 case EVENT_EXIT_SIGNAL:
889 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200890 /* Smuggle back in the vfork return breakpoint, so
891 * that our parent can trip over it once again. */
892 if (self->bp_addr != NULL) {
893 sbp = dict_find_entry(event->proc->leader->breakpoints,
894 self->bp_addr);
895 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +0200896 insert_breakpoint(event->proc->parent,
897 self->bp_addr,
898 sbp->libsym, 1);
Petr Machata134a1082011-09-27 20:25:58 +0200899 }
900
Petr Machataba9911f2011-09-27 21:09:47 +0200901 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200902
903 /* Remove the leader that we artificially set up
904 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200905 change_process_leader(event->proc, event->proc);
906 destroy_event_handler(event->proc);
907
Petr Machatacbe29c62011-09-27 02:27:58 +0200908 default:
909 ;
910 }
911
912 return event;
913}
914
915void
916continue_after_vfork(Process * proc)
917{
918 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200919 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +0200920 if (handler == NULL) {
921 perror("malloc vfork handler");
922 /* Carry on not bothering to treat the process as
923 * necessary. */
924 continue_process(proc->parent->pid);
925 return;
926 }
927
928 /* We must set up custom event handler, so that we see
929 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +0200930 handler->super.on_event = process_vfork_on_event;
931 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +0200932
933 /* Make sure that the child is sole thread. */
934 assert(proc->leader == proc);
935 assert(proc->next == NULL || proc->next->leader != proc);
936
937 /* Make sure that the child's parent is properly set up. */
938 assert(proc->parent != NULL);
939 assert(proc->parent->leader != NULL);
940
941 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +0200942}
943
Petr Machata9d29b3e2011-11-09 16:46:56 +0100944static int
945is_mid_stopping(Process *proc)
946{
947 return proc != NULL
948 && proc->event_handler != NULL
949 && proc->event_handler->on_event == &process_stopping_on_event;
950}
951
Petr Machata43d2fe52011-11-02 13:25:49 +0100952void
953continue_after_syscall(Process * proc, int sysnum, int ret_p)
954{
955 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +0100956 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
957 debug(DEBUG_PROCESS,
958 "continue_after_syscall: don't continue %d",
959 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +0100960 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +0100961 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100962 continue_process(proc->pid);
963}
964
Petr Machata602330f2011-07-09 11:15:34 +0200965/* If ltrace gets SIGINT, the processes directly or indirectly run by
966 * ltrace get it too. We just have to wait long enough for the signal
967 * to be delivered and the process terminated, which we notice and
968 * exit ltrace, too. So there's not much we need to do there. We
969 * want to keep tracing those processes as usual, in case they just
970 * SIG_IGN the SIGINT to do their shutdown etc.
971 *
972 * For processes ran on the background, we want to install an exit
973 * handler that stops all the threads, removes all breakpoints, and
974 * detaches.
975 */
976void
977ltrace_exiting(void)
978{
979 struct opt_p_t * it;
980 for (it = opt_p; it != NULL; it = it->next) {
981 Process * proc = pid2proc(it->pid);
982 if (proc == NULL || proc->leader == NULL)
983 continue;
984 if (ltrace_exiting_install_handler(proc->leader) < 0)
985 fprintf(stderr,
986 "Couldn't install exiting handler for %d.\n",
987 proc->pid);
988 }
989}
990
Joe Damatodfa3fa32010-11-08 15:47:35 -0800991size_t
992umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
993
994 union {
995 long a;
996 char c[sizeof(long)];
997 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800998 int started = 0;
999 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001000
1001 while (offset < len) {
1002 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1003 if (a.a == -1 && errno) {
1004 if (started && errno == EIO)
1005 return bytes_read;
1006 else
1007 return -1;
1008 }
1009 started = 1;
1010
1011 if (len - offset >= sizeof(long)) {
1012 memcpy(laddr + offset, &a.c[0], sizeof(long));
1013 bytes_read += sizeof(long);
1014 }
1015 else {
1016 memcpy(laddr + offset, &a.c[0], len - offset);
1017 bytes_read += (len - offset);
1018 }
1019 offset += sizeof(long);
1020 }
1021
1022 return bytes_read;
1023}
1024
Steve Fink7bafff02006-08-07 04:50:42 +02001025/* Read a series of bytes starting at the process's memory address
1026 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1027 have been read.
1028*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001029int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001030umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001031 union {
1032 long a;
1033 char c[sizeof(long)];
1034 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001035 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001036 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001037
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001038 while (offset < len) {
1039 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1040 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001041 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001042 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001043 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001044 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001045 return 0;
1046 }
1047 }
1048 offset += sizeof(long);
1049 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001050 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001051 return 0;
1052}