blob: 4513540c1173063b2d2bce72ecac1f5ca26be907 [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:
Petr Machataebfe7d62012-04-13 21:34:49 +020091 debug(1, "[%d] event: signal (%s [%d])",
92 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +020093 shortsignal(event->proc, event->e_un.signum),
94 event->e_un.signum);
95 handle_signal(event);
96 return;
97 case EVENT_EXIT:
Petr Machataebfe7d62012-04-13 21:34:49 +020098 debug(1, "[%d] event: exit (%d)",
99 event->proc->pid,
100 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200101 handle_exit(event);
102 return;
103 case EVENT_EXIT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200104 debug(1, "[%d] event: exit signal (%s [%d])",
105 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200106 shortsignal(event->proc, event->e_un.signum),
107 event->e_un.signum);
108 handle_exit_signal(event);
109 return;
110 case EVENT_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200111 debug(1, "[%d] event: syscall (%s [%d])",
112 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200113 sysname(event->proc, event->e_un.sysnum),
114 event->e_un.sysnum);
115 handle_syscall(event);
116 return;
117 case EVENT_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200118 debug(1, "[%d] event: sysret (%s [%d])",
119 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200120 sysname(event->proc, event->e_un.sysnum),
121 event->e_un.sysnum);
122 handle_sysret(event);
123 return;
124 case EVENT_ARCH_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200125 debug(1, "[%d] event: arch_syscall (%s [%d])",
126 event->proc->pid,
127 arch_sysname(event->proc, event->e_un.sysnum),
128 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200129 handle_arch_syscall(event);
130 return;
131 case EVENT_ARCH_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200132 debug(1, "[%d] event: arch_sysret (%s [%d])",
133 event->proc->pid,
134 arch_sysname(event->proc, event->e_un.sysnum),
135 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200136 handle_arch_sysret(event);
137 return;
138 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200139 case EVENT_VFORK:
Petr Machataebfe7d62012-04-13 21:34:49 +0200140 debug(1, "[%d] event: clone (%u)",
141 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200142 handle_clone(event);
143 return;
144 case EVENT_EXEC:
Petr Machataebfe7d62012-04-13 21:34:49 +0200145 debug(1, "[%d] event: exec()",
146 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200147 handle_exec(event);
148 return;
149 case EVENT_BREAKPOINT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200150 debug(1, "[%d] event: breakpoint %p",
151 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200152 handle_breakpoint(event);
153 return;
154 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200155 debug(1, "[%d] event: new process",
156 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200157 handle_new(event);
158 return;
159 default:
160 fprintf(stderr, "Error! unknown event?\n");
161 exit(1);
162 }
163}
164
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200165typedef struct Pending_New Pending_New;
166struct Pending_New {
167 pid_t pid;
168 Pending_New * next;
169};
170static Pending_New * pending_news = NULL;
171
172static int
173pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200174 Pending_New * p;
175
176 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
177
178 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200179 while (p) {
180 if (p->pid == pid) {
181 return 1;
182 }
183 p = p->next;
184 }
185 return 0;
186}
187
188static void
189pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200190 Pending_New * p;
191
192 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
193
194 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200195 if (!p) {
196 perror("malloc()");
197 exit(1);
198 }
199 p->pid = pid;
200 p->next = pending_news;
201 pending_news = p;
202}
203
204static void
205pending_new_remove(pid_t pid) {
206 Pending_New *p, *pred;
207
Juan Cespedescd8976d2009-05-14 13:47:58 +0200208 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
209
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200210 p = pending_news;
211 if (p->pid == pid) {
212 pending_news = p->next;
213 free(p);
214 } else {
215 while (p) {
216 if (p->pid == pid) {
217 pred->next = p->next;
218 free(p);
219 }
220 pred = p;
221 p = p->next;
222 }
223 }
224}
225
226static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227handle_clone(Event *event)
228{
Juan Cespedes03192f82009-07-03 10:16:22 +0200229 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200230
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231 struct Process *proc = malloc(sizeof(*proc));
232 if (proc == NULL) {
233 fail:
234 free(proc);
235 /* XXX proper error handling here, please. */
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200236 perror("malloc()");
237 exit(1);
238 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100239
240 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
241 goto fail;
242 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200243
Petr Machata75dcf7d2011-10-06 14:30:19 +0200244 /* We save register values to the arch pointer, and these need
245 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200247
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248 if (pending_new(proc->pid)) {
249 pending_new_remove(proc->pid);
250 /* XXX this used to be destroy_event_handler call, but
251 * I don't think we want to call that on a shared
252 * state. */
253 proc->event_handler = NULL;
254 if (event->proc->state == STATE_ATTACHED && options.follow)
255 proc->state = STATE_ATTACHED;
256 else
257 proc->state = STATE_IGNORED;
258 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200259 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200261 }
Petr Machata534e00f2011-09-27 17:58:38 +0200262
Petr Machatacbe29c62011-09-27 02:27:58 +0200263 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100264 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200265 else
266 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200267}
268
269static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200270handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200271 Process * proc;
272
Juan Cespedes03192f82009-07-03 10:16:22 +0200273 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200274
275 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200276 if (!proc) {
277 pending_new_insert(event->e_un.newpid);
278 } else {
279 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200280 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200281 proc->state = STATE_ATTACHED;
282 } else {
283 proc->state = STATE_IGNORED;
284 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200285 continue_process(proc->pid);
286 }
287}
288
Juan Cespedesf1350522008-12-16 18:19:58 +0100289static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200290shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 static char *signalent0[] = {
292#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 static char *signalent1[] = {
295#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100296 };
297 static char **signalents[] = { signalent0, signalent1 };
298 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 sizeof signalent1 / sizeof signalent1[0]
300 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100301
Juan Cespedescd8976d2009-05-14 13:47:58 +0200302 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
303
Ian Wienand9a2ad352006-02-20 22:44:45 +0100304 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100306 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100307 return "UNKNOWN_SIGNAL";
308 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100309 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100310 }
311}
312
Juan Cespedesf1350522008-12-16 18:19:58 +0100313static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200314sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100315 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 static char *syscalent0[] = {
317#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100318 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 static char *syscalent1[] = {
320#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100321 };
322 static char **syscalents[] = { syscalent0, syscalent1 };
323 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 sizeof syscalent1 / sizeof syscalent1[0]
325 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100326
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
328
Ian Wienand9a2ad352006-02-20 22:44:45 +0100329 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100331 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332 sprintf(result, "SYS_%d", sysnum);
333 return result;
334 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 sprintf(result, "SYS_%s",
336 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337 return result;
338 }
339}
340
Juan Cespedesf1350522008-12-16 18:19:58 +0100341static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200342arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100343 static char result[128];
344 static char *arch_syscalent[] = {
345#include "arch_syscallent.h"
346 };
347 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
348
Juan Cespedescd8976d2009-05-14 13:47:58 +0200349 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
350
Juan Cespedes63184be2008-12-10 13:30:12 +0100351 if (sysnum < 0 || sysnum >= nsyscals) {
352 sprintf(result, "ARCH_%d", sysnum);
353 return result;
354 } else {
355 sprintf(result, "ARCH_%s",
356 arch_syscalent[sysnum]);
357 return result;
358 }
359}
360
Juan Cespedesf1350522008-12-16 18:19:58 +0100361static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200362handle_signal(Event *event) {
363 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800364 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200365 output_line(event->proc, "--- %s (%s) ---",
366 shortsignal(event->proc, event->e_un.signum),
367 strsignal(event->e_un.signum));
368 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100369 continue_after_signal(event->proc->pid, event->e_un.signum);
370}
371
Juan Cespedesf1350522008-12-16 18:19:58 +0100372static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200373handle_exit(Event *event) {
374 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200375 if (event->proc->state != STATE_IGNORED) {
376 output_line(event->proc, "+++ exited (status %d) +++",
377 event->e_un.ret_val);
378 }
Petr Machatacebb8842011-07-09 11:14:11 +0200379 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100380 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100381}
382
Juan Cespedesf1350522008-12-16 18:19:58 +0100383static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200384handle_exit_signal(Event *event) {
385 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200386 if (event->proc->state != STATE_IGNORED) {
387 output_line(event->proc, "+++ killed by %s +++",
388 shortsignal(event->proc, event->e_un.signum));
389 }
Petr Machatacebb8842011-07-09 11:14:11 +0200390 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100391 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100392}
393
Petr Machata29add4f2012-02-18 16:38:05 +0100394static struct library_symbol *
395temporary_syscall_symbol(const char *name)
396{
397 struct library *syscalls = malloc(sizeof(*syscalls));
398 struct library_symbol *syscall = malloc(sizeof(*syscall));
399 if (syscalls == NULL || syscall == NULL) {
400 free(syscalls);
401 free(syscall);
402 return NULL;
403 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200404 library_init(syscalls, (enum library_type)-1);
Petr Machata0b55b582012-04-02 00:38:46 +0200405 library_set_soname(syscalls, "SYS", 0);
Petr Machatae6523e62012-03-24 04:54:06 +0100406 library_symbol_init(syscall, 0, name, 0, LS_TOPLT_NONE);
Petr Machata29add4f2012-02-18 16:38:05 +0100407 library_add_symbol(syscalls, syscall);
408 return syscall;
409}
410
411static void
412output_syscall_left(struct Process *proc, const char *name)
413{
414 struct library_symbol *syscall = temporary_syscall_symbol(name);
415 output_left(LT_TOF_SYSCALL, proc, syscall);
416 struct library *lib = syscall->lib;
417 library_destroy(lib);
418 free(lib);
419}
420
421static void
422output_syscall_right(struct Process *proc, const char *name)
423{
424 struct library_symbol *syscall = temporary_syscall_symbol(name);
425 output_right(LT_TOF_SYSCALLR, proc, syscall);
426 struct library *lib = syscall->lib;
427 library_destroy(lib);
428 free(lib);
429}
430
Juan Cespedesf1350522008-12-16 18:19:58 +0100431static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200432handle_syscall(Event *event) {
433 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200434 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100435 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100436 if (options.syscalls)
437 output_syscall_left(event->proc,
438 sysname(event->proc,
439 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100440 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100441 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100442}
443
Juan Cespedesf1350522008-12-16 18:19:58 +0100444static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200445handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200446 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200447
Juan Cespedes03192f82009-07-03 10:16:22 +0200448 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200449 if (proc->state == STATE_IGNORED) {
450 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200451 remove_process(proc);
Petr Machata464026f2012-03-02 00:10:58 +0100452 free(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200453 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200454 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200455 output_line(proc, "--- Called exec() ---");
456 proc->mask_32bit = 0;
457 proc->personality = 0;
458 proc->arch_ptr = NULL;
459 free(proc->filename);
460 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200461 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200462 proc->callstack_depth = 0;
463 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200464
465 /* After the exec, we expect to hit the first executable
466 * instruction. It would be nice to have this removed, but
467 * then we need to do that also for initial call to
468 * wait_for_proc in execute_program. XXX todo. */
469 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}