blob: b77ad60a1edb4c1c5e62fd5e07e9ac1e722ce4a9 [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
Ian Wienand2d45b1a2006-02-20 22:48:07 +010021struct breakpoint *address2bpstruct(struct process *proc, void *addr)
22{
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
Ian Wienand2d45b1a2006-02-20 22:48:07 +010027insert_breakpoint(struct process *proc, void *addr,
28 struct library_symbol *libsym)
29{
30 struct breakpoint *sbp;
Petr Machatab3f8fef2006-11-30 14:45:07 +010031 debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020032
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 if (!addr)
34 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010035
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010037 libsym->needs_init = 0;
38
Juan Cespedescac15c32003-01-31 18:58:58 +010039 sbp = dict_find_entry(proc->breakpoints, addr);
40 if (!sbp) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010041 sbp = calloc(1, sizeof(struct breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010042 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010043 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010044 }
45 dict_enter(proc->breakpoints, addr, sbp);
46 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010047 sbp->libsym = libsym;
48 if (libsym)
49 libsym->brkpnt = sbp;
Juan Cespedescac15c32003-01-31 18:58:58 +010050 }
Juan Cespedes63184be2008-12-10 13:30:12 +010051#ifdef __arm__
52 sbp->thumb_mode = proc->thumb_mode;
53 proc->thumb_mode = 0;
54#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056 if (sbp->enabled == 1 && proc->pid)
57 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020058}
59
Ian Wienand2d45b1a2006-02-20 22:48:07 +010060void delete_breakpoint(struct process *proc, void *addr)
61{
62 struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
63 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 if (sbp == NULL)
66 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020067
68 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 if (sbp->enabled == 0)
70 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020071 assert(sbp->enabled >= 0);
72}
73
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074static void enable_bp_cb(void *addr, void *sbp, void *proc)
75{
Juan Cespedescac15c32003-01-31 18:58:58 +010076 if (((struct breakpoint *)sbp)->enabled) {
77 enable_breakpoint(((struct process *)proc)->pid, sbp);
78 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020079}
80
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081void enable_all_breakpoints(struct process *proc)
82{
Juan Cespedes5e01f651998-03-08 22:31:44 +010083 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010084#ifdef __powerpc__
85 unsigned long a;
86
87 /*
88 * PPC HACK! (XXX FIXME TODO)
89 * If the dynamic linker hasn't populated the PLT then
90 * dont enable the breakpoints
91 */
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020092 if (opt_L) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020094 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020096 if (a == 0x0)
97 return;
98 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +010099#endif
100
Juan Cespedescac15c32003-01-31 18:58:58 +0100101 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100102 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
104 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100105 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100106#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200107 {
108 // I'm sure there is a nicer way to do this. We need to
109 // insert breakpoints _after_ the child has been started.
110 struct library_symbol *sym;
111 struct library_symbol *new_sym;
112 sym=proc->list_of_symbols;
113 while(sym){
114 void *addr= sym2addr(proc,sym);
115 if(!addr){
116 sym=sym->next;
117 continue;
118 }
119 if(dict_find_entry(proc->breakpoints,addr)){
120 sym=sym->next;
121 continue;
122 }
123 debug(2,"inserting bp %p %s",addr,sym->name);
124 new_sym=malloc(sizeof(*new_sym));
125 memcpy(new_sym,sym,sizeof(*new_sym));
126 new_sym->next=proc->list_of_symbols;
127 proc->list_of_symbols=new_sym;
128 new_sym->brkpnt=0;
129 insert_breakpoint(proc, addr, new_sym);
130 sym=sym->next;
131 }
132 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100133#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 }
135 proc->breakpoints_enabled = 1;
136}
137
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138static void disable_bp_cb(void *addr, void *sbp, void *proc)
139{
Juan Cespedescac15c32003-01-31 18:58:58 +0100140 if (((struct breakpoint *)sbp)->enabled) {
141 disable_breakpoint(((struct process *)proc)->pid, sbp);
142 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200143}
144
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100145void disable_all_breakpoints(struct process *proc)
146{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100147 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100148 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
149 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100150 }
151 proc->breakpoints_enabled = 0;
152}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100153
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154static void free_bp_cb(void *addr, void *sbp, void *data)
155{
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100156 assert(sbp);
157 free(sbp);
158}
159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160void breakpoints_init(struct process *proc)
161{
162 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100163
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100164 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100165 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
166 dict_clear(proc->breakpoints);
167 proc->breakpoints = NULL;
168 }
Petr Machata89a53602007-01-25 18:05:44 +0100169 proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100170
171 if (opt_L && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100172 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100173 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100174 struct library_symbol **tmp1 = &(proc->list_of_symbols);
175 while (*tmp1) {
176 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100177 int keep = !opt_e_enable;
178
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100179 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100180 if (!strcmp((*tmp1)->name, tmp2->name)) {
181 keep = opt_e_enable;
182 }
183 tmp2 = tmp2->next;
184 }
185 if (!keep) {
186 *tmp1 = (*tmp1)->next;
187 } else {
188 tmp1 = &((*tmp1)->next);
189 }
190 }
191 }
192 } else {
193 proc->list_of_symbols = NULL;
194 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100195 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200197 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100198 }
199 proc->callstack_depth = 0;
200 proc->breakpoints_enabled = -1;
201}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100202
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100203void reinitialize_breakpoints(struct process *proc)
204{
205 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100206
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 while (sym) {
208 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200209 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 sym);
211 if (sym->needs_init && !sym->is_weak) {
212 fprintf(stderr,
213 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
214 sym->name, proc->filename);
215 exit(1);
216 }
217 }
218 sym = sym->next;
219 }
220}