blob: f01f27e57812cf4f602db9c238eeb595661a364a [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#define _GNU_SOURCE
6#include <stdio.h>
7#include <string.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01008#include <stdlib.h>
Juan Cespedes28f60191998-04-12 00:04:39 +02009#include <signal.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020010#include <assert.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010011#include <sys/time.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
13#include "ltrace.h"
14#include "output.h"
15#include "options.h"
Juan Cespedes81690ef1998-03-13 19:31:29 +010016#include "elf.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010017#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Juan Cespedesf1bfe202002-03-27 00:22:23 +010019#ifdef __powerpc__
20#include <sys/ptrace.h>
21#endif
22
Ian Wienand2d45b1a2006-02-20 22:48:07 +010023static void process_signal(struct event *event);
24static void process_exit(struct event *event);
25static void process_exit_signal(struct event *event);
26static void process_syscall(struct event *event);
27static void process_sysret(struct event *event);
28static void process_breakpoint(struct event *event);
29static void remove_proc(struct process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010030
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031static void callstack_push_syscall(struct process *proc, int sysnum);
32static void callstack_push_symfunc(struct process *proc,
33 struct library_symbol *sym);
34static void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036static char *shortsignal(struct process *proc, int signum)
37{
38 static char *signalent0[] = {
39#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010040 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +010041 static char *signalent1[] = {
42#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010043 };
44 static char **signalents[] = { signalent0, signalent1 };
45 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +010046 sizeof signalent1 / sizeof signalent1[0]
47 };
Juan Cespedes5e01f651998-03-08 22:31:44 +010048
Ian Wienand9a2ad352006-02-20 22:44:45 +010049 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +010051 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010052 return "UNKNOWN_SIGNAL";
53 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +010054 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +010055 }
56}
57
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058static char *sysname(struct process *proc, int sysnum)
59{
Juan Cespedes5e01f651998-03-08 22:31:44 +010060 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 static char *syscalent0[] = {
62#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010063 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 static char *syscalent1[] = {
65#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010066 };
67 static char **syscalents[] = { syscalent0, syscalent1 };
68 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 sizeof syscalent1 / sizeof syscalent1[0]
70 };
Juan Cespedes5e01f651998-03-08 22:31:44 +010071
Ian Wienand9a2ad352006-02-20 22:44:45 +010072 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +010074 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 sprintf(result, "SYS_%d", sysnum);
76 return result;
77 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 sprintf(result, "SYS_%s",
79 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +010080 return result;
81 }
82}
83
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084void process_event(struct event *event)
85{
Juan Cespedesefe85f02004-04-04 01:31:38 +020086 switch (event->thing) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 case LT_EV_NONE:
88 debug(1, "event: none");
89 return;
90 case LT_EV_SIGNAL:
91 debug(1, "event: signal (%s [%d])",
92 shortsignal(event->proc, event->e_un.signum),
93 event->e_un.signum);
94 process_signal(event);
95 return;
96 case LT_EV_EXIT:
97 debug(1, "event: exit (%d)", event->e_un.ret_val);
98 process_exit(event);
99 return;
100 case LT_EV_EXIT_SIGNAL:
101 debug(1, "event: exit signal (%s [%d])",
102 shortsignal(event->proc, event->e_un.signum),
103 event->e_un.signum);
104 process_exit_signal(event);
105 return;
106 case LT_EV_SYSCALL:
107 debug(1, "event: syscall (%s [%d])",
108 sysname(event->proc, event->e_un.sysnum),
109 event->e_un.sysnum);
110 process_syscall(event);
111 return;
112 case LT_EV_SYSRET:
113 debug(1, "event: sysret (%s [%d])",
114 sysname(event->proc, event->e_un.sysnum),
115 event->e_un.sysnum);
116 process_sysret(event);
117 return;
118 case LT_EV_BREAKPOINT:
119 debug(1, "event: breakpoint");
120 process_breakpoint(event);
121 return;
122 default:
123 fprintf(stderr, "Error! unknown event?\n");
124 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200125 }
126}
127
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128static void process_signal(struct event *event)
129{
Juan Cespedes28f60191998-04-12 00:04:39 +0200130 if (exiting && event->e_un.signum == SIGSTOP) {
131 pid_t pid = event->proc->pid;
132 disable_all_breakpoints(event->proc);
133 untrace_pid(pid);
134 remove_proc(event->proc);
135 continue_after_signal(pid, event->e_un.signum);
136 return;
137 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100138 output_line(event->proc, "--- %s (%s) ---",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 shortsignal(event->proc, event->e_un.signum),
140 strsignal(event->e_un.signum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 continue_after_signal(event->proc->pid, event->e_un.signum);
142}
143
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100144static void process_exit(struct event *event)
145{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146 output_line(event->proc, "+++ exited (status %d) +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 event->e_un.ret_val);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100148 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149}
150
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151static void process_exit_signal(struct event *event)
152{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100153 output_line(event->proc, "+++ killed by %s +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 shortsignal(event->proc, event->e_un.signum));
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100155 remove_proc(event->proc);
156}
157
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158static void remove_proc(struct process *proc)
159{
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100160 struct process *tmp, *tmp2;
161
Juan Cespedescac15c32003-01-31 18:58:58 +0100162 debug(1, "Removing pid %u\n", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200163
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100164 if (list_of_processes == proc) {
165 tmp = list_of_processes;
166 list_of_processes = list_of_processes->next;
167 free(tmp);
168 return;
169 }
170 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100171 while (tmp->next) {
172 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100173 tmp2 = tmp->next;
174 tmp->next = tmp->next->next;
175 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200176 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100177 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100178 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100179 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180}
181
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100182static void process_syscall(struct event *event)
183{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100184 if (opt_S) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100185 output_left(LT_TOF_SYSCALL, event->proc,
186 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100187 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100188 if (fork_p(event->proc, event->e_un.sysnum)
189 || exec_p(event->proc, event->e_un.sysnum)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 disable_all_breakpoints(event->proc);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100191 } else if (event->proc->breakpoints_enabled == 0) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100192 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100193 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100194 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195 continue_process(event->proc->pid);
196}
197
Juan Cespedesd65efa32003-02-03 00:22:30 +0100198struct timeval current_time_spent;
199
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200static void calc_time_spent(struct process *proc)
201{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100202 struct timeval tv;
203 struct timezone tz;
204 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100206
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100208
209 gettimeofday(&tv, &tz);
210
211 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
212 if (tv.tv_usec >= elem->time_spent.tv_usec) {
213 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
214 } else {
215 diff.tv_sec++;
216 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
217 }
218 current_time_spent = diff;
219}
220
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221static void process_sysret(struct event *event)
222{
Juan Cespedesd65efa32003-02-03 00:22:30 +0100223 if (opt_T || opt_c) {
224 calc_time_spent(event->proc);
225 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100226 if (fork_p(event->proc, event->e_un.sysnum)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227 if (opt_f) {
Steve Fink65b53df2006-09-25 02:27:08 +0200228 arg_type_info info;
229 info.arg_num = -1; /* Return value */
230 info.type = ARGTYPE_LONG;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 pid_t child =
Steve Fink65b53df2006-09-25 02:27:08 +0200232 gimme_arg(LT_TOF_SYSCALLR, event->proc, &info);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 if (child > 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100234 open_pid(child, 0);
235 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100236 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100237 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100238 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200239 callstack_pop(event->proc);
Juan Cespedes21c63a12001-07-07 20:56:56 +0200240 if (opt_S) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100241 output_right(LT_TOF_SYSCALLR, event->proc,
242 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200243 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100244 if (exec_p(event->proc, event->e_un.sysnum)) {
Steve Fink65b53df2006-09-25 02:27:08 +0200245 arg_type_info info;
246 info.arg_num = -1; /* Return value */
247 info.type = ARGTYPE_LONG;
248 if (gimme_arg(LT_TOF_SYSCALLR, event->proc, &info) == 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100249 pid_t saved_pid;
250 event->proc->mask_32bit = 0;
251 event->proc->personality = 0;
252 /* FIXME: Leak, should have arch_dep_free.
253 But we are leaking here much more than that. */
254 event->proc->arch_ptr = NULL;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100255 event->proc->filename = pid2name(event->proc->pid);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100256 saved_pid = event->proc->pid;
257 event->proc->pid = 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100258 breakpoints_init(event->proc);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100259 event->proc->pid = saved_pid;
260 } else
261 enable_all_breakpoints(event->proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100262 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100263 continue_process(event->proc->pid);
264}
265
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100266static void process_breakpoint(struct event *event)
267{
268 int i, j;
Steve Fink65b53df2006-09-25 02:27:08 +0200269 struct breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270
Juan Cespedesefe85f02004-04-04 01:31:38 +0200271 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200272 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
273#ifdef __powerpc__
Steve Fink65b53df2006-09-25 02:27:08 +0200274 struct breakpoint *nxtbp;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200275 char nop_inst[] = PPC_NOP;
276 if (memcmp(sbp->orig_value, nop_inst, PPC_NOP_LENGTH) == 0) {
277 nxtbp = address2bpstruct(event->proc,
278 event->e_un.brk_addr +
279 PPC_NOP_LENGTH);
280 if (nxtbp != 0) {
281 enable_breakpoint(event->proc->pid, sbp);
282 continue_after_breakpoint(event->proc, nxtbp);
283 return;
284 }
285 }
286#endif
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100287 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100288 continue_enabling_breakpoint(event->proc->pid,
289 event->proc->
290 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100291 event->proc->breakpoint_being_enabled = NULL;
292 return;
293 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200294
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
296 if (event->e_un.brk_addr ==
297 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200298#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100299 /*
300 * PPC HACK! (XXX FIXME TODO)
301 * The PLT gets modified during the first call,
302 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100303 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100304 unsigned long a;
305 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200307 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200308
Paul Gilliam76c61f12006-06-14 06:55:21 +0200309 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100310 unsigned char break_insn[] = BREAKPOINT_VALUE;
311
312 sbp = address2bpstruct(event->proc, addr);
313 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100314 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
315 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100316
Paul Gilliam76c61f12006-06-14 06:55:21 +0200317 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100318 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 insert_breakpoint(event->proc, addr,
320 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100321 }
322 } else {
323 sbp = libsym->brkpnt;
324 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200325 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 insert_breakpoint(event->proc, addr,
327 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200328 }
Ian Wienand3219f322006-02-16 06:00:00 +0100329 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200330#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100332 callstack_pop(event->proc);
333 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100334 if (opt_T || opt_c) {
335 calc_time_spent(event->proc);
336 }
337 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100338 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200339 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100340 event->proc->callstack[i].c_un.libfunc->
341 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100342 continue_after_breakpoint(event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 address2bpstruct(event->proc,
344 event->e_un.
345 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100346 return;
347 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348 }
349
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
351 event->proc->stack_pointer = get_stack_pointer(event->proc);
352 event->proc->return_addr =
353 get_return_addr(event->proc, event->proc->stack_pointer);
354 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
355 callstack_push_symfunc(event->proc, sbp->libsym);
Paul Gilliambe320772006-04-24 22:06:23 +0200356#ifdef PLT_REINITALISATION_BP
357 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
359 0))
360 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200361#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362
363 continue_after_breakpoint(event->proc, sbp);
364 return;
365 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100366
367 output_line(event->proc, "unexpected breakpoint at %p",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100369 continue_process(event->proc->pid);
370}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200371
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372static void callstack_push_syscall(struct process *proc, int sysnum)
373{
374 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200375
376 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100377 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200378 fprintf(stderr, "Error: call nesting too deep!\n");
379 return;
380 }
381
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200383 elem->is_syscall = 1;
384 elem->c_un.syscall = sysnum;
385 elem->return_addr = NULL;
386
387 proc->callstack_depth++;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100388 if (opt_T || opt_c) {
389 struct timezone tz;
390 gettimeofday(&elem->time_spent, &tz);
391 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200392}
393
Juan Cespedes21c63a12001-07-07 20:56:56 +0200394static void
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395callstack_push_symfunc(struct process *proc, struct library_symbol *sym)
396{
397 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200398
399 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200401 fprintf(stderr, "Error: call nesting too deep!\n");
402 return;
403 }
404
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200406 elem->is_syscall = 0;
407 elem->c_un.libfunc = sym;
408
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200409 elem->return_addr = proc->return_addr;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200410 if (elem->return_addr) {
411 insert_breakpoint(proc, elem->return_addr, 0);
412 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200413
414 proc->callstack_depth++;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100415 if (opt_T || opt_c) {
416 struct timezone tz;
417 gettimeofday(&elem->time_spent, &tz);
418 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200419}
420
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100421static void callstack_pop(struct process *proc)
422{
423 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200424 assert(proc->callstack_depth > 0);
425
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200427 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200428 delete_breakpoint(proc, elem->return_addr);
429 }
430 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200431}