blob: a6d167ecdbd92ec5a5d2be26d581722f32d2c6b5 [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
Juan Cespedes393f1d02009-05-07 11:13:54 +020023static void process_signal(Event *event);
24static void process_exit(Event *event);
25static void process_exit_signal(Event *event);
26static void process_syscall(Event *event);
27static void process_arch_syscall(Event *event);
28static void process_sysret(Event *event);
29static void process_arch_sysret(Event *event);
Juan Cespedes393f1d02009-05-07 11:13:54 +020030static void process_clone(Event *event);
31static void process_exec(Event *event);
32static void process_breakpoint(Event *event);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020033static void process_new(Event *event);
Juan Cespedesa8909f72009-04-28 20:02:41 +020034static void remove_proc(Process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010035
Juan Cespedesa8909f72009-04-28 20:02:41 +020036static void callstack_push_syscall(Process *proc, int sysnum);
37static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010038 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020039static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020040
Juan Cespedesbc8caf02009-05-07 19:38:38 +020041/* TODO */
42void * address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020043 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020044 return addr;
45}
46
47void * breakpoint_clone(void * bp) {
48 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +020049 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020050 b = malloc(sizeof(Breakpoint));
51 if (!b) {
52 perror("malloc()");
53 exit(1);
54 }
55 memcpy(b, bp, sizeof(Breakpoint));
56 return b;
57}
58
59typedef struct Pending_New Pending_New;
60struct Pending_New {
61 pid_t pid;
62 Pending_New * next;
63};
64static Pending_New * pending_news = NULL;
65
66static int
67pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020068 Pending_New * p;
69
70 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
71
72 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +020073 while (p) {
74 if (p->pid == pid) {
75 return 1;
76 }
77 p = p->next;
78 }
79 return 0;
80}
81
82static void
83pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020084 Pending_New * p;
85
86 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
87
88 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +020089 if (!p) {
90 perror("malloc()");
91 exit(1);
92 }
93 p->pid = pid;
94 p->next = pending_news;
95 pending_news = p;
96}
97
98static void
99pending_new_remove(pid_t pid) {
100 Pending_New *p, *pred;
101
Juan Cespedescd8976d2009-05-14 13:47:58 +0200102 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
103
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200104 p = pending_news;
105 if (p->pid == pid) {
106 pending_news = p->next;
107 free(p);
108 } else {
109 while (p) {
110 if (p->pid == pid) {
111 pred->next = p->next;
112 free(p);
113 }
114 pred = p;
115 p = p->next;
116 }
117 }
118}
119
120static void
121process_clone(Event * event) {
122 Process *p;
123
Juan Cespedescd8976d2009-05-14 13:47:58 +0200124 debug(DEBUG_FUNCTION, "process_clone(pid=%d)", event->proc->pid);
125
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200126 p = malloc(sizeof(Process));
127 if (!p) {
128 perror("malloc()");
129 exit(1);
130 }
131 memcpy(p, event->proc, sizeof(Process));
132 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
133 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200134 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200135
136 if (pending_new(p->pid)) {
137 pending_new_remove(p->pid);
138 if (p->breakpoint_being_enabled) {
139 enable_breakpoint(p->pid, p->breakpoint_being_enabled);
140 p->breakpoint_being_enabled = NULL;
141 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200142 if (event->proc->state == STATE_ATTACHED && options.follow) {
143 p->state = STATE_ATTACHED;
144 } else {
145 p->state = STATE_IGNORED;
146 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200147 continue_process(p->pid);
148 p->next = list_of_processes;
149 list_of_processes = p;
150 } else {
151 p->state = STATE_BEING_CREATED;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200152 p->next = list_of_processes;
153 list_of_processes = p;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200154 }
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200155 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200156}
157
158static void
159process_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200160 Process * proc;
161
162 debug(DEBUG_FUNCTION, "process_new(pid=%d)", event->e_un.newpid);
163
164 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200165 if (!proc) {
166 pending_new_insert(event->e_un.newpid);
167 } else {
168 assert(proc->state == STATE_BEING_CREATED);
169 if (proc->breakpoint_being_enabled) {
170 enable_breakpoint(proc->pid, proc->breakpoint_being_enabled);
171 proc->breakpoint_being_enabled = NULL;
172 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200173 if (proc->parent->state == STATE_ATTACHED && options.follow) {
174 proc->state = STATE_ATTACHED;
175 } else {
176 proc->state = STATE_IGNORED;
177 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200178 continue_process(proc->pid);
179 }
180}
181
Juan Cespedesf1350522008-12-16 18:19:58 +0100182static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200183shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100184 static char *signalent0[] = {
185#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100187 static char *signalent1[] = {
188#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100189 };
190 static char **signalents[] = { signalent0, signalent1 };
191 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100192 sizeof signalent1 / sizeof signalent1[0]
193 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100194
Juan Cespedescd8976d2009-05-14 13:47:58 +0200195 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
196
Ian Wienand9a2ad352006-02-20 22:44:45 +0100197 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100199 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 return "UNKNOWN_SIGNAL";
201 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100202 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 }
204}
205
Juan Cespedesf1350522008-12-16 18:19:58 +0100206static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200207sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 static char *syscalent0[] = {
210#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100211 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 static char *syscalent1[] = {
213#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100214 };
215 static char **syscalents[] = { syscalent0, syscalent1 };
216 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 sizeof syscalent1 / sizeof syscalent1[0]
218 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219
Juan Cespedescd8976d2009-05-14 13:47:58 +0200220 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
221
Ian Wienand9a2ad352006-02-20 22:44:45 +0100222 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100224 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100225 sprintf(result, "SYS_%d", sysnum);
226 return result;
227 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 sprintf(result, "SYS_%s",
229 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100230 return result;
231 }
232}
233
Juan Cespedesf1350522008-12-16 18:19:58 +0100234static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200235arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100236 static char result[128];
237 static char *arch_syscalent[] = {
238#include "arch_syscallent.h"
239 };
240 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
241
Juan Cespedescd8976d2009-05-14 13:47:58 +0200242 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
243
Juan Cespedes63184be2008-12-10 13:30:12 +0100244 if (sysnum < 0 || sysnum >= nsyscals) {
245 sprintf(result, "ARCH_%d", sysnum);
246 return result;
247 } else {
248 sprintf(result, "ARCH_%s",
249 arch_syscalent[sysnum]);
250 return result;
251 }
252}
253
Juan Cespedesf1350522008-12-16 18:19:58 +0100254void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200255process_event(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200256 debug(DEBUG_FUNCTION, "process_event(pid=%d, type=%d)", event->proc ? event->proc->pid : -1, event->type);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200257 switch (event->type) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200258 case EVENT_NONE:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 debug(1, "event: none");
260 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200261 case EVENT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 debug(1, "event: signal (%s [%d])",
263 shortsignal(event->proc, event->e_un.signum),
264 event->e_un.signum);
265 process_signal(event);
266 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200267 case EVENT_EXIT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 debug(1, "event: exit (%d)", event->e_un.ret_val);
269 process_exit(event);
270 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200271 case EVENT_EXIT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 debug(1, "event: exit signal (%s [%d])",
273 shortsignal(event->proc, event->e_un.signum),
274 event->e_un.signum);
275 process_exit_signal(event);
276 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200277 case EVENT_SYSCALL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 debug(1, "event: syscall (%s [%d])",
279 sysname(event->proc, event->e_un.sysnum),
280 event->e_un.sysnum);
281 process_syscall(event);
282 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200283 case EVENT_SYSRET:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 debug(1, "event: sysret (%s [%d])",
285 sysname(event->proc, event->e_un.sysnum),
286 event->e_un.sysnum);
287 process_sysret(event);
288 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200289 case EVENT_ARCH_SYSCALL:
Juan Cespedes63184be2008-12-10 13:30:12 +0100290 debug(1, "event: arch_syscall (%s [%d])",
291 arch_sysname(event->proc, event->e_un.sysnum),
292 event->e_un.sysnum);
293 process_arch_syscall(event);
294 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200295 case EVENT_ARCH_SYSRET:
Juan Cespedes63184be2008-12-10 13:30:12 +0100296 debug(1, "event: arch_sysret (%s [%d])",
297 arch_sysname(event->proc, event->e_un.sysnum),
298 event->e_un.sysnum);
299 process_arch_sysret(event);
300 return;
Juan Cespedes1e583132009-04-07 18:17:11 +0200301 case EVENT_CLONE:
302 debug(1, "event: clone (%u)", event->e_un.newpid);
303 process_clone(event);
304 return;
305 case EVENT_EXEC:
306 debug(1, "event: exec()");
307 process_exec(event);
308 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200309 case EVENT_BREAKPOINT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100310 debug(1, "event: breakpoint");
311 process_breakpoint(event);
312 return;
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200313 case EVENT_NEW:
314 debug(1, "event: new process");
315 process_new(event);
316 return;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100317 default:
318 fprintf(stderr, "Error! unknown event?\n");
319 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200320 }
321}
322
Juan Cespedesf1350522008-12-16 18:19:58 +0100323static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200324process_signal(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200325 debug(DEBUG_FUNCTION, "process_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes28f60191998-04-12 00:04:39 +0200326 if (exiting && event->e_un.signum == SIGSTOP) {
327 pid_t pid = event->proc->pid;
328 disable_all_breakpoints(event->proc);
329 untrace_pid(pid);
330 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200331 return;
332 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200333 if (event->proc->state != STATE_IGNORED) {
334 output_line(event->proc, "--- %s (%s) ---",
335 shortsignal(event->proc, event->e_un.signum),
336 strsignal(event->e_un.signum));
337 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100338 continue_after_signal(event->proc->pid, event->e_un.signum);
339}
340
Juan Cespedesf1350522008-12-16 18:19:58 +0100341static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200342process_exit(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200343 debug(DEBUG_FUNCTION, "process_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200344 if (event->proc->state != STATE_IGNORED) {
345 output_line(event->proc, "+++ exited (status %d) +++",
346 event->e_un.ret_val);
347 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100348 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100349}
350
Juan Cespedesf1350522008-12-16 18:19:58 +0100351static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200352process_exit_signal(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200353 debug(DEBUG_FUNCTION, "process_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200354 if (event->proc->state != STATE_IGNORED) {
355 output_line(event->proc, "+++ killed by %s +++",
356 shortsignal(event->proc, event->e_un.signum));
357 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100358 remove_proc(event->proc);
359}
360
Juan Cespedesf1350522008-12-16 18:19:58 +0100361static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200362remove_proc(Process *proc) {
363 Process *tmp, *tmp2;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100364
Juan Cespedescd8976d2009-05-14 13:47:58 +0200365 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200366
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100367 if (list_of_processes == proc) {
368 tmp = list_of_processes;
369 list_of_processes = list_of_processes->next;
370 free(tmp);
371 return;
372 }
373 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 while (tmp->next) {
375 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100376 tmp2 = tmp->next;
377 tmp->next = tmp->next->next;
378 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200379 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100380 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100381 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100382 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100383}
384
Juan Cespedesf1350522008-12-16 18:19:58 +0100385static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200386process_syscall(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200387 debug(DEBUG_FUNCTION, "process_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200388 if (event->proc->state != STATE_IGNORED) {
389 if (options.syscalls) {
390 output_left(LT_TOF_SYSCALL, event->proc,
391 sysname(event->proc, event->e_un.sysnum));
392 }
393 if (event->proc->breakpoints_enabled == 0) {
394 enable_all_breakpoints(event->proc);
395 }
396 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100397 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100398 continue_process(event->proc->pid);
399}
400
Juan Cespedesf1350522008-12-16 18:19:58 +0100401static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200402process_exec(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200403 debug(DEBUG_FUNCTION, "process_exec(pid=%d)", event->proc->pid);
Juan Cespedes5c682042009-05-21 15:59:56 +0200404 if (event->proc->state == STATE_IGNORED) {
405 untrace_pid(event->proc->pid);
406 remove_proc(event->proc);
407 }
408 /* TODO */
Juan Cespedes1e583132009-04-07 18:17:11 +0200409 output_line(event->proc, "--- exec() ---");
410 abort();
411}
412
413static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200414process_arch_syscall(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200415 debug(DEBUG_FUNCTION, "process_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200416 if (event->proc->state != STATE_IGNORED) {
417 if (options.syscalls) {
418 output_left(LT_TOF_SYSCALL, event->proc,
419 arch_sysname(event->proc, event->e_un.sysnum));
420 }
421 if (event->proc->breakpoints_enabled == 0) {
422 enable_all_breakpoints(event->proc);
423 }
424 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes63184be2008-12-10 13:30:12 +0100425 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100426 continue_process(event->proc->pid);
427}
428
Juan Cespedesd65efa32003-02-03 00:22:30 +0100429struct timeval current_time_spent;
430
Juan Cespedesf1350522008-12-16 18:19:58 +0100431static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200432calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100433 struct timeval tv;
434 struct timezone tz;
435 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100437
Juan Cespedescd8976d2009-05-14 13:47:58 +0200438 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100440
441 gettimeofday(&tv, &tz);
442
443 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
444 if (tv.tv_usec >= elem->time_spent.tv_usec) {
445 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
446 } else {
447 diff.tv_sec++;
448 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
449 }
450 current_time_spent = diff;
451}
452
Juan Cespedesf1350522008-12-16 18:19:58 +0100453static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200454process_sysret(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200455 debug(DEBUG_FUNCTION, "process_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200456 if (event->proc->state != STATE_IGNORED) {
457 if (opt_T || options.summary) {
458 calc_time_spent(event->proc);
459 }
460 callstack_pop(event->proc);
461 if (options.syscalls) {
462 output_right(LT_TOF_SYSCALLR, event->proc,
463 sysname(event->proc, event->e_un.sysnum));
464 }
Juan Cespedes21c63a12001-07-07 20:56:56 +0200465 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100466 continue_process(event->proc->pid);
467}
468
Juan Cespedesf1350522008-12-16 18:19:58 +0100469static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200470process_arch_sysret(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200471 debug(DEBUG_FUNCTION, "process_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200472 if (event->proc->state != STATE_IGNORED) {
473 if (opt_T || options.summary) {
474 calc_time_spent(event->proc);
475 }
476 callstack_pop(event->proc);
477 if (options.syscalls) {
478 output_right(LT_TOF_SYSCALLR, event->proc,
479 arch_sysname(event->proc, event->e_un.sysnum));
480 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100481 }
482 continue_process(event->proc->pid);
483}
484
Juan Cespedesf1350522008-12-16 18:19:58 +0100485static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200486process_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100487 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200488 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100489
Juan Cespedescd8976d2009-05-14 13:47:58 +0200490 debug(DEBUG_FUNCTION, "process_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200491 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100492
Paul Gilliam76c61f12006-06-14 06:55:21 +0200493#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100494 /* Need to skip following NOP's to prevent a fake function from being stacked. */
495 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200496 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100497 char nop_instruction[] = PPC_NOP;
498
499 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
500
501 if (stub_bp) {
502 unsigned char *bp_instruction = stub_bp->orig_value;
503
504 if (memcmp(bp_instruction, nop_instruction,
505 PPC_NOP_LENGTH) == 0) {
506 if (stub_addr != (long) event->e_un.brk_addr) {
507 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
508 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200509 return;
510 }
511 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100512 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200513#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100514 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100515 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100516 continue_enabling_breakpoint(event->proc->pid,
517 event->proc->
518 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100519 event->proc->breakpoint_being_enabled = NULL;
520 return;
521 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200522
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100523 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
524 if (event->e_un.brk_addr ==
525 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200526#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100527 /*
528 * PPC HACK! (XXX FIXME TODO)
529 * The PLT gets modified during the first call,
530 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100531 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100532 unsigned long a;
533 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100534 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200535 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200536
Paul Gilliam76c61f12006-06-14 06:55:21 +0200537 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100538 unsigned char break_insn[] = BREAKPOINT_VALUE;
539
540 sbp = address2bpstruct(event->proc, addr);
541 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100542 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
543 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100544
Paul Gilliam76c61f12006-06-14 06:55:21 +0200545 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100546 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100547 insert_breakpoint(event->proc, addr,
548 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100549 }
550 } else {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200551 sbp = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, libsym));
Ian Wienand9a2ad352006-02-20 22:44:45 +0100552 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200553 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100554 insert_breakpoint(event->proc, addr,
555 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200556 }
Ian Wienand3219f322006-02-16 06:00:00 +0100557 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100558#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200559 void *addr;
560 void *old_addr;
561 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200562 assert(sym);
563 old_addr = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, sym))->addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200564 addr=sym2addr(event->proc,sym);
565 assert(old_addr !=0 && addr !=0);
566 if(addr != old_addr){
567 struct library_symbol *new_sym;
568 new_sym=malloc(sizeof(*new_sym));
569 memcpy(new_sym,sym,sizeof(*new_sym));
570 new_sym->next=event->proc->list_of_symbols;
571 event->proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200572 insert_breakpoint(event->proc, addr, new_sym);
573 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200574#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100575 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100576 callstack_pop(event->proc);
577 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200578 if (event->proc->state != STATE_IGNORED) {
579 if (opt_T || options.summary) {
580 calc_time_spent(event->proc);
581 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100582 }
583 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100584 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200585 if (event->proc->state != STATE_IGNORED) {
586 output_right(LT_TOF_FUNCTIONR, event->proc,
587 event->proc->callstack[i].c_un.libfunc->name);
588 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100589 continue_after_breakpoint(event->proc,
Juan Cespedes5c682042009-05-21 15:59:56 +0200590 address2bpstruct(event->proc,
591 event->e_un.brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100592 return;
593 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100594 }
595
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100596 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200597 if (event->proc->state != STATE_IGNORED) {
598 event->proc->stack_pointer = get_stack_pointer(event->proc);
599 event->proc->return_addr =
600 get_return_addr(event->proc, event->proc->stack_pointer);
601 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
602 callstack_push_symfunc(event->proc, sbp->libsym);
603 }
Paul Gilliambe320772006-04-24 22:06:23 +0200604#ifdef PLT_REINITALISATION_BP
605 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
607 0))
608 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200609#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610
611 continue_after_breakpoint(event->proc, sbp);
612 return;
613 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100614
Juan Cespedes5c682042009-05-21 15:59:56 +0200615 if (event->proc->state != STATE_IGNORED) {
616 output_line(event->proc, "unexpected breakpoint at %p",
617 (void *)event->e_un.brk_addr);
618 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100619 continue_process(event->proc->pid);
620}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200621
Juan Cespedesf1350522008-12-16 18:19:58 +0100622static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200623callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100624 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200625
Juan Cespedescd8976d2009-05-14 13:47:58 +0200626 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200627 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100628 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200629 fprintf(stderr, "Error: call nesting too deep!\n");
630 return;
631 }
632
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100633 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200634 elem->is_syscall = 1;
635 elem->c_un.syscall = sysnum;
636 elem->return_addr = NULL;
637
638 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200639 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100640 struct timezone tz;
641 gettimeofday(&elem->time_spent, &tz);
642 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200643}
644
Juan Cespedes21c63a12001-07-07 20:56:56 +0200645static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200646callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100647 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200648
Juan Cespedescd8976d2009-05-14 13:47:58 +0200649 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200650 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100651 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200652 fprintf(stderr, "Error: call nesting too deep!\n");
653 return;
654 }
655
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100656 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200657 elem->is_syscall = 0;
658 elem->c_un.libfunc = sym;
659
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200660 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200661 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200662 insert_breakpoint(proc, elem->return_addr, 0);
663 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200664
665 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200666 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100667 struct timezone tz;
668 gettimeofday(&elem->time_spent, &tz);
669 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200670}
671
Juan Cespedesf1350522008-12-16 18:19:58 +0100672static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200673callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100674 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200675 assert(proc->callstack_depth > 0);
676
Juan Cespedescd8976d2009-05-14 13:47:58 +0200677 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100678 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200679 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200680 delete_breakpoint(proc, elem->return_addr);
681 }
682 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200683}