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