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