2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * Author: Richard P. Draves, Carnegie Mellon University
63 #include <mach/boolean.h>
64 #include <mach/vm_param.h>
65 #include <mach/machine/vm_types.h>
66 #include <mach/machine/vm_param.h>
67 #include <vm/vm_map.h>
69 #include <machine/db_machdep.h>
70 #include <ddb/db_lex.h>
71 #include <ddb/db_watch.h>
72 #include <ddb/db_access.h>
73 #include <ddb/db_sym.h>
74 #include <ddb/db_task_thread.h>
75 #include <ddb/db_command.h>
76 #include <ddb/db_expr.h>
77 #include <ddb/db_output.h> /* For db_printf() */
78 #include <ddb/db_run.h> /* For db_single_step() */
84 boolean_t db_watchpoints_inserted
= TRUE
;
86 #define NWATCHPOINTS 100
87 struct db_watchpoint db_watch_table
[NWATCHPOINTS
];
88 db_watchpoint_t db_next_free_watchpoint
= &db_watch_table
[0];
89 db_watchpoint_t db_free_watchpoints
= 0;
90 db_watchpoint_t db_watchpoint_list
= 0;
92 extern vm_map_t kernel_map
;
96 /* Prototypes for functions local to this file. XXX -- should be static.
99 db_watchpoint_t
db_watchpoint_alloc(void);
101 void db_watchpoint_free(register db_watchpoint_t watch
);
103 void db_set_watchpoint(
108 void db_delete_watchpoint(
112 static int db_get_task(
117 void db_list_watchpoints(void);
122 db_watchpoint_alloc(void)
124 register db_watchpoint_t watch
;
126 if ((watch
= db_free_watchpoints
) != 0) {
127 db_free_watchpoints
= watch
->link
;
130 if (db_next_free_watchpoint
== &db_watch_table
[NWATCHPOINTS
]) {
131 db_printf("All watchpoints used.\n");
134 watch
= db_next_free_watchpoint
;
135 db_next_free_watchpoint
++;
141 db_watchpoint_free(register db_watchpoint_t watch
)
143 watch
->link
= db_free_watchpoints
;
144 db_free_watchpoints
= watch
;
153 register db_watchpoint_t watch
;
156 * Should we do anything fancy with overlapping regions?
159 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
160 if (watch
->task
== task
&&
161 (watch
->loaddr
== addr
) &&
162 (watch
->hiaddr
== addr
+size
)) {
163 db_printf("Already set.\n");
168 watch
= db_watchpoint_alloc();
170 db_printf("Too many watchpoints.\n");
175 watch
->loaddr
= addr
;
176 watch
->hiaddr
= addr
+size
;
178 watch
->link
= db_watchpoint_list
;
179 db_watchpoint_list
= watch
;
181 db_watchpoints_inserted
= FALSE
;
185 db_delete_watchpoint(
189 register db_watchpoint_t watch
;
190 register db_watchpoint_t
*prev
;
192 for (prev
= &db_watchpoint_list
; (watch
= *prev
) != 0;
193 prev
= &watch
->link
) {
194 if (watch
->task
== task
&&
195 (watch
->loaddr
<= addr
) &&
196 (addr
< watch
->hiaddr
)) {
198 db_watchpoint_free(watch
);
203 db_printf("Not set.\n");
207 db_list_watchpoints(void)
209 register db_watchpoint_t watch
;
212 if (db_watchpoint_list
== 0) {
213 db_printf("No watchpoints set\n");
217 db_printf("Space Address Size\n");
218 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
219 if (watch
->task
== TASK_NULL
)
220 db_printf("kernel ");
222 task_id
= db_lookup_task(watch
->task
);
224 db_printf("%*X", 2*sizeof(vm_offset_t
), watch
->task
);
226 db_printf("task%-3d ", task_id
);
228 db_printf(" %*X %X\n", 2*sizeof(vm_offset_t
), watch
->loaddr
,
229 watch
->hiaddr
- watch
->loaddr
);
239 task_t task
= TASK_NULL
;
241 boolean_t user_space
;
243 user_space
= db_option(modif
, 'T');
245 if (db_expression(&value
)) {
246 task
= (task_t
)(unsigned long)value
;
247 if (db_lookup_task(task
) < 0) {
248 db_printf("bad task address %X\n", task
);
252 task
= db_default_task
;
253 if (task
== TASK_NULL
) {
254 if ((task
= db_current_task()) == TASK_NULL
) {
255 db_printf("no task\n");
261 if (!DB_VALID_ADDRESS(addr
, user_space
)) {
262 db_printf("Address %#X is not in %s space\n", addr
,
263 (user_space
)? "user": "kernel");
270 /* Delete watchpoint */
272 db_deletewatch_cmd(db_expr_t addr
, __unused boolean_t have_addr
,
273 __unused db_expr_t count
, char *modif
)
277 if (db_get_task(modif
, &task
, addr
) < 0)
279 db_delete_watchpoint(task
, addr
);
284 db_watchpoint_cmd(db_expr_t addr
, __unused boolean_t have_addr
,
285 __unused db_expr_t count
, char *modif
)
291 if (db_get_task(modif
, &task
, addr
) < 0)
293 if (db_expression(&value
))
294 size
= (vm_size_t
) value
;
297 db_set_watchpoint(task
, addr
, size
);
300 /* list watchpoints */
302 db_listwatch_cmd(__unused db_expr_t addr
, __unused boolean_t have_addr
,
303 __unused db_expr_t count
, __unused
char *modif
)
305 db_list_watchpoints();
309 db_set_watchpoints(void)
311 register db_watchpoint_t watch
;
314 if (!db_watchpoints_inserted
) {
315 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
316 map
= (watch
->task
)? watch
->task
->map
: kernel_map
;
317 pmap_protect(map
->pmap
,
318 vm_map_trunc_page(watch
->loaddr
),
319 vm_map_round_page(watch
->hiaddr
),
322 db_watchpoints_inserted
= TRUE
;
327 db_clear_watchpoints(void)
329 db_watchpoints_inserted
= FALSE
;
338 register db_watchpoint_t watch
;
339 db_watchpoint_t found
= 0;
340 register task_t task_space
;
342 task_space
= (vm_map_pmap(map
) == kernel_pmap
)?
343 TASK_NULL
: db_current_space();
344 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
345 if (watch
->task
== task_space
) {
346 if ((watch
->loaddr
<= addr
) && (addr
< watch
->hiaddr
))
348 else if ((trunc_page(watch
->loaddr
) <= addr
) &&
349 (addr
< round_page(watch
->hiaddr
)))
355 * We didn't hit exactly on a watchpoint, but we are
356 * in a protected region. We want to single-step
357 * and then re-protect.
361 db_watchpoints_inserted
= FALSE
;
362 db_single_step(regs
, task_space
);