blob: 8664f902d9ffec5f4ac3e1605fbbda0ed5146017 [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 Machata26627682011-07-08 18:15:32 +020040 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
41 event->proc ? event->proc->pid : -1, event->type);
Juan Cespedes61da3372009-07-03 11:55:44 +020042 switch (event->type) {
43 case EVENT_NONE:
44 debug(1, "event: none");
45 return;
46 case EVENT_SIGNAL:
47 debug(1, "event: signal (%s [%d])",
48 shortsignal(event->proc, event->e_un.signum),
49 event->e_un.signum);
50 handle_signal(event);
51 return;
52 case EVENT_EXIT:
53 debug(1, "event: exit (%d)", event->e_un.ret_val);
54 handle_exit(event);
55 return;
56 case EVENT_EXIT_SIGNAL:
57 debug(1, "event: exit signal (%s [%d])",
58 shortsignal(event->proc, event->e_un.signum),
59 event->e_un.signum);
60 handle_exit_signal(event);
61 return;
62 case EVENT_SYSCALL:
63 debug(1, "event: syscall (%s [%d])",
64 sysname(event->proc, event->e_un.sysnum),
65 event->e_un.sysnum);
66 handle_syscall(event);
67 return;
68 case EVENT_SYSRET:
69 debug(1, "event: sysret (%s [%d])",
70 sysname(event->proc, event->e_un.sysnum),
71 event->e_un.sysnum);
72 handle_sysret(event);
73 return;
74 case EVENT_ARCH_SYSCALL:
75 debug(1, "event: arch_syscall (%s [%d])",
76 arch_sysname(event->proc, event->e_un.sysnum),
77 event->e_un.sysnum);
78 handle_arch_syscall(event);
79 return;
80 case EVENT_ARCH_SYSRET:
81 debug(1, "event: arch_sysret (%s [%d])",
82 arch_sysname(event->proc, event->e_un.sysnum),
83 event->e_un.sysnum);
84 handle_arch_sysret(event);
85 return;
86 case EVENT_CLONE:
87 debug(1, "event: clone (%u)", event->e_un.newpid);
88 handle_clone(event);
89 return;
90 case EVENT_EXEC:
91 debug(1, "event: exec()");
92 handle_exec(event);
93 return;
94 case EVENT_BREAKPOINT:
95 debug(1, "event: breakpoint");
96 handle_breakpoint(event);
97 return;
98 case EVENT_NEW:
99 debug(1, "event: new process");
100 handle_new(event);
101 return;
102 default:
103 fprintf(stderr, "Error! unknown event?\n");
104 exit(1);
105 }
106}
107
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200108/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200109static void *
110address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200111 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200112 return addr;
113}
114
Juan Cespedes61da3372009-07-03 11:55:44 +0200115static void *
116breakpoint_clone(void * bp) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200117 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200118 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200119 b = malloc(sizeof(Breakpoint));
120 if (!b) {
121 perror("malloc()");
122 exit(1);
123 }
124 memcpy(b, bp, sizeof(Breakpoint));
125 return b;
126}
127
128typedef struct Pending_New Pending_New;
129struct Pending_New {
130 pid_t pid;
131 Pending_New * next;
132};
133static Pending_New * pending_news = NULL;
134
135static int
136pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200137 Pending_New * p;
138
139 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
140
141 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200142 while (p) {
143 if (p->pid == pid) {
144 return 1;
145 }
146 p = p->next;
147 }
148 return 0;
149}
150
151static void
152pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200153 Pending_New * p;
154
155 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
156
157 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200158 if (!p) {
159 perror("malloc()");
160 exit(1);
161 }
162 p->pid = pid;
163 p->next = pending_news;
164 pending_news = p;
165}
166
167static void
168pending_new_remove(pid_t pid) {
169 Pending_New *p, *pred;
170
Juan Cespedescd8976d2009-05-14 13:47:58 +0200171 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
172
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200173 p = pending_news;
174 if (p->pid == pid) {
175 pending_news = p->next;
176 free(p);
177 } else {
178 while (p) {
179 if (p->pid == pid) {
180 pred->next = p->next;
181 free(p);
182 }
183 pred = p;
184 p = p->next;
185 }
186 }
187}
188
189static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200190handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200191 Process *p;
192
Juan Cespedes03192f82009-07-03 10:16:22 +0200193 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200194
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200195 p = malloc(sizeof(Process));
196 if (!p) {
197 perror("malloc()");
198 exit(1);
199 }
200 memcpy(p, event->proc, sizeof(Process));
201 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
202 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200203 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200204
205 if (pending_new(p->pid)) {
206 pending_new_remove(p->pid);
207 if (p->breakpoint_being_enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200208 enable_breakpoint(p, p->breakpoint_being_enabled);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200209 p->breakpoint_being_enabled = NULL;
210 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200211 if (event->proc->state == STATE_ATTACHED && options.follow) {
212 p->state = STATE_ATTACHED;
213 } else {
214 p->state = STATE_IGNORED;
215 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200216 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200217 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200218 } else {
219 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200220 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200221 }
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200222 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200223}
224
225static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200226handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200227 Process * proc;
228
Juan Cespedes03192f82009-07-03 10:16:22 +0200229 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200230
231 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200232 if (!proc) {
233 pending_new_insert(event->e_un.newpid);
234 } else {
235 assert(proc->state == STATE_BEING_CREATED);
236 if (proc->breakpoint_being_enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200237 enable_breakpoint(proc, proc->breakpoint_being_enabled);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200238 proc->breakpoint_being_enabled = NULL;
239 }
Juan Cespedes30439b42009-05-22 19:03:09 +0200240 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200241 proc->state = STATE_ATTACHED;
242 } else {
243 proc->state = STATE_IGNORED;
244 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200245 continue_process(proc->pid);
246 }
247}
248
Juan Cespedesf1350522008-12-16 18:19:58 +0100249static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200250shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 static char *signalent0[] = {
252#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100253 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 static char *signalent1[] = {
255#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100256 };
257 static char **signalents[] = { signalent0, signalent1 };
258 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 sizeof signalent1 / sizeof signalent1[0]
260 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100261
Juan Cespedescd8976d2009-05-14 13:47:58 +0200262 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
263
Ian Wienand9a2ad352006-02-20 22:44:45 +0100264 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100266 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100267 return "UNKNOWN_SIGNAL";
268 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100269 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 }
271}
272
Juan Cespedesf1350522008-12-16 18:19:58 +0100273static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200274sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100275 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 static char *syscalent0[] = {
277#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100278 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 static char *syscalent1[] = {
280#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100281 };
282 static char **syscalents[] = { syscalent0, syscalent1 };
283 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 sizeof syscalent1 / sizeof syscalent1[0]
285 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100286
Juan Cespedescd8976d2009-05-14 13:47:58 +0200287 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
288
Ian Wienand9a2ad352006-02-20 22:44:45 +0100289 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100290 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100291 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100292 sprintf(result, "SYS_%d", sysnum);
293 return result;
294 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 sprintf(result, "SYS_%s",
296 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100297 return result;
298 }
299}
300
Juan Cespedesf1350522008-12-16 18:19:58 +0100301static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200302arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100303 static char result[128];
304 static char *arch_syscalent[] = {
305#include "arch_syscallent.h"
306 };
307 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
308
Juan Cespedescd8976d2009-05-14 13:47:58 +0200309 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
310
Juan Cespedes63184be2008-12-10 13:30:12 +0100311 if (sysnum < 0 || sysnum >= nsyscals) {
312 sprintf(result, "ARCH_%d", sysnum);
313 return result;
314 } else {
315 sprintf(result, "ARCH_%s",
316 arch_syscalent[sysnum]);
317 return result;
318 }
319}
320
Juan Cespedesf1350522008-12-16 18:19:58 +0100321static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200322handle_signal(Event *event) {
323 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes28f60191998-04-12 00:04:39 +0200324 if (exiting && event->e_un.signum == SIGSTOP) {
325 pid_t pid = event->proc->pid;
326 disable_all_breakpoints(event->proc);
327 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200328 remove_process(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200329 return;
330 }
Joe Damato59e3fb12009-11-06 19:45:10 -0800331 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200332 output_line(event->proc, "--- %s (%s) ---",
333 shortsignal(event->proc, event->e_un.signum),
334 strsignal(event->e_un.signum));
335 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100336 continue_after_signal(event->proc->pid, event->e_un.signum);
337}
338
Juan Cespedesf1350522008-12-16 18:19:58 +0100339static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200340handle_exit(Event *event) {
341 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200342 if (event->proc->state != STATE_IGNORED) {
343 output_line(event->proc, "+++ exited (status %d) +++",
344 event->e_un.ret_val);
345 }
Petr Machatacebb8842011-07-09 11:14:11 +0200346 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100347}
348
Juan Cespedesf1350522008-12-16 18:19:58 +0100349static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200350handle_exit_signal(Event *event) {
351 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200352 if (event->proc->state != STATE_IGNORED) {
353 output_line(event->proc, "+++ killed by %s +++",
354 shortsignal(event->proc, event->e_un.signum));
355 }
Petr Machatacebb8842011-07-09 11:14:11 +0200356 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357}
358
Juan Cespedesf1350522008-12-16 18:19:58 +0100359static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200360handle_syscall(Event *event) {
361 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200362 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100363 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200364 if (options.syscalls) {
365 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200366 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200367 }
368 if (event->proc->breakpoints_enabled == 0) {
369 enable_all_breakpoints(event->proc);
370 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100371 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100372 continue_process(event->proc->pid);
373}
374
Juan Cespedesf1350522008-12-16 18:19:58 +0100375static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200376handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200377 Process * proc = event->proc;
378 pid_t saved_pid;
379
Juan Cespedes03192f82009-07-03 10:16:22 +0200380 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200381 if (proc->state == STATE_IGNORED) {
382 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200383 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200384 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200385 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200386 output_line(proc, "--- Called exec() ---");
387 proc->mask_32bit = 0;
388 proc->personality = 0;
389 proc->arch_ptr = NULL;
390 free(proc->filename);
391 proc->filename = pid2name(proc->pid);
392 saved_pid = proc->pid;
393 proc->pid = 0;
Petr Machatac7585b62011-07-08 22:58:12 +0200394 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200395 proc->pid = saved_pid;
396 proc->callstack_depth = 0;
397 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200398}
399
400static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200401handle_arch_syscall(Event *event) {
402 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200403 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100404 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200405 if (options.syscalls) {
406 output_left(LT_TOF_SYSCALL, event->proc,
407 arch_sysname(event->proc, event->e_un.sysnum));
408 }
409 if (event->proc->breakpoints_enabled == 0) {
410 enable_all_breakpoints(event->proc);
411 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100412 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100413 continue_process(event->proc->pid);
414}
415
Juan Cespedesd65efa32003-02-03 00:22:30 +0100416struct timeval current_time_spent;
417
Juan Cespedesf1350522008-12-16 18:19:58 +0100418static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200419calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100420 struct timeval tv;
421 struct timezone tz;
422 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100424
Juan Cespedescd8976d2009-05-14 13:47:58 +0200425 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100427
428 gettimeofday(&tv, &tz);
429
430 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
431 if (tv.tv_usec >= elem->time_spent.tv_usec) {
432 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
433 } else {
434 diff.tv_sec++;
435 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
436 }
437 current_time_spent = diff;
438}
439
Juan Cespedesf1350522008-12-16 18:19:58 +0100440static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200441handle_sysret(Event *event) {
442 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200443 if (event->proc->state != STATE_IGNORED) {
444 if (opt_T || options.summary) {
445 calc_time_spent(event->proc);
446 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200447 if (options.syscalls) {
448 output_right(LT_TOF_SYSCALLR, event->proc,
449 sysname(event->proc, event->e_un.sysnum));
450 }
Petr Machata211f0882010-11-03 18:42:18 +0100451 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200452 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100453 continue_process(event->proc->pid);
454}
455
Juan Cespedesf1350522008-12-16 18:19:58 +0100456static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200457handle_arch_sysret(Event *event) {
458 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200459 if (event->proc->state != STATE_IGNORED) {
460 if (opt_T || options.summary) {
461 calc_time_spent(event->proc);
462 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200463 if (options.syscalls) {
464 output_right(LT_TOF_SYSCALLR, event->proc,
465 arch_sysname(event->proc, event->e_un.sysnum));
466 }
Petr Machata211f0882010-11-03 18:42:18 +0100467 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100468 }
469 continue_process(event->proc->pid);
470}
471
Petr Machata067322d2010-10-25 14:47:55 +0200472#ifdef __powerpc__
473void *get_count_register (Process *proc);
474#endif
475
Juan Cespedesf1350522008-12-16 18:19:58 +0100476static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200477handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100478 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200479 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100480
Juan Cespedes03192f82009-07-03 10:16:22 +0200481 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200482 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100483
Paul Gilliam76c61f12006-06-14 06:55:21 +0200484#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100485 /* Need to skip following NOP's to prevent a fake function from being stacked. */
486 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200487 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100488 char nop_instruction[] = PPC_NOP;
489
490 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
491
492 if (stub_bp) {
493 unsigned char *bp_instruction = stub_bp->orig_value;
494
495 if (memcmp(bp_instruction, nop_instruction,
496 PPC_NOP_LENGTH) == 0) {
497 if (stub_addr != (long) event->e_un.brk_addr) {
498 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
499 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200500 return;
501 }
502 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100503 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200504#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100505 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100506 /* Reinsert breakpoint */
Petr Machataf789c9c2011-07-09 10:54:27 +0200507 sbp = event->proc->breakpoint_being_enabled;
508 continue_enabling_breakpoint(event->proc, sbp);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100509 event->proc->breakpoint_being_enabled = NULL;
510 return;
511 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200512
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
514 if (event->e_un.brk_addr ==
515 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200516#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100517 /*
518 * PPC HACK! (XXX FIXME TODO)
519 * The PLT gets modified during the first call,
520 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100521 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100522 unsigned long a;
523 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200525 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200526
Paul Gilliam76c61f12006-06-14 06:55:21 +0200527 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100528 unsigned char break_insn[] = BREAKPOINT_VALUE;
529
530 sbp = address2bpstruct(event->proc, addr);
531 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
533 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100534
Paul Gilliam76c61f12006-06-14 06:55:21 +0200535 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100536 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100537 insert_breakpoint(event->proc, addr,
538 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100539 }
540 } else {
Petr Machata067322d2010-10-25 14:47:55 +0200541 sbp = dict_find_entry(event->proc->breakpoints, addr);
542 /* On powerpc, the breakpoint address
543 may end up being actual entry point
544 of the library symbol, not the PLT
545 address we computed. In that case,
546 sbp is NULL. */
547 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100548 insert_breakpoint(event->proc, addr,
549 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200550 }
Ian Wienand3219f322006-02-16 06:00:00 +0100551 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100552#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500553 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200554 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500555 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200556 assert(sym);
Petr Machata26627682011-07-08 18:15:32 +0200557 addr = sym2addr(event->proc, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500558 sbp = dict_find_entry(event->proc->breakpoints, addr);
559 if (sbp) {
560 if (addr != sbp->addr) {
561 insert_breakpoint(event->proc, addr, sym);
562 }
563 } else {
564 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
565 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200566 new_sym->next = leader->list_of_symbols;
567 event->proc->list_of_symbols = new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200568 insert_breakpoint(event->proc, addr, new_sym);
569 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200570#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100571 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100572 callstack_pop(event->proc);
573 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200574 if (event->proc->state != STATE_IGNORED) {
575 if (opt_T || options.summary) {
576 calc_time_spent(event->proc);
577 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100578 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100579 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200580 if (event->proc->state != STATE_IGNORED) {
581 output_right(LT_TOF_FUNCTIONR, event->proc,
582 event->proc->callstack[i].c_un.libfunc->name);
583 }
Petr Machata211f0882010-11-03 18:42:18 +0100584 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100585 continue_after_breakpoint(event->proc,
Juan Cespedes5c682042009-05-21 15:59:56 +0200586 address2bpstruct(event->proc,
587 event->e_un.brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100588 return;
589 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100590 }
591
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800593 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200594 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800595 arch_check_dbg(event->proc);
Zachary T Welch97baa652010-12-08 13:34:03 -0800596 }
597 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200598 event->proc->stack_pointer = get_stack_pointer(event->proc);
599 event->proc->return_addr =
600 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200601 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100602 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200603 }
Paul Gilliambe320772006-04-24 22:06:23 +0200604#ifdef PLT_REINITALISATION_BP
605 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
607 0))
608 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200609#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610
611 continue_after_breakpoint(event->proc, sbp);
612 return;
613 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100614
Joe Damatofa2aefc2010-10-30 19:56:50 -0700615 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200616 output_line(event->proc, "unexpected breakpoint at %p",
617 (void *)event->e_un.brk_addr);
618 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100619 continue_process(event->proc->pid);
620}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200621
Juan Cespedesf1350522008-12-16 18:19:58 +0100622static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200623callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100624 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200625
Juan Cespedescd8976d2009-05-14 13:47:58 +0200626 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200627 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100628 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500629 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
630 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200631 return;
632 }
633
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100634 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200635 elem->is_syscall = 1;
636 elem->c_un.syscall = sysnum;
637 elem->return_addr = NULL;
638
639 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200640 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100641 struct timezone tz;
642 gettimeofday(&elem->time_spent, &tz);
643 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200644}
645
Juan Cespedes21c63a12001-07-07 20:56:56 +0200646static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200647callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500648 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200649
Juan Cespedescd8976d2009-05-14 13:47:58 +0200650 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200651 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100652 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500653 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
654 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200655 return;
656 }
657
Arnaud Patard26570082010-01-08 08:40:12 -0500658 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100659 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200660 elem->is_syscall = 0;
661 elem->c_un.libfunc = sym;
662
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200663 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200664 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200665 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200666 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200667
Arnaud Patard26570082010-01-08 08:40:12 -0500668 /* handle functions like atexit() on mips which have no return */
669 if (elem->return_addr != prev->return_addr)
670 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200671 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100672 struct timezone tz;
673 gettimeofday(&elem->time_spent, &tz);
674 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200675}
676
Juan Cespedesf1350522008-12-16 18:19:58 +0100677static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200678callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100679 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200680 assert(proc->callstack_depth > 0);
681
Juan Cespedescd8976d2009-05-14 13:47:58 +0200682 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100683 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200684 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200685 delete_breakpoint(proc, elem->return_addr);
686 }
Petr Machata211f0882010-11-03 18:42:18 +0100687 if (elem->arch_ptr != NULL) {
688 free(elem->arch_ptr);
689 elem->arch_ptr = NULL;
690 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200691 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200692}