blob: ea73785b024696d12566f29f1eadc1bdbcf9744b [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
Petr Machatac1f1bf42013-01-09 22:39:41 +0100253pending_new_remove(pid_t pid)
254{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200255 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
256
Petr Machatac1f1bf42013-01-09 22:39:41 +0100257 Pending_New **pp;
258 for (pp = &pending_news; *pp != NULL; pp = &(*pp)->next)
259 if ((*pp)->pid == pid) {
260 Pending_New *p = *pp;
261 *pp = p->next;
262 free(p);
263 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200264 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200265}
266
267static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100268handle_clone(Event *event)
269{
Juan Cespedes03192f82009-07-03 10:16:22 +0200270 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200271
Petr Machata929bd572012-12-17 03:20:34 +0100272 struct process *proc = malloc(sizeof(*proc));
Petr Machata6095c132013-01-07 19:08:18 +0100273 pid_t newpid = event->e_un.newpid;
274 if (proc == NULL
275 || process_clone(proc, event->proc, newpid) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276 free(proc);
Petr Machata6095c132013-01-07 19:08:18 +0100277 proc = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100278 fprintf(stderr,
Petr Machata6095c132013-01-07 19:08:18 +0100279 "Couldn't initialize tracing of process %d.\n",
280 newpid);
281
282 } else {
283 proc->parent = event->proc;
284 /* We save register values to the arch pointer, and
285 * these need to be per-thread. XXX arch_ptr should
286 * be retired in favor of fetch interface anyway. */
287 proc->arch_ptr = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200288 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289
Petr Machata6095c132013-01-07 19:08:18 +0100290 if (pending_new(newpid)) {
291 pending_new_remove(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200292
Petr Machata6095c132013-01-07 19:08:18 +0100293 if (proc != NULL) {
294 proc->event_handler = NULL;
295 if (event->proc->state == STATE_ATTACHED
296 && options.follow)
297 proc->state = STATE_ATTACHED;
298 else
299 proc->state = STATE_IGNORED;
300 }
Petr Machata75dcf7d2011-10-06 14:30:19 +0200301
Petr Machata6095c132013-01-07 19:08:18 +0100302 continue_process(newpid);
303
304 } else if (proc != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200306 }
Petr Machata534e00f2011-09-27 17:58:38 +0200307
Petr Machata6095c132013-01-07 19:08:18 +0100308 if (event->type != EVENT_VFORK)
309 continue_process(event->proc->pid);
310 else if (proc != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100311 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200312 else
Petr Machata6095c132013-01-07 19:08:18 +0100313 continue_process(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200314}
315
316static void
Petr Machata929bd572012-12-17 03:20:34 +0100317handle_new(Event *event)
318{
Juan Cespedes03192f82009-07-03 10:16:22 +0200319 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200320
Petr Machata929bd572012-12-17 03:20:34 +0100321 struct process *proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200322 if (!proc) {
323 pending_new_insert(event->e_un.newpid);
324 } else {
325 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200326 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200327 proc->state = STATE_ATTACHED;
328 } else {
329 proc->state = STATE_IGNORED;
330 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200331 continue_process(proc->pid);
332 }
333}
334
Juan Cespedesf1350522008-12-16 18:19:58 +0100335static char *
Petr Machata929bd572012-12-17 03:20:34 +0100336shortsignal(struct process *proc, int signum)
337{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100338 static char *signalent0[] = {
339#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100340 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 static char *signalent1[] = {
342#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100343 };
344 static char **signalents[] = { signalent0, signalent1 };
345 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 sizeof signalent1 / sizeof signalent1[0]
347 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348
Juan Cespedescd8976d2009-05-14 13:47:58 +0200349 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
350
Petr Machata8e682c22013-01-09 16:20:20 +0100351 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100352 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100353 return "UNKNOWN_SIGNAL";
354 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100355 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100356 }
357}
358
Juan Cespedesf1350522008-12-16 18:19:58 +0100359static char *
Petr Machata929bd572012-12-17 03:20:34 +0100360sysname(struct process *proc, int sysnum)
361{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100362 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100363 static char *syscallent0[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100365 };
Petr Machata8e682c22013-01-09 16:20:20 +0100366 static char *syscallent1[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100368 };
Petr Machata8e682c22013-01-09 16:20:20 +0100369 static char **syscallents[] = { syscallent0, syscallent1 };
370 int nsyscalls[] = {
371 sizeof syscallent0 / sizeof syscallent0[0],
372 sizeof syscallent1 / sizeof syscallent1[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100374
Juan Cespedescd8976d2009-05-14 13:47:58 +0200375 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
376
Petr Machata8e682c22013-01-09 16:20:20 +0100377 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
378 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100379 sprintf(result, "SYS_%d", sysnum);
380 return result;
381 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 sprintf(result, "SYS_%s",
Petr Machata8e682c22013-01-09 16:20:20 +0100383 syscallents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100384 return result;
385 }
386}
387
Juan Cespedesf1350522008-12-16 18:19:58 +0100388static char *
Petr Machata929bd572012-12-17 03:20:34 +0100389arch_sysname(struct process *proc, int sysnum)
390{
Juan Cespedes63184be2008-12-10 13:30:12 +0100391 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100392 static char *arch_syscallent[] = {
Juan Cespedes63184be2008-12-10 13:30:12 +0100393#include "arch_syscallent.h"
394 };
Petr Machata8e682c22013-01-09 16:20:20 +0100395 int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0];
Juan Cespedes63184be2008-12-10 13:30:12 +0100396
Juan Cespedescd8976d2009-05-14 13:47:58 +0200397 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
398
Petr Machata8e682c22013-01-09 16:20:20 +0100399 if (sysnum < 0 || sysnum >= nsyscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100400 sprintf(result, "ARCH_%d", sysnum);
401 return result;
402 } else {
Petr Machata8e682c22013-01-09 16:20:20 +0100403 sprintf(result, "ARCH_%s", arch_syscallent[sysnum]);
Juan Cespedes63184be2008-12-10 13:30:12 +0100404 return result;
405 }
406}
407
Petr Machata7c4d3112012-12-09 11:55:03 +0100408#ifndef HAVE_STRSIGNAL
409# define strsignal(SIGNUM) "???"
410#endif
411
Juan Cespedesf1350522008-12-16 18:19:58 +0100412static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200413handle_signal(Event *event) {
414 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800415 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200416 output_line(event->proc, "--- %s (%s) ---",
417 shortsignal(event->proc, event->e_un.signum),
418 strsignal(event->e_un.signum));
419 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100420 continue_after_signal(event->proc->pid, event->e_un.signum);
421}
422
Juan Cespedesf1350522008-12-16 18:19:58 +0100423static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200424handle_exit(Event *event) {
425 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5c682042009-05-21 15:59:56 +0200426 if (event->proc->state != STATE_IGNORED) {
427 output_line(event->proc, "+++ exited (status %d) +++",
428 event->e_un.ret_val);
429 }
Petr Machatacebb8842011-07-09 11:14:11 +0200430 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100431}
432
Juan Cespedesf1350522008-12-16 18:19:58 +0100433static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200434handle_exit_signal(Event *event) {
435 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200436 if (event->proc->state != STATE_IGNORED) {
437 output_line(event->proc, "+++ killed by %s +++",
438 shortsignal(event->proc, event->e_un.signum));
439 }
Petr Machatacebb8842011-07-09 11:14:11 +0200440 remove_process(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100441}
442
Petr Machataa32ef7c2012-04-23 18:05:24 +0200443static void
Petr Machata929bd572012-12-17 03:20:34 +0100444output_syscall(struct process *proc, const char *name, enum tof tof,
445 void (*output)(enum tof, struct process *,
Petr Machataa32ef7c2012-04-23 18:05:24 +0200446 struct library_symbol *))
Petr Machata29add4f2012-02-18 16:38:05 +0100447{
Petr Machataa32ef7c2012-04-23 18:05:24 +0200448 struct library_symbol syscall;
449 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200450 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200451 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100452 }
Petr Machata29add4f2012-02-18 16:38:05 +0100453}
454
455static void
Petr Machata929bd572012-12-17 03:20:34 +0100456output_syscall_left(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100457{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200458 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100459}
460
461static void
Petr Machata929bd572012-12-17 03:20:34 +0100462output_syscall_right(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100463{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200464 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100465}
466
Juan Cespedesf1350522008-12-16 18:19:58 +0100467static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200468handle_syscall(Event *event) {
469 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200470 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100471 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100472 if (options.syscalls)
473 output_syscall_left(event->proc,
474 sysname(event->proc,
475 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100476 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100477 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100478}
479
Juan Cespedesf1350522008-12-16 18:19:58 +0100480static void
Petr Machata929bd572012-12-17 03:20:34 +0100481handle_exec(Event *event)
482{
483 struct process *proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200484
Petr Machata3d0c91c2012-04-14 02:37:38 +0200485 /* Save the PID so that we can use it after unsuccessful
486 * process_exec. */
487 pid_t pid = proc->pid;
488
Juan Cespedes03192f82009-07-03 10:16:22 +0200489 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200490 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200491 untrace:
492 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200493 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200494 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200495 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200496 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200497
498 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200499 fprintf(stderr,
500 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200501 goto untrace;
502 }
503
Petr Machata057caa52013-01-30 23:28:47 +0100504 continue_after_exec(proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200505}
506
507static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200508handle_arch_syscall(Event *event) {
509 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200510 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100511 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200512 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100513 output_syscall_left(event->proc,
514 arch_sysname(event->proc,
515 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200516 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100517 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100518 continue_process(event->proc->pid);
519}
520
Juan Cespedesd65efa32003-02-03 00:22:30 +0100521struct timeval current_time_spent;
522
Juan Cespedesf1350522008-12-16 18:19:58 +0100523static void
Petr Machata929bd572012-12-17 03:20:34 +0100524calc_time_spent(struct process *proc)
525{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100526 struct timeval tv;
527 struct timezone tz;
528 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100529 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100530
Juan Cespedescd8976d2009-05-14 13:47:58 +0200531 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100533
534 gettimeofday(&tv, &tz);
535
536 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
537 if (tv.tv_usec >= elem->time_spent.tv_usec) {
538 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
539 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400540 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100541 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
542 }
543 current_time_spent = diff;
544}
545
Juan Cespedesf1350522008-12-16 18:19:58 +0100546static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200547handle_sysret(Event *event) {
548 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200549 if (event->proc->state != STATE_IGNORED) {
550 if (opt_T || options.summary) {
551 calc_time_spent(event->proc);
552 }
Petr Machata29add4f2012-02-18 16:38:05 +0100553 if (options.syscalls)
554 output_syscall_right(event->proc,
555 sysname(event->proc,
556 event->e_un.sysnum));
557
Petr Machata43d2fe52011-11-02 13:25:49 +0100558 assert(event->proc->callstack_depth > 0);
559 unsigned d = event->proc->callstack_depth - 1;
560 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100561 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200562 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100563 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100564}
565
Juan Cespedesf1350522008-12-16 18:19:58 +0100566static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200567handle_arch_sysret(Event *event) {
568 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200569 if (event->proc->state != STATE_IGNORED) {
570 if (opt_T || options.summary) {
571 calc_time_spent(event->proc);
572 }
Petr Machata29add4f2012-02-18 16:38:05 +0100573 if (options.syscalls)
574 output_syscall_right(event->proc,
575 arch_sysname(event->proc,
576 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100577 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100578 }
579 continue_process(event->proc->pid);
580}
581
Juan Cespedesf1350522008-12-16 18:19:58 +0100582static void
Petr Machata929bd572012-12-17 03:20:34 +0100583output_right_tos(struct process *proc)
Petr Machata14298742012-04-12 23:09:21 +0200584{
585 size_t d = proc->callstack_depth;
586 struct callstack_element *elem = &proc->callstack[d - 1];
587 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200588 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200589}
590
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200591#ifndef ARCH_HAVE_SYMBOL_RET
Petr Machata929bd572012-12-17 03:20:34 +0100592void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200593{
594}
595#endif
596
Petr Machata14298742012-04-12 23:09:21 +0200597static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100598handle_breakpoint(Event *event)
599{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100600 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100601 struct breakpoint *sbp;
Petr Machata929bd572012-12-17 03:20:34 +0100602 struct process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200603 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200604
605 /* The leader has terminated. */
606 if (leader == NULL) {
607 continue_process(event->proc->pid);
608 return;
609 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100610
Petr Machata31b2f9f2012-04-12 22:23:40 +0200611 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
612 event->proc->pid, brk_addr);
613 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100614
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200616 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100617 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100618 callstack_pop(event->proc);
619 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200620 if (event->proc->state != STATE_IGNORED) {
621 if (opt_T || options.summary) {
622 calc_time_spent(event->proc);
623 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100624 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200625 event->proc->return_addr = brk_addr;
626
Petr Machata9e1e9692012-04-19 02:29:38 +0200627 struct library_symbol *libsym =
628 event->proc->callstack[i].c_un.libfunc;
629
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200630 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200631 output_right_tos(event->proc);
632 callstack_pop(event->proc);
633
Petr Machata31b2f9f2012-04-12 22:23:40 +0200634 /* Pop also any other entries that seem like
635 * they are linked to the current one: they
636 * have the same return address, but were made
637 * for different symbols. This should only
638 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200639 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200640 while (event->proc->callstack_depth > 0) {
641 struct callstack_element *prev;
642 size_t d = event->proc->callstack_depth;
643 prev = &event->proc->callstack[d - 1];
644
Petr Machata14298742012-04-12 23:09:21 +0200645 if (prev->c_un.libfunc == libsym
646 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200647 break;
Petr Machata14298742012-04-12 23:09:21 +0200648
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200649 arch_symbol_ret(event->proc,
650 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200651 output_right_tos(event->proc);
652 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200653 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200654
Petr Machata5ee36822012-04-19 17:01:51 +0200655 /* Maybe the previous callstack_pop's got rid
656 * of the breakpoint, but if we are in a
657 * recursive call, it's still enabled. In
658 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200659 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200660 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200661 } else {
662 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200663 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200664 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100665 return;
666 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100667 }
668
Petr Machata5ee36822012-04-19 17:01:51 +0200669 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100670 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200671 else if (event->proc->state != STATE_IGNORED)
672 output_line(event->proc,
673 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100674
Petr Machata5ee36822012-04-19 17:01:51 +0200675 /* breakpoint_on_hit may delete its own breakpoint, so we have
676 * to look it up again. */
677 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100678 if (event->proc->state != STATE_IGNORED
679 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200680 event->proc->stack_pointer = get_stack_pointer(event->proc);
681 event->proc->return_addr =
682 get_return_addr(event->proc, event->proc->stack_pointer);
Juan Cespedes5c682042009-05-21 15:59:56 +0200683 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100684 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200685 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100686
Petr Machata56a9ea62012-03-27 03:09:29 +0200687 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100688 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200689 } else {
690 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100691 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100692
Juan Cespedes5e01f651998-03-08 22:31:44 +0100693 continue_process(event->proc->pid);
694}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200695
Juan Cespedesf1350522008-12-16 18:19:58 +0100696static void
Petr Machata929bd572012-12-17 03:20:34 +0100697callstack_push_syscall(struct process *proc, int sysnum)
698{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100699 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_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
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
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100709 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 = 1;
712 elem->c_un.syscall = sysnum;
713 elem->return_addr = NULL;
714
715 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200716 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100717 struct timezone tz;
718 gettimeofday(&elem->time_spent, &tz);
719 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200720}
721
Juan Cespedes21c63a12001-07-07 20:56:56 +0200722static void
Petr Machata929bd572012-12-17 03:20:34 +0100723callstack_push_symfunc(struct process *proc, struct library_symbol *sym)
724{
Petr Machata14298742012-04-12 23:09:21 +0200725 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200726
Juan Cespedescd8976d2009-05-14 13:47:58 +0200727 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200728 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100729 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500730 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
731 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200732 return;
733 }
734
Petr Machata14298742012-04-12 23:09:21 +0200735 elem = &proc->callstack[proc->callstack_depth++];
Petr Machata8ae36732012-04-30 01:19:09 +0200736 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200737 elem->is_syscall = 0;
738 elem->c_un.libfunc = sym;
739
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200740 elem->return_addr = proc->return_addr;
Petr Machata9df15012012-02-20 12:49:46 +0100741 if (elem->return_addr)
742 insert_breakpoint(proc, elem->return_addr, NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200743
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
Petr Machatae655ccf2012-10-26 22:21:59 +0200750void
Petr Machata929bd572012-12-17 03:20:34 +0100751callstack_pop(struct process *proc)
Petr Machatae655ccf2012-10-26 22:21:59 +0200752{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100753 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200754 assert(proc->callstack_depth > 0);
755
Juan Cespedescd8976d2009-05-14 13:47:58 +0200756 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100757 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatae655ccf2012-10-26 22:21:59 +0200758 if (!elem->is_syscall && elem->return_addr)
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200759 delete_breakpoint(proc, elem->return_addr);
Petr Machatae655ccf2012-10-26 22:21:59 +0200760
761 if (elem->fetch_context != NULL)
762 fetch_arg_done(elem->fetch_context);
763
764 if (elem->arguments != NULL) {
765 val_dict_destroy(elem->arguments);
766 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200767 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200768
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200769 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200770}