Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.19
      1 /*	$NetBSD: db_examine.c,v 1.19 2000/08/09 19:51:44 tv 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 	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 	char		tbuf[24];
     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, db_printf);
     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_format_radix(tbuf, 24, value, FALSE);
    120 				db_printf("%-*s", width, tbuf);
    121 				break;
    122 			case 'x':	/* unsigned hex */
    123 				value = db_get_value(addr, size, FALSE);
    124 				addr += size;
    125 				db_printf("%-*lx", width, value);
    126 				break;
    127 			case 'z':	/* signed hex */
    128 				value = db_get_value(addr, size, TRUE);
    129 				addr += size;
    130 				db_format_hex(tbuf, 24, value, FALSE);
    131 				db_printf("%-*s", width, tbuf);
    132 				break;
    133 			case 'd':	/* signed decimal */
    134 				value = db_get_value(addr, size, TRUE);
    135 				addr += size;
    136 				db_printf("%-*ld", width, value);
    137 				break;
    138 			case 'u':	/* unsigned decimal */
    139 				value = db_get_value(addr, size, FALSE);
    140 				addr += size;
    141 				db_printf("%-*lu", width, value);
    142 				break;
    143 			case 'o':	/* unsigned octal */
    144 				value = db_get_value(addr, size, FALSE);
    145 				addr += size;
    146 				db_printf("%-*lo", width, value);
    147 				break;
    148 			case 'c':	/* character */
    149 				value = db_get_value(addr, 1, FALSE);
    150 				addr += 1;
    151 				if (value >= ' ' && value <= '~')
    152 					db_printf("%c", (char)value);
    153 				else
    154 					db_printf("\\%03lo", value);
    155 				break;
    156 			case 's':	/* null-terminated string */
    157 				for (;;) {
    158 					value = db_get_value(addr, 1, FALSE);
    159 					addr += 1;
    160 					if (value == 0)
    161 						break;
    162 					if (value >= ' ' && value <= '~')
    163 						db_printf("%c", (char)value);
    164 					else
    165 						db_printf("\\%03lo", value);
    166 				}
    167 				break;
    168 			case 'i':	/* instruction */
    169 				addr = db_disasm(addr, FALSE);
    170 				break;
    171 			case 'I':	/* instruction, alternate form */
    172 				addr = db_disasm(addr, TRUE);
    173 				break;
    174 			default:
    175 				break;
    176 			}
    177 			if (db_print_position() != 0)
    178 				db_end_line();
    179 		}
    180 	}
    181 	db_next = addr;
    182 }
    183 
    184 /*
    185  * Print value.
    186  */
    187 char	db_print_format = 'x';
    188 
    189 /*ARGSUSED*/
    190 void
    191 db_print_cmd(addr, have_addr, count, modif)
    192 	db_expr_t	addr;
    193 	int		have_addr;
    194 	db_expr_t	count;
    195 	char *		modif;
    196 {
    197 	db_expr_t	value;
    198 
    199 	if (modif[0] != '\0')
    200 		db_print_format = modif[0];
    201 
    202 	switch (db_print_format) {
    203 	case 'a':
    204 		db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
    205 		break;
    206 	case 'r':
    207 		{
    208 			char tbuf[24];
    209 
    210 			db_format_radix(tbuf, 24, addr, FALSE);
    211 			db_printf("%11s", tbuf);
    212 			break;
    213 		}
    214 	case 'x':
    215 		db_printf("%8lx", addr);
    216 		break;
    217 	case 'z':
    218 		{
    219 			char tbuf[24];
    220 
    221 			db_format_hex(tbuf, 24, addr, FALSE);
    222 			db_printf("%8s", tbuf);
    223 			break;
    224 		}
    225 	case 'd':
    226 		db_printf("%11ld", addr);
    227 		break;
    228 	case 'u':
    229 		db_printf("%11lu", addr);
    230 		break;
    231 	case 'o':
    232 		db_printf("%16lo", addr);
    233 		break;
    234 	case 'c':
    235 		value = addr & 0xFF;
    236 		if (value >= ' ' && value <= '~')
    237 			db_printf("%c", (char)value);
    238 		else
    239 			db_printf("\\%03lo", value);
    240 		break;
    241 	}
    242 	db_printf("\n");
    243 }
    244 
    245 void
    246 db_print_loc_and_inst(loc)
    247 	db_addr_t	loc;
    248 {
    249 	db_printsym(loc, DB_STGY_PROC, db_printf);
    250 	db_printf(":\t");
    251 	(void) db_disasm(loc, FALSE);
    252 }
    253 
    254 void
    255 db_strcpy(dst, src)
    256 	char *dst;
    257 	char *src;
    258 {
    259 	while ((*dst++ = *src++) != '\0')
    260 		;
    261 }
    262 
    263 /*
    264  * Search for a value in memory.
    265  * Syntax: search [/bhl] addr value [mask] [,count]
    266  */
    267 /*ARGSUSED*/
    268 void
    269 db_search_cmd(daddr, have_addr, dcount, modif)
    270 	db_expr_t	daddr;
    271 	int		have_addr;
    272 	db_expr_t	dcount;
    273 	char *		modif;
    274 {
    275 	int		t;
    276 	db_addr_t	addr;
    277 	int		size;
    278 	db_expr_t	value;
    279 	db_expr_t	mask;
    280 	db_expr_t	count;
    281 
    282 	t = db_read_token();
    283 	if (t == tSLASH) {
    284 		t = db_read_token();
    285 		if (t != tIDENT) {
    286 			bad_modifier:
    287 			db_printf("Bad modifier\n");
    288 			db_flush_lex();
    289 			return;
    290 		}
    291 
    292 		if (!strcmp(db_tok_string, "b"))
    293 			size = 1;
    294 		else if (!strcmp(db_tok_string, "h"))
    295 			size = 2;
    296 		else if (!strcmp(db_tok_string, "l"))
    297 			size = 4;
    298 		else
    299 			goto bad_modifier;
    300 	} else {
    301 		db_unread_token(t);
    302 		size = 4;
    303 	}
    304 
    305 	if (!db_expression(&value)) {
    306 		db_printf("Address missing\n");
    307 		db_flush_lex();
    308 		return;
    309 	}
    310 	addr = (db_addr_t) value;
    311 
    312 	if (!db_expression(&value)) {
    313 		db_printf("Value missing\n");
    314 		db_flush_lex();
    315 		return;
    316 	}
    317 
    318 	if (!db_expression(&mask))
    319 		mask = (int) ~0;
    320 
    321 	t = db_read_token();
    322 	if (t == tCOMMA) {
    323 		if (!db_expression(&count)) {
    324 			db_printf("Count missing\n");
    325 			db_flush_lex();
    326 			return;
    327 		}
    328 	} else {
    329 		db_unread_token(t);
    330 		count = -1;		/* effectively forever */
    331 	}
    332 	db_skip_to_eol();
    333 
    334 	db_search(addr, size, value, mask, count);
    335 }
    336 
    337 void
    338 db_search(addr, size, value, mask, count)
    339 	db_addr_t	addr;
    340 	int		size;
    341 	db_expr_t	value;
    342 	db_expr_t	mask;
    343 	unsigned int	count;
    344 {
    345 	while (count-- != 0) {
    346 		db_prev = addr;
    347 		if ((db_get_value(addr, size, FALSE) & mask) == value)
    348 			break;
    349 		addr += size;
    350 	}
    351 	db_next = addr;
    352 }
    353