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>
32 #if defined(HAVE_LIBUNWIND)
33 #include <libunwind.h>
34 #include <libunwind-ptrace.h>
35 #endif /* defined(HAVE_LIBUNWIND) */
38 #include "breakpoint.h"
43 #include "value_dict.h"
45 #ifndef OS_HAVE_PROCESS_DATA
47 os_process_init(struct process *proc)
53 os_process_destroy(struct process *proc)
58 os_process_clone(struct process *retp, struct process *proc)
64 os_process_exec(struct process *proc)
70 #ifndef ARCH_HAVE_PROCESS_DATA
72 arch_process_init(struct process *proc)
78 arch_process_destroy(struct process *proc)
83 arch_process_clone(struct process *retp, struct process *proc)
89 arch_process_exec(struct process *proc)
95 #ifndef ARCH_HAVE_DYNLINK_DONE
97 arch_dynlink_done(struct process *proc)
102 static int add_process(struct process *proc, int was_exec);
103 static void unlist_process(struct process *proc);
106 destroy_unwind(struct process *proc)
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) */
117 process_bare_init(struct process *proc, const char *filename,
118 pid_t pid, int was_exec)
121 memset(proc, 0, sizeof(*proc));
123 proc->filename = strdup(filename);
124 if (proc->filename == NULL) {
126 free(proc->filename);
127 if (proc->breakpoints != NULL) {
128 dict_destroy(proc->breakpoints,
130 free(proc->breakpoints);
131 proc->breakpoints = NULL;
137 /* Add process so that we know who the leader is. */
139 if (add_process(proc, was_exec) < 0)
141 if (proc->leader == NULL) {
144 unlist_process(proc);
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);
156 proc->breakpoints = NULL;
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);
164 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
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;
173 #endif /* defined(HAVE_LIBUNWIND) */
179 process_bare_destroy(struct process *proc, int was_exec)
181 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
182 free(proc->breakpoints);
184 free(proc->filename);
185 unlist_process(proc);
186 destroy_unwind(proc);
191 process_init_main(struct process *proc)
193 if (breakpoints_init(proc) < 0) {
194 fprintf(stderr, "failed to init breakpoints %d\n",
203 process_init(struct process *proc, const char *filename, pid_t pid)
205 if (process_bare_init(proc, filename, pid, 0) < 0) {
207 fprintf(stderr, "failed to initialize process %d: %s\n",
208 pid, strerror(errno));
212 if (os_process_init(proc) < 0) {
213 process_bare_destroy(proc, 0);
217 if (arch_process_init(proc) < 0) {
218 os_process_destroy(proc);
219 process_bare_destroy(proc, 0);
223 if (proc->leader != proc)
225 if (process_init_main(proc) < 0) {
226 process_bare_destroy(proc, 0);
232 static enum callback_status
233 destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
235 breakpoint_destroy(bp);
240 // XXX see comment in handle_event.c
241 void callstack_pop(struct process *proc);
244 private_process_destroy(struct process *proc, int was_exec)
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;
262 free(proc->filename);
264 /* Libraries and symbols. This is only relevant in
267 for (lib = proc->libraries; lib != NULL; ) {
268 struct library *next = lib->next;
269 library_destroy(lib);
273 proc->libraries = NULL;
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;
283 destroy_unwind(proc);
287 process_destroy(struct process *proc)
289 arch_process_destroy(proc);
290 os_process_destroy(proc);
291 private_process_destroy(proc, 0);
295 process_exec(struct process *proc)
297 /* Call exec handlers first, before we destroy the main
299 if (arch_process_exec(proc) < 0
300 || os_process_exec(proc) < 0)
303 private_process_destroy(proc, 1);
305 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
307 if (process_init_main(proc) < 0) {
308 process_bare_destroy(proc, 1);
315 open_program(const char *filename, pid_t pid)
318 struct process *proc = malloc(sizeof(*proc));
319 if (proc == NULL || process_init(proc, filename, pid) < 0) {
326 struct clone_single_bp_data {
327 struct process *old_proc;
328 struct process *new_proc;
331 static enum callback_status
332 clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
334 struct breakpoint *bp = *bpp;
335 struct clone_single_bp_data *data = u;
337 struct breakpoint *clone = malloc(sizeof(*clone));
339 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
344 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
345 breakpoint_destroy(clone);
352 process_clone(struct process *retp, struct process *proc, pid_t pid)
354 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
356 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
357 proc->pid, pid, strerror(errno));
361 retp->tracesysgood = proc->tracesysgood;
362 retp->e_machine = proc->e_machine;
363 retp->e_class = proc->e_class;
365 /* For non-leader processes, that's all we need to do. */
366 if (retp->leader != retp)
369 /* Clone symbols first so that we can clone and relink
372 struct library **nlibp = &retp->libraries;
373 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
374 *nlibp = malloc(sizeof(**nlibp));
377 || library_clone(*nlibp, lib) < 0) {
382 process_bare_destroy(retp, 0);
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);
394 nlibp = &(*nlibp)->next;
397 /* Now clone breakpoints. Symbol relinking is done in
398 * clone_single_bp. */
399 struct clone_single_bp_data data = {
403 if (DICT_EACH(proc->leader->breakpoints,
404 arch_addr_t, struct breakpoint *, NULL,
405 clone_single_bp, &data) != NULL)
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;
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;
420 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
424 for (j = 0; j < i; ++j) {
425 nctx = retp->callstack[j].fetch_context;
426 fetch_arg_done(nctx);
427 elem->fetch_context = NULL;
431 elem->fetch_context = nctx;
434 if (elem->arguments != NULL) {
435 struct value_dict *nargs = malloc(sizeof(*nargs));
437 || val_dict_clone(nargs, elem->arguments) < 0) {
439 for (j = 0; j < i; ++j) {
440 nargs = retp->callstack[j].arguments;
441 val_dict_destroy(nargs);
443 elem->arguments = NULL;
446 /* Pretend that this round went well,
447 * so that fail3 frees I-th
452 elem->arguments = nargs;
455 /* If it's not a syscall, we need to find the
456 * corresponding library symbol in the cloned
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);
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);
472 if (arch_process_clone(retp, proc) < 0) {
473 os_process_destroy(retp);
474 private_process_destroy(retp, 0);
482 open_one_pid(pid_t pid)
484 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
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) {
495 struct process *proc = open_program(filename, pid);
499 trace_set_options(proc);
504 static enum callback_status
505 start_one_pid(struct process *proc, void *data)
507 continue_process(proc->pid);
511 static enum callback_status
512 is_main(struct process *proc, struct library *lib, void *data)
514 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
518 process_hit_start(struct process *proc)
520 struct process *leader = proc->leader;
521 assert(leader != NULL);
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);
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)
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);
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
556 size_t old_ntasks = 0;
563 if (process_tasks(pid, &tasks, &ntasks) < 0) {
564 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
565 pid, strerror(errno));
570 for (i = 0; i < ntasks; ++i)
571 if (pid2proc(tasks[i]) == NULL
572 && open_one_pid(tasks[i]) < 0)
577 if (have_all && old_ntasks == ntasks)
582 struct process *leader = pid2proc(pid)->leader;
584 /* XXX Is there a way to figure out whether _start has
585 * actually already been hit? */
586 process_hit_start(leader);
588 /* Done. Continue everyone. */
589 each_task(leader, NULL, start_one_pid, NULL);
592 static enum callback_status
593 find_proc(struct process *proc, void *data)
595 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
601 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
604 static struct process *list_of_processes = NULL;
607 unlist_process(struct process *proc)
609 if (list_of_processes == proc) {
610 list_of_processes = list_of_processes->next;
615 for (tmp = list_of_processes; ; tmp = tmp->next) {
616 /* If the following assert fails, the process wasn't
618 assert(tmp->next != NULL);
620 if (tmp->next == proc) {
621 tmp->next = tmp->next->next;
628 each_process(struct process *start_after,
629 enum callback_status(*cb)(struct process *proc, void *data),
632 struct process *it = start_after == NULL ? list_of_processes
636 /* Callback might call remove_process. */
637 struct process *next = it->next;
638 switch ((*cb)(it, data)) {
652 each_task(struct process *proc, struct process *start_after,
653 enum callback_status(*cb)(struct process *proc, void *data),
656 assert(proc != NULL);
657 struct process *it = start_after == NULL ? proc->leader
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)) {
680 add_process(struct process *proc, int was_exec)
682 struct process **leaderp = &list_of_processes;
684 pid_t tgid = process_leader(proc->pid);
686 /* Must have been terminated before we managed
687 * to fully attach. */
689 if (tgid == proc->pid) {
692 struct process *leader = pid2proc(tgid);
693 proc->leader = leader;
695 leaderp = &leader->next;
700 proc->next = *leaderp;
707 change_process_leader(struct process *proc, struct process *leader)
709 struct process **leaderp = &list_of_processes;
710 if (proc->leader == leader)
713 assert(leader != NULL);
714 unlist_process(proc);
716 leaderp = &leader->next;
718 proc->leader = leader;
719 proc->next = *leaderp;
723 static enum callback_status
724 clear_leader(struct process *proc, void *data)
726 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
727 proc->pid, proc->leader->pid);
733 remove_process(struct process *proc)
735 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
737 if (proc->leader == proc)
738 each_task(proc, NULL, &clear_leader, NULL);
740 unlist_process(proc);
741 process_removed(proc);
742 process_destroy(proc);
747 install_event_handler(struct process *proc, struct event_handler *handler)
749 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
750 assert(proc->event_handler == NULL);
751 proc->event_handler = handler;
755 destroy_event_handler(struct process *proc)
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);
763 proc->event_handler = NULL;
767 breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
770 assert(proc->leader == proc);
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);
780 bp_addr = sym2addr(proc, libsym);
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.
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
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
809 assert(bp->libsym == NULL);
815 bp = malloc(sizeof(*bp));
817 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
822 if (proc_add_breakpoint(proc, bp) < 0) {
823 breakpoint_destroy(bp);
827 if (breakpoint_turn_on(bp, proc) < 0) {
828 proc_remove_breakpoint(proc, bp);
829 breakpoint_destroy(bp);
836 static enum callback_status
837 cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
839 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
843 proc_activate_latent_symbol(struct process *proc,
844 struct library_symbol *libsym)
846 assert(libsym->latent);
848 debug(DEBUG_FUNCTION, "activated latent symbol");
849 return breakpoint_for_symbol(libsym, proc);
853 proc_activate_delayed_symbol(struct process *proc,
854 struct library_symbol *libsym)
856 assert(libsym->delayed);
858 debug(DEBUG_FUNCTION, "activated delayed symbol");
859 return breakpoint_for_symbol(libsym, proc);
862 static enum callback_status
863 activate_latent_in(struct process *proc, struct library *lib, void *data)
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))
873 && proc_activate_latent_symbol(proc, libsym) < 0)
880 proc_add_library(struct process *proc, struct library *lib)
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);
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,
894 "Couldn't insert breakpoint for %s to %d: %s.\n",
895 libsym->name, proc->pid, strerror(errno));
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)
904 "Couldn't activate latent symbols for %s in %d: %s.\n",
905 lib2->soname, proc->pid, strerror(errno));
909 proc_remove_library(struct process *proc, struct library *lib)
911 struct library **libp;
912 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
921 proc_each_library(struct process *proc, struct library *it,
922 enum callback_status (*cb)(struct process *proc,
923 struct library *lib, void *data),
927 it = proc->libraries;
932 struct library *next = it->next;
934 switch (cb(proc, it, data)) {
950 check_leader(struct process *proc)
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);
960 proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
962 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
963 proc->pid, breakpoint_name(bp), bp->addr);
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);
971 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
973 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
974 breakpoint_name(bp), bp->addr, strerror(errno));
982 proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
984 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
985 proc->pid, breakpoint_name(bp), bp->addr);
987 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
992 struct each_breakpoint_data
994 struct process *proc;
995 enum callback_status (*cb)(struct process *proc,
996 struct breakpoint *bp,
1001 static enum callback_status
1002 each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
1004 struct each_breakpoint_data *data = d;
1005 return data->cb(data->proc, *bpp, data->cb_data);
1009 proc_each_breakpoint(struct process *proc, void *start,
1010 enum callback_status (*cb)(struct process *proc,
1011 struct breakpoint *bp,
1012 void *data), void *data)
1014 struct each_breakpoint_data dd = {
1019 return DICT_EACH(proc->breakpoints,
1020 arch_addr_t, struct breakpoint *, start,
1021 &each_breakpoint_cb, &dd);
1025 proc_find_symbol(struct process *proc, struct library_symbol *sym,
1026 struct library **retlib, struct library_symbol **retsym)
1028 struct library *lib = sym->lib;
1029 assert(lib != NULL);
1031 struct library *flib
1032 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1036 struct library_symbol *fsym
1037 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1050 struct library_symbol *
1051 proc_each_symbol(struct process *proc, struct library_symbol *start_after,
1052 enum callback_status (*cb)(struct library_symbol *, void *),
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)
1066 #define DEF_READER(NAME, SIZE) \
1068 NAME(struct process *proc, arch_addr_t addr, \
1069 uint##SIZE##_t *lp) \
1072 uint##SIZE##_t dst; \
1075 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1082 DEF_READER(proc_read_8, 8)
1083 DEF_READER(proc_read_16, 16)
1084 DEF_READER(proc_read_32, 32)
1085 DEF_READER(proc_read_64, 64)