blob: 2416f3c6e0a5f950cc39769b174529b13d20f915 [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
Juan Cespedescac15c32003-01-31 18:58:58 +010033 if (!proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 proc->breakpoints =
35 dict_init(dict_key2hash_int, dict_key_cmp_int);
36 /* atexit(brk_dict_clear); *//* why bother to do this on exit? */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020037 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010038
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 if (!addr)
40 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010041
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010043 libsym->needs_init = 0;
44
Juan Cespedescac15c32003-01-31 18:58:58 +010045 sbp = dict_find_entry(proc->breakpoints, addr);
46 if (!sbp) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010047 sbp = calloc(1, sizeof(struct breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010048 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010050 }
51 dict_enter(proc->breakpoints, addr, sbp);
52 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010053 sbp->libsym = libsym;
54 if (libsym)
55 libsym->brkpnt = sbp;
Juan Cespedescac15c32003-01-31 18:58:58 +010056 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020057 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058 if (sbp->enabled == 1 && proc->pid)
59 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020060}
61
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062void delete_breakpoint(struct process *proc, void *addr)
63{
64 struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
65 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020066 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010067 if (sbp == NULL)
68 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020069
70 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 if (sbp->enabled == 0)
72 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020073 assert(sbp->enabled >= 0);
74}
75
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076static void enable_bp_cb(void *addr, void *sbp, void *proc)
77{
Juan Cespedescac15c32003-01-31 18:58:58 +010078 if (((struct breakpoint *)sbp)->enabled) {
79 enable_breakpoint(((struct process *)proc)->pid, sbp);
80 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020081}
82
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083void enable_all_breakpoints(struct process *proc)
84{
Juan Cespedes5e01f651998-03-08 22:31:44 +010085 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010086#ifdef __powerpc__
87 unsigned long a;
88
89 /*
90 * PPC HACK! (XXX FIXME TODO)
91 * If the dynamic linker hasn't populated the PLT then
92 * dont enable the breakpoints
93 */
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020094 if (opt_L) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020096 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020098 if (a == 0x0)
99 return;
100 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100101#endif
102
Juan Cespedescac15c32003-01-31 18:58:58 +0100103 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100104 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
106 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100107 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100108#ifdef __mips__
109 {
110 // I'm sure there is a nicer way to do this. We need to
111 // insert breakpoints _after_ the child has been started.
112 struct library_symbol *sym;
113 struct library_symbol *new_sym;
114 sym=proc->list_of_symbols;
115 while(sym){
116 void *addr= sym2addr(proc,sym);
117 if(!addr){
118 sym=sym->next;
119 continue;
120 }
121 if(dict_find_entry(proc->breakpoints,addr)){
122 sym=sym->next;
123 continue;
124 }
125 debug(2,"inserting bp %p %s",addr,sym->name);
126 new_sym=malloc(sizeof(*new_sym));
127 memcpy(new_sym,sym,sizeof(*new_sym));
128 new_sym->next=proc->list_of_symbols;
129 proc->list_of_symbols=new_sym;
130 new_sym->brkpnt=0;
131 insert_breakpoint(proc, addr, new_sym);
132 sym=sym->next;
133 }
134 }
135#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 }
137 proc->breakpoints_enabled = 1;
138}
139
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140static void disable_bp_cb(void *addr, void *sbp, void *proc)
141{
Juan Cespedescac15c32003-01-31 18:58:58 +0100142 if (((struct breakpoint *)sbp)->enabled) {
143 disable_breakpoint(((struct process *)proc)->pid, sbp);
144 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200145}
146
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147void disable_all_breakpoints(struct process *proc)
148{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100150 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
151 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 }
153 proc->breakpoints_enabled = 0;
154}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100155
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100156static void free_bp_cb(void *addr, void *sbp, void *data)
157{
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100158 assert(sbp);
159 free(sbp);
160}
161
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100162void breakpoints_init(struct process *proc)
163{
164 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100165
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100166 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100167 /* TODO FIXME XXX: free() all "struct breakpoint"s */
168 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
169 dict_clear(proc->breakpoints);
170 proc->breakpoints = NULL;
171 }
172
173 if (opt_L && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100174 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100175 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 struct library_symbol **tmp1 = &(proc->list_of_symbols);
177 while (*tmp1) {
178 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100179 int keep = !opt_e_enable;
180
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100182 if (!strcmp((*tmp1)->name, tmp2->name)) {
183 keep = opt_e_enable;
184 }
185 tmp2 = tmp2->next;
186 }
187 if (!keep) {
188 *tmp1 = (*tmp1)->next;
189 } else {
190 tmp1 = &((*tmp1)->next);
191 }
192 }
193 }
194 } else {
195 proc->list_of_symbols = NULL;
196 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100197 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200199 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100200 }
201 proc->callstack_depth = 0;
202 proc->breakpoints_enabled = -1;
203}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100204
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205void reinitialize_breakpoints(struct process *proc)
206{
207 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100208
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 while (sym) {
210 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200211 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 sym);
213 if (sym->needs_init && !sym->is_weak) {
214 fprintf(stderr,
215 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
216 sym->name, proc->filename);
217 exit(1);
218 }
219 }
220 sym = sym->next;
221 }
222}