blob: 384e8682c3272b11c0b2145bd0cabca9d91ea94b [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;
Sedat Dilek7d804e92012-09-01 02:30:52 +0200235 pred = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200236 if (p->pid == pid) {
237 pending_news = p->next;
238 free(p);
239 } else {
240 while (p) {
241 if (p->pid == pid) {
242 pred->next = p->next;
243 free(p);
244 }
245 pred = p;
246 p = p->next;
247 }
248 }
249}
250
251static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252handle_clone(Event *event)
253{
Juan Cespedes03192f82009-07-03 10:16:22 +0200254 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200255
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 struct Process *proc = malloc(sizeof(*proc));
257 if (proc == NULL) {
258 fail:
259 free(proc);
Petr Machata94078ec2012-01-05 18:07:02 +0100260 fprintf(stderr,
261 "Error during init of tracing process %d\n"
262 "This process won't be traced.\n",
263 event->proc->pid);
264 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200265 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100266
267 if (process_clone(proc, event->proc, event->e_un.newpid) < 0)
268 goto fail;
269 proc->parent = event->proc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200270
Petr Machata75dcf7d2011-10-06 14:30:19 +0200271 /* We save register values to the arch pointer, and these need
272 to be per-thread. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100273 proc->arch_ptr = NULL;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200274
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275 if (pending_new(proc->pid)) {
276 pending_new_remove(proc->pid);
277 /* XXX this used to be destroy_event_handler call, but
278 * I don't think we want to call that on a shared
279 * state. */
280 proc->event_handler = NULL;
281 if (event->proc->state == STATE_ATTACHED && options.follow)
282 proc->state = STATE_ATTACHED;
283 else
284 proc->state = STATE_IGNORED;
285 continue_process(proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200286 } else {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200288 }
Petr Machata534e00f2011-09-27 17:58:38 +0200289
Petr Machatacbe29c62011-09-27 02:27:58 +0200290 if (event->type == EVENT_VFORK)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200292 else
293 continue_process(event->proc->pid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200294}
295
296static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200297handle_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200298 Process * proc;
299
Juan Cespedes03192f82009-07-03 10:16:22 +0200300 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200301
302 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200303 if (!proc) {
304 pending_new_insert(event->e_un.newpid);
305 } else {
306 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200307 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200308 proc->state = STATE_ATTACHED;
309 } else {
310 proc->state = STATE_IGNORED;
311 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200312 continue_process(proc->pid);
313 }
314}
315
Juan Cespedesf1350522008-12-16 18:19:58 +0100316static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200317shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100318 static char *signalent0[] = {
319#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100320 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100321 static char *signalent1[] = {
322#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100323 };
324 static char **signalents[] = { signalent0, signalent1 };
325 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 sizeof signalent1 / sizeof signalent1[0]
327 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100328
Juan Cespedescd8976d2009-05-14 13:47:58 +0200329 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
330
Ian Wienand9a2ad352006-02-20 22:44:45 +0100331 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100332 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100333 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100334 return "UNKNOWN_SIGNAL";
335 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100336 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337 }
338}
339
Juan Cespedesf1350522008-12-16 18:19:58 +0100340static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200341sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100342 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 static char *syscalent0[] = {
344#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 static char *syscalent1[] = {
347#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100348 };
349 static char **syscalents[] = { syscalent0, syscalent1 };
350 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 sizeof syscalent1 / sizeof syscalent1[0]
352 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100353
Juan Cespedescd8976d2009-05-14 13:47:58 +0200354 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
355
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100358 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100359 sprintf(result, "SYS_%d", sysnum);
360 return result;
361 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 sprintf(result, "SYS_%s",
363 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100364 return result;
365 }
366}
367
Juan Cespedesf1350522008-12-16 18:19:58 +0100368static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200369arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100370 static char result[128];
371 static char *arch_syscalent[] = {
372#include "arch_syscallent.h"
373 };
374 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
375
Juan Cespedescd8976d2009-05-14 13:47:58 +0200376 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
377
Juan Cespedes63184be2008-12-10 13:30:12 +0100378 if (sysnum < 0 || sysnum >= nsyscals) {
379 sprintf(result, "ARCH_%d", sysnum);
380 return result;
381 } else {
382 sprintf(result, "ARCH_%s",
383 arch_syscalent[sysnum]);
384 return result;
385 }
386}
387
Juan Cespedesf1350522008-12-16 18:19:58 +0100388static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200389handle_signal(Event *event) {
390 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800391 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200392 output_line(event->proc, "--- %s (%s) ---",
393 shortsignal(event->proc, event->e_un.signum),
394 strsignal(event->e_un.signum));
395 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100396 continue_after_signal(event->proc->pid, event->e_un.signum);
397}
398
Juan Cespedesf1350522008-12-16 18:19:58 +0100399static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200400handle_exit(Event *event) {
401 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200402 if (event->proc->state != STATE_IGNORED) {
403 output_line(event->proc, "+++ exited (status %d) +++",
404 event->e_un.ret_val);
405 }
Petr Machatacebb8842011-07-09 11:14:11 +0200406 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100407}
408
Juan Cespedesf1350522008-12-16 18:19:58 +0100409static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200410handle_exit_signal(Event *event) {
411 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200412 if (event->proc->state != STATE_IGNORED) {
413 output_line(event->proc, "+++ killed by %s +++",
414 shortsignal(event->proc, event->e_un.signum));
415 }
Petr Machatacebb8842011-07-09 11:14:11 +0200416 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100417}
418
Petr Machataa32ef7c2012-04-23 18:05:24 +0200419static void
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200420output_syscall(struct Process *proc, const char *name, enum tof tof,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200421 void (*output)(enum tof, struct Process *,
422 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100423{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200424 struct library_symbol syscall;
425 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200426 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200427 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100428 }
Petr Machata29add4f2012-02-18 16:38:05 +0100429}
430
431static void
432output_syscall_left(struct Process *proc, const char *name)
433{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200434 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100435}
436
437static void
438output_syscall_right(struct Process *proc, const char *name)
439{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200440 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100441}
442
Juan Cespedesf1350522008-12-16 18:19:58 +0100443static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200444handle_syscall(Event *event) {
445 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200446 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100447 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100448 if (options.syscalls)
449 output_syscall_left(event->proc,
450 sysname(event->proc,
451 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100452 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100453 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100454}
455
Juan Cespedesf1350522008-12-16 18:19:58 +0100456static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200457handle_exec(Event * event) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200458 Process * proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200459
Petr Machata3d0c91c2012-04-14 02:37:38 +0200460 /* Save the PID so that we can use it after unsuccessful
461 * process_exec. */
462 pid_t pid = proc->pid;
463
Juan Cespedes03192f82009-07-03 10:16:22 +0200464 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200465 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200466 untrace:
467 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200468 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200469 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200471 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200472
473 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200474 fprintf(stderr,
475 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200476 goto untrace;
477 }
478
Juan Cespedese0660df2009-05-21 18:14:39 +0200479 continue_process(proc->pid);
Petr Machatacb39a402012-04-13 22:19:02 +0200480
481 /* After the exec, we expect to hit the first executable
Petr Machata9847abe2012-04-14 00:36:03 +0200482 * instruction.
483 *
484 * XXX TODO It would be nice to have this removed, but then we
485 * need to do that also for initial call to wait_for_proc in
486 * execute_program. In that case we could generate a
487 * EVENT_FIRST event or something, or maybe this could somehow
488 * be rolled into EVENT_NEW. */
Petr Machatacb39a402012-04-13 22:19:02 +0200489 wait_for_proc(proc->pid);
490 continue_process(proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200491}
492
493static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200494handle_arch_syscall(Event *event) {
495 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200496 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100497 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200498 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100499 output_syscall_left(event->proc,
500 arch_sysname(event->proc,
501 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200502 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100503 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100504 continue_process(event->proc->pid);
505}
506
Juan Cespedesd65efa32003-02-03 00:22:30 +0100507struct timeval current_time_spent;
508
Juan Cespedesf1350522008-12-16 18:19:58 +0100509static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200510calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100511 struct timeval tv;
512 struct timezone tz;
513 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100515
Juan Cespedescd8976d2009-05-14 13:47:58 +0200516 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100517 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100518
519 gettimeofday(&tv, &tz);
520
521 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
522 if (tv.tv_usec >= elem->time_spent.tv_usec) {
523 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
524 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400525 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100526 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
527 }
528 current_time_spent = diff;
529}
530
Juan Cespedesf1350522008-12-16 18:19:58 +0100531static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200532handle_sysret(Event *event) {
533 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200534 if (event->proc->state != STATE_IGNORED) {
535 if (opt_T || options.summary) {
536 calc_time_spent(event->proc);
537 }
Petr Machata29add4f2012-02-18 16:38:05 +0100538 if (options.syscalls)
539 output_syscall_right(event->proc,
540 sysname(event->proc,
541 event->e_un.sysnum));
542
Petr Machata43d2fe52011-11-02 13:25:49 +0100543 assert(event->proc->callstack_depth > 0);
544 unsigned d = event->proc->callstack_depth - 1;
545 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100546 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200547 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100548 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100549}
550
Juan Cespedesf1350522008-12-16 18:19:58 +0100551static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200552handle_arch_sysret(Event *event) {
553 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200554 if (event->proc->state != STATE_IGNORED) {
555 if (opt_T || options.summary) {
556 calc_time_spent(event->proc);
557 }
Petr Machata29add4f2012-02-18 16:38:05 +0100558 if (options.syscalls)
559 output_syscall_right(event->proc,
560 arch_sysname(event->proc,
561 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100562 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100563 }
564 continue_process(event->proc->pid);
565}
566
Juan Cespedesf1350522008-12-16 18:19:58 +0100567static void
Petr Machata14298742012-04-12 23:09:21 +0200568output_right_tos(struct Process *proc)
569{
570 size_t d = proc->callstack_depth;
571 struct callstack_element *elem = &proc->callstack[d - 1];
572 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200573 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200574}
575
576static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100577handle_breakpoint(Event *event)
578{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100579 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100580 struct breakpoint *sbp;
Petr Machata9a5420c2011-07-09 11:21:23 +0200581 Process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200582 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200583
584 /* The leader has terminated. */
585 if (leader == NULL) {
586 continue_process(event->proc->pid);
587 return;
588 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100589
Petr Machata31b2f9f2012-04-12 22:23:40 +0200590 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
591 event->proc->pid, brk_addr);
592 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100593
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100594 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200595 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100596 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100597 callstack_pop(event->proc);
598 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200599 if (event->proc->state != STATE_IGNORED) {
600 if (opt_T || options.summary) {
601 calc_time_spent(event->proc);
602 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100603 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200604 event->proc->return_addr = brk_addr;
605
Petr Machata9e1e9692012-04-19 02:29:38 +0200606 struct library_symbol *libsym =
607 event->proc->callstack[i].c_un.libfunc;
608
Petr Machata14298742012-04-12 23:09:21 +0200609 output_right_tos(event->proc);
610 callstack_pop(event->proc);
611
Petr Machata31b2f9f2012-04-12 22:23:40 +0200612 /* Pop also any other entries that seem like
613 * they are linked to the current one: they
614 * have the same return address, but were made
615 * for different symbols. This should only
616 * happen for entry point tracing, i.e. for -x
617 * everywhere, or -x and -e on PPC64. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200618 while (event->proc->callstack_depth > 0) {
619 struct callstack_element *prev;
620 size_t d = event->proc->callstack_depth;
621 prev = &event->proc->callstack[d - 1];
622
Petr Machata14298742012-04-12 23:09:21 +0200623 if (prev->c_un.libfunc == libsym
624 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200625 break;
Petr Machata14298742012-04-12 23:09:21 +0200626
627 output_right_tos(event->proc);
628 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200629 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200630
Petr Machata5ee36822012-04-19 17:01:51 +0200631 /* Maybe the previous callstack_pop's got rid
632 * of the breakpoint, but if we are in a
633 * recursive call, it's still enabled. In
634 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200635 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200636 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200637 } else {
638 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200639 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200640 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100641 return;
642 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100643 }
644
Petr Machata5ee36822012-04-19 17:01:51 +0200645 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100646 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200647 else if (event->proc->state != STATE_IGNORED)
648 output_line(event->proc,
649 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100650
Petr Machata5ee36822012-04-19 17:01:51 +0200651 /* breakpoint_on_hit may delete its own breakpoint, so we have
652 * to look it up again. */
653 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100654 if (event->proc->state != STATE_IGNORED
655 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200656 event->proc->stack_pointer = get_stack_pointer(event->proc);
657 event->proc->return_addr =
658 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200659 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100660 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200661 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662
Petr Machata56a9ea62012-03-27 03:09:29 +0200663 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100664 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200665 } else {
666 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100667 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100668
Juan Cespedes5e01f651998-03-08 22:31:44 +0100669 continue_process(event->proc->pid);
670}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200671
Juan Cespedesf1350522008-12-16 18:19:58 +0100672static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200673callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100674 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200675
Juan Cespedescd8976d2009-05-14 13:47:58 +0200676 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200677 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100678 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500679 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
680 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200681 return;
682 }
683
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100684 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200685 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200686 elem->is_syscall = 1;
687 elem->c_un.syscall = sysnum;
688 elem->return_addr = NULL;
689
690 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200691 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100692 struct timezone tz;
693 gettimeofday(&elem->time_spent, &tz);
694 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200695}
696
Juan Cespedes21c63a12001-07-07 20:56:56 +0200697static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200698callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Petr Machata14298742012-04-12 23:09:21 +0200699 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200700
Juan Cespedescd8976d2009-05-14 13:47:58 +0200701 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200702 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100703 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500704 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
705 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200706 return;
707 }
708
Petr Machata14298742012-04-12 23:09:21 +0200709 elem = &proc->callstack[proc->callstack_depth++];
Petr Machata8ae36732012-04-30 01:19:09 +0200710 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200711 elem->is_syscall = 0;
712 elem->c_un.libfunc = sym;
713
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200714 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100715 if (elem->return_addr)
716 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200717
Juan Cespedesda9b9532009-04-07 15:33:50 +0200718 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100719 struct timezone tz;
720 gettimeofday(&elem->time_spent, &tz);
721 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200722}
723
Juan Cespedesf1350522008-12-16 18:19:58 +0100724static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200725callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100726 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200727 assert(proc->callstack_depth > 0);
728
Juan Cespedescd8976d2009-05-14 13:47:58 +0200729 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100730 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200731 if (!elem->is_syscall && elem->return_addr) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200732 assert(proc->leader != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200733 delete_breakpoint(proc, elem->return_addr);
734 }
735 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200736}