| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| Petr Machata | 057caa5 | 2013-01-30 23:28:47 +0100 | [diff] [blame] | 3 | * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc. |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 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 |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 |
| 23 | * 02110-1301 USA |
| 24 | */ |
| 25 | |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 26 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 27 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 28 | #define _GNU_SOURCE |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 29 | #include <assert.h> |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/time.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 36 | |
| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 37 | #include "backend.h" |
| Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 38 | #include "breakpoint.h" |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 39 | #include "common.h" |
| 40 | #include "fetch.h" |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 41 | #include "library.h" |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 42 | #include "proc.h" |
| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 43 | #include "value_dict.h" |
| Petr Machata | fed1e8d | 2012-02-07 02:06:29 +0100 | [diff] [blame] | 44 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 45 | static void handle_signal(Event *event); |
| 46 | static void handle_exit(Event *event); |
| 47 | static void handle_exit_signal(Event *event); |
| 48 | static void handle_syscall(Event *event); |
| 49 | static void handle_arch_syscall(Event *event); |
| 50 | static void handle_sysret(Event *event); |
| 51 | static void handle_arch_sysret(Event *event); |
| 52 | static void handle_clone(Event *event); |
| 53 | static void handle_exec(Event *event); |
| 54 | static void handle_breakpoint(Event *event); |
| 55 | static void handle_new(Event *event); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 56 | |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 57 | static void callstack_push_syscall(struct process *proc, int sysnum); |
| 58 | static void callstack_push_symfunc(struct process *proc, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 59 | struct library_symbol *sym); |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 60 | /* XXX Stack maintenance should be moved to a dedicated module, or to |
| 61 | * proc.c, and push/pop should be visible outside this module. For |
| 62 | * now, because we need this in proc.c, this is non-static. */ |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 63 | void callstack_pop(struct process *proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 64 | |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 65 | static char *shortsignal(struct process *proc, int signum); |
| 66 | static char *sysname(struct process *proc, int sysnum); |
| 67 | static char *arch_sysname(struct process *proc, int sysnum); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 68 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 69 | static Event * |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 70 | call_handler(struct process *proc, Event *event) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 71 | { |
| 72 | assert(proc != NULL); |
| 73 | |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 74 | struct event_handler *handler = proc->event_handler; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 75 | if (handler == NULL) |
| 76 | return event; |
| 77 | |
| 78 | return (*handler->on_event) (handler, event); |
| 79 | } |
| 80 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 81 | void |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 82 | handle_event(Event *event) |
| 83 | { |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 84 | if (exiting == 1) { |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 85 | debug(1, "ltrace about to exit"); |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 86 | os_ltrace_exiting(); |
| 87 | exiting = 2; |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 88 | } |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 89 | debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)", |
| 90 | event->proc ? event->proc->pid : -1, event->type); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 91 | |
| 92 | /* If the thread group or an individual task define an |
| 93 | overriding event handler, give them a chance to kick in. |
| 94 | We will end up calling both handlers, if the first one |
| 95 | doesn't sink the event. */ |
| 96 | if (event->proc != NULL) { |
| 97 | event = call_handler(event->proc, event); |
| 98 | if (event == NULL) |
| 99 | /* It was handled. */ |
| 100 | return; |
| 101 | |
| 102 | /* Note: the previous handler has a chance to alter |
| 103 | * the event. */ |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 104 | if (event->proc != NULL |
| 105 | && event->proc->leader != NULL |
| 106 | && event->proc != event->proc->leader) { |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 107 | event = call_handler(event->proc->leader, event); |
| Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 108 | if (event == NULL) |
| Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | } |
| 112 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 113 | switch (event->type) { |
| 114 | case EVENT_NONE: |
| 115 | debug(1, "event: none"); |
| 116 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 117 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 118 | case EVENT_SIGNAL: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 119 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 120 | debug(1, "[%d] event: signal (%s [%d])", |
| 121 | event->proc->pid, |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 122 | shortsignal(event->proc, event->e_un.signum), |
| 123 | event->e_un.signum); |
| 124 | handle_signal(event); |
| 125 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 126 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 127 | case EVENT_EXIT: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 128 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 129 | debug(1, "[%d] event: exit (%d)", |
| 130 | event->proc->pid, |
| 131 | event->e_un.ret_val); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 132 | handle_exit(event); |
| 133 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 134 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 135 | case EVENT_EXIT_SIGNAL: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 136 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 137 | debug(1, "[%d] event: exit signal (%s [%d])", |
| 138 | event->proc->pid, |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 139 | shortsignal(event->proc, event->e_un.signum), |
| 140 | event->e_un.signum); |
| 141 | handle_exit_signal(event); |
| 142 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 143 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 144 | case EVENT_SYSCALL: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 145 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 146 | debug(1, "[%d] event: syscall (%s [%d])", |
| 147 | event->proc->pid, |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 148 | sysname(event->proc, event->e_un.sysnum), |
| 149 | event->e_un.sysnum); |
| 150 | handle_syscall(event); |
| 151 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 152 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 153 | case EVENT_SYSRET: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 154 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 155 | debug(1, "[%d] event: sysret (%s [%d])", |
| 156 | event->proc->pid, |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 157 | sysname(event->proc, event->e_un.sysnum), |
| 158 | event->e_un.sysnum); |
| 159 | handle_sysret(event); |
| 160 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 161 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 162 | case EVENT_ARCH_SYSCALL: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 163 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 164 | debug(1, "[%d] event: arch_syscall (%s [%d])", |
| 165 | event->proc->pid, |
| 166 | arch_sysname(event->proc, event->e_un.sysnum), |
| 167 | event->e_un.sysnum); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 168 | handle_arch_syscall(event); |
| 169 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 170 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 171 | case EVENT_ARCH_SYSRET: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 172 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 173 | debug(1, "[%d] event: arch_sysret (%s [%d])", |
| 174 | event->proc->pid, |
| 175 | arch_sysname(event->proc, event->e_un.sysnum), |
| 176 | event->e_un.sysnum); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 177 | handle_arch_sysret(event); |
| 178 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 179 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 180 | case EVENT_CLONE: |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 181 | case EVENT_VFORK: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 182 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 183 | debug(1, "[%d] event: clone (%u)", |
| 184 | event->proc->pid, event->e_un.newpid); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 185 | handle_clone(event); |
| 186 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 187 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 188 | case EVENT_EXEC: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 189 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 190 | debug(1, "[%d] event: exec()", |
| 191 | event->proc->pid); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 192 | handle_exec(event); |
| 193 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 194 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 195 | case EVENT_BREAKPOINT: |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 196 | assert(event->proc != NULL); |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 197 | debug(1, "[%d] event: breakpoint %p", |
| 198 | event->proc->pid, event->e_un.brk_addr); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 199 | handle_breakpoint(event); |
| 200 | return; |
| Petr Machata | 9978de4 | 2013-01-08 23:46:22 +0100 | [diff] [blame] | 201 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 202 | case EVENT_NEW: |
| Petr Machata | ebfe7d6 | 2012-04-13 21:34:49 +0200 | [diff] [blame] | 203 | debug(1, "[%d] event: new process", |
| 204 | event->e_un.newpid); |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 205 | handle_new(event); |
| 206 | return; |
| 207 | default: |
| 208 | fprintf(stderr, "Error! unknown event?\n"); |
| 209 | exit(1); |
| 210 | } |
| 211 | } |
| 212 | |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 213 | typedef struct Pending_New Pending_New; |
| 214 | struct Pending_New { |
| 215 | pid_t pid; |
| 216 | Pending_New * next; |
| 217 | }; |
| 218 | static Pending_New * pending_news = NULL; |
| 219 | |
| 220 | static int |
| 221 | pending_new(pid_t pid) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 222 | Pending_New * p; |
| 223 | |
| 224 | debug(DEBUG_FUNCTION, "pending_new(%d)", pid); |
| 225 | |
| 226 | p = pending_news; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 227 | while (p) { |
| 228 | if (p->pid == pid) { |
| 229 | return 1; |
| 230 | } |
| 231 | p = p->next; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | pending_new_insert(pid_t pid) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 238 | Pending_New * p; |
| 239 | |
| 240 | debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid); |
| 241 | |
| 242 | p = malloc(sizeof(Pending_New)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 243 | if (!p) { |
| 244 | perror("malloc()"); |
| 245 | exit(1); |
| 246 | } |
| 247 | p->pid = pid; |
| 248 | p->next = pending_news; |
| 249 | pending_news = p; |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | pending_new_remove(pid_t pid) { |
| 254 | Pending_New *p, *pred; |
| 255 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 256 | debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid); |
| 257 | |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 258 | p = pending_news; |
| Sedat Dilek | 7d804e9 | 2012-09-01 02:30:52 +0200 | [diff] [blame] | 259 | pred = NULL; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 260 | if (p->pid == pid) { |
| 261 | pending_news = p->next; |
| 262 | free(p); |
| 263 | } else { |
| 264 | while (p) { |
| 265 | if (p->pid == pid) { |
| 266 | pred->next = p->next; |
| 267 | free(p); |
| 268 | } |
| 269 | pred = p; |
| 270 | p = p->next; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 276 | handle_clone(Event *event) |
| 277 | { |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 278 | debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 279 | |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 280 | struct process *proc = malloc(sizeof(*proc)); |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 281 | pid_t newpid = event->e_un.newpid; |
| 282 | if (proc == NULL |
| 283 | || process_clone(proc, event->proc, newpid) < 0) { |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 284 | free(proc); |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 285 | proc = NULL; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 286 | fprintf(stderr, |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 287 | "Couldn't initialize tracing of process %d.\n", |
| 288 | newpid); |
| 289 | |
| 290 | } else { |
| 291 | proc->parent = event->proc; |
| 292 | /* We save register values to the arch pointer, and |
| 293 | * these need to be per-thread. XXX arch_ptr should |
| 294 | * be retired in favor of fetch interface anyway. */ |
| 295 | proc->arch_ptr = NULL; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 296 | } |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 297 | |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 298 | if (pending_new(newpid)) { |
| 299 | pending_new_remove(newpid); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 300 | |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 301 | if (proc != NULL) { |
| 302 | proc->event_handler = NULL; |
| 303 | if (event->proc->state == STATE_ATTACHED |
| 304 | && options.follow) |
| 305 | proc->state = STATE_ATTACHED; |
| 306 | else |
| 307 | proc->state = STATE_IGNORED; |
| 308 | } |
| Petr Machata | 75dcf7d | 2011-10-06 14:30:19 +0200 | [diff] [blame] | 309 | |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 310 | continue_process(newpid); |
| 311 | |
| 312 | } else if (proc != NULL) { |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 313 | proc->state = STATE_BEING_CREATED; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 314 | } |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 315 | |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 316 | if (event->type != EVENT_VFORK) |
| 317 | continue_process(event->proc->pid); |
| 318 | else if (proc != NULL) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 319 | continue_after_vfork(proc); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 320 | else |
| Petr Machata | 6095c13 | 2013-01-07 19:08:18 +0100 | [diff] [blame] | 321 | continue_process(newpid); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 325 | handle_new(Event *event) |
| 326 | { |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 327 | debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 328 | |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 329 | struct process *proc = pid2proc(event->e_un.newpid); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 330 | if (!proc) { |
| 331 | pending_new_insert(event->e_un.newpid); |
| 332 | } else { |
| 333 | assert(proc->state == STATE_BEING_CREATED); |
| Juan Cespedes | 30439b4 | 2009-05-22 19:03:09 +0200 | [diff] [blame] | 334 | if (options.follow) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 335 | proc->state = STATE_ATTACHED; |
| 336 | } else { |
| 337 | proc->state = STATE_IGNORED; |
| 338 | } |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 339 | continue_process(proc->pid); |
| 340 | } |
| 341 | } |
| 342 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 343 | static char * |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 344 | shortsignal(struct process *proc, int signum) |
| 345 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 346 | static char *signalent0[] = { |
| 347 | #include "signalent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 348 | }; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 349 | static char *signalent1[] = { |
| 350 | #include "signalent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 351 | }; |
| 352 | static char **signalents[] = { signalent0, signalent1 }; |
| 353 | int nsignals[] = { sizeof signalent0 / sizeof signalent0[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 354 | sizeof signalent1 / sizeof signalent1[0] |
| 355 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 356 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 357 | debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum); |
| 358 | |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 359 | assert(proc->personality < sizeof signalents / sizeof signalents[0]); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 360 | if (signum < 0 || signum >= nsignals[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 361 | return "UNKNOWN_SIGNAL"; |
| 362 | } else { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 363 | return signalents[proc->personality][signum]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 367 | static char * |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 368 | sysname(struct process *proc, int sysnum) |
| 369 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 370 | static char result[128]; |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 371 | static char *syscallent0[] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 372 | #include "syscallent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 373 | }; |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 374 | static char *syscallent1[] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 375 | #include "syscallent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 376 | }; |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 377 | static char **syscallents[] = { syscallent0, syscallent1 }; |
| 378 | int nsyscalls[] = { |
| 379 | sizeof syscallent0 / sizeof syscallent0[0], |
| 380 | sizeof syscallent1 / sizeof syscallent1[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 381 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 382 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 383 | debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum); |
| 384 | |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 385 | assert(proc->personality < sizeof syscallents / sizeof syscallents[0]); |
| 386 | if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 387 | sprintf(result, "SYS_%d", sysnum); |
| 388 | return result; |
| 389 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 390 | sprintf(result, "SYS_%s", |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 391 | syscallents[proc->personality][sysnum]); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | } |
| 395 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 396 | static char * |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 397 | arch_sysname(struct process *proc, int sysnum) |
| 398 | { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 399 | static char result[128]; |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 400 | static char *arch_syscallent[] = { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 401 | #include "arch_syscallent.h" |
| 402 | }; |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 403 | int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0]; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 404 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 405 | debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum); |
| 406 | |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 407 | if (sysnum < 0 || sysnum >= nsyscalls) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 408 | sprintf(result, "ARCH_%d", sysnum); |
| 409 | return result; |
| 410 | } else { |
| Petr Machata | 8e682c2 | 2013-01-09 16:20:20 +0100 | [diff] [blame] | 411 | sprintf(result, "ARCH_%s", arch_syscallent[sysnum]); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 412 | return result; |
| 413 | } |
| 414 | } |
| 415 | |
| Petr Machata | 7c4d311 | 2012-12-09 11:55:03 +0100 | [diff] [blame] | 416 | #ifndef HAVE_STRSIGNAL |
| 417 | # define strsignal(SIGNUM) "???" |
| 418 | #endif |
| 419 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 420 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 421 | handle_signal(Event *event) { |
| 422 | debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum); |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 423 | if (event->proc->state != STATE_IGNORED && !options.no_signals) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 424 | output_line(event->proc, "--- %s (%s) ---", |
| 425 | shortsignal(event->proc, event->e_un.signum), |
| 426 | strsignal(event->e_un.signum)); |
| 427 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 428 | continue_after_signal(event->proc->pid, event->e_un.signum); |
| 429 | } |
| 430 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 431 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 432 | handle_exit(Event *event) { |
| 433 | debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 434 | if (event->proc->state != STATE_IGNORED) { |
| 435 | output_line(event->proc, "+++ exited (status %d) +++", |
| 436 | event->e_un.ret_val); |
| 437 | } |
| Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 438 | remove_process(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 441 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 442 | handle_exit_signal(Event *event) { |
| 443 | debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 444 | if (event->proc->state != STATE_IGNORED) { |
| 445 | output_line(event->proc, "+++ killed by %s +++", |
| 446 | shortsignal(event->proc, event->e_un.signum)); |
| 447 | } |
| Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 448 | remove_process(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| Petr Machata | a32ef7c | 2012-04-23 18:05:24 +0200 | [diff] [blame] | 451 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 452 | output_syscall(struct process *proc, const char *name, enum tof tof, |
| 453 | void (*output)(enum tof, struct process *, |
| Petr Machata | a32ef7c | 2012-04-23 18:05:24 +0200 | [diff] [blame] | 454 | struct library_symbol *)) |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 455 | { |
| Petr Machata | a32ef7c | 2012-04-23 18:05:24 +0200 | [diff] [blame] | 456 | struct library_symbol syscall; |
| 457 | if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) { |
| Petr Machata | 1e0b9dc | 2012-04-28 15:58:43 +0200 | [diff] [blame] | 458 | (*output)(tof, proc, &syscall); |
| Petr Machata | a32ef7c | 2012-04-23 18:05:24 +0200 | [diff] [blame] | 459 | library_symbol_destroy(&syscall); |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 460 | } |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 464 | output_syscall_left(struct process *proc, const char *name) |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 465 | { |
| Petr Machata | 1e0b9dc | 2012-04-28 15:58:43 +0200 | [diff] [blame] | 466 | output_syscall(proc, name, LT_TOF_SYSCALL, &output_left); |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 470 | output_syscall_right(struct process *proc, const char *name) |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 471 | { |
| Petr Machata | 1e0b9dc | 2012-04-28 15:58:43 +0200 | [diff] [blame] | 472 | output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right); |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 475 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 476 | handle_syscall(Event *event) { |
| 477 | debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 478 | if (event->proc->state != STATE_IGNORED) { |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 479 | callstack_push_syscall(event->proc, event->e_un.sysnum); |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 480 | if (options.syscalls) |
| 481 | output_syscall_left(event->proc, |
| 482 | sysname(event->proc, |
| 483 | event->e_un.sysnum)); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 484 | } |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 485 | continue_after_syscall(event->proc, event->e_un.sysnum, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 488 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 489 | handle_exec(Event *event) |
| 490 | { |
| 491 | struct process *proc = event->proc; |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 492 | |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 493 | /* Save the PID so that we can use it after unsuccessful |
| 494 | * process_exec. */ |
| 495 | pid_t pid = proc->pid; |
| 496 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 497 | debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid); |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 498 | if (proc->state == STATE_IGNORED) { |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 499 | untrace: |
| 500 | untrace_pid(pid); |
| Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 501 | remove_process(proc); |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 502 | return; |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 503 | } |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 504 | output_line(proc, "--- Called exec() ---"); |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 505 | |
| 506 | if (process_exec(proc) < 0) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 507 | fprintf(stderr, |
| 508 | "couldn't reinitialize process %d after exec\n", pid); |
| Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 509 | goto untrace; |
| 510 | } |
| 511 | |
| Petr Machata | 057caa5 | 2013-01-30 23:28:47 +0100 | [diff] [blame] | 512 | continue_after_exec(proc); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 516 | handle_arch_syscall(Event *event) { |
| 517 | debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 518 | if (event->proc->state != STATE_IGNORED) { |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 519 | callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 520 | if (options.syscalls) { |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 521 | output_syscall_left(event->proc, |
| 522 | arch_sysname(event->proc, |
| 523 | event->e_un.sysnum)); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 524 | } |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 525 | } |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 526 | continue_process(event->proc->pid); |
| 527 | } |
| 528 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 529 | struct timeval current_time_spent; |
| 530 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 531 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 532 | calc_time_spent(struct process *proc) |
| 533 | { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 534 | struct timeval tv; |
| 535 | struct timezone tz; |
| 536 | struct timeval diff; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 537 | struct callstack_element *elem; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 538 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 539 | debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 540 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 541 | |
| 542 | gettimeofday(&tv, &tz); |
| 543 | |
| 544 | diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec; |
| 545 | if (tv.tv_usec >= elem->time_spent.tv_usec) { |
| 546 | diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec; |
| 547 | } else { |
| Paul Buerger | 6aa0152 | 2012-09-12 10:58:52 -0400 | [diff] [blame] | 548 | diff.tv_sec--; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 549 | diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec; |
| 550 | } |
| 551 | current_time_spent = diff; |
| 552 | } |
| 553 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 554 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 555 | handle_sysret(Event *event) { |
| 556 | debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 557 | if (event->proc->state != STATE_IGNORED) { |
| 558 | if (opt_T || options.summary) { |
| 559 | calc_time_spent(event->proc); |
| 560 | } |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 561 | if (options.syscalls) |
| 562 | output_syscall_right(event->proc, |
| 563 | sysname(event->proc, |
| 564 | event->e_un.sysnum)); |
| 565 | |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 566 | assert(event->proc->callstack_depth > 0); |
| 567 | unsigned d = event->proc->callstack_depth - 1; |
| 568 | assert(event->proc->callstack[d].is_syscall); |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 569 | callstack_pop(event->proc); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 570 | } |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 571 | continue_after_syscall(event->proc, event->e_un.sysnum, 1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 574 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 575 | handle_arch_sysret(Event *event) { |
| 576 | debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 577 | if (event->proc->state != STATE_IGNORED) { |
| 578 | if (opt_T || options.summary) { |
| 579 | calc_time_spent(event->proc); |
| 580 | } |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 581 | if (options.syscalls) |
| 582 | output_syscall_right(event->proc, |
| 583 | arch_sysname(event->proc, |
| 584 | event->e_un.sysnum)); |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 585 | callstack_pop(event->proc); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 586 | } |
| 587 | continue_process(event->proc->pid); |
| 588 | } |
| 589 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 590 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 591 | output_right_tos(struct process *proc) |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 592 | { |
| 593 | size_t d = proc->callstack_depth; |
| 594 | struct callstack_element *elem = &proc->callstack[d - 1]; |
| 595 | if (proc->state != STATE_IGNORED) |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 596 | output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc); |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| Edgar E. Iglesias | 3a1806d | 2012-09-27 17:02:40 +0200 | [diff] [blame] | 599 | #ifndef ARCH_HAVE_SYMBOL_RET |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 600 | void arch_symbol_ret(struct process *proc, struct library_symbol *libsym) |
| Edgar E. Iglesias | 3a1806d | 2012-09-27 17:02:40 +0200 | [diff] [blame] | 601 | { |
| 602 | } |
| 603 | #endif |
| 604 | |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 605 | static void |
| Petr Machata | fed1e8d | 2012-02-07 02:06:29 +0100 | [diff] [blame] | 606 | handle_breakpoint(Event *event) |
| 607 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 608 | int i, j; |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 609 | struct breakpoint *sbp; |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 610 | struct process *leader = event->proc->leader; |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 611 | void *brk_addr = event->e_un.brk_addr; |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 612 | |
| 613 | /* The leader has terminated. */ |
| 614 | if (leader == NULL) { |
| 615 | continue_process(event->proc->pid); |
| 616 | return; |
| 617 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 618 | |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 619 | debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", |
| 620 | event->proc->pid, brk_addr); |
| 621 | debug(2, "event: breakpoint (%p)", brk_addr); |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 622 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 623 | for (i = event->proc->callstack_depth - 1; i >= 0; i--) { |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 624 | if (brk_addr == event->proc->callstack[i].return_addr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 625 | for (j = event->proc->callstack_depth - 1; j > i; j--) { |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 626 | callstack_pop(event->proc); |
| 627 | } |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 628 | if (event->proc->state != STATE_IGNORED) { |
| 629 | if (opt_T || options.summary) { |
| 630 | calc_time_spent(event->proc); |
| 631 | } |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 632 | } |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 633 | event->proc->return_addr = brk_addr; |
| 634 | |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 635 | struct library_symbol *libsym = |
| 636 | event->proc->callstack[i].c_un.libfunc; |
| 637 | |
| Edgar E. Iglesias | 3a1806d | 2012-09-27 17:02:40 +0200 | [diff] [blame] | 638 | arch_symbol_ret(event->proc, libsym); |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 639 | output_right_tos(event->proc); |
| 640 | callstack_pop(event->proc); |
| 641 | |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 642 | /* Pop also any other entries that seem like |
| 643 | * they are linked to the current one: they |
| 644 | * have the same return address, but were made |
| 645 | * for different symbols. This should only |
| 646 | * happen for entry point tracing, i.e. for -x |
| Edgar E. Iglesias | 3a1806d | 2012-09-27 17:02:40 +0200 | [diff] [blame] | 647 | * everywhere, or -x and -e on MIPS. */ |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 648 | while (event->proc->callstack_depth > 0) { |
| 649 | struct callstack_element *prev; |
| 650 | size_t d = event->proc->callstack_depth; |
| 651 | prev = &event->proc->callstack[d - 1]; |
| 652 | |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 653 | if (prev->c_un.libfunc == libsym |
| 654 | || prev->return_addr != brk_addr) |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 655 | break; |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 656 | |
| Edgar E. Iglesias | 3a1806d | 2012-09-27 17:02:40 +0200 | [diff] [blame] | 657 | arch_symbol_ret(event->proc, |
| 658 | prev->c_un.libfunc); |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 659 | output_right_tos(event->proc); |
| 660 | callstack_pop(event->proc); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 661 | } |
| Petr Machata | 31b2f9f | 2012-04-12 22:23:40 +0200 | [diff] [blame] | 662 | |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 663 | /* Maybe the previous callstack_pop's got rid |
| 664 | * of the breakpoint, but if we are in a |
| 665 | * recursive call, it's still enabled. In |
| 666 | * that case we need to skip it properly. */ |
| Petr Machata | c0649d8 | 2012-04-20 02:39:33 +0200 | [diff] [blame] | 667 | if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) { |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 668 | continue_after_breakpoint(event->proc, sbp); |
| Petr Machata | c0649d8 | 2012-04-20 02:39:33 +0200 | [diff] [blame] | 669 | } else { |
| 670 | set_instruction_pointer(event->proc, brk_addr); |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 671 | continue_process(event->proc->pid); |
| Petr Machata | c0649d8 | 2012-04-20 02:39:33 +0200 | [diff] [blame] | 672 | } |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 673 | return; |
| 674 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 677 | if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) |
| Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 678 | breakpoint_on_hit(sbp, event->proc); |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 679 | else if (event->proc->state != STATE_IGNORED) |
| 680 | output_line(event->proc, |
| 681 | "unexpected breakpoint at %p", brk_addr); |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 682 | |
| Petr Machata | 5ee3682 | 2012-04-19 17:01:51 +0200 | [diff] [blame] | 683 | /* breakpoint_on_hit may delete its own breakpoint, so we have |
| 684 | * to look it up again. */ |
| 685 | if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) { |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 686 | if (event->proc->state != STATE_IGNORED |
| 687 | && sbp->libsym != NULL) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 688 | event->proc->stack_pointer = get_stack_pointer(event->proc); |
| 689 | event->proc->return_addr = |
| 690 | get_return_addr(event->proc, event->proc->stack_pointer); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 691 | callstack_push_symfunc(event->proc, sbp->libsym); |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 692 | output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 693 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 694 | |
| Petr Machata | 56a9ea6 | 2012-03-27 03:09:29 +0200 | [diff] [blame] | 695 | breakpoint_on_continue(sbp, event->proc); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 696 | return; |
| Petr Machata | c0649d8 | 2012-04-20 02:39:33 +0200 | [diff] [blame] | 697 | } else { |
| 698 | set_instruction_pointer(event->proc, brk_addr); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 699 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 700 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 701 | continue_process(event->proc->pid); |
| 702 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 703 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 704 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 705 | callstack_push_syscall(struct process *proc, int sysnum) |
| 706 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 707 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 708 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 709 | debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 710 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 711 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Arnaud Patard | 91a1f32 | 2010-01-08 08:40:13 -0500 | [diff] [blame] | 712 | fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__); |
| 713 | abort(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 717 | elem = &proc->callstack[proc->callstack_depth]; |
| Petr Machata | 8ae3673 | 2012-04-30 01:19:09 +0200 | [diff] [blame] | 718 | *elem = (struct callstack_element){}; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 719 | elem->is_syscall = 1; |
| 720 | elem->c_un.syscall = sysnum; |
| 721 | elem->return_addr = NULL; |
| 722 | |
| 723 | proc->callstack_depth++; |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 724 | if (opt_T || options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 725 | struct timezone tz; |
| 726 | gettimeofday(&elem->time_spent, &tz); |
| 727 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 730 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 731 | callstack_push_symfunc(struct process *proc, struct library_symbol *sym) |
| 732 | { |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 733 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 734 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 735 | debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 736 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 737 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Arnaud Patard | 91a1f32 | 2010-01-08 08:40:13 -0500 | [diff] [blame] | 738 | fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__); |
| 739 | abort(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
| Petr Machata | 1429874 | 2012-04-12 23:09:21 +0200 | [diff] [blame] | 743 | elem = &proc->callstack[proc->callstack_depth++]; |
| Petr Machata | 8ae3673 | 2012-04-30 01:19:09 +0200 | [diff] [blame] | 744 | *elem = (struct callstack_element){}; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 745 | elem->is_syscall = 0; |
| 746 | elem->c_un.libfunc = sym; |
| 747 | |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 748 | elem->return_addr = proc->return_addr; |
| Petr Machata | 9df1501 | 2012-02-20 12:49:46 +0100 | [diff] [blame] | 749 | if (elem->return_addr) |
| 750 | insert_breakpoint(proc, elem->return_addr, NULL); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 751 | |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 752 | if (opt_T || options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 753 | struct timezone tz; |
| 754 | gettimeofday(&elem->time_spent, &tz); |
| 755 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 758 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 759 | callstack_pop(struct process *proc) |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 760 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 761 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 762 | assert(proc->callstack_depth > 0); |
| 763 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 764 | debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 765 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 766 | if (!elem->is_syscall && elem->return_addr) |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 767 | delete_breakpoint(proc, elem->return_addr); |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 768 | |
| 769 | if (elem->fetch_context != NULL) |
| 770 | fetch_arg_done(elem->fetch_context); |
| 771 | |
| 772 | if (elem->arguments != NULL) { |
| 773 | val_dict_destroy(elem->arguments); |
| 774 | free(elem->arguments); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 775 | } |
| Petr Machata | e655ccf | 2012-10-26 22:21:59 +0200 | [diff] [blame] | 776 | |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 777 | proc->callstack_depth--; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 778 | } |