blob: 08e15ccbdc06c7f5a8adec76ffa2bd907cb09af6 [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
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020026/*****************************************************************************/
27
Petr Machata9294d822012-02-07 12:35:58 +010028struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010029address2bpstruct(Process *proc, void *addr)
30{
Petr Machata26627682011-07-08 18:15:32 +020031 assert(proc != NULL);
32 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020033 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020034 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010035 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036}
37
Petr Machata8cce1192012-03-25 01:37:19 +010038#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +010039int
40arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
41{
42 return 0;
43}
Petr Machata8cce1192012-03-25 01:37:19 +010044
45void
46arch_breakpoint_destroy(struct breakpoint *sbp)
47{
48}
Petr Machata2b46cfc2012-02-18 11:17:29 +010049#endif
50
51int
52breakpoint_init(struct breakpoint *bp, struct Process *proc,
53 target_address_t addr, struct library_symbol *libsym,
54 struct bp_callbacks *cbs)
55{
56 bp->cbs = cbs;
57 bp->addr = addr;
58 memset(bp->orig_value, 0, sizeof(bp->orig_value));
59 bp->enabled = 0;
60 bp->libsym = libsym;
61 return arch_breakpoint_init(proc, bp);
62}
63
Petr Machata8cce1192012-03-25 01:37:19 +010064void
65breakpoint_destroy(struct breakpoint *bp)
66{
67 if (bp == NULL)
68 return;
69
70 /* XXX I'm not convinced that we need on_destroy. We already
71 * have arch_breakpoint_destroy, which is necessary as a
72 * counterpart of arch_breakpoint_init in any case. */
73 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
74 (bp->cbs->on_destroy) (bp);
75
76 arch_breakpoint_destroy(bp);
77}
78
Petr Machata9294d822012-02-07 12:35:58 +010079struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020080insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010081 struct library_symbol *libsym, int enable)
82{
Petr Machata9a5420c2011-07-09 11:21:23 +020083 Process * leader = proc->leader;
84
85 /* Only the group leader should be getting the breakpoints and
86 * thus have ->breakpoint initialized. */
87 assert(leader != NULL);
88 assert(leader->breakpoints != NULL);
89
Juan Cespedescd8976d2009-05-14 13:47:58 +020090 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 +010091 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020092
Petr Machata81c65272012-03-21 04:57:25 +010093 if (addr == 0) {
94 /* XXX we need a better way to deal with this. For
95 * now, just abuse errno to carry the error
96 * information. */
97 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +010098 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +010099 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100100
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100102 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100103 sbp = malloc(sizeof(*sbp));
104 if (sbp == NULL
105 || breakpoint_init(sbp, proc, addr, libsym, NULL) < 0
106 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
107 free(sbp);
108 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100109 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100110 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200112 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +0200113 if (sbp->enabled == 1 && enable) {
114 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200115 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200116 }
Petr Machata9294d822012-02-07 12:35:58 +0100117
118 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200119}
120
Juan Cespedesf1350522008-12-16 18:19:58 +0100121void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100122delete_breakpoint(Process *proc, void *addr)
123{
Petr Machata9294d822012-02-07 12:35:58 +0100124 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200125
126 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
127
Petr Machata9a5420c2011-07-09 11:21:23 +0200128 Process * leader = proc->leader;
129 assert(leader != NULL);
130
131 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100132 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200133 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134 if (sbp == NULL)
135 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200136
137 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200139 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200140 assert(sbp->enabled >= 0);
141}
142
Petr Machatae9aebd62012-03-25 01:38:53 +0100143const char *
144breakpoint_name(const struct breakpoint *bp)
145{
146 assert(bp != NULL);
147 return bp->libsym != NULL ? bp->libsym->name : NULL;
148}
149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100151enable_bp_cb(void *addr, void *sbp, void *proc)
152{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200153 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100154 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200155 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200156}
157
Juan Cespedesf1350522008-12-16 18:19:58 +0100158void
Petr Machatabc373262012-02-07 23:31:15 +0100159enable_all_breakpoints(Process *proc)
160{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200161 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100162
163 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
164 if (proc->breakpoints) {
165 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
166 proc);
167 }
168#ifdef __mips__
169 {
170 /*
171 * I'm sure there is a nicer way to do this. We need to
172 * insert breakpoints _after_ the child has been started.
173 */
174 struct library_symbol *sym;
175 struct library_symbol *new_sym;
176 sym=proc->list_of_symbols;
177 while(sym){
178 void *addr= sym2addr(proc,sym);
179 if(!addr){
180 sym=sym->next;
181 continue;
182 }
183 if(dict_find_entry(proc->breakpoints,addr)){
184 sym=sym->next;
185 continue;
186 }
187 debug(2,"inserting bp %p %s",addr,sym->name);
188 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
189 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
190 new_sym->next=proc->list_of_symbols;
191 proc->list_of_symbols=new_sym;
192 insert_breakpoint(proc, addr, new_sym);
193 sym=sym->next;
194 }
195 }
196#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197}
198
Juan Cespedesf1350522008-12-16 18:19:58 +0100199static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100200disable_bp_cb(void *addr, void *sbp, void *proc)
201{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200202 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100203 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200204 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200205}
206
Juan Cespedesf1350522008-12-16 18:19:58 +0100207void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200208disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200209 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200210 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100211 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100212}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100213
Petr Machata02648a12012-02-07 13:44:54 +0100214static void
215entry_callback_hit(struct breakpoint *bp, struct Process *proc)
216{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100217 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100218 if (proc == NULL || proc->leader == NULL)
219 return;
220 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata4e2073f2012-03-21 05:15:44 +0100221 enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222
223 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100224}
225
Petr Machata1974dbc2011-08-19 18:58:01 +0200226int
Petr Machatac7585b62011-07-08 22:58:12 +0200227breakpoints_init(Process *proc, int enable)
228{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200230 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200231
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232 /* XXX breakpoint dictionary should be initialized
233 * outside. Here we just put in breakpoints. */
234 assert(proc->breakpoints != NULL);
235
236 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200237 assert(proc->leader == proc);
238
Juan Cespedesce377d52008-12-16 19:38:10 +0100239 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
241 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100242 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 proc_remove_library(proc, lib);
244 library_destroy(lib);
245 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200246 return -1;
247 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248 proc_add_library(proc, lib);
249 fprintf(stderr, "note: symbols in %s were not filtered.\n",
250 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252 struct breakpoint *entry_bp
253 = insert_breakpoint(proc, lib->entry, NULL, 1);
254 if (entry_bp == NULL) {
255 error(0, errno, "couldn't insert entry breakpoint");
256 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100257 }
Petr Machatac7585b62011-07-08 22:58:12 +0200258
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
260 static struct bp_callbacks entry_callbacks = {
261 .on_hit = entry_callback_hit,
262 };
263 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100264 }
265
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100266 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200267 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100268}