| 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 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 21 | struct breakpoint * |
| 22 | address2bpstruct(struct process *proc, void *addr) { |
| 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, |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 28 | struct library_symbol *libsym) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | struct breakpoint *sbp; |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 30 | debug(1, "symbol=%s, addr=%p", libsym?libsym->name:"(nil)", addr); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 31 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 32 | if (!addr) |
| 33 | return; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 34 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 35 | if (libsym) |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 36 | libsym->needs_init = 0; |
| 37 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 38 | sbp = dict_find_entry(proc->breakpoints, addr); |
| 39 | if (!sbp) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 40 | sbp = calloc(1, sizeof(struct breakpoint)); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 41 | if (!sbp) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | return; /* TODO FIXME XXX: error_mem */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 43 | } |
| 44 | dict_enter(proc->breakpoints, addr, sbp); |
| 45 | sbp->addr = addr; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 46 | sbp->libsym = libsym; |
| 47 | if (libsym) |
| 48 | libsym->brkpnt = sbp; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 49 | } |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 50 | #ifdef __arm__ |
| 51 | sbp->thumb_mode = proc->thumb_mode; |
| 52 | proc->thumb_mode = 0; |
| 53 | #endif |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 54 | sbp->enabled++; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 55 | if (sbp->enabled == 1 && proc->pid) |
| 56 | enable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 59 | void |
| 60 | delete_breakpoint(struct process *proc, void *addr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 61 | struct breakpoint *sbp = dict_find_entry(proc->breakpoints, addr); |
| 62 | assert(sbp); /* FIXME: remove after debugging has been done. */ |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 63 | /* This should only happen on out-of-memory conditions. */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 64 | if (sbp == NULL) |
| 65 | return; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 66 | |
| 67 | sbp->enabled--; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 68 | if (sbp->enabled == 0) |
| 69 | disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 70 | assert(sbp->enabled >= 0); |
| 71 | } |
| 72 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 73 | static void |
| 74 | enable_bp_cb(void *addr, void *sbp, void *proc) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 75 | if (((struct breakpoint *)sbp)->enabled) { |
| 76 | enable_breakpoint(((struct process *)proc)->pid, sbp); |
| 77 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 80 | void |
| 81 | enable_all_breakpoints(struct process *proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 82 | if (proc->breakpoints_enabled <= 0) { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 83 | #ifdef __powerpc__ |
| 84 | unsigned long a; |
| 85 | |
| 86 | /* |
| 87 | * PPC HACK! (XXX FIXME TODO) |
| 88 | * If the dynamic linker hasn't populated the PLT then |
| 89 | * dont enable the breakpoints |
| 90 | */ |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame^] | 91 | if (options.libcalls) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 92 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 93 | sym2addr(proc, proc->list_of_symbols), |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 94 | 0); |
| Juan Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 95 | if (a == 0x0) |
| 96 | return; |
| 97 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 98 | #endif |
| 99 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 100 | debug(1, "Enabling breakpoints for pid %u...", proc->pid); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 101 | if (proc->breakpoints) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 102 | dict_apply_to_all(proc->breakpoints, enable_bp_cb, |
| 103 | proc); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 104 | } |
| Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 105 | #ifdef __mips__ |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 106 | { |
| 107 | // I'm sure there is a nicer way to do this. We need to |
| 108 | // insert breakpoints _after_ the child has been started. |
| 109 | struct library_symbol *sym; |
| 110 | struct library_symbol *new_sym; |
| 111 | sym=proc->list_of_symbols; |
| 112 | while(sym){ |
| 113 | void *addr= sym2addr(proc,sym); |
| 114 | if(!addr){ |
| 115 | sym=sym->next; |
| 116 | continue; |
| 117 | } |
| 118 | if(dict_find_entry(proc->breakpoints,addr)){ |
| 119 | sym=sym->next; |
| 120 | continue; |
| 121 | } |
| 122 | debug(2,"inserting bp %p %s",addr,sym->name); |
| 123 | new_sym=malloc(sizeof(*new_sym)); |
| 124 | memcpy(new_sym,sym,sizeof(*new_sym)); |
| 125 | new_sym->next=proc->list_of_symbols; |
| 126 | proc->list_of_symbols=new_sym; |
| 127 | new_sym->brkpnt=0; |
| 128 | insert_breakpoint(proc, addr, new_sym); |
| 129 | sym=sym->next; |
| 130 | } |
| 131 | } |
| Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 132 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 133 | } |
| 134 | proc->breakpoints_enabled = 1; |
| 135 | } |
| 136 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 137 | static void |
| 138 | disable_bp_cb(void *addr, void *sbp, void *proc) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 139 | if (((struct breakpoint *)sbp)->enabled) { |
| 140 | disable_breakpoint(((struct process *)proc)->pid, sbp); |
| 141 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 144 | void |
| 145 | disable_all_breakpoints(struct process *proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 146 | if (proc->breakpoints_enabled) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 147 | debug(1, "Disabling breakpoints for pid %u...", proc->pid); |
| 148 | dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 149 | } |
| 150 | proc->breakpoints_enabled = 0; |
| 151 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 152 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 153 | static void |
| 154 | free_bp_cb(void *addr, void *sbp, void *data) { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 155 | assert(sbp); |
| 156 | free(sbp); |
| 157 | } |
| 158 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 159 | void |
| 160 | breakpoints_init(struct process *proc) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 161 | struct library_symbol *sym; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 162 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 163 | if (proc->breakpoints) { /* let's remove that struct */ |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 164 | dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL); |
| 165 | dict_clear(proc->breakpoints); |
| 166 | proc->breakpoints = NULL; |
| 167 | } |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 168 | proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int); |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 169 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame^] | 170 | if (options.libcalls && proc->filename) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 171 | proc->list_of_symbols = read_elf(proc); |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 172 | if (opt_e) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 173 | struct library_symbol **tmp1 = &(proc->list_of_symbols); |
| 174 | while (*tmp1) { |
| 175 | struct opt_e_t *tmp2 = opt_e; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 176 | int keep = !opt_e_enable; |
| 177 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 178 | while (tmp2) { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 179 | if (!strcmp((*tmp1)->name, tmp2->name)) { |
| 180 | keep = opt_e_enable; |
| 181 | } |
| 182 | tmp2 = tmp2->next; |
| 183 | } |
| 184 | if (!keep) { |
| 185 | *tmp1 = (*tmp1)->next; |
| 186 | } else { |
| 187 | tmp1 = &((*tmp1)->next); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } else { |
| 192 | proc->list_of_symbols = NULL; |
| 193 | } |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 194 | for (sym = proc->list_of_symbols; sym; sym = sym->next) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 195 | /* proc->pid==0 delays enabling. */ |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 196 | insert_breakpoint(proc, sym2addr(proc, sym), sym); |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 197 | } |
| 198 | proc->callstack_depth = 0; |
| 199 | proc->breakpoints_enabled = -1; |
| 200 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 201 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 202 | void |
| 203 | reinitialize_breakpoints(struct process *proc) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 204 | struct library_symbol *sym = proc->list_of_symbols; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 205 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 206 | while (sym) { |
| 207 | if (sym->needs_init) { |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 208 | insert_breakpoint(proc, sym2addr(proc, sym), |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 209 | sym); |
| 210 | if (sym->needs_init && !sym->is_weak) { |
| 211 | fprintf(stderr, |
| 212 | "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n", |
| 213 | sym->name, proc->filename); |
| 214 | exit(1); |
| 215 | } |
| 216 | } |
| 217 | sym = sym->next; |
| 218 | } |
| 219 | } |