Home | History | Annotate | Line # | Download | only in ddb
db_break.c revision 1.14.2.3
      1  1.14.2.3   nathanw /*	$NetBSD: db_break.c,v 1.14.2.3 2002/02/28 04:13:04 nathanw Exp $	*/
      2       1.4       cgd 
      3  1.14.2.3   nathanw /*
      4       1.1       cgd  * Mach Operating System
      5       1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6       1.1       cgd  * All Rights Reserved.
      7  1.14.2.3   nathanw  *
      8       1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9       1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10       1.1       cgd  * notice and this permission notice appear in all copies of the
     11       1.1       cgd  * software, derivative works or modified versions, and any portions
     12       1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13  1.14.2.3   nathanw  *
     14      1.11        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15       1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16       1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.14.2.3   nathanw  *
     18       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.14.2.3   nathanw  *
     20       1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21       1.1       cgd  *  School of Computer Science
     22       1.1       cgd  *  Carnegie Mellon University
     23       1.1       cgd  *  Pittsburgh PA 15213-3890
     24  1.14.2.3   nathanw  *
     25       1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26       1.1       cgd  * rights to redistribute these changes.
     27       1.2       cgd  *
     28       1.1       cgd  *	Author: David B. Golub, Carnegie Mellon University
     29       1.1       cgd  *	Date:	7/90
     30       1.1       cgd  */
     31       1.3   mycroft 
     32       1.1       cgd /*
     33       1.1       cgd  * Breakpoints.
     34       1.1       cgd  */
     35  1.14.2.2   nathanw 
     36  1.14.2.2   nathanw #include <sys/cdefs.h>
     37  1.14.2.3   nathanw __KERNEL_RCSID(0, "$NetBSD: db_break.c,v 1.14.2.3 2002/02/28 04:13:04 nathanw Exp $");
     38  1.14.2.2   nathanw 
     39       1.3   mycroft #include <sys/param.h>
     40       1.3   mycroft #include <sys/proc.h>
     41       1.3   mycroft 
     42       1.1       cgd #include <machine/db_machdep.h>		/* type definitions */
     43       1.1       cgd 
     44       1.1       cgd #include <ddb/db_lex.h>
     45       1.1       cgd #include <ddb/db_access.h>
     46       1.1       cgd #include <ddb/db_sym.h>
     47       1.1       cgd #include <ddb/db_break.h>
     48       1.6  christos #include <ddb/db_output.h>
     49       1.1       cgd 
     50       1.1       cgd #define	NBREAKPOINTS	100
     51  1.14.2.3   nathanw static struct db_breakpoint	db_break_table[NBREAKPOINTS];
     52  1.14.2.3   nathanw static db_breakpoint_t		db_next_free_breakpoint = &db_break_table[0];
     53  1.14.2.3   nathanw static db_breakpoint_t		db_free_breakpoints = 0;
     54  1.14.2.3   nathanw static db_breakpoint_t		db_breakpoint_list = 0;
     55  1.14.2.3   nathanw 
     56  1.14.2.3   nathanw static db_breakpoint_t	db_breakpoint_alloc(void);
     57  1.14.2.3   nathanw static void		db_breakpoint_free(db_breakpoint_t);
     58  1.14.2.3   nathanw static void		db_delete_breakpoint(struct vm_map *, db_addr_t);
     59  1.14.2.3   nathanw static db_breakpoint_t	db_find_breakpoint(struct vm_map *, db_addr_t);
     60  1.14.2.3   nathanw static void		db_list_breakpoints(void);
     61  1.14.2.3   nathanw static void		db_set_breakpoint(struct vm_map *, db_addr_t, int);
     62       1.1       cgd 
     63  1.14.2.3   nathanw static db_breakpoint_t
     64  1.14.2.3   nathanw db_breakpoint_alloc(void)
     65       1.1       cgd {
     66      1.12  augustss 	db_breakpoint_t	bkpt;
     67       1.1       cgd 
     68       1.1       cgd 	if ((bkpt = db_free_breakpoints) != 0) {
     69  1.14.2.3   nathanw 		db_free_breakpoints = bkpt->link;
     70  1.14.2.3   nathanw 		return (bkpt);
     71       1.1       cgd 	}
     72       1.1       cgd 	if (db_next_free_breakpoint == &db_break_table[NBREAKPOINTS]) {
     73  1.14.2.3   nathanw 		db_printf("All breakpoints used.\n");
     74  1.14.2.3   nathanw 		return (0);
     75       1.1       cgd 	}
     76       1.1       cgd 	bkpt = db_next_free_breakpoint;
     77       1.1       cgd 	db_next_free_breakpoint++;
     78       1.1       cgd 
     79       1.1       cgd 	return (bkpt);
     80       1.1       cgd }
     81       1.1       cgd 
     82  1.14.2.3   nathanw static void
     83  1.14.2.3   nathanw db_breakpoint_free(db_breakpoint_t bkpt)
     84       1.1       cgd {
     85       1.1       cgd 	bkpt->link = db_free_breakpoints;
     86       1.1       cgd 	db_free_breakpoints = bkpt;
     87       1.1       cgd }
     88       1.1       cgd 
     89       1.1       cgd void
     90  1.14.2.3   nathanw db_set_breakpoint(struct vm_map *map, db_addr_t addr, int count)
     91       1.1       cgd {
     92      1.12  augustss 	db_breakpoint_t	bkpt;
     93       1.1       cgd 
     94       1.1       cgd 	if (db_find_breakpoint(map, addr)) {
     95  1.14.2.3   nathanw 		db_printf("Already set.\n");
     96  1.14.2.3   nathanw 		return;
     97       1.1       cgd 	}
     98       1.1       cgd 
     99       1.1       cgd 	bkpt = db_breakpoint_alloc();
    100       1.1       cgd 	if (bkpt == 0) {
    101  1.14.2.3   nathanw 		db_printf("Too many breakpoints.\n");
    102  1.14.2.3   nathanw 		return;
    103       1.1       cgd 	}
    104       1.1       cgd 
    105       1.1       cgd 	bkpt->map = map;
    106       1.1       cgd 	bkpt->address = addr;
    107       1.1       cgd 	bkpt->flags = 0;
    108       1.1       cgd 	bkpt->init_count = count;
    109       1.1       cgd 	bkpt->count = count;
    110       1.1       cgd 
    111       1.1       cgd 	bkpt->link = db_breakpoint_list;
    112       1.1       cgd 	db_breakpoint_list = bkpt;
    113       1.1       cgd }
    114       1.1       cgd 
    115  1.14.2.3   nathanw static void
    116  1.14.2.3   nathanw db_delete_breakpoint(struct vm_map *map, db_addr_t addr)
    117       1.1       cgd {
    118      1.12  augustss 	db_breakpoint_t	bkpt;
    119      1.12  augustss 	db_breakpoint_t	*prev;
    120       1.1       cgd 
    121       1.1       cgd 	for (prev = &db_breakpoint_list;
    122       1.1       cgd 	     (bkpt = *prev) != 0;
    123       1.1       cgd 	     prev = &bkpt->link) {
    124  1.14.2.3   nathanw 		if (db_map_equal(bkpt->map, map) &&
    125  1.14.2.3   nathanw 		    (bkpt->address == addr)) {
    126  1.14.2.3   nathanw 			*prev = bkpt->link;
    127  1.14.2.3   nathanw 			break;
    128  1.14.2.3   nathanw 		}
    129       1.1       cgd 	}
    130       1.1       cgd 	if (bkpt == 0) {
    131  1.14.2.3   nathanw 		db_printf("Not set.\n");
    132  1.14.2.3   nathanw 		return;
    133       1.1       cgd 	}
    134       1.1       cgd 
    135       1.1       cgd 	db_breakpoint_free(bkpt);
    136       1.1       cgd }
    137       1.1       cgd 
    138       1.1       cgd db_breakpoint_t
    139  1.14.2.3   nathanw db_find_breakpoint(struct vm_map *map, db_addr_t addr)
    140       1.1       cgd {
    141      1.12  augustss 	db_breakpoint_t	bkpt;
    142       1.1       cgd 
    143       1.1       cgd 	for (bkpt = db_breakpoint_list;
    144       1.1       cgd 	     bkpt != 0;
    145       1.1       cgd 	     bkpt = bkpt->link)
    146  1.14.2.3   nathanw 		if (db_map_equal(bkpt->map, map) &&
    147  1.14.2.3   nathanw 		    (bkpt->address == addr))
    148  1.14.2.3   nathanw 			return (bkpt);
    149  1.14.2.3   nathanw 
    150       1.1       cgd 	return (0);
    151       1.1       cgd }
    152       1.1       cgd 
    153       1.1       cgd db_breakpoint_t
    154  1.14.2.3   nathanw db_find_breakpoint_here(db_addr_t addr)
    155       1.1       cgd {
    156  1.14.2.3   nathanw 	return db_find_breakpoint(db_map_addr(addr), addr);
    157       1.1       cgd }
    158       1.1       cgd 
    159  1.14.2.3   nathanw static boolean_t db_breakpoints_inserted = TRUE;
    160       1.1       cgd 
    161       1.1       cgd void
    162  1.14.2.3   nathanw db_set_breakpoints(void)
    163       1.1       cgd {
    164      1.12  augustss 	db_breakpoint_t	bkpt;
    165       1.1       cgd 
    166       1.1       cgd 	if (!db_breakpoints_inserted) {
    167       1.1       cgd 
    168  1.14.2.3   nathanw 		for (bkpt = db_breakpoint_list;
    169  1.14.2.3   nathanw 		     bkpt != 0;
    170  1.14.2.3   nathanw 		     bkpt = bkpt->link)
    171  1.14.2.3   nathanw 			if (db_map_current(bkpt->map)) {
    172  1.14.2.3   nathanw 				bkpt->bkpt_inst = db_get_value(bkpt->address,
    173  1.14.2.3   nathanw 				    BKPT_SIZE, FALSE);
    174  1.14.2.3   nathanw 				db_put_value(bkpt->address,
    175  1.14.2.3   nathanw 				    BKPT_SIZE, BKPT_SET(bkpt->bkpt_inst));
    176  1.14.2.3   nathanw 			}
    177  1.14.2.3   nathanw 		db_breakpoints_inserted = TRUE;
    178       1.1       cgd 	}
    179       1.1       cgd }
    180       1.1       cgd 
    181       1.1       cgd void
    182  1.14.2.3   nathanw db_clear_breakpoints(void)
    183       1.1       cgd {
    184      1.12  augustss 	db_breakpoint_t	bkpt;
    185       1.1       cgd 
    186       1.1       cgd 	if (db_breakpoints_inserted) {
    187       1.1       cgd 
    188  1.14.2.3   nathanw 		for (bkpt = db_breakpoint_list;
    189  1.14.2.3   nathanw 		     bkpt != 0;
    190  1.14.2.3   nathanw 		     bkpt = bkpt->link)
    191  1.14.2.3   nathanw 			if (db_map_current(bkpt->map))
    192  1.14.2.3   nathanw 			    db_put_value(bkpt->address, BKPT_SIZE,
    193  1.14.2.3   nathanw 				bkpt->bkpt_inst);
    194  1.14.2.3   nathanw 		db_breakpoints_inserted = FALSE;
    195       1.1       cgd 	}
    196       1.1       cgd }
    197       1.1       cgd 
    198       1.1       cgd /*
    199       1.1       cgd  * List breakpoints.
    200       1.1       cgd  */
    201       1.1       cgd void
    202  1.14.2.3   nathanw db_list_breakpoints(void)
    203       1.1       cgd {
    204      1.12  augustss 	db_breakpoint_t	bkpt;
    205       1.1       cgd 
    206       1.1       cgd 	if (db_breakpoint_list == 0) {
    207  1.14.2.3   nathanw 		db_printf("No breakpoints set\n");
    208  1.14.2.3   nathanw 		return;
    209       1.1       cgd 	}
    210       1.1       cgd 
    211       1.1       cgd 	db_printf(" Map      Count    Address\n");
    212       1.1       cgd 	for (bkpt = db_breakpoint_list;
    213       1.1       cgd 	     bkpt != 0;
    214  1.14.2.3   nathanw 	     bkpt = bkpt->link) {
    215  1.14.2.3   nathanw 		db_printf("%s%p %5d    ",
    216  1.14.2.3   nathanw 		    db_map_current(bkpt->map) ? "*" : " ",
    217  1.14.2.3   nathanw 		    bkpt->map, bkpt->init_count);
    218  1.14.2.3   nathanw 		db_printsym(bkpt->address, DB_STGY_PROC, db_printf);
    219  1.14.2.3   nathanw 		db_printf("\n");
    220       1.1       cgd 	}
    221       1.1       cgd }
    222       1.1       cgd 
    223       1.1       cgd /* Delete breakpoint */
    224       1.1       cgd /*ARGSUSED*/
    225       1.1       cgd void
    226  1.14.2.3   nathanw db_delete_cmd(db_expr_t addr, int have_addr, db_expr_t count, char * modif)
    227       1.1       cgd {
    228  1.14.2.3   nathanw 
    229       1.1       cgd 	db_delete_breakpoint(db_map_addr(addr), (db_addr_t)addr);
    230       1.1       cgd }
    231       1.1       cgd 
    232       1.1       cgd /* Set breakpoint with skip count */
    233       1.1       cgd /*ARGSUSED*/
    234       1.1       cgd void
    235  1.14.2.3   nathanw db_breakpoint_cmd(db_expr_t addr, int have_addr, db_expr_t count, char * modif)
    236       1.1       cgd {
    237  1.14.2.3   nathanw 
    238       1.1       cgd 	if (count == -1)
    239  1.14.2.3   nathanw 		count = 1;
    240       1.1       cgd 
    241       1.1       cgd 	db_set_breakpoint(db_map_addr(addr), (db_addr_t)addr, count);
    242       1.1       cgd }
    243       1.1       cgd 
    244       1.1       cgd /* list breakpoints */
    245       1.6  christos /*ARGSUSED*/
    246       1.1       cgd void
    247  1.14.2.3   nathanw db_listbreak_cmd(db_expr_t addr, int have_addr, db_expr_t count, char * modif)
    248       1.1       cgd {
    249  1.14.2.3   nathanw 
    250       1.1       cgd 	db_list_breakpoints();
    251       1.1       cgd }
    252       1.1       cgd 
    253      1.14       mrg #include <uvm/uvm_extern.h>
    254       1.1       cgd 
    255       1.1       cgd /*
    256       1.1       cgd  *	We want ddb to be usable before most of the kernel has been
    257       1.1       cgd  *	initialized.  In particular, current_thread() or kernel_map
    258       1.1       cgd  *	(or both) may be null.
    259       1.1       cgd  */
    260       1.1       cgd 
    261       1.1       cgd boolean_t
    262  1.14.2.3   nathanw db_map_equal(struct vm_map *map1, struct vm_map *map2)
    263       1.1       cgd {
    264  1.14.2.3   nathanw 
    265       1.1       cgd 	return ((map1 == map2) ||
    266       1.1       cgd 		((map1 == NULL) && (map2 == kernel_map)) ||
    267       1.1       cgd 		((map1 == kernel_map) && (map2 == NULL)));
    268       1.1       cgd }
    269       1.1       cgd 
    270       1.1       cgd boolean_t
    271  1.14.2.3   nathanw db_map_current(struct vm_map *map)
    272       1.1       cgd {
    273       1.1       cgd #if 0
    274       1.1       cgd 	thread_t	thread;
    275       1.1       cgd 
    276       1.1       cgd 	return ((map == NULL) ||
    277       1.1       cgd 		(map == kernel_map) ||
    278       1.1       cgd 		(((thread = current_thread()) != NULL) &&
    279       1.1       cgd 		 (map == thread->task->map)));
    280       1.1       cgd #else
    281  1.14.2.3   nathanw 
    282       1.1       cgd 	return (1);
    283       1.1       cgd #endif
    284       1.1       cgd }
    285       1.1       cgd 
    286  1.14.2.1   nathanw struct vm_map *
    287  1.14.2.3   nathanw db_map_addr(vaddr_t addr)
    288       1.1       cgd {
    289       1.1       cgd #if 0
    290       1.1       cgd 	thread_t	thread;
    291       1.1       cgd 
    292       1.1       cgd 	/*
    293       1.1       cgd 	 *	We want to return kernel_map for all
    294       1.1       cgd 	 *	non-user addresses, even when debugging
    295       1.1       cgd 	 *	kernel tasks with their own maps.
    296       1.1       cgd 	 */
    297       1.1       cgd 
    298  1.14.2.3   nathanw 	if ((VM_MIN_ADDRESS <= addr) && (addr < VM_MAX_ADDRESS) &&
    299       1.1       cgd 	    ((thread = current_thread()) != NULL))
    300  1.14.2.3   nathanw 		return thread->task->map;
    301       1.1       cgd 	else
    302       1.1       cgd #endif
    303  1.14.2.3   nathanw 		return kernel_map;
    304       1.1       cgd }
    305