db_watch.c revision 1.13 1 /* $NetBSD: db_watch.c,v 1.13 2000/03/30 11:31:27 augustss Exp $ */
2
3 /*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
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.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 *
28 * Author: Richard P. Draves, Carnegie Mellon University
29 * Date: 10/90
30 */
31
32 #include <sys/param.h>
33 #include <sys/proc.h>
34
35 #include <machine/db_machdep.h>
36
37 #include <ddb/db_break.h>
38 #include <ddb/db_watch.h>
39 #include <ddb/db_lex.h>
40 #include <ddb/db_access.h>
41 #include <ddb/db_run.h>
42 #include <ddb/db_sym.h>
43 #include <ddb/db_output.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_extern.h>
46
47 /*
48 * Watchpoints.
49 */
50
51 boolean_t db_watchpoints_inserted = TRUE;
52
53 #define NWATCHPOINTS 100
54 struct db_watchpoint db_watch_table[NWATCHPOINTS];
55 db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
56 db_watchpoint_t db_free_watchpoints = 0;
57 db_watchpoint_t db_watchpoint_list = 0;
58
59 db_watchpoint_t
60 db_watchpoint_alloc()
61 {
62 db_watchpoint_t watch;
63
64 if ((watch = db_free_watchpoints) != 0) {
65 db_free_watchpoints = watch->link;
66 return (watch);
67 }
68 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
69 db_printf("All watchpoints used.\n");
70 return (0);
71 }
72 watch = db_next_free_watchpoint;
73 db_next_free_watchpoint++;
74
75 return (watch);
76 }
77
78 void
79 db_watchpoint_free(watch)
80 db_watchpoint_t watch;
81 {
82 watch->link = db_free_watchpoints;
83 db_free_watchpoints = watch;
84 }
85
86 void
87 db_set_watchpoint(map, addr, size)
88 vm_map_t map;
89 db_addr_t addr;
90 vsize_t size;
91 {
92 db_watchpoint_t watch;
93
94 if (map == NULL) {
95 db_printf("No map.\n");
96 return;
97 }
98
99 /*
100 * Should we do anything fancy with overlapping regions?
101 */
102
103 for (watch = db_watchpoint_list;
104 watch != 0;
105 watch = watch->link)
106 if (db_map_equal(watch->map, map) &&
107 (watch->loaddr == addr) &&
108 (watch->hiaddr == addr+size)) {
109 db_printf("Already set.\n");
110 return;
111 }
112
113 watch = db_watchpoint_alloc();
114 if (watch == 0) {
115 db_printf("Too many watchpoints.\n");
116 return;
117 }
118
119 watch->map = map;
120 watch->loaddr = addr;
121 watch->hiaddr = addr+size;
122
123 watch->link = db_watchpoint_list;
124 db_watchpoint_list = watch;
125
126 db_watchpoints_inserted = FALSE;
127 }
128
129 void
130 db_delete_watchpoint(map, addr)
131 vm_map_t map;
132 db_addr_t addr;
133 {
134 db_watchpoint_t watch;
135 db_watchpoint_t *prev;
136
137 for (prev = &db_watchpoint_list;
138 (watch = *prev) != 0;
139 prev = &watch->link)
140 if (db_map_equal(watch->map, map) &&
141 (watch->loaddr <= addr) &&
142 (addr < watch->hiaddr)) {
143 *prev = watch->link;
144 db_watchpoint_free(watch);
145 return;
146 }
147
148 db_printf("Not set.\n");
149 }
150
151 void
152 db_list_watchpoints()
153 {
154 db_watchpoint_t watch;
155
156 if (db_watchpoint_list == 0) {
157 db_printf("No watchpoints set\n");
158 return;
159 }
160
161 db_printf(" Map Address Size\n");
162 for (watch = db_watchpoint_list;
163 watch != 0;
164 watch = watch->link)
165 db_printf("%s%p %8lx %lx\n",
166 db_map_current(watch->map) ? "*" : " ",
167 watch->map, watch->loaddr,
168 watch->hiaddr - watch->loaddr);
169 }
170
171 /* Delete watchpoint */
172 /*ARGSUSED*/
173 void
174 db_deletewatch_cmd(addr, have_addr, count, modif)
175 db_expr_t addr;
176 int have_addr;
177 db_expr_t count;
178 char * modif;
179 {
180 db_delete_watchpoint(db_map_addr(addr), addr);
181 }
182
183 /* Set watchpoint */
184 /*ARGSUSED*/
185 void
186 db_watchpoint_cmd(addr, have_addr, count, modif)
187 db_expr_t addr;
188 int have_addr;
189 db_expr_t count;
190 char * modif;
191 {
192 vsize_t size;
193 db_expr_t value;
194
195 if (db_expression(&value))
196 size = (vsize_t) value;
197 else
198 size = 4;
199 db_skip_to_eol();
200
201 db_set_watchpoint(db_map_addr(addr), addr, size);
202 }
203
204 /* list watchpoints */
205 /*ARGSUSED*/
206 void
207 db_listwatch_cmd(addr, have_addr, count, modif)
208 db_expr_t addr;
209 int have_addr;
210 db_expr_t count;
211 char * modif;
212 {
213 db_list_watchpoints();
214 }
215
216 void
217 db_set_watchpoints()
218 {
219 db_watchpoint_t watch;
220
221 if (!db_watchpoints_inserted) {
222 for (watch = db_watchpoint_list;
223 watch != 0;
224 watch = watch->link)
225 pmap_protect(watch->map->pmap,
226 trunc_page(watch->loaddr),
227 round_page(watch->hiaddr),
228 VM_PROT_READ);
229
230 db_watchpoints_inserted = TRUE;
231 }
232 }
233
234 void
235 db_clear_watchpoints()
236 {
237 db_watchpoints_inserted = FALSE;
238 }
239
240 boolean_t
241 db_find_watchpoint(map, addr, regs)
242 vm_map_t map;
243 db_addr_t addr;
244 db_regs_t *regs;
245 {
246 db_watchpoint_t watch;
247 db_watchpoint_t found = 0;
248
249 for (watch = db_watchpoint_list;
250 watch != 0;
251 watch = watch->link)
252 if (db_map_equal(watch->map, map)) {
253 if ((watch->loaddr <= addr) &&
254 (addr < watch->hiaddr))
255 return (TRUE);
256 else if ((trunc_page(watch->loaddr) <= addr) &&
257 (addr < round_page(watch->hiaddr)))
258 found = watch;
259 }
260
261 /*
262 * We didn't hit exactly on a watchpoint, but we are
263 * in a protected region. We want to single-step
264 * and then re-protect.
265 */
266
267 if (found) {
268 db_watchpoints_inserted = FALSE;
269 db_single_step(regs);
270 }
271
272 return (FALSE);
273 }
274