blob: ffce2ff9bab5dcf64568ac75dec5503fce004b0e [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Arnaud Patard, Mandriva SA
5 * Copyright (C) 1998,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2008 Luis Machado, IBM Corporation
7 * Copyright (C) 2006 Ian Wienand
8 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
Juan Cespedesd44c6b81998-09-25 14:48:42 +020026#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020027
Petr Machata94078ec2012-01-05 18:07:02 +010028#define _GNU_SOURCE
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020029#include <assert.h>
Petr Machata534e00f2011-09-27 17:58:38 +020030#include <errno.h>
Petr Machata3d0c91c2012-04-14 02:37:38 +020031#include <signal.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/time.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010036
Petr Machata94078ec2012-01-05 18:07:02 +010037#include "common.h"
38#include "value_dict.h"
Petr Machata9294d822012-02-07 12:35:58 +010039#include "breakpoint.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010040#include "library.h"
Petr Machata3d0c91c2012-04-14 02:37:38 +020041#include "proc.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010042#include "fetch.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010043
Juan Cespedes03192f82009-07-03 10:16:22 +020044static void handle_signal(Event *event);
45static void handle_exit(Event *event);
46static void handle_exit_signal(Event *event);
47static void handle_syscall(Event *event);
48static void handle_arch_syscall(Event *event);
49static void handle_sysret(Event *event);
50static void handle_arch_sysret(Event *event);
51static void handle_clone(Event *event);
52static void handle_exec(Event *event);
53static void handle_breakpoint(Event *event);
54static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010055
Juan Cespedesa8909f72009-04-28 20:02:41 +020056static void callstack_push_syscall(Process *proc, int sysnum);
57static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020059static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020060
Juan Cespedes61da3372009-07-03 11:55:44 +020061static char * shortsignal(Process *proc, int signum);
62static char * sysname(Process *proc, int sysnum);
63static char * arch_sysname(Process *proc, int sysnum);
64
Petr Machatacbe29c62011-09-27 02:27:58 +020065static Event *
66call_handler(Process * proc, Event * event)
67{
68 assert(proc != NULL);
69
Petr Machata366c2f42012-02-09 19:34:36 +010070 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020071 if (handler == NULL)
72 return event;
73
74 return (*handler->on_event) (handler, event);
75}
76
Juan Cespedes61da3372009-07-03 11:55:44 +020077void
Petr Machataffe4cd22012-04-11 18:01:44 +020078handle_event(Event *event)
79{
Petr Machata602330f2011-07-09 11:15:34 +020080 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020081 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020082 os_ltrace_exiting();
83 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020084 }
Petr Machata26627682011-07-08 18:15:32 +020085 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
86 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020087
88 /* If the thread group or an individual task define an
89 overriding event handler, give them a chance to kick in.
90 We will end up calling both handlers, if the first one
91 doesn't sink the event. */
92 if (event->proc != NULL) {
93 event = call_handler(event->proc, event);
94 if (event == NULL)
95 /* It was handled. */
96 return;
97
98 /* Note: the previous handler has a chance to alter
99 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100100 if (event->proc != NULL
101 && event->proc->leader != NULL
102 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200103 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200104 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200105 return;
106 }
107 }
108
Juan Cespedes61da3372009-07-03 11:55:44 +0200109 switch (event->type) {
110 case EVENT_NONE:
111 debug(1, "event: none");
112 return;
113 case EVENT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200114 debug(1, "[%d] event: signal (%s [%d])",
115 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200116 shortsignal(event->proc, event->e_un.signum),
117 event->e_un.signum);
118 handle_signal(event);
119 return;
120 case EVENT_EXIT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200121 debug(1, "[%d] event: exit (%d)",
122 event->proc->pid,
123 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200124 handle_exit(event);
125 return;
126 case EVENT_EXIT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200127 debug(1, "[%d] event: exit signal (%s [%d])",
128 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200129 shortsignal(event->proc, event->e_un.signum),
130 event->e_un.signum);
131 handle_exit_signal(event);
132 return;
133 case EVENT_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200134 debug(1, "[%d] event: syscall (%s [%d])",
135 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200136 sysname(event->proc, event->e_un.sysnum),
137 event->e_un.sysnum);
138 handle_syscall(event);
139 return;
140 case EVENT_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200141 debug(1, "[%d] event: sysret (%s [%d])",
142 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200143 sysname(event->proc, event->e_un.sysnum),
144 event->e_un.sysnum);
145 handle_sysret(event);
146 return;
147 case EVENT_ARCH_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200148 debug(1, "[%d] event: arch_syscall (%s [%d])",
149 event->proc->pid,
150 arch_sysname(event->proc, event->e_un.sysnum),
151 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200152 handle_arch_syscall(event);
153 return;
154 case EVENT_ARCH_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200155 debug(1, "[%d] event: arch_sysret (%s [%d])",
156 event->proc->pid,
157 arch_sysname(event->proc, event->e_un.sysnum),
158 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200159 handle_arch_sysret(event);
160 return;
161 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200162 case EVENT_VFORK:
Petr Machataebfe7d62012-04-13 21:34:49 +0200163 debug(1, "[%d] event: clone (%u)",
164 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200165 handle_clone(event);
166 return;
167 case EVENT_EXEC:
Petr Machataebfe7d62012-04-13 21:34:49 +0200168 debug(1, "[%d] event: exec()",
169 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200170 handle_exec(event);
171 return;
172 case EVENT_BREAKPOINT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200173 debug(1, "[%d] event: breakpoint %p",
174 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200175 handle_breakpoint(event);
176 return;
177 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200178 debug(1, "[%d] event: new process",
179 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200180 handle_new(event);
181 return;
182 default:
183 fprintf(stderr, "Error! unknown event?\n");
184 exit(1);
185 }
186}
187
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200188typedef struct Pending_New Pending_New;
189struct Pending_New {
190 pid_t pid;
191 Pending_New * next;
192};
193static Pending_New * pending_news = NULL;
194
195static int
196pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200197 Pending_New * p;
198
199 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
200
201 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200202 while (p) {
203 if (p->pid == pid) {
204 return 1;
205 }
206 p = p->next;
207 }
208 return 0;
209}
210
211static void
212pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200213 Pending_New * p;
214
215 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
216
217 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200218 if (!p) {
219 perror("malloc()");
220 exit(1);
221 }
222 p->pid = pid;
223 p->next = pending_news;
224 pending_news = p;
225}
226
227static void
228pending_new_remove(pid_t pid) {
229 Pending_New *p, *pred;
230
Juan Cespedescd8976d2009-05-14 13:47:58 +0200231 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
232
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200233 p = pending_news;
234 if (p->pid == pid) {
235 pending_news = p->next;
236 free(p);
237 } else {
238 while (p) {
239 if (p->pid == pid) {
240 pred->next = p->next;
241 free(p);
242 }
243 pred = p;
244 p = p->next;
245 }
246 }
247}
248
249static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100250handle_clone(Event *event)
251{
Juan Cespedes03192f82009-07-03 10:16:22 +0200252 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200253
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254 struct Process *proc = malloc(sizeof(*proc));
255 if (proc == NULL) {
256 fail:
257 free(proc);
Petr Machata94078ec2012-01-05 18:07:02 +0100258 fprintf(stderr,
259 "Error during init of tracing process %d\n"
260 "This process won't be traced.\n",
261 event->proc->pid);
262 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200263 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100264
265 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
266 goto fail;
267 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200268
Petr Machata75dcf7d2011-10-06 14:30:19 +0200269 /* We save register values to the arch pointer, and these need
270 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100271 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200272
Petr Machata2b46cfc2012-02-18 11:17:29 +0100273 if (pending_new(proc->pid)) {
274 pending_new_remove(proc->pid);
275 /* XXX this used to be destroy_event_handler call, but
276 * I don't think we want to call that on a shared
277 * state. */
278 proc->event_handler = NULL;
279 if (event->proc->state == STATE_ATTACHED && options.follow)
280 proc->state = STATE_ATTACHED;
281 else
282 proc->state = STATE_IGNORED;
283 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200284 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100285 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200286 }
Petr Machata534e00f2011-09-27 17:58:38 +0200287
Petr Machatacbe29c62011-09-27 02:27:58 +0200288 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200290 else
291 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200292}
293
294static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200295handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200296 Process * proc;
297
Juan Cespedes03192f82009-07-03 10:16:22 +0200298 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200299
300 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200301 if (!proc) {
302 pending_new_insert(event->e_un.newpid);
303 } else {
304 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200305 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200306 proc->state = STATE_ATTACHED;
307 } else {
308 proc->state = STATE_IGNORED;
309 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200310 continue_process(proc->pid);
311 }
312}
313
Juan Cespedesf1350522008-12-16 18:19:58 +0100314static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200315shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 static char *signalent0[] = {
317#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100318 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 static char *signalent1[] = {
320#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100321 };
322 static char **signalents[] = { signalent0, signalent1 };
323 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 sizeof signalent1 / sizeof signalent1[0]
325 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100326
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
328
Ian Wienand9a2ad352006-02-20 22:44:45 +0100329 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100331 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332 return "UNKNOWN_SIGNAL";
333 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100334 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100335 }
336}
337
Juan Cespedesf1350522008-12-16 18:19:58 +0100338static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200339sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100340 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 static char *syscalent0[] = {
342#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100343 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 static char *syscalent1[] = {
345#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100346 };
347 static char **syscalents[] = { syscalent0, syscalent1 };
348 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 sizeof syscalent1 / sizeof syscalent1[0]
350 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100351
Juan Cespedescd8976d2009-05-14 13:47:58 +0200352 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
353
Ian Wienand9a2ad352006-02-20 22:44:45 +0100354 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357 sprintf(result, "SYS_%d", sysnum);
358 return result;
359 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 sprintf(result, "SYS_%s",
361 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 return result;
363 }
364}
365
Juan Cespedesf1350522008-12-16 18:19:58 +0100366static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200367arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100368 static char result[128];
369 static char *arch_syscalent[] = {
370#include "arch_syscallent.h"
371 };
372 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
373
Juan Cespedescd8976d2009-05-14 13:47:58 +0200374 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
375
Juan Cespedes63184be2008-12-10 13:30:12 +0100376 if (sysnum < 0 || sysnum >= nsyscals) {
377 sprintf(result, "ARCH_%d", sysnum);
378 return result;
379 } else {
380 sprintf(result, "ARCH_%s",
381 arch_syscalent[sysnum]);
382 return result;
383 }
384}
385
Juan Cespedesf1350522008-12-16 18:19:58 +0100386static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200387handle_signal(Event *event) {
388 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800389 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200390 output_line(event->proc, "--- %s (%s) ---",
391 shortsignal(event->proc, event->e_un.signum),
392 strsignal(event->e_un.signum));
393 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100394 continue_after_signal(event->proc->pid, event->e_un.signum);
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200398handle_exit(Event *event) {
399 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200400 if (event->proc->state != STATE_IGNORED) {
401 output_line(event->proc, "+++ exited (status %d) +++",
402 event->e_un.ret_val);
403 }
Petr Machatacebb8842011-07-09 11:14:11 +0200404 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100405}
406
Juan Cespedesf1350522008-12-16 18:19:58 +0100407static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200408handle_exit_signal(Event *event) {
409 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200410 if (event->proc->state != STATE_IGNORED) {
411 output_line(event->proc, "+++ killed by %s +++",
412 shortsignal(event->proc, event->e_un.signum));
413 }
Petr Machatacebb8842011-07-09 11:14:11 +0200414 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100415}
416
Petr Machataa32ef7c2012-04-23 18:05:24 +0200417static void
418output_syscall(struct Process *proc, const char *name,
419 void (*output)(enum tof, struct Process *,
420 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100421{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200422 struct library_symbol syscall;
423 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
424 (*output)(LT_TOF_SYSCALL, proc, &syscall);
425 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100426 }
Petr Machata29add4f2012-02-18 16:38:05 +0100427}
428
429static void
430output_syscall_left(struct Process *proc, const char *name)
431{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200432 output_syscall(proc, name, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100433}
434
435static void
436output_syscall_right(struct Process *proc, const char *name)
437{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200438 output_syscall(proc, name, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100439}
440
Juan Cespedesf1350522008-12-16 18:19:58 +0100441static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200442handle_syscall(Event *event) {
443 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200444 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100445 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100446 if (options.syscalls)
447 output_syscall_left(event->proc,
448 sysname(event->proc,
449 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100450 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100451 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100452}
453
Juan Cespedesf1350522008-12-16 18:19:58 +0100454static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200455handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200456 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200457
Petr Machata3d0c91c2012-04-14 02:37:38 +0200458 /* Save the PID so that we can use it after unsuccessful
459 * process_exec. */
460 pid_t pid = proc->pid;
461
Juan Cespedes03192f82009-07-03 10:16:22 +0200462 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200463 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200464 untrace:
465 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200466 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200467 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200468 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200469 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200470
471 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200472 fprintf(stderr,
473 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200474 goto untrace;
475 }
476
Juan Cespedese0660df2009-05-21 18:14:39 +0200477 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200478
479 /* After the exec, we expect to hit the first executable
Petr Machata9847abe2012-04-14 00:36:03 +0200480 * instruction.
481 *
482 * XXX TODO It would be nice to have this removed, but then we
483 * need to do that also for initial call to wait_for_proc in
484 * execute_program. In that case we could generate a
485 * EVENT_FIRST event or something, or maybe this could somehow
486 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200487 wait_for_proc(proc->pid);
488 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200489}
490
491static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200492handle_arch_syscall(Event *event) {
493 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200494 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100495 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200496 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100497 output_syscall_left(event->proc,
498 arch_sysname(event->proc,
499 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200500 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100501 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100502 continue_process(event->proc->pid);
503}
504
Juan Cespedesd65efa32003-02-03 00:22:30 +0100505struct timeval current_time_spent;
506
Juan Cespedesf1350522008-12-16 18:19:58 +0100507static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200508calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100509 struct timeval tv;
510 struct timezone tz;
511 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100513
Juan Cespedescd8976d2009-05-14 13:47:58 +0200514 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100515 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100516
517 gettimeofday(&tv, &tz);
518
519 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
520 if (tv.tv_usec >= elem->time_spent.tv_usec) {
521 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
522 } else {
523 diff.tv_sec++;
524 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
525 }
526 current_time_spent = diff;
527}
528
Juan Cespedesf1350522008-12-16 18:19:58 +0100529static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200530handle_sysret(Event *event) {
531 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200532 if (event->proc->state != STATE_IGNORED) {
533 if (opt_T || options.summary) {
534 calc_time_spent(event->proc);
535 }
Petr Machata29add4f2012-02-18 16:38:05 +0100536 if (options.syscalls)
537 output_syscall_right(event->proc,
538 sysname(event->proc,
539 event->e_un.sysnum));
540
Petr Machata43d2fe52011-11-02 13:25:49 +0100541 assert(event->proc->callstack_depth > 0);
542 unsigned d = event->proc->callstack_depth - 1;
543 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100544 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200545 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100546 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100547}
548
Juan Cespedesf1350522008-12-16 18:19:58 +0100549static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200550handle_arch_sysret(Event *event) {
551 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200552 if (event->proc->state != STATE_IGNORED) {
553 if (opt_T || options.summary) {
554 calc_time_spent(event->proc);
555 }
Petr Machata29add4f2012-02-18 16:38:05 +0100556 if (options.syscalls)
557 output_syscall_right(event->proc,
558 arch_sysname(event->proc,
559 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100560 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100561 }
562 continue_process(event->proc->pid);
563}
564
Juan Cespedesf1350522008-12-16 18:19:58 +0100565static void
Petr Machata14298742012-04-12 23:09:21 +0200566output_right_tos(struct Process *proc)
567{
568 size_t d = proc->callstack_depth;
569 struct callstack_element *elem = &proc->callstack[d - 1];
570 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200571 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200572}
573
574static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100575handle_breakpoint(Event *event)
576{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100577 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100578 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200579 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200580 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200581
582 /* The leader has terminated. */
583 if (leader == NULL) {
584 continue_process(event->proc->pid);
585 return;
586 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100587
Petr Machata31b2f9f2012-04-12 22:23:40 +0200588 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
589 event->proc->pid, brk_addr);
590 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100591
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200593 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200594#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500595 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200596 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500597 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200598 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100599 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200600 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500601 if (sbp) {
602 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200603 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500604 }
605 } else {
606 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
607 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200608 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200609 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200610 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200611 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200612#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100613 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100614 callstack_pop(event->proc);
615 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200616 if (event->proc->state != STATE_IGNORED) {
617 if (opt_T || options.summary) {
618 calc_time_spent(event->proc);
619 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100620 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200621 event->proc->return_addr = brk_addr;
622
Petr Machata9e1e9692012-04-19 02:29:38 +0200623 struct library_symbol *libsym =
624 event->proc->callstack[i].c_un.libfunc;
625
Petr Machata14298742012-04-12 23:09:21 +0200626 output_right_tos(event->proc);
627 callstack_pop(event->proc);
628
Petr Machata31b2f9f2012-04-12 22:23:40 +0200629 /* Pop also any other entries that seem like
630 * they are linked to the current one: they
631 * have the same return address, but were made
632 * for different symbols. This should only
633 * happen for entry point tracing, i.e. for -x
634 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200635 while (event->proc->callstack_depth > 0) {
636 struct callstack_element *prev;
637 size_t d = event->proc->callstack_depth;
638 prev = &event->proc->callstack[d - 1];
639
Petr Machata14298742012-04-12 23:09:21 +0200640 if (prev->c_un.libfunc == libsym
641 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200642 break;
Petr Machata14298742012-04-12 23:09:21 +0200643
644 output_right_tos(event->proc);
645 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200646 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200647
Petr Machata5ee36822012-04-19 17:01:51 +0200648 /* Maybe the previous callstack_pop's got rid
649 * of the breakpoint, but if we are in a
650 * recursive call, it's still enabled. In
651 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200652 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200653 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200654 } else {
655 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200656 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200657 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100658 return;
659 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100660 }
661
Petr Machata5ee36822012-04-19 17:01:51 +0200662 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100663 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200664 else if (event->proc->state != STATE_IGNORED)
665 output_line(event->proc,
666 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100667
Petr Machata5ee36822012-04-19 17:01:51 +0200668 /* breakpoint_on_hit may delete its own breakpoint, so we have
669 * to look it up again. */
670 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100671 if (event->proc->state != STATE_IGNORED
672 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200673 event->proc->stack_pointer = get_stack_pointer(event->proc);
674 event->proc->return_addr =
675 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200676 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100677 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200678 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100679
Petr Machata56a9ea62012-03-27 03:09:29 +0200680 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100681 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200682 } else {
683 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100684 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100685
Juan Cespedes5e01f651998-03-08 22:31:44 +0100686 continue_process(event->proc->pid);
687}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200688
Juan Cespedesf1350522008-12-16 18:19:58 +0100689static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200690callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100691 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200692
Juan Cespedescd8976d2009-05-14 13:47:58 +0200693 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200694 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100695 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500696 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
697 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200698 return;
699 }
700
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100701 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200702 elem->is_syscall = 1;
703 elem->c_un.syscall = sysnum;
704 elem->return_addr = NULL;
705
706 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200707 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100708 struct timezone tz;
709 gettimeofday(&elem->time_spent, &tz);
710 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200711}
712
Juan Cespedes21c63a12001-07-07 20:56:56 +0200713static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200714callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200715 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200716
Juan Cespedescd8976d2009-05-14 13:47:58 +0200717 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200718 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100719 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500720 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
721 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200722 return;
723 }
724
Petr Machata14298742012-04-12 23:09:21 +0200725 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200726 elem->is_syscall = 0;
727 elem->c_un.libfunc = sym;
728
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200729 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100730 if (elem->return_addr)
731 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200732
Arnaud Patard26570082010-01-08 08:40:12 -0500733 /* handle functions like atexit() on mips which have no return */
Juan Cespedesda9b9532009-04-07 15:33:50 +0200734 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100735 struct timezone tz;
736 gettimeofday(&elem->time_spent, &tz);
737 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200738}
739
Juan Cespedesf1350522008-12-16 18:19:58 +0100740static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200741callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100742 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200743 assert(proc->callstack_depth > 0);
744
Juan Cespedescd8976d2009-05-14 13:47:58 +0200745 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100746 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200747 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200748 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200749 delete_breakpoint(proc, elem->return_addr);
750 }
751 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200752}