blob: bff9ce5448abebb375bc345e10f45b89e83b97e3 [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 Machata366c2f42012-02-09 19:34:36 +010018#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010019#include "library.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010020
Juan Cespedes03192f82009-07-03 10:16:22 +020021static void handle_signal(Event *event);
22static void handle_exit(Event *event);
23static void handle_exit_signal(Event *event);
24static void handle_syscall(Event *event);
25static void handle_arch_syscall(Event *event);
26static void handle_sysret(Event *event);
27static void handle_arch_sysret(Event *event);
28static void handle_clone(Event *event);
29static void handle_exec(Event *event);
30static void handle_breakpoint(Event *event);
31static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010032
Juan Cespedesa8909f72009-04-28 20:02:41 +020033static void callstack_push_syscall(Process *proc, int sysnum);
34static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020036static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020037
Juan Cespedes61da3372009-07-03 11:55:44 +020038static char * shortsignal(Process *proc, int signum);
39static char * sysname(Process *proc, int sysnum);
40static char * arch_sysname(Process *proc, int sysnum);
41
Petr Machatacbe29c62011-09-27 02:27:58 +020042static Event *
43call_handler(Process * proc, Event * event)
44{
45 assert(proc != NULL);
46
Petr Machata366c2f42012-02-09 19:34:36 +010047 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020048 if (handler == NULL)
49 return event;
50
51 return (*handler->on_event) (handler, event);
52}
53
Juan Cespedes61da3372009-07-03 11:55:44 +020054void
Petr Machataffe4cd22012-04-11 18:01:44 +020055handle_event(Event *event)
56{
Petr Machata602330f2011-07-09 11:15:34 +020057 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020058 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020059 os_ltrace_exiting();
60 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020061 }
Petr Machata26627682011-07-08 18:15:32 +020062 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
63 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020064
65 /* If the thread group or an individual task define an
66 overriding event handler, give them a chance to kick in.
67 We will end up calling both handlers, if the first one
68 doesn't sink the event. */
69 if (event->proc != NULL) {
70 event = call_handler(event->proc, event);
71 if (event == NULL)
72 /* It was handled. */
73 return;
74
75 /* Note: the previous handler has a chance to alter
76 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +010077 if (event->proc != NULL
78 && event->proc->leader != NULL
79 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +020080 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020081 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020082 return;
83 }
84 }
85
Juan Cespedes61da3372009-07-03 11:55:44 +020086 switch (event->type) {
87 case EVENT_NONE:
88 debug(1, "event: none");
89 return;
90 case EVENT_SIGNAL:
91 debug(1, "event: signal (%s [%d])",
92 shortsignal(event->proc, event->e_un.signum),
93 event->e_un.signum);
94 handle_signal(event);
95 return;
96 case EVENT_EXIT:
97 debug(1, "event: exit (%d)", event->e_un.ret_val);
98 handle_exit(event);
99 return;
100 case EVENT_EXIT_SIGNAL:
101 debug(1, "event: exit signal (%s [%d])",
102 shortsignal(event->proc, event->e_un.signum),
103 event->e_un.signum);
104 handle_exit_signal(event);
105 return;
106 case EVENT_SYSCALL:
107 debug(1, "event: syscall (%s [%d])",
108 sysname(event->proc, event->e_un.sysnum),
109 event->e_un.sysnum);
110 handle_syscall(event);
111 return;
112 case EVENT_SYSRET:
113 debug(1, "event: sysret (%s [%d])",
114 sysname(event->proc, event->e_un.sysnum),
115 event->e_un.sysnum);
116 handle_sysret(event);
117 return;
118 case EVENT_ARCH_SYSCALL:
119 debug(1, "event: arch_syscall (%s [%d])",
120 arch_sysname(event->proc, event->e_un.sysnum),
121 event->e_un.sysnum);
122 handle_arch_syscall(event);
123 return;
124 case EVENT_ARCH_SYSRET:
125 debug(1, "event: arch_sysret (%s [%d])",
126 arch_sysname(event->proc, event->e_un.sysnum),
127 event->e_un.sysnum);
128 handle_arch_sysret(event);
129 return;
130 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200131 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200132 debug(1, "event: clone (%u)", event->e_un.newpid);
133 handle_clone(event);
134 return;
135 case EVENT_EXEC:
136 debug(1, "event: exec()");
137 handle_exec(event);
138 return;
139 case EVENT_BREAKPOINT:
140 debug(1, "event: breakpoint");
141 handle_breakpoint(event);
142 return;
143 case EVENT_NEW:
144 debug(1, "event: new process");
145 handle_new(event);
146 return;
147 default:
148 fprintf(stderr, "Error! unknown event?\n");
149 exit(1);
150 }
151}
152
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200153typedef struct Pending_New Pending_New;
154struct Pending_New {
155 pid_t pid;
156 Pending_New * next;
157};
158static Pending_New * pending_news = NULL;
159
160static int
161pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200162 Pending_New * p;
163
164 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
165
166 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200167 while (p) {
168 if (p->pid == pid) {
169 return 1;
170 }
171 p = p->next;
172 }
173 return 0;
174}
175
176static void
177pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200178 Pending_New * p;
179
180 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
181
182 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200183 if (!p) {
184 perror("malloc()");
185 exit(1);
186 }
187 p->pid = pid;
188 p->next = pending_news;
189 pending_news = p;
190}
191
192static void
193pending_new_remove(pid_t pid) {
194 Pending_New *p, *pred;
195
Juan Cespedescd8976d2009-05-14 13:47:58 +0200196 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
197
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200198 p = pending_news;
199 if (p->pid == pid) {
200 pending_news = p->next;
201 free(p);
202 } else {
203 while (p) {
204 if (p->pid == pid) {
205 pred->next = p->next;
206 free(p);
207 }
208 pred = p;
209 p = p->next;
210 }
211 }
212}
213
Petr Machata2b46cfc2012-02-18 11:17:29 +0100214#if 0
Petr Machata534e00f2011-09-27 17:58:38 +0200215static int
216clone_breakpoints(Process * proc, Process * orig_proc)
217{
218 /* When copying breakpoints, we also have to copy the
219 * referenced symbols, and link them properly. */
220 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
221 struct library_symbol * it = proc->list_of_symbols;
222 proc->list_of_symbols = NULL;
223 for (; it != NULL; it = it->next) {
224 struct library_symbol * libsym = clone_library_symbol(it);
225 if (libsym == NULL) {
226 int save_errno;
227 err:
228 save_errno = errno;
229 destroy_library_symbol_chain(proc->list_of_symbols);
230 dict_clear(map);
231 errno = save_errno;
232 return -1;
233 }
234 libsym->next = proc->list_of_symbols;
235 proc->list_of_symbols = libsym;
236 if (dict_enter(map, it, libsym) != 0)
237 goto err;
238 }
239
240 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
241 address_clone, breakpoint_clone, map);
242 if (proc->breakpoints == NULL)
243 goto err;
244
245 dict_clear(map);
246 return 0;
247}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248#endif
Petr Machata534e00f2011-09-27 17:58:38 +0200249
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200250static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251handle_clone(Event *event)
252{
Juan Cespedes03192f82009-07-03 10:16:22 +0200253 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200254
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 struct Process *proc = malloc(sizeof(*proc));
256 if (proc == NULL) {
257 fail:
258 free(proc);
259 /* XXX proper error handling here, please. */
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200260 perror("malloc()");
261 exit(1);
262 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263
264 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
265 goto fail;
266 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200267
Petr Machata75dcf7d2011-10-06 14:30:19 +0200268 /* We save register values to the arch pointer, and these need
269 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200271
Petr Machata2b46cfc2012-02-18 11:17:29 +0100272 if (pending_new(proc->pid)) {
273 pending_new_remove(proc->pid);
274 /* XXX this used to be destroy_event_handler call, but
275 * I don't think we want to call that on a shared
276 * state. */
277 proc->event_handler = NULL;
278 if (event->proc->state == STATE_ATTACHED && options.follow)
279 proc->state = STATE_ATTACHED;
280 else
281 proc->state = STATE_IGNORED;
282 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200283 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200285 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 add_process(proc);
Petr Machata534e00f2011-09-27 17:58:38 +0200287
Petr Machatacbe29c62011-09-27 02:27:58 +0200288 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200290 else
291 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200292}
293
294static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200295handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200296 Process * proc;
297
Juan Cespedes03192f82009-07-03 10:16:22 +0200298 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200299
300 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200301 if (!proc) {
302 pending_new_insert(event->e_un.newpid);
303 } else {
304 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200305 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200306 proc->state = STATE_ATTACHED;
307 } else {
308 proc->state = STATE_IGNORED;
309 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200310 continue_process(proc->pid);
311 }
312}
313
Juan Cespedesf1350522008-12-16 18:19:58 +0100314static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200315shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 static char *signalent0[] = {
317#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100318 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 static char *signalent1[] = {
320#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100321 };
322 static char **signalents[] = { signalent0, signalent1 };
323 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 sizeof signalent1 / sizeof signalent1[0]
325 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100326
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
328
Ian Wienand9a2ad352006-02-20 22:44:45 +0100329 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100331 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332 return "UNKNOWN_SIGNAL";
333 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100334 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100335 }
336}
337
Juan Cespedesf1350522008-12-16 18:19:58 +0100338static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200339sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100340 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 static char *syscalent0[] = {
342#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100343 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 static char *syscalent1[] = {
345#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100346 };
347 static char **syscalents[] = { syscalent0, syscalent1 };
348 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 sizeof syscalent1 / sizeof syscalent1[0]
350 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100351
Juan Cespedescd8976d2009-05-14 13:47:58 +0200352 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
353
Ian Wienand9a2ad352006-02-20 22:44:45 +0100354 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357 sprintf(result, "SYS_%d", sysnum);
358 return result;
359 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 sprintf(result, "SYS_%s",
361 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 return result;
363 }
364}
365
Juan Cespedesf1350522008-12-16 18:19:58 +0100366static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200367arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100368 static char result[128];
369 static char *arch_syscalent[] = {
370#include "arch_syscallent.h"
371 };
372 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
373
Juan Cespedescd8976d2009-05-14 13:47:58 +0200374 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
375
Juan Cespedes63184be2008-12-10 13:30:12 +0100376 if (sysnum < 0 || sysnum >= nsyscals) {
377 sprintf(result, "ARCH_%d", sysnum);
378 return result;
379 } else {
380 sprintf(result, "ARCH_%s",
381 arch_syscalent[sysnum]);
382 return result;
383 }
384}
385
Juan Cespedesf1350522008-12-16 18:19:58 +0100386static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200387handle_signal(Event *event) {
388 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800389 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200390 output_line(event->proc, "--- %s (%s) ---",
391 shortsignal(event->proc, event->e_un.signum),
392 strsignal(event->e_un.signum));
393 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100394 continue_after_signal(event->proc->pid, event->e_un.signum);
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200398handle_exit(Event *event) {
399 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200400 if (event->proc->state != STATE_IGNORED) {
401 output_line(event->proc, "+++ exited (status %d) +++",
402 event->e_un.ret_val);
403 }
Petr Machatacebb8842011-07-09 11:14:11 +0200404 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100405 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100406}
407
Juan Cespedesf1350522008-12-16 18:19:58 +0100408static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200409handle_exit_signal(Event *event) {
410 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200411 if (event->proc->state != STATE_IGNORED) {
412 output_line(event->proc, "+++ killed by %s +++",
413 shortsignal(event->proc, event->e_un.signum));
414 }
Petr Machatacebb8842011-07-09 11:14:11 +0200415 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100416 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100417}
418
Petr Machata29add4f2012-02-18 16:38:05 +0100419static struct library_symbol *
420temporary_syscall_symbol(const char *name)
421{
422 struct library *syscalls = malloc(sizeof(*syscalls));
423 struct library_symbol *syscall = malloc(sizeof(*syscall));
424 if (syscalls == NULL || syscall == NULL) {
425 free(syscalls);
426 free(syscall);
427 return NULL;
428 }
429 library_init(syscalls, "SYS", 0);
430 library_symbol_init(syscall, syscalls, 0, name, 0, LS_TOPLT_NONE, 0);
431 library_add_symbol(syscalls, syscall);
432 return syscall;
433}
434
435static void
436output_syscall_left(struct Process *proc, const char *name)
437{
438 struct library_symbol *syscall = temporary_syscall_symbol(name);
439 output_left(LT_TOF_SYSCALL, proc, syscall);
440 struct library *lib = syscall->lib;
441 library_destroy(lib);
442 free(lib);
443}
444
445static void
446output_syscall_right(struct Process *proc, const char *name)
447{
448 struct library_symbol *syscall = temporary_syscall_symbol(name);
449 output_right(LT_TOF_SYSCALLR, proc, syscall);
450 struct library *lib = syscall->lib;
451 library_destroy(lib);
452 free(lib);
453}
454
Juan Cespedesf1350522008-12-16 18:19:58 +0100455static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200456handle_syscall(Event *event) {
457 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200458 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100459 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100460 if (options.syscalls)
461 output_syscall_left(event->proc,
462 sysname(event->proc,
463 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100464 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100465 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100466}
467
Juan Cespedesf1350522008-12-16 18:19:58 +0100468static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200469handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200470 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200471
Juan Cespedes03192f82009-07-03 10:16:22 +0200472 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200473 if (proc->state == STATE_IGNORED) {
474 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200475 remove_process(proc);
Petr Machata464026f2012-03-02 00:10:58 +0100476 free(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200477 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200478 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200479 output_line(proc, "--- Called exec() ---");
480 proc->mask_32bit = 0;
481 proc->personality = 0;
482 proc->arch_ptr = NULL;
483 free(proc->filename);
484 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200485 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200486 proc->callstack_depth = 0;
487 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200488}
489
490static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200491handle_arch_syscall(Event *event) {
492 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200493 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100494 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200495 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100496 output_syscall_left(event->proc,
497 arch_sysname(event->proc,
498 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200499 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100500 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100501 continue_process(event->proc->pid);
502}
503
Juan Cespedesd65efa32003-02-03 00:22:30 +0100504struct timeval current_time_spent;
505
Juan Cespedesf1350522008-12-16 18:19:58 +0100506static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200507calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100508 struct timeval tv;
509 struct timezone tz;
510 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100512
Juan Cespedescd8976d2009-05-14 13:47:58 +0200513 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100515
516 gettimeofday(&tv, &tz);
517
518 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
519 if (tv.tv_usec >= elem->time_spent.tv_usec) {
520 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
521 } else {
522 diff.tv_sec++;
523 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
524 }
525 current_time_spent = diff;
526}
527
Juan Cespedesf1350522008-12-16 18:19:58 +0100528static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200529handle_sysret(Event *event) {
530 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200531 if (event->proc->state != STATE_IGNORED) {
532 if (opt_T || options.summary) {
533 calc_time_spent(event->proc);
534 }
Petr Machata29add4f2012-02-18 16:38:05 +0100535 if (options.syscalls)
536 output_syscall_right(event->proc,
537 sysname(event->proc,
538 event->e_un.sysnum));
539
Petr Machata43d2fe52011-11-02 13:25:49 +0100540 assert(event->proc->callstack_depth > 0);
541 unsigned d = event->proc->callstack_depth - 1;
542 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100543 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200544 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100545 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100546}
547
Juan Cespedesf1350522008-12-16 18:19:58 +0100548static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200549handle_arch_sysret(Event *event) {
550 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200551 if (event->proc->state != STATE_IGNORED) {
552 if (opt_T || options.summary) {
553 calc_time_spent(event->proc);
554 }
Petr Machata29add4f2012-02-18 16:38:05 +0100555 if (options.syscalls)
556 output_syscall_right(event->proc,
557 arch_sysname(event->proc,
558 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100559 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100560 }
561 continue_process(event->proc->pid);
562}
563
Juan Cespedesf1350522008-12-16 18:19:58 +0100564static void
Petr Machata14298742012-04-12 23:09:21 +0200565output_right_tos(struct Process *proc)
566{
567 size_t d = proc->callstack_depth;
568 struct callstack_element *elem = &proc->callstack[d - 1];
569 if (proc->state != STATE_IGNORED)
570 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
571}
572
573static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100574handle_breakpoint(Event *event)
575{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100577 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200578 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200579 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200580
581 /* The leader has terminated. */
582 if (leader == NULL) {
583 continue_process(event->proc->pid);
584 return;
585 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100586
Petr Machata31b2f9f2012-04-12 22:23:40 +0200587 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
588 event->proc->pid, brk_addr);
589 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100590
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100591 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200592 if (brk_addr == event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200593#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100594 /*
595 * PPC HACK! (XXX FIXME TODO)
596 * The PLT gets modified during the first call,
597 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100598 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100599 unsigned long a;
600 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100601 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200602 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200603
Paul Gilliam76c61f12006-06-14 06:55:21 +0200604 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100605 unsigned char break_insn[] = BREAKPOINT_VALUE;
606
Petr Machata9a5420c2011-07-09 11:21:23 +0200607 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100608 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100609 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
610 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100611
Paul Gilliam76c61f12006-06-14 06:55:21 +0200612 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100613 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100614 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200615 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100616 }
617 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200618 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200619 /* On powerpc, the breakpoint address
620 may end up being actual entry point
621 of the library symbol, not the PLT
622 address we computed. In that case,
623 sbp is NULL. */
624 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100625 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200626 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200627 }
Ian Wienand3219f322006-02-16 06:00:00 +0100628 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100629#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500630 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200631 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500632 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200633 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100634 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200635 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500636 if (sbp) {
637 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200638 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500639 }
640 } else {
641 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
642 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200643 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200644 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200645 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200646 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200647#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100648 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100649 callstack_pop(event->proc);
650 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200651 if (event->proc->state != STATE_IGNORED) {
652 if (opt_T || options.summary) {
653 calc_time_spent(event->proc);
654 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100655 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200656 event->proc->return_addr = brk_addr;
657
Petr Machata14298742012-04-12 23:09:21 +0200658 output_right_tos(event->proc);
659 callstack_pop(event->proc);
660
Petr Machata31b2f9f2012-04-12 22:23:40 +0200661 /* Pop also any other entries that seem like
662 * they are linked to the current one: they
663 * have the same return address, but were made
664 * for different symbols. This should only
665 * happen for entry point tracing, i.e. for -x
666 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200667 while (event->proc->callstack_depth > 0) {
668 struct callstack_element *prev;
669 size_t d = event->proc->callstack_depth;
670 prev = &event->proc->callstack[d - 1];
671
Petr Machata14298742012-04-12 23:09:21 +0200672 if (prev->c_un.libfunc == libsym
673 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200674 break;
Petr Machata14298742012-04-12 23:09:21 +0200675
676 output_right_tos(event->proc);
677 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200678 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200679
680 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200681 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100682 return;
683 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100684 }
685
Petr Machata31b2f9f2012-04-12 22:23:40 +0200686 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100687 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100688
Petr Machata2b46cfc2012-02-18 11:17:29 +0100689 if (event->proc->state != STATE_IGNORED
690 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200691 event->proc->stack_pointer = get_stack_pointer(event->proc);
692 event->proc->return_addr =
693 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200694 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100695 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200696 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100697
698 continue_after_breakpoint(event->proc, sbp);
699 return;
700 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100701
Joe Damatofa2aefc2010-10-30 19:56:50 -0700702 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200703 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200704 brk_addr);
Juan Cespedes5c682042009-05-21 15:59:56 +0200705 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100706 continue_process(event->proc->pid);
707}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200708
Juan Cespedesf1350522008-12-16 18:19:58 +0100709static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200710callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100711 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200712
Juan Cespedescd8976d2009-05-14 13:47:58 +0200713 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100715 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500716 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
717 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200718 return;
719 }
720
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100721 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200722 elem->is_syscall = 1;
723 elem->c_un.syscall = sysnum;
724 elem->return_addr = NULL;
725
726 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200727 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100728 struct timezone tz;
729 gettimeofday(&elem->time_spent, &tz);
730 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200731}
732
Juan Cespedes21c63a12001-07-07 20:56:56 +0200733static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200734callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200735 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200736
Juan Cespedescd8976d2009-05-14 13:47:58 +0200737 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200738 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100739 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500740 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
741 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200742 return;
743 }
744
Petr Machata14298742012-04-12 23:09:21 +0200745 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200746 elem->is_syscall = 0;
747 elem->c_un.libfunc = sym;
748
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200749 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200750 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200751 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200752 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200753
Arnaud Patard26570082010-01-08 08:40:12 -0500754 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200755 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100756 struct timezone tz;
757 gettimeofday(&elem->time_spent, &tz);
758 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200759}
760
Juan Cespedesf1350522008-12-16 18:19:58 +0100761static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200762callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100763 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200764 assert(proc->callstack_depth > 0);
765
Juan Cespedescd8976d2009-05-14 13:47:58 +0200766 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100767 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200768 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200769 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200770 delete_breakpoint(proc, elem->return_addr);
771 }
Petr Machata211f0882010-11-03 18:42:18 +0100772 if (elem->arch_ptr != NULL) {
773 free(elem->arch_ptr);
774 elem->arch_ptr = NULL;
775 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200776 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200777}