blob: ab61bf22c1e057a5f0ebcd3fbd0e5106b999a0c1 [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 Machatad3cc9882012-04-13 21:40:23 +020069
70int
71arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
72{
73 return 0;
74}
Petr Machata2b46cfc2012-02-18 11:17:29 +010075#endif
76
Petr Machatad3cc9882012-04-13 21:40:23 +020077static void
78breakpoint_init_base(struct breakpoint *bp, struct Process *proc,
79 target_address_t addr, struct library_symbol *libsym)
80{
81 bp->cbs = NULL;
82 bp->addr = addr;
83 memset(bp->orig_value, 0, sizeof(bp->orig_value));
84 bp->enabled = 0;
85 bp->libsym = libsym;
86}
87
Petr Machata52dbfb12012-03-29 16:38:26 +020088/* On second thought, I don't think we need PROC. All the translation
89 * (arch_translate_address in particular) should be doable using
90 * static lookups of various sections in the ELF file. We shouldn't
91 * need process for anything. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010092int
93breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020094 target_address_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +010095{
Petr Machatad3cc9882012-04-13 21:40:23 +020096 breakpoint_init_base(bp, proc, addr, libsym);
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 return arch_breakpoint_init(proc, bp);
98}
99
Petr Machata8cce1192012-03-25 01:37:19 +0100100void
Petr Machata55ac9322012-03-27 03:07:35 +0200101breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
102{
103 if (bp->cbs != NULL)
104 assert(bp->cbs == NULL);
105 bp->cbs = cbs;
106}
107
108void
Petr Machata8cce1192012-03-25 01:37:19 +0100109breakpoint_destroy(struct breakpoint *bp)
110{
111 if (bp == NULL)
112 return;
Petr Machata8cce1192012-03-25 01:37:19 +0100113 arch_breakpoint_destroy(bp);
114}
115
Petr Machatad3cc9882012-04-13 21:40:23 +0200116struct find_symbol_data {
117 struct library_symbol *old_libsym;
118 struct library_symbol *found_libsym;
119};
120
121static enum callback_status
122find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
123{
124 struct find_symbol_data *fs = u;
125 fs->found_libsym
126 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
127 fs->old_libsym);
128 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
129}
130
131int
132breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
133 struct breakpoint *bp, struct Process *old_proc)
134{
135 /* Find library and symbol that this breakpoint was linked to. */
136 struct library_symbol *libsym = bp->libsym;
137 struct library *lib = NULL;
138 if (libsym != NULL) {
139 struct find_symbol_data f_data = {
140 .old_libsym = libsym,
141 };
142 lib = proc_each_library(old_proc, NULL,
143 find_sym_in_lib, &f_data);
144 assert(lib != NULL);
145 libsym = f_data.found_libsym;
146 }
147
148 /* LIB and LIBSYM now hold the new library and symbol that
149 * correspond to the original breakpoint. Now we can do the
150 * clone itself. */
151 breakpoint_init_base(retp, new_proc, bp->addr, libsym);
152 memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
153 retp->enabled = bp->enabled;
154 retp->debug_enabled = bp->debug_enabled;
155 if (arch_breakpoint_clone(retp, bp) < 0)
156 return -1;
157 breakpoint_set_callbacks(retp, bp->cbs);
158 return 0;
159}
160
Petr Machata52dbfb12012-03-29 16:38:26 +0200161int
Petr Machatafa0c5702012-04-13 18:43:40 +0200162breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200163{
164 /* Make sure it was inserted. XXX In a clean world, we would
165 * have breakpoint_site representing a place and breakpoint
166 * representing inserted breakpoint. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200167 bp->enabled++;
168 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200169 assert(proc->pid != 0);
170 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200171 }
172 return 0;
173}
174
175int
Petr Machatafa0c5702012-04-13 18:43:40 +0200176breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200177{
Petr Machata52dbfb12012-03-29 16:38:26 +0200178 bp->enabled--;
179 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200180 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200181 assert(bp->enabled >= 0);
182 return 0;
183}
184
Petr Machata9294d822012-02-07 12:35:58 +0100185struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100186insert_breakpoint(struct Process *proc, void *addr,
187 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100188{
Petr Machata9df15012012-02-20 12:49:46 +0100189 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200190
191 /* Only the group leader should be getting the breakpoints and
192 * thus have ->breakpoint initialized. */
193 assert(leader != NULL);
194 assert(leader->breakpoints != NULL);
195
Petr Machata050b0a62012-04-03 01:30:30 +0200196 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
197 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200198
Petr Machata81c65272012-03-21 04:57:25 +0100199 if (addr == 0) {
200 /* XXX we need a better way to deal with this. For
201 * now, just abuse errno to carry the error
202 * information. */
203 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100204 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100205 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100206
Petr Machata52dbfb12012-03-29 16:38:26 +0200207 /* XXX what we need to do instead is have a list of
208 * breakpoints that are enabled at this address. The
209 * following works if every breakpoint is the same and there's
210 * no extra data, but that doesn't hold anymore. For now it
211 * will suffice, about the only realistic case where we need
212 * to have more than one breakpoint per address is return from
213 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100214 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100215 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100216 sbp = malloc(sizeof(*sbp));
217 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200218 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
219 free(sbp);
220 return NULL;
221 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200222 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200223 fail:
224 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 free(sbp);
226 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100227 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100228 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229
Petr Machatafa0c5702012-04-13 18:43:40 +0200230 if (breakpoint_turn_on(sbp, proc) < 0)
Petr Machata52dbfb12012-03-29 16:38:26 +0200231 goto fail;
Petr Machata9294d822012-02-07 12:35:58 +0100232
233 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200234}
235
Juan Cespedesf1350522008-12-16 18:19:58 +0100236void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100237delete_breakpoint(Process *proc, void *addr)
238{
Petr Machata9294d822012-02-07 12:35:58 +0100239 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200240
241 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
242
Petr Machata9a5420c2011-07-09 11:21:23 +0200243 Process * leader = proc->leader;
244 assert(leader != NULL);
245
246 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200248 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100249 if (sbp == NULL)
250 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200251
Petr Machatafa0c5702012-04-13 18:43:40 +0200252 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200253 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
254 breakpoint_name(sbp), sbp->addr);
255 return;
256 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200257}
258
Petr Machatae9aebd62012-03-25 01:38:53 +0100259const char *
260breakpoint_name(const struct breakpoint *bp)
261{
262 assert(bp != NULL);
263 return bp->libsym != NULL ? bp->libsym->name : NULL;
264}
265
Petr Machata52dbfb12012-03-29 16:38:26 +0200266struct library *
267breakpoint_library(const struct breakpoint *bp)
268{
269 assert(bp != NULL);
270 return bp->libsym != NULL ? bp->libsym->lib : NULL;
271}
272
Juan Cespedesf1350522008-12-16 18:19:58 +0100273static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100274enable_bp_cb(void *addr, void *sbp, void *proc)
275{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200276 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100277 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200278 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200279}
280
Juan Cespedesf1350522008-12-16 18:19:58 +0100281void
Petr Machatabc373262012-02-07 23:31:15 +0100282enable_all_breakpoints(Process *proc)
283{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200284 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100285
286 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
287 if (proc->breakpoints) {
288 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
289 proc);
290 }
291#ifdef __mips__
292 {
293 /*
294 * I'm sure there is a nicer way to do this. We need to
295 * insert breakpoints _after_ the child has been started.
296 */
297 struct library_symbol *sym;
298 struct library_symbol *new_sym;
299 sym=proc->list_of_symbols;
300 while(sym){
301 void *addr= sym2addr(proc,sym);
302 if(!addr){
303 sym=sym->next;
304 continue;
305 }
306 if(dict_find_entry(proc->breakpoints,addr)){
307 sym=sym->next;
308 continue;
309 }
310 debug(2,"inserting bp %p %s",addr,sym->name);
311 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
312 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
313 new_sym->next=proc->list_of_symbols;
314 proc->list_of_symbols=new_sym;
315 insert_breakpoint(proc, addr, new_sym);
316 sym=sym->next;
317 }
318 }
319#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100320}
321
Juan Cespedesf1350522008-12-16 18:19:58 +0100322static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100323disable_bp_cb(void *addr, void *sbp, void *proc)
324{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200325 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100326 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200327 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200328}
329
Juan Cespedesf1350522008-12-16 18:19:58 +0100330void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200331disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200332 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200333 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100334 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100335}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100336
Petr Machatad09d2402012-04-13 21:34:08 +0200337/* XXX This is not currently properly supported. On clone, this is
338 * just sliced. Hopefully at the point that clone is done, this
339 * breakpoint is not necessary anymore. If this use case ends up
340 * being important, we need to add a clone and destroy callbacks to
341 * breakpoints, and we should also probably drop arch_breakpoint_data
342 * so that we don't end up with two different customization mechanisms
343 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200344struct entry_breakpoint {
345 struct breakpoint super;
346 target_address_t dyn_addr;
347};
348
Petr Machata02648a12012-02-07 13:44:54 +0100349static void
Petr Machata12affff2012-03-29 18:33:03 +0200350entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100351{
Petr Machata00928202012-04-07 01:14:24 +0200352 fprintf(stderr, "entry_breakpoint_on_hit\n");
Petr Machata52dbfb12012-03-29 16:38:26 +0200353 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100354 if (proc == NULL || proc->leader == NULL)
355 return;
Petr Machata3fd099b2012-04-03 02:25:42 +0200356 delete_breakpoint(proc, bp->super.addr);
Petr Machata52dbfb12012-03-29 16:38:26 +0200357 linkmap_init(proc, bp->dyn_addr);
358}
359
360int
361entry_breakpoint_init(struct Process *proc,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200362 struct entry_breakpoint *bp, target_address_t addr,
363 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200364{
365 int err;
366 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
367 return err;
368
369 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200370 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200371 };
372 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200373 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200374 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100375}
376
Petr Machata1974dbc2011-08-19 18:58:01 +0200377int
Petr Machata75934ad2012-04-14 02:28:03 +0200378breakpoints_init(Process *proc)
Petr Machatac7585b62011-07-08 22:58:12 +0200379{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200380 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200381
Petr Machata2b46cfc2012-02-18 11:17:29 +0100382 /* XXX breakpoint dictionary should be initialized
383 * outside. Here we just put in breakpoints. */
384 assert(proc->breakpoints != NULL);
385
386 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200387 assert(proc->leader == proc);
388
Petr Machata807cdd82012-04-05 02:08:25 +0200389 /* N.B. the following used to be conditional on this, and
390 * maybe it still needs to be. */
391 assert(proc->filename != NULL);
392
393 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
394 struct entry_breakpoint *entry_bp = NULL;
395 int bp_state = 0;
396 int result = -1;
397 switch (lib != NULL) {
398 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200399 switch (bp_state) {
400 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200401 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200402 proc_remove_breakpoint(proc, &entry_bp->super);
403 case 1:
404 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200405 }
Petr Machataa2416362012-04-06 02:43:34 +0200406 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200407 free(entry_bp);
408 case 0:
409 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100410 }
411
Petr Machata807cdd82012-04-05 02:08:25 +0200412 entry_bp = malloc(sizeof(*entry_bp));
413 if (entry_bp == NULL
414 || (result = entry_breakpoint_init(proc, entry_bp,
415 lib->entry, lib)) < 0)
416 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200417 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200418
Petr Machata807cdd82012-04-05 02:08:25 +0200419 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
420 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200421 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200422
Petr Machatafa0c5702012-04-13 18:43:40 +0200423 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
Petr Machata807cdd82012-04-05 02:08:25 +0200424 goto fail;
Petr Machataa2416362012-04-06 02:43:34 +0200425 proc_add_library(proc, lib);
426
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100427 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200428 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100429}