blob: 5a473a9d4a8954d849af31cab1f91e981a8b5330 [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>
6
Juan Cespedesf1bfe202002-03-27 00:22:23 +01007#ifdef __powerpc__
8#include <sys/ptrace.h>
9#endif
10
Petr Machata9294d822012-02-07 12:35:58 +010011#include "breakpoint.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010013#include "proc.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020014
Petr Machataa9fd8f42012-02-07 13:25:56 +010015void
16breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
17{
18 assert(bp != NULL);
19 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
20 (bp->cbs->on_hit) (bp, proc);
21}
22
23void
24breakpoint_on_destroy(struct breakpoint *bp)
25{
26 assert(bp != NULL);
27 if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
28 (bp->cbs->on_destroy) (bp);
29}
30
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020031/*****************************************************************************/
32
Petr Machata9294d822012-02-07 12:35:58 +010033struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010034address2bpstruct(Process *proc, void *addr)
35{
Petr Machata26627682011-07-08 18:15:32 +020036 assert(proc != NULL);
37 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020038 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020039 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010040 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020041}
42
Petr Machata9294d822012-02-07 12:35:58 +010043struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020044insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010045 struct library_symbol *libsym, int enable)
46{
Petr Machata9294d822012-02-07 12:35:58 +010047 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020048
Petr Machata9a5420c2011-07-09 11:21:23 +020049 Process * leader = proc->leader;
50
51 /* Only the group leader should be getting the breakpoints and
52 * thus have ->breakpoint initialized. */
53 assert(leader != NULL);
54 assert(leader->breakpoints != NULL);
55
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070056#ifdef __arm__
57 int thumb_mode = (int)addr & 1;
58 if (thumb_mode)
59 addr = (void *)((int)addr & ~1);
60#endif
61
Juan Cespedescd8976d2009-05-14 13:47:58 +020062 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 +010063 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 if (!addr)
Petr Machata9294d822012-02-07 12:35:58 +010066 return NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010067
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010069 libsym->needs_init = 0;
70
Petr Machata9a5420c2011-07-09 11:21:23 +020071 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010072 if (sbp == NULL) {
73 sbp = calloc(1, sizeof(*sbp));
74 if (sbp == NULL) {
Petr Machata9294d822012-02-07 12:35:58 +010075 return NULL; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010076 }
Petr Machata9a5420c2011-07-09 11:21:23 +020077 dict_enter(leader->breakpoints, addr, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010078 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010080 }
Juan Cespedes63184be2008-12-10 13:30:12 +010081#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070082 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010083 proc->thumb_mode = 0;
84#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020085 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +020086 if (sbp->enabled == 1 && enable) {
87 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +020088 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +020089 }
Petr Machata9294d822012-02-07 12:35:58 +010090
91 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020092}
93
Juan Cespedesf1350522008-12-16 18:19:58 +010094void
Petr Machatafed1e8d2012-02-07 02:06:29 +010095delete_breakpoint(Process *proc, void *addr)
96{
Petr Machata9294d822012-02-07 12:35:58 +010097 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020098
99 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
100
Petr Machata9a5420c2011-07-09 11:21:23 +0200101 Process * leader = proc->leader;
102 assert(leader != NULL);
103
104 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200106 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 if (sbp == NULL)
108 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200109
110 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +0200112 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200113 assert(sbp->enabled >= 0);
114}
115
Juan Cespedesf1350522008-12-16 18:19:58 +0100116static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100117enable_bp_cb(void *addr, void *sbp, void *proc)
118{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200119 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100120 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200121 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200122}
123
Juan Cespedesf1350522008-12-16 18:19:58 +0100124void
Petr Machatabc373262012-02-07 23:31:15 +0100125enable_all_breakpoints(Process *proc)
126{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200127 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100128
129 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
130 if (proc->breakpoints) {
131 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
132 proc);
133 }
134#ifdef __mips__
135 {
136 /*
137 * I'm sure there is a nicer way to do this. We need to
138 * insert breakpoints _after_ the child has been started.
139 */
140 struct library_symbol *sym;
141 struct library_symbol *new_sym;
142 sym=proc->list_of_symbols;
143 while(sym){
144 void *addr= sym2addr(proc,sym);
145 if(!addr){
146 sym=sym->next;
147 continue;
148 }
149 if(dict_find_entry(proc->breakpoints,addr)){
150 sym=sym->next;
151 continue;
152 }
153 debug(2,"inserting bp %p %s",addr,sym->name);
154 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
155 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
156 new_sym->next=proc->list_of_symbols;
157 proc->list_of_symbols=new_sym;
158 insert_breakpoint(proc, addr, new_sym);
159 sym=sym->next;
160 }
161 }
162#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163}
164
Juan Cespedesf1350522008-12-16 18:19:58 +0100165static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100166disable_bp_cb(void *addr, void *sbp, void *proc)
167{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200168 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100169 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200170 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200171}
172
Juan Cespedesf1350522008-12-16 18:19:58 +0100173void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200174disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200175 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200176 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100177 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100178}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100179
Juan Cespedesf1350522008-12-16 18:19:58 +0100180static void
181free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200182 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100183 assert(sbp);
184 free(sbp);
185}
186
Petr Machata02648a12012-02-07 13:44:54 +0100187static void
188entry_callback_hit(struct breakpoint *bp, struct Process *proc)
189{
190 if (proc == NULL || proc->leader == NULL)
191 return;
192 delete_breakpoint(proc, bp->addr); // xxx
193 reinitialize_breakpoints(proc->leader);
194}
195
Petr Machata1974dbc2011-08-19 18:58:01 +0200196int
Petr Machatac7585b62011-07-08 22:58:12 +0200197breakpoints_init(Process *proc, int enable)
198{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200199 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100201 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
202 dict_clear(proc->breakpoints);
203 proc->breakpoints = NULL;
204 }
Petr Machata26627682011-07-08 18:15:32 +0200205
Petr Machata9a5420c2011-07-09 11:21:23 +0200206 /* Only the thread group leader should hold the breakpoints.
207 * (N.B. PID may be set to 0 temporarily when called by
208 * handle_exec). */
209 assert(proc->leader == proc);
210
Petr Machata26627682011-07-08 18:15:32 +0200211 proc->breakpoints = dict_init(dict_key2hash_int,
212 dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100213
Petr Machata534e00f2011-09-27 17:58:38 +0200214 destroy_library_symbol_chain(proc->list_of_symbols);
Petr Machata3d7e4b82011-07-08 18:15:19 +0200215 proc->list_of_symbols = NULL;
216
Petr Machatae84fa002012-02-07 13:43:03 +0100217 GElf_Addr entry;
Juan Cespedesce377d52008-12-16 19:38:10 +0100218 if (options.libcalls && proc->filename) {
Petr Machatae84fa002012-02-07 13:43:03 +0100219 proc->list_of_symbols = read_elf(proc, &entry);
Petr Machata1974dbc2011-08-19 18:58:01 +0200220 if (proc->list_of_symbols == NULL) {
Petr Machata02648a12012-02-07 13:44:54 +0100221 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200222 /* XXX leak breakpoints */
223 return -1;
224 }
225
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100226 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200227 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 while (*tmp1) {
229 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100230 int keep = !opt_e_enable;
231
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200233 if (!strcmp((*tmp1)->name,
234 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100235 keep = opt_e_enable;
236 }
237 tmp2 = tmp2->next;
238 }
239 if (!keep) {
240 *tmp1 = (*tmp1)->next;
241 } else {
242 tmp1 = &((*tmp1)->next);
243 }
244 }
245 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100246 }
Petr Machatac7585b62011-07-08 22:58:12 +0200247
Petr Machata02648a12012-02-07 13:44:54 +0100248 struct breakpoint *entry_bp
Petr Machata61196a42012-02-07 16:41:03 +0100249 = insert_breakpoint(proc, (void *)(uintptr_t)entry, NULL, 1);
Petr Machata02648a12012-02-07 13:44:54 +0100250 if (entry_bp == NULL) {
251 fprintf(stderr, "fail!\n");
252 goto fail;
253 }
254
255 static struct bp_callbacks entry_callbacks = {
256 .on_hit = entry_callback_hit,
257 };
258 entry_bp->cbs = &entry_callbacks;
Petr Machatac7585b62011-07-08 22:58:12 +0200259
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100260 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200261 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100262}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100263
Juan Cespedesf1350522008-12-16 18:19:58 +0100264void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200265reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200266 struct library_symbol *sym;
267
268 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
269
270 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100271
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 while (sym) {
273 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200274 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 if (sym->needs_init && !sym->is_weak) {
276 fprintf(stderr,
277 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
278 sym->name, proc->filename);
279 exit(1);
280 }
281 }
282 sym = sym->next;
283 }
284}