Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.5.2.1
      1  1.5.2.1  mycroft /*	$NetBSD: db_examine.c,v 1.5.2.1 1994/10/06 05:21:51 mycroft Exp $	*/
      2      1.4      cgd 
      3  1.5.2.1  mycroft /*
      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.5.2.1  mycroft  *
      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.5.2.1  mycroft  *
     14  1.5.2.1  mycroft  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
     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.5.2.1  mycroft  *
     18      1.1      cgd  * Carnegie Mellon requests users of this software to return to
     19  1.5.2.1  mycroft  *
     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.5.2.1  mycroft  *
     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.3  mycroft #include <sys/param.h>
     33      1.3  mycroft #include <sys/proc.h>
     34      1.3  mycroft 
     35      1.1      cgd #include <machine/db_machdep.h>		/* type definitions */
     36      1.1      cgd 
     37      1.1      cgd #include <ddb/db_lex.h>
     38      1.1      cgd #include <ddb/db_output.h>
     39      1.1      cgd #include <ddb/db_command.h>
     40      1.1      cgd #include <ddb/db_sym.h>
     41      1.1      cgd 
     42      1.1      cgd char	db_examine_format[TOK_STRING_SIZE] = "x";
     43      1.1      cgd 
     44      1.1      cgd extern	db_addr_t db_disasm(/* db_addr_t, boolean_t */);
     45      1.1      cgd 			/* instruction disassembler */
     46      1.1      cgd 
     47      1.1      cgd /*
     48  1.5.2.1  mycroft  * Examine (print) data.  Syntax is:
     49  1.5.2.1  mycroft  *		x/[bhl][cdiorsuxz]*
     50  1.5.2.1  mycroft  * For example, the command:
     51  1.5.2.1  mycroft  *  	x/bxxxx
     52  1.5.2.1  mycroft  * should print:
     53  1.5.2.1  mycroft  *  	address:  01  23  45  67
     54      1.1      cgd  */
     55      1.1      cgd /*ARGSUSED*/
     56      1.1      cgd void
     57      1.1      cgd db_examine_cmd(addr, have_addr, count, modif)
     58      1.1      cgd 	db_expr_t	addr;
     59      1.1      cgd 	int		have_addr;
     60      1.1      cgd 	db_expr_t	count;
     61      1.1      cgd 	char *		modif;
     62      1.1      cgd {
     63      1.1      cgd 	if (modif[0] != '\0')
     64  1.5.2.1  mycroft 		db_strcpy(db_examine_format, modif);
     65      1.1      cgd 
     66      1.1      cgd 	if (count == -1)
     67  1.5.2.1  mycroft 		count = 1;
     68      1.1      cgd 
     69      1.1      cgd 	db_examine((db_addr_t) addr, db_examine_format, count);
     70      1.1      cgd }
     71      1.1      cgd 
     72      1.1      cgd db_examine(addr, fmt, count)
     73      1.1      cgd 	register
     74  1.5.2.1  mycroft 		db_addr_t	addr;
     75      1.1      cgd 	char *		fmt;	/* format string */
     76      1.1      cgd 	int		count;	/* repeat count */
     77      1.1      cgd {
     78      1.1      cgd 	int		c;
     79      1.1      cgd 	db_expr_t	value;
     80      1.1      cgd 	int		size;
     81      1.1      cgd 	int		width;
     82      1.1      cgd 	char *		fp;
     83      1.1      cgd 
     84      1.1      cgd 	while (--count >= 0) {
     85  1.5.2.1  mycroft 		fp = fmt;
     86  1.5.2.1  mycroft 		size = 4;
     87  1.5.2.1  mycroft 		width = 16;
     88  1.5.2.1  mycroft 		while ((c = *fp++) != 0) {
     89  1.5.2.1  mycroft 			switch (c) {
     90  1.5.2.1  mycroft 			case 'b':
     91  1.5.2.1  mycroft 				size = 1;
     92  1.5.2.1  mycroft 				width = 4;
     93  1.5.2.1  mycroft 				break;
     94  1.5.2.1  mycroft 			case 'h':
     95  1.5.2.1  mycroft 				size = 2;
     96  1.5.2.1  mycroft 				width = 8;
     97  1.5.2.1  mycroft 				break;
     98  1.5.2.1  mycroft 			case 'l':
     99  1.5.2.1  mycroft 				size = 4;
    100  1.5.2.1  mycroft 				width = 16;
    101  1.5.2.1  mycroft 				break;
    102  1.5.2.1  mycroft 			case 'a':	/* address */
    103  1.5.2.1  mycroft 				/* always forces a new line */
    104  1.5.2.1  mycroft 				if (db_print_position() != 0)
    105  1.5.2.1  mycroft 					db_printf("\n");
    106  1.5.2.1  mycroft 				db_prev = addr;
    107  1.5.2.1  mycroft 				db_printsym(addr, DB_STGY_ANY);
    108  1.5.2.1  mycroft 				db_printf(":\t");
    109  1.5.2.1  mycroft 				break;
    110  1.5.2.1  mycroft 			}
    111      1.1      cgd 			if (db_print_position() == 0) {
    112      1.1      cgd 
    113  1.5.2.1  mycroft 				/* Always print the address. */
    114  1.5.2.1  mycroft 				db_printsym(addr, DB_STGY_ANY);
    115  1.5.2.1  mycroft 				db_printf(":\t");
    116      1.1      cgd 
    117  1.5.2.1  mycroft 				db_prev = addr;
    118  1.5.2.1  mycroft 			}
    119      1.1      cgd 			switch (c) {
    120  1.5.2.1  mycroft 			case 'r':	/* signed, current radix */
    121      1.1      cgd 				value = db_get_value(addr, size, TRUE);
    122      1.1      cgd 				addr += size;
    123      1.1      cgd 				db_printf("%-*r", width, value);
    124      1.1      cgd 				break;
    125  1.5.2.1  mycroft 			case 'x':	/* unsigned hex */
    126      1.1      cgd 				value = db_get_value(addr, size, FALSE);
    127      1.1      cgd 				addr += size;
    128      1.1      cgd 				db_printf("%-*x", width, value);
    129      1.1      cgd 				break;
    130  1.5.2.1  mycroft 			case 'z':	/* signed hex */
    131      1.1      cgd 				value = db_get_value(addr, size, TRUE);
    132      1.1      cgd 				addr += size;
    133      1.1      cgd 				db_printf("%-*z", width, value);
    134      1.1      cgd 				break;
    135  1.5.2.1  mycroft 			case 'd':	/* signed decimal */
    136      1.1      cgd 				value = db_get_value(addr, size, TRUE);
    137      1.1      cgd 				addr += size;
    138      1.1      cgd 				db_printf("%-*d", width, value);
    139      1.1      cgd 				break;
    140  1.5.2.1  mycroft 			case 'u':	/* unsigned decimal */
    141      1.1      cgd 				value = db_get_value(addr, size, FALSE);
    142      1.1      cgd 				addr += size;
    143      1.1      cgd 				db_printf("%-*u", width, value);
    144      1.1      cgd 				break;
    145  1.5.2.1  mycroft 			case 'o':	/* unsigned octal */
    146      1.1      cgd 				value = db_get_value(addr, size, FALSE);
    147      1.1      cgd 				addr += size;
    148      1.1      cgd 				db_printf("%-*o", width, value);
    149      1.1      cgd 				break;
    150  1.5.2.1  mycroft 			case 'c':	/* character */
    151      1.1      cgd 				value = db_get_value(addr, 1, FALSE);
    152      1.1      cgd 				addr += 1;
    153      1.1      cgd 				if (value >= ' ' && value <= '~')
    154  1.5.2.1  mycroft 					db_printf("%c", value);
    155      1.1      cgd 				else
    156  1.5.2.1  mycroft 					db_printf("\\%03o", value);
    157      1.1      cgd 				break;
    158  1.5.2.1  mycroft 			case 's':	/* null-terminated string */
    159      1.1      cgd 				for (;;) {
    160  1.5.2.1  mycroft 					value = db_get_value(addr, 1, FALSE);
    161  1.5.2.1  mycroft 					addr += 1;
    162  1.5.2.1  mycroft 					if (value == 0)
    163  1.5.2.1  mycroft 						break;
    164  1.5.2.1  mycroft 					if (value >= ' ' && value <= '~')
    165  1.5.2.1  mycroft 						db_printf("%c", value);
    166  1.5.2.1  mycroft 					else
    167  1.5.2.1  mycroft 						db_printf("\\%03o", value);
    168      1.1      cgd 				}
    169      1.1      cgd 				break;
    170  1.5.2.1  mycroft 			case 'i':	/* instruction */
    171      1.1      cgd 				addr = db_disasm(addr, FALSE);
    172      1.1      cgd 				break;
    173  1.5.2.1  mycroft 			case 'I':	/* instruction, alternate form */
    174      1.1      cgd 				addr = db_disasm(addr, TRUE);
    175      1.1      cgd 				break;
    176  1.5.2.1  mycroft 			default:
    177      1.1      cgd 				break;
    178      1.1      cgd 			}
    179      1.1      cgd 			if (db_print_position() != 0)
    180  1.5.2.1  mycroft 				db_end_line();
    181      1.1      cgd 		}
    182      1.1      cgd 	}
    183      1.1      cgd 	db_next = addr;
    184      1.1      cgd }
    185      1.1      cgd 
    186      1.1      cgd /*
    187      1.1      cgd  * Print value.
    188      1.1      cgd  */
    189      1.1      cgd char	db_print_format = 'x';
    190      1.1      cgd 
    191      1.1      cgd /*ARGSUSED*/
    192      1.1      cgd void
    193      1.1      cgd db_print_cmd(addr, have_addr, count, modif)
    194      1.1      cgd 	db_expr_t	addr;
    195      1.1      cgd 	int		have_addr;
    196      1.1      cgd 	db_expr_t	count;
    197      1.1      cgd 	char *		modif;
    198      1.1      cgd {
    199      1.1      cgd 	db_expr_t	value;
    200      1.1      cgd 
    201      1.1      cgd 	if (modif[0] != '\0')
    202  1.5.2.1  mycroft 		db_print_format = modif[0];
    203      1.1      cgd 
    204      1.1      cgd 	switch (db_print_format) {
    205  1.5.2.1  mycroft 	case 'a':
    206      1.1      cgd 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
    207      1.1      cgd 		break;
    208  1.5.2.1  mycroft 	case 'r':
    209      1.1      cgd 		db_printf("%11r", addr);
    210      1.1      cgd 		break;
    211  1.5.2.1  mycroft 	case 'x':
    212      1.1      cgd 		db_printf("%8x", addr);
    213      1.1      cgd 		break;
    214  1.5.2.1  mycroft 	case 'z':
    215      1.1      cgd 		db_printf("%8z", addr);
    216      1.1      cgd 		break;
    217  1.5.2.1  mycroft 	case 'd':
    218      1.1      cgd 		db_printf("%11d", addr);
    219      1.1      cgd 		break;
    220  1.5.2.1  mycroft 	case 'u':
    221      1.1      cgd 		db_printf("%11u", addr);
    222      1.1      cgd 		break;
    223  1.5.2.1  mycroft 	case 'o':
    224      1.1      cgd 		db_printf("%16o", addr);
    225      1.1      cgd 		break;
    226  1.5.2.1  mycroft 	case 'c':
    227      1.1      cgd 		value = addr & 0xFF;
    228      1.1      cgd 		if (value >= ' ' && value <= '~')
    229  1.5.2.1  mycroft 			db_printf("%c", value);
    230      1.1      cgd 		else
    231  1.5.2.1  mycroft 			db_printf("\\%03o", value);
    232      1.1      cgd 		break;
    233      1.1      cgd 	}
    234      1.1      cgd 	db_printf("\n");
    235      1.1      cgd }
    236      1.1      cgd 
    237      1.1      cgd db_print_loc_and_inst(loc)
    238      1.1      cgd 	db_addr_t	loc;
    239      1.1      cgd {
    240      1.1      cgd 	db_printsym(loc, DB_STGY_PROC);
    241      1.1      cgd 	db_printf(":\t");
    242      1.1      cgd 	(void) db_disasm(loc, TRUE);
    243      1.1      cgd }
    244      1.1      cgd 
    245      1.1      cgd db_strcpy(dst, src)
    246      1.1      cgd 	register char *dst;
    247      1.1      cgd 	register char *src;
    248      1.1      cgd {
    249      1.1      cgd 	while (*dst++ = *src++)
    250  1.5.2.1  mycroft 		;
    251      1.1      cgd }
    252      1.1      cgd 
    253      1.1      cgd /*
    254      1.1      cgd  * Search for a value in memory.
    255      1.1      cgd  * Syntax: search [/bhl] addr value [mask] [,count]
    256      1.1      cgd  */
    257      1.1      cgd void
    258      1.1      cgd db_search_cmd()
    259      1.1      cgd {
    260      1.1      cgd 	int		t;
    261      1.1      cgd 	db_addr_t	addr;
    262      1.1      cgd 	int		size;
    263      1.1      cgd 	db_expr_t	value;
    264      1.1      cgd 	db_expr_t	mask;
    265      1.1      cgd 	unsigned int	count;
    266      1.1      cgd 
    267      1.1      cgd 	t = db_read_token();
    268      1.1      cgd 	if (t == tSLASH) {
    269  1.5.2.1  mycroft 		t = db_read_token();
    270  1.5.2.1  mycroft 		if (t != tIDENT) {
    271  1.5.2.1  mycroft 			bad_modifier:
    272  1.5.2.1  mycroft 			db_printf("Bad modifier\n");
    273  1.5.2.1  mycroft 			db_flush_lex();
    274  1.5.2.1  mycroft 			return;
    275  1.5.2.1  mycroft 		}
    276      1.1      cgd 
    277  1.5.2.1  mycroft 		if (!strcmp(db_tok_string, "b"))
    278  1.5.2.1  mycroft 			size = 1;
    279  1.5.2.1  mycroft 		else if (!strcmp(db_tok_string, "h"))
    280  1.5.2.1  mycroft 			size = 2;
    281  1.5.2.1  mycroft 		else if (!strcmp(db_tok_string, "l"))
    282  1.5.2.1  mycroft 			size = 4;
    283  1.5.2.1  mycroft 		else
    284  1.5.2.1  mycroft 			goto bad_modifier;
    285      1.1      cgd 	} else {
    286  1.5.2.1  mycroft 		db_unread_token(t);
    287  1.5.2.1  mycroft 		size = 4;
    288      1.1      cgd 	}
    289      1.1      cgd 
    290      1.1      cgd 	if (!db_expression(&addr)) {
    291  1.5.2.1  mycroft 		db_printf("Address missing\n");
    292  1.5.2.1  mycroft 		db_flush_lex();
    293  1.5.2.1  mycroft 		return;
    294      1.1      cgd 	}
    295      1.1      cgd 
    296      1.1      cgd 	if (!db_expression(&value)) {
    297  1.5.2.1  mycroft 		db_printf("Value missing\n");
    298  1.5.2.1  mycroft 		db_flush_lex();
    299  1.5.2.1  mycroft 		return;
    300      1.1      cgd 	}
    301      1.1      cgd 
    302      1.1      cgd 	if (!db_expression(&mask))
    303  1.5.2.1  mycroft 		mask = 0xffffffff;
    304      1.1      cgd 
    305      1.1      cgd 	t = db_read_token();
    306      1.1      cgd 	if (t == tCOMMA) {
    307  1.5.2.1  mycroft 		if (!db_expression(&count)) {
    308  1.5.2.1  mycroft 			db_printf("Count missing\n");
    309  1.5.2.1  mycroft 			db_flush_lex();
    310  1.5.2.1  mycroft 			return;
    311  1.5.2.1  mycroft 		}
    312      1.1      cgd 	} else {
    313  1.5.2.1  mycroft 		db_unread_token(t);
    314  1.5.2.1  mycroft 		count = -1;		/* effectively forever */
    315      1.1      cgd 	}
    316      1.1      cgd 	db_skip_to_eol();
    317      1.1      cgd 
    318      1.1      cgd 	db_search(addr, size, value, mask, count);
    319      1.1      cgd }
    320      1.1      cgd 
    321      1.1      cgd db_search(addr, size, value, mask, count)
    322      1.1      cgd 	register
    323      1.1      cgd 	db_addr_t	addr;
    324      1.1      cgd 	int		size;
    325      1.1      cgd 	db_expr_t	value;
    326      1.1      cgd 	db_expr_t	mask;
    327      1.1      cgd 	unsigned int	count;
    328      1.1      cgd {
    329      1.1      cgd 	while (count-- != 0) {
    330      1.1      cgd 		db_prev = addr;
    331      1.1      cgd 		if ((db_get_value(addr, size, FALSE) & mask) == value)
    332      1.1      cgd 			break;
    333      1.1      cgd 		addr += size;
    334      1.1      cgd 	}
    335      1.1      cgd 	db_next = addr;
    336      1.1      cgd }
    337