2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: Richard P. Draves, Carnegie Mellon University
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/kernel.h>
44 #include <vm/vm_map.h>
46 #include <machine/kdb.h>
49 #include <ddb/db_watch.h>
55 static bool db_watchpoints_inserted = true;
57 #define NWATCHPOINTS 100
58 static struct db_watchpoint db_watch_table[NWATCHPOINTS];
59 static db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
60 static db_watchpoint_t db_free_watchpoints = 0;
61 static db_watchpoint_t db_watchpoint_list = 0;
63 static db_watchpoint_t db_watchpoint_alloc(void);
64 static void db_watchpoint_free(db_watchpoint_t watch);
65 static void db_delete_watchpoint(vm_map_t map, db_addr_t addr);
67 static bool db_find_watchpoint(vm_map_t map, db_addr_t addr,
70 static void db_list_watchpoints(void);
71 static void db_set_watchpoint(vm_map_t map, db_addr_t addr,
74 static db_watchpoint_t
75 db_watchpoint_alloc(void)
77 db_watchpoint_t watch;
79 if ((watch = db_free_watchpoints) != 0) {
80 db_free_watchpoints = watch->link;
83 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
84 db_printf("All watchpoints used.\n");
87 watch = db_next_free_watchpoint;
88 db_next_free_watchpoint++;
94 db_watchpoint_free(db_watchpoint_t watch)
96 watch->link = db_free_watchpoints;
97 db_free_watchpoints = watch;
101 db_set_watchpoint(vm_map_t map, db_addr_t addr, vm_size_t size)
103 db_watchpoint_t watch;
106 db_printf("No map.\n");
111 * Should we do anything fancy with overlapping regions?
114 for (watch = db_watchpoint_list;
117 if (db_map_equal(watch->map, map) &&
118 (watch->loaddr == addr) &&
119 (watch->hiaddr == addr+size)) {
120 db_printf("Already set.\n");
124 watch = db_watchpoint_alloc();
126 db_printf("Too many watchpoints.\n");
131 watch->loaddr = addr;
132 watch->hiaddr = addr+size;
134 watch->link = db_watchpoint_list;
135 db_watchpoint_list = watch;
137 db_watchpoints_inserted = false;
141 db_delete_watchpoint(vm_map_t map, db_addr_t addr)
143 db_watchpoint_t watch;
144 db_watchpoint_t *prev;
146 for (prev = &db_watchpoint_list;
147 (watch = *prev) != 0;
149 if (db_map_equal(watch->map, map) &&
150 (watch->loaddr <= addr) &&
151 (addr < watch->hiaddr)) {
153 db_watchpoint_free(watch);
157 db_printf("Not set.\n");
161 db_list_watchpoints(void)
163 db_watchpoint_t watch;
165 if (db_watchpoint_list == 0) {
166 db_printf("No watchpoints set\n");
171 db_printf(" Map Address Size\n");
173 db_printf(" Map Address Size\n");
175 for (watch = db_watchpoint_list;
179 db_printf("%s%16p %16lx %lx\n",
181 db_printf("%s%8p %8lx %lx\n",
183 db_map_current(watch->map) ? "*" : " ",
184 (void *)watch->map, (long)watch->loaddr,
185 (long)watch->hiaddr - (long)watch->loaddr);
188 /* Delete watchpoint */
191 db_deletewatch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
194 db_delete_watchpoint(db_map_addr(addr), addr);
200 db_watchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
206 if (db_expression(&value))
207 size = (vm_size_t) value;
212 db_set_watchpoint(db_map_addr(addr), addr, size);
216 * At least one non-optional show-command must be implemented using
217 * DB_SHOW_COMMAND() so that db_show_cmd_set gets created. Here is one.
219 DB_SHOW_COMMAND_FLAGS(watches, db_listwatch_cmd, DB_CMD_MEMSAFE)
221 db_list_watchpoints();
222 db_md_list_watchpoints();
226 db_set_watchpoints(void)
228 db_watchpoint_t watch;
230 if (!db_watchpoints_inserted) {
231 for (watch = db_watchpoint_list;
234 pmap_protect(watch->map->pmap,
235 trunc_page(watch->loaddr),
236 round_page(watch->hiaddr),
239 db_watchpoints_inserted = true;
244 db_clear_watchpoints(void)
246 db_watchpoints_inserted = false;
251 db_find_watchpoint(vm_map_t map, db_addr_t addr, db_regs_t regs)
253 db_watchpoint_t watch;
254 db_watchpoint_t found = 0;
256 for (watch = db_watchpoint_list;
259 if (db_map_equal(watch->map, map)) {
260 if ((watch->loaddr <= addr) &&
261 (addr < watch->hiaddr))
263 else if ((trunc_page(watch->loaddr) <= addr) &&
264 (addr < round_page(watch->hiaddr)))
269 * We didn't hit exactly on a watchpoint, but we are
270 * in a protected region. We want to single-step
271 * and then re-protect.
275 db_watchpoints_inserted = false;
276 db_single_step(regs);
283 /* Delete hardware watchpoint */
285 db_deletehwatch_cmd(db_expr_t addr, bool have_addr, db_expr_t size,
293 rc = kdb_cpu_clr_watchpoint((vm_offset_t)addr, (vm_size_t)size);
296 /* Not supported, ignored. */
299 db_printf("Invalid watchpoint address or size.\n");
303 db_printf("Hardware watchpoint could not be deleted, "
309 /* Set hardware watchpoint */
311 db_hwatchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t size,
319 rc = kdb_cpu_set_watchpoint((vm_offset_t)addr, (vm_size_t)size,
324 db_printf("Invalid watchpoint size or address.\n");
327 db_printf("No hardware watchpoints available.\n");
330 db_printf("Hardware watchpoints are not supported on this platform.\n");
334 db_printf("Could not set hardware watchpoint, "