blob: df0e2fc5a5034693e165b3de546609211d20d0ed [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 Cespedesa8909f72009-04-28 20:02:41 +020028static void remove_proc(Process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010029
Juan Cespedesa8909f72009-04-28 20:02:41 +020030static void callstack_push_syscall(Process *proc, int sysnum);
31static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020033static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020034
Juan Cespedes61da3372009-07-03 11:55:44 +020035static char * shortsignal(Process *proc, int signum);
36static char * sysname(Process *proc, int sysnum);
37static char * arch_sysname(Process *proc, int sysnum);
38
39void
40handle_event(Event *event) {
Petr Machata26627682011-07-08 18:15:32 +020041 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
42 event->proc ? event->proc->pid : -1, event->type);
Juan Cespedes61da3372009-07-03 11:55:44 +020043 switch (event->type) {
44 case EVENT_NONE:
45 debug(1, "event: none");
46 return;
47 case EVENT_SIGNAL:
48 debug(1, "event: signal (%s [%d])",
49 shortsignal(event->proc, event->e_un.signum),
50 event->e_un.signum);
51 handle_signal(event);
52 return;
53 case EVENT_EXIT:
54 debug(1, "event: exit (%d)", event->e_un.ret_val);
55 handle_exit(event);
56 return;
57 case EVENT_EXIT_SIGNAL:
58 debug(1, "event: exit signal (%s [%d])",
59 shortsignal(event->proc, event->e_un.signum),
60 event->e_un.signum);
61 handle_exit_signal(event);
62 return;
63 case EVENT_SYSCALL:
64 debug(1, "event: syscall (%s [%d])",
65 sysname(event->proc, event->e_un.sysnum),
66 event->e_un.sysnum);
67 handle_syscall(event);
68 return;
69 case EVENT_SYSRET:
70 debug(1, "event: sysret (%s [%d])",
71 sysname(event->proc, event->e_un.sysnum),
72 event->e_un.sysnum);
73 handle_sysret(event);
74 return;
75 case EVENT_ARCH_SYSCALL:
76 debug(1, "event: arch_syscall (%s [%d])",
77 arch_sysname(event->proc, event->e_un.sysnum),
78 event->e_un.sysnum);
79 handle_arch_syscall(event);
80 return;
81 case EVENT_ARCH_SYSRET:
82 debug(1, "event: arch_sysret (%s [%d])",
83 arch_sysname(event->proc, event->e_un.sysnum),
84 event->e_un.sysnum);
85 handle_arch_sysret(event);
86 return;
87 case EVENT_CLONE:
88 debug(1, "event: clone (%u)", event->e_un.newpid);
89 handle_clone(event);
90 return;
91 case EVENT_EXEC:
92 debug(1, "event: exec()");
93 handle_exec(event);
94 return;
95 case EVENT_BREAKPOINT:
96 debug(1, "event: breakpoint");
97 handle_breakpoint(event);
98 return;
99 case EVENT_NEW:
100 debug(1, "event: new process");
101 handle_new(event);
102 return;
103 default:
104 fprintf(stderr, "Error! unknown event?\n");
105 exit(1);
106 }
107}
108
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200109/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200110static void *
111address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200112 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200113 return addr;
114}
115
Juan Cespedes61da3372009-07-03 11:55:44 +0200116static void *
117breakpoint_clone(void * bp) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200118 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200119 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200120 b = malloc(sizeof(Breakpoint));
121 if (!b) {
122 perror("malloc()");
123 exit(1);
124 }
125 memcpy(b, bp, sizeof(Breakpoint));
126 return b;
127}
128
129typedef struct Pending_New Pending_New;
130struct Pending_New {
131 pid_t pid;
132 Pending_New * next;
133};
134static Pending_New * pending_news = NULL;
135
136static int
137pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200138 Pending_New * p;
139
140 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
141
142 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200143 while (p) {
144 if (p->pid == pid) {
145 return 1;
146 }
147 p = p->next;
148 }
149 return 0;
150}
151
152static void
153pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200154 Pending_New * p;
155
156 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
157
158 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200159 if (!p) {
160 perror("malloc()");
161 exit(1);
162 }
163 p->pid = pid;
164 p->next = pending_news;
165 pending_news = p;
166}
167
168static void
169pending_new_remove(pid_t pid) {
170 Pending_New *p, *pred;
171
Juan Cespedescd8976d2009-05-14 13:47:58 +0200172 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
173
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200174 p = pending_news;
175 if (p->pid == pid) {
176 pending_news = p->next;
177 free(p);
178 } else {
179 while (p) {
180 if (p->pid == pid) {
181 pred->next = p->next;
182 free(p);
183 }
184 pred = p;
185 p = p->next;
186 }
187 }
188}
189
190static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200191handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200192 Process *p;
193
Juan Cespedes03192f82009-07-03 10:16:22 +0200194 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200195
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200196 p = malloc(sizeof(Process));
197 if (!p) {
198 perror("malloc()");
199 exit(1);
200 }
201 memcpy(p, event->proc, sizeof(Process));
202 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
203 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200204 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200205
206 if (pending_new(p->pid)) {
207 pending_new_remove(p->pid);
208 if (p->breakpoint_being_enabled) {
209 enable_breakpoint(p->pid, p->breakpoint_being_enabled);
210 p->breakpoint_being_enabled = NULL;
211 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200212 if (event->proc->state == STATE_ATTACHED && options.follow) {
213 p->state = STATE_ATTACHED;
214 } else {
215 p->state = STATE_IGNORED;
216 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200217 continue_process(p->pid);
218 p->next = list_of_processes;
219 list_of_processes = p;
220 } else {
221 p->state = STATE_BEING_CREATED;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200222 p->next = list_of_processes;
223 list_of_processes = p;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200224 }
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200225 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200226}
227
228static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200229handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200230 Process * proc;
231
Juan Cespedes03192f82009-07-03 10:16:22 +0200232 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200233
234 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200235 if (!proc) {
236 pending_new_insert(event->e_un.newpid);
237 } else {
238 assert(proc->state == STATE_BEING_CREATED);
239 if (proc->breakpoint_being_enabled) {
240 enable_breakpoint(proc->pid, proc->breakpoint_being_enabled);
241 proc->breakpoint_being_enabled = NULL;
242 }
Juan Cespedes30439b42009-05-22 19:03:09 +0200243 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200244 proc->state = STATE_ATTACHED;
245 } else {
246 proc->state = STATE_IGNORED;
247 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200248 continue_process(proc->pid);
249 }
250}
251
Juan Cespedesf1350522008-12-16 18:19:58 +0100252static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200253shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 static char *signalent0[] = {
255#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100256 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 static char *signalent1[] = {
258#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100259 };
260 static char **signalents[] = { signalent0, signalent1 };
261 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 sizeof signalent1 / sizeof signalent1[0]
263 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100264
Juan Cespedescd8976d2009-05-14 13:47:58 +0200265 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
266
Ian Wienand9a2ad352006-02-20 22:44:45 +0100267 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100269 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 return "UNKNOWN_SIGNAL";
271 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100272 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100273 }
274}
275
Juan Cespedesf1350522008-12-16 18:19:58 +0100276static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200277sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100278 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 static char *syscalent0[] = {
280#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100281 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 static char *syscalent1[] = {
283#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100284 };
285 static char **syscalents[] = { syscalent0, syscalent1 };
286 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 sizeof syscalent1 / sizeof syscalent1[0]
288 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100289
Juan Cespedescd8976d2009-05-14 13:47:58 +0200290 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
291
Ian Wienand9a2ad352006-02-20 22:44:45 +0100292 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100294 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100295 sprintf(result, "SYS_%d", sysnum);
296 return result;
297 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100298 sprintf(result, "SYS_%s",
299 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300 return result;
301 }
302}
303
Juan Cespedesf1350522008-12-16 18:19:58 +0100304static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200305arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100306 static char result[128];
307 static char *arch_syscalent[] = {
308#include "arch_syscallent.h"
309 };
310 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
311
Juan Cespedescd8976d2009-05-14 13:47:58 +0200312 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
313
Juan Cespedes63184be2008-12-10 13:30:12 +0100314 if (sysnum < 0 || sysnum >= nsyscals) {
315 sprintf(result, "ARCH_%d", sysnum);
316 return result;
317 } else {
318 sprintf(result, "ARCH_%s",
319 arch_syscalent[sysnum]);
320 return result;
321 }
322}
323
Juan Cespedesf1350522008-12-16 18:19:58 +0100324static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200325handle_signal(Event *event) {
326 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes28f60191998-04-12 00:04:39 +0200327 if (exiting && event->e_un.signum == SIGSTOP) {
328 pid_t pid = event->proc->pid;
329 disable_all_breakpoints(event->proc);
330 untrace_pid(pid);
331 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200332 return;
333 }
Joe Damato59e3fb12009-11-06 19:45:10 -0800334 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200335 output_line(event->proc, "--- %s (%s) ---",
336 shortsignal(event->proc, event->e_un.signum),
337 strsignal(event->e_un.signum));
338 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100339 continue_after_signal(event->proc->pid, event->e_un.signum);
340}
341
Juan Cespedesf1350522008-12-16 18:19:58 +0100342static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200343handle_exit(Event *event) {
344 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200345 if (event->proc->state != STATE_IGNORED) {
346 output_line(event->proc, "+++ exited (status %d) +++",
347 event->e_un.ret_val);
348 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100349 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100350}
351
Juan Cespedesf1350522008-12-16 18:19:58 +0100352static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200353handle_exit_signal(Event *event) {
354 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200355 if (event->proc->state != STATE_IGNORED) {
356 output_line(event->proc, "+++ killed by %s +++",
357 shortsignal(event->proc, event->e_un.signum));
358 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100359 remove_proc(event->proc);
360}
361
Juan Cespedesf1350522008-12-16 18:19:58 +0100362static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200363remove_proc(Process *proc) {
364 Process *tmp, *tmp2;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100365
Juan Cespedescd8976d2009-05-14 13:47:58 +0200366 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200367
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100368 if (list_of_processes == proc) {
369 tmp = list_of_processes;
370 list_of_processes = list_of_processes->next;
371 free(tmp);
372 return;
373 }
374 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 while (tmp->next) {
376 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100377 tmp2 = tmp->next;
378 tmp->next = tmp->next->next;
379 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200380 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100381 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100382 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100383 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100384}
385
Juan Cespedesf1350522008-12-16 18:19:58 +0100386static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200387handle_syscall(Event *event) {
388 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200389 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100390 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200391 if (options.syscalls) {
392 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200393 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200394 }
395 if (event->proc->breakpoints_enabled == 0) {
396 enable_all_breakpoints(event->proc);
397 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100398 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100399 continue_process(event->proc->pid);
400}
401
Juan Cespedesf1350522008-12-16 18:19:58 +0100402static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200403handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200404 Process * proc = event->proc;
405 pid_t saved_pid;
406
Juan Cespedes03192f82009-07-03 10:16:22 +0200407 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200408 if (proc->state == STATE_IGNORED) {
409 untrace_pid(proc->pid);
410 remove_proc(proc);
411 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200412 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200413 output_line(proc, "--- Called exec() ---");
414 proc->mask_32bit = 0;
415 proc->personality = 0;
416 proc->arch_ptr = NULL;
417 free(proc->filename);
418 proc->filename = pid2name(proc->pid);
419 saved_pid = proc->pid;
420 proc->pid = 0;
421 breakpoints_init(proc);
422 proc->pid = saved_pid;
423 proc->callstack_depth = 0;
424 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200425}
426
427static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200428handle_arch_syscall(Event *event) {
429 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200430 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100431 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200432 if (options.syscalls) {
433 output_left(LT_TOF_SYSCALL, event->proc,
434 arch_sysname(event->proc, event->e_un.sysnum));
435 }
436 if (event->proc->breakpoints_enabled == 0) {
437 enable_all_breakpoints(event->proc);
438 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100439 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100440 continue_process(event->proc->pid);
441}
442
Juan Cespedesd65efa32003-02-03 00:22:30 +0100443struct timeval current_time_spent;
444
Juan Cespedesf1350522008-12-16 18:19:58 +0100445static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200446calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100447 struct timeval tv;
448 struct timezone tz;
449 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100450 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100451
Juan Cespedescd8976d2009-05-14 13:47:58 +0200452 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100453 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100454
455 gettimeofday(&tv, &tz);
456
457 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
458 if (tv.tv_usec >= elem->time_spent.tv_usec) {
459 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
460 } else {
461 diff.tv_sec++;
462 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
463 }
464 current_time_spent = diff;
465}
466
Juan Cespedesf1350522008-12-16 18:19:58 +0100467static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200468handle_sysret(Event *event) {
469 debug(DEBUG_FUNCTION, "handle_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 sysname(event->proc, event->e_un.sysnum));
477 }
Petr Machata211f0882010-11-03 18:42:18 +0100478 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200479 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100480 continue_process(event->proc->pid);
481}
482
Juan Cespedesf1350522008-12-16 18:19:58 +0100483static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200484handle_arch_sysret(Event *event) {
485 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200486 if (event->proc->state != STATE_IGNORED) {
487 if (opt_T || options.summary) {
488 calc_time_spent(event->proc);
489 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200490 if (options.syscalls) {
491 output_right(LT_TOF_SYSCALLR, event->proc,
492 arch_sysname(event->proc, event->e_un.sysnum));
493 }
Petr Machata211f0882010-11-03 18:42:18 +0100494 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100495 }
496 continue_process(event->proc->pid);
497}
498
Petr Machata067322d2010-10-25 14:47:55 +0200499#ifdef __powerpc__
500void *get_count_register (Process *proc);
501#endif
502
Juan Cespedesf1350522008-12-16 18:19:58 +0100503static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200504handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200506 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100507
Juan Cespedes03192f82009-07-03 10:16:22 +0200508 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200509 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100510
Paul Gilliam76c61f12006-06-14 06:55:21 +0200511#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100512 /* Need to skip following NOP's to prevent a fake function from being stacked. */
513 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200514 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100515 char nop_instruction[] = PPC_NOP;
516
517 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
518
519 if (stub_bp) {
520 unsigned char *bp_instruction = stub_bp->orig_value;
521
522 if (memcmp(bp_instruction, nop_instruction,
523 PPC_NOP_LENGTH) == 0) {
524 if (stub_addr != (long) event->e_un.brk_addr) {
525 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
526 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200527 return;
528 }
529 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100530 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200531#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100532 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100533 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100534 continue_enabling_breakpoint(event->proc->pid,
535 event->proc->
536 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100537 event->proc->breakpoint_being_enabled = NULL;
538 return;
539 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200540
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100541 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
542 if (event->e_un.brk_addr ==
543 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200544#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100545 /*
546 * PPC HACK! (XXX FIXME TODO)
547 * The PLT gets modified during the first call,
548 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100549 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100550 unsigned long a;
551 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100552 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200553 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200554
Paul Gilliam76c61f12006-06-14 06:55:21 +0200555 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100556 unsigned char break_insn[] = BREAKPOINT_VALUE;
557
558 sbp = address2bpstruct(event->proc, addr);
559 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100560 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
561 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100562
Paul Gilliam76c61f12006-06-14 06:55:21 +0200563 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100564 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100565 insert_breakpoint(event->proc, addr,
566 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100567 }
568 } else {
Petr Machata067322d2010-10-25 14:47:55 +0200569 sbp = dict_find_entry(event->proc->breakpoints, addr);
570 /* On powerpc, the breakpoint address
571 may end up being actual entry point
572 of the library symbol, not the PLT
573 address we computed. In that case,
574 sbp is NULL. */
575 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576 insert_breakpoint(event->proc, addr,
577 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200578 }
Ian Wienand3219f322006-02-16 06:00:00 +0100579 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100580#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500581 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200582 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500583 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200584 assert(sym);
Petr Machata26627682011-07-08 18:15:32 +0200585 addr = sym2addr(event->proc, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500586 sbp = dict_find_entry(event->proc->breakpoints, addr);
587 if (sbp) {
588 if (addr != sbp->addr) {
589 insert_breakpoint(event->proc, addr, sym);
590 }
591 } else {
592 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
593 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200594 new_sym->next = leader->list_of_symbols;
595 event->proc->list_of_symbols = new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200596 insert_breakpoint(event->proc, addr, new_sym);
597 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200598#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100600 callstack_pop(event->proc);
601 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200602 if (event->proc->state != STATE_IGNORED) {
603 if (opt_T || options.summary) {
604 calc_time_spent(event->proc);
605 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100606 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100607 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200608 if (event->proc->state != STATE_IGNORED) {
609 output_right(LT_TOF_FUNCTIONR, event->proc,
610 event->proc->callstack[i].c_un.libfunc->name);
611 }
Petr Machata211f0882010-11-03 18:42:18 +0100612 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100613 continue_after_breakpoint(event->proc,
Juan Cespedes5c682042009-05-21 15:59:56 +0200614 address2bpstruct(event->proc,
615 event->e_un.brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100616 return;
617 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100618 }
619
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100620 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800621 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200622 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800623 arch_check_dbg(event->proc);
Zachary T Welch97baa652010-12-08 13:34:03 -0800624 }
625 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200626 event->proc->stack_pointer = get_stack_pointer(event->proc);
627 event->proc->return_addr =
628 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200629 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100630 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200631 }
Paul Gilliambe320772006-04-24 22:06:23 +0200632#ifdef PLT_REINITALISATION_BP
633 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100634 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
635 0))
636 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200637#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100638
639 continue_after_breakpoint(event->proc, sbp);
640 return;
641 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100642
Joe Damatofa2aefc2010-10-30 19:56:50 -0700643 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200644 output_line(event->proc, "unexpected breakpoint at %p",
645 (void *)event->e_un.brk_addr);
646 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100647 continue_process(event->proc->pid);
648}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200649
Juan Cespedesf1350522008-12-16 18:19:58 +0100650static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200651callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100652 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200653
Juan Cespedescd8976d2009-05-14 13:47:58 +0200654 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200655 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100656 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500657 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
658 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200659 return;
660 }
661
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200663 elem->is_syscall = 1;
664 elem->c_un.syscall = sysnum;
665 elem->return_addr = NULL;
666
667 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200668 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100669 struct timezone tz;
670 gettimeofday(&elem->time_spent, &tz);
671 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200672}
673
Juan Cespedes21c63a12001-07-07 20:56:56 +0200674static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200675callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500676 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200677
Juan Cespedescd8976d2009-05-14 13:47:58 +0200678 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200679 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100680 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500681 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
682 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200683 return;
684 }
685
Arnaud Patard26570082010-01-08 08:40:12 -0500686 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100687 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200688 elem->is_syscall = 0;
689 elem->c_un.libfunc = sym;
690
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200691 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200692 if (elem->return_addr) {
Petr Machata26627682011-07-08 18:15:32 +0200693 insert_breakpoint(proc, elem->return_addr, NULL);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200694 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200695
Arnaud Patard26570082010-01-08 08:40:12 -0500696 /* handle functions like atexit() on mips which have no return */
697 if (elem->return_addr != prev->return_addr)
698 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200699 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100700 struct timezone tz;
701 gettimeofday(&elem->time_spent, &tz);
702 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200703}
704
Juan Cespedesf1350522008-12-16 18:19:58 +0100705static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200706callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100707 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200708 assert(proc->callstack_depth > 0);
709
Juan Cespedescd8976d2009-05-14 13:47:58 +0200710 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100711 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200712 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200713 delete_breakpoint(proc, elem->return_addr);
714 }
Petr Machata211f0882010-11-03 18:42:18 +0100715 if (elem->arch_ptr != NULL) {
716 free(elem->arch_ptr);
717 elem->arch_ptr = NULL;
718 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200719 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200720}