blob: f522598212e4c273ad2111b3710493aa3651898c [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)
Petr Machata55ac9322012-03-27 03:07:35 +020023 (bp->cbs->on_hit)(bp, proc);
24}
25
26void
27breakpoint_on_continue(struct breakpoint *bp, struct Process *proc)
28{
29 assert(bp != NULL);
30 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
31 (bp->cbs->on_continue)(bp, proc);
32 else
33 continue_after_breakpoint(proc, bp);
Petr Machataa9fd8f42012-02-07 13:25:56 +010034}
35
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036/*****************************************************************************/
37
Petr Machata9294d822012-02-07 12:35:58 +010038struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010039address2bpstruct(Process *proc, void *addr)
40{
Petr Machata26627682011-07-08 18:15:32 +020041 assert(proc != NULL);
42 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020043 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020044 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010045 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020046}
47
Petr Machata8cce1192012-03-25 01:37:19 +010048#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +010049int
50arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
51{
52 return 0;
53}
Petr Machata8cce1192012-03-25 01:37:19 +010054
55void
56arch_breakpoint_destroy(struct breakpoint *sbp)
57{
58}
Petr Machata2b46cfc2012-02-18 11:17:29 +010059#endif
60
61int
62breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020063 target_address_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +010064{
Petr Machata55ac9322012-03-27 03:07:35 +020065 bp->cbs = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +010066 bp->addr = addr;
67 memset(bp->orig_value, 0, sizeof(bp->orig_value));
68 bp->enabled = 0;
69 bp->libsym = libsym;
70 return arch_breakpoint_init(proc, bp);
71}
72
Petr Machata8cce1192012-03-25 01:37:19 +010073void
Petr Machata55ac9322012-03-27 03:07:35 +020074breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
75{
76 if (bp->cbs != NULL)
77 assert(bp->cbs == NULL);
78 bp->cbs = cbs;
79}
80
81void
Petr Machata8cce1192012-03-25 01:37:19 +010082breakpoint_destroy(struct breakpoint *bp)
83{
84 if (bp == NULL)
85 return;
86
87 /* XXX I'm not convinced that we need on_destroy. We already
88 * have arch_breakpoint_destroy, which is necessary as a
89 * counterpart of arch_breakpoint_init in any case. */
90 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
91 (bp->cbs->on_destroy) (bp);
92
93 arch_breakpoint_destroy(bp);
94}
95
Petr Machata9294d822012-02-07 12:35:58 +010096struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020097insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010098 struct library_symbol *libsym, int enable)
99{
Petr Machata9a5420c2011-07-09 11:21:23 +0200100 Process * leader = proc->leader;
101
102 /* Only the group leader should be getting the breakpoints and
103 * thus have ->breakpoint initialized. */
104 assert(leader != NULL);
105 assert(leader->breakpoints != NULL);
106
Juan Cespedescd8976d2009-05-14 13:47:58 +0200107 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 +0100108 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200109
Petr Machata81c65272012-03-21 04:57:25 +0100110 if (addr == 0) {
111 /* XXX we need a better way to deal with this. For
112 * now, just abuse errno to carry the error
113 * information. */
114 errno = EINVAL;
Petr Machata9294d822012-02-07 12:35:58 +0100115 return NULL;
Petr Machata81c65272012-03-21 04:57:25 +0100116 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100117
Petr Machata2b46cfc2012-02-18 11:17:29 +0100118 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100119 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120 sbp = malloc(sizeof(*sbp));
121 if (sbp == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200122 || breakpoint_init(sbp, proc, addr, libsym) < 0
Petr Machata2b46cfc2012-02-18 11:17:29 +0100123 || dict_enter(leader->breakpoints, addr, sbp) < 0) {
124 free(sbp);
125 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100126 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100127 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100128
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200129 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +0200130 if (sbp->enabled == 1 && enable) {
131 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +0200132 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +0200133 }
Petr Machata9294d822012-02-07 12:35:58 +0100134
135 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200136}
137
Juan Cespedesf1350522008-12-16 18:19:58 +0100138void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100139delete_breakpoint(Process *proc, void *addr)
140{
Petr Machata9294d822012-02-07 12:35:58 +0100141 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200142
143 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
144
Petr Machata9a5420c2011-07-09 11:21:23 +0200145 Process * leader = proc->leader;
146 assert(leader != NULL);
147
148 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100149 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200150 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 if (sbp == NULL)
152 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200153
154 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100155 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200156 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200157 assert(sbp->enabled >= 0);
158}
159
Petr Machatae9aebd62012-03-25 01:38:53 +0100160const char *
161breakpoint_name(const struct breakpoint *bp)
162{
163 assert(bp != NULL);
164 return bp->libsym != NULL ? bp->libsym->name : NULL;
165}
166
Juan Cespedesf1350522008-12-16 18:19:58 +0100167static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100168enable_bp_cb(void *addr, void *sbp, void *proc)
169{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200170 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100171 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200172 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200173}
174
Juan Cespedesf1350522008-12-16 18:19:58 +0100175void
Petr Machatabc373262012-02-07 23:31:15 +0100176enable_all_breakpoints(Process *proc)
177{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200178 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100179
180 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
181 if (proc->breakpoints) {
182 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
183 proc);
184 }
185#ifdef __mips__
186 {
187 /*
188 * I'm sure there is a nicer way to do this. We need to
189 * insert breakpoints _after_ the child has been started.
190 */
191 struct library_symbol *sym;
192 struct library_symbol *new_sym;
193 sym=proc->list_of_symbols;
194 while(sym){
195 void *addr= sym2addr(proc,sym);
196 if(!addr){
197 sym=sym->next;
198 continue;
199 }
200 if(dict_find_entry(proc->breakpoints,addr)){
201 sym=sym->next;
202 continue;
203 }
204 debug(2,"inserting bp %p %s",addr,sym->name);
205 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
206 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
207 new_sym->next=proc->list_of_symbols;
208 proc->list_of_symbols=new_sym;
209 insert_breakpoint(proc, addr, new_sym);
210 sym=sym->next;
211 }
212 }
213#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100214}
215
Juan Cespedesf1350522008-12-16 18:19:58 +0100216static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100217disable_bp_cb(void *addr, void *sbp, void *proc)
218{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200219 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100220 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200221 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200222}
223
Juan Cespedesf1350522008-12-16 18:19:58 +0100224void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200225disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200226 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200227 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100228 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100229}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100230
Petr Machata02648a12012-02-07 13:44:54 +0100231static void
232entry_callback_hit(struct breakpoint *bp, struct Process *proc)
233{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234 fprintf(stderr, "entry_callback_hit\n");
Petr Machata02648a12012-02-07 13:44:54 +0100235 if (proc == NULL || proc->leader == NULL)
236 return;
237 delete_breakpoint(proc, bp->addr); // xxx
Petr Machata4e2073f2012-03-21 05:15:44 +0100238 enable_all_breakpoints(proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100239
240 linkmap_init(proc);
Petr Machata02648a12012-02-07 13:44:54 +0100241}
242
Petr Machata1974dbc2011-08-19 18:58:01 +0200243int
Petr Machatac7585b62011-07-08 22:58:12 +0200244breakpoints_init(Process *proc, int enable)
245{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 fprintf(stderr, "breakpoints_init %d enable=%d\n", proc->pid, enable);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200247 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200248
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 /* XXX breakpoint dictionary should be initialized
250 * outside. Here we just put in breakpoints. */
251 assert(proc->breakpoints != NULL);
252
253 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200254 assert(proc->leader == proc);
255
Juan Cespedesce377d52008-12-16 19:38:10 +0100256 if (options.libcalls && proc->filename) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
258 switch (lib != NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100259 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260 proc_remove_library(proc, lib);
261 library_destroy(lib);
262 case 0:
Petr Machata1974dbc2011-08-19 18:58:01 +0200263 return -1;
264 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265 proc_add_library(proc, lib);
266 fprintf(stderr, "note: symbols in %s were not filtered.\n",
267 lib->name);
Petr Machata1974dbc2011-08-19 18:58:01 +0200268
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269 struct breakpoint *entry_bp
270 = insert_breakpoint(proc, lib->entry, NULL, 1);
271 if (entry_bp == NULL) {
272 error(0, errno, "couldn't insert entry breakpoint");
273 goto fail;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100274 }
Petr Machatac7585b62011-07-08 22:58:12 +0200275
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276 fprintf(stderr, "setting entry_callbacks by hand, fix it\n");
277 static struct bp_callbacks entry_callbacks = {
278 .on_hit = entry_callback_hit,
279 };
280 entry_bp->cbs = &entry_callbacks;
Petr Machata02648a12012-02-07 13:44:54 +0100281 }
282
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100283 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200284 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100285}