| 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 | 8cc1b9d | 2002-03-01 19:54:23 +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 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 26 | void |
| 27 | insert_breakpoint(struct process * proc, void * addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 28 | struct breakpoint * sbp; |
| 29 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 30 | if (!proc->breakpoints) { |
| 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 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 34 | sbp = dict_find_entry(proc->breakpoints, addr); |
| 35 | if (!sbp) { |
| 36 | sbp = malloc(sizeof(struct breakpoint)); |
| 37 | if (!sbp) { |
| 38 | return; /* TODO FIXME XXX: error_mem */ |
| 39 | } |
| 40 | dict_enter(proc->breakpoints, addr, sbp); |
| 41 | sbp->addr = addr; |
| 42 | sbp->enabled = 0; |
| 43 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 44 | sbp->enabled++; |
| 45 | if (sbp->enabled==1 && proc->pid) enable_breakpoint(proc->pid, sbp); |
| 46 | } |
| 47 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 48 | void |
| 49 | delete_breakpoint(struct process * proc, void * addr) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 50 | struct breakpoint * sbp = dict_find_entry(proc->breakpoints, addr); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 51 | assert(sbp); /* FIXME: remove after debugging has been done. */ |
| 52 | /* This should only happen on out-of-memory conditions. */ |
| 53 | if (sbp == NULL) return; |
| 54 | |
| 55 | sbp->enabled--; |
| 56 | if (sbp->enabled == 0) disable_breakpoint(proc->pid, sbp); |
| 57 | assert(sbp->enabled >= 0); |
| 58 | } |
| 59 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 60 | static void |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 61 | enable_bp_cb(void * addr, void * sbp, void * proc) { |
| 62 | if (((struct breakpoint *)sbp)->enabled) { |
| 63 | enable_breakpoint(((struct process *)proc)->pid, sbp); |
| 64 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 67 | void |
| 68 | enable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 69 | if (proc->breakpoints_enabled <= 0) { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 70 | #ifdef __powerpc__ |
| 71 | unsigned long a; |
| 72 | |
| 73 | /* |
| 74 | * PPC HACK! (XXX FIXME TODO) |
| 75 | * If the dynamic linker hasn't populated the PLT then |
| 76 | * dont enable the breakpoints |
| 77 | */ |
| Juan Cespedes | de5a7eb | 2002-03-31 20:53:52 +0200 | [diff] [blame] | 78 | if (opt_L) { |
| 79 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, proc->list_of_symbols->enter_addr, 0); |
| 80 | if (a == 0x0) |
| 81 | return; |
| 82 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 83 | #endif |
| 84 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 85 | debug(1, "Enabling breakpoints for pid %u...", proc->pid); |
| 86 | dict_apply_to_all(proc->breakpoints, enable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 87 | } |
| 88 | proc->breakpoints_enabled = 1; |
| 89 | } |
| 90 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 91 | static void |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 92 | disable_bp_cb(void * addr, void * sbp, void * proc) { |
| 93 | if (((struct breakpoint *)sbp)->enabled) { |
| 94 | disable_breakpoint(((struct process *)proc)->pid, sbp); |
| 95 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 98 | void |
| 99 | disable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 100 | if (proc->breakpoints_enabled) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 101 | debug(1, "Disabling breakpoints for pid %u...", proc->pid); |
| 102 | dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 103 | } |
| 104 | proc->breakpoints_enabled = 0; |
| 105 | } |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame^] | 106 | |
| 107 | static void |
| 108 | free_bp_cb(void * addr, void * sbp, void * data) { |
| 109 | assert(sbp); |
| 110 | free(sbp); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | breakpoints_init(struct process * proc) { |
| 115 | struct library_symbol * sym; |
| 116 | |
| 117 | if (proc->breakpoints) { /* let's remove that struct */ |
| 118 | /* TODO FIXME XXX: free() all "struct breakpoint"s */ |
| 119 | dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL); |
| 120 | dict_clear(proc->breakpoints); |
| 121 | proc->breakpoints = NULL; |
| 122 | } |
| 123 | |
| 124 | if (opt_L && proc->filename) { |
| 125 | proc->list_of_symbols = read_elf(proc->filename); |
| 126 | if (opt_e) { |
| 127 | struct library_symbol ** tmp1 = &(proc->list_of_symbols); |
| 128 | while(*tmp1) { |
| 129 | struct opt_e_t * tmp2 = opt_e; |
| 130 | int keep = !opt_e_enable; |
| 131 | |
| 132 | while(tmp2) { |
| 133 | if (!strcmp((*tmp1)->name, tmp2->name)) { |
| 134 | keep = opt_e_enable; |
| 135 | } |
| 136 | tmp2 = tmp2->next; |
| 137 | } |
| 138 | if (!keep) { |
| 139 | *tmp1 = (*tmp1)->next; |
| 140 | } else { |
| 141 | tmp1 = &((*tmp1)->next); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } else { |
| 146 | proc->list_of_symbols = NULL; |
| 147 | } |
| 148 | sym = proc->list_of_symbols; |
| 149 | while (sym) { |
| 150 | insert_breakpoint(proc, sym->enter_addr); /* proc->pid==0 delays enabling. */ |
| 151 | sym = sym->next; |
| 152 | } |
| 153 | proc->callstack_depth = 0; |
| 154 | proc->breakpoints_enabled = -1; |
| 155 | } |