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.
26 * $FreeBSD: src/sys/ddb/db_watch.c,v 1.20.2.1 2001/07/25 01:00:08 bsd Exp $
27 * $DragonFly: src/sys/ddb/db_watch.c,v 1.4 2003/08/27 10:47:13 rob Exp $
31 * Author: Richard P. Draves, Carnegie Mellon University
35 #include <sys/param.h>
36 #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,
63 static boolean_t db_find_watchpoint (vm_map_t map, db_addr_t addr,
66 static void db_list_watchpoints (void);
67 static void db_set_watchpoint (vm_map_t map, db_addr_t addr,
70 int db_md_set_watchpoint (db_expr_t addr, db_expr_t size);
71 int db_md_clr_watchpoint (db_expr_t addr, db_expr_t size);
72 void db_md_list_watchpoints (void);
78 db_watchpoint_t watch;
80 if ((watch = db_free_watchpoints) != 0) {
81 db_free_watchpoints = watch->link;
84 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
85 db_printf("All watchpoints used.\n");
88 watch = db_next_free_watchpoint;
89 db_next_free_watchpoint++;
95 db_watchpoint_free(watch)
96 db_watchpoint_t watch;
98 watch->link = db_free_watchpoints;
99 db_free_watchpoints = watch;
103 db_set_watchpoint(map, addr, size)
108 db_watchpoint_t watch;
111 db_printf("No map.\n");
116 * Should we do anything fancy with overlapping regions?
119 for (watch = db_watchpoint_list;
122 if (db_map_equal(watch->map, map) &&
123 (watch->loaddr == addr) &&
124 (watch->hiaddr == addr+size)) {
125 db_printf("Already set.\n");
129 watch = db_watchpoint_alloc();
131 db_printf("Too many watchpoints.\n");
136 watch->loaddr = addr;
137 watch->hiaddr = addr+size;
139 watch->link = db_watchpoint_list;
140 db_watchpoint_list = watch;
142 db_watchpoints_inserted = FALSE;
146 db_delete_watchpoint(map, addr)
150 db_watchpoint_t watch;
151 db_watchpoint_t *prev;
153 for (prev = &db_watchpoint_list;
154 (watch = *prev) != 0;
156 if (db_map_equal(watch->map, map) &&
157 (watch->loaddr <= addr) &&
158 (addr < watch->hiaddr)) {
160 db_watchpoint_free(watch);
164 db_printf("Not set.\n");
168 db_list_watchpoints()
170 db_watchpoint_t watch;
172 if (db_watchpoint_list == 0) {
173 db_printf("No watchpoints set\n");
177 db_printf(" Map Address Size\n");
178 for (watch = db_watchpoint_list;
181 db_printf("%s%8p %8lx %lx\n",
182 db_map_current(watch->map) ? "*" : " ",
183 (void *)watch->map, (long)watch->loaddr,
184 (long)watch->hiaddr - (long)watch->loaddr);
187 /* Delete watchpoint */
190 db_deletewatch_cmd(addr, have_addr, count, modif)
196 db_delete_watchpoint(db_map_addr(addr), addr);
202 db_watchpoint_cmd(addr, have_addr, count, modif)
211 if (db_expression(&value))
212 size = (vm_size_t) value;
217 db_set_watchpoint(db_map_addr(addr), addr, size);
221 * At least one non-optional show-command must be implemented using
222 * DB_SHOW_COMMAND() so that db_show_cmd_set gets created. Here is one.
224 DB_SHOW_COMMAND(watches, db_listwatch_cmd)
226 db_list_watchpoints();
227 db_md_list_watchpoints();
233 db_watchpoint_t watch;
235 if (!db_watchpoints_inserted) {
236 for (watch = db_watchpoint_list;
239 pmap_protect(watch->map->pmap,
240 trunc_page(watch->loaddr),
241 round_page(watch->hiaddr),
244 db_watchpoints_inserted = TRUE;
249 db_clear_watchpoints()
251 db_watchpoints_inserted = FALSE;
256 db_find_watchpoint(map, addr, regs)
261 db_watchpoint_t watch;
262 db_watchpoint_t found = 0;
264 for (watch = db_watchpoint_list;
267 if (db_map_equal(watch->map, map)) {
268 if ((watch->loaddr <= addr) &&
269 (addr < watch->hiaddr))
271 else if ((trunc_page(watch->loaddr) <= addr) &&
272 (addr < round_page(watch->hiaddr)))
277 * We didn't hit exactly on a watchpoint, but we are
278 * in a protected region. We want to single-step
279 * and then re-protect.
283 db_watchpoints_inserted = FALSE;
284 db_single_step(regs);
293 /* Delete hardware watchpoint */
296 db_deletehwatch_cmd(addr, have_addr, count, modif)
307 rc = db_md_clr_watchpoint(addr, count);
309 db_printf("hardware watchpoint could not be deleted\n");
312 /* Set hardware watchpoint */
315 db_hwatchpoint_cmd(addr, have_addr, count, modif)
326 rc = db_md_set_watchpoint(addr, count);
328 db_printf("hardware watchpoint could not be set\n");