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
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.
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.
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
25 #include <sys/types.h>
33 #include "breakpoint.h"
38 #include "value_dict.h"
40 #ifndef OS_HAVE_PROCESS_DATA
42 os_process_init(struct process *proc)
48 os_process_destroy(struct process *proc)
53 os_process_clone(struct process *retp, struct process *proc)
59 os_process_exec(struct process *proc)
65 #ifndef ARCH_HAVE_PROCESS_DATA
67 arch_process_init(struct process *proc)
73 arch_process_destroy(struct process *proc)
78 arch_process_clone(struct process *retp, struct process *proc)
84 arch_process_exec(struct process *proc)
90 #ifndef ARCH_HAVE_DYNLINK_DONE
92 arch_dynlink_done(struct process *proc)
97 static int add_process(struct process *proc, int was_exec);
98 static void unlist_process(struct process *proc);
101 destroy_unwind(struct process *proc)
103 #if defined(HAVE_LIBUNWIND)
104 if (proc->unwind_priv != NULL)
105 _UPT_destroy(proc->unwind_priv);
106 if (proc->unwind_as != NULL)
107 unw_destroy_addr_space(proc->unwind_as);
108 #endif /* defined(HAVE_LIBUNWIND) */
112 process_bare_init(struct process *proc, const char *filename,
113 pid_t pid, int was_exec)
116 memset(proc, 0, sizeof(*proc));
118 proc->filename = strdup(filename);
119 if (proc->filename == NULL) {
121 free(proc->filename);
122 if (proc->breakpoints != NULL) {
123 dict_destroy(proc->breakpoints,
125 free(proc->breakpoints);
126 proc->breakpoints = NULL;
132 /* Add process so that we know who the leader is. */
134 if (add_process(proc, was_exec) < 0)
136 if (proc->leader == NULL) {
139 unlist_process(proc);
143 if (proc->leader == proc) {
144 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
145 if (proc->breakpoints == NULL)
146 goto unlist_and_fail;
147 DICT_INIT(proc->breakpoints,
148 arch_addr_t, struct breakpoint *,
149 arch_addr_hash, arch_addr_eq, NULL);
151 proc->breakpoints = NULL;
154 #if defined(HAVE_LIBUNWIND)
155 if (options.bt_depth > 0) {
156 proc->unwind_priv = _UPT_create(pid);
157 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
159 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
161 "Couldn't initialize unwinding "
162 "for process %d\n", proc->pid);
163 destroy_unwind(proc);
164 proc->unwind_priv = NULL;
165 proc->unwind_as = NULL;
168 #endif /* defined(HAVE_LIBUNWIND) */
174 process_bare_destroy(struct process *proc, int was_exec)
176 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
177 free(proc->breakpoints);
179 free(proc->filename);
180 unlist_process(proc);
181 destroy_unwind(proc);
186 process_init_main(struct process *proc)
188 if (breakpoints_init(proc) < 0) {
189 fprintf(stderr, "failed to init breakpoints %d\n",
198 process_init(struct process *proc, const char *filename, pid_t pid)
200 if (process_bare_init(proc, filename, pid, 0) < 0) {
202 fprintf(stderr, "failed to initialize process %d: %s\n",
203 pid, strerror(errno));
207 if (os_process_init(proc) < 0) {
208 process_bare_destroy(proc, 0);
212 if (arch_process_init(proc) < 0) {
213 os_process_destroy(proc);
214 process_bare_destroy(proc, 0);
218 if (proc->leader != proc)
220 if (process_init_main(proc) < 0) {
221 process_bare_destroy(proc, 0);
227 static enum callback_status
228 destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
230 breakpoint_destroy(bp);
235 // XXX see comment in handle_event.c
236 void callstack_pop(struct process *proc);
239 private_process_destroy(struct process *proc, int was_exec)
241 /* Pop remaining stack elements. */
242 while (proc->callstack_depth > 0) {
243 /* When this is called just before a process is
244 * destroyed, the breakpoints should either have been
245 * retracted by now, or were killed by exec. In any
246 * case, it's safe to pretend that there are no
247 * breakpoints associated with the stack elements, so
248 * that stack_pop doesn't attempt to destroy them. */
249 size_t i = proc->callstack_depth - 1;
250 if (!proc->callstack[i].is_syscall)
251 proc->callstack[i].return_addr = 0;
257 free(proc->filename);
259 /* Libraries and symbols. This is only relevant in
262 for (lib = proc->libraries; lib != NULL; ) {
263 struct library *next = lib->next;
264 library_destroy(lib);
268 proc->libraries = NULL;
271 if (proc->breakpoints != NULL) {
272 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
273 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
274 free(proc->breakpoints);
275 proc->breakpoints = NULL;
278 destroy_unwind(proc);
282 process_destroy(struct process *proc)
284 arch_process_destroy(proc);
285 os_process_destroy(proc);
286 private_process_destroy(proc, 0);
290 process_exec(struct process *proc)
292 /* Call exec handlers first, before we destroy the main
294 if (arch_process_exec(proc) < 0
295 || os_process_exec(proc) < 0)
298 private_process_destroy(proc, 1);
300 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
302 if (process_init_main(proc) < 0) {
303 process_bare_destroy(proc, 1);
310 open_program(const char *filename, pid_t pid)
313 struct process *proc = malloc(sizeof(*proc));
314 if (proc == NULL || process_init(proc, filename, pid) < 0) {
321 struct clone_single_bp_data {
322 struct process *old_proc;
323 struct process *new_proc;
326 static enum callback_status
327 clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
329 struct breakpoint *bp = *bpp;
330 struct clone_single_bp_data *data = u;
332 struct breakpoint *clone = malloc(sizeof(*clone));
334 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
339 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
340 breakpoint_destroy(clone);
347 process_clone(struct process *retp, struct process *proc, pid_t pid)
349 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
351 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
352 proc->pid, pid, strerror(errno));
356 retp->tracesysgood = proc->tracesysgood;
357 retp->e_machine = proc->e_machine;
358 retp->e_class = proc->e_class;
360 /* For non-leader processes, that's all we need to do. */
361 if (retp->leader != retp)
364 /* Clone symbols first so that we can clone and relink
367 struct library **nlibp = &retp->libraries;
368 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
369 *nlibp = malloc(sizeof(**nlibp));
372 || library_clone(*nlibp, lib) < 0) {
377 process_bare_destroy(retp, 0);
379 /* Error when cloning. Unroll what was done. */
380 for (lib = retp->libraries; lib != NULL; ) {
381 struct library *next = lib->next;
382 library_destroy(lib);
389 nlibp = &(*nlibp)->next;
392 /* Now clone breakpoints. Symbol relinking is done in
393 * clone_single_bp. */
394 struct clone_single_bp_data data = {
398 if (DICT_EACH(proc->leader->breakpoints,
399 arch_addr_t, struct breakpoint *, NULL,
400 clone_single_bp, &data) != NULL)
403 /* And finally the call stack. */
404 /* XXX clearly the callstack handling should be moved to a
405 * separate module and this whole business extracted to
406 * callstack_clone, or callstack_element_clone. */
407 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
408 retp->callstack_depth = proc->callstack_depth;
411 for (i = 0; i < retp->callstack_depth; ++i) {
412 struct callstack_element *elem = &retp->callstack[i];
413 struct fetch_context *ctx = elem->fetch_context;
415 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
419 for (j = 0; j < i; ++j) {
420 nctx = retp->callstack[j].fetch_context;
421 fetch_arg_done(nctx);
422 elem->fetch_context = NULL;
426 elem->fetch_context = nctx;
429 if (elem->arguments != NULL) {
430 struct value_dict *nargs = malloc(sizeof(*nargs));
432 || val_dict_clone(nargs, elem->arguments) < 0) {
434 for (j = 0; j < i; ++j) {
435 nargs = retp->callstack[j].arguments;
436 val_dict_destroy(nargs);
438 elem->arguments = NULL;
441 /* Pretend that this round went well,
442 * so that fail3 frees I-th
447 elem->arguments = nargs;
450 /* If it's not a syscall, we need to find the
451 * corresponding library symbol in the cloned
453 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
454 struct library_symbol *libfunc = elem->c_un.libfunc;
455 int rc = proc_find_symbol(retp, libfunc,
456 NULL, &elem->c_un.libfunc);
461 /* At this point, retp is fully initialized, except for OS and
462 * arch parts, and we can call private_process_destroy. */
463 if (os_process_clone(retp, proc) < 0) {
464 private_process_destroy(retp, 0);
467 if (arch_process_clone(retp, proc) < 0) {
468 os_process_destroy(retp);
469 private_process_destroy(retp, 0);
477 open_one_pid(pid_t pid)
479 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
481 /* Get the filename first. Should the trace_pid fail, we can
482 * easily free it, untracing is more work. */
483 char *filename = pid2name(pid);
484 if (filename == NULL || trace_pid(pid) < 0) {
490 struct process *proc = open_program(filename, pid);
494 trace_set_options(proc);
499 static enum callback_status
500 start_one_pid(struct process *proc, void *data)
502 continue_process(proc->pid);
506 static enum callback_status
507 is_main(struct process *proc, struct library *lib, void *data)
509 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
513 process_hit_start(struct process *proc)
515 struct process *leader = proc->leader;
516 assert(leader != NULL);
518 struct library *mainlib
519 = proc_each_library(leader, NULL, is_main, NULL);
520 assert(mainlib != NULL);
521 linkmap_init(leader, mainlib->dyn_addr);
522 arch_dynlink_done(leader);
528 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
529 /* If we are already tracing this guy, we should be seeing all
530 * his children via normal tracing route. */
531 if (pid2proc(pid) != NULL)
534 /* First, see if we can attach the requested PID itself. */
535 if (open_one_pid(pid) < 0) {
536 fprintf(stderr, "Cannot attach to pid %u: %s\n",
537 pid, strerror(errno));
538 trace_fail_warning(pid);
542 /* Now attach to all tasks that belong to that PID. There's a
543 * race between process_tasks and open_one_pid. So when we
544 * fail in open_one_pid below, we just do another round.
545 * Chances are that by then that PID will have gone away, and
546 * that's why we have seen the failure. The processes that we
547 * manage to open_one_pid are stopped, so we should eventually
548 * reach a point where process_tasks doesn't give any new
549 * processes (because there's nobody left to produce
551 size_t old_ntasks = 0;
558 if (process_tasks(pid, &tasks, &ntasks) < 0) {
559 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
560 pid, strerror(errno));
565 for (i = 0; i < ntasks; ++i)
566 if (pid2proc(tasks[i]) == NULL
567 && open_one_pid(tasks[i]) < 0)
572 if (have_all && old_ntasks == ntasks)
577 struct process *leader = pid2proc(pid)->leader;
579 /* XXX Is there a way to figure out whether _start has
580 * actually already been hit? */
581 process_hit_start(leader);
583 /* Done. Continue everyone. */
584 each_task(leader, NULL, start_one_pid, NULL);
587 static enum callback_status
588 find_proc(struct process *proc, void *data)
590 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
596 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
599 static struct process *list_of_processes = NULL;
602 unlist_process(struct process *proc)
604 if (list_of_processes == proc) {
605 list_of_processes = list_of_processes->next;
610 for (tmp = list_of_processes; ; tmp = tmp->next) {
611 /* If the following assert fails, the process wasn't
613 assert(tmp->next != NULL);
615 if (tmp->next == proc) {
616 tmp->next = tmp->next->next;
623 each_process(struct process *start_after,
624 enum callback_status(*cb)(struct process *proc, void *data),
627 struct process *it = start_after == NULL ? list_of_processes
631 /* Callback might call remove_process. */
632 struct process *next = it->next;
633 switch ((*cb)(it, data)) {
647 each_task(struct process *proc, struct process *start_after,
648 enum callback_status(*cb)(struct process *proc, void *data),
651 assert(proc != NULL);
652 struct process *it = start_after == NULL ? proc->leader
656 struct process *leader = it->leader;
657 while (it != NULL && it->leader == leader) {
658 /* Callback might call remove_process. */
659 struct process *next = it->next;
660 switch ((*cb)(it, data)) {
675 add_process(struct process *proc, int was_exec)
677 struct process **leaderp = &list_of_processes;
679 pid_t tgid = process_leader(proc->pid);
681 /* Must have been terminated before we managed
682 * to fully attach. */
684 if (tgid == proc->pid) {
687 struct process *leader = pid2proc(tgid);
688 proc->leader = leader;
690 leaderp = &leader->next;
695 proc->next = *leaderp;
702 change_process_leader(struct process *proc, struct process *leader)
704 struct process **leaderp = &list_of_processes;
705 if (proc->leader == leader)
708 assert(leader != NULL);
709 unlist_process(proc);
711 leaderp = &leader->next;
713 proc->leader = leader;
714 proc->next = *leaderp;
718 static enum callback_status
719 clear_leader(struct process *proc, void *data)
721 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
722 proc->pid, proc->leader->pid);
728 remove_process(struct process *proc)
730 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
732 if (proc->leader == proc)
733 each_task(proc, NULL, &clear_leader, NULL);
735 unlist_process(proc);
736 process_removed(proc);
737 process_destroy(proc);
742 install_event_handler(struct process *proc, struct event_handler *handler)
744 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
745 assert(proc->event_handler == NULL);
746 proc->event_handler = handler;
750 destroy_event_handler(struct process *proc)
752 struct event_handler *handler = proc->event_handler;
753 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
754 assert(handler != NULL);
755 if (handler->destroy != NULL)
756 handler->destroy(handler);
758 proc->event_handler = NULL;
762 breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
765 assert(proc->leader == proc);
767 /* Don't enable latent or delayed symbols. */
768 if (libsym->latent || libsym->delayed) {
769 debug(DEBUG_FUNCTION,
770 "delayed and/or latent breakpoint pid=%d, %s@%p",
771 proc->pid, libsym->name, libsym->enter_addr);
775 bp_addr = sym2addr(proc, libsym);
777 /* If there is an artificial breakpoint on the same address,
778 * its libsym will be NULL, and we can smuggle our libsym
779 * there. That artificial breakpoint is there presumably for
780 * the callbacks, which we don't touch. If there is a real
781 * breakpoint, then this is a bug. ltrace-elf.c should filter
782 * symbols and ignore extra symbol aliases.
784 * The other direction is more complicated and currently not
785 * supported. If a breakpoint has custom callbacks, it might
786 * be also custom-allocated, and we would really need to swap
787 * the two: delete the one now in the dictionary, swap values
788 * around, and put the new breakpoint back in. */
789 struct breakpoint *bp;
790 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
791 /* MIPS backend makes duplicate requests. This is
792 * likely a bug in the backend. Currently there's no
793 * point assigning more than one symbol to a
794 * breakpoint, because when it hits, we won't know
795 * what to print out. But it's easier to fix it here
796 * before someone who understands MIPS has the time to
797 * look into it. So turn the sanity check off on
800 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
801 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
804 assert(bp->libsym == NULL);
810 bp = malloc(sizeof(*bp));
812 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
817 if (proc_add_breakpoint(proc, bp) < 0) {
818 breakpoint_destroy(bp);
822 if (breakpoint_turn_on(bp, proc) < 0) {
823 proc_remove_breakpoint(proc, bp);
824 breakpoint_destroy(bp);
831 static enum callback_status
832 cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
834 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
838 proc_activate_latent_symbol(struct process *proc,
839 struct library_symbol *libsym)
841 assert(libsym->latent);
843 debug(DEBUG_FUNCTION, "activated latent symbol");
844 return breakpoint_for_symbol(libsym, proc);
848 proc_activate_delayed_symbol(struct process *proc,
849 struct library_symbol *libsym)
851 assert(libsym->delayed);
853 debug(DEBUG_FUNCTION, "activated delayed symbol");
854 return breakpoint_for_symbol(libsym, proc);
857 static enum callback_status
858 activate_latent_in(struct process *proc, struct library *lib, void *data)
860 struct library_exported_name *exported;
861 for (exported = data; exported != NULL; exported = exported->next) {
862 struct library_symbol *libsym = NULL;
863 while ((libsym = library_each_symbol(lib, libsym,
864 library_symbol_named_cb,
865 (void *)exported->name))
868 && proc_activate_latent_symbol(proc, libsym) < 0)
875 proc_add_library(struct process *proc, struct library *lib)
877 assert(lib->next == NULL);
878 lib->next = proc->libraries;
879 proc->libraries = lib;
880 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
881 lib->soname, lib->base, lib->pathname, proc->pid);
883 /* Insert breakpoints for all active (non-latent) symbols. */
884 struct library_symbol *libsym = NULL;
885 while ((libsym = library_each_symbol(lib, libsym,
886 cb_breakpoint_for_symbol,
889 "Couldn't insert breakpoint for %s to %d: %s.\n",
890 libsym->name, proc->pid, strerror(errno));
892 /* Look through export list of the new library and compare it
893 * with latent symbols of all libraries (including this
894 * library itself). */
895 struct library *lib2 = NULL;
896 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
897 lib->exported_names)) != NULL)
899 "Couldn't activate latent symbols for %s in %d: %s.\n",
900 lib2->soname, proc->pid, strerror(errno));
904 proc_remove_library(struct process *proc, struct library *lib)
906 struct library **libp;
907 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
916 proc_each_library(struct process *proc, struct library *it,
917 enum callback_status (*cb)(struct process *proc,
918 struct library *lib, void *data),
922 it = proc->libraries;
927 struct library *next = it->next;
929 switch (cb(proc, it, data)) {
945 check_leader(struct process *proc)
947 /* Only the group leader should be getting the breakpoints and
948 * thus have ->breakpoint initialized. */
949 assert(proc->leader != NULL);
950 assert(proc->leader == proc);
951 assert(proc->breakpoints != NULL);
955 proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
957 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
958 proc->pid, breakpoint_name(bp), bp->addr);
961 /* XXX We might merge bp->libsym instead of the following
962 * assert, but that's not necessary right now. Read the
963 * comment in breakpoint_for_symbol. */
964 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
966 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
968 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
969 breakpoint_name(bp), bp->addr, strerror(errno));
977 proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
979 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
980 proc->pid, breakpoint_name(bp), bp->addr);
982 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
987 struct each_breakpoint_data
989 struct process *proc;
990 enum callback_status (*cb)(struct process *proc,
991 struct breakpoint *bp,
996 static enum callback_status
997 each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
999 struct each_breakpoint_data *data = d;
1000 return data->cb(data->proc, *bpp, data->cb_data);
1004 proc_each_breakpoint(struct process *proc, void *start,
1005 enum callback_status (*cb)(struct process *proc,
1006 struct breakpoint *bp,
1007 void *data), void *data)
1009 struct each_breakpoint_data dd = {
1014 return DICT_EACH(proc->breakpoints,
1015 arch_addr_t, struct breakpoint *, start,
1016 &each_breakpoint_cb, &dd);
1020 proc_find_symbol(struct process *proc, struct library_symbol *sym,
1021 struct library **retlib, struct library_symbol **retsym)
1023 struct library *lib = sym->lib;
1024 assert(lib != NULL);
1026 struct library *flib
1027 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1031 struct library_symbol *fsym
1032 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1045 struct library_symbol *
1046 proc_each_symbol(struct process *proc, struct library_symbol *start_after,
1047 enum callback_status (*cb)(struct library_symbol *, void *),
1050 struct library *lib;
1051 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1052 lib != NULL; lib = lib->next) {
1053 start_after = library_each_symbol(lib, start_after, cb, data);
1054 if (start_after != NULL)
1061 #define DEF_READER(NAME, SIZE) \
1063 NAME(struct process *proc, arch_addr_t addr, \
1064 uint##SIZE##_t *lp) \
1067 uint##SIZE##_t dst; \
1070 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1077 DEF_READER(proc_read_8, 8)
1078 DEF_READER(proc_read_16, 16)
1079 DEF_READER(proc_read_32, 32)
1080 DEF_READER(proc_read_64, 64)