proc.c revision b5f80ac8982c40f79915ce1e1cb9bf8650ac5fe7
1#include "config.h" 2 3#if defined(HAVE_LIBUNWIND) 4#include <libunwind.h> 5#include <libunwind-ptrace.h> 6#endif /* defined(HAVE_LIBUNWIND) */ 7 8#include <sys/types.h> 9#include <string.h> 10#include <stdio.h> 11#include <errno.h> 12#include <stdlib.h> 13#include <assert.h> 14#include <error.h> 15 16#include "common.h" 17#include "breakpoint.h" 18#include "proc.h" 19 20static int 21process_bare_init(struct Process *proc, const char *filename, pid_t pid) 22{ 23 fprintf(stderr, "process_bare_init %s %d\n", filename, pid); 24 memset(proc, 0, sizeof(*proc)); 25 26 proc->filename = strdup(filename); 27 if (proc->filename == NULL) { 28 fail: 29 free(proc->filename); 30 if (proc->breakpoints != NULL) 31 dict_clear(proc->breakpoints); 32 return -1; 33 } 34 35 /* Add process so that we know who the leader is. */ 36 proc->pid = pid; 37 add_process(proc); 38 if (proc->leader == NULL) 39 goto fail; 40 41 if (proc->leader == proc) { 42 proc->breakpoints = dict_init(dict_key2hash_int, 43 dict_key_cmp_int); 44 if (proc->breakpoints == NULL) 45 goto fail; 46 } else { 47 proc->breakpoints = NULL; 48 } 49 50#if defined(HAVE_LIBUNWIND) 51 proc->unwind_priv = _UPT_create(pid); 52 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0); 53#endif /* defined(HAVE_LIBUNWIND) */ 54 55 return 0; 56} 57 58static void 59process_bare_destroy(struct Process *proc) 60{ 61 free(proc->filename); 62 dict_clear(proc->breakpoints); 63 remove_process(proc); 64} 65 66int 67process_init(struct Process *proc, const char *filename, pid_t pid, int enable) 68{ 69 fprintf(stderr, "process_init %s %d enable=%d\n", filename, pid, enable); 70 if (process_bare_init(proc, filename, pid) < 0) { 71 error(0, errno, "init process %d", pid); 72 return -1; 73 } 74 75 if (proc->leader == proc && breakpoints_init(proc, enable) < 0) { 76 fprintf(stderr, "failed to init breakpoints %d\n", 77 proc->pid); 78 process_bare_destroy(proc); 79 return -1; 80 } 81 82 return 0; 83} 84 85struct Process * 86open_program(const char *filename, pid_t pid, int enable) 87{ 88 fprintf(stderr, "open_program %s %d enable=%d\n", 89 filename, pid, enable); 90 assert(pid != 0); 91 struct Process *proc = malloc(sizeof(*proc)); 92 if (proc == NULL 93 || process_init(proc, filename, pid, enable) < 0) { 94 free(proc); 95 return NULL; 96 } 97 return proc; 98} 99 100struct clone_single_bp_data { 101 struct Process *old_proc; 102 struct Process *new_proc; 103 int error; 104}; 105 106struct find_symbol_data { 107 struct library_symbol *old_libsym; 108 struct library_symbol *found_libsym; 109}; 110 111static enum callback_status 112find_sym_in_lib(struct Process *proc, struct library *lib, void *u) 113{ 114 struct find_symbol_data *fs = u; 115 fs->found_libsym 116 = library_each_symbol(lib, NULL, library_symbol_equal_cb, 117 fs->old_libsym); 118 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT; 119} 120 121static void 122clone_single_bp(void *key, void *value, void *u) 123{ 124 target_address_t addr = (target_address_t)key; 125 struct breakpoint *bp = value; 126 struct clone_single_bp_data *data = u; 127 128 /* Find library and symbol that this symbol was linked to. */ 129 struct library_symbol *libsym = bp->libsym; 130 struct library *lib = NULL; 131 if (libsym != NULL) { 132 struct find_symbol_data f_data = { 133 .old_libsym = libsym, 134 }; 135 lib = proc_each_library(data->old_proc, NULL, 136 find_sym_in_lib, &f_data); 137 assert(lib != NULL); 138 libsym = f_data.found_libsym; 139 } 140 141 /* LIB and LIBSYM now hold the new library and symbol that 142 * correspond to the original breakpoint. Now we can do the 143 * clone itself. */ 144 struct breakpoint *clone = malloc(sizeof(*clone)); 145 if (clone == NULL 146 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) { 147 data->error = -1; 148 return; 149 } 150 breakpoint_set_callbacks(clone, bp->cbs); 151} 152 153int 154process_clone(struct Process *retp, struct Process *proc, pid_t pid) 155{ 156 if (process_bare_init(retp, proc->filename, pid) < 0) { 157 fail: 158 error(0, errno, "clone process %d->%d", proc->pid, pid); 159 return -1; 160 } 161 162 /* For non-leader processes, that's all we need to do. */ 163 if (proc->leader != proc) 164 return 0; 165 166 /* Clone symbols first so that we can clone and relink 167 * breakpoints. */ 168 struct library *lib; 169 struct library **nlibp = &retp->libraries; 170 for (lib = proc->libraries; lib != NULL; lib = lib->next) { 171 *nlibp = malloc(sizeof(**nlibp)); 172 if (*nlibp == NULL 173 || library_clone(*nlibp, lib) < 0) { 174 fail2: 175 process_bare_destroy(retp); 176 177 /* Error when cloning. Unroll what was done. */ 178 for (lib = retp->libraries; lib != NULL; ) { 179 struct library *next = lib->next; 180 library_destroy(lib); 181 free(lib); 182 lib = next; 183 } 184 goto fail; 185 } 186 187 nlibp = &(*nlibp)->next; 188 } 189 190 /* Now clone breakpoints. Symbol relinking is done in 191 * clone_single_bp. */ 192 struct clone_single_bp_data data = { 193 .old_proc = proc, 194 .new_proc = retp, 195 .error = 0, 196 }; 197 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data); 198 199 if (data.error < 0) 200 goto fail2; 201 202 return 0; 203} 204 205static int 206open_one_pid(pid_t pid) 207{ 208 Process *proc; 209 char *filename; 210 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid); 211 212 /* Get the filename first. Should the trace_pid fail, we can 213 * easily free it, untracing is more work. */ 214 if ((filename = pid2name(pid)) == NULL 215 || trace_pid(pid) < 0) { 216 free(filename); 217 return -1; 218 } 219 220 proc = open_program(filename, pid, 0); 221 if (proc == NULL) 222 return -1; 223 trace_set_options(proc, pid); 224 225 return 0; 226} 227 228static enum callback_status 229start_one_pid(Process * proc, void * data) 230{ 231 continue_process(proc->pid); 232 return CBS_CONT; 233} 234 235void 236open_pid(pid_t pid) 237{ 238 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); 239 /* If we are already tracing this guy, we should be seeing all 240 * his children via normal tracing route. */ 241 if (pid2proc(pid) != NULL) 242 return; 243 244 /* First, see if we can attach the requested PID itself. */ 245 if (open_one_pid(pid)) { 246 fprintf(stderr, "Cannot attach to pid %u: %s\n", 247 pid, strerror(errno)); 248 trace_fail_warning(pid); 249 return; 250 } 251 252 /* Now attach to all tasks that belong to that PID. There's a 253 * race between process_tasks and open_one_pid. So when we 254 * fail in open_one_pid below, we just do another round. 255 * Chances are that by then that PID will have gone away, and 256 * that's why we have seen the failure. The processes that we 257 * manage to open_one_pid are stopped, so we should eventually 258 * reach a point where process_tasks doesn't give any new 259 * processes (because there's nobody left to produce 260 * them). */ 261 size_t old_ntasks = 0; 262 int have_all; 263 while (1) { 264 pid_t *tasks; 265 size_t ntasks; 266 size_t i; 267 268 if (process_tasks(pid, &tasks, &ntasks) < 0) { 269 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", 270 pid, strerror(errno)); 271 break; 272 } 273 274 have_all = 1; 275 for (i = 0; i < ntasks; ++i) 276 if (pid2proc(tasks[i]) == NULL 277 && open_one_pid(tasks[i])) 278 have_all = 0; 279 280 free(tasks); 281 282 if (have_all && old_ntasks == ntasks) 283 break; 284 old_ntasks = ntasks; 285 } 286 287 /* Done. Now initialize breakpoints and then continue 288 * everyone. */ 289 Process * leader; 290 leader = pid2proc(pid)->leader; 291 enable_all_breakpoints(leader); 292 293 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL); 294} 295 296static enum callback_status 297find_proc(Process * proc, void * data) 298{ 299 pid_t pid = (pid_t)(uintptr_t)data; 300 return proc->pid == pid ? CBS_STOP : CBS_CONT; 301} 302 303Process * 304pid2proc(pid_t pid) { 305 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); 306} 307 308static Process * list_of_processes = NULL; 309 310static void 311unlist_process(Process * proc) 312{ 313 Process *tmp; 314 315 if (list_of_processes == proc) { 316 list_of_processes = list_of_processes->next; 317 return; 318 } 319 320 for (tmp = list_of_processes; ; tmp = tmp->next) { 321 /* If the following assert fails, the process wasn't 322 * in the list. */ 323 assert(tmp->next != NULL); 324 325 if (tmp->next == proc) { 326 tmp->next = tmp->next->next; 327 return; 328 } 329 } 330} 331 332struct Process * 333each_process(struct Process *start_after, 334 enum callback_status(*cb)(struct Process *proc, void *data), 335 void *data) 336{ 337 struct Process *it = start_after == NULL ? list_of_processes 338 : start_after->next; 339 340 while (it != NULL) { 341 /* Callback might call remove_process. */ 342 struct Process *next = it->next; 343 switch ((*cb)(it, data)) { 344 case CBS_FAIL: 345 /* XXX handle me */ 346 case CBS_STOP: 347 return it; 348 case CBS_CONT: 349 break; 350 } 351 it = next; 352 } 353 return NULL; 354} 355 356Process * 357each_task(struct Process *proc, struct Process *start_after, 358 enum callback_status(*cb)(struct Process *proc, void *data), 359 void *data) 360{ 361 assert(proc != NULL); 362 struct Process *it = start_after == NULL ? proc->leader 363 : start_after->next; 364 365 if (it != NULL) { 366 struct Process *leader = it->leader; 367 while (it != NULL && it->leader == leader) { 368 /* Callback might call remove_process. */ 369 struct Process *next = it->next; 370 switch ((*cb)(it, data)) { 371 case CBS_FAIL: 372 /* XXX handle me */ 373 case CBS_STOP: 374 return it; 375 case CBS_CONT: 376 break; 377 } 378 it = next; 379 } 380 } 381 return NULL; 382} 383 384void 385add_process(Process * proc) 386{ 387 fprintf(stderr, "add_process %d\n", proc->pid); 388 Process ** leaderp = &list_of_processes; 389 if (proc->pid) { 390 pid_t tgid = process_leader(proc->pid); 391 fprintf(stderr, " + leader is %d\n", tgid); 392 if (tgid == 0) 393 /* Must have been terminated before we managed 394 * to fully attach. */ 395 return; 396 if (tgid == proc->pid) 397 proc->leader = proc; 398 else { 399 Process * leader = pid2proc(tgid); 400 proc->leader = leader; 401 if (leader != NULL) 402 leaderp = &leader->next; 403 } 404 } 405 proc->next = *leaderp; 406 *leaderp = proc; 407} 408 409void 410change_process_leader(Process * proc, Process * leader) 411{ 412 Process ** leaderp = &list_of_processes; 413 if (proc->leader == leader) 414 return; 415 416 assert(leader != NULL); 417 unlist_process(proc); 418 if (proc != leader) 419 leaderp = &leader->next; 420 421 proc->leader = leader; 422 proc->next = *leaderp; 423 *leaderp = proc; 424} 425 426static enum callback_status 427clear_leader(struct Process *proc, void *data) 428{ 429 debug(DEBUG_FUNCTION, "detach_task %d from leader %d", 430 proc->pid, proc->leader->pid); 431 proc->leader = NULL; 432 return CBS_CONT; 433} 434 435static enum ecb_status 436event_for_proc(Event * event, void * data) 437{ 438 if (event->proc == data) 439 return ecb_deque; 440 else 441 return ecb_cont; 442} 443 444static void 445delete_events_for(Process * proc) 446{ 447 Event * event; 448 while ((event = each_qd_event(&event_for_proc, proc)) != NULL) 449 free(event); 450} 451 452void 453remove_process(Process *proc) 454{ 455 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); 456 457 if (proc->leader == proc) 458 each_task(proc, NULL, &clear_leader, NULL); 459 460 unlist_process(proc); 461 delete_events_for(proc); 462} 463 464void 465install_event_handler(Process *proc, struct event_handler *handler) 466{ 467 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler); 468 assert(proc->event_handler == NULL); 469 proc->event_handler = handler; 470} 471 472void 473destroy_event_handler(Process * proc) 474{ 475 struct event_handler *handler = proc->event_handler; 476 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler); 477 assert(handler != NULL); 478 if (handler->destroy != NULL) 479 handler->destroy(handler); 480 free(handler); 481 proc->event_handler = NULL; 482} 483 484static enum callback_status 485breakpoint_for_symbol(struct library_symbol *libsym, void *data) 486{ 487 struct Process *proc = data; 488 489 struct breakpoint *bp = malloc(sizeof(*bp)); 490 if (bp == NULL 491 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) { 492 fail: 493 free(bp); 494 return CBS_FAIL; 495 } 496 if (proc_add_breakpoint(proc, bp) < 0) { 497 breakpoint_destroy(bp); 498 goto fail; 499 } 500 501 /* If this is dlopened library, turn on the breakpoint right 502 * away. */ 503 if (proc->fixed_libs != NULL 504 && breakpoint_turn_on(bp) < 0) { 505 proc_remove_breakpoint(proc, bp); 506 breakpoint_destroy(bp); 507 goto fail; 508 } 509 510 return CBS_CONT; 511} 512 513void 514proc_add_library(struct Process *proc, struct library *lib) 515{ 516 assert(lib->next == NULL); 517 lib->next = proc->libraries; 518 proc->libraries = lib; 519 fprintf(stderr, "=== Added library %s@%p (%s) to %d:\n", 520 lib->soname, lib->base, lib->pathname, proc->pid); 521 522 struct library_symbol *libsym = NULL; 523 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol, 524 proc)) != NULL) 525 error(0, errno, "insert breakpoint for %s", libsym->name); 526} 527 528int 529proc_remove_library(struct Process *proc, struct library *lib) 530{ 531 struct library **libp; 532 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next) 533 if (*libp == lib) { 534 *libp = lib->next; 535 return 0; 536 } 537 return -1; 538} 539 540struct library * 541proc_each_library(struct Process *proc, struct library *it, 542 enum callback_status (*cb)(struct Process *proc, 543 struct library *lib, void *data), 544 void *data) 545{ 546 if (it == NULL) 547 it = proc->libraries; 548 549 while (it != NULL) { 550 struct library *next = it->next; 551 552 switch (cb(proc, it, data)) { 553 case CBS_FAIL: 554 /* XXX handle me */ 555 case CBS_STOP: 556 return it; 557 case CBS_CONT: 558 break; 559 } 560 561 it = next; 562 } 563 564 return NULL; 565} 566 567int 568proc_add_breakpoint(struct Process *proc, struct breakpoint *bp) 569{ 570 struct Process *leader = proc->leader; 571 572 /* Only the group leader should be getting the breakpoints and 573 * thus have ->breakpoint initialized. */ 574 assert(leader != NULL); 575 assert(leader->breakpoints != NULL); 576 577 /* Make sure it wasn't inserted yet. */ 578 assert(bp->proc == NULL); 579 580 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)", 581 proc->pid, breakpoint_name(bp), bp->addr); 582 583 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL); 584 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) { 585 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary", 586 breakpoint_name(bp), bp->addr); 587 return -1; 588 } 589 590 bp->proc = proc; 591 return 0; 592} 593 594int 595proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp) 596{ 597 /* XXX We can't, really. We are missing dict_remove. */ 598 assert(!"Not yet implemented!"); 599 abort(); 600} 601