blob: e3d3d0aa46f06dc4416e1ac6a2bd3987d29e21ac [file] [log] [blame]
Petr Machatafed1e8d2012-02-07 02:06:29 +01001#define _GNU_SOURCE
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02003
Juan Cespedes5e01f651998-03-08 22:31:44 +01004#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 Cespedesf1bfe202002-03-27 00:22:23 +010012#ifdef __powerpc__
13#include <sys/ptrace.h>
14#endif
15
Petr Machatafed1e8d2012-02-07 02:06:29 +010016#include "common.h"
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. */
Petr Machata43d2fe52011-11-02 13:25:49 +010073 if (event->proc != NULL
74 && event->proc->leader != NULL
75 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +020076 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020077 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020078 return;
79 }
80 }
81
Juan Cespedes61da3372009-07-03 11:55:44 +020082 switch (event->type) {
83 case EVENT_NONE:
84 debug(1, "event: none");
85 return;
86 case EVENT_SIGNAL:
87 debug(1, "event: signal (%s [%d])",
88 shortsignal(event->proc, event->e_un.signum),
89 event->e_un.signum);
90 handle_signal(event);
91 return;
92 case EVENT_EXIT:
93 debug(1, "event: exit (%d)", event->e_un.ret_val);
94 handle_exit(event);
95 return;
96 case EVENT_EXIT_SIGNAL:
97 debug(1, "event: exit signal (%s [%d])",
98 shortsignal(event->proc, event->e_un.signum),
99 event->e_un.signum);
100 handle_exit_signal(event);
101 return;
102 case EVENT_SYSCALL:
103 debug(1, "event: syscall (%s [%d])",
104 sysname(event->proc, event->e_un.sysnum),
105 event->e_un.sysnum);
106 handle_syscall(event);
107 return;
108 case EVENT_SYSRET:
109 debug(1, "event: sysret (%s [%d])",
110 sysname(event->proc, event->e_un.sysnum),
111 event->e_un.sysnum);
112 handle_sysret(event);
113 return;
114 case EVENT_ARCH_SYSCALL:
115 debug(1, "event: arch_syscall (%s [%d])",
116 arch_sysname(event->proc, event->e_un.sysnum),
117 event->e_un.sysnum);
118 handle_arch_syscall(event);
119 return;
120 case EVENT_ARCH_SYSRET:
121 debug(1, "event: arch_sysret (%s [%d])",
122 arch_sysname(event->proc, event->e_un.sysnum),
123 event->e_un.sysnum);
124 handle_arch_sysret(event);
125 return;
126 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200127 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200128 debug(1, "event: clone (%u)", event->e_un.newpid);
129 handle_clone(event);
130 return;
131 case EVENT_EXEC:
132 debug(1, "event: exec()");
133 handle_exec(event);
134 return;
135 case EVENT_BREAKPOINT:
136 debug(1, "event: breakpoint");
137 handle_breakpoint(event);
138 return;
139 case EVENT_NEW:
140 debug(1, "event: new process");
141 handle_new(event);
142 return;
143 default:
144 fprintf(stderr, "Error! unknown event?\n");
145 exit(1);
146 }
147}
148
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200149/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200150static void *
Petr Machata534e00f2011-09-27 17:58:38 +0200151address_clone(void * addr, void * data)
152{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200153 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200154 return addr;
155}
156
Juan Cespedes61da3372009-07-03 11:55:44 +0200157static void *
Petr Machatafed1e8d2012-02-07 02:06:29 +0100158breakpoint_clone(void *bp, void *data)
Petr Machata534e00f2011-09-27 17:58:38 +0200159{
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200160 Breakpoint * b;
Petr Machatafed1e8d2012-02-07 02:06:29 +0100161 Dict *map = data;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200162 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200163 b = malloc(sizeof(Breakpoint));
164 if (!b) {
165 perror("malloc()");
166 exit(1);
167 }
Petr Machatafed1e8d2012-02-07 02:06:29 +0100168 memcpy(b, bp, sizeof(*b));
Petr Machata534e00f2011-09-27 17:58:38 +0200169 if (b->libsym != NULL) {
Petr Machatafed1e8d2012-02-07 02:06:29 +0100170 struct library_symbol *sym = dict_find_entry(map, b->libsym);
Petr Machata534e00f2011-09-27 17:58:38 +0200171 if (b->libsym == NULL) {
172 fprintf(stderr, "Can't find cloned symbol %s.\n",
173 b->libsym->name);
174 return NULL;
175 }
176 b->libsym = sym;
177 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200178 return b;
179}
180
181typedef struct Pending_New Pending_New;
182struct Pending_New {
183 pid_t pid;
184 Pending_New * next;
185};
186static Pending_New * pending_news = NULL;
187
188static int
189pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200190 Pending_New * p;
191
192 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
193
194 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200195 while (p) {
196 if (p->pid == pid) {
197 return 1;
198 }
199 p = p->next;
200 }
201 return 0;
202}
203
204static void
205pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200206 Pending_New * p;
207
208 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
209
210 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200211 if (!p) {
212 perror("malloc()");
213 exit(1);
214 }
215 p->pid = pid;
216 p->next = pending_news;
217 pending_news = p;
218}
219
220static void
221pending_new_remove(pid_t pid) {
222 Pending_New *p, *pred;
223
Juan Cespedescd8976d2009-05-14 13:47:58 +0200224 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
225
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200226 p = pending_news;
227 if (p->pid == pid) {
228 pending_news = p->next;
229 free(p);
230 } else {
231 while (p) {
232 if (p->pid == pid) {
233 pred->next = p->next;
234 free(p);
235 }
236 pred = p;
237 p = p->next;
238 }
239 }
240}
241
Petr Machata534e00f2011-09-27 17:58:38 +0200242static int
243clone_breakpoints(Process * proc, Process * orig_proc)
244{
245 /* When copying breakpoints, we also have to copy the
246 * referenced symbols, and link them properly. */
247 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
248 struct library_symbol * it = proc->list_of_symbols;
249 proc->list_of_symbols = NULL;
250 for (; it != NULL; it = it->next) {
251 struct library_symbol * libsym = clone_library_symbol(it);
252 if (libsym == NULL) {
253 int save_errno;
254 err:
255 save_errno = errno;
256 destroy_library_symbol_chain(proc->list_of_symbols);
257 dict_clear(map);
258 errno = save_errno;
259 return -1;
260 }
261 libsym->next = proc->list_of_symbols;
262 proc->list_of_symbols = libsym;
263 if (dict_enter(map, it, libsym) != 0)
264 goto err;
265 }
266
267 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
268 address_clone, breakpoint_clone, map);
269 if (proc->breakpoints == NULL)
270 goto err;
271
272 dict_clear(map);
273 return 0;
274}
275
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200276static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200277handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200278 Process *p;
279
Juan Cespedes03192f82009-07-03 10:16:22 +0200280 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200281
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200282 p = malloc(sizeof(Process));
283 if (!p) {
284 perror("malloc()");
285 exit(1);
286 }
287 memcpy(p, event->proc, sizeof(Process));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200288 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200289 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200290
Petr Machata75dcf7d2011-10-06 14:30:19 +0200291 /* We save register values to the arch pointer, and these need
292 to be per-thread. */
293 p->arch_ptr = NULL;
294
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200295 if (pending_new(p->pid)) {
296 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200297 if (p->event_handler != NULL)
298 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200299 if (event->proc->state == STATE_ATTACHED && options.follow) {
300 p->state = STATE_ATTACHED;
301 } else {
302 p->state = STATE_IGNORED;
303 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200304 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200305 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200306 } else {
307 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200308 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200309 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200310
Petr Machata534e00f2011-09-27 17:58:38 +0200311 if (p->leader == p)
Petr Machataba9911f2011-09-27 21:09:47 +0200312 clone_breakpoints(p, event->proc->leader);
Petr Machata534e00f2011-09-27 17:58:38 +0200313 else
314 /* Thread groups share breakpoints. */
315 p->breakpoints = NULL;
316
Petr Machatacbe29c62011-09-27 02:27:58 +0200317 if (event->type == EVENT_VFORK)
318 continue_after_vfork(p);
319 else
320 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200321}
322
323static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200324handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200325 Process * proc;
326
Juan Cespedes03192f82009-07-03 10:16:22 +0200327 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200328
329 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200330 if (!proc) {
331 pending_new_insert(event->e_un.newpid);
332 } else {
333 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200334 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200335 proc->state = STATE_ATTACHED;
336 } else {
337 proc->state = STATE_IGNORED;
338 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200339 continue_process(proc->pid);
340 }
341}
342
Juan Cespedesf1350522008-12-16 18:19:58 +0100343static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200344shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 static char *signalent0[] = {
346#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100347 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 static char *signalent1[] = {
349#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100350 };
351 static char **signalents[] = { signalent0, signalent1 };
352 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100353 sizeof signalent1 / sizeof signalent1[0]
354 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100355
Juan Cespedescd8976d2009-05-14 13:47:58 +0200356 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
357
Ian Wienand9a2ad352006-02-20 22:44:45 +0100358 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100359 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100360 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100361 return "UNKNOWN_SIGNAL";
362 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100363 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100364 }
365}
366
Juan Cespedesf1350522008-12-16 18:19:58 +0100367static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200368sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100369 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 static char *syscalent0[] = {
371#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100372 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 static char *syscalent1[] = {
374#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100375 };
376 static char **syscalents[] = { syscalent0, syscalent1 };
377 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100378 sizeof syscalent1 / sizeof syscalent1[0]
379 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100380
Juan Cespedescd8976d2009-05-14 13:47:58 +0200381 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
382
Ian Wienand9a2ad352006-02-20 22:44:45 +0100383 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100385 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100386 sprintf(result, "SYS_%d", sysnum);
387 return result;
388 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 sprintf(result, "SYS_%s",
390 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100391 return result;
392 }
393}
394
Juan Cespedesf1350522008-12-16 18:19:58 +0100395static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200396arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100397 static char result[128];
398 static char *arch_syscalent[] = {
399#include "arch_syscallent.h"
400 };
401 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
402
Juan Cespedescd8976d2009-05-14 13:47:58 +0200403 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
404
Juan Cespedes63184be2008-12-10 13:30:12 +0100405 if (sysnum < 0 || sysnum >= nsyscals) {
406 sprintf(result, "ARCH_%d", sysnum);
407 return result;
408 } else {
409 sprintf(result, "ARCH_%s",
410 arch_syscalent[sysnum]);
411 return result;
412 }
413}
414
Juan Cespedesf1350522008-12-16 18:19:58 +0100415static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200416handle_signal(Event *event) {
417 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800418 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200419 output_line(event->proc, "--- %s (%s) ---",
420 shortsignal(event->proc, event->e_un.signum),
421 strsignal(event->e_un.signum));
422 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100423 continue_after_signal(event->proc->pid, event->e_un.signum);
424}
425
Juan Cespedesf1350522008-12-16 18:19:58 +0100426static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200427handle_exit(Event *event) {
428 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200429 if (event->proc->state != STATE_IGNORED) {
430 output_line(event->proc, "+++ exited (status %d) +++",
431 event->e_un.ret_val);
432 }
Petr Machatacebb8842011-07-09 11:14:11 +0200433 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100434}
435
Juan Cespedesf1350522008-12-16 18:19:58 +0100436static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200437handle_exit_signal(Event *event) {
438 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200439 if (event->proc->state != STATE_IGNORED) {
440 output_line(event->proc, "+++ killed by %s +++",
441 shortsignal(event->proc, event->e_un.signum));
442 }
Petr Machatacebb8842011-07-09 11:14:11 +0200443 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100444}
445
Juan Cespedesf1350522008-12-16 18:19:58 +0100446static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200447handle_syscall(Event *event) {
448 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200449 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100450 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200451 if (options.syscalls) {
452 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200453 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200454 }
455 if (event->proc->breakpoints_enabled == 0) {
456 enable_all_breakpoints(event->proc);
457 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100458 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100459 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100460}
461
Juan Cespedesf1350522008-12-16 18:19:58 +0100462static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200463handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200464 Process * proc = event->proc;
465 pid_t saved_pid;
466
Juan Cespedes03192f82009-07-03 10:16:22 +0200467 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200468 if (proc->state == STATE_IGNORED) {
469 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200470 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200471 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200472 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200473 output_line(proc, "--- Called exec() ---");
474 proc->mask_32bit = 0;
475 proc->personality = 0;
476 proc->arch_ptr = NULL;
477 free(proc->filename);
478 proc->filename = pid2name(proc->pid);
479 saved_pid = proc->pid;
480 proc->pid = 0;
Petr Machatac7585b62011-07-08 22:58:12 +0200481 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200482 proc->pid = saved_pid;
483 proc->callstack_depth = 0;
484 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200485}
486
487static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200488handle_arch_syscall(Event *event) {
489 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200490 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100491 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200492 if (options.syscalls) {
493 output_left(LT_TOF_SYSCALL, event->proc,
494 arch_sysname(event->proc, event->e_un.sysnum));
495 }
496 if (event->proc->breakpoints_enabled == 0) {
497 enable_all_breakpoints(event->proc);
498 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100499 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100500 continue_process(event->proc->pid);
501}
502
Juan Cespedesd65efa32003-02-03 00:22:30 +0100503struct timeval current_time_spent;
504
Juan Cespedesf1350522008-12-16 18:19:58 +0100505static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200506calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100507 struct timeval tv;
508 struct timezone tz;
509 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100511
Juan Cespedescd8976d2009-05-14 13:47:58 +0200512 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100514
515 gettimeofday(&tv, &tz);
516
517 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
518 if (tv.tv_usec >= elem->time_spent.tv_usec) {
519 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
520 } else {
521 diff.tv_sec++;
522 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
523 }
524 current_time_spent = diff;
525}
526
Juan Cespedesf1350522008-12-16 18:19:58 +0100527static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200528handle_sysret(Event *event) {
529 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200530 if (event->proc->state != STATE_IGNORED) {
531 if (opt_T || options.summary) {
532 calc_time_spent(event->proc);
533 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200534 if (options.syscalls) {
535 output_right(LT_TOF_SYSCALLR, event->proc,
536 sysname(event->proc, event->e_un.sysnum));
537 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100538 assert(event->proc->callstack_depth > 0);
539 unsigned d = event->proc->callstack_depth - 1;
540 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100541 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200542 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100543 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100544}
545
Juan Cespedesf1350522008-12-16 18:19:58 +0100546static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200547handle_arch_sysret(Event *event) {
548 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200549 if (event->proc->state != STATE_IGNORED) {
550 if (opt_T || options.summary) {
551 calc_time_spent(event->proc);
552 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200553 if (options.syscalls) {
554 output_right(LT_TOF_SYSCALLR, event->proc,
555 arch_sysname(event->proc, event->e_un.sysnum));
556 }
Petr Machata211f0882010-11-03 18:42:18 +0100557 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100558 }
559 continue_process(event->proc->pid);
560}
561
Petr Machata067322d2010-10-25 14:47:55 +0200562#ifdef __powerpc__
563void *get_count_register (Process *proc);
564#endif
565
Juan Cespedesf1350522008-12-16 18:19:58 +0100566static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100567handle_breakpoint(Event *event)
568{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100569 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200570 Breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200571 Process *leader = event->proc->leader;
572
573 /* The leader has terminated. */
574 if (leader == NULL) {
575 continue_process(event->proc->pid);
576 return;
577 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100578
Juan Cespedes03192f82009-07-03 10:16:22 +0200579 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200580 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100581
Paul Gilliam76c61f12006-06-14 06:55:21 +0200582#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100583 /* Need to skip following NOP's to prevent a fake function from being stacked. */
584 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200585 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100586 char nop_instruction[] = PPC_NOP;
587
Petr Machata9a5420c2011-07-09 11:21:23 +0200588 stub_bp = address2bpstruct(leader, event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100589
590 if (stub_bp) {
591 unsigned char *bp_instruction = stub_bp->orig_value;
592
593 if (memcmp(bp_instruction, nop_instruction,
594 PPC_NOP_LENGTH) == 0) {
595 if (stub_addr != (long) event->e_un.brk_addr) {
596 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
597 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200598 return;
599 }
600 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100601 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200602#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200603
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
605 if (event->e_un.brk_addr ==
606 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200607#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100608 /*
609 * PPC HACK! (XXX FIXME TODO)
610 * The PLT gets modified during the first call,
611 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100612 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100613 unsigned long a;
614 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200616 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200617
Paul Gilliam76c61f12006-06-14 06:55:21 +0200618 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100619 unsigned char break_insn[] = BREAKPOINT_VALUE;
620
Petr Machata9a5420c2011-07-09 11:21:23 +0200621 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100622 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100623 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
624 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100625
Paul Gilliam76c61f12006-06-14 06:55:21 +0200626 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100627 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100628 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200629 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100630 }
631 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200632 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200633 /* On powerpc, the breakpoint address
634 may end up being actual entry point
635 of the library symbol, not the PLT
636 address we computed. In that case,
637 sbp is NULL. */
638 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100639 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200640 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200641 }
Ian Wienand3219f322006-02-16 06:00:00 +0100642 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100643#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500644 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200645 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500646 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200647 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100648 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200649 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500650 if (sbp) {
651 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200652 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500653 }
654 } else {
655 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
656 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200657 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200658 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200659 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200660 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200661#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100663 callstack_pop(event->proc);
664 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200665 if (event->proc->state != STATE_IGNORED) {
666 if (opt_T || options.summary) {
667 calc_time_spent(event->proc);
668 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100669 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100670 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c682042009-05-21 15:59:56 +0200671 if (event->proc->state != STATE_IGNORED) {
672 output_right(LT_TOF_FUNCTIONR, event->proc,
673 event->proc->callstack[i].c_un.libfunc->name);
674 }
Petr Machata211f0882010-11-03 18:42:18 +0100675 callstack_pop(event->proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200676 sbp = address2bpstruct(leader, event->e_un.brk_addr);
677 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100678 return;
679 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100680 }
681
Petr Machata9a5420c2011-07-09 11:21:23 +0200682 if ((sbp = address2bpstruct(leader, event->e_un.brk_addr))) {
683 if (sbp->libsym == NULL) {
684 continue_after_breakpoint(event->proc, sbp);
685 return;
686 }
687
Joe Damatof0bd98b2010-11-08 15:47:42 -0800688 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200689 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200690 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800691 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200692
Zachary T Welch97baa652010-12-08 13:34:03 -0800693 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200694 event->proc->stack_pointer = get_stack_pointer(event->proc);
695 event->proc->return_addr =
696 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200697 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100698 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200699 }
Paul Gilliambe320772006-04-24 22:06:23 +0200700#ifdef PLT_REINITALISATION_BP
701 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100702 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
703 0))
Petr Machata9a5420c2011-07-09 11:21:23 +0200704 reinitialize_breakpoints(leader);
Paul Gilliambe320772006-04-24 22:06:23 +0200705#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100706
707 continue_after_breakpoint(event->proc, sbp);
708 return;
709 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100710
Joe Damatofa2aefc2010-10-30 19:56:50 -0700711 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200712 output_line(event->proc, "unexpected breakpoint at %p",
713 (void *)event->e_un.brk_addr);
714 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100715 continue_process(event->proc->pid);
716}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200717
Juan Cespedesf1350522008-12-16 18:19:58 +0100718static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200719callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100720 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200721
Juan Cespedescd8976d2009-05-14 13:47:58 +0200722 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200723 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100724 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500725 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
726 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200727 return;
728 }
729
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100730 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200731 elem->is_syscall = 1;
732 elem->c_un.syscall = sysnum;
733 elem->return_addr = NULL;
734
735 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200736 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100737 struct timezone tz;
738 gettimeofday(&elem->time_spent, &tz);
739 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200740}
741
Juan Cespedes21c63a12001-07-07 20:56:56 +0200742static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200743callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Arnaud Patard26570082010-01-08 08:40:12 -0500744 struct callstack_element *elem, *prev;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200745
Juan Cespedescd8976d2009-05-14 13:47:58 +0200746 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200747 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100748 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500749 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
750 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200751 return;
752 }
753
Arnaud Patard26570082010-01-08 08:40:12 -0500754 prev = &proc->callstack[proc->callstack_depth-1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100755 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200756 elem->is_syscall = 0;
757 elem->c_un.libfunc = sym;
758
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200759 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200760 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200761 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200762 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200763
Arnaud Patard26570082010-01-08 08:40:12 -0500764 /* handle functions like atexit() on mips which have no return */
765 if (elem->return_addr != prev->return_addr)
766 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200767 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100768 struct timezone tz;
769 gettimeofday(&elem->time_spent, &tz);
770 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200771}
772
Juan Cespedesf1350522008-12-16 18:19:58 +0100773static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200774callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100775 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200776 assert(proc->callstack_depth > 0);
777
Juan Cespedescd8976d2009-05-14 13:47:58 +0200778 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100779 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200780 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200781 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200782 delete_breakpoint(proc, elem->return_addr);
783 }
Petr Machata211f0882010-11-03 18:42:18 +0100784 if (elem->arch_ptr != NULL) {
785 free(elem->arch_ptr);
786 elem->arch_ptr = NULL;
787 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200788 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200789}