blob: bb257ed9b4f651a93543e0bd73939e771b72e6d4 [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
Petr Machata9294d822012-02-07 12:35:58 +010011#include "breakpoint.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020013
Petr Machataa9fd8f42012-02-07 13:25:56 +010014void
15breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
16{
17 assert(bp != NULL);
18 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
19 (bp->cbs->on_hit) (bp, proc);
20}
21
22void
23breakpoint_on_destroy(struct breakpoint *bp)
24{
25 assert(bp != NULL);
26 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
27 (bp->cbs->on_destroy) (bp);
28}
29
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020030/*****************************************************************************/
31
Petr Machata9294d822012-02-07 12:35:58 +010032struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010033address2bpstruct(Process *proc, void *addr)
34{
Petr Machata26627682011-07-08 18:15:32 +020035 assert(proc != NULL);
36 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020037 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020038 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010039 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020040}
41
Petr Machata9294d822012-02-07 12:35:58 +010042struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020043insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010044 struct library_symbol *libsym, int enable)
45{
Petr Machata9294d822012-02-07 12:35:58 +010046 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020047
Petr Machata9a5420c2011-07-09 11:21:23 +020048 Process * leader = proc->leader;
49
50 /* Only the group leader should be getting the breakpoints and
51 * thus have ->breakpoint initialized. */
52 assert(leader != NULL);
53 assert(leader->breakpoints != NULL);
54
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070055#ifdef __arm__
56 int thumb_mode = (int)addr & 1;
57 if (thumb_mode)
58 addr = (void *)((int)addr & ~1);
59#endif
60
Juan Cespedescd8976d2009-05-14 13:47:58 +020061 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 +010062 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020063
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 if (!addr)
Petr Machata9294d822012-02-07 12:35:58 +010065 return NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010066
Ian Wienand2d45b1a2006-02-20 22:48:07 +010067 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010068 libsym->needs_init = 0;
69
Petr Machata9a5420c2011-07-09 11:21:23 +020070 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010071 if (sbp == NULL) {
72 sbp = calloc(1, sizeof(*sbp));
73 if (sbp == NULL) {
Petr Machata9294d822012-02-07 12:35:58 +010074 return NULL; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010075 }
Petr Machata9a5420c2011-07-09 11:21:23 +020076 dict_enter(leader->breakpoints, addr, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010077 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010079 }
Juan Cespedes63184be2008-12-10 13:30:12 +010080#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070081 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010082 proc->thumb_mode = 0;
83#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020084 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +020085 if (sbp->enabled == 1 && enable) {
86 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +020087 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +020088 }
Petr Machata9294d822012-02-07 12:35:58 +010089
90 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020091}
92
Juan Cespedesf1350522008-12-16 18:19:58 +010093void
Petr Machatafed1e8d2012-02-07 02:06:29 +010094delete_breakpoint(Process *proc, void *addr)
95{
Petr Machata9294d822012-02-07 12:35:58 +010096 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020097
98 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
99
Petr Machata9a5420c2011-07-09 11:21:23 +0200100 Process * leader = proc->leader;
101 assert(leader != NULL);
102
103 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200105 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 if (sbp == NULL)
107 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200108
109 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200111 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200112 assert(sbp->enabled >= 0);
113}
114
Juan Cespedesf1350522008-12-16 18:19:58 +0100115static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100116enable_bp_cb(void *addr, void *sbp, void *proc)
117{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200118 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200119 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200120 enable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100121 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200122}
123
Juan Cespedesf1350522008-12-16 18:19:58 +0100124void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200125enable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200126 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100127 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100128#ifdef __powerpc__
129 unsigned long a;
130
131 /*
132 * PPC HACK! (XXX FIXME TODO)
133 * If the dynamic linker hasn't populated the PLT then
134 * dont enable the breakpoints
135 */
Juan Cespedesce377d52008-12-16 19:38:10 +0100136 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200138 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +0200140 if (a == 0x0)
141 return;
142 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100143#endif
144
Juan Cespedescac15c32003-01-31 18:58:58 +0100145 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100146 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
148 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100149 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100150#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200151 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200152 /*
153 * I'm sure there is a nicer way to do this. We need to
154 * insert breakpoints _after_ the child has been started.
155 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200156 struct library_symbol *sym;
157 struct library_symbol *new_sym;
158 sym=proc->list_of_symbols;
159 while(sym){
160 void *addr= sym2addr(proc,sym);
161 if(!addr){
162 sym=sym->next;
163 continue;
164 }
165 if(dict_find_entry(proc->breakpoints,addr)){
166 sym=sym->next;
167 continue;
168 }
169 debug(2,"inserting bp %p %s",addr,sym->name);
Arnaud Patard47950872010-01-08 08:40:15 -0500170 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
171 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200172 new_sym->next=proc->list_of_symbols;
173 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200174 insert_breakpoint(proc, addr, new_sym);
175 sym=sym->next;
176 }
177 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100178#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100179 }
180 proc->breakpoints_enabled = 1;
181}
182
Juan Cespedesf1350522008-12-16 18:19:58 +0100183static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100184disable_bp_cb(void *addr, void *sbp, void *proc)
185{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200186 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200187 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200188 disable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100189 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200190}
191
Juan Cespedesf1350522008-12-16 18:19:58 +0100192void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200193disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200194 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200195 assert(proc->leader == proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100196 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100197 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
198 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
200 proc->breakpoints_enabled = 0;
201}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100202
Juan Cespedesf1350522008-12-16 18:19:58 +0100203static void
204free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200205 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100206 assert(sbp);
207 free(sbp);
208}
209
Petr Machata02648a12012-02-07 13:44:54 +0100210static void
211entry_callback_hit(struct breakpoint *bp, struct Process *proc)
212{
213 if (proc == NULL || proc->leader == NULL)
214 return;
215 delete_breakpoint(proc, bp->addr); // xxx
216 reinitialize_breakpoints(proc->leader);
217}
218
Petr Machata1974dbc2011-08-19 18:58:01 +0200219int
Petr Machatac7585b62011-07-08 22:58:12 +0200220breakpoints_init(Process *proc, int enable)
221{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200222 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100224 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
225 dict_clear(proc->breakpoints);
226 proc->breakpoints = NULL;
227 }
Petr Machata26627682011-07-08 18:15:32 +0200228
Petr Machata9a5420c2011-07-09 11:21:23 +0200229 /* Only the thread group leader should hold the breakpoints.
230 * (N.B. PID may be set to 0 temporarily when called by
231 * handle_exec). */
232 assert(proc->leader == proc);
233
Petr Machata26627682011-07-08 18:15:32 +0200234 proc->breakpoints = dict_init(dict_key2hash_int,
235 dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100236
Petr Machata534e00f2011-09-27 17:58:38 +0200237 destroy_library_symbol_chain(proc->list_of_symbols);
Petr Machata3d7e4b82011-07-08 18:15:19 +0200238 proc->list_of_symbols = NULL;
239
Petr Machatae84fa002012-02-07 13:43:03 +0100240 GElf_Addr entry;
Juan Cespedesce377d52008-12-16 19:38:10 +0100241 if (options.libcalls && proc->filename) {
Petr Machatae84fa002012-02-07 13:43:03 +0100242 proc->list_of_symbols = read_elf(proc, &entry);
Petr Machata1974dbc2011-08-19 18:58:01 +0200243 if (proc->list_of_symbols == NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100244 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200245 /* XXX leak breakpoints */
246 return -1;
247 }
248
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100249 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200250 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 while (*tmp1) {
252 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100253 int keep = !opt_e_enable;
254
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200256 if (!strcmp((*tmp1)->name,
257 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100258 keep = opt_e_enable;
259 }
260 tmp2 = tmp2->next;
261 }
262 if (!keep) {
263 *tmp1 = (*tmp1)->next;
264 } else {
265 tmp1 = &((*tmp1)->next);
266 }
267 }
268 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100269 }
Petr Machatac7585b62011-07-08 22:58:12 +0200270
Petr Machata02648a12012-02-07 13:44:54 +0100271 struct breakpoint *entry_bp
272 = insert_breakpoint(proc, (void *)entry, NULL, 1);
273 if (entry_bp == NULL) {
274 fprintf(stderr, "fail!\n");
275 goto fail;
276 }
277
278 static struct bp_callbacks entry_callbacks = {
279 .on_hit = entry_callback_hit,
280 };
281 entry_bp->cbs = &entry_callbacks;
Petr Machatac7585b62011-07-08 22:58:12 +0200282
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100283 proc->callstack_depth = 0;
284 proc->breakpoints_enabled = -1;
Petr Machata1974dbc2011-08-19 18:58:01 +0200285 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100286}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100287
Juan Cespedesf1350522008-12-16 18:19:58 +0100288void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200289reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200290 struct library_symbol *sym;
291
292 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
293
294 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100295
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100296 while (sym) {
297 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200298 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 if (sym->needs_init && !sym->is_weak) {
300 fprintf(stderr,
301 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
302 sym->name, proc->filename);
303 exit(1);
304 }
305 }
306 sym = sym->next;
307 }
308}