blob: 1ea406ab5793fc4aa6e4f1ef49875fd73f139f61 [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) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020017 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010018 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020019}
20
Ian Wienand9a2ad352006-02-20 22:44:45 +010021void
Juan Cespedesa8909f72009-04-28 20:02:41 +020022insert_breakpoint(Process *proc, void *addr,
Juan Cespedesf1350522008-12-16 18:19:58 +010023 struct library_symbol *libsym) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020024 Breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020025
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070026#ifdef __arm__
27 int thumb_mode = (int)addr & 1;
28 if (thumb_mode)
29 addr = (void *)((int)addr & ~1);
30#endif
31
Juan Cespedescd8976d2009-05-14 13:47:58 +020032 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 +010033 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020034
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 if (!addr)
36 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010037
Ian Wienand2d45b1a2006-02-20 22:48:07 +010038 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010039 libsym->needs_init = 0;
40
Juan Cespedescac15c32003-01-31 18:58:58 +010041 sbp = dict_find_entry(proc->breakpoints, addr);
42 if (!sbp) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020043 sbp = calloc(1, sizeof(Breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010044 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010045 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010046 }
47 dict_enter(proc->breakpoints, addr, sbp);
48 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010050 }
Juan Cespedes63184be2008-12-10 13:30:12 +010051#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070052 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010053 proc->thumb_mode = 0;
54#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056 if (sbp->enabled == 1 && proc->pid)
57 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020058}
59
Juan Cespedesf1350522008-12-16 18:19:58 +010060void
Juan Cespedesa8909f72009-04-28 20:02:41 +020061delete_breakpoint(Process *proc, void *addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020062 Breakpoint *sbp;
63
64 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
65
66 sbp = dict_find_entry(proc->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010067 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020068 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 if (sbp == NULL)
70 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020071
72 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 if (sbp->enabled == 0)
74 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020075 assert(sbp->enabled >= 0);
76}
77
Juan Cespedesf1350522008-12-16 18:19:58 +010078static void
79enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020080 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +020081 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020082 enable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010083 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020084}
85
Juan Cespedesf1350522008-12-16 18:19:58 +010086void
Juan Cespedesa8909f72009-04-28 20:02:41 +020087enable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020088 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010089 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010090#ifdef __powerpc__
91 unsigned long a;
92
93 /*
94 * PPC HACK! (XXX FIXME TODO)
95 * If the dynamic linker hasn't populated the PLT then
96 * dont enable the breakpoints
97 */
Juan Cespedesce377d52008-12-16 19:38:10 +010098 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200100 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +0200102 if (a == 0x0)
103 return;
104 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100105#endif
106
Juan Cespedescac15c32003-01-31 18:58:58 +0100107 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100108 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
110 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100111 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100112#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200113 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200114 /*
115 * I'm sure there is a nicer way to do this. We need to
116 * insert breakpoints _after_ the child has been started.
117 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200118 struct library_symbol *sym;
119 struct library_symbol *new_sym;
120 sym=proc->list_of_symbols;
121 while(sym){
122 void *addr= sym2addr(proc,sym);
123 if(!addr){
124 sym=sym->next;
125 continue;
126 }
127 if(dict_find_entry(proc->breakpoints,addr)){
128 sym=sym->next;
129 continue;
130 }
131 debug(2,"inserting bp %p %s",addr,sym->name);
Arnaud Patard47950872010-01-08 08:40:15 -0500132 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
133 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200134 new_sym->next=proc->list_of_symbols;
135 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200136 insert_breakpoint(proc, addr, new_sym);
137 sym=sym->next;
138 }
139 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100140#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 }
142 proc->breakpoints_enabled = 1;
143}
144
Juan Cespedesf1350522008-12-16 18:19:58 +0100145static void
146disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200147 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200148 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200149 disable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100150 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200151}
152
Juan Cespedesf1350522008-12-16 18:19:58 +0100153void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200154disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200155 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100156 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100157 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
158 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 }
160 proc->breakpoints_enabled = 0;
161}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100162
Juan Cespedesf1350522008-12-16 18:19:58 +0100163static void
164free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200165 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100166 assert(sbp);
167 free(sbp);
168}
169
Juan Cespedesf1350522008-12-16 18:19:58 +0100170void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200171breakpoints_init(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100173
Juan Cespedescd8976d2009-05-14 13:47:58 +0200174 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100176 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
177 dict_clear(proc->breakpoints);
178 proc->breakpoints = NULL;
179 }
Petr Machata89a53602007-01-25 18:05:44 +0100180 proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100181
Juan Cespedesce377d52008-12-16 19:38:10 +0100182 if (options.libcalls && proc->filename) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200183 /* FIXME: memory leak when called by exec(): */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100184 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100185 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 struct library_symbol **tmp1 = &(proc->list_of_symbols);
187 while (*tmp1) {
188 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100189 int keep = !opt_e_enable;
190
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100191 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100192 if (!strcmp((*tmp1)->name, tmp2->name)) {
193 keep = opt_e_enable;
194 }
195 tmp2 = tmp2->next;
196 }
197 if (!keep) {
198 *tmp1 = (*tmp1)->next;
199 } else {
200 tmp1 = &((*tmp1)->next);
201 }
202 }
203 }
204 } else {
205 proc->list_of_symbols = NULL;
206 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100207 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200209 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100210 }
211 proc->callstack_depth = 0;
212 proc->breakpoints_enabled = -1;
213}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100214
Juan Cespedesf1350522008-12-16 18:19:58 +0100215void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200216reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200217 struct library_symbol *sym;
218
219 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
220
221 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100222
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 while (sym) {
224 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200225 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 sym);
227 if (sym->needs_init && !sym->is_weak) {
228 fprintf(stderr,
229 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
230 sym->name, proc->filename);
231 exit(1);
232 }
233 }
234 sym = sym->next;
235 }
236}