blob: 6e2f490ea7e9b2b20d72582a4af7789e4320eda8 [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"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020013
14/*****************************************************************************/
15
Petr Machata9294d822012-02-07 12:35:58 +010016struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010017address2bpstruct(Process *proc, void *addr)
18{
Petr Machata26627682011-07-08 18:15:32 +020019 assert(proc != NULL);
20 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020021 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020022 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010023 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020024}
25
Petr Machata9294d822012-02-07 12:35:58 +010026struct breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020027insert_breakpoint(Process *proc, void *addr,
Petr Machatafed1e8d2012-02-07 02:06:29 +010028 struct library_symbol *libsym, int enable)
29{
Petr Machata9294d822012-02-07 12:35:58 +010030 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020031
Petr Machata9a5420c2011-07-09 11:21:23 +020032 Process * leader = proc->leader;
33
34 /* Only the group leader should be getting the breakpoints and
35 * thus have ->breakpoint initialized. */
36 assert(leader != NULL);
37 assert(leader->breakpoints != NULL);
38
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070039#ifdef __arm__
40 int thumb_mode = (int)addr & 1;
41 if (thumb_mode)
42 addr = (void *)((int)addr & ~1);
43#endif
44
Juan Cespedescd8976d2009-05-14 13:47:58 +020045 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 +010046 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020047
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 if (!addr)
Petr Machata9294d822012-02-07 12:35:58 +010049 return NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010050
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010052 libsym->needs_init = 0;
53
Petr Machata9a5420c2011-07-09 11:21:23 +020054 sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +010055 if (sbp == NULL) {
56 sbp = calloc(1, sizeof(*sbp));
57 if (sbp == NULL) {
Petr Machata9294d822012-02-07 12:35:58 +010058 return NULL; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010059 }
Petr Machata9a5420c2011-07-09 11:21:23 +020060 dict_enter(leader->breakpoints, addr, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010061 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010063 }
Juan Cespedes63184be2008-12-10 13:30:12 +010064#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070065 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010066 proc->thumb_mode = 0;
67#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020068 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +020069 if (sbp->enabled == 1 && enable) {
70 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +020071 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +020072 }
Petr Machata9294d822012-02-07 12:35:58 +010073
74 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020075}
76
Juan Cespedesf1350522008-12-16 18:19:58 +010077void
Petr Machatafed1e8d2012-02-07 02:06:29 +010078delete_breakpoint(Process *proc, void *addr)
79{
Petr Machata9294d822012-02-07 12:35:58 +010080 struct breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020081
82 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
83
Petr Machata9a5420c2011-07-09 11:21:23 +020084 Process * leader = proc->leader;
85 assert(leader != NULL);
86
87 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020089 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 if (sbp == NULL)
91 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020092
93 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +020095 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020096 assert(sbp->enabled >= 0);
97}
98
Juan Cespedesf1350522008-12-16 18:19:58 +010099static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100100enable_bp_cb(void *addr, void *sbp, void *proc)
101{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200102 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200103 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200104 enable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100105 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200106}
107
Juan Cespedesf1350522008-12-16 18:19:58 +0100108void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200109enable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200110 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100111 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100112#ifdef __powerpc__
113 unsigned long a;
114
115 /*
116 * PPC HACK! (XXX FIXME TODO)
117 * If the dynamic linker hasn't populated the PLT then
118 * dont enable the breakpoints
119 */
Juan Cespedesce377d52008-12-16 19:38:10 +0100120 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100121 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200122 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +0200124 if (a == 0x0)
125 return;
126 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100127#endif
128
Juan Cespedescac15c32003-01-31 18:58:58 +0100129 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100130 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
132 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100133 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100134#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200135 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200136 /*
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 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200140 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);
Arnaud Patard47950872010-01-08 08:40:15 -0500154 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
155 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200156 new_sym->next=proc->list_of_symbols;
157 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200158 insert_breakpoint(proc, addr, new_sym);
159 sym=sym->next;
160 }
161 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100162#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163 }
164 proc->breakpoints_enabled = 1;
165}
166
Juan Cespedesf1350522008-12-16 18:19:58 +0100167static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100168disable_bp_cb(void *addr, void *sbp, void *proc)
169{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200170 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200171 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200172 disable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100173 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200174}
175
Juan Cespedesf1350522008-12-16 18:19:58 +0100176void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200177disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200178 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200179 assert(proc->leader == proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100181 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
182 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183 }
184 proc->breakpoints_enabled = 0;
185}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100186
Juan Cespedesf1350522008-12-16 18:19:58 +0100187static void
188free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200189 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100190 assert(sbp);
191 free(sbp);
192}
193
Petr Machata1974dbc2011-08-19 18:58:01 +0200194int
Petr Machatac7585b62011-07-08 22:58:12 +0200195breakpoints_init(Process *proc, int enable)
196{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100198
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
Juan Cespedesce377d52008-12-16 19:38:10 +0100217 if (options.libcalls && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100218 proc->list_of_symbols = read_elf(proc);
Petr Machata1974dbc2011-08-19 18:58:01 +0200219 if (proc->list_of_symbols == NULL) {
220 /* XXX leak breakpoints */
221 return -1;
222 }
223
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100224 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200225 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 while (*tmp1) {
227 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100228 int keep = !opt_e_enable;
229
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200231 if (!strcmp((*tmp1)->name,
232 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100233 keep = opt_e_enable;
234 }
235 tmp2 = tmp2->next;
236 }
237 if (!keep) {
238 *tmp1 = (*tmp1)->next;
239 } else {
240 tmp1 = &((*tmp1)->next);
241 }
242 }
243 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100244 }
Petr Machatac7585b62011-07-08 22:58:12 +0200245
246 for (sym = proc->list_of_symbols; sym; sym = sym->next)
247 insert_breakpoint(proc, sym2addr(proc, sym), sym, enable);
248
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100249 proc->callstack_depth = 0;
250 proc->breakpoints_enabled = -1;
Petr Machata1974dbc2011-08-19 18:58:01 +0200251 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100252}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100253
Juan Cespedesf1350522008-12-16 18:19:58 +0100254void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200255reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200256 struct library_symbol *sym;
257
258 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
259
260 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100261
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 while (sym) {
263 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200264 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 if (sym->needs_init && !sym->is_weak) {
266 fprintf(stderr,
267 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
268 sym->name, proc->filename);
269 exit(1);
270 }
271 }
272 sym = sym->next;
273 }
274}