blob: afabd9689ce33bd9d5e5f3fe7f46d300e47d94b0 [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 Machatafed1e8d2012-02-07 02:06:29 +010019
Juan Cespedes03192f82009-07-03 10:16:22 +020020static void handle_signal(Event *event);
21static void handle_exit(Event *event);
22static void handle_exit_signal(Event *event);
23static void handle_syscall(Event *event);
24static void handle_arch_syscall(Event *event);
25static void handle_sysret(Event *event);
26static void handle_arch_sysret(Event *event);
27static void handle_clone(Event *event);
28static void handle_exec(Event *event);
29static void handle_breakpoint(Event *event);
30static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010031
Juan Cespedesa8909f72009-04-28 20:02:41 +020032static void callstack_push_syscall(Process *proc, int sysnum);
33static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020035static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036
Juan Cespedes61da3372009-07-03 11:55:44 +020037static char * shortsignal(Process *proc, int signum);
38static char * sysname(Process *proc, int sysnum);
39static char * arch_sysname(Process *proc, int sysnum);
40
Petr Machatacbe29c62011-09-27 02:27:58 +020041static Event *
42call_handler(Process * proc, Event * event)
43{
44 assert(proc != NULL);
45
Petr Machata366c2f42012-02-09 19:34:36 +010046 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020047 if (handler == NULL)
48 return event;
49
50 return (*handler->on_event) (handler, event);
51}
52
Juan Cespedes61da3372009-07-03 11:55:44 +020053void
Petr Machataffe4cd22012-04-11 18:01:44 +020054handle_event(Event *event)
55{
Petr Machata602330f2011-07-09 11:15:34 +020056 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020057 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020058 os_ltrace_exiting();
59 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020060 }
Petr Machata26627682011-07-08 18:15:32 +020061 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
62 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020063
64 /* If the thread group or an individual task define an
65 overriding event handler, give them a chance to kick in.
66 We will end up calling both handlers, if the first one
67 doesn't sink the event. */
68 if (event->proc != NULL) {
69 event = call_handler(event->proc, event);
70 if (event == NULL)
71 /* It was handled. */
72 return;
73
74 /* Note: the previous handler has a chance to alter
75 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +010076 if (event->proc != NULL
77 && event->proc->leader != NULL
78 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +020079 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +020080 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +020081 return;
82 }
83 }
84
Juan Cespedes61da3372009-07-03 11:55:44 +020085 switch (event->type) {
86 case EVENT_NONE:
87 debug(1, "event: none");
88 return;
89 case EVENT_SIGNAL:
90 debug(1, "event: signal (%s [%d])",
91 shortsignal(event->proc, event->e_un.signum),
92 event->e_un.signum);
93 handle_signal(event);
94 return;
95 case EVENT_EXIT:
96 debug(1, "event: exit (%d)", event->e_un.ret_val);
97 handle_exit(event);
98 return;
99 case EVENT_EXIT_SIGNAL:
100 debug(1, "event: exit signal (%s [%d])",
101 shortsignal(event->proc, event->e_un.signum),
102 event->e_un.signum);
103 handle_exit_signal(event);
104 return;
105 case EVENT_SYSCALL:
106 debug(1, "event: syscall (%s [%d])",
107 sysname(event->proc, event->e_un.sysnum),
108 event->e_un.sysnum);
109 handle_syscall(event);
110 return;
111 case EVENT_SYSRET:
112 debug(1, "event: sysret (%s [%d])",
113 sysname(event->proc, event->e_un.sysnum),
114 event->e_un.sysnum);
115 handle_sysret(event);
116 return;
117 case EVENT_ARCH_SYSCALL:
118 debug(1, "event: arch_syscall (%s [%d])",
119 arch_sysname(event->proc, event->e_un.sysnum),
120 event->e_un.sysnum);
121 handle_arch_syscall(event);
122 return;
123 case EVENT_ARCH_SYSRET:
124 debug(1, "event: arch_sysret (%s [%d])",
125 arch_sysname(event->proc, event->e_un.sysnum),
126 event->e_un.sysnum);
127 handle_arch_sysret(event);
128 return;
129 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200130 case EVENT_VFORK:
Juan Cespedes61da3372009-07-03 11:55:44 +0200131 debug(1, "event: clone (%u)", event->e_un.newpid);
132 handle_clone(event);
133 return;
134 case EVENT_EXEC:
135 debug(1, "event: exec()");
136 handle_exec(event);
137 return;
138 case EVENT_BREAKPOINT:
139 debug(1, "event: breakpoint");
140 handle_breakpoint(event);
141 return;
142 case EVENT_NEW:
143 debug(1, "event: new process");
144 handle_new(event);
145 return;
146 default:
147 fprintf(stderr, "Error! unknown event?\n");
148 exit(1);
149 }
150}
151
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200152/* TODO */
Juan Cespedes61da3372009-07-03 11:55:44 +0200153static void *
Petr Machata534e00f2011-09-27 17:58:38 +0200154address_clone(void * addr, void * data)
155{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200156 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200157 return addr;
158}
159
Juan Cespedes61da3372009-07-03 11:55:44 +0200160static void *
Petr Machatafed1e8d2012-02-07 02:06:29 +0100161breakpoint_clone(void *bp, void *data)
Petr Machata534e00f2011-09-27 17:58:38 +0200162{
Petr Machatafed1e8d2012-02-07 02:06:29 +0100163 Dict *map = data;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200164 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Petr Machata9294d822012-02-07 12:35:58 +0100165 struct breakpoint *b = malloc(sizeof(*b));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200166 if (!b) {
167 perror("malloc()");
168 exit(1);
169 }
Petr Machatafed1e8d2012-02-07 02:06:29 +0100170 memcpy(b, bp, sizeof(*b));
Petr Machata534e00f2011-09-27 17:58:38 +0200171 if (b->libsym != NULL) {
Petr Machatafed1e8d2012-02-07 02:06:29 +0100172 struct library_symbol *sym = dict_find_entry(map, b->libsym);
Petr Machata534e00f2011-09-27 17:58:38 +0200173 if (b->libsym == NULL) {
174 fprintf(stderr, "Can't find cloned symbol %s.\n",
175 b->libsym->name);
176 return NULL;
177 }
178 b->libsym = sym;
179 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200180 return b;
181}
182
183typedef struct Pending_New Pending_New;
184struct Pending_New {
185 pid_t pid;
186 Pending_New * next;
187};
188static Pending_New * pending_news = NULL;
189
190static int
191pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200192 Pending_New * p;
193
194 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
195
196 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200197 while (p) {
198 if (p->pid == pid) {
199 return 1;
200 }
201 p = p->next;
202 }
203 return 0;
204}
205
206static void
207pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200208 Pending_New * p;
209
210 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
211
212 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200213 if (!p) {
214 perror("malloc()");
215 exit(1);
216 }
217 p->pid = pid;
218 p->next = pending_news;
219 pending_news = p;
220}
221
222static void
223pending_new_remove(pid_t pid) {
224 Pending_New *p, *pred;
225
Juan Cespedescd8976d2009-05-14 13:47:58 +0200226 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
227
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200228 p = pending_news;
229 if (p->pid == pid) {
230 pending_news = p->next;
231 free(p);
232 } else {
233 while (p) {
234 if (p->pid == pid) {
235 pred->next = p->next;
236 free(p);
237 }
238 pred = p;
239 p = p->next;
240 }
241 }
242}
243
Petr Machata534e00f2011-09-27 17:58:38 +0200244static int
245clone_breakpoints(Process * proc, Process * orig_proc)
246{
247 /* When copying breakpoints, we also have to copy the
248 * referenced symbols, and link them properly. */
249 Dict * map = dict_init(&dict_key2hash_int, &dict_key_cmp_int);
250 struct library_symbol * it = proc->list_of_symbols;
251 proc->list_of_symbols = NULL;
252 for (; it != NULL; it = it->next) {
253 struct library_symbol * libsym = clone_library_symbol(it);
254 if (libsym == NULL) {
255 int save_errno;
256 err:
257 save_errno = errno;
258 destroy_library_symbol_chain(proc->list_of_symbols);
259 dict_clear(map);
260 errno = save_errno;
261 return -1;
262 }
263 libsym->next = proc->list_of_symbols;
264 proc->list_of_symbols = libsym;
265 if (dict_enter(map, it, libsym) != 0)
266 goto err;
267 }
268
269 proc->breakpoints = dict_clone2(orig_proc->breakpoints,
270 address_clone, breakpoint_clone, map);
271 if (proc->breakpoints == NULL)
272 goto err;
273
274 dict_clear(map);
275 return 0;
276}
277
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200278static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200279handle_clone(Event * event) {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200280 Process *p;
281
Juan Cespedes03192f82009-07-03 10:16:22 +0200282 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200283
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200284 p = malloc(sizeof(Process));
285 if (!p) {
286 perror("malloc()");
287 exit(1);
288 }
289 memcpy(p, event->proc, sizeof(Process));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200290 p->pid = event->e_un.newpid;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200291 p->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200292
Petr Machata75dcf7d2011-10-06 14:30:19 +0200293 /* We save register values to the arch pointer, and these need
294 to be per-thread. */
295 p->arch_ptr = NULL;
296
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200297 if (pending_new(p->pid)) {
298 pending_new_remove(p->pid);
Petr Machata4007d742011-07-09 11:29:42 +0200299 if (p->event_handler != NULL)
300 destroy_event_handler(p);
Juan Cespedes5c682042009-05-21 15:59:56 +0200301 if (event->proc->state == STATE_ATTACHED && options.follow) {
302 p->state = STATE_ATTACHED;
303 } else {
304 p->state = STATE_IGNORED;
305 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200306 continue_process(p->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200307 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200308 } else {
309 p->state = STATE_BEING_CREATED;
Petr Machatacebb8842011-07-09 11:14:11 +0200310 add_process(p);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200311 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200312
Petr Machata534e00f2011-09-27 17:58:38 +0200313 if (p->leader == p)
Petr Machataba9911f2011-09-27 21:09:47 +0200314 clone_breakpoints(p, event->proc->leader);
Petr Machata534e00f2011-09-27 17:58:38 +0200315 else
316 /* Thread groups share breakpoints. */
317 p->breakpoints = NULL;
318
Petr Machatacbe29c62011-09-27 02:27:58 +0200319 if (event->type == EVENT_VFORK)
320 continue_after_vfork(p);
321 else
322 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200323}
324
325static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200326handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 Process * proc;
328
Juan Cespedes03192f82009-07-03 10:16:22 +0200329 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200330
331 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200332 if (!proc) {
333 pending_new_insert(event->e_un.newpid);
334 } else {
335 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200336 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200337 proc->state = STATE_ATTACHED;
338 } else {
339 proc->state = STATE_IGNORED;
340 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200341 continue_process(proc->pid);
342 }
343}
344
Juan Cespedesf1350522008-12-16 18:19:58 +0100345static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200346shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 static char *signalent0[] = {
348#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100349 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 static char *signalent1[] = {
351#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100352 };
353 static char **signalents[] = { signalent0, signalent1 };
354 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 sizeof signalent1 / sizeof signalent1[0]
356 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357
Juan Cespedescd8976d2009-05-14 13:47:58 +0200358 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
359
Ian Wienand9a2ad352006-02-20 22:44:45 +0100360 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100362 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100363 return "UNKNOWN_SIGNAL";
364 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100365 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100366 }
367}
368
Juan Cespedesf1350522008-12-16 18:19:58 +0100369static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200370sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100371 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 static char *syscalent0[] = {
373#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100374 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 static char *syscalent1[] = {
376#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100377 };
378 static char **syscalents[] = { syscalent0, syscalent1 };
379 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 sizeof syscalent1 / sizeof syscalent1[0]
381 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100382
Juan Cespedescd8976d2009-05-14 13:47:58 +0200383 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
384
Ian Wienand9a2ad352006-02-20 22:44:45 +0100385 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100387 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100388 sprintf(result, "SYS_%d", sysnum);
389 return result;
390 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 sprintf(result, "SYS_%s",
392 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100393 return result;
394 }
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200398arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100399 static char result[128];
400 static char *arch_syscalent[] = {
401#include "arch_syscallent.h"
402 };
403 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
404
Juan Cespedescd8976d2009-05-14 13:47:58 +0200405 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
406
Juan Cespedes63184be2008-12-10 13:30:12 +0100407 if (sysnum < 0 || sysnum >= nsyscals) {
408 sprintf(result, "ARCH_%d", sysnum);
409 return result;
410 } else {
411 sprintf(result, "ARCH_%s",
412 arch_syscalent[sysnum]);
413 return result;
414 }
415}
416
Juan Cespedesf1350522008-12-16 18:19:58 +0100417static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200418handle_signal(Event *event) {
419 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800420 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200421 output_line(event->proc, "--- %s (%s) ---",
422 shortsignal(event->proc, event->e_un.signum),
423 strsignal(event->e_un.signum));
424 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100425 continue_after_signal(event->proc->pid, event->e_un.signum);
426}
427
Juan Cespedesf1350522008-12-16 18:19:58 +0100428static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200429handle_exit(Event *event) {
430 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200431 if (event->proc->state != STATE_IGNORED) {
432 output_line(event->proc, "+++ exited (status %d) +++",
433 event->e_un.ret_val);
434 }
Petr Machatacebb8842011-07-09 11:14:11 +0200435 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100436}
437
Juan Cespedesf1350522008-12-16 18:19:58 +0100438static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200439handle_exit_signal(Event *event) {
440 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200441 if (event->proc->state != STATE_IGNORED) {
442 output_line(event->proc, "+++ killed by %s +++",
443 shortsignal(event->proc, event->e_un.signum));
444 }
Petr Machatacebb8842011-07-09 11:14:11 +0200445 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100446}
447
Juan Cespedesf1350522008-12-16 18:19:58 +0100448static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200449handle_syscall(Event *event) {
450 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200451 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100452 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200453 if (options.syscalls) {
454 output_left(LT_TOF_SYSCALL, event->proc,
Petr Machata26627682011-07-08 18:15:32 +0200455 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200456 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100457 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100458 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100459}
460
Juan Cespedesf1350522008-12-16 18:19:58 +0100461static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200462handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200463 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200464
Juan Cespedes03192f82009-07-03 10:16:22 +0200465 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200466 if (proc->state == STATE_IGNORED) {
467 untrace_pid(proc->pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200468 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200469 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200471 output_line(proc, "--- Called exec() ---");
472 proc->mask_32bit = 0;
473 proc->personality = 0;
474 proc->arch_ptr = NULL;
475 free(proc->filename);
476 proc->filename = pid2name(proc->pid);
Petr Machatac7585b62011-07-08 22:58:12 +0200477 breakpoints_init(proc, 0);
Juan Cespedese0660df2009-05-21 18:14:39 +0200478 proc->callstack_depth = 0;
479 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200480}
481
482static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200483handle_arch_syscall(Event *event) {
484 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200485 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100486 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200487 if (options.syscalls) {
488 output_left(LT_TOF_SYSCALL, event->proc,
489 arch_sysname(event->proc, event->e_un.sysnum));
490 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100491 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100492 continue_process(event->proc->pid);
493}
494
Juan Cespedesd65efa32003-02-03 00:22:30 +0100495struct timeval current_time_spent;
496
Juan Cespedesf1350522008-12-16 18:19:58 +0100497static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200498calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100499 struct timeval tv;
500 struct timezone tz;
501 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100502 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100503
Juan Cespedescd8976d2009-05-14 13:47:58 +0200504 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100506
507 gettimeofday(&tv, &tz);
508
509 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
510 if (tv.tv_usec >= elem->time_spent.tv_usec) {
511 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
512 } else {
513 diff.tv_sec++;
514 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
515 }
516 current_time_spent = diff;
517}
518
Juan Cespedesf1350522008-12-16 18:19:58 +0100519static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200520handle_sysret(Event *event) {
521 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200522 if (event->proc->state != STATE_IGNORED) {
523 if (opt_T || options.summary) {
524 calc_time_spent(event->proc);
525 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200526 if (options.syscalls) {
527 output_right(LT_TOF_SYSCALLR, event->proc,
528 sysname(event->proc, event->e_un.sysnum));
529 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100530 assert(event->proc->callstack_depth > 0);
531 unsigned d = event->proc->callstack_depth - 1;
532 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100533 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200534 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100535 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100536}
537
Juan Cespedesf1350522008-12-16 18:19:58 +0100538static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200539handle_arch_sysret(Event *event) {
540 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200541 if (event->proc->state != STATE_IGNORED) {
542 if (opt_T || options.summary) {
543 calc_time_spent(event->proc);
544 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200545 if (options.syscalls) {
546 output_right(LT_TOF_SYSCALLR, event->proc,
547 arch_sysname(event->proc, event->e_un.sysnum));
548 }
Petr Machata211f0882010-11-03 18:42:18 +0100549 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100550 }
551 continue_process(event->proc->pid);
552}
553
Juan Cespedesf1350522008-12-16 18:19:58 +0100554static void
Petr Machata14298742012-04-12 23:09:21 +0200555output_right_tos(struct Process *proc)
556{
557 size_t d = proc->callstack_depth;
558 struct callstack_element *elem = &proc->callstack[d - 1];
559 if (proc->state != STATE_IGNORED)
560 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc->name);
561}
562
563static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100564handle_breakpoint(Event *event)
565{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100566 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100567 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200568 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200569 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200570
571 /* The leader has terminated. */
572 if (leader == NULL) {
573 continue_process(event->proc->pid);
574 return;
575 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100576
Petr Machata31b2f9f2012-04-12 22:23:40 +0200577 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
578 event->proc->pid, brk_addr);
579 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100580
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100581 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200582 if (brk_addr == event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200583#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100584 /*
585 * PPC HACK! (XXX FIXME TODO)
586 * The PLT gets modified during the first call,
587 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100589 unsigned long a;
590 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100591 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200592 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200593
Paul Gilliam76c61f12006-06-14 06:55:21 +0200594 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100595 unsigned char break_insn[] = BREAKPOINT_VALUE;
596
Petr Machata9a5420c2011-07-09 11:21:23 +0200597 sbp = address2bpstruct(leader, addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100598 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
600 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100601
Paul Gilliam76c61f12006-06-14 06:55:21 +0200602 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100603 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200605 libsym, 1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100606 }
607 } else {
Petr Machata9a5420c2011-07-09 11:21:23 +0200608 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machata067322d2010-10-25 14:47:55 +0200609 /* On powerpc, the breakpoint address
610 may end up being actual entry point
611 of the library symbol, not the PLT
612 address we computed. In that case,
613 sbp is NULL. */
614 if (sbp == NULL || addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 insert_breakpoint(event->proc, addr,
Petr Machata46d66ab2011-08-20 05:29:25 +0200616 libsym, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200617 }
Ian Wienand3219f322006-02-16 06:00:00 +0100618 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100619#elif defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500620 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200621 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500622 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200623 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100624 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200625 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500626 if (sbp) {
627 if (addr != sbp->addr) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200628 insert_breakpoint(event->proc, addr, sym, 1);
Arnaud Patard161193f2010-01-08 08:40:14 -0500629 }
630 } else {
631 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
632 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200633 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200634 leader->list_of_symbols = new_sym;
Petr Machata46d66ab2011-08-20 05:29:25 +0200635 insert_breakpoint(event->proc, addr, new_sym, 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200636 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200637#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100638 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100639 callstack_pop(event->proc);
640 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200641 if (event->proc->state != STATE_IGNORED) {
642 if (opt_T || options.summary) {
643 calc_time_spent(event->proc);
644 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100645 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200646 event->proc->return_addr = brk_addr;
647
Petr Machata14298742012-04-12 23:09:21 +0200648 output_right_tos(event->proc);
649 callstack_pop(event->proc);
650
Petr Machata31b2f9f2012-04-12 22:23:40 +0200651 /* Pop also any other entries that seem like
652 * they are linked to the current one: they
653 * have the same return address, but were made
654 * for different symbols. This should only
655 * happen for entry point tracing, i.e. for -x
656 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200657 while (event->proc->callstack_depth > 0) {
658 struct callstack_element *prev;
659 size_t d = event->proc->callstack_depth;
660 prev = &event->proc->callstack[d - 1];
661
Petr Machata14298742012-04-12 23:09:21 +0200662 if (prev->c_un.libfunc == libsym
663 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200664 break;
Petr Machata14298742012-04-12 23:09:21 +0200665
666 output_right_tos(event->proc);
667 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200668 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200669
670 sbp = address2bpstruct(leader, brk_addr);
Petr Machata9a5420c2011-07-09 11:21:23 +0200671 continue_after_breakpoint(event->proc, sbp);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100672 return;
673 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100674 }
675
Petr Machata31b2f9f2012-04-12 22:23:40 +0200676 if ((sbp = address2bpstruct(leader, brk_addr))) {
Petr Machataa9fd8f42012-02-07 13:25:56 +0100677 breakpoint_on_hit(sbp, event->proc);
Petr Machatabc373262012-02-07 23:31:15 +0100678
Petr Machata9a5420c2011-07-09 11:21:23 +0200679 if (sbp->libsym == NULL) {
680 continue_after_breakpoint(event->proc, sbp);
681 return;
682 }
683
Joe Damatof0bd98b2010-11-08 15:47:42 -0800684 if (strcmp(sbp->libsym->name, "") == 0) {
Petr Machata26627682011-07-08 18:15:32 +0200685 debug(DEBUG_PROCESS, "Hit _dl_debug_state breakpoint!\n");
Petr Machata9a5420c2011-07-09 11:21:23 +0200686 arch_check_dbg(leader);
Zachary T Welch97baa652010-12-08 13:34:03 -0800687 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200688
Zachary T Welch97baa652010-12-08 13:34:03 -0800689 if (event->proc->state != STATE_IGNORED) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200690 event->proc->stack_pointer = get_stack_pointer(event->proc);
691 event->proc->return_addr =
692 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200693 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata211f0882010-11-03 18:42:18 +0100694 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
Juan Cespedes5c682042009-05-21 15:59:56 +0200695 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100696
697 continue_after_breakpoint(event->proc, sbp);
698 return;
699 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100700
Joe Damatofa2aefc2010-10-30 19:56:50 -0700701 if (event->proc->state != STATE_IGNORED && !options.no_plt) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200702 output_line(event->proc, "unexpected breakpoint at %p",
Petr Machata31b2f9f2012-04-12 22:23:40 +0200703 brk_addr);
Juan Cespedes5c682042009-05-21 15:59:56 +0200704 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100705 continue_process(event->proc->pid);
706}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200707
Juan Cespedesf1350522008-12-16 18:19:58 +0100708static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200709callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100710 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200711
Juan Cespedescd8976d2009-05-14 13:47:58 +0200712 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200713 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100714 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500715 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
716 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200717 return;
718 }
719
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100720 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200721 elem->is_syscall = 1;
722 elem->c_un.syscall = sysnum;
723 elem->return_addr = NULL;
724
725 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200726 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100727 struct timezone tz;
728 gettimeofday(&elem->time_spent, &tz);
729 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200730}
731
Juan Cespedes21c63a12001-07-07 20:56:56 +0200732static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200733callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200734 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200735
Juan Cespedescd8976d2009-05-14 13:47:58 +0200736 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200737 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100738 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500739 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
740 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200741 return;
742 }
743
Petr Machata14298742012-04-12 23:09:21 +0200744 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200745 elem->is_syscall = 0;
746 elem->c_un.libfunc = sym;
747
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200748 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200749 if (elem->return_addr) {
Petr Machatac7585b62011-07-08 22:58:12 +0200750 insert_breakpoint(proc, elem->return_addr, NULL, 1);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200751 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200752
Arnaud Patard26570082010-01-08 08:40:12 -0500753 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200754 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100755 struct timezone tz;
756 gettimeofday(&elem->time_spent, &tz);
757 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200758}
759
Juan Cespedesf1350522008-12-16 18:19:58 +0100760static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200761callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100762 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200763 assert(proc->callstack_depth > 0);
764
Juan Cespedescd8976d2009-05-14 13:47:58 +0200765 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100766 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200767 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200768 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200769 delete_breakpoint(proc, elem->return_addr);
770 }
Petr Machata211f0882010-11-03 18:42:18 +0100771 if (elem->arch_ptr != NULL) {
772 free(elem->arch_ptr);
773 elem->arch_ptr = NULL;
774 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200775 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200776}