blob: 4844822f7e822f28145b85ef6bba0b3ae58eb067 [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
Petr Machata81c65272012-03-21 04:57:25 +010083 if (addr == 0) {
84 /* XXX we need a better way to deal with this. For
85 * now, just abuse errno to carry the error
86 * information. */
87 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +010088 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +010089 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010090
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010092 libsym->needs_init = 0;
93
Petr Machata2b46cfc2012-02-18 11:17:29 +010094 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010095 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +010096 sbp = malloc(sizeof(*sbp));
97 if (sbp == NULL
98 || breakpoint_init(sbp, proc, addr, libsym, NULL) < 0
99 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
100 free(sbp);
101 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100102 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100103 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100104
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200105 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +0200106 if (sbp->enabled == 1 && enable) {
107 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200108 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200109 }
Petr Machata9294d822012-02-07 12:35:58 +0100110
111 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200112}
113
Juan Cespedesf1350522008-12-16 18:19:58 +0100114void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100115delete_breakpoint(Process *proc, void *addr)
116{
Petr Machata9294d822012-02-07 12:35:58 +0100117 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200118
119 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
120
Petr Machata9a5420c2011-07-09 11:21:23 +0200121 Process * leader = proc->leader;
122 assert(leader != NULL);
123
124 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100125 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200126 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 if (sbp == NULL)
128 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200129
130 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200132 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200133 assert(sbp->enabled >= 0);
134}
135
Juan Cespedesf1350522008-12-16 18:19:58 +0100136static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100137enable_bp_cb(void *addr, void *sbp, void *proc)
138{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200139 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100140 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200141 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200142}
143
Juan Cespedesf1350522008-12-16 18:19:58 +0100144void
Petr Machatabc373262012-02-07 23:31:15 +0100145enable_all_breakpoints(Process *proc)
146{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200147 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100148
149 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
150 if (proc->breakpoints) {
151 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
152 proc);
153 }
154#ifdef __mips__
155 {
156 /*
157 * I'm sure there is a nicer way to do this. We need to
158 * insert breakpoints _after_ the child has been started.
159 */
160 struct library_symbol *sym;
161 struct library_symbol *new_sym;
162 sym=proc->list_of_symbols;
163 while(sym){
164 void *addr= sym2addr(proc,sym);
165 if(!addr){
166 sym=sym->next;
167 continue;
168 }
169 if(dict_find_entry(proc->breakpoints,addr)){
170 sym=sym->next;
171 continue;
172 }
173 debug(2,"inserting bp %p %s",addr,sym->name);
174 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
175 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
176 new_sym->next=proc->list_of_symbols;
177 proc->list_of_symbols=new_sym;
178 insert_breakpoint(proc, addr, new_sym);
179 sym=sym->next;
180 }
181 }
182#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183}
184
Juan Cespedesf1350522008-12-16 18:19:58 +0100185static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100186disable_bp_cb(void *addr, void *sbp, void *proc)
187{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200188 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100189 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200190 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200191}
192
Juan Cespedesf1350522008-12-16 18:19:58 +0100193void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200194disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200195 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200196 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100197 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100198}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100199
Petr Machata02648a12012-02-07 13:44:54 +0100200static void
201entry_callback_hit(struct breakpoint *bp, struct Process *proc)
202{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100203 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100204 if (proc == NULL || proc->leader == NULL)
205 return;
206 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata4e2073f2012-03-21 05:15:44 +0100207 enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208
209 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100210}
211
Petr Machata1974dbc2011-08-19 18:58:01 +0200212int
Petr Machatac7585b62011-07-08 22:58:12 +0200213breakpoints_init(Process *proc, int enable)
214{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200216 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200217
Petr Machata2b46cfc2012-02-18 11:17:29 +0100218 /* XXX breakpoint dictionary should be initialized
219 * outside. Here we just put in breakpoints. */
220 assert(proc->breakpoints != NULL);
221
222 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200223 assert(proc->leader == proc);
224
Juan Cespedesce377d52008-12-16 19:38:10 +0100225 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
227 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100228 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 proc_remove_library(proc, lib);
230 library_destroy(lib);
231 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200232 return -1;
233 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234 proc_add_library(proc, lib);
235 fprintf(stderr, "note: symbols in %s were not filtered.\n",
236 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200237
Petr Machata2b46cfc2012-02-18 11:17:29 +0100238 struct breakpoint *entry_bp
239 = insert_breakpoint(proc, lib->entry, NULL, 1);
240 if (entry_bp == NULL) {
241 error(0, errno, "couldn't insert entry breakpoint");
242 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100243 }
Petr Machatac7585b62011-07-08 22:58:12 +0200244
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
246 static struct bp_callbacks entry_callbacks = {
247 .on_hit = entry_callback_hit,
248 };
249 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100250 }
251
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100252 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200253 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100254}