| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 3 | #define _GNU_SOURCE |
| 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> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 10 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 11 | #include "common.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 12 | |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 13 | #ifdef __powerpc__ |
| 14 | #include <sys/ptrace.h> |
| 15 | #endif |
| 16 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 17 | static void handle_signal(Event *event); |
| 18 | static void handle_exit(Event *event); |
| 19 | static void handle_exit_signal(Event *event); |
| 20 | static void handle_syscall(Event *event); |
| 21 | static void handle_arch_syscall(Event *event); |
| 22 | static void handle_sysret(Event *event); |
| 23 | static void handle_arch_sysret(Event *event); |
| 24 | static void handle_clone(Event *event); |
| 25 | static void handle_exec(Event *event); |
| 26 | static void handle_breakpoint(Event *event); |
| 27 | static void handle_new(Event *event); |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 28 | static void remove_proc(Process *proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 29 | |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 30 | static void callstack_push_syscall(Process *proc, int sysnum); |
| 31 | static void callstack_push_symfunc(Process *proc, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 32 | struct library_symbol *sym); |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 33 | static void callstack_pop(Process *proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 34 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 35 | static char * shortsignal(Process *proc, int signum); |
| 36 | static char * sysname(Process *proc, int sysnum); |
| 37 | static char * arch_sysname(Process *proc, int sysnum); |
| 38 | |
| 39 | void |
| 40 | handle_event(Event *event) { |
| 41 | debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)", event->proc ? event->proc->pid : -1, event->type); |
| 42 | switch (event->type) { |
| 43 | case EVENT_NONE: |
| 44 | debug(1, "event: none"); |
| 45 | return; |
| 46 | case EVENT_SIGNAL: |
| 47 | debug(1, "event: signal (%s [%d])", |
| 48 | shortsignal(event->proc, event->e_un.signum), |
| 49 | event->e_un.signum); |
| 50 | handle_signal(event); |
| 51 | return; |
| 52 | case EVENT_EXIT: |
| 53 | debug(1, "event: exit (%d)", event->e_un.ret_val); |
| 54 | handle_exit(event); |
| 55 | return; |
| 56 | case EVENT_EXIT_SIGNAL: |
| 57 | debug(1, "event: exit signal (%s [%d])", |
| 58 | shortsignal(event->proc, event->e_un.signum), |
| 59 | event->e_un.signum); |
| 60 | handle_exit_signal(event); |
| 61 | return; |
| 62 | case EVENT_SYSCALL: |
| 63 | debug(1, "event: syscall (%s [%d])", |
| 64 | sysname(event->proc, event->e_un.sysnum), |
| 65 | event->e_un.sysnum); |
| 66 | handle_syscall(event); |
| 67 | return; |
| 68 | case EVENT_SYSRET: |
| 69 | debug(1, "event: sysret (%s [%d])", |
| 70 | sysname(event->proc, event->e_un.sysnum), |
| 71 | event->e_un.sysnum); |
| 72 | handle_sysret(event); |
| 73 | return; |
| 74 | case EVENT_ARCH_SYSCALL: |
| 75 | debug(1, "event: arch_syscall (%s [%d])", |
| 76 | arch_sysname(event->proc, event->e_un.sysnum), |
| 77 | event->e_un.sysnum); |
| 78 | handle_arch_syscall(event); |
| 79 | return; |
| 80 | case EVENT_ARCH_SYSRET: |
| 81 | debug(1, "event: arch_sysret (%s [%d])", |
| 82 | arch_sysname(event->proc, event->e_un.sysnum), |
| 83 | event->e_un.sysnum); |
| 84 | handle_arch_sysret(event); |
| 85 | return; |
| 86 | case EVENT_CLONE: |
| 87 | debug(1, "event: clone (%u)", event->e_un.newpid); |
| 88 | handle_clone(event); |
| 89 | return; |
| 90 | case EVENT_EXEC: |
| 91 | debug(1, "event: exec()"); |
| 92 | handle_exec(event); |
| 93 | return; |
| 94 | case EVENT_BREAKPOINT: |
| 95 | debug(1, "event: breakpoint"); |
| 96 | handle_breakpoint(event); |
| 97 | return; |
| 98 | case EVENT_NEW: |
| 99 | debug(1, "event: new process"); |
| 100 | handle_new(event); |
| 101 | return; |
| 102 | default: |
| 103 | fprintf(stderr, "Error! unknown event?\n"); |
| 104 | exit(1); |
| 105 | } |
| 106 | } |
| 107 | |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 108 | /* TODO */ |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 109 | static void * |
| 110 | address_clone(void * addr) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 111 | debug(DEBUG_FUNCTION, "address_clone(%p)", addr); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 112 | return addr; |
| 113 | } |
| 114 | |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 115 | static void * |
| 116 | breakpoint_clone(void * bp) { |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 117 | Breakpoint * b; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 118 | debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 119 | b = malloc(sizeof(Breakpoint)); |
| 120 | if (!b) { |
| 121 | perror("malloc()"); |
| 122 | exit(1); |
| 123 | } |
| 124 | memcpy(b, bp, sizeof(Breakpoint)); |
| 125 | return b; |
| 126 | } |
| 127 | |
| 128 | typedef struct Pending_New Pending_New; |
| 129 | struct Pending_New { |
| 130 | pid_t pid; |
| 131 | Pending_New * next; |
| 132 | }; |
| 133 | static Pending_New * pending_news = NULL; |
| 134 | |
| 135 | static int |
| 136 | pending_new(pid_t pid) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 137 | Pending_New * p; |
| 138 | |
| 139 | debug(DEBUG_FUNCTION, "pending_new(%d)", pid); |
| 140 | |
| 141 | p = pending_news; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 142 | while (p) { |
| 143 | if (p->pid == pid) { |
| 144 | return 1; |
| 145 | } |
| 146 | p = p->next; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | pending_new_insert(pid_t pid) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 153 | Pending_New * p; |
| 154 | |
| 155 | debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid); |
| 156 | |
| 157 | p = malloc(sizeof(Pending_New)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 158 | if (!p) { |
| 159 | perror("malloc()"); |
| 160 | exit(1); |
| 161 | } |
| 162 | p->pid = pid; |
| 163 | p->next = pending_news; |
| 164 | pending_news = p; |
| 165 | } |
| 166 | |
| 167 | static void |
| 168 | pending_new_remove(pid_t pid) { |
| 169 | Pending_New *p, *pred; |
| 170 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 171 | debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid); |
| 172 | |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 173 | p = pending_news; |
| 174 | if (p->pid == pid) { |
| 175 | pending_news = p->next; |
| 176 | free(p); |
| 177 | } else { |
| 178 | while (p) { |
| 179 | if (p->pid == pid) { |
| 180 | pred->next = p->next; |
| 181 | free(p); |
| 182 | } |
| 183 | pred = p; |
| 184 | p = p->next; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 190 | handle_clone(Event * event) { |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 191 | Process *p; |
| 192 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 193 | debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 194 | |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 195 | p = malloc(sizeof(Process)); |
| 196 | if (!p) { |
| 197 | perror("malloc()"); |
| 198 | exit(1); |
| 199 | } |
| 200 | memcpy(p, event->proc, sizeof(Process)); |
| 201 | p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone); |
| 202 | p->pid = event->e_un.newpid; |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 203 | p->parent = event->proc; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 204 | |
| 205 | if (pending_new(p->pid)) { |
| 206 | pending_new_remove(p->pid); |
| 207 | if (p->breakpoint_being_enabled) { |
| 208 | enable_breakpoint(p->pid, p->breakpoint_being_enabled); |
| 209 | p->breakpoint_being_enabled = NULL; |
| 210 | } |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 211 | if (event->proc->state == STATE_ATTACHED && options.follow) { |
| 212 | p->state = STATE_ATTACHED; |
| 213 | } else { |
| 214 | p->state = STATE_IGNORED; |
| 215 | } |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 216 | continue_process(p->pid); |
| 217 | p->next = list_of_processes; |
| 218 | list_of_processes = p; |
| 219 | } else { |
| 220 | p->state = STATE_BEING_CREATED; |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 221 | p->next = list_of_processes; |
| 222 | list_of_processes = p; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 223 | } |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 224 | continue_process(event->proc->pid); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 228 | handle_new(Event * event) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 229 | Process * proc; |
| 230 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 231 | debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 232 | |
| 233 | proc = pid2proc(event->e_un.newpid); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 234 | if (!proc) { |
| 235 | pending_new_insert(event->e_un.newpid); |
| 236 | } else { |
| 237 | assert(proc->state == STATE_BEING_CREATED); |
| 238 | if (proc->breakpoint_being_enabled) { |
| 239 | enable_breakpoint(proc->pid, proc->breakpoint_being_enabled); |
| 240 | proc->breakpoint_being_enabled = NULL; |
| 241 | } |
| Juan Cespedes | 30439b4 | 2009-05-22 19:03:09 +0200 | [diff] [blame] | 242 | if (options.follow) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 243 | proc->state = STATE_ATTACHED; |
| 244 | } else { |
| 245 | proc->state = STATE_IGNORED; |
| 246 | } |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 247 | continue_process(proc->pid); |
| 248 | } |
| 249 | } |
| 250 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 251 | static char * |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 252 | shortsignal(Process *proc, int signum) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 253 | static char *signalent0[] = { |
| 254 | #include "signalent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 255 | }; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 256 | static char *signalent1[] = { |
| 257 | #include "signalent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 258 | }; |
| 259 | static char **signalents[] = { signalent0, signalent1 }; |
| 260 | int nsignals[] = { sizeof signalent0 / sizeof signalent0[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 261 | sizeof signalent1 / sizeof signalent1[0] |
| 262 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 263 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 264 | debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum); |
| 265 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 266 | if (proc->personality > sizeof signalents / sizeof signalents[0]) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 267 | abort(); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 268 | if (signum < 0 || signum >= nsignals[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 269 | return "UNKNOWN_SIGNAL"; |
| 270 | } else { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 271 | return signalents[proc->personality][signum]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 275 | static char * |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 276 | sysname(Process *proc, int sysnum) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 277 | static char result[128]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 278 | static char *syscalent0[] = { |
| 279 | #include "syscallent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 280 | }; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 281 | static char *syscalent1[] = { |
| 282 | #include "syscallent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 283 | }; |
| 284 | static char **syscalents[] = { syscalent0, syscalent1 }; |
| 285 | int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 286 | sizeof syscalent1 / sizeof syscalent1[0] |
| 287 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 288 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 289 | debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum); |
| 290 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 291 | if (proc->personality > sizeof syscalents / sizeof syscalents[0]) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 292 | abort(); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 293 | if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 294 | sprintf(result, "SYS_%d", sysnum); |
| 295 | return result; |
| 296 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 297 | sprintf(result, "SYS_%s", |
| 298 | syscalents[proc->personality][sysnum]); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 299 | return result; |
| 300 | } |
| 301 | } |
| 302 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 303 | static char * |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 304 | arch_sysname(Process *proc, int sysnum) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 305 | static char result[128]; |
| 306 | static char *arch_syscalent[] = { |
| 307 | #include "arch_syscallent.h" |
| 308 | }; |
| 309 | int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0]; |
| 310 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 311 | debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum); |
| 312 | |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 313 | if (sysnum < 0 || sysnum >= nsyscals) { |
| 314 | sprintf(result, "ARCH_%d", sysnum); |
| 315 | return result; |
| 316 | } else { |
| 317 | sprintf(result, "ARCH_%s", |
| 318 | arch_syscalent[sysnum]); |
| 319 | return result; |
| 320 | } |
| 321 | } |
| 322 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 323 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 324 | handle_signal(Event *event) { |
| 325 | debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 326 | if (exiting && event->e_un.signum == SIGSTOP) { |
| 327 | pid_t pid = event->proc->pid; |
| 328 | disable_all_breakpoints(event->proc); |
| 329 | untrace_pid(pid); |
| 330 | remove_proc(event->proc); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 331 | return; |
| 332 | } |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 333 | if (event->proc->state != STATE_IGNORED && !options.no_signals) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 334 | output_line(event->proc, "--- %s (%s) ---", |
| 335 | shortsignal(event->proc, event->e_un.signum), |
| 336 | strsignal(event->e_un.signum)); |
| 337 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 338 | continue_after_signal(event->proc->pid, event->e_un.signum); |
| 339 | } |
| 340 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 341 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 342 | handle_exit(Event *event) { |
| 343 | 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] | 344 | if (event->proc->state != STATE_IGNORED) { |
| 345 | output_line(event->proc, "+++ exited (status %d) +++", |
| 346 | event->e_un.ret_val); |
| 347 | } |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 348 | remove_proc(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 351 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 352 | handle_exit_signal(Event *event) { |
| 353 | 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] | 354 | if (event->proc->state != STATE_IGNORED) { |
| 355 | output_line(event->proc, "+++ killed by %s +++", |
| 356 | shortsignal(event->proc, event->e_un.signum)); |
| 357 | } |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 358 | remove_proc(event->proc); |
| 359 | } |
| 360 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 361 | static void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 362 | remove_proc(Process *proc) { |
| 363 | Process *tmp, *tmp2; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 364 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 365 | debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 366 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 367 | if (list_of_processes == proc) { |
| 368 | tmp = list_of_processes; |
| 369 | list_of_processes = list_of_processes->next; |
| 370 | free(tmp); |
| 371 | return; |
| 372 | } |
| 373 | tmp = list_of_processes; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 374 | while (tmp->next) { |
| 375 | if (tmp->next == proc) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 376 | tmp2 = tmp->next; |
| 377 | tmp->next = tmp->next->next; |
| 378 | free(tmp2); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 379 | continue; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 380 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 381 | tmp = tmp->next; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 382 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 385 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 386 | handle_syscall(Event *event) { |
| 387 | 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] | 388 | if (event->proc->state != STATE_IGNORED) { |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 389 | callstack_push_syscall(event->proc, event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 390 | if (options.syscalls) { |
| 391 | output_left(LT_TOF_SYSCALL, event->proc, |
| 392 | sysname(event->proc, event->e_un.sysnum)); |
| 393 | } |
| 394 | if (event->proc->breakpoints_enabled == 0) { |
| 395 | enable_all_breakpoints(event->proc); |
| 396 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 397 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 398 | continue_process(event->proc->pid); |
| 399 | } |
| 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_exec(Event * event) { |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 403 | Process * proc = event->proc; |
| 404 | pid_t saved_pid; |
| 405 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 406 | debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid); |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 407 | if (proc->state == STATE_IGNORED) { |
| 408 | untrace_pid(proc->pid); |
| 409 | remove_proc(proc); |
| 410 | return; |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 411 | } |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 412 | output_line(proc, "--- Called exec() ---"); |
| 413 | proc->mask_32bit = 0; |
| 414 | proc->personality = 0; |
| 415 | proc->arch_ptr = NULL; |
| 416 | free(proc->filename); |
| 417 | proc->filename = pid2name(proc->pid); |
| 418 | saved_pid = proc->pid; |
| 419 | proc->pid = 0; |
| 420 | breakpoints_init(proc); |
| 421 | proc->pid = saved_pid; |
| 422 | proc->callstack_depth = 0; |
| 423 | continue_process(proc->pid); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 427 | handle_arch_syscall(Event *event) { |
| 428 | 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] | 429 | if (event->proc->state != STATE_IGNORED) { |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 430 | callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 431 | if (options.syscalls) { |
| 432 | output_left(LT_TOF_SYSCALL, event->proc, |
| 433 | arch_sysname(event->proc, event->e_un.sysnum)); |
| 434 | } |
| 435 | if (event->proc->breakpoints_enabled == 0) { |
| 436 | enable_all_breakpoints(event->proc); |
| 437 | } |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 438 | } |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 439 | continue_process(event->proc->pid); |
| 440 | } |
| 441 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 442 | struct timeval current_time_spent; |
| 443 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 444 | static void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 445 | calc_time_spent(Process *proc) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 446 | struct timeval tv; |
| 447 | struct timezone tz; |
| 448 | struct timeval diff; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 449 | struct callstack_element *elem; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 450 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 451 | debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 452 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 453 | |
| 454 | gettimeofday(&tv, &tz); |
| 455 | |
| 456 | diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec; |
| 457 | if (tv.tv_usec >= elem->time_spent.tv_usec) { |
| 458 | diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec; |
| 459 | } else { |
| 460 | diff.tv_sec++; |
| 461 | diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec; |
| 462 | } |
| 463 | current_time_spent = diff; |
| 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_sysret(Event *event) { |
| 468 | 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] | 469 | if (event->proc->state != STATE_IGNORED) { |
| 470 | if (opt_T || options.summary) { |
| 471 | calc_time_spent(event->proc); |
| 472 | } |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 473 | if (options.syscalls) { |
| 474 | output_right(LT_TOF_SYSCALLR, event->proc, |
| 475 | sysname(event->proc, event->e_un.sysnum)); |
| 476 | } |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 477 | callstack_pop(event->proc); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 478 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 479 | continue_process(event->proc->pid); |
| 480 | } |
| 481 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 482 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 483 | handle_arch_sysret(Event *event) { |
| 484 | 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] | 485 | if (event->proc->state != STATE_IGNORED) { |
| 486 | if (opt_T || options.summary) { |
| 487 | calc_time_spent(event->proc); |
| 488 | } |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 489 | if (options.syscalls) { |
| 490 | output_right(LT_TOF_SYSCALLR, event->proc, |
| 491 | arch_sysname(event->proc, event->e_un.sysnum)); |
| 492 | } |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 493 | callstack_pop(event->proc); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 494 | } |
| 495 | continue_process(event->proc->pid); |
| 496 | } |
| 497 | |
| Petr Machata | 067322d | 2010-10-25 14:47:55 +0200 | [diff] [blame^] | 498 | #ifdef __powerpc__ |
| 499 | void *get_count_register (Process *proc); |
| 500 | #endif |
| 501 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 502 | static void |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 503 | handle_breakpoint(Event *event) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 504 | int i, j; |
| Juan Cespedes | 1dec217 | 2009-05-07 10:12:10 +0200 | [diff] [blame] | 505 | Breakpoint *sbp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 506 | |
| Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 507 | debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr); |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 508 | debug(2, "event: breakpoint (%p)", event->e_un.brk_addr); |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 509 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 510 | #ifdef __powerpc__ |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 511 | /* Need to skip following NOP's to prevent a fake function from being stacked. */ |
| 512 | long stub_addr = (long) get_count_register(event->proc); |
| Juan Cespedes | 1dec217 | 2009-05-07 10:12:10 +0200 | [diff] [blame] | 513 | Breakpoint *stub_bp = NULL; |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 514 | char nop_instruction[] = PPC_NOP; |
| 515 | |
| 516 | stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr); |
| 517 | |
| 518 | if (stub_bp) { |
| 519 | unsigned char *bp_instruction = stub_bp->orig_value; |
| 520 | |
| 521 | if (memcmp(bp_instruction, nop_instruction, |
| 522 | PPC_NOP_LENGTH) == 0) { |
| 523 | if (stub_addr != (long) event->e_un.brk_addr) { |
| 524 | set_instruction_pointer (event->proc, event->e_un.brk_addr + 4); |
| 525 | continue_process(event->proc->pid); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | } |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 529 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 530 | #endif |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 531 | if ((sbp = event->proc->breakpoint_being_enabled) != 0) { |
| Juan Cespedes | b1dd77d | 2002-03-03 00:22:06 +0100 | [diff] [blame] | 532 | /* Reinsert breakpoint */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 533 | continue_enabling_breakpoint(event->proc->pid, |
| 534 | event->proc-> |
| 535 | breakpoint_being_enabled); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 536 | event->proc->breakpoint_being_enabled = NULL; |
| 537 | return; |
| 538 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 539 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 540 | for (i = event->proc->callstack_depth - 1; i >= 0; i--) { |
| 541 | if (event->e_un.brk_addr == |
| 542 | event->proc->callstack[i].return_addr) { |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 543 | #ifdef __powerpc__ |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 544 | /* |
| 545 | * PPC HACK! (XXX FIXME TODO) |
| 546 | * The PLT gets modified during the first call, |
| 547 | * so be sure to re-enable the breakpoint. |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 548 | */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 549 | unsigned long a; |
| 550 | struct library_symbol *libsym = |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 551 | event->proc->callstack[i].c_un.libfunc; |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 552 | void *addr = sym2addr(event->proc, libsym); |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 553 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 554 | if (libsym->plt_type != LS_TOPLT_POINT) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 555 | unsigned char break_insn[] = BREAKPOINT_VALUE; |
| 556 | |
| 557 | sbp = address2bpstruct(event->proc, addr); |
| 558 | assert(sbp); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 559 | a = ptrace(PTRACE_PEEKTEXT, event->proc->pid, |
| 560 | addr); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 561 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 562 | if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 563 | sbp->enabled--; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 564 | insert_breakpoint(event->proc, addr, |
| 565 | libsym); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 566 | } |
| 567 | } else { |
| Petr Machata | 067322d | 2010-10-25 14:47:55 +0200 | [diff] [blame^] | 568 | sbp = dict_find_entry(event->proc->breakpoints, addr); |
| 569 | /* On powerpc, the breakpoint address |
| 570 | may end up being actual entry point |
| 571 | of the library symbol, not the PLT |
| 572 | address we computed. In that case, |
| 573 | sbp is NULL. */ |
| 574 | if (sbp == NULL || addr != sbp->addr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 575 | insert_breakpoint(event->proc, addr, |
| 576 | libsym); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 577 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 578 | } |
| Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 579 | #elif defined(__mips__) |
| Arnaud Patard | 161193f | 2010-01-08 08:40:14 -0500 | [diff] [blame] | 580 | void *addr = NULL; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 581 | struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc; |
| Arnaud Patard | 161193f | 2010-01-08 08:40:14 -0500 | [diff] [blame] | 582 | struct library_symbol *new_sym; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 583 | assert(sym); |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 584 | addr=sym2addr(event->proc,sym); |
| Arnaud Patard | 161193f | 2010-01-08 08:40:14 -0500 | [diff] [blame] | 585 | sbp = dict_find_entry(event->proc->breakpoints, addr); |
| 586 | if (sbp) { |
| 587 | if (addr != sbp->addr) { |
| 588 | insert_breakpoint(event->proc, addr, sym); |
| 589 | } |
| 590 | } else { |
| 591 | new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1); |
| 592 | memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1); |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 593 | new_sym->next=event->proc->list_of_symbols; |
| 594 | event->proc->list_of_symbols=new_sym; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 595 | insert_breakpoint(event->proc, addr, new_sym); |
| 596 | } |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 597 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 598 | for (j = event->proc->callstack_depth - 1; j > i; j--) { |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 599 | callstack_pop(event->proc); |
| 600 | } |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 601 | if (event->proc->state != STATE_IGNORED) { |
| 602 | if (opt_T || options.summary) { |
| 603 | calc_time_spent(event->proc); |
| 604 | } |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 605 | } |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 606 | event->proc->return_addr = event->e_un.brk_addr; |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 607 | if (event->proc->state != STATE_IGNORED) { |
| 608 | output_right(LT_TOF_FUNCTIONR, event->proc, |
| 609 | event->proc->callstack[i].c_un.libfunc->name); |
| 610 | } |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 611 | callstack_pop(event->proc); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 612 | continue_after_breakpoint(event->proc, |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 613 | address2bpstruct(event->proc, |
| 614 | event->e_un.brk_addr)); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 615 | return; |
| 616 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 619 | if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 620 | if (event->proc->state != STATE_IGNORED) { |
| 621 | event->proc->stack_pointer = get_stack_pointer(event->proc); |
| 622 | event->proc->return_addr = |
| 623 | get_return_addr(event->proc, event->proc->stack_pointer); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 624 | callstack_push_symfunc(event->proc, sbp->libsym); |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 625 | output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name); |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 626 | } |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 627 | #ifdef PLT_REINITALISATION_BP |
| 628 | if (event->proc->need_to_reinitialize_breakpoints |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 629 | && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) == |
| 630 | 0)) |
| 631 | reinitialize_breakpoints(event->proc); |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 632 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 633 | |
| 634 | continue_after_breakpoint(event->proc, sbp); |
| 635 | return; |
| 636 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 637 | |
| Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 638 | if (event->proc->state != STATE_IGNORED && !options.no_plt) { |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 639 | output_line(event->proc, "unexpected breakpoint at %p", |
| 640 | (void *)event->e_un.brk_addr); |
| 641 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 642 | continue_process(event->proc->pid); |
| 643 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 644 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 645 | static void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 646 | callstack_push_syscall(Process *proc, int sysnum) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 647 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 648 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 649 | 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] | 650 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 651 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Arnaud Patard | 91a1f32 | 2010-01-08 08:40:13 -0500 | [diff] [blame] | 652 | fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__); |
| 653 | abort(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 657 | elem = &proc->callstack[proc->callstack_depth]; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 658 | elem->is_syscall = 1; |
| 659 | elem->c_un.syscall = sysnum; |
| 660 | elem->return_addr = NULL; |
| 661 | |
| 662 | proc->callstack_depth++; |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 663 | if (opt_T || options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 664 | struct timezone tz; |
| 665 | gettimeofday(&elem->time_spent, &tz); |
| 666 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 667 | } |
| 668 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 669 | static void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 670 | callstack_push_symfunc(Process *proc, struct library_symbol *sym) { |
| Arnaud Patard | 2657008 | 2010-01-08 08:40:12 -0500 | [diff] [blame] | 671 | struct callstack_element *elem, *prev; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 672 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 673 | 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] | 674 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 675 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Arnaud Patard | 91a1f32 | 2010-01-08 08:40:13 -0500 | [diff] [blame] | 676 | fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__); |
| 677 | abort(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
| Arnaud Patard | 2657008 | 2010-01-08 08:40:12 -0500 | [diff] [blame] | 681 | prev = &proc->callstack[proc->callstack_depth-1]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 682 | elem = &proc->callstack[proc->callstack_depth]; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 683 | elem->is_syscall = 0; |
| 684 | elem->c_un.libfunc = sym; |
| 685 | |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 686 | elem->return_addr = proc->return_addr; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 687 | if (elem->return_addr) { |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 688 | insert_breakpoint(proc, elem->return_addr, 0); |
| 689 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 690 | |
| Arnaud Patard | 2657008 | 2010-01-08 08:40:12 -0500 | [diff] [blame] | 691 | /* handle functions like atexit() on mips which have no return */ |
| 692 | if (elem->return_addr != prev->return_addr) |
| 693 | proc->callstack_depth++; |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 694 | if (opt_T || options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 695 | struct timezone tz; |
| 696 | gettimeofday(&elem->time_spent, &tz); |
| 697 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 700 | static void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 701 | callstack_pop(Process *proc) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 702 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 703 | assert(proc->callstack_depth > 0); |
| 704 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 705 | debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 706 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 707 | if (!elem->is_syscall && elem->return_addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 708 | delete_breakpoint(proc, elem->return_addr); |
| 709 | } |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 710 | if (elem->arch_ptr != NULL) { |
| 711 | free(elem->arch_ptr); |
| 712 | elem->arch_ptr = NULL; |
| 713 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 714 | proc->callstack_depth--; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 715 | } |