blob: 0d2fa5f2bb7ff8310efe56e62178fbcf7dc74c9b [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
71int
72breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020073 target_address_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +010074{
Petr Machata55ac9322012-03-27 03:07:35 +020075 bp->cbs = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +010076 bp->addr = addr;
77 memset(bp->orig_value, 0, sizeof(bp->orig_value));
78 bp->enabled = 0;
79 bp->libsym = libsym;
80 return arch_breakpoint_init(proc, bp);
81}
82
Petr Machata8cce1192012-03-25 01:37:19 +010083void
Petr Machata55ac9322012-03-27 03:07:35 +020084breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
85{
86 if (bp->cbs != NULL)
87 assert(bp->cbs == NULL);
88 bp->cbs = cbs;
89}
90
91void
Petr Machata8cce1192012-03-25 01:37:19 +010092breakpoint_destroy(struct breakpoint *bp)
93{
94 if (bp == NULL)
95 return;
96
97 /* XXX I'm not convinced that we need on_destroy. We already
98 * have arch_breakpoint_destroy, which is necessary as a
99 * counterpart of arch_breakpoint_init in any case. */
100 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
101 (bp->cbs->on_destroy) (bp);
102
103 arch_breakpoint_destroy(bp);
104}
105
Petr Machata9294d822012-02-07 12:35:58 +0100106struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100107insert_breakpoint(struct Process *proc, void *addr,
108 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100109{
Petr Machata9df15012012-02-20 12:49:46 +0100110 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200111
112 /* Only the group leader should be getting the breakpoints and
113 * thus have ->breakpoint initialized. */
114 assert(leader != NULL);
115 assert(leader->breakpoints != NULL);
116
Juan Cespedescd8976d2009-05-14 13:47:58 +0200117 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)", proc->pid, addr, libsym ? libsym->name : "NULL");
Petr Machatab3f8fef2006-11-30 14:45:07 +0100118 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200119
Petr Machata81c65272012-03-21 04:57:25 +0100120 if (addr == 0) {
121 /* XXX we need a better way to deal with this. For
122 * now, just abuse errno to carry the error
123 * information. */
124 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100125 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100126 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100127
Petr Machata2b46cfc2012-02-18 11:17:29 +0100128 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100129 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 sbp = malloc(sizeof(*sbp));
131 if (sbp == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200132 || breakpoint_init(sbp, proc, addr, libsym) < 0
Petr Machata2b46cfc2012-02-18 11:17:29 +0100133 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
134 free(sbp);
135 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100136 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100137 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100138
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200139 sbp->enabled++;
Petr Machata9df15012012-02-20 12:49:46 +0100140 if (sbp->enabled == 1) {
Petr Machatac7585b62011-07-08 22:58:12 +0200141 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200142 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200143 }
Petr Machata9294d822012-02-07 12:35:58 +0100144
145 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200146}
147
Juan Cespedesf1350522008-12-16 18:19:58 +0100148void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100149delete_breakpoint(Process *proc, void *addr)
150{
Petr Machata9294d822012-02-07 12:35:58 +0100151 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200152
153 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
154
Petr Machata9a5420c2011-07-09 11:21:23 +0200155 Process * leader = proc->leader;
156 assert(leader != NULL);
157
158 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100159 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200160 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100161 if (sbp == NULL)
162 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200163
164 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200166 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200167 assert(sbp->enabled >= 0);
168}
169
Petr Machatae9aebd62012-03-25 01:38:53 +0100170const char *
171breakpoint_name(const struct breakpoint *bp)
172{
173 assert(bp != NULL);
174 return bp->libsym != NULL ? bp->libsym->name : NULL;
175}
176
Juan Cespedesf1350522008-12-16 18:19:58 +0100177static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100178enable_bp_cb(void *addr, void *sbp, void *proc)
179{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200180 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100181 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200182 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200183}
184
Juan Cespedesf1350522008-12-16 18:19:58 +0100185void
Petr Machatabc373262012-02-07 23:31:15 +0100186enable_all_breakpoints(Process *proc)
187{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200188 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100189
190 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
191 if (proc->breakpoints) {
192 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
193 proc);
194 }
195#ifdef __mips__
196 {
197 /*
198 * I'm sure there is a nicer way to do this. We need to
199 * insert breakpoints _after_ the child has been started.
200 */
201 struct library_symbol *sym;
202 struct library_symbol *new_sym;
203 sym=proc->list_of_symbols;
204 while(sym){
205 void *addr= sym2addr(proc,sym);
206 if(!addr){
207 sym=sym->next;
208 continue;
209 }
210 if(dict_find_entry(proc->breakpoints,addr)){
211 sym=sym->next;
212 continue;
213 }
214 debug(2,"inserting bp %p %s",addr,sym->name);
215 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
216 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
217 new_sym->next=proc->list_of_symbols;
218 proc->list_of_symbols=new_sym;
219 insert_breakpoint(proc, addr, new_sym);
220 sym=sym->next;
221 }
222 }
223#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100224}
225
Juan Cespedesf1350522008-12-16 18:19:58 +0100226static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100227disable_bp_cb(void *addr, void *sbp, void *proc)
228{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200229 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100230 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200231 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200232}
233
Juan Cespedesf1350522008-12-16 18:19:58 +0100234void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200235disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200236 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200237 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100238 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100239}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100240
Petr Machata02648a12012-02-07 13:44:54 +0100241static void
242entry_callback_hit(struct breakpoint *bp, struct Process *proc)
243{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100245 if (proc == NULL || proc->leader == NULL)
246 return;
247 delete_breakpoint(proc, bp->addr); // xxx
Petr Machatacb9a28d2012-03-28 11:11:32 +0200248 //enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249
250 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100251}
252
Petr Machata1974dbc2011-08-19 18:58:01 +0200253int
Petr Machatac7585b62011-07-08 22:58:12 +0200254breakpoints_init(Process *proc, int enable)
255{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200257 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200258
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259 /* XXX breakpoint dictionary should be initialized
260 * outside. Here we just put in breakpoints. */
261 assert(proc->breakpoints != NULL);
262
263 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200264 assert(proc->leader == proc);
265
Juan Cespedesce377d52008-12-16 19:38:10 +0100266 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100267 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
268 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100269 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270 proc_remove_library(proc, lib);
271 library_destroy(lib);
272 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200273 return -1;
274 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275 proc_add_library(proc, lib);
276 fprintf(stderr, "note: symbols in %s were not filtered.\n",
277 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200278
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279 struct breakpoint *entry_bp
Petr Machata9df15012012-02-20 12:49:46 +0100280 = insert_breakpoint(proc, lib->entry, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100281 if (entry_bp == NULL) {
282 error(0, errno, "couldn't insert entry breakpoint");
283 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100284 }
Petr Machatac7585b62011-07-08 22:58:12 +0200285
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
287 static struct bp_callbacks entry_callbacks = {
288 .on_hit = entry_callback_hit,
289 };
290 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100291 }
292
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100293 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200294 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100295}