blob: f7a88b0475680df62257eb92e62ca3f391428755 [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 Cespedesaee09312007-08-31 18:49:48 +0200346 if (fork_p(event->proc, event->e_un.sysnum)) {
347 disable_all_breakpoints(event->proc);
348 } else if (event->proc->breakpoints_enabled == 0) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100349 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100350 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100351 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100352 continue_process(event->proc->pid);
353}
354
Juan Cespedesf1350522008-12-16 18:19:58 +0100355static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200356process_exec(Event * event) {
Juan Cespedes1e583132009-04-07 18:17:11 +0200357 output_line(event->proc, "--- exec() ---");
358 abort();
359}
360
361static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200362process_arch_syscall(Event *event) {
Juan Cespedesce377d52008-12-16 19:38:10 +0100363 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100364 output_left(LT_TOF_SYSCALL, event->proc,
365 arch_sysname(event->proc, event->e_un.sysnum));
366 }
367 if (event->proc->breakpoints_enabled == 0) {
368 enable_all_breakpoints(event->proc);
369 }
370 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
371 continue_process(event->proc->pid);
372}
373
Juan Cespedesd65efa32003-02-03 00:22:30 +0100374struct timeval current_time_spent;
375
Juan Cespedesf1350522008-12-16 18:19:58 +0100376static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200377calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100378 struct timeval tv;
379 struct timezone tz;
380 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100382
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100384
385 gettimeofday(&tv, &tz);
386
387 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
388 if (tv.tv_usec >= elem->time_spent.tv_usec) {
389 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
390 } else {
391 diff.tv_sec++;
392 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
393 }
394 current_time_spent = diff;
395}
396
Juan Cespedesf1350522008-12-16 18:19:58 +0100397static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200398process_sysret(Event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200399 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100400 calc_time_spent(event->proc);
401 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100402 if (fork_p(event->proc, event->e_un.sysnum)) {
Juan Cespedescc813cd2009-04-07 15:45:53 +0200403 if (options.follow) {
Steve Fink65b53df2006-09-25 02:27:08 +0200404 arg_type_info info;
Steve Fink65b53df2006-09-25 02:27:08 +0200405 info.type = ARGTYPE_LONG;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 pid_t child =
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200407 gimme_arg(LT_TOF_SYSCALLR, event->proc, -1, &info);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 if (child > 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100409 open_pid(child, 0);
410 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100411 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100412 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100413 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200414 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100415 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 output_right(LT_TOF_SYSCALLR, event->proc,
417 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200418 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100419 continue_process(event->proc->pid);
420}
421
Juan Cespedesf1350522008-12-16 18:19:58 +0100422static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200423process_arch_sysret(Event *event) {
Juan Cespedesda9b9532009-04-07 15:33:50 +0200424 if (opt_T || options.summary) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100425 calc_time_spent(event->proc);
426 }
427 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100428 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100429 output_right(LT_TOF_SYSCALLR, event->proc,
430 arch_sysname(event->proc, event->e_un.sysnum));
431 }
432 continue_process(event->proc->pid);
433}
434
Juan Cespedesf1350522008-12-16 18:19:58 +0100435static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200436process_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200438 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100439
Juan Cespedesefe85f02004-04-04 01:31:38 +0200440 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100441
Paul Gilliam76c61f12006-06-14 06:55:21 +0200442#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100443 /* Need to skip following NOP's to prevent a fake function from being stacked. */
444 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200445 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100446 char nop_instruction[] = PPC_NOP;
447
448 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
449
450 if (stub_bp) {
451 unsigned char *bp_instruction = stub_bp->orig_value;
452
453 if (memcmp(bp_instruction, nop_instruction,
454 PPC_NOP_LENGTH) == 0) {
455 if (stub_addr != (long) event->e_un.brk_addr) {
456 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
457 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200458 return;
459 }
460 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100461 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200462#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100463 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100464 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 continue_enabling_breakpoint(event->proc->pid,
466 event->proc->
467 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100468 event->proc->breakpoint_being_enabled = NULL;
469 return;
470 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200471
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
473 if (event->e_un.brk_addr ==
474 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200475#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100476 /*
477 * PPC HACK! (XXX FIXME TODO)
478 * The PLT gets modified during the first call,
479 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100480 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100481 unsigned long a;
482 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200484 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200485
Paul Gilliam76c61f12006-06-14 06:55:21 +0200486 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100487 unsigned char break_insn[] = BREAKPOINT_VALUE;
488
489 sbp = address2bpstruct(event->proc, addr);
490 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100491 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
492 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100493
Paul Gilliam76c61f12006-06-14 06:55:21 +0200494 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100495 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100496 insert_breakpoint(event->proc, addr,
497 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100498 }
499 } else {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200500 sbp = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, libsym));
Ian Wienand9a2ad352006-02-20 22:44:45 +0100501 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200502 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100503 insert_breakpoint(event->proc, addr,
504 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200505 }
Ian Wienand3219f322006-02-16 06:00:00 +0100506 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100507#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200508 void *addr;
509 void *old_addr;
510 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200511 assert(sym);
512 old_addr = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, sym))->addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200513 addr=sym2addr(event->proc,sym);
514 assert(old_addr !=0 && addr !=0);
515 if(addr != old_addr){
516 struct library_symbol *new_sym;
517 new_sym=malloc(sizeof(*new_sym));
518 memcpy(new_sym,sym,sizeof(*new_sym));
519 new_sym->next=event->proc->list_of_symbols;
520 event->proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200521 insert_breakpoint(event->proc, addr, new_sym);
522 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200523#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100525 callstack_pop(event->proc);
526 }
Juan Cespedesda9b9532009-04-07 15:33:50 +0200527 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100528 calc_time_spent(event->proc);
529 }
530 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100531 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200532 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100533 event->proc->callstack[i].c_un.libfunc->
534 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100535 continue_after_breakpoint(event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100536 address2bpstruct(event->proc,
537 event->e_un.
538 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100539 return;
540 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100541 }
542
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100543 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
544 event->proc->stack_pointer = get_stack_pointer(event->proc);
545 event->proc->return_addr =
546 get_return_addr(event->proc, event->proc->stack_pointer);
547 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
548 callstack_push_symfunc(event->proc, sbp->libsym);
Paul Gilliambe320772006-04-24 22:06:23 +0200549#ifdef PLT_REINITALISATION_BP
550 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
552 0))
553 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200554#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100555
556 continue_after_breakpoint(event->proc, sbp);
557 return;
558 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100559
560 output_line(event->proc, "unexpected breakpoint at %p",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100562 continue_process(event->proc->pid);
563}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200564
Juan Cespedesf1350522008-12-16 18:19:58 +0100565static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200566callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100567 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200568
569 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100570 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200571 fprintf(stderr, "Error: call nesting too deep!\n");
572 return;
573 }
574
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100575 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200576 elem->is_syscall = 1;
577 elem->c_un.syscall = sysnum;
578 elem->return_addr = NULL;
579
580 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200581 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100582 struct timezone tz;
583 gettimeofday(&elem->time_spent, &tz);
584 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200585}
586
Juan Cespedes21c63a12001-07-07 20:56:56 +0200587static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200588callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100589 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200590
591 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200593 fprintf(stderr, "Error: call nesting too deep!\n");
594 return;
595 }
596
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100597 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200598 elem->is_syscall = 0;
599 elem->c_un.libfunc = sym;
600
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200601 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200602 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200603 insert_breakpoint(proc, elem->return_addr, 0);
604 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200605
606 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200607 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100608 struct timezone tz;
609 gettimeofday(&elem->time_spent, &tz);
610 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200611}
612
Juan Cespedesf1350522008-12-16 18:19:58 +0100613static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200614callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200616 assert(proc->callstack_depth > 0);
617
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100618 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200619 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200620 delete_breakpoint(proc, elem->return_addr);
621 }
622 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200623}