xref: /freebsd/sys/ddb/db_break.c (revision 6b7b2d80ed4d728d3ffd12c422e57798c1b63a84)
1 /*-
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
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.
11  *
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.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  *
26  */
27 /*
28  *	Author: David B. Golub, Carnegie Mellon University
29  *	Date:	7/90
30  */
31 /*
32  * Breakpoints.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_kern.h>
42 
43 #include <ddb/ddb.h>
44 #include <ddb/db_break.h>
45 #include <ddb/db_access.h>
46 #include <ddb/db_sym.h>
47 
48 #define	NBREAKPOINTS	100
49 static struct db_breakpoint	db_break_table[NBREAKPOINTS];
50 static db_breakpoint_t		db_next_free_breakpoint = &db_break_table[0];
51 static db_breakpoint_t		db_free_breakpoints = 0;
52 static db_breakpoint_t		db_breakpoint_list = 0;
53 
54 static db_breakpoint_t	db_breakpoint_alloc(void);
55 static void	db_breakpoint_free(db_breakpoint_t bkpt);
56 static void	db_delete_breakpoint(vm_map_t map, db_addr_t addr);
57 static db_breakpoint_t	db_find_breakpoint(vm_map_t map, db_addr_t addr);
58 static void	db_list_breakpoints(void);
59 static void	db_set_breakpoint(vm_map_t map, db_addr_t addr, int count);
60 
61 static db_breakpoint_t
62 db_breakpoint_alloc(void)
63 {
64 	register db_breakpoint_t	bkpt;
65 
66 	if ((bkpt = db_free_breakpoints) != 0) {
67 	    db_free_breakpoints = bkpt->link;
68 	    return (bkpt);
69 	}
70 	if (db_next_free_breakpoint == &db_break_table[NBREAKPOINTS]) {
71 	    db_printf("All breakpoints used.\n");
72 	    return (0);
73 	}
74 	bkpt = db_next_free_breakpoint;
75 	db_next_free_breakpoint++;
76 
77 	return (bkpt);
78 }
79 
80 static void
81 db_breakpoint_free(db_breakpoint_t bkpt)
82 {
83 	bkpt->link = db_free_breakpoints;
84 	db_free_breakpoints = bkpt;
85 }
86 
87 static void
88 db_set_breakpoint(vm_map_t map, db_addr_t addr, int count)
89 {
90 	register db_breakpoint_t	bkpt;
91 
92 	if (db_find_breakpoint(map, addr)) {
93 	    db_printf("Already set.\n");
94 	    return;
95 	}
96 
97 	bkpt = db_breakpoint_alloc();
98 	if (bkpt == 0) {
99 	    db_printf("Too many breakpoints.\n");
100 	    return;
101 	}
102 
103 	bkpt->map = map;
104 	bkpt->address = addr;
105 	bkpt->flags = 0;
106 	bkpt->init_count = count;
107 	bkpt->count = count;
108 
109 	bkpt->link = db_breakpoint_list;
110 	db_breakpoint_list = bkpt;
111 }
112 
113 static void
114 db_delete_breakpoint(vm_map_t map, db_addr_t addr)
115 {
116 	register db_breakpoint_t	bkpt;
117 	register db_breakpoint_t	*prev;
118 
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;
126 	    }
127 	}
128 	if (bkpt == 0) {
129 	    db_printf("Not set.\n");
130 	    return;
131 	}
132 
133 	db_breakpoint_free(bkpt);
134 }
135 
136 static db_breakpoint_t
137 db_find_breakpoint(vm_map_t map, db_addr_t addr)
138 {
139 	register db_breakpoint_t	bkpt;
140 
141 	for (bkpt = db_breakpoint_list;
142 	     bkpt != 0;
143 	     bkpt = bkpt->link)
144 	{
145 	    if (db_map_equal(bkpt->map, map) &&
146 		(bkpt->address == addr))
147 		return (bkpt);
148 	}
149 	return (0);
150 }
151 
152 db_breakpoint_t
153 db_find_breakpoint_here(db_addr_t addr)
154 {
155 	return db_find_breakpoint(db_map_addr(addr), addr);
156 }
157 
158 static bool	db_breakpoints_inserted = true;
159 
160 #ifndef BKPT_WRITE
161 #define	BKPT_WRITE(addr, storage)				\
162 do {								\
163 	*storage = db_get_value(addr, BKPT_SIZE, false);	\
164 	db_put_value(addr, BKPT_SIZE, BKPT_SET(*storage));	\
165 } while (0)
166 #endif
167 
168 #ifndef BKPT_CLEAR
169 #define	BKPT_CLEAR(addr, storage) \
170 	db_put_value(addr, BKPT_SIZE, *storage)
171 #endif
172 
173 void
174 db_set_breakpoints(void)
175 {
176 	register db_breakpoint_t	bkpt;
177 
178 	if (!db_breakpoints_inserted) {
179 
180 		for (bkpt = db_breakpoint_list;
181 		     bkpt != 0;
182 		     bkpt = bkpt->link)
183 			if (db_map_current(bkpt->map)) {
184 				BKPT_WRITE(bkpt->address, &bkpt->bkpt_inst);
185 			}
186 		db_breakpoints_inserted = true;
187 	}
188 }
189 
190 void
191 db_clear_breakpoints(void)
192 {
193 	register db_breakpoint_t	bkpt;
194 
195 	if (db_breakpoints_inserted) {
196 
197 		for (bkpt = db_breakpoint_list;
198 		     bkpt != 0;
199 		     bkpt = bkpt->link)
200 			if (db_map_current(bkpt->map)) {
201 				BKPT_CLEAR(bkpt->address, &bkpt->bkpt_inst);
202 			}
203 		db_breakpoints_inserted = false;
204 	}
205 }
206 
207 #ifdef SOFTWARE_SSTEP
208 /*
209  * Set a temporary breakpoint.
210  * The instruction is changed immediately,
211  * so the breakpoint does not have to be on the breakpoint list.
212  */
213 db_breakpoint_t
214 db_set_temp_breakpoint(db_addr_t addr)
215 {
216 	register db_breakpoint_t	bkpt;
217 
218 	bkpt = db_breakpoint_alloc();
219 	if (bkpt == 0) {
220 	    db_printf("Too many breakpoints.\n");
221 	    return 0;
222 	}
223 
224 	bkpt->map = NULL;
225 	bkpt->address = addr;
226 	bkpt->flags = BKPT_TEMP;
227 	bkpt->init_count = 1;
228 	bkpt->count = 1;
229 
230 	BKPT_WRITE(bkpt->address, &bkpt->bkpt_inst);
231 	return bkpt;
232 }
233 
234 void
235 db_delete_temp_breakpoint(db_breakpoint_t bkpt)
236 {
237 	BKPT_CLEAR(bkpt->address, &bkpt->bkpt_inst);
238 	db_breakpoint_free(bkpt);
239 }
240 #endif /* SOFTWARE_SSTEP */
241 
242 /*
243  * List breakpoints.
244  */
245 static void
246 db_list_breakpoints(void)
247 {
248 	register db_breakpoint_t	bkpt;
249 
250 	if (db_breakpoint_list == 0) {
251 	    db_printf("No breakpoints set\n");
252 	    return;
253 	}
254 
255 	db_printf(" Map      Count    Address\n");
256 	for (bkpt = db_breakpoint_list;
257 	     bkpt != 0;
258 	     bkpt = bkpt->link) {
259 	    db_printf("%s%8p %5d    ",
260 		      db_map_current(bkpt->map) ? "*" : " ",
261 		      (void *)bkpt->map, bkpt->init_count);
262 	    db_printsym(bkpt->address, DB_STGY_PROC);
263 	    db_printf("\n");
264 	}
265 }
266 
267 /* Delete breakpoint */
268 /*ARGSUSED*/
269 void
270 db_delete_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
271 {
272 	db_delete_breakpoint(db_map_addr(addr), (db_addr_t)addr);
273 }
274 
275 /* Set breakpoint with skip count */
276 /*ARGSUSED*/
277 void
278 db_breakpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
279 {
280 	if (count == -1)
281 	    count = 1;
282 
283 	db_set_breakpoint(db_map_addr(addr), (db_addr_t)addr, count);
284 }
285 
286 /* list breakpoints */
287 void
288 db_listbreak_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
289 {
290 	db_list_breakpoints();
291 }
292 
293 /*
294  *	We want ddb to be usable before most of the kernel has been
295  *	initialized.  In particular, current_thread() or kernel_map
296  *	(or both) may be null.
297  */
298 
299 bool
300 db_map_equal(vm_map_t map1, vm_map_t map2)
301 {
302 	return ((map1 == map2) ||
303 		((map1 == NULL) && (map2 == kernel_map)) ||
304 		((map1 == kernel_map) && (map2 == NULL)));
305 }
306 
307 bool
308 db_map_current(vm_map_t map)
309 {
310 #if 0
311 	thread_t	thread;
312 
313 	return ((map == NULL) ||
314 		(map == kernel_map) ||
315 		(((thread = current_thread()) != NULL) &&
316 		 (map == thread->task->map)));
317 #else
318 	return (true);
319 #endif
320 }
321 
322 vm_map_t
323 db_map_addr(vm_offset_t addr)
324 {
325 #if 0
326 	thread_t	thread;
327 
328 	/*
329 	 *	We want to return kernel_map for all
330 	 *	non-user addresses, even when debugging
331 	 *	kernel tasks with their own maps.
332 	 */
333 
334 	if ((VM_MIN_ADDRESS <= addr) &&
335 	    (addr < VM_MAX_ADDRESS) &&
336 	    ((thread = current_thread()) != NULL))
337 	    return thread->task->map;
338 	else
339 #endif
340 	    return kernel_map;
341 }
342