blob: 2b1f3044e7d45cbc078daa7d4c6db61beca42833 [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 <errno.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02007
Juan Cespedesf1bfe202002-03-27 00:22:23 +01008#ifdef __powerpc__
9#include <sys/ptrace.h>
10#endif
11
Petr Machata9294d822012-02-07 12:35:58 +010012#include "breakpoint.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020013#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010014#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010015#include "library.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020016
Petr Machatac67a6e62012-03-28 02:39:49 +020017#ifndef ARCH_HAVE_TRANSLATE_ADDRESS
18int
19arch_translate_address(struct Process *proc,
20 target_address_t addr, target_address_t *ret)
21{
22 *ret = addr;
23 return 0;
24}
25#endif
26
Petr Machataa9fd8f42012-02-07 13:25:56 +010027void
28breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
29{
30 assert(bp != NULL);
31 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
Petr Machata55ac9322012-03-27 03:07:35 +020032 (bp->cbs->on_hit)(bp, proc);
33}
34
35void
36breakpoint_on_continue(struct breakpoint *bp, struct Process *proc)
37{
38 assert(bp != NULL);
39 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
40 (bp->cbs->on_continue)(bp, proc);
41 else
42 continue_after_breakpoint(proc, bp);
Petr Machataa9fd8f42012-02-07 13:25:56 +010043}
44
Petr Machata86d38282012-04-24 18:09:01 +020045void
46breakpoint_on_retract(struct breakpoint *bp, struct Process *proc)
47{
48 assert(bp != NULL);
49 if (bp->cbs != NULL && bp->cbs->on_retract != NULL)
50 (bp->cbs->on_retract)(bp, proc);
51}
52
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020053/*****************************************************************************/
54
Petr Machata9294d822012-02-07 12:35:58 +010055struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010056address2bpstruct(Process *proc, void *addr)
57{
Petr Machata26627682011-07-08 18:15:32 +020058 assert(proc != NULL);
59 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020060 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020061 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010062 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020063}
64
Petr Machata8cce1192012-03-25 01:37:19 +010065#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +010066int
67arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
68{
69 return 0;
70}
Petr Machata8cce1192012-03-25 01:37:19 +010071
72void
73arch_breakpoint_destroy(struct breakpoint *sbp)
74{
75}
Petr Machatad3cc9882012-04-13 21:40:23 +020076
77int
78arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
79{
80 return 0;
81}
Petr Machata2b46cfc2012-02-18 11:17:29 +010082#endif
83
Petr Machatad3cc9882012-04-13 21:40:23 +020084static void
85breakpoint_init_base(struct breakpoint *bp, struct Process *proc,
86 target_address_t addr, struct library_symbol *libsym)
87{
88 bp->cbs = NULL;
89 bp->addr = addr;
90 memset(bp->orig_value, 0, sizeof(bp->orig_value));
91 bp->enabled = 0;
92 bp->libsym = libsym;
93}
94
Petr Machata52dbfb12012-03-29 16:38:26 +020095/* On second thought, I don't think we need PROC. All the translation
96 * (arch_translate_address in particular) should be doable using
97 * static lookups of various sections in the ELF file. We shouldn't
98 * need process for anything. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010099int
100breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +0200101 target_address_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102{
Petr Machatad3cc9882012-04-13 21:40:23 +0200103 breakpoint_init_base(bp, proc, addr, libsym);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100104 return arch_breakpoint_init(proc, bp);
105}
106
Petr Machata8cce1192012-03-25 01:37:19 +0100107void
Petr Machata55ac9322012-03-27 03:07:35 +0200108breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
109{
110 if (bp->cbs != NULL)
111 assert(bp->cbs == NULL);
112 bp->cbs = cbs;
113}
114
115void
Petr Machata8cce1192012-03-25 01:37:19 +0100116breakpoint_destroy(struct breakpoint *bp)
117{
118 if (bp == NULL)
119 return;
Petr Machata8cce1192012-03-25 01:37:19 +0100120 arch_breakpoint_destroy(bp);
121}
122
Petr Machatad3cc9882012-04-13 21:40:23 +0200123struct find_symbol_data {
124 struct library_symbol *old_libsym;
125 struct library_symbol *found_libsym;
126};
127
128static enum callback_status
129find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
130{
131 struct find_symbol_data *fs = u;
132 fs->found_libsym
133 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
134 fs->old_libsym);
135 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
136}
137
138int
139breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
140 struct breakpoint *bp, struct Process *old_proc)
141{
142 /* Find library and symbol that this breakpoint was linked to. */
143 struct library_symbol *libsym = bp->libsym;
144 struct library *lib = NULL;
145 if (libsym != NULL) {
146 struct find_symbol_data f_data = {
147 .old_libsym = libsym,
148 };
149 lib = proc_each_library(old_proc, NULL,
150 find_sym_in_lib, &f_data);
151 assert(lib != NULL);
152 libsym = f_data.found_libsym;
153 }
154
155 /* LIB and LIBSYM now hold the new library and symbol that
156 * correspond to the original breakpoint. Now we can do the
157 * clone itself. */
158 breakpoint_init_base(retp, new_proc, bp->addr, libsym);
159 memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
160 retp->enabled = bp->enabled;
Petr Machatad3cc9882012-04-13 21:40:23 +0200161 if (arch_breakpoint_clone(retp, bp) < 0)
162 return -1;
163 breakpoint_set_callbacks(retp, bp->cbs);
164 return 0;
165}
166
Petr Machata52dbfb12012-03-29 16:38:26 +0200167int
Petr Machatafa0c5702012-04-13 18:43:40 +0200168breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200169{
Petr Machata52dbfb12012-03-29 16:38:26 +0200170 bp->enabled++;
171 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200172 assert(proc->pid != 0);
173 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200174 }
175 return 0;
176}
177
178int
Petr Machatafa0c5702012-04-13 18:43:40 +0200179breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200180{
Petr Machata52dbfb12012-03-29 16:38:26 +0200181 bp->enabled--;
182 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200183 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200184 assert(bp->enabled >= 0);
185 return 0;
186}
187
Petr Machata9294d822012-02-07 12:35:58 +0100188struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100189insert_breakpoint(struct Process *proc, void *addr,
190 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100191{
Petr Machata9df15012012-02-20 12:49:46 +0100192 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200193
194 /* Only the group leader should be getting the breakpoints and
195 * thus have ->breakpoint initialized. */
196 assert(leader != NULL);
197 assert(leader->breakpoints != NULL);
198
Petr Machata050b0a62012-04-03 01:30:30 +0200199 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
200 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200201
Petr Machata218c5ff2012-04-15 04:22:39 +0200202 assert(addr != 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100203
Petr Machata52dbfb12012-03-29 16:38:26 +0200204 /* XXX what we need to do instead is have a list of
205 * breakpoints that are enabled at this address. The
206 * following works if every breakpoint is the same and there's
207 * no extra data, but that doesn't hold anymore. For now it
208 * will suffice, about the only realistic case where we need
209 * to have more than one breakpoint per address is return from
210 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100211 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100212 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100213 sbp = malloc(sizeof(*sbp));
214 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200215 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
216 free(sbp);
217 return NULL;
218 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200219 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200220 fail:
221 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 free(sbp);
223 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100224 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100225 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226
Petr Machata45728772012-04-15 04:23:55 +0200227 if (breakpoint_turn_on(sbp, proc) < 0) {
228 proc_remove_breakpoint(leader, sbp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200229 goto fail;
Petr Machata45728772012-04-15 04:23:55 +0200230 }
Petr Machata9294d822012-02-07 12:35:58 +0100231
232 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200233}
234
Juan Cespedesf1350522008-12-16 18:19:58 +0100235void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100236delete_breakpoint(Process *proc, void *addr)
237{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200238 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
239
Petr Machata9a5420c2011-07-09 11:21:23 +0200240 Process * leader = proc->leader;
241 assert(leader != NULL);
242
Petr Machataf7fee432012-04-19 17:00:53 +0200243 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
244 assert(sbp != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200245 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100246 if (sbp == NULL)
247 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200248
Petr Machatafa0c5702012-04-13 18:43:40 +0200249 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200250 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
251 breakpoint_name(sbp), sbp->addr);
252 return;
253 }
Petr Machataf7fee432012-04-19 17:00:53 +0200254 if (sbp->enabled == 0) {
255 proc_remove_breakpoint(leader, sbp);
256 breakpoint_destroy(sbp);
257 free(sbp);
258 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200259}
260
Petr Machatae9aebd62012-03-25 01:38:53 +0100261const char *
262breakpoint_name(const struct breakpoint *bp)
263{
264 assert(bp != NULL);
265 return bp->libsym != NULL ? bp->libsym->name : NULL;
266}
267
Petr Machata52dbfb12012-03-29 16:38:26 +0200268struct library *
269breakpoint_library(const struct breakpoint *bp)
270{
271 assert(bp != NULL);
272 return bp->libsym != NULL ? bp->libsym->lib : NULL;
273}
274
Juan Cespedesf1350522008-12-16 18:19:58 +0100275static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100276enable_bp_cb(void *addr, void *sbp, void *proc)
277{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200278 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100279 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200280 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200281}
282
Juan Cespedesf1350522008-12-16 18:19:58 +0100283void
Petr Machatabc373262012-02-07 23:31:15 +0100284enable_all_breakpoints(Process *proc)
285{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200286 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100287
288 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
289 if (proc->breakpoints) {
290 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
291 proc);
292 }
293#ifdef __mips__
294 {
295 /*
296 * I'm sure there is a nicer way to do this. We need to
297 * insert breakpoints _after_ the child has been started.
298 */
299 struct library_symbol *sym;
300 struct library_symbol *new_sym;
301 sym=proc->list_of_symbols;
302 while(sym){
303 void *addr= sym2addr(proc,sym);
304 if(!addr){
305 sym=sym->next;
306 continue;
307 }
308 if(dict_find_entry(proc->breakpoints,addr)){
309 sym=sym->next;
310 continue;
311 }
312 debug(2,"inserting bp %p %s",addr,sym->name);
313 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
314 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
315 new_sym->next=proc->list_of_symbols;
316 proc->list_of_symbols=new_sym;
317 insert_breakpoint(proc, addr, new_sym);
318 sym=sym->next;
319 }
320 }
321#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100322}
323
Juan Cespedesf1350522008-12-16 18:19:58 +0100324static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100325disable_bp_cb(void *addr, void *sbp, void *proc)
326{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100328 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200329 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200330}
331
Juan Cespedesf1350522008-12-16 18:19:58 +0100332void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200333disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200334 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200335 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100336 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100337}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100338
Petr Machatad09d2402012-04-13 21:34:08 +0200339/* XXX This is not currently properly supported. On clone, this is
340 * just sliced. Hopefully at the point that clone is done, this
341 * breakpoint is not necessary anymore. If this use case ends up
342 * being important, we need to add a clone and destroy callbacks to
343 * breakpoints, and we should also probably drop arch_breakpoint_data
344 * so that we don't end up with two different customization mechanisms
345 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200346struct entry_breakpoint {
347 struct breakpoint super;
348 target_address_t dyn_addr;
349};
350
Petr Machata02648a12012-02-07 13:44:54 +0100351static void
Petr Machata12affff2012-03-29 18:33:03 +0200352entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100353{
Petr Machata52dbfb12012-03-29 16:38:26 +0200354 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100355 if (proc == NULL || proc->leader == NULL)
356 return;
Petr Machata5ee36822012-04-19 17:01:51 +0200357 target_address_t dyn_addr = bp->dyn_addr;
Petr Machata3fd099b2012-04-03 02:25:42 +0200358 delete_breakpoint(proc, bp->super.addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200359 linkmap_init(proc, dyn_addr);
Petr Machata93d95df2012-04-17 05:16:19 +0200360 arch_dynlink_done(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200361}
362
363int
364entry_breakpoint_init(struct Process *proc,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200365 struct entry_breakpoint *bp, target_address_t addr,
366 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200367{
368 int err;
369 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
370 return err;
371
372 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200373 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200374 };
375 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200376 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200377 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100378}
379
Petr Machata1974dbc2011-08-19 18:58:01 +0200380int
Petr Machata75934ad2012-04-14 02:28:03 +0200381breakpoints_init(Process *proc)
Petr Machatac7585b62011-07-08 22:58:12 +0200382{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200383 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200384
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385 /* XXX breakpoint dictionary should be initialized
386 * outside. Here we just put in breakpoints. */
387 assert(proc->breakpoints != NULL);
388
389 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200390 assert(proc->leader == proc);
391
Petr Machata807cdd82012-04-05 02:08:25 +0200392 /* N.B. the following used to be conditional on this, and
393 * maybe it still needs to be. */
394 assert(proc->filename != NULL);
395
396 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
397 struct entry_breakpoint *entry_bp = NULL;
398 int bp_state = 0;
399 int result = -1;
400 switch (lib != NULL) {
401 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200402 switch (bp_state) {
403 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200404 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200405 proc_remove_breakpoint(proc, &entry_bp->super);
406 case 1:
407 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200408 }
Petr Machataa2416362012-04-06 02:43:34 +0200409 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200410 free(entry_bp);
411 case 0:
412 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100413 }
414
Petr Machata807cdd82012-04-05 02:08:25 +0200415 entry_bp = malloc(sizeof(*entry_bp));
416 if (entry_bp == NULL
417 || (result = entry_breakpoint_init(proc, entry_bp,
418 lib->entry, lib)) < 0)
419 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200420 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200421
Petr Machata807cdd82012-04-05 02:08:25 +0200422 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
423 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200424 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200425
Petr Machatafa0c5702012-04-13 18:43:40 +0200426 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
Petr Machata807cdd82012-04-05 02:08:25 +0200427 goto fail;
Petr Machataa2416362012-04-06 02:43:34 +0200428 proc_add_library(proc, lib);
429
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100430 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200431 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100432}