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