blob: b85287372c825c3c9d8dd527672cfc5ae672dc5f [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);
Petr Machatacb39a402012-04-13 22:19:02 +0200500
501 /* After the exec, we expect to hit the first executable
502 * instruction. It would be nice to have this removed, but
503 * then we need to do that also for initial call to
504 * wait_for_proc in execute_program. XXX todo. */
505 wait_for_proc(proc->pid);
506 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200507}
508
509static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200510handle_arch_syscall(Event *event) {
511 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200512 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100513 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200514 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100515 output_syscall_left(event->proc,
516 arch_sysname(event->proc,
517 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200518 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100519 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100520 continue_process(event->proc->pid);
521}
522
Juan Cespedesd65efa32003-02-03 00:22:30 +0100523struct timeval current_time_spent;
524
Juan Cespedesf1350522008-12-16 18:19:58 +0100525static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200526calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100527 struct timeval tv;
528 struct timezone tz;
529 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100530 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100531
Juan Cespedescd8976d2009-05-14 13:47:58 +0200532 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100533 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100534
535 gettimeofday(&tv, &tz);
536
537 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
538 if (tv.tv_usec >= elem->time_spent.tv_usec) {
539 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
540 } else {
541 diff.tv_sec++;
542 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
543 }
544 current_time_spent = diff;
545}
546
Juan Cespedesf1350522008-12-16 18:19:58 +0100547static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200548handle_sysret(Event *event) {
549 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200550 if (event->proc->state != STATE_IGNORED) {
551 if (opt_T || options.summary) {
552 calc_time_spent(event->proc);
553 }
Petr Machata29add4f2012-02-18 16:38:05 +0100554 if (options.syscalls)
555 output_syscall_right(event->proc,
556 sysname(event->proc,
557 event->e_un.sysnum));
558
Petr Machata43d2fe52011-11-02 13:25:49 +0100559 assert(event->proc->callstack_depth > 0);
560 unsigned d = event->proc->callstack_depth - 1;
561 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100562 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200563 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100564 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100565}
566
Juan Cespedesf1350522008-12-16 18:19:58 +0100567static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200568handle_arch_sysret(Event *event) {
569 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200570 if (event->proc->state != STATE_IGNORED) {
571 if (opt_T || options.summary) {
572 calc_time_spent(event->proc);
573 }
Petr Machata29add4f2012-02-18 16:38:05 +0100574 if (options.syscalls)
575 output_syscall_right(event->proc,
576 arch_sysname(event->proc,
577 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100578 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100579 }
580 continue_process(event->proc->pid);
581}
582
Juan Cespedesf1350522008-12-16 18:19:58 +0100583static void
Petr Machata14298742012-04-12 23:09:21 +0200584output_right_tos(struct Process *proc)
585{
586 size_t d = proc->callstack_depth;
587 struct callstack_element *elem = &proc->callstack[d - 1];
588 if (proc->state != STATE_IGNORED)
589 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
590}
591
592static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100593handle_breakpoint(Event *event)
594{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100595 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100596 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200597 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200598 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200599
600 /* The leader has terminated. */
601 if (leader == NULL) {
602 continue_process(event->proc->pid);
603 return;
604 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100605
Petr Machata31b2f9f2012-04-12 22:23:40 +0200606 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
607 event->proc->pid, brk_addr);
608 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100609
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200611 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200612#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500613 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200614 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500615 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200616 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100617 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200618 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500619 if (sbp) {
620 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200621 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500622 }
623 } else {
624 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
625 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200626 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200627 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200628 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200629 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200630#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100631 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100632 callstack_pop(event->proc);
633 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200634 if (event->proc->state != STATE_IGNORED) {
635 if (opt_T || options.summary) {
636 calc_time_spent(event->proc);
637 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100638 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200639 event->proc->return_addr = brk_addr;
640
Petr Machata14298742012-04-12 23:09:21 +0200641 output_right_tos(event->proc);
642 callstack_pop(event->proc);
643
Petr Machata31b2f9f2012-04-12 22:23:40 +0200644 /* Pop also any other entries that seem like
645 * they are linked to the current one: they
646 * have the same return address, but were made
647 * for different symbols. This should only
648 * happen for entry point tracing, i.e. for -x
649 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200650 while (event->proc->callstack_depth > 0) {
651 struct callstack_element *prev;
652 size_t d = event->proc->callstack_depth;
653 prev = &event->proc->callstack[d - 1];
654
Petr Machata14298742012-04-12 23:09:21 +0200655 if (prev->c_un.libfunc == libsym
656 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200657 break;
Petr Machata14298742012-04-12 23:09:21 +0200658
659 output_right_tos(event->proc);
660 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200661 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200662
663 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200664 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100665 return;
666 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100667 }
668
Petr Machata31b2f9f2012-04-12 22:23:40 +0200669 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100670 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100671
Petr Machata2b46cfc2012-02-18 11:17:29 +0100672 if (event->proc->state != STATE_IGNORED
673 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200674 event->proc->stack_pointer = get_stack_pointer(event->proc);
675 event->proc->return_addr =
676 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200677 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100678 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200679 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100680
Petr Machata56a9ea62012-03-27 03:09:29 +0200681 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100682 return;
683 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100684
Petr Machata796267f2012-04-04 19:09:37 +0200685 if (event->proc->state != STATE_IGNORED)
Juan Cespedes5c682042009-05-21 15:59:56 +0200686 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200687 brk_addr);
Petr Machata796267f2012-04-04 19:09:37 +0200688
Juan Cespedes5e01f651998-03-08 22:31:44 +0100689 continue_process(event->proc->pid);
690}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200691
Juan Cespedesf1350522008-12-16 18:19:58 +0100692static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200693callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100694 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200695
Juan Cespedescd8976d2009-05-14 13:47:58 +0200696 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200697 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100698 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500699 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
700 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200701 return;
702 }
703
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100704 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200705 elem->is_syscall = 1;
706 elem->c_un.syscall = sysnum;
707 elem->return_addr = NULL;
708
709 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200710 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100711 struct timezone tz;
712 gettimeofday(&elem->time_spent, &tz);
713 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714}
715
Juan Cespedes21c63a12001-07-07 20:56:56 +0200716static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200717callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200718 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200719
Juan Cespedescd8976d2009-05-14 13:47:58 +0200720 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200721 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100722 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500723 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
724 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200725 return;
726 }
727
Petr Machata14298742012-04-12 23:09:21 +0200728 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200729 elem->is_syscall = 0;
730 elem->c_un.libfunc = sym;
731
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200732 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100733 if (elem->return_addr)
734 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200735
Arnaud Patard26570082010-01-08 08:40:12 -0500736 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200737 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100738 struct timezone tz;
739 gettimeofday(&elem->time_spent, &tz);
740 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200741}
742
Juan Cespedesf1350522008-12-16 18:19:58 +0100743static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200744callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100745 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200746 assert(proc->callstack_depth > 0);
747
Juan Cespedescd8976d2009-05-14 13:47:58 +0200748 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100749 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200750 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200751 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200752 delete_breakpoint(proc, elem->return_addr);
753 }
Petr Machata211f0882010-11-03 18:42:18 +0100754 if (elem->arch_ptr != NULL) {
755 free(elem->arch_ptr);
756 elem->arch_ptr = NULL;
757 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200758 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200759}