| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 5 | #include <stdlib.h> |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 6 | #include <string.h> |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 9 | #ifdef __powerpc__ |
| 10 | #include <sys/ptrace.h> |
| 11 | #endif |
| 12 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 13 | #include "ltrace.h" |
| 14 | #include "options.h" |
| 15 | #include "debug.h" |
| 16 | #include "dict.h" |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 17 | #include "elf.h" |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 18 | |
| 19 | /*****************************************************************************/ |
| 20 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 21 | struct breakpoint *address2bpstruct(struct process *proc, void *addr) |
| 22 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 23 | return dict_find_entry(proc->breakpoints, addr); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 26 | void |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 27 | insert_breakpoint(struct process *proc, void *addr, |
| 28 | struct library_symbol *libsym) |
| 29 | { |
| 30 | struct breakpoint *sbp; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 31 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 32 | if (!proc->breakpoints) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 33 | 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 Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 36 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 37 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 38 | if (!addr) |
| 39 | return; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 40 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 41 | if (libsym) |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 42 | libsym->needs_init = 0; |
| 43 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 44 | sbp = dict_find_entry(proc->breakpoints, addr); |
| 45 | if (!sbp) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 46 | sbp = calloc(1, sizeof(struct breakpoint)); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 47 | if (!sbp) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 48 | return; /* TODO FIXME XXX: error_mem */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 49 | } |
| 50 | dict_enter(proc->breakpoints, addr, sbp); |
| 51 | sbp->addr = addr; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 52 | sbp->libsym = libsym; |
| 53 | if (libsym) |
| 54 | libsym->brkpnt = sbp; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 55 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 56 | sbp->enabled++; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 57 | if (sbp->enabled == 1 && proc->pid) |
| 58 | enable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 61 | void 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 Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 65 | /* This should only happen on out-of-memory conditions. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 66 | if (sbp == NULL) |
| 67 | return; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 68 | |
| 69 | sbp->enabled--; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 70 | if (sbp->enabled == 0) |
| 71 | disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 72 | assert(sbp->enabled >= 0); |
| 73 | } |
| 74 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 75 | static void enable_bp_cb(void *addr, void *sbp, void *proc) |
| 76 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 77 | if (((struct breakpoint *)sbp)->enabled) { |
| 78 | enable_breakpoint(((struct process *)proc)->pid, sbp); |
| 79 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 82 | void enable_all_breakpoints(struct process *proc) |
| 83 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 84 | if (proc->breakpoints_enabled <= 0) { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 85 | #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 Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 93 | if (opt_L) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 94 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, |
| 95 | plt2addr(proc, |
| 96 | proc->list_of_symbols->enter_addr), |
| 97 | 0); |
| Juan Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 98 | if (a == 0x0) |
| 99 | return; |
| 100 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 101 | #endif |
| 102 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 103 | debug(1, "Enabling breakpoints for pid %u...", proc->pid); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 104 | if (proc->breakpoints) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 105 | dict_apply_to_all(proc->breakpoints, enable_bp_cb, |
| 106 | proc); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 107 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 108 | } |
| 109 | proc->breakpoints_enabled = 1; |
| 110 | } |
| 111 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 112 | static void disable_bp_cb(void *addr, void *sbp, void *proc) |
| 113 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 114 | if (((struct breakpoint *)sbp)->enabled) { |
| 115 | disable_breakpoint(((struct process *)proc)->pid, sbp); |
| 116 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 119 | void disable_all_breakpoints(struct process *proc) |
| 120 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 121 | if (proc->breakpoints_enabled) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 122 | debug(1, "Disabling breakpoints for pid %u...", proc->pid); |
| 123 | dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 124 | } |
| 125 | proc->breakpoints_enabled = 0; |
| 126 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 127 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 128 | static void free_bp_cb(void *addr, void *sbp, void *data) |
| 129 | { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 130 | assert(sbp); |
| 131 | free(sbp); |
| 132 | } |
| 133 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 134 | void breakpoints_init(struct process *proc) |
| 135 | { |
| 136 | struct library_symbol *sym; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 137 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 138 | if (proc->breakpoints) { /* let's remove that struct */ |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 139 | /* 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 Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 146 | proc->list_of_symbols = read_elf(proc); |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 147 | if (opt_e) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 148 | struct library_symbol **tmp1 = &(proc->list_of_symbols); |
| 149 | while (*tmp1) { |
| 150 | struct opt_e_t *tmp2 = opt_e; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 151 | int keep = !opt_e_enable; |
| 152 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 153 | while (tmp2) { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 154 | 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 | } |
| 169 | sym = proc->list_of_symbols; |
| 170 | while (sym) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 171 | /* proc->pid==0 delays enabling. */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 172 | if (sym->static_plt2addr) { |
| 173 | insert_breakpoint(proc, sym->enter_addr, sym); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 174 | } else { |
| 175 | insert_breakpoint(proc, plt2addr(proc, sym->enter_addr), sym); /* proc->pid==0 delays enabling. */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 176 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 177 | sym = sym->next; |
| 178 | } |
| 179 | proc->callstack_depth = 0; |
| 180 | proc->breakpoints_enabled = -1; |
| 181 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 182 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 183 | void reinitialize_breakpoints(struct process *proc) |
| 184 | { |
| 185 | struct library_symbol *sym = proc->list_of_symbols; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 186 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 187 | while (sym) { |
| 188 | if (sym->needs_init) { |
| 189 | insert_breakpoint(proc, plt2addr(proc, sym->enter_addr), |
| 190 | sym); |
| 191 | if (sym->needs_init && !sym->is_weak) { |
| 192 | fprintf(stderr, |
| 193 | "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n", |
| 194 | sym->name, proc->filename); |
| 195 | exit(1); |
| 196 | } |
| 197 | } |
| 198 | sym = sym->next; |
| 199 | } |
| 200 | } |