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