2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * Author: Richard P. Draves, Carnegie Mellon University
58 #include <mach/boolean.h>
59 #include <mach/vm_param.h>
60 #include <mach/machine/vm_types.h>
61 #include <mach/machine/vm_param.h>
62 #include <vm/vm_map.h>
64 #include <machine/db_machdep.h>
65 #include <ddb/db_lex.h>
66 #include <ddb/db_watch.h>
67 #include <ddb/db_access.h>
68 #include <ddb/db_sym.h>
69 #include <ddb/db_task_thread.h>
70 #include <ddb/db_command.h>
71 #include <ddb/db_expr.h>
72 #include <ddb/db_output.h> /* For db_printf() */
73 #include <ddb/db_run.h> /* For db_single_step() */
79 boolean_t db_watchpoints_inserted
= TRUE
;
81 #define NWATCHPOINTS 100
82 struct db_watchpoint db_watch_table
[NWATCHPOINTS
];
83 db_watchpoint_t db_next_free_watchpoint
= &db_watch_table
[0];
84 db_watchpoint_t db_free_watchpoints
= 0;
85 db_watchpoint_t db_watchpoint_list
= 0;
87 extern vm_map_t kernel_map
;
91 /* Prototypes for functions local to this file. XXX -- should be static.
94 db_watchpoint_t
db_watchpoint_alloc(void);
96 void db_watchpoint_free(register db_watchpoint_t watch
);
98 void db_set_watchpoint(
103 void db_delete_watchpoint(
107 static int db_get_task(
112 void db_list_watchpoints(void);
117 db_watchpoint_alloc(void)
119 register db_watchpoint_t watch
;
121 if ((watch
= db_free_watchpoints
) != 0) {
122 db_free_watchpoints
= watch
->link
;
125 if (db_next_free_watchpoint
== &db_watch_table
[NWATCHPOINTS
]) {
126 db_printf("All watchpoints used.\n");
129 watch
= db_next_free_watchpoint
;
130 db_next_free_watchpoint
++;
136 db_watchpoint_free(register db_watchpoint_t watch
)
138 watch
->link
= db_free_watchpoints
;
139 db_free_watchpoints
= watch
;
148 register db_watchpoint_t watch
;
151 * Should we do anything fancy with overlapping regions?
154 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
155 if (watch
->task
== task
&&
156 (watch
->loaddr
== addr
) &&
157 (watch
->hiaddr
== addr
+size
)) {
158 db_printf("Already set.\n");
163 watch
= db_watchpoint_alloc();
165 db_printf("Too many watchpoints.\n");
170 watch
->loaddr
= addr
;
171 watch
->hiaddr
= addr
+size
;
173 watch
->link
= db_watchpoint_list
;
174 db_watchpoint_list
= watch
;
176 db_watchpoints_inserted
= FALSE
;
180 db_delete_watchpoint(
184 register db_watchpoint_t watch
;
185 register db_watchpoint_t
*prev
;
187 for (prev
= &db_watchpoint_list
; (watch
= *prev
) != 0;
188 prev
= &watch
->link
) {
189 if (watch
->task
== task
&&
190 (watch
->loaddr
<= addr
) &&
191 (addr
< watch
->hiaddr
)) {
193 db_watchpoint_free(watch
);
198 db_printf("Not set.\n");
202 db_list_watchpoints(void)
204 register db_watchpoint_t watch
;
207 if (db_watchpoint_list
== 0) {
208 db_printf("No watchpoints set\n");
212 db_printf("Space Address Size\n");
213 for (watch
= db_watchpoint_list
; watch
!= 0; watch
= watch
->link
) {
214 if (watch
->task
== TASK_NULL
)
215 db_printf("kernel ");
217 task_id
= db_lookup_task(watch
->task
);
219 db_printf("%*X", 2*sizeof(vm_offset_t
), watch
->task
);
221 db_printf("task%-3d ", task_id
);
223 db_printf(" %*X %X\n", 2*sizeof(vm_offset_t
), watch
->loaddr
,
224 watch
->hiaddr
- watch
->loaddr
);
234 task_t task
= TASK_NULL
;
236 boolean_t user_space
;
238 user_space
= db_option(modif
, 'T');
240 if (db_expression(&value
)) {
241 task
= (task_t
)value
;
242 if (db_lookup_task(task
) < 0) {
243 db_printf("bad task address %X\n", task
);
247 task
= db_default_task
;
248 if (task
== TASK_NULL
) {
249 if ((task
= db_current_task()) == TASK_NULL
) {
250 db_printf("no task\n");
256 if (!DB_VALID_ADDRESS(addr
, user_space
)) {
257 db_printf("Address %#X is not in %s space\n", addr
,
258 (user_space
)? "user": "kernel");
265 /* Delete watchpoint */
275 if (db_get_task(modif
, &task
, addr
) < 0)
277 db_delete_watchpoint(task
, addr
);
292 if (db_get_task(modif
, &task
, addr
) < 0)
294 if (db_expression(&value
))
295 size
= (vm_size_t
) value
;
298 db_set_watchpoint(task
, addr
, size
);
301 /* list watchpoints */
303 db_listwatch_cmd(void)
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
);