2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
26 * $FreeBSD: src/sys/ddb/db_break.c,v 1.18 1999/08/28 00:41:05 peter Exp $
27 * $DragonFly: src/sys/ddb/db_break.c,v 1.7 2006/12/28 21:24:01 dillon Exp $
31 * Author: David B. Golub, Carnegie Mellon University
37 #include <sys/param.h>
40 #include <vm/vm_kern.h>
43 #include <ddb/db_break.h>
44 #include <ddb/db_access.h>
45 #include <ddb/db_sym.h>
47 #define NBREAKPOINTS 100
48 static struct db_breakpoint db_break_table
[NBREAKPOINTS
];
49 static db_breakpoint_t db_next_free_breakpoint
= &db_break_table
[0];
50 static db_breakpoint_t db_free_breakpoints
= 0;
51 static db_breakpoint_t db_breakpoint_list
= 0;
53 static db_breakpoint_t
db_breakpoint_alloc (void);
54 static void db_breakpoint_free (db_breakpoint_t bkpt
);
55 static void db_delete_breakpoint (vm_map_t map
, db_addr_t addr
);
56 static db_breakpoint_t
db_find_breakpoint (vm_map_t map
, db_addr_t addr
);
57 static void db_list_breakpoints (void);
58 static void db_set_breakpoint (vm_map_t map
, db_addr_t addr
,
61 static db_breakpoint_t
62 db_breakpoint_alloc(void)
66 if ((bkpt
= db_free_breakpoints
) != 0) {
67 db_free_breakpoints
= bkpt
->link
;
70 if (db_next_free_breakpoint
== &db_break_table
[NBREAKPOINTS
]) {
71 db_printf("All breakpoints used.\n");
74 bkpt
= db_next_free_breakpoint
;
75 db_next_free_breakpoint
++;
81 db_breakpoint_free(db_breakpoint_t bkpt
)
83 bkpt
->link
= db_free_breakpoints
;
84 db_free_breakpoints
= bkpt
;
88 db_set_breakpoint(vm_map_t map
, db_addr_t addr
, int count
)
92 if (db_find_breakpoint(map
, addr
)) {
93 db_printf("Already set.\n");
97 bkpt
= db_breakpoint_alloc();
99 db_printf("Too many breakpoints.\n");
104 bkpt
->address
= addr
;
106 bkpt
->init_count
= count
;
109 bkpt
->link
= db_breakpoint_list
;
110 db_breakpoint_list
= bkpt
;
114 db_delete_breakpoint(vm_map_t map
, db_addr_t addr
)
116 db_breakpoint_t bkpt
;
117 db_breakpoint_t
*prev
;
119 for (prev
= &db_breakpoint_list
;
121 prev
= &bkpt
->link
) {
122 if (db_map_equal(bkpt
->map
, map
) &&
123 (bkpt
->address
== addr
)) {
129 db_printf("Not set.\n");
133 db_breakpoint_free(bkpt
);
136 static db_breakpoint_t
137 db_find_breakpoint(vm_map_t map
, db_addr_t addr
)
139 db_breakpoint_t bkpt
;
141 for (bkpt
= db_breakpoint_list
;
145 if (db_map_equal(bkpt
->map
, map
) &&
146 (bkpt
->address
== addr
))
153 db_find_breakpoint_here(db_addr_t addr
)
155 return db_find_breakpoint(db_map_addr(addr
), addr
);
158 static boolean_t db_breakpoints_inserted
= TRUE
;
161 db_set_breakpoints(void)
163 db_breakpoint_t bkpt
;
165 if (!db_breakpoints_inserted
) {
167 for (bkpt
= db_breakpoint_list
;
170 if (db_map_current(bkpt
->map
)) {
171 bkpt
->bkpt_inst
= db_get_value(bkpt
->address
,
174 db_put_value(bkpt
->address
,
176 BKPT_SET(bkpt
->bkpt_inst
));
178 db_breakpoints_inserted
= TRUE
;
183 db_clear_breakpoints(void)
185 db_breakpoint_t bkpt
;
187 if (db_breakpoints_inserted
) {
189 for (bkpt
= db_breakpoint_list
;
192 if (db_map_current(bkpt
->map
)) {
193 db_put_value(bkpt
->address
, BKPT_SIZE
, bkpt
->bkpt_inst
);
195 db_breakpoints_inserted
= FALSE
;
199 #ifdef SOFTWARE_SSTEP
201 * Set a temporary breakpoint.
202 * The instruction is changed immediately,
203 * so the breakpoint does not have to be on the breakpoint list.
206 db_set_temp_breakpoint(db_addr_t addr
)
208 db_breakpoint_t bkpt
;
210 bkpt
= db_breakpoint_alloc();
212 db_printf("Too many breakpoints.\n");
217 bkpt
->address
= addr
;
218 bkpt
->flags
= BKPT_TEMP
;
219 bkpt
->init_count
= 1;
222 bkpt
->bkpt_inst
= db_get_value(bkpt
->address
, BKPT_SIZE
, FALSE
);
223 db_put_value(bkpt
->address
, BKPT_SIZE
, BKPT_SET(bkpt
->bkpt_inst
));
228 db_delete_temp_breakpoint(db_breakpoint_t bkpt
)
230 db_put_value(bkpt
->address
, BKPT_SIZE
, bkpt
->bkpt_inst
);
231 db_breakpoint_free(bkpt
);
233 #endif /* SOFTWARE_SSTEP */
239 db_list_breakpoints(void)
241 db_breakpoint_t bkpt
;
243 if (db_breakpoint_list
== 0) {
244 db_printf("No breakpoints set\n");
248 db_printf(" Map Count Address\n");
249 for (bkpt
= db_breakpoint_list
;
252 db_printf("%s%8p %5d ",
253 db_map_current(bkpt
->map
) ? "*" : " ",
254 (void *)bkpt
->map
, bkpt
->init_count
);
255 db_printsym(bkpt
->address
, DB_STGY_PROC
);
260 /* Delete breakpoint */
263 db_delete_cmd(db_expr_t addr
, boolean_t have_addr
, db_expr_t count
, char *modif
)
265 db_delete_breakpoint(db_map_addr(addr
), (db_addr_t
)addr
);
268 /* Set breakpoint with skip count */
271 db_breakpoint_cmd(db_expr_t addr
, boolean_t have_addr
, db_expr_t count
,
277 db_set_breakpoint(db_map_addr(addr
), (db_addr_t
)addr
, count
);
280 /* list breakpoints */
282 db_listbreak_cmd(db_expr_t dummy1
, boolean_t dummy2
, db_expr_t dummy3
,
285 db_list_breakpoints();
289 * We want ddb to be usable before most of the kernel has been
290 * initialized. In particular, current_thread() or kernel_map
291 * (or both) may be null.
295 db_map_equal(vm_map_t map1
, vm_map_t map2
)
297 return ((map1
== map2
) ||
298 ((map1
== NULL
) && (map2
== &kernel_map
)) ||
299 ((map1
== &kernel_map
) && (map2
== NULL
)));
303 db_map_current(vm_map_t map
)
308 return ((map
== NULL
) ||
309 (map
== &kernel_map
) ||
310 (((thread
= current_thread()) != NULL
) &&
311 (map
== thread
->task
->map
)));
318 db_map_addr(vm_offset_t addr
)