2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Arnaud Patard, Mandriva SA
5 * Copyright (C) 1998,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2008 Luis Machado, IBM Corporation
7 * Copyright (C) 2006 Ian Wienand
8 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
39 #include "breakpoint.h"
44 #include "value_dict.h"
45 #include "prototype.h"
47 static void handle_signal(Event *event);
48 static void handle_exit(Event *event);
49 static void handle_exit_signal(Event *event);
50 static void handle_syscall(Event *event);
51 static void handle_arch_syscall(Event *event);
52 static void handle_sysret(Event *event);
53 static void handle_arch_sysret(Event *event);
54 static void handle_clone(Event *event);
55 static void handle_exec(Event *event);
56 static void handle_breakpoint(Event *event);
57 static void handle_new(Event *event);
59 static void callstack_push_syscall(struct process *proc, int sysnum);
60 static void callstack_push_symfunc(struct process *proc, struct breakpoint *bp);
61 /* XXX Stack maintenance should be moved to a dedicated module, or to
62 * proc.c, and push/pop should be visible outside this module. For
63 * now, because we need this in proc.c, this is non-static. */
64 void callstack_pop(struct process *proc);
66 static char *shortsignal(struct process *proc, int signum);
67 static char *sysname(struct process *proc, int sysnum);
68 static char *arch_sysname(struct process *proc, int sysnum);
71 call_handler(struct process *proc, Event *event)
75 struct event_handler *handler = proc->event_handler;
79 return (*handler->on_event) (handler, event);
83 handle_event(Event *event)
86 debug(1, "ltrace about to exit");
90 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
91 event->proc ? event->proc->pid : -1, event->type);
93 /* If the thread group or an individual task define an
94 overriding event handler, give them a chance to kick in.
95 We will end up calling both handlers, if the first one
96 doesn't sink the event. */
97 if (event->proc != NULL) {
98 event = call_handler(event->proc, event);
100 /* It was handled. */
103 /* Note: the previous handler has a chance to alter
105 if (event->proc != NULL
106 && event->proc->leader != NULL
107 && event->proc != event->proc->leader) {
108 event = call_handler(event->proc->leader, event);
114 switch (event->type) {
116 debug(1, "event: none");
120 assert(event->proc != NULL);
121 debug(1, "[%d] event: signal (%s [%d])",
123 shortsignal(event->proc, event->e_un.signum),
125 handle_signal(event);
129 assert(event->proc != NULL);
130 debug(1, "[%d] event: exit (%d)",
132 event->e_un.ret_val);
136 case EVENT_EXIT_SIGNAL:
137 assert(event->proc != NULL);
138 debug(1, "[%d] event: exit signal (%s [%d])",
140 shortsignal(event->proc, event->e_un.signum),
142 handle_exit_signal(event);
146 assert(event->proc != NULL);
147 debug(1, "[%d] event: syscall (%s [%d])",
149 sysname(event->proc, event->e_un.sysnum),
151 handle_syscall(event);
155 assert(event->proc != NULL);
156 debug(1, "[%d] event: sysret (%s [%d])",
158 sysname(event->proc, event->e_un.sysnum),
160 handle_sysret(event);
163 case EVENT_ARCH_SYSCALL:
164 assert(event->proc != NULL);
165 debug(1, "[%d] event: arch_syscall (%s [%d])",
167 arch_sysname(event->proc, event->e_un.sysnum),
169 handle_arch_syscall(event);
172 case EVENT_ARCH_SYSRET:
173 assert(event->proc != NULL);
174 debug(1, "[%d] event: arch_sysret (%s [%d])",
176 arch_sysname(event->proc, event->e_un.sysnum),
178 handle_arch_sysret(event);
183 assert(event->proc != NULL);
184 debug(1, "[%d] event: clone (%u)",
185 event->proc->pid, event->e_un.newpid);
190 assert(event->proc != NULL);
191 debug(1, "[%d] event: exec()",
196 case EVENT_BREAKPOINT:
197 assert(event->proc != NULL);
198 debug(1, "[%d] event: breakpoint %p",
199 event->proc->pid, event->e_un.brk_addr);
200 handle_breakpoint(event);
204 debug(1, "[%d] event: new process",
209 fprintf(stderr, "Error! unknown event?\n");
214 typedef struct Pending_New Pending_New;
219 static Pending_New * pending_news = NULL;
222 pending_new(pid_t pid) {
225 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
238 pending_new_insert(pid_t pid) {
241 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
243 p = malloc(sizeof(Pending_New));
249 p->next = pending_news;
254 pending_new_remove(pid_t pid)
256 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
259 for (pp = &pending_news; *pp != NULL; pp = &(*pp)->next)
260 if ((*pp)->pid == pid) {
261 Pending_New *p = *pp;
269 handle_clone(Event *event)
271 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
273 struct process *proc = malloc(sizeof(*proc));
274 pid_t newpid = event->e_un.newpid;
276 || process_clone(proc, event->proc, newpid) < 0) {
280 "Couldn't initialize tracing of process %d.\n",
284 proc->parent = event->proc;
285 /* We save register values to the arch pointer, and
286 * these need to be per-thread. XXX arch_ptr should
287 * be retired in favor of fetch interface anyway. */
288 proc->arch_ptr = NULL;
291 if (pending_new(newpid)) {
292 pending_new_remove(newpid);
295 proc->event_handler = NULL;
296 if (event->proc->state == STATE_ATTACHED
298 proc->state = STATE_ATTACHED;
300 proc->state = STATE_IGNORED;
303 continue_process(newpid);
305 } else if (proc != NULL) {
306 proc->state = STATE_BEING_CREATED;
309 if (event->type != EVENT_VFORK)
310 continue_process(event->proc->pid);
311 else if (proc != NULL)
312 continue_after_vfork(proc);
314 continue_process(newpid);
318 handle_new(Event *event)
320 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
322 struct process *proc = pid2proc(event->e_un.newpid);
324 pending_new_insert(event->e_un.newpid);
326 assert(proc->state == STATE_BEING_CREATED);
327 if (options.follow) {
328 proc->state = STATE_ATTACHED;
330 proc->state = STATE_IGNORED;
332 continue_process(proc->pid);
337 shortsignal(struct process *proc, int signum)
339 static char *signalent0[] = {
340 #include "signalent.h"
342 static char *signalent1[] = {
343 #include "signalent1.h"
345 static char **signalents[] = { signalent0, signalent1 };
346 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
347 sizeof signalent1 / sizeof signalent1[0]
350 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
352 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
353 if (signum < 0 || signum >= nsignals[proc->personality]) {
354 return "UNKNOWN_SIGNAL";
356 return signalents[proc->personality][signum];
361 sysname(struct process *proc, int sysnum)
363 static char result[128];
364 static char *syscallent0[] = {
365 #include "syscallent.h"
367 static char *syscallent1[] = {
368 #include "syscallent1.h"
370 static char **syscallents[] = { syscallent0, syscallent1 };
372 sizeof syscallent0 / sizeof syscallent0[0],
373 sizeof syscallent1 / sizeof syscallent1[0],
376 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
378 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
379 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
380 sprintf(result, "SYS_%d", sysnum);
383 return syscallents[proc->personality][sysnum];
388 arch_sysname(struct process *proc, int sysnum)
390 static char result[128];
391 static char *arch_syscallent[] = {
392 #include "arch_syscallent.h"
394 int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0];
396 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
398 if (sysnum < 0 || sysnum >= nsyscalls) {
399 sprintf(result, "ARCH_%d", sysnum);
402 sprintf(result, "ARCH_%s", arch_syscallent[sysnum]);
407 #ifndef HAVE_STRSIGNAL
408 # define strsignal(SIGNUM) "???"
412 handle_signal(Event *event) {
413 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
414 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
415 output_line(event->proc, "--- %s (%s) ---",
416 shortsignal(event->proc, event->e_un.signum),
417 strsignal(event->e_un.signum));
419 continue_after_signal(event->proc->pid, event->e_un.signum);
423 handle_exit(Event *event) {
424 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
425 if (event->proc->state != STATE_IGNORED) {
426 output_line(event->proc, "+++ exited (status %d) +++",
427 event->e_un.ret_val);
429 remove_process(event->proc);
433 handle_exit_signal(Event *event) {
434 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
435 if (event->proc->state != STATE_IGNORED) {
436 output_line(event->proc, "+++ killed by %s +++",
437 shortsignal(event->proc, event->e_un.signum));
439 remove_process(event->proc);
443 output_syscall(struct process *proc, const char *name, enum tof tof,
444 void (*output)(enum tof, struct process *,
445 struct library_symbol *))
447 static struct library syscall_lib;
448 if (syscall_lib.protolib == NULL) {
449 struct protolib *protolib
450 = protolib_cache_load(&g_protocache, "syscalls", 0, 1);
451 if (protolib == NULL) {
452 fprintf(stderr, "Couldn't load system call prototypes:"
453 " %s.\n", strerror(errno));
455 /* Instead, get a fake one just so we can
456 * carry on, limping. */
457 protolib = malloc(sizeof *protolib);
458 if (protolib == NULL) {
459 fprintf(stderr, "Couldn't even allocate a fake "
460 "prototype library: %s.\n",
464 protolib_init(protolib);
467 assert(protolib != NULL);
468 if (library_init(&syscall_lib, LT_LIBTYPE_SYSCALL) < 0) {
469 fprintf(stderr, "Couldn't initialize system call "
470 "library: %s.\n", strerror(errno));
474 library_set_soname(&syscall_lib, "SYS", 0);
475 syscall_lib.protolib = protolib;
478 struct library_symbol syscall;
479 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
480 syscall.lib = &syscall_lib;
481 (*output)(tof, proc, &syscall);
482 library_symbol_destroy(&syscall);
487 output_syscall_left(struct process *proc, const char *name)
489 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
493 output_syscall_right(struct process *proc, const char *name)
495 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
499 handle_syscall(Event *event) {
500 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
501 if (event->proc->state != STATE_IGNORED) {
502 callstack_push_syscall(event->proc, event->e_un.sysnum);
503 if (options.syscalls)
504 output_syscall_left(event->proc,
506 event->e_un.sysnum));
508 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
512 handle_exec(Event *event)
514 struct process *proc = event->proc;
516 /* Save the PID so that we can use it after unsuccessful
518 pid_t pid = proc->pid;
520 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
521 if (proc->state == STATE_IGNORED) {
524 remove_process(proc);
527 output_line(proc, "--- Called exec() ---");
529 if (process_exec(proc) < 0) {
531 "couldn't reinitialize process %d after exec\n", pid);
535 continue_after_exec(proc);
539 handle_arch_syscall(Event *event) {
540 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
541 if (event->proc->state != STATE_IGNORED) {
542 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
543 if (options.syscalls) {
544 output_syscall_left(event->proc,
545 arch_sysname(event->proc,
546 event->e_un.sysnum));
549 continue_process(event->proc->pid);
552 struct timeval current_time_spent;
555 calc_time_spent(struct process *proc)
560 struct callstack_element *elem;
562 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
563 elem = &proc->callstack[proc->callstack_depth - 1];
565 gettimeofday(&tv, &tz);
567 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
568 if (tv.tv_usec >= elem->time_spent.tv_usec) {
569 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
572 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
574 current_time_spent = diff;
578 handle_sysret(Event *event) {
579 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
580 if (event->proc->state != STATE_IGNORED) {
581 if (opt_T || options.summary) {
582 calc_time_spent(event->proc);
584 if (options.syscalls)
585 output_syscall_right(event->proc,
587 event->e_un.sysnum));
589 assert(event->proc->callstack_depth > 0);
590 unsigned d = event->proc->callstack_depth - 1;
591 assert(event->proc->callstack[d].is_syscall);
592 callstack_pop(event->proc);
594 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
598 handle_arch_sysret(Event *event) {
599 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
600 if (event->proc->state != STATE_IGNORED) {
601 if (opt_T || options.summary) {
602 calc_time_spent(event->proc);
604 if (options.syscalls)
605 output_syscall_right(event->proc,
606 arch_sysname(event->proc,
607 event->e_un.sysnum));
608 callstack_pop(event->proc);
610 continue_process(event->proc->pid);
614 output_right_tos(struct process *proc)
616 size_t d = proc->callstack_depth;
617 struct callstack_element *elem = &proc->callstack[d - 1];
618 if (proc->state != STATE_IGNORED)
619 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
622 #ifndef ARCH_HAVE_SYMBOL_RET
623 void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
629 handle_breakpoint(Event *event)
632 struct breakpoint *sbp;
633 struct process *leader = event->proc->leader;
634 void *brk_addr = event->e_un.brk_addr;
636 /* The leader has terminated. */
637 if (leader == NULL) {
638 continue_process(event->proc->pid);
642 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
643 event->proc->pid, brk_addr);
644 debug(2, "event: breakpoint (%p)", brk_addr);
646 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
647 if (brk_addr == event->proc->callstack[i].return_addr) {
648 for (j = event->proc->callstack_depth - 1; j > i; j--) {
649 callstack_pop(event->proc);
651 if (event->proc->state != STATE_IGNORED) {
652 if (opt_T || options.summary) {
653 calc_time_spent(event->proc);
657 struct library_symbol *libsym =
658 event->proc->callstack[i].c_un.libfunc;
660 arch_symbol_ret(event->proc, libsym);
661 output_right_tos(event->proc);
662 callstack_pop(event->proc);
664 /* Pop also any other entries that seem like
665 * they are linked to the current one: they
666 * have the same return address, but were made
667 * for different symbols. This should only
668 * happen for entry point tracing, i.e. for -x
669 * everywhere, or -x and -e on MIPS. */
670 while (event->proc->callstack_depth > 0) {
671 struct callstack_element *prev;
672 size_t d = event->proc->callstack_depth;
673 prev = &event->proc->callstack[d - 1];
675 if (prev->c_un.libfunc == libsym
676 || prev->return_addr != brk_addr)
679 arch_symbol_ret(event->proc,
681 output_right_tos(event->proc);
682 callstack_pop(event->proc);
685 /* Maybe the previous callstack_pop's got rid
686 * of the breakpoint, but if we are in a
687 * recursive call, it's still enabled. In
688 * that case we need to skip it properly. */
689 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
690 continue_after_breakpoint(event->proc, sbp);
692 set_instruction_pointer(event->proc, brk_addr);
693 continue_process(event->proc->pid);
699 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
700 breakpoint_on_hit(sbp, event->proc);
701 else if (event->proc->state != STATE_IGNORED)
702 output_line(event->proc,
703 "unexpected breakpoint at %p", brk_addr);
705 /* breakpoint_on_hit may delete its own breakpoint, so we have
706 * to look it up again. */
707 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
708 if (event->proc->state != STATE_IGNORED
709 && sbp->libsym != NULL) {
710 event->proc->stack_pointer = get_stack_pointer(event->proc);
711 callstack_push_symfunc(event->proc, sbp);
712 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
715 breakpoint_on_continue(sbp, event->proc);
718 set_instruction_pointer(event->proc, brk_addr);
721 continue_process(event->proc->pid);
725 callstack_push_syscall(struct process *proc, int sysnum)
727 struct callstack_element *elem;
729 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
730 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
731 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
732 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
737 elem = &proc->callstack[proc->callstack_depth];
738 *elem = (struct callstack_element){};
739 elem->is_syscall = 1;
740 elem->c_un.syscall = sysnum;
741 elem->return_addr = NULL;
743 proc->callstack_depth++;
744 if (opt_T || options.summary) {
746 gettimeofday(&elem->time_spent, &tz);
751 callstack_push_symfunc(struct process *proc, struct breakpoint *bp)
753 struct callstack_element *elem;
755 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)",
756 proc->pid, bp->libsym->name);
757 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
758 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
759 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
764 elem = &proc->callstack[proc->callstack_depth++];
765 *elem = (struct callstack_element){};
766 elem->is_syscall = 0;
767 elem->c_un.libfunc = bp->libsym;
769 struct breakpoint *rbp = NULL;
770 if (breakpoint_get_return_bp(&rbp, bp, proc) == 0
772 struct breakpoint *ext_rbp = insert_breakpoint(proc, rbp);
773 if (ext_rbp != rbp) {
774 breakpoint_destroy(rbp);
780 elem->return_addr = rbp != NULL ? rbp->addr : 0;
782 if (opt_T || options.summary) {
784 gettimeofday(&elem->time_spent, &tz);
789 callstack_pop(struct process *proc)
791 struct callstack_element *elem;
792 assert(proc->callstack_depth > 0);
794 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
795 elem = &proc->callstack[proc->callstack_depth - 1];
796 if (!elem->is_syscall && elem->return_addr) {
797 struct breakpoint *bp
798 = address2bpstruct(proc->leader, elem->return_addr);
800 breakpoint_on_hit(bp, proc);
801 delete_breakpoint(proc, bp);
805 if (elem->fetch_context != NULL)
806 fetch_arg_done(elem->fetch_context);
808 if (elem->arguments != NULL) {
809 val_dict_destroy(elem->arguments);
810 free(elem->arguments);
813 proc->callstack_depth--;