blob: d15d8f7e30fcdd22aa8a6a77df71147ac391d3fd [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
Juan Cespedes393f1d02009-05-07 11:13:54 +020023static void process_signal(Event *event);
24static void process_exit(Event *event);
25static void process_exit_signal(Event *event);
26static void process_syscall(Event *event);
27static void process_arch_syscall(Event *event);
28static void process_sysret(Event *event);
29static void process_arch_sysret(Event *event);
Juan Cespedes393f1d02009-05-07 11:13:54 +020030static void process_clone(Event *event);
31static void process_exec(Event *event);
32static void process_breakpoint(Event *event);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020033static void process_new(Event *event);
Juan Cespedesa8909f72009-04-28 20:02:41 +020034static void remove_proc(Process *proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +010035
Juan Cespedesa8909f72009-04-28 20:02:41 +020036static void callstack_push_syscall(Process *proc, int sysnum);
37static void callstack_push_symfunc(Process *proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010038 struct library_symbol *sym);
Juan Cespedesa8909f72009-04-28 20:02:41 +020039static void callstack_pop(Process *proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020040
Juan Cespedesbc8caf02009-05-07 19:38:38 +020041/* TODO */
42void * address_clone(void * addr) {
43 return addr;
44}
45
46void * breakpoint_clone(void * bp) {
47 Breakpoint * b;
48 b = malloc(sizeof(Breakpoint));
49 if (!b) {
50 perror("malloc()");
51 exit(1);
52 }
53 memcpy(b, bp, sizeof(Breakpoint));
54 return b;
55}
56
57typedef struct Pending_New Pending_New;
58struct Pending_New {
59 pid_t pid;
60 Pending_New * next;
61};
62static Pending_New * pending_news = NULL;
63
64static int
65pending_new(pid_t pid) {
66 Pending_New * p = pending_news;
67 while (p) {
68 if (p->pid == pid) {
69 return 1;
70 }
71 p = p->next;
72 }
73 return 0;
74}
75
76static void
77pending_new_insert(pid_t pid) {
78 Pending_New * p = malloc(sizeof(Pending_New));
79 if (!p) {
80 perror("malloc()");
81 exit(1);
82 }
83 p->pid = pid;
84 p->next = pending_news;
85 pending_news = p;
86}
87
88static void
89pending_new_remove(pid_t pid) {
90 Pending_New *p, *pred;
91
92 p = pending_news;
93 if (p->pid == pid) {
94 pending_news = p->next;
95 free(p);
96 } else {
97 while (p) {
98 if (p->pid == pid) {
99 pred->next = p->next;
100 free(p);
101 }
102 pred = p;
103 p = p->next;
104 }
105 }
106}
107
108static void
109process_clone(Event * event) {
110 Process *p;
111
112 p = malloc(sizeof(Process));
113 if (!p) {
114 perror("malloc()");
115 exit(1);
116 }
117 memcpy(p, event->proc, sizeof(Process));
118 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
119 p->pid = event->e_un.newpid;
120
121 if (pending_new(p->pid)) {
122 pending_new_remove(p->pid);
123 if (p->breakpoint_being_enabled) {
124 enable_breakpoint(p->pid, p->breakpoint_being_enabled);
125 p->breakpoint_being_enabled = NULL;
126 }
127 p->state = STATE_ATTACHED;
128 continue_process(p->pid);
129 p->next = list_of_processes;
130 list_of_processes = p;
131 } else {
132 p->state = STATE_BEING_CREATED;
133 }
134 /* look for previous process_new() */
135}
136
137static void
138process_new(Event * event) {
139 Process * proc = pid2proc(event->e_un.newpid);
140 if (!proc) {
141 pending_new_insert(event->e_un.newpid);
142 } else {
143 assert(proc->state == STATE_BEING_CREATED);
144 if (proc->breakpoint_being_enabled) {
145 enable_breakpoint(proc->pid, proc->breakpoint_being_enabled);
146 proc->breakpoint_being_enabled = NULL;
147 }
148 proc->state = STATE_ATTACHED;
149 continue_process(proc->pid);
150 }
151}
152
Juan Cespedesf1350522008-12-16 18:19:58 +0100153static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200154shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100155 static char *signalent0[] = {
156#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100157 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 static char *signalent1[] = {
159#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100160 };
161 static char **signalents[] = { signalent0, signalent1 };
162 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100163 sizeof signalent1 / sizeof signalent1[0]
164 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100165
Ian Wienand9a2ad352006-02-20 22:44:45 +0100166 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100167 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100168 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 return "UNKNOWN_SIGNAL";
170 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100171 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100172 }
173}
174
Juan Cespedesf1350522008-12-16 18:19:58 +0100175static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200176sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100177 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100178 static char *syscalent0[] = {
179#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 static char *syscalent1[] = {
182#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100183 };
184 static char **syscalents[] = { syscalent0, syscalent1 };
185 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 sizeof syscalent1 / sizeof syscalent1[0]
187 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100188
Ian Wienand9a2ad352006-02-20 22:44:45 +0100189 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100190 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100191 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100192 sprintf(result, "SYS_%d", sysnum);
193 return result;
194 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 sprintf(result, "SYS_%s",
196 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 return result;
198 }
199}
200
Juan Cespedesf1350522008-12-16 18:19:58 +0100201static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200202arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100203 static char result[128];
204 static char *arch_syscalent[] = {
205#include "arch_syscallent.h"
206 };
207 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
208
209 if (sysnum < 0 || sysnum >= nsyscals) {
210 sprintf(result, "ARCH_%d", sysnum);
211 return result;
212 } else {
213 sprintf(result, "ARCH_%s",
214 arch_syscalent[sysnum]);
215 return result;
216 }
217}
218
Juan Cespedesf1350522008-12-16 18:19:58 +0100219void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200220process_event(Event *event) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200221 switch (event->type) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200222 case EVENT_NONE:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 debug(1, "event: none");
224 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200225 case EVENT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 debug(1, "event: signal (%s [%d])",
227 shortsignal(event->proc, event->e_un.signum),
228 event->e_un.signum);
229 process_signal(event);
230 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200231 case EVENT_EXIT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 debug(1, "event: exit (%d)", event->e_un.ret_val);
233 process_exit(event);
234 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200235 case EVENT_EXIT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100236 debug(1, "event: exit signal (%s [%d])",
237 shortsignal(event->proc, event->e_un.signum),
238 event->e_un.signum);
239 process_exit_signal(event);
240 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200241 case EVENT_SYSCALL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100242 debug(1, "event: syscall (%s [%d])",
243 sysname(event->proc, event->e_un.sysnum),
244 event->e_un.sysnum);
245 process_syscall(event);
246 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200247 case EVENT_SYSRET:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 debug(1, "event: sysret (%s [%d])",
249 sysname(event->proc, event->e_un.sysnum),
250 event->e_un.sysnum);
251 process_sysret(event);
252 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200253 case EVENT_ARCH_SYSCALL:
Juan Cespedes63184be2008-12-10 13:30:12 +0100254 debug(1, "event: arch_syscall (%s [%d])",
255 arch_sysname(event->proc, event->e_un.sysnum),
256 event->e_un.sysnum);
257 process_arch_syscall(event);
258 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200259 case EVENT_ARCH_SYSRET:
Juan Cespedes63184be2008-12-10 13:30:12 +0100260 debug(1, "event: arch_sysret (%s [%d])",
261 arch_sysname(event->proc, event->e_un.sysnum),
262 event->e_un.sysnum);
263 process_arch_sysret(event);
264 return;
Juan Cespedes1e583132009-04-07 18:17:11 +0200265 case EVENT_CLONE:
266 debug(1, "event: clone (%u)", event->e_un.newpid);
267 process_clone(event);
268 return;
269 case EVENT_EXEC:
270 debug(1, "event: exec()");
271 process_exec(event);
272 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200273 case EVENT_BREAKPOINT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100274 debug(1, "event: breakpoint");
275 process_breakpoint(event);
276 return;
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200277 case EVENT_NEW:
278 debug(1, "event: new process");
279 process_new(event);
280 return;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 default:
282 fprintf(stderr, "Error! unknown event?\n");
283 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200284 }
285}
286
Juan Cespedesf1350522008-12-16 18:19:58 +0100287static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200288process_signal(Event *event) {
Juan Cespedes28f60191998-04-12 00:04:39 +0200289 if (exiting && event->e_un.signum == SIGSTOP) {
290 pid_t pid = event->proc->pid;
291 disable_all_breakpoints(event->proc);
292 untrace_pid(pid);
293 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200294 return;
295 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100296 output_line(event->proc, "--- %s (%s) ---",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100297 shortsignal(event->proc, event->e_un.signum),
298 strsignal(event->e_un.signum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100299 continue_after_signal(event->proc->pid, event->e_un.signum);
300}
301
Juan Cespedesf1350522008-12-16 18:19:58 +0100302static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200303process_exit(Event *event) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304 output_line(event->proc, "+++ exited (status %d) +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 event->e_un.ret_val);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100306 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100307}
308
Juan Cespedesf1350522008-12-16 18:19:58 +0100309static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200310process_exit_signal(Event *event) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100311 output_line(event->proc, "+++ killed by %s +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 shortsignal(event->proc, event->e_un.signum));
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100313 remove_proc(event->proc);
314}
315
Juan Cespedesf1350522008-12-16 18:19:58 +0100316static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200317remove_proc(Process *proc) {
318 Process *tmp, *tmp2;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100319
Juan Cespedescac15c32003-01-31 18:58:58 +0100320 debug(1, "Removing pid %u\n", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200321
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100322 if (list_of_processes == proc) {
323 tmp = list_of_processes;
324 list_of_processes = list_of_processes->next;
325 free(tmp);
326 return;
327 }
328 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100329 while (tmp->next) {
330 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100331 tmp2 = tmp->next;
332 tmp->next = tmp->next->next;
333 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200334 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100335 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100336 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100337 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100338}
339
Juan Cespedesf1350522008-12-16 18:19:58 +0100340static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200341process_syscall(Event *event) {
Juan Cespedesce377d52008-12-16 19:38:10 +0100342 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 output_left(LT_TOF_SYSCALL, event->proc,
344 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345 }
Juan Cespedes7c3b4312009-05-14 11:35:00 +0200346 if (event->proc->breakpoints_enabled == 0) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100347 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100349 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100350 continue_process(event->proc->pid);
351}
352
Juan Cespedesf1350522008-12-16 18:19:58 +0100353static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200354process_exec(Event * event) {
Juan Cespedes1e583132009-04-07 18:17:11 +0200355 output_line(event->proc, "--- exec() ---");
356 abort();
357}
358
359static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200360process_arch_syscall(Event *event) {
Juan Cespedesce377d52008-12-16 19:38:10 +0100361 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100362 output_left(LT_TOF_SYSCALL, event->proc,
363 arch_sysname(event->proc, event->e_un.sysnum));
364 }
365 if (event->proc->breakpoints_enabled == 0) {
366 enable_all_breakpoints(event->proc);
367 }
368 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
369 continue_process(event->proc->pid);
370}
371
Juan Cespedesd65efa32003-02-03 00:22:30 +0100372struct timeval current_time_spent;
373
Juan Cespedesf1350522008-12-16 18:19:58 +0100374static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200375calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100376 struct timeval tv;
377 struct timezone tz;
378 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100380
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100382
383 gettimeofday(&tv, &tz);
384
385 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
386 if (tv.tv_usec >= elem->time_spent.tv_usec) {
387 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
388 } else {
389 diff.tv_sec++;
390 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
391 }
392 current_time_spent = diff;
393}
394
Juan Cespedesf1350522008-12-16 18:19:58 +0100395static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200396process_sysret(Event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200397 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100398 calc_time_spent(event->proc);
399 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200400 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100401 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100402 output_right(LT_TOF_SYSCALLR, event->proc,
403 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200404 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100405 continue_process(event->proc->pid);
406}
407
Juan Cespedesf1350522008-12-16 18:19:58 +0100408static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200409process_arch_sysret(Event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200410 if (opt_T || options.summary) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100411 calc_time_spent(event->proc);
412 }
413 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100414 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100415 output_right(LT_TOF_SYSCALLR, event->proc,
416 arch_sysname(event->proc, event->e_un.sysnum));
417 }
418 continue_process(event->proc->pid);
419}
420
Juan Cespedesf1350522008-12-16 18:19:58 +0100421static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200422process_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200424 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100425
Juan Cespedesefe85f02004-04-04 01:31:38 +0200426 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100427
Paul Gilliam76c61f12006-06-14 06:55:21 +0200428#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100429 /* Need to skip following NOP's to prevent a fake function from being stacked. */
430 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200431 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100432 char nop_instruction[] = PPC_NOP;
433
434 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
435
436 if (stub_bp) {
437 unsigned char *bp_instruction = stub_bp->orig_value;
438
439 if (memcmp(bp_instruction, nop_instruction,
440 PPC_NOP_LENGTH) == 0) {
441 if (stub_addr != (long) event->e_un.brk_addr) {
442 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
443 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200444 return;
445 }
446 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100447 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200448#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100449 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100450 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 continue_enabling_breakpoint(event->proc->pid,
452 event->proc->
453 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100454 event->proc->breakpoint_being_enabled = NULL;
455 return;
456 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200457
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100458 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
459 if (event->e_un.brk_addr ==
460 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200461#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100462 /*
463 * PPC HACK! (XXX FIXME TODO)
464 * The PLT gets modified during the first call,
465 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100466 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100467 unsigned long a;
468 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100469 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200470 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200471
Paul Gilliam76c61f12006-06-14 06:55:21 +0200472 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100473 unsigned char break_insn[] = BREAKPOINT_VALUE;
474
475 sbp = address2bpstruct(event->proc, addr);
476 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100477 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
478 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100479
Paul Gilliam76c61f12006-06-14 06:55:21 +0200480 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100481 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100482 insert_breakpoint(event->proc, addr,
483 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100484 }
485 } else {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200486 sbp = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, libsym));
Ian Wienand9a2ad352006-02-20 22:44:45 +0100487 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200488 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100489 insert_breakpoint(event->proc, addr,
490 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200491 }
Ian Wienand3219f322006-02-16 06:00:00 +0100492 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100493#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200494 void *addr;
495 void *old_addr;
496 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200497 assert(sym);
498 old_addr = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, sym))->addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200499 addr=sym2addr(event->proc,sym);
500 assert(old_addr !=0 && addr !=0);
501 if(addr != old_addr){
502 struct library_symbol *new_sym;
503 new_sym=malloc(sizeof(*new_sym));
504 memcpy(new_sym,sym,sizeof(*new_sym));
505 new_sym->next=event->proc->list_of_symbols;
506 event->proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200507 insert_breakpoint(event->proc, addr, new_sym);
508 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200509#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100511 callstack_pop(event->proc);
512 }
Juan Cespedesda9b9532009-04-07 15:33:50 +0200513 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100514 calc_time_spent(event->proc);
515 }
516 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100517 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200518 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100519 event->proc->callstack[i].c_un.libfunc->
520 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100521 continue_after_breakpoint(event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 address2bpstruct(event->proc,
523 event->e_un.
524 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100525 return;
526 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100527 }
528
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100529 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
530 event->proc->stack_pointer = get_stack_pointer(event->proc);
531 event->proc->return_addr =
532 get_return_addr(event->proc, event->proc->stack_pointer);
533 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
534 callstack_push_symfunc(event->proc, sbp->libsym);
Paul Gilliambe320772006-04-24 22:06:23 +0200535#ifdef PLT_REINITALISATION_BP
536 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100537 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
538 0))
539 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200540#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100541
542 continue_after_breakpoint(event->proc, sbp);
543 return;
544 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100545
546 output_line(event->proc, "unexpected breakpoint at %p",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100547 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100548 continue_process(event->proc->pid);
549}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200550
Juan Cespedesf1350522008-12-16 18:19:58 +0100551static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200552callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100553 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200554
555 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100556 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200557 fprintf(stderr, "Error: call nesting too deep!\n");
558 return;
559 }
560
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200562 elem->is_syscall = 1;
563 elem->c_un.syscall = sysnum;
564 elem->return_addr = NULL;
565
566 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200567 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100568 struct timezone tz;
569 gettimeofday(&elem->time_spent, &tz);
570 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200571}
572
Juan Cespedes21c63a12001-07-07 20:56:56 +0200573static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200574callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100575 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200576
577 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100578 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200579 fprintf(stderr, "Error: call nesting too deep!\n");
580 return;
581 }
582
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100583 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200584 elem->is_syscall = 0;
585 elem->c_un.libfunc = sym;
586
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200587 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200588 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200589 insert_breakpoint(proc, elem->return_addr, 0);
590 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200591
592 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200593 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100594 struct timezone tz;
595 gettimeofday(&elem->time_spent, &tz);
596 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200597}
598
Juan Cespedesf1350522008-12-16 18:19:58 +0100599static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200600callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100601 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200602 assert(proc->callstack_depth > 0);
603
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200605 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200606 delete_breakpoint(proc, elem->return_addr);
607 }
608 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200609}