blob: 9c5e89554f70d06ac72213d1b85b516931a8ada3 [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 Machata1974dbc2011-08-19 18:58:01 +0200210int
Petr Machatac7585b62011-07-08 22:58:12 +0200211breakpoints_init(Process *proc, int enable)
212{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100214
Juan Cespedescd8976d2009-05-14 13:47:58 +0200215 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100217 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
218 dict_clear(proc->breakpoints);
219 proc->breakpoints = NULL;
220 }
Petr Machata26627682011-07-08 18:15:32 +0200221
Petr Machata9a5420c2011-07-09 11:21:23 +0200222 /* Only the thread group leader should hold the breakpoints.
223 * (N.B. PID may be set to 0 temporarily when called by
224 * handle_exec). */
225 assert(proc->leader == proc);
226
Petr Machata26627682011-07-08 18:15:32 +0200227 proc->breakpoints = dict_init(dict_key2hash_int,
228 dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100229
Petr Machata534e00f2011-09-27 17:58:38 +0200230 destroy_library_symbol_chain(proc->list_of_symbols);
Petr Machata3d7e4b82011-07-08 18:15:19 +0200231 proc->list_of_symbols = NULL;
232
Petr Machatae84fa002012-02-07 13:43:03 +0100233 GElf_Addr entry;
Juan Cespedesce377d52008-12-16 19:38:10 +0100234 if (options.libcalls && proc->filename) {
Petr Machatae84fa002012-02-07 13:43:03 +0100235 proc->list_of_symbols = read_elf(proc, &entry);
Petr Machata1974dbc2011-08-19 18:58:01 +0200236 if (proc->list_of_symbols == NULL) {
237 /* XXX leak breakpoints */
238 return -1;
239 }
240
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100241 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200242 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 while (*tmp1) {
244 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100245 int keep = !opt_e_enable;
246
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200248 if (!strcmp((*tmp1)->name,
249 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100250 keep = opt_e_enable;
251 }
252 tmp2 = tmp2->next;
253 }
254 if (!keep) {
255 *tmp1 = (*tmp1)->next;
256 } else {
257 tmp1 = &((*tmp1)->next);
258 }
259 }
260 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100261 }
Petr Machatac7585b62011-07-08 22:58:12 +0200262
263 for (sym = proc->list_of_symbols; sym; sym = sym->next)
264 insert_breakpoint(proc, sym2addr(proc, sym), sym, enable);
265
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100266 proc->callstack_depth = 0;
267 proc->breakpoints_enabled = -1;
Petr Machata1974dbc2011-08-19 18:58:01 +0200268 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100269}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100270
Juan Cespedesf1350522008-12-16 18:19:58 +0100271void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200272reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200273 struct library_symbol *sym;
274
275 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
276
277 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100278
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 while (sym) {
280 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200281 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 if (sym->needs_init && !sym->is_weak) {
283 fprintf(stderr,
284 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
285 sym->name, proc->filename);
286 exit(1);
287 }
288 }
289 sym = sym->next;
290 }
291}