2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
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.
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.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * Author: Richard P. Draves, Carnegie Mellon University
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>
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() */
86 boolean_t db_watchpoints_inserted
= TRUE
;
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;
94 extern vm_map_t kernel_map
;
98 /* Prototypes for functions local to this file. XXX -- should be static.
101 db_watchpoint_t
db_watchpoint_alloc(void);
103 void db_watchpoint_free(register db_watchpoint_t watch
);
105 void db_set_watchpoint(
110 void db_delete_watchpoint(
114 static int db_get_task(
119 void db_list_watchpoints(void);
124 db_watchpoint_alloc(void)
126 register db_watchpoint_t watch
;
128 if ((watch
= db_free_watchpoints
) != 0) {
129 db_free_watchpoints
= watch
->link
;
132 if (db_next_free_watchpoint
== &db_watch_table
[NWATCHPOINTS
]) {
133 db_printf("All watchpoints used.\n");
136 watch
= db_next_free_watchpoint
;
137 db_next_free_watchpoint
++;
143 db_watchpoint_free(register db_watchpoint_t watch
)
145 watch
->link
= db_free_watchpoints
;
146 db_free_watchpoints
= watch
;
155 register db_watchpoint_t watch
;
158 * Should we do anything fancy with overlapping regions?
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");
170 watch
= db_watchpoint_alloc();
172 db_printf("Too many watchpoints.\n");
177 watch
->loaddr
= addr
;
178 watch
->hiaddr
= addr
+size
;
180 watch
->link
= db_watchpoint_list
;
181 db_watchpoint_list
= watch
;
183 db_watchpoints_inserted
= FALSE
;
187 db_delete_watchpoint(
191 register db_watchpoint_t watch
;
192 register db_watchpoint_t
*prev
;
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
)) {
200 db_watchpoint_free(watch
);
205 db_printf("Not set.\n");
209 db_list_watchpoints(void)
211 register db_watchpoint_t watch
;
214 if (db_watchpoint_list
== 0) {
215 db_printf("No watchpoints set\n");
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 ");
224 task_id
= db_lookup_task(watch
->task
);
226 db_printf("%*X", 2*sizeof(vm_offset_t
), watch
->task
);
228 db_printf("task%-3d ", task_id
);
230 db_printf(" %*X %X\n", 2*sizeof(vm_offset_t
), watch
->loaddr
,
231 watch
->hiaddr
- watch
->loaddr
);
241 task_t task
= TASK_NULL
;
243 boolean_t user_space
;
245 user_space
= db_option(modif
, 'T');
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
);
254 task
= db_default_task
;
255 if (task
== TASK_NULL
) {
256 if ((task
= db_current_task()) == TASK_NULL
) {
257 db_printf("no task\n");
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");
272 /* Delete watchpoint */
282 if (db_get_task(modif
, &task
, addr
) < 0)
284 db_delete_watchpoint(task
, addr
);
299 if (db_get_task(modif
, &task
, addr
) < 0)
301 if (db_expression(&value
))
302 size
= (vm_size_t
) value
;
305 db_set_watchpoint(task
, addr
, size
);
308 /* list watchpoints */
310 db_listwatch_cmd(void)
312 db_list_watchpoints();
316 db_set_watchpoints(void)
318 register db_watchpoint_t watch
;
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
),
329 db_watchpoints_inserted
= TRUE
;
334 db_clear_watchpoints(void)
336 db_watchpoints_inserted
= FALSE
;
345 register db_watchpoint_t watch
;
346 db_watchpoint_t found
= 0;
347 register task_t task_space
;
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
))
355 else if ((trunc_page(watch
->loaddr
) <= addr
) &&
356 (addr
< round_page(watch
->hiaddr
)))
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.
368 db_watchpoints_inserted
= FALSE
;
369 db_single_step(regs
, task_space
);