blob: a72dcd7873508b886ab076aed8dd616d0accca1b [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
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010092 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +010093 sbp = malloc(sizeof(*sbp));
94 if (sbp == NULL
95 || breakpoint_init(sbp, proc, addr, libsym, NULL) < 0
96 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
97 free(sbp);
98 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +010099 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100100 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200102 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +0200103 if (sbp->enabled == 1 && enable) {
104 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200105 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200106 }
Petr Machata9294d822012-02-07 12:35:58 +0100107
108 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200109}
110
Juan Cespedesf1350522008-12-16 18:19:58 +0100111void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100112delete_breakpoint(Process *proc, void *addr)
113{
Petr Machata9294d822012-02-07 12:35:58 +0100114 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200115
116 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
117
Petr Machata9a5420c2011-07-09 11:21:23 +0200118 Process * leader = proc->leader;
119 assert(leader != NULL);
120
121 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200123 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 if (sbp == NULL)
125 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200126
127 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200129 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200130 assert(sbp->enabled >= 0);
131}
132
Juan Cespedesf1350522008-12-16 18:19:58 +0100133static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100134enable_bp_cb(void *addr, void *sbp, void *proc)
135{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200136 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100137 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200138 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200139}
140
Juan Cespedesf1350522008-12-16 18:19:58 +0100141void
Petr Machatabc373262012-02-07 23:31:15 +0100142enable_all_breakpoints(Process *proc)
143{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200144 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100145
146 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
147 if (proc->breakpoints) {
148 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
149 proc);
150 }
151#ifdef __mips__
152 {
153 /*
154 * I'm sure there is a nicer way to do this. We need to
155 * insert breakpoints _after_ the child has been started.
156 */
157 struct library_symbol *sym;
158 struct library_symbol *new_sym;
159 sym=proc->list_of_symbols;
160 while(sym){
161 void *addr= sym2addr(proc,sym);
162 if(!addr){
163 sym=sym->next;
164 continue;
165 }
166 if(dict_find_entry(proc->breakpoints,addr)){
167 sym=sym->next;
168 continue;
169 }
170 debug(2,"inserting bp %p %s",addr,sym->name);
171 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
172 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
173 new_sym->next=proc->list_of_symbols;
174 proc->list_of_symbols=new_sym;
175 insert_breakpoint(proc, addr, new_sym);
176 sym=sym->next;
177 }
178 }
179#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180}
181
Juan Cespedesf1350522008-12-16 18:19:58 +0100182static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100183disable_bp_cb(void *addr, void *sbp, void *proc)
184{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200185 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100186 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200187 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200188}
189
Juan Cespedesf1350522008-12-16 18:19:58 +0100190void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200191disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200192 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200193 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100194 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100196
Petr Machata02648a12012-02-07 13:44:54 +0100197static void
198entry_callback_hit(struct breakpoint *bp, struct Process *proc)
199{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100200 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100201 if (proc == NULL || proc->leader == NULL)
202 return;
203 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata4e2073f2012-03-21 05:15:44 +0100204 enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100205
206 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100207}
208
Petr Machata1974dbc2011-08-19 18:58:01 +0200209int
Petr Machatac7585b62011-07-08 22:58:12 +0200210breakpoints_init(Process *proc, int enable)
211{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100212 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200213 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200214
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215 /* XXX breakpoint dictionary should be initialized
216 * outside. Here we just put in breakpoints. */
217 assert(proc->breakpoints != NULL);
218
219 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200220 assert(proc->leader == proc);
221
Juan Cespedesce377d52008-12-16 19:38:10 +0100222 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
224 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100225 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 proc_remove_library(proc, lib);
227 library_destroy(lib);
228 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200229 return -1;
230 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231 proc_add_library(proc, lib);
232 fprintf(stderr, "note: symbols in %s were not filtered.\n",
233 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200234
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235 struct breakpoint *entry_bp
236 = insert_breakpoint(proc, lib->entry, NULL, 1);
237 if (entry_bp == NULL) {
238 error(0, errno, "couldn't insert entry breakpoint");
239 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100240 }
Petr Machatac7585b62011-07-08 22:58:12 +0200241
Petr Machata2b46cfc2012-02-18 11:17:29 +0100242 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
243 static struct bp_callbacks entry_callbacks = {
244 .on_hit = entry_callback_hit,
245 };
246 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100247 }
248
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100249 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200250 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100251}