2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 * Author: Richard P. Draves, Carnegie Mellon University
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
41 #include <vm/vm_map.h>
44 #include <ddb/db_watch.h>
50 static boolean_t db_watchpoints_inserted = TRUE;
52 #define NWATCHPOINTS 100
53 static struct db_watchpoint db_watch_table[NWATCHPOINTS];
54 static db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
55 static db_watchpoint_t db_free_watchpoints = 0;
56 static db_watchpoint_t db_watchpoint_list = 0;
58 static db_watchpoint_t db_watchpoint_alloc(void);
59 static void db_watchpoint_free(db_watchpoint_t watch);
60 static void db_delete_watchpoint(vm_map_t map, db_addr_t addr);
62 static boolean_t db_find_watchpoint(vm_map_t map, db_addr_t addr,
65 static void db_list_watchpoints(void);
66 static void db_set_watchpoint(vm_map_t map, db_addr_t addr,
69 static db_watchpoint_t
72 register db_watchpoint_t watch;
74 if ((watch = db_free_watchpoints) != 0) {
75 db_free_watchpoints = watch->link;
78 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
79 db_printf("All watchpoints used.\n");
82 watch = db_next_free_watchpoint;
83 db_next_free_watchpoint++;
89 db_watchpoint_free(watch)
90 register db_watchpoint_t watch;
92 watch->link = db_free_watchpoints;
93 db_free_watchpoints = watch;
97 db_set_watchpoint(map, addr, size)
102 register db_watchpoint_t watch;
105 db_printf("No map.\n");
110 * Should we do anything fancy with overlapping regions?
113 for (watch = db_watchpoint_list;
116 if (db_map_equal(watch->map, map) &&
117 (watch->loaddr == addr) &&
118 (watch->hiaddr == addr+size)) {
119 db_printf("Already set.\n");
123 watch = db_watchpoint_alloc();
125 db_printf("Too many watchpoints.\n");
130 watch->loaddr = addr;
131 watch->hiaddr = addr+size;
133 watch->link = db_watchpoint_list;
134 db_watchpoint_list = watch;
136 db_watchpoints_inserted = FALSE;
140 db_delete_watchpoint(map, addr)
144 register db_watchpoint_t watch;
145 register db_watchpoint_t *prev;
147 for (prev = &db_watchpoint_list;
148 (watch = *prev) != 0;
150 if (db_map_equal(watch->map, map) &&
151 (watch->loaddr <= addr) &&
152 (addr < watch->hiaddr)) {
154 db_watchpoint_free(watch);
158 db_printf("Not set.\n");
162 db_list_watchpoints()
164 register db_watchpoint_t watch;
166 if (db_watchpoint_list == 0) {
167 db_printf("No watchpoints set\n");
172 db_printf(" Map Address Size\n");
174 db_printf(" Map Address Size\n");
176 for (watch = db_watchpoint_list;
180 db_printf("%s%16p %16lx %lx\n",
182 db_printf("%s%8p %8lx %lx\n",
184 db_map_current(watch->map) ? "*" : " ",
185 (void *)watch->map, (long)watch->loaddr,
186 (long)watch->hiaddr - (long)watch->loaddr);
189 /* Delete watchpoint */
192 db_deletewatch_cmd(addr, have_addr, count, modif)
198 db_delete_watchpoint(db_map_addr(addr), addr);
204 db_watchpoint_cmd(addr, have_addr, count, modif)
213 if (db_expression(&value))
214 size = (vm_size_t) value;
219 db_set_watchpoint(db_map_addr(addr), addr, size);
223 * At least one non-optional show-command must be implemented using
224 * DB_SHOW_COMMAND() so that db_show_cmd_set gets created. Here is one.
226 DB_SHOW_COMMAND(watches, db_listwatch_cmd)
228 db_list_watchpoints();
229 db_md_list_watchpoints();
235 register db_watchpoint_t watch;
237 if (!db_watchpoints_inserted) {
238 for (watch = db_watchpoint_list;
241 pmap_protect(watch->map->pmap,
242 trunc_page(watch->loaddr),
243 round_page(watch->hiaddr),
246 db_watchpoints_inserted = TRUE;
251 db_clear_watchpoints()
253 db_watchpoints_inserted = FALSE;
258 db_find_watchpoint(map, addr, regs)
263 register db_watchpoint_t watch;
264 db_watchpoint_t found = 0;
266 for (watch = db_watchpoint_list;
269 if (db_map_equal(watch->map, map)) {
270 if ((watch->loaddr <= addr) &&
271 (addr < watch->hiaddr))
273 else if ((trunc_page(watch->loaddr) <= addr) &&
274 (addr < round_page(watch->hiaddr)))
279 * We didn't hit exactly on a watchpoint, but we are
280 * in a protected region. We want to single-step
281 * and then re-protect.
285 db_watchpoints_inserted = FALSE;
286 db_single_step(regs);
295 /* Delete hardware watchpoint */
298 db_deletehwatch_cmd(addr, have_addr, count, modif)
309 rc = db_md_clr_watchpoint(addr, count);
311 db_printf("hardware watchpoint could not be deleted\n");
314 /* Set hardware watchpoint */
317 db_hwatchpoint_cmd(addr, have_addr, count, modif)
328 rc = db_md_set_watchpoint(addr, count);
330 db_printf("hardware watchpoint could not be set\n");