blob: 5a49387f484c4032dbc3ee1edc72b57b55ef8c44 [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
Petr Machatafed1e8d2012-02-07 02:06:29 +010012#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010013#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010014#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010015#include "library.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010016
Juan Cespedes03192f82009-07-03 10:16:22 +020017static void handle_signal(Event *event);
18static void handle_exit(Event *event);
19static void handle_exit_signal(Event *event);
20static void handle_syscall(Event *event);
21static void handle_arch_syscall(Event *event);
22static void handle_sysret(Event *event);
23static void handle_arch_sysret(Event *event);
24static void handle_clone(Event *event);
25static void handle_exec(Event *event);
26static void handle_breakpoint(Event *event);
27static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010028
Juan Cespedesa8909f72009-04-28 20:02:41 +020029static void callstack_push_syscall(Process *proc, int sysnum);
30static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020032static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020033
Juan Cespedes61da3372009-07-03 11:55:44 +020034static char * shortsignal(Process *proc, int signum);
35static char * sysname(Process *proc, int sysnum);
36static char * arch_sysname(Process *proc, int sysnum);
37
Petr Machatacbe29c62011-09-27 02:27:58 +020038static Event *
39call_handler(Process * proc, Event * event)
40{
41 assert(proc != NULL);
42
Petr Machata366c2f42012-02-09 19:34:36 +010043 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020044 if (handler == NULL)
45 return event;
46
47 return (*handler->on_event) (handler, event);
48}
49
Juan Cespedes61da3372009-07-03 11:55:44 +020050void
Petr Machataffe4cd22012-04-11 18:01:44 +020051handle_event(Event *event)
52{
Petr Machata602330f2011-07-09 11:15:34 +020053 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020054 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020055 os_ltrace_exiting();
56 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020057 }
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:
Petr Machataebfe7d62012-04-13 21:34:49 +020087 debug(1, "[%d] event: signal (%s [%d])",
88 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +020089 shortsignal(event->proc, event->e_un.signum),
90 event->e_un.signum);
91 handle_signal(event);
92 return;
93 case EVENT_EXIT:
Petr Machataebfe7d62012-04-13 21:34:49 +020094 debug(1, "[%d] event: exit (%d)",
95 event->proc->pid,
96 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +020097 handle_exit(event);
98 return;
99 case EVENT_EXIT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200100 debug(1, "[%d] event: exit signal (%s [%d])",
101 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200102 shortsignal(event->proc, event->e_un.signum),
103 event->e_un.signum);
104 handle_exit_signal(event);
105 return;
106 case EVENT_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200107 debug(1, "[%d] event: syscall (%s [%d])",
108 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200109 sysname(event->proc, event->e_un.sysnum),
110 event->e_un.sysnum);
111 handle_syscall(event);
112 return;
113 case EVENT_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200114 debug(1, "[%d] event: sysret (%s [%d])",
115 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200116 sysname(event->proc, event->e_un.sysnum),
117 event->e_un.sysnum);
118 handle_sysret(event);
119 return;
120 case EVENT_ARCH_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200121 debug(1, "[%d] event: arch_syscall (%s [%d])",
122 event->proc->pid,
123 arch_sysname(event->proc, event->e_un.sysnum),
124 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200125 handle_arch_syscall(event);
126 return;
127 case EVENT_ARCH_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200128 debug(1, "[%d] event: arch_sysret (%s [%d])",
129 event->proc->pid,
130 arch_sysname(event->proc, event->e_un.sysnum),
131 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200132 handle_arch_sysret(event);
133 return;
134 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200135 case EVENT_VFORK:
Petr Machataebfe7d62012-04-13 21:34:49 +0200136 debug(1, "[%d] event: clone (%u)",
137 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200138 handle_clone(event);
139 return;
140 case EVENT_EXEC:
Petr Machataebfe7d62012-04-13 21:34:49 +0200141 debug(1, "[%d] event: exec()",
142 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200143 handle_exec(event);
144 return;
145 case EVENT_BREAKPOINT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200146 debug(1, "[%d] event: breakpoint %p",
147 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200148 handle_breakpoint(event);
149 return;
150 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200151 debug(1, "[%d] event: new process",
152 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200153 handle_new(event);
154 return;
155 default:
156 fprintf(stderr, "Error! unknown event?\n");
157 exit(1);
158 }
159}
160
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200161typedef struct Pending_New Pending_New;
162struct Pending_New {
163 pid_t pid;
164 Pending_New * next;
165};
166static Pending_New * pending_news = NULL;
167
168static int
169pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200170 Pending_New * p;
171
172 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
173
174 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200175 while (p) {
176 if (p->pid == pid) {
177 return 1;
178 }
179 p = p->next;
180 }
181 return 0;
182}
183
184static void
185pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200186 Pending_New * p;
187
188 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
189
190 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200191 if (!p) {
192 perror("malloc()");
193 exit(1);
194 }
195 p->pid = pid;
196 p->next = pending_news;
197 pending_news = p;
198}
199
200static void
201pending_new_remove(pid_t pid) {
202 Pending_New *p, *pred;
203
Juan Cespedescd8976d2009-05-14 13:47:58 +0200204 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
205
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200206 p = pending_news;
207 if (p->pid == pid) {
208 pending_news = p->next;
209 free(p);
210 } else {
211 while (p) {
212 if (p->pid == pid) {
213 pred->next = p->next;
214 free(p);
215 }
216 pred = p;
217 p = p->next;
218 }
219 }
220}
221
222static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223handle_clone(Event *event)
224{
Juan Cespedes03192f82009-07-03 10:16:22 +0200225 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200226
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227 struct Process *proc = malloc(sizeof(*proc));
228 if (proc == NULL) {
229 fail:
230 free(proc);
231 /* XXX proper error handling here, please. */
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200232 perror("malloc()");
233 exit(1);
234 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235
236 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
237 goto fail;
238 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200239
Petr Machata75dcf7d2011-10-06 14:30:19 +0200240 /* We save register values to the arch pointer, and these need
241 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100242 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200243
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 if (pending_new(proc->pid)) {
245 pending_new_remove(proc->pid);
246 /* XXX this used to be destroy_event_handler call, but
247 * I don't think we want to call that on a shared
248 * state. */
249 proc->event_handler = NULL;
250 if (event->proc->state == STATE_ATTACHED && options.follow)
251 proc->state = STATE_ATTACHED;
252 else
253 proc->state = STATE_IGNORED;
254 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200255 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200257 }
Petr Machata534e00f2011-09-27 17:58:38 +0200258
Petr Machatacbe29c62011-09-27 02:27:58 +0200259 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200261 else
262 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200263}
264
265static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200266handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200267 Process * proc;
268
Juan Cespedes03192f82009-07-03 10:16:22 +0200269 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200270
271 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200272 if (!proc) {
273 pending_new_insert(event->e_un.newpid);
274 } else {
275 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200276 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200277 proc->state = STATE_ATTACHED;
278 } else {
279 proc->state = STATE_IGNORED;
280 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200281 continue_process(proc->pid);
282 }
283}
284
Juan Cespedesf1350522008-12-16 18:19:58 +0100285static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200286shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 static char *signalent0[] = {
288#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100289 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100290 static char *signalent1[] = {
291#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100292 };
293 static char **signalents[] = { signalent0, signalent1 };
294 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 sizeof signalent1 / sizeof signalent1[0]
296 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100297
Juan Cespedescd8976d2009-05-14 13:47:58 +0200298 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
299
Ian Wienand9a2ad352006-02-20 22:44:45 +0100300 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100301 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100302 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100303 return "UNKNOWN_SIGNAL";
304 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100305 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100306 }
307}
308
Juan Cespedesf1350522008-12-16 18:19:58 +0100309static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200310sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100311 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 static char *syscalent0[] = {
313#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100314 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100315 static char *syscalent1[] = {
316#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100317 };
318 static char **syscalents[] = { syscalent0, syscalent1 };
319 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 sizeof syscalent1 / sizeof syscalent1[0]
321 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100322
Juan Cespedescd8976d2009-05-14 13:47:58 +0200323 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
324
Ian Wienand9a2ad352006-02-20 22:44:45 +0100325 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100327 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100328 sprintf(result, "SYS_%d", sysnum);
329 return result;
330 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 sprintf(result, "SYS_%s",
332 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100333 return result;
334 }
335}
336
Juan Cespedesf1350522008-12-16 18:19:58 +0100337static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200338arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100339 static char result[128];
340 static char *arch_syscalent[] = {
341#include "arch_syscallent.h"
342 };
343 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
344
Juan Cespedescd8976d2009-05-14 13:47:58 +0200345 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
346
Juan Cespedes63184be2008-12-10 13:30:12 +0100347 if (sysnum < 0 || sysnum >= nsyscals) {
348 sprintf(result, "ARCH_%d", sysnum);
349 return result;
350 } else {
351 sprintf(result, "ARCH_%s",
352 arch_syscalent[sysnum]);
353 return result;
354 }
355}
356
Juan Cespedesf1350522008-12-16 18:19:58 +0100357static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200358handle_signal(Event *event) {
359 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800360 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200361 output_line(event->proc, "--- %s (%s) ---",
362 shortsignal(event->proc, event->e_un.signum),
363 strsignal(event->e_un.signum));
364 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100365 continue_after_signal(event->proc->pid, event->e_un.signum);
366}
367
Juan Cespedesf1350522008-12-16 18:19:58 +0100368static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200369handle_exit(Event *event) {
370 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200371 if (event->proc->state != STATE_IGNORED) {
372 output_line(event->proc, "+++ exited (status %d) +++",
373 event->e_un.ret_val);
374 }
Petr Machatacebb8842011-07-09 11:14:11 +0200375 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100376 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100377}
378
Juan Cespedesf1350522008-12-16 18:19:58 +0100379static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200380handle_exit_signal(Event *event) {
381 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200382 if (event->proc->state != STATE_IGNORED) {
383 output_line(event->proc, "+++ killed by %s +++",
384 shortsignal(event->proc, event->e_un.signum));
385 }
Petr Machatacebb8842011-07-09 11:14:11 +0200386 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100387 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100388}
389
Petr Machata29add4f2012-02-18 16:38:05 +0100390static struct library_symbol *
391temporary_syscall_symbol(const char *name)
392{
393 struct library *syscalls = malloc(sizeof(*syscalls));
394 struct library_symbol *syscall = malloc(sizeof(*syscall));
395 if (syscalls == NULL || syscall == NULL) {
396 free(syscalls);
397 free(syscall);
398 return NULL;
399 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200400 library_init(syscalls, (enum library_type)-1);
Petr Machata0b55b582012-04-02 00:38:46 +0200401 library_set_soname(syscalls, "SYS", 0);
Petr Machatae6523e62012-03-24 04:54:06 +0100402 library_symbol_init(syscall, 0, name, 0, LS_TOPLT_NONE);
Petr Machata29add4f2012-02-18 16:38:05 +0100403 library_add_symbol(syscalls, syscall);
404 return syscall;
405}
406
407static void
408output_syscall_left(struct Process *proc, const char *name)
409{
410 struct library_symbol *syscall = temporary_syscall_symbol(name);
411 output_left(LT_TOF_SYSCALL, proc, syscall);
412 struct library *lib = syscall->lib;
413 library_destroy(lib);
414 free(lib);
415}
416
417static void
418output_syscall_right(struct Process *proc, const char *name)
419{
420 struct library_symbol *syscall = temporary_syscall_symbol(name);
421 output_right(LT_TOF_SYSCALLR, proc, syscall);
422 struct library *lib = syscall->lib;
423 library_destroy(lib);
424 free(lib);
425}
426
Juan Cespedesf1350522008-12-16 18:19:58 +0100427static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200428handle_syscall(Event *event) {
429 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200430 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100431 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100432 if (options.syscalls)
433 output_syscall_left(event->proc,
434 sysname(event->proc,
435 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100436 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100437 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100438}
439
Juan Cespedesf1350522008-12-16 18:19:58 +0100440static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200441handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200442 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200443
Juan Cespedes03192f82009-07-03 10:16:22 +0200444 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200445 if (proc->state == STATE_IGNORED) {
446 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200447 remove_process(proc);
Petr Machata464026f2012-03-02 00:10:58 +0100448 free(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200449 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200450 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200451 output_line(proc, "--- Called exec() ---");
452 proc->mask_32bit = 0;
453 proc->personality = 0;
454 proc->arch_ptr = NULL;
455 free(proc->filename);
456 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200457 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200458 proc->callstack_depth = 0;
459 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200460
461 /* After the exec, we expect to hit the first executable
Petr Machata9847abe2012-04-14 00:36:03 +0200462 * instruction.
463 *
464 * XXX TODO It would be nice to have this removed, but then we
465 * need to do that also for initial call to wait_for_proc in
466 * execute_program. In that case we could generate a
467 * EVENT_FIRST event or something, or maybe this could somehow
468 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200469 wait_for_proc(proc->pid);
470 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200471}
472
473static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200474handle_arch_syscall(Event *event) {
475 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200476 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100477 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200478 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100479 output_syscall_left(event->proc,
480 arch_sysname(event->proc,
481 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200482 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100483 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100484 continue_process(event->proc->pid);
485}
486
Juan Cespedesd65efa32003-02-03 00:22:30 +0100487struct timeval current_time_spent;
488
Juan Cespedesf1350522008-12-16 18:19:58 +0100489static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200490calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100491 struct timeval tv;
492 struct timezone tz;
493 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100495
Juan Cespedescd8976d2009-05-14 13:47:58 +0200496 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100497 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100498
499 gettimeofday(&tv, &tz);
500
501 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
502 if (tv.tv_usec >= elem->time_spent.tv_usec) {
503 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
504 } else {
505 diff.tv_sec++;
506 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
507 }
508 current_time_spent = diff;
509}
510
Juan Cespedesf1350522008-12-16 18:19:58 +0100511static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200512handle_sysret(Event *event) {
513 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200514 if (event->proc->state != STATE_IGNORED) {
515 if (opt_T || options.summary) {
516 calc_time_spent(event->proc);
517 }
Petr Machata29add4f2012-02-18 16:38:05 +0100518 if (options.syscalls)
519 output_syscall_right(event->proc,
520 sysname(event->proc,
521 event->e_un.sysnum));
522
Petr Machata43d2fe52011-11-02 13:25:49 +0100523 assert(event->proc->callstack_depth > 0);
524 unsigned d = event->proc->callstack_depth - 1;
525 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100526 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200527 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100528 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100529}
530
Juan Cespedesf1350522008-12-16 18:19:58 +0100531static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200532handle_arch_sysret(Event *event) {
533 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200534 if (event->proc->state != STATE_IGNORED) {
535 if (opt_T || options.summary) {
536 calc_time_spent(event->proc);
537 }
Petr Machata29add4f2012-02-18 16:38:05 +0100538 if (options.syscalls)
539 output_syscall_right(event->proc,
540 arch_sysname(event->proc,
541 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100542 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100543 }
544 continue_process(event->proc->pid);
545}
546
Juan Cespedesf1350522008-12-16 18:19:58 +0100547static void
Petr Machata14298742012-04-12 23:09:21 +0200548output_right_tos(struct Process *proc)
549{
550 size_t d = proc->callstack_depth;
551 struct callstack_element *elem = &proc->callstack[d - 1];
552 if (proc->state != STATE_IGNORED)
553 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
554}
555
556static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100557handle_breakpoint(Event *event)
558{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100559 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100560 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200561 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200562 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200563
564 /* The leader has terminated. */
565 if (leader == NULL) {
566 continue_process(event->proc->pid);
567 return;
568 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100569
Petr Machata31b2f9f2012-04-12 22:23:40 +0200570 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
571 event->proc->pid, brk_addr);
572 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100573
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100574 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200575 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200576#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500577 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200578 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500579 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200580 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100581 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200582 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500583 if (sbp) {
584 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200585 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500586 }
587 } else {
588 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
589 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200590 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200591 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200592 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200593 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200594#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100595 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100596 callstack_pop(event->proc);
597 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200598 if (event->proc->state != STATE_IGNORED) {
599 if (opt_T || options.summary) {
600 calc_time_spent(event->proc);
601 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100602 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200603 event->proc->return_addr = brk_addr;
604
Petr Machata14298742012-04-12 23:09:21 +0200605 output_right_tos(event->proc);
606 callstack_pop(event->proc);
607
Petr Machata31b2f9f2012-04-12 22:23:40 +0200608 /* Pop also any other entries that seem like
609 * they are linked to the current one: they
610 * have the same return address, but were made
611 * for different symbols. This should only
612 * happen for entry point tracing, i.e. for -x
613 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200614 while (event->proc->callstack_depth > 0) {
615 struct callstack_element *prev;
616 size_t d = event->proc->callstack_depth;
617 prev = &event->proc->callstack[d - 1];
618
Petr Machata14298742012-04-12 23:09:21 +0200619 if (prev->c_un.libfunc == libsym
620 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200621 break;
Petr Machata14298742012-04-12 23:09:21 +0200622
623 output_right_tos(event->proc);
624 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200625 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200626
627 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200628 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100629 return;
630 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100631 }
632
Petr Machata31b2f9f2012-04-12 22:23:40 +0200633 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100634 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100635
Petr Machata2b46cfc2012-02-18 11:17:29 +0100636 if (event->proc->state != STATE_IGNORED
637 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200638 event->proc->stack_pointer = get_stack_pointer(event->proc);
639 event->proc->return_addr =
640 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200641 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100642 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200643 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100644
Petr Machata56a9ea62012-03-27 03:09:29 +0200645 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100646 return;
647 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100648
Petr Machata796267f2012-04-04 19:09:37 +0200649 if (event->proc->state != STATE_IGNORED)
Juan Cespedes5c682042009-05-21 15:59:56 +0200650 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200651 brk_addr);
Petr Machata796267f2012-04-04 19:09:37 +0200652
Juan Cespedes5e01f651998-03-08 22:31:44 +0100653 continue_process(event->proc->pid);
654}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200655
Juan Cespedesf1350522008-12-16 18:19:58 +0100656static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200657callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100658 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200659
Juan Cespedescd8976d2009-05-14 13:47:58 +0200660 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200661 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500663 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
664 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200665 return;
666 }
667
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100668 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200669 elem->is_syscall = 1;
670 elem->c_un.syscall = sysnum;
671 elem->return_addr = NULL;
672
673 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200674 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100675 struct timezone tz;
676 gettimeofday(&elem->time_spent, &tz);
677 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200678}
679
Juan Cespedes21c63a12001-07-07 20:56:56 +0200680static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200681callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200682 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200683
Juan Cespedescd8976d2009-05-14 13:47:58 +0200684 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200685 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100686 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500687 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
688 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200689 return;
690 }
691
Petr Machata14298742012-04-12 23:09:21 +0200692 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200693 elem->is_syscall = 0;
694 elem->c_un.libfunc = sym;
695
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200696 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100697 if (elem->return_addr)
698 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200699
Arnaud Patard26570082010-01-08 08:40:12 -0500700 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200701 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100702 struct timezone tz;
703 gettimeofday(&elem->time_spent, &tz);
704 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200705}
706
Juan Cespedesf1350522008-12-16 18:19:58 +0100707static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200708callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100709 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200710 assert(proc->callstack_depth > 0);
711
Juan Cespedescd8976d2009-05-14 13:47:58 +0200712 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100713 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200714 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200715 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200716 delete_breakpoint(proc, elem->return_addr);
717 }
Petr Machata211f0882010-11-03 18:42:18 +0100718 if (elem->arch_ptr != NULL) {
719 free(elem->arch_ptr);
720 elem->arch_ptr = NULL;
721 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200722 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200723}