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