blob: 03fbb72189b52e9d448316d654b840432352b274 [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 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100108 }
109 proc->breakpoints_enabled = 1;
110}
111
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112static void disable_bp_cb(void *addr, void *sbp, void *proc)
113{
Juan Cespedescac15c32003-01-31 18:58:58 +0100114 if (((struct breakpoint *)sbp)->enabled) {
115 disable_breakpoint(((struct process *)proc)->pid, sbp);
116 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200117}
118
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119void disable_all_breakpoints(struct process *proc)
120{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100121 if (proc->breakpoints_enabled) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100122 debug(1, "Disabling breakpoints for pid %u...", proc->pid);
123 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100124 }
125 proc->breakpoints_enabled = 0;
126}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100127
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128static void free_bp_cb(void *addr, void *sbp, void *data)
129{
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100130 assert(sbp);
131 free(sbp);
132}
133
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134void breakpoints_init(struct process *proc)
135{
136 struct library_symbol *sym;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100137
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 if (proc->breakpoints) { /* let's remove that struct */
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100139 /* TODO FIXME XXX: free() all "struct breakpoint"s */
140 dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
141 dict_clear(proc->breakpoints);
142 proc->breakpoints = NULL;
143 }
144
145 if (opt_L && proc->filename) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100146 proc->list_of_symbols = read_elf(proc);
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100147 if (opt_e) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100148 struct library_symbol **tmp1 = &(proc->list_of_symbols);
149 while (*tmp1) {
150 struct opt_e_t *tmp2 = opt_e;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100151 int keep = !opt_e_enable;
152
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100153 while (tmp2) {
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100154 if (!strcmp((*tmp1)->name, tmp2->name)) {
155 keep = opt_e_enable;
156 }
157 tmp2 = tmp2->next;
158 }
159 if (!keep) {
160 *tmp1 = (*tmp1)->next;
161 } else {
162 tmp1 = &((*tmp1)->next);
163 }
164 }
165 }
166 } else {
167 proc->list_of_symbols = NULL;
168 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100169 for (sym = proc->list_of_symbols; sym; sym = sym->next) {
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 }
173 proc->callstack_depth = 0;
174 proc->breakpoints_enabled = -1;
175}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100176
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100177void reinitialize_breakpoints(struct process *proc)
178{
179 struct library_symbol *sym = proc->list_of_symbols;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100180
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 while (sym) {
182 if (sym->needs_init) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200183 insert_breakpoint(proc, sym2addr(proc, sym),
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100184 sym);
185 if (sym->needs_init && !sym->is_weak) {
186 fprintf(stderr,
187 "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
188 sym->name, proc->filename);
189 exit(1);
190 }
191 }
192 sym = sym->next;
193 }
194}