blob: ab0e688c1464b69bc14b4a9e15077bd482f504b3 [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 Machata2b46cfc2012-02-18 11:17:29 +010080 bp->addr = addr;
81 memset(bp->orig_value, 0, sizeof(bp->orig_value));
82 bp->enabled = 0;
83 bp->libsym = libsym;
84 return arch_breakpoint_init(proc, bp);
85}
86
Petr Machata8cce1192012-03-25 01:37:19 +010087void
Petr Machata55ac9322012-03-27 03:07:35 +020088breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
89{
90 if (bp->cbs != NULL)
91 assert(bp->cbs == NULL);
92 bp->cbs = cbs;
93}
94
95void
Petr Machata8cce1192012-03-25 01:37:19 +010096breakpoint_destroy(struct breakpoint *bp)
97{
98 if (bp == NULL)
99 return;
100
101 /* XXX I'm not convinced that we need on_destroy. We already
102 * have arch_breakpoint_destroy, which is necessary as a
103 * counterpart of arch_breakpoint_init in any case. */
104 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
105 (bp->cbs->on_destroy) (bp);
106
107 arch_breakpoint_destroy(bp);
108}
109
Petr Machata52dbfb12012-03-29 16:38:26 +0200110int
Petr Machatafa0c5702012-04-13 18:43:40 +0200111breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200112{
113 /* Make sure it was inserted. XXX In a clean world, we would
114 * have breakpoint_site representing a place and breakpoint
115 * representing inserted breakpoint. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200116 bp->enabled++;
117 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200118 assert(proc->pid != 0);
119 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200120 }
121 return 0;
122}
123
124int
Petr Machatafa0c5702012-04-13 18:43:40 +0200125breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200126{
Petr Machata52dbfb12012-03-29 16:38:26 +0200127 bp->enabled--;
128 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200129 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200130 assert(bp->enabled >= 0);
131 return 0;
132}
133
Petr Machata9294d822012-02-07 12:35:58 +0100134struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100135insert_breakpoint(struct Process *proc, void *addr,
136 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100137{
Petr Machata9df15012012-02-20 12:49:46 +0100138 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200139
140 /* Only the group leader should be getting the breakpoints and
141 * thus have ->breakpoint initialized. */
142 assert(leader != NULL);
143 assert(leader->breakpoints != NULL);
144
Petr Machata050b0a62012-04-03 01:30:30 +0200145 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
146 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200147
Petr Machata81c65272012-03-21 04:57:25 +0100148 if (addr == 0) {
149 /* XXX we need a better way to deal with this. For
150 * now, just abuse errno to carry the error
151 * information. */
152 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100153 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100154 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100155
Petr Machata52dbfb12012-03-29 16:38:26 +0200156 /* XXX what we need to do instead is have a list of
157 * breakpoints that are enabled at this address. The
158 * following works if every breakpoint is the same and there's
159 * no extra data, but that doesn't hold anymore. For now it
160 * will suffice, about the only realistic case where we need
161 * to have more than one breakpoint per address is return from
162 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100163 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100164 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100165 sbp = malloc(sizeof(*sbp));
166 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200167 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
168 free(sbp);
169 return NULL;
170 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200171 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200172 fail:
173 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100174 free(sbp);
175 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100176 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100177 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100178
Petr Machatafa0c5702012-04-13 18:43:40 +0200179 if (breakpoint_turn_on(sbp, proc) < 0)
Petr Machata52dbfb12012-03-29 16:38:26 +0200180 goto fail;
Petr Machata9294d822012-02-07 12:35:58 +0100181
182 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200183}
184
Juan Cespedesf1350522008-12-16 18:19:58 +0100185void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100186delete_breakpoint(Process *proc, void *addr)
187{
Petr Machata9294d822012-02-07 12:35:58 +0100188 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200189
190 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
191
Petr Machata9a5420c2011-07-09 11:21:23 +0200192 Process * leader = proc->leader;
193 assert(leader != NULL);
194
195 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200197 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 if (sbp == NULL)
199 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200200
Petr Machatafa0c5702012-04-13 18:43:40 +0200201 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200202 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
203 breakpoint_name(sbp), sbp->addr);
204 return;
205 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200206}
207
Petr Machatae9aebd62012-03-25 01:38:53 +0100208const char *
209breakpoint_name(const struct breakpoint *bp)
210{
211 assert(bp != NULL);
212 return bp->libsym != NULL ? bp->libsym->name : NULL;
213}
214
Petr Machata52dbfb12012-03-29 16:38:26 +0200215struct library *
216breakpoint_library(const struct breakpoint *bp)
217{
218 assert(bp != NULL);
219 return bp->libsym != NULL ? bp->libsym->lib : NULL;
220}
221
Juan Cespedesf1350522008-12-16 18:19:58 +0100222static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100223enable_bp_cb(void *addr, void *sbp, void *proc)
224{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200225 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100226 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200227 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200228}
229
Juan Cespedesf1350522008-12-16 18:19:58 +0100230void
Petr Machatabc373262012-02-07 23:31:15 +0100231enable_all_breakpoints(Process *proc)
232{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200233 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100234
235 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
236 if (proc->breakpoints) {
237 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
238 proc);
239 }
240#ifdef __mips__
241 {
242 /*
243 * I'm sure there is a nicer way to do this. We need to
244 * insert breakpoints _after_ the child has been started.
245 */
246 struct library_symbol *sym;
247 struct library_symbol *new_sym;
248 sym=proc->list_of_symbols;
249 while(sym){
250 void *addr= sym2addr(proc,sym);
251 if(!addr){
252 sym=sym->next;
253 continue;
254 }
255 if(dict_find_entry(proc->breakpoints,addr)){
256 sym=sym->next;
257 continue;
258 }
259 debug(2,"inserting bp %p %s",addr,sym->name);
260 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
261 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
262 new_sym->next=proc->list_of_symbols;
263 proc->list_of_symbols=new_sym;
264 insert_breakpoint(proc, addr, new_sym);
265 sym=sym->next;
266 }
267 }
268#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100269}
270
Juan Cespedesf1350522008-12-16 18:19:58 +0100271static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100272disable_bp_cb(void *addr, void *sbp, void *proc)
273{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200274 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100275 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200276 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200277}
278
Juan Cespedesf1350522008-12-16 18:19:58 +0100279void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200280disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200281 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200282 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100283 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100285
Petr Machata52dbfb12012-03-29 16:38:26 +0200286struct entry_breakpoint {
287 struct breakpoint super;
288 target_address_t dyn_addr;
289};
290
Petr Machata02648a12012-02-07 13:44:54 +0100291static void
Petr Machata12affff2012-03-29 18:33:03 +0200292entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100293{
Petr Machata00928202012-04-07 01:14:24 +0200294 fprintf(stderr, "entry_breakpoint_on_hit\n");
Petr Machata52dbfb12012-03-29 16:38:26 +0200295 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100296 if (proc == NULL || proc->leader == NULL)
297 return;
Petr Machata3fd099b2012-04-03 02:25:42 +0200298 delete_breakpoint(proc, bp->super.addr);
Petr Machata52dbfb12012-03-29 16:38:26 +0200299 linkmap_init(proc, bp->dyn_addr);
300}
301
Petr Machata12affff2012-03-29 18:33:03 +0200302static void
303entry_breakpoint_on_destroy(struct breakpoint *a)
304{
305}
306
Petr Machata52dbfb12012-03-29 16:38:26 +0200307int
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 = {
Petr Machata12affff2012-03-29 18:33:03 +0200317 .on_hit = entry_breakpoint_on_hit,
318 .on_destroy = entry_breakpoint_on_destroy,
Petr Machata52dbfb12012-03-29 16:38:26 +0200319 };
320 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200321 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200322 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100323}
324
Petr Machata1974dbc2011-08-19 18:58:01 +0200325int
Petr Machatac7585b62011-07-08 22:58:12 +0200326breakpoints_init(Process *proc, int enable)
327{
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
Petr Machata807cdd82012-04-05 02:08:25 +0200337 /* N.B. the following used to be conditional on this, and
338 * maybe it still needs to be. */
339 assert(proc->filename != NULL);
340
341 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
342 struct entry_breakpoint *entry_bp = NULL;
343 int bp_state = 0;
344 int result = -1;
345 switch (lib != NULL) {
346 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200347 switch (bp_state) {
348 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200349 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200350 proc_remove_breakpoint(proc, &entry_bp->super);
351 case 1:
352 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200353 }
Petr Machataa2416362012-04-06 02:43:34 +0200354 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200355 free(entry_bp);
356 case 0:
357 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100358 }
359
Petr Machata807cdd82012-04-05 02:08:25 +0200360 entry_bp = malloc(sizeof(*entry_bp));
361 if (entry_bp == NULL
362 || (result = entry_breakpoint_init(proc, entry_bp,
363 lib->entry, lib)) < 0)
364 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200365 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200366
Petr Machata807cdd82012-04-05 02:08:25 +0200367 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
368 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200369 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200370
Petr Machatafa0c5702012-04-13 18:43:40 +0200371 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
Petr Machata807cdd82012-04-05 02:08:25 +0200372 goto fail;
Petr Machataa2416362012-04-06 02:43:34 +0200373 proc_add_library(proc, lib);
374
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100375 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200376 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100377}