blob: 2b77e02eb8d6e16000b312af24db8230e290646e [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
26 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 +010027 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020028
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029 if (!addr)
30 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010031
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010033 libsym->needs_init = 0;
34
Juan Cespedescac15c32003-01-31 18:58:58 +010035 sbp = dict_find_entry(proc->breakpoints, addr);
36 if (!sbp) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020037 sbp = calloc(1, sizeof(Breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010038 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010040 }
41 dict_enter(proc->breakpoints, addr, sbp);
42 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010043 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010044 }
Juan Cespedes63184be2008-12-10 13:30:12 +010045#ifdef __arm__
46 sbp->thumb_mode = proc->thumb_mode;
47 proc->thumb_mode = 0;
48#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020049 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 if (sbp->enabled == 1 && proc->pid)
51 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020052}
53
Juan Cespedesf1350522008-12-16 18:19:58 +010054void
Juan Cespedesa8909f72009-04-28 20:02:41 +020055delete_breakpoint(Process *proc, void *addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020056 Breakpoint *sbp;
57
58 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
59
60 sbp = dict_find_entry(proc->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020062 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 if (sbp == NULL)
64 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065
66 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010067 if (sbp->enabled == 0)
68 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020069 assert(sbp->enabled >= 0);
70}
71
Juan Cespedesf1350522008-12-16 18:19:58 +010072static void
73enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020074 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +020075 if (((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 Cespedescd8976d2009-05-14 13:47:58 +020082 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010083 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010084#ifdef __powerpc__
85 unsigned long a;
86
87 /*
88 * PPC HACK! (XXX FIXME TODO)
89 * If the dynamic linker hasn't populated the PLT then
90 * dont enable the breakpoints
91 */
Juan Cespedesce377d52008-12-16 19:38:10 +010092 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020094 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020096 if (a == 0x0)
97 return;
98 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +010099#endif
100
Juan Cespedescac15c32003-01-31 18:58:58 +0100101 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100102 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
104 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100105 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100106#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200107 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200108 /*
109 * I'm sure there is a nicer way to do this. We need to
110 * insert breakpoints _after_ the child has been started.
111 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200112 struct library_symbol *sym;
113 struct library_symbol *new_sym;
114 sym=proc->list_of_symbols;
115 while(sym){
116 void *addr= sym2addr(proc,sym);
117 if(!addr){
118 sym=sym->next;
119 continue;
120 }
121 if(dict_find_entry(proc->breakpoints,addr)){
122 sym=sym->next;
123 continue;
124 }
125 debug(2,"inserting bp %p %s",addr,sym->name);
Arnaud Patard47950872010-01-08 08:40:15 -0500126 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
127 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200128 new_sym->next=proc->list_of_symbols;
129 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200130 insert_breakpoint(proc, addr, new_sym);
131 sym=sym->next;
132 }
133 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100134#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100135 }
136 proc->breakpoints_enabled = 1;
137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139static void
140disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200141 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200142 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200143 disable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100144 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200145}
146
Juan Cespedesf1350522008-12-16 18:19:58 +0100147void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200148disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200149 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100150 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100151 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
152 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100153 }
154 proc->breakpoints_enabled = 0;
155}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100156
Juan Cespedesf1350522008-12-16 18:19:58 +0100157static void
158free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200159 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100160 assert(sbp);
161 free(sbp);
162}
163
Juan Cespedesf1350522008-12-16 18:19:58 +0100164void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200165breakpoints_init(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100166 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100167
Juan Cespedescd8976d2009-05-14 13:47:58 +0200168 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100169 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100170 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
171 dict_clear(proc->breakpoints);
172 proc->breakpoints = NULL;
173 }
Petr Machata89a53602007-01-25 18:05:44 +0100174 proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100175
Juan Cespedesce377d52008-12-16 19:38:10 +0100176 if (options.libcalls && proc->filename) {
Juan Cespedese0660df2009-05-21 18:14:39 +0200177 /* FIXME: memory leak when called by exec(): */
Ian Wienand9a2ad352006-02-20 22:44:45 +0100178 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100179 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100180 struct library_symbol **tmp1 = &(proc->list_of_symbols);
181 while (*tmp1) {
182 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100183 int keep = !opt_e_enable;
184
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100185 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100186 if (!strcmp((*tmp1)->name, tmp2->name)) {
187 keep = opt_e_enable;
188 }
189 tmp2 = tmp2->next;
190 }
191 if (!keep) {
192 *tmp1 = (*tmp1)->next;
193 } else {
194 tmp1 = &((*tmp1)->next);
195 }
196 }
197 }
198 } else {
199 proc->list_of_symbols = NULL;
200 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100201 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100202 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200203 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100204 }
205 proc->callstack_depth = 0;
206 proc->breakpoints_enabled = -1;
207}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100208
Juan Cespedesf1350522008-12-16 18:19:58 +0100209void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200210reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200211 struct library_symbol *sym;
212
213 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
214
215 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 while (sym) {
218 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200219 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 sym);
221 if (sym->needs_init && !sym->is_weak) {
222 fprintf(stderr,
223 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
224 sym->name, proc->filename);
225 exit(1);
226 }
227 }
228 sym = sym->next;
229 }
230}