blob: 0aa40f77df27e4b867ef9c347177232e124b7cf2 [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
38void
39handle_event(Event *event) {
Petr Machata602330f2011-07-09 11:15:34 +020040 if (exiting == 1) {
41 exiting = 2;
42 debug(1, "ltrace about to exit");
43 ltrace_exiting();
44 }
Petr Machata26627682011-07-08 18:15:32 +020045 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
46 event->proc ? event->proc->pid : -1, event->type);
Petr Machata4007d742011-07-09 11:29:42 +020047 /* If the thread group defines an overriding event handler,
48 give it a chance to kick in. */
49 if (event->proc != NULL
50 && event->proc->leader != NULL) {
51 Event_Handler * handler = event->proc->leader->event_handler;
52 if (handler != NULL) {
53 event = (*handler->on_event) (handler, event);
54 if (event == NULL)
55 /* It was handled. */
56 return;
57 }
58 }
59
Juan Cespedes61da3372009-07-03 11:55:44 +020060 switch (event->type) {
61 case EVENT_NONE:
62 debug(1, "event: none");
63 return;
64 case EVENT_SIGNAL:
65 debug(1, "event: signal (%s [%d])",
66 shortsignal(event->proc, event->e_un.signum),
67 event->e_un.signum);
68 handle_signal(event);
69 return;
70 case EVENT_EXIT:
71 debug(1, "event: exit (%d)", event->e_un.ret_val);
72 handle_exit(event);
73 return;
74 case EVENT_EXIT_SIGNAL:
75 debug(1, "event: exit signal (%s [%d])",
76 shortsignal(event->proc, event->e_un.signum),
77 event->e_un.signum);
78 handle_exit_signal(event);
79 return;
80 case EVENT_SYSCALL:
81 debug(1, "event: syscall (%s [%d])",
82 sysname(event->proc, event->e_un.sysnum),
83 event->e_un.sysnum);
84 handle_syscall(event);
85 return;
86 case EVENT_SYSRET:
87 debug(1, "event: sysret (%s [%d])",
88 sysname(event->proc, event->e_un.sysnum),
89 event->e_un.sysnum);
90 handle_sysret(event);
91 return;
92 case EVENT_ARCH_SYSCALL:
93 debug(1, "event: arch_syscall (%s [%d])",
94 arch_sysname(event->proc, event->e_un.sysnum),
95 event->e_un.sysnum);
96 handle_arch_syscall(event);
97 return;
98 case EVENT_ARCH_SYSRET:
99 debug(1, "event: arch_sysret (%s [%d])",
100 arch_sysname(event->proc, event->e_un.sysnum),
101 event->e_un.sysnum);
102 handle_arch_sysret(event);
103 return;
104 case EVENT_CLONE:
105 debug(1, "event: clone (%u)", event->e_un.newpid);
106 handle_clone(event);
107 return;
108 case EVENT_EXEC:
109 debug(1, "event: exec()");
110 handle_exec(event);
111 return;
112 case EVENT_BREAKPOINT:
113 debug(1, "event: breakpoint");
114 handle_breakpoint(event);
115 return;
116 case EVENT_NEW:
117 debug(1, "event: new process");
118 handle_new(event);
119 return;
120 default:
121 fprintf(stderr, "Error! unknown event?\n");
122 exit(1);
123 }
124}
125
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200126/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200127static void *
128address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200129 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200130 return addr;
131}
132
Juan Cespedes61da3372009-07-03 11:55:44 +0200133static void *
134breakpoint_clone(void * bp) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200135 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200136 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200137 b = malloc(sizeof(Breakpoint));
138 if (!b) {
139 perror("malloc()");
140 exit(1);
141 }
142 memcpy(b, bp, sizeof(Breakpoint));
143 return b;
144}
145
146typedef struct Pending_New Pending_New;
147struct Pending_New {
148 pid_t pid;
149 Pending_New * next;
150};
151static Pending_New * pending_news = NULL;
152
153static int
154pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200155 Pending_New * p;
156
157 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
158
159 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200160 while (p) {
161 if (p->pid == pid) {
162 return 1;
163 }
164 p = p->next;
165 }
166 return 0;
167}
168
169static void
170pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200171 Pending_New * p;
172
173 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
174
175 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200176 if (!p) {
177 perror("malloc()");
178 exit(1);
179 }
180 p->pid = pid;
181 p->next = pending_news;
182 pending_news = p;
183}
184
185static void
186pending_new_remove(pid_t pid) {
187 Pending_New *p, *pred;
188
Juan Cespedescd8976d2009-05-14 13:47:58 +0200189 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
190
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200191 p = pending_news;
192 if (p->pid == pid) {
193 pending_news = p->next;
194 free(p);
195 } else {
196 while (p) {
197 if (p->pid == pid) {
198 pred->next = p->next;
199 free(p);
200 }
201 pred = p;
202 p = p->next;
203 }
204 }
205}
206
207static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200208handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200209 Process *p;
210
Juan Cespedes03192f82009-07-03 10:16:22 +0200211 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200212
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200213 p = malloc(sizeof(Process));
214 if (!p) {
215 perror("malloc()");
216 exit(1);
217 }
218 memcpy(p, event->proc, sizeof(Process));
219 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
220 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200221 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200222
Petr Machata75dcf7d2011-10-06 14:30:19 +0200223 /* We save register values to the arch pointer, and these need
224 to be per-thread. */
225 p->arch_ptr = NULL;
226
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200227 if (pending_new(p->pid)) {
228 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200229 if (p->event_handler != NULL)
230 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200231 if (event->proc->state == STATE_ATTACHED && options.follow) {
232 p->state = STATE_ATTACHED;
233 } else {
234 p->state = STATE_IGNORED;
235 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200236 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200237 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200238 } else {
239 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200240 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200241 }
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200242 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200243}
244
245static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200246handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200247 Process * proc;
248
Juan Cespedes03192f82009-07-03 10:16:22 +0200249 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200250
251 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200252 if (!proc) {
253 pending_new_insert(event->e_un.newpid);
254 } else {
255 assert(proc->state == STATE_BEING_CREATED);
Petr Machata4007d742011-07-09 11:29:42 +0200256 if (proc->event_handler != NULL)
257 destroy_event_handler(proc);
Juan Cespedes30439b42009-05-22 19:03:09 +0200258 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200259 proc->state = STATE_ATTACHED;
260 } else {
261 proc->state = STATE_IGNORED;
262 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200263 continue_process(proc->pid);
264 }
265}
266
Juan Cespedesf1350522008-12-16 18:19:58 +0100267static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200268shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 static char *signalent0[] = {
270#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100271 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 static char *signalent1[] = {
273#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100274 };
275 static char **signalents[] = { signalent0, signalent1 };
276 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 sizeof signalent1 / sizeof signalent1[0]
278 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279
Juan Cespedescd8976d2009-05-14 13:47:58 +0200280 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
281
Ian Wienand9a2ad352006-02-20 22:44:45 +0100282 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100284 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100285 return "UNKNOWN_SIGNAL";
286 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100287 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100288 }
289}
290
Juan Cespedesf1350522008-12-16 18:19:58 +0100291static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200292sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 static char *syscalent0[] = {
295#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100296 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100297 static char *syscalent1[] = {
298#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100299 };
300 static char **syscalents[] = { syscalent0, syscalent1 };
301 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100302 sizeof syscalent1 / sizeof syscalent1[0]
303 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304
Juan Cespedescd8976d2009-05-14 13:47:58 +0200305 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
306
Ian Wienand9a2ad352006-02-20 22:44:45 +0100307 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100309 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100310 sprintf(result, "SYS_%d", sysnum);
311 return result;
312 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313 sprintf(result, "SYS_%s",
314 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100315 return result;
316 }
317}
318
Juan Cespedesf1350522008-12-16 18:19:58 +0100319static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200320arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100321 static char result[128];
322 static char *arch_syscalent[] = {
323#include "arch_syscallent.h"
324 };
325 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
326
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
328
Juan Cespedes63184be2008-12-10 13:30:12 +0100329 if (sysnum < 0 || sysnum >= nsyscals) {
330 sprintf(result, "ARCH_%d", sysnum);
331 return result;
332 } else {
333 sprintf(result, "ARCH_%s",
334 arch_syscalent[sysnum]);
335 return result;
336 }
337}
338
Juan Cespedesf1350522008-12-16 18:19:58 +0100339static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200340handle_signal(Event *event) {
341 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800342 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200343 output_line(event->proc, "--- %s (%s) ---",
344 shortsignal(event->proc, event->e_un.signum),
345 strsignal(event->e_un.signum));
346 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100347 continue_after_signal(event->proc->pid, event->e_un.signum);
348}
349
Juan Cespedesf1350522008-12-16 18:19:58 +0100350static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200351handle_exit(Event *event) {
352 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200353 if (event->proc->state != STATE_IGNORED) {
354 output_line(event->proc, "+++ exited (status %d) +++",
355 event->e_un.ret_val);
356 }
Petr Machatacebb8842011-07-09 11:14:11 +0200357 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100358}
359
Juan Cespedesf1350522008-12-16 18:19:58 +0100360static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200361handle_exit_signal(Event *event) {
362 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200363 if (event->proc->state != STATE_IGNORED) {
364 output_line(event->proc, "+++ killed by %s +++",
365 shortsignal(event->proc, event->e_un.signum));
366 }
Petr Machatacebb8842011-07-09 11:14:11 +0200367 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100368}
369
Juan Cespedesf1350522008-12-16 18:19:58 +0100370static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200371handle_syscall(Event *event) {
372 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200373 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100374 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200375 if (options.syscalls) {
376 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200377 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200378 }
379 if (event->proc->breakpoints_enabled == 0) {
380 enable_all_breakpoints(event->proc);
381 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100382 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100383 continue_process(event->proc->pid);
384}
385
Juan Cespedesf1350522008-12-16 18:19:58 +0100386static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200387handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200388 Process * proc = event->proc;
389 pid_t saved_pid;
390
Juan Cespedes03192f82009-07-03 10:16:22 +0200391 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200392 if (proc->state == STATE_IGNORED) {
393 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200394 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200395 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200396 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200397 output_line(proc, "--- Called exec() ---");
398 proc->mask_32bit = 0;
399 proc->personality = 0;
400 proc->arch_ptr = NULL;
401 free(proc->filename);
402 proc->filename = pid2name(proc->pid);
403 saved_pid = proc->pid;
404 proc->pid = 0;
Petr Machatac7585b62011-07-08 22:58:12 +0200405 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200406 proc->pid = saved_pid;
407 proc->callstack_depth = 0;
408 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200409}
410
411static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200412handle_arch_syscall(Event *event) {
413 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200414 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100415 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200416 if (options.syscalls) {
417 output_left(LT_TOF_SYSCALL, event->proc,
418 arch_sysname(event->proc, event->e_un.sysnum));
419 }
420 if (event->proc->breakpoints_enabled == 0) {
421 enable_all_breakpoints(event->proc);
422 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100423 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100424 continue_process(event->proc->pid);
425}
426
Juan Cespedesd65efa32003-02-03 00:22:30 +0100427struct timeval current_time_spent;
428
Juan Cespedesf1350522008-12-16 18:19:58 +0100429static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200430calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100431 struct timeval tv;
432 struct timezone tz;
433 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100434 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100435
Juan Cespedescd8976d2009-05-14 13:47:58 +0200436 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100438
439 gettimeofday(&tv, &tz);
440
441 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
442 if (tv.tv_usec >= elem->time_spent.tv_usec) {
443 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
444 } else {
445 diff.tv_sec++;
446 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
447 }
448 current_time_spent = diff;
449}
450
Juan Cespedesf1350522008-12-16 18:19:58 +0100451static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200452handle_sysret(Event *event) {
453 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200454 if (event->proc->state != STATE_IGNORED) {
455 if (opt_T || options.summary) {
456 calc_time_spent(event->proc);
457 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200458 if (options.syscalls) {
459 output_right(LT_TOF_SYSCALLR, event->proc,
460 sysname(event->proc, event->e_un.sysnum));
461 }
Petr Machata211f0882010-11-03 18:42:18 +0100462 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200463 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100464 continue_process(event->proc->pid);
465}
466
Juan Cespedesf1350522008-12-16 18:19:58 +0100467static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200468handle_arch_sysret(Event *event) {
469 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 if (event->proc->state != STATE_IGNORED) {
471 if (opt_T || options.summary) {
472 calc_time_spent(event->proc);
473 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200474 if (options.syscalls) {
475 output_right(LT_TOF_SYSCALLR, event->proc,
476 arch_sysname(event->proc, event->e_un.sysnum));
477 }
Petr Machata211f0882010-11-03 18:42:18 +0100478 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100479 }
480 continue_process(event->proc->pid);
481}
482
Petr Machata067322d2010-10-25 14:47:55 +0200483#ifdef __powerpc__
484void *get_count_register (Process *proc);
485#endif
486
Juan Cespedesf1350522008-12-16 18:19:58 +0100487static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200488handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100489 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200490 Breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200491 Process *leader = event->proc->leader;
492
493 /* The leader has terminated. */
494 if (leader == NULL) {
495 continue_process(event->proc->pid);
496 return;
497 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100498
Juan Cespedes03192f82009-07-03 10:16:22 +0200499 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200500 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100501
Paul Gilliam76c61f12006-06-14 06:55:21 +0200502#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100503 /* Need to skip following NOP's to prevent a fake function from being stacked. */
504 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200505 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100506 char nop_instruction[] = PPC_NOP;
507
Petr Machata9a5420c2011-07-09 11:21:23 +0200508 stub_bp = address2bpstruct(leader, event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100509
510 if (stub_bp) {
511 unsigned char *bp_instruction = stub_bp->orig_value;
512
513 if (memcmp(bp_instruction, nop_instruction,
514 PPC_NOP_LENGTH) == 0) {
515 if (stub_addr != (long) event->e_un.brk_addr) {
516 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
517 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200518 return;
519 }
520 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100521 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200522#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200523
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
525 if (event->e_un.brk_addr ==
526 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200527#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100528 /*
529 * PPC HACK! (XXX FIXME TODO)
530 * The PLT gets modified during the first call,
531 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100533 unsigned long a;
534 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100535 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200536 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200537
Paul Gilliam76c61f12006-06-14 06:55:21 +0200538 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100539 unsigned char break_insn[] = BREAKPOINT_VALUE;
540
Petr Machata9a5420c2011-07-09 11:21:23 +0200541 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100542 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100543 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
544 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100545
Paul Gilliam76c61f12006-06-14 06:55:21 +0200546 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100547 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100548 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200549 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100550 }
551 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200552 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200553 /* On powerpc, the breakpoint address
554 may end up being actual entry point
555 of the library symbol, not the PLT
556 address we computed. In that case,
557 sbp is NULL. */
558 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100559 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200560 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200561 }
Ian Wienand3219f322006-02-16 06:00:00 +0100562 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100563#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500564 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200565 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500566 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200567 assert(sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200568 addr = sym2addr(leader, sym);
569 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500570 if (sbp) {
571 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200572 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500573 }
574 } else {
575 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
576 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200577 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200578 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200579 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200580 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200581#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100582 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100583 callstack_pop(event->proc);
584 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200585 if (event->proc->state != STATE_IGNORED) {
586 if (opt_T || options.summary) {
587 calc_time_spent(event->proc);
588 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100589 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100590 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200591 if (event->proc->state != STATE_IGNORED) {
592 output_right(LT_TOF_FUNCTIONR, event->proc,
593 event->proc->callstack[i].c_un.libfunc->name);
594 }
Petr Machata211f0882010-11-03 18:42:18 +0100595 callstack_pop(event->proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200596 sbp = address2bpstruct(leader, event->e_un.brk_addr);
597 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100598 return;
599 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100600 }
601
Petr Machata9a5420c2011-07-09 11:21:23 +0200602 if ((sbp = address2bpstruct(leader, event->e_un.brk_addr))) {
603 if (sbp->libsym == NULL) {
604 continue_after_breakpoint(event->proc, sbp);
605 return;
606 }
607
Joe Damatof0bd98b2010-11-08 15:47:42 -0800608 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200609 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200610 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800611 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200612
Zachary T Welch97baa652010-12-08 13:34:03 -0800613 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200614 event->proc->stack_pointer = get_stack_pointer(event->proc);
615 event->proc->return_addr =
616 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200617 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100618 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200619 }
Paul Gilliambe320772006-04-24 22:06:23 +0200620#ifdef PLT_REINITALISATION_BP
621 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100622 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
623 0))
Petr Machata9a5420c2011-07-09 11:21:23 +0200624 reinitialize_breakpoints(leader);
Paul Gilliambe320772006-04-24 22:06:23 +0200625#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100626
627 continue_after_breakpoint(event->proc, sbp);
628 return;
629 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100630
Joe Damatofa2aefc2010-10-30 19:56:50 -0700631 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200632 output_line(event->proc, "unexpected breakpoint at %p",
633 (void *)event->e_un.brk_addr);
634 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100635 continue_process(event->proc->pid);
636}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200637
Juan Cespedesf1350522008-12-16 18:19:58 +0100638static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200639callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100640 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200641
Juan Cespedescd8976d2009-05-14 13:47:58 +0200642 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200643 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100644 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500645 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
646 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200647 return;
648 }
649
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100650 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200651 elem->is_syscall = 1;
652 elem->c_un.syscall = sysnum;
653 elem->return_addr = NULL;
654
655 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200656 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100657 struct timezone tz;
658 gettimeofday(&elem->time_spent, &tz);
659 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200660}
661
Juan Cespedes21c63a12001-07-07 20:56:56 +0200662static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200663callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500664 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200665
Juan Cespedescd8976d2009-05-14 13:47:58 +0200666 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200667 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100668 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500669 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
670 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200671 return;
672 }
673
Arnaud Patard26570082010-01-08 08:40:12 -0500674 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100675 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200676 elem->is_syscall = 0;
677 elem->c_un.libfunc = sym;
678
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200679 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200680 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200681 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200682 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200683
Arnaud Patard26570082010-01-08 08:40:12 -0500684 /* handle functions like atexit() on mips which have no return */
685 if (elem->return_addr != prev->return_addr)
686 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200687 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100688 struct timezone tz;
689 gettimeofday(&elem->time_spent, &tz);
690 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200691}
692
Juan Cespedesf1350522008-12-16 18:19:58 +0100693static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200694callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100695 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200696 assert(proc->callstack_depth > 0);
697
Juan Cespedescd8976d2009-05-14 13:47:58 +0200698 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100699 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200700 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200701 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200702 delete_breakpoint(proc, elem->return_addr);
703 }
Petr Machata211f0882010-11-03 18:42:18 +0100704 if (elem->arch_ptr != NULL) {
705 free(elem->arch_ptr);
706 elem->arch_ptr = NULL;
707 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200708 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200709}