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
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"
42 #include "value_dict.h"
44 #ifndef ARCH_HAVE_PROCESS_DATA
46 arch_process_init(struct Process *proc)
52 arch_process_destroy(struct Process *proc)
57 arch_process_clone(struct Process *retp, struct Process *proc)
63 arch_process_exec(struct Process *proc)
69 #ifndef OS_HAVE_PROCESS_DATA
71 os_process_init(struct Process *proc)
77 os_process_destroy(struct Process *proc)
82 os_process_clone(struct Process *retp, struct Process *proc)
88 os_process_exec(struct Process *proc)
94 #ifndef ARCH_HAVE_DYNLINK_DONE
96 arch_dynlink_done(struct Process *proc)
101 static void add_process(struct Process *proc, int was_exec);
102 static void unlist_process(struct Process *proc);
105 destroy_unwind(struct Process *proc)
107 #if defined(HAVE_LIBUNWIND)
108 _UPT_destroy(proc->unwind_priv);
109 unw_destroy_addr_space(proc->unwind_as);
110 #endif /* defined(HAVE_LIBUNWIND) */
114 process_bare_init(struct Process *proc, const char *filename,
115 pid_t pid, int was_exec)
118 memset(proc, 0, sizeof(*proc));
120 proc->filename = strdup(filename);
121 if (proc->filename == NULL) {
123 free(proc->filename);
124 if (proc->breakpoints != NULL)
125 dict_clear(proc->breakpoints);
130 /* Add process so that we know who the leader is. */
132 add_process(proc, was_exec);
133 if (proc->leader == NULL)
136 if (proc->leader == proc) {
137 proc->breakpoints = dict_init(target_address_hash,
139 if (proc->breakpoints == NULL)
142 proc->breakpoints = NULL;
145 #if defined(HAVE_LIBUNWIND)
146 proc->unwind_priv = _UPT_create(pid);
147 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
148 #endif /* defined(HAVE_LIBUNWIND) */
154 process_bare_destroy(struct Process *proc, int was_exec)
156 dict_clear(proc->breakpoints);
158 free(proc->filename);
159 unlist_process(proc);
160 destroy_unwind(proc);
165 process_init_main(struct Process *proc)
167 if (breakpoints_init(proc) < 0) {
168 fprintf(stderr, "failed to init breakpoints %d\n",
177 process_init(struct Process *proc, const char *filename, pid_t pid)
179 if (process_bare_init(proc, filename, pid, 0) < 0) {
181 fprintf(stderr, "failed to initialize process %d: %s\n",
182 pid, strerror(errno));
186 if (os_process_init(proc) < 0) {
187 process_bare_destroy(proc, 0);
191 if (arch_process_init(proc) < 0) {
192 os_process_destroy(proc);
193 process_bare_destroy(proc, 0);
197 if (proc->leader != proc)
199 if (process_init_main(proc) < 0) {
200 process_bare_destroy(proc, 0);
206 static enum callback_status
207 destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
209 breakpoint_destroy(bp);
214 // XXX see comment in handle_event.c
215 void callstack_pop(struct Process *proc);
218 private_process_destroy(struct Process *proc, int was_exec)
220 /* Pop remaining stack elements. */
221 while (proc->callstack_depth > 0) {
222 /* When this is called just before a process is
223 * destroyed, the breakpoints should either have been
224 * retracted by now, or were killed by exec. In any
225 * case, it's safe to pretend that there are no
226 * breakpoints associated with the stack elements, so
227 * that stack_pop doesn't attempt to destroy them. */
228 size_t i = proc->callstack_depth - 1;
229 if (!proc->callstack[i].is_syscall)
230 proc->callstack[i].return_addr = 0;
236 free(proc->filename);
238 /* Libraries and symbols. This is only relevant in
241 for (lib = proc->libraries; lib != NULL; ) {
242 struct library *next = lib->next;
243 library_destroy(lib);
247 proc->libraries = NULL;
250 if (proc->breakpoints != NULL) {
251 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
252 dict_clear(proc->breakpoints);
253 proc->breakpoints = NULL;
256 destroy_unwind(proc);
260 process_destroy(struct Process *proc)
262 arch_process_destroy(proc);
263 os_process_destroy(proc);
264 private_process_destroy(proc, 0);
268 process_exec(struct Process *proc)
270 /* Call exec handlers first, before we destroy the main
272 if (arch_process_exec(proc) < 0
273 || os_process_exec(proc) < 0)
276 private_process_destroy(proc, 1);
278 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
280 if (process_init_main(proc) < 0) {
281 process_bare_destroy(proc, 1);
288 open_program(const char *filename, pid_t pid)
291 struct Process *proc = malloc(sizeof(*proc));
292 if (proc == NULL || process_init(proc, filename, pid) < 0) {
299 struct clone_single_bp_data {
300 struct Process *old_proc;
301 struct Process *new_proc;
306 clone_single_bp(void *key, void *value, void *u)
308 struct breakpoint *bp = value;
309 struct clone_single_bp_data *data = u;
311 /* Don't bother if there were errors anyway. */
312 if (data->error != 0)
315 struct breakpoint *clone = malloc(sizeof(*clone));
317 || breakpoint_clone(clone, data->new_proc,
318 bp, data->old_proc) < 0) {
323 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
324 breakpoint_destroy(clone);
330 process_clone(struct Process *retp, struct Process *proc, pid_t pid)
332 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
334 fprintf(stderr, "failed to clone process %d->%d : %s\n",
335 proc->pid, pid, strerror(errno));
339 retp->tracesysgood = proc->tracesysgood;
340 retp->e_machine = proc->e_machine;
341 retp->e_class = proc->e_class;
343 /* For non-leader processes, that's all we need to do. */
344 if (retp->leader != retp)
347 /* Clone symbols first so that we can clone and relink
350 struct library **nlibp = &retp->libraries;
351 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
352 *nlibp = malloc(sizeof(**nlibp));
354 || library_clone(*nlibp, lib) < 0) {
356 process_bare_destroy(retp, 0);
358 /* Error when cloning. Unroll what was done. */
359 for (lib = retp->libraries; lib != NULL; ) {
360 struct library *next = lib->next;
361 library_destroy(lib);
368 nlibp = &(*nlibp)->next;
371 /* Now clone breakpoints. Symbol relinking is done in
372 * clone_single_bp. */
373 struct clone_single_bp_data data = {
378 dict_apply_to_all(proc->leader->breakpoints, &clone_single_bp, &data);
382 /* And finally the call stack. */
383 /* XXX clearly the callstack handling should be moved to a
384 * separate module and this whole business extracted to
385 * callstack_clone, or callstack_element_clone. */
386 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
387 retp->callstack_depth = proc->callstack_depth;
390 for (i = 0; i < retp->callstack_depth; ++i) {
391 struct callstack_element *elem = &retp->callstack[i];
392 struct fetch_context *ctx = elem->fetch_context;
394 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
398 for (j = 0; j < i; ++j) {
399 nctx = elem->fetch_context;
400 fetch_arg_done(nctx);
401 elem->fetch_context = NULL;
405 elem->fetch_context = nctx;
408 struct value_dict *args = elem->arguments;
410 struct value_dict *nargs = malloc(sizeof(*nargs));
412 || val_dict_clone(nargs, args) < 0) {
414 for (j = 0; j < i; ++j) {
415 nargs = elem->arguments;
416 val_dict_destroy(nargs);
418 elem->arguments = NULL;
421 /* Pretend that this round went well,
422 * so that fail3 frees I-th
427 elem->arguments = nargs;
430 /* If it's not a syscall, we need to find the
431 * corresponding library symbol in the cloned
433 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
434 struct library_symbol *libfunc = elem->c_un.libfunc;
435 int rc = proc_find_symbol(retp, libfunc,
436 NULL, &elem->c_un.libfunc);
441 /* At this point, retp is fully initialized, except for OS and
442 * arch parts, and we can call private_process_destroy. */
443 if (os_process_clone(retp, proc) < 0) {
444 private_process_destroy(retp, 0);
447 if (arch_process_clone(retp, proc) < 0) {
448 os_process_destroy(retp);
449 private_process_destroy(retp, 0);
457 open_one_pid(pid_t pid)
461 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
463 /* Get the filename first. Should the trace_pid fail, we can
464 * easily free it, untracing is more work. */
465 if ((filename = pid2name(pid)) == NULL
466 || trace_pid(pid) < 0) {
472 proc = open_program(filename, pid);
476 trace_set_options(proc);
481 static enum callback_status
482 start_one_pid(Process * proc, void * data)
484 continue_process(proc->pid);
491 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
492 /* If we are already tracing this guy, we should be seeing all
493 * his children via normal tracing route. */
494 if (pid2proc(pid) != NULL)
497 /* First, see if we can attach the requested PID itself. */
498 if (open_one_pid(pid)) {
499 fprintf(stderr, "Cannot attach to pid %u: %s\n",
500 pid, strerror(errno));
501 trace_fail_warning(pid);
505 /* Now attach to all tasks that belong to that PID. There's a
506 * race between process_tasks and open_one_pid. So when we
507 * fail in open_one_pid below, we just do another round.
508 * Chances are that by then that PID will have gone away, and
509 * that's why we have seen the failure. The processes that we
510 * manage to open_one_pid are stopped, so we should eventually
511 * reach a point where process_tasks doesn't give any new
512 * processes (because there's nobody left to produce
514 size_t old_ntasks = 0;
521 if (process_tasks(pid, &tasks, &ntasks) < 0) {
522 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
523 pid, strerror(errno));
528 for (i = 0; i < ntasks; ++i)
529 if (pid2proc(tasks[i]) == NULL
530 && open_one_pid(tasks[i]))
535 if (have_all && old_ntasks == ntasks)
540 struct Process *leader = pid2proc(pid)->leader;
542 /* XXX Is there a way to figure out whether _start has
543 * actually already been hit? */
544 arch_dynlink_done(leader);
546 /* Done. Continue everyone. */
547 each_task(leader, NULL, start_one_pid, NULL);
550 static enum callback_status
551 find_proc(Process * proc, void * data)
553 pid_t pid = (pid_t)(uintptr_t)data;
554 return proc->pid == pid ? CBS_STOP : CBS_CONT;
558 pid2proc(pid_t pid) {
559 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
562 static Process * list_of_processes = NULL;
565 unlist_process(Process * proc)
569 if (list_of_processes == proc) {
570 list_of_processes = list_of_processes->next;
574 for (tmp = list_of_processes; ; tmp = tmp->next) {
575 /* If the following assert fails, the process wasn't
577 assert(tmp->next != NULL);
579 if (tmp->next == proc) {
580 tmp->next = tmp->next->next;
587 each_process(struct Process *start_after,
588 enum callback_status(*cb)(struct Process *proc, void *data),
591 struct Process *it = start_after == NULL ? list_of_processes
595 /* Callback might call remove_process. */
596 struct Process *next = it->next;
597 switch ((*cb)(it, data)) {
611 each_task(struct Process *proc, struct Process *start_after,
612 enum callback_status(*cb)(struct Process *proc, void *data),
615 assert(proc != NULL);
616 struct Process *it = start_after == NULL ? proc->leader
620 struct Process *leader = it->leader;
621 while (it != NULL && it->leader == leader) {
622 /* Callback might call remove_process. */
623 struct Process *next = it->next;
624 switch ((*cb)(it, data)) {
639 add_process(struct Process *proc, int was_exec)
641 Process ** leaderp = &list_of_processes;
643 pid_t tgid = process_leader(proc->pid);
645 /* Must have been terminated before we managed
646 * to fully attach. */
648 if (tgid == proc->pid)
651 Process * leader = pid2proc(tgid);
652 proc->leader = leader;
654 leaderp = &leader->next;
659 proc->next = *leaderp;
665 change_process_leader(Process * proc, Process * leader)
667 Process ** leaderp = &list_of_processes;
668 if (proc->leader == leader)
671 assert(leader != NULL);
672 unlist_process(proc);
674 leaderp = &leader->next;
676 proc->leader = leader;
677 proc->next = *leaderp;
681 static enum callback_status
682 clear_leader(struct Process *proc, void *data)
684 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
685 proc->pid, proc->leader->pid);
691 remove_process(Process *proc)
693 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
695 if (proc->leader == proc)
696 each_task(proc, NULL, &clear_leader, NULL);
698 unlist_process(proc);
699 process_removed(proc);
700 process_destroy(proc);
705 install_event_handler(Process *proc, struct event_handler *handler)
707 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
708 assert(proc->event_handler == NULL);
709 proc->event_handler = handler;
713 destroy_event_handler(Process * proc)
715 struct event_handler *handler = proc->event_handler;
716 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
717 assert(handler != NULL);
718 if (handler->destroy != NULL)
719 handler->destroy(handler);
721 proc->event_handler = NULL;
725 breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
728 assert(proc->leader == proc);
730 /* Don't enable latent or delayed symbols. */
731 if (libsym->latent || libsym->delayed) {
732 debug(DEBUG_FUNCTION,
733 "delayed and/or latent breakpoint pid=%d, %s@%p",
734 proc->pid, libsym->name, libsym->enter_addr);
738 bp_addr = sym2addr(proc, libsym);
740 /* If there is an artificial breakpoint on the same address,
741 * its libsym will be NULL, and we can smuggle our libsym
742 * there. That artificial breakpoint is there presumably for
743 * the callbacks, which we don't touch. If there is a real
744 * breakpoint, then this is a bug. ltrace-elf.c should filter
745 * symbols and ignore extra symbol aliases.
747 * The other direction is more complicated and currently not
748 * supported. If a breakpoint has custom callbacks, it might
749 * be also custom-allocated, and we would really need to swap
750 * the two: delete the one now in the dictionary, swap values
751 * around, and put the new breakpoint back in. */
752 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
755 /* MIPS backend makes duplicate requests. This is
756 * likely a bug in the backend. Currently there's no
757 * point assigning more than one symbol to a
758 * breakpoint, because when it hits, we won't know
759 * what to print out. But it's easier to fix it here
760 * before someone who understands MIPS has the time to
761 * look into it. So turn the sanity check off on
764 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
765 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
768 assert(bp->libsym == NULL);
774 bp = malloc(sizeof(*bp));
776 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
781 if (proc_add_breakpoint(proc, bp) < 0) {
782 breakpoint_destroy(bp);
786 if (breakpoint_turn_on(bp, proc) < 0) {
787 proc_remove_breakpoint(proc, bp);
788 breakpoint_destroy(bp);
795 static enum callback_status
796 cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
798 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
802 proc_activate_latent_symbol(struct Process *proc,
803 struct library_symbol *libsym)
805 assert(libsym->latent);
807 debug(DEBUG_FUNCTION, "activated latent symbol");
808 return breakpoint_for_symbol(libsym, proc);
812 proc_activate_delayed_symbol(struct Process *proc,
813 struct library_symbol *libsym)
815 assert(libsym->delayed);
817 debug(DEBUG_FUNCTION, "activated delayed symbol");
818 return breakpoint_for_symbol(libsym, proc);
821 static enum callback_status
822 activate_latent_in(struct Process *proc, struct library *lib, void *data)
824 struct library_exported_name *exported;
825 for (exported = data; exported != NULL; exported = exported->next) {
826 struct library_symbol *libsym = NULL;
827 while ((libsym = library_each_symbol(lib, libsym,
828 library_symbol_named_cb,
829 (void *)exported->name))
832 && proc_activate_latent_symbol(proc, libsym) < 0)
839 proc_add_library(struct Process *proc, struct library *lib)
841 assert(lib->next == NULL);
842 lib->next = proc->libraries;
843 proc->libraries = lib;
844 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
845 lib->soname, lib->base, lib->pathname, proc->pid);
847 /* Insert breakpoints for all active (non-latent) symbols. */
848 struct library_symbol *libsym = NULL;
849 while ((libsym = library_each_symbol(lib, libsym,
850 cb_breakpoint_for_symbol,
852 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
853 libsym->name, proc->pid, strerror(errno));
855 /* Look through export list of the new library and compare it
856 * with latent symbols of all libraries (including this
857 * library itself). */
858 struct library *lib2 = NULL;
859 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
860 lib->exported_names)) != NULL)
862 "Couldn't activate latent symbols for %s in %d: %s.",
863 libsym->name, proc->pid, strerror(errno));
867 proc_remove_library(struct Process *proc, struct library *lib)
869 struct library **libp;
870 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
879 proc_each_library(struct Process *proc, struct library *it,
880 enum callback_status (*cb)(struct Process *proc,
881 struct library *lib, void *data),
885 it = proc->libraries;
888 struct library *next = it->next;
890 switch (cb(proc, it, data)) {
906 check_leader(struct Process *proc)
908 /* Only the group leader should be getting the breakpoints and
909 * thus have ->breakpoint initialized. */
910 assert(proc->leader != NULL);
911 assert(proc->leader == proc);
912 assert(proc->breakpoints != NULL);
916 proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
918 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
919 proc->pid, breakpoint_name(bp), bp->addr);
922 /* XXX We might merge bp->libsym instead of the following
923 * assert, but that's not necessary right now. Read the
924 * comment in breakpoint_for_symbol. */
925 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
927 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
929 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
930 breakpoint_name(bp), bp->addr, strerror(errno));
938 proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
940 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
941 proc->pid, breakpoint_name(bp), bp->addr);
943 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
944 assert(removed == bp);
947 /* Dict doesn't support iteration restarts, so here's this contraption
948 * for now. XXX add restarts to dict. */
949 struct each_breakpoint_data
953 struct Process *proc;
954 enum callback_status (*cb)(struct Process *proc,
955 struct breakpoint *bp,
961 each_breakpoint_cb(void *key, void *value, void *d)
963 struct each_breakpoint_data *data = d;
964 if (data->end != NULL)
966 if (data->start == key)
969 if (data->start == NULL) {
970 switch (data->cb(data->proc, value, data->cb_data)) {
982 proc_each_breakpoint(struct Process *proc, void *start,
983 enum callback_status (*cb)(struct Process *proc,
984 struct breakpoint *bp,
985 void *data), void *data)
987 struct each_breakpoint_data dd = {
993 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
998 proc_find_symbol(struct Process *proc, struct library_symbol *sym,
999 struct library **retlib, struct library_symbol **retsym)
1001 struct library *lib = sym->lib;
1002 assert(lib != NULL);
1004 struct library *flib
1005 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1009 struct library_symbol *fsym
1010 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1023 struct library_symbol *
1024 proc_each_symbol(struct Process *proc, struct library_symbol *start_after,
1025 enum callback_status (*cb)(struct library_symbol *, void *),
1028 struct library *lib;
1029 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1030 lib != NULL; lib = lib->next) {
1031 start_after = library_each_symbol(lib, start_after, cb, data);
1032 if (start_after != NULL)