blob: db5d3bddb67021ee52868bc71690a7ebb59108d6 [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);
Juan Cespedes63184be2008-12-10 13:30:12 +010027static void process_arch_syscall(struct event *event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010028static void process_sysret(struct event *event);
Juan Cespedes63184be2008-12-10 13:30:12 +010029static void process_arch_sysret(struct event *event);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010030static void process_breakpoint(struct event *event);
31static void remove_proc(struct process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010032
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033static void callstack_push_syscall(struct process *proc, int sysnum);
34static void callstack_push_symfunc(struct process *proc,
35 struct library_symbol *sym);
36static void callstack_pop(struct process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020037
Juan Cespedesf1350522008-12-16 18:19:58 +010038static char *
39shortsignal(struct process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 static char *signalent0[] = {
41#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010042 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +010043 static char *signalent1[] = {
44#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010045 };
46 static char **signalents[] = { signalent0, signalent1 };
47 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 sizeof signalent1 / sizeof signalent1[0]
49 };
Juan Cespedes5e01f651998-03-08 22:31:44 +010050
Ian Wienand9a2ad352006-02-20 22:44:45 +010051 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +010052 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +010053 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010054 return "UNKNOWN_SIGNAL";
55 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +010056 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +010057 }
58}
59
Juan Cespedesf1350522008-12-16 18:19:58 +010060static char *
61sysname(struct process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010062 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 static char *syscalent0[] = {
64#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010065 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 static char *syscalent1[] = {
67#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010068 };
69 static char **syscalents[] = { syscalent0, syscalent1 };
70 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 sizeof syscalent1 / sizeof syscalent1[0]
72 };
Juan Cespedes5e01f651998-03-08 22:31:44 +010073
Ian Wienand9a2ad352006-02-20 22:44:45 +010074 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +010076 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010077 sprintf(result, "SYS_%d", sysnum);
78 return result;
79 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 sprintf(result, "SYS_%s",
81 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +010082 return result;
83 }
84}
85
Juan Cespedesf1350522008-12-16 18:19:58 +010086static char *
87arch_sysname(struct process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +010088 static char result[128];
89 static char *arch_syscalent[] = {
90#include "arch_syscallent.h"
91 };
92 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
93
94 if (sysnum < 0 || sysnum >= nsyscals) {
95 sprintf(result, "ARCH_%d", sysnum);
96 return result;
97 } else {
98 sprintf(result, "ARCH_%s",
99 arch_syscalent[sysnum]);
100 return result;
101 }
102}
103
Juan Cespedesf1350522008-12-16 18:19:58 +0100104void
105process_event(struct event *event) {
Juan Cespedesefe85f02004-04-04 01:31:38 +0200106 switch (event->thing) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200107 case EVENT_NONE:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 debug(1, "event: none");
109 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200110 case EVENT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 debug(1, "event: signal (%s [%d])",
112 shortsignal(event->proc, event->e_un.signum),
113 event->e_un.signum);
114 process_signal(event);
115 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200116 case EVENT_EXIT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 debug(1, "event: exit (%d)", event->e_un.ret_val);
118 process_exit(event);
119 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200120 case EVENT_EXIT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100121 debug(1, "event: exit signal (%s [%d])",
122 shortsignal(event->proc, event->e_un.signum),
123 event->e_un.signum);
124 process_exit_signal(event);
125 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200126 case EVENT_SYSCALL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 debug(1, "event: syscall (%s [%d])",
128 sysname(event->proc, event->e_un.sysnum),
129 event->e_un.sysnum);
130 process_syscall(event);
131 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200132 case EVENT_SYSRET:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100133 debug(1, "event: sysret (%s [%d])",
134 sysname(event->proc, event->e_un.sysnum),
135 event->e_un.sysnum);
136 process_sysret(event);
137 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200138 case EVENT_ARCH_SYSCALL:
Juan Cespedes63184be2008-12-10 13:30:12 +0100139 debug(1, "event: arch_syscall (%s [%d])",
140 arch_sysname(event->proc, event->e_un.sysnum),
141 event->e_un.sysnum);
142 process_arch_syscall(event);
143 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200144 case EVENT_ARCH_SYSRET:
Juan Cespedes63184be2008-12-10 13:30:12 +0100145 debug(1, "event: arch_sysret (%s [%d])",
146 arch_sysname(event->proc, event->e_un.sysnum),
147 event->e_un.sysnum);
148 process_arch_sysret(event);
149 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200150 case EVENT_BREAKPOINT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 debug(1, "event: breakpoint");
152 process_breakpoint(event);
153 return;
154 default:
155 fprintf(stderr, "Error! unknown event?\n");
156 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200157 }
158}
159
Juan Cespedesf1350522008-12-16 18:19:58 +0100160static void
161process_signal(struct event *event) {
Juan Cespedes28f60191998-04-12 00:04:39 +0200162 if (exiting && event->e_un.signum == SIGSTOP) {
163 pid_t pid = event->proc->pid;
164 disable_all_breakpoints(event->proc);
165 untrace_pid(pid);
166 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200167 return;
168 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 output_line(event->proc, "--- %s (%s) ---",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100170 shortsignal(event->proc, event->e_un.signum),
171 strsignal(event->e_un.signum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100172 continue_after_signal(event->proc->pid, event->e_un.signum);
173}
174
Juan Cespedesf1350522008-12-16 18:19:58 +0100175static void
176process_exit(struct event *event) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100177 output_line(event->proc, "+++ exited (status %d) +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100178 event->e_un.ret_val);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100179 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180}
181
Juan Cespedesf1350522008-12-16 18:19:58 +0100182static void
183process_exit_signal(struct event *event) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100184 output_line(event->proc, "+++ killed by %s +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100185 shortsignal(event->proc, event->e_un.signum));
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100186 remove_proc(event->proc);
187}
188
Juan Cespedesf1350522008-12-16 18:19:58 +0100189static void
190remove_proc(struct process *proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100191 struct process *tmp, *tmp2;
192
Juan Cespedescac15c32003-01-31 18:58:58 +0100193 debug(1, "Removing pid %u\n", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200194
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100195 if (list_of_processes == proc) {
196 tmp = list_of_processes;
197 list_of_processes = list_of_processes->next;
198 free(tmp);
199 return;
200 }
201 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100202 while (tmp->next) {
203 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100204 tmp2 = tmp->next;
205 tmp->next = tmp->next->next;
206 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200207 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100208 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100209 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100210 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100211}
212
Juan Cespedesf1350522008-12-16 18:19:58 +0100213static void
214process_syscall(struct event *event) {
Juan Cespedesce377d52008-12-16 19:38:10 +0100215 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 output_left(LT_TOF_SYSCALL, event->proc,
217 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100218 }
Juan Cespedesaee09312007-08-31 18:49:48 +0200219 if (fork_p(event->proc, event->e_un.sysnum)) {
220 disable_all_breakpoints(event->proc);
221 } else if (event->proc->breakpoints_enabled == 0) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100222 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100223 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100224 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100225 continue_process(event->proc->pid);
226}
227
Juan Cespedesf1350522008-12-16 18:19:58 +0100228static void
229process_arch_syscall(struct event *event) {
Juan Cespedesce377d52008-12-16 19:38:10 +0100230 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100231 output_left(LT_TOF_SYSCALL, event->proc,
232 arch_sysname(event->proc, event->e_un.sysnum));
233 }
234 if (event->proc->breakpoints_enabled == 0) {
235 enable_all_breakpoints(event->proc);
236 }
237 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
238 continue_process(event->proc->pid);
239}
240
Juan Cespedesd65efa32003-02-03 00:22:30 +0100241struct timeval current_time_spent;
242
Juan Cespedesf1350522008-12-16 18:19:58 +0100243static void
244calc_time_spent(struct process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100245 struct timeval tv;
246 struct timezone tz;
247 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100249
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100251
252 gettimeofday(&tv, &tz);
253
254 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
255 if (tv.tv_usec >= elem->time_spent.tv_usec) {
256 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
257 } else {
258 diff.tv_sec++;
259 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
260 }
261 current_time_spent = diff;
262}
263
Juan Cespedesf1350522008-12-16 18:19:58 +0100264static void
265process_sysret(struct event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200266 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100267 calc_time_spent(event->proc);
268 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100269 if (fork_p(event->proc, event->e_un.sysnum)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 if (opt_f) {
Steve Fink65b53df2006-09-25 02:27:08 +0200271 arg_type_info info;
Steve Fink65b53df2006-09-25 02:27:08 +0200272 info.type = ARGTYPE_LONG;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 pid_t child =
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200274 gimme_arg(LT_TOF_SYSCALLR, event->proc, -1, &info);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 if (child > 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100276 open_pid(child, 0);
277 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100278 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100279 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100280 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200281 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100282 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 output_right(LT_TOF_SYSCALLR, event->proc,
284 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200285 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100286 continue_process(event->proc->pid);
287}
288
Juan Cespedesf1350522008-12-16 18:19:58 +0100289static void
290process_arch_sysret(struct event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200291 if (opt_T || options.summary) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100292 calc_time_spent(event->proc);
293 }
294 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100295 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100296 output_right(LT_TOF_SYSCALLR, event->proc,
297 arch_sysname(event->proc, event->e_un.sysnum));
298 }
299 continue_process(event->proc->pid);
300}
301
Juan Cespedesf1350522008-12-16 18:19:58 +0100302static void
303process_breakpoint(struct event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100304 int i, j;
Steve Fink65b53df2006-09-25 02:27:08 +0200305 struct breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100306
Juan Cespedesefe85f02004-04-04 01:31:38 +0200307 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100308
Paul Gilliam76c61f12006-06-14 06:55:21 +0200309#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100310 /* Need to skip following NOP's to prevent a fake function from being stacked. */
311 long stub_addr = (long) get_count_register(event->proc);
312 struct breakpoint *stub_bp = NULL;
313 char nop_instruction[] = PPC_NOP;
314
315 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
316
317 if (stub_bp) {
318 unsigned char *bp_instruction = stub_bp->orig_value;
319
320 if (memcmp(bp_instruction, nop_instruction,
321 PPC_NOP_LENGTH) == 0) {
322 if (stub_addr != (long) event->e_un.brk_addr) {
323 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
324 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200325 return;
326 }
327 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100328 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200329#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100330 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100331 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100332 continue_enabling_breakpoint(event->proc->pid,
333 event->proc->
334 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100335 event->proc->breakpoint_being_enabled = NULL;
336 return;
337 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200338
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
340 if (event->e_un.brk_addr ==
341 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200342#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100343 /*
344 * PPC HACK! (XXX FIXME TODO)
345 * The PLT gets modified during the first call,
346 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100348 unsigned long a;
349 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200351 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200352
Paul Gilliam76c61f12006-06-14 06:55:21 +0200353 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100354 unsigned char break_insn[] = BREAKPOINT_VALUE;
355
356 sbp = address2bpstruct(event->proc, addr);
357 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
359 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100360
Paul Gilliam76c61f12006-06-14 06:55:21 +0200361 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100362 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100363 insert_breakpoint(event->proc, addr,
364 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100365 }
366 } else {
367 sbp = libsym->brkpnt;
368 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200369 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 insert_breakpoint(event->proc, addr,
371 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200372 }
Ian Wienand3219f322006-02-16 06:00:00 +0100373 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100374#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200375 void *addr;
376 void *old_addr;
377 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
378 assert(sym && sym->brkpnt);
379 old_addr=sym->brkpnt->addr;
380 addr=sym2addr(event->proc,sym);
381 assert(old_addr !=0 && addr !=0);
382 if(addr != old_addr){
383 struct library_symbol *new_sym;
384 new_sym=malloc(sizeof(*new_sym));
385 memcpy(new_sym,sym,sizeof(*new_sym));
386 new_sym->next=event->proc->list_of_symbols;
387 event->proc->list_of_symbols=new_sym;
388 new_sym->brkpnt=0;
389 insert_breakpoint(event->proc, addr, new_sym);
390 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200391#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100393 callstack_pop(event->proc);
394 }
Juan Cespedesda9b9532009-04-07 15:33:50 +0200395 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100396 calc_time_spent(event->proc);
397 }
398 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100399 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200400 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 event->proc->callstack[i].c_un.libfunc->
402 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100403 continue_after_breakpoint(event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100404 address2bpstruct(event->proc,
405 event->e_un.
406 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100407 return;
408 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100409 }
410
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100411 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
412 event->proc->stack_pointer = get_stack_pointer(event->proc);
413 event->proc->return_addr =
414 get_return_addr(event->proc, event->proc->stack_pointer);
415 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
416 callstack_push_symfunc(event->proc, sbp->libsym);
Paul Gilliambe320772006-04-24 22:06:23 +0200417#ifdef PLT_REINITALISATION_BP
418 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
420 0))
421 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200422#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423
424 continue_after_breakpoint(event->proc, sbp);
425 return;
426 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100427
428 output_line(event->proc, "unexpected breakpoint at %p",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100429 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100430 continue_process(event->proc->pid);
431}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200432
Juan Cespedesf1350522008-12-16 18:19:58 +0100433static void
434callstack_push_syscall(struct process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100435 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200436
437 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200439 fprintf(stderr, "Error: call nesting too deep!\n");
440 return;
441 }
442
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200444 elem->is_syscall = 1;
445 elem->c_un.syscall = sysnum;
446 elem->return_addr = NULL;
447
448 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200449 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100450 struct timezone tz;
451 gettimeofday(&elem->time_spent, &tz);
452 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200453}
454
Juan Cespedes21c63a12001-07-07 20:56:56 +0200455static void
Juan Cespedesf1350522008-12-16 18:19:58 +0100456callstack_push_symfunc(struct process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200458
459 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100460 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200461 fprintf(stderr, "Error: call nesting too deep!\n");
462 return;
463 }
464
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200466 elem->is_syscall = 0;
467 elem->c_un.libfunc = sym;
468
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200469 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200470 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200471 insert_breakpoint(proc, elem->return_addr, 0);
472 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200473
474 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200475 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100476 struct timezone tz;
477 gettimeofday(&elem->time_spent, &tz);
478 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200479}
480
Juan Cespedesf1350522008-12-16 18:19:58 +0100481static void
482callstack_pop(struct process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200484 assert(proc->callstack_depth > 0);
485
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100486 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200487 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200488 delete_breakpoint(proc, elem->return_addr);
489 }
490 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200491}