blob: 6fa7e98da6de4bbd35a2cfe1a379370a8d50d972 [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>
Petr Machata82f748d2013-10-23 00:39:23 +020035#include <stdbool.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 Machata82f748d2013-10-23 00:39:23 +020043#include "prototype.h"
Petr Machata8a730f32013-11-21 20:43:51 +010044#include "summary.h"
45#include "value_dict.h"
Petr Machatafed1e8d2012-02-07 02:06:29 +010046
Juan Cespedes03192f82009-07-03 10:16:22 +020047static void handle_signal(Event *event);
48static void handle_exit(Event *event);
49static void handle_exit_signal(Event *event);
50static void handle_syscall(Event *event);
51static void handle_arch_syscall(Event *event);
52static void handle_sysret(Event *event);
53static void handle_arch_sysret(Event *event);
54static void handle_clone(Event *event);
55static void handle_exec(Event *event);
56static void handle_breakpoint(Event *event);
57static void handle_new(Event *event);
Juan Cespedes5e01f651998-03-08 22:31:44 +010058
Petr Machata929bd572012-12-17 03:20:34 +010059static void callstack_push_syscall(struct process *proc, int sysnum);
Petr Machatacf989232013-10-11 21:17:24 +020060static void callstack_push_symfunc(struct process *proc, struct breakpoint *bp);
Petr Machatae655ccf2012-10-26 22:21:59 +020061/* XXX Stack maintenance should be moved to a dedicated module, or to
62 * proc.c, and push/pop should be visible outside this module. For
63 * now, because we need this in proc.c, this is non-static. */
Petr Machata929bd572012-12-17 03:20:34 +010064void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065
Petr Machata929bd572012-12-17 03:20:34 +010066static char *shortsignal(struct process *proc, int signum);
67static char *sysname(struct process *proc, int sysnum);
68static char *arch_sysname(struct process *proc, int sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +020069
Petr Machatacbe29c62011-09-27 02:27:58 +020070static Event *
Petr Machata929bd572012-12-17 03:20:34 +010071call_handler(struct process *proc, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +020072{
73 assert(proc != NULL);
74
Petr Machata366c2f42012-02-09 19:34:36 +010075 struct event_handler *handler = proc->event_handler;
Petr Machatacbe29c62011-09-27 02:27:58 +020076 if (handler == NULL)
77 return event;
78
79 return (*handler->on_event) (handler, event);
80}
81
Juan Cespedes61da3372009-07-03 11:55:44 +020082void
Petr Machataffe4cd22012-04-11 18:01:44 +020083handle_event(Event *event)
84{
Petr Machata602330f2011-07-09 11:15:34 +020085 if (exiting == 1) {
Petr Machata602330f2011-07-09 11:15:34 +020086 debug(1, "ltrace about to exit");
Petr Machataffe4cd22012-04-11 18:01:44 +020087 os_ltrace_exiting();
88 exiting = 2;
Petr Machata602330f2011-07-09 11:15:34 +020089 }
Petr Machata26627682011-07-08 18:15:32 +020090 debug(DEBUG_FUNCTION, "handle_event(pid=%d, type=%d)",
91 event->proc ? event->proc->pid : -1, event->type);
Petr Machatacbe29c62011-09-27 02:27:58 +020092
93 /* If the thread group or an individual task define an
94 overriding event handler, give them a chance to kick in.
95 We will end up calling both handlers, if the first one
96 doesn't sink the event. */
97 if (event->proc != NULL) {
98 event = call_handler(event->proc, event);
99 if (event == NULL)
100 /* It was handled. */
101 return;
102
103 /* Note: the previous handler has a chance to alter
104 * the event. */
Petr Machata43d2fe52011-11-02 13:25:49 +0100105 if (event->proc != NULL
106 && event->proc->leader != NULL
107 && event->proc != event->proc->leader) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200108 event = call_handler(event->proc->leader, event);
Petr Machata4007d742011-07-09 11:29:42 +0200109 if (event == NULL)
Petr Machata4007d742011-07-09 11:29:42 +0200110 return;
111 }
112 }
113
Juan Cespedes61da3372009-07-03 11:55:44 +0200114 switch (event->type) {
115 case EVENT_NONE:
116 debug(1, "event: none");
117 return;
Petr Machata9978de42013-01-08 23:46:22 +0100118
Juan Cespedes61da3372009-07-03 11:55:44 +0200119 case EVENT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100120 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200121 debug(1, "[%d] event: signal (%s [%d])",
122 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200123 shortsignal(event->proc, event->e_un.signum),
124 event->e_un.signum);
125 handle_signal(event);
126 return;
Petr Machata9978de42013-01-08 23:46:22 +0100127
Juan Cespedes61da3372009-07-03 11:55:44 +0200128 case EVENT_EXIT:
Petr Machata9978de42013-01-08 23:46:22 +0100129 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200130 debug(1, "[%d] event: exit (%d)",
131 event->proc->pid,
132 event->e_un.ret_val);
Juan Cespedes61da3372009-07-03 11:55:44 +0200133 handle_exit(event);
134 return;
Petr Machata9978de42013-01-08 23:46:22 +0100135
Juan Cespedes61da3372009-07-03 11:55:44 +0200136 case EVENT_EXIT_SIGNAL:
Petr Machata9978de42013-01-08 23:46:22 +0100137 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200138 debug(1, "[%d] event: exit signal (%s [%d])",
139 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200140 shortsignal(event->proc, event->e_un.signum),
141 event->e_un.signum);
142 handle_exit_signal(event);
143 return;
Petr Machata9978de42013-01-08 23:46:22 +0100144
Juan Cespedes61da3372009-07-03 11:55:44 +0200145 case EVENT_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100146 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200147 debug(1, "[%d] event: syscall (%s [%d])",
148 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200149 sysname(event->proc, event->e_un.sysnum),
150 event->e_un.sysnum);
151 handle_syscall(event);
152 return;
Petr Machata9978de42013-01-08 23:46:22 +0100153
Juan Cespedes61da3372009-07-03 11:55:44 +0200154 case EVENT_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100155 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200156 debug(1, "[%d] event: sysret (%s [%d])",
157 event->proc->pid,
Juan Cespedes61da3372009-07-03 11:55:44 +0200158 sysname(event->proc, event->e_un.sysnum),
159 event->e_un.sysnum);
160 handle_sysret(event);
161 return;
Petr Machata9978de42013-01-08 23:46:22 +0100162
Juan Cespedes61da3372009-07-03 11:55:44 +0200163 case EVENT_ARCH_SYSCALL:
Petr Machata9978de42013-01-08 23:46:22 +0100164 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200165 debug(1, "[%d] event: arch_syscall (%s [%d])",
166 event->proc->pid,
167 arch_sysname(event->proc, event->e_un.sysnum),
168 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200169 handle_arch_syscall(event);
170 return;
Petr Machata9978de42013-01-08 23:46:22 +0100171
Juan Cespedes61da3372009-07-03 11:55:44 +0200172 case EVENT_ARCH_SYSRET:
Petr Machata9978de42013-01-08 23:46:22 +0100173 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200174 debug(1, "[%d] event: arch_sysret (%s [%d])",
175 event->proc->pid,
176 arch_sysname(event->proc, event->e_un.sysnum),
177 event->e_un.sysnum);
Juan Cespedes61da3372009-07-03 11:55:44 +0200178 handle_arch_sysret(event);
179 return;
Petr Machata9978de42013-01-08 23:46:22 +0100180
Juan Cespedes61da3372009-07-03 11:55:44 +0200181 case EVENT_CLONE:
Petr Machatacbe29c62011-09-27 02:27:58 +0200182 case EVENT_VFORK:
Petr Machata9978de42013-01-08 23:46:22 +0100183 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200184 debug(1, "[%d] event: clone (%u)",
185 event->proc->pid, event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200186 handle_clone(event);
187 return;
Petr Machata9978de42013-01-08 23:46:22 +0100188
Juan Cespedes61da3372009-07-03 11:55:44 +0200189 case EVENT_EXEC:
Petr Machata9978de42013-01-08 23:46:22 +0100190 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200191 debug(1, "[%d] event: exec()",
192 event->proc->pid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200193 handle_exec(event);
194 return;
Petr Machata9978de42013-01-08 23:46:22 +0100195
Juan Cespedes61da3372009-07-03 11:55:44 +0200196 case EVENT_BREAKPOINT:
Petr Machata9978de42013-01-08 23:46:22 +0100197 assert(event->proc != NULL);
Petr Machataebfe7d62012-04-13 21:34:49 +0200198 debug(1, "[%d] event: breakpoint %p",
199 event->proc->pid, event->e_un.brk_addr);
Juan Cespedes61da3372009-07-03 11:55:44 +0200200 handle_breakpoint(event);
201 return;
Petr Machata9978de42013-01-08 23:46:22 +0100202
Juan Cespedes61da3372009-07-03 11:55:44 +0200203 case EVENT_NEW:
Petr Machataebfe7d62012-04-13 21:34:49 +0200204 debug(1, "[%d] event: new process",
205 event->e_un.newpid);
Juan Cespedes61da3372009-07-03 11:55:44 +0200206 handle_new(event);
207 return;
208 default:
209 fprintf(stderr, "Error! unknown event?\n");
210 exit(1);
211 }
212}
213
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200214typedef struct Pending_New Pending_New;
215struct Pending_New {
216 pid_t pid;
217 Pending_New * next;
218};
219static Pending_New * pending_news = NULL;
220
221static int
222pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200223 Pending_New * p;
224
225 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
226
227 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200228 while (p) {
229 if (p->pid == pid) {
230 return 1;
231 }
232 p = p->next;
233 }
234 return 0;
235}
236
237static void
238pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200239 Pending_New * p;
240
241 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
242
243 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200244 if (!p) {
245 perror("malloc()");
246 exit(1);
247 }
248 p->pid = pid;
249 p->next = pending_news;
250 pending_news = p;
251}
252
253static void
Petr Machatac1f1bf42013-01-09 22:39:41 +0100254pending_new_remove(pid_t pid)
255{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200256 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
257
Petr Machatac1f1bf42013-01-09 22:39:41 +0100258 Pending_New **pp;
259 for (pp = &pending_news; *pp != NULL; pp = &(*pp)->next)
260 if ((*pp)->pid == pid) {
261 Pending_New *p = *pp;
262 *pp = p->next;
263 free(p);
264 return;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200265 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200266}
267
268static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269handle_clone(Event *event)
270{
Juan Cespedes03192f82009-07-03 10:16:22 +0200271 debug(DEBUG_FUNCTION, "handle_clone(pid=%d)", event->proc->pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200272
Petr Machata929bd572012-12-17 03:20:34 +0100273 struct process *proc = malloc(sizeof(*proc));
Petr Machata6095c132013-01-07 19:08:18 +0100274 pid_t newpid = event->e_un.newpid;
275 if (proc == NULL
276 || process_clone(proc, event->proc, newpid) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 free(proc);
Petr Machata6095c132013-01-07 19:08:18 +0100278 proc = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100279 fprintf(stderr,
Petr Machata6095c132013-01-07 19:08:18 +0100280 "Couldn't initialize tracing of process %d.\n",
281 newpid);
282
283 } else {
284 proc->parent = event->proc;
285 /* We save register values to the arch pointer, and
286 * these need to be per-thread. XXX arch_ptr should
287 * be retired in favor of fetch interface anyway. */
288 proc->arch_ptr = NULL;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200289 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290
Petr Machata6095c132013-01-07 19:08:18 +0100291 if (pending_new(newpid)) {
292 pending_new_remove(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200293
Petr Machata6095c132013-01-07 19:08:18 +0100294 if (proc != NULL) {
295 proc->event_handler = NULL;
296 if (event->proc->state == STATE_ATTACHED
297 && options.follow)
298 proc->state = STATE_ATTACHED;
299 else
300 proc->state = STATE_IGNORED;
301 }
Petr Machata75dcf7d2011-10-06 14:30:19 +0200302
Petr Machata6095c132013-01-07 19:08:18 +0100303 continue_process(newpid);
304
305 } else if (proc != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100306 proc->state = STATE_BEING_CREATED;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200307 }
Petr Machata534e00f2011-09-27 17:58:38 +0200308
Petr Machata6095c132013-01-07 19:08:18 +0100309 if (event->type != EVENT_VFORK)
310 continue_process(event->proc->pid);
311 else if (proc != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 continue_after_vfork(proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200313 else
Petr Machata6095c132013-01-07 19:08:18 +0100314 continue_process(newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200315}
316
317static void
Petr Machata929bd572012-12-17 03:20:34 +0100318handle_new(Event *event)
319{
Juan Cespedes03192f82009-07-03 10:16:22 +0200320 debug(DEBUG_FUNCTION, "handle_new(pid=%d)", event->e_un.newpid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200321
Petr Machata929bd572012-12-17 03:20:34 +0100322 struct process *proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200323 if (!proc) {
324 pending_new_insert(event->e_un.newpid);
325 } else {
326 assert(proc->state == STATE_BEING_CREATED);
Juan Cespedes30439b42009-05-22 19:03:09 +0200327 if (options.follow) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200328 proc->state = STATE_ATTACHED;
329 } else {
330 proc->state = STATE_IGNORED;
331 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200332 continue_process(proc->pid);
333 }
334}
335
Juan Cespedesf1350522008-12-16 18:19:58 +0100336static char *
Petr Machata929bd572012-12-17 03:20:34 +0100337shortsignal(struct process *proc, int signum)
338{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 static char *signalent0[] = {
340#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100341 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 static char *signalent1[] = {
343#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100344 };
345 static char **signalents[] = { signalent0, signalent1 };
346 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 sizeof signalent1 / sizeof signalent1[0]
348 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100349
Juan Cespedescd8976d2009-05-14 13:47:58 +0200350 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
351
Petr Machata8e682c22013-01-09 16:20:20 +0100352 assert(proc->personality < sizeof signalents / sizeof signalents[0]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100353 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100354 return "UNKNOWN_SIGNAL";
355 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100356 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100357 }
358}
359
Juan Cespedesf1350522008-12-16 18:19:58 +0100360static char *
Petr Machata929bd572012-12-17 03:20:34 +0100361sysname(struct process *proc, int sysnum)
362{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100363 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100364 static char *syscallent0[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100366 };
Petr Machata8e682c22013-01-09 16:20:20 +0100367 static char *syscallent1[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100369 };
Petr Machata8e682c22013-01-09 16:20:20 +0100370 static char **syscallents[] = { syscallent0, syscallent1 };
371 int nsyscalls[] = {
372 sizeof syscallent0 / sizeof syscallent0[0],
373 sizeof syscallent1 / sizeof syscallent1[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100375
Juan Cespedescd8976d2009-05-14 13:47:58 +0200376 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
377
Petr Machata8e682c22013-01-09 16:20:20 +0100378 assert(proc->personality < sizeof syscallents / sizeof syscallents[0]);
379 if (sysnum < 0 || sysnum >= nsyscalls[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100380 sprintf(result, "SYS_%d", sysnum);
381 return result;
382 } else {
Petr Machata82f748d2013-10-23 00:39:23 +0200383 return syscallents[proc->personality][sysnum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100384 }
385}
386
Juan Cespedesf1350522008-12-16 18:19:58 +0100387static char *
Petr Machata929bd572012-12-17 03:20:34 +0100388arch_sysname(struct process *proc, int sysnum)
389{
Juan Cespedes63184be2008-12-10 13:30:12 +0100390 static char result[128];
Petr Machata8e682c22013-01-09 16:20:20 +0100391 static char *arch_syscallent[] = {
Juan Cespedes63184be2008-12-10 13:30:12 +0100392#include "arch_syscallent.h"
393 };
Petr Machata8e682c22013-01-09 16:20:20 +0100394 int nsyscalls = sizeof arch_syscallent / sizeof arch_syscallent[0];
Juan Cespedes63184be2008-12-10 13:30:12 +0100395
Juan Cespedescd8976d2009-05-14 13:47:58 +0200396 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
397
Petr Machata8e682c22013-01-09 16:20:20 +0100398 if (sysnum < 0 || sysnum >= nsyscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100399 sprintf(result, "ARCH_%d", sysnum);
400 return result;
401 } else {
Petr Machata8e682c22013-01-09 16:20:20 +0100402 sprintf(result, "ARCH_%s", arch_syscallent[sysnum]);
Juan Cespedes63184be2008-12-10 13:30:12 +0100403 return result;
404 }
405}
406
Petr Machata7c4d3112012-12-09 11:55:03 +0100407#ifndef HAVE_STRSIGNAL
408# define strsignal(SIGNUM) "???"
409#endif
410
Juan Cespedesf1350522008-12-16 18:19:58 +0100411static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200412handle_signal(Event *event) {
413 debug(DEBUG_FUNCTION, "handle_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Joe Damato59e3fb12009-11-06 19:45:10 -0800414 if (event->proc->state != STATE_IGNORED && !options.no_signals) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200415 output_line(event->proc, "--- %s (%s) ---",
416 shortsignal(event->proc, event->e_un.signum),
417 strsignal(event->e_un.signum));
418 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100419 continue_after_signal(event->proc->pid, event->e_un.signum);
420}
421
Petr Machata8a730f32013-11-21 20:43:51 +0100422static int
423init_syscall_symbol(struct library_symbol *libsym, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100424{
Petr Machata82f748d2013-10-23 00:39:23 +0200425 static struct library syscall_lib;
Petr Machata8a730f32013-11-21 20:43:51 +0100426
Petr Machata82f748d2013-10-23 00:39:23 +0200427 if (syscall_lib.protolib == NULL) {
428 struct protolib *protolib
Petr Machata98a7dce2013-10-23 17:37:47 +0200429 = protolib_cache_load(&g_protocache, "syscalls", 0, 1);
Petr Machata82f748d2013-10-23 00:39:23 +0200430 if (protolib == NULL) {
431 fprintf(stderr, "Couldn't load system call prototypes:"
432 " %s.\n", strerror(errno));
433
434 /* Instead, get a fake one just so we can
435 * carry on, limping. */
436 protolib = malloc(sizeof *protolib);
437 if (protolib == NULL) {
438 fprintf(stderr, "Couldn't even allocate a fake "
439 "prototype library: %s.\n",
440 strerror(errno));
441 abort();
442 }
443 protolib_init(protolib);
444 }
445
446 assert(protolib != NULL);
447 if (library_init(&syscall_lib, LT_LIBTYPE_SYSCALL) < 0) {
448 fprintf(stderr, "Couldn't initialize system call "
449 "library: %s.\n", strerror(errno));
450 abort();
451 }
452
453 library_set_soname(&syscall_lib, "SYS", 0);
454 syscall_lib.protolib = protolib;
455 }
456
Petr Machata8a730f32013-11-21 20:43:51 +0100457 if (library_symbol_init(libsym, 0, name, 0, LS_TOPLT_NONE) < 0)
458 return -1;
459
460 libsym->lib = &syscall_lib;
461 return 0;
462}
463
464/* Account the unfinished functions on the call stack. */
465static void
466account_current_callstack(struct process *proc)
467{
468 if (! options.summary)
469 return;
470
471 struct timedelta spent[proc->callstack_depth];
472
473 size_t i;
474 for (i = 0; i < proc->callstack_depth; ++i) {
475 struct callstack_element *elem = &proc->callstack[i];
476 spent[i] = calc_time_spent(elem->enter_time);
477 }
478
479 for (i = 0; i < proc->callstack_depth; ++i) {
480 struct callstack_element *elem = &proc->callstack[i];
481 struct library_symbol syscall, *libsym = NULL;
482 if (elem->is_syscall) {
483 const char *name = sysname(proc, elem->c_un.syscall);
484 if (init_syscall_symbol(&syscall, name) >= 0)
485 libsym = &syscall;
486
487 } else {
488 libsym = elem->c_un.libfunc;
489 }
490
491 if (libsym != NULL) {
492 summary_account_call(libsym, spent[i]);
493
494 if (elem->is_syscall)
495 library_symbol_destroy(&syscall);
496 }
497 }
498}
499
500static void
501handle_exit(Event *event) {
502 debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
503 if (event->proc->state != STATE_IGNORED) {
504 output_line(event->proc, "+++ exited (status %d) +++",
505 event->e_un.ret_val);
506 }
507
508 account_current_callstack(event->proc);
509 remove_process(event->proc);
510}
511
512static void
513handle_exit_signal(Event *event) {
514 debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
515 if (event->proc->state != STATE_IGNORED) {
516 output_line(event->proc, "+++ killed by %s +++",
517 shortsignal(event->proc, event->e_un.signum));
518 }
519
520 account_current_callstack(event->proc);
521 remove_process(event->proc);
522}
523
524static void
525output_syscall(struct process *proc, const char *name, enum tof tof,
526 bool left, struct timedelta *spent)
527{
528 if (left)
529 assert(spent == NULL);
530
Petr Machataa32ef7c2012-04-23 18:05:24 +0200531 struct library_symbol syscall;
Petr Machata8a730f32013-11-21 20:43:51 +0100532 if (init_syscall_symbol(&syscall, name) >= 0) {
533 if (left) {
534 if (! options.summary)
535 output_left(tof, proc, &syscall);
536 } else if (options.summary) {
537 summary_account_call(&syscall, *spent);
538 } else {
539 output_right(tof, proc, &syscall, spent);
540 }
541
Petr Machataa32ef7c2012-04-23 18:05:24 +0200542 library_symbol_destroy(&syscall);
Petr Machata29add4f2012-02-18 16:38:05 +0100543 }
Petr Machata29add4f2012-02-18 16:38:05 +0100544}
545
546static void
Petr Machata929bd572012-12-17 03:20:34 +0100547output_syscall_left(struct process *proc, const char *name)
Petr Machata29add4f2012-02-18 16:38:05 +0100548{
Petr Machata8a730f32013-11-21 20:43:51 +0100549 output_syscall(proc, name, LT_TOF_SYSCALL, true, NULL);
Petr Machata29add4f2012-02-18 16:38:05 +0100550}
551
552static void
Petr Machata8a730f32013-11-21 20:43:51 +0100553output_syscall_right(struct process *proc, const char *name,
554 struct timedelta *spent)
Petr Machata29add4f2012-02-18 16:38:05 +0100555{
Petr Machata8a730f32013-11-21 20:43:51 +0100556 output_syscall(proc, name, LT_TOF_SYSCALLR, false, spent);
Petr Machata29add4f2012-02-18 16:38:05 +0100557}
558
Juan Cespedesf1350522008-12-16 18:19:58 +0100559static void
Petr Machata8a730f32013-11-21 20:43:51 +0100560handle_syscall(Event *event)
561{
Juan Cespedes03192f82009-07-03 10:16:22 +0200562 debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200563 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100564 callstack_push_syscall(event->proc, event->e_un.sysnum);
Petr Machata29add4f2012-02-18 16:38:05 +0100565 if (options.syscalls)
566 output_syscall_left(event->proc,
567 sysname(event->proc,
568 event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100569 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100570 continue_after_syscall(event->proc, event->e_un.sysnum, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100571}
572
Juan Cespedesf1350522008-12-16 18:19:58 +0100573static void
Petr Machata929bd572012-12-17 03:20:34 +0100574handle_exec(Event *event)
575{
576 struct process *proc = event->proc;
Juan Cespedese0660df2009-05-21 18:14:39 +0200577
Petr Machata3d0c91c2012-04-14 02:37:38 +0200578 /* Save the PID so that we can use it after unsuccessful
579 * process_exec. */
580 pid_t pid = proc->pid;
581
Juan Cespedes03192f82009-07-03 10:16:22 +0200582 debug(DEBUG_FUNCTION, "handle_exec(pid=%d)", proc->pid);
Juan Cespedese0660df2009-05-21 18:14:39 +0200583 if (proc->state == STATE_IGNORED) {
Petr Machata3d0c91c2012-04-14 02:37:38 +0200584 untrace:
585 untrace_pid(pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200586 remove_process(proc);
Juan Cespedese0660df2009-05-21 18:14:39 +0200587 return;
Juan Cespedes5c682042009-05-21 15:59:56 +0200588 }
Juan Cespedese0660df2009-05-21 18:14:39 +0200589 output_line(proc, "--- Called exec() ---");
Petr Machata3d0c91c2012-04-14 02:37:38 +0200590
Petr Machata8a730f32013-11-21 20:43:51 +0100591 account_current_callstack(proc);
592
Petr Machata3d0c91c2012-04-14 02:37:38 +0200593 if (process_exec(proc) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200594 fprintf(stderr,
595 "couldn't reinitialize process %d after exec\n", pid);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200596 goto untrace;
597 }
598
Petr Machata057caa52013-01-30 23:28:47 +0100599 continue_after_exec(proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200600}
601
602static void
Juan Cespedes03192f82009-07-03 10:16:22 +0200603handle_arch_syscall(Event *event) {
604 debug(DEBUG_FUNCTION, "handle_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200605 if (event->proc->state != STATE_IGNORED) {
Petr Machata211f0882010-11-03 18:42:18 +0100606 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
Juan Cespedes5c682042009-05-21 15:59:56 +0200607 if (options.syscalls) {
Petr Machata29add4f2012-02-18 16:38:05 +0100608 output_syscall_left(event->proc,
609 arch_sysname(event->proc,
610 event->e_un.sysnum));
Juan Cespedes5c682042009-05-21 15:59:56 +0200611 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100612 }
Juan Cespedes63184be2008-12-10 13:30:12 +0100613 continue_process(event->proc->pid);
614}
615
Juan Cespedesf1350522008-12-16 18:19:58 +0100616static void
Petr Machata8a730f32013-11-21 20:43:51 +0100617handle_x_sysret(Event *event, char *(*name_cb)(struct process *, int))
Petr Machata929bd572012-12-17 03:20:34 +0100618{
Petr Machata8a730f32013-11-21 20:43:51 +0100619 debug(DEBUG_FUNCTION, "handle_x_sysret(pid=%d, sysnum=%d)",
620 event->proc->pid, event->e_un.sysnum);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100621
Petr Machata8a730f32013-11-21 20:43:51 +0100622 unsigned d = event->proc->callstack_depth;
623 assert(d > 0);
624 struct callstack_element *elem = &event->proc->callstack[d - 1];
625 assert(elem->is_syscall);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100626
Juan Cespedes5c682042009-05-21 15:59:56 +0200627 if (event->proc->state != STATE_IGNORED) {
Petr Machata8a730f32013-11-21 20:43:51 +0100628 struct timedelta spent = calc_time_spent(elem->enter_time);
Petr Machata29add4f2012-02-18 16:38:05 +0100629 if (options.syscalls)
630 output_syscall_right(event->proc,
Petr Machata8a730f32013-11-21 20:43:51 +0100631 name_cb(event->proc,
632 event->e_un.sysnum),
633 &spent);
Petr Machata29add4f2012-02-18 16:38:05 +0100634
Petr Machata211f0882010-11-03 18:42:18 +0100635 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200636 }
Petr Machata43d2fe52011-11-02 13:25:49 +0100637 continue_after_syscall(event->proc, event->e_un.sysnum, 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100638}
639
Juan Cespedesf1350522008-12-16 18:19:58 +0100640static void
Petr Machata8a730f32013-11-21 20:43:51 +0100641handle_sysret(Event *event)
642{
643 handle_x_sysret(event, &sysname);
644}
645
646static void
647handle_arch_sysret(Event *event)
648{
649 handle_x_sysret(event, &arch_sysname);
Juan Cespedes63184be2008-12-10 13:30:12 +0100650}
651
Juan Cespedesf1350522008-12-16 18:19:58 +0100652static void
Petr Machata929bd572012-12-17 03:20:34 +0100653output_right_tos(struct process *proc)
Petr Machata14298742012-04-12 23:09:21 +0200654{
655 size_t d = proc->callstack_depth;
Petr Machata8a730f32013-11-21 20:43:51 +0100656 assert(d > 0);
Petr Machata14298742012-04-12 23:09:21 +0200657 struct callstack_element *elem = &proc->callstack[d - 1];
Petr Machata8a730f32013-11-21 20:43:51 +0100658 assert(! elem->is_syscall);
659
660 if (proc->state != STATE_IGNORED) {
661 struct timedelta spent = calc_time_spent(elem->enter_time);
662 if (options.summary)
663 summary_account_call(elem->c_un.libfunc, spent);
664 else
665 output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc,
666 &spent);
667 }
Petr Machata14298742012-04-12 23:09:21 +0200668}
669
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200670#ifndef ARCH_HAVE_SYMBOL_RET
Petr Machata929bd572012-12-17 03:20:34 +0100671void arch_symbol_ret(struct process *proc, struct library_symbol *libsym)
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200672{
673}
674#endif
675
Petr Machata14298742012-04-12 23:09:21 +0200676static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100677handle_breakpoint(Event *event)
678{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100679 int i, j;
Petr Machatabc373262012-02-07 23:31:15 +0100680 struct breakpoint *sbp;
Petr Machata929bd572012-12-17 03:20:34 +0100681 struct process *leader = event->proc->leader;
Petr Machata31b2f9f2012-04-12 22:23:40 +0200682 void *brk_addr = event->e_un.brk_addr;
Petr Machata9a5420c2011-07-09 11:21:23 +0200683
684 /* The leader has terminated. */
685 if (leader == NULL) {
686 continue_process(event->proc->pid);
687 return;
688 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100689
Petr Machata31b2f9f2012-04-12 22:23:40 +0200690 debug(DEBUG_FUNCTION, "handle_breakpoint(pid=%d, addr=%p)",
691 event->proc->pid, brk_addr);
692 debug(2, "event: breakpoint (%p)", brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100693
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100694 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
Petr Machata31b2f9f2012-04-12 22:23:40 +0200695 if (brk_addr == event->proc->callstack[i].return_addr) {
Petr Machata8a730f32013-11-21 20:43:51 +0100696 for (j = event->proc->callstack_depth - 1; j > i; j--)
Juan Cespedes5916fda2002-02-25 00:19:21 +0100697 callstack_pop(event->proc);
Petr Machata31b2f9f2012-04-12 22:23:40 +0200698
Petr Machata9e1e9692012-04-19 02:29:38 +0200699 struct library_symbol *libsym =
700 event->proc->callstack[i].c_un.libfunc;
701
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200702 arch_symbol_ret(event->proc, libsym);
Petr Machata14298742012-04-12 23:09:21 +0200703 output_right_tos(event->proc);
704 callstack_pop(event->proc);
705
Petr Machata31b2f9f2012-04-12 22:23:40 +0200706 /* Pop also any other entries that seem like
707 * they are linked to the current one: they
708 * have the same return address, but were made
709 * for different symbols. This should only
710 * happen for entry point tracing, i.e. for -x
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200711 * everywhere, or -x and -e on MIPS. */
Petr Machata31b2f9f2012-04-12 22:23:40 +0200712 while (event->proc->callstack_depth > 0) {
713 struct callstack_element *prev;
714 size_t d = event->proc->callstack_depth;
715 prev = &event->proc->callstack[d - 1];
716
Petr Machata14298742012-04-12 23:09:21 +0200717 if (prev->c_un.libfunc == libsym
718 || prev->return_addr != brk_addr)
Petr Machata31b2f9f2012-04-12 22:23:40 +0200719 break;
Petr Machata14298742012-04-12 23:09:21 +0200720
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200721 arch_symbol_ret(event->proc,
722 prev->c_un.libfunc);
Petr Machata14298742012-04-12 23:09:21 +0200723 output_right_tos(event->proc);
724 callstack_pop(event->proc);
Juan Cespedes5c682042009-05-21 15:59:56 +0200725 }
Petr Machata31b2f9f2012-04-12 22:23:40 +0200726
Petr Machata5ee36822012-04-19 17:01:51 +0200727 /* Maybe the previous callstack_pop's got rid
728 * of the breakpoint, but if we are in a
729 * recursive call, it's still enabled. In
730 * that case we need to skip it properly. */
Petr Machatac0649d82012-04-20 02:39:33 +0200731 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata5ee36822012-04-19 17:01:51 +0200732 continue_after_breakpoint(event->proc, sbp);
Petr Machatac0649d82012-04-20 02:39:33 +0200733 } else {
734 set_instruction_pointer(event->proc, brk_addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200735 continue_process(event->proc->pid);
Petr Machatac0649d82012-04-20 02:39:33 +0200736 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100737 return;
738 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100739 }
740
Petr Machata5ee36822012-04-19 17:01:51 +0200741 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL)
Petr Machataa9fd8f42012-02-07 13:25:56 +0100742 breakpoint_on_hit(sbp, event->proc);
Petr Machata5ee36822012-04-19 17:01:51 +0200743 else if (event->proc->state != STATE_IGNORED)
744 output_line(event->proc,
745 "unexpected breakpoint at %p", brk_addr);
Petr Machatabc373262012-02-07 23:31:15 +0100746
Petr Machata5ee36822012-04-19 17:01:51 +0200747 /* breakpoint_on_hit may delete its own breakpoint, so we have
748 * to look it up again. */
749 if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
Petr Machata8a730f32013-11-21 20:43:51 +0100750
Petr Machata2b46cfc2012-02-18 11:17:29 +0100751 if (event->proc->state != STATE_IGNORED
752 && sbp->libsym != NULL) {
Juan Cespedes5c682042009-05-21 15:59:56 +0200753 event->proc->stack_pointer = get_stack_pointer(event->proc);
Petr Machatacf989232013-10-11 21:17:24 +0200754 callstack_push_symfunc(event->proc, sbp);
Petr Machata8a730f32013-11-21 20:43:51 +0100755 if (! options.summary)
756 output_left(LT_TOF_FUNCTION, event->proc,
757 sbp->libsym);
Juan Cespedes5c682042009-05-21 15:59:56 +0200758 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100759
Petr Machata56a9ea62012-03-27 03:09:29 +0200760 breakpoint_on_continue(sbp, event->proc);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100761 return;
Petr Machatac0649d82012-04-20 02:39:33 +0200762 } else {
763 set_instruction_pointer(event->proc, brk_addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100764 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100765
Juan Cespedes5e01f651998-03-08 22:31:44 +0100766 continue_process(event->proc->pid);
767}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200768
Juan Cespedesf1350522008-12-16 18:19:58 +0100769static void
Petr Machata929bd572012-12-17 03:20:34 +0100770callstack_push_syscall(struct process *proc, int sysnum)
771{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100772 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200773
Juan Cespedescd8976d2009-05-14 13:47:58 +0200774 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200775 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100776 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500777 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
778 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200779 return;
780 }
781
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100782 elem = &proc->callstack[proc->callstack_depth];
Petr Machata8ae36732012-04-30 01:19:09 +0200783 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200784 elem->is_syscall = 1;
785 elem->c_un.syscall = sysnum;
786 elem->return_addr = NULL;
787
788 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200789 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100790 struct timezone tz;
Petr Machata8a730f32013-11-21 20:43:51 +0100791 gettimeofday(&elem->enter_time, &tz);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100792 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200793}
794
Juan Cespedes21c63a12001-07-07 20:56:56 +0200795static void
Petr Machatacf989232013-10-11 21:17:24 +0200796callstack_push_symfunc(struct process *proc, struct breakpoint *bp)
Petr Machata929bd572012-12-17 03:20:34 +0100797{
Petr Machata14298742012-04-12 23:09:21 +0200798 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200799
Petr Machatacf989232013-10-11 21:17:24 +0200800 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)",
801 proc->pid, bp->libsym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200802 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100803 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Arnaud Patard91a1f322010-01-08 08:40:13 -0500804 fprintf(stderr, "%s: Error: call nesting too deep!\n", __func__);
805 abort();
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200806 return;
807 }
808
Petr Machata14298742012-04-12 23:09:21 +0200809 elem = &proc->callstack[proc->callstack_depth++];
Petr Machata8ae36732012-04-30 01:19:09 +0200810 *elem = (struct callstack_element){};
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200811 elem->is_syscall = 0;
Petr Machatacf989232013-10-11 21:17:24 +0200812 elem->c_un.libfunc = bp->libsym;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200813
Petr Machataf9d93c52013-01-30 23:09:02 +0100814 struct breakpoint *rbp = NULL;
Petr Machatae7746d32013-10-30 00:08:34 +0100815 if (breakpoint_get_return_bp(&rbp, bp, proc) == 0
816 && rbp != NULL) {
Petr Machatacf989232013-10-11 21:17:24 +0200817 struct breakpoint *ext_rbp = insert_breakpoint(proc, rbp);
818 if (ext_rbp != rbp) {
819 breakpoint_destroy(rbp);
820 free(rbp);
821 rbp = ext_rbp;
822 }
823 }
824
Petr Machataf9d93c52013-01-30 23:09:02 +0100825 elem->return_addr = rbp != NULL ? rbp->addr : 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200826
Juan Cespedesda9b9532009-04-07 15:33:50 +0200827 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100828 struct timezone tz;
Petr Machata8a730f32013-11-21 20:43:51 +0100829 gettimeofday(&elem->enter_time, &tz);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100830 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200831}
832
Petr Machatae655ccf2012-10-26 22:21:59 +0200833void
Petr Machata929bd572012-12-17 03:20:34 +0100834callstack_pop(struct process *proc)
Petr Machatae655ccf2012-10-26 22:21:59 +0200835{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100836 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200837 assert(proc->callstack_depth > 0);
838
Juan Cespedescd8976d2009-05-14 13:47:58 +0200839 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100840 elem = &proc->callstack[proc->callstack_depth - 1];
Petr Machatab9440772013-10-14 10:36:26 +0200841 if (!elem->is_syscall && elem->return_addr) {
842 struct breakpoint *bp
843 = address2bpstruct(proc->leader, elem->return_addr);
844 if (bp != NULL) {
Petr Machataba473402013-10-14 10:41:19 +0200845 breakpoint_on_hit(bp, proc);
Petr Machatab9440772013-10-14 10:36:26 +0200846 delete_breakpoint(proc, bp);
847 }
848 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200849
850 if (elem->fetch_context != NULL)
851 fetch_arg_done(elem->fetch_context);
852
853 if (elem->arguments != NULL) {
854 val_dict_destroy(elem->arguments);
855 free(elem->arguments);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200856 }
Petr Machatae655ccf2012-10-26 22:21:59 +0200857
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200858 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200859}