blob: 21ed7b26fd3dfe6873c9b6e8e595d13778cb1166 [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
Petr Machata9847abe2012-04-14 00:36:03 +0200466 * instruction.
467 *
468 * XXX TODO It would be nice to have this removed, but then we
469 * need to do that also for initial call to wait_for_proc in
470 * execute_program. In that case we could generate a
471 * EVENT_FIRST event or something, or maybe this could somehow
472 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200473 wait_for_proc(proc->pid);
474 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200475}
476
477static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200478handle_arch_syscall(Event *event) {
479 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200480 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100481 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200482 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100483 output_syscall_left(event->proc,
484 arch_sysname(event->proc,
485 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200486 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100487 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100488 continue_process(event->proc->pid);
489}
490
Juan Cespedesd65efa32003-02-03 00:22:30 +0100491struct timeval current_time_spent;
492
Juan Cespedesf1350522008-12-16 18:19:58 +0100493static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200494calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100495 struct timeval tv;
496 struct timezone tz;
497 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100498 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100499
Juan Cespedescd8976d2009-05-14 13:47:58 +0200500 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100502
503 gettimeofday(&tv, &tz);
504
505 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
506 if (tv.tv_usec >= elem->time_spent.tv_usec) {
507 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
508 } else {
509 diff.tv_sec++;
510 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
511 }
512 current_time_spent = diff;
513}
514
Juan Cespedesf1350522008-12-16 18:19:58 +0100515static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200516handle_sysret(Event *event) {
517 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200518 if (event->proc->state != STATE_IGNORED) {
519 if (opt_T || options.summary) {
520 calc_time_spent(event->proc);
521 }
Petr Machata29add4f2012-02-18 16:38:05 +0100522 if (options.syscalls)
523 output_syscall_right(event->proc,
524 sysname(event->proc,
525 event->e_un.sysnum));
526
Petr Machata43d2fe52011-11-02 13:25:49 +0100527 assert(event->proc->callstack_depth > 0);
528 unsigned d = event->proc->callstack_depth - 1;
529 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100530 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200531 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100532 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100533}
534
Juan Cespedesf1350522008-12-16 18:19:58 +0100535static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200536handle_arch_sysret(Event *event) {
537 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200538 if (event->proc->state != STATE_IGNORED) {
539 if (opt_T || options.summary) {
540 calc_time_spent(event->proc);
541 }
Petr Machata29add4f2012-02-18 16:38:05 +0100542 if (options.syscalls)
543 output_syscall_right(event->proc,
544 arch_sysname(event->proc,
545 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100546 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100547 }
548 continue_process(event->proc->pid);
549}
550
Juan Cespedesf1350522008-12-16 18:19:58 +0100551static void
Petr Machata14298742012-04-12 23:09:21 +0200552output_right_tos(struct Process *proc)
553{
554 size_t d = proc->callstack_depth;
555 struct callstack_element *elem = &proc->callstack[d - 1];
556 if (proc->state != STATE_IGNORED)
557 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
558}
559
560static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100561handle_breakpoint(Event *event)
562{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100563 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100564 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200565 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200566 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200567
568 /* The leader has terminated. */
569 if (leader == NULL) {
570 continue_process(event->proc->pid);
571 return;
572 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100573
Petr Machata31b2f9f2012-04-12 22:23:40 +0200574 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
575 event->proc->pid, brk_addr);
576 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100577
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100578 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200579 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200580#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500581 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200582 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500583 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200584 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100585 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200586 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500587 if (sbp) {
588 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200589 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500590 }
591 } else {
592 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
593 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200594 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200595 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200596 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200597 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200598#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100600 callstack_pop(event->proc);
601 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200602 if (event->proc->state != STATE_IGNORED) {
603 if (opt_T || options.summary) {
604 calc_time_spent(event->proc);
605 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100606 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200607 event->proc->return_addr = brk_addr;
608
Petr Machata14298742012-04-12 23:09:21 +0200609 output_right_tos(event->proc);
610 callstack_pop(event->proc);
611
Petr Machata31b2f9f2012-04-12 22:23:40 +0200612 /* Pop also any other entries that seem like
613 * they are linked to the current one: they
614 * have the same return address, but were made
615 * for different symbols. This should only
616 * happen for entry point tracing, i.e. for -x
617 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200618 while (event->proc->callstack_depth > 0) {
619 struct callstack_element *prev;
620 size_t d = event->proc->callstack_depth;
621 prev = &event->proc->callstack[d - 1];
622
Petr Machata14298742012-04-12 23:09:21 +0200623 if (prev->c_un.libfunc == libsym
624 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200625 break;
Petr Machata14298742012-04-12 23:09:21 +0200626
627 output_right_tos(event->proc);
628 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200629 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200630
631 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200632 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100633 return;
634 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100635 }
636
Petr Machata31b2f9f2012-04-12 22:23:40 +0200637 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100638 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100639
Petr Machata2b46cfc2012-02-18 11:17:29 +0100640 if (event->proc->state != STATE_IGNORED
641 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200642 event->proc->stack_pointer = get_stack_pointer(event->proc);
643 event->proc->return_addr =
644 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200645 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100646 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200647 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100648
Petr Machata56a9ea62012-03-27 03:09:29 +0200649 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100650 return;
651 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100652
Petr Machata796267f2012-04-04 19:09:37 +0200653 if (event->proc->state != STATE_IGNORED)
Juan Cespedes5c682042009-05-21 15:59:56 +0200654 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200655 brk_addr);
Petr Machata796267f2012-04-04 19:09:37 +0200656
Juan Cespedes5e01f651998-03-08 22:31:44 +0100657 continue_process(event->proc->pid);
658}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200659
Juan Cespedesf1350522008-12-16 18:19:58 +0100660static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200661callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200663
Juan Cespedescd8976d2009-05-14 13:47:58 +0200664 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200665 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100666 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500667 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
668 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200669 return;
670 }
671
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100672 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200673 elem->is_syscall = 1;
674 elem->c_un.syscall = sysnum;
675 elem->return_addr = NULL;
676
677 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200678 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100679 struct timezone tz;
680 gettimeofday(&elem->time_spent, &tz);
681 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200682}
683
Juan Cespedes21c63a12001-07-07 20:56:56 +0200684static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200685callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200686 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200687
Juan Cespedescd8976d2009-05-14 13:47:58 +0200688 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200689 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100690 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500691 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
692 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200693 return;
694 }
695
Petr Machata14298742012-04-12 23:09:21 +0200696 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200697 elem->is_syscall = 0;
698 elem->c_un.libfunc = sym;
699
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200700 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100701 if (elem->return_addr)
702 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200703
Arnaud Patard26570082010-01-08 08:40:12 -0500704 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200705 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100706 struct timezone tz;
707 gettimeofday(&elem->time_spent, &tz);
708 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200709}
710
Juan Cespedesf1350522008-12-16 18:19:58 +0100711static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200712callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100713 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714 assert(proc->callstack_depth > 0);
715
Juan Cespedescd8976d2009-05-14 13:47:58 +0200716 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100717 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200718 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200719 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200720 delete_breakpoint(proc, elem->return_addr);
721 }
Petr Machata211f0882010-11-03 18:42:18 +0100722 if (elem->arch_ptr != NULL) {
723 free(elem->arch_ptr);
724 elem->arch_ptr = NULL;
725 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200726 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200727}