kernel - Fix auto port assignment collision in network code
[dragonfly.git] / sys / ddb / db_break.c
blob27d8379d98bee12c0a19dc4a7c2a05a205f015d1
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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
32 * Date: 7/90
35 * Breakpoints.
37 #include <sys/param.h>
39 #include <vm/vm.h>
40 #include <vm/vm_kern.h>
42 #include <ddb/ddb.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,
59 int count);
61 static db_breakpoint_t
62 db_breakpoint_alloc(void)
64 db_breakpoint_t bkpt;
66 if ((bkpt = db_free_breakpoints) != 0) {
67 db_free_breakpoints = bkpt->link;
68 return (bkpt);
70 if (db_next_free_breakpoint == &db_break_table[NBREAKPOINTS]) {
71 db_printf("All breakpoints used.\n");
72 return (0);
74 bkpt = db_next_free_breakpoint;
75 db_next_free_breakpoint++;
77 return (bkpt);
80 static void
81 db_breakpoint_free(db_breakpoint_t bkpt)
83 bkpt->link = db_free_breakpoints;
84 db_free_breakpoints = bkpt;
87 static void
88 db_set_breakpoint(vm_map_t map, db_addr_t addr, int count)
90 db_breakpoint_t bkpt;
92 if (db_find_breakpoint(map, addr)) {
93 db_printf("Already set.\n");
94 return;
97 bkpt = db_breakpoint_alloc();
98 if (bkpt == 0) {
99 db_printf("Too many breakpoints.\n");
100 return;
103 bkpt->map = map;
104 bkpt->address = addr;
105 bkpt->flags = 0;
106 bkpt->init_count = count;
107 bkpt->count = count;
109 bkpt->link = db_breakpoint_list;
110 db_breakpoint_list = bkpt;
113 static void
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;
120 (bkpt = *prev) != 0;
121 prev = &bkpt->link) {
122 if (db_map_equal(bkpt->map, map) &&
123 (bkpt->address == addr)) {
124 *prev = bkpt->link;
125 break;
128 if (bkpt == 0) {
129 db_printf("Not set.\n");
130 return;
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;
142 bkpt != 0;
143 bkpt = bkpt->link)
145 if (db_map_equal(bkpt->map, map) &&
146 (bkpt->address == addr))
147 return (bkpt);
149 return (0);
152 db_breakpoint_t
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;
160 void
161 db_set_breakpoints(void)
163 db_breakpoint_t bkpt;
165 if (!db_breakpoints_inserted) {
167 for (bkpt = db_breakpoint_list;
168 bkpt != 0;
169 bkpt = bkpt->link)
170 if (db_map_current(bkpt->map)) {
171 bkpt->bkpt_inst = db_get_value(bkpt->address,
172 BKPT_SIZE,
173 FALSE);
174 db_put_value(bkpt->address,
175 BKPT_SIZE,
176 BKPT_SET(bkpt->bkpt_inst));
178 db_breakpoints_inserted = TRUE;
182 void
183 db_clear_breakpoints(void)
185 db_breakpoint_t bkpt;
187 if (db_breakpoints_inserted) {
189 for (bkpt = db_breakpoint_list;
190 bkpt != 0;
191 bkpt = bkpt->link)
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.
205 db_breakpoint_t
206 db_set_temp_breakpoint(db_addr_t addr)
208 db_breakpoint_t bkpt;
210 bkpt = db_breakpoint_alloc();
211 if (bkpt == 0) {
212 db_printf("Too many breakpoints.\n");
213 return 0;
216 bkpt->map = NULL;
217 bkpt->address = addr;
218 bkpt->flags = BKPT_TEMP;
219 bkpt->init_count = 1;
220 bkpt->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));
224 return bkpt;
227 void
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 */
236 * List breakpoints.
238 static void
239 db_list_breakpoints(void)
241 db_breakpoint_t bkpt;
243 if (db_breakpoint_list == 0) {
244 db_printf("No breakpoints set\n");
245 return;
248 db_printf(" Map Count Address\n");
249 for (bkpt = db_breakpoint_list;
250 bkpt != 0;
251 bkpt = bkpt->link) {
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);
256 db_printf("\n");
260 /* Delete breakpoint */
261 /*ARGSUSED*/
262 void
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 */
269 /*ARGSUSED*/
270 void
271 db_breakpoint_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
272 char *modif)
274 if (count == -1)
275 count = 1;
277 db_set_breakpoint(db_map_addr(addr), (db_addr_t)addr, count);
280 /* list breakpoints */
281 void
282 db_listbreak_cmd(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3,
283 char *dummy4)
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.
294 boolean_t
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)));
302 boolean_t
303 db_map_current(vm_map_t map)
305 #if 0
306 thread_t thread;
308 return ((map == NULL) ||
309 (map == kernel_map) ||
310 (((thread = current_thread()) != NULL) &&
311 (map == thread->task->map)));
312 #else
313 return (1);
314 #endif
317 vm_map_t
318 db_map_addr(vm_offset_t addr)
320 return kernel_map;