blob: 4e74923c122fb9c15020d3f9b7106cd443fc4e05 [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
Juan Cespedesf7281232009-06-25 16:11:21 +020011#include "common.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020012
13/*****************************************************************************/
14
Juan Cespedes1dec2172009-05-07 10:12:10 +020015Breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020016address2bpstruct(Process *proc, void *addr) {
Petr Machata26627682011-07-08 18:15:32 +020017 assert(proc != NULL);
18 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020019 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020020 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010021 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020022}
23
Ian Wienand9a2ad352006-02-20 22:44:45 +010024void
Juan Cespedesa8909f72009-04-28 20:02:41 +020025insert_breakpoint(Process *proc, void *addr,
Petr Machatac7585b62011-07-08 22:58:12 +020026 struct library_symbol *libsym, int enable) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020027 Breakpoint *sbp;
Juan Cespedescd8976d2009-05-14 13:47:58 +020028
Petr Machata9a5420c2011-07-09 11:21:23 +020029 Process * leader = proc->leader;
30
31 /* Only the group leader should be getting the breakpoints and
32 * thus have ->breakpoint initialized. */
33 assert(leader != NULL);
34 assert(leader->breakpoints != NULL);
35
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070036#ifdef __arm__
37 int thumb_mode = (int)addr & 1;
38 if (thumb_mode)
39 addr = (void *)((int)addr & ~1);
40#endif
41
Juan Cespedescd8976d2009-05-14 13:47:58 +020042 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 +010043 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020044
Ian Wienand2d45b1a2006-02-20 22:48:07 +010045 if (!addr)
46 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010047
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010049 libsym->needs_init = 0;
50
Petr Machata9a5420c2011-07-09 11:21:23 +020051 sbp = dict_find_entry(leader->breakpoints, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010052 if (!sbp) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020053 sbp = calloc(1, sizeof(Breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010054 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010056 }
Petr Machata9a5420c2011-07-09 11:21:23 +020057 dict_enter(leader->breakpoints, addr, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010058 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010059 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010060 }
Juan Cespedes63184be2008-12-10 13:30:12 +010061#ifdef __arm__
Zachary T Welcha2ff9d62010-10-08 11:47:49 -070062 sbp->thumb_mode = thumb_mode | proc->thumb_mode;
Juan Cespedes63184be2008-12-10 13:30:12 +010063 proc->thumb_mode = 0;
64#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065 sbp->enabled++;
Petr Machatac7585b62011-07-08 22:58:12 +020066 if (sbp->enabled == 1 && enable) {
67 assert(proc->pid != 0);
Petr Machataf789c9c2011-07-09 10:54:27 +020068 enable_breakpoint(proc, sbp);
Petr Machatac7585b62011-07-08 22:58:12 +020069 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020070}
71
Juan Cespedesf1350522008-12-16 18:19:58 +010072void
Juan Cespedesa8909f72009-04-28 20:02:41 +020073delete_breakpoint(Process *proc, void *addr) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020074 Breakpoint *sbp;
75
76 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
77
Petr Machata9a5420c2011-07-09 11:21:23 +020078 Process * leader = proc->leader;
79 assert(leader != NULL);
80
81 sbp = dict_find_entry(leader->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020083 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 if (sbp == NULL)
85 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020086
87 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 if (sbp->enabled == 0)
Petr Machataf789c9c2011-07-09 10:54:27 +020089 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020090 assert(sbp->enabled >= 0);
91}
92
Juan Cespedesf1350522008-12-16 18:19:58 +010093static void
94enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020095 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +020096 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +020097 enable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010098 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020099}
100
Juan Cespedesf1350522008-12-16 18:19:58 +0100101void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200102enable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200103 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100104 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100105#ifdef __powerpc__
106 unsigned long a;
107
108 /*
109 * PPC HACK! (XXX FIXME TODO)
110 * If the dynamic linker hasn't populated the PLT then
111 * dont enable the breakpoints
112 */
Juan Cespedesce377d52008-12-16 19:38:10 +0100113 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200115 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +0200117 if (a == 0x0)
118 return;
119 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100120#endif
121
Juan Cespedescac15c32003-01-31 18:58:58 +0100122 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100123 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
125 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100126 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100127#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200128 {
Juan Cespedes5c682042009-05-21 15:59:56 +0200129 /*
130 * I'm sure there is a nicer way to do this. We need to
131 * insert breakpoints _after_ the child has been started.
132 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200133 struct library_symbol *sym;
134 struct library_symbol *new_sym;
135 sym=proc->list_of_symbols;
136 while(sym){
137 void *addr= sym2addr(proc,sym);
138 if(!addr){
139 sym=sym->next;
140 continue;
141 }
142 if(dict_find_entry(proc->breakpoints,addr)){
143 sym=sym->next;
144 continue;
145 }
146 debug(2,"inserting bp %p %s",addr,sym->name);
Arnaud Patard47950872010-01-08 08:40:15 -0500147 new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
148 memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200149 new_sym->next=proc->list_of_symbols;
150 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200151 insert_breakpoint(proc, addr, new_sym);
152 sym=sym->next;
153 }
154 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100155#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100156 }
157 proc->breakpoints_enabled = 1;
158}
159
Juan Cespedesf1350522008-12-16 18:19:58 +0100160static void
161disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200162 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Juan Cespedes1dec2172009-05-07 10:12:10 +0200163 if (((Breakpoint *)sbp)->enabled) {
Petr Machataf789c9c2011-07-09 10:54:27 +0200164 disable_breakpoint(proc, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100165 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200166}
167
Juan Cespedesf1350522008-12-16 18:19:58 +0100168void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200169disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200170 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200171 assert(proc->leader == proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100172 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100173 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
174 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100175 }
176 proc->breakpoints_enabled = 0;
177}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100178
Juan Cespedesf1350522008-12-16 18:19:58 +0100179static void
180free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200181 debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100182 assert(sbp);
183 free(sbp);
184}
185
Juan Cespedesf1350522008-12-16 18:19:58 +0100186void
Petr Machatac7585b62011-07-08 22:58:12 +0200187breakpoints_init(Process *proc, int enable)
188{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100189 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100190
Juan Cespedescd8976d2009-05-14 13:47:58 +0200191 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100192 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100193 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
194 dict_clear(proc->breakpoints);
195 proc->breakpoints = NULL;
196 }
Petr Machata26627682011-07-08 18:15:32 +0200197
Petr Machata9a5420c2011-07-09 11:21:23 +0200198 /* Only the thread group leader should hold the breakpoints.
199 * (N.B. PID may be set to 0 temporarily when called by
200 * handle_exec). */
201 assert(proc->leader == proc);
202
Petr Machata26627682011-07-08 18:15:32 +0200203 proc->breakpoints = dict_init(dict_key2hash_int,
204 dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100205
Petr Machata3d7e4b82011-07-08 18:15:19 +0200206 if (proc->list_of_symbols != NULL) {
207 struct library_symbol * sym = proc->list_of_symbols;
208 while (sym != NULL) {
209 struct library_symbol * next = sym->next;
210 free(sym);
211 sym = next;
212 }
213 }
214 proc->list_of_symbols = NULL;
215
Juan Cespedesce377d52008-12-16 19:38:10 +0100216 if (options.libcalls && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100217 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100218 if (opt_e) {
Petr Machata26627682011-07-08 18:15:32 +0200219 struct library_symbol **tmp1 = &proc->list_of_symbols;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 while (*tmp1) {
221 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100222 int keep = !opt_e_enable;
223
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 while (tmp2) {
Petr Machata26627682011-07-08 18:15:32 +0200225 if (!strcmp((*tmp1)->name,
226 tmp2->name)) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100227 keep = opt_e_enable;
228 }
229 tmp2 = tmp2->next;
230 }
231 if (!keep) {
232 *tmp1 = (*tmp1)->next;
233 } else {
234 tmp1 = &((*tmp1)->next);
235 }
236 }
237 }
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100238 }
Petr Machatac7585b62011-07-08 22:58:12 +0200239
240 for (sym = proc->list_of_symbols; sym; sym = sym->next)
241 insert_breakpoint(proc, sym2addr(proc, sym), sym, enable);
242
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100243 proc->callstack_depth = 0;
244 proc->breakpoints_enabled = -1;
245}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100246
Juan Cespedesf1350522008-12-16 18:19:58 +0100247void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200248reinitialize_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200249 struct library_symbol *sym;
250
251 debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);
252
253 sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100254
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 while (sym) {
256 if (sym->needs_init) {
Petr Machatac7585b62011-07-08 22:58:12 +0200257 insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 if (sym->needs_init && !sym->is_weak) {
259 fprintf(stderr,
260 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
261 sym->name, proc->filename);
262 exit(1);
263 }
264 }
265 sym = sym->next;
266 }
267}