blob: 1323d3948e2da72d02646a349d508262f9710c52 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#define _GNU_SOURCE
6#include <stdio.h>
7#include <string.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01008#include <stdlib.h>
Juan Cespedes28f60191998-04-12 00:04:39 +02009#include <signal.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020010#include <assert.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010011#include <sys/time.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
13#include "ltrace.h"
14#include "output.h"
15#include "options.h"
Juan Cespedes81690ef1998-03-13 19:31:29 +010016#include "elf.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010017#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Juan Cespedesf1bfe202002-03-27 00:22:23 +010019#ifdef __powerpc__
20#include <sys/ptrace.h>
21#endif
22
Ian Wienand3219f322006-02-16 06:00:00 +010023static void process_signal(struct event *event);
24static void process_exit(struct event *event);
25static void process_exit_signal(struct event *event);
26static void process_syscall(struct event *event);
27static void process_sysret(struct event *event);
28static void process_breakpoint(struct event *event);
29static void remove_proc(struct process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010030
Ian Wienand3219f322006-02-16 06:00:00 +010031static void callstack_push_syscall(struct process *proc, int sysnum);
32static void callstack_push_symfunc(struct process *proc,
33 struct library_symbol *sym);
34static void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035
Ian Wienand3219f322006-02-16 06:00:00 +010036static char *shortsignal(int signum)
37{
38 static char *signalent0[] = {
39#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010040 };
41 int nsignals0 = sizeof signalent0 / sizeof signalent0[0];
42
Juan Cespedes504a3852003-02-04 23:24:38 +010043 if (signum < 0 || signum >= nsignals0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010044 return "UNKNOWN_SIGNAL";
45 } else {
46 return signalent0[signum];
47 }
48}
49
Ian Wienand3219f322006-02-16 06:00:00 +010050static char *sysname(int sysnum)
51{
Juan Cespedes5e01f651998-03-08 22:31:44 +010052 static char result[128];
Ian Wienand3219f322006-02-16 06:00:00 +010053 static char *syscalent0[] = {
54#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010055 };
56 int nsyscals0 = sizeof syscalent0 / sizeof syscalent0[0];
57
Juan Cespedes504a3852003-02-04 23:24:38 +010058 if (sysnum < 0 || sysnum >= nsyscals0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010059 sprintf(result, "SYS_%d", sysnum);
60 return result;
61 } else {
62 sprintf(result, "SYS_%s", syscalent0[sysnum]);
63 return result;
64 }
65}
66
Ian Wienand3219f322006-02-16 06:00:00 +010067void process_event(struct event *event)
68{
Juan Cespedesefe85f02004-04-04 01:31:38 +020069 switch (event->thing) {
Ian Wienand3219f322006-02-16 06:00:00 +010070 case LT_EV_NONE:
71 debug(1, "event: none");
72 return;
73 case LT_EV_SIGNAL:
74 debug(1, "event: signal (%s [%d])",
75 shortsignal(event->e_un.signum), event->e_un.signum);
76 process_signal(event);
77 return;
78 case LT_EV_EXIT:
79 debug(1, "event: exit (%d)", event->e_un.ret_val);
80 process_exit(event);
81 return;
82 case LT_EV_EXIT_SIGNAL:
83 debug(1, "event: exit signal (%s [%d])",
84 shortsignal(event->e_un.signum), event->e_un.signum);
85 process_exit_signal(event);
86 return;
87 case LT_EV_SYSCALL:
88 debug(1, "event: syscall (%s [%d])",
89 sysname(event->e_un.sysnum), event->e_un.sysnum);
90 process_syscall(event);
91 return;
92 case LT_EV_SYSRET:
93 debug(1, "event: sysret (%s [%d])", sysname(event->e_un.sysnum),
94 event->e_un.sysnum);
95 process_sysret(event);
96 return;
97 case LT_EV_BREAKPOINT:
98 debug(1, "event: breakpoint");
99 process_breakpoint(event);
100 return;
101 default:
102 fprintf(stderr, "Error! unknown event?\n");
103 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200104 }
105}
106
Ian Wienand3219f322006-02-16 06:00:00 +0100107static void process_signal(struct event *event)
108{
Juan Cespedes28f60191998-04-12 00:04:39 +0200109 if (exiting && event->e_un.signum == SIGSTOP) {
110 pid_t pid = event->proc->pid;
111 disable_all_breakpoints(event->proc);
112 untrace_pid(pid);
113 remove_proc(event->proc);
114 continue_after_signal(pid, event->e_un.signum);
115 return;
116 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100117 output_line(event->proc, "--- %s (%s) ---",
Ian Wienand3219f322006-02-16 06:00:00 +0100118 shortsignal(event->e_un.signum),
119 strsignal(event->e_un.signum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120 continue_after_signal(event->proc->pid, event->e_un.signum);
121}
122
Ian Wienand3219f322006-02-16 06:00:00 +0100123static void process_exit(struct event *event)
124{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100125 output_line(event->proc, "+++ exited (status %d) +++",
Ian Wienand3219f322006-02-16 06:00:00 +0100126 event->e_un.ret_val);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100127 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100128}
129
Ian Wienand3219f322006-02-16 06:00:00 +0100130static void process_exit_signal(struct event *event)
131{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100132 output_line(event->proc, "+++ killed by %s +++",
Ian Wienand3219f322006-02-16 06:00:00 +0100133 shortsignal(event->e_un.signum));
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100134 remove_proc(event->proc);
135}
136
Ian Wienand3219f322006-02-16 06:00:00 +0100137static void remove_proc(struct process *proc)
138{
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100139 struct process *tmp, *tmp2;
140
Juan Cespedescac15c32003-01-31 18:58:58 +0100141 debug(1, "Removing pid %u\n", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200142
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100143 if (list_of_processes == proc) {
144 tmp = list_of_processes;
145 list_of_processes = list_of_processes->next;
146 free(tmp);
147 return;
148 }
149 tmp = list_of_processes;
Ian Wienand3219f322006-02-16 06:00:00 +0100150 while (tmp->next) {
151 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100152 tmp2 = tmp->next;
153 tmp->next = tmp->next->next;
154 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200155 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100156 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100157 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100158 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159}
160
Ian Wienand3219f322006-02-16 06:00:00 +0100161static void process_syscall(struct event *event)
162{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163 if (opt_S) {
Ian Wienand3219f322006-02-16 06:00:00 +0100164 output_left(LT_TOF_SYSCALL, event->proc,
165 sysname(event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100166 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100167 if (fork_p(event->e_un.sysnum)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100168 disable_all_breakpoints(event->proc);
Juan Cespedes81690ef1998-03-13 19:31:29 +0100169 } else if (!event->proc->breakpoints_enabled) {
170 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100172 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100173 continue_process(event->proc->pid);
174}
175
Juan Cespedesd65efa32003-02-03 00:22:30 +0100176struct timeval current_time_spent;
177
Ian Wienand3219f322006-02-16 06:00:00 +0100178static void calc_time_spent(struct process *proc)
179{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100180 struct timeval tv;
181 struct timezone tz;
182 struct timeval diff;
Ian Wienand3219f322006-02-16 06:00:00 +0100183 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100184
Ian Wienand3219f322006-02-16 06:00:00 +0100185 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100186
187 gettimeofday(&tv, &tz);
188
189 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
190 if (tv.tv_usec >= elem->time_spent.tv_usec) {
191 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
192 } else {
193 diff.tv_sec++;
194 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
195 }
196 current_time_spent = diff;
197}
198
Ian Wienand3219f322006-02-16 06:00:00 +0100199static void process_sysret(struct event *event)
200{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100201 if (opt_T || opt_c) {
202 calc_time_spent(event->proc);
203 }
Juan Cespedes81690ef1998-03-13 19:31:29 +0100204 if (fork_p(event->e_un.sysnum)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205 if (opt_f) {
Ian Wienand3219f322006-02-16 06:00:00 +0100206 pid_t child =
207 gimme_arg(LT_TOF_SYSCALLR, event->proc, -1);
208 if (child > 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100209 open_pid(child, 0);
210 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100211 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100212 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100213 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200214 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200215 if (opt_S) {
Ian Wienand3219f322006-02-16 06:00:00 +0100216 output_right(LT_TOF_SYSCALLR, event->proc,
217 sysname(event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200218 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100219 if (exec_p(event->e_un.sysnum)) {
Ian Wienand3219f322006-02-16 06:00:00 +0100220 if (gimme_arg(LT_TOF_SYSCALLR, event->proc, -1) == 0) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100221 event->proc->filename = pid2name(event->proc->pid);
222 breakpoints_init(event->proc);
223 }
224 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100225 continue_process(event->proc->pid);
226}
227
Ian Wienand3219f322006-02-16 06:00:00 +0100228static void process_breakpoint(struct event *event)
229{
230 struct library_symbol *tmp;
231 int i, j;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100232
Juan Cespedesefe85f02004-04-04 01:31:38 +0200233 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100234 if (event->proc->breakpoint_being_enabled) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100235 /* Reinsert breakpoint */
Ian Wienand3219f322006-02-16 06:00:00 +0100236 continue_enabling_breakpoint(event->proc->pid,
237 event->proc->
238 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100239 event->proc->breakpoint_being_enabled = NULL;
240 return;
241 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200242
Ian Wienand3219f322006-02-16 06:00:00 +0100243 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
244 if (event->e_un.brk_addr ==
245 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200246#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100247 unsigned long a;
248 unsigned long addr =
249 event->proc->callstack[i].c_un.libfunc->enter_addr;
250 struct breakpoint *sbp =
251 address2bpstruct(event->proc, addr);
252 unsigned char break_insn[] = BREAKPOINT_VALUE;
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200253
Ian Wienand3219f322006-02-16 06:00:00 +0100254 /*
255 * PPC HACK! (XXX FIXME TODO)
256 * The PLT gets modified during the first call,
257 * so be sure to re-enable the breakpoint.
258 */
259 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid, addr);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200260
Ian Wienand3219f322006-02-16 06:00:00 +0100261 if (memcmp(&a, break_insn, 4)) {
262 sbp->enabled--;
263 insert_breakpoint(event->proc, addr);
264 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200265#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100266 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100267 callstack_pop(event->proc);
268 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100269 if (opt_T || opt_c) {
270 calc_time_spent(event->proc);
271 }
272 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100273 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200274 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand3219f322006-02-16 06:00:00 +0100275 event->proc->callstack[i].c_un.libfunc->
276 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100277 continue_after_breakpoint(event->proc,
Ian Wienand3219f322006-02-16 06:00:00 +0100278 address2bpstruct(event->proc,
279 event->e_un.
280 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100281 return;
282 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 }
284
285 tmp = event->proc->list_of_symbols;
Ian Wienand3219f322006-02-16 06:00:00 +0100286 while (tmp) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200287 if (event->e_un.brk_addr == tmp->enter_addr) {
Ian Wienand3219f322006-02-16 06:00:00 +0100288 event->proc->stack_pointer =
289 get_stack_pointer(event->proc);
290 event->proc->return_addr =
291 get_return_addr(event->proc,
292 event->proc->stack_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293 output_left(LT_TOF_FUNCTION, event->proc, tmp->name);
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200294 callstack_push_symfunc(event->proc, tmp);
Ian Wienand3219f322006-02-16 06:00:00 +0100295 continue_after_breakpoint(event->proc,
296 address2bpstruct(event->proc,
297 tmp->
298 enter_addr));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100299 return;
300 }
301 tmp = tmp->next;
302 }
Juan Cespedesefe85f02004-04-04 01:31:38 +0200303 output_line(event->proc, "breakpointed at %p (?)",
Ian Wienand3219f322006-02-16 06:00:00 +0100304 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100305 continue_process(event->proc->pid);
306}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200307
Ian Wienand3219f322006-02-16 06:00:00 +0100308static void callstack_push_syscall(struct process *proc, int sysnum)
309{
310 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200311
312 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand3219f322006-02-16 06:00:00 +0100313 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200314 fprintf(stderr, "Error: call nesting too deep!\n");
315 return;
316 }
317
Ian Wienand3219f322006-02-16 06:00:00 +0100318 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200319 elem->is_syscall = 1;
320 elem->c_un.syscall = sysnum;
321 elem->return_addr = NULL;
322
323 proc->callstack_depth++;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100324 if (opt_T || opt_c) {
325 struct timezone tz;
326 gettimeofday(&elem->time_spent, &tz);
327 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200328}
329
Juan Cespedes21c63a12001-07-07 20:56:56 +0200330static void
Ian Wienand3219f322006-02-16 06:00:00 +0100331callstack_push_symfunc(struct process *proc, struct library_symbol *sym)
332{
333 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200334
335 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand3219f322006-02-16 06:00:00 +0100336 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200337 fprintf(stderr, "Error: call nesting too deep!\n");
338 return;
339 }
340
Ian Wienand3219f322006-02-16 06:00:00 +0100341 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200342 elem->is_syscall = 0;
343 elem->c_un.libfunc = sym;
344
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200345 elem->return_addr = proc->return_addr;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200346 insert_breakpoint(proc, elem->return_addr);
347
348 proc->callstack_depth++;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100349 if (opt_T || opt_c) {
350 struct timezone tz;
351 gettimeofday(&elem->time_spent, &tz);
352 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200353}
354
Ian Wienand3219f322006-02-16 06:00:00 +0100355static void callstack_pop(struct process *proc)
356{
357 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200358 assert(proc->callstack_depth > 0);
359
Ian Wienand3219f322006-02-16 06:00:00 +0100360 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200361 if (!elem->is_syscall) {
362 delete_breakpoint(proc, elem->return_addr);
363 }
364 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200365}