blob: 8aa35aaf1d6b0be0b8ce7b101fa37bf874a948dc [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 Machata64262602012-01-07 03:41:36 +010037#include "backend.h"
Petr Machata9294d822012-02-07 12:35:58 +010038#include "breakpoint.h"
Petr Machataba1664b2012-04-28 14:59:05 +020039#include "common.h"
40#include "fetch.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010041#include "library.h"
Petr Machata3d0c91c2012-04-14 02:37:38 +020042#include "proc.h"
Petr Machata64262602012-01-07 03:41:36 +010043#include "value_dict.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010044
Juan Cespedes03192f82009-07-03 10:16:22 +020045static void handle_signal(Event *event);
46static void handle_exit(Event *event);
47static void handle_exit_signal(Event *event);
48static void handle_syscall(Event *event);
49static void handle_arch_syscall(Event *event);
50static void handle_sysret(Event *event);
51static void handle_arch_sysret(Event *event);
52static void handle_clone(Event *event);
53static void handle_exec(Event *event);
54static void handle_breakpoint(Event *event);
55static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010056
Juan Cespedesa8909f72009-04-28 20:02:41 +020057static void callstack_push_syscall(Process *proc, int sysnum);
58static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010059 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020060static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020061
Juan Cespedes61da3372009-07-03 11:55:44 +020062static char * shortsignal(Process *proc, int signum);
63static char * sysname(Process *proc, int sysnum);
64static char * arch_sysname(Process *proc, int sysnum);
65
Petr Machatacbe29c62011-09-27 02:27:58 +020066static Event *
67call_handler(Process * proc, Event * event)
68{
69 assert(proc != NULL);
70
Petr Machata366c2f42012-02-09 19:34:36 +010071 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020072 if (handler == NULL)
73 return event;
74
75 return (*handler->on_event) (handler, event);
76}
77
Juan Cespedes61da3372009-07-03 11:55:44 +020078void
Petr Machataffe4cd22012-04-11 18:01:44 +020079handle_event(Event *event)
80{
Petr Machata602330f2011-07-09 11:15:34 +020081 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020082 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020083 os_ltrace_exiting();
84 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020085 }
Petr Machata26627682011-07-08 18:15:32 +020086 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
87 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020088
89 /* If the thread group or an individual task define an
90 overriding event handler, give them a chance to kick in.
91 We will end up calling both handlers, if the first one
92 doesn't sink the event. */
93 if (event->proc != NULL) {
94 event = call_handler(event->proc, event);
95 if (event == NULL)
96 /* It was handled. */
97 return;
98
99 /* Note: the previous handler has a chance to alter
100 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100101 if (event->proc != NULL
102 && event->proc->leader != NULL
103 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200104 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200105 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200106 return;
107 }
108 }
109
Juan Cespedes61da3372009-07-03 11:55:44 +0200110 switch (event->type) {
111 case EVENT_NONE:
112 debug(1, "event: none");
113 return;
114 case EVENT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200115 debug(1, "[%d] event: signal (%s [%d])",
116 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200117 shortsignal(event->proc, event->e_un.signum),
118 event->e_un.signum);
119 handle_signal(event);
120 return;
121 case EVENT_EXIT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200122 debug(1, "[%d] event: exit (%d)",
123 event->proc->pid,
124 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200125 handle_exit(event);
126 return;
127 case EVENT_EXIT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200128 debug(1, "[%d] event: exit signal (%s [%d])",
129 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200130 shortsignal(event->proc, event->e_un.signum),
131 event->e_un.signum);
132 handle_exit_signal(event);
133 return;
134 case EVENT_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200135 debug(1, "[%d] event: syscall (%s [%d])",
136 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200137 sysname(event->proc, event->e_un.sysnum),
138 event->e_un.sysnum);
139 handle_syscall(event);
140 return;
141 case EVENT_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200142 debug(1, "[%d] event: sysret (%s [%d])",
143 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200144 sysname(event->proc, event->e_un.sysnum),
145 event->e_un.sysnum);
146 handle_sysret(event);
147 return;
148 case EVENT_ARCH_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200149 debug(1, "[%d] event: arch_syscall (%s [%d])",
150 event->proc->pid,
151 arch_sysname(event->proc, event->e_un.sysnum),
152 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200153 handle_arch_syscall(event);
154 return;
155 case EVENT_ARCH_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200156 debug(1, "[%d] event: arch_sysret (%s [%d])",
157 event->proc->pid,
158 arch_sysname(event->proc, event->e_un.sysnum),
159 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200160 handle_arch_sysret(event);
161 return;
162 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200163 case EVENT_VFORK:
Petr Machataebfe7d62012-04-13 21:34:49 +0200164 debug(1, "[%d] event: clone (%u)",
165 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200166 handle_clone(event);
167 return;
168 case EVENT_EXEC:
Petr Machataebfe7d62012-04-13 21:34:49 +0200169 debug(1, "[%d] event: exec()",
170 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200171 handle_exec(event);
172 return;
173 case EVENT_BREAKPOINT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200174 debug(1, "[%d] event: breakpoint %p",
175 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200176 handle_breakpoint(event);
177 return;
178 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200179 debug(1, "[%d] event: new process",
180 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200181 handle_new(event);
182 return;
183 default:
184 fprintf(stderr, "Error! unknown event?\n");
185 exit(1);
186 }
187}
188
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200189typedef struct Pending_New Pending_New;
190struct Pending_New {
191 pid_t pid;
192 Pending_New * next;
193};
194static Pending_New * pending_news = NULL;
195
196static int
197pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200198 Pending_New * p;
199
200 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
201
202 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200203 while (p) {
204 if (p->pid == pid) {
205 return 1;
206 }
207 p = p->next;
208 }
209 return 0;
210}
211
212static void
213pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200214 Pending_New * p;
215
216 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
217
218 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200219 if (!p) {
220 perror("malloc()");
221 exit(1);
222 }
223 p->pid = pid;
224 p->next = pending_news;
225 pending_news = p;
226}
227
228static void
229pending_new_remove(pid_t pid) {
230 Pending_New *p, *pred;
231
Juan Cespedescd8976d2009-05-14 13:47:58 +0200232 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
233
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200234 p = pending_news;
235 if (p->pid == pid) {
236 pending_news = p->next;
237 free(p);
238 } else {
239 while (p) {
240 if (p->pid == pid) {
241 pred->next = p->next;
242 free(p);
243 }
244 pred = p;
245 p = p->next;
246 }
247 }
248}
249
250static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251handle_clone(Event *event)
252{
Juan Cespedes03192f82009-07-03 10:16:22 +0200253 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200254
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 struct Process *proc = malloc(sizeof(*proc));
256 if (proc == NULL) {
257 fail:
258 free(proc);
Petr Machata94078ec2012-01-05 18:07:02 +0100259 fprintf(stderr,
260 "Error during init of tracing process %d\n"
261 "This process won't be traced.\n",
262 event->proc->pid);
263 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200264 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265
266 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
267 goto fail;
268 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200269
Petr Machata75dcf7d2011-10-06 14:30:19 +0200270 /* We save register values to the arch pointer, and these need
271 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100272 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200273
Petr Machata2b46cfc2012-02-18 11:17:29 +0100274 if (pending_new(proc->pid)) {
275 pending_new_remove(proc->pid);
276 /* XXX this used to be destroy_event_handler call, but
277 * I don't think we want to call that on a shared
278 * state. */
279 proc->event_handler = NULL;
280 if (event->proc->state == STATE_ATTACHED && options.follow)
281 proc->state = STATE_ATTACHED;
282 else
283 proc->state = STATE_IGNORED;
284 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200285 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200287 }
Petr Machata534e00f2011-09-27 17:58:38 +0200288
Petr Machatacbe29c62011-09-27 02:27:58 +0200289 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200291 else
292 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200293}
294
295static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200296handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200297 Process * proc;
298
Juan Cespedes03192f82009-07-03 10:16:22 +0200299 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200300
301 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200302 if (!proc) {
303 pending_new_insert(event->e_un.newpid);
304 } else {
305 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200306 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200307 proc->state = STATE_ATTACHED;
308 } else {
309 proc->state = STATE_IGNORED;
310 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200311 continue_process(proc->pid);
312 }
313}
314
Juan Cespedesf1350522008-12-16 18:19:58 +0100315static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200316shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100317 static char *signalent0[] = {
318#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100319 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 static char *signalent1[] = {
321#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100322 };
323 static char **signalents[] = { signalent0, signalent1 };
324 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 sizeof signalent1 / sizeof signalent1[0]
326 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100327
Juan Cespedescd8976d2009-05-14 13:47:58 +0200328 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
329
Ian Wienand9a2ad352006-02-20 22:44:45 +0100330 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100332 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100333 return "UNKNOWN_SIGNAL";
334 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100335 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100336 }
337}
338
Juan Cespedesf1350522008-12-16 18:19:58 +0100339static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200340sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100341 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 static char *syscalent0[] = {
343#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100344 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 static char *syscalent1[] = {
346#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100347 };
348 static char **syscalents[] = { syscalent0, syscalent1 };
349 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 sizeof syscalent1 / sizeof syscalent1[0]
351 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100352
Juan Cespedescd8976d2009-05-14 13:47:58 +0200353 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
354
Ian Wienand9a2ad352006-02-20 22:44:45 +0100355 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100356 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100357 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100358 sprintf(result, "SYS_%d", sysnum);
359 return result;
360 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 sprintf(result, "SYS_%s",
362 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100363 return result;
364 }
365}
366
Juan Cespedesf1350522008-12-16 18:19:58 +0100367static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200368arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100369 static char result[128];
370 static char *arch_syscalent[] = {
371#include "arch_syscallent.h"
372 };
373 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
374
Juan Cespedescd8976d2009-05-14 13:47:58 +0200375 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
376
Juan Cespedes63184be2008-12-10 13:30:12 +0100377 if (sysnum < 0 || sysnum >= nsyscals) {
378 sprintf(result, "ARCH_%d", sysnum);
379 return result;
380 } else {
381 sprintf(result, "ARCH_%s",
382 arch_syscalent[sysnum]);
383 return result;
384 }
385}
386
Juan Cespedesf1350522008-12-16 18:19:58 +0100387static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200388handle_signal(Event *event) {
389 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800390 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200391 output_line(event->proc, "--- %s (%s) ---",
392 shortsignal(event->proc, event->e_un.signum),
393 strsignal(event->e_un.signum));
394 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100395 continue_after_signal(event->proc->pid, event->e_un.signum);
396}
397
Juan Cespedesf1350522008-12-16 18:19:58 +0100398static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200399handle_exit(Event *event) {
400 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200401 if (event->proc->state != STATE_IGNORED) {
402 output_line(event->proc, "+++ exited (status %d) +++",
403 event->e_un.ret_val);
404 }
Petr Machatacebb8842011-07-09 11:14:11 +0200405 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100406}
407
Juan Cespedesf1350522008-12-16 18:19:58 +0100408static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200409handle_exit_signal(Event *event) {
410 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200411 if (event->proc->state != STATE_IGNORED) {
412 output_line(event->proc, "+++ killed by %s +++",
413 shortsignal(event->proc, event->e_un.signum));
414 }
Petr Machatacebb8842011-07-09 11:14:11 +0200415 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100416}
417
Petr Machataa32ef7c2012-04-23 18:05:24 +0200418static void
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200419output_syscall(struct Process *proc, const char *name, enum tof tof,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200420 void (*output)(enum tof, struct Process *,
421 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100422{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200423 struct library_symbol syscall;
424 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200425 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200426 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100427 }
Petr Machata29add4f2012-02-18 16:38:05 +0100428}
429
430static void
431output_syscall_left(struct Process *proc, const char *name)
432{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200433 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100434}
435
436static void
437output_syscall_right(struct Process *proc, const char *name)
438{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200439 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100440}
441
Juan Cespedesf1350522008-12-16 18:19:58 +0100442static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200443handle_syscall(Event *event) {
444 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200445 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100446 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100447 if (options.syscalls)
448 output_syscall_left(event->proc,
449 sysname(event->proc,
450 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100451 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100452 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100453}
454
Juan Cespedesf1350522008-12-16 18:19:58 +0100455static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200456handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200457 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200458
Petr Machata3d0c91c2012-04-14 02:37:38 +0200459 /* Save the PID so that we can use it after unsuccessful
460 * process_exec. */
461 pid_t pid = proc->pid;
462
Juan Cespedes03192f82009-07-03 10:16:22 +0200463 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200464 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200465 untrace:
466 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200467 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200468 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200469 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200470 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200471
472 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200473 fprintf(stderr,
474 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200475 goto untrace;
476 }
477
Juan Cespedese0660df2009-05-21 18:14:39 +0200478 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200479
480 /* After the exec, we expect to hit the first executable
Petr Machata9847abe2012-04-14 00:36:03 +0200481 * instruction.
482 *
483 * XXX TODO It would be nice to have this removed, but then we
484 * need to do that also for initial call to wait_for_proc in
485 * execute_program. In that case we could generate a
486 * EVENT_FIRST event or something, or maybe this could somehow
487 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200488 wait_for_proc(proc->pid);
489 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200490}
491
492static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200493handle_arch_syscall(Event *event) {
494 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200495 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100496 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200497 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100498 output_syscall_left(event->proc,
499 arch_sysname(event->proc,
500 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200501 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100502 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100503 continue_process(event->proc->pid);
504}
505
Juan Cespedesd65efa32003-02-03 00:22:30 +0100506struct timeval current_time_spent;
507
Juan Cespedesf1350522008-12-16 18:19:58 +0100508static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200509calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100510 struct timeval tv;
511 struct timezone tz;
512 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100514
Juan Cespedescd8976d2009-05-14 13:47:58 +0200515 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100516 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100517
518 gettimeofday(&tv, &tz);
519
520 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
521 if (tv.tv_usec >= elem->time_spent.tv_usec) {
522 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
523 } else {
524 diff.tv_sec++;
525 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
526 }
527 current_time_spent = diff;
528}
529
Juan Cespedesf1350522008-12-16 18:19:58 +0100530static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200531handle_sysret(Event *event) {
532 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200533 if (event->proc->state != STATE_IGNORED) {
534 if (opt_T || options.summary) {
535 calc_time_spent(event->proc);
536 }
Petr Machata29add4f2012-02-18 16:38:05 +0100537 if (options.syscalls)
538 output_syscall_right(event->proc,
539 sysname(event->proc,
540 event->e_un.sysnum));
541
Petr Machata43d2fe52011-11-02 13:25:49 +0100542 assert(event->proc->callstack_depth > 0);
543 unsigned d = event->proc->callstack_depth - 1;
544 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100545 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200546 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100547 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100548}
549
Juan Cespedesf1350522008-12-16 18:19:58 +0100550static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200551handle_arch_sysret(Event *event) {
552 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200553 if (event->proc->state != STATE_IGNORED) {
554 if (opt_T || options.summary) {
555 calc_time_spent(event->proc);
556 }
Petr Machata29add4f2012-02-18 16:38:05 +0100557 if (options.syscalls)
558 output_syscall_right(event->proc,
559 arch_sysname(event->proc,
560 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100561 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100562 }
563 continue_process(event->proc->pid);
564}
565
Juan Cespedesf1350522008-12-16 18:19:58 +0100566static void
Petr Machata14298742012-04-12 23:09:21 +0200567output_right_tos(struct Process *proc)
568{
569 size_t d = proc->callstack_depth;
570 struct callstack_element *elem = &proc->callstack[d - 1];
571 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200572 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200573}
574
575static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100576handle_breakpoint(Event *event)
577{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100578 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100579 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200580 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200581 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200582
583 /* The leader has terminated. */
584 if (leader == NULL) {
585 continue_process(event->proc->pid);
586 return;
587 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100588
Petr Machata31b2f9f2012-04-12 22:23:40 +0200589 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
590 event->proc->pid, brk_addr);
591 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100592
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100593 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200594 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machatad09d9ce2012-04-06 13:25:37 +0200595#if defined(__mips__)
Arnaud Patard161193f2010-01-08 08:40:14 -0500596 void *addr = NULL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200597 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Arnaud Patard161193f2010-01-08 08:40:14 -0500598 struct library_symbol *new_sym;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200599 assert(sym);
Petr Machatac185aff2011-11-09 16:18:06 +0100600 addr = sym2addr(event->proc, sym);
Petr Machata9a5420c2011-07-09 11:21:23 +0200601 sbp = dict_find_entry(leader->breakpoints, addr);
Arnaud Patard161193f2010-01-08 08:40:14 -0500602 if (sbp) {
603 if (addr != sbp->addr) {
Petr Machatadb30b102012-03-28 02:44:18 +0200604 insert_breakpoint(event->proc, addr, sym);
Arnaud Patard161193f2010-01-08 08:40:14 -0500605 }
606 } else {
607 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
608 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Petr Machata26627682011-07-08 18:15:32 +0200609 new_sym->next = leader->list_of_symbols;
Petr Machata9a5420c2011-07-09 11:21:23 +0200610 leader->list_of_symbols = new_sym;
Petr Machatadb30b102012-03-28 02:44:18 +0200611 insert_breakpoint(event->proc, addr, new_sym);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200612 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200613#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100614 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100615 callstack_pop(event->proc);
616 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200617 if (event->proc->state != STATE_IGNORED) {
618 if (opt_T || options.summary) {
619 calc_time_spent(event->proc);
620 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100621 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200622 event->proc->return_addr = brk_addr;
623
Petr Machata9e1e9692012-04-19 02:29:38 +0200624 struct library_symbol *libsym =
625 event->proc->callstack[i].c_un.libfunc;
626
Petr Machata14298742012-04-12 23:09:21 +0200627 output_right_tos(event->proc);
628 callstack_pop(event->proc);
629
Petr Machata31b2f9f2012-04-12 22:23:40 +0200630 /* Pop also any other entries that seem like
631 * they are linked to the current one: they
632 * have the same return address, but were made
633 * for different symbols. This should only
634 * happen for entry point tracing, i.e. for -x
635 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200636 while (event->proc->callstack_depth > 0) {
637 struct callstack_element *prev;
638 size_t d = event->proc->callstack_depth;
639 prev = &event->proc->callstack[d - 1];
640
Petr Machata14298742012-04-12 23:09:21 +0200641 if (prev->c_un.libfunc == libsym
642 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200643 break;
Petr Machata14298742012-04-12 23:09:21 +0200644
645 output_right_tos(event->proc);
646 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200647 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200648
Petr Machata5ee36822012-04-19 17:01:51 +0200649 /* Maybe the previous callstack_pop's got rid
650 * of the breakpoint, but if we are in a
651 * recursive call, it's still enabled. In
652 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200653 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200654 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200655 } else {
656 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200657 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200658 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100659 return;
660 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100661 }
662
Petr Machata5ee36822012-04-19 17:01:51 +0200663 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100664 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200665 else if (event->proc->state != STATE_IGNORED)
666 output_line(event->proc,
667 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100668
Petr Machata5ee36822012-04-19 17:01:51 +0200669 /* breakpoint_on_hit may delete its own breakpoint, so we have
670 * to look it up again. */
671 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
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;
Petr Machatac0649d82012-04-20 02:39:33 +0200683 } else {
684 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100685 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100686
Juan Cespedes5e01f651998-03-08 22:31:44 +0100687 continue_process(event->proc->pid);
688}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200689
Juan Cespedesf1350522008-12-16 18:19:58 +0100690static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200691callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100692 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200693
Juan Cespedescd8976d2009-05-14 13:47:58 +0200694 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200695 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100696 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500697 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
698 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200699 return;
700 }
701
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100702 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200703 elem->is_syscall = 1;
704 elem->c_un.syscall = sysnum;
705 elem->return_addr = NULL;
706
707 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200708 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100709 struct timezone tz;
710 gettimeofday(&elem->time_spent, &tz);
711 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200712}
713
Juan Cespedes21c63a12001-07-07 20:56:56 +0200714static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200715callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200716 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200717
Juan Cespedescd8976d2009-05-14 13:47:58 +0200718 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200719 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100720 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500721 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
722 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200723 return;
724 }
725
Petr Machata14298742012-04-12 23:09:21 +0200726 elem = &proc->callstack[proc->callstack_depth++];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200727 elem->is_syscall = 0;
728 elem->c_un.libfunc = sym;
729
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200730 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100731 if (elem->return_addr)
732 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200733
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}