| 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 | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #include "ltrace.h" |
| 6 | #include "options.h" |
| 7 | #include "output.h" |
| 8 | |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | #include <assert.h> |
| 11 | |
| 12 | /*****************************************************************************/ |
| 13 | |
| 14 | /* |
| 15 | Dictionary code done by Morten Eriksen <mortene@sim.no>. |
| 16 | |
| 17 | FIXME: should we merge with dictionary code in demangle.c? 19990704 mortene. |
| 18 | */ |
| 19 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 20 | struct dict_entry { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 21 | struct process * proc; |
| 22 | struct breakpoint brk; /* addr field of struct is the hash key. */ |
| 23 | struct dict_entry * next; |
| 24 | }; |
| 25 | |
| 26 | #define DICTTABLESIZE 997 /* Semi-randomly selected prime number. */ |
| 27 | static struct dict_entry * dict_buckets[DICTTABLESIZE]; |
| 28 | static int dict_initialized = 0; |
| 29 | |
| 30 | static void dict_init(void); |
| 31 | static void dict_clear(void); |
| 32 | static struct breakpoint * dict_enter(struct process * proc, void * brkaddr); |
| 33 | static struct breakpoint * dict_find_entry(struct process * proc, void * brkaddr); |
| 34 | static void dict_apply_to_all(void (* func)(struct process *, struct breakpoint *, void * data), void * data); |
| 35 | |
| 36 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 37 | static void |
| 38 | dict_init(void) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 39 | int i; |
| 40 | /* FIXME: is this necessary? Check with ANSI C spec. 19990702 mortene. */ |
| 41 | for (i = 0; i < DICTTABLESIZE; i++) dict_buckets[i] = NULL; |
| 42 | dict_initialized = 1; |
| 43 | } |
| 44 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 45 | static void |
| 46 | dict_clear(void) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 47 | int i; |
| 48 | struct dict_entry * entry, * nextentry; |
| 49 | |
| 50 | for (i = 0; i < DICTTABLESIZE; i++) { |
| 51 | for (entry = dict_buckets[i]; entry != NULL; entry = nextentry) { |
| 52 | nextentry = entry->next; |
| 53 | free(entry); |
| 54 | } |
| 55 | dict_buckets[i] = NULL; |
| 56 | } |
| 57 | } |
| 58 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 59 | static struct breakpoint * |
| 60 | dict_enter(struct process * proc, void * brkaddr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 61 | struct dict_entry * entry, * newentry; |
| 62 | unsigned int bucketpos = ((unsigned long int)brkaddr) % DICTTABLESIZE; |
| 63 | |
| 64 | newentry = malloc(sizeof(struct dict_entry)); |
| 65 | if (!newentry) { |
| 66 | perror("malloc"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | newentry->proc = proc; |
| 71 | newentry->brk.addr = brkaddr; |
| 72 | newentry->brk.enabled = 0; |
| 73 | newentry->next = NULL; |
| 74 | |
| 75 | entry = dict_buckets[bucketpos]; |
| 76 | while (entry && entry->next) entry = entry->next; |
| 77 | |
| 78 | if (entry) entry->next = newentry; |
| 79 | else dict_buckets[bucketpos] = newentry; |
| 80 | |
| 81 | if (opt_d > 2) |
| 82 | output_line(0, "new brk dict entry at %p\n", brkaddr); |
| 83 | |
| 84 | return &(newentry->brk); |
| 85 | } |
| 86 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 87 | static struct breakpoint * |
| 88 | dict_find_entry(struct process * proc, void * brkaddr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 89 | unsigned int bucketpos = ((unsigned long int)brkaddr) % DICTTABLESIZE; |
| 90 | struct dict_entry * entry = dict_buckets[bucketpos]; |
| 91 | while (entry) { |
| 92 | if ((entry->brk.addr == brkaddr) && (entry->proc == proc)) break; |
| 93 | entry = entry->next; |
| 94 | } |
| 95 | return entry ? &(entry->brk) : NULL; |
| 96 | } |
| 97 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 98 | static void |
| 99 | dict_apply_to_all(void (* func)(struct process *, struct breakpoint *, void * data), void * data) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < DICTTABLESIZE; i++) { |
| 103 | struct dict_entry * entry = dict_buckets[i]; |
| 104 | while (entry) { |
| 105 | func(entry->proc, &(entry->brk), data); |
| 106 | entry = entry->next; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #undef DICTTABLESIZE |
| 112 | |
| 113 | /*****************************************************************************/ |
| 114 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 115 | struct breakpoint * |
| 116 | address2bpstruct(struct process * proc, void * addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 117 | return dict_find_entry(proc, addr); |
| 118 | } |
| 119 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 120 | void |
| 121 | insert_breakpoint(struct process * proc, void * addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 122 | struct breakpoint * sbp; |
| 123 | |
| 124 | if (!dict_initialized) { |
| 125 | dict_init(); |
| 126 | atexit(dict_clear); |
| 127 | } |
| 128 | |
| 129 | sbp = dict_find_entry(proc, addr); |
| 130 | if (!sbp) sbp = dict_enter(proc, addr); |
| 131 | if (!sbp) return; |
| 132 | |
| 133 | sbp->enabled++; |
| 134 | if (sbp->enabled==1 && proc->pid) enable_breakpoint(proc->pid, sbp); |
| 135 | } |
| 136 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 137 | void |
| 138 | delete_breakpoint(struct process * proc, void * addr) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 139 | struct breakpoint * sbp = dict_find_entry(proc, addr); |
| 140 | assert(sbp); /* FIXME: remove after debugging has been done. */ |
| 141 | /* This should only happen on out-of-memory conditions. */ |
| 142 | if (sbp == NULL) return; |
| 143 | |
| 144 | sbp->enabled--; |
| 145 | if (sbp->enabled == 0) disable_breakpoint(proc->pid, sbp); |
| 146 | assert(sbp->enabled >= 0); |
| 147 | } |
| 148 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 149 | static void |
| 150 | enable_bp_cb(struct process * proc, struct breakpoint * sbp, void * data) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 151 | struct process * myproc = (struct process *)data; |
| 152 | if (myproc == proc && sbp->enabled) enable_breakpoint(proc->pid, sbp); |
| 153 | } |
| 154 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 155 | void |
| 156 | enable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 157 | if (proc->breakpoints_enabled <= 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 158 | if (opt_d>0) { |
| 159 | output_line(0, "Enabling breakpoints for pid %u...", proc->pid); |
| 160 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 161 | dict_apply_to_all(enable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 162 | } |
| 163 | proc->breakpoints_enabled = 1; |
| 164 | } |
| 165 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 166 | static void |
| 167 | disable_bp_cb(struct process * proc, struct breakpoint * sbp, void * data) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 168 | struct process * myproc = (struct process *)data; |
| 169 | if (myproc == proc && sbp->enabled) disable_breakpoint(proc->pid, sbp); |
| 170 | } |
| 171 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 172 | void |
| 173 | disable_all_breakpoints(struct process * proc) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 174 | if (proc->breakpoints_enabled) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 175 | if (opt_d>0) { |
| 176 | output_line(0, "Disabling breakpoints for pid %u...", proc->pid); |
| 177 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 178 | dict_apply_to_all(disable_bp_cb, proc); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 179 | } |
| 180 | proc->breakpoints_enabled = 0; |
| 181 | } |