blob: 78767568339cbe6f660c8834aa90c1efc2cfc51f [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 Cespedesf1bfe202002-03-27 00:22:23 +0100127#ifdef __powerpc__
Petr Machata61196a42012-02-07 16:41:03 +0100128 unsigned long a;
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100129
Petr Machata61196a42012-02-07 16:41:03 +0100130 /*
131 * PPC HACK! (XXX FIXME TODO)
132 * If the dynamic linker hasn't populated the PLT then
133 * dont enable the breakpoints
134 */
135 if (options.libcalls) {
136 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
137 sym2addr(proc, proc->list_of_symbols),
138 0);
139 if (a == 0x0)
140 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 }
Petr Machata61196a42012-02-07 16:41:03 +0100142#endif
143
144 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
145 if (proc->breakpoints) {
146 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
147 proc);
148 }
149#ifdef __mips__
150 {
151 /*
152 * I'm sure there is a nicer way to do this. We need to
153 * insert breakpoints _after_ the child has been started.
154 */
155 struct library_symbol *sym;
156 struct library_symbol *new_sym;
157 sym=proc->list_of_symbols;
158 while(sym){
159 void *addr= sym2addr(proc,sym);
160 if(!addr){
161 sym=sym->next;
162 continue;
163 }
164 if(dict_find_entry(proc->breakpoints,addr)){
165 sym=sym->next;
166 continue;
167 }
168 debug(2,"inserting bp %p %s",addr,sym->name);
169 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
170 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
171 new_sym->next=proc->list_of_symbols;
172 proc->list_of_symbols=new_sym;
173 insert_breakpoint(proc, addr, new_sym);
174 sym=sym->next;
175 }
176 }
177#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100178}
179
Juan Cespedesf1350522008-12-16 18:19:58 +0100180static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100181disable_bp_cb(void *addr, void *sbp, void *proc)
182{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200183 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200184 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200185 disable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100186 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200187}
188
Juan Cespedesf1350522008-12-16 18:19:58 +0100189void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200190disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200191 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200192 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100193 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100194}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100195
Juan Cespedesf1350522008-12-16 18:19:58 +0100196static void
197free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200198 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100199 assert(sbp);
200 free(sbp);
201}
202
Petr Machata02648a12012-02-07 13:44:54 +0100203static void
204entry_callback_hit(struct breakpoint *bp, struct Process *proc)
205{
206 if (proc == NULL || proc->leader == NULL)
207 return;
208 delete_breakpoint(proc, bp->addr); // xxx
209 reinitialize_breakpoints(proc->leader);
210}
211
Petr Machata1974dbc2011-08-19 18:58:01 +0200212int
Petr Machatac7585b62011-07-08 22:58:12 +0200213breakpoints_init(Process *proc, int enable)
214{
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) {
Petr Machata02648a12012-02-07 13:44:54 +0100237 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200238 /* XXX leak breakpoints */
239 return -1;
240 }
241
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100242 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200243 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100244 while (*tmp1) {
245 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100246 int keep = !opt_e_enable;
247
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200249 if (!strcmp((*tmp1)->name,
250 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100251 keep = opt_e_enable;
252 }
253 tmp2 = tmp2->next;
254 }
255 if (!keep) {
256 *tmp1 = (*tmp1)->next;
257 } else {
258 tmp1 = &((*tmp1)->next);
259 }
260 }
261 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100262 }
Petr Machatac7585b62011-07-08 22:58:12 +0200263
Petr Machata02648a12012-02-07 13:44:54 +0100264 struct breakpoint *entry_bp
Petr Machata61196a42012-02-07 16:41:03 +0100265 = insert_breakpoint(proc, (void *)(uintptr_t)entry, NULL, 1);
Petr Machata02648a12012-02-07 13:44:54 +0100266 if (entry_bp == NULL) {
267 fprintf(stderr, "fail!\n");
268 goto fail;
269 }
270
271 static struct bp_callbacks entry_callbacks = {
272 .on_hit = entry_callback_hit,
273 };
274 entry_bp->cbs = &entry_callbacks;
Petr Machatac7585b62011-07-08 22:58:12 +0200275
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100276 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200277 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100278}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100279
Juan Cespedesf1350522008-12-16 18:19:58 +0100280void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200281reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200282 struct library_symbol *sym;
283
284 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
285
286 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100287
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100288 while (sym) {
289 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200290 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 if (sym->needs_init && !sym->is_weak) {
292 fprintf(stderr,
293 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
294 sym->name, proc->filename);
295 exit(1);
296 }
297 }
298 sym = sym->next;
299 }
300}