Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.37
      1 /*	$NetBSD: db_examine.c,v 1.37 2019/02/03 03:19:26 mrg 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/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: db_examine.c,v 1.37 2019/02/03 03:19:26 mrg Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/buf.h>
     38 #include <sys/proc.h>
     39 
     40 #include <ddb/ddb.h>
     41 
     42 static char	db_examine_format[TOK_STRING_SIZE] = "x";
     43 
     44 static void	db_examine(db_addr_t, char *, int);
     45 static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, unsigned int);
     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(db_expr_t addr, bool have_addr, db_expr_t count,
     58     const char *modif)
     59 {
     60 	if (modif[0] != '\0')
     61 		strlcpy(db_examine_format, modif, sizeof(db_examine_format));
     62 
     63 	if (count == -1)
     64 		count = 1;
     65 
     66 	db_examine((db_addr_t) addr, db_examine_format, count);
     67 }
     68 
     69 static void
     70 db_examine(db_addr_t addr, char *fmt, int count)
     71 {
     72 	int		i, c;
     73 	db_expr_t	value;
     74 	int		size;
     75 	int		width;
     76 	int		bytes;
     77 	char *		fp;
     78 	char		tbuf[24];
     79 
     80 	while (--count >= 0) {
     81 		fp = fmt;
     82 		size = 4;
     83 		width = 12;
     84 		while ((c = *fp++) != 0) {
     85 			if (db_print_position() == 0) {
     86 				/* Always print the address. */
     87 				db_printsym(addr, DB_STGY_ANY, db_printf);
     88 				db_printf(":\t");
     89 				db_prev = addr;
     90 			}
     91 			switch (c) {
     92 			case 'b':	/* byte */
     93 				size = 1;
     94 				width = 4;
     95 				break;
     96 			case 'h':	/* half-word */
     97 				size = 2;
     98 				width = 8;
     99 				break;
    100 			case 'l':	/* long-word */
    101 				size = 4;
    102 				width = 12;
    103 				break;
    104 			case 'q':
    105 				if (sizeof(db_expr_t) != sizeof(uint64_t)) {
    106 					size = -1;
    107 					db_error("q not supported\n");
    108 					/*NOTREACHED*/
    109 				}
    110 				/* FALLTHROUGH */
    111 			case 'L':	/* implementation maximum */
    112 				size = sizeof value;
    113 				width = 12 * (sizeof value / 4);
    114 				break;
    115 			case 'a':	/* address */
    116 				db_printf("= 0x%lx\n", (long)addr);
    117 				break;
    118 			case 'r':	/* signed, current radix */
    119 				value = db_get_value(addr, size, true);
    120 				addr += size;
    121 				db_format_radix(tbuf, 24, value, false);
    122 				db_printf("%-*s", width, tbuf);
    123 				break;
    124 			case 'x':	/* unsigned hex */
    125 				value = db_get_value(addr, size, false);
    126 				addr += size;
    127 				db_printf("%-*" DDB_EXPR_FMT "x", width, value);
    128 				break;
    129 			case 'm':	/* hex dump */
    130 				/*
    131 				 * Print off in chunks of size. Try to print 16
    132 				 * bytes at a time into 4 columns. This
    133 				 * loops modify's count extra times in order
    134 				 * to get the nicely formatted lines.
    135 				 */
    136 
    137 				bytes = 0;
    138 				do {
    139 					for (i = 0; i < size; i++) {
    140 						value =
    141  						    db_get_value(addr+bytes, 1,
    142 							false);
    143 						db_printf(
    144 						    "%02" DDB_EXPR_FMT "x",
    145 						    value);
    146 						bytes++;
    147 						if (!(bytes % 4))
    148 							db_printf(" ");
    149 					}
    150 				} while ((bytes != 16) && count--);
    151 				/* True up the columns before continuing */
    152 				for (i = 4; i >= (bytes / 4); i--)
    153 					db_printf ("\t");
    154 				/* Print chars,  use . for non-printable's. */
    155 				while (bytes--) {
    156 					value = db_get_value(addr, 1, false);
    157 					addr += 1;
    158 					if (value >= ' ' && value <= '~')
    159 						db_printf("%c", (char)value);
    160 					else
    161 						db_printf(".");
    162 				}
    163 				db_printf("\n");
    164 				break;
    165 			case 'z':	/* signed hex */
    166 				value = db_get_value(addr, size, true);
    167 				addr += size;
    168 				db_format_hex(tbuf, 24, value, false);
    169 				db_printf("%-*s", width, tbuf);
    170 				break;
    171 			case 'd':	/* signed decimal */
    172 				value = db_get_value(addr, size, true);
    173 				addr += size;
    174 				db_printf("%-*" DDB_EXPR_FMT "d", width, value);
    175 				break;
    176 			case 'u':	/* unsigned decimal */
    177 				value = db_get_value(addr, size, false);
    178 				addr += size;
    179 				db_printf("%-*" DDB_EXPR_FMT "u", width, value);
    180 				break;
    181 			case 'o':	/* unsigned octal */
    182 				value = db_get_value(addr, size, false);
    183 				addr += size;
    184 				db_printf("%-*" DDB_EXPR_FMT "o", width, value);
    185 				break;
    186 			case 'c':	/* character */
    187 				value = db_get_value(addr, 1, false);
    188 				addr += 1;
    189 				if (value >= ' ' && value <= '~')
    190 					db_printf("%c", (char)value);
    191 				else
    192 					db_printf("\\%03o", (int)value);
    193 				break;
    194 			case 's':	/* null-terminated string */
    195 				for (;;) {
    196 					value = db_get_value(addr, 1, false);
    197 					addr += 1;
    198 					if (value == 0)
    199 						break;
    200 					if (value >= ' ' && value <= '~')
    201 						db_printf("%c", (char)value);
    202 					else
    203 						db_printf("\\%03o", (int)value);
    204 				}
    205 				break;
    206 			case 'i':	/* instruction */
    207 				addr = db_disasm(addr, false);
    208 				break;
    209 			case 'I':	/* instruction, alternate form */
    210 				addr = db_disasm(addr, true);
    211 				break;
    212 			default:
    213 				break;
    214 			}
    215 			if (db_print_position() != 0)
    216 				db_end_line();
    217 		}
    218 	}
    219 	db_next = addr;
    220 }
    221 
    222 /*
    223  * Print value.
    224  */
    225 static char	db_print_format = 'x';
    226 
    227 /*ARGSUSED*/
    228 void
    229 db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    230     const char *modif)
    231 {
    232 	db_expr_t	value;
    233 
    234 	if (modif[0] != '\0')
    235 		db_print_format = modif[0];
    236 
    237 	switch (db_print_format) {
    238 	case 'a':
    239 		db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
    240 		break;
    241 	case 'r':
    242 		{
    243 			char tbuf[24];
    244 
    245 			db_format_radix(tbuf, 24, addr, false);
    246 			db_printf("%11s", tbuf);
    247 			break;
    248 		}
    249 	case 'x':
    250 		db_printf("%16" DDB_EXPR_FMT "x", addr);
    251 		break;
    252 	case 'z':
    253 		{
    254 			char tbuf[24];
    255 
    256 			db_format_hex(tbuf, 24, addr, false);
    257 			db_printf("%8s", tbuf);
    258 			break;
    259 		}
    260 	case 'd':
    261 		db_printf("%11" DDB_EXPR_FMT "d", addr);
    262 		break;
    263 	case 'u':
    264 		db_printf("%11" DDB_EXPR_FMT "u", addr);
    265 		break;
    266 	case 'o':
    267 		db_printf("%15" DDB_EXPR_FMT "o", addr);
    268 		break;
    269 	case 'c':
    270 		value = addr & 0xFF;
    271 		if (value >= ' ' && value <= '~')
    272 			db_printf("%c", (char)value);
    273 		else
    274 			db_printf("\\%03o", (int)value);
    275 		break;
    276 	}
    277 	db_printf("\n");
    278 }
    279 
    280 void
    281 db_print_loc_and_inst(db_addr_t loc)
    282 {
    283 
    284 	db_printsym(loc, DB_STGY_PROC, db_printf);
    285 	db_printf(":\t");
    286 	(void) db_disasm(loc, false);
    287 }
    288 
    289 /*
    290  * Search for a value in memory.
    291  * Syntax: search [/bhl] addr value [mask] [,count]
    292  */
    293 /*ARGSUSED*/
    294 void
    295 db_search_cmd(db_expr_t daddr, bool have_addr,
    296     db_expr_t dcount, const char *modif)
    297 {
    298 	int		t;
    299 	db_addr_t	addr;
    300 	int		size;
    301 	db_expr_t	value;
    302 	db_expr_t	mask;
    303 	db_expr_t	count;
    304 
    305 	t = db_read_token();
    306 	if (t == tSLASH) {
    307 		t = db_read_token();
    308 		if (t != tIDENT) {
    309 			bad_modifier:
    310 			db_printf("Bad modifier\n");
    311 			db_flush_lex();
    312 			return;
    313 		}
    314 
    315 		if (!strcmp(db_tok_string, "b"))
    316 			size = 1;
    317 		else if (!strcmp(db_tok_string, "h"))
    318 			size = 2;
    319 		else if (!strcmp(db_tok_string, "l"))
    320 			size = 4;
    321 		else
    322 			goto bad_modifier;
    323 	} else {
    324 		db_unread_token(t);
    325 		size = 4;
    326 	}
    327 
    328 	if (!db_expression(&value)) {
    329 		db_printf("Address missing\n");
    330 		db_flush_lex();
    331 		return;
    332 	}
    333 	addr = (db_addr_t) value;
    334 
    335 	if (!db_expression(&value)) {
    336 		db_printf("Value missing\n");
    337 		db_flush_lex();
    338 		return;
    339 	}
    340 
    341 	if (!db_expression(&mask))
    342 		mask = (int) ~0;
    343 
    344 	t = db_read_token();
    345 	if (t == tCOMMA) {
    346 		if (!db_expression(&count)) {
    347 			db_printf("Count missing\n");
    348 			db_flush_lex();
    349 			return;
    350 		}
    351 	} else {
    352 		db_unread_token(t);
    353 		count = -1;		/* effectively forever */
    354 	}
    355 	db_skip_to_eol();
    356 
    357 	db_search(addr, size, value, mask, count);
    358 }
    359 
    360 static void
    361 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
    362     unsigned int count)
    363 {
    364 	while (count-- != 0) {
    365 		db_prev = addr;
    366 		if ((db_get_value(addr, size, false) & mask) == value)
    367 			break;
    368 		addr += size;
    369 	}
    370 	db_next = addr;
    371 }
    372