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