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