blob: 668344b3a49051f2a249d20c789cdccf656605e3 [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>
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
Petr Machata929bd572012-12-17 03:20:34 +010057static void callstack_push_syscall(struct process *proc, int sysnum);
58static void callstack_push_symfunc(struct 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. */
Petr Machata929bd572012-12-17 03:20:34 +010063void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064
Petr Machata929bd572012-12-17 03:20:34 +010065static char *shortsignal(struct process *proc, int signum);
66static char *sysname(struct process *proc, int sysnum);
67static char *arch_sysname(struct process *proc, int sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +020068
Petr Machatacbe29c62011-09-27 02:27:58 +020069static Event *
Petr Machata929bd572012-12-17 03:20:34 +010070call_handler(struct process *proc, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +020071{
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;
Petr Machata9978de42013-01-08 23:46:22 +0100117
Juan Cespedes61da3372009-07-03 11:55:44 +0200118 case EVENT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100119 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200120 debug(1, "[%d] event: signal (%s [%d])",
121 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200122 shortsignal(event->proc, event->e_un.signum),
123 event->e_un.signum);
124 handle_signal(event);
125 return;
Petr Machata9978de42013-01-08 23:46:22 +0100126
Juan Cespedes61da3372009-07-03 11:55:44 +0200127 case EVENT_EXIT:
Petr Machata9978de42013-01-08 23:46:22 +0100128 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200129 debug(1, "[%d] event: exit (%d)",
130 event->proc->pid,
131 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200132 handle_exit(event);
133 return;
Petr Machata9978de42013-01-08 23:46:22 +0100134
Juan Cespedes61da3372009-07-03 11:55:44 +0200135 case EVENT_EXIT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100136 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200137 debug(1, "[%d] event: exit signal (%s [%d])",
138 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200139 shortsignal(event->proc, event->e_un.signum),
140 event->e_un.signum);
141 handle_exit_signal(event);
142 return;
Petr Machata9978de42013-01-08 23:46:22 +0100143
Juan Cespedes61da3372009-07-03 11:55:44 +0200144 case EVENT_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100145 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200146 debug(1, "[%d] event: syscall (%s [%d])",
147 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200148 sysname(event->proc, event->e_un.sysnum),
149 event->e_un.sysnum);
150 handle_syscall(event);
151 return;
Petr Machata9978de42013-01-08 23:46:22 +0100152
Juan Cespedes61da3372009-07-03 11:55:44 +0200153 case EVENT_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100154 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200155 debug(1, "[%d] event: sysret (%s [%d])",
156 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200157 sysname(event->proc, event->e_un.sysnum),
158 event->e_un.sysnum);
159 handle_sysret(event);
160 return;
Petr Machata9978de42013-01-08 23:46:22 +0100161
Juan Cespedes61da3372009-07-03 11:55:44 +0200162 case EVENT_ARCH_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100163 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200164 debug(1, "[%d] event: arch_syscall (%s [%d])",
165 event->proc->pid,
166 arch_sysname(event->proc, event->e_un.sysnum),
167 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200168 handle_arch_syscall(event);
169 return;
Petr Machata9978de42013-01-08 23:46:22 +0100170
Juan Cespedes61da3372009-07-03 11:55:44 +0200171 case EVENT_ARCH_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100172 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200173 debug(1, "[%d] event: arch_sysret (%s [%d])",
174 event->proc->pid,
175 arch_sysname(event->proc, event->e_un.sysnum),
176 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200177 handle_arch_sysret(event);
178 return;
Petr Machata9978de42013-01-08 23:46:22 +0100179
Juan Cespedes61da3372009-07-03 11:55:44 +0200180 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200181 case EVENT_VFORK:
Petr Machata9978de42013-01-08 23:46:22 +0100182 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200183 debug(1, "[%d] event: clone (%u)",
184 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200185 handle_clone(event);
186 return;
Petr Machata9978de42013-01-08 23:46:22 +0100187
Juan Cespedes61da3372009-07-03 11:55:44 +0200188 case EVENT_EXEC:
Petr Machata9978de42013-01-08 23:46:22 +0100189 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200190 debug(1, "[%d] event: exec()",
191 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200192 handle_exec(event);
193 return;
Petr Machata9978de42013-01-08 23:46:22 +0100194
Juan Cespedes61da3372009-07-03 11:55:44 +0200195 case EVENT_BREAKPOINT:
Petr Machata9978de42013-01-08 23:46:22 +0100196 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200197 debug(1, "[%d] event: breakpoint %p",
198 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200199 handle_breakpoint(event);
200 return;
Petr Machata9978de42013-01-08 23:46:22 +0100201
Juan Cespedes61da3372009-07-03 11:55:44 +0200202 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200203 debug(1, "[%d] event: new process",
204 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200205 handle_new(event);
206 return;
207 default:
208 fprintf(stderr, "Error! unknown event?\n");
209 exit(1);
210 }
211}
212
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200213typedef struct Pending_New Pending_New;
214struct Pending_New {
215 pid_t pid;
216 Pending_New * next;
217};
218static Pending_New * pending_news = NULL;
219
220static int
221pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200222 Pending_New * p;
223
224 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
225
226 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200227 while (p) {
228 if (p->pid == pid) {
229 return 1;
230 }
231 p = p->next;
232 }
233 return 0;
234}
235
236static void
237pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200238 Pending_New * p;
239
240 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
241
242 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200243 if (!p) {
244 perror("malloc()");
245 exit(1);
246 }
247 p->pid = pid;
248 p->next = pending_news;
249 pending_news = p;
250}
251
252static void
253pending_new_remove(pid_t pid) {
254 Pending_New *p, *pred;
255
Juan Cespedescd8976d2009-05-14 13:47:58 +0200256 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
257
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200258 p = pending_news;
Sedat Dilek7d804e92012-09-01 02:30:52 +0200259 pred = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200260 if (p->pid == pid) {
261 pending_news = p->next;
262 free(p);
263 } else {
264 while (p) {
265 if (p->pid == pid) {
266 pred->next = p->next;
267 free(p);
268 }
269 pred = p;
270 p = p->next;
271 }
272 }
273}
274
275static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276handle_clone(Event *event)
277{
Juan Cespedes03192f82009-07-03 10:16:22 +0200278 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200279
Petr Machata929bd572012-12-17 03:20:34 +0100280 struct process *proc = malloc(sizeof(*proc));
Petr Machata6095c132013-01-07 19:08:18 +0100281 pid_t newpid = event->e_un.newpid;
282 if (proc == NULL
283 || process_clone(proc, event->proc, newpid) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 free(proc);
Petr Machata6095c132013-01-07 19:08:18 +0100285 proc = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100286 fprintf(stderr,
Petr Machata6095c132013-01-07 19:08:18 +0100287 "Couldn't initialize tracing of process %d.\n",
288 newpid);
289
290 } else {
291 proc->parent = event->proc;
292 /* We save register values to the arch pointer, and
293 * these need to be per-thread. XXX arch_ptr should
294 * be retired in favor of fetch interface anyway. */
295 proc->arch_ptr = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200296 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297
Petr Machata6095c132013-01-07 19:08:18 +0100298 if (pending_new(newpid)) {
299 pending_new_remove(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200300
Petr Machata6095c132013-01-07 19:08:18 +0100301 if (proc != NULL) {
302 proc->event_handler = NULL;
303 if (event->proc->state == STATE_ATTACHED
304 && options.follow)
305 proc->state = STATE_ATTACHED;
306 else
307 proc->state = STATE_IGNORED;
308 }
Petr Machata75dcf7d2011-10-06 14:30:19 +0200309
Petr Machata6095c132013-01-07 19:08:18 +0100310 continue_process(newpid);
311
312 } else if (proc != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100313 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200314 }
Petr Machata534e00f2011-09-27 17:58:38 +0200315
Petr Machata6095c132013-01-07 19:08:18 +0100316 if (event->type != EVENT_VFORK)
317 continue_process(event->proc->pid);
318 else if (proc != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200320 else
Petr Machata6095c132013-01-07 19:08:18 +0100321 continue_process(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200322}
323
324static void
Petr Machata929bd572012-12-17 03:20:34 +0100325handle_new(Event *event)
326{
Juan Cespedes03192f82009-07-03 10:16:22 +0200327 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200328
Petr Machata929bd572012-12-17 03:20:34 +0100329 struct process *proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200330 if (!proc) {
331 pending_new_insert(event->e_un.newpid);
332 } else {
333 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200334 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200335 proc->state = STATE_ATTACHED;
336 } else {
337 proc->state = STATE_IGNORED;
338 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200339 continue_process(proc->pid);
340 }
341}
342
Juan Cespedesf1350522008-12-16 18:19:58 +0100343static char *
Petr Machata929bd572012-12-17 03:20:34 +0100344shortsignal(struct process *proc, int signum)
345{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 static char *signalent0[] = {
347#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 static char *signalent1[] = {
350#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100351 };
352 static char **signalents[] = { signalent0, signalent1 };
353 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 sizeof signalent1 / sizeof signalent1[0]
355 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100356
Juan Cespedescd8976d2009-05-14 13:47:58 +0200357 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
358
Petr Machata8e682c22013-01-09 16:20:20 +0100359 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100360 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100361 return "UNKNOWN_SIGNAL";
362 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100363 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100364 }
365}
366
Juan Cespedesf1350522008-12-16 18:19:58 +0100367static char *
Petr Machata929bd572012-12-17 03:20:34 +0100368sysname(struct process *proc, int sysnum)
369{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100370 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100371 static char *syscallent0[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100373 };
Petr Machata8e682c22013-01-09 16:20:20 +0100374 static char *syscallent1[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100376 };
Petr Machata8e682c22013-01-09 16:20:20 +0100377 static char **syscallents[] = { syscallent0, syscallent1 };
378 int nsyscalls[] = {
379 sizeof syscallent0 / sizeof syscallent0[0],
380 sizeof syscallent1 / sizeof syscallent1[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100382
Juan Cespedescd8976d2009-05-14 13:47:58 +0200383 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
384
Petr Machata8e682c22013-01-09 16:20:20 +0100385 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
386 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100387 sprintf(result, "SYS_%d", sysnum);
388 return result;
389 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 sprintf(result, "SYS_%s",
Petr Machata8e682c22013-01-09 16:20:20 +0100391 syscallents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100392 return result;
393 }
394}
395
Juan Cespedesf1350522008-12-16 18:19:58 +0100396static char *
Petr Machata929bd572012-12-17 03:20:34 +0100397arch_sysname(struct process *proc, int sysnum)
398{
Juan Cespedes63184be2008-12-10 13:30:12 +0100399 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100400 static char *arch_syscallent[] = {
Juan Cespedes63184be2008-12-10 13:30:12 +0100401#include "arch_syscallent.h"
402 };
Petr Machata8e682c22013-01-09 16:20:20 +0100403 int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0];
Juan Cespedes63184be2008-12-10 13:30:12 +0100404
Juan Cespedescd8976d2009-05-14 13:47:58 +0200405 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
406
Petr Machata8e682c22013-01-09 16:20:20 +0100407 if (sysnum < 0 || sysnum >= nsyscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100408 sprintf(result, "ARCH_%d", sysnum);
409 return result;
410 } else {
Petr Machata8e682c22013-01-09 16:20:20 +0100411 sprintf(result, "ARCH_%s", arch_syscallent[sysnum]);
Juan Cespedes63184be2008-12-10 13:30:12 +0100412 return result;
413 }
414}
415
Petr Machata7c4d3112012-12-09 11:55:03 +0100416#ifndef HAVE_STRSIGNAL
417# define strsignal(SIGNUM) "???"
418#endif
419
Juan Cespedesf1350522008-12-16 18:19:58 +0100420static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200421handle_signal(Event *event) {
422 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800423 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200424 output_line(event->proc, "--- %s (%s) ---",
425 shortsignal(event->proc, event->e_un.signum),
426 strsignal(event->e_un.signum));
427 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100428 continue_after_signal(event->proc->pid, event->e_un.signum);
429}
430
Juan Cespedesf1350522008-12-16 18:19:58 +0100431static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200432handle_exit(Event *event) {
433 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200434 if (event->proc->state != STATE_IGNORED) {
435 output_line(event->proc, "+++ exited (status %d) +++",
436 event->e_un.ret_val);
437 }
Petr Machatacebb8842011-07-09 11:14:11 +0200438 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100439}
440
Juan Cespedesf1350522008-12-16 18:19:58 +0100441static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200442handle_exit_signal(Event *event) {
443 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200444 if (event->proc->state != STATE_IGNORED) {
445 output_line(event->proc, "+++ killed by %s +++",
446 shortsignal(event->proc, event->e_un.signum));
447 }
Petr Machatacebb8842011-07-09 11:14:11 +0200448 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100449}
450
Petr Machataa32ef7c2012-04-23 18:05:24 +0200451static void
Petr Machata929bd572012-12-17 03:20:34 +0100452output_syscall(struct process *proc, const char *name, enum tof tof,
453 void (*output)(enum tof, struct process *,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200454 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100455{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200456 struct library_symbol syscall;
457 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200458 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200459 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100460 }
Petr Machata29add4f2012-02-18 16:38:05 +0100461}
462
463static void
Petr Machata929bd572012-12-17 03:20:34 +0100464output_syscall_left(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100465{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200466 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100467}
468
469static void
Petr Machata929bd572012-12-17 03:20:34 +0100470output_syscall_right(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100471{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200472 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100473}
474
Juan Cespedesf1350522008-12-16 18:19:58 +0100475static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200476handle_syscall(Event *event) {
477 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200478 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100479 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100480 if (options.syscalls)
481 output_syscall_left(event->proc,
482 sysname(event->proc,
483 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100484 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100485 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100486}
487
Juan Cespedesf1350522008-12-16 18:19:58 +0100488static void
Petr Machata929bd572012-12-17 03:20:34 +0100489handle_exec(Event *event)
490{
491 struct process *proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200492
Petr Machata3d0c91c2012-04-14 02:37:38 +0200493 /* Save the PID so that we can use it after unsuccessful
494 * process_exec. */
495 pid_t pid = proc->pid;
496
Juan Cespedes03192f82009-07-03 10:16:22 +0200497 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200498 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200499 untrace:
500 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200501 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200502 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200503 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200504 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200505
506 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200507 fprintf(stderr,
508 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200509 goto untrace;
510 }
511
Petr Machata057caa52013-01-30 23:28:47 +0100512 continue_after_exec(proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200513}
514
515static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200516handle_arch_syscall(Event *event) {
517 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200518 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100519 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200520 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100521 output_syscall_left(event->proc,
522 arch_sysname(event->proc,
523 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200524 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100525 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100526 continue_process(event->proc->pid);
527}
528
Juan Cespedesd65efa32003-02-03 00:22:30 +0100529struct timeval current_time_spent;
530
Juan Cespedesf1350522008-12-16 18:19:58 +0100531static void
Petr Machata929bd572012-12-17 03:20:34 +0100532calc_time_spent(struct process *proc)
533{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100534 struct timeval tv;
535 struct timezone tz;
536 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100537 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100538
Juan Cespedescd8976d2009-05-14 13:47:58 +0200539 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100540 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100541
542 gettimeofday(&tv, &tz);
543
544 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
545 if (tv.tv_usec >= elem->time_spent.tv_usec) {
546 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
547 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400548 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100549 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
550 }
551 current_time_spent = diff;
552}
553
Juan Cespedesf1350522008-12-16 18:19:58 +0100554static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200555handle_sysret(Event *event) {
556 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200557 if (event->proc->state != STATE_IGNORED) {
558 if (opt_T || options.summary) {
559 calc_time_spent(event->proc);
560 }
Petr Machata29add4f2012-02-18 16:38:05 +0100561 if (options.syscalls)
562 output_syscall_right(event->proc,
563 sysname(event->proc,
564 event->e_un.sysnum));
565
Petr Machata43d2fe52011-11-02 13:25:49 +0100566 assert(event->proc->callstack_depth > 0);
567 unsigned d = event->proc->callstack_depth - 1;
568 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100569 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200570 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100571 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100572}
573
Juan Cespedesf1350522008-12-16 18:19:58 +0100574static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200575handle_arch_sysret(Event *event) {
576 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200577 if (event->proc->state != STATE_IGNORED) {
578 if (opt_T || options.summary) {
579 calc_time_spent(event->proc);
580 }
Petr Machata29add4f2012-02-18 16:38:05 +0100581 if (options.syscalls)
582 output_syscall_right(event->proc,
583 arch_sysname(event->proc,
584 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100585 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100586 }
587 continue_process(event->proc->pid);
588}
589
Juan Cespedesf1350522008-12-16 18:19:58 +0100590static void
Petr Machata929bd572012-12-17 03:20:34 +0100591output_right_tos(struct process *proc)
Petr Machata14298742012-04-12 23:09:21 +0200592{
593 size_t d = proc->callstack_depth;
594 struct callstack_element *elem = &proc->callstack[d - 1];
595 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200596 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200597}
598
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200599#ifndef ARCH_HAVE_SYMBOL_RET
Petr Machata929bd572012-12-17 03:20:34 +0100600void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200601{
602}
603#endif
604
Petr Machata14298742012-04-12 23:09:21 +0200605static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100606handle_breakpoint(Event *event)
607{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100608 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100609 struct breakpoint *sbp;
Petr Machata929bd572012-12-17 03:20:34 +0100610 struct process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200611 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200612
613 /* The leader has terminated. */
614 if (leader == NULL) {
615 continue_process(event->proc->pid);
616 return;
617 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100618
Petr Machata31b2f9f2012-04-12 22:23:40 +0200619 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
620 event->proc->pid, brk_addr);
621 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100622
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100623 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200624 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100625 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100626 callstack_pop(event->proc);
627 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200628 if (event->proc->state != STATE_IGNORED) {
629 if (opt_T || options.summary) {
630 calc_time_spent(event->proc);
631 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100632 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200633 event->proc->return_addr = brk_addr;
634
Petr Machata9e1e9692012-04-19 02:29:38 +0200635 struct library_symbol *libsym =
636 event->proc->callstack[i].c_un.libfunc;
637
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200638 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200639 output_right_tos(event->proc);
640 callstack_pop(event->proc);
641
Petr Machata31b2f9f2012-04-12 22:23:40 +0200642 /* Pop also any other entries that seem like
643 * they are linked to the current one: they
644 * have the same return address, but were made
645 * for different symbols. This should only
646 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200647 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200648 while (event->proc->callstack_depth > 0) {
649 struct callstack_element *prev;
650 size_t d = event->proc->callstack_depth;
651 prev = &event->proc->callstack[d - 1];
652
Petr Machata14298742012-04-12 23:09:21 +0200653 if (prev->c_un.libfunc == libsym
654 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200655 break;
Petr Machata14298742012-04-12 23:09:21 +0200656
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200657 arch_symbol_ret(event->proc,
658 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200659 output_right_tos(event->proc);
660 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200661 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200662
Petr Machata5ee36822012-04-19 17:01:51 +0200663 /* Maybe the previous callstack_pop's got rid
664 * of the breakpoint, but if we are in a
665 * recursive call, it's still enabled. In
666 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200667 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200668 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200669 } else {
670 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200671 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200672 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100673 return;
674 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100675 }
676
Petr Machata5ee36822012-04-19 17:01:51 +0200677 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100678 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200679 else if (event->proc->state != STATE_IGNORED)
680 output_line(event->proc,
681 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100682
Petr Machata5ee36822012-04-19 17:01:51 +0200683 /* breakpoint_on_hit may delete its own breakpoint, so we have
684 * to look it up again. */
685 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100686 if (event->proc->state != STATE_IGNORED
687 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200688 event->proc->stack_pointer = get_stack_pointer(event->proc);
689 event->proc->return_addr =
690 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200691 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100692 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200693 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100694
Petr Machata56a9ea62012-03-27 03:09:29 +0200695 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100696 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200697 } else {
698 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100699 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100700
Juan Cespedes5e01f651998-03-08 22:31:44 +0100701 continue_process(event->proc->pid);
702}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200703
Juan Cespedesf1350522008-12-16 18:19:58 +0100704static void
Petr Machata929bd572012-12-17 03:20:34 +0100705callstack_push_syscall(struct process *proc, int sysnum)
706{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100707 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200708
Juan Cespedescd8976d2009-05-14 13:47:58 +0200709 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200710 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100711 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500712 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
713 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200714 return;
715 }
716
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100717 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200718 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200719 elem->is_syscall = 1;
720 elem->c_un.syscall = sysnum;
721 elem->return_addr = NULL;
722
723 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200724 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100725 struct timezone tz;
726 gettimeofday(&elem->time_spent, &tz);
727 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200728}
729
Juan Cespedes21c63a12001-07-07 20:56:56 +0200730static void
Petr Machata929bd572012-12-17 03:20:34 +0100731callstack_push_symfunc(struct process *proc, struct library_symbol *sym)
732{
Petr Machata14298742012-04-12 23:09:21 +0200733 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200734
Juan Cespedescd8976d2009-05-14 13:47:58 +0200735 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200736 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100737 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500738 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
739 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200740 return;
741 }
742
Petr Machata14298742012-04-12 23:09:21 +0200743 elem = &proc->callstack[proc->callstack_depth++];
Petr Machata8ae36732012-04-30 01:19:09 +0200744 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200745 elem->is_syscall = 0;
746 elem->c_un.libfunc = sym;
747
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200748 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100749 if (elem->return_addr)
750 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200751
Juan Cespedesda9b9532009-04-07 15:33:50 +0200752 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100753 struct timezone tz;
754 gettimeofday(&elem->time_spent, &tz);
755 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200756}
757
Petr Machatae655ccf2012-10-26 22:21:59 +0200758void
Petr Machata929bd572012-12-17 03:20:34 +0100759callstack_pop(struct process *proc)
Petr Machatae655ccf2012-10-26 22:21:59 +0200760{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100761 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200762 assert(proc->callstack_depth > 0);
763
Juan Cespedescd8976d2009-05-14 13:47:58 +0200764 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100765 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatae655ccf2012-10-26 22:21:59 +0200766 if (!elem->is_syscall && elem->return_addr)
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200767 delete_breakpoint(proc, elem->return_addr);
Petr Machatae655ccf2012-10-26 22:21:59 +0200768
769 if (elem->fetch_context != NULL)
770 fetch_arg_done(elem->fetch_context);
771
772 if (elem->arguments != NULL) {
773 val_dict_destroy(elem->arguments);
774 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200775 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200776
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200777 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200778}