blob: 18de1e26cdc987750f1f70945769794aad7650e9 [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 Cespedes5b3ffdf2001-07-02 00:52:45 +020051 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010052 if (sbp->enabled == 1 && proc->pid)
53 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020054}
55
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056void delete_breakpoint(struct process *proc, void *addr)
57{
58 struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
59 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020060 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 if (sbp == NULL)
62 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020063
64 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 if (sbp->enabled == 0)
66 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020067 assert(sbp->enabled >= 0);
68}
69
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070static void enable_bp_cb(void *addr, void *sbp, void *proc)
71{
Juan Cespedescac15c32003-01-31 18:58:58 +010072 if (((struct breakpoint *)sbp)->enabled) {
73 enable_breakpoint(((struct process *)proc)->pid, sbp);
74 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020075}
76
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077void enable_all_breakpoints(struct process *proc)
78{
Juan Cespedes5e01f651998-03-08 22:31:44 +010079 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010080#ifdef __powerpc__
81 unsigned long a;
82
83 /*
84 * PPC HACK! (XXX FIXME TODO)
85 * If the dynamic linker hasn't populated the PLT then
86 * dont enable the breakpoints
87 */
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020088 if (opt_L) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020090 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020092 if (a == 0x0)
93 return;
94 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +010095#endif
96
Juan Cespedescac15c32003-01-31 18:58:58 +010097 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +010098 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
100 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100101 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100102#ifdef __mips__
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200103 {
104 // I'm sure there is a nicer way to do this. We need to
105 // insert breakpoints _after_ the child has been started.
106 struct library_symbol *sym;
107 struct library_symbol *new_sym;
108 sym=proc->list_of_symbols;
109 while(sym){
110 void *addr= sym2addr(proc,sym);
111 if(!addr){
112 sym=sym->next;
113 continue;
114 }
115 if(dict_find_entry(proc->breakpoints,addr)){
116 sym=sym->next;
117 continue;
118 }
119 debug(2,"inserting bp %p %s",addr,sym->name);
120 new_sym=malloc(sizeof(*new_sym));
121 memcpy(new_sym,sym,sizeof(*new_sym));
122 new_sym->next=proc->list_of_symbols;
123 proc->list_of_symbols=new_sym;
124 new_sym->brkpnt=0;
125 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
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134static void disable_bp_cb(void *addr, void *sbp, void *proc)
135{
Juan Cespedescac15c32003-01-31 18:58:58 +0100136 if (((struct breakpoint *)sbp)->enabled) {
137 disable_breakpoint(((struct process *)proc)->pid, sbp);
138 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200139}
140
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141void disable_all_breakpoints(struct process *proc)
142{
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
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100150static void free_bp_cb(void *addr, void *sbp, void *data)
151{
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100152 assert(sbp);
153 free(sbp);
154}
155
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100156void breakpoints_init(struct process *proc)
157{
158 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
167 if (opt_L && 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
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199void reinitialize_breakpoints(struct process *proc)
200{
201 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}