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 #include <sys/param.h>
36 #include <sys/kernel.h>
42 #include <vm/vm_map.h>
44 #include <machine/kdb.h>
47 #include <ddb/db_watch.h>
53 static bool db_watchpoints_inserted = true;
55 #define NWATCHPOINTS 100
56 static struct db_watchpoint db_watch_table[NWATCHPOINTS];
57 static db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
58 static db_watchpoint_t db_free_watchpoints = 0;
59 static db_watchpoint_t db_watchpoint_list = 0;
61 static db_watchpoint_t db_watchpoint_alloc(void);
62 static void db_watchpoint_free(db_watchpoint_t watch);
63 static void db_delete_watchpoint(vm_map_t map, db_addr_t addr);
65 static bool db_find_watchpoint(vm_map_t map, db_addr_t addr,
68 static void db_list_watchpoints(void);
69 static void db_set_watchpoint(vm_map_t map, db_addr_t addr,
72 static db_watchpoint_t
73 db_watchpoint_alloc(void)
75 db_watchpoint_t watch;
77 if ((watch = db_free_watchpoints) != 0) {
78 db_free_watchpoints = watch->link;
81 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
82 db_printf("All watchpoints used.\n");
85 watch = db_next_free_watchpoint;
86 db_next_free_watchpoint++;
92 db_watchpoint_free(db_watchpoint_t watch)
94 watch->link = db_free_watchpoints;
95 db_free_watchpoints = watch;
99 db_set_watchpoint(vm_map_t map, db_addr_t addr, vm_size_t size)
101 db_watchpoint_t watch;
104 db_printf("No map.\n");
109 * Should we do anything fancy with overlapping regions?
112 for (watch = db_watchpoint_list;
115 if (db_map_equal(watch->map, map) &&
116 (watch->loaddr == addr) &&
117 (watch->hiaddr == addr+size)) {
118 db_printf("Already set.\n");
122 watch = db_watchpoint_alloc();
124 db_printf("Too many watchpoints.\n");
129 watch->loaddr = addr;
130 watch->hiaddr = addr+size;
132 watch->link = db_watchpoint_list;
133 db_watchpoint_list = watch;
135 db_watchpoints_inserted = false;
139 db_delete_watchpoint(vm_map_t map, db_addr_t addr)
141 db_watchpoint_t watch;
142 db_watchpoint_t *prev;
144 for (prev = &db_watchpoint_list;
145 (watch = *prev) != 0;
147 if (db_map_equal(watch->map, map) &&
148 (watch->loaddr <= addr) &&
149 (addr < watch->hiaddr)) {
151 db_watchpoint_free(watch);
155 db_printf("Not set.\n");
159 db_list_watchpoints(void)
161 db_watchpoint_t watch;
163 if (db_watchpoint_list == 0) {
164 db_printf("No watchpoints set\n");
169 db_printf(" Map Address Size\n");
171 db_printf(" Map Address Size\n");
173 for (watch = db_watchpoint_list;
177 db_printf("%s%16p %16lx %lx\n",
179 db_printf("%s%8p %8lx %lx\n",
181 db_map_current(watch->map) ? "*" : " ",
182 (void *)watch->map, (long)watch->loaddr,
183 (long)watch->hiaddr - (long)watch->loaddr);
186 /* Delete watchpoint */
189 db_deletewatch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
192 db_delete_watchpoint(db_map_addr(addr), addr);
198 db_watchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
204 if (db_expression(&value))
205 size = (vm_size_t) value;
210 db_set_watchpoint(db_map_addr(addr), addr, size);
214 * At least one non-optional show-command must be implemented using
215 * DB_SHOW_COMMAND() so that db_show_cmd_set gets created. Here is one.
217 DB_SHOW_COMMAND_FLAGS(watches, db_listwatch_cmd, DB_CMD_MEMSAFE)
219 db_list_watchpoints();
220 db_md_list_watchpoints();
224 db_set_watchpoints(void)
226 db_watchpoint_t watch;
228 if (!db_watchpoints_inserted) {
229 for (watch = db_watchpoint_list;
232 pmap_protect(watch->map->pmap,
233 trunc_page(watch->loaddr),
234 round_page(watch->hiaddr),
237 db_watchpoints_inserted = true;
242 db_clear_watchpoints(void)
244 db_watchpoints_inserted = false;
249 db_find_watchpoint(vm_map_t map, db_addr_t addr, db_regs_t regs)
251 db_watchpoint_t watch;
252 db_watchpoint_t found = 0;
254 for (watch = db_watchpoint_list;
257 if (db_map_equal(watch->map, map)) {
258 if ((watch->loaddr <= addr) &&
259 (addr < watch->hiaddr))
261 else if ((trunc_page(watch->loaddr) <= addr) &&
262 (addr < round_page(watch->hiaddr)))
267 * We didn't hit exactly on a watchpoint, but we are
268 * in a protected region. We want to single-step
269 * and then re-protect.
273 db_watchpoints_inserted = false;
274 db_single_step(regs);
281 /* Delete hardware watchpoint */
283 db_deletehwatch_cmd(db_expr_t addr, bool have_addr, db_expr_t size,
291 rc = kdb_cpu_clr_watchpoint((vm_offset_t)addr, (vm_size_t)size);
294 /* Not supported, ignored. */
297 db_printf("Invalid watchpoint address or size.\n");
301 db_printf("Hardware watchpoint could not be deleted, "
307 /* Set hardware watchpoint */
309 db_hwatchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t size,
317 rc = kdb_cpu_set_watchpoint((vm_offset_t)addr, (vm_size_t)size,
322 db_printf("Invalid watchpoint size or address.\n");
325 db_printf("No hardware watchpoints available.\n");
328 db_printf("Hardware watchpoints are not supported on this platform.\n");
332 db_printf("Could not set hardware watchpoint, "