blob: e7e9f1e23e4e9890071336a1d55fb58b697d0909 [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
Petr Machata057caa52013-01-30 23:28:47 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata94078ec2012-01-05 18:07:02 +01004 * 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>
Petr Machata82f748d2013-10-23 00:39:23 +020036#include <stdbool.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010037
Petr Machata64262602012-01-07 03:41:36 +010038#include "backend.h"
Petr Machata9294d822012-02-07 12:35:58 +010039#include "breakpoint.h"
Petr Machataba1664b2012-04-28 14:59:05 +020040#include "common.h"
41#include "fetch.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010042#include "library.h"
Petr Machata3d0c91c2012-04-14 02:37:38 +020043#include "proc.h"
Petr Machata64262602012-01-07 03:41:36 +010044#include "value_dict.h"
Petr Machata82f748d2013-10-23 00:39:23 +020045#include "prototype.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010046
Juan Cespedes03192f82009-07-03 10:16:22 +020047static void handle_signal(Event *event);
48static void handle_exit(Event *event);
49static void handle_exit_signal(Event *event);
50static void handle_syscall(Event *event);
51static void handle_arch_syscall(Event *event);
52static void handle_sysret(Event *event);
53static void handle_arch_sysret(Event *event);
54static void handle_clone(Event *event);
55static void handle_exec(Event *event);
56static void handle_breakpoint(Event *event);
57static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010058
Petr Machata929bd572012-12-17 03:20:34 +010059static void callstack_push_syscall(struct process *proc, int sysnum);
Petr Machatacf989232013-10-11 21:17:24 +020060static void callstack_push_symfunc(struct process *proc, struct breakpoint *bp);
Petr Machatae655ccf2012-10-26 22:21:59 +020061/* XXX Stack maintenance should be moved to a dedicated module, or to
62 * proc.c, and push/pop should be visible outside this module. For
63 * now, because we need this in proc.c, this is non-static. */
Petr Machata929bd572012-12-17 03:20:34 +010064void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065
Petr Machata929bd572012-12-17 03:20:34 +010066static char *shortsignal(struct process *proc, int signum);
67static char *sysname(struct process *proc, int sysnum);
68static char *arch_sysname(struct process *proc, int sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +020069
Petr Machatacbe29c62011-09-27 02:27:58 +020070static Event *
Petr Machata929bd572012-12-17 03:20:34 +010071call_handler(struct process *proc, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +020072{
73 assert(proc != NULL);
74
Petr Machata366c2f42012-02-09 19:34:36 +010075 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020076 if (handler == NULL)
77 return event;
78
79 return (*handler->on_event) (handler, event);
80}
81
Juan Cespedes61da3372009-07-03 11:55:44 +020082void
Petr Machataffe4cd22012-04-11 18:01:44 +020083handle_event(Event *event)
84{
Petr Machata602330f2011-07-09 11:15:34 +020085 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020086 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020087 os_ltrace_exiting();
88 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020089 }
Petr Machata26627682011-07-08 18:15:32 +020090 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
91 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020092
93 /* If the thread group or an individual task define an
94 overriding event handler, give them a chance to kick in.
95 We will end up calling both handlers, if the first one
96 doesn't sink the event. */
97 if (event->proc != NULL) {
98 event = call_handler(event->proc, event);
99 if (event == NULL)
100 /* It was handled. */
101 return;
102
103 /* Note: the previous handler has a chance to alter
104 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100105 if (event->proc != NULL
106 && event->proc->leader != NULL
107 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200108 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200109 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200110 return;
111 }
112 }
113
Juan Cespedes61da3372009-07-03 11:55:44 +0200114 switch (event->type) {
115 case EVENT_NONE:
116 debug(1, "event: none");
117 return;
Petr Machata9978de42013-01-08 23:46:22 +0100118
Juan Cespedes61da3372009-07-03 11:55:44 +0200119 case EVENT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100120 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200121 debug(1, "[%d] event: signal (%s [%d])",
122 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200123 shortsignal(event->proc, event->e_un.signum),
124 event->e_un.signum);
125 handle_signal(event);
126 return;
Petr Machata9978de42013-01-08 23:46:22 +0100127
Juan Cespedes61da3372009-07-03 11:55:44 +0200128 case EVENT_EXIT:
Petr Machata9978de42013-01-08 23:46:22 +0100129 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200130 debug(1, "[%d] event: exit (%d)",
131 event->proc->pid,
132 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200133 handle_exit(event);
134 return;
Petr Machata9978de42013-01-08 23:46:22 +0100135
Juan Cespedes61da3372009-07-03 11:55:44 +0200136 case EVENT_EXIT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100137 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200138 debug(1, "[%d] event: exit signal (%s [%d])",
139 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200140 shortsignal(event->proc, event->e_un.signum),
141 event->e_un.signum);
142 handle_exit_signal(event);
143 return;
Petr Machata9978de42013-01-08 23:46:22 +0100144
Juan Cespedes61da3372009-07-03 11:55:44 +0200145 case EVENT_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100146 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200147 debug(1, "[%d] event: syscall (%s [%d])",
148 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200149 sysname(event->proc, event->e_un.sysnum),
150 event->e_un.sysnum);
151 handle_syscall(event);
152 return;
Petr Machata9978de42013-01-08 23:46:22 +0100153
Juan Cespedes61da3372009-07-03 11:55:44 +0200154 case EVENT_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100155 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200156 debug(1, "[%d] event: sysret (%s [%d])",
157 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200158 sysname(event->proc, event->e_un.sysnum),
159 event->e_un.sysnum);
160 handle_sysret(event);
161 return;
Petr Machata9978de42013-01-08 23:46:22 +0100162
Juan Cespedes61da3372009-07-03 11:55:44 +0200163 case EVENT_ARCH_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100164 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200165 debug(1, "[%d] event: arch_syscall (%s [%d])",
166 event->proc->pid,
167 arch_sysname(event->proc, event->e_un.sysnum),
168 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200169 handle_arch_syscall(event);
170 return;
Petr Machata9978de42013-01-08 23:46:22 +0100171
Juan Cespedes61da3372009-07-03 11:55:44 +0200172 case EVENT_ARCH_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100173 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200174 debug(1, "[%d] event: arch_sysret (%s [%d])",
175 event->proc->pid,
176 arch_sysname(event->proc, event->e_un.sysnum),
177 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200178 handle_arch_sysret(event);
179 return;
Petr Machata9978de42013-01-08 23:46:22 +0100180
Juan Cespedes61da3372009-07-03 11:55:44 +0200181 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200182 case EVENT_VFORK:
Petr Machata9978de42013-01-08 23:46:22 +0100183 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200184 debug(1, "[%d] event: clone (%u)",
185 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200186 handle_clone(event);
187 return;
Petr Machata9978de42013-01-08 23:46:22 +0100188
Juan Cespedes61da3372009-07-03 11:55:44 +0200189 case EVENT_EXEC:
Petr Machata9978de42013-01-08 23:46:22 +0100190 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200191 debug(1, "[%d] event: exec()",
192 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200193 handle_exec(event);
194 return;
Petr Machata9978de42013-01-08 23:46:22 +0100195
Juan Cespedes61da3372009-07-03 11:55:44 +0200196 case EVENT_BREAKPOINT:
Petr Machata9978de42013-01-08 23:46:22 +0100197 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200198 debug(1, "[%d] event: breakpoint %p",
199 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200200 handle_breakpoint(event);
201 return;
Petr Machata9978de42013-01-08 23:46:22 +0100202
Juan Cespedes61da3372009-07-03 11:55:44 +0200203 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200204 debug(1, "[%d] event: new process",
205 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200206 handle_new(event);
207 return;
208 default:
209 fprintf(stderr, "Error! unknown event?\n");
210 exit(1);
211 }
212}
213
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200214typedef struct Pending_New Pending_New;
215struct Pending_New {
216 pid_t pid;
217 Pending_New * next;
218};
219static Pending_New * pending_news = NULL;
220
221static int
222pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200223 Pending_New * p;
224
225 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
226
227 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200228 while (p) {
229 if (p->pid == pid) {
230 return 1;
231 }
232 p = p->next;
233 }
234 return 0;
235}
236
237static void
238pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200239 Pending_New * p;
240
241 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
242
243 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200244 if (!p) {
245 perror("malloc()");
246 exit(1);
247 }
248 p->pid = pid;
249 p->next = pending_news;
250 pending_news = p;
251}
252
253static void
Petr Machatac1f1bf42013-01-09 22:39:41 +0100254pending_new_remove(pid_t pid)
255{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200256 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
257
Petr Machatac1f1bf42013-01-09 22:39:41 +0100258 Pending_New **pp;
259 for (pp = &pending_news; *pp != NULL; pp = &(*pp)->next)
260 if ((*pp)->pid == pid) {
261 Pending_New *p = *pp;
262 *pp = p->next;
263 free(p);
264 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200265 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200266}
267
268static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269handle_clone(Event *event)
270{
Juan Cespedes03192f82009-07-03 10:16:22 +0200271 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200272
Petr Machata929bd572012-12-17 03:20:34 +0100273 struct process *proc = malloc(sizeof(*proc));
Petr Machata6095c132013-01-07 19:08:18 +0100274 pid_t newpid = event->e_un.newpid;
275 if (proc == NULL
276 || process_clone(proc, event->proc, newpid) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 free(proc);
Petr Machata6095c132013-01-07 19:08:18 +0100278 proc = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100279 fprintf(stderr,
Petr Machata6095c132013-01-07 19:08:18 +0100280 "Couldn't initialize tracing of process %d.\n",
281 newpid);
282
283 } else {
284 proc->parent = event->proc;
285 /* We save register values to the arch pointer, and
286 * these need to be per-thread. XXX arch_ptr should
287 * be retired in favor of fetch interface anyway. */
288 proc->arch_ptr = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200289 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290
Petr Machata6095c132013-01-07 19:08:18 +0100291 if (pending_new(newpid)) {
292 pending_new_remove(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200293
Petr Machata6095c132013-01-07 19:08:18 +0100294 if (proc != NULL) {
295 proc->event_handler = NULL;
296 if (event->proc->state == STATE_ATTACHED
297 && options.follow)
298 proc->state = STATE_ATTACHED;
299 else
300 proc->state = STATE_IGNORED;
301 }
Petr Machata75dcf7d2011-10-06 14:30:19 +0200302
Petr Machata6095c132013-01-07 19:08:18 +0100303 continue_process(newpid);
304
305 } else if (proc != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100306 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200307 }
Petr Machata534e00f2011-09-27 17:58:38 +0200308
Petr Machata6095c132013-01-07 19:08:18 +0100309 if (event->type != EVENT_VFORK)
310 continue_process(event->proc->pid);
311 else if (proc != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200313 else
Petr Machata6095c132013-01-07 19:08:18 +0100314 continue_process(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200315}
316
317static void
Petr Machata929bd572012-12-17 03:20:34 +0100318handle_new(Event *event)
319{
Juan Cespedes03192f82009-07-03 10:16:22 +0200320 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200321
Petr Machata929bd572012-12-17 03:20:34 +0100322 struct process *proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200323 if (!proc) {
324 pending_new_insert(event->e_un.newpid);
325 } else {
326 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200327 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200328 proc->state = STATE_ATTACHED;
329 } else {
330 proc->state = STATE_IGNORED;
331 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200332 continue_process(proc->pid);
333 }
334}
335
Juan Cespedesf1350522008-12-16 18:19:58 +0100336static char *
Petr Machata929bd572012-12-17 03:20:34 +0100337shortsignal(struct process *proc, int signum)
338{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 static char *signalent0[] = {
340#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100341 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 static char *signalent1[] = {
343#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100344 };
345 static char **signalents[] = { signalent0, signalent1 };
346 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 sizeof signalent1 / sizeof signalent1[0]
348 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100349
Juan Cespedescd8976d2009-05-14 13:47:58 +0200350 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
351
Petr Machata8e682c22013-01-09 16:20:20 +0100352 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100353 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100354 return "UNKNOWN_SIGNAL";
355 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357 }
358}
359
Juan Cespedesf1350522008-12-16 18:19:58 +0100360static char *
Petr Machata929bd572012-12-17 03:20:34 +0100361sysname(struct process *proc, int sysnum)
362{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100363 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100364 static char *syscallent0[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100366 };
Petr Machata8e682c22013-01-09 16:20:20 +0100367 static char *syscallent1[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100369 };
Petr Machata8e682c22013-01-09 16:20:20 +0100370 static char **syscallents[] = { syscallent0, syscallent1 };
371 int nsyscalls[] = {
372 sizeof syscallent0 / sizeof syscallent0[0],
373 sizeof syscallent1 / sizeof syscallent1[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100375
Juan Cespedescd8976d2009-05-14 13:47:58 +0200376 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
377
Petr Machata8e682c22013-01-09 16:20:20 +0100378 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
379 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100380 sprintf(result, "SYS_%d", sysnum);
381 return result;
382 } else {
Petr Machata82f748d2013-10-23 00:39:23 +0200383 return syscallents[proc->personality][sysnum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100384 }
385}
386
Juan Cespedesf1350522008-12-16 18:19:58 +0100387static char *
Petr Machata929bd572012-12-17 03:20:34 +0100388arch_sysname(struct process *proc, int sysnum)
389{
Juan Cespedes63184be2008-12-10 13:30:12 +0100390 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100391 static char *arch_syscallent[] = {
Juan Cespedes63184be2008-12-10 13:30:12 +0100392#include "arch_syscallent.h"
393 };
Petr Machata8e682c22013-01-09 16:20:20 +0100394 int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0];
Juan Cespedes63184be2008-12-10 13:30:12 +0100395
Juan Cespedescd8976d2009-05-14 13:47:58 +0200396 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
397
Petr Machata8e682c22013-01-09 16:20:20 +0100398 if (sysnum < 0 || sysnum >= nsyscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100399 sprintf(result, "ARCH_%d", sysnum);
400 return result;
401 } else {
Petr Machata8e682c22013-01-09 16:20:20 +0100402 sprintf(result, "ARCH_%s", arch_syscallent[sysnum]);
Juan Cespedes63184be2008-12-10 13:30:12 +0100403 return result;
404 }
405}
406
Petr Machata7c4d3112012-12-09 11:55:03 +0100407#ifndef HAVE_STRSIGNAL
408# define strsignal(SIGNUM) "???"
409#endif
410
Juan Cespedesf1350522008-12-16 18:19:58 +0100411static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200412handle_signal(Event *event) {
413 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800414 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200415 output_line(event->proc, "--- %s (%s) ---",
416 shortsignal(event->proc, event->e_un.signum),
417 strsignal(event->e_un.signum));
418 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100419 continue_after_signal(event->proc->pid, event->e_un.signum);
420}
421
Juan Cespedesf1350522008-12-16 18:19:58 +0100422static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200423handle_exit(Event *event) {
424 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200425 if (event->proc->state != STATE_IGNORED) {
426 output_line(event->proc, "+++ exited (status %d) +++",
427 event->e_un.ret_val);
428 }
Petr Machatacebb8842011-07-09 11:14:11 +0200429 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100430}
431
Juan Cespedesf1350522008-12-16 18:19:58 +0100432static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200433handle_exit_signal(Event *event) {
434 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200435 if (event->proc->state != STATE_IGNORED) {
436 output_line(event->proc, "+++ killed by %s +++",
437 shortsignal(event->proc, event->e_un.signum));
438 }
Petr Machatacebb8842011-07-09 11:14:11 +0200439 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100440}
441
Petr Machataa32ef7c2012-04-23 18:05:24 +0200442static void
Petr Machata929bd572012-12-17 03:20:34 +0100443output_syscall(struct process *proc, const char *name, enum tof tof,
444 void (*output)(enum tof, struct process *,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200445 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100446{
Petr Machata82f748d2013-10-23 00:39:23 +0200447 static struct library syscall_lib;
448 if (syscall_lib.protolib == NULL) {
449 struct protolib *protolib
450 = protolib_cache_search(&g_protocache, "syscalls", 0, 1);
451 if (protolib == NULL) {
452 fprintf(stderr, "Couldn't load system call prototypes:"
453 " %s.\n", strerror(errno));
454
455 /* Instead, get a fake one just so we can
456 * carry on, limping. */
457 protolib = malloc(sizeof *protolib);
458 if (protolib == NULL) {
459 fprintf(stderr, "Couldn't even allocate a fake "
460 "prototype library: %s.\n",
461 strerror(errno));
462 abort();
463 }
464 protolib_init(protolib);
465 }
466
467 assert(protolib != NULL);
468 if (library_init(&syscall_lib, LT_LIBTYPE_SYSCALL) < 0) {
469 fprintf(stderr, "Couldn't initialize system call "
470 "library: %s.\n", strerror(errno));
471 abort();
472 }
473
474 library_set_soname(&syscall_lib, "SYS", 0);
475 syscall_lib.protolib = protolib;
476 }
477
Petr Machataa32ef7c2012-04-23 18:05:24 +0200478 struct library_symbol syscall;
479 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata82f748d2013-10-23 00:39:23 +0200480 syscall.lib = &syscall_lib;
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200481 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200482 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100483 }
Petr Machata29add4f2012-02-18 16:38:05 +0100484}
485
486static void
Petr Machata929bd572012-12-17 03:20:34 +0100487output_syscall_left(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100488{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200489 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100490}
491
492static void
Petr Machata929bd572012-12-17 03:20:34 +0100493output_syscall_right(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100494{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200495 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100496}
497
Juan Cespedesf1350522008-12-16 18:19:58 +0100498static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200499handle_syscall(Event *event) {
500 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200501 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100502 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100503 if (options.syscalls)
504 output_syscall_left(event->proc,
505 sysname(event->proc,
506 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100507 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100508 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100509}
510
Juan Cespedesf1350522008-12-16 18:19:58 +0100511static void
Petr Machata929bd572012-12-17 03:20:34 +0100512handle_exec(Event *event)
513{
514 struct process *proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200515
Petr Machata3d0c91c2012-04-14 02:37:38 +0200516 /* Save the PID so that we can use it after unsuccessful
517 * process_exec. */
518 pid_t pid = proc->pid;
519
Juan Cespedes03192f82009-07-03 10:16:22 +0200520 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200521 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200522 untrace:
523 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200524 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200525 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200526 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200527 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200528
529 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200530 fprintf(stderr,
531 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200532 goto untrace;
533 }
534
Petr Machata057caa52013-01-30 23:28:47 +0100535 continue_after_exec(proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200536}
537
538static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200539handle_arch_syscall(Event *event) {
540 debug(DEBUG_FUNCTION, "handle_arch_syscall(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) {
Petr Machata211f0882010-11-03 18:42:18 +0100542 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200543 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100544 output_syscall_left(event->proc,
545 arch_sysname(event->proc,
546 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200547 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100548 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100549 continue_process(event->proc->pid);
550}
551
Juan Cespedesd65efa32003-02-03 00:22:30 +0100552struct timeval current_time_spent;
553
Juan Cespedesf1350522008-12-16 18:19:58 +0100554static void
Petr Machata929bd572012-12-17 03:20:34 +0100555calc_time_spent(struct process *proc)
556{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100557 struct timeval tv;
558 struct timezone tz;
559 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100560 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100561
Juan Cespedescd8976d2009-05-14 13:47:58 +0200562 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100563 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100564
565 gettimeofday(&tv, &tz);
566
567 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
568 if (tv.tv_usec >= elem->time_spent.tv_usec) {
569 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
570 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400571 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100572 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
573 }
574 current_time_spent = diff;
575}
576
Juan Cespedesf1350522008-12-16 18:19:58 +0100577static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200578handle_sysret(Event *event) {
579 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200580 if (event->proc->state != STATE_IGNORED) {
581 if (opt_T || options.summary) {
582 calc_time_spent(event->proc);
583 }
Petr Machata29add4f2012-02-18 16:38:05 +0100584 if (options.syscalls)
585 output_syscall_right(event->proc,
586 sysname(event->proc,
587 event->e_un.sysnum));
588
Petr Machata43d2fe52011-11-02 13:25:49 +0100589 assert(event->proc->callstack_depth > 0);
590 unsigned d = event->proc->callstack_depth - 1;
591 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100592 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200593 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100594 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100595}
596
Juan Cespedesf1350522008-12-16 18:19:58 +0100597static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200598handle_arch_sysret(Event *event) {
599 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200600 if (event->proc->state != STATE_IGNORED) {
601 if (opt_T || options.summary) {
602 calc_time_spent(event->proc);
603 }
Petr Machata29add4f2012-02-18 16:38:05 +0100604 if (options.syscalls)
605 output_syscall_right(event->proc,
606 arch_sysname(event->proc,
607 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100608 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100609 }
610 continue_process(event->proc->pid);
611}
612
Juan Cespedesf1350522008-12-16 18:19:58 +0100613static void
Petr Machata929bd572012-12-17 03:20:34 +0100614output_right_tos(struct process *proc)
Petr Machata14298742012-04-12 23:09:21 +0200615{
616 size_t d = proc->callstack_depth;
617 struct callstack_element *elem = &proc->callstack[d - 1];
618 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200619 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200620}
621
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200622#ifndef ARCH_HAVE_SYMBOL_RET
Petr Machata929bd572012-12-17 03:20:34 +0100623void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200624{
625}
626#endif
627
Petr Machata14298742012-04-12 23:09:21 +0200628static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100629handle_breakpoint(Event *event)
630{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100631 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100632 struct breakpoint *sbp;
Petr Machata929bd572012-12-17 03:20:34 +0100633 struct process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200634 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200635
636 /* The leader has terminated. */
637 if (leader == NULL) {
638 continue_process(event->proc->pid);
639 return;
640 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100641
Petr Machata31b2f9f2012-04-12 22:23:40 +0200642 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
643 event->proc->pid, brk_addr);
644 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100645
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100646 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200647 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100648 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100649 callstack_pop(event->proc);
650 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200651 if (event->proc->state != STATE_IGNORED) {
652 if (opt_T || options.summary) {
653 calc_time_spent(event->proc);
654 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100655 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200656
Petr Machata9e1e9692012-04-19 02:29:38 +0200657 struct library_symbol *libsym =
658 event->proc->callstack[i].c_un.libfunc;
659
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200660 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200661 output_right_tos(event->proc);
662 callstack_pop(event->proc);
663
Petr Machata31b2f9f2012-04-12 22:23:40 +0200664 /* Pop also any other entries that seem like
665 * they are linked to the current one: they
666 * have the same return address, but were made
667 * for different symbols. This should only
668 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200669 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200670 while (event->proc->callstack_depth > 0) {
671 struct callstack_element *prev;
672 size_t d = event->proc->callstack_depth;
673 prev = &event->proc->callstack[d - 1];
674
Petr Machata14298742012-04-12 23:09:21 +0200675 if (prev->c_un.libfunc == libsym
676 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200677 break;
Petr Machata14298742012-04-12 23:09:21 +0200678
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200679 arch_symbol_ret(event->proc,
680 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200681 output_right_tos(event->proc);
682 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200683 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200684
Petr Machata5ee36822012-04-19 17:01:51 +0200685 /* Maybe the previous callstack_pop's got rid
686 * of the breakpoint, but if we are in a
687 * recursive call, it's still enabled. In
688 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200689 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200690 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200691 } else {
692 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200693 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200694 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100695 return;
696 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100697 }
698
Petr Machata5ee36822012-04-19 17:01:51 +0200699 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100700 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200701 else if (event->proc->state != STATE_IGNORED)
702 output_line(event->proc,
703 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100704
Petr Machata5ee36822012-04-19 17:01:51 +0200705 /* breakpoint_on_hit may delete its own breakpoint, so we have
706 * to look it up again. */
707 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100708 if (event->proc->state != STATE_IGNORED
709 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200710 event->proc->stack_pointer = get_stack_pointer(event->proc);
Petr Machatacf989232013-10-11 21:17:24 +0200711 callstack_push_symfunc(event->proc, sbp);
Petr Machata29add4f2012-02-18 16:38:05 +0100712 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200713 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100714
Petr Machata56a9ea62012-03-27 03:09:29 +0200715 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100716 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200717 } else {
718 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100719 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100720
Juan Cespedes5e01f651998-03-08 22:31:44 +0100721 continue_process(event->proc->pid);
722}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200723
Juan Cespedesf1350522008-12-16 18:19:58 +0100724static void
Petr Machata929bd572012-12-17 03:20:34 +0100725callstack_push_syscall(struct process *proc, int sysnum)
726{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100727 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200728
Juan Cespedescd8976d2009-05-14 13:47:58 +0200729 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200730 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100731 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500732 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
733 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200734 return;
735 }
736
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100737 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200738 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200739 elem->is_syscall = 1;
740 elem->c_un.syscall = sysnum;
741 elem->return_addr = NULL;
742
743 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200744 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100745 struct timezone tz;
746 gettimeofday(&elem->time_spent, &tz);
747 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200748}
749
Juan Cespedes21c63a12001-07-07 20:56:56 +0200750static void
Petr Machatacf989232013-10-11 21:17:24 +0200751callstack_push_symfunc(struct process *proc, struct breakpoint *bp)
Petr Machata929bd572012-12-17 03:20:34 +0100752{
Petr Machata14298742012-04-12 23:09:21 +0200753 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200754
Petr Machatacf989232013-10-11 21:17:24 +0200755 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)",
756 proc->pid, bp->libsym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200757 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100758 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500759 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
760 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200761 return;
762 }
763
Petr Machata14298742012-04-12 23:09:21 +0200764 elem = &proc->callstack[proc->callstack_depth++];
Petr Machata8ae36732012-04-30 01:19:09 +0200765 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200766 elem->is_syscall = 0;
Petr Machatacf989232013-10-11 21:17:24 +0200767 elem->c_un.libfunc = bp->libsym;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200768
Petr Machataf9d93c52013-01-30 23:09:02 +0100769 struct breakpoint *rbp = NULL;
Petr Machatacf989232013-10-11 21:17:24 +0200770 if (breakpoint_get_return_bp(&rbp, bp, proc) == 0) {
771 struct breakpoint *ext_rbp = insert_breakpoint(proc, rbp);
772 if (ext_rbp != rbp) {
773 breakpoint_destroy(rbp);
774 free(rbp);
775 rbp = ext_rbp;
776 }
777 }
778
Petr Machataf9d93c52013-01-30 23:09:02 +0100779 elem->return_addr = rbp != NULL ? rbp->addr : 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200780
Juan Cespedesda9b9532009-04-07 15:33:50 +0200781 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100782 struct timezone tz;
783 gettimeofday(&elem->time_spent, &tz);
784 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200785}
786
Petr Machatae655ccf2012-10-26 22:21:59 +0200787void
Petr Machata929bd572012-12-17 03:20:34 +0100788callstack_pop(struct process *proc)
Petr Machatae655ccf2012-10-26 22:21:59 +0200789{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100790 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200791 assert(proc->callstack_depth > 0);
792
Juan Cespedescd8976d2009-05-14 13:47:58 +0200793 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100794 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatae655ccf2012-10-26 22:21:59 +0200795 if (!elem->is_syscall && elem->return_addr)
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200796 delete_breakpoint(proc, elem->return_addr);
Petr Machatae655ccf2012-10-26 22:21:59 +0200797
798 if (elem->fetch_context != NULL)
799 fetch_arg_done(elem->fetch_context);
800
801 if (elem->arguments != NULL) {
802 val_dict_destroy(elem->arguments);
803 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200804 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200805
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200806 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200807}