blob: b2ae525765b56e9ef18b471d14b0c5e501b58e5f [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;
Petr Machatad3cc9882012-04-13 21:40:23 +0200154 if (arch_breakpoint_clone(retp, bp) < 0)
155 return -1;
156 breakpoint_set_callbacks(retp, bp->cbs);
157 return 0;
158}
159
Petr Machata52dbfb12012-03-29 16:38:26 +0200160int
Petr Machatafa0c5702012-04-13 18:43:40 +0200161breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200162{
Petr Machata52dbfb12012-03-29 16:38:26 +0200163 bp->enabled++;
164 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200165 assert(proc->pid != 0);
166 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200167 }
168 return 0;
169}
170
171int
Petr Machatafa0c5702012-04-13 18:43:40 +0200172breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200173{
Petr Machata52dbfb12012-03-29 16:38:26 +0200174 bp->enabled--;
175 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200176 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200177 assert(bp->enabled >= 0);
178 return 0;
179}
180
Petr Machata9294d822012-02-07 12:35:58 +0100181struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100182insert_breakpoint(struct Process *proc, void *addr,
183 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100184{
Petr Machata9df15012012-02-20 12:49:46 +0100185 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200186
187 /* Only the group leader should be getting the breakpoints and
188 * thus have ->breakpoint initialized. */
189 assert(leader != NULL);
190 assert(leader->breakpoints != NULL);
191
Petr Machata050b0a62012-04-03 01:30:30 +0200192 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
193 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200194
Petr Machata218c5ff2012-04-15 04:22:39 +0200195 assert(addr != 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100196
Petr Machata52dbfb12012-03-29 16:38:26 +0200197 /* XXX what we need to do instead is have a list of
198 * breakpoints that are enabled at this address. The
199 * following works if every breakpoint is the same and there's
200 * no extra data, but that doesn't hold anymore. For now it
201 * will suffice, about the only realistic case where we need
202 * to have more than one breakpoint per address is return from
203 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100204 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100205 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206 sbp = malloc(sizeof(*sbp));
207 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200208 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
209 free(sbp);
210 return NULL;
211 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200212 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200213 fail:
214 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215 free(sbp);
216 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100217 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100218 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219
Petr Machata45728772012-04-15 04:23:55 +0200220 if (breakpoint_turn_on(sbp, proc) < 0) {
221 proc_remove_breakpoint(leader, sbp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200222 goto fail;
Petr Machata45728772012-04-15 04:23:55 +0200223 }
Petr Machata9294d822012-02-07 12:35:58 +0100224
225 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200226}
227
Juan Cespedesf1350522008-12-16 18:19:58 +0100228void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100229delete_breakpoint(Process *proc, void *addr)
230{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200231 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
232
Petr Machata9a5420c2011-07-09 11:21:23 +0200233 Process * leader = proc->leader;
234 assert(leader != NULL);
235
Petr Machataf7fee432012-04-19 17:00:53 +0200236 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
237 assert(sbp != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200238 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 if (sbp == NULL)
240 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200241
Petr Machatafa0c5702012-04-13 18:43:40 +0200242 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200243 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
244 breakpoint_name(sbp), sbp->addr);
245 return;
246 }
Petr Machataf7fee432012-04-19 17:00:53 +0200247 if (sbp->enabled == 0) {
248 proc_remove_breakpoint(leader, sbp);
249 breakpoint_destroy(sbp);
250 free(sbp);
251 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200252}
253
Petr Machatae9aebd62012-03-25 01:38:53 +0100254const char *
255breakpoint_name(const struct breakpoint *bp)
256{
257 assert(bp != NULL);
258 return bp->libsym != NULL ? bp->libsym->name : NULL;
259}
260
Petr Machata52dbfb12012-03-29 16:38:26 +0200261struct library *
262breakpoint_library(const struct breakpoint *bp)
263{
264 assert(bp != NULL);
265 return bp->libsym != NULL ? bp->libsym->lib : NULL;
266}
267
Juan Cespedesf1350522008-12-16 18:19:58 +0100268static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100269enable_bp_cb(void *addr, void *sbp, void *proc)
270{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200271 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100272 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200273 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200274}
275
Juan Cespedesf1350522008-12-16 18:19:58 +0100276void
Petr Machatabc373262012-02-07 23:31:15 +0100277enable_all_breakpoints(Process *proc)
278{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200279 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100280
281 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
282 if (proc->breakpoints) {
283 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
284 proc);
285 }
286#ifdef __mips__
287 {
288 /*
289 * I'm sure there is a nicer way to do this. We need to
290 * insert breakpoints _after_ the child has been started.
291 */
292 struct library_symbol *sym;
293 struct library_symbol *new_sym;
294 sym=proc->list_of_symbols;
295 while(sym){
296 void *addr= sym2addr(proc,sym);
297 if(!addr){
298 sym=sym->next;
299 continue;
300 }
301 if(dict_find_entry(proc->breakpoints,addr)){
302 sym=sym->next;
303 continue;
304 }
305 debug(2,"inserting bp %p %s",addr,sym->name);
306 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
307 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
308 new_sym->next=proc->list_of_symbols;
309 proc->list_of_symbols=new_sym;
310 insert_breakpoint(proc, addr, new_sym);
311 sym=sym->next;
312 }
313 }
314#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100315}
316
Juan Cespedesf1350522008-12-16 18:19:58 +0100317static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100318disable_bp_cb(void *addr, void *sbp, void *proc)
319{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200320 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100321 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200322 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200323}
324
Juan Cespedesf1350522008-12-16 18:19:58 +0100325void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200326disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200327 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200328 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100329 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100330}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100331
Petr Machatad09d2402012-04-13 21:34:08 +0200332/* XXX This is not currently properly supported. On clone, this is
333 * just sliced. Hopefully at the point that clone is done, this
334 * breakpoint is not necessary anymore. If this use case ends up
335 * being important, we need to add a clone and destroy callbacks to
336 * breakpoints, and we should also probably drop arch_breakpoint_data
337 * so that we don't end up with two different customization mechanisms
338 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200339struct entry_breakpoint {
340 struct breakpoint super;
341 target_address_t dyn_addr;
342};
343
Petr Machata02648a12012-02-07 13:44:54 +0100344static void
Petr Machata12affff2012-03-29 18:33:03 +0200345entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100346{
Petr Machata00928202012-04-07 01:14:24 +0200347 fprintf(stderr, "entry_breakpoint_on_hit\n");
Petr Machata52dbfb12012-03-29 16:38:26 +0200348 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100349 if (proc == NULL || proc->leader == NULL)
350 return;
Petr Machata5ee36822012-04-19 17:01:51 +0200351 target_address_t dyn_addr = bp->dyn_addr;
Petr Machata3fd099b2012-04-03 02:25:42 +0200352 delete_breakpoint(proc, bp->super.addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200353 linkmap_init(proc, dyn_addr);
Petr Machata93d95df2012-04-17 05:16:19 +0200354 arch_dynlink_done(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200355}
356
357int
358entry_breakpoint_init(struct Process *proc,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200359 struct entry_breakpoint *bp, target_address_t addr,
360 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200361{
362 int err;
363 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
364 return err;
365
366 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200367 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200368 };
369 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200370 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200371 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100372}
373
Petr Machata1974dbc2011-08-19 18:58:01 +0200374int
Petr Machata75934ad2012-04-14 02:28:03 +0200375breakpoints_init(Process *proc)
Petr Machatac7585b62011-07-08 22:58:12 +0200376{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200377 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200378
Petr Machata2b46cfc2012-02-18 11:17:29 +0100379 /* XXX breakpoint dictionary should be initialized
380 * outside. Here we just put in breakpoints. */
381 assert(proc->breakpoints != NULL);
382
383 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200384 assert(proc->leader == proc);
385
Petr Machata807cdd82012-04-05 02:08:25 +0200386 /* N.B. the following used to be conditional on this, and
387 * maybe it still needs to be. */
388 assert(proc->filename != NULL);
389
390 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
391 struct entry_breakpoint *entry_bp = NULL;
392 int bp_state = 0;
393 int result = -1;
394 switch (lib != NULL) {
395 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200396 switch (bp_state) {
397 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200398 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200399 proc_remove_breakpoint(proc, &entry_bp->super);
400 case 1:
401 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200402 }
Petr Machataa2416362012-04-06 02:43:34 +0200403 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200404 free(entry_bp);
405 case 0:
406 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100407 }
408
Petr Machata807cdd82012-04-05 02:08:25 +0200409 entry_bp = malloc(sizeof(*entry_bp));
410 if (entry_bp == NULL
411 || (result = entry_breakpoint_init(proc, entry_bp,
412 lib->entry, lib)) < 0)
413 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200414 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200415
Petr Machata807cdd82012-04-05 02:08:25 +0200416 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
417 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200418 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200419
Petr Machatafa0c5702012-04-13 18:43:40 +0200420 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
Petr Machata807cdd82012-04-05 02:08:25 +0200421 goto fail;
Petr Machataa2416362012-04-06 02:43:34 +0200422 proc_add_library(proc, lib);
423
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100424 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200425 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100426}