blob: dd1a010d441faf06b2dcb26c2c195abfde142804 [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
Petr Machata8cce1192012-03-25 01:37:19 +0100101
102 arch_breakpoint_destroy(bp);
103}
104
Petr Machata52dbfb12012-03-29 16:38:26 +0200105int
Petr Machatafa0c5702012-04-13 18:43:40 +0200106breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200107{
108 /* Make sure it was inserted. XXX In a clean world, we would
109 * have breakpoint_site representing a place and breakpoint
110 * representing inserted breakpoint. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200111 bp->enabled++;
112 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200113 assert(proc->pid != 0);
114 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200115 }
116 return 0;
117}
118
119int
Petr Machatafa0c5702012-04-13 18:43:40 +0200120breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200121{
Petr Machata52dbfb12012-03-29 16:38:26 +0200122 bp->enabled--;
123 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200124 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200125 assert(bp->enabled >= 0);
126 return 0;
127}
128
Petr Machata9294d822012-02-07 12:35:58 +0100129struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100130insert_breakpoint(struct Process *proc, void *addr,
131 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100132{
Petr Machata9df15012012-02-20 12:49:46 +0100133 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200134
135 /* Only the group leader should be getting the breakpoints and
136 * thus have ->breakpoint initialized. */
137 assert(leader != NULL);
138 assert(leader->breakpoints != NULL);
139
Petr Machata050b0a62012-04-03 01:30:30 +0200140 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
141 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200142
Petr Machata81c65272012-03-21 04:57:25 +0100143 if (addr == 0) {
144 /* XXX we need a better way to deal with this. For
145 * now, just abuse errno to carry the error
146 * information. */
147 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100148 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100149 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100150
Petr Machata52dbfb12012-03-29 16:38:26 +0200151 /* XXX what we need to do instead is have a list of
152 * breakpoints that are enabled at this address. The
153 * following works if every breakpoint is the same and there's
154 * no extra data, but that doesn't hold anymore. For now it
155 * will suffice, about the only realistic case where we need
156 * to have more than one breakpoint per address is return from
157 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100158 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100159 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100160 sbp = malloc(sizeof(*sbp));
161 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200162 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
163 free(sbp);
164 return NULL;
165 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200166 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200167 fail:
168 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100169 free(sbp);
170 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100171 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100172 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173
Petr Machatafa0c5702012-04-13 18:43:40 +0200174 if (breakpoint_turn_on(sbp, proc) < 0)
Petr Machata52dbfb12012-03-29 16:38:26 +0200175 goto fail;
Petr Machata9294d822012-02-07 12:35:58 +0100176
177 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200178}
179
Juan Cespedesf1350522008-12-16 18:19:58 +0100180void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100181delete_breakpoint(Process *proc, void *addr)
182{
Petr Machata9294d822012-02-07 12:35:58 +0100183 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200184
185 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
186
Petr Machata9a5420c2011-07-09 11:21:23 +0200187 Process * leader = proc->leader;
188 assert(leader != NULL);
189
190 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100191 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200192 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 if (sbp == NULL)
194 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200195
Petr Machatafa0c5702012-04-13 18:43:40 +0200196 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200197 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
198 breakpoint_name(sbp), sbp->addr);
199 return;
200 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200201}
202
Petr Machatae9aebd62012-03-25 01:38:53 +0100203const char *
204breakpoint_name(const struct breakpoint *bp)
205{
206 assert(bp != NULL);
207 return bp->libsym != NULL ? bp->libsym->name : NULL;
208}
209
Petr Machata52dbfb12012-03-29 16:38:26 +0200210struct library *
211breakpoint_library(const struct breakpoint *bp)
212{
213 assert(bp != NULL);
214 return bp->libsym != NULL ? bp->libsym->lib : NULL;
215}
216
Juan Cespedesf1350522008-12-16 18:19:58 +0100217static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100218enable_bp_cb(void *addr, void *sbp, void *proc)
219{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200220 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100221 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200222 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200223}
224
Juan Cespedesf1350522008-12-16 18:19:58 +0100225void
Petr Machatabc373262012-02-07 23:31:15 +0100226enable_all_breakpoints(Process *proc)
227{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200228 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100229
230 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
231 if (proc->breakpoints) {
232 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
233 proc);
234 }
235#ifdef __mips__
236 {
237 /*
238 * I'm sure there is a nicer way to do this. We need to
239 * insert breakpoints _after_ the child has been started.
240 */
241 struct library_symbol *sym;
242 struct library_symbol *new_sym;
243 sym=proc->list_of_symbols;
244 while(sym){
245 void *addr= sym2addr(proc,sym);
246 if(!addr){
247 sym=sym->next;
248 continue;
249 }
250 if(dict_find_entry(proc->breakpoints,addr)){
251 sym=sym->next;
252 continue;
253 }
254 debug(2,"inserting bp %p %s",addr,sym->name);
255 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
256 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
257 new_sym->next=proc->list_of_symbols;
258 proc->list_of_symbols=new_sym;
259 insert_breakpoint(proc, addr, new_sym);
260 sym=sym->next;
261 }
262 }
263#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100264}
265
Juan Cespedesf1350522008-12-16 18:19:58 +0100266static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100267disable_bp_cb(void *addr, void *sbp, void *proc)
268{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200269 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100270 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200271 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200272}
273
Juan Cespedesf1350522008-12-16 18:19:58 +0100274void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200275disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200276 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200277 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100278 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100280
Petr Machatad09d2402012-04-13 21:34:08 +0200281/* XXX This is not currently properly supported. On clone, this is
282 * just sliced. Hopefully at the point that clone is done, this
283 * breakpoint is not necessary anymore. If this use case ends up
284 * being important, we need to add a clone and destroy callbacks to
285 * breakpoints, and we should also probably drop arch_breakpoint_data
286 * so that we don't end up with two different customization mechanisms
287 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200288struct entry_breakpoint {
289 struct breakpoint super;
290 target_address_t dyn_addr;
291};
292
Petr Machata02648a12012-02-07 13:44:54 +0100293static void
Petr Machata12affff2012-03-29 18:33:03 +0200294entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100295{
Petr Machata00928202012-04-07 01:14:24 +0200296 fprintf(stderr, "entry_breakpoint_on_hit\n");
Petr Machata52dbfb12012-03-29 16:38:26 +0200297 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100298 if (proc == NULL || proc->leader == NULL)
299 return;
Petr Machata3fd099b2012-04-03 02:25:42 +0200300 delete_breakpoint(proc, bp->super.addr);
Petr Machata52dbfb12012-03-29 16:38:26 +0200301 linkmap_init(proc, bp->dyn_addr);
302}
303
304int
305entry_breakpoint_init(struct Process *proc,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200306 struct entry_breakpoint *bp, target_address_t addr,
307 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200308{
309 int err;
310 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
311 return err;
312
313 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200314 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200315 };
316 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200317 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200318 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100319}
320
Petr Machata1974dbc2011-08-19 18:58:01 +0200321int
Petr Machatac7585b62011-07-08 22:58:12 +0200322breakpoints_init(Process *proc, int enable)
323{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200324 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200325
Petr Machata2b46cfc2012-02-18 11:17:29 +0100326 /* XXX breakpoint dictionary should be initialized
327 * outside. Here we just put in breakpoints. */
328 assert(proc->breakpoints != NULL);
329
330 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200331 assert(proc->leader == proc);
332
Petr Machata807cdd82012-04-05 02:08:25 +0200333 /* N.B. the following used to be conditional on this, and
334 * maybe it still needs to be. */
335 assert(proc->filename != NULL);
336
337 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
338 struct entry_breakpoint *entry_bp = NULL;
339 int bp_state = 0;
340 int result = -1;
341 switch (lib != NULL) {
342 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200343 switch (bp_state) {
344 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200345 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200346 proc_remove_breakpoint(proc, &entry_bp->super);
347 case 1:
348 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200349 }
Petr Machataa2416362012-04-06 02:43:34 +0200350 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200351 free(entry_bp);
352 case 0:
353 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100354 }
355
Petr Machata807cdd82012-04-05 02:08:25 +0200356 entry_bp = malloc(sizeof(*entry_bp));
357 if (entry_bp == NULL
358 || (result = entry_breakpoint_init(proc, entry_bp,
359 lib->entry, lib)) < 0)
360 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200361 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200362
Petr Machata807cdd82012-04-05 02:08:25 +0200363 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
364 goto fail;
Petr Machata807cdd82012-04-05 02:08:25 +0200365 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200366
Petr Machatafa0c5702012-04-13 18:43:40 +0200367 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
Petr Machata807cdd82012-04-05 02:08:25 +0200368 goto fail;
Petr Machataa2416362012-04-06 02:43:34 +0200369 proc_add_library(proc, lib);
370
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100371 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200372 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100373}