blob: f56c537c3f22e061e6c3dc3e466dec21e97824c5 [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>
Petr Machata534e00f2011-09-27 17:58:38 +020010#include <errno.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010011
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010013
Juan Cespedesf1bfe202002-03-27 00:22:23 +010014#ifdef __powerpc__
15#include <sys/ptrace.h>
16#endif
17
Juan Cespedes03192f82009-07-03 10:16:22 +020018static void handle_signal(Event *event);
19static void handle_exit(Event *event);
20static void handle_exit_signal(Event *event);
21static void handle_syscall(Event *event);
22static void handle_arch_syscall(Event *event);
23static void handle_sysret(Event *event);
24static void handle_arch_sysret(Event *event);
25static void handle_clone(Event *event);
26static void handle_exec(Event *event);
27static void handle_breakpoint(Event *event);
28static void handle_new(Event *event);
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
Petr Machatacbe29c62011-09-27 02:27:58 +020039static Event *
40call_handler(Process * proc, Event * event)
41{
42 assert(proc != NULL);
43
44 Event_Handler * handler = proc->event_handler;
45 if (handler == NULL)
46 return event;
47
48 return (*handler->on_event) (handler, event);
49}
50
Juan Cespedes61da3372009-07-03 11:55:44 +020051void
52handle_event(Event *event) {
Petr Machata602330f2011-07-09 11:15:34 +020053 if (exiting == 1) {
54 exiting = 2;
55 debug(1, "ltrace about to exit");
56 ltrace_exiting();
57 }
Petr Machata26627682011-07-08 18:15:32 +020058 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
59 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020060
61 /* If the thread group or an individual task define an
62 overriding event handler, give them a chance to kick in.
63 We will end up calling both handlers, if the first one
64 doesn't sink the event. */
65 if (event->proc != NULL) {
66 event = call_handler(event->proc, event);
67 if (event == NULL)
68 /* It was handled. */
69 return;
70
71 /* Note: the previous handler has a chance to alter
72 * the event. */
73 if (event->proc->leader != NULL) {
74 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020075 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020076 return;
77 }
78 }
79
Juan Cespedes61da3372009-07-03 11:55:44 +020080 switch (event->type) {
81 case EVENT_NONE:
82 debug(1, "event: none");
83 return;
84 case EVENT_SIGNAL:
85 debug(1, "event: signal (%s [%d])",
86 shortsignal(event->proc, event->e_un.signum),
87 event->e_un.signum);
88 handle_signal(event);
89 return;
90 case EVENT_EXIT:
91 debug(1, "event: exit (%d)", event->e_un.ret_val);
92 handle_exit(event);
93 return;
94 case EVENT_EXIT_SIGNAL:
95 debug(1, "event: exit signal (%s [%d])",
96 shortsignal(event->proc, event->e_un.signum),
97 event->e_un.signum);
98 handle_exit_signal(event);
99 return;
100 case EVENT_SYSCALL:
101 debug(1, "event: syscall (%s [%d])",
102 sysname(event->proc, event->e_un.sysnum),
103 event->e_un.sysnum);
104 handle_syscall(event);
105 return;
106 case EVENT_SYSRET:
107 debug(1, "event: sysret (%s [%d])",
108 sysname(event->proc, event->e_un.sysnum),
109 event->e_un.sysnum);
110 handle_sysret(event);
111 return;
112 case EVENT_ARCH_SYSCALL:
113 debug(1, "event: arch_syscall (%s [%d])",
114 arch_sysname(event->proc, event->e_un.sysnum),
115 event->e_un.sysnum);
116 handle_arch_syscall(event);
117 return;
118 case EVENT_ARCH_SYSRET:
119 debug(1, "event: arch_sysret (%s [%d])",
120 arch_sysname(event->proc, event->e_un.sysnum),
121 event->e_un.sysnum);
122 handle_arch_sysret(event);
123 return;
124 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200125 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200126 debug(1, "event: clone (%u)", event->e_un.newpid);
127 handle_clone(event);
128 return;
129 case EVENT_EXEC:
130 debug(1, "event: exec()");
131 handle_exec(event);
132 return;
133 case EVENT_BREAKPOINT:
134 debug(1, "event: breakpoint");
135 handle_breakpoint(event);
136 return;
137 case EVENT_NEW:
138 debug(1, "event: new process");
139 handle_new(event);
140 return;
141 default:
142 fprintf(stderr, "Error! unknown event?\n");
143 exit(1);
144 }
145}
146
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200147/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200148static void *
Petr Machata534e00f2011-09-27 17:58:38 +0200149address_clone(void * addr, void * data)
150{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200151 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200152 return addr;
153}
154
Juan Cespedes61da3372009-07-03 11:55:44 +0200155static void *
Petr Machata534e00f2011-09-27 17:58:38 +0200156breakpoint_clone(void * bp, void * data)
157{
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200158 Breakpoint * b;
Petr Machata534e00f2011-09-27 17:58:38 +0200159 Dict * map = data;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200160 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200161 b = malloc(sizeof(Breakpoint));
162 if (!b) {
163 perror("malloc()");
164 exit(1);
165 }
166 memcpy(b, bp, sizeof(Breakpoint));
Petr Machata534e00f2011-09-27 17:58:38 +0200167 if (b->libsym != NULL) {
168 struct library_symbol * sym = dict_find_entry(map, b->libsym);
169 if (b->libsym == NULL) {
170 fprintf(stderr, "Can't find cloned symbol %s.\n",
171 b->libsym->name);
172 return NULL;
173 }
174 b->libsym = sym;
175 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200176 return b;
177}
178
179typedef struct Pending_New Pending_New;
180struct Pending_New {
181 pid_t pid;
182 Pending_New * next;
183};
184static Pending_New * pending_news = NULL;
185
186static int
187pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200188 Pending_New * p;
189
190 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
191
192 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200193 while (p) {
194 if (p->pid == pid) {
195 return 1;
196 }
197 p = p->next;
198 }
199 return 0;
200}
201
202static void
203pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200204 Pending_New * p;
205
206 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
207
208 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200209 if (!p) {
210 perror("malloc()");
211 exit(1);
212 }
213 p->pid = pid;
214 p->next = pending_news;
215 pending_news = p;
216}
217
218static void
219pending_new_remove(pid_t pid) {
220 Pending_New *p, *pred;
221
Juan Cespedescd8976d2009-05-14 13:47:58 +0200222 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
223
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200224 p = pending_news;
225 if (p->pid == pid) {
226 pending_news = p->next;
227 free(p);
228 } else {
229 while (p) {
230 if (p->pid == pid) {
231 pred->next = p->next;
232 free(p);
233 }
234 pred = p;
235 p = p->next;
236 }
237 }
238}
239
Petr Machata534e00f2011-09-27 17:58:38 +0200240static int
241clone_breakpoints(Process * proc, Process * orig_proc)
242{
243 /* When copying breakpoints, we also have to copy the
244 * referenced symbols, and link them properly. */
245 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
246 struct library_symbol * it = proc->list_of_symbols;
247 proc->list_of_symbols = NULL;
248 for (; it != NULL; it = it->next) {
249 struct library_symbol * libsym = clone_library_symbol(it);
250 if (libsym == NULL) {
251 int save_errno;
252 err:
253 save_errno = errno;
254 destroy_library_symbol_chain(proc->list_of_symbols);
255 dict_clear(map);
256 errno = save_errno;
257 return -1;
258 }
259 libsym->next = proc->list_of_symbols;
260 proc->list_of_symbols = libsym;
261 if (dict_enter(map, it, libsym) != 0)
262 goto err;
263 }
264
265 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
266 address_clone, breakpoint_clone, map);
267 if (proc->breakpoints == NULL)
268 goto err;
269
270 dict_clear(map);
271 return 0;
272}
273
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200274static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200275handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200276 Process *p;
277
Juan Cespedes03192f82009-07-03 10:16:22 +0200278 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200279
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200280 p = malloc(sizeof(Process));
281 if (!p) {
282 perror("malloc()");
283 exit(1);
284 }
285 memcpy(p, event->proc, sizeof(Process));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200286 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200287 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200288
Petr Machata75dcf7d2011-10-06 14:30:19 +0200289 /* We save register values to the arch pointer, and these need
290 to be per-thread. */
291 p->arch_ptr = NULL;
292
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200293 if (pending_new(p->pid)) {
294 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200295 if (p->event_handler != NULL)
296 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200297 if (event->proc->state == STATE_ATTACHED && options.follow) {
298 p->state = STATE_ATTACHED;
299 } else {
300 p->state = STATE_IGNORED;
301 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200302 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200303 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200304 } else {
305 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200306 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200307 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200308
Petr Machata534e00f2011-09-27 17:58:38 +0200309 if (p->leader == p)
Petr Machataba9911f2011-09-27 21:09:47 +0200310 clone_breakpoints(p, event->proc->leader);
Petr Machata534e00f2011-09-27 17:58:38 +0200311 else
312 /* Thread groups share breakpoints. */
313 p->breakpoints = NULL;
314
Petr Machatacbe29c62011-09-27 02:27:58 +0200315 if (event->type == EVENT_VFORK)
316 continue_after_vfork(p);
317 else
318 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200319}
320
321static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200322handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200323 Process * proc;
324
Juan Cespedes03192f82009-07-03 10:16:22 +0200325 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200326
327 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200328 if (!proc) {
329 pending_new_insert(event->e_un.newpid);
330 } else {
331 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200332 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200333 proc->state = STATE_ATTACHED;
334 } else {
335 proc->state = STATE_IGNORED;
336 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200337 continue_process(proc->pid);
338 }
339}
340
Juan Cespedesf1350522008-12-16 18:19:58 +0100341static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200342shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 static char *signalent0[] = {
344#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 static char *signalent1[] = {
347#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100348 };
349 static char **signalents[] = { signalent0, signalent1 };
350 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 sizeof signalent1 / sizeof signalent1[0]
352 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100353
Juan Cespedescd8976d2009-05-14 13:47:58 +0200354 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
355
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100358 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100359 return "UNKNOWN_SIGNAL";
360 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100361 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 }
363}
364
Juan Cespedesf1350522008-12-16 18:19:58 +0100365static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200366sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100367 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 static char *syscalent0[] = {
369#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100370 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 static char *syscalent1[] = {
372#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100373 };
374 static char **syscalents[] = { syscalent0, syscalent1 };
375 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 sizeof syscalent1 / sizeof syscalent1[0]
377 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100378
Juan Cespedescd8976d2009-05-14 13:47:58 +0200379 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
380
Ian Wienand9a2ad352006-02-20 22:44:45 +0100381 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100383 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100384 sprintf(result, "SYS_%d", sysnum);
385 return result;
386 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100387 sprintf(result, "SYS_%s",
388 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100389 return result;
390 }
391}
392
Juan Cespedesf1350522008-12-16 18:19:58 +0100393static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200394arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100395 static char result[128];
396 static char *arch_syscalent[] = {
397#include "arch_syscallent.h"
398 };
399 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
400
Juan Cespedescd8976d2009-05-14 13:47:58 +0200401 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
402
Juan Cespedes63184be2008-12-10 13:30:12 +0100403 if (sysnum < 0 || sysnum >= nsyscals) {
404 sprintf(result, "ARCH_%d", sysnum);
405 return result;
406 } else {
407 sprintf(result, "ARCH_%s",
408 arch_syscalent[sysnum]);
409 return result;
410 }
411}
412
Juan Cespedesf1350522008-12-16 18:19:58 +0100413static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200414handle_signal(Event *event) {
415 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800416 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200417 output_line(event->proc, "--- %s (%s) ---",
418 shortsignal(event->proc, event->e_un.signum),
419 strsignal(event->e_un.signum));
420 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100421 continue_after_signal(event->proc->pid, event->e_un.signum);
422}
423
Juan Cespedesf1350522008-12-16 18:19:58 +0100424static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200425handle_exit(Event *event) {
426 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200427 if (event->proc->state != STATE_IGNORED) {
428 output_line(event->proc, "+++ exited (status %d) +++",
429 event->e_un.ret_val);
430 }
Petr Machatacebb8842011-07-09 11:14:11 +0200431 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100432}
433
Juan Cespedesf1350522008-12-16 18:19:58 +0100434static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200435handle_exit_signal(Event *event) {
436 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200437 if (event->proc->state != STATE_IGNORED) {
438 output_line(event->proc, "+++ killed by %s +++",
439 shortsignal(event->proc, event->e_un.signum));
440 }
Petr Machatacebb8842011-07-09 11:14:11 +0200441 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100442}
443
Juan Cespedesf1350522008-12-16 18:19:58 +0100444static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200445handle_syscall(Event *event) {
446 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200447 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100448 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200449 if (options.syscalls) {
450 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200451 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200452 }
453 if (event->proc->breakpoints_enabled == 0) {
454 enable_all_breakpoints(event->proc);
455 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100456 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100457 continue_process(event->proc->pid);
458}
459
Juan Cespedesf1350522008-12-16 18:19:58 +0100460static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200461handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200462 Process * proc = event->proc;
463 pid_t saved_pid;
464
Juan Cespedes03192f82009-07-03 10:16:22 +0200465 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200466 if (proc->state == STATE_IGNORED) {
467 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200468 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200469 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200471 output_line(proc, "--- Called exec() ---");
472 proc->mask_32bit = 0;
473 proc->personality = 0;
474 proc->arch_ptr = NULL;
475 free(proc->filename);
476 proc->filename = pid2name(proc->pid);
477 saved_pid = proc->pid;
478 proc->pid = 0;
Petr Machatac7585b62011-07-08 22:58:12 +0200479 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200480 proc->pid = saved_pid;
481 proc->callstack_depth = 0;
482 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200483}
484
485static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200486handle_arch_syscall(Event *event) {
487 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200488 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100489 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200490 if (options.syscalls) {
491 output_left(LT_TOF_SYSCALL, event->proc,
492 arch_sysname(event->proc, event->e_un.sysnum));
493 }
494 if (event->proc->breakpoints_enabled == 0) {
495 enable_all_breakpoints(event->proc);
496 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100497 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100498 continue_process(event->proc->pid);
499}
500
Juan Cespedesd65efa32003-02-03 00:22:30 +0100501struct timeval current_time_spent;
502
Juan Cespedesf1350522008-12-16 18:19:58 +0100503static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200504calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100505 struct timeval tv;
506 struct timezone tz;
507 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100508 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100509
Juan Cespedescd8976d2009-05-14 13:47:58 +0200510 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100512
513 gettimeofday(&tv, &tz);
514
515 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
516 if (tv.tv_usec >= elem->time_spent.tv_usec) {
517 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
518 } else {
519 diff.tv_sec++;
520 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
521 }
522 current_time_spent = diff;
523}
524
Juan Cespedesf1350522008-12-16 18:19:58 +0100525static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200526handle_sysret(Event *event) {
527 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200528 if (event->proc->state != STATE_IGNORED) {
529 if (opt_T || options.summary) {
530 calc_time_spent(event->proc);
531 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200532 if (options.syscalls) {
533 output_right(LT_TOF_SYSCALLR, event->proc,
534 sysname(event->proc, event->e_un.sysnum));
535 }
Petr Machata211f0882010-11-03 18:42:18 +0100536 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200537 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100538 continue_process(event->proc->pid);
539}
540
Juan Cespedesf1350522008-12-16 18:19:58 +0100541static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200542handle_arch_sysret(Event *event) {
543 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200544 if (event->proc->state != STATE_IGNORED) {
545 if (opt_T || options.summary) {
546 calc_time_spent(event->proc);
547 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200548 if (options.syscalls) {
549 output_right(LT_TOF_SYSCALLR, event->proc,
550 arch_sysname(event->proc, event->e_un.sysnum));
551 }
Petr Machata211f0882010-11-03 18:42:18 +0100552 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100553 }
554 continue_process(event->proc->pid);
555}
556
Petr Machata067322d2010-10-25 14:47:55 +0200557#ifdef __powerpc__
558void *get_count_register (Process *proc);
559#endif
560
Juan Cespedesf1350522008-12-16 18:19:58 +0100561static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200562handle_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100563 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200564 Breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200565 Process *leader = event->proc->leader;
566
567 /* The leader has terminated. */
568 if (leader == NULL) {
569 continue_process(event->proc->pid);
570 return;
571 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100572
Juan Cespedes03192f82009-07-03 10:16:22 +0200573 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200574 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100575
Paul Gilliam76c61f12006-06-14 06:55:21 +0200576#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100577 /* Need to skip following NOP's to prevent a fake function from being stacked. */
578 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200579 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100580 char nop_instruction[] = PPC_NOP;
581
Petr Machata9a5420c2011-07-09 11:21:23 +0200582 stub_bp = address2bpstruct(leader, event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100583
584 if (stub_bp) {
585 unsigned char *bp_instruction = stub_bp->orig_value;
586
587 if (memcmp(bp_instruction, nop_instruction,
588 PPC_NOP_LENGTH) == 0) {
589 if (stub_addr != (long) event->e_un.brk_addr) {
590 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
591 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200592 return;
593 }
594 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100595 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200596#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200597
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100598 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
599 if (event->e_un.brk_addr ==
600 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200601#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100602 /*
603 * PPC HACK! (XXX FIXME TODO)
604 * The PLT gets modified during the first call,
605 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100607 unsigned long a;
608 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100609 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200610 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200611
Paul Gilliam76c61f12006-06-14 06:55:21 +0200612 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100613 unsigned char break_insn[] = BREAKPOINT_VALUE;
614
Petr Machata9a5420c2011-07-09 11:21:23 +0200615 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100616 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100617 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
618 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100619
Paul Gilliam76c61f12006-06-14 06:55:21 +0200620 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100621 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100622 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200623 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100624 }
625 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200626 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200627 /* On powerpc, the breakpoint address
628 may end up being actual entry point
629 of the library symbol, not the PLT
630 address we computed. In that case,
631 sbp is NULL. */
632 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100633 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200634 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200635 }
Ian Wienand3219f322006-02-16 06:00:00 +0100636 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100637#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500638 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200639 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500640 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200641 assert(sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200642 addr = sym2addr(leader, sym);
643 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500644 if (sbp) {
645 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200646 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500647 }
648 } else {
649 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
650 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200651 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200652 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200653 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200654 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200655#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100656 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100657 callstack_pop(event->proc);
658 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200659 if (event->proc->state != STATE_IGNORED) {
660 if (opt_T || options.summary) {
661 calc_time_spent(event->proc);
662 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100663 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100664 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200665 if (event->proc->state != STATE_IGNORED) {
666 output_right(LT_TOF_FUNCTIONR, event->proc,
667 event->proc->callstack[i].c_un.libfunc->name);
668 }
Petr Machata211f0882010-11-03 18:42:18 +0100669 callstack_pop(event->proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200670 sbp = address2bpstruct(leader, event->e_un.brk_addr);
671 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100672 return;
673 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100674 }
675
Petr Machata9a5420c2011-07-09 11:21:23 +0200676 if ((sbp = address2bpstruct(leader, event->e_un.brk_addr))) {
677 if (sbp->libsym == NULL) {
678 continue_after_breakpoint(event->proc, sbp);
679 return;
680 }
681
Joe Damatof0bd98b2010-11-08 15:47:42 -0800682 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200683 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200684 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800685 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200686
Zachary T Welch97baa652010-12-08 13:34:03 -0800687 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200688 event->proc->stack_pointer = get_stack_pointer(event->proc);
689 event->proc->return_addr =
690 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200691 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100692 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200693 }
Paul Gilliambe320772006-04-24 22:06:23 +0200694#ifdef PLT_REINITALISATION_BP
695 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100696 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
697 0))
Petr Machata9a5420c2011-07-09 11:21:23 +0200698 reinitialize_breakpoints(leader);
Paul Gilliambe320772006-04-24 22:06:23 +0200699#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100700
701 continue_after_breakpoint(event->proc, sbp);
702 return;
703 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100704
Joe Damatofa2aefc2010-10-30 19:56:50 -0700705 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200706 output_line(event->proc, "unexpected breakpoint at %p",
707 (void *)event->e_un.brk_addr);
708 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100709 continue_process(event->proc->pid);
710}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200711
Juan Cespedesf1350522008-12-16 18:19:58 +0100712static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200713callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100714 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200715
Juan Cespedescd8976d2009-05-14 13:47:58 +0200716 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200717 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100718 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500719 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
720 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200721 return;
722 }
723
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100724 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200725 elem->is_syscall = 1;
726 elem->c_un.syscall = sysnum;
727 elem->return_addr = NULL;
728
729 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200730 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100731 struct timezone tz;
732 gettimeofday(&elem->time_spent, &tz);
733 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200734}
735
Juan Cespedes21c63a12001-07-07 20:56:56 +0200736static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200737callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500738 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200739
Juan Cespedescd8976d2009-05-14 13:47:58 +0200740 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200741 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100742 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500743 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
744 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200745 return;
746 }
747
Arnaud Patard26570082010-01-08 08:40:12 -0500748 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100749 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200750 elem->is_syscall = 0;
751 elem->c_un.libfunc = sym;
752
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200753 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200754 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200755 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200756 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200757
Arnaud Patard26570082010-01-08 08:40:12 -0500758 /* handle functions like atexit() on mips which have no return */
759 if (elem->return_addr != prev->return_addr)
760 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200761 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100762 struct timezone tz;
763 gettimeofday(&elem->time_spent, &tz);
764 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200765}
766
Juan Cespedesf1350522008-12-16 18:19:58 +0100767static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200768callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100769 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200770 assert(proc->callstack_depth > 0);
771
Juan Cespedescd8976d2009-05-14 13:47:58 +0200772 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100773 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200774 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200775 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200776 delete_breakpoint(proc, elem->return_addr);
777 }
Petr Machata211f0882010-11-03 18:42:18 +0100778 if (elem->arch_ptr != NULL) {
779 free(elem->arch_ptr);
780 elem->arch_ptr = NULL;
781 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200782 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200783}