proc.c revision 23124cc5c33c6b7a547eb1008505f60590f67593
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 OS_HAVE_PROCESS_DATA 46int 47os_process_init(struct process *proc) 48{ 49 return 0; 50} 51 52void 53os_process_destroy(struct process *proc) 54{ 55} 56 57int 58os_process_clone(struct process *retp, struct process *proc) 59{ 60 return 0; 61} 62 63int 64os_process_exec(struct process *proc) 65{ 66 return 0; 67} 68#endif 69 70#ifndef ARCH_HAVE_PROCESS_DATA 71int 72arch_process_init(struct process *proc) 73{ 74 return 0; 75} 76 77void 78arch_process_destroy(struct process *proc) 79{ 80} 81 82int 83arch_process_clone(struct process *retp, struct process *proc) 84{ 85 return 0; 86} 87 88int 89arch_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 511static enum callback_status 512is_main(struct process *proc, struct library *lib, void *data) 513{ 514 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN); 515} 516 517void 518process_hit_start(struct process *proc) 519{ 520 struct process *leader = proc->leader; 521 assert(leader != NULL); 522 523 struct library *mainlib 524 = proc_each_library(leader, NULL, is_main, NULL); 525 assert(mainlib != NULL); 526 linkmap_init(leader, mainlib->dyn_addr); 527 arch_dynlink_done(leader); 528} 529 530void 531open_pid(pid_t pid) 532{ 533 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); 534 /* If we are already tracing this guy, we should be seeing all 535 * his children via normal tracing route. */ 536 if (pid2proc(pid) != NULL) 537 return; 538 539 /* First, see if we can attach the requested PID itself. */ 540 if (open_one_pid(pid) < 0) { 541 fprintf(stderr, "Cannot attach to pid %u: %s\n", 542 pid, strerror(errno)); 543 trace_fail_warning(pid); 544 return; 545 } 546 547 /* Now attach to all tasks that belong to that PID. There's a 548 * race between process_tasks and open_one_pid. So when we 549 * fail in open_one_pid below, we just do another round. 550 * Chances are that by then that PID will have gone away, and 551 * that's why we have seen the failure. The processes that we 552 * manage to open_one_pid are stopped, so we should eventually 553 * reach a point where process_tasks doesn't give any new 554 * processes (because there's nobody left to produce 555 * them). */ 556 size_t old_ntasks = 0; 557 int have_all; 558 while (1) { 559 pid_t *tasks; 560 size_t ntasks; 561 size_t i; 562 563 if (process_tasks(pid, &tasks, &ntasks) < 0) { 564 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", 565 pid, strerror(errno)); 566 break; 567 } 568 569 have_all = 1; 570 for (i = 0; i < ntasks; ++i) 571 if (pid2proc(tasks[i]) == NULL 572 && open_one_pid(tasks[i]) < 0) 573 have_all = 0; 574 575 free(tasks); 576 577 if (have_all && old_ntasks == ntasks) 578 break; 579 old_ntasks = ntasks; 580 } 581 582 struct process *leader = pid2proc(pid)->leader; 583 584 /* XXX Is there a way to figure out whether _start has 585 * actually already been hit? */ 586 process_hit_start(leader); 587 588 /* Done. Continue everyone. */ 589 each_task(leader, NULL, start_one_pid, NULL); 590} 591 592static enum callback_status 593find_proc(struct process *proc, void *data) 594{ 595 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data); 596} 597 598struct process * 599pid2proc(pid_t pid) 600{ 601 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); 602} 603 604static struct process *list_of_processes = NULL; 605 606static void 607unlist_process(struct process *proc) 608{ 609 if (list_of_processes == proc) { 610 list_of_processes = list_of_processes->next; 611 return; 612 } 613 614 struct process *tmp; 615 for (tmp = list_of_processes; ; tmp = tmp->next) { 616 /* If the following assert fails, the process wasn't 617 * in the list. */ 618 assert(tmp->next != NULL); 619 620 if (tmp->next == proc) { 621 tmp->next = tmp->next->next; 622 return; 623 } 624 } 625} 626 627struct process * 628each_process(struct process *start_after, 629 enum callback_status(*cb)(struct process *proc, void *data), 630 void *data) 631{ 632 struct process *it = start_after == NULL ? list_of_processes 633 : start_after->next; 634 635 while (it != NULL) { 636 /* Callback might call remove_process. */ 637 struct process *next = it->next; 638 switch ((*cb)(it, data)) { 639 case CBS_FAIL: 640 /* XXX handle me */ 641 case CBS_STOP: 642 return it; 643 case CBS_CONT: 644 break; 645 } 646 it = next; 647 } 648 return NULL; 649} 650 651struct process * 652each_task(struct process *proc, struct process *start_after, 653 enum callback_status(*cb)(struct process *proc, void *data), 654 void *data) 655{ 656 assert(proc != NULL); 657 struct process *it = start_after == NULL ? proc->leader 658 : start_after->next; 659 660 if (it != NULL) { 661 struct process *leader = it->leader; 662 while (it != NULL && it->leader == leader) { 663 /* Callback might call remove_process. */ 664 struct process *next = it->next; 665 switch ((*cb)(it, data)) { 666 case CBS_FAIL: 667 /* XXX handle me */ 668 case CBS_STOP: 669 return it; 670 case CBS_CONT: 671 break; 672 } 673 it = next; 674 } 675 } 676 return NULL; 677} 678 679static int 680add_process(struct process *proc, int was_exec) 681{ 682 struct process **leaderp = &list_of_processes; 683 if (proc->pid) { 684 pid_t tgid = process_leader(proc->pid); 685 if (tgid == 0) 686 /* Must have been terminated before we managed 687 * to fully attach. */ 688 return -1; 689 if (tgid == proc->pid) { 690 proc->leader = proc; 691 } else { 692 struct process *leader = pid2proc(tgid); 693 proc->leader = leader; 694 if (leader != NULL) 695 leaderp = &leader->next; 696 } 697 } 698 699 if (!was_exec) { 700 proc->next = *leaderp; 701 *leaderp = proc; 702 } 703 return 0; 704} 705 706void 707change_process_leader(struct process *proc, struct process *leader) 708{ 709 struct process **leaderp = &list_of_processes; 710 if (proc->leader == leader) 711 return; 712 713 assert(leader != NULL); 714 unlist_process(proc); 715 if (proc != leader) 716 leaderp = &leader->next; 717 718 proc->leader = leader; 719 proc->next = *leaderp; 720 *leaderp = proc; 721} 722 723static enum callback_status 724clear_leader(struct process *proc, void *data) 725{ 726 debug(DEBUG_FUNCTION, "detach_task %d from leader %d", 727 proc->pid, proc->leader->pid); 728 proc->leader = NULL; 729 return CBS_CONT; 730} 731 732void 733remove_process(struct process *proc) 734{ 735 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); 736 737 if (proc->leader == proc) 738 each_task(proc, NULL, &clear_leader, NULL); 739 740 unlist_process(proc); 741 process_removed(proc); 742 process_destroy(proc); 743 free(proc); 744} 745 746void 747install_event_handler(struct process *proc, struct event_handler *handler) 748{ 749 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler); 750 assert(proc->event_handler == NULL); 751 proc->event_handler = handler; 752} 753 754void 755destroy_event_handler(struct process *proc) 756{ 757 struct event_handler *handler = proc->event_handler; 758 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler); 759 assert(handler != NULL); 760 if (handler->destroy != NULL) 761 handler->destroy(handler); 762 free(handler); 763 proc->event_handler = NULL; 764} 765 766static int 767breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc) 768{ 769 arch_addr_t bp_addr; 770 assert(proc->leader == proc); 771 772 /* Don't enable latent or delayed symbols. */ 773 if (libsym->latent || libsym->delayed) { 774 debug(DEBUG_FUNCTION, 775 "delayed and/or latent breakpoint pid=%d, %s@%p", 776 proc->pid, libsym->name, libsym->enter_addr); 777 return 0; 778 } 779 780 bp_addr = sym2addr(proc, libsym); 781 782 /* If there is an artificial breakpoint on the same address, 783 * its libsym will be NULL, and we can smuggle our libsym 784 * there. That artificial breakpoint is there presumably for 785 * the callbacks, which we don't touch. If there is a real 786 * breakpoint, then this is a bug. ltrace-elf.c should filter 787 * symbols and ignore extra symbol aliases. 788 * 789 * The other direction is more complicated and currently not 790 * supported. If a breakpoint has custom callbacks, it might 791 * be also custom-allocated, and we would really need to swap 792 * the two: delete the one now in the dictionary, swap values 793 * around, and put the new breakpoint back in. */ 794 struct breakpoint *bp; 795 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) { 796 /* MIPS backend makes duplicate requests. This is 797 * likely a bug in the backend. Currently there's no 798 * point assigning more than one symbol to a 799 * breakpoint, because when it hits, we won't know 800 * what to print out. But it's easier to fix it here 801 * before someone who understands MIPS has the time to 802 * look into it. So turn the sanity check off on 803 * MIPS. References: 804 * 805 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html 806 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html 807 */ 808#ifndef __mips__ 809 assert(bp->libsym == NULL); 810 bp->libsym = libsym; 811#endif 812 return 0; 813 } 814 815 bp = malloc(sizeof(*bp)); 816 if (bp == NULL 817 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) { 818 fail: 819 free(bp); 820 return -1; 821 } 822 if (proc_add_breakpoint(proc, bp) < 0) { 823 breakpoint_destroy(bp); 824 goto fail; 825 } 826 827 if (breakpoint_turn_on(bp, proc) < 0) { 828 proc_remove_breakpoint(proc, bp); 829 breakpoint_destroy(bp); 830 goto fail; 831 } 832 833 return 0; 834} 835 836static enum callback_status 837cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data) 838{ 839 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0); 840} 841 842static int 843proc_activate_latent_symbol(struct process *proc, 844 struct library_symbol *libsym) 845{ 846 assert(libsym->latent); 847 libsym->latent = 0; 848 debug(DEBUG_FUNCTION, "activated latent symbol"); 849 return breakpoint_for_symbol(libsym, proc); 850} 851 852int 853proc_activate_delayed_symbol(struct process *proc, 854 struct library_symbol *libsym) 855{ 856 assert(libsym->delayed); 857 libsym->delayed = 0; 858 debug(DEBUG_FUNCTION, "activated delayed symbol"); 859 return breakpoint_for_symbol(libsym, proc); 860} 861 862static enum callback_status 863activate_latent_in(struct process *proc, struct library *lib, void *data) 864{ 865 struct library_exported_name *exported; 866 for (exported = data; exported != NULL; exported = exported->next) { 867 struct library_symbol *libsym = NULL; 868 while ((libsym = library_each_symbol(lib, libsym, 869 library_symbol_named_cb, 870 (void *)exported->name)) 871 != NULL) 872 if (libsym->latent 873 && proc_activate_latent_symbol(proc, libsym) < 0) 874 return CBS_FAIL; 875 } 876 return CBS_CONT; 877} 878 879void 880proc_add_library(struct process *proc, struct library *lib) 881{ 882 assert(lib->next == NULL); 883 lib->next = proc->libraries; 884 proc->libraries = lib; 885 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d", 886 lib->soname, lib->base, lib->pathname, proc->pid); 887 888 /* Insert breakpoints for all active (non-latent) symbols. */ 889 struct library_symbol *libsym = NULL; 890 while ((libsym = library_each_symbol(lib, libsym, 891 cb_breakpoint_for_symbol, 892 proc)) != NULL) 893 fprintf(stderr, 894 "Couldn't insert breakpoint for %s to %d: %s.\n", 895 libsym->name, proc->pid, strerror(errno)); 896 897 /* Look through export list of the new library and compare it 898 * with latent symbols of all libraries (including this 899 * library itself). */ 900 struct library *lib2 = NULL; 901 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in, 902 lib->exported_names)) != NULL) 903 fprintf(stderr, 904 "Couldn't activate latent symbols for %s in %d: %s.\n", 905 lib2->soname, proc->pid, strerror(errno)); 906} 907 908int 909proc_remove_library(struct process *proc, struct library *lib) 910{ 911 struct library **libp; 912 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next) 913 if (*libp == lib) { 914 *libp = lib->next; 915 return 0; 916 } 917 return -1; 918} 919 920struct library * 921proc_each_library(struct process *proc, struct library *it, 922 enum callback_status (*cb)(struct process *proc, 923 struct library *lib, void *data), 924 void *data) 925{ 926 if (it == NULL) 927 it = proc->libraries; 928 else 929 it = it->next; 930 931 while (it != NULL) { 932 struct library *next = it->next; 933 934 switch (cb(proc, it, data)) { 935 case CBS_FAIL: 936 /* XXX handle me */ 937 case CBS_STOP: 938 return it; 939 case CBS_CONT: 940 break; 941 } 942 943 it = next; 944 } 945 946 return NULL; 947} 948 949static void 950check_leader(struct process *proc) 951{ 952 /* Only the group leader should be getting the breakpoints and 953 * thus have ->breakpoint initialized. */ 954 assert(proc->leader != NULL); 955 assert(proc->leader == proc); 956 assert(proc->breakpoints != NULL); 957} 958 959int 960proc_add_breakpoint(struct process *proc, struct breakpoint *bp) 961{ 962 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)", 963 proc->pid, breakpoint_name(bp), bp->addr); 964 check_leader(proc); 965 966 /* XXX We might merge bp->libsym instead of the following 967 * assert, but that's not necessary right now. Read the 968 * comment in breakpoint_for_symbol. */ 969 assert(dict_find(proc->breakpoints, &bp->addr) == NULL); 970 971 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) { 972 fprintf(stderr, 973 "couldn't enter breakpoint %s@%p to dictionary: %s\n", 974 breakpoint_name(bp), bp->addr, strerror(errno)); 975 return -1; 976 } 977 978 return 0; 979} 980 981void 982proc_remove_breakpoint(struct process *proc, struct breakpoint *bp) 983{ 984 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)", 985 proc->pid, breakpoint_name(bp), bp->addr); 986 check_leader(proc); 987 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *, 988 NULL, NULL, NULL); 989 assert(rc == 0); 990} 991 992struct each_breakpoint_data 993{ 994 struct process *proc; 995 enum callback_status (*cb)(struct process *proc, 996 struct breakpoint *bp, 997 void *data); 998 void *cb_data; 999}; 1000 1001static enum callback_status 1002each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d) 1003{ 1004 struct each_breakpoint_data *data = d; 1005 return data->cb(data->proc, *bpp, data->cb_data); 1006} 1007 1008void * 1009proc_each_breakpoint(struct process *proc, void *start, 1010 enum callback_status (*cb)(struct process *proc, 1011 struct breakpoint *bp, 1012 void *data), void *data) 1013{ 1014 struct each_breakpoint_data dd = { 1015 .proc = proc, 1016 .cb = cb, 1017 .cb_data = data, 1018 }; 1019 return DICT_EACH(proc->breakpoints, 1020 arch_addr_t, struct breakpoint *, start, 1021 &each_breakpoint_cb, &dd); 1022} 1023 1024int 1025proc_find_symbol(struct process *proc, struct library_symbol *sym, 1026 struct library **retlib, struct library_symbol **retsym) 1027{ 1028 struct library *lib = sym->lib; 1029 assert(lib != NULL); 1030 1031 struct library *flib 1032 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key); 1033 if (flib == NULL) 1034 return -1; 1035 1036 struct library_symbol *fsym 1037 = library_each_symbol(flib, NULL, library_symbol_named_cb, 1038 (char *)sym->name); 1039 if (fsym == NULL) 1040 return -1; 1041 1042 if (retlib != NULL) 1043 *retlib = flib; 1044 if (retsym != NULL) 1045 *retsym = fsym; 1046 1047 return 0; 1048} 1049 1050struct library_symbol * 1051proc_each_symbol(struct process *proc, struct library_symbol *start_after, 1052 enum callback_status (*cb)(struct library_symbol *, void *), 1053 void *data) 1054{ 1055 struct library *lib; 1056 for (lib = start_after != NULL ? start_after->lib : proc->libraries; 1057 lib != NULL; lib = lib->next) { 1058 start_after = library_each_symbol(lib, start_after, cb, data); 1059 if (start_after != NULL) 1060 return start_after; 1061 } 1062 1063 return NULL; 1064} 1065 1066#define DEF_READER(NAME, SIZE) \ 1067 int \ 1068 NAME(struct process *proc, arch_addr_t addr, \ 1069 uint##SIZE##_t *lp) \ 1070 { \ 1071 union { \ 1072 uint##SIZE##_t dst; \ 1073 char buf[0]; \ 1074 } u; \ 1075 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \ 1076 != sizeof(u.dst)) \ 1077 return -1; \ 1078 *lp = u.dst; \ 1079 return 0; \ 1080 } 1081 1082DEF_READER(proc_read_8, 8) 1083DEF_READER(proc_read_16, 16) 1084DEF_READER(proc_read_32, 32) 1085DEF_READER(proc_read_64, 64) 1086 1087#undef DEF_READER 1088