blob: 18f64c6a3fd79887ca31bd7316ceab01089f0fc5 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#define _GNU_SOURCE
6#include <stdio.h>
7#include <string.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01008#include <stdlib.h>
Juan Cespedes28f60191998-04-12 00:04:39 +02009#include <signal.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020010#include <assert.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010011#include <sys/time.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
Juan Cespedesf7281232009-06-25 16:11:21 +020013#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010014
Juan Cespedesf1bfe202002-03-27 00:22:23 +010015#ifdef __powerpc__
16#include <sys/ptrace.h>
17#endif
18
Juan Cespedes03192f82009-07-03 10:16:22 +020019static void handle_signal(Event *event);
20static void handle_exit(Event *event);
21static void handle_exit_signal(Event *event);
22static void handle_syscall(Event *event);
23static void handle_arch_syscall(Event *event);
24static void handle_sysret(Event *event);
25static void handle_arch_sysret(Event *event);
26static void handle_clone(Event *event);
27static void handle_exec(Event *event);
28static void handle_breakpoint(Event *event);
29static void handle_new(Event *event);
Juan Cespedesa8909f72009-04-28 20:02:41 +020030static void remove_proc(Process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010031
Juan Cespedesa8909f72009-04-28 20:02:41 +020032static void callstack_push_syscall(Process *proc, int sysnum);
33static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020035static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036
Juan Cespedesbc8caf02009-05-07 19:38:38 +020037/* TODO */
38void * address_clone(void * addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020039 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020040 return addr;
41}
42
43void * breakpoint_clone(void * bp) {
44 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +020045 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020046 b = malloc(sizeof(Breakpoint));
47 if (!b) {
48 perror("malloc()");
49 exit(1);
50 }
51 memcpy(b, bp, sizeof(Breakpoint));
52 return b;
53}
54
55typedef struct Pending_New Pending_New;
56struct Pending_New {
57 pid_t pid;
58 Pending_New * next;
59};
60static Pending_New * pending_news = NULL;
61
62static int
63pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020064 Pending_New * p;
65
66 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
67
68 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +020069 while (p) {
70 if (p->pid == pid) {
71 return 1;
72 }
73 p = p->next;
74 }
75 return 0;
76}
77
78static void
79pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020080 Pending_New * p;
81
82 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
83
84 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +020085 if (!p) {
86 perror("malloc()");
87 exit(1);
88 }
89 p->pid = pid;
90 p->next = pending_news;
91 pending_news = p;
92}
93
94static void
95pending_new_remove(pid_t pid) {
96 Pending_New *p, *pred;
97
Juan Cespedescd8976d2009-05-14 13:47:58 +020098 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
99
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200100 p = pending_news;
101 if (p->pid == pid) {
102 pending_news = p->next;
103 free(p);
104 } else {
105 while (p) {
106 if (p->pid == pid) {
107 pred->next = p->next;
108 free(p);
109 }
110 pred = p;
111 p = p->next;
112 }
113 }
114}
115
116static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200117handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200118 Process *p;
119
Juan Cespedes03192f82009-07-03 10:16:22 +0200120 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200121
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200122 p = malloc(sizeof(Process));
123 if (!p) {
124 perror("malloc()");
125 exit(1);
126 }
127 memcpy(p, event->proc, sizeof(Process));
128 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
129 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200130 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200131
132 if (pending_new(p->pid)) {
133 pending_new_remove(p->pid);
134 if (p->breakpoint_being_enabled) {
135 enable_breakpoint(p->pid, p->breakpoint_being_enabled);
136 p->breakpoint_being_enabled = NULL;
137 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200138 if (event->proc->state == STATE_ATTACHED && options.follow) {
139 p->state = STATE_ATTACHED;
140 } else {
141 p->state = STATE_IGNORED;
142 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200143 continue_process(p->pid);
144 p->next = list_of_processes;
145 list_of_processes = p;
146 } else {
147 p->state = STATE_BEING_CREATED;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200148 p->next = list_of_processes;
149 list_of_processes = p;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200150 }
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200151 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200152}
153
154static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200155handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200156 Process * proc;
157
Juan Cespedes03192f82009-07-03 10:16:22 +0200158 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200159
160 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200161 if (!proc) {
162 pending_new_insert(event->e_un.newpid);
163 } else {
164 assert(proc->state == STATE_BEING_CREATED);
165 if (proc->breakpoint_being_enabled) {
166 enable_breakpoint(proc->pid, proc->breakpoint_being_enabled);
167 proc->breakpoint_being_enabled = NULL;
168 }
Juan Cespedes30439b42009-05-22 19:03:09 +0200169 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200170 proc->state = STATE_ATTACHED;
171 } else {
172 proc->state = STATE_IGNORED;
173 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200174 continue_process(proc->pid);
175 }
176}
177
Juan Cespedesf1350522008-12-16 18:19:58 +0100178static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200179shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100180 static char *signalent0[] = {
181#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100182 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100183 static char *signalent1[] = {
184#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100185 };
186 static char **signalents[] = { signalent0, signalent1 };
187 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 sizeof signalent1 / sizeof signalent1[0]
189 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190
Juan Cespedescd8976d2009-05-14 13:47:58 +0200191 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
192
Ian Wienand9a2ad352006-02-20 22:44:45 +0100193 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100195 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100196 return "UNKNOWN_SIGNAL";
197 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100198 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
200}
201
Juan Cespedesf1350522008-12-16 18:19:58 +0100202static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200203sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 static char *syscalent0[] = {
206#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 static char *syscalent1[] = {
209#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100210 };
211 static char **syscalents[] = { syscalent0, syscalent1 };
212 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 sizeof syscalent1 / sizeof syscalent1[0]
214 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100215
Juan Cespedescd8976d2009-05-14 13:47:58 +0200216 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
217
Ian Wienand9a2ad352006-02-20 22:44:45 +0100218 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100220 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100221 sprintf(result, "SYS_%d", sysnum);
222 return result;
223 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 sprintf(result, "SYS_%s",
225 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100226 return result;
227 }
228}
229
Juan Cespedesf1350522008-12-16 18:19:58 +0100230static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200231arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100232 static char result[128];
233 static char *arch_syscalent[] = {
234#include "arch_syscallent.h"
235 };
236 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
237
Juan Cespedescd8976d2009-05-14 13:47:58 +0200238 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
239
Juan Cespedes63184be2008-12-10 13:30:12 +0100240 if (sysnum < 0 || sysnum >= nsyscals) {
241 sprintf(result, "ARCH_%d", sysnum);
242 return result;
243 } else {
244 sprintf(result, "ARCH_%s",
245 arch_syscalent[sysnum]);
246 return result;
247 }
248}
249
Juan Cespedesf1350522008-12-16 18:19:58 +0100250void
Juan Cespedes03192f82009-07-03 10:16:22 +0200251handle_event(Event *event) {
252 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)", event->proc ? event->proc->pid : -1, event->type);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200253 switch (event->type) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200254 case EVENT_NONE:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 debug(1, "event: none");
256 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200257 case EVENT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 debug(1, "event: signal (%s [%d])",
259 shortsignal(event->proc, event->e_un.signum),
260 event->e_un.signum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200261 handle_signal(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200263 case EVENT_EXIT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100264 debug(1, "event: exit (%d)", event->e_un.ret_val);
Juan Cespedes03192f82009-07-03 10:16:22 +0200265 handle_exit(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100266 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200267 case EVENT_EXIT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 debug(1, "event: exit signal (%s [%d])",
269 shortsignal(event->proc, event->e_un.signum),
270 event->e_un.signum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200271 handle_exit_signal(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200273 case EVENT_SYSCALL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100274 debug(1, "event: syscall (%s [%d])",
275 sysname(event->proc, event->e_un.sysnum),
276 event->e_un.sysnum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200277 handle_syscall(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200279 case EVENT_SYSRET:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 debug(1, "event: sysret (%s [%d])",
281 sysname(event->proc, event->e_un.sysnum),
282 event->e_un.sysnum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200283 handle_sysret(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200285 case EVENT_ARCH_SYSCALL:
Juan Cespedes63184be2008-12-10 13:30:12 +0100286 debug(1, "event: arch_syscall (%s [%d])",
287 arch_sysname(event->proc, event->e_un.sysnum),
288 event->e_un.sysnum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200289 handle_arch_syscall(event);
Juan Cespedes63184be2008-12-10 13:30:12 +0100290 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200291 case EVENT_ARCH_SYSRET:
Juan Cespedes63184be2008-12-10 13:30:12 +0100292 debug(1, "event: arch_sysret (%s [%d])",
293 arch_sysname(event->proc, event->e_un.sysnum),
294 event->e_un.sysnum);
Juan Cespedes03192f82009-07-03 10:16:22 +0200295 handle_arch_sysret(event);
Juan Cespedes63184be2008-12-10 13:30:12 +0100296 return;
Juan Cespedes1e583132009-04-07 18:17:11 +0200297 case EVENT_CLONE:
298 debug(1, "event: clone (%u)", event->e_un.newpid);
Juan Cespedes03192f82009-07-03 10:16:22 +0200299 handle_clone(event);
Juan Cespedes1e583132009-04-07 18:17:11 +0200300 return;
301 case EVENT_EXEC:
302 debug(1, "event: exec()");
Juan Cespedes03192f82009-07-03 10:16:22 +0200303 handle_exec(event);
Juan Cespedes1e583132009-04-07 18:17:11 +0200304 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200305 case EVENT_BREAKPOINT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 debug(1, "event: breakpoint");
Juan Cespedes03192f82009-07-03 10:16:22 +0200307 handle_breakpoint(event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 return;
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200309 case EVENT_NEW:
310 debug(1, "event: new process");
Juan Cespedes03192f82009-07-03 10:16:22 +0200311 handle_new(event);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200312 return;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313 default:
314 fprintf(stderr, "Error! unknown event?\n");
315 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200316 }
317}
318
Juan Cespedesf1350522008-12-16 18:19:58 +0100319static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200320handle_signal(Event *event) {
321 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes28f60191998-04-12 00:04:39 +0200322 if (exiting && event->e_un.signum == SIGSTOP) {
323 pid_t pid = event->proc->pid;
324 disable_all_breakpoints(event->proc);
325 untrace_pid(pid);
326 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200327 return;
328 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200329 if (event->proc->state != STATE_IGNORED) {
330 output_line(event->proc, "--- %s (%s) ---",
331 shortsignal(event->proc, event->e_un.signum),
332 strsignal(event->e_un.signum));
333 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100334 continue_after_signal(event->proc->pid, event->e_un.signum);
335}
336
Juan Cespedesf1350522008-12-16 18:19:58 +0100337static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200338handle_exit(Event *event) {
339 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200340 if (event->proc->state != STATE_IGNORED) {
341 output_line(event->proc, "+++ exited (status %d) +++",
342 event->e_un.ret_val);
343 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100344 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345}
346
Juan Cespedesf1350522008-12-16 18:19:58 +0100347static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200348handle_exit_signal(Event *event) {
349 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200350 if (event->proc->state != STATE_IGNORED) {
351 output_line(event->proc, "+++ killed by %s +++",
352 shortsignal(event->proc, event->e_un.signum));
353 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100354 remove_proc(event->proc);
355}
356
Juan Cespedesf1350522008-12-16 18:19:58 +0100357static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200358remove_proc(Process *proc) {
359 Process *tmp, *tmp2;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100360
Juan Cespedescd8976d2009-05-14 13:47:58 +0200361 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200362
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100363 if (list_of_processes == proc) {
364 tmp = list_of_processes;
365 list_of_processes = list_of_processes->next;
366 free(tmp);
367 return;
368 }
369 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 while (tmp->next) {
371 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100372 tmp2 = tmp->next;
373 tmp->next = tmp->next->next;
374 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200375 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100376 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100377 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100378 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100379}
380
Juan Cespedesf1350522008-12-16 18:19:58 +0100381static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200382handle_syscall(Event *event) {
383 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200384 if (event->proc->state != STATE_IGNORED) {
385 if (options.syscalls) {
386 output_left(LT_TOF_SYSCALL, event->proc,
387 sysname(event->proc, event->e_un.sysnum));
388 }
389 if (event->proc->breakpoints_enabled == 0) {
390 enable_all_breakpoints(event->proc);
391 }
392 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100393 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100394 continue_process(event->proc->pid);
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200398handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200399 Process * proc = event->proc;
400 pid_t saved_pid;
401
Juan Cespedes03192f82009-07-03 10:16:22 +0200402 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200403 if (proc->state == STATE_IGNORED) {
404 untrace_pid(proc->pid);
405 remove_proc(proc);
406 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200407 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200408 output_line(proc, "--- Called exec() ---");
409 proc->mask_32bit = 0;
410 proc->personality = 0;
411 proc->arch_ptr = NULL;
412 free(proc->filename);
413 proc->filename = pid2name(proc->pid);
414 saved_pid = proc->pid;
415 proc->pid = 0;
416 breakpoints_init(proc);
417 proc->pid = saved_pid;
418 proc->callstack_depth = 0;
419 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200420}
421
422static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200423handle_arch_syscall(Event *event) {
424 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200425 if (event->proc->state != STATE_IGNORED) {
426 if (options.syscalls) {
427 output_left(LT_TOF_SYSCALL, event->proc,
428 arch_sysname(event->proc, event->e_un.sysnum));
429 }
430 if (event->proc->breakpoints_enabled == 0) {
431 enable_all_breakpoints(event->proc);
432 }
433 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes63184be2008-12-10 13:30:12 +0100434 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100435 continue_process(event->proc->pid);
436}
437
Juan Cespedesd65efa32003-02-03 00:22:30 +0100438struct timeval current_time_spent;
439
Juan Cespedesf1350522008-12-16 18:19:58 +0100440static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200441calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100442 struct timeval tv;
443 struct timezone tz;
444 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100445 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100446
Juan Cespedescd8976d2009-05-14 13:47:58 +0200447 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100448 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100449
450 gettimeofday(&tv, &tz);
451
452 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
453 if (tv.tv_usec >= elem->time_spent.tv_usec) {
454 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
455 } else {
456 diff.tv_sec++;
457 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
458 }
459 current_time_spent = diff;
460}
461
Juan Cespedesf1350522008-12-16 18:19:58 +0100462static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200463handle_sysret(Event *event) {
464 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200465 if (event->proc->state != STATE_IGNORED) {
466 if (opt_T || options.summary) {
467 calc_time_spent(event->proc);
468 }
469 callstack_pop(event->proc);
470 if (options.syscalls) {
471 output_right(LT_TOF_SYSCALLR, event->proc,
472 sysname(event->proc, event->e_un.sysnum));
473 }
Juan Cespedes21c63a12001-07-07 20:56:56 +0200474 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100475 continue_process(event->proc->pid);
476}
477
Juan Cespedesf1350522008-12-16 18:19:58 +0100478static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200479handle_arch_sysret(Event *event) {
480 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200481 if (event->proc->state != STATE_IGNORED) {
482 if (opt_T || options.summary) {
483 calc_time_spent(event->proc);
484 }
485 callstack_pop(event->proc);
486 if (options.syscalls) {
487 output_right(LT_TOF_SYSCALLR, event->proc,
488 arch_sysname(event->proc, event->e_un.sysnum));
489 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100490 }
491 continue_process(event->proc->pid);
492}
493
Juan Cespedesf1350522008-12-16 18:19:58 +0100494static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200495handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100496 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200497 Breakpoint *sbp;
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
508 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
509
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
Luis Machado55c5feb2008-03-12 15:56:01 +0100523 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100524 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100525 continue_enabling_breakpoint(event->proc->pid,
526 event->proc->
527 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100528 event->proc->breakpoint_being_enabled = NULL;
529 return;
530 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200531
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
533 if (event->e_un.brk_addr ==
534 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200535#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100536 /*
537 * PPC HACK! (XXX FIXME TODO)
538 * The PLT gets modified during the first call,
539 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100540 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100541 unsigned long a;
542 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100543 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200544 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200545
Paul Gilliam76c61f12006-06-14 06:55:21 +0200546 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100547 unsigned char break_insn[] = BREAKPOINT_VALUE;
548
549 sbp = address2bpstruct(event->proc, addr);
550 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
552 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100553
Paul Gilliam76c61f12006-06-14 06:55:21 +0200554 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100555 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100556 insert_breakpoint(event->proc, addr,
557 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100558 }
559 } else {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200560 sbp = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, libsym));
Ian Wienand9a2ad352006-02-20 22:44:45 +0100561 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200562 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100563 insert_breakpoint(event->proc, addr,
564 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200565 }
Ian Wienand3219f322006-02-16 06:00:00 +0100566 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100567#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200568 void *addr;
569 void *old_addr;
570 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200571 assert(sym);
572 old_addr = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, sym))->addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200573 addr=sym2addr(event->proc,sym);
574 assert(old_addr !=0 && addr !=0);
575 if(addr != old_addr){
576 struct library_symbol *new_sym;
577 new_sym=malloc(sizeof(*new_sym));
578 memcpy(new_sym,sym,sizeof(*new_sym));
579 new_sym->next=event->proc->list_of_symbols;
580 event->proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200581 insert_breakpoint(event->proc, addr, new_sym);
582 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200583#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100584 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100585 callstack_pop(event->proc);
586 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200587 if (event->proc->state != STATE_IGNORED) {
588 if (opt_T || options.summary) {
589 calc_time_spent(event->proc);
590 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100591 }
592 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100593 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200594 if (event->proc->state != STATE_IGNORED) {
595 output_right(LT_TOF_FUNCTIONR, event->proc,
596 event->proc->callstack[i].c_un.libfunc->name);
597 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100598 continue_after_breakpoint(event->proc,
Juan Cespedes5c682042009-05-21 15:59:56 +0200599 address2bpstruct(event->proc,
600 event->e_un.brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100601 return;
602 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100603 }
604
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100605 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200606 if (event->proc->state != STATE_IGNORED) {
607 event->proc->stack_pointer = get_stack_pointer(event->proc);
608 event->proc->return_addr =
609 get_return_addr(event->proc, event->proc->stack_pointer);
610 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
611 callstack_push_symfunc(event->proc, sbp->libsym);
612 }
Paul Gilliambe320772006-04-24 22:06:23 +0200613#ifdef PLT_REINITALISATION_BP
614 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
616 0))
617 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200618#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100619
620 continue_after_breakpoint(event->proc, sbp);
621 return;
622 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100623
Juan Cespedes5c682042009-05-21 15:59:56 +0200624 if (event->proc->state != STATE_IGNORED) {
625 output_line(event->proc, "unexpected breakpoint at %p",
626 (void *)event->e_un.brk_addr);
627 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100628 continue_process(event->proc->pid);
629}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200630
Juan Cespedesf1350522008-12-16 18:19:58 +0100631static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200632callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100633 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200634
Juan Cespedescd8976d2009-05-14 13:47:58 +0200635 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200636 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100637 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200638 fprintf(stderr, "Error: call nesting too deep!\n");
639 return;
640 }
641
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100642 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200643 elem->is_syscall = 1;
644 elem->c_un.syscall = sysnum;
645 elem->return_addr = NULL;
646
647 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200648 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100649 struct timezone tz;
650 gettimeofday(&elem->time_spent, &tz);
651 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200652}
653
Juan Cespedes21c63a12001-07-07 20:56:56 +0200654static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200655callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100656 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200657
Juan Cespedescd8976d2009-05-14 13:47:58 +0200658 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200659 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100660 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200661 fprintf(stderr, "Error: call nesting too deep!\n");
662 return;
663 }
664
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100665 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200666 elem->is_syscall = 0;
667 elem->c_un.libfunc = sym;
668
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200669 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200670 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200671 insert_breakpoint(proc, elem->return_addr, 0);
672 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200673
674 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200675 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100676 struct timezone tz;
677 gettimeofday(&elem->time_spent, &tz);
678 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200679}
680
Juan Cespedesf1350522008-12-16 18:19:58 +0100681static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200682callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100683 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200684 assert(proc->callstack_depth > 0);
685
Juan Cespedescd8976d2009-05-14 13:47:58 +0200686 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100687 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200688 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200689 delete_breakpoint(proc, elem->return_addr);
690 }
691 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200692}