blob: f899d213deab230950cae59a639f347f92366f58 [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"
Petr Machata9294d822012-02-07 12:35:58 +010017#include "breakpoint.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010018
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 Cespedes5e01f651998-03-08 22:31:44 +010030
Juan Cespedesa8909f72009-04-28 20:02:41 +020031static void callstack_push_syscall(Process *proc, int sysnum);
32static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020034static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035
Juan Cespedes61da3372009-07-03 11:55:44 +020036static char * shortsignal(Process *proc, int signum);
37static char * sysname(Process *proc, int sysnum);
38static char * arch_sysname(Process *proc, int sysnum);
39
Petr Machatacbe29c62011-09-27 02:27:58 +020040static Event *
41call_handler(Process * proc, Event * event)
42{
43 assert(proc != NULL);
44
45 Event_Handler * handler = proc->event_handler;
46 if (handler == NULL)
47 return event;
48
49 return (*handler->on_event) (handler, event);
50}
51
Juan Cespedes61da3372009-07-03 11:55:44 +020052void
Petr Machataffe4cd22012-04-11 18:01:44 +020053handle_event(Event *event)
54{
Petr Machata602330f2011-07-09 11:15:34 +020055 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020056 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020057 os_ltrace_exiting();
58 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020059 }
Petr Machata26627682011-07-08 18:15:32 +020060 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
61 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020062
63 /* If the thread group or an individual task define an
64 overriding event handler, give them a chance to kick in.
65 We will end up calling both handlers, if the first one
66 doesn't sink the event. */
67 if (event->proc != NULL) {
68 event = call_handler(event->proc, event);
69 if (event == NULL)
70 /* It was handled. */
71 return;
72
73 /* Note: the previous handler has a chance to alter
74 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +010075 if (event->proc != NULL
76 && event->proc->leader != NULL
77 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +020078 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020079 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020080 return;
81 }
82 }
83
Juan Cespedes61da3372009-07-03 11:55:44 +020084 switch (event->type) {
85 case EVENT_NONE:
86 debug(1, "event: none");
87 return;
88 case EVENT_SIGNAL:
89 debug(1, "event: signal (%s [%d])",
90 shortsignal(event->proc, event->e_un.signum),
91 event->e_un.signum);
92 handle_signal(event);
93 return;
94 case EVENT_EXIT:
95 debug(1, "event: exit (%d)", event->e_un.ret_val);
96 handle_exit(event);
97 return;
98 case EVENT_EXIT_SIGNAL:
99 debug(1, "event: exit signal (%s [%d])",
100 shortsignal(event->proc, event->e_un.signum),
101 event->e_un.signum);
102 handle_exit_signal(event);
103 return;
104 case EVENT_SYSCALL:
105 debug(1, "event: syscall (%s [%d])",
106 sysname(event->proc, event->e_un.sysnum),
107 event->e_un.sysnum);
108 handle_syscall(event);
109 return;
110 case EVENT_SYSRET:
111 debug(1, "event: sysret (%s [%d])",
112 sysname(event->proc, event->e_un.sysnum),
113 event->e_un.sysnum);
114 handle_sysret(event);
115 return;
116 case EVENT_ARCH_SYSCALL:
117 debug(1, "event: arch_syscall (%s [%d])",
118 arch_sysname(event->proc, event->e_un.sysnum),
119 event->e_un.sysnum);
120 handle_arch_syscall(event);
121 return;
122 case EVENT_ARCH_SYSRET:
123 debug(1, "event: arch_sysret (%s [%d])",
124 arch_sysname(event->proc, event->e_un.sysnum),
125 event->e_un.sysnum);
126 handle_arch_sysret(event);
127 return;
128 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200129 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200130 debug(1, "event: clone (%u)", event->e_un.newpid);
131 handle_clone(event);
132 return;
133 case EVENT_EXEC:
134 debug(1, "event: exec()");
135 handle_exec(event);
136 return;
137 case EVENT_BREAKPOINT:
138 debug(1, "event: breakpoint");
139 handle_breakpoint(event);
140 return;
141 case EVENT_NEW:
142 debug(1, "event: new process");
143 handle_new(event);
144 return;
145 default:
146 fprintf(stderr, "Error! unknown event?\n");
147 exit(1);
148 }
149}
150
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200151/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200152static void *
Petr Machata534e00f2011-09-27 17:58:38 +0200153address_clone(void * addr, void * data)
154{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200155 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200156 return addr;
157}
158
Juan Cespedes61da3372009-07-03 11:55:44 +0200159static void *
Petr Machatafed1e8d2012-02-07 02:06:29 +0100160breakpoint_clone(void *bp, void *data)
Petr Machata534e00f2011-09-27 17:58:38 +0200161{
Petr Machatafed1e8d2012-02-07 02:06:29 +0100162 Dict *map = data;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200163 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Petr Machata9294d822012-02-07 12:35:58 +0100164 struct breakpoint *b = malloc(sizeof(*b));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200165 if (!b) {
166 perror("malloc()");
167 exit(1);
168 }
Petr Machatafed1e8d2012-02-07 02:06:29 +0100169 memcpy(b, bp, sizeof(*b));
Petr Machata534e00f2011-09-27 17:58:38 +0200170 if (b->libsym != NULL) {
Petr Machatafed1e8d2012-02-07 02:06:29 +0100171 struct library_symbol *sym = dict_find_entry(map, b->libsym);
Petr Machata534e00f2011-09-27 17:58:38 +0200172 if (b->libsym == NULL) {
173 fprintf(stderr, "Can't find cloned symbol %s.\n",
174 b->libsym->name);
175 return NULL;
176 }
177 b->libsym = sym;
178 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200179 return b;
180}
181
182typedef struct Pending_New Pending_New;
183struct Pending_New {
184 pid_t pid;
185 Pending_New * next;
186};
187static Pending_New * pending_news = NULL;
188
189static int
190pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200191 Pending_New * p;
192
193 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
194
195 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200196 while (p) {
197 if (p->pid == pid) {
198 return 1;
199 }
200 p = p->next;
201 }
202 return 0;
203}
204
205static void
206pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200207 Pending_New * p;
208
209 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
210
211 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200212 if (!p) {
213 perror("malloc()");
214 exit(1);
215 }
216 p->pid = pid;
217 p->next = pending_news;
218 pending_news = p;
219}
220
221static void
222pending_new_remove(pid_t pid) {
223 Pending_New *p, *pred;
224
Juan Cespedescd8976d2009-05-14 13:47:58 +0200225 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
226
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200227 p = pending_news;
228 if (p->pid == pid) {
229 pending_news = p->next;
230 free(p);
231 } else {
232 while (p) {
233 if (p->pid == pid) {
234 pred->next = p->next;
235 free(p);
236 }
237 pred = p;
238 p = p->next;
239 }
240 }
241}
242
Petr Machata534e00f2011-09-27 17:58:38 +0200243static int
244clone_breakpoints(Process * proc, Process * orig_proc)
245{
246 /* When copying breakpoints, we also have to copy the
247 * referenced symbols, and link them properly. */
248 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
249 struct library_symbol * it = proc->list_of_symbols;
250 proc->list_of_symbols = NULL;
251 for (; it != NULL; it = it->next) {
252 struct library_symbol * libsym = clone_library_symbol(it);
253 if (libsym == NULL) {
254 int save_errno;
255 err:
256 save_errno = errno;
257 destroy_library_symbol_chain(proc->list_of_symbols);
258 dict_clear(map);
259 errno = save_errno;
260 return -1;
261 }
262 libsym->next = proc->list_of_symbols;
263 proc->list_of_symbols = libsym;
264 if (dict_enter(map, it, libsym) != 0)
265 goto err;
266 }
267
268 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
269 address_clone, breakpoint_clone, map);
270 if (proc->breakpoints == NULL)
271 goto err;
272
273 dict_clear(map);
274 return 0;
275}
276
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200277static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200278handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200279 Process *p;
280
Juan Cespedes03192f82009-07-03 10:16:22 +0200281 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200282
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200283 p = malloc(sizeof(Process));
284 if (!p) {
285 perror("malloc()");
286 exit(1);
287 }
288 memcpy(p, event->proc, sizeof(Process));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200289 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200290 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200291
Petr Machata75dcf7d2011-10-06 14:30:19 +0200292 /* We save register values to the arch pointer, and these need
293 to be per-thread. */
294 p->arch_ptr = NULL;
295
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200296 if (pending_new(p->pid)) {
297 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200298 if (p->event_handler != NULL)
299 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200300 if (event->proc->state == STATE_ATTACHED && options.follow) {
301 p->state = STATE_ATTACHED;
302 } else {
303 p->state = STATE_IGNORED;
304 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200305 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200306 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200307 } else {
308 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200309 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200310 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200311
Petr Machata534e00f2011-09-27 17:58:38 +0200312 if (p->leader == p)
Petr Machataba9911f2011-09-27 21:09:47 +0200313 clone_breakpoints(p, event->proc->leader);
Petr Machata534e00f2011-09-27 17:58:38 +0200314 else
315 /* Thread groups share breakpoints. */
316 p->breakpoints = NULL;
317
Petr Machatacbe29c62011-09-27 02:27:58 +0200318 if (event->type == EVENT_VFORK)
319 continue_after_vfork(p);
320 else
321 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200322}
323
324static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200325handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200326 Process * proc;
327
Juan Cespedes03192f82009-07-03 10:16:22 +0200328 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200329
330 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200331 if (!proc) {
332 pending_new_insert(event->e_un.newpid);
333 } else {
334 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200335 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200336 proc->state = STATE_ATTACHED;
337 } else {
338 proc->state = STATE_IGNORED;
339 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200340 continue_process(proc->pid);
341 }
342}
343
Juan Cespedesf1350522008-12-16 18:19:58 +0100344static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200345shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 static char *signalent0[] = {
347#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 static char *signalent1[] = {
350#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100351 };
352 static char **signalents[] = { signalent0, signalent1 };
353 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 sizeof signalent1 / sizeof signalent1[0]
355 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100356
Juan Cespedescd8976d2009-05-14 13:47:58 +0200357 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
358
Ian Wienand9a2ad352006-02-20 22:44:45 +0100359 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100361 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 return "UNKNOWN_SIGNAL";
363 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100364 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100365 }
366}
367
Juan Cespedesf1350522008-12-16 18:19:58 +0100368static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200369sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100370 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 static char *syscalent0[] = {
372#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100373 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 static char *syscalent1[] = {
375#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100376 };
377 static char **syscalents[] = { syscalent0, syscalent1 };
378 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 sizeof syscalent1 / sizeof syscalent1[0]
380 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100381
Juan Cespedescd8976d2009-05-14 13:47:58 +0200382 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
383
Ian Wienand9a2ad352006-02-20 22:44:45 +0100384 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100385 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100386 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100387 sprintf(result, "SYS_%d", sysnum);
388 return result;
389 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 sprintf(result, "SYS_%s",
391 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100392 return result;
393 }
394}
395
Juan Cespedesf1350522008-12-16 18:19:58 +0100396static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200397arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100398 static char result[128];
399 static char *arch_syscalent[] = {
400#include "arch_syscallent.h"
401 };
402 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
403
Juan Cespedescd8976d2009-05-14 13:47:58 +0200404 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
405
Juan Cespedes63184be2008-12-10 13:30:12 +0100406 if (sysnum < 0 || sysnum >= nsyscals) {
407 sprintf(result, "ARCH_%d", sysnum);
408 return result;
409 } else {
410 sprintf(result, "ARCH_%s",
411 arch_syscalent[sysnum]);
412 return result;
413 }
414}
415
Juan Cespedesf1350522008-12-16 18:19:58 +0100416static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200417handle_signal(Event *event) {
418 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800419 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200420 output_line(event->proc, "--- %s (%s) ---",
421 shortsignal(event->proc, event->e_un.signum),
422 strsignal(event->e_un.signum));
423 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100424 continue_after_signal(event->proc->pid, event->e_un.signum);
425}
426
Juan Cespedesf1350522008-12-16 18:19:58 +0100427static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200428handle_exit(Event *event) {
429 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200430 if (event->proc->state != STATE_IGNORED) {
431 output_line(event->proc, "+++ exited (status %d) +++",
432 event->e_un.ret_val);
433 }
Petr Machatacebb8842011-07-09 11:14:11 +0200434 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100435}
436
Juan Cespedesf1350522008-12-16 18:19:58 +0100437static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200438handle_exit_signal(Event *event) {
439 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200440 if (event->proc->state != STATE_IGNORED) {
441 output_line(event->proc, "+++ killed by %s +++",
442 shortsignal(event->proc, event->e_un.signum));
443 }
Petr Machatacebb8842011-07-09 11:14:11 +0200444 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100445}
446
Juan Cespedesf1350522008-12-16 18:19:58 +0100447static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200448handle_syscall(Event *event) {
449 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200450 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100451 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200452 if (options.syscalls) {
453 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200454 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200455 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100456 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100457 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100458}
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;
Juan Cespedese0660df2009-05-21 18:14:39 +0200463
Juan Cespedes03192f82009-07-03 10:16:22 +0200464 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200465 if (proc->state == STATE_IGNORED) {
466 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200467 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200468 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200469 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200470 output_line(proc, "--- Called exec() ---");
471 proc->mask_32bit = 0;
472 proc->personality = 0;
473 proc->arch_ptr = NULL;
474 free(proc->filename);
475 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200476 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200477 proc->callstack_depth = 0;
478 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200479}
480
481static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200482handle_arch_syscall(Event *event) {
483 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200484 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100485 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200486 if (options.syscalls) {
487 output_left(LT_TOF_SYSCALL, event->proc,
488 arch_sysname(event->proc, event->e_un.sysnum));
489 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100490 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100491 continue_process(event->proc->pid);
492}
493
Juan Cespedesd65efa32003-02-03 00:22:30 +0100494struct timeval current_time_spent;
495
Juan Cespedesf1350522008-12-16 18:19:58 +0100496static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200497calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100498 struct timeval tv;
499 struct timezone tz;
500 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100502
Juan Cespedescd8976d2009-05-14 13:47:58 +0200503 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100504 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100505
506 gettimeofday(&tv, &tz);
507
508 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
509 if (tv.tv_usec >= elem->time_spent.tv_usec) {
510 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
511 } else {
512 diff.tv_sec++;
513 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
514 }
515 current_time_spent = diff;
516}
517
Juan Cespedesf1350522008-12-16 18:19:58 +0100518static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200519handle_sysret(Event *event) {
520 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200521 if (event->proc->state != STATE_IGNORED) {
522 if (opt_T || options.summary) {
523 calc_time_spent(event->proc);
524 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200525 if (options.syscalls) {
526 output_right(LT_TOF_SYSCALLR, event->proc,
527 sysname(event->proc, event->e_un.sysnum));
528 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100529 assert(event->proc->callstack_depth > 0);
530 unsigned d = event->proc->callstack_depth - 1;
531 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100532 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200533 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100534 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100535}
536
Juan Cespedesf1350522008-12-16 18:19:58 +0100537static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200538handle_arch_sysret(Event *event) {
539 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200540 if (event->proc->state != STATE_IGNORED) {
541 if (opt_T || options.summary) {
542 calc_time_spent(event->proc);
543 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200544 if (options.syscalls) {
545 output_right(LT_TOF_SYSCALLR, event->proc,
546 arch_sysname(event->proc, event->e_un.sysnum));
547 }
Petr Machata211f0882010-11-03 18:42:18 +0100548 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100549 }
550 continue_process(event->proc->pid);
551}
552
Petr Machata067322d2010-10-25 14:47:55 +0200553#ifdef __powerpc__
554void *get_count_register (Process *proc);
555#endif
556
Juan Cespedesf1350522008-12-16 18:19:58 +0100557static void
Petr Machata14298742012-04-12 23:09:21 +0200558output_right_tos(struct Process *proc)
559{
560 size_t d = proc->callstack_depth;
561 struct callstack_element *elem = &proc->callstack[d - 1];
562 if (proc->state != STATE_IGNORED)
563 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
564}
565
566static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100567handle_breakpoint(Event *event)
568{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100569 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100570 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200571 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200572 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200573
574 /* The leader has terminated. */
575 if (leader == NULL) {
576 continue_process(event->proc->pid);
577 return;
578 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100579
Petr Machata31b2f9f2012-04-12 22:23:40 +0200580 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
581 event->proc->pid, brk_addr);
582 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100583
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100584 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200585 if (brk_addr == event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200586#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100587 /*
588 * PPC HACK! (XXX FIXME TODO)
589 * The PLT gets modified during the first call,
590 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100591 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100592 unsigned long a;
593 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100594 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200595 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200596
Paul Gilliam76c61f12006-06-14 06:55:21 +0200597 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100598 unsigned char break_insn[] = BREAKPOINT_VALUE;
599
Petr Machata9a5420c2011-07-09 11:21:23 +0200600 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100601 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100602 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
603 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100604
Paul Gilliam76c61f12006-06-14 06:55:21 +0200605 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100606 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100607 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200608 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100609 }
610 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200611 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200612 /* On powerpc, the breakpoint address
613 may end up being actual entry point
614 of the library symbol, not the PLT
615 address we computed. In that case,
616 sbp is NULL. */
617 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100618 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200619 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200620 }
Ian Wienand3219f322006-02-16 06:00:00 +0100621 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100622#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500623 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200624 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500625 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200626 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100627 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200628 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500629 if (sbp) {
630 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200631 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500632 }
633 } else {
634 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
635 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200636 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200637 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200638 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200639 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200640#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100641 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100642 callstack_pop(event->proc);
643 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200644 if (event->proc->state != STATE_IGNORED) {
645 if (opt_T || options.summary) {
646 calc_time_spent(event->proc);
647 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100648 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200649 event->proc->return_addr = brk_addr;
650
Petr Machata14298742012-04-12 23:09:21 +0200651 output_right_tos(event->proc);
652 callstack_pop(event->proc);
653
Petr Machata31b2f9f2012-04-12 22:23:40 +0200654 /* Pop also any other entries that seem like
655 * they are linked to the current one: they
656 * have the same return address, but were made
657 * for different symbols. This should only
658 * happen for entry point tracing, i.e. for -x
659 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200660 while (event->proc->callstack_depth > 0) {
661 struct callstack_element *prev;
662 size_t d = event->proc->callstack_depth;
663 prev = &event->proc->callstack[d - 1];
664
Petr Machata14298742012-04-12 23:09:21 +0200665 if (prev->c_un.libfunc == libsym
666 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200667 break;
Petr Machata14298742012-04-12 23:09:21 +0200668
669 output_right_tos(event->proc);
670 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200671 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200672
673 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200674 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100675 return;
676 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100677 }
678
Petr Machata31b2f9f2012-04-12 22:23:40 +0200679 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100680 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100681
Petr Machata9a5420c2011-07-09 11:21:23 +0200682 if (sbp->libsym == NULL) {
683 continue_after_breakpoint(event->proc, sbp);
684 return;
685 }
686
Joe Damatof0bd98b2010-11-08 15:47:42 -0800687 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200688 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200689 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800690 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200691
Zachary T Welch97baa652010-12-08 13:34:03 -0800692 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200693 event->proc->stack_pointer = get_stack_pointer(event->proc);
694 event->proc->return_addr =
695 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200696 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100697 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200698 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100699
700 continue_after_breakpoint(event->proc, sbp);
701 return;
702 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100703
Joe Damatofa2aefc2010-10-30 19:56:50 -0700704 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200705 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200706 brk_addr);
Juan Cespedes5c682042009-05-21 15:59:56 +0200707 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100708 continue_process(event->proc->pid);
709}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200710
Juan Cespedesf1350522008-12-16 18:19:58 +0100711static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200712callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100713 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714
Juan Cespedescd8976d2009-05-14 13:47:58 +0200715 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200716 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100717 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500718 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
719 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200720 return;
721 }
722
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100723 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200724 elem->is_syscall = 1;
725 elem->c_un.syscall = sysnum;
726 elem->return_addr = NULL;
727
728 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200729 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100730 struct timezone tz;
731 gettimeofday(&elem->time_spent, &tz);
732 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200733}
734
Juan Cespedes21c63a12001-07-07 20:56:56 +0200735static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200736callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200737 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200738
Juan Cespedescd8976d2009-05-14 13:47:58 +0200739 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200740 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100741 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500742 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
743 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200744 return;
745 }
746
Petr Machata14298742012-04-12 23:09:21 +0200747 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200748 elem->is_syscall = 0;
749 elem->c_un.libfunc = sym;
750
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200751 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200752 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200753 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200754 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200755
Arnaud Patard26570082010-01-08 08:40:12 -0500756 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200757 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100758 struct timezone tz;
759 gettimeofday(&elem->time_spent, &tz);
760 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200761}
762
Juan Cespedesf1350522008-12-16 18:19:58 +0100763static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200764callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100765 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200766 assert(proc->callstack_depth > 0);
767
Juan Cespedescd8976d2009-05-14 13:47:58 +0200768 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100769 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200770 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200771 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200772 delete_breakpoint(proc, elem->return_addr);
773 }
Petr Machata211f0882010-11-03 18:42:18 +0100774 if (elem->arch_ptr != NULL) {
775 free(elem->arch_ptr);
776 elem->arch_ptr = NULL;
777 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200778 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200779}