| 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 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 23 | static void process_signal(struct event *event); |
| 24 | static void process_exit(struct event *event); |
| 25 | static void process_exit_signal(struct event *event); |
| 26 | static void process_syscall(struct event *event); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 27 | static void process_arch_syscall(struct event *event); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 28 | static void process_sysret(struct event *event); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 29 | static void process_arch_sysret(struct event *event); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | static void process_breakpoint(struct event *event); |
| 31 | static void remove_proc(struct process *proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 32 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 33 | static void callstack_push_syscall(struct process *proc, int sysnum); |
| 34 | static void callstack_push_symfunc(struct process *proc, |
| 35 | struct library_symbol *sym); |
| 36 | static void callstack_pop(struct process *proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 37 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 38 | static char *shortsignal(struct process *proc, int signum) |
| 39 | { |
| 40 | static char *signalent0[] = { |
| 41 | #include "signalent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 42 | }; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 43 | static char *signalent1[] = { |
| 44 | #include "signalent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 45 | }; |
| 46 | static char **signalents[] = { signalent0, signalent1 }; |
| 47 | int nsignals[] = { sizeof signalent0 / sizeof signalent0[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 48 | sizeof signalent1 / sizeof signalent1[0] |
| 49 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 50 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 51 | if (proc->personality > sizeof signalents / sizeof signalents[0]) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | abort(); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 53 | if (signum < 0 || signum >= nsignals[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 54 | return "UNKNOWN_SIGNAL"; |
| 55 | } else { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 56 | return signalents[proc->personality][signum]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 60 | static char *sysname(struct process *proc, int sysnum) |
| 61 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 62 | static char result[128]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 63 | static char *syscalent0[] = { |
| 64 | #include "syscallent.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 65 | }; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | static char *syscalent1[] = { |
| 67 | #include "syscallent1.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 68 | }; |
| 69 | static char **syscalents[] = { syscalent0, syscalent1 }; |
| 70 | int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0], |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 71 | sizeof syscalent1 / sizeof syscalent1[0] |
| 72 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 73 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 74 | if (proc->personality > sizeof syscalents / sizeof syscalents[0]) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 75 | abort(); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 76 | if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 77 | sprintf(result, "SYS_%d", sysnum); |
| 78 | return result; |
| 79 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 80 | sprintf(result, "SYS_%s", |
| 81 | syscalents[proc->personality][sysnum]); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 82 | return result; |
| 83 | } |
| 84 | } |
| 85 | |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 86 | static char *arch_sysname(struct process *proc, int sysnum) |
| 87 | { |
| 88 | static char result[128]; |
| 89 | static char *arch_syscalent[] = { |
| 90 | #include "arch_syscallent.h" |
| 91 | }; |
| 92 | int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0]; |
| 93 | |
| 94 | if (sysnum < 0 || sysnum >= nsyscals) { |
| 95 | sprintf(result, "ARCH_%d", sysnum); |
| 96 | return result; |
| 97 | } else { |
| 98 | sprintf(result, "ARCH_%s", |
| 99 | arch_syscalent[sysnum]); |
| 100 | return result; |
| 101 | } |
| 102 | } |
| 103 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | void process_event(struct event *event) |
| 105 | { |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 106 | switch (event->thing) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 107 | case LT_EV_NONE: |
| 108 | debug(1, "event: none"); |
| 109 | return; |
| 110 | case LT_EV_SIGNAL: |
| 111 | debug(1, "event: signal (%s [%d])", |
| 112 | shortsignal(event->proc, event->e_un.signum), |
| 113 | event->e_un.signum); |
| 114 | process_signal(event); |
| 115 | return; |
| 116 | case LT_EV_EXIT: |
| 117 | debug(1, "event: exit (%d)", event->e_un.ret_val); |
| 118 | process_exit(event); |
| 119 | return; |
| 120 | case LT_EV_EXIT_SIGNAL: |
| 121 | debug(1, "event: exit signal (%s [%d])", |
| 122 | shortsignal(event->proc, event->e_un.signum), |
| 123 | event->e_un.signum); |
| 124 | process_exit_signal(event); |
| 125 | return; |
| 126 | case LT_EV_SYSCALL: |
| 127 | debug(1, "event: syscall (%s [%d])", |
| 128 | sysname(event->proc, event->e_un.sysnum), |
| 129 | event->e_un.sysnum); |
| 130 | process_syscall(event); |
| 131 | return; |
| 132 | case LT_EV_SYSRET: |
| 133 | debug(1, "event: sysret (%s [%d])", |
| 134 | sysname(event->proc, event->e_un.sysnum), |
| 135 | event->e_un.sysnum); |
| 136 | process_sysret(event); |
| 137 | return; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 138 | case LT_EV_ARCH_SYSCALL: |
| 139 | debug(1, "event: arch_syscall (%s [%d])", |
| 140 | arch_sysname(event->proc, event->e_un.sysnum), |
| 141 | event->e_un.sysnum); |
| 142 | process_arch_syscall(event); |
| 143 | return; |
| 144 | case LT_EV_ARCH_SYSRET: |
| 145 | debug(1, "event: arch_sysret (%s [%d])", |
| 146 | arch_sysname(event->proc, event->e_un.sysnum), |
| 147 | event->e_un.sysnum); |
| 148 | process_arch_sysret(event); |
| 149 | return; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 150 | case LT_EV_BREAKPOINT: |
| 151 | debug(1, "event: breakpoint"); |
| 152 | process_breakpoint(event); |
| 153 | return; |
| 154 | default: |
| 155 | fprintf(stderr, "Error! unknown event?\n"); |
| 156 | exit(1); |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 160 | static void process_signal(struct event *event) |
| 161 | { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 162 | if (exiting && event->e_un.signum == SIGSTOP) { |
| 163 | pid_t pid = event->proc->pid; |
| 164 | disable_all_breakpoints(event->proc); |
| 165 | untrace_pid(pid); |
| 166 | remove_proc(event->proc); |
| 167 | continue_after_signal(pid, event->e_un.signum); |
| 168 | return; |
| 169 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 170 | output_line(event->proc, "--- %s (%s) ---", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 171 | shortsignal(event->proc, event->e_un.signum), |
| 172 | strsignal(event->e_un.signum)); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 173 | continue_after_signal(event->proc->pid, event->e_un.signum); |
| 174 | } |
| 175 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 176 | static void process_exit(struct event *event) |
| 177 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 178 | output_line(event->proc, "+++ exited (status %d) +++", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 179 | event->e_un.ret_val); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 180 | remove_proc(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 183 | static void process_exit_signal(struct event *event) |
| 184 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 185 | output_line(event->proc, "+++ killed by %s +++", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 186 | shortsignal(event->proc, event->e_un.signum)); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 187 | remove_proc(event->proc); |
| 188 | } |
| 189 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 190 | static void remove_proc(struct process *proc) |
| 191 | { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 192 | struct process *tmp, *tmp2; |
| 193 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 194 | debug(1, "Removing pid %u\n", proc->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 195 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 196 | if (list_of_processes == proc) { |
| 197 | tmp = list_of_processes; |
| 198 | list_of_processes = list_of_processes->next; |
| 199 | free(tmp); |
| 200 | return; |
| 201 | } |
| 202 | tmp = list_of_processes; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 203 | while (tmp->next) { |
| 204 | if (tmp->next == proc) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 205 | tmp2 = tmp->next; |
| 206 | tmp->next = tmp->next->next; |
| 207 | free(tmp2); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 208 | continue; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 209 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 210 | tmp = tmp->next; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 211 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 214 | static void process_syscall(struct event *event) |
| 215 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 216 | if (opt_S) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 217 | output_left(LT_TOF_SYSCALL, event->proc, |
| 218 | sysname(event->proc, event->e_un.sysnum)); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 219 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 220 | if (fork_p(event->proc, event->e_un.sysnum)) { |
| 221 | disable_all_breakpoints(event->proc); |
| 222 | } else if (event->proc->breakpoints_enabled == 0) { |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 223 | enable_all_breakpoints(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 224 | } |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 225 | callstack_push_syscall(event->proc, event->e_un.sysnum); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 226 | continue_process(event->proc->pid); |
| 227 | } |
| 228 | |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 229 | static void process_arch_syscall(struct event *event) |
| 230 | { |
| 231 | if (opt_S) { |
| 232 | output_left(LT_TOF_SYSCALL, event->proc, |
| 233 | arch_sysname(event->proc, event->e_un.sysnum)); |
| 234 | } |
| 235 | if (event->proc->breakpoints_enabled == 0) { |
| 236 | enable_all_breakpoints(event->proc); |
| 237 | } |
| 238 | callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum); |
| 239 | continue_process(event->proc->pid); |
| 240 | } |
| 241 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 242 | struct timeval current_time_spent; |
| 243 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 244 | static void calc_time_spent(struct process *proc) |
| 245 | { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 246 | struct timeval tv; |
| 247 | struct timezone tz; |
| 248 | struct timeval diff; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 249 | struct callstack_element *elem; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 250 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 251 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 252 | |
| 253 | gettimeofday(&tv, &tz); |
| 254 | |
| 255 | diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec; |
| 256 | if (tv.tv_usec >= elem->time_spent.tv_usec) { |
| 257 | diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec; |
| 258 | } else { |
| 259 | diff.tv_sec++; |
| 260 | diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec; |
| 261 | } |
| 262 | current_time_spent = diff; |
| 263 | } |
| 264 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 265 | static void process_sysret(struct event *event) |
| 266 | { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 267 | if (opt_T || opt_c) { |
| 268 | calc_time_spent(event->proc); |
| 269 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 270 | if (fork_p(event->proc, event->e_un.sysnum)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 271 | if (opt_f) { |
| Steve Fink | 65b53df | 2006-09-25 02:27:08 +0200 | [diff] [blame] | 272 | arg_type_info info; |
| Steve Fink | 65b53df | 2006-09-25 02:27:08 +0200 | [diff] [blame] | 273 | info.type = ARGTYPE_LONG; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 274 | pid_t child = |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 275 | gimme_arg(LT_TOF_SYSCALLR, event->proc, -1, &info); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 276 | if (child > 0) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 277 | open_pid(child, 0); |
| 278 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 279 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 280 | enable_all_breakpoints(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 281 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 282 | callstack_pop(event->proc); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 283 | if (opt_S) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 284 | output_right(LT_TOF_SYSCALLR, event->proc, |
| 285 | sysname(event->proc, event->e_un.sysnum)); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 286 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 287 | continue_process(event->proc->pid); |
| 288 | } |
| 289 | |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 290 | static void process_arch_sysret(struct event *event) |
| 291 | { |
| 292 | if (opt_T || opt_c) { |
| 293 | calc_time_spent(event->proc); |
| 294 | } |
| 295 | callstack_pop(event->proc); |
| 296 | if (opt_S) { |
| 297 | output_right(LT_TOF_SYSCALLR, event->proc, |
| 298 | arch_sysname(event->proc, event->e_un.sysnum)); |
| 299 | } |
| 300 | continue_process(event->proc->pid); |
| 301 | } |
| 302 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 303 | static void process_breakpoint(struct event *event) |
| 304 | { |
| 305 | int i, j; |
| Steve Fink | 65b53df | 2006-09-25 02:27:08 +0200 | [diff] [blame] | 306 | struct breakpoint *sbp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 307 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 308 | debug(2, "event: breakpoint (%p)", event->e_un.brk_addr); |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 309 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 310 | #ifdef __powerpc__ |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 311 | /* Need to skip following NOP's to prevent a fake function from being stacked. */ |
| 312 | long stub_addr = (long) get_count_register(event->proc); |
| 313 | struct breakpoint *stub_bp = NULL; |
| 314 | char nop_instruction[] = PPC_NOP; |
| 315 | |
| 316 | stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr); |
| 317 | |
| 318 | if (stub_bp) { |
| 319 | unsigned char *bp_instruction = stub_bp->orig_value; |
| 320 | |
| 321 | if (memcmp(bp_instruction, nop_instruction, |
| 322 | PPC_NOP_LENGTH) == 0) { |
| 323 | if (stub_addr != (long) event->e_un.brk_addr) { |
| 324 | set_instruction_pointer (event->proc, event->e_un.brk_addr + 4); |
| 325 | continue_process(event->proc->pid); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | } |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 329 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 330 | #endif |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 331 | if ((sbp = event->proc->breakpoint_being_enabled) != 0) { |
| Juan Cespedes | b1dd77d | 2002-03-03 00:22:06 +0100 | [diff] [blame] | 332 | /* Reinsert breakpoint */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 333 | continue_enabling_breakpoint(event->proc->pid, |
| 334 | event->proc-> |
| 335 | breakpoint_being_enabled); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 336 | event->proc->breakpoint_being_enabled = NULL; |
| 337 | return; |
| 338 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 339 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 340 | for (i = event->proc->callstack_depth - 1; i >= 0; i--) { |
| 341 | if (event->e_un.brk_addr == |
| 342 | event->proc->callstack[i].return_addr) { |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 343 | #ifdef __powerpc__ |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 344 | /* |
| 345 | * PPC HACK! (XXX FIXME TODO) |
| 346 | * The PLT gets modified during the first call, |
| 347 | * so be sure to re-enable the breakpoint. |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 348 | */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 349 | unsigned long a; |
| 350 | struct library_symbol *libsym = |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 351 | event->proc->callstack[i].c_un.libfunc; |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 352 | void *addr = sym2addr(event->proc, libsym); |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 353 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 354 | if (libsym->plt_type != LS_TOPLT_POINT) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 355 | unsigned char break_insn[] = BREAKPOINT_VALUE; |
| 356 | |
| 357 | sbp = address2bpstruct(event->proc, addr); |
| 358 | assert(sbp); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 359 | a = ptrace(PTRACE_PEEKTEXT, event->proc->pid, |
| 360 | addr); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 361 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 362 | if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 363 | sbp->enabled--; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 364 | insert_breakpoint(event->proc, addr, |
| 365 | libsym); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 366 | } |
| 367 | } else { |
| 368 | sbp = libsym->brkpnt; |
| 369 | assert(sbp); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 370 | if (addr != sbp->addr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 371 | insert_breakpoint(event->proc, addr, |
| 372 | libsym); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 373 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 374 | } |
| Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 375 | #elif defined(__mips__) |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 376 | void *addr; |
| 377 | void *old_addr; |
| 378 | struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc; |
| 379 | assert(sym && sym->brkpnt); |
| 380 | old_addr=sym->brkpnt->addr; |
| 381 | addr=sym2addr(event->proc,sym); |
| 382 | assert(old_addr !=0 && addr !=0); |
| 383 | if(addr != old_addr){ |
| 384 | struct library_symbol *new_sym; |
| 385 | new_sym=malloc(sizeof(*new_sym)); |
| 386 | memcpy(new_sym,sym,sizeof(*new_sym)); |
| 387 | new_sym->next=event->proc->list_of_symbols; |
| 388 | event->proc->list_of_symbols=new_sym; |
| 389 | new_sym->brkpnt=0; |
| 390 | insert_breakpoint(event->proc, addr, new_sym); |
| 391 | } |
| Juan Cespedes | 5bfb061 | 2002-03-31 20:01:28 +0200 | [diff] [blame] | 392 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 393 | for (j = event->proc->callstack_depth - 1; j > i; j--) { |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 394 | callstack_pop(event->proc); |
| 395 | } |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 396 | if (opt_T || opt_c) { |
| 397 | calc_time_spent(event->proc); |
| 398 | } |
| 399 | callstack_pop(event->proc); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 400 | event->proc->return_addr = event->e_un.brk_addr; |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 401 | output_right(LT_TOF_FUNCTIONR, event->proc, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 402 | event->proc->callstack[i].c_un.libfunc-> |
| 403 | name); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 404 | continue_after_breakpoint(event->proc, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 405 | address2bpstruct(event->proc, |
| 406 | event->e_un. |
| 407 | brk_addr)); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 408 | return; |
| 409 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 412 | if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) { |
| 413 | event->proc->stack_pointer = get_stack_pointer(event->proc); |
| 414 | event->proc->return_addr = |
| 415 | get_return_addr(event->proc, event->proc->stack_pointer); |
| 416 | output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name); |
| 417 | callstack_push_symfunc(event->proc, sbp->libsym); |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 418 | #ifdef PLT_REINITALISATION_BP |
| 419 | if (event->proc->need_to_reinitialize_breakpoints |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 420 | && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) == |
| 421 | 0)) |
| 422 | reinitialize_breakpoints(event->proc); |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 423 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 424 | |
| 425 | continue_after_breakpoint(event->proc, sbp); |
| 426 | return; |
| 427 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 428 | |
| 429 | output_line(event->proc, "unexpected breakpoint at %p", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 430 | (void *)event->e_un.brk_addr); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 431 | continue_process(event->proc->pid); |
| 432 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 433 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 434 | static void callstack_push_syscall(struct process *proc, int sysnum) |
| 435 | { |
| 436 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 437 | |
| 438 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 439 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 440 | fprintf(stderr, "Error: call nesting too deep!\n"); |
| 441 | return; |
| 442 | } |
| 443 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 444 | elem = &proc->callstack[proc->callstack_depth]; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 445 | elem->is_syscall = 1; |
| 446 | elem->c_un.syscall = sysnum; |
| 447 | elem->return_addr = NULL; |
| 448 | |
| 449 | proc->callstack_depth++; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 450 | if (opt_T || opt_c) { |
| 451 | struct timezone tz; |
| 452 | gettimeofday(&elem->time_spent, &tz); |
| 453 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 456 | static void |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 457 | callstack_push_symfunc(struct process *proc, struct library_symbol *sym) |
| 458 | { |
| 459 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 460 | |
| 461 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 462 | if (proc->callstack_depth == MAX_CALLDEPTH - 1) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 463 | fprintf(stderr, "Error: call nesting too deep!\n"); |
| 464 | return; |
| 465 | } |
| 466 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 467 | elem = &proc->callstack[proc->callstack_depth]; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 468 | elem->is_syscall = 0; |
| 469 | elem->c_un.libfunc = sym; |
| 470 | |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 471 | elem->return_addr = proc->return_addr; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 472 | if (elem->return_addr) { |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 473 | insert_breakpoint(proc, elem->return_addr, 0); |
| 474 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 475 | |
| 476 | proc->callstack_depth++; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 477 | if (opt_T || opt_c) { |
| 478 | struct timezone tz; |
| 479 | gettimeofday(&elem->time_spent, &tz); |
| 480 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 483 | static void callstack_pop(struct process *proc) |
| 484 | { |
| 485 | struct callstack_element *elem; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 486 | assert(proc->callstack_depth > 0); |
| 487 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 488 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 489 | if (!elem->is_syscall && elem->return_addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 490 | delete_breakpoint(proc, elem->return_addr); |
| 491 | } |
| 492 | proc->callstack_depth--; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 493 | } |