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