blob: fada8e405d349e26b340e98ceb1cc9facdcfaed8 [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>
Petr Machata2b46cfc2012-02-18 11:17:29 +01006#include <error.h>
7#include <errno.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02008
Juan Cespedesf1bfe202002-03-27 00:22:23 +01009#ifdef __powerpc__
10#include <sys/ptrace.h>
11#endif
12
Petr Machata9294d822012-02-07 12:35:58 +010013#include "breakpoint.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020014#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010015#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010016#include "library.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020017
Petr Machatac67a6e62012-03-28 02:39:49 +020018#ifndef ARCH_HAVE_TRANSLATE_ADDRESS
19int
20arch_translate_address(struct Process *proc,
21 target_address_t addr, target_address_t *ret)
22{
23 *ret = addr;
24 return 0;
25}
26#endif
27
Petr Machataa9fd8f42012-02-07 13:25:56 +010028void
29breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
30{
31 assert(bp != NULL);
32 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
Petr Machata55ac9322012-03-27 03:07:35 +020033 (bp->cbs->on_hit)(bp, proc);
34}
35
36void
37breakpoint_on_continue(struct breakpoint *bp, struct Process *proc)
38{
39 assert(bp != NULL);
40 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
41 (bp->cbs->on_continue)(bp, proc);
42 else
43 continue_after_breakpoint(proc, bp);
Petr Machataa9fd8f42012-02-07 13:25:56 +010044}
45
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020046/*****************************************************************************/
47
Petr Machata9294d822012-02-07 12:35:58 +010048struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010049address2bpstruct(Process *proc, void *addr)
50{
Petr Machata26627682011-07-08 18:15:32 +020051 assert(proc != NULL);
52 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020053 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020054 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010055 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020056}
57
Petr Machata8cce1192012-03-25 01:37:19 +010058#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +010059int
60arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
61{
62 return 0;
63}
Petr Machata8cce1192012-03-25 01:37:19 +010064
65void
66arch_breakpoint_destroy(struct breakpoint *sbp)
67{
68}
Petr Machata2b46cfc2012-02-18 11:17:29 +010069#endif
70
Petr Machata52dbfb12012-03-29 16:38:26 +020071/* On second thought, I don't think we need PROC. All the translation
72 * (arch_translate_address in particular) should be doable using
73 * static lookups of various sections in the ELF file. We shouldn't
74 * need process for anything. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010075int
76breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020077 target_address_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +010078{
Petr Machata55ac9322012-03-27 03:07:35 +020079 bp->cbs = NULL;
Petr Machata52dbfb12012-03-29 16:38:26 +020080 bp->proc = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +010081 bp->addr = addr;
82 memset(bp->orig_value, 0, sizeof(bp->orig_value));
83 bp->enabled = 0;
84 bp->libsym = libsym;
85 return arch_breakpoint_init(proc, bp);
86}
87
Petr Machata8cce1192012-03-25 01:37:19 +010088void
Petr Machata55ac9322012-03-27 03:07:35 +020089breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
90{
91 if (bp->cbs != NULL)
92 assert(bp->cbs == NULL);
93 bp->cbs = cbs;
94}
95
96void
Petr Machata8cce1192012-03-25 01:37:19 +010097breakpoint_destroy(struct breakpoint *bp)
98{
99 if (bp == NULL)
100 return;
101
102 /* XXX I'm not convinced that we need on_destroy. We already
103 * have arch_breakpoint_destroy, which is necessary as a
104 * counterpart of arch_breakpoint_init in any case. */
105 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
106 (bp->cbs->on_destroy) (bp);
107
108 arch_breakpoint_destroy(bp);
109}
110
Petr Machata52dbfb12012-03-29 16:38:26 +0200111int
112breakpoint_turn_on(struct breakpoint *bp)
113{
114 /* Make sure it was inserted. XXX In a clean world, we would
115 * have breakpoint_site representing a place and breakpoint
116 * representing inserted breakpoint. */
117 assert(bp->proc != NULL);
118 bp->enabled++;
119 if (bp->enabled == 1) {
120 assert(bp->proc->pid != 0);
121 enable_breakpoint(bp->proc, bp);
122 }
123 return 0;
124}
125
126int
127breakpoint_turn_off(struct breakpoint *bp)
128{
129 assert(bp->proc != NULL);
130 bp->enabled--;
131 if (bp->enabled == 0)
132 disable_breakpoint(bp->proc, bp);
133 assert(bp->enabled >= 0);
134 return 0;
135}
136
Petr Machata9294d822012-02-07 12:35:58 +0100137struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100138insert_breakpoint(struct Process *proc, void *addr,
139 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100140{
Petr Machata9df15012012-02-20 12:49:46 +0100141 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200142
143 /* Only the group leader should be getting the breakpoints and
144 * thus have ->breakpoint initialized. */
145 assert(leader != NULL);
146 assert(leader->breakpoints != NULL);
147
Juan Cespedescd8976d2009-05-14 13:47:58 +0200148 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 +0100149 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200150
Petr Machata81c65272012-03-21 04:57:25 +0100151 if (addr == 0) {
152 /* XXX we need a better way to deal with this. For
153 * now, just abuse errno to carry the error
154 * information. */
155 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100156 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100157 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100158
Petr Machata52dbfb12012-03-29 16:38:26 +0200159 /* XXX what we need to do instead is have a list of
160 * breakpoints that are enabled at this address. The
161 * following works if every breakpoint is the same and there's
162 * no extra data, but that doesn't hold anymore. For now it
163 * will suffice, about the only realistic case where we need
164 * to have more than one breakpoint per address is return from
165 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100166 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100167 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168 sbp = malloc(sizeof(*sbp));
169 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200170 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
171 free(sbp);
172 return NULL;
173 }
174 if (proc_add_breakpoint(proc, sbp) < 0) {
175 fail:
176 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177 free(sbp);
178 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100179 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100180 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100181
Petr Machata52dbfb12012-03-29 16:38:26 +0200182 if (breakpoint_turn_on(sbp) < 0)
183 goto fail;
Petr Machata9294d822012-02-07 12:35:58 +0100184
185 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200186}
187
Juan Cespedesf1350522008-12-16 18:19:58 +0100188void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100189delete_breakpoint(Process *proc, void *addr)
190{
Petr Machata9294d822012-02-07 12:35:58 +0100191 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200192
193 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
194
Petr Machata9a5420c2011-07-09 11:21:23 +0200195 Process * leader = proc->leader;
196 assert(leader != NULL);
197
198 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200200 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 if (sbp == NULL)
202 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200203
Petr Machata52dbfb12012-03-29 16:38:26 +0200204 if (breakpoint_turn_off(sbp) < 0) {
205 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
206 breakpoint_name(sbp), sbp->addr);
207 return;
208 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200209}
210
Petr Machatae9aebd62012-03-25 01:38:53 +0100211const char *
212breakpoint_name(const struct breakpoint *bp)
213{
214 assert(bp != NULL);
215 return bp->libsym != NULL ? bp->libsym->name : NULL;
216}
217
Petr Machata52dbfb12012-03-29 16:38:26 +0200218struct library *
219breakpoint_library(const struct breakpoint *bp)
220{
221 assert(bp != NULL);
222 return bp->libsym != NULL ? bp->libsym->lib : NULL;
223}
224
Juan Cespedesf1350522008-12-16 18:19:58 +0100225static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100226enable_bp_cb(void *addr, void *sbp, void *proc)
227{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200228 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100229 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200230 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200231}
232
Juan Cespedesf1350522008-12-16 18:19:58 +0100233void
Petr Machatabc373262012-02-07 23:31:15 +0100234enable_all_breakpoints(Process *proc)
235{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200236 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100237
238 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
239 if (proc->breakpoints) {
240 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
241 proc);
242 }
243#ifdef __mips__
244 {
245 /*
246 * I'm sure there is a nicer way to do this. We need to
247 * insert breakpoints _after_ the child has been started.
248 */
249 struct library_symbol *sym;
250 struct library_symbol *new_sym;
251 sym=proc->list_of_symbols;
252 while(sym){
253 void *addr= sym2addr(proc,sym);
254 if(!addr){
255 sym=sym->next;
256 continue;
257 }
258 if(dict_find_entry(proc->breakpoints,addr)){
259 sym=sym->next;
260 continue;
261 }
262 debug(2,"inserting bp %p %s",addr,sym->name);
263 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
264 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
265 new_sym->next=proc->list_of_symbols;
266 proc->list_of_symbols=new_sym;
267 insert_breakpoint(proc, addr, new_sym);
268 sym=sym->next;
269 }
270 }
271#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100272}
273
Juan Cespedesf1350522008-12-16 18:19:58 +0100274static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100275disable_bp_cb(void *addr, void *sbp, void *proc)
276{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200277 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100278 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200279 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200280}
281
Juan Cespedesf1350522008-12-16 18:19:58 +0100282void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200283disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200284 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200285 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100286 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100287}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100288
Petr Machata52dbfb12012-03-29 16:38:26 +0200289struct entry_breakpoint {
290 struct breakpoint super;
291 target_address_t dyn_addr;
292};
293
Petr Machata02648a12012-02-07 13:44:54 +0100294static void
Petr Machata52dbfb12012-03-29 16:38:26 +0200295entry_callback_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100296{
Petr Machata52dbfb12012-03-29 16:38:26 +0200297 struct entry_breakpoint *bp = (void *)a;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100299 if (proc == NULL || proc->leader == NULL)
300 return;
Petr Machata52dbfb12012-03-29 16:38:26 +0200301 delete_breakpoint(proc, bp->super.addr); // xxx
Petr Machatacb9a28d2012-03-28 11:11:32 +0200302 //enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100303
Petr Machata52dbfb12012-03-29 16:38:26 +0200304 linkmap_init(proc, bp->dyn_addr);
305}
306
307int
308entry_breakpoint_init(struct Process *proc,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200309 struct entry_breakpoint *bp, target_address_t addr,
310 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200311{
312 int err;
313 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
314 return err;
315
316 static struct bp_callbacks entry_callbacks = {
317 .on_hit = entry_callback_hit,
318 };
319 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200320 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200321 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100322}
323
Petr Machata1974dbc2011-08-19 18:58:01 +0200324int
Petr Machatac7585b62011-07-08 22:58:12 +0200325breakpoints_init(Process *proc, int enable)
326{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100327 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200328 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200329
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 /* XXX breakpoint dictionary should be initialized
331 * outside. Here we just put in breakpoints. */
332 assert(proc->breakpoints != NULL);
333
334 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200335 assert(proc->leader == proc);
336
Juan Cespedesce377d52008-12-16 19:38:10 +0100337 if (options.libcalls && proc->filename) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200338 struct library *lib = ltelf_read_main_binary(proc,
339 proc->filename);
340 struct entry_breakpoint *entry_bp = NULL;
341 int bp_state = 0;
342 int result = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100344 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345 proc_remove_library(proc, lib);
346 library_destroy(lib);
Petr Machata52dbfb12012-03-29 16:38:26 +0200347 switch (bp_state) {
348 case 2:
349 proc_remove_breakpoint(proc, &entry_bp->super);
350 case 1:
351 breakpoint_destroy(&entry_bp->super);
352 }
353 free(entry_bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354 case 0:
Petr Machata52dbfb12012-03-29 16:38:26 +0200355 return result;
Petr Machata1974dbc2011-08-19 18:58:01 +0200356 }
Petr Machata52dbfb12012-03-29 16:38:26 +0200357
Petr Machata2b46cfc2012-02-18 11:17:29 +0100358 proc_add_library(proc, lib);
359 fprintf(stderr, "note: symbols in %s were not filtered.\n",
360 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200361
Petr Machata52dbfb12012-03-29 16:38:26 +0200362 entry_bp = malloc(sizeof(*entry_bp));
363 if (entry_bp == NULL
364 || (result = entry_breakpoint_init(proc, entry_bp,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200365 lib->entry, lib)) < 0)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 goto fail;
Petr Machatac7585b62011-07-08 22:58:12 +0200367
Petr Machata52dbfb12012-03-29 16:38:26 +0200368 ++bp_state;
369 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
370 goto fail;
371
372 ++bp_state;
373 if ((result = breakpoint_turn_on(&entry_bp->super)) < 0)
374 goto fail;
Petr Machata02648a12012-02-07 13:44:54 +0100375 }
376
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100377 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200378 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100379}