Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.9
      1 /*	$NetBSD: db_examine.c,v 1.9 1994/11/17 04:51:50 gwr Exp $	*/
      2 
      3 /*
      4  * Mach Operating System
      5  * Copyright (c) 1991,1990 Carnegie Mellon University
      6  * All Rights Reserved.
      7  *
      8  * Permission to use, copy, modify and distribute this software and its
      9  * documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  *
     28  *	Author: David B. Golub, Carnegie Mellon University
     29  *	Date:	7/90
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/proc.h>
     34 
     35 #include <machine/db_machdep.h>		/* type definitions */
     36 
     37 #include <ddb/db_lex.h>
     38 #include <ddb/db_output.h>
     39 #include <ddb/db_command.h>
     40 #include <ddb/db_sym.h>
     41 
     42 char	db_examine_format[TOK_STRING_SIZE] = "x";
     43 
     44 extern	db_addr_t db_disasm(/* db_addr_t, boolean_t */);
     45 			/* instruction disassembler */
     46 
     47 /*
     48  * Examine (print) data.  Syntax is:
     49  *		x/[bhl][cdiorsuxz]*
     50  * For example, the command:
     51  *  	x/bxxxx
     52  * should print:
     53  *  	address:  01  23  45  67
     54  */
     55 /*ARGSUSED*/
     56 void
     57 db_examine_cmd(addr, have_addr, count, modif)
     58 	db_expr_t	addr;
     59 	int		have_addr;
     60 	db_expr_t	count;
     61 	char *		modif;
     62 {
     63 	if (modif[0] != '\0')
     64 		db_strcpy(db_examine_format, modif);
     65 
     66 	if (count == -1)
     67 		count = 1;
     68 
     69 	db_examine((db_addr_t) addr, db_examine_format, count);
     70 }
     71 
     72 db_examine(addr, fmt, count)
     73 	register
     74 		db_addr_t	addr;
     75 	char *		fmt;	/* format string */
     76 	int		count;	/* repeat count */
     77 {
     78 	int		c;
     79 	db_expr_t	value;
     80 	int		size;
     81 	int		width;
     82 	char *		fp;
     83 
     84 	while (--count >= 0) {
     85 		fp = fmt;
     86 		size = 4;
     87 		width = 12;
     88 		while ((c = *fp++) != 0) {
     89 			if (db_print_position() == 0) {
     90 				/* Always print the address. */
     91 				db_printsym(addr, DB_STGY_ANY);
     92 				db_printf(":\t");
     93 				db_prev = addr;
     94 			}
     95 			switch (c) {
     96 			case 'b':	/* byte */
     97 				size = 1;
     98 				width = 4;
     99 				break;
    100 			case 'h':	/* half-word */
    101 				size = 2;
    102 				width = 8;
    103 				break;
    104 			case 'l':	/* long-word */
    105 				size = 4;
    106 				width = 12;
    107 				break;
    108 			case 'a':	/* address */
    109 				db_printf("= 0x%x\n", addr);
    110 				break;
    111 			case 'r':	/* signed, current radix */
    112 				value = db_get_value(addr, size, TRUE);
    113 				addr += size;
    114 				db_printf("%-*r", width, value);
    115 				break;
    116 			case 'x':	/* unsigned hex */
    117 				value = db_get_value(addr, size, FALSE);
    118 				addr += size;
    119 				db_printf("%-*x", width, value);
    120 				break;
    121 			case 'z':	/* signed hex */
    122 				value = db_get_value(addr, size, TRUE);
    123 				addr += size;
    124 				db_printf("%-*z", width, value);
    125 				break;
    126 			case 'd':	/* signed decimal */
    127 				value = db_get_value(addr, size, TRUE);
    128 				addr += size;
    129 				db_printf("%-*d", width, value);
    130 				break;
    131 			case 'u':	/* unsigned decimal */
    132 				value = db_get_value(addr, size, FALSE);
    133 				addr += size;
    134 				db_printf("%-*u", width, value);
    135 				break;
    136 			case 'o':	/* unsigned octal */
    137 				value = db_get_value(addr, size, FALSE);
    138 				addr += size;
    139 				db_printf("%-*o", width, value);
    140 				break;
    141 			case 'c':	/* character */
    142 				value = db_get_value(addr, 1, FALSE);
    143 				addr += 1;
    144 				if (value >= ' ' && value <= '~')
    145 					db_printf("%c", value);
    146 				else
    147 					db_printf("\\%03o", value);
    148 				break;
    149 			case 's':	/* null-terminated string */
    150 				for (;;) {
    151 					value = db_get_value(addr, 1, FALSE);
    152 					addr += 1;
    153 					if (value == 0)
    154 						break;
    155 					if (value >= ' ' && value <= '~')
    156 						db_printf("%c", value);
    157 					else
    158 						db_printf("\\%03o", value);
    159 				}
    160 				break;
    161 			case 'i':	/* instruction */
    162 				addr = db_disasm(addr, FALSE);
    163 				break;
    164 			case 'I':	/* instruction, alternate form */
    165 				addr = db_disasm(addr, TRUE);
    166 				break;
    167 			default:
    168 				break;
    169 			}
    170 			if (db_print_position() != 0)
    171 				db_end_line();
    172 		}
    173 	}
    174 	db_next = addr;
    175 }
    176 
    177 /*
    178  * Print value.
    179  */
    180 char	db_print_format = 'x';
    181 
    182 /*ARGSUSED*/
    183 void
    184 db_print_cmd(addr, have_addr, count, modif)
    185 	db_expr_t	addr;
    186 	int		have_addr;
    187 	db_expr_t	count;
    188 	char *		modif;
    189 {
    190 	db_expr_t	value;
    191 
    192 	if (modif[0] != '\0')
    193 		db_print_format = modif[0];
    194 
    195 	switch (db_print_format) {
    196 	case 'a':
    197 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
    198 		break;
    199 	case 'r':
    200 		db_printf("%11r", addr);
    201 		break;
    202 	case 'x':
    203 		db_printf("%8x", addr);
    204 		break;
    205 	case 'z':
    206 		db_printf("%8z", addr);
    207 		break;
    208 	case 'd':
    209 		db_printf("%11d", addr);
    210 		break;
    211 	case 'u':
    212 		db_printf("%11u", addr);
    213 		break;
    214 	case 'o':
    215 		db_printf("%16o", addr);
    216 		break;
    217 	case 'c':
    218 		value = addr & 0xFF;
    219 		if (value >= ' ' && value <= '~')
    220 			db_printf("%c", value);
    221 		else
    222 			db_printf("\\%03o", value);
    223 		break;
    224 	}
    225 	db_printf("\n");
    226 }
    227 
    228 db_print_loc_and_inst(loc)
    229 	db_addr_t	loc;
    230 {
    231 	db_printsym(loc, DB_STGY_PROC);
    232 	db_printf(":\t");
    233 	(void) db_disasm(loc, FALSE);
    234 }
    235 
    236 db_strcpy(dst, src)
    237 	register char *dst;
    238 	register char *src;
    239 {
    240 	while (*dst++ = *src++)
    241 		;
    242 }
    243 
    244 /*
    245  * Search for a value in memory.
    246  * Syntax: search [/bhl] addr value [mask] [,count]
    247  */
    248 void
    249 db_search_cmd()
    250 {
    251 	int		t;
    252 	db_addr_t	addr;
    253 	int		size;
    254 	db_expr_t	value;
    255 	db_expr_t	mask;
    256 	unsigned int	count;
    257 
    258 	t = db_read_token();
    259 	if (t == tSLASH) {
    260 		t = db_read_token();
    261 		if (t != tIDENT) {
    262 			bad_modifier:
    263 			db_printf("Bad modifier\n");
    264 			db_flush_lex();
    265 			return;
    266 		}
    267 
    268 		if (!strcmp(db_tok_string, "b"))
    269 			size = 1;
    270 		else if (!strcmp(db_tok_string, "h"))
    271 			size = 2;
    272 		else if (!strcmp(db_tok_string, "l"))
    273 			size = 4;
    274 		else
    275 			goto bad_modifier;
    276 	} else {
    277 		db_unread_token(t);
    278 		size = 4;
    279 	}
    280 
    281 	if (!db_expression(&addr)) {
    282 		db_printf("Address missing\n");
    283 		db_flush_lex();
    284 		return;
    285 	}
    286 
    287 	if (!db_expression(&value)) {
    288 		db_printf("Value missing\n");
    289 		db_flush_lex();
    290 		return;
    291 	}
    292 
    293 	if (!db_expression(&mask))
    294 		mask = 0xffffffff;
    295 
    296 	t = db_read_token();
    297 	if (t == tCOMMA) {
    298 		if (!db_expression(&count)) {
    299 			db_printf("Count missing\n");
    300 			db_flush_lex();
    301 			return;
    302 		}
    303 	} else {
    304 		db_unread_token(t);
    305 		count = -1;		/* effectively forever */
    306 	}
    307 	db_skip_to_eol();
    308 
    309 	db_search(addr, size, value, mask, count);
    310 }
    311 
    312 db_search(addr, size, value, mask, count)
    313 	register
    314 	db_addr_t	addr;
    315 	int		size;
    316 	db_expr_t	value;
    317 	db_expr_t	mask;
    318 	unsigned int	count;
    319 {
    320 	while (count-- != 0) {
    321 		db_prev = addr;
    322 		if ((db_get_value(addr, size, FALSE) & mask) == value)
    323 			break;
    324 		addr += size;
    325 	}
    326 	db_next = addr;
    327 }
    328