proc.c revision ad64047d1d724637896d0f93af1a8fcce758c9b5
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012 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 "proc.h" 42#include "value_dict.h" 43 44#ifndef ARCH_HAVE_PROCESS_DATA 45int 46arch_process_init(struct Process *proc) 47{ 48 return 0; 49} 50 51void 52arch_process_destroy(struct Process *proc) 53{ 54} 55 56int 57arch_process_clone(struct Process *retp, struct Process *proc) 58{ 59 return 0; 60} 61 62int 63arch_process_exec(struct Process *proc) 64{ 65 return 0; 66} 67#endif 68 69#ifndef ARCH_HAVE_DYNLINK_DONE 70void 71arch_dynlink_done(struct Process *proc) 72{ 73} 74#endif 75 76static void add_process(struct Process *proc, int was_exec); 77static void unlist_process(struct Process *proc); 78 79static int 80process_bare_init(struct Process *proc, const char *filename, 81 pid_t pid, int was_exec) 82{ 83 if (!was_exec) { 84 memset(proc, 0, sizeof(*proc)); 85 86 proc->filename = strdup(filename); 87 if (proc->filename == NULL) { 88 fail: 89 free(proc->filename); 90 if (proc->breakpoints != NULL) 91 dict_clear(proc->breakpoints); 92 return -1; 93 } 94 } 95 96 /* Add process so that we know who the leader is. */ 97 proc->pid = pid; 98 add_process(proc, was_exec); 99 if (proc->leader == NULL) 100 goto fail; 101 102 if (proc->leader == proc) { 103 proc->breakpoints = dict_init(target_address_hash, 104 target_address_cmp); 105 if (proc->breakpoints == NULL) 106 goto fail; 107 } else { 108 proc->breakpoints = NULL; 109 } 110 111#if defined(HAVE_LIBUNWIND) 112 proc->unwind_priv = _UPT_create(pid); 113 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0); 114#endif /* defined(HAVE_LIBUNWIND) */ 115 116 return 0; 117} 118 119static void 120process_bare_destroy(struct Process *proc, int was_exec) 121{ 122 dict_clear(proc->breakpoints); 123 if (!was_exec) { 124 free(proc->filename); 125 unlist_process(proc); 126 } 127} 128 129static int 130process_init_main(struct Process *proc) 131{ 132 if (breakpoints_init(proc) < 0) { 133 fprintf(stderr, "failed to init breakpoints %d\n", 134 proc->pid); 135 return -1; 136 } 137 138 return 0; 139} 140 141int 142process_init(struct Process *proc, const char *filename, pid_t pid) 143{ 144 if (process_bare_init(proc, filename, pid, 0) < 0) { 145 fail: 146 fprintf(stderr, "failed to initialize process %d: %s\n", 147 pid, strerror(errno)); 148 return -1; 149 } 150 151 if (arch_process_init(proc) < 0) { 152 process_bare_destroy(proc, 0); 153 goto fail; 154 } 155 156 if (proc->leader != proc) 157 return 0; 158 if (process_init_main(proc) < 0) { 159 process_bare_destroy(proc, 0); 160 goto fail; 161 } 162 return 0; 163} 164 165static enum callback_status 166destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data) 167{ 168 breakpoint_destroy(bp); 169 free(bp); 170 return CBS_CONT; 171} 172 173static void 174private_process_destroy(struct Process *proc, int keep_filename) 175{ 176 if (!keep_filename) 177 free(proc->filename); 178 179 /* Libraries and symbols. This is only relevant in 180 * leader. */ 181 struct library *lib; 182 for (lib = proc->libraries; lib != NULL; ) { 183 struct library *next = lib->next; 184 library_destroy(lib); 185 free(lib); 186 lib = next; 187 } 188 proc->libraries = NULL; 189 190 /* Breakpoints. */ 191 if (proc->breakpoints != NULL) { 192 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL); 193 dict_clear(proc->breakpoints); 194 proc->breakpoints = NULL; 195 } 196} 197 198void 199process_destroy(struct Process *proc) 200{ 201 private_process_destroy(proc, 0); 202 arch_process_destroy(proc); 203} 204 205int 206process_exec(struct Process *proc) 207{ 208 /* Call exec first, before we destroy the main state. */ 209 if (arch_process_exec(proc) < 0) 210 return -1; 211 212 private_process_destroy(proc, 1); 213 if (process_bare_init(proc, NULL, proc->pid, 1) < 0) 214 return -1; 215 if (process_init_main(proc) < 0) { 216 process_bare_destroy(proc, 1); 217 return -1; 218 } 219 return 0; 220} 221 222struct Process * 223open_program(const char *filename, pid_t pid) 224{ 225 assert(pid != 0); 226 struct Process *proc = malloc(sizeof(*proc)); 227 if (proc == NULL || process_init(proc, filename, pid) < 0) { 228 free(proc); 229 return NULL; 230 } 231 return proc; 232} 233 234struct clone_single_bp_data { 235 struct Process *old_proc; 236 struct Process *new_proc; 237 int error; 238}; 239 240static void 241clone_single_bp(void *key, void *value, void *u) 242{ 243 struct breakpoint *bp = value; 244 struct clone_single_bp_data *data = u; 245 246 data->error = 0; 247 struct breakpoint *clone = malloc(sizeof(*clone)); 248 if (clone == NULL 249 || breakpoint_clone(clone, data->new_proc, 250 bp, data->old_proc) < 0) { 251 fail: 252 free(clone); 253 data->error = -1; 254 } 255 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) { 256 breakpoint_destroy(clone); 257 goto fail; 258 } 259} 260 261int 262process_clone(struct Process *retp, struct Process *proc, pid_t pid) 263{ 264 if (process_bare_init(retp, proc->filename, pid, 0) < 0) { 265 fail1: 266 fprintf(stderr, "failed to clone process %d->%d : %s\n", 267 proc->pid, pid, strerror(errno)); 268 return -1; 269 } 270 271 retp->tracesysgood = proc->tracesysgood; 272 retp->e_machine = proc->e_machine; 273 retp->e_class = proc->e_class; 274 275 /* For non-leader processes, that's all we need to do. */ 276 if (retp->leader != retp) 277 return 0; 278 279 /* Clone symbols first so that we can clone and relink 280 * breakpoints. */ 281 struct library *lib; 282 struct library **nlibp = &retp->libraries; 283 for (lib = proc->libraries; lib != NULL; lib = lib->next) { 284 *nlibp = malloc(sizeof(**nlibp)); 285 if (*nlibp == NULL 286 || library_clone(*nlibp, lib) < 0) { 287 fail2: 288 process_bare_destroy(retp, 0); 289 290 /* Error when cloning. Unroll what was done. */ 291 for (lib = retp->libraries; lib != NULL; ) { 292 struct library *next = lib->next; 293 library_destroy(lib); 294 free(lib); 295 lib = next; 296 } 297 goto fail1; 298 } 299 300 nlibp = &(*nlibp)->next; 301 } 302 303 /* Now clone breakpoints. Symbol relinking is done in 304 * clone_single_bp. */ 305 struct clone_single_bp_data data = { 306 .old_proc = proc, 307 .new_proc = retp, 308 .error = 0, 309 }; 310 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data); 311 if (data.error < 0) 312 goto fail2; 313 314 /* And finally the call stack. */ 315 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack)); 316 retp->callstack_depth = proc->callstack_depth; 317 318 size_t i; 319 for (i = 0; i < retp->callstack_depth; ++i) { 320 struct fetch_context *ctx = retp->callstack[i].fetch_context; 321 if (ctx != NULL) { 322 struct fetch_context *nctx = fetch_arg_clone(retp, ctx); 323 if (nctx == NULL) { 324 size_t j; 325 fail3: 326 for (j = 0; j < i; ++j) { 327 nctx = retp->callstack[i].fetch_context; 328 fetch_arg_done(nctx); 329 retp->callstack[i].fetch_context = NULL; 330 } 331 goto fail2; 332 } 333 retp->callstack[i].fetch_context = nctx; 334 } 335 336 struct value_dict *args = retp->callstack[i].arguments; 337 if (args != NULL) { 338 struct value_dict *nargs = malloc(sizeof(*nargs)); 339 if (nargs == NULL 340 || val_dict_clone(nargs, args) < 0) { 341 size_t j; 342 fail4: 343 for (j = 0; j < i; ++j) { 344 nargs = retp->callstack[i].arguments; 345 val_dict_destroy(nargs); 346 free(nargs); 347 retp->callstack[i].arguments = NULL; 348 } 349 350 /* Pretend that this round went well, 351 * so that fail3 frees I-th 352 * fetch_context. */ 353 ++i; 354 goto fail3; 355 } 356 retp->callstack[i].arguments = nargs; 357 } 358 } 359 360 if (arch_process_clone(retp, proc) < 0) 361 goto fail4; 362 363 return 0; 364} 365 366static int 367open_one_pid(pid_t pid) 368{ 369 Process *proc; 370 char *filename; 371 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid); 372 373 /* Get the filename first. Should the trace_pid fail, we can 374 * easily free it, untracing is more work. */ 375 if ((filename = pid2name(pid)) == NULL 376 || trace_pid(pid) < 0) { 377 free(filename); 378 return -1; 379 } 380 381 proc = open_program(filename, pid); 382 if (proc == NULL) 383 return -1; 384 trace_set_options(proc); 385 386 return 0; 387} 388 389static enum callback_status 390start_one_pid(Process * proc, void * data) 391{ 392 continue_process(proc->pid); 393 return CBS_CONT; 394} 395 396void 397open_pid(pid_t pid) 398{ 399 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); 400 /* If we are already tracing this guy, we should be seeing all 401 * his children via normal tracing route. */ 402 if (pid2proc(pid) != NULL) 403 return; 404 405 /* First, see if we can attach the requested PID itself. */ 406 if (open_one_pid(pid)) { 407 fprintf(stderr, "Cannot attach to pid %u: %s\n", 408 pid, strerror(errno)); 409 trace_fail_warning(pid); 410 return; 411 } 412 413 /* Now attach to all tasks that belong to that PID. There's a 414 * race between process_tasks and open_one_pid. So when we 415 * fail in open_one_pid below, we just do another round. 416 * Chances are that by then that PID will have gone away, and 417 * that's why we have seen the failure. The processes that we 418 * manage to open_one_pid are stopped, so we should eventually 419 * reach a point where process_tasks doesn't give any new 420 * processes (because there's nobody left to produce 421 * them). */ 422 size_t old_ntasks = 0; 423 int have_all; 424 while (1) { 425 pid_t *tasks; 426 size_t ntasks; 427 size_t i; 428 429 if (process_tasks(pid, &tasks, &ntasks) < 0) { 430 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", 431 pid, strerror(errno)); 432 break; 433 } 434 435 have_all = 1; 436 for (i = 0; i < ntasks; ++i) 437 if (pid2proc(tasks[i]) == NULL 438 && open_one_pid(tasks[i])) 439 have_all = 0; 440 441 free(tasks); 442 443 if (have_all && old_ntasks == ntasks) 444 break; 445 old_ntasks = ntasks; 446 } 447 448 struct Process *leader = pid2proc(pid)->leader; 449 450 /* XXX Is there a way to figure out whether _start has 451 * actually already been hit? */ 452 arch_dynlink_done(leader); 453 454 /* Done. Continue everyone. */ 455 each_task(leader, NULL, start_one_pid, NULL); 456} 457 458static enum callback_status 459find_proc(Process * proc, void * data) 460{ 461 pid_t pid = (pid_t)(uintptr_t)data; 462 return proc->pid == pid ? CBS_STOP : CBS_CONT; 463} 464 465Process * 466pid2proc(pid_t pid) { 467 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); 468} 469 470static Process * list_of_processes = NULL; 471 472static void 473unlist_process(Process * proc) 474{ 475 Process *tmp; 476 477 if (list_of_processes == proc) { 478 list_of_processes = list_of_processes->next; 479 return; 480 } 481 482 for (tmp = list_of_processes; ; tmp = tmp->next) { 483 /* If the following assert fails, the process wasn't 484 * in the list. */ 485 assert(tmp->next != NULL); 486 487 if (tmp->next == proc) { 488 tmp->next = tmp->next->next; 489 return; 490 } 491 } 492} 493 494struct Process * 495each_process(struct Process *start_after, 496 enum callback_status(*cb)(struct Process *proc, void *data), 497 void *data) 498{ 499 struct Process *it = start_after == NULL ? list_of_processes 500 : start_after->next; 501 502 while (it != NULL) { 503 /* Callback might call remove_process. */ 504 struct Process *next = it->next; 505 switch ((*cb)(it, data)) { 506 case CBS_FAIL: 507 /* XXX handle me */ 508 case CBS_STOP: 509 return it; 510 case CBS_CONT: 511 break; 512 } 513 it = next; 514 } 515 return NULL; 516} 517 518Process * 519each_task(struct Process *proc, struct Process *start_after, 520 enum callback_status(*cb)(struct Process *proc, void *data), 521 void *data) 522{ 523 assert(proc != NULL); 524 struct Process *it = start_after == NULL ? proc->leader 525 : start_after->next; 526 527 if (it != NULL) { 528 struct Process *leader = it->leader; 529 while (it != NULL && it->leader == leader) { 530 /* Callback might call remove_process. */ 531 struct Process *next = it->next; 532 switch ((*cb)(it, data)) { 533 case CBS_FAIL: 534 /* XXX handle me */ 535 case CBS_STOP: 536 return it; 537 case CBS_CONT: 538 break; 539 } 540 it = next; 541 } 542 } 543 return NULL; 544} 545 546static void 547add_process(struct Process *proc, int was_exec) 548{ 549 Process ** leaderp = &list_of_processes; 550 if (proc->pid) { 551 pid_t tgid = process_leader(proc->pid); 552 if (tgid == 0) 553 /* Must have been terminated before we managed 554 * to fully attach. */ 555 return; 556 if (tgid == proc->pid) 557 proc->leader = proc; 558 else { 559 Process * leader = pid2proc(tgid); 560 proc->leader = leader; 561 if (leader != NULL) 562 leaderp = &leader->next; 563 } 564 } 565 566 if (!was_exec) { 567 proc->next = *leaderp; 568 *leaderp = proc; 569 } 570} 571 572void 573change_process_leader(Process * proc, Process * leader) 574{ 575 Process ** leaderp = &list_of_processes; 576 if (proc->leader == leader) 577 return; 578 579 assert(leader != NULL); 580 unlist_process(proc); 581 if (proc != leader) 582 leaderp = &leader->next; 583 584 proc->leader = leader; 585 proc->next = *leaderp; 586 *leaderp = proc; 587} 588 589static enum callback_status 590clear_leader(struct Process *proc, void *data) 591{ 592 debug(DEBUG_FUNCTION, "detach_task %d from leader %d", 593 proc->pid, proc->leader->pid); 594 proc->leader = NULL; 595 return CBS_CONT; 596} 597 598void 599remove_process(Process *proc) 600{ 601 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); 602 603 if (proc->leader == proc) 604 each_task(proc, NULL, &clear_leader, NULL); 605 606 unlist_process(proc); 607 process_removed(proc); 608 process_destroy(proc); 609 free(proc); 610} 611 612void 613install_event_handler(Process *proc, struct event_handler *handler) 614{ 615 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler); 616 assert(proc->event_handler == NULL); 617 proc->event_handler = handler; 618} 619 620void 621destroy_event_handler(Process * proc) 622{ 623 struct event_handler *handler = proc->event_handler; 624 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler); 625 assert(handler != NULL); 626 if (handler->destroy != NULL) 627 handler->destroy(handler); 628 free(handler); 629 proc->event_handler = NULL; 630} 631 632static enum callback_status 633breakpoint_for_symbol(struct library_symbol *libsym, void *data) 634{ 635 arch_addr_t bp_addr; 636 struct Process *proc = data; 637 assert(proc->leader == proc); 638 639 bp_addr = sym2addr(proc, libsym); 640 /* If there is an artificial breakpoint on the same address, 641 * its libsym will be NULL, and we can smuggle our libsym 642 * there. That artificial breakpoint is there presumably for 643 * the callbacks, which we don't touch. If there is a real 644 * breakpoint, then this is a bug. ltrace-elf.c should filter 645 * symbols and ignore extra symbol aliases. 646 * 647 * The other direction is more complicated and currently not 648 * supported. If a breakpoint has custom callbacks, it might 649 * be also custom-allocated, and we would really need to swap 650 * the two: delete the one now in the dictionary, swap values 651 * around, and put the new breakpoint back in. */ 652 struct breakpoint *bp = dict_find_entry(proc->breakpoints, 653 bp_addr); 654 if (bp != NULL) { 655 assert(bp->libsym == NULL); 656 bp->libsym = libsym; 657 return CBS_CONT; 658 } 659 660 bp = malloc(sizeof(*bp)); 661 if (bp == NULL 662 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) { 663 fail: 664 free(bp); 665 return CBS_FAIL; 666 } 667 if (proc_add_breakpoint(proc, bp) < 0) { 668 breakpoint_destroy(bp); 669 goto fail; 670 } 671 672 if (breakpoint_turn_on(bp, proc) < 0) { 673 proc_remove_breakpoint(proc, bp); 674 breakpoint_destroy(bp); 675 goto fail; 676 } 677 678 return CBS_CONT; 679} 680 681void 682proc_add_library(struct Process *proc, struct library *lib) 683{ 684 assert(lib->next == NULL); 685 lib->next = proc->libraries; 686 proc->libraries = lib; 687 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d", 688 lib->soname, lib->base, lib->pathname, proc->pid); 689 690 struct library_symbol *libsym = NULL; 691 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol, 692 proc)) != NULL) 693 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s", 694 libsym->name, proc->pid, strerror(errno)); 695} 696 697int 698proc_remove_library(struct Process *proc, struct library *lib) 699{ 700 struct library **libp; 701 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next) 702 if (*libp == lib) { 703 *libp = lib->next; 704 return 0; 705 } 706 return -1; 707} 708 709struct library * 710proc_each_library(struct Process *proc, struct library *it, 711 enum callback_status (*cb)(struct Process *proc, 712 struct library *lib, void *data), 713 void *data) 714{ 715 if (it == NULL) 716 it = proc->libraries; 717 718 while (it != NULL) { 719 struct library *next = it->next; 720 721 switch (cb(proc, it, data)) { 722 case CBS_FAIL: 723 /* XXX handle me */ 724 case CBS_STOP: 725 return it; 726 case CBS_CONT: 727 break; 728 } 729 730 it = next; 731 } 732 733 return NULL; 734} 735 736static void 737check_leader(struct Process *proc) 738{ 739 /* Only the group leader should be getting the breakpoints and 740 * thus have ->breakpoint initialized. */ 741 assert(proc->leader != NULL); 742 assert(proc->leader == proc); 743 assert(proc->breakpoints != NULL); 744} 745 746int 747proc_add_breakpoint(struct Process *proc, struct breakpoint *bp) 748{ 749 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)", 750 proc->pid, breakpoint_name(bp), bp->addr); 751 check_leader(proc); 752 753 /* XXX We might merge bp->libsym instead of the following 754 * assert, but that's not necessary right now. Read the 755 * comment in breakpoint_for_symbol. */ 756 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL); 757 758 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) { 759 fprintf(stderr, 760 "couldn't enter breakpoint %s@%p to dictionary: %s\n", 761 breakpoint_name(bp), bp->addr, strerror(errno)); 762 return -1; 763 } 764 765 return 0; 766} 767 768void 769proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp) 770{ 771 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)", 772 proc->pid, breakpoint_name(bp), bp->addr); 773 check_leader(proc); 774 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr); 775 assert(removed == bp); 776} 777 778/* Dict doesn't support iteration restarts, so here's this contraption 779 * for now. XXX add restarts to dict. */ 780struct each_breakpoint_data 781{ 782 void *start; 783 void *end; 784 struct Process *proc; 785 enum callback_status (*cb)(struct Process *proc, 786 struct breakpoint *bp, 787 void *data); 788 void *cb_data; 789}; 790 791static void 792each_breakpoint_cb(void *key, void *value, void *d) 793{ 794 struct each_breakpoint_data *data = d; 795 if (data->end != NULL) 796 return; 797 if (data->start == key) 798 data->start = NULL; 799 800 if (data->start == NULL) { 801 switch (data->cb(data->proc, value, data->cb_data)) { 802 case CBS_FAIL: 803 /* XXX handle me */ 804 case CBS_STOP: 805 data->end = key; 806 case CBS_CONT: 807 return; 808 } 809 } 810} 811 812void * 813proc_each_breakpoint(struct Process *proc, void *start, 814 enum callback_status (*cb)(struct Process *proc, 815 struct breakpoint *bp, 816 void *data), void *data) 817{ 818 struct each_breakpoint_data dd = { 819 .start = start, 820 .proc = proc, 821 .cb = cb, 822 .cb_data = data, 823 }; 824 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd); 825 return dd.end; 826} 827