blob: 043ed5645d6462aa68756b4db6f1be4a9835b9ea [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02005#include <stdlib.h>
Juan Cespedes7186e2a2003-01-31 19:56:34 +01006#include <string.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02007#include <assert.h>
8
Juan Cespedesf1bfe202002-03-27 00:22:23 +01009#ifdef __powerpc__
10#include <sys/ptrace.h>
11#endif
12
Juan Cespedescac15c32003-01-31 18:58:58 +010013#include "ltrace.h"
14#include "options.h"
15#include "debug.h"
16#include "dict.h"
Juan Cespedes7186e2a2003-01-31 19:56:34 +010017#include "elf.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020018
19/*****************************************************************************/
20
Juan Cespedesf1350522008-12-16 18:19:58 +010021struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020022address2bpstruct(Process *proc, void *addr) {
Juan Cespedescac15c32003-01-31 18:58:58 +010023 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020024}
25
Ian Wienand9a2ad352006-02-20 22:44:45 +010026void
Juan Cespedesa8909f72009-04-28 20:02:41 +020027insert_breakpoint(Process *proc, void *addr,
Juan Cespedesf1350522008-12-16 18:19:58 +010028 struct library_symbol *libsym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029 struct breakpoint *sbp;
Petr Machatab3f8fef2006-11-30 14:45:07 +010030 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020031
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 if (!addr)
33 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010034
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010036 libsym->needs_init = 0;
37
Juan Cespedescac15c32003-01-31 18:58:58 +010038 sbp = dict_find_entry(proc->breakpoints, addr);
39 if (!sbp) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010040 sbp = calloc(1, sizeof(struct breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010041 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010043 }
44 dict_enter(proc->breakpoints, addr, sbp);
45 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010046 sbp->libsym = libsym;
47 if (libsym)
48 libsym->brkpnt = sbp;
Juan Cespedescac15c32003-01-31 18:58:58 +010049 }
Juan Cespedes63184be2008-12-10 13:30:12 +010050#ifdef __arm__
51 sbp->thumb_mode = proc->thumb_mode;
52 proc->thumb_mode = 0;
53#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020054 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055 if (sbp->enabled == 1 && proc->pid)
56 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020057}
58
Juan Cespedesf1350522008-12-16 18:19:58 +010059void
Juan Cespedesa8909f72009-04-28 20:02:41 +020060delete_breakpoint(Process *proc, void *addr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
62 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020063 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 if (sbp == NULL)
65 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020066
67 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 if (sbp->enabled == 0)
69 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020070 assert(sbp->enabled >= 0);
71}
72
Juan Cespedesf1350522008-12-16 18:19:58 +010073static void
74enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescac15c32003-01-31 18:58:58 +010075 if (((struct breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020076 enable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010077 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020078}
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080void
Juan Cespedesa8909f72009-04-28 20:02:41 +020081enable_all_breakpoints(Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010082 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010083#ifdef __powerpc__
84 unsigned long a;
85
86 /*
87 * PPC HACK! (XXX FIXME TODO)
88 * If the dynamic linker hasn't populated the PLT then
89 * dont enable the breakpoints
90 */
Juan Cespedesce377d52008-12-16 19:38:10 +010091 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020093 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020095 if (a == 0x0)
96 return;
97 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +010098#endif
99
Juan Cespedescac15c32003-01-31 18:58:58 +0100100 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100101 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
103 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100104 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100105#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200106 {
107 // I'm sure there is a nicer way to do this. We need to
108 // insert breakpoints _after_ the child has been started.
109 struct library_symbol *sym;
110 struct library_symbol *new_sym;
111 sym=proc->list_of_symbols;
112 while(sym){
113 void *addr= sym2addr(proc,sym);
114 if(!addr){
115 sym=sym->next;
116 continue;
117 }
118 if(dict_find_entry(proc->breakpoints,addr)){
119 sym=sym->next;
120 continue;
121 }
122 debug(2,"inserting bp %p %s",addr,sym->name);
123 new_sym=malloc(sizeof(*new_sym));
124 memcpy(new_sym,sym,sizeof(*new_sym));
125 new_sym->next=proc->list_of_symbols;
126 proc->list_of_symbols=new_sym;
127 new_sym->brkpnt=0;
128 insert_breakpoint(proc, addr, new_sym);
129 sym=sym->next;
130 }
131 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100132#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133 }
134 proc->breakpoints_enabled = 1;
135}
136
Juan Cespedesf1350522008-12-16 18:19:58 +0100137static void
138disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100139 if (((struct breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200140 disable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100141 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200142}
143
Juan Cespedesf1350522008-12-16 18:19:58 +0100144void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200145disable_all_breakpoints(Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100147 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
148 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149 }
150 proc->breakpoints_enabled = 0;
151}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100152
Juan Cespedesf1350522008-12-16 18:19:58 +0100153static void
154free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100155 assert(sbp);
156 free(sbp);
157}
158
Juan Cespedesf1350522008-12-16 18:19:58 +0100159void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200160breakpoints_init(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100161 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100162
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100163 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100164 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
165 dict_clear(proc->breakpoints);
166 proc->breakpoints = NULL;
167 }
Petr Machata89a53602007-01-25 18:05:44 +0100168 proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100169
Juan Cespedesce377d52008-12-16 19:38:10 +0100170 if (options.libcalls && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100171 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100172 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100173 struct library_symbol **tmp1 = &(proc->list_of_symbols);
174 while (*tmp1) {
175 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100176 int keep = !opt_e_enable;
177
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100178 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100179 if (!strcmp((*tmp1)->name, tmp2->name)) {
180 keep = opt_e_enable;
181 }
182 tmp2 = tmp2->next;
183 }
184 if (!keep) {
185 *tmp1 = (*tmp1)->next;
186 } else {
187 tmp1 = &((*tmp1)->next);
188 }
189 }
190 }
191 } else {
192 proc->list_of_symbols = NULL;
193 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100194 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200196 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100197 }
198 proc->callstack_depth = 0;
199 proc->breakpoints_enabled = -1;
200}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100201
Juan Cespedesf1350522008-12-16 18:19:58 +0100202void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200203reinitialize_breakpoints(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100204 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100205
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 while (sym) {
207 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200208 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 sym);
210 if (sym->needs_init && !sym->is_weak) {
211 fprintf(stderr,
212 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
213 sym->name, proc->filename);
214 exit(1);
215 }
216 }
217 sym = sym->next;
218 }
219}