blob: 968d1f17dc29795ccfe184c0fb20152f2540f736 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02005#include <stdlib.h>
Juan Cespedes7186e2a2003-01-31 19:56:34 +01006#include <string.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02007#include <assert.h>
8
Juan Cespedesf1bfe202002-03-27 00:22:23 +01009#ifdef __powerpc__
10#include <sys/ptrace.h>
11#endif
12
Juan Cespedescac15c32003-01-31 18:58:58 +010013#include "ltrace.h"
14#include "options.h"
15#include "debug.h"
16#include "dict.h"
Juan Cespedes7186e2a2003-01-31 19:56:34 +010017#include "elf.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020018
19/*****************************************************************************/
20
Juan Cespedes1dec2172009-05-07 10:12:10 +020021Breakpoint *
Juan Cespedesa8909f72009-04-28 20:02:41 +020022address2bpstruct(Process *proc, void *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
Ian Wienand9a2ad352006-02-20 22:44:45 +010026void
Juan Cespedesa8909f72009-04-28 20:02:41 +020027insert_breakpoint(Process *proc, void *addr,
Juan Cespedesf1350522008-12-16 18:19:58 +010028 struct library_symbol *libsym) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020029 Breakpoint *sbp;
Petr Machatab3f8fef2006-11-30 14:45:07 +010030 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020031
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 if (!addr)
33 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010034
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010036 libsym->needs_init = 0;
37
Juan Cespedescac15c32003-01-31 18:58:58 +010038 sbp = dict_find_entry(proc->breakpoints, addr);
39 if (!sbp) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020040 sbp = calloc(1, sizeof(Breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010041 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010043 }
44 dict_enter(proc->breakpoints, addr, sbp);
45 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010046 sbp->libsym = libsym;
Juan Cespedescac15c32003-01-31 18:58:58 +010047 }
Juan Cespedes63184be2008-12-10 13:30:12 +010048#ifdef __arm__
49 sbp->thumb_mode = proc->thumb_mode;
50 proc->thumb_mode = 0;
51#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020052 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010053 if (sbp->enabled == 1 && proc->pid)
54 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055}
56
Juan Cespedesf1350522008-12-16 18:19:58 +010057void
Juan Cespedesa8909f72009-04-28 20:02:41 +020058delete_breakpoint(Process *proc, void *addr) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020059 Breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010060 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020061 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062 if (sbp == NULL)
63 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064
65 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 if (sbp->enabled == 0)
67 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020068 assert(sbp->enabled >= 0);
69}
70
Juan Cespedesf1350522008-12-16 18:19:58 +010071static void
72enable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedes1dec2172009-05-07 10:12:10 +020073 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020074 enable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +010075 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020076}
77
Juan Cespedesf1350522008-12-16 18:19:58 +010078void
Juan Cespedesa8909f72009-04-28 20:02:41 +020079enable_all_breakpoints(Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010080 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010081#ifdef __powerpc__
82 unsigned long a;
83
84 /*
85 * PPC HACK! (XXX FIXME TODO)
86 * If the dynamic linker hasn't populated the PLT then
87 * dont enable the breakpoints
88 */
Juan Cespedesce377d52008-12-16 19:38:10 +010089 if (options.libcalls) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020091 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020093 if (a == 0x0)
94 return;
95 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +010096#endif
97
Juan Cespedescac15c32003-01-31 18:58:58 +010098 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +010099 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
101 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100102 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100103#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200104 {
105 // I'm sure there is a nicer way to do this. We need to
106 // insert breakpoints _after_ the child has been started.
107 struct library_symbol *sym;
108 struct library_symbol *new_sym;
109 sym=proc->list_of_symbols;
110 while(sym){
111 void *addr= sym2addr(proc,sym);
112 if(!addr){
113 sym=sym->next;
114 continue;
115 }
116 if(dict_find_entry(proc->breakpoints,addr)){
117 sym=sym->next;
118 continue;
119 }
120 debug(2,"inserting bp %p %s",addr,sym->name);
121 new_sym=malloc(sizeof(*new_sym));
122 memcpy(new_sym,sym,sizeof(*new_sym));
123 new_sym->next=proc->list_of_symbols;
124 proc->list_of_symbols=new_sym;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200125 insert_breakpoint(proc, addr, new_sym);
126 sym=sym->next;
127 }
128 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100129#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 }
131 proc->breakpoints_enabled = 1;
132}
133
Juan Cespedesf1350522008-12-16 18:19:58 +0100134static void
135disable_bp_cb(void *addr, void *sbp, void *proc) {
Juan Cespedes1dec2172009-05-07 10:12:10 +0200136 if (((Breakpoint *)sbp)->enabled) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200137 disable_breakpoint(((Process *)proc)->pid, sbp);
Juan Cespedescac15c32003-01-31 18:58:58 +0100138 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200139}
140
Juan Cespedesf1350522008-12-16 18:19:58 +0100141void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200142disable_all_breakpoints(Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100144 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
145 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146 }
147 proc->breakpoints_enabled = 0;
148}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150static void
151free_bp_cb(void *addr, void *sbp, void *data) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100152 assert(sbp);
153 free(sbp);
154}
155
Juan Cespedesf1350522008-12-16 18:19:58 +0100156void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200157breakpoints_init(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100161 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
162 dict_clear(proc->breakpoints);
163 proc->breakpoints = NULL;
164 }
Petr Machata89a53602007-01-25 18:05:44 +0100165 proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100166
Juan Cespedesce377d52008-12-16 19:38:10 +0100167 if (options.libcalls && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100168 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100169 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100170 struct library_symbol **tmp1 = &(proc->list_of_symbols);
171 while (*tmp1) {
172 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100173 int keep = !opt_e_enable;
174
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100176 if (!strcmp((*tmp1)->name, tmp2->name)) {
177 keep = opt_e_enable;
178 }
179 tmp2 = tmp2->next;
180 }
181 if (!keep) {
182 *tmp1 = (*tmp1)->next;
183 } else {
184 tmp1 = &((*tmp1)->next);
185 }
186 }
187 }
188 } else {
189 proc->list_of_symbols = NULL;
190 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100191 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100192 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200193 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100194 }
195 proc->callstack_depth = 0;
196 proc->breakpoints_enabled = -1;
197}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100198
Juan Cespedesf1350522008-12-16 18:19:58 +0100199void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200200reinitialize_breakpoints(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100202
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100203 while (sym) {
204 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200205 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 sym);
207 if (sym->needs_init && !sym->is_weak) {
208 fprintf(stderr,
209 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
210 sym->name, proc->filename);
211 exit(1);
212 }
213 }
214 sym = sym->next;
215 }
216}