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