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>
37 #include <sys/kernel.h>
43 #include <vm/vm_map.h>
46 #include <ddb/db_watch.h>
52 static bool db_watchpoints_inserted = true;
54 #define NWATCHPOINTS 100
55 static struct db_watchpoint db_watch_table[NWATCHPOINTS];
56 static db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
57 static db_watchpoint_t db_free_watchpoints = 0;
58 static db_watchpoint_t db_watchpoint_list = 0;
60 static db_watchpoint_t db_watchpoint_alloc(void);
61 static void db_watchpoint_free(db_watchpoint_t watch);
62 static void db_delete_watchpoint(vm_map_t map, db_addr_t addr);
64 static bool db_find_watchpoint(vm_map_t map, db_addr_t addr,
67 static void db_list_watchpoints(void);
68 static void db_set_watchpoint(vm_map_t map, db_addr_t addr,
71 static db_watchpoint_t
72 db_watchpoint_alloc(void)
74 db_watchpoint_t watch;
76 if ((watch = db_free_watchpoints) != 0) {
77 db_free_watchpoints = watch->link;
80 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
81 db_printf("All watchpoints used.\n");
84 watch = db_next_free_watchpoint;
85 db_next_free_watchpoint++;
91 db_watchpoint_free(db_watchpoint_t watch)
93 watch->link = db_free_watchpoints;
94 db_free_watchpoints = watch;
98 db_set_watchpoint(vm_map_t map, db_addr_t addr, vm_size_t size)
100 db_watchpoint_t watch;
103 db_printf("No map.\n");
108 * Should we do anything fancy with overlapping regions?
111 for (watch = db_watchpoint_list;
114 if (db_map_equal(watch->map, map) &&
115 (watch->loaddr == addr) &&
116 (watch->hiaddr == addr+size)) {
117 db_printf("Already set.\n");
121 watch = db_watchpoint_alloc();
123 db_printf("Too many watchpoints.\n");
128 watch->loaddr = addr;
129 watch->hiaddr = addr+size;
131 watch->link = db_watchpoint_list;
132 db_watchpoint_list = watch;
134 db_watchpoints_inserted = false;
138 db_delete_watchpoint(vm_map_t map, db_addr_t addr)
140 db_watchpoint_t watch;
141 db_watchpoint_t *prev;
143 for (prev = &db_watchpoint_list;
144 (watch = *prev) != 0;
146 if (db_map_equal(watch->map, map) &&
147 (watch->loaddr <= addr) &&
148 (addr < watch->hiaddr)) {
150 db_watchpoint_free(watch);
154 db_printf("Not set.\n");
158 db_list_watchpoints(void)
160 db_watchpoint_t watch;
162 if (db_watchpoint_list == 0) {
163 db_printf("No watchpoints set\n");
168 db_printf(" Map Address Size\n");
170 db_printf(" Map Address Size\n");
172 for (watch = db_watchpoint_list;
176 db_printf("%s%16p %16lx %lx\n",
178 db_printf("%s%8p %8lx %lx\n",
180 db_map_current(watch->map) ? "*" : " ",
181 (void *)watch->map, (long)watch->loaddr,
182 (long)watch->hiaddr - (long)watch->loaddr);
185 /* Delete watchpoint */
188 db_deletewatch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
191 db_delete_watchpoint(db_map_addr(addr), addr);
197 db_watchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
203 if (db_expression(&value))
204 size = (vm_size_t) value;
209 db_set_watchpoint(db_map_addr(addr), addr, size);
213 * At least one non-optional show-command must be implemented using
214 * DB_SHOW_COMMAND() so that db_show_cmd_set gets created. Here is one.
216 DB_SHOW_COMMAND(watches, db_listwatch_cmd)
218 db_list_watchpoints();
219 db_md_list_watchpoints();
223 db_set_watchpoints(void)
225 db_watchpoint_t watch;
227 if (!db_watchpoints_inserted) {
228 for (watch = db_watchpoint_list;
231 pmap_protect(watch->map->pmap,
232 trunc_page(watch->loaddr),
233 round_page(watch->hiaddr),
236 db_watchpoints_inserted = true;
241 db_clear_watchpoints(void)
243 db_watchpoints_inserted = false;
248 db_find_watchpoint(vm_map_t map, db_addr_t addr, db_regs_t regs)
250 db_watchpoint_t watch;
251 db_watchpoint_t found = 0;
253 for (watch = db_watchpoint_list;
256 if (db_map_equal(watch->map, map)) {
257 if ((watch->loaddr <= addr) &&
258 (addr < watch->hiaddr))
260 else if ((trunc_page(watch->loaddr) <= addr) &&
261 (addr < round_page(watch->hiaddr)))
266 * We didn't hit exactly on a watchpoint, but we are
267 * in a protected region. We want to single-step
268 * and then re-protect.
272 db_watchpoints_inserted = false;
273 db_single_step(regs);
282 /* Delete hardware watchpoint */
285 db_deletehwatch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
293 rc = db_md_clr_watchpoint(addr, count);
295 db_printf("hardware watchpoint could not be deleted\n");
298 /* Set hardware watchpoint */
301 db_hwatchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
309 rc = db_md_set_watchpoint(addr, count);
311 db_printf("hardware watchpoint could not be set\n");