| 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 | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | |
| 12 | #include "ltrace.h" |
| 13 | #include "output.h" |
| 14 | #include "options.h" |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 15 | #include "elf.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame^] | 17 | #ifdef __powerpc__ |
| 18 | #include <sys/ptrace.h> |
| 19 | #endif |
| 20 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 21 | static void process_signal(struct event * event); |
| 22 | static void process_exit(struct event * event); |
| 23 | static void process_exit_signal(struct event * event); |
| 24 | static void process_syscall(struct event * event); |
| 25 | static void process_sysret(struct event * event); |
| 26 | static void process_breakpoint(struct event * event); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 27 | static void remove_proc(struct process * proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 28 | |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 29 | static void callstack_push_syscall(struct process * proc, int sysnum); |
| 30 | static void callstack_push_symfunc(struct process * proc, struct library_symbol * sym); |
| 31 | static void callstack_pop(struct process * proc); |
| 32 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 33 | void |
| 34 | process_event(struct event * event) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 35 | switch (event->thing) { |
| 36 | case LT_EV_NONE: |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 37 | if (opt_d>0) { |
| 38 | output_line(0, "event: none"); |
| 39 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 40 | return; |
| 41 | case LT_EV_SIGNAL: |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 42 | if (opt_d>0) { |
| 43 | output_line(0, "event: signal (%d)", event->e_un.signum); |
| 44 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 45 | process_signal(event); |
| 46 | return; |
| 47 | case LT_EV_EXIT: |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 48 | if (opt_d>0) { |
| 49 | output_line(0, "event: exit (%d)", event->e_un.ret_val); |
| 50 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 51 | process_exit(event); |
| 52 | return; |
| 53 | case LT_EV_EXIT_SIGNAL: |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 54 | if (opt_d>0) { |
| 55 | output_line(0, "event: exit signal (%d)", event->e_un.signum); |
| 56 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 57 | process_exit_signal(event); |
| 58 | return; |
| 59 | case LT_EV_SYSCALL: |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 60 | if (opt_d>0) { |
| 61 | output_line(0, "event: syscall (%d)", event->e_un.sysnum); |
| 62 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 63 | process_syscall(event); |
| 64 | return; |
| 65 | case LT_EV_SYSRET: |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 66 | if (opt_d>0) { |
| 67 | output_line(0, "event: sysret (%d)", event->e_un.sysnum); |
| 68 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 69 | process_sysret(event); |
| 70 | return; |
| 71 | case LT_EV_BREAKPOINT: |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 72 | if (opt_d>0) { |
| 73 | output_line(0, "event: breakpoint"); |
| 74 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 75 | process_breakpoint(event); |
| 76 | return; |
| 77 | default: |
| 78 | fprintf(stderr, "Error! unknown event?\n"); |
| 79 | exit(1); |
| 80 | } |
| 81 | } |
| 82 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 83 | static char * |
| 84 | shortsignal(int signum) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 85 | static char * signalent0[] = { |
| 86 | #include "signalent.h" |
| 87 | }; |
| 88 | int nsignals0 = sizeof signalent0 / sizeof signalent0[0]; |
| 89 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 90 | if (signum<0 || signum>=nsignals0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 91 | return "UNKNOWN_SIGNAL"; |
| 92 | } else { |
| 93 | return signalent0[signum]; |
| 94 | } |
| 95 | } |
| 96 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 97 | static char * |
| 98 | sysname(int sysnum) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 99 | static char result[128]; |
| 100 | static char * syscalent0[] = { |
| 101 | #include "syscallent.h" |
| 102 | }; |
| 103 | int nsyscals0 = sizeof syscalent0 / sizeof syscalent0[0]; |
| 104 | |
| 105 | if (sysnum<0 || sysnum>nsyscals0) { |
| 106 | sprintf(result, "SYS_%d", sysnum); |
| 107 | return result; |
| 108 | } else { |
| 109 | sprintf(result, "SYS_%s", syscalent0[sysnum]); |
| 110 | return result; |
| 111 | } |
| 112 | } |
| 113 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 114 | static void |
| 115 | process_signal(struct event * event) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 116 | if (exiting && event->e_un.signum == SIGSTOP) { |
| 117 | pid_t pid = event->proc->pid; |
| 118 | disable_all_breakpoints(event->proc); |
| 119 | untrace_pid(pid); |
| 120 | remove_proc(event->proc); |
| 121 | continue_after_signal(pid, event->e_un.signum); |
| 122 | return; |
| 123 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 124 | output_line(event->proc, "--- %s (%s) ---", |
| 125 | shortsignal(event->e_un.signum), strsignal(event->e_un.signum)); |
| 126 | continue_after_signal(event->proc->pid, event->e_un.signum); |
| 127 | } |
| 128 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 129 | static void |
| 130 | process_exit(struct event * event) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 131 | output_line(event->proc, "+++ exited (status %d) +++", |
| 132 | event->e_un.ret_val); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 133 | remove_proc(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 136 | static void |
| 137 | process_exit_signal(struct event * event) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 138 | output_line(event->proc, "+++ killed by %s +++", |
| 139 | shortsignal(event->e_un.signum)); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 140 | remove_proc(event->proc); |
| 141 | } |
| 142 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 143 | static void |
| 144 | remove_proc(struct process * proc) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 145 | struct process *tmp, *tmp2; |
| 146 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 147 | if (opt_d) { |
| 148 | output_line(0,"Removing pid %u\n", proc->pid); |
| 149 | } |
| 150 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 151 | if (list_of_processes == proc) { |
| 152 | tmp = list_of_processes; |
| 153 | list_of_processes = list_of_processes->next; |
| 154 | free(tmp); |
| 155 | return; |
| 156 | } |
| 157 | tmp = list_of_processes; |
| 158 | while(tmp->next) { |
| 159 | if (tmp->next==proc) { |
| 160 | tmp2 = tmp->next; |
| 161 | tmp->next = tmp->next->next; |
| 162 | free(tmp2); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 163 | continue; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 164 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 165 | tmp = tmp->next; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 166 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 169 | static void |
| 170 | process_syscall(struct event * event) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 171 | if (opt_S) { |
| 172 | output_left(LT_TOF_SYSCALL, event->proc, sysname(event->e_un.sysnum)); |
| 173 | } |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 174 | callstack_push_syscall(event->proc, event->e_un.sysnum); |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 175 | if (fork_p(event->e_un.sysnum) || exec_p(event->e_un.sysnum)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 176 | disable_all_breakpoints(event->proc); |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 177 | } else if (!event->proc->breakpoints_enabled) { |
| 178 | enable_all_breakpoints(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 179 | } |
| 180 | continue_process(event->proc->pid); |
| 181 | } |
| 182 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 183 | static void |
| 184 | process_sysret(struct event * event) { |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 185 | if (exec_p(event->e_un.sysnum)) { |
| 186 | if (gimme_arg(LT_TOF_SYSCALL,event->proc,-1)==0) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 187 | |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 188 | event->proc->filename = pid2name(event->proc->pid); |
| 189 | event->proc->list_of_symbols = read_elf(event->proc->filename); |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 190 | |
| 191 | /* The kernel will stop the process just after an execve() |
| 192 | * and we will be able to enable breakpoints again |
| 193 | */ |
| Juan Cespedes | 53eabb1 | 2001-12-10 04:11:39 +0100 | [diff] [blame] | 194 | event->proc->breakpoints_enabled = -1; |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | if (fork_p(event->e_un.sysnum)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 198 | if (opt_f) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 199 | pid_t child = gimme_arg(LT_TOF_SYSCALL,event->proc,-1); |
| 200 | if (child>0) { |
| 201 | open_pid(child, 0); |
| 202 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 203 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 204 | enable_all_breakpoints(event->proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 205 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 206 | callstack_pop(event->proc); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 207 | if (opt_S) { |
| 208 | output_right(LT_TOF_SYSCALL, event->proc, sysname(event->e_un.sysnum)); |
| 209 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 210 | continue_process(event->proc->pid); |
| 211 | } |
| 212 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 213 | static void |
| 214 | process_breakpoint(struct event * event) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 215 | struct library_symbol * tmp; |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 216 | int i,j; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 217 | |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 218 | if (opt_d>1) { |
| 219 | output_line(0,"event: breakpoint (0x%08x)", event->e_un.brk_addr); |
| 220 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 221 | if (event->proc->breakpoint_being_enabled) { |
| Juan Cespedes | b1dd77d | 2002-03-03 00:22:06 +0100 | [diff] [blame] | 222 | /* Reinsert breakpoint */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 223 | continue_enabling_breakpoint(event->proc->pid, event->proc->breakpoint_being_enabled); |
| 224 | event->proc->breakpoint_being_enabled = NULL; |
| 225 | return; |
| 226 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 227 | |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 228 | for(i=event->proc->callstack_depth-1; i>=0; i--) { |
| 229 | if (event->e_un.brk_addr == event->proc->callstack[i].return_addr) { |
| 230 | for(j=event->proc->callstack_depth-1; j>=i; j--) { |
| 231 | callstack_pop(event->proc); |
| 232 | } |
| 233 | event->proc->return_addr = event->e_un.brk_addr; |
| 234 | output_right(LT_TOF_FUNCTION, event->proc, |
| 235 | event->proc->callstack[i].c_un.libfunc->name); |
| 236 | continue_after_breakpoint(event->proc, |
| 237 | address2bpstruct(event->proc, event->e_un.brk_addr)); |
| 238 | return; |
| 239 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | tmp = event->proc->list_of_symbols; |
| 243 | while(tmp) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 244 | if (event->e_un.brk_addr == tmp->enter_addr) { |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 245 | event->proc->stack_pointer = get_stack_pointer(event->proc->pid); |
| 246 | event->proc->return_addr = get_return_addr(event->proc->pid, event->proc->stack_pointer); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 247 | output_left(LT_TOF_FUNCTION, event->proc, tmp->name); |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 248 | callstack_push_symfunc(event->proc, tmp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 249 | continue_after_breakpoint(event->proc, address2bpstruct(event->proc, tmp->enter_addr)); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | tmp = tmp->next; |
| 253 | } |
| 254 | output_line(event->proc, "breakpointed at 0x%08x (?)", |
| 255 | (unsigned)event->e_un.brk_addr); |
| 256 | continue_process(event->proc->pid); |
| 257 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 258 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 259 | static void |
| 260 | callstack_push_syscall(struct process * proc, int sysnum) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 261 | struct callstack_element * elem; |
| 262 | |
| 263 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| 264 | if (proc->callstack_depth == MAX_CALLDEPTH-1) { |
| 265 | fprintf(stderr, "Error: call nesting too deep!\n"); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | elem = & proc->callstack[proc->callstack_depth]; |
| 270 | elem->is_syscall = 1; |
| 271 | elem->c_un.syscall = sysnum; |
| 272 | elem->return_addr = NULL; |
| 273 | |
| 274 | proc->callstack_depth++; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 277 | static void |
| 278 | callstack_push_symfunc(struct process * proc, struct library_symbol * sym) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 279 | struct callstack_element * elem; |
| 280 | |
| 281 | /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */ |
| 282 | if (proc->callstack_depth == MAX_CALLDEPTH-1) { |
| 283 | fprintf(stderr, "Error: call nesting too deep!\n"); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | elem = & proc->callstack[proc->callstack_depth]; |
| 288 | elem->is_syscall = 0; |
| 289 | elem->c_un.libfunc = sym; |
| 290 | |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 291 | elem->return_addr = proc->return_addr; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 292 | insert_breakpoint(proc, elem->return_addr); |
| 293 | |
| 294 | proc->callstack_depth++; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 297 | static void |
| 298 | callstack_pop(struct process * proc) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 299 | struct callstack_element * elem; |
| 300 | assert(proc->callstack_depth > 0); |
| 301 | |
| 302 | elem = & proc->callstack[proc->callstack_depth-1]; |
| 303 | if (!elem->is_syscall) { |
| 304 | delete_breakpoint(proc, elem->return_addr); |
| 305 | } |
| 306 | proc->callstack_depth--; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 307 | } |