blob: 055c591dada74bc08273fbf0d37fe2fd3d92f36d [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02003#include <stdlib.h>
Juan Cespedes7186e2a2003-01-31 19:56:34 +01004#include <string.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02005#include <assert.h>
6
Juan Cespedesf1bfe202002-03-27 00:22:23 +01007#ifdef __powerpc__
8#include <sys/ptrace.h>
9#endif
10
Juan Cespedesf7281232009-06-25 16:11:21 +020011#include "common.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020012
13/*****************************************************************************/
14
Juan Cespedes1dec2172009-05-07 10:12:10 +020015Breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020016address2bpstruct(Process *proc, void *addr) {
Petr Machata26627682011-07-08 18:15:32 +020017 assert(proc != NULL);
18 assert(proc->breakpoints != NULL);
Juan Cespedescd8976d2009-05-14 13:47:58 +020019 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010020 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020021}
22
Ian Wienand9a2ad352006-02-20 22:44:45 +010023void
Juan Cespedesa8909f72009-04-28 20:02:41 +020024insert_breakpoint(Process *proc, void *addr,
Juan Cespedesf1350522008-12-16 18:19:58 +010025 struct library_symbol *libsym) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020026 Breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020027
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070028#ifdef __arm__
29 int thumb_mode = (int)addr & 1;
30 if (thumb_mode)
31 addr = (void *)((int)addr & ~1);
32#endif
33
Juan Cespedescd8976d2009-05-14 13:47:58 +020034 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)", proc->pid, addr, libsym ? libsym->name : "NULL");
Petr Machatab3f8fef2006-11-30 14:45:07 +010035 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036
Ian Wienand2d45b1a2006-02-20 22:48:07 +010037 if (!addr)
38 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010039
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010041 libsym->needs_init = 0;
42
Juan Cespedescac15c32003-01-31 18:58:58 +010043 sbp = dict_find_entry(proc->breakpoints, addr);
44 if (!sbp) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020045 sbp = calloc(1, sizeof(Breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010046 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010047 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010048 }
49 dict_enter(proc->breakpoints, addr, sbp);
50 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010052 }
Juan Cespedes63184be2008-12-10 13:30:12 +010053#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070054 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010055 proc->thumb_mode = 0;
56#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020057 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058 if (sbp->enabled == 1 && proc->pid)
59 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020060}
61
Juan Cespedesf1350522008-12-16 18:19:58 +010062void
Juan Cespedesa8909f72009-04-28 20:02:41 +020063delete_breakpoint(Process *proc, void *addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020064 Breakpoint *sbp;
65
66 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
67
68 sbp = dict_find_entry(proc->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020070 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 if (sbp == NULL)
72 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020073
74 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 if (sbp->enabled == 0)
76 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020077 assert(sbp->enabled >= 0);
78}
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080static void
81enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020082 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +020083 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020084 enable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010085 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020086}
87
Juan Cespedesf1350522008-12-16 18:19:58 +010088void
Juan Cespedesa8909f72009-04-28 20:02:41 +020089enable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020090 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010091 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010092#ifdef __powerpc__
93 unsigned long a;
94
95 /*
96 * PPC HACK! (XXX FIXME TODO)
97 * If the dynamic linker hasn't populated the PLT then
98 * dont enable the breakpoints
99 */
Juan Cespedesce377d52008-12-16 19:38:10 +0100100 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200102 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +0200104 if (a == 0x0)
105 return;
106 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100107#endif
108
Juan Cespedescac15c32003-01-31 18:58:58 +0100109 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100110 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
112 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100113 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100114#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200115 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200116 /*
117 * I'm sure there is a nicer way to do this. We need to
118 * insert breakpoints _after_ the child has been started.
119 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200120 struct library_symbol *sym;
121 struct library_symbol *new_sym;
122 sym=proc->list_of_symbols;
123 while(sym){
124 void *addr= sym2addr(proc,sym);
125 if(!addr){
126 sym=sym->next;
127 continue;
128 }
129 if(dict_find_entry(proc->breakpoints,addr)){
130 sym=sym->next;
131 continue;
132 }
133 debug(2,"inserting bp %p %s",addr,sym->name);
Arnaud Patard47950872010-01-08 08:40:15 -0500134 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
135 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200136 new_sym->next=proc->list_of_symbols;
137 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200138 insert_breakpoint(proc, addr, new_sym);
139 sym=sym->next;
140 }
141 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100142#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143 }
144 proc->breakpoints_enabled = 1;
145}
146
Juan Cespedesf1350522008-12-16 18:19:58 +0100147static void
148disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200149 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200150 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200151 disable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100152 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200153}
154
Juan Cespedesf1350522008-12-16 18:19:58 +0100155void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200156disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200157 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100158 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100159 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
160 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161 }
162 proc->breakpoints_enabled = 0;
163}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100164
Juan Cespedesf1350522008-12-16 18:19:58 +0100165static void
166free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200167 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100168 assert(sbp);
169 free(sbp);
170}
171
Juan Cespedesf1350522008-12-16 18:19:58 +0100172void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200173breakpoints_init(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100174 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100175
Juan Cespedescd8976d2009-05-14 13:47:58 +0200176 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100177 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100178 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
179 dict_clear(proc->breakpoints);
180 proc->breakpoints = NULL;
181 }
Petr Machata26627682011-07-08 18:15:32 +0200182
183 proc->breakpoints = dict_init(dict_key2hash_int,
184 dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100185
Petr Machata3d7e4b82011-07-08 18:15:19 +0200186 if (proc->list_of_symbols != NULL) {
187 struct library_symbol * sym = proc->list_of_symbols;
188 while (sym != NULL) {
189 struct library_symbol * next = sym->next;
190 free(sym);
191 sym = next;
192 }
193 }
194 proc->list_of_symbols = NULL;
195
Juan Cespedesce377d52008-12-16 19:38:10 +0100196 if (options.libcalls && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100197 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100198 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200199 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 while (*tmp1) {
201 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100202 int keep = !opt_e_enable;
203
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100204 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200205 if (!strcmp((*tmp1)->name,
206 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100207 keep = opt_e_enable;
208 }
209 tmp2 = tmp2->next;
210 }
211 if (!keep) {
212 *tmp1 = (*tmp1)->next;
213 } else {
214 tmp1 = &((*tmp1)->next);
215 }
216 }
217 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100218 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100219 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200221 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100222 }
223 proc->callstack_depth = 0;
224 proc->breakpoints_enabled = -1;
225}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100226
Juan Cespedesf1350522008-12-16 18:19:58 +0100227void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200228reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200229 struct library_symbol *sym;
230
231 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
232
233 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100234
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100235 while (sym) {
236 if (sym->needs_init) {
Petr Machata26627682011-07-08 18:15:32 +0200237 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100238 if (sym->needs_init && !sym->is_weak) {
239 fprintf(stderr,
240 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
241 sym->name, proc->filename);
242 exit(1);
243 }
244 }
245 sym = sym->next;
246 }
247}