blob: c711b14a8eba8eab7cd821cf9bf886d288c83e14 [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);
Petr Machatae655ccf2012-10-26 22:21:59 +020060/* XXX Stack maintenance should be moved to a dedicated module, or to
61 * proc.c, and push/pop should be visible outside this module. For
62 * now, because we need this in proc.c, this is non-static. */
63void callstack_pop(struct Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064
Juan Cespedes61da3372009-07-03 11:55:44 +020065static char * shortsignal(Process *proc, int signum);
66static char * sysname(Process *proc, int sysnum);
67static char * arch_sysname(Process *proc, int sysnum);
68
Petr Machatacbe29c62011-09-27 02:27:58 +020069static Event *
70call_handler(Process * proc, Event * event)
71{
72 assert(proc != NULL);
73
Petr Machata366c2f42012-02-09 19:34:36 +010074 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020075 if (handler == NULL)
76 return event;
77
78 return (*handler->on_event) (handler, event);
79}
80
Juan Cespedes61da3372009-07-03 11:55:44 +020081void
Petr Machataffe4cd22012-04-11 18:01:44 +020082handle_event(Event *event)
83{
Petr Machata602330f2011-07-09 11:15:34 +020084 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020085 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020086 os_ltrace_exiting();
87 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020088 }
Petr Machata26627682011-07-08 18:15:32 +020089 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
90 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020091
92 /* If the thread group or an individual task define an
93 overriding event handler, give them a chance to kick in.
94 We will end up calling both handlers, if the first one
95 doesn't sink the event. */
96 if (event->proc != NULL) {
97 event = call_handler(event->proc, event);
98 if (event == NULL)
99 /* It was handled. */
100 return;
101
102 /* Note: the previous handler has a chance to alter
103 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100104 if (event->proc != NULL
105 && event->proc->leader != NULL
106 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200107 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200108 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200109 return;
110 }
111 }
112
Juan Cespedes61da3372009-07-03 11:55:44 +0200113 switch (event->type) {
114 case EVENT_NONE:
115 debug(1, "event: none");
116 return;
117 case EVENT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200118 debug(1, "[%d] event: signal (%s [%d])",
119 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200120 shortsignal(event->proc, event->e_un.signum),
121 event->e_un.signum);
122 handle_signal(event);
123 return;
124 case EVENT_EXIT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200125 debug(1, "[%d] event: exit (%d)",
126 event->proc->pid,
127 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200128 handle_exit(event);
129 return;
130 case EVENT_EXIT_SIGNAL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200131 debug(1, "[%d] event: exit signal (%s [%d])",
132 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200133 shortsignal(event->proc, event->e_un.signum),
134 event->e_un.signum);
135 handle_exit_signal(event);
136 return;
137 case EVENT_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200138 debug(1, "[%d] event: syscall (%s [%d])",
139 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200140 sysname(event->proc, event->e_un.sysnum),
141 event->e_un.sysnum);
142 handle_syscall(event);
143 return;
144 case EVENT_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200145 debug(1, "[%d] event: sysret (%s [%d])",
146 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200147 sysname(event->proc, event->e_un.sysnum),
148 event->e_un.sysnum);
149 handle_sysret(event);
150 return;
151 case EVENT_ARCH_SYSCALL:
Petr Machataebfe7d62012-04-13 21:34:49 +0200152 debug(1, "[%d] event: arch_syscall (%s [%d])",
153 event->proc->pid,
154 arch_sysname(event->proc, event->e_un.sysnum),
155 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200156 handle_arch_syscall(event);
157 return;
158 case EVENT_ARCH_SYSRET:
Petr Machataebfe7d62012-04-13 21:34:49 +0200159 debug(1, "[%d] event: arch_sysret (%s [%d])",
160 event->proc->pid,
161 arch_sysname(event->proc, event->e_un.sysnum),
162 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200163 handle_arch_sysret(event);
164 return;
165 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200166 case EVENT_VFORK:
Petr Machataebfe7d62012-04-13 21:34:49 +0200167 debug(1, "[%d] event: clone (%u)",
168 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200169 handle_clone(event);
170 return;
171 case EVENT_EXEC:
Petr Machataebfe7d62012-04-13 21:34:49 +0200172 debug(1, "[%d] event: exec()",
173 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200174 handle_exec(event);
175 return;
176 case EVENT_BREAKPOINT:
Petr Machataebfe7d62012-04-13 21:34:49 +0200177 debug(1, "[%d] event: breakpoint %p",
178 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200179 handle_breakpoint(event);
180 return;
181 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200182 debug(1, "[%d] event: new process",
183 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200184 handle_new(event);
185 return;
186 default:
187 fprintf(stderr, "Error! unknown event?\n");
188 exit(1);
189 }
190}
191
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200192typedef struct Pending_New Pending_New;
193struct Pending_New {
194 pid_t pid;
195 Pending_New * next;
196};
197static Pending_New * pending_news = NULL;
198
199static int
200pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200201 Pending_New * p;
202
203 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
204
205 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200206 while (p) {
207 if (p->pid == pid) {
208 return 1;
209 }
210 p = p->next;
211 }
212 return 0;
213}
214
215static void
216pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200217 Pending_New * p;
218
219 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
220
221 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200222 if (!p) {
223 perror("malloc()");
224 exit(1);
225 }
226 p->pid = pid;
227 p->next = pending_news;
228 pending_news = p;
229}
230
231static void
232pending_new_remove(pid_t pid) {
233 Pending_New *p, *pred;
234
Juan Cespedescd8976d2009-05-14 13:47:58 +0200235 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
236
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200237 p = pending_news;
Sedat Dilek7d804e92012-09-01 02:30:52 +0200238 pred = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200239 if (p->pid == pid) {
240 pending_news = p->next;
241 free(p);
242 } else {
243 while (p) {
244 if (p->pid == pid) {
245 pred->next = p->next;
246 free(p);
247 }
248 pred = p;
249 p = p->next;
250 }
251 }
252}
253
254static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255handle_clone(Event *event)
256{
Juan Cespedes03192f82009-07-03 10:16:22 +0200257 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200258
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259 struct Process *proc = malloc(sizeof(*proc));
260 if (proc == NULL) {
261 fail:
262 free(proc);
Petr Machata94078ec2012-01-05 18:07:02 +0100263 fprintf(stderr,
264 "Error during init of tracing process %d\n"
265 "This process won't be traced.\n",
266 event->proc->pid);
267 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200268 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269
270 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
271 goto fail;
272 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200273
Petr Machata75dcf7d2011-10-06 14:30:19 +0200274 /* We save register values to the arch pointer, and these need
275 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200277
Petr Machata2b46cfc2012-02-18 11:17:29 +0100278 if (pending_new(proc->pid)) {
279 pending_new_remove(proc->pid);
280 /* XXX this used to be destroy_event_handler call, but
281 * I don't think we want to call that on a shared
282 * state. */
283 proc->event_handler = NULL;
284 if (event->proc->state == STATE_ATTACHED && options.follow)
285 proc->state = STATE_ATTACHED;
286 else
287 proc->state = STATE_IGNORED;
288 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200289 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200291 }
Petr Machata534e00f2011-09-27 17:58:38 +0200292
Petr Machatacbe29c62011-09-27 02:27:58 +0200293 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200295 else
296 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200297}
298
299static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200300handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200301 Process * proc;
302
Juan Cespedes03192f82009-07-03 10:16:22 +0200303 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200304
305 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200306 if (!proc) {
307 pending_new_insert(event->e_un.newpid);
308 } else {
309 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200310 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200311 proc->state = STATE_ATTACHED;
312 } else {
313 proc->state = STATE_IGNORED;
314 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200315 continue_process(proc->pid);
316 }
317}
318
Juan Cespedesf1350522008-12-16 18:19:58 +0100319static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200320shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100321 static char *signalent0[] = {
322#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100323 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 static char *signalent1[] = {
325#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100326 };
327 static char **signalents[] = { signalent0, signalent1 };
328 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100329 sizeof signalent1 / sizeof signalent1[0]
330 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100331
Juan Cespedescd8976d2009-05-14 13:47:58 +0200332 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
333
Ian Wienand9a2ad352006-02-20 22:44:45 +0100334 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100336 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337 return "UNKNOWN_SIGNAL";
338 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100339 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100340 }
341}
342
Juan Cespedesf1350522008-12-16 18:19:58 +0100343static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200344sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 static char *syscalent0[] = {
347#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 static char *syscalent1[] = {
350#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100351 };
352 static char **syscalents[] = { syscalent0, syscalent1 };
353 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 sizeof syscalent1 / sizeof syscalent1[0]
355 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100356
Juan Cespedescd8976d2009-05-14 13:47:58 +0200357 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
358
Ian Wienand9a2ad352006-02-20 22:44:45 +0100359 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100361 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 sprintf(result, "SYS_%d", sysnum);
363 return result;
364 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 sprintf(result, "SYS_%s",
366 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100367 return result;
368 }
369}
370
Juan Cespedesf1350522008-12-16 18:19:58 +0100371static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200372arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100373 static char result[128];
374 static char *arch_syscalent[] = {
375#include "arch_syscallent.h"
376 };
377 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
378
Juan Cespedescd8976d2009-05-14 13:47:58 +0200379 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
380
Juan Cespedes63184be2008-12-10 13:30:12 +0100381 if (sysnum < 0 || sysnum >= nsyscals) {
382 sprintf(result, "ARCH_%d", sysnum);
383 return result;
384 } else {
385 sprintf(result, "ARCH_%s",
386 arch_syscalent[sysnum]);
387 return result;
388 }
389}
390
Petr Machata7c4d3112012-12-09 11:55:03 +0100391#ifndef HAVE_STRSIGNAL
392# define strsignal(SIGNUM) "???"
393#endif
394
Juan Cespedesf1350522008-12-16 18:19:58 +0100395static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200396handle_signal(Event *event) {
397 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800398 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200399 output_line(event->proc, "--- %s (%s) ---",
400 shortsignal(event->proc, event->e_un.signum),
401 strsignal(event->e_un.signum));
402 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100403 continue_after_signal(event->proc->pid, event->e_un.signum);
404}
405
Juan Cespedesf1350522008-12-16 18:19:58 +0100406static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200407handle_exit(Event *event) {
408 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200409 if (event->proc->state != STATE_IGNORED) {
410 output_line(event->proc, "+++ exited (status %d) +++",
411 event->e_un.ret_val);
412 }
Petr Machatacebb8842011-07-09 11:14:11 +0200413 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100414}
415
Juan Cespedesf1350522008-12-16 18:19:58 +0100416static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200417handle_exit_signal(Event *event) {
418 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200419 if (event->proc->state != STATE_IGNORED) {
420 output_line(event->proc, "+++ killed by %s +++",
421 shortsignal(event->proc, event->e_un.signum));
422 }
Petr Machatacebb8842011-07-09 11:14:11 +0200423 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100424}
425
Petr Machataa32ef7c2012-04-23 18:05:24 +0200426static void
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200427output_syscall(struct Process *proc, const char *name, enum tof tof,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200428 void (*output)(enum tof, struct Process *,
429 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100430{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200431 struct library_symbol syscall;
432 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200433 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200434 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100435 }
Petr Machata29add4f2012-02-18 16:38:05 +0100436}
437
438static void
439output_syscall_left(struct Process *proc, const char *name)
440{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200441 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100442}
443
444static void
445output_syscall_right(struct Process *proc, const char *name)
446{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200447 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100448}
449
Juan Cespedesf1350522008-12-16 18:19:58 +0100450static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200451handle_syscall(Event *event) {
452 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200453 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100454 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100455 if (options.syscalls)
456 output_syscall_left(event->proc,
457 sysname(event->proc,
458 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100459 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100460 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100461}
462
Juan Cespedesf1350522008-12-16 18:19:58 +0100463static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200464handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200465 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200466
Petr Machata3d0c91c2012-04-14 02:37:38 +0200467 /* Save the PID so that we can use it after unsuccessful
468 * process_exec. */
469 pid_t pid = proc->pid;
470
Juan Cespedes03192f82009-07-03 10:16:22 +0200471 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200472 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200473 untrace:
474 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200475 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200476 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200477 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200478 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200479
480 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200481 fprintf(stderr,
482 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200483 goto untrace;
484 }
485
Juan Cespedese0660df2009-05-21 18:14:39 +0200486 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200487
488 /* After the exec, we expect to hit the first executable
Petr Machata9847abe2012-04-14 00:36:03 +0200489 * instruction.
490 *
491 * XXX TODO It would be nice to have this removed, but then we
492 * need to do that also for initial call to wait_for_proc in
493 * execute_program. In that case we could generate a
494 * EVENT_FIRST event or something, or maybe this could somehow
495 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200496 wait_for_proc(proc->pid);
497 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200498}
499
500static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200501handle_arch_syscall(Event *event) {
502 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200503 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100504 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200505 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100506 output_syscall_left(event->proc,
507 arch_sysname(event->proc,
508 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200509 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100510 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100511 continue_process(event->proc->pid);
512}
513
Juan Cespedesd65efa32003-02-03 00:22:30 +0100514struct timeval current_time_spent;
515
Juan Cespedesf1350522008-12-16 18:19:58 +0100516static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200517calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100518 struct timeval tv;
519 struct timezone tz;
520 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100521 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100522
Juan Cespedescd8976d2009-05-14 13:47:58 +0200523 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100525
526 gettimeofday(&tv, &tz);
527
528 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
529 if (tv.tv_usec >= elem->time_spent.tv_usec) {
530 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
531 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400532 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100533 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
534 }
535 current_time_spent = diff;
536}
537
Juan Cespedesf1350522008-12-16 18:19:58 +0100538static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200539handle_sysret(Event *event) {
540 debug(DEBUG_FUNCTION, "handle_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 }
Petr Machata29add4f2012-02-18 16:38:05 +0100545 if (options.syscalls)
546 output_syscall_right(event->proc,
547 sysname(event->proc,
548 event->e_un.sysnum));
549
Petr Machata43d2fe52011-11-02 13:25:49 +0100550 assert(event->proc->callstack_depth > 0);
551 unsigned d = event->proc->callstack_depth - 1;
552 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100553 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200554 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100555 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100556}
557
Juan Cespedesf1350522008-12-16 18:19:58 +0100558static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200559handle_arch_sysret(Event *event) {
560 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200561 if (event->proc->state != STATE_IGNORED) {
562 if (opt_T || options.summary) {
563 calc_time_spent(event->proc);
564 }
Petr Machata29add4f2012-02-18 16:38:05 +0100565 if (options.syscalls)
566 output_syscall_right(event->proc,
567 arch_sysname(event->proc,
568 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100569 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100570 }
571 continue_process(event->proc->pid);
572}
573
Juan Cespedesf1350522008-12-16 18:19:58 +0100574static void
Petr Machata14298742012-04-12 23:09:21 +0200575output_right_tos(struct Process *proc)
576{
577 size_t d = proc->callstack_depth;
578 struct callstack_element *elem = &proc->callstack[d - 1];
579 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200580 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200581}
582
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200583#ifndef ARCH_HAVE_SYMBOL_RET
584void arch_symbol_ret(struct Process *proc, struct library_symbol *libsym)
585{
586}
587#endif
588
Petr Machata14298742012-04-12 23:09:21 +0200589static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100590handle_breakpoint(Event *event)
591{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100593 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200594 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200595 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200596
597 /* The leader has terminated. */
598 if (leader == NULL) {
599 continue_process(event->proc->pid);
600 return;
601 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100602
Petr Machata31b2f9f2012-04-12 22:23:40 +0200603 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
604 event->proc->pid, brk_addr);
605 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100606
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100607 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200608 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100609 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100610 callstack_pop(event->proc);
611 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200612 if (event->proc->state != STATE_IGNORED) {
613 if (opt_T || options.summary) {
614 calc_time_spent(event->proc);
615 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100616 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200617 event->proc->return_addr = brk_addr;
618
Petr Machata9e1e9692012-04-19 02:29:38 +0200619 struct library_symbol *libsym =
620 event->proc->callstack[i].c_un.libfunc;
621
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200622 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200623 output_right_tos(event->proc);
624 callstack_pop(event->proc);
625
Petr Machata31b2f9f2012-04-12 22:23:40 +0200626 /* Pop also any other entries that seem like
627 * they are linked to the current one: they
628 * have the same return address, but were made
629 * for different symbols. This should only
630 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200631 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200632 while (event->proc->callstack_depth > 0) {
633 struct callstack_element *prev;
634 size_t d = event->proc->callstack_depth;
635 prev = &event->proc->callstack[d - 1];
636
Petr Machata14298742012-04-12 23:09:21 +0200637 if (prev->c_un.libfunc == libsym
638 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200639 break;
Petr Machata14298742012-04-12 23:09:21 +0200640
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200641 arch_symbol_ret(event->proc,
642 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200643 output_right_tos(event->proc);
644 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200645 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200646
Petr Machata5ee36822012-04-19 17:01:51 +0200647 /* Maybe the previous callstack_pop's got rid
648 * of the breakpoint, but if we are in a
649 * recursive call, it's still enabled. In
650 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200651 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200652 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200653 } else {
654 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200655 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200656 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100657 return;
658 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100659 }
660
Petr Machata5ee36822012-04-19 17:01:51 +0200661 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100662 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200663 else if (event->proc->state != STATE_IGNORED)
664 output_line(event->proc,
665 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100666
Petr Machata5ee36822012-04-19 17:01:51 +0200667 /* breakpoint_on_hit may delete its own breakpoint, so we have
668 * to look it up again. */
669 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100670 if (event->proc->state != STATE_IGNORED
671 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200672 event->proc->stack_pointer = get_stack_pointer(event->proc);
673 event->proc->return_addr =
674 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200675 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100676 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200677 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100678
Petr Machata56a9ea62012-03-27 03:09:29 +0200679 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100680 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200681 } else {
682 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100683 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100684
Juan Cespedes5e01f651998-03-08 22:31:44 +0100685 continue_process(event->proc->pid);
686}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200687
Juan Cespedesf1350522008-12-16 18:19:58 +0100688static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200689callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100690 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200691
Juan Cespedescd8976d2009-05-14 13:47:58 +0200692 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200693 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100694 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500695 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
696 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200697 return;
698 }
699
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100700 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200701 *elem = (struct callstack_element){};
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++];
Petr Machata8ae36732012-04-30 01:19:09 +0200726 *elem = (struct callstack_element){};
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
Petr Machatae655ccf2012-10-26 22:21:59 +0200740void
741callstack_pop(struct Process *proc)
742{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100743 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200744 assert(proc->callstack_depth > 0);
745
Juan Cespedescd8976d2009-05-14 13:47:58 +0200746 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100747 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatae655ccf2012-10-26 22:21:59 +0200748 if (!elem->is_syscall && elem->return_addr)
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200749 delete_breakpoint(proc, elem->return_addr);
Petr Machatae655ccf2012-10-26 22:21:59 +0200750
751 if (elem->fetch_context != NULL)
752 fetch_arg_done(elem->fetch_context);
753
754 if (elem->arguments != NULL) {
755 val_dict_destroy(elem->arguments);
756 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200757 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200758
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200759 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200760}