1 /* $NetBSD: db_watch.c,v 1.17 2001/10/13 15:17:38 bjh21 Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 * 28 * Author: Richard P. Draves, Carnegie Mellon University 29 * Date: 10/90 30 */ 31 32 #include <sys/param.h> 33 #include <sys/proc.h> 34 35 #include <machine/db_machdep.h> 36 37 #include <ddb/db_break.h> 38 #include <ddb/db_watch.h> 39 #include <ddb/db_lex.h> 40 #include <ddb/db_access.h> 41 #include <ddb/db_run.h> 42 #include <ddb/db_sym.h> 43 #include <ddb/db_output.h> 44 #include <ddb/db_command.h> 45 #include <ddb/db_extern.h> 46 47 /* 48 * Watchpoints. 49 */ 50 51 boolean_t db_watchpoints_inserted = TRUE; 52 53 #define NWATCHPOINTS 100 54 struct db_watchpoint db_watch_table[NWATCHPOINTS]; 55 db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0]; 56 db_watchpoint_t db_free_watchpoints = 0; 57 db_watchpoint_t db_watchpoint_list = 0; 58 59 db_watchpoint_t 60 db_watchpoint_alloc() 61 { 62 db_watchpoint_t watch; 63 64 if ((watch = db_free_watchpoints) != 0) { 65 db_free_watchpoints = watch->link; 66 return (watch); 67 } 68 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) { 69 db_printf("All watchpoints used.\n"); 70 return (0); 71 } 72 watch = db_next_free_watchpoint; 73 db_next_free_watchpoint++; 74 75 return (watch); 76 } 77 78 void 79 db_watchpoint_free(watch) 80 db_watchpoint_t watch; 81 { 82 watch->link = db_free_watchpoints; 83 db_free_watchpoints = watch; 84 } 85 86 void 87 db_set_watchpoint(map, addr, size) 88 struct vm_map *map; 89 db_addr_t addr; 90 vsize_t size; 91 { 92 db_watchpoint_t watch; 93 94 if (map == NULL) { 95 db_printf("No map.\n"); 96 return; 97 } 98 99 /* 100 * Should we do anything fancy with overlapping regions? 101 */ 102 103 for (watch = db_watchpoint_list; 104 watch != 0; 105 watch = watch->link) 106 if (db_map_equal(watch->map, map) && 107 (watch->loaddr == addr) && 108 (watch->hiaddr == addr+size)) { 109 db_printf("Already set.\n"); 110 return; 111 } 112 113 watch = db_watchpoint_alloc(); 114 if (watch == 0) { 115 db_printf("Too many watchpoints.\n"); 116 return; 117 } 118 119 watch->map = map; 120 watch->loaddr = addr; 121 watch->hiaddr = addr+size; 122 123 watch->link = db_watchpoint_list; 124 db_watchpoint_list = watch; 125 126 db_watchpoints_inserted = FALSE; 127 } 128 129 void 130 db_delete_watchpoint(map, addr) 131 struct vm_map *map; 132 db_addr_t addr; 133 { 134 db_watchpoint_t watch; 135 db_watchpoint_t *prev; 136 137 for (prev = &db_watchpoint_list; 138 (watch = *prev) != 0; 139 prev = &watch->link) 140 if (db_map_equal(watch->map, map) && 141 (watch->loaddr <= addr) && 142 (addr < watch->hiaddr)) { 143 *prev = watch->link; 144 db_watchpoint_free(watch); 145 return; 146 } 147 148 db_printf("Not set.\n"); 149 } 150 151 void 152 db_list_watchpoints() 153 { 154 db_watchpoint_t watch; 155 156 if (db_watchpoint_list == 0) { 157 db_printf("No watchpoints set\n"); 158 return; 159 } 160 161 db_printf(" Map Address Size\n"); 162 for (watch = db_watchpoint_list; 163 watch != 0; 164 watch = watch->link) 165 db_printf("%s%p %8lx %lx\n", 166 db_map_current(watch->map) ? "*" : " ", 167 watch->map, watch->loaddr, 168 watch->hiaddr - watch->loaddr); 169 } 170 171 /* Delete watchpoint */ 172 /*ARGSUSED*/ 173 void 174 db_deletewatch_cmd(addr, have_addr, count, modif) 175 db_expr_t addr; 176 int have_addr; 177 db_expr_t count; 178 char * modif; 179 { 180 db_delete_watchpoint(db_map_addr(addr), addr); 181 } 182 183 /* Set watchpoint */ 184 /*ARGSUSED*/ 185 void 186 db_watchpoint_cmd(addr, have_addr, count, modif) 187 db_expr_t addr; 188 int have_addr; 189 db_expr_t count; 190 char * modif; 191 { 192 vsize_t size; 193 db_expr_t value; 194 195 if (db_expression(&value)) 196 size = (vsize_t) value; 197 else 198 size = 4; 199 db_skip_to_eol(); 200 201 db_set_watchpoint(db_map_addr(addr), addr, size); 202 } 203 204 /* list watchpoints */ 205 /*ARGSUSED*/ 206 void 207 db_listwatch_cmd(addr, have_addr, count, modif) 208 db_expr_t addr; 209 int have_addr; 210 db_expr_t count; 211 char * modif; 212 { 213 db_list_watchpoints(); 214 } 215 216 void 217 db_set_watchpoints() 218 { 219 db_watchpoint_t watch; 220 221 if (!db_watchpoints_inserted) { 222 for (watch = db_watchpoint_list; 223 watch != 0; 224 watch = watch->link) { 225 pmap_protect(watch->map->pmap, 226 trunc_page(watch->loaddr), 227 round_page(watch->hiaddr), 228 VM_PROT_READ); 229 pmap_update(watch->map->pmap); 230 } 231 232 db_watchpoints_inserted = TRUE; 233 } 234 } 235 236 void 237 db_clear_watchpoints() 238 { 239 db_watchpoints_inserted = FALSE; 240 } 241 242 boolean_t 243 db_find_watchpoint(map, addr, regs) 244 struct vm_map *map; 245 db_addr_t addr; 246 db_regs_t *regs; 247 { 248 db_watchpoint_t watch; 249 db_watchpoint_t found = 0; 250 251 for (watch = db_watchpoint_list; 252 watch != 0; 253 watch = watch->link) 254 if (db_map_equal(watch->map, map)) { 255 if ((watch->loaddr <= addr) && 256 (addr < watch->hiaddr)) 257 return (TRUE); 258 else if ((trunc_page(watch->loaddr) <= addr) && 259 (addr < round_page(watch->hiaddr))) 260 found = watch; 261 } 262 263 /* 264 * We didn't hit exactly on a watchpoint, but we are 265 * in a protected region. We want to single-step 266 * and then re-protect. 267 */ 268 269 if (found) { 270 db_watchpoints_inserted = FALSE; 271 db_single_step(regs); 272 } 273 274 return (FALSE); 275 } 276