blob: 8efd284476d3ce996b8fbdb963bbce26dec75bd7 [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
Juan Cespedesf1350522008-12-16 18:19:58 +010080int
81trace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +020082 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +010083 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010084 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010085 }
Petr Machata89a53602007-01-25 18:05:44 +010086
Juan Cespedes714ee9d2009-04-07 13:28:54 +020087 /* man ptrace: PTRACE_ATTACH attaches to the process specified
88 in pid. The child is sent a SIGSTOP, but will not
89 necessarily have stopped by the completion of this call;
90 use wait() to wait for the child to stop. */
Petr Machata9a5420c2011-07-09 11:21:23 +020091 if (waitpid (pid, NULL, __WALL) != pid) {
Juan Cespedes714ee9d2009-04-07 13:28:54 +020092 perror ("trace_pid: waitpid");
Petr Machata9a5420c2011-07-09 11:21:23 +020093 return -1;
Juan Cespedes714ee9d2009-04-07 13:28:54 +020094 }
95
Juan Cespedes273ea6d1998-03-14 23:02:40 +010096 return 0;
97}
98
Juan Cespedesf1350522008-12-16 18:19:58 +010099void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200100trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100101 if (proc->tracesysgood & 0x80)
102 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200103
Petr Machata26627682011-07-08 18:15:32 +0200104 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200105
Juan Cespedes1e583132009-04-07 18:17:11 +0200106 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
107 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
108 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200109 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
110 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100111 perror("PTRACE_SETOPTIONS");
112 return;
113 }
114 proc->tracesysgood |= 0x80;
115}
116
Juan Cespedesf1350522008-12-16 18:19:58 +0100117void
118untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200119 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100120 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100121}
122
Juan Cespedesf1350522008-12-16 18:19:58 +0100123void
124continue_after_signal(pid_t pid, int signum) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200125 Process *proc;
Juan Cespedese74c80d2009-02-11 11:32:31 +0100126
Juan Cespedescd8976d2009-05-14 13:47:58 +0200127 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
128
Juan Cespedese74c80d2009-02-11 11:32:31 +0100129 proc = pid2proc(pid);
Petr Machata98f09922011-07-09 10:55:29 +0200130 ptrace(PTRACE_SYSCALL, pid, 0, signum);
131}
132
133static enum ecb_status
134event_for_pid(Event * event, void * data)
135{
136 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
137 return ecb_yield;
138 return ecb_cont;
139}
140
141static int
142have_events_for(pid_t pid)
143{
144 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
145}
146
147void
148continue_process(pid_t pid)
149{
150 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
151 //printf("continue_process %d\n", pid);
152
153 /* Only really continue the process if there are no events in
154 the queue for this process. Otherwise just for the other
155 events to arrive. */
156 if (!have_events_for(pid))
157 /* We always trace syscalls to control fork(),
158 * clone(), execve()... */
159 ptrace(PTRACE_SYSCALL, pid, 0, 0);
160 else
161 debug(DEBUG_PROCESS,
162 "putting off the continue, events in que.");
163}
164
165/**
166 * This is used for bookkeeping related to PIDs that the event
167 * handlers work with. */
168struct pid_task {
169 pid_t pid;
170 int sigstopped;
171 int got_event;
172 int delivered;
173} * pids;
174
175struct pid_set {
176 struct pid_task * tasks;
177 size_t count;
178 size_t alloc;
179};
180
181/**
182 * Breakpoint re-enablement. When we hit a breakpoint, we must
183 * disable it, single-step, and re-enable it. That single-step can be
184 * done only by one task in a task group, while others are stopped,
185 * otherwise the processes would race for who sees the breakpoint
186 * disabled and who doesn't. The following is to keep track of it
187 * all.
188 */
189struct process_stopping_handler
190{
191 Event_Handler super;
192
193 /* The task that is doing the re-enablement. */
194 Process * task_enabling_breakpoint;
195
196 /* The pointer being re-enabled. */
197 Breakpoint * breakpoint_being_enabled;
198
199 enum {
200 /* We are waiting for everyone to land in t/T. */
201 psh_stopping = 0,
202
203 /* We are doing the PTRACE_SINGLESTEP. */
204 psh_singlestep,
205
206 /* We are waiting for all the SIGSTOPs to arrive so
207 * that we can sink them. */
208 psh_sinking,
209 } state;
210
211 struct pid_set pids;
212};
213
214static enum pcb_status
215task_stopped(Process * task, void * data)
216{
Petr Machata98f09922011-07-09 10:55:29 +0200217 /* If the task is already stopped, don't worry about it.
218 * Likewise if it managed to become a zombie or terminate in
219 * the meantime. This can happen when the whole thread group
220 * is terminating. */
Petr Machata617ff0b2011-10-06 14:23:24 +0200221 switch (process_status(task->pid)) {
222 case ps_invalid:
223 case ps_tracing_stop:
224 case ps_zombie:
Petr Machata98f09922011-07-09 10:55:29 +0200225 return pcb_cont;
Petr Machata617ff0b2011-10-06 14:23:24 +0200226 default:
227 return pcb_stop;
228 }
Petr Machata98f09922011-07-09 10:55:29 +0200229}
230
231static struct pid_task *
232get_task_info(struct pid_set * pids, pid_t pid)
233{
234 size_t i;
235 for (i = 0; i < pids->count; ++i)
236 if (pids->tasks[i].pid == pid)
237 return &pids->tasks[i];
238
239 return NULL;
240}
241
242static struct pid_task *
243add_task_info(struct pid_set * pids, pid_t pid)
244{
245 if (pids->count == pids->alloc) {
246 size_t ns = (2 * pids->alloc) ?: 4;
247 struct pid_task * n = realloc(pids->tasks,
248 sizeof(*pids->tasks) * ns);
249 if (n == NULL)
250 return NULL;
251 pids->tasks = n;
252 pids->alloc = ns;
253 }
254 struct pid_task * task_info = &pids->tasks[pids->count++];
255 memset(task_info, 0, sizeof(*task_info));
256 task_info->pid = pid;
257 return task_info;
258}
259
260static enum pcb_status
261send_sigstop(Process * task, void * data)
262{
263 Process * leader = task->leader;
264 struct pid_set * pids = data;
265
266 /* Look for pre-existing task record, or add new. */
267 struct pid_task * task_info = get_task_info(pids, task->pid);
268 if (task_info == NULL)
269 task_info = add_task_info(pids, task->pid);
270 if (task_info == NULL) {
271 perror("send_sigstop: add_task_info");
272 destroy_event_handler(leader);
273 /* Signal failure upwards. */
274 return pcb_stop;
275 }
276
277 /* This task still has not been attached to. It should be
278 stopped by the kernel. */
279 if (task->state == STATE_BEING_CREATED)
280 return pcb_cont;
281
282 /* Don't bother sending SIGSTOP if we are already stopped, or
283 * if we sent the SIGSTOP already, which happens when we
284 * inherit the handler from breakpoint re-enablement. */
285 if (task_stopped(task, NULL) == pcb_cont)
286 return pcb_cont;
287 if (task_info->sigstopped) {
288 if (!task_info->delivered)
289 return pcb_cont;
290 task_info->delivered = 0;
291 }
292
293 if (task_kill(task->pid, SIGSTOP) >= 0) {
294 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
295 task_info->sigstopped = 1;
296 } else
297 fprintf(stderr,
298 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
299
300 return pcb_cont;
301}
302
303static void
304process_stopping_done(struct process_stopping_handler * self, Process * leader)
305{
306 debug(DEBUG_PROCESS, "process stopping done %d",
307 self->task_enabling_breakpoint->pid);
308 size_t i;
309 for (i = 0; i < self->pids.count; ++i)
310 if (self->pids.tasks[i].delivered)
311 continue_process(self->pids.tasks[i].pid);
312 continue_process(self->task_enabling_breakpoint->pid);
313 destroy_event_handler(leader);
314}
315
316static void
317handle_stopping_event(struct pid_task * task_info, Event ** eventp)
318{
319 /* Mark all events, so that we know whom to SIGCONT later. */
320 if (task_info != NULL && task_info->sigstopped)
321 task_info->got_event = 1;
322
323 Event * event = *eventp;
324
325 /* In every state, sink SIGSTOP events for tasks that it was
326 * sent to. */
327 if (task_info != NULL
328 && event->type == EVENT_SIGNAL
329 && event->e_un.signum == SIGSTOP) {
330 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
331 if (task_info->sigstopped
332 && !task_info->delivered) {
333 task_info->delivered = 1;
334 *eventp = NULL; // sink the event
335 } else
336 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
337 "sigstopped=%d and delivered=%d\n",
338 task_info->pid, task_info->sigstopped,
339 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100340 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100341}
342
Petr Machata98f09922011-07-09 10:55:29 +0200343/* Some SIGSTOPs may have not been delivered to their respective tasks
344 * yet. They are still in the queue. If we have seen an event for
345 * that process, continue it, so that the SIGSTOP can be delivered and
346 * caught by ltrace. */
347static void
348continue_for_sigstop_delivery(struct pid_set * pids)
349{
350 size_t i;
351 for (i = 0; i < pids->count; ++i) {
352 if (pids->tasks[i].sigstopped
353 && !pids->tasks[i].delivered
354 && pids->tasks[i].got_event) {
355 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
356 pids->tasks[i].pid);
357 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
358 }
359 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100360}
361
Petr Machata98f09922011-07-09 10:55:29 +0200362static int
363event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200364{
Petr Machata98f09922011-07-09 10:55:29 +0200365 return event == NULL
366 || event->type == EVENT_EXIT
367 || event->type == EVENT_EXIT_SIGNAL
368 || event->type == EVENT_NONE;
369}
370
371static int
372await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
373 Event * event)
374{
375 /* If we still didn't get our SIGSTOP, continue the process
376 * and carry on. */
377 if (event != NULL && !event_exit_or_none_p(event)
378 && task_info != NULL && task_info->sigstopped) {
379 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
380 task_info->pid);
381 /* We should get the signal the first thing
382 * after this, so it should be OK to continue
383 * even if we are over a breakpoint. */
384 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
385
386 } else {
387 /* If all SIGSTOPs were delivered, uninstall the
388 * handler and continue everyone. */
389 /* XXX I suspect that we should check tasks that are
390 * still around. Is things are now, there should be a
391 * race between waiting for everyone to stop and one
392 * of the tasks exiting. */
393 int all_clear = 1;
394 size_t i;
395 for (i = 0; i < pids->count; ++i)
396 if (pids->tasks[i].sigstopped
397 && !pids->tasks[i].delivered) {
398 all_clear = 0;
399 break;
400 }
401 return all_clear;
402 }
403
404 return 0;
405}
406
407/* This event handler is installed when we are in the process of
408 * stopping the whole thread group to do the pointer re-enablement for
409 * one of the threads. We pump all events to the queue for later
410 * processing while we wait for all the threads to stop. When this
411 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
412 * re-enable, and continue everyone. */
413static Event *
414process_stopping_on_event(Event_Handler * super, Event * event)
415{
416 struct process_stopping_handler * self = (void *)super;
417 Process * task = event->proc;
418 Process * leader = task->leader;
419
420 debug(DEBUG_PROCESS,
421 "pid %d; event type %d; state %d",
422 task->pid, event->type, self->state);
423
424 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
425 if (task_info == NULL)
426 fprintf(stderr, "new task??? %d\n", task->pid);
427 handle_stopping_event(task_info, &event);
428
429 int state = self->state;
430 int event_to_queue = !event_exit_or_none_p(event);
431
432 switch (state) {
433 case psh_stopping:
434 /* If everyone is stopped, singlestep. */
435 if (each_task(leader, &task_stopped, NULL) == NULL) {
436 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
437 self->task_enabling_breakpoint->pid);
438 ptrace(PTRACE_SINGLESTEP,
439 self->task_enabling_breakpoint->pid, 0, 0);
440 self->state = state = psh_singlestep;
441 }
442 break;
443
444 case psh_singlestep: {
445 /* In singlestep state, breakpoint signifies that we
446 * have now stepped, and can re-enable the breakpoint. */
447 if (event != NULL
448 && task == self->task_enabling_breakpoint) {
449 /* Essentially we don't care what event caused
450 * the thread to stop. We can do the
451 * re-enablement now. */
452 enable_breakpoint(self->task_enabling_breakpoint,
453 self->breakpoint_being_enabled);
454
455 continue_for_sigstop_delivery(&self->pids);
456
457 self->breakpoint_being_enabled = NULL;
458 self->state = state = psh_sinking;
459
460 if (event->type == EVENT_BREAKPOINT)
461 event = NULL; // handled
462 } else
463 break;
464 }
465
466 /* fall-through */
467
468 case psh_sinking:
469 if (await_sigstop_delivery(&self->pids, task_info, event))
470 process_stopping_done(self, leader);
471 }
472
473 if (event != NULL && event_to_queue) {
474 enque_event(event);
475 event = NULL; // sink the event
476 }
477
478 return event;
479}
480
481static void
482process_stopping_destroy(Event_Handler * super)
483{
484 struct process_stopping_handler * self = (void *)super;
485 if (self->breakpoint_being_enabled != NULL)
486 enable_breakpoint(self->task_enabling_breakpoint,
487 self->breakpoint_being_enabled);
488 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100489}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100490
Juan Cespedesf1350522008-12-16 18:19:58 +0100491void
Petr Machata26627682011-07-08 18:15:32 +0200492continue_after_breakpoint(Process *proc, Breakpoint *sbp)
493{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494 if (sbp->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200495 disable_breakpoint(proc, sbp);
496
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200497 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100498 if (sbp->enabled == 0) {
499 continue_process(proc->pid);
500 } else {
Petr Machata26627682011-07-08 18:15:32 +0200501 debug(DEBUG_PROCESS,
502 "continue_after_breakpoint: pid=%d, addr=%p",
503 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500504#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100505 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200506 continue_process(proc->pid);
507#else
Petr Machata98f09922011-07-09 10:55:29 +0200508 struct process_stopping_handler * handler
509 = calloc(sizeof(*handler), 1);
510 if (handler == NULL) {
511 perror("malloc breakpoint disable handler");
512 fatal:
513 /* Carry on not bothering to re-enable. */
514 continue_process(proc->pid);
515 return;
516 }
517
518 handler->super.on_event = process_stopping_on_event;
519 handler->super.destroy = process_stopping_destroy;
520 handler->task_enabling_breakpoint = proc;
521 handler->breakpoint_being_enabled = sbp;
522 install_event_handler(proc->leader, &handler->super);
523
524 if (each_task(proc->leader, &send_sigstop,
525 &handler->pids) != NULL)
526 goto fatal;
527
528 /* And deliver the first fake event, in case all the
529 * conditions are already fulfilled. */
530 Event ev;
531 ev.type = EVENT_NONE;
532 ev.proc = proc;
533 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200534#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100535 }
536}
537
Petr Machata602330f2011-07-09 11:15:34 +0200538/**
539 * Ltrace exit. When we are about to exit, we have to go through all
540 * the processes, stop them all, remove all the breakpoints, and then
541 * detach the processes that we attached to using -p. If we left the
542 * other tasks running, they might hit stray return breakpoints and
543 * produce artifacts, so we better stop everyone, even if it's a bit
544 * of extra work.
545 */
546struct ltrace_exiting_handler
547{
548 Event_Handler super;
549 struct pid_set pids;
550};
551
552static enum pcb_status
553remove_task(Process * task, void * data)
554{
555 /* Don't untrace leader just yet. */
556 if (task != data)
557 remove_process(task);
558 return pcb_cont;
559}
560
561static enum pcb_status
562untrace_task(Process * task, void * data)
563{
564 untrace_pid(task->pid);
565 return pcb_cont;
566}
567
568static Event *
569ltrace_exiting_on_event(Event_Handler * super, Event * event)
570{
571 struct ltrace_exiting_handler * self = (void *)super;
572 Process * task = event->proc;
573 Process * leader = task->leader;
574
575 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
576
577 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
578 handle_stopping_event(task_info, &event);
579
580 if (await_sigstop_delivery(&self->pids, task_info, event)) {
581 debug(DEBUG_PROCESS, "all SIGSTOPs delivered %d", leader->pid);
582 disable_all_breakpoints(leader);
583
584 /* Now untrace the process, if it was attached to by -p. */
585 struct opt_p_t * it;
586 for (it = opt_p; it != NULL; it = it->next) {
587 Process * proc = pid2proc(it->pid);
588 if (proc == NULL)
589 continue;
590 if (proc->leader == leader) {
591 each_task(leader, &untrace_task, NULL);
592 break;
593 }
594 }
595
596 each_task(leader, &remove_task, leader);
597 destroy_event_handler(leader);
598 remove_task(leader, NULL);
599 return NULL;
600 }
601
602 /* Sink all non-exit events. We are about to exit, so we
603 * don't bother with queuing them. */
604 if (event_exit_or_none_p(event))
605 return event;
606 else
607 return NULL;
608}
609
610static void
611ltrace_exiting_destroy(Event_Handler * super)
612{
613 struct ltrace_exiting_handler * self = (void *)super;
614 free(self->pids.tasks);
615}
616
617static int
618ltrace_exiting_install_handler(Process * proc)
619{
620 /* Only install to leader. */
621 if (proc->leader != proc)
622 return 0;
623
624 /* Perhaps we are already installed, if the user passed
625 * several -p options that are tasks of one process. */
626 if (proc->event_handler != NULL
627 && proc->event_handler->on_event == &ltrace_exiting_on_event)
628 return 0;
629
630 struct ltrace_exiting_handler * handler
631 = calloc(sizeof(*handler), 1);
632 if (handler == NULL) {
633 perror("malloc exiting handler");
634 fatal:
635 /* XXXXXXXXXXXXXXXXXXX fixme */
636 return -1;
637 }
638
639 /* If we are in the middle of breakpoint, extract the
640 * pid-state information from that handler so that we can take
641 * over the SIGSTOP handling. */
642 if (proc->event_handler != NULL) {
643 debug(DEBUG_PROCESS, "taking over breakpoint handling");
644 assert(proc->event_handler->on_event
645 == &process_stopping_on_event);
646 struct process_stopping_handler * other
647 = (void *)proc->event_handler;
648 size_t i;
649 for (i = 0; i < other->pids.count; ++i) {
650 struct pid_task * oti = &other->pids.tasks[i];
651 struct pid_task * task_info
652 = add_task_info(&handler->pids, oti->pid);
653 if (task_info == NULL) {
654 perror("ltrace_exiting_install_handler"
655 ":add_task_info");
656 goto fatal;
657 }
658 /* Copy over the state. */
659 *task_info = *oti;
660 }
661
662 /* And destroy the original handler. */
663 destroy_event_handler(proc);
664 }
665
666 handler->super.on_event = ltrace_exiting_on_event;
667 handler->super.destroy = ltrace_exiting_destroy;
668 install_event_handler(proc->leader, &handler->super);
669
670 if (each_task(proc->leader, &send_sigstop,
671 &handler->pids) != NULL)
672 goto fatal;
673
674 return 0;
675}
676
677/* If ltrace gets SIGINT, the processes directly or indirectly run by
678 * ltrace get it too. We just have to wait long enough for the signal
679 * to be delivered and the process terminated, which we notice and
680 * exit ltrace, too. So there's not much we need to do there. We
681 * want to keep tracing those processes as usual, in case they just
682 * SIG_IGN the SIGINT to do their shutdown etc.
683 *
684 * For processes ran on the background, we want to install an exit
685 * handler that stops all the threads, removes all breakpoints, and
686 * detaches.
687 */
688void
689ltrace_exiting(void)
690{
691 struct opt_p_t * it;
692 for (it = opt_p; it != NULL; it = it->next) {
693 Process * proc = pid2proc(it->pid);
694 if (proc == NULL || proc->leader == NULL)
695 continue;
696 if (ltrace_exiting_install_handler(proc->leader) < 0)
697 fprintf(stderr,
698 "Couldn't install exiting handler for %d.\n",
699 proc->pid);
700 }
701}
702
Joe Damatodfa3fa32010-11-08 15:47:35 -0800703size_t
704umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
705
706 union {
707 long a;
708 char c[sizeof(long)];
709 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800710 int started = 0;
711 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800712
713 while (offset < len) {
714 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
715 if (a.a == -1 && errno) {
716 if (started && errno == EIO)
717 return bytes_read;
718 else
719 return -1;
720 }
721 started = 1;
722
723 if (len - offset >= sizeof(long)) {
724 memcpy(laddr + offset, &a.c[0], sizeof(long));
725 bytes_read += sizeof(long);
726 }
727 else {
728 memcpy(laddr + offset, &a.c[0], len - offset);
729 bytes_read += (len - offset);
730 }
731 offset += sizeof(long);
732 }
733
734 return bytes_read;
735}
736
Steve Fink7bafff02006-08-07 04:50:42 +0200737/* Read a series of bytes starting at the process's memory address
738 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
739 have been read.
740*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100741int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200742umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100743 union {
744 long a;
745 char c[sizeof(long)];
746 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800747 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100748 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100749
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100750 while (offset < len) {
751 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
752 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200753 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100754 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100755 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100756 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100757 return 0;
758 }
759 }
760 offset += sizeof(long);
761 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100762 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100763 return 0;
764}