| 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 | 9a2ad35 | 2006-02-20 22:44:45 +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 |
| 27 | insert_breakpoint(struct process * proc, void * addr, struct library_symbol * libsym) { |
| 28 | struct breakpoint * sbp; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 29 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 30 | if (!proc->breakpoints) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 31 | proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int); |
| 32 | /* atexit(brk_dict_clear); */ /* why bother to do this on exit? */ |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 33 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 34 | |
| 35 | if (!addr) |
| 36 | return; |
| 37 | |
| 38 | if (libsym) |
| 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) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 43 | sbp = calloc(1, sizeof(struct breakpoint)); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 44 | if (!sbp) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +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 | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 49 | sbp->libsym = libsym; |
| 50 | if (libsym) |
| 51 | libsym->brkpnt = sbp; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 52 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 53 | sbp->enabled++; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 54 | if (sbp->enabled==1 && proc->pid) enable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 57 | void |
| 58 | delete_breakpoint(struct process * proc, void * addr) { |
| 59 | struct breakpoint * sbp = dict_find_entry(proc->breakpoints, addr); |
| 60 | assert(sbp); /* FIXME: remove after debugging has been done. */ |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 61 | /* This should only happen on out-of-memory conditions. */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 62 | if (sbp == NULL) return; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 63 | |
| 64 | sbp->enabled--; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 65 | if (sbp->enabled == 0) disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 66 | assert(sbp->enabled >= 0); |
| 67 | } |
| 68 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 69 | static void |
| 70 | enable_bp_cb(void * addr, void * sbp, void * proc) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 71 | if (((struct breakpoint *)sbp)->enabled) { |
| 72 | enable_breakpoint(((struct process *)proc)->pid, sbp); |
| 73 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 76 | void |
| 77 | enable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 78 | if (proc->breakpoints_enabled <= 0) { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 79 | #ifdef __powerpc__ |
| 80 | unsigned long a; |
| 81 | |
| 82 | /* |
| 83 | * PPC HACK! (XXX FIXME TODO) |
| 84 | * If the dynamic linker hasn't populated the PLT then |
| 85 | * dont enable the breakpoints |
| 86 | */ |
| Juan Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 87 | if (opt_L) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 88 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, plt2addr(proc, proc->list_of_symbols->enter_addr), 0); |
| Juan Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 89 | if (a == 0x0) |
| 90 | return; |
| 91 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 94 | debug(1, "Enabling breakpoints for pid %u...", proc->pid); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 95 | if (proc->breakpoints) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 96 | dict_apply_to_all(proc->breakpoints, enable_bp_cb, proc); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 97 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 98 | } |
| 99 | proc->breakpoints_enabled = 1; |
| 100 | } |
| 101 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 102 | static void |
| 103 | disable_bp_cb(void * addr, void * sbp, void * proc) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 104 | if (((struct breakpoint *)sbp)->enabled) { |
| 105 | disable_breakpoint(((struct process *)proc)->pid, sbp); |
| 106 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 109 | void |
| 110 | disable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 111 | if (proc->breakpoints_enabled) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 112 | debug(1, "Disabling breakpoints for pid %u...", proc->pid); |
| 113 | dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 114 | } |
| 115 | proc->breakpoints_enabled = 0; |
| 116 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 117 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 118 | static void |
| 119 | free_bp_cb(void * addr, void * sbp, void * data) { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 120 | assert(sbp); |
| 121 | free(sbp); |
| 122 | } |
| 123 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 124 | void |
| 125 | breakpoints_init(struct process * proc) { |
| 126 | struct library_symbol * sym; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 127 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 128 | if (proc->breakpoints) { /* let's remove that struct */ |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 129 | /* TODO FIXME XXX: free() all "struct breakpoint"s */ |
| 130 | dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL); |
| 131 | dict_clear(proc->breakpoints); |
| 132 | proc->breakpoints = NULL; |
| 133 | } |
| 134 | |
| 135 | if (opt_L && proc->filename) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 136 | proc->list_of_symbols = read_elf(proc); |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 137 | if (opt_e) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 138 | struct library_symbol ** tmp1 = &(proc->list_of_symbols); |
| 139 | while(*tmp1) { |
| 140 | struct opt_e_t * tmp2 = opt_e; |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 141 | int keep = !opt_e_enable; |
| 142 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 143 | while(tmp2) { |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 144 | if (!strcmp((*tmp1)->name, tmp2->name)) { |
| 145 | keep = opt_e_enable; |
| 146 | } |
| 147 | tmp2 = tmp2->next; |
| 148 | } |
| 149 | if (!keep) { |
| 150 | *tmp1 = (*tmp1)->next; |
| 151 | } else { |
| 152 | tmp1 = &((*tmp1)->next); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | proc->list_of_symbols = NULL; |
| 158 | } |
| 159 | sym = proc->list_of_symbols; |
| 160 | while (sym) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 161 | /* proc->pid==0 delays enabling. */ |
| 162 | if (sym->static_plt2addr) { |
| 163 | insert_breakpoint(proc, sym->enter_addr, sym); |
| 164 | } else { |
| 165 | insert_breakpoint(proc, plt2addr(proc, sym->enter_addr), sym); /* proc->pid==0 delays enabling. */ |
| 166 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 167 | sym = sym->next; |
| 168 | } |
| 169 | proc->callstack_depth = 0; |
| 170 | proc->breakpoints_enabled = -1; |
| 171 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 172 | |
| 173 | void |
| 174 | reinitialize_breakpoints (struct process * proc) { |
| 175 | struct library_symbol * sym = proc->list_of_symbols; |
| 176 | |
| 177 | while (sym) { |
| 178 | if (sym->needs_init) { |
| 179 | insert_breakpoint(proc, plt2addr(proc, sym->enter_addr), sym); |
| 180 | if (sym->needs_init && !sym->is_weak) { |
| 181 | fprintf(stderr, "could not re-initialize breakpoint for \"%s\" in file \"%s\"\n", sym->name, proc->filename); |
| 182 | exit(1); |
| 183 | } |
| 184 | } |
| 185 | sym = sym->next; |
| 186 | } |
| 187 | } |
| 188 | |