1 /*- 2 * SPDX-License-Identifier: MIT-CMU 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 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 */ 29 /* 30 * Author: David B. Golub, Carnegie Mellon University 31 * Date: 7/90 32 */ 33 /* 34 * Breakpoints. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 42 #include <vm/vm.h> 43 #include <vm/vm_kern.h> 44 45 #include <ddb/ddb.h> 46 #include <ddb/db_break.h> 47 #include <ddb/db_access.h> 48 #include <ddb/db_sym.h> 49 50 #define NBREAKPOINTS 100 51 static struct db_breakpoint db_break_table[NBREAKPOINTS]; 52 static db_breakpoint_t db_next_free_breakpoint = &db_break_table[0]; 53 static db_breakpoint_t db_free_breakpoints = 0; 54 static db_breakpoint_t db_breakpoint_list = 0; 55 56 static db_breakpoint_t db_breakpoint_alloc(void); 57 static void db_breakpoint_free(db_breakpoint_t bkpt); 58 static void db_delete_breakpoint(vm_map_t map, db_addr_t addr); 59 static db_breakpoint_t db_find_breakpoint(vm_map_t map, db_addr_t addr); 60 static void db_list_breakpoints(void); 61 static void db_set_breakpoint(vm_map_t map, db_addr_t addr, int count); 62 63 static db_breakpoint_t 64 db_breakpoint_alloc(void) 65 { 66 register db_breakpoint_t bkpt; 67 68 if ((bkpt = db_free_breakpoints) != 0) { 69 db_free_breakpoints = bkpt->link; 70 return (bkpt); 71 } 72 if (db_next_free_breakpoint == &db_break_table[NBREAKPOINTS]) { 73 db_printf("All breakpoints used.\n"); 74 return (0); 75 } 76 bkpt = db_next_free_breakpoint; 77 db_next_free_breakpoint++; 78 79 return (bkpt); 80 } 81 82 static void 83 db_breakpoint_free(db_breakpoint_t bkpt) 84 { 85 bkpt->link = db_free_breakpoints; 86 db_free_breakpoints = bkpt; 87 } 88 89 static void 90 db_set_breakpoint(vm_map_t map, db_addr_t addr, int count) 91 { 92 register db_breakpoint_t bkpt; 93 94 if (db_find_breakpoint(map, addr)) { 95 db_printf("Already set.\n"); 96 return; 97 } 98 99 bkpt = db_breakpoint_alloc(); 100 if (bkpt == 0) { 101 db_printf("Too many breakpoints.\n"); 102 return; 103 } 104 105 bkpt->map = map; 106 bkpt->address = addr; 107 bkpt->flags = 0; 108 bkpt->init_count = count; 109 bkpt->count = count; 110 111 bkpt->link = db_breakpoint_list; 112 db_breakpoint_list = bkpt; 113 } 114 115 static void 116 db_delete_breakpoint(vm_map_t map, db_addr_t addr) 117 { 118 register db_breakpoint_t bkpt; 119 register db_breakpoint_t *prev; 120 121 for (prev = &db_breakpoint_list; 122 (bkpt = *prev) != 0; 123 prev = &bkpt->link) { 124 if (db_map_equal(bkpt->map, map) && 125 (bkpt->address == addr)) { 126 *prev = bkpt->link; 127 break; 128 } 129 } 130 if (bkpt == 0) { 131 db_printf("Not set.\n"); 132 return; 133 } 134 135 db_breakpoint_free(bkpt); 136 } 137 138 static db_breakpoint_t 139 db_find_breakpoint(vm_map_t map, db_addr_t addr) 140 { 141 register db_breakpoint_t bkpt; 142 143 for (bkpt = db_breakpoint_list; 144 bkpt != 0; 145 bkpt = bkpt->link) 146 { 147 if (db_map_equal(bkpt->map, map) && 148 (bkpt->address == addr)) 149 return (bkpt); 150 } 151 return (0); 152 } 153 154 db_breakpoint_t 155 db_find_breakpoint_here(db_addr_t addr) 156 { 157 return db_find_breakpoint(db_map_addr(addr), addr); 158 } 159 160 static bool db_breakpoints_inserted = true; 161 162 #ifndef BKPT_WRITE 163 #define BKPT_WRITE(addr, storage) \ 164 do { \ 165 *storage = db_get_value(addr, BKPT_SIZE, false); \ 166 db_put_value(addr, BKPT_SIZE, BKPT_SET(*storage)); \ 167 } while (0) 168 #endif 169 170 #ifndef BKPT_CLEAR 171 #define BKPT_CLEAR(addr, storage) \ 172 db_put_value(addr, BKPT_SIZE, *storage) 173 #endif 174 175 void 176 db_set_breakpoints(void) 177 { 178 register db_breakpoint_t bkpt; 179 180 if (!db_breakpoints_inserted) { 181 for (bkpt = db_breakpoint_list; 182 bkpt != 0; 183 bkpt = bkpt->link) 184 if (db_map_current(bkpt->map)) { 185 BKPT_WRITE(bkpt->address, &bkpt->bkpt_inst); 186 } 187 db_breakpoints_inserted = true; 188 } 189 } 190 191 void 192 db_clear_breakpoints(void) 193 { 194 register db_breakpoint_t bkpt; 195 196 if (db_breakpoints_inserted) { 197 for (bkpt = db_breakpoint_list; 198 bkpt != 0; 199 bkpt = bkpt->link) 200 if (db_map_current(bkpt->map)) { 201 BKPT_CLEAR(bkpt->address, &bkpt->bkpt_inst); 202 } 203 db_breakpoints_inserted = false; 204 } 205 } 206 207 #ifdef SOFTWARE_SSTEP 208 /* 209 * Set a temporary breakpoint. 210 * The instruction is changed immediately, 211 * so the breakpoint does not have to be on the breakpoint list. 212 */ 213 db_breakpoint_t 214 db_set_temp_breakpoint(db_addr_t addr) 215 { 216 register db_breakpoint_t bkpt; 217 218 bkpt = db_breakpoint_alloc(); 219 if (bkpt == 0) { 220 db_printf("Too many breakpoints.\n"); 221 return 0; 222 } 223 224 bkpt->map = NULL; 225 bkpt->address = addr; 226 bkpt->flags = BKPT_TEMP; 227 bkpt->init_count = 1; 228 bkpt->count = 1; 229 230 BKPT_WRITE(bkpt->address, &bkpt->bkpt_inst); 231 return bkpt; 232 } 233 234 void 235 db_delete_temp_breakpoint(db_breakpoint_t bkpt) 236 { 237 BKPT_CLEAR(bkpt->address, &bkpt->bkpt_inst); 238 db_breakpoint_free(bkpt); 239 } 240 #endif /* SOFTWARE_SSTEP */ 241 242 /* 243 * List breakpoints. 244 */ 245 static void 246 db_list_breakpoints(void) 247 { 248 register db_breakpoint_t bkpt; 249 250 if (db_breakpoint_list == 0) { 251 db_printf("No breakpoints set\n"); 252 return; 253 } 254 255 db_printf(" Map Count Address\n"); 256 for (bkpt = db_breakpoint_list; 257 bkpt != 0; 258 bkpt = bkpt->link) { 259 db_printf("%s%8p %5d ", 260 db_map_current(bkpt->map) ? "*" : " ", 261 (void *)bkpt->map, bkpt->init_count); 262 db_printsym(bkpt->address, DB_STGY_PROC); 263 db_printf("\n"); 264 } 265 } 266 267 /* Delete breakpoint */ 268 /*ARGSUSED*/ 269 void 270 db_delete_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 271 { 272 db_delete_breakpoint(db_map_addr(addr), (db_addr_t)addr); 273 } 274 275 /* Set breakpoint with skip count */ 276 /*ARGSUSED*/ 277 void 278 db_breakpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 279 { 280 if (count == -1) 281 count = 1; 282 283 db_set_breakpoint(db_map_addr(addr), (db_addr_t)addr, count); 284 } 285 286 /* list breakpoints */ 287 void 288 db_listbreak_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 289 { 290 db_list_breakpoints(); 291 } 292 293 /* 294 * We want ddb to be usable before most of the kernel has been 295 * initialized. In particular, current_thread() or kernel_map 296 * (or both) may be null. 297 */ 298 299 bool 300 db_map_equal(vm_map_t map1, vm_map_t map2) 301 { 302 return ((map1 == map2) || 303 ((map1 == NULL) && (map2 == kernel_map)) || 304 ((map1 == kernel_map) && (map2 == NULL))); 305 } 306 307 bool 308 db_map_current(vm_map_t map) 309 { 310 #if 0 311 thread_t thread; 312 313 return ((map == NULL) || 314 (map == kernel_map) || 315 (((thread = current_thread()) != NULL) && 316 (map == thread->task->map))); 317 #else 318 return (true); 319 #endif 320 } 321 322 vm_map_t 323 db_map_addr(vm_offset_t addr) 324 { 325 #if 0 326 thread_t thread; 327 328 /* 329 * We want to return kernel_map for all 330 * non-user addresses, even when debugging 331 * kernel tasks with their own maps. 332 */ 333 334 if ((VM_MIN_ADDRESS <= addr) && 335 (addr < VM_MAX_ADDRESS) && 336 ((thread = current_thread()) != NULL)) 337 return thread->task->map; 338 else 339 #endif 340 return kernel_map; 341 } 342