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