blob: f0f8db4e25e2a717cfe4e21722c4e84451f3248d [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 Machataa9fd8f42012-02-07 13:25:56 +010018void
19breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
20{
21 assert(bp != NULL);
22 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
23 (bp->cbs->on_hit) (bp, proc);
24}
25
26void
27breakpoint_on_destroy(struct breakpoint *bp)
28{
29 assert(bp != NULL);
30 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
31 (bp->cbs->on_destroy) (bp);
32}
33
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020034/*****************************************************************************/
35
Petr Machata9294d822012-02-07 12:35:58 +010036struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010037address2bpstruct(Process *proc, void *addr)
38{
Petr Machata26627682011-07-08 18:15:32 +020039 assert(proc != NULL);
40 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020041 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020042 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010043 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020044}
45
Petr Machata2b46cfc2012-02-18 11:17:29 +010046#ifdef ARCH_HAVE_BREAKPOINT_DATA
47int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp);
48#else
49int
50arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
51{
52 return 0;
53}
54#endif
55
56int
57breakpoint_init(struct breakpoint *bp, struct Process *proc,
58 target_address_t addr, struct library_symbol *libsym,
59 struct bp_callbacks *cbs)
60{
61 bp->cbs = cbs;
62 bp->addr = addr;
63 memset(bp->orig_value, 0, sizeof(bp->orig_value));
64 bp->enabled = 0;
65 bp->libsym = libsym;
66 return arch_breakpoint_init(proc, bp);
67}
68
Petr Machata9294d822012-02-07 12:35:58 +010069struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020070insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010071 struct library_symbol *libsym, int enable)
72{
Petr Machata9a5420c2011-07-09 11:21:23 +020073 Process * leader = proc->leader;
74
75 /* Only the group leader should be getting the breakpoints and
76 * thus have ->breakpoint initialized. */
77 assert(leader != NULL);
78 assert(leader->breakpoints != NULL);
79
Juan Cespedescd8976d2009-05-14 13:47:58 +020080 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 +010081 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020082
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 if (!addr)
Petr Machata9294d822012-02-07 12:35:58 +010084 return NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010085
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010087 libsym->needs_init = 0;
88
Petr Machata2b46cfc2012-02-18 11:17:29 +010089 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010090 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 sbp = malloc(sizeof(*sbp));
92 if (sbp == NULL
93 || breakpoint_init(sbp, proc, addr, libsym, NULL) < 0
94 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
95 free(sbp);
96 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +010097 }
Juan Cespedescac15c32003-01-31 18:58:58 +010098 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010099
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200100 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +0200101 if (sbp->enabled == 1 && enable) {
102 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200103 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200104 }
Petr Machata9294d822012-02-07 12:35:58 +0100105
106 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200107}
108
Juan Cespedesf1350522008-12-16 18:19:58 +0100109void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100110delete_breakpoint(Process *proc, void *addr)
111{
Petr Machata9294d822012-02-07 12:35:58 +0100112 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200113
114 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
115
Petr Machata9a5420c2011-07-09 11:21:23 +0200116 Process * leader = proc->leader;
117 assert(leader != NULL);
118
119 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200121 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 if (sbp == NULL)
123 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200124
125 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200127 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200128 assert(sbp->enabled >= 0);
129}
130
Juan Cespedesf1350522008-12-16 18:19:58 +0100131static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100132enable_bp_cb(void *addr, void *sbp, void *proc)
133{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200134 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100135 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200136 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139void
Petr Machatabc373262012-02-07 23:31:15 +0100140enable_all_breakpoints(Process *proc)
141{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200142 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100143
144 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
145 if (proc->breakpoints) {
146 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
147 proc);
148 }
149#ifdef __mips__
150 {
151 /*
152 * I'm sure there is a nicer way to do this. We need to
153 * insert breakpoints _after_ the child has been started.
154 */
155 struct library_symbol *sym;
156 struct library_symbol *new_sym;
157 sym=proc->list_of_symbols;
158 while(sym){
159 void *addr= sym2addr(proc,sym);
160 if(!addr){
161 sym=sym->next;
162 continue;
163 }
164 if(dict_find_entry(proc->breakpoints,addr)){
165 sym=sym->next;
166 continue;
167 }
168 debug(2,"inserting bp %p %s",addr,sym->name);
169 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
170 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
171 new_sym->next=proc->list_of_symbols;
172 proc->list_of_symbols=new_sym;
173 insert_breakpoint(proc, addr, new_sym);
174 sym=sym->next;
175 }
176 }
177#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100178}
179
Juan Cespedesf1350522008-12-16 18:19:58 +0100180static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100181disable_bp_cb(void *addr, void *sbp, void *proc)
182{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200183 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100184 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200185 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200186}
187
Juan Cespedesf1350522008-12-16 18:19:58 +0100188void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200189disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200190 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200191 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100192 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100193}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100194
Petr Machata2b46cfc2012-02-18 11:17:29 +0100195static enum callback_status
196reinitialize_breakpoints(struct Process *proc, struct library *library,
197 void *data)
198{
199 debug(DEBUG_FUNCTION, "reinitialize_breakpoints_in(pid=%d, %s)",
200 proc->pid, library->name);
201
202 struct library_symbol *sym;
203 for (sym = library->symbols; sym != NULL; sym = sym->next)
204 if (sym->needs_init) {
205 target_address_t addr = sym2addr(proc, sym);
206 if (insert_breakpoint(proc, addr, sym, 1) == NULL
207 || (sym->needs_init && !sym->is_weak))
208 fprintf(stderr,
209 "could not re-initialize breakpoint "
210 "for \"%s\" in file \"%s\"\n",
211 sym->name, proc->filename);
212 }
213
214 return CBS_CONT;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100215}
216
Petr Machata02648a12012-02-07 13:44:54 +0100217static void
218entry_callback_hit(struct breakpoint *bp, struct Process *proc)
219{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100220 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100221 if (proc == NULL || proc->leader == NULL)
222 return;
223 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata2b46cfc2012-02-18 11:17:29 +0100224
225 linkmap_init(proc);
226 proc_each_library(proc->leader, NULL, reinitialize_breakpoints, NULL);
Petr Machata02648a12012-02-07 13:44:54 +0100227}
228
Petr Machata1974dbc2011-08-19 18:58:01 +0200229int
Petr Machatac7585b62011-07-08 22:58:12 +0200230breakpoints_init(Process *proc, int enable)
231{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200233 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200234
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235 /* XXX breakpoint dictionary should be initialized
236 * outside. Here we just put in breakpoints. */
237 assert(proc->breakpoints != NULL);
238
239 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200240 assert(proc->leader == proc);
241
Juan Cespedesce377d52008-12-16 19:38:10 +0100242 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
244 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100245 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 proc_remove_library(proc, lib);
247 library_destroy(lib);
248 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200249 return -1;
250 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 proc_add_library(proc, lib);
252 fprintf(stderr, "note: symbols in %s were not filtered.\n",
253 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200254
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 struct breakpoint *entry_bp
256 = insert_breakpoint(proc, lib->entry, NULL, 1);
257 if (entry_bp == NULL) {
258 error(0, errno, "couldn't insert entry breakpoint");
259 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100260 }
Petr Machatac7585b62011-07-08 22:58:12 +0200261
Petr Machata2b46cfc2012-02-18 11:17:29 +0100262 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
263 static struct bp_callbacks entry_callbacks = {
264 .on_hit = entry_callback_hit,
265 };
266 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100267 }
268
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100269 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200270 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100271}