proc.c revision 7725d4b171138e8d2b77c8a1186e9f8215b6c91f
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2010 Joe Damato 5 * Copyright (C) 1998,2009 Juan Cespedes 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA 21 */ 22 23#include "config.h" 24 25#include <sys/types.h> 26#include <assert.h> 27#include <errno.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31 32#if defined(HAVE_LIBUNWIND) 33#include <libunwind.h> 34#include <libunwind-ptrace.h> 35#endif /* defined(HAVE_LIBUNWIND) */ 36 37#include "backend.h" 38#include "breakpoint.h" 39#include "debug.h" 40#include "fetch.h" 41#include "options.h" 42#include "proc.h" 43#include "value_dict.h" 44 45#ifndef ARCH_HAVE_PROCESS_DATA 46int 47arch_process_init(struct process *proc) 48{ 49 return 0; 50} 51 52void 53arch_process_destroy(struct process *proc) 54{ 55} 56 57int 58arch_process_clone(struct process *retp, struct process *proc) 59{ 60 return 0; 61} 62 63int 64arch_process_exec(struct process *proc) 65{ 66 return 0; 67} 68#endif 69 70#ifndef OS_HAVE_PROCESS_DATA 71int 72os_process_init(struct process *proc) 73{ 74 return 0; 75} 76 77void 78os_process_destroy(struct process *proc) 79{ 80} 81 82int 83os_process_clone(struct process *retp, struct process *proc) 84{ 85 return 0; 86} 87 88int 89os_process_exec(struct process *proc) 90{ 91 return 0; 92} 93#endif 94 95#ifndef ARCH_HAVE_DYNLINK_DONE 96void 97arch_dynlink_done(struct process *proc) 98{ 99} 100#endif 101 102static int add_process(struct process *proc, int was_exec); 103static void unlist_process(struct process *proc); 104 105static void 106destroy_unwind(struct process *proc) 107{ 108#if defined(HAVE_LIBUNWIND) 109 if (proc->unwind_priv != NULL) 110 _UPT_destroy(proc->unwind_priv); 111 if (proc->unwind_as != NULL) 112 unw_destroy_addr_space(proc->unwind_as); 113#endif /* defined(HAVE_LIBUNWIND) */ 114} 115 116static int 117process_bare_init(struct process *proc, const char *filename, 118 pid_t pid, int was_exec) 119{ 120 if (!was_exec) { 121 memset(proc, 0, sizeof(*proc)); 122 123 proc->filename = strdup(filename); 124 if (proc->filename == NULL) { 125 fail: 126 free(proc->filename); 127 if (proc->breakpoints != NULL) { 128 dict_destroy(proc->breakpoints, 129 NULL, NULL, NULL); 130 free(proc->breakpoints); 131 proc->breakpoints = NULL; 132 } 133 return -1; 134 } 135 } 136 137 /* Add process so that we know who the leader is. */ 138 proc->pid = pid; 139 if (add_process(proc, was_exec) < 0) 140 goto fail; 141 if (proc->leader == NULL) { 142 unlist_and_fail: 143 if (!was_exec) 144 unlist_process(proc); 145 goto fail; 146 } 147 148 if (proc->leader == proc) { 149 proc->breakpoints = malloc(sizeof(*proc->breakpoints)); 150 if (proc->breakpoints == NULL) 151 goto unlist_and_fail; 152 DICT_INIT(proc->breakpoints, 153 arch_addr_t, struct breakpoint *, 154 arch_addr_hash, arch_addr_eq, NULL); 155 } else { 156 proc->breakpoints = NULL; 157 } 158 159#if defined(HAVE_LIBUNWIND) 160 if (options.bt_depth > 0) { 161 proc->unwind_priv = _UPT_create(pid); 162 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0); 163 164 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) { 165 fprintf(stderr, 166 "Couldn't initialize unwinding " 167 "for process %d\n", proc->pid); 168 destroy_unwind(proc); 169 proc->unwind_priv = NULL; 170 proc->unwind_as = NULL; 171 } 172 } 173#endif /* defined(HAVE_LIBUNWIND) */ 174 175 return 0; 176} 177 178static void 179process_bare_destroy(struct process *proc, int was_exec) 180{ 181 dict_destroy(proc->breakpoints, NULL, NULL, NULL); 182 free(proc->breakpoints); 183 if (!was_exec) { 184 free(proc->filename); 185 unlist_process(proc); 186 destroy_unwind(proc); 187 } 188} 189 190static int 191process_init_main(struct process *proc) 192{ 193 if (breakpoints_init(proc) < 0) { 194 fprintf(stderr, "failed to init breakpoints %d\n", 195 proc->pid); 196 return -1; 197 } 198 199 return 0; 200} 201 202int 203process_init(struct process *proc, const char *filename, pid_t pid) 204{ 205 if (process_bare_init(proc, filename, pid, 0) < 0) { 206 fail: 207 fprintf(stderr, "failed to initialize process %d: %s\n", 208 pid, strerror(errno)); 209 return -1; 210 } 211 212 if (os_process_init(proc) < 0) { 213 process_bare_destroy(proc, 0); 214 goto fail; 215 } 216 217 if (arch_process_init(proc) < 0) { 218 os_process_destroy(proc); 219 process_bare_destroy(proc, 0); 220 goto fail; 221 } 222 223 if (proc->leader != proc) 224 return 0; 225 if (process_init_main(proc) < 0) { 226 process_bare_destroy(proc, 0); 227 goto fail; 228 } 229 return 0; 230} 231 232static enum callback_status 233destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data) 234{ 235 breakpoint_destroy(bp); 236 free(bp); 237 return CBS_CONT; 238} 239 240// XXX see comment in handle_event.c 241void callstack_pop(struct process *proc); 242 243static void 244private_process_destroy(struct process *proc, int was_exec) 245{ 246 /* Pop remaining stack elements. */ 247 while (proc->callstack_depth > 0) { 248 /* When this is called just before a process is 249 * destroyed, the breakpoints should either have been 250 * retracted by now, or were killed by exec. In any 251 * case, it's safe to pretend that there are no 252 * breakpoints associated with the stack elements, so 253 * that stack_pop doesn't attempt to destroy them. */ 254 size_t i = proc->callstack_depth - 1; 255 if (!proc->callstack[i].is_syscall) 256 proc->callstack[i].return_addr = 0; 257 258 callstack_pop(proc); 259 } 260 261 if (!was_exec) 262 free(proc->filename); 263 264 /* Libraries and symbols. This is only relevant in 265 * leader. */ 266 struct library *lib; 267 for (lib = proc->libraries; lib != NULL; ) { 268 struct library *next = lib->next; 269 library_destroy(lib); 270 free(lib); 271 lib = next; 272 } 273 proc->libraries = NULL; 274 275 /* Breakpoints. */ 276 if (proc->breakpoints != NULL) { 277 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL); 278 dict_destroy(proc->breakpoints, NULL, NULL, NULL); 279 free(proc->breakpoints); 280 proc->breakpoints = NULL; 281 } 282 283 destroy_unwind(proc); 284} 285 286void 287process_destroy(struct process *proc) 288{ 289 arch_process_destroy(proc); 290 os_process_destroy(proc); 291 private_process_destroy(proc, 0); 292} 293 294int 295process_exec(struct process *proc) 296{ 297 /* Call exec handlers first, before we destroy the main 298 * state. */ 299 if (arch_process_exec(proc) < 0 300 || os_process_exec(proc) < 0) 301 return -1; 302 303 private_process_destroy(proc, 1); 304 305 if (process_bare_init(proc, NULL, proc->pid, 1) < 0) 306 return -1; 307 if (process_init_main(proc) < 0) { 308 process_bare_destroy(proc, 1); 309 return -1; 310 } 311 return 0; 312} 313 314struct process * 315open_program(const char *filename, pid_t pid) 316{ 317 assert(pid != 0); 318 struct process *proc = malloc(sizeof(*proc)); 319 if (proc == NULL || process_init(proc, filename, pid) < 0) { 320 free(proc); 321 return NULL; 322 } 323 return proc; 324} 325 326struct clone_single_bp_data { 327 struct process *old_proc; 328 struct process *new_proc; 329}; 330 331static enum callback_status 332clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u) 333{ 334 struct breakpoint *bp = *bpp; 335 struct clone_single_bp_data *data = u; 336 337 struct breakpoint *clone = malloc(sizeof(*clone)); 338 if (clone == NULL 339 || breakpoint_clone(clone, data->new_proc, bp) < 0) { 340 fail: 341 free(clone); 342 return CBS_STOP; 343 } 344 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) { 345 breakpoint_destroy(clone); 346 goto fail; 347 } 348 return CBS_CONT; 349} 350 351int 352process_clone(struct process *retp, struct process *proc, pid_t pid) 353{ 354 if (process_bare_init(retp, proc->filename, pid, 0) < 0) { 355 fail1: 356 fprintf(stderr, "Failed to clone process %d to %d: %s\n", 357 proc->pid, pid, strerror(errno)); 358 return -1; 359 } 360 361 retp->tracesysgood = proc->tracesysgood; 362 retp->e_machine = proc->e_machine; 363 retp->e_class = proc->e_class; 364 365 /* For non-leader processes, that's all we need to do. */ 366 if (retp->leader != retp) 367 return 0; 368 369 /* Clone symbols first so that we can clone and relink 370 * breakpoints. */ 371 struct library *lib; 372 struct library **nlibp = &retp->libraries; 373 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) { 374 *nlibp = malloc(sizeof(**nlibp)); 375 376 if (*nlibp == NULL 377 || library_clone(*nlibp, lib) < 0) { 378 free(*nlibp); 379 *nlibp = NULL; 380 381 fail2: 382 process_bare_destroy(retp, 0); 383 384 /* Error when cloning. Unroll what was done. */ 385 for (lib = retp->libraries; lib != NULL; ) { 386 struct library *next = lib->next; 387 library_destroy(lib); 388 free(lib); 389 lib = next; 390 } 391 goto fail1; 392 } 393 394 nlibp = &(*nlibp)->next; 395 } 396 397 /* Now clone breakpoints. Symbol relinking is done in 398 * clone_single_bp. */ 399 struct clone_single_bp_data data = { 400 .old_proc = proc, 401 .new_proc = retp, 402 }; 403 if (DICT_EACH(proc->leader->breakpoints, 404 arch_addr_t, struct breakpoint *, NULL, 405 clone_single_bp, &data) != NULL) 406 goto fail2; 407 408 /* And finally the call stack. */ 409 /* XXX clearly the callstack handling should be moved to a 410 * separate module and this whole business extracted to 411 * callstack_clone, or callstack_element_clone. */ 412 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack)); 413 retp->callstack_depth = proc->callstack_depth; 414 415 size_t i; 416 for (i = 0; i < retp->callstack_depth; ++i) { 417 struct callstack_element *elem = &retp->callstack[i]; 418 struct fetch_context *ctx = elem->fetch_context; 419 if (ctx != NULL) { 420 struct fetch_context *nctx = fetch_arg_clone(retp, ctx); 421 if (nctx == NULL) { 422 size_t j; 423 fail3: 424 for (j = 0; j < i; ++j) { 425 nctx = retp->callstack[j].fetch_context; 426 fetch_arg_done(nctx); 427 elem->fetch_context = NULL; 428 } 429 goto fail2; 430 } 431 elem->fetch_context = nctx; 432 } 433 434 if (elem->arguments != NULL) { 435 struct value_dict *nargs = malloc(sizeof(*nargs)); 436 if (nargs == NULL 437 || val_dict_clone(nargs, elem->arguments) < 0) { 438 size_t j; 439 for (j = 0; j < i; ++j) { 440 nargs = retp->callstack[j].arguments; 441 val_dict_destroy(nargs); 442 free(nargs); 443 elem->arguments = NULL; 444 } 445 446 /* Pretend that this round went well, 447 * so that fail3 frees I-th 448 * fetch_context. */ 449 ++i; 450 goto fail3; 451 } 452 elem->arguments = nargs; 453 } 454 455 /* If it's not a syscall, we need to find the 456 * corresponding library symbol in the cloned 457 * library. */ 458 if (!elem->is_syscall && elem->c_un.libfunc != NULL) { 459 struct library_symbol *libfunc = elem->c_un.libfunc; 460 int rc = proc_find_symbol(retp, libfunc, 461 NULL, &elem->c_un.libfunc); 462 assert(rc == 0); 463 } 464 } 465 466 /* At this point, retp is fully initialized, except for OS and 467 * arch parts, and we can call private_process_destroy. */ 468 if (os_process_clone(retp, proc) < 0) { 469 private_process_destroy(retp, 0); 470 return -1; 471 } 472 if (arch_process_clone(retp, proc) < 0) { 473 os_process_destroy(retp); 474 private_process_destroy(retp, 0); 475 return -1; 476 } 477 478 return 0; 479} 480 481static int 482open_one_pid(pid_t pid) 483{ 484 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid); 485 486 /* Get the filename first. Should the trace_pid fail, we can 487 * easily free it, untracing is more work. */ 488 char *filename = pid2name(pid); 489 if (filename == NULL || trace_pid(pid) < 0) { 490 fail: 491 free(filename); 492 return -1; 493 } 494 495 struct process *proc = open_program(filename, pid); 496 if (proc == NULL) 497 goto fail; 498 free(filename); 499 trace_set_options(proc); 500 501 return 0; 502} 503 504static enum callback_status 505start_one_pid(struct process *proc, void *data) 506{ 507 continue_process(proc->pid); 508 return CBS_CONT; 509} 510 511void 512open_pid(pid_t pid) 513{ 514 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); 515 /* If we are already tracing this guy, we should be seeing all 516 * his children via normal tracing route. */ 517 if (pid2proc(pid) != NULL) 518 return; 519 520 /* First, see if we can attach the requested PID itself. */ 521 if (open_one_pid(pid) < 0) { 522 fprintf(stderr, "Cannot attach to pid %u: %s\n", 523 pid, strerror(errno)); 524 trace_fail_warning(pid); 525 return; 526 } 527 528 /* Now attach to all tasks that belong to that PID. There's a 529 * race between process_tasks and open_one_pid. So when we 530 * fail in open_one_pid below, we just do another round. 531 * Chances are that by then that PID will have gone away, and 532 * that's why we have seen the failure. The processes that we 533 * manage to open_one_pid are stopped, so we should eventually 534 * reach a point where process_tasks doesn't give any new 535 * processes (because there's nobody left to produce 536 * them). */ 537 size_t old_ntasks = 0; 538 int have_all; 539 while (1) { 540 pid_t *tasks; 541 size_t ntasks; 542 size_t i; 543 544 if (process_tasks(pid, &tasks, &ntasks) < 0) { 545 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", 546 pid, strerror(errno)); 547 break; 548 } 549 550 have_all = 1; 551 for (i = 0; i < ntasks; ++i) 552 if (pid2proc(tasks[i]) == NULL 553 && open_one_pid(tasks[i]) < 0) 554 have_all = 0; 555 556 free(tasks); 557 558 if (have_all && old_ntasks == ntasks) 559 break; 560 old_ntasks = ntasks; 561 } 562 563 struct process *leader = pid2proc(pid)->leader; 564 565 /* XXX Is there a way to figure out whether _start has 566 * actually already been hit? */ 567 arch_dynlink_done(leader); 568 569 /* Done. Continue everyone. */ 570 each_task(leader, NULL, start_one_pid, NULL); 571} 572 573static enum callback_status 574find_proc(struct process *proc, void *data) 575{ 576 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data); 577} 578 579struct process * 580pid2proc(pid_t pid) 581{ 582 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); 583} 584 585static struct process *list_of_processes = NULL; 586 587static void 588unlist_process(struct process *proc) 589{ 590 if (list_of_processes == proc) { 591 list_of_processes = list_of_processes->next; 592 return; 593 } 594 595 struct process *tmp; 596 for (tmp = list_of_processes; ; tmp = tmp->next) { 597 /* If the following assert fails, the process wasn't 598 * in the list. */ 599 assert(tmp->next != NULL); 600 601 if (tmp->next == proc) { 602 tmp->next = tmp->next->next; 603 return; 604 } 605 } 606} 607 608struct process * 609each_process(struct process *start_after, 610 enum callback_status(*cb)(struct process *proc, void *data), 611 void *data) 612{ 613 struct process *it = start_after == NULL ? list_of_processes 614 : start_after->next; 615 616 while (it != NULL) { 617 /* Callback might call remove_process. */ 618 struct process *next = it->next; 619 switch ((*cb)(it, data)) { 620 case CBS_FAIL: 621 /* XXX handle me */ 622 case CBS_STOP: 623 return it; 624 case CBS_CONT: 625 break; 626 } 627 it = next; 628 } 629 return NULL; 630} 631 632struct process * 633each_task(struct process *proc, struct process *start_after, 634 enum callback_status(*cb)(struct process *proc, void *data), 635 void *data) 636{ 637 assert(proc != NULL); 638 struct process *it = start_after == NULL ? proc->leader 639 : start_after->next; 640 641 if (it != NULL) { 642 struct process *leader = it->leader; 643 while (it != NULL && it->leader == leader) { 644 /* Callback might call remove_process. */ 645 struct process *next = it->next; 646 switch ((*cb)(it, data)) { 647 case CBS_FAIL: 648 /* XXX handle me */ 649 case CBS_STOP: 650 return it; 651 case CBS_CONT: 652 break; 653 } 654 it = next; 655 } 656 } 657 return NULL; 658} 659 660static int 661add_process(struct process *proc, int was_exec) 662{ 663 struct process **leaderp = &list_of_processes; 664 if (proc->pid) { 665 pid_t tgid = process_leader(proc->pid); 666 if (tgid == 0) 667 /* Must have been terminated before we managed 668 * to fully attach. */ 669 return -1; 670 if (tgid == proc->pid) { 671 proc->leader = proc; 672 } else { 673 struct process *leader = pid2proc(tgid); 674 proc->leader = leader; 675 if (leader != NULL) 676 leaderp = &leader->next; 677 } 678 } 679 680 if (!was_exec) { 681 proc->next = *leaderp; 682 *leaderp = proc; 683 } 684 return 0; 685} 686 687void 688change_process_leader(struct process *proc, struct process *leader) 689{ 690 struct process **leaderp = &list_of_processes; 691 if (proc->leader == leader) 692 return; 693 694 assert(leader != NULL); 695 unlist_process(proc); 696 if (proc != leader) 697 leaderp = &leader->next; 698 699 proc->leader = leader; 700 proc->next = *leaderp; 701 *leaderp = proc; 702} 703 704static enum callback_status 705clear_leader(struct process *proc, void *data) 706{ 707 debug(DEBUG_FUNCTION, "detach_task %d from leader %d", 708 proc->pid, proc->leader->pid); 709 proc->leader = NULL; 710 return CBS_CONT; 711} 712 713void 714remove_process(struct process *proc) 715{ 716 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); 717 718 if (proc->leader == proc) 719 each_task(proc, NULL, &clear_leader, NULL); 720 721 unlist_process(proc); 722 process_removed(proc); 723 process_destroy(proc); 724 free(proc); 725} 726 727void 728install_event_handler(struct process *proc, struct event_handler *handler) 729{ 730 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler); 731 assert(proc->event_handler == NULL); 732 proc->event_handler = handler; 733} 734 735void 736destroy_event_handler(struct process *proc) 737{ 738 struct event_handler *handler = proc->event_handler; 739 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler); 740 assert(handler != NULL); 741 if (handler->destroy != NULL) 742 handler->destroy(handler); 743 free(handler); 744 proc->event_handler = NULL; 745} 746 747static int 748breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc) 749{ 750 arch_addr_t bp_addr; 751 assert(proc->leader == proc); 752 753 /* Don't enable latent or delayed symbols. */ 754 if (libsym->latent || libsym->delayed) { 755 debug(DEBUG_FUNCTION, 756 "delayed and/or latent breakpoint pid=%d, %s@%p", 757 proc->pid, libsym->name, libsym->enter_addr); 758 return 0; 759 } 760 761 bp_addr = sym2addr(proc, libsym); 762 763 /* If there is an artificial breakpoint on the same address, 764 * its libsym will be NULL, and we can smuggle our libsym 765 * there. That artificial breakpoint is there presumably for 766 * the callbacks, which we don't touch. If there is a real 767 * breakpoint, then this is a bug. ltrace-elf.c should filter 768 * symbols and ignore extra symbol aliases. 769 * 770 * The other direction is more complicated and currently not 771 * supported. If a breakpoint has custom callbacks, it might 772 * be also custom-allocated, and we would really need to swap 773 * the two: delete the one now in the dictionary, swap values 774 * around, and put the new breakpoint back in. */ 775 struct breakpoint *bp; 776 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) { 777 /* MIPS backend makes duplicate requests. This is 778 * likely a bug in the backend. Currently there's no 779 * point assigning more than one symbol to a 780 * breakpoint, because when it hits, we won't know 781 * what to print out. But it's easier to fix it here 782 * before someone who understands MIPS has the time to 783 * look into it. So turn the sanity check off on 784 * MIPS. References: 785 * 786 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html 787 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html 788 */ 789#ifndef __mips__ 790 assert(bp->libsym == NULL); 791 bp->libsym = libsym; 792#endif 793 return 0; 794 } 795 796 bp = malloc(sizeof(*bp)); 797 if (bp == NULL 798 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) { 799 fail: 800 free(bp); 801 return -1; 802 } 803 if (proc_add_breakpoint(proc, bp) < 0) { 804 breakpoint_destroy(bp); 805 goto fail; 806 } 807 808 if (breakpoint_turn_on(bp, proc) < 0) { 809 proc_remove_breakpoint(proc, bp); 810 breakpoint_destroy(bp); 811 goto fail; 812 } 813 814 return 0; 815} 816 817static enum callback_status 818cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data) 819{ 820 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0); 821} 822 823static int 824proc_activate_latent_symbol(struct process *proc, 825 struct library_symbol *libsym) 826{ 827 assert(libsym->latent); 828 libsym->latent = 0; 829 debug(DEBUG_FUNCTION, "activated latent symbol"); 830 return breakpoint_for_symbol(libsym, proc); 831} 832 833int 834proc_activate_delayed_symbol(struct process *proc, 835 struct library_symbol *libsym) 836{ 837 assert(libsym->delayed); 838 libsym->delayed = 0; 839 debug(DEBUG_FUNCTION, "activated delayed symbol"); 840 return breakpoint_for_symbol(libsym, proc); 841} 842 843static enum callback_status 844activate_latent_in(struct process *proc, struct library *lib, void *data) 845{ 846 struct library_exported_name *exported; 847 for (exported = data; exported != NULL; exported = exported->next) { 848 struct library_symbol *libsym = NULL; 849 while ((libsym = library_each_symbol(lib, libsym, 850 library_symbol_named_cb, 851 (void *)exported->name)) 852 != NULL) 853 if (libsym->latent 854 && proc_activate_latent_symbol(proc, libsym) < 0) 855 return CBS_FAIL; 856 } 857 return CBS_CONT; 858} 859 860void 861proc_add_library(struct process *proc, struct library *lib) 862{ 863 assert(lib->next == NULL); 864 lib->next = proc->libraries; 865 proc->libraries = lib; 866 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d", 867 lib->soname, lib->base, lib->pathname, proc->pid); 868 869 /* Insert breakpoints for all active (non-latent) symbols. */ 870 struct library_symbol *libsym = NULL; 871 while ((libsym = library_each_symbol(lib, libsym, 872 cb_breakpoint_for_symbol, 873 proc)) != NULL) 874 fprintf(stderr, 875 "Couldn't insert breakpoint for %s to %d: %s.\n", 876 libsym->name, proc->pid, strerror(errno)); 877 878 /* Look through export list of the new library and compare it 879 * with latent symbols of all libraries (including this 880 * library itself). */ 881 struct library *lib2 = NULL; 882 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in, 883 lib->exported_names)) != NULL) 884 fprintf(stderr, 885 "Couldn't activate latent symbols for %s in %d: %s.\n", 886 lib2->soname, proc->pid, strerror(errno)); 887} 888 889int 890proc_remove_library(struct process *proc, struct library *lib) 891{ 892 struct library **libp; 893 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next) 894 if (*libp == lib) { 895 *libp = lib->next; 896 return 0; 897 } 898 return -1; 899} 900 901struct library * 902proc_each_library(struct process *proc, struct library *it, 903 enum callback_status (*cb)(struct process *proc, 904 struct library *lib, void *data), 905 void *data) 906{ 907 if (it == NULL) 908 it = proc->libraries; 909 else 910 it = it->next; 911 912 while (it != NULL) { 913 struct library *next = it->next; 914 915 switch (cb(proc, it, data)) { 916 case CBS_FAIL: 917 /* XXX handle me */ 918 case CBS_STOP: 919 return it; 920 case CBS_CONT: 921 break; 922 } 923 924 it = next; 925 } 926 927 return NULL; 928} 929 930static void 931check_leader(struct process *proc) 932{ 933 /* Only the group leader should be getting the breakpoints and 934 * thus have ->breakpoint initialized. */ 935 assert(proc->leader != NULL); 936 assert(proc->leader == proc); 937 assert(proc->breakpoints != NULL); 938} 939 940int 941proc_add_breakpoint(struct process *proc, struct breakpoint *bp) 942{ 943 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)", 944 proc->pid, breakpoint_name(bp), bp->addr); 945 check_leader(proc); 946 947 /* XXX We might merge bp->libsym instead of the following 948 * assert, but that's not necessary right now. Read the 949 * comment in breakpoint_for_symbol. */ 950 assert(dict_find(proc->breakpoints, &bp->addr) == NULL); 951 952 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) { 953 fprintf(stderr, 954 "couldn't enter breakpoint %s@%p to dictionary: %s\n", 955 breakpoint_name(bp), bp->addr, strerror(errno)); 956 return -1; 957 } 958 959 return 0; 960} 961 962void 963proc_remove_breakpoint(struct process *proc, struct breakpoint *bp) 964{ 965 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)", 966 proc->pid, breakpoint_name(bp), bp->addr); 967 check_leader(proc); 968 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *, 969 NULL, NULL, NULL); 970 assert(rc == 0); 971} 972 973struct each_breakpoint_data 974{ 975 struct process *proc; 976 enum callback_status (*cb)(struct process *proc, 977 struct breakpoint *bp, 978 void *data); 979 void *cb_data; 980}; 981 982static enum callback_status 983each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d) 984{ 985 struct each_breakpoint_data *data = d; 986 return data->cb(data->proc, *bpp, data->cb_data); 987} 988 989void * 990proc_each_breakpoint(struct process *proc, void *start, 991 enum callback_status (*cb)(struct process *proc, 992 struct breakpoint *bp, 993 void *data), void *data) 994{ 995 struct each_breakpoint_data dd = { 996 .proc = proc, 997 .cb = cb, 998 .cb_data = data, 999 }; 1000 return DICT_EACH(proc->breakpoints, 1001 arch_addr_t, struct breakpoint *, start, 1002 &each_breakpoint_cb, &dd); 1003} 1004 1005int 1006proc_find_symbol(struct process *proc, struct library_symbol *sym, 1007 struct library **retlib, struct library_symbol **retsym) 1008{ 1009 struct library *lib = sym->lib; 1010 assert(lib != NULL); 1011 1012 struct library *flib 1013 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key); 1014 if (flib == NULL) 1015 return -1; 1016 1017 struct library_symbol *fsym 1018 = library_each_symbol(flib, NULL, library_symbol_named_cb, 1019 (char *)sym->name); 1020 if (fsym == NULL) 1021 return -1; 1022 1023 if (retlib != NULL) 1024 *retlib = flib; 1025 if (retsym != NULL) 1026 *retsym = fsym; 1027 1028 return 0; 1029} 1030 1031struct library_symbol * 1032proc_each_symbol(struct process *proc, struct library_symbol *start_after, 1033 enum callback_status (*cb)(struct library_symbol *, void *), 1034 void *data) 1035{ 1036 struct library *lib; 1037 for (lib = start_after != NULL ? start_after->lib : proc->libraries; 1038 lib != NULL; lib = lib->next) { 1039 start_after = library_each_symbol(lib, start_after, cb, data); 1040 if (start_after != NULL) 1041 return start_after; 1042 } 1043 1044 return NULL; 1045} 1046 1047#define DEF_READER(NAME, SIZE) \ 1048 int \ 1049 NAME(struct process *proc, arch_addr_t addr, \ 1050 uint##SIZE##_t *lp) \ 1051 { \ 1052 union { \ 1053 uint##SIZE##_t dst; \ 1054 char buf[0]; \ 1055 } u; \ 1056 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \ 1057 != sizeof(u.dst)) \ 1058 return -1; \ 1059 *lp = u.dst; \ 1060 return 0; \ 1061 } 1062 1063DEF_READER(proc_read_8, 8) 1064DEF_READER(proc_read_16, 16) 1065DEF_READER(proc_read_32, 32) 1066DEF_READER(proc_read_64, 64) 1067 1068#undef DEF_READER 1069