blob: e47b9a2786f309988d8f3315420ad85354997a81 [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
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226#if 0
Petr Machata534e00f2011-09-27 17:58:38 +0200227static int
228clone_breakpoints(Process * proc, Process * orig_proc)
229{
230 /* When copying breakpoints, we also have to copy the
231 * referenced symbols, and link them properly. */
232 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
233 struct library_symbol * it = proc->list_of_symbols;
234 proc->list_of_symbols = NULL;
235 for (; it != NULL; it = it->next) {
236 struct library_symbol * libsym = clone_library_symbol(it);
237 if (libsym == NULL) {
238 int save_errno;
239 err:
240 save_errno = errno;
241 destroy_library_symbol_chain(proc->list_of_symbols);
242 dict_clear(map);
243 errno = save_errno;
244 return -1;
245 }
246 libsym->next = proc->list_of_symbols;
247 proc->list_of_symbols = libsym;
248 if (dict_enter(map, it, libsym) != 0)
249 goto err;
250 }
251
252 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
253 address_clone, breakpoint_clone, map);
254 if (proc->breakpoints == NULL)
255 goto err;
256
257 dict_clear(map);
258 return 0;
259}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260#endif
Petr Machata534e00f2011-09-27 17:58:38 +0200261
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200262static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263handle_clone(Event *event)
264{
Juan Cespedes03192f82009-07-03 10:16:22 +0200265 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200266
Petr Machata2b46cfc2012-02-18 11:17:29 +0100267 struct Process *proc = malloc(sizeof(*proc));
268 if (proc == NULL) {
269 fail:
270 free(proc);
271 /* XXX proper error handling here, please. */
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200272 perror("malloc()");
273 exit(1);
274 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275
276 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
277 goto fail;
278 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200279
Petr Machata75dcf7d2011-10-06 14:30:19 +0200280 /* We save register values to the arch pointer, and these need
281 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100282 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200283
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 if (pending_new(proc->pid)) {
285 pending_new_remove(proc->pid);
286 /* XXX this used to be destroy_event_handler call, but
287 * I don't think we want to call that on a shared
288 * state. */
289 proc->event_handler = NULL;
290 if (event->proc->state == STATE_ATTACHED && options.follow)
291 proc->state = STATE_ATTACHED;
292 else
293 proc->state = STATE_IGNORED;
294 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200295 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200297 }
Petr Machata534e00f2011-09-27 17:58:38 +0200298
Petr Machatacbe29c62011-09-27 02:27:58 +0200299 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100300 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200301 else
302 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200303}
304
305static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200306handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200307 Process * proc;
308
Juan Cespedes03192f82009-07-03 10:16:22 +0200309 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200310
311 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200312 if (!proc) {
313 pending_new_insert(event->e_un.newpid);
314 } else {
315 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200316 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200317 proc->state = STATE_ATTACHED;
318 } else {
319 proc->state = STATE_IGNORED;
320 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200321 continue_process(proc->pid);
322 }
323}
324
Juan Cespedesf1350522008-12-16 18:19:58 +0100325static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200326shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100327 static char *signalent0[] = {
328#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100329 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 static char *signalent1[] = {
331#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100332 };
333 static char **signalents[] = { signalent0, signalent1 };
334 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 sizeof signalent1 / sizeof signalent1[0]
336 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337
Juan Cespedescd8976d2009-05-14 13:47:58 +0200338 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
339
Ian Wienand9a2ad352006-02-20 22:44:45 +0100340 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100342 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100343 return "UNKNOWN_SIGNAL";
344 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100345 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100346 }
347}
348
Juan Cespedesf1350522008-12-16 18:19:58 +0100349static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200350sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100351 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100352 static char *syscalent0[] = {
353#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100354 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 static char *syscalent1[] = {
356#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100357 };
358 static char **syscalents[] = { syscalent0, syscalent1 };
359 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 sizeof syscalent1 / sizeof syscalent1[0]
361 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362
Juan Cespedescd8976d2009-05-14 13:47:58 +0200363 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
364
Ian Wienand9a2ad352006-02-20 22:44:45 +0100365 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100366 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100367 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100368 sprintf(result, "SYS_%d", sysnum);
369 return result;
370 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 sprintf(result, "SYS_%s",
372 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100373 return result;
374 }
375}
376
Juan Cespedesf1350522008-12-16 18:19:58 +0100377static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200378arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100379 static char result[128];
380 static char *arch_syscalent[] = {
381#include "arch_syscallent.h"
382 };
383 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
384
Juan Cespedescd8976d2009-05-14 13:47:58 +0200385 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
386
Juan Cespedes63184be2008-12-10 13:30:12 +0100387 if (sysnum < 0 || sysnum >= nsyscals) {
388 sprintf(result, "ARCH_%d", sysnum);
389 return result;
390 } else {
391 sprintf(result, "ARCH_%s",
392 arch_syscalent[sysnum]);
393 return result;
394 }
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200398handle_signal(Event *event) {
399 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800400 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200401 output_line(event->proc, "--- %s (%s) ---",
402 shortsignal(event->proc, event->e_un.signum),
403 strsignal(event->e_un.signum));
404 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100405 continue_after_signal(event->proc->pid, event->e_un.signum);
406}
407
Juan Cespedesf1350522008-12-16 18:19:58 +0100408static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200409handle_exit(Event *event) {
410 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200411 if (event->proc->state != STATE_IGNORED) {
412 output_line(event->proc, "+++ exited (status %d) +++",
413 event->e_un.ret_val);
414 }
Petr Machatacebb8842011-07-09 11:14:11 +0200415 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100416 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100417}
418
Juan Cespedesf1350522008-12-16 18:19:58 +0100419static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200420handle_exit_signal(Event *event) {
421 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200422 if (event->proc->state != STATE_IGNORED) {
423 output_line(event->proc, "+++ killed by %s +++",
424 shortsignal(event->proc, event->e_un.signum));
425 }
Petr Machatacebb8842011-07-09 11:14:11 +0200426 remove_process(event->proc);
Petr Machata464026f2012-03-02 00:10:58 +0100427 free(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100428}
429
Petr Machata29add4f2012-02-18 16:38:05 +0100430static struct library_symbol *
431temporary_syscall_symbol(const char *name)
432{
433 struct library *syscalls = malloc(sizeof(*syscalls));
434 struct library_symbol *syscall = malloc(sizeof(*syscall));
435 if (syscalls == NULL || syscall == NULL) {
436 free(syscalls);
437 free(syscall);
438 return NULL;
439 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200440 library_init(syscalls, (enum library_type)-1);
Petr Machata0b55b582012-04-02 00:38:46 +0200441 library_set_soname(syscalls, "SYS", 0);
Petr Machatae6523e62012-03-24 04:54:06 +0100442 library_symbol_init(syscall, 0, name, 0, LS_TOPLT_NONE);
Petr Machata29add4f2012-02-18 16:38:05 +0100443 library_add_symbol(syscalls, syscall);
444 return syscall;
445}
446
447static void
448output_syscall_left(struct Process *proc, const char *name)
449{
450 struct library_symbol *syscall = temporary_syscall_symbol(name);
451 output_left(LT_TOF_SYSCALL, proc, syscall);
452 struct library *lib = syscall->lib;
453 library_destroy(lib);
454 free(lib);
455}
456
457static void
458output_syscall_right(struct Process *proc, const char *name)
459{
460 struct library_symbol *syscall = temporary_syscall_symbol(name);
461 output_right(LT_TOF_SYSCALLR, proc, syscall);
462 struct library *lib = syscall->lib;
463 library_destroy(lib);
464 free(lib);
465}
466
Juan Cespedesf1350522008-12-16 18:19:58 +0100467static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200468handle_syscall(Event *event) {
469 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100471 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100472 if (options.syscalls)
473 output_syscall_left(event->proc,
474 sysname(event->proc,
475 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100476 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100477 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100478}
479
Juan Cespedesf1350522008-12-16 18:19:58 +0100480static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200481handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200482 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200483
Juan Cespedes03192f82009-07-03 10:16:22 +0200484 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200485 if (proc->state == STATE_IGNORED) {
486 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200487 remove_process(proc);
Petr Machata464026f2012-03-02 00:10:58 +0100488 free(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200489 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200490 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200491 output_line(proc, "--- Called exec() ---");
492 proc->mask_32bit = 0;
493 proc->personality = 0;
494 proc->arch_ptr = NULL;
495 free(proc->filename);
496 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200497 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200498 proc->callstack_depth = 0;
499 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200500}
501
502static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200503handle_arch_syscall(Event *event) {
504 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200505 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100506 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200507 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100508 output_syscall_left(event->proc,
509 arch_sysname(event->proc,
510 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200511 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100512 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100513 continue_process(event->proc->pid);
514}
515
Juan Cespedesd65efa32003-02-03 00:22:30 +0100516struct timeval current_time_spent;
517
Juan Cespedesf1350522008-12-16 18:19:58 +0100518static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200519calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100520 struct timeval tv;
521 struct timezone tz;
522 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100523 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100524
Juan Cespedescd8976d2009-05-14 13:47:58 +0200525 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100526 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100527
528 gettimeofday(&tv, &tz);
529
530 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
531 if (tv.tv_usec >= elem->time_spent.tv_usec) {
532 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
533 } else {
534 diff.tv_sec++;
535 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
536 }
537 current_time_spent = diff;
538}
539
Juan Cespedesf1350522008-12-16 18:19:58 +0100540static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200541handle_sysret(Event *event) {
542 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200543 if (event->proc->state != STATE_IGNORED) {
544 if (opt_T || options.summary) {
545 calc_time_spent(event->proc);
546 }
Petr Machata29add4f2012-02-18 16:38:05 +0100547 if (options.syscalls)
548 output_syscall_right(event->proc,
549 sysname(event->proc,
550 event->e_un.sysnum));
551
Petr Machata43d2fe52011-11-02 13:25:49 +0100552 assert(event->proc->callstack_depth > 0);
553 unsigned d = event->proc->callstack_depth - 1;
554 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100555 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200556 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100557 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100558}
559
Juan Cespedesf1350522008-12-16 18:19:58 +0100560static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200561handle_arch_sysret(Event *event) {
562 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200563 if (event->proc->state != STATE_IGNORED) {
564 if (opt_T || options.summary) {
565 calc_time_spent(event->proc);
566 }
Petr Machata29add4f2012-02-18 16:38:05 +0100567 if (options.syscalls)
568 output_syscall_right(event->proc,
569 arch_sysname(event->proc,
570 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100571 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100572 }
573 continue_process(event->proc->pid);
574}
575
Juan Cespedesf1350522008-12-16 18:19:58 +0100576static void
Petr Machata14298742012-04-12 23:09:21 +0200577output_right_tos(struct Process *proc)
578{
579 size_t d = proc->callstack_depth;
580 struct callstack_element *elem = &proc->callstack[d - 1];
581 if (proc->state != STATE_IGNORED)
582 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
583}
584
585static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100586handle_breakpoint(Event *event)
587{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100589 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200590 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200591 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200592
593 /* The leader has terminated. */
594 if (leader == NULL) {
595 continue_process(event->proc->pid);
596 return;
597 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100598
Petr Machata31b2f9f2012-04-12 22:23:40 +0200599 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
600 event->proc->pid, brk_addr);
601 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100602
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100603 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200604 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200605#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500606 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200607 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500608 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200609 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100610 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200611 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500612 if (sbp) {
613 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200614 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500615 }
616 } else {
617 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
618 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200619 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200620 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200621 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200622 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200623#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100624 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100625 callstack_pop(event->proc);
626 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200627 if (event->proc->state != STATE_IGNORED) {
628 if (opt_T || options.summary) {
629 calc_time_spent(event->proc);
630 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100631 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200632 event->proc->return_addr = brk_addr;
633
Petr Machata14298742012-04-12 23:09:21 +0200634 output_right_tos(event->proc);
635 callstack_pop(event->proc);
636
Petr Machata31b2f9f2012-04-12 22:23:40 +0200637 /* Pop also any other entries that seem like
638 * they are linked to the current one: they
639 * have the same return address, but were made
640 * for different symbols. This should only
641 * happen for entry point tracing, i.e. for -x
642 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200643 while (event->proc->callstack_depth > 0) {
644 struct callstack_element *prev;
645 size_t d = event->proc->callstack_depth;
646 prev = &event->proc->callstack[d - 1];
647
Petr Machata14298742012-04-12 23:09:21 +0200648 if (prev->c_un.libfunc == libsym
649 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200650 break;
Petr Machata14298742012-04-12 23:09:21 +0200651
652 output_right_tos(event->proc);
653 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200654 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200655
656 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200657 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100658 return;
659 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100660 }
661
Petr Machata31b2f9f2012-04-12 22:23:40 +0200662 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100663 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100664
Petr Machata2b46cfc2012-02-18 11:17:29 +0100665 if (event->proc->state != STATE_IGNORED
666 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200667 event->proc->stack_pointer = get_stack_pointer(event->proc);
668 event->proc->return_addr =
669 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200670 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100671 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200672 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100673
Petr Machata56a9ea62012-03-27 03:09:29 +0200674 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100675 return;
676 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100677
Petr Machata796267f2012-04-04 19:09:37 +0200678 if (event->proc->state != STATE_IGNORED)
Juan Cespedes5c682042009-05-21 15:59:56 +0200679 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200680 brk_addr);
Petr Machata796267f2012-04-04 19:09:37 +0200681
Juan Cespedes5e01f651998-03-08 22:31:44 +0100682 continue_process(event->proc->pid);
683}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200684
Juan Cespedesf1350522008-12-16 18:19:58 +0100685static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200686callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100687 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200688
Juan Cespedescd8976d2009-05-14 13:47:58 +0200689 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200690 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100691 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500692 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
693 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200694 return;
695 }
696
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100697 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200698 elem->is_syscall = 1;
699 elem->c_un.syscall = sysnum;
700 elem->return_addr = NULL;
701
702 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200703 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100704 struct timezone tz;
705 gettimeofday(&elem->time_spent, &tz);
706 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200707}
708
Juan Cespedes21c63a12001-07-07 20:56:56 +0200709static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200710callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200711 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200712
Juan Cespedescd8976d2009-05-14 13:47:58 +0200713 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100715 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500716 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
717 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200718 return;
719 }
720
Petr Machata14298742012-04-12 23:09:21 +0200721 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200722 elem->is_syscall = 0;
723 elem->c_un.libfunc = sym;
724
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200725 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100726 if (elem->return_addr)
727 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200728
Arnaud Patard26570082010-01-08 08:40:12 -0500729 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200730 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100731 struct timezone tz;
732 gettimeofday(&elem->time_spent, &tz);
733 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200734}
735
Juan Cespedesf1350522008-12-16 18:19:58 +0100736static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200737callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100738 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200739 assert(proc->callstack_depth > 0);
740
Juan Cespedescd8976d2009-05-14 13:47:58 +0200741 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100742 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200743 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200744 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200745 delete_breakpoint(proc, elem->return_addr);
746 }
Petr Machata211f0882010-11-03 18:42:18 +0100747 if (elem->arch_ptr != NULL) {
748 free(elem->arch_ptr);
749 elem->arch_ptr = NULL;
750 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200751 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200752}