blob: 048c4e373dfe6e4ff4cab9120c4aa87c64c9e33c [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
Juan Cespedesf1350522008-12-16 18:19:58 +0100143static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100144enable_bp_cb(void *addr, void *sbp, void *proc)
145{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200146 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100147 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200148 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200149}
150
Juan Cespedesf1350522008-12-16 18:19:58 +0100151void
Petr Machatabc373262012-02-07 23:31:15 +0100152enable_all_breakpoints(Process *proc)
153{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200154 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100155
156 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
157 if (proc->breakpoints) {
158 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
159 proc);
160 }
161#ifdef __mips__
162 {
163 /*
164 * I'm sure there is a nicer way to do this. We need to
165 * insert breakpoints _after_ the child has been started.
166 */
167 struct library_symbol *sym;
168 struct library_symbol *new_sym;
169 sym=proc->list_of_symbols;
170 while(sym){
171 void *addr= sym2addr(proc,sym);
172 if(!addr){
173 sym=sym->next;
174 continue;
175 }
176 if(dict_find_entry(proc->breakpoints,addr)){
177 sym=sym->next;
178 continue;
179 }
180 debug(2,"inserting bp %p %s",addr,sym->name);
181 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
182 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
183 new_sym->next=proc->list_of_symbols;
184 proc->list_of_symbols=new_sym;
185 insert_breakpoint(proc, addr, new_sym);
186 sym=sym->next;
187 }
188 }
189#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190}
191
Juan Cespedesf1350522008-12-16 18:19:58 +0100192static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100193disable_bp_cb(void *addr, void *sbp, void *proc)
194{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200195 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100196 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200197 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200198}
199
Juan Cespedesf1350522008-12-16 18:19:58 +0100200void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200201disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200202 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200203 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100204 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100206
Petr Machata02648a12012-02-07 13:44:54 +0100207static void
208entry_callback_hit(struct breakpoint *bp, struct Process *proc)
209{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100210 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100211 if (proc == NULL || proc->leader == NULL)
212 return;
213 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata4e2073f2012-03-21 05:15:44 +0100214 enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215
216 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100217}
218
Petr Machata1974dbc2011-08-19 18:58:01 +0200219int
Petr Machatac7585b62011-07-08 22:58:12 +0200220breakpoints_init(Process *proc, int enable)
221{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200223 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200224
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 /* XXX breakpoint dictionary should be initialized
226 * outside. Here we just put in breakpoints. */
227 assert(proc->breakpoints != NULL);
228
229 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200230 assert(proc->leader == proc);
231
Juan Cespedesce377d52008-12-16 19:38:10 +0100232 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100233 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
234 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100235 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236 proc_remove_library(proc, lib);
237 library_destroy(lib);
238 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200239 return -1;
240 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241 proc_add_library(proc, lib);
242 fprintf(stderr, "note: symbols in %s were not filtered.\n",
243 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200244
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245 struct breakpoint *entry_bp
246 = insert_breakpoint(proc, lib->entry, NULL, 1);
247 if (entry_bp == NULL) {
248 error(0, errno, "couldn't insert entry breakpoint");
249 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100250 }
Petr Machatac7585b62011-07-08 22:58:12 +0200251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
253 static struct bp_callbacks entry_callbacks = {
254 .on_hit = entry_callback_hit,
255 };
256 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100257 }
258
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100259 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200260 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100261}