blob: ae61ade561ca4d3f25738e90fbf8a874d10f2a62 [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);
60static void callstack_push_symfunc(struct process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 struct library_symbol *sym);
Petr Machatae655ccf2012-10-26 22:21:59 +020062/* XXX Stack maintenance should be moved to a dedicated module, or to
63 * proc.c, and push/pop should be visible outside this module. For
64 * now, because we need this in proc.c, this is non-static. */
Petr Machata929bd572012-12-17 03:20:34 +010065void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020066
Petr Machata929bd572012-12-17 03:20:34 +010067static char *shortsignal(struct process *proc, int signum);
68static char *sysname(struct process *proc, int sysnum);
69static char *arch_sysname(struct process *proc, int sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +020070
Petr Machatacbe29c62011-09-27 02:27:58 +020071static Event *
Petr Machata929bd572012-12-17 03:20:34 +010072call_handler(struct process *proc, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +020073{
74 assert(proc != NULL);
75
Petr Machata366c2f42012-02-09 19:34:36 +010076 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020077 if (handler == NULL)
78 return event;
79
80 return (*handler->on_event) (handler, event);
81}
82
Juan Cespedes61da3372009-07-03 11:55:44 +020083void
Petr Machataffe4cd22012-04-11 18:01:44 +020084handle_event(Event *event)
85{
Petr Machata602330f2011-07-09 11:15:34 +020086 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020087 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020088 os_ltrace_exiting();
89 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020090 }
Petr Machata26627682011-07-08 18:15:32 +020091 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
92 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020093
94 /* If the thread group or an individual task define an
95 overriding event handler, give them a chance to kick in.
96 We will end up calling both handlers, if the first one
97 doesn't sink the event. */
98 if (event->proc != NULL) {
99 event = call_handler(event->proc, event);
100 if (event == NULL)
101 /* It was handled. */
102 return;
103
104 /* Note: the previous handler has a chance to alter
105 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100106 if (event->proc != NULL
107 && event->proc->leader != NULL
108 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200109 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200110 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200111 return;
112 }
113 }
114
Juan Cespedes61da3372009-07-03 11:55:44 +0200115 switch (event->type) {
116 case EVENT_NONE:
117 debug(1, "event: none");
118 return;
Petr Machata9978de42013-01-08 23:46:22 +0100119
Juan Cespedes61da3372009-07-03 11:55:44 +0200120 case EVENT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100121 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200122 debug(1, "[%d] event: signal (%s [%d])",
123 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200124 shortsignal(event->proc, event->e_un.signum),
125 event->e_un.signum);
126 handle_signal(event);
127 return;
Petr Machata9978de42013-01-08 23:46:22 +0100128
Juan Cespedes61da3372009-07-03 11:55:44 +0200129 case EVENT_EXIT:
Petr Machata9978de42013-01-08 23:46:22 +0100130 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200131 debug(1, "[%d] event: exit (%d)",
132 event->proc->pid,
133 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200134 handle_exit(event);
135 return;
Petr Machata9978de42013-01-08 23:46:22 +0100136
Juan Cespedes61da3372009-07-03 11:55:44 +0200137 case EVENT_EXIT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100138 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200139 debug(1, "[%d] event: exit signal (%s [%d])",
140 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200141 shortsignal(event->proc, event->e_un.signum),
142 event->e_un.signum);
143 handle_exit_signal(event);
144 return;
Petr Machata9978de42013-01-08 23:46:22 +0100145
Juan Cespedes61da3372009-07-03 11:55:44 +0200146 case EVENT_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100147 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200148 debug(1, "[%d] event: syscall (%s [%d])",
149 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200150 sysname(event->proc, event->e_un.sysnum),
151 event->e_un.sysnum);
152 handle_syscall(event);
153 return;
Petr Machata9978de42013-01-08 23:46:22 +0100154
Juan Cespedes61da3372009-07-03 11:55:44 +0200155 case EVENT_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100156 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200157 debug(1, "[%d] event: sysret (%s [%d])",
158 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200159 sysname(event->proc, event->e_un.sysnum),
160 event->e_un.sysnum);
161 handle_sysret(event);
162 return;
Petr Machata9978de42013-01-08 23:46:22 +0100163
Juan Cespedes61da3372009-07-03 11:55:44 +0200164 case EVENT_ARCH_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100165 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200166 debug(1, "[%d] event: arch_syscall (%s [%d])",
167 event->proc->pid,
168 arch_sysname(event->proc, event->e_un.sysnum),
169 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200170 handle_arch_syscall(event);
171 return;
Petr Machata9978de42013-01-08 23:46:22 +0100172
Juan Cespedes61da3372009-07-03 11:55:44 +0200173 case EVENT_ARCH_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100174 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200175 debug(1, "[%d] event: arch_sysret (%s [%d])",
176 event->proc->pid,
177 arch_sysname(event->proc, event->e_un.sysnum),
178 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200179 handle_arch_sysret(event);
180 return;
Petr Machata9978de42013-01-08 23:46:22 +0100181
Juan Cespedes61da3372009-07-03 11:55:44 +0200182 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200183 case EVENT_VFORK:
Petr Machata9978de42013-01-08 23:46:22 +0100184 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200185 debug(1, "[%d] event: clone (%u)",
186 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200187 handle_clone(event);
188 return;
Petr Machata9978de42013-01-08 23:46:22 +0100189
Juan Cespedes61da3372009-07-03 11:55:44 +0200190 case EVENT_EXEC:
Petr Machata9978de42013-01-08 23:46:22 +0100191 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200192 debug(1, "[%d] event: exec()",
193 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200194 handle_exec(event);
195 return;
Petr Machata9978de42013-01-08 23:46:22 +0100196
Juan Cespedes61da3372009-07-03 11:55:44 +0200197 case EVENT_BREAKPOINT:
Petr Machata9978de42013-01-08 23:46:22 +0100198 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200199 debug(1, "[%d] event: breakpoint %p",
200 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200201 handle_breakpoint(event);
202 return;
Petr Machata9978de42013-01-08 23:46:22 +0100203
Juan Cespedes61da3372009-07-03 11:55:44 +0200204 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200205 debug(1, "[%d] event: new process",
206 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200207 handle_new(event);
208 return;
209 default:
210 fprintf(stderr, "Error! unknown event?\n");
211 exit(1);
212 }
213}
214
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200215typedef struct Pending_New Pending_New;
216struct Pending_New {
217 pid_t pid;
218 Pending_New * next;
219};
220static Pending_New * pending_news = NULL;
221
222static int
223pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200224 Pending_New * p;
225
226 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
227
228 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200229 while (p) {
230 if (p->pid == pid) {
231 return 1;
232 }
233 p = p->next;
234 }
235 return 0;
236}
237
238static void
239pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200240 Pending_New * p;
241
242 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
243
244 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200245 if (!p) {
246 perror("malloc()");
247 exit(1);
248 }
249 p->pid = pid;
250 p->next = pending_news;
251 pending_news = p;
252}
253
254static void
Petr Machatac1f1bf42013-01-09 22:39:41 +0100255pending_new_remove(pid_t pid)
256{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200257 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
258
Petr Machatac1f1bf42013-01-09 22:39:41 +0100259 Pending_New **pp;
260 for (pp = &pending_news; *pp != NULL; pp = &(*pp)->next)
261 if ((*pp)->pid == pid) {
262 Pending_New *p = *pp;
263 *pp = p->next;
264 free(p);
265 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200266 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200267}
268
269static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270handle_clone(Event *event)
271{
Juan Cespedes03192f82009-07-03 10:16:22 +0200272 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200273
Petr Machata929bd572012-12-17 03:20:34 +0100274 struct process *proc = malloc(sizeof(*proc));
Petr Machata6095c132013-01-07 19:08:18 +0100275 pid_t newpid = event->e_un.newpid;
276 if (proc == NULL
277 || process_clone(proc, event->proc, newpid) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100278 free(proc);
Petr Machata6095c132013-01-07 19:08:18 +0100279 proc = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100280 fprintf(stderr,
Petr Machata6095c132013-01-07 19:08:18 +0100281 "Couldn't initialize tracing of process %d.\n",
282 newpid);
283
284 } else {
285 proc->parent = event->proc;
286 /* We save register values to the arch pointer, and
287 * these need to be per-thread. XXX arch_ptr should
288 * be retired in favor of fetch interface anyway. */
289 proc->arch_ptr = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200290 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291
Petr Machata6095c132013-01-07 19:08:18 +0100292 if (pending_new(newpid)) {
293 pending_new_remove(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200294
Petr Machata6095c132013-01-07 19:08:18 +0100295 if (proc != NULL) {
296 proc->event_handler = NULL;
297 if (event->proc->state == STATE_ATTACHED
298 && options.follow)
299 proc->state = STATE_ATTACHED;
300 else
301 proc->state = STATE_IGNORED;
302 }
Petr Machata75dcf7d2011-10-06 14:30:19 +0200303
Petr Machata6095c132013-01-07 19:08:18 +0100304 continue_process(newpid);
305
306 } else if (proc != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100307 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200308 }
Petr Machata534e00f2011-09-27 17:58:38 +0200309
Petr Machata6095c132013-01-07 19:08:18 +0100310 if (event->type != EVENT_VFORK)
311 continue_process(event->proc->pid);
312 else if (proc != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100313 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200314 else
Petr Machata6095c132013-01-07 19:08:18 +0100315 continue_process(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200316}
317
318static void
Petr Machata929bd572012-12-17 03:20:34 +0100319handle_new(Event *event)
320{
Juan Cespedes03192f82009-07-03 10:16:22 +0200321 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200322
Petr Machata929bd572012-12-17 03:20:34 +0100323 struct process *proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200324 if (!proc) {
325 pending_new_insert(event->e_un.newpid);
326 } else {
327 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200328 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200329 proc->state = STATE_ATTACHED;
330 } else {
331 proc->state = STATE_IGNORED;
332 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200333 continue_process(proc->pid);
334 }
335}
336
Juan Cespedesf1350522008-12-16 18:19:58 +0100337static char *
Petr Machata929bd572012-12-17 03:20:34 +0100338shortsignal(struct process *proc, int signum)
339{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100340 static char *signalent0[] = {
341#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100342 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 static char *signalent1[] = {
344#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100345 };
346 static char **signalents[] = { signalent0, signalent1 };
347 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 sizeof signalent1 / sizeof signalent1[0]
349 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100350
Juan Cespedescd8976d2009-05-14 13:47:58 +0200351 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
352
Petr Machata8e682c22013-01-09 16:20:20 +0100353 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100354 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100355 return "UNKNOWN_SIGNAL";
356 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100357 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100358 }
359}
360
Juan Cespedesf1350522008-12-16 18:19:58 +0100361static char *
Petr Machata929bd572012-12-17 03:20:34 +0100362sysname(struct process *proc, int sysnum)
363{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100364 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100365 static char *syscallent0[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100366#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100367 };
Petr Machata8e682c22013-01-09 16:20:20 +0100368 static char *syscallent1[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100370 };
Petr Machata8e682c22013-01-09 16:20:20 +0100371 static char **syscallents[] = { syscallent0, syscallent1 };
372 int nsyscalls[] = {
373 sizeof syscallent0 / sizeof syscallent0[0],
374 sizeof syscallent1 / sizeof syscallent1[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100376
Juan Cespedescd8976d2009-05-14 13:47:58 +0200377 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
378
Petr Machata8e682c22013-01-09 16:20:20 +0100379 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
380 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100381 sprintf(result, "SYS_%d", sysnum);
382 return result;
383 } else {
Petr Machata82f748d2013-10-23 00:39:23 +0200384 return syscallents[proc->personality][sysnum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100385 }
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 Machata82f748d2013-10-23 00:39:23 +0200448 static struct library syscall_lib;
449 if (syscall_lib.protolib == NULL) {
450 struct protolib *protolib
451 = protolib_cache_search(&g_protocache, "syscalls", 0, 1);
452 if (protolib == NULL) {
453 fprintf(stderr, "Couldn't load system call prototypes:"
454 " %s.\n", strerror(errno));
455
456 /* Instead, get a fake one just so we can
457 * carry on, limping. */
458 protolib = malloc(sizeof *protolib);
459 if (protolib == NULL) {
460 fprintf(stderr, "Couldn't even allocate a fake "
461 "prototype library: %s.\n",
462 strerror(errno));
463 abort();
464 }
465 protolib_init(protolib);
466 }
467
468 assert(protolib != NULL);
469 if (library_init(&syscall_lib, LT_LIBTYPE_SYSCALL) < 0) {
470 fprintf(stderr, "Couldn't initialize system call "
471 "library: %s.\n", strerror(errno));
472 abort();
473 }
474
475 library_set_soname(&syscall_lib, "SYS", 0);
476 syscall_lib.protolib = protolib;
477 }
478
Petr Machataa32ef7c2012-04-23 18:05:24 +0200479 struct library_symbol syscall;
480 if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
Petr Machata82f748d2013-10-23 00:39:23 +0200481 syscall.lib = &syscall_lib;
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200482 (*output)(tof, proc, &syscall);
Petr Machataa32ef7c2012-04-23 18:05:24 +0200483 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100484 }
Petr Machata29add4f2012-02-18 16:38:05 +0100485}
486
487static void
Petr Machata929bd572012-12-17 03:20:34 +0100488output_syscall_left(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100489{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200490 output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
Petr Machata29add4f2012-02-18 16:38:05 +0100491}
492
493static void
Petr Machata929bd572012-12-17 03:20:34 +0100494output_syscall_right(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100495{
Petr Machata1e0b9dc2012-04-28 15:58:43 +0200496 output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
Petr Machata29add4f2012-02-18 16:38:05 +0100497}
498
Juan Cespedesf1350522008-12-16 18:19:58 +0100499static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200500handle_syscall(Event *event) {
501 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200502 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100503 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100504 if (options.syscalls)
505 output_syscall_left(event->proc,
506 sysname(event->proc,
507 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100508 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100509 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100510}
511
Juan Cespedesf1350522008-12-16 18:19:58 +0100512static void
Petr Machata929bd572012-12-17 03:20:34 +0100513handle_exec(Event *event)
514{
515 struct process *proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200516
Petr Machata3d0c91c2012-04-14 02:37:38 +0200517 /* Save the PID so that we can use it after unsuccessful
518 * process_exec. */
519 pid_t pid = proc->pid;
520
Juan Cespedes03192f82009-07-03 10:16:22 +0200521 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200522 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200523 untrace:
524 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200525 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200526 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200527 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200528 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200529
530 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200531 fprintf(stderr,
532 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200533 goto untrace;
534 }
535
Petr Machata057caa52013-01-30 23:28:47 +0100536 continue_after_exec(proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200537}
538
539static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200540handle_arch_syscall(Event *event) {
541 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200542 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100543 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200544 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100545 output_syscall_left(event->proc,
546 arch_sysname(event->proc,
547 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200548 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100549 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100550 continue_process(event->proc->pid);
551}
552
Juan Cespedesd65efa32003-02-03 00:22:30 +0100553struct timeval current_time_spent;
554
Juan Cespedesf1350522008-12-16 18:19:58 +0100555static void
Petr Machata929bd572012-12-17 03:20:34 +0100556calc_time_spent(struct process *proc)
557{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100558 struct timeval tv;
559 struct timezone tz;
560 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100562
Juan Cespedescd8976d2009-05-14 13:47:58 +0200563 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100564 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100565
566 gettimeofday(&tv, &tz);
567
568 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
569 if (tv.tv_usec >= elem->time_spent.tv_usec) {
570 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
571 } else {
Paul Buerger6aa01522012-09-12 10:58:52 -0400572 diff.tv_sec--;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100573 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
574 }
575 current_time_spent = diff;
576}
577
Juan Cespedesf1350522008-12-16 18:19:58 +0100578static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200579handle_sysret(Event *event) {
580 debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200581 if (event->proc->state != STATE_IGNORED) {
582 if (opt_T || options.summary) {
583 calc_time_spent(event->proc);
584 }
Petr Machata29add4f2012-02-18 16:38:05 +0100585 if (options.syscalls)
586 output_syscall_right(event->proc,
587 sysname(event->proc,
588 event->e_un.sysnum));
589
Petr Machata43d2fe52011-11-02 13:25:49 +0100590 assert(event->proc->callstack_depth > 0);
591 unsigned d = event->proc->callstack_depth - 1;
592 assert(event->proc->callstack[d].is_syscall);
Petr Machata211f0882010-11-03 18:42:18 +0100593 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200594 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100595 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100596}
597
Juan Cespedesf1350522008-12-16 18:19:58 +0100598static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200599handle_arch_sysret(Event *event) {
600 debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200601 if (event->proc->state != STATE_IGNORED) {
602 if (opt_T || options.summary) {
603 calc_time_spent(event->proc);
604 }
Petr Machata29add4f2012-02-18 16:38:05 +0100605 if (options.syscalls)
606 output_syscall_right(event->proc,
607 arch_sysname(event->proc,
608 event->e_un.sysnum));
Petr Machata211f0882010-11-03 18:42:18 +0100609 callstack_pop(event->proc);
Juan Cespedes63184be2008-12-10 13:30:12 +0100610 }
611 continue_process(event->proc->pid);
612}
613
Juan Cespedesf1350522008-12-16 18:19:58 +0100614static void
Petr Machata929bd572012-12-17 03:20:34 +0100615output_right_tos(struct process *proc)
Petr Machata14298742012-04-12 23:09:21 +0200616{
617 size_t d = proc->callstack_depth;
618 struct callstack_element *elem = &proc->callstack[d - 1];
619 if (proc->state != STATE_IGNORED)
Petr Machata9e1e9692012-04-19 02:29:38 +0200620 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200621}
622
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200623#ifndef ARCH_HAVE_SYMBOL_RET
Petr Machata929bd572012-12-17 03:20:34 +0100624void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200625{
626}
627#endif
628
Petr Machata14298742012-04-12 23:09:21 +0200629static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100630handle_breakpoint(Event *event)
631{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100632 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100633 struct breakpoint *sbp;
Petr Machata929bd572012-12-17 03:20:34 +0100634 struct process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200635 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200636
637 /* The leader has terminated. */
638 if (leader == NULL) {
639 continue_process(event->proc->pid);
640 return;
641 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100642
Petr Machata31b2f9f2012-04-12 22:23:40 +0200643 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
644 event->proc->pid, brk_addr);
645 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100646
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100647 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200648 if (brk_addr == event->proc->callstack[i].return_addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100649 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100650 callstack_pop(event->proc);
651 }
Juan Cespedes5c682042009-05-21 15:59:56 +0200652 if (event->proc->state != STATE_IGNORED) {
653 if (opt_T || options.summary) {
654 calc_time_spent(event->proc);
655 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100656 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200657
Petr Machata9e1e9692012-04-19 02:29:38 +0200658 struct library_symbol *libsym =
659 event->proc->callstack[i].c_un.libfunc;
660
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200661 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200662 output_right_tos(event->proc);
663 callstack_pop(event->proc);
664
Petr Machata31b2f9f2012-04-12 22:23:40 +0200665 /* Pop also any other entries that seem like
666 * they are linked to the current one: they
667 * have the same return address, but were made
668 * for different symbols. This should only
669 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200670 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200671 while (event->proc->callstack_depth > 0) {
672 struct callstack_element *prev;
673 size_t d = event->proc->callstack_depth;
674 prev = &event->proc->callstack[d - 1];
675
Petr Machata14298742012-04-12 23:09:21 +0200676 if (prev->c_un.libfunc == libsym
677 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200678 break;
Petr Machata14298742012-04-12 23:09:21 +0200679
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200680 arch_symbol_ret(event->proc,
681 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200682 output_right_tos(event->proc);
683 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200684 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200685
Petr Machata5ee36822012-04-19 17:01:51 +0200686 /* Maybe the previous callstack_pop's got rid
687 * of the breakpoint, but if we are in a
688 * recursive call, it's still enabled. In
689 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200690 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200691 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200692 } else {
693 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200694 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200695 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100696 return;
697 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100698 }
699
Petr Machata5ee36822012-04-19 17:01:51 +0200700 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100701 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200702 else if (event->proc->state != STATE_IGNORED)
703 output_line(event->proc,
704 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100705
Petr Machata5ee36822012-04-19 17:01:51 +0200706 /* breakpoint_on_hit may delete its own breakpoint, so we have
707 * to look it up again. */
708 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100709 if (event->proc->state != STATE_IGNORED
710 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200711 event->proc->stack_pointer = get_stack_pointer(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200712 callstack_push_symfunc(event->proc, sbp->libsym);
Petr Machata29add4f2012-02-18 16:38:05 +0100713 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200714 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100715
Petr Machata56a9ea62012-03-27 03:09:29 +0200716 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100717 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200718 } else {
719 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100720 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100721
Juan Cespedes5e01f651998-03-08 22:31:44 +0100722 continue_process(event->proc->pid);
723}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200724
Juan Cespedesf1350522008-12-16 18:19:58 +0100725static void
Petr Machata929bd572012-12-17 03:20:34 +0100726callstack_push_syscall(struct process *proc, int sysnum)
727{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100728 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200729
Juan Cespedescd8976d2009-05-14 13:47:58 +0200730 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200731 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100732 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500733 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
734 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200735 return;
736 }
737
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100738 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200739 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200740 elem->is_syscall = 1;
741 elem->c_un.syscall = sysnum;
742 elem->return_addr = NULL;
743
744 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200745 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100746 struct timezone tz;
747 gettimeofday(&elem->time_spent, &tz);
748 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200749}
750
Juan Cespedes21c63a12001-07-07 20:56:56 +0200751static void
Petr Machata929bd572012-12-17 03:20:34 +0100752callstack_push_symfunc(struct process *proc, struct library_symbol *sym)
753{
Petr Machata14298742012-04-12 23:09:21 +0200754 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200755
Juan Cespedescd8976d2009-05-14 13:47:58 +0200756 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->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;
767 elem->c_un.libfunc = sym;
768
Petr Machataf9d93c52013-01-30 23:09:02 +0100769 arch_addr_t return_addr = get_return_addr(proc, proc->stack_pointer);
770 struct breakpoint *rbp = NULL;
771 if (return_addr != 0)
Petr Machata02a796e2013-10-11 17:24:30 +0200772 rbp = insert_breakpoint_at(proc, return_addr, NULL);
Petr Machataf9d93c52013-01-30 23:09:02 +0100773 elem->return_addr = rbp != NULL ? rbp->addr : 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200774
Juan Cespedesda9b9532009-04-07 15:33:50 +0200775 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100776 struct timezone tz;
777 gettimeofday(&elem->time_spent, &tz);
778 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200779}
780
Petr Machatae655ccf2012-10-26 22:21:59 +0200781void
Petr Machata929bd572012-12-17 03:20:34 +0100782callstack_pop(struct process *proc)
Petr Machatae655ccf2012-10-26 22:21:59 +0200783{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100784 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200785 assert(proc->callstack_depth > 0);
786
Juan Cespedescd8976d2009-05-14 13:47:58 +0200787 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100788 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatae655ccf2012-10-26 22:21:59 +0200789 if (!elem->is_syscall && elem->return_addr)
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200790 delete_breakpoint(proc, elem->return_addr);
Petr Machatae655ccf2012-10-26 22:21:59 +0200791
792 if (elem->fetch_context != NULL)
793 fetch_arg_done(elem->fetch_context);
794
795 if (elem->arguments != NULL) {
796 val_dict_destroy(elem->arguments);
797 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200798 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200799
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200800 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200801}