Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.16
      1 /*	$NetBSD: db_examine.c,v 1.16 1999/05/31 06:53:45 ross 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 "AS IS"
     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 #include <ddb/db_access.h>
     42 #include <ddb/db_extern.h>
     43 #include <ddb/db_interface.h>
     44 
     45 char	db_examine_format[TOK_STRING_SIZE] = "x";
     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 void
     73 db_examine(addr, fmt, count)
     74 	register
     75 	    db_addr_t	addr;
     76 	char *		fmt;	/* format string */
     77 	int		count;	/* repeat count */
     78 {
     79 	int		c;
     80 	db_expr_t	value;
     81 	int		size;
     82 	int		width;
     83 	char *		fp;
     84 
     85 	while (--count >= 0) {
     86 		fp = fmt;
     87 		size = 4;
     88 		width = 12;
     89 		while ((c = *fp++) != 0) {
     90 			if (db_print_position() == 0) {
     91 				/* Always print the address. */
     92 				db_printsym(addr, DB_STGY_ANY);
     93 				db_printf(":\t");
     94 				db_prev = addr;
     95 			}
     96 			switch (c) {
     97 			case 'b':	/* byte */
     98 				size = 1;
     99 				width = 4;
    100 				break;
    101 			case 'h':	/* half-word */
    102 				size = 2;
    103 				width = 8;
    104 				break;
    105 			case 'l':	/* long-word */
    106 				size = 4;
    107 				width = 12;
    108 				break;
    109 			case 'L':	/* implementation maximum */
    110 				size = sizeof value;
    111 				width = 12 * (sizeof value / 4);
    112 				break;
    113 			case 'a':	/* address */
    114 				db_printf("= 0x%lx\n", addr);
    115 				break;
    116 			case 'r':	/* signed, current radix */
    117 				value = db_get_value(addr, size, TRUE);
    118 				addr += size;
    119 				db_printf("%-*lr", width, value);
    120 				break;
    121 			case 'x':	/* unsigned hex */
    122 				value = db_get_value(addr, size, FALSE);
    123 				addr += size;
    124 				db_printf("%-*lx", width, value);
    125 				break;
    126 			case 'z':	/* signed hex */
    127 				value = db_get_value(addr, size, TRUE);
    128 				addr += size;
    129 				db_printf("%-*lz", width, value);
    130 				break;
    131 			case 'd':	/* signed decimal */
    132 				value = db_get_value(addr, size, TRUE);
    133 				addr += size;
    134 				db_printf("%-*ld", width, value);
    135 				break;
    136 			case 'u':	/* unsigned decimal */
    137 				value = db_get_value(addr, size, FALSE);
    138 				addr += size;
    139 				db_printf("%-*lu", width, value);
    140 				break;
    141 			case 'o':	/* unsigned octal */
    142 				value = db_get_value(addr, size, FALSE);
    143 				addr += size;
    144 				db_printf("%-*lo", width, value);
    145 				break;
    146 			case 'c':	/* character */
    147 				value = db_get_value(addr, 1, FALSE);
    148 				addr += 1;
    149 				if (value >= ' ' && value <= '~')
    150 					db_printf("%c", (char)value);
    151 				else
    152 					db_printf("\\%03lo", value);
    153 				break;
    154 			case 's':	/* null-terminated string */
    155 				for (;;) {
    156 					value = db_get_value(addr, 1, FALSE);
    157 					addr += 1;
    158 					if (value == 0)
    159 						break;
    160 					if (value >= ' ' && value <= '~')
    161 						db_printf("%c", (char)value);
    162 					else
    163 						db_printf("\\%03lo", value);
    164 				}
    165 				break;
    166 			case 'i':	/* instruction */
    167 				addr = db_disasm(addr, FALSE);
    168 				break;
    169 			case 'I':	/* instruction, alternate form */
    170 				addr = db_disasm(addr, TRUE);
    171 				break;
    172 			default:
    173 				break;
    174 			}
    175 			if (db_print_position() != 0)
    176 				db_end_line();
    177 		}
    178 	}
    179 	db_next = addr;
    180 }
    181 
    182 /*
    183  * Print value.
    184  */
    185 char	db_print_format = 'x';
    186 
    187 /*ARGSUSED*/
    188 void
    189 db_print_cmd(addr, have_addr, count, modif)
    190 	db_expr_t	addr;
    191 	int		have_addr;
    192 	db_expr_t	count;
    193 	char *		modif;
    194 {
    195 	db_expr_t	value;
    196 
    197 	if (modif[0] != '\0')
    198 		db_print_format = modif[0];
    199 
    200 	switch (db_print_format) {
    201 	case 'a':
    202 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
    203 		break;
    204 	case 'r':
    205 		db_printf("%11lr", addr);
    206 		break;
    207 	case 'x':
    208 		db_printf("%8lx", addr);
    209 		break;
    210 	case 'z':
    211 		db_printf("%8lz", addr);
    212 		break;
    213 	case 'd':
    214 		db_printf("%11ld", addr);
    215 		break;
    216 	case 'u':
    217 		db_printf("%11lu", addr);
    218 		break;
    219 	case 'o':
    220 		db_printf("%16lo", addr);
    221 		break;
    222 	case 'c':
    223 		value = addr & 0xFF;
    224 		if (value >= ' ' && value <= '~')
    225 			db_printf("%c", (char)value);
    226 		else
    227 			db_printf("\\%03lo", value);
    228 		break;
    229 	}
    230 	db_printf("\n");
    231 }
    232 
    233 void
    234 db_print_loc_and_inst(loc)
    235 	db_addr_t	loc;
    236 {
    237 	db_printsym(loc, DB_STGY_PROC);
    238 	db_printf(":\t");
    239 	(void) db_disasm(loc, FALSE);
    240 }
    241 
    242 void
    243 db_strcpy(dst, src)
    244 	register char *dst;
    245 	register char *src;
    246 {
    247 	while ((*dst++ = *src++) != '\0')
    248 		;
    249 }
    250 
    251 /*
    252  * Search for a value in memory.
    253  * Syntax: search [/bhl] addr value [mask] [,count]
    254  */
    255 /*ARGSUSED*/
    256 void
    257 db_search_cmd(daddr, have_addr, dcount, modif)
    258 	db_expr_t	daddr;
    259 	int		have_addr;
    260 	db_expr_t	dcount;
    261 	char *		modif;
    262 {
    263 	int		t;
    264 	db_addr_t	addr;
    265 	int		size;
    266 	db_expr_t	value;
    267 	db_expr_t	mask;
    268 	db_expr_t	count;
    269 
    270 	t = db_read_token();
    271 	if (t == tSLASH) {
    272 		t = db_read_token();
    273 		if (t != tIDENT) {
    274 			bad_modifier:
    275 			db_printf("Bad modifier\n");
    276 			db_flush_lex();
    277 			return;
    278 		}
    279 
    280 		if (!strcmp(db_tok_string, "b"))
    281 			size = 1;
    282 		else if (!strcmp(db_tok_string, "h"))
    283 			size = 2;
    284 		else if (!strcmp(db_tok_string, "l"))
    285 			size = 4;
    286 		else
    287 			goto bad_modifier;
    288 	} else {
    289 		db_unread_token(t);
    290 		size = 4;
    291 	}
    292 
    293 	if (!db_expression(&value)) {
    294 		db_printf("Address missing\n");
    295 		db_flush_lex();
    296 		return;
    297 	}
    298 	addr = (db_addr_t) value;
    299 
    300 	if (!db_expression(&value)) {
    301 		db_printf("Value missing\n");
    302 		db_flush_lex();
    303 		return;
    304 	}
    305 
    306 	if (!db_expression(&mask))
    307 		mask = (int) ~0;
    308 
    309 	t = db_read_token();
    310 	if (t == tCOMMA) {
    311 		if (!db_expression(&count)) {
    312 			db_printf("Count missing\n");
    313 			db_flush_lex();
    314 			return;
    315 		}
    316 	} else {
    317 		db_unread_token(t);
    318 		count = -1;		/* effectively forever */
    319 	}
    320 	db_skip_to_eol();
    321 
    322 	db_search(addr, size, value, mask, count);
    323 }
    324 
    325 void
    326 db_search(addr, size, value, mask, count)
    327 	register
    328 	db_addr_t	addr;
    329 	int		size;
    330 	db_expr_t	value;
    331 	db_expr_t	mask;
    332 	unsigned int	count;
    333 {
    334 	while (count-- != 0) {
    335 		db_prev = addr;
    336 		if ((db_get_value(addr, size, FALSE) & mask) == value)
    337 			break;
    338 		addr += size;
    339 	}
    340 	db_next = addr;
    341 }
    342