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