blob: 94ecaac49f9828d6247e5974fba32415c16dbfab [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) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020043 debug(DEBUG_FUNCTION, "address_clone(%p)", addr);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020044 return addr;
45}
46
47void * breakpoint_clone(void * bp) {
48 Breakpoint * b;
Juan Cespedescd8976d2009-05-14 13:47:58 +020049 debug(DEBUG_FUNCTION, "breakpoint_clone(%p)", bp);
Juan Cespedesbc8caf02009-05-07 19:38:38 +020050 b = malloc(sizeof(Breakpoint));
51 if (!b) {
52 perror("malloc()");
53 exit(1);
54 }
55 memcpy(b, bp, sizeof(Breakpoint));
56 return b;
57}
58
59typedef struct Pending_New Pending_New;
60struct Pending_New {
61 pid_t pid;
62 Pending_New * next;
63};
64static Pending_New * pending_news = NULL;
65
66static int
67pending_new(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020068 Pending_New * p;
69
70 debug(DEBUG_FUNCTION, "pending_new(%d)", pid);
71
72 p = pending_news;
Juan Cespedesbc8caf02009-05-07 19:38:38 +020073 while (p) {
74 if (p->pid == pid) {
75 return 1;
76 }
77 p = p->next;
78 }
79 return 0;
80}
81
82static void
83pending_new_insert(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020084 Pending_New * p;
85
86 debug(DEBUG_FUNCTION, "pending_new_insert(%d)", pid);
87
88 p = malloc(sizeof(Pending_New));
Juan Cespedesbc8caf02009-05-07 19:38:38 +020089 if (!p) {
90 perror("malloc()");
91 exit(1);
92 }
93 p->pid = pid;
94 p->next = pending_news;
95 pending_news = p;
96}
97
98static void
99pending_new_remove(pid_t pid) {
100 Pending_New *p, *pred;
101
Juan Cespedescd8976d2009-05-14 13:47:58 +0200102 debug(DEBUG_FUNCTION, "pending_new_remove(%d)", pid);
103
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200104 p = pending_news;
105 if (p->pid == pid) {
106 pending_news = p->next;
107 free(p);
108 } else {
109 while (p) {
110 if (p->pid == pid) {
111 pred->next = p->next;
112 free(p);
113 }
114 pred = p;
115 p = p->next;
116 }
117 }
118}
119
120static void
121process_clone(Event * event) {
122 Process *p;
123
Juan Cespedescd8976d2009-05-14 13:47:58 +0200124 debug(DEBUG_FUNCTION, "process_clone(pid=%d)", event->proc->pid);
125
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200126 p = malloc(sizeof(Process));
127 if (!p) {
128 perror("malloc()");
129 exit(1);
130 }
131 memcpy(p, event->proc, sizeof(Process));
132 p->breakpoints = dict_clone(event->proc->breakpoints, address_clone, breakpoint_clone);
133 p->pid = event->e_un.newpid;
134
135 if (pending_new(p->pid)) {
136 pending_new_remove(p->pid);
137 if (p->breakpoint_being_enabled) {
138 enable_breakpoint(p->pid, p->breakpoint_being_enabled);
139 p->breakpoint_being_enabled = NULL;
140 }
141 p->state = STATE_ATTACHED;
142 continue_process(p->pid);
143 p->next = list_of_processes;
144 list_of_processes = p;
145 } else {
146 p->state = STATE_BEING_CREATED;
147 }
148 /* look for previous process_new() */
149}
150
151static void
152process_new(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200153 Process * proc;
154
155 debug(DEBUG_FUNCTION, "process_new(pid=%d)", event->e_un.newpid);
156
157 proc = pid2proc(event->e_un.newpid);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200158 if (!proc) {
159 pending_new_insert(event->e_un.newpid);
160 } else {
161 assert(proc->state == STATE_BEING_CREATED);
162 if (proc->breakpoint_being_enabled) {
163 enable_breakpoint(proc->pid, proc->breakpoint_being_enabled);
164 proc->breakpoint_being_enabled = NULL;
165 }
166 proc->state = STATE_ATTACHED;
167 continue_process(proc->pid);
168 }
169}
170
Juan Cespedesf1350522008-12-16 18:19:58 +0100171static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200172shortsignal(Process *proc, int signum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100173 static char *signalent0[] = {
174#include "signalent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100175 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 static char *signalent1[] = {
177#include "signalent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100178 };
179 static char **signalents[] = { signalent0, signalent1 };
180 int nsignals[] = { sizeof signalent0 / sizeof signalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 sizeof signalent1 / sizeof signalent1[0]
182 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183
Juan Cespedescd8976d2009-05-14 13:47:58 +0200184 debug(DEBUG_FUNCTION, "shortsignal(pid=%d, signum=%d)", proc->pid, signum);
185
Ian Wienand9a2ad352006-02-20 22:44:45 +0100186 if (proc->personality > sizeof signalents / sizeof signalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100187 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100188 if (signum < 0 || signum >= nsignals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100189 return "UNKNOWN_SIGNAL";
190 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100191 return signalents[proc->personality][signum];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100192 }
193}
194
Juan Cespedesf1350522008-12-16 18:19:58 +0100195static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200196sysname(Process *proc, int sysnum) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 static char result[128];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 static char *syscalent0[] = {
199#include "syscallent.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 };
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 static char *syscalent1[] = {
202#include "syscallent1.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +0100203 };
204 static char **syscalents[] = { syscalent0, syscalent1 };
205 int nsyscals[] = { sizeof syscalent0 / sizeof syscalent0[0],
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 sizeof syscalent1 / sizeof syscalent1[0]
207 };
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208
Juan Cespedescd8976d2009-05-14 13:47:58 +0200209 debug(DEBUG_FUNCTION, "sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
210
Ian Wienand9a2ad352006-02-20 22:44:45 +0100211 if (proc->personality > sizeof syscalents / sizeof syscalents[0])
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 abort();
Ian Wienand9a2ad352006-02-20 22:44:45 +0100213 if (sysnum < 0 || sysnum >= nsyscals[proc->personality]) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100214 sprintf(result, "SYS_%d", sysnum);
215 return result;
216 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 sprintf(result, "SYS_%s",
218 syscalents[proc->personality][sysnum]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219 return result;
220 }
221}
222
Juan Cespedesf1350522008-12-16 18:19:58 +0100223static char *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200224arch_sysname(Process *proc, int sysnum) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100225 static char result[128];
226 static char *arch_syscalent[] = {
227#include "arch_syscallent.h"
228 };
229 int nsyscals = sizeof arch_syscalent / sizeof arch_syscalent[0];
230
Juan Cespedescd8976d2009-05-14 13:47:58 +0200231 debug(DEBUG_FUNCTION, "arch_sysname(pid=%d, sysnum=%d)", proc->pid, sysnum);
232
Juan Cespedes63184be2008-12-10 13:30:12 +0100233 if (sysnum < 0 || sysnum >= nsyscals) {
234 sprintf(result, "ARCH_%d", sysnum);
235 return result;
236 } else {
237 sprintf(result, "ARCH_%s",
238 arch_syscalent[sysnum]);
239 return result;
240 }
241}
242
Juan Cespedesf1350522008-12-16 18:19:58 +0100243void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200244process_event(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200245 debug(DEBUG_FUNCTION, "process_event(pid=%d, type=%d)", event->proc ? event->proc->pid : -1, event->type);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200246 switch (event->type) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200247 case EVENT_NONE:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 debug(1, "event: none");
249 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200250 case EVENT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 debug(1, "event: signal (%s [%d])",
252 shortsignal(event->proc, event->e_un.signum),
253 event->e_un.signum);
254 process_signal(event);
255 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200256 case EVENT_EXIT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 debug(1, "event: exit (%d)", event->e_un.ret_val);
258 process_exit(event);
259 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200260 case EVENT_EXIT_SIGNAL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100261 debug(1, "event: exit signal (%s [%d])",
262 shortsignal(event->proc, event->e_un.signum),
263 event->e_un.signum);
264 process_exit_signal(event);
265 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200266 case EVENT_SYSCALL:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100267 debug(1, "event: syscall (%s [%d])",
268 sysname(event->proc, event->e_un.sysnum),
269 event->e_un.sysnum);
270 process_syscall(event);
271 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200272 case EVENT_SYSRET:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 debug(1, "event: sysret (%s [%d])",
274 sysname(event->proc, event->e_un.sysnum),
275 event->e_un.sysnum);
276 process_sysret(event);
277 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200278 case EVENT_ARCH_SYSCALL:
Juan Cespedes63184be2008-12-10 13:30:12 +0100279 debug(1, "event: arch_syscall (%s [%d])",
280 arch_sysname(event->proc, event->e_un.sysnum),
281 event->e_un.sysnum);
282 process_arch_syscall(event);
283 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200284 case EVENT_ARCH_SYSRET:
Juan Cespedes63184be2008-12-10 13:30:12 +0100285 debug(1, "event: arch_sysret (%s [%d])",
286 arch_sysname(event->proc, event->e_un.sysnum),
287 event->e_un.sysnum);
288 process_arch_sysret(event);
289 return;
Juan Cespedes1e583132009-04-07 18:17:11 +0200290 case EVENT_CLONE:
291 debug(1, "event: clone (%u)", event->e_un.newpid);
292 process_clone(event);
293 return;
294 case EVENT_EXEC:
295 debug(1, "event: exec()");
296 process_exec(event);
297 return;
Juan Cespedes138d41c2009-04-07 00:49:12 +0200298 case EVENT_BREAKPOINT:
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 debug(1, "event: breakpoint");
300 process_breakpoint(event);
301 return;
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200302 case EVENT_NEW:
303 debug(1, "event: new process");
304 process_new(event);
305 return;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 default:
307 fprintf(stderr, "Error! unknown event?\n");
308 exit(1);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200309 }
310}
311
Juan Cespedesf1350522008-12-16 18:19:58 +0100312static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200313process_signal(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200314 debug(DEBUG_FUNCTION, "process_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes28f60191998-04-12 00:04:39 +0200315 if (exiting && event->e_un.signum == SIGSTOP) {
316 pid_t pid = event->proc->pid;
317 disable_all_breakpoints(event->proc);
318 untrace_pid(pid);
319 remove_proc(event->proc);
Juan Cespedes28f60191998-04-12 00:04:39 +0200320 return;
321 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100322 output_line(event->proc, "--- %s (%s) ---",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100323 shortsignal(event->proc, event->e_un.signum),
324 strsignal(event->e_un.signum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100325 continue_after_signal(event->proc->pid, event->e_un.signum);
326}
327
Juan Cespedesf1350522008-12-16 18:19:58 +0100328static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200329process_exit(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200330 debug(DEBUG_FUNCTION, "process_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100331 output_line(event->proc, "+++ exited (status %d) +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100332 event->e_un.ret_val);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100333 remove_proc(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100334}
335
Juan Cespedesf1350522008-12-16 18:19:58 +0100336static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200337process_exit_signal(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200338 debug(DEBUG_FUNCTION, "process_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100339 output_line(event->proc, "+++ killed by %s +++",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100340 shortsignal(event->proc, event->e_un.signum));
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100341 remove_proc(event->proc);
342}
343
Juan Cespedesf1350522008-12-16 18:19:58 +0100344static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200345remove_proc(Process *proc) {
346 Process *tmp, *tmp2;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100347
Juan Cespedescd8976d2009-05-14 13:47:58 +0200348 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +0200349
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100350 if (list_of_processes == proc) {
351 tmp = list_of_processes;
352 list_of_processes = list_of_processes->next;
353 free(tmp);
354 return;
355 }
356 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 while (tmp->next) {
358 if (tmp->next == proc) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100359 tmp2 = tmp->next;
360 tmp->next = tmp->next->next;
361 free(tmp2);
Juan Cespedes28f60191998-04-12 00:04:39 +0200362 continue;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100363 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100364 tmp = tmp->next;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100365 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100366}
367
Juan Cespedesf1350522008-12-16 18:19:58 +0100368static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200369process_syscall(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200370 debug(DEBUG_FUNCTION, "process_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedesce377d52008-12-16 19:38:10 +0100371 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 output_left(LT_TOF_SYSCALL, event->proc,
373 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100374 }
Juan Cespedes7c3b4312009-05-14 11:35:00 +0200375 if (event->proc->breakpoints_enabled == 0) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100376 enable_all_breakpoints(event->proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100377 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100378 callstack_push_syscall(event->proc, event->e_un.sysnum);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100379 continue_process(event->proc->pid);
380}
381
Juan Cespedesf1350522008-12-16 18:19:58 +0100382static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200383process_exec(Event * event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200384 debug(DEBUG_FUNCTION, "process_exec(pid=%d)", event->proc->pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200385 output_line(event->proc, "--- exec() ---");
386 abort();
387}
388
389static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200390process_arch_syscall(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200391 debug(DEBUG_FUNCTION, "process_arch_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedesce377d52008-12-16 19:38:10 +0100392 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100393 output_left(LT_TOF_SYSCALL, event->proc,
394 arch_sysname(event->proc, event->e_un.sysnum));
395 }
396 if (event->proc->breakpoints_enabled == 0) {
397 enable_all_breakpoints(event->proc);
398 }
399 callstack_push_syscall(event->proc, 0xf0000 + event->e_un.sysnum);
400 continue_process(event->proc->pid);
401}
402
Juan Cespedesd65efa32003-02-03 00:22:30 +0100403struct timeval current_time_spent;
404
Juan Cespedesf1350522008-12-16 18:19:58 +0100405static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200406calc_time_spent(Process *proc) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100407 struct timeval tv;
408 struct timezone tz;
409 struct timeval diff;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100410 struct callstack_element *elem;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100411
Juan Cespedescd8976d2009-05-14 13:47:58 +0200412 debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100413 elem = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesd65efa32003-02-03 00:22:30 +0100414
415 gettimeofday(&tv, &tz);
416
417 diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
418 if (tv.tv_usec >= elem->time_spent.tv_usec) {
419 diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
420 } else {
421 diff.tv_sec++;
422 diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
423 }
424 current_time_spent = diff;
425}
426
Juan Cespedesf1350522008-12-16 18:19:58 +0100427static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200428process_sysret(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200429 debug(DEBUG_FUNCTION, "process_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedesda9b9532009-04-07 15:33:50 +0200430 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100431 calc_time_spent(event->proc);
432 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200433 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100434 if (options.syscalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100435 output_right(LT_TOF_SYSCALLR, event->proc,
436 sysname(event->proc, event->e_un.sysnum));
Juan Cespedes21c63a12001-07-07 20:56:56 +0200437 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100438 continue_process(event->proc->pid);
439}
440
Juan Cespedesf1350522008-12-16 18:19:58 +0100441static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200442process_arch_sysret(Event *event) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200443 debug(DEBUG_FUNCTION, "process_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
Juan Cespedesda9b9532009-04-07 15:33:50 +0200444 if (opt_T || options.summary) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100445 calc_time_spent(event->proc);
446 }
447 callstack_pop(event->proc);
Juan Cespedesce377d52008-12-16 19:38:10 +0100448 if (options.syscalls) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100449 output_right(LT_TOF_SYSCALLR, event->proc,
450 arch_sysname(event->proc, event->e_un.sysnum));
451 }
452 continue_process(event->proc->pid);
453}
454
Juan Cespedesf1350522008-12-16 18:19:58 +0100455static void
Juan Cespedes393f1d02009-05-07 11:13:54 +0200456process_breakpoint(Event *event) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 int i, j;
Juan Cespedes1dec2172009-05-07 10:12:10 +0200458 Breakpoint *sbp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100459
Juan Cespedescd8976d2009-05-14 13:47:58 +0200460 debug(DEBUG_FUNCTION, "process_breakpoint(pid=%d, addr=%p)", event->proc->pid, event->e_un.brk_addr);
Juan Cespedesefe85f02004-04-04 01:31:38 +0200461 debug(2, "event: breakpoint (%p)", event->e_un.brk_addr);
Luis Machado55c5feb2008-03-12 15:56:01 +0100462
Paul Gilliam76c61f12006-06-14 06:55:21 +0200463#ifdef __powerpc__
Luis Machado55c5feb2008-03-12 15:56:01 +0100464 /* Need to skip following NOP's to prevent a fake function from being stacked. */
465 long stub_addr = (long) get_count_register(event->proc);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200466 Breakpoint *stub_bp = NULL;
Luis Machado55c5feb2008-03-12 15:56:01 +0100467 char nop_instruction[] = PPC_NOP;
468
469 stub_bp = address2bpstruct (event->proc, event->e_un.brk_addr);
470
471 if (stub_bp) {
472 unsigned char *bp_instruction = stub_bp->orig_value;
473
474 if (memcmp(bp_instruction, nop_instruction,
475 PPC_NOP_LENGTH) == 0) {
476 if (stub_addr != (long) event->e_un.brk_addr) {
477 set_instruction_pointer (event->proc, event->e_un.brk_addr + 4);
478 continue_process(event->proc->pid);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200479 return;
480 }
481 }
Luis Machado55c5feb2008-03-12 15:56:01 +0100482 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200483#endif
Luis Machado55c5feb2008-03-12 15:56:01 +0100484 if ((sbp = event->proc->breakpoint_being_enabled) != 0) {
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100485 /* Reinsert breakpoint */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100486 continue_enabling_breakpoint(event->proc->pid,
487 event->proc->
488 breakpoint_being_enabled);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100489 event->proc->breakpoint_being_enabled = NULL;
490 return;
491 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200492
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100493 for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
494 if (event->e_un.brk_addr ==
495 event->proc->callstack[i].return_addr) {
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200496#ifdef __powerpc__
Ian Wienand3219f322006-02-16 06:00:00 +0100497 /*
498 * PPC HACK! (XXX FIXME TODO)
499 * The PLT gets modified during the first call,
500 * so be sure to re-enable the breakpoint.
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100502 unsigned long a;
503 struct library_symbol *libsym =
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100504 event->proc->callstack[i].c_un.libfunc;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200505 void *addr = sym2addr(event->proc, libsym);
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200506
Paul Gilliam76c61f12006-06-14 06:55:21 +0200507 if (libsym->plt_type != LS_TOPLT_POINT) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100508 unsigned char break_insn[] = BREAKPOINT_VALUE;
509
510 sbp = address2bpstruct(event->proc, addr);
511 assert(sbp);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 a = ptrace(PTRACE_PEEKTEXT, event->proc->pid,
513 addr);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100514
Paul Gilliam76c61f12006-06-14 06:55:21 +0200515 if (memcmp(&a, break_insn, BREAKPOINT_LENGTH)) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100516 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100517 insert_breakpoint(event->proc, addr,
518 libsym);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100519 }
520 } else {
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200521 sbp = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, libsym));
Ian Wienand9a2ad352006-02-20 22:44:45 +0100522 assert(sbp);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200523 if (addr != sbp->addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 insert_breakpoint(event->proc, addr,
525 libsym);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200526 }
Ian Wienand3219f322006-02-16 06:00:00 +0100527 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100528#elif defined(__mips__)
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200529 void *addr;
530 void *old_addr;
531 struct library_symbol *sym= event->proc->callstack[i].c_un.libfunc;
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200532 assert(sym);
533 old_addr = dict_find_entry(event->proc->breakpoints, sym2addr(event->proc, sym))->addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200534 addr=sym2addr(event->proc,sym);
535 assert(old_addr !=0 && addr !=0);
536 if(addr != old_addr){
537 struct library_symbol *new_sym;
538 new_sym=malloc(sizeof(*new_sym));
539 memcpy(new_sym,sym,sizeof(*new_sym));
540 new_sym->next=event->proc->list_of_symbols;
541 event->proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200542 insert_breakpoint(event->proc, addr, new_sym);
543 }
Juan Cespedes5bfb0612002-03-31 20:01:28 +0200544#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100545 for (j = event->proc->callstack_depth - 1; j > i; j--) {
Juan Cespedes5916fda2002-02-25 00:19:21 +0100546 callstack_pop(event->proc);
547 }
Juan Cespedesda9b9532009-04-07 15:33:50 +0200548 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100549 calc_time_spent(event->proc);
550 }
551 callstack_pop(event->proc);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100552 event->proc->return_addr = event->e_un.brk_addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200553 output_right(LT_TOF_FUNCTIONR, event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100554 event->proc->callstack[i].c_un.libfunc->
555 name);
Juan Cespedes5916fda2002-02-25 00:19:21 +0100556 continue_after_breakpoint(event->proc,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100557 address2bpstruct(event->proc,
558 event->e_un.
559 brk_addr));
Juan Cespedes5916fda2002-02-25 00:19:21 +0100560 return;
561 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100562 }
563
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100564 if ((sbp = address2bpstruct(event->proc, event->e_un.brk_addr))) {
565 event->proc->stack_pointer = get_stack_pointer(event->proc);
566 event->proc->return_addr =
567 get_return_addr(event->proc, event->proc->stack_pointer);
568 output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym->name);
569 callstack_push_symfunc(event->proc, sbp->libsym);
Paul Gilliambe320772006-04-24 22:06:23 +0200570#ifdef PLT_REINITALISATION_BP
571 if (event->proc->need_to_reinitialize_breakpoints
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100572 && (strcmp(sbp->libsym->name, PLTs_initialized_by_here) ==
573 0))
574 reinitialize_breakpoints(event->proc);
Paul Gilliambe320772006-04-24 22:06:23 +0200575#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576
577 continue_after_breakpoint(event->proc, sbp);
578 return;
579 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100580
581 output_line(event->proc, "unexpected breakpoint at %p",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100582 (void *)event->e_un.brk_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100583 continue_process(event->proc->pid);
584}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200585
Juan Cespedesf1350522008-12-16 18:19:58 +0100586static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200587callstack_push_syscall(Process *proc, int sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200589
Juan Cespedescd8976d2009-05-14 13:47:58 +0200590 debug(DEBUG_FUNCTION, "callstack_push_syscall(pid=%d, sysnum=%d)", proc->pid, sysnum);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200591 /* 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 = 1;
599 elem->c_un.syscall = sysnum;
600 elem->return_addr = NULL;
601
602 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200603 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100604 struct timezone tz;
605 gettimeofday(&elem->time_spent, &tz);
606 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200607}
608
Juan Cespedes21c63a12001-07-07 20:56:56 +0200609static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200610callstack_push_symfunc(Process *proc, struct library_symbol *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100611 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200612
Juan Cespedescd8976d2009-05-14 13:47:58 +0200613 debug(DEBUG_FUNCTION, "callstack_push_symfunc(pid=%d, symbol=%s)", proc->pid, sym->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200614 /* FIXME: not good -- should use dynamic allocation. 19990703 mortene. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100615 if (proc->callstack_depth == MAX_CALLDEPTH - 1) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200616 fprintf(stderr, "Error: call nesting too deep!\n");
617 return;
618 }
619
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100620 elem = &proc->callstack[proc->callstack_depth];
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200621 elem->is_syscall = 0;
622 elem->c_un.libfunc = sym;
623
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200624 elem->return_addr = proc->return_addr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200625 if (elem->return_addr) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200626 insert_breakpoint(proc, elem->return_addr, 0);
627 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200628
629 proc->callstack_depth++;
Juan Cespedesda9b9532009-04-07 15:33:50 +0200630 if (opt_T || options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100631 struct timezone tz;
632 gettimeofday(&elem->time_spent, &tz);
633 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200634}
635
Juan Cespedesf1350522008-12-16 18:19:58 +0100636static void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200637callstack_pop(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100638 struct callstack_element *elem;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200639 assert(proc->callstack_depth > 0);
640
Juan Cespedescd8976d2009-05-14 13:47:58 +0200641 debug(DEBUG_FUNCTION, "callstack_pop(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100642 elem = &proc->callstack[proc->callstack_depth - 1];
Paul Gilliam76c61f12006-06-14 06:55:21 +0200643 if (!elem->is_syscall && elem->return_addr) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200644 delete_breakpoint(proc, elem->return_addr);
645 }
646 proc->callstack_depth--;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200647}