trace.c revision 06986d5432c92337dc2fc0bd81fa43640b48d1e2
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <errno.h> 5#include <unistd.h> 6#include <sys/types.h> 7#include <sys/wait.h> 8#include "ptrace.h" 9#include <asm/unistd.h> 10#include <assert.h> 11 12#include "common.h" 13 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 */ 40 41#ifdef ARCH_HAVE_UMOVELONG 42extern int arch_umovelong (Process *, void *, long *, arg_type_info *); 43int 44umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { 45 return arch_umovelong (proc, addr, result, info); 46} 47#else 48/* Read a single long from the process's memory address 'addr' */ 49int 50umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { 51 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; 59 if (info) { 60 switch(info->type) { 61 case ARGTYPE_INT: 62 *result &= 0x00000000ffffffffUL; 63 default: 64 break; 65 }; 66 } 67 return 0; 68} 69#endif 70 71void 72trace_me(void) { 73 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid()); 74 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { 75 perror("PTRACE_TRACEME"); 76 exit(1); 77 } 78} 79 80int 81trace_pid(pid_t pid) { 82 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid); 83 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { 84 return -1; 85 } 86 87 /* 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. */ 91 if (waitpid (pid, NULL, __WALL) != pid) { 92 perror ("trace_pid: waitpid"); 93 return -1; 94 } 95 96 return 0; 97} 98 99void 100trace_set_options(Process *proc, pid_t pid) { 101 if (proc->tracesysgood & 0x80) 102 return; 103 104 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid); 105 106 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK | 107 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | 108 PTRACE_O_TRACEEXEC; 109 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 && 110 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) { 111 perror("PTRACE_SETOPTIONS"); 112 return; 113 } 114 proc->tracesysgood |= 0x80; 115} 116 117void 118untrace_pid(pid_t pid) { 119 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid); 120 ptrace(PTRACE_DETACH, pid, 1, 0); 121} 122 123void 124continue_after_signal(pid_t pid, int signum) { 125 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum); 126 ptrace(PTRACE_SYSCALL, pid, 0, signum); 127} 128 129static enum ecb_status 130event_for_pid(Event * event, void * data) 131{ 132 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data) 133 return ecb_yield; 134 return ecb_cont; 135} 136 137static int 138have_events_for(pid_t pid) 139{ 140 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL; 141} 142 143void 144continue_process(pid_t pid) 145{ 146 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid); 147 148 /* Only really continue the process if there are no events in 149 the queue for this process. Otherwise just wait for the 150 other events to arrive. */ 151 if (!have_events_for(pid)) 152 /* We always trace syscalls to control fork(), 153 * clone(), execve()... */ 154 ptrace(PTRACE_SYSCALL, pid, 0, 0); 155 else 156 debug(DEBUG_PROCESS, 157 "putting off the continue, events in que."); 158} 159 160/** 161 * This is used for bookkeeping related to PIDs that the event 162 * handlers work with. 163 */ 164struct pid_task { 165 pid_t pid; /* This may be 0 for tasks that exited 166 * mid-handling. */ 167 int sigstopped : 1; 168 int got_event : 1; 169 int delivered : 1; 170 int vforked : 1; 171} * pids; 172 173struct pid_set { 174 struct pid_task * tasks; 175 size_t count; 176 size_t alloc; 177}; 178 179/** 180 * Breakpoint re-enablement. When we hit a breakpoint, we must 181 * disable it, single-step, and re-enable it. That single-step can be 182 * done only by one task in a task group, while others are stopped, 183 * otherwise the processes would race for who sees the breakpoint 184 * disabled and who doesn't. The following is to keep track of it 185 * all. 186 */ 187struct process_stopping_handler 188{ 189 Event_Handler super; 190 191 /* The task that is doing the re-enablement. */ 192 Process * task_enabling_breakpoint; 193 194 /* The pointer being re-enabled. */ 195 Breakpoint * breakpoint_being_enabled; 196 197 enum { 198 /* We are waiting for everyone to land in t/T. */ 199 psh_stopping = 0, 200 201 /* We are doing the PTRACE_SINGLESTEP. */ 202 psh_singlestep, 203 204 /* We are waiting for all the SIGSTOPs to arrive so 205 * that we can sink them. */ 206 psh_sinking, 207 208 /* This is for tracking the ugly workaround. */ 209 psh_ugly_workaround, 210 } state; 211 212 int exiting; 213 214 struct pid_set pids; 215}; 216 217static struct pid_task * 218get_task_info(struct pid_set * pids, pid_t pid) 219{ 220 assert(pid != 0); 221 size_t i; 222 for (i = 0; i < pids->count; ++i) 223 if (pids->tasks[i].pid == pid) 224 return &pids->tasks[i]; 225 226 return NULL; 227} 228 229static struct pid_task * 230add_task_info(struct pid_set * pids, pid_t pid) 231{ 232 if (pids->count == pids->alloc) { 233 size_t ns = (2 * pids->alloc) ?: 4; 234 struct pid_task * n = realloc(pids->tasks, 235 sizeof(*pids->tasks) * ns); 236 if (n == NULL) 237 return NULL; 238 pids->tasks = n; 239 pids->alloc = ns; 240 } 241 struct pid_task * task_info = &pids->tasks[pids->count++]; 242 memset(task_info, 0, sizeof(*task_info)); 243 task_info->pid = pid; 244 return task_info; 245} 246 247static enum pcb_status 248task_stopped(Process * task, void * data) 249{ 250 enum process_status st = process_status(task->pid); 251 if (data != NULL) 252 *(enum process_status *)data = st; 253 254 /* If the task is already stopped, don't worry about it. 255 * Likewise if it managed to become a zombie or terminate in 256 * the meantime. This can happen when the whole thread group 257 * is terminating. */ 258 switch (st) { 259 case ps_invalid: 260 case ps_tracing_stop: 261 case ps_zombie: 262 case ps_sleeping: 263 return pcb_cont; 264 case ps_stop: 265 case ps_other: 266 return pcb_stop; 267 } 268 269 abort (); 270} 271 272/* Task is blocked if it's stopped, or if it's a vfork parent. */ 273static enum pcb_status 274task_blocked(Process * task, void * data) 275{ 276 struct pid_set * pids = data; 277 struct pid_task * task_info = get_task_info(pids, task->pid); 278 if (task_info != NULL 279 && task_info->vforked) 280 return pcb_cont; 281 282 return task_stopped(task, NULL); 283} 284 285static Event * process_vfork_on_event(Event_Handler * super, Event * event); 286 287static enum pcb_status 288task_vforked(Process * task, void * data) 289{ 290 if (task->event_handler != NULL 291 && task->event_handler->on_event == &process_vfork_on_event) 292 return pcb_stop; 293 return pcb_cont; 294} 295 296static int 297is_vfork_parent(Process * task) 298{ 299 return each_task(task->leader, &task_vforked, NULL) != NULL; 300} 301 302static enum pcb_status 303send_sigstop(Process * task, void * data) 304{ 305 Process * leader = task->leader; 306 struct pid_set * pids = data; 307 308 /* Look for pre-existing task record, or add new. */ 309 struct pid_task * task_info = get_task_info(pids, task->pid); 310 if (task_info == NULL) 311 task_info = add_task_info(pids, task->pid); 312 if (task_info == NULL) { 313 perror("send_sigstop: add_task_info"); 314 destroy_event_handler(leader); 315 /* Signal failure upwards. */ 316 return pcb_stop; 317 } 318 319 /* This task still has not been attached to. It should be 320 stopped by the kernel. */ 321 if (task->state == STATE_BEING_CREATED) 322 return pcb_cont; 323 324 /* Don't bother sending SIGSTOP if we are already stopped, or 325 * if we sent the SIGSTOP already, which happens when we are 326 * handling "onexit" and inherited the handler from breakpoint 327 * re-enablement. */ 328 enum process_status st; 329 if (task_stopped(task, &st) == pcb_cont) 330 return pcb_cont; 331 if (task_info->sigstopped) { 332 if (!task_info->delivered) 333 return pcb_cont; 334 task_info->delivered = 0; 335 } 336 337 /* Also don't attempt to stop the process if it's a parent of 338 * vforked process. We set up event handler specially to hint 339 * us. In that case parent is in D state, which we use to 340 * weed out unnecessary looping. */ 341 if (st == ps_sleeping 342 && is_vfork_parent (task)) { 343 task_info->vforked = 1; 344 return pcb_cont; 345 } 346 347 if (task_kill(task->pid, SIGSTOP) >= 0) { 348 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid); 349 task_info->sigstopped = 1; 350 } else 351 fprintf(stderr, 352 "Warning: couldn't send SIGSTOP to %d\n", task->pid); 353 354 return pcb_cont; 355} 356 357/* On certain kernels, detaching right after a singlestep causes the 358 tracee to be killed with a SIGTRAP (that even though the singlestep 359 was properly caught by waitpid. The ugly workaround is to put a 360 breakpoint where IP points and let the process continue. After 361 this the breakpoint can be retracted and the process detached. */ 362static void 363ugly_workaround(Process * proc) 364{ 365 void * ip = get_instruction_pointer(proc); 366 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip); 367 if (sbp != NULL) 368 enable_breakpoint(proc, sbp); 369 else 370 insert_breakpoint(proc, ip, NULL, 1); 371 ptrace(PTRACE_CONT, proc->pid, 0, 0); 372} 373 374static void 375process_stopping_done(struct process_stopping_handler * self, Process * leader) 376{ 377 debug(DEBUG_PROCESS, "process stopping done %d", 378 self->task_enabling_breakpoint->pid); 379 size_t i; 380 if (!self->exiting) { 381 for (i = 0; i < self->pids.count; ++i) 382 if (self->pids.tasks[i].pid != 0 383 && self->pids.tasks[i].delivered) 384 continue_process(self->pids.tasks[i].pid); 385 continue_process(self->task_enabling_breakpoint->pid); 386 destroy_event_handler(leader); 387 } else { 388 self->state = psh_ugly_workaround; 389 ugly_workaround(self->task_enabling_breakpoint); 390 } 391} 392 393/* Before we detach, we need to make sure that task's IP is on the 394 * edge of an instruction. So for tasks that have a breakpoint event 395 * in the queue, we adjust the instruction pointer, just like 396 * continue_after_breakpoint does. */ 397static enum ecb_status 398undo_breakpoint(Event * event, void * data) 399{ 400 if (event != NULL 401 && event->proc->leader == data 402 && event->type == EVENT_BREAKPOINT) 403 set_instruction_pointer(event->proc, event->e_un.brk_addr); 404 return ecb_cont; 405} 406 407static enum pcb_status 408untrace_task(Process * task, void * data) 409{ 410 if (task != data) 411 untrace_pid(task->pid); 412 return pcb_cont; 413} 414 415static enum pcb_status 416remove_task(Process * task, void * data) 417{ 418 /* Don't untrace leader just yet. */ 419 if (task != data) 420 remove_process(task); 421 return pcb_cont; 422} 423 424static void 425detach_process(Process * leader) 426{ 427 each_qd_event(&undo_breakpoint, leader); 428 disable_all_breakpoints(leader); 429 430 /* Now untrace the process, if it was attached to by -p. */ 431 struct opt_p_t * it; 432 for (it = opt_p; it != NULL; it = it->next) { 433 Process * proc = pid2proc(it->pid); 434 if (proc == NULL) 435 continue; 436 if (proc->leader == leader) { 437 each_task(leader, &untrace_task, NULL); 438 break; 439 } 440 } 441 each_task(leader, &remove_task, leader); 442 destroy_event_handler(leader); 443 remove_task(leader, NULL); 444} 445 446static void 447handle_stopping_event(struct pid_task * task_info, Event ** eventp) 448{ 449 /* Mark all events, so that we know whom to SIGCONT later. */ 450 if (task_info != NULL) 451 task_info->got_event = 1; 452 453 Event * event = *eventp; 454 455 /* In every state, sink SIGSTOP events for tasks that it was 456 * sent to. */ 457 if (task_info != NULL 458 && event->type == EVENT_SIGNAL 459 && event->e_un.signum == SIGSTOP) { 460 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid); 461 if (task_info->sigstopped 462 && !task_info->delivered) { 463 task_info->delivered = 1; 464 *eventp = NULL; // sink the event 465 } else 466 fprintf(stderr, "suspicious: %d got SIGSTOP, but " 467 "sigstopped=%d and delivered=%d\n", 468 task_info->pid, task_info->sigstopped, 469 task_info->delivered); 470 } 471} 472 473/* Some SIGSTOPs may have not been delivered to their respective tasks 474 * yet. They are still in the queue. If we have seen an event for 475 * that process, continue it, so that the SIGSTOP can be delivered and 476 * caught by ltrace. We don't mind that the process is after 477 * breakpoint (and therefore potentially doesn't have aligned IP), 478 * because the signal will be delivered without the process actually 479 * starting. */ 480static void 481continue_for_sigstop_delivery(struct pid_set * pids) 482{ 483 size_t i; 484 for (i = 0; i < pids->count; ++i) { 485 if (pids->tasks[i].pid != 0 486 && pids->tasks[i].sigstopped 487 && !pids->tasks[i].delivered 488 && pids->tasks[i].got_event) { 489 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", 490 pids->tasks[i].pid); 491 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0); 492 } 493 } 494} 495 496static int 497event_exit_p(Event * event) 498{ 499 return event != NULL && (event->type == EVENT_EXIT 500 || event->type == EVENT_EXIT_SIGNAL); 501} 502 503static int 504event_exit_or_none_p(Event * event) 505{ 506 return event == NULL || event_exit_p(event) 507 || event->type == EVENT_NONE; 508} 509 510static int 511await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info, 512 Event * event) 513{ 514 /* If we still didn't get our SIGSTOP, continue the process 515 * and carry on. */ 516 if (event != NULL && !event_exit_or_none_p(event) 517 && task_info != NULL && task_info->sigstopped) { 518 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", 519 task_info->pid); 520 /* We should get the signal the first thing 521 * after this, so it should be OK to continue 522 * even if we are over a breakpoint. */ 523 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0); 524 525 } else { 526 /* If all SIGSTOPs were delivered, uninstall the 527 * handler and continue everyone. */ 528 /* XXX I suspect that we should check tasks that are 529 * still around. Is things are now, there should be a 530 * race between waiting for everyone to stop and one 531 * of the tasks exiting. */ 532 int all_clear = 1; 533 size_t i; 534 for (i = 0; i < pids->count; ++i) 535 if (pids->tasks[i].pid != 0 536 && pids->tasks[i].sigstopped 537 && !pids->tasks[i].delivered) { 538 all_clear = 0; 539 break; 540 } 541 return all_clear; 542 } 543 544 return 0; 545} 546 547static int 548all_stops_accountable(struct pid_set * pids) 549{ 550 size_t i; 551 for (i = 0; i < pids->count; ++i) 552 if (pids->tasks[i].pid != 0 553 && !pids->tasks[i].got_event 554 && !have_events_for(pids->tasks[i].pid)) 555 return 0; 556 return 1; 557} 558 559static void 560singlestep(Process * proc) 561{ 562 debug(1, "PTRACE_SINGLESTEP"); 563 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) 564 perror("PTRACE_SINGLESTEP"); 565} 566 567/* This event handler is installed when we are in the process of 568 * stopping the whole thread group to do the pointer re-enablement for 569 * one of the threads. We pump all events to the queue for later 570 * processing while we wait for all the threads to stop. When this 571 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP, 572 * re-enable, and continue everyone. */ 573static Event * 574process_stopping_on_event(Event_Handler * super, Event * event) 575{ 576 struct process_stopping_handler * self = (void *)super; 577 Process * task = event->proc; 578 Process * leader = task->leader; 579 Breakpoint * sbp = self->breakpoint_being_enabled; 580 Process * teb = self->task_enabling_breakpoint; 581 582 debug(DEBUG_PROCESS, 583 "pid %d; event type %d; state %d", 584 task->pid, event->type, self->state); 585 586 struct pid_task * task_info = get_task_info(&self->pids, task->pid); 587 if (task_info == NULL) 588 fprintf(stderr, "new task??? %d\n", task->pid); 589 handle_stopping_event(task_info, &event); 590 591 int state = self->state; 592 int event_to_queue = !event_exit_or_none_p(event); 593 594 /* Deactivate the entry if the task exits. */ 595 if (event_exit_p(event) && task_info != NULL) 596 task_info->pid = 0; 597 598 switch (state) { 599 case psh_stopping: 600 /* If everyone is stopped, singlestep. */ 601 if (each_task(leader, &task_blocked, &self->pids) == NULL) { 602 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d", 603 teb->pid); 604 if (sbp->enabled) 605 disable_breakpoint(teb, sbp); 606 singlestep(teb); 607 self->state = state = psh_singlestep; 608 } 609 break; 610 611 case psh_singlestep: 612 /* In singlestep state, breakpoint signifies that we 613 * have now stepped, and can re-enable the breakpoint. */ 614 if (event != NULL && task == teb) { 615 616 /* This is not the singlestep that we are waiting for. */ 617 if (event->type == EVENT_SIGNAL) { 618 singlestep(task); 619 break; 620 } 621 622 /* Essentially we don't care what event caused 623 * the thread to stop. We can do the 624 * re-enablement now. */ 625 if (sbp->enabled) 626 enable_breakpoint(teb, sbp); 627 628 continue_for_sigstop_delivery(&self->pids); 629 630 self->breakpoint_being_enabled = NULL; 631 self->state = state = psh_sinking; 632 633 if (event->type == EVENT_BREAKPOINT) 634 event = NULL; // handled 635 } else 636 break; 637 638 /* fall-through */ 639 640 case psh_sinking: 641 if (await_sigstop_delivery(&self->pids, task_info, event)) 642 process_stopping_done(self, leader); 643 break; 644 645 case psh_ugly_workaround: 646 if (event == NULL) 647 break; 648 if (event->type == EVENT_BREAKPOINT) { 649 undo_breakpoint(event, leader); 650 if (task == teb) 651 self->task_enabling_breakpoint = NULL; 652 } 653 if (self->task_enabling_breakpoint == NULL 654 && all_stops_accountable(&self->pids)) { 655 undo_breakpoint(event, leader); 656 detach_process(leader); 657 event = NULL; // handled 658 } 659 } 660 661 if (event != NULL && event_to_queue) { 662 enque_event(event); 663 event = NULL; // sink the event 664 } 665 666 return event; 667} 668 669static void 670process_stopping_destroy(Event_Handler * super) 671{ 672 struct process_stopping_handler * self = (void *)super; 673 free(self->pids.tasks); 674} 675 676void 677continue_after_breakpoint(Process *proc, Breakpoint *sbp) 678{ 679 set_instruction_pointer(proc, sbp->addr); 680 if (sbp->enabled == 0) { 681 continue_process(proc->pid); 682 } else { 683 debug(DEBUG_PROCESS, 684 "continue_after_breakpoint: pid=%d, addr=%p", 685 proc->pid, sbp->addr); 686#if defined __sparc__ || defined __ia64___ || defined __mips__ 687 /* we don't want to singlestep here */ 688 continue_process(proc->pid); 689#else 690 struct process_stopping_handler * handler 691 = calloc(sizeof(*handler), 1); 692 if (handler == NULL) { 693 perror("malloc breakpoint disable handler"); 694 fatal: 695 /* Carry on not bothering to re-enable. */ 696 continue_process(proc->pid); 697 return; 698 } 699 700 handler->super.on_event = process_stopping_on_event; 701 handler->super.destroy = process_stopping_destroy; 702 handler->task_enabling_breakpoint = proc; 703 handler->breakpoint_being_enabled = sbp; 704 install_event_handler(proc->leader, &handler->super); 705 706 if (each_task(proc->leader, &send_sigstop, 707 &handler->pids) != NULL) 708 goto fatal; 709 710 /* And deliver the first fake event, in case all the 711 * conditions are already fulfilled. */ 712 Event ev; 713 ev.type = EVENT_NONE; 714 ev.proc = proc; 715 process_stopping_on_event(&handler->super, &ev); 716#endif 717 } 718} 719 720/** 721 * Ltrace exit. When we are about to exit, we have to go through all 722 * the processes, stop them all, remove all the breakpoints, and then 723 * detach the processes that we attached to using -p. If we left the 724 * other tasks running, they might hit stray return breakpoints and 725 * produce artifacts, so we better stop everyone, even if it's a bit 726 * of extra work. 727 */ 728struct ltrace_exiting_handler 729{ 730 Event_Handler super; 731 struct pid_set pids; 732}; 733 734static Event * 735ltrace_exiting_on_event(Event_Handler * super, Event * event) 736{ 737 struct ltrace_exiting_handler * self = (void *)super; 738 Process * task = event->proc; 739 Process * leader = task->leader; 740 741 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type); 742 743 struct pid_task * task_info = get_task_info(&self->pids, task->pid); 744 handle_stopping_event(task_info, &event); 745 746 if (event != NULL && event->type == EVENT_BREAKPOINT) 747 undo_breakpoint(event, leader); 748 749 if (await_sigstop_delivery(&self->pids, task_info, event) 750 && all_stops_accountable(&self->pids)) 751 detach_process(leader); 752 753 /* Sink all non-exit events. We are about to exit, so we 754 * don't bother with queuing them. */ 755 if (event_exit_or_none_p(event)) 756 return event; 757 758 return NULL; 759} 760 761static void 762ltrace_exiting_destroy(Event_Handler * super) 763{ 764 struct ltrace_exiting_handler * self = (void *)super; 765 free(self->pids.tasks); 766} 767 768static int 769ltrace_exiting_install_handler(Process * proc) 770{ 771 /* Only install to leader. */ 772 if (proc->leader != proc) 773 return 0; 774 775 /* Perhaps we are already installed, if the user passed 776 * several -p options that are tasks of one process. */ 777 if (proc->event_handler != NULL 778 && proc->event_handler->on_event == <race_exiting_on_event) 779 return 0; 780 781 /* If stopping handler is already present, let it do the 782 * work. */ 783 if (proc->event_handler != NULL) { 784 assert(proc->event_handler->on_event 785 == &process_stopping_on_event); 786 struct process_stopping_handler * other 787 = (void *)proc->event_handler; 788 other->exiting = 1; 789 return 0; 790 } 791 792 struct ltrace_exiting_handler * handler 793 = calloc(sizeof(*handler), 1); 794 if (handler == NULL) { 795 perror("malloc exiting handler"); 796 fatal: 797 /* XXXXXXXXXXXXXXXXXXX fixme */ 798 return -1; 799 } 800 801 handler->super.on_event = ltrace_exiting_on_event; 802 handler->super.destroy = ltrace_exiting_destroy; 803 install_event_handler(proc->leader, &handler->super); 804 805 if (each_task(proc->leader, &send_sigstop, 806 &handler->pids) != NULL) 807 goto fatal; 808 809 return 0; 810} 811 812/* 813 * When the traced process vforks, it's suspended until the child 814 * process calls _exit or exec*. In the meantime, the two share the 815 * address space. 816 * 817 * The child process should only ever call _exit or exec*, but we 818 * can't count on that (it's not the role of ltrace to policy, but to 819 * observe). In any case, we will _at least_ have to deal with 820 * removal of vfork return breakpoint (which we have to smuggle back 821 * in, so that the parent can see it, too), and introduction of exec* 822 * return breakpoint. Since we already have both breakpoint actions 823 * to deal with, we might as well support it all. 824 * 825 * The gist is that we pretend that the child is in a thread group 826 * with its parent, and handle it as a multi-threaded case, with the 827 * exception that we know that the parent is blocked, and don't 828 * attempt to stop it. When the child execs, we undo the setup. 829 */ 830 831struct process_vfork_handler 832{ 833 Event_Handler super; 834 void * bp_addr; 835}; 836 837static Event * 838process_vfork_on_event(Event_Handler * super, Event * event) 839{ 840 struct process_vfork_handler * self = (void *)super; 841 Breakpoint * sbp; 842 assert(self != NULL); 843 844 switch (event->type) { 845 case EVENT_BREAKPOINT: 846 /* Remember the vfork return breakpoint. */ 847 if (self->bp_addr == NULL) 848 self->bp_addr = event->e_un.brk_addr; 849 break; 850 851 case EVENT_EXIT: 852 case EVENT_EXIT_SIGNAL: 853 case EVENT_EXEC: 854 /* Smuggle back in the vfork return breakpoint, so 855 * that our parent can trip over it once again. */ 856 if (self->bp_addr != NULL) { 857 sbp = dict_find_entry(event->proc->leader->breakpoints, 858 self->bp_addr); 859 if (sbp != NULL) 860 insert_breakpoint(event->proc->parent, 861 self->bp_addr, 862 sbp->libsym, 1); 863 } 864 865 continue_process(event->proc->parent->pid); 866 867 /* Remove the leader that we artificially set up 868 * earlier. */ 869 change_process_leader(event->proc, event->proc); 870 destroy_event_handler(event->proc); 871 872 default: 873 ; 874 } 875 876 return event; 877} 878 879void 880continue_after_vfork(Process * proc) 881{ 882 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid); 883 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1); 884 if (handler == NULL) { 885 perror("malloc vfork handler"); 886 /* Carry on not bothering to treat the process as 887 * necessary. */ 888 continue_process(proc->parent->pid); 889 return; 890 } 891 892 /* We must set up custom event handler, so that we see 893 * exec/exit events for the task itself. */ 894 handler->super.on_event = process_vfork_on_event; 895 install_event_handler(proc, &handler->super); 896 897 /* Make sure that the child is sole thread. */ 898 assert(proc->leader == proc); 899 assert(proc->next == NULL || proc->next->leader != proc); 900 901 /* Make sure that the child's parent is properly set up. */ 902 assert(proc->parent != NULL); 903 assert(proc->parent->leader != NULL); 904 905 change_process_leader(proc, proc->parent->leader); 906} 907 908/* If ltrace gets SIGINT, the processes directly or indirectly run by 909 * ltrace get it too. We just have to wait long enough for the signal 910 * to be delivered and the process terminated, which we notice and 911 * exit ltrace, too. So there's not much we need to do there. We 912 * want to keep tracing those processes as usual, in case they just 913 * SIG_IGN the SIGINT to do their shutdown etc. 914 * 915 * For processes ran on the background, we want to install an exit 916 * handler that stops all the threads, removes all breakpoints, and 917 * detaches. 918 */ 919void 920ltrace_exiting(void) 921{ 922 struct opt_p_t * it; 923 for (it = opt_p; it != NULL; it = it->next) { 924 Process * proc = pid2proc(it->pid); 925 if (proc == NULL || proc->leader == NULL) 926 continue; 927 if (ltrace_exiting_install_handler(proc->leader) < 0) 928 fprintf(stderr, 929 "Couldn't install exiting handler for %d.\n", 930 proc->pid); 931 } 932} 933 934size_t 935umovebytes(Process *proc, void *addr, void *laddr, size_t len) { 936 937 union { 938 long a; 939 char c[sizeof(long)]; 940 } a; 941 int started = 0; 942 size_t offset = 0, bytes_read = 0; 943 944 while (offset < len) { 945 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); 946 if (a.a == -1 && errno) { 947 if (started && errno == EIO) 948 return bytes_read; 949 else 950 return -1; 951 } 952 started = 1; 953 954 if (len - offset >= sizeof(long)) { 955 memcpy(laddr + offset, &a.c[0], sizeof(long)); 956 bytes_read += sizeof(long); 957 } 958 else { 959 memcpy(laddr + offset, &a.c[0], len - offset); 960 bytes_read += (len - offset); 961 } 962 offset += sizeof(long); 963 } 964 965 return bytes_read; 966} 967 968/* Read a series of bytes starting at the process's memory address 969 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes 970 have been read. 971*/ 972int 973umovestr(Process *proc, void *addr, int len, void *laddr) { 974 union { 975 long a; 976 char c[sizeof(long)]; 977 } a; 978 unsigned i; 979 int offset = 0; 980 981 while (offset < len) { 982 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); 983 for (i = 0; i < sizeof(long); i++) { 984 if (a.c[i] && offset + (signed)i < len) { 985 *(char *)(laddr + offset + i) = a.c[i]; 986 } else { 987 *(char *)(laddr + offset + i) = '\0'; 988 return 0; 989 } 990 } 991 offset += sizeof(long); 992 } 993 *(char *)(laddr + offset) = '\0'; 994 return 0; 995} 996