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