blob: 86788e0c42ddcf60a1e80f5f2f29232f015ec4b1 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedes5e01f651998-03-08 22:31:44 +01003#define _GNU_SOURCE
4#include <stdio.h>
5#include <string.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01006#include <stdlib.h>
Juan Cespedes28f60191998-04-12 00:04:39 +02007#include <signal.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02008#include <assert.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +01009#include <sys/time.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010010
Juan Cespedesf7281232009-06-25 16:11:21 +020011#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
Juan Cespedesf1bfe202002-03-27 00:22:23 +010013#ifdef __powerpc__
14#include <sys/ptrace.h>
15#endif
16
Juan Cespedes03192f82009-07-03 10:16:22 +020017static void handle_signal(Event *event);
18static void handle_exit(Event *event);
19static void handle_exit_signal(Event *event);
20static void handle_syscall(Event *event);
21static void handle_arch_syscall(Event *event);
22static void handle_sysret(Event *event);
23static void handle_arch_sysret(Event *event);
24static void handle_clone(Event *event);
25static void handle_exec(Event *event);
26static void handle_breakpoint(Event *event);
27static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010028
Juan Cespedesa8909f72009-04-28 20:02:41 +020029static void callstack_push_syscall(Process *proc, int sysnum);
30static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020032static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020033
Juan Cespedes61da3372009-07-03 11:55:44 +020034static char * shortsignal(Process *proc, int signum);
35static char * sysname(Process *proc, int sysnum);
36static char * arch_sysname(Process *proc, int sysnum);
37
Petr Machatacbe29c62011-09-27 02:27:58 +020038static Event *
39call_handler(Process * proc, Event * event)
40{
41 assert(proc != NULL);
42
43 Event_Handler * handler = proc->event_handler;
44 if (handler == NULL)
45 return event;
46
47 return (*handler->on_event) (handler, event);
48}
49
Juan Cespedes61da3372009-07-03 11:55:44 +020050void
51handle_event(Event *event) {
Petr Machata602330f2011-07-09 11:15:34 +020052 if (exiting == 1) {
53 exiting = 2;
54 debug(1, "ltrace about to exit");
55 ltrace_exiting();
56 }
Petr Machata26627682011-07-08 18:15:32 +020057 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
58 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020059
60 /* If the thread group or an individual task define an
61 overriding event handler, give them a chance to kick in.
62 We will end up calling both handlers, if the first one
63 doesn't sink the event. */
64 if (event->proc != NULL) {
65 event = call_handler(event->proc, event);
66 if (event == NULL)
67 /* It was handled. */
68 return;
69
70 /* Note: the previous handler has a chance to alter
71 * the event. */
72 if (event->proc->leader != NULL) {
73 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020074 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020075 return;
76 }
77 }
78
Juan Cespedes61da3372009-07-03 11:55:44 +020079 switch (event->type) {
80 case EVENT_NONE:
81 debug(1, "event: none");
82 return;
83 case EVENT_SIGNAL:
84 debug(1, "event: signal (%s [%d])",
85 shortsignal(event->proc, event->e_un.signum),
86 event->e_un.signum);
87 handle_signal(event);
88 return;
89 case EVENT_EXIT:
90 debug(1, "event: exit (%d)", event->e_un.ret_val);
91 handle_exit(event);
92 return;
93 case EVENT_EXIT_SIGNAL:
94 debug(1, "event: exit signal (%s [%d])",
95 shortsignal(event->proc, event->e_un.signum),
96 event->e_un.signum);
97 handle_exit_signal(event);
98 return;
99 case EVENT_SYSCALL:
100 debug(1, "event: syscall (%s [%d])",
101 sysname(event->proc, event->e_un.sysnum),
102 event->e_un.sysnum);
103 handle_syscall(event);
104 return;
105 case EVENT_SYSRET:
106 debug(1, "event: sysret (%s [%d])",
107 sysname(event->proc, event->e_un.sysnum),
108 event->e_un.sysnum);
109 handle_sysret(event);
110 return;
111 case EVENT_ARCH_SYSCALL:
112 debug(1, "event: arch_syscall (%s [%d])",
113 arch_sysname(event->proc, event->e_un.sysnum),
114 event->e_un.sysnum);
115 handle_arch_syscall(event);
116 return;
117 case EVENT_ARCH_SYSRET:
118 debug(1, "event: arch_sysret (%s [%d])",
119 arch_sysname(event->proc, event->e_un.sysnum),
120 event->e_un.sysnum);
121 handle_arch_sysret(event);
122 return;
123 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200124 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200125 debug(1, "event: clone (%u)", event->e_un.newpid);
126 handle_clone(event);
127 return;
128 case EVENT_EXEC:
129 debug(1, "event: exec()");
130 handle_exec(event);
131 return;
132 case EVENT_BREAKPOINT:
133 debug(1, "event: breakpoint");
134 handle_breakpoint(event);
135 return;
136 case EVENT_NEW:
137 debug(1, "event: new process");
138 handle_new(event);
139 return;
140 default:
141 fprintf(stderr, "Error! unknown event?\n");
142 exit(1);
143 }
144}
145
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200146/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200147static void *
148address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200149 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200150 return addr;
151}
152
Juan Cespedes61da3372009-07-03 11:55:44 +0200153static void *
154breakpoint_clone(void * bp) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200155 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200156 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200157 b = malloc(sizeof(Breakpoint));
158 if (!b) {
159 perror("malloc()");
160 exit(1);
161 }
162 memcpy(b, bp, sizeof(Breakpoint));
163 return b;
164}
165
166typedef struct Pending_New Pending_New;
167struct Pending_New {
168 pid_t pid;
169 Pending_New * next;
170};
171static Pending_New * pending_news = NULL;
172
173static int
174pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200175 Pending_New * p;
176
177 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
178
179 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200180 while (p) {
181 if (p->pid == pid) {
182 return 1;
183 }
184 p = p->next;
185 }
186 return 0;
187}
188
189static void
190pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200191 Pending_New * p;
192
193 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
194
195 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200196 if (!p) {
197 perror("malloc()");
198 exit(1);
199 }
200 p->pid = pid;
201 p->next = pending_news;
202 pending_news = p;
203}
204
205static void
206pending_new_remove(pid_t pid) {
207 Pending_New *p, *pred;
208
Juan Cespedescd8976d2009-05-14 13:47:58 +0200209 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
210
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200211 p = pending_news;
212 if (p->pid == pid) {
213 pending_news = p->next;
214 free(p);
215 } else {
216 while (p) {
217 if (p->pid == pid) {
218 pred->next = p->next;
219 free(p);
220 }
221 pred = p;
222 p = p->next;
223 }
224 }
225}
226
227static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200228handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200229 Process *p;
230
Juan Cespedes03192f82009-07-03 10:16:22 +0200231 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200232
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200233 p = malloc(sizeof(Process));
234 if (!p) {
235 perror("malloc()");
236 exit(1);
237 }
238 memcpy(p, event->proc, sizeof(Process));
239 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
240 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200241 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200242
Petr Machata75dcf7d2011-10-06 14:30:19 +0200243 /* We save register values to the arch pointer, and these need
244 to be per-thread. */
245 p->arch_ptr = NULL;
246
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200247 if (pending_new(p->pid)) {
248 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200249 if (p->event_handler != NULL)
250 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200251 if (event->proc->state == STATE_ATTACHED && options.follow) {
252 p->state = STATE_ATTACHED;
253 } else {
254 p->state = STATE_IGNORED;
255 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200256 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200257 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200258 } else {
259 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200260 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200261 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200262
263 if (event->type == EVENT_VFORK)
264 continue_after_vfork(p);
265 else
266 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200267}
268
269static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200270handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200271 Process * proc;
272
Juan Cespedes03192f82009-07-03 10:16:22 +0200273 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200274
275 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200276 if (!proc) {
277 pending_new_insert(event->e_un.newpid);
278 } else {
279 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200280 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200281 proc->state = STATE_ATTACHED;
282 } else {
283 proc->state = STATE_IGNORED;
284 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200285 continue_process(proc->pid);
286 }
287}
288
Juan Cespedesf1350522008-12-16 18:19:58 +0100289static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200290shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 static char *signalent0[] = {
292#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 static char *signalent1[] = {
295#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100296 };
297 static char **signalents[] = { signalent0, signalent1 };
298 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 sizeof signalent1 / sizeof signalent1[0]
300 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100301
Juan Cespedescd8976d2009-05-14 13:47:58 +0200302 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
303
Ian Wienand9a2ad352006-02-20 22:44:45 +0100304 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100306 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100307 return "UNKNOWN_SIGNAL";
308 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100309 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100310 }
311}
312
Juan Cespedesf1350522008-12-16 18:19:58 +0100313static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200314sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100315 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 static char *syscalent0[] = {
317#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100318 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 static char *syscalent1[] = {
320#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100321 };
322 static char **syscalents[] = { syscalent0, syscalent1 };
323 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 sizeof syscalent1 / sizeof syscalent1[0]
325 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100326
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
328
Ian Wienand9a2ad352006-02-20 22:44:45 +0100329 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100331 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332 sprintf(result, "SYS_%d", sysnum);
333 return result;
334 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 sprintf(result, "SYS_%s",
336 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337 return result;
338 }
339}
340
Juan Cespedesf1350522008-12-16 18:19:58 +0100341static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200342arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100343 static char result[128];
344 static char *arch_syscalent[] = {
345#include "arch_syscallent.h"
346 };
347 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
348
Juan Cespedescd8976d2009-05-14 13:47:58 +0200349 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
350
Juan Cespedes63184be2008-12-10 13:30:12 +0100351 if (sysnum < 0 || sysnum >= nsyscals) {
352 sprintf(result, "ARCH_%d", sysnum);
353 return result;
354 } else {
355 sprintf(result, "ARCH_%s",
356 arch_syscalent[sysnum]);
357 return result;
358 }
359}
360
Juan Cespedesf1350522008-12-16 18:19:58 +0100361static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200362handle_signal(Event *event) {
363 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800364 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200365 output_line(event->proc, "--- %s (%s) ---",
366 shortsignal(event->proc, event->e_un.signum),
367 strsignal(event->e_un.signum));
368 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100369 continue_after_signal(event->proc->pid, event->e_un.signum);
370}
371
Juan Cespedesf1350522008-12-16 18:19:58 +0100372static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200373handle_exit(Event *event) {
374 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200375 if (event->proc->state != STATE_IGNORED) {
376 output_line(event->proc, "+++ exited (status %d) +++",
377 event->e_un.ret_val);
378 }
Petr Machatacebb8842011-07-09 11:14:11 +0200379 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100380}
381
Juan Cespedesf1350522008-12-16 18:19:58 +0100382static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200383handle_exit_signal(Event *event) {
384 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200385 if (event->proc->state != STATE_IGNORED) {
386 output_line(event->proc, "+++ killed by %s +++",
387 shortsignal(event->proc, event->e_un.signum));
388 }
Petr Machatacebb8842011-07-09 11:14:11 +0200389 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100390}
391
Juan Cespedesf1350522008-12-16 18:19:58 +0100392static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200393handle_syscall(Event *event) {
394 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200395 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100396 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200397 if (options.syscalls) {
398 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200399 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200400 }
401 if (event->proc->breakpoints_enabled == 0) {
402 enable_all_breakpoints(event->proc);
403 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100404 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100405 continue_process(event->proc->pid);
406}
407
Juan Cespedesf1350522008-12-16 18:19:58 +0100408static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200409handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200410 Process * proc = event->proc;
411 pid_t saved_pid;
412
Juan Cespedes03192f82009-07-03 10:16:22 +0200413 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200414 if (proc->state == STATE_IGNORED) {
415 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200416 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200417 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200418 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200419 output_line(proc, "--- Called exec() ---");
420 proc->mask_32bit = 0;
421 proc->personality = 0;
422 proc->arch_ptr = NULL;
423 free(proc->filename);
424 proc->filename = pid2name(proc->pid);
425 saved_pid = proc->pid;
426 proc->pid = 0;
Petr Machatac7585b62011-07-08 22:58:12 +0200427 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200428 proc->pid = saved_pid;
429 proc->callstack_depth = 0;
430 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200431}
432
433static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200434handle_arch_syscall(Event *event) {
435 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200436 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100437 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200438 if (options.syscalls) {
439 output_left(LT_TOF_SYSCALL, event->proc,
440 arch_sysname(event->proc, event->e_un.sysnum));
441 }
442 if (event->proc->breakpoints_enabled == 0) {
443 enable_all_breakpoints(event->proc);
444 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100445 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100446 continue_process(event->proc->pid);
447}
448
Juan Cespedesd65efa32003-02-03 00:22:30 +0100449struct timeval current_time_spent;
450
Juan Cespedesf1350522008-12-16 18:19:58 +0100451static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200452calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100453 struct timeval tv;
454 struct timezone tz;
455 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100457
Juan Cespedescd8976d2009-05-14 13:47:58 +0200458 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100460
461 gettimeofday(&tv, &tz);
462
463 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
464 if (tv.tv_usec >= elem->time_spent.tv_usec) {
465 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
466 } else {
467 diff.tv_sec++;
468 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
469 }
470 current_time_spent = diff;
471}
472
Juan Cespedesf1350522008-12-16 18:19:58 +0100473static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200474handle_sysret(Event *event) {
475 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200476 if (event->proc->state != STATE_IGNORED) {
477 if (opt_T || options.summary) {
478 calc_time_spent(event->proc);
479 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200480 if (options.syscalls) {
481 output_right(LT_TOF_SYSCALLR, event->proc,
482 sysname(event->proc, event->e_un.sysnum));
483 }
Petr Machata211f0882010-11-03 18:42:18 +0100484 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200485 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100486 continue_process(event->proc->pid);
487}
488
Juan Cespedesf1350522008-12-16 18:19:58 +0100489static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200490handle_arch_sysret(Event *event) {
491 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200492 if (event->proc->state != STATE_IGNORED) {
493 if (opt_T || options.summary) {
494 calc_time_spent(event->proc);
495 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200496 if (options.syscalls) {
497 output_right(LT_TOF_SYSCALLR, event->proc,
498 arch_sysname(event->proc, event->e_un.sysnum));
499 }
Petr Machata211f0882010-11-03 18:42:18 +0100500 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100501 }
502 continue_process(event->proc->pid);
503}
504
Petr Machata067322d2010-10-25 14:47:55 +0200505#ifdef __powerpc__
506void *get_count_register (Process *proc);
507#endif
508
Juan Cespedesf1350522008-12-16 18:19:58 +0100509static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200510handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200512 Breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200513 Process *leader = event->proc->leader;
514
515 /* The leader has terminated. */
516 if (leader == NULL) {
517 continue_process(event->proc->pid);
518 return;
519 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100520
Juan Cespedes03192f82009-07-03 10:16:22 +0200521 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200522 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100523
Paul Gilliam76c61f12006-06-14 06:55:21 +0200524#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100525 /* Need to skip following NOP's to prevent a fake function from being stacked. */
526 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200527 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100528 char nop_instruction[] = PPC_NOP;
529
Petr Machata9a5420c2011-07-09 11:21:23 +0200530 stub_bp = address2bpstruct(leader, event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100531
532 if (stub_bp) {
533 unsigned char *bp_instruction = stub_bp->orig_value;
534
535 if (memcmp(bp_instruction, nop_instruction,
536 PPC_NOP_LENGTH) == 0) {
537 if (stub_addr != (long) event->e_un.brk_addr) {
538 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
539 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200540 return;
541 }
542 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100543 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200544#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200545
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100546 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
547 if (event->e_un.brk_addr ==
548 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200549#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100550 /*
551 * PPC HACK! (XXX FIXME TODO)
552 * The PLT gets modified during the first call,
553 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100554 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100555 unsigned long a;
556 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100557 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200558 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200559
Paul Gilliam76c61f12006-06-14 06:55:21 +0200560 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100561 unsigned char break_insn[] = BREAKPOINT_VALUE;
562
Petr Machata9a5420c2011-07-09 11:21:23 +0200563 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100564 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100565 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
566 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100567
Paul Gilliam76c61f12006-06-14 06:55:21 +0200568 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100569 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100570 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200571 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100572 }
573 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200574 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200575 /* On powerpc, the breakpoint address
576 may end up being actual entry point
577 of the library symbol, not the PLT
578 address we computed. In that case,
579 sbp is NULL. */
580 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100581 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200582 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200583 }
Ian Wienand3219f322006-02-16 06:00:00 +0100584 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100585#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500586 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200587 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500588 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200589 assert(sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200590 addr = sym2addr(leader, sym);
591 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500592 if (sbp) {
593 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200594 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500595 }
596 } else {
597 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
598 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200599 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200600 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200601 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200602 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200603#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100605 callstack_pop(event->proc);
606 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200607 if (event->proc->state != STATE_IGNORED) {
608 if (opt_T || options.summary) {
609 calc_time_spent(event->proc);
610 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100611 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100612 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200613 if (event->proc->state != STATE_IGNORED) {
614 output_right(LT_TOF_FUNCTIONR, event->proc,
615 event->proc->callstack[i].c_un.libfunc->name);
616 }
Petr Machata211f0882010-11-03 18:42:18 +0100617 callstack_pop(event->proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200618 sbp = address2bpstruct(leader, event->e_un.brk_addr);
619 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100620 return;
621 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100622 }
623
Petr Machata9a5420c2011-07-09 11:21:23 +0200624 if ((sbp = address2bpstruct(leader, event->e_un.brk_addr))) {
625 if (sbp->libsym == NULL) {
626 continue_after_breakpoint(event->proc, sbp);
627 return;
628 }
629
Joe Damatof0bd98b2010-11-08 15:47:42 -0800630 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200631 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200632 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800633 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200634
Zachary T Welch97baa652010-12-08 13:34:03 -0800635 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200636 event->proc->stack_pointer = get_stack_pointer(event->proc);
637 event->proc->return_addr =
638 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200639 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100640 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200641 }
Paul Gilliambe320772006-04-24 22:06:23 +0200642#ifdef PLT_REINITALISATION_BP
643 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100644 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
645 0))
Petr Machata9a5420c2011-07-09 11:21:23 +0200646 reinitialize_breakpoints(leader);
Paul Gilliambe320772006-04-24 22:06:23 +0200647#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100648
649 continue_after_breakpoint(event->proc, sbp);
650 return;
651 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100652
Joe Damatofa2aefc2010-10-30 19:56:50 -0700653 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200654 output_line(event->proc, "unexpected breakpoint at %p",
655 (void *)event->e_un.brk_addr);
656 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100657 continue_process(event->proc->pid);
658}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200659
Juan Cespedesf1350522008-12-16 18:19:58 +0100660static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200661callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200663
Juan Cespedescd8976d2009-05-14 13:47:58 +0200664 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200665 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100666 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500667 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
668 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200669 return;
670 }
671
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100672 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200673 elem->is_syscall = 1;
674 elem->c_un.syscall = sysnum;
675 elem->return_addr = NULL;
676
677 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200678 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100679 struct timezone tz;
680 gettimeofday(&elem->time_spent, &tz);
681 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200682}
683
Juan Cespedes21c63a12001-07-07 20:56:56 +0200684static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200685callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500686 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200687
Juan Cespedescd8976d2009-05-14 13:47:58 +0200688 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200689 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100690 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500691 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
692 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200693 return;
694 }
695
Arnaud Patard26570082010-01-08 08:40:12 -0500696 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100697 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200698 elem->is_syscall = 0;
699 elem->c_un.libfunc = sym;
700
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200701 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200702 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200703 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200704 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200705
Arnaud Patard26570082010-01-08 08:40:12 -0500706 /* handle functions like atexit() on mips which have no return */
707 if (elem->return_addr != prev->return_addr)
708 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200709 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100710 struct timezone tz;
711 gettimeofday(&elem->time_spent, &tz);
712 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200713}
714
Juan Cespedesf1350522008-12-16 18:19:58 +0100715static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200716callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100717 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200718 assert(proc->callstack_depth > 0);
719
Juan Cespedescd8976d2009-05-14 13:47:58 +0200720 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100721 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200722 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200723 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200724 delete_breakpoint(proc, elem->return_addr);
725 }
Petr Machata211f0882010-11-03 18:42:18 +0100726 if (elem->arch_ptr != NULL) {
727 free(elem->arch_ptr);
728 elem->arch_ptr = NULL;
729 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200730 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200731}