blob: a47d9891b564cffc9ff3241882bea892ea90c5bb [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;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020031
Juan Cespedescac15c32003-01-31 18:58:58 +010032 if (!proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 proc->breakpoints =
34 dict_init(dict_key2hash_int, dict_key_cmp_int);
35 /* atexit(brk_dict_clear); *//* why bother to do this on exit? */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020036 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010037
Ian Wienand2d45b1a2006-02-20 22:48:07 +010038 if (!addr)
39 return;
Ian Wienand9a2ad352006-02-20 22:44:45 +010040
Ian Wienand2d45b1a2006-02-20 22:48:07 +010041 if (libsym)
Ian Wienand9a2ad352006-02-20 22:44:45 +010042 libsym->needs_init = 0;
43
Juan Cespedescac15c32003-01-31 18:58:58 +010044 sbp = dict_find_entry(proc->breakpoints, addr);
45 if (!sbp) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010046 sbp = calloc(1, sizeof(struct breakpoint));
Juan Cespedescac15c32003-01-31 18:58:58 +010047 if (!sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 return; /* TODO FIXME XXX: error_mem */
Juan Cespedescac15c32003-01-31 18:58:58 +010049 }
50 dict_enter(proc->breakpoints, addr, sbp);
51 sbp->addr = addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010052 sbp->libsym = libsym;
53 if (libsym)
54 libsym->brkpnt = sbp;
Juan Cespedescac15c32003-01-31 18:58:58 +010055 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020056 sbp->enabled++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010057 if (sbp->enabled == 1 && proc->pid)
58 enable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020059}
60
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061void delete_breakpoint(struct process *proc, void *addr)
62{
63 struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr);
64 assert(sbp); /* FIXME: remove after debugging has been done. */
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 if (sbp == NULL)
67 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020068
69 sbp->enabled--;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 if (sbp->enabled == 0)
71 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020072 assert(sbp->enabled >= 0);
73}
74
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075static void enable_bp_cb(void *addr, void *sbp, void *proc)
76{
Juan Cespedescac15c32003-01-31 18:58:58 +010077 if (((struct breakpoint *)sbp)->enabled) {
78 enable_breakpoint(((struct process *)proc)->pid, sbp);
79 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020080}
81
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082void enable_all_breakpoints(struct process *proc)
83{
Juan Cespedes5e01f651998-03-08 22:31:44 +010084 if (proc->breakpoints_enabled <= 0) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +010085#ifdef __powerpc__
86 unsigned long a;
87
88 /*
89 * PPC HACK! (XXX FIXME TODO)
90 * If the dynamic linker hasn't populated the PLT then
91 * dont enable the breakpoints
92 */
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020093 if (opt_L) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 a = ptrace(PTRACE_PEEKTEXT, proc->pid,
Paul Gilliam76c61f12006-06-14 06:55:21 +020095 sym2addr(proc, proc->list_of_symbols),
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 0);
Juan Cespedesde5a7eb2002-03-31 20:53:52 +020097 if (a == 0x0)
98 return;
99 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100100#endif
101
Juan Cespedescac15c32003-01-31 18:58:58 +0100102 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100103 if (proc->breakpoints) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
105 proc);
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100106 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107 }
108 proc->breakpoints_enabled = 1;
109}
110
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111static void disable_bp_cb(void *addr, void *sbp, void *proc)
112{
Juan Cespedescac15c32003-01-31 18:58:58 +0100113 if (((struct breakpoint *)sbp)->enabled) {
114 disable_breakpoint(((struct process *)proc)->pid, sbp);
115 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200116}
117
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118void disable_all_breakpoints(struct process *proc)
119{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100121 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
122 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 }
124 proc->breakpoints_enabled = 0;
125}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100126
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127static void free_bp_cb(void *addr, void *sbp, void *data)
128{
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100129 assert(sbp);
130 free(sbp);
131}
132
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100133void breakpoints_init(struct process *proc)
134{
135 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100136
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100138 /* TODO FIXME XXX: free() all "struct breakpoint"s */
139 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
140 dict_clear(proc->breakpoints);
141 proc->breakpoints = NULL;
142 }
143
144 if (opt_L && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100145 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100146 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 struct library_symbol **tmp1 = &(proc->list_of_symbols);
148 while (*tmp1) {
149 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100150 int keep = !opt_e_enable;
151
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100152 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100153 if (!strcmp((*tmp1)->name, tmp2->name)) {
154 keep = opt_e_enable;
155 }
156 tmp2 = tmp2->next;
157 }
158 if (!keep) {
159 *tmp1 = (*tmp1)->next;
160 } else {
161 tmp1 = &((*tmp1)->next);
162 }
163 }
164 }
165 } else {
166 proc->list_of_symbols = NULL;
167 }
168 sym = proc->list_of_symbols;
169 while (sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100170 /* proc->pid==0 delays enabling. */
Paul Gilliam76c61f12006-06-14 06:55:21 +0200171 insert_breakpoint(proc, sym2addr(proc, sym), sym);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100172 sym = sym->next;
173 }
174 proc->callstack_depth = 0;
175 proc->breakpoints_enabled = -1;
176}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100177
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100178void reinitialize_breakpoints(struct process *proc)
179{
180 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100181
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100182 while (sym) {
183 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200184 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100185 sym);
186 if (sym->needs_init && !sym->is_weak) {
187 fprintf(stderr,
188 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
189 sym->name, proc->filename);
190 exit(1);
191 }
192 }
193 sym = sym->next;
194 }
195}