]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_watch.c
fa67b73349a2d1c241c6f84781412b57a8f8c579
[apple/xnu.git] / osfmk / ddb / db_watch.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60 /*
61 * Author: Richard P. Draves, Carnegie Mellon University
62 * Date: 10/90
63 */
64
65 #include <mach/boolean.h>
66 #include <mach/vm_param.h>
67 #include <mach/machine/vm_types.h>
68 #include <mach/machine/vm_param.h>
69 #include <vm/vm_map.h>
70
71 #include <machine/db_machdep.h>
72 #include <ddb/db_lex.h>
73 #include <ddb/db_watch.h>
74 #include <ddb/db_access.h>
75 #include <ddb/db_sym.h>
76 #include <ddb/db_task_thread.h>
77 #include <ddb/db_command.h>
78 #include <ddb/db_expr.h>
79 #include <ddb/db_output.h> /* For db_printf() */
80 #include <ddb/db_run.h> /* For db_single_step() */
81
82 /*
83 * Watchpoints.
84 */
85
86 boolean_t db_watchpoints_inserted = TRUE;
87
88 #define NWATCHPOINTS 100
89 struct db_watchpoint db_watch_table[NWATCHPOINTS];
90 db_watchpoint_t db_next_free_watchpoint = &db_watch_table[0];
91 db_watchpoint_t db_free_watchpoints = 0;
92 db_watchpoint_t db_watchpoint_list = 0;
93
94 extern vm_map_t kernel_map;
95
96
97
98 /* Prototypes for functions local to this file. XXX -- should be static.
99 */
100
101 db_watchpoint_t db_watchpoint_alloc(void);
102
103 void db_watchpoint_free(register db_watchpoint_t watch);
104
105 void db_set_watchpoint(
106 task_t task,
107 db_addr_t addr,
108 vm_size_t size);
109
110 void db_delete_watchpoint(
111 task_t task,
112 db_addr_t addr);
113
114 static int db_get_task(
115 char *modif,
116 task_t *taskp,
117 db_addr_t addr);
118
119 void db_list_watchpoints(void);
120
121
122
123 db_watchpoint_t
124 db_watchpoint_alloc(void)
125 {
126 register db_watchpoint_t watch;
127
128 if ((watch = db_free_watchpoints) != 0) {
129 db_free_watchpoints = watch->link;
130 return (watch);
131 }
132 if (db_next_free_watchpoint == &db_watch_table[NWATCHPOINTS]) {
133 db_printf("All watchpoints used.\n");
134 return (0);
135 }
136 watch = db_next_free_watchpoint;
137 db_next_free_watchpoint++;
138
139 return (watch);
140 }
141
142 void
143 db_watchpoint_free(register db_watchpoint_t watch)
144 {
145 watch->link = db_free_watchpoints;
146 db_free_watchpoints = watch;
147 }
148
149 void
150 db_set_watchpoint(
151 task_t task,
152 db_addr_t addr,
153 vm_size_t size)
154 {
155 register db_watchpoint_t watch;
156
157 /*
158 * Should we do anything fancy with overlapping regions?
159 */
160
161 for (watch = db_watchpoint_list; watch != 0; watch = watch->link) {
162 if (watch->task == task &&
163 (watch->loaddr == addr) &&
164 (watch->hiaddr == addr+size)) {
165 db_printf("Already set.\n");
166 return;
167 }
168 }
169
170 watch = db_watchpoint_alloc();
171 if (watch == 0) {
172 db_printf("Too many watchpoints.\n");
173 return;
174 }
175
176 watch->task = task;
177 watch->loaddr = addr;
178 watch->hiaddr = addr+size;
179
180 watch->link = db_watchpoint_list;
181 db_watchpoint_list = watch;
182
183 db_watchpoints_inserted = FALSE;
184 }
185
186 void
187 db_delete_watchpoint(
188 task_t task,
189 db_addr_t addr)
190 {
191 register db_watchpoint_t watch;
192 register db_watchpoint_t *prev;
193
194 for (prev = &db_watchpoint_list; (watch = *prev) != 0;
195 prev = &watch->link) {
196 if (watch->task == task &&
197 (watch->loaddr <= addr) &&
198 (addr < watch->hiaddr)) {
199 *prev = watch->link;
200 db_watchpoint_free(watch);
201 return;
202 }
203 }
204
205 db_printf("Not set.\n");
206 }
207
208 void
209 db_list_watchpoints(void)
210 {
211 register db_watchpoint_t watch;
212 int task_id;
213
214 if (db_watchpoint_list == 0) {
215 db_printf("No watchpoints set\n");
216 return;
217 }
218
219 db_printf("Space Address Size\n");
220 for (watch = db_watchpoint_list; watch != 0; watch = watch->link) {
221 if (watch->task == TASK_NULL)
222 db_printf("kernel ");
223 else {
224 task_id = db_lookup_task(watch->task);
225 if (task_id < 0)
226 db_printf("%*X", 2*sizeof(vm_offset_t), watch->task);
227 else
228 db_printf("task%-3d ", task_id);
229 }
230 db_printf(" %*X %X\n", 2*sizeof(vm_offset_t), watch->loaddr,
231 watch->hiaddr - watch->loaddr);
232 }
233 }
234
235 static int
236 db_get_task(
237 char *modif,
238 task_t *taskp,
239 db_addr_t addr)
240 {
241 task_t task = TASK_NULL;
242 db_expr_t value;
243 boolean_t user_space;
244
245 user_space = db_option(modif, 'T');
246 if (user_space) {
247 if (db_expression(&value)) {
248 task = (task_t)value;
249 if (db_lookup_task(task) < 0) {
250 db_printf("bad task address %X\n", task);
251 return(-1);
252 }
253 } else {
254 task = db_default_task;
255 if (task == TASK_NULL) {
256 if ((task = db_current_task()) == TASK_NULL) {
257 db_printf("no task\n");
258 return(-1);
259 }
260 }
261 }
262 }
263 if (!DB_VALID_ADDRESS(addr, user_space)) {
264 db_printf("Address %#X is not in %s space\n", addr,
265 (user_space)? "user": "kernel");
266 return(-1);
267 }
268 *taskp = task;
269 return(0);
270 }
271
272 /* Delete watchpoint */
273 void
274 db_deletewatch_cmd(
275 db_expr_t addr,
276 int have_addr,
277 db_expr_t count,
278 char * modif)
279 {
280 task_t task;
281
282 if (db_get_task(modif, &task, addr) < 0)
283 return;
284 db_delete_watchpoint(task, addr);
285 }
286
287 /* Set watchpoint */
288 void
289 db_watchpoint_cmd(
290 db_expr_t addr,
291 int have_addr,
292 db_expr_t count,
293 char * modif)
294 {
295 vm_size_t size;
296 db_expr_t value;
297 task_t task;
298
299 if (db_get_task(modif, &task, addr) < 0)
300 return;
301 if (db_expression(&value))
302 size = (vm_size_t) value;
303 else
304 size = sizeof(int);
305 db_set_watchpoint(task, addr, size);
306 }
307
308 /* list watchpoints */
309 void
310 db_listwatch_cmd(void)
311 {
312 db_list_watchpoints();
313 }
314
315 void
316 db_set_watchpoints(void)
317 {
318 register db_watchpoint_t watch;
319 vm_map_t map;
320
321 if (!db_watchpoints_inserted) {
322 for (watch = db_watchpoint_list; watch != 0; watch = watch->link) {
323 map = (watch->task)? watch->task->map: kernel_map;
324 pmap_protect(map->pmap,
325 vm_map_trunc_page(watch->loaddr),
326 vm_map_round_page(watch->hiaddr),
327 VM_PROT_READ);
328 }
329 db_watchpoints_inserted = TRUE;
330 }
331 }
332
333 void
334 db_clear_watchpoints(void)
335 {
336 db_watchpoints_inserted = FALSE;
337 }
338
339 boolean_t
340 db_find_watchpoint(
341 vm_map_t map,
342 db_addr_t addr,
343 db_regs_t *regs)
344 {
345 register db_watchpoint_t watch;
346 db_watchpoint_t found = 0;
347 register task_t task_space;
348
349 task_space = (vm_map_pmap(map) == kernel_pmap)?
350 TASK_NULL: db_current_space();
351 for (watch = db_watchpoint_list; watch != 0; watch = watch->link) {
352 if (watch->task == task_space) {
353 if ((watch->loaddr <= addr) && (addr < watch->hiaddr))
354 return (TRUE);
355 else if ((trunc_page(watch->loaddr) <= addr) &&
356 (addr < round_page(watch->hiaddr)))
357 found = watch;
358 }
359 }
360
361 /*
362 * We didn't hit exactly on a watchpoint, but we are
363 * in a protected region. We want to single-step
364 * and then re-protect.
365 */
366
367 if (found) {
368 db_watchpoints_inserted = FALSE;
369 db_single_step(regs, task_space);
370 }
371
372 return (FALSE);
373 }