Home | History | Annotate | Line # | Download | only in ddb
db_examine.c revision 1.32.38.1
      1  1.32.38.1       mjf /*	$NetBSD: db_examine.c,v 1.32.38.1 2009/01/17 13:28:51 mjf Exp $	*/
      2        1.4       cgd 
      3        1.7   mycroft /*
      4        1.1       cgd  * Mach Operating System
      5        1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6        1.1       cgd  * All Rights Reserved.
      7        1.7   mycroft  *
      8        1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9        1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10        1.1       cgd  * notice and this permission notice appear in all copies of the
     11        1.1       cgd  * software, derivative works or modified versions, and any portions
     12        1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13        1.7   mycroft  *
     14       1.15        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15        1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16        1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17        1.7   mycroft  *
     18        1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19        1.7   mycroft  *
     20        1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21        1.1       cgd  *  School of Computer Science
     22        1.1       cgd  *  Carnegie Mellon University
     23        1.1       cgd  *  Pittsburgh PA 15213-3890
     24        1.7   mycroft  *
     25        1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26        1.1       cgd  * rights to redistribute these changes.
     27        1.2       cgd  *
     28        1.1       cgd  *	Author: David B. Golub, Carnegie Mellon University
     29        1.1       cgd  *	Date:	7/90
     30        1.1       cgd  */
     31       1.22     lukem 
     32       1.22     lukem #include <sys/cdefs.h>
     33  1.32.38.1       mjf __KERNEL_RCSID(0, "$NetBSD: db_examine.c,v 1.32.38.1 2009/01/17 13:28:51 mjf Exp $");
     34        1.3   mycroft 
     35        1.3   mycroft #include <sys/param.h>
     36       1.21       cgd #include <sys/systm.h>
     37  1.32.38.1       mjf #include <sys/buf.h>
     38        1.3   mycroft #include <sys/proc.h>
     39        1.3   mycroft 
     40        1.1       cgd #include <machine/db_machdep.h>		/* type definitions */
     41        1.1       cgd 
     42        1.1       cgd #include <ddb/db_lex.h>
     43        1.1       cgd #include <ddb/db_output.h>
     44        1.1       cgd #include <ddb/db_command.h>
     45        1.1       cgd #include <ddb/db_sym.h>
     46       1.10  christos #include <ddb/db_access.h>
     47       1.10  christos #include <ddb/db_extern.h>
     48       1.10  christos #include <ddb/db_interface.h>
     49        1.1       cgd 
     50       1.23    simonb static char	db_examine_format[TOK_STRING_SIZE] = "x";
     51       1.23    simonb 
     52       1.23    simonb static void	db_examine(db_addr_t, char *, int);
     53       1.23    simonb static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, unsigned int);
     54        1.1       cgd 
     55        1.1       cgd /*
     56        1.6       gwr  * Examine (print) data.  Syntax is:
     57        1.6       gwr  *		x/[bhl][cdiorsuxz]*
     58        1.6       gwr  * For example, the command:
     59        1.6       gwr  *  	x/bxxxx
     60        1.6       gwr  * should print:
     61        1.6       gwr  *  	address:  01  23  45  67
     62        1.1       cgd  */
     63        1.1       cgd /*ARGSUSED*/
     64        1.1       cgd void
     65       1.31      matt db_examine_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
     66       1.29  christos     const char *modif)
     67        1.1       cgd {
     68        1.1       cgd 	if (modif[0] != '\0')
     69       1.24    itojun 		strlcpy(db_examine_format, modif, sizeof(db_examine_format));
     70        1.1       cgd 
     71        1.1       cgd 	if (count == -1)
     72        1.7   mycroft 		count = 1;
     73        1.1       cgd 
     74        1.1       cgd 	db_examine((db_addr_t) addr, db_examine_format, count);
     75        1.1       cgd }
     76        1.1       cgd 
     77       1.23    simonb static void
     78       1.23    simonb db_examine(db_addr_t addr, char *fmt, int count)
     79        1.1       cgd {
     80       1.20       jmc 	int		i, c;
     81        1.1       cgd 	db_expr_t	value;
     82        1.1       cgd 	int		size;
     83        1.1       cgd 	int		width;
     84       1.20       jmc 	int		bytes;
     85        1.1       cgd 	char *		fp;
     86       1.19        tv 	char		tbuf[24];
     87        1.7   mycroft 
     88        1.1       cgd 	while (--count >= 0) {
     89        1.7   mycroft 		fp = fmt;
     90        1.7   mycroft 		size = 4;
     91        1.8       gwr 		width = 12;
     92        1.7   mycroft 		while ((c = *fp++) != 0) {
     93        1.9       gwr 			if (db_print_position() == 0) {
     94        1.9       gwr 				/* Always print the address. */
     95       1.18     jhawk 				db_printsym(addr, DB_STGY_ANY, db_printf);
     96        1.9       gwr 				db_printf(":\t");
     97        1.9       gwr 				db_prev = addr;
     98        1.9       gwr 			}
     99        1.6       gwr 			switch (c) {
    100        1.9       gwr 			case 'b':	/* byte */
    101        1.6       gwr 				size = 1;
    102        1.6       gwr 				width = 4;
    103        1.6       gwr 				break;
    104        1.9       gwr 			case 'h':	/* half-word */
    105        1.6       gwr 				size = 2;
    106        1.6       gwr 				width = 8;
    107        1.6       gwr 				break;
    108        1.9       gwr 			case 'l':	/* long-word */
    109        1.6       gwr 				size = 4;
    110        1.8       gwr 				width = 12;
    111       1.16      ross 				break;
    112       1.16      ross 			case 'L':	/* implementation maximum */
    113       1.16      ross 				size = sizeof value;
    114       1.16      ross 				width = 12 * (sizeof value / 4);
    115        1.6       gwr 				break;
    116        1.7   mycroft 			case 'a':	/* address */
    117       1.26       scw 				db_printf("= 0x%lx\n", (long)addr);
    118        1.6       gwr 				break;
    119        1.6       gwr 			case 'r':	/* signed, current radix */
    120       1.32   thorpej 				value = db_get_value(addr, size, true);
    121        1.1       cgd 				addr += size;
    122       1.32   thorpej 				db_format_radix(tbuf, 24, value, false);
    123       1.19        tv 				db_printf("%-*s", width, tbuf);
    124        1.1       cgd 				break;
    125        1.6       gwr 			case 'x':	/* unsigned hex */
    126       1.32   thorpej 				value = db_get_value(addr, size, false);
    127        1.1       cgd 				addr += size;
    128       1.25       scw 				db_printf(DB_EXPR_T_IS_QUAD ? "%-*qx" : "%-*lx",
    129       1.25       scw 				    width, value);
    130       1.20       jmc 				break;
    131       1.20       jmc 			case 'm':	/* hex dump */
    132       1.23    simonb 				/*
    133       1.20       jmc 				 * Print off in chunks of size. Try to print 16
    134       1.23    simonb 				 * bytes at a time into 4 columns. This
    135       1.20       jmc 				 * loops modify's count extra times in order
    136       1.20       jmc 				 * to get the nicely formatted lines.
    137       1.20       jmc 				 */
    138       1.23    simonb 
    139       1.20       jmc 				bytes = 0;
    140       1.20       jmc 				do {
    141       1.20       jmc 					for (i = 0; i < size; i++) {
    142       1.23    simonb 						value =
    143       1.20       jmc  						    db_get_value(addr+bytes, 1,
    144       1.32   thorpej 							false);
    145       1.25       scw 						db_printf(
    146       1.25       scw 						    DB_EXPR_T_IS_QUAD ? "%02qx":
    147       1.25       scw 						    "%02lx", value);
    148       1.20       jmc 						bytes++;
    149       1.20       jmc 						if (!(bytes % 4))
    150       1.20       jmc 							db_printf(" ");
    151       1.20       jmc 					}
    152       1.20       jmc 				} while ((bytes != 16) && count--);
    153       1.20       jmc 				/* True up the columns before continuing */
    154       1.20       jmc 				for (i = 4; i >= (bytes / 4); i--)
    155       1.20       jmc 					db_printf ("\t");
    156       1.20       jmc 				/* Print chars,  use . for non-printable's. */
    157       1.20       jmc 				while (bytes--) {
    158       1.32   thorpej 					value = db_get_value(addr, 1, false);
    159       1.20       jmc 					addr += 1;
    160       1.20       jmc 					if (value >= ' ' && value <= '~')
    161       1.20       jmc 						db_printf("%c", (char)value);
    162       1.20       jmc 					else
    163       1.20       jmc 						db_printf(".");
    164       1.23    simonb 				}
    165       1.20       jmc 				db_printf("\n");
    166        1.1       cgd 				break;
    167        1.6       gwr 			case 'z':	/* signed hex */
    168       1.32   thorpej 				value = db_get_value(addr, size, true);
    169        1.1       cgd 				addr += size;
    170       1.32   thorpej 				db_format_hex(tbuf, 24, value, false);
    171       1.19        tv 				db_printf("%-*s", width, tbuf);
    172        1.1       cgd 				break;
    173        1.6       gwr 			case 'd':	/* signed decimal */
    174       1.32   thorpej 				value = db_get_value(addr, size, true);
    175        1.1       cgd 				addr += size;
    176       1.25       scw 				db_printf(DB_EXPR_T_IS_QUAD ? "%-*qd" : "%-*ld",
    177       1.25       scw 				    width, value);
    178        1.1       cgd 				break;
    179        1.6       gwr 			case 'u':	/* unsigned decimal */
    180       1.32   thorpej 				value = db_get_value(addr, size, false);
    181        1.1       cgd 				addr += size;
    182       1.25       scw 				db_printf(DB_EXPR_T_IS_QUAD ? "%-*qu" : "%-*lu",
    183       1.25       scw 				    width, value);
    184        1.1       cgd 				break;
    185        1.6       gwr 			case 'o':	/* unsigned octal */
    186       1.32   thorpej 				value = db_get_value(addr, size, false);
    187        1.1       cgd 				addr += size;
    188       1.25       scw 				db_printf(DB_EXPR_T_IS_QUAD ? "%-*qo" : "%-*lo",
    189       1.25       scw 				    width, value);
    190        1.1       cgd 				break;
    191        1.6       gwr 			case 'c':	/* character */
    192       1.32   thorpej 				value = db_get_value(addr, 1, false);
    193        1.1       cgd 				addr += 1;
    194        1.1       cgd 				if (value >= ' ' && value <= '~')
    195       1.13   mycroft 					db_printf("%c", (char)value);
    196        1.1       cgd 				else
    197       1.25       scw 					db_printf("\\%03o", (int)value);
    198        1.1       cgd 				break;
    199        1.6       gwr 			case 's':	/* null-terminated string */
    200        1.1       cgd 				for (;;) {
    201       1.32   thorpej 					value = db_get_value(addr, 1, false);
    202        1.7   mycroft 					addr += 1;
    203        1.7   mycroft 					if (value == 0)
    204        1.6       gwr 						break;
    205        1.7   mycroft 					if (value >= ' ' && value <= '~')
    206       1.13   mycroft 						db_printf("%c", (char)value);
    207        1.7   mycroft 					else
    208       1.25       scw 						db_printf("\\%03o", (int)value);
    209        1.1       cgd 				}
    210        1.1       cgd 				break;
    211        1.6       gwr 			case 'i':	/* instruction */
    212       1.32   thorpej 				addr = db_disasm(addr, false);
    213        1.1       cgd 				break;
    214        1.6       gwr 			case 'I':	/* instruction, alternate form */
    215       1.32   thorpej 				addr = db_disasm(addr, true);
    216        1.1       cgd 				break;
    217        1.6       gwr 			default:
    218        1.1       cgd 				break;
    219        1.1       cgd 			}
    220        1.1       cgd 			if (db_print_position() != 0)
    221        1.6       gwr 				db_end_line();
    222        1.7   mycroft 		}
    223        1.1       cgd 	}
    224        1.1       cgd 	db_next = addr;
    225        1.1       cgd }
    226        1.1       cgd 
    227        1.1       cgd /*
    228        1.1       cgd  * Print value.
    229        1.1       cgd  */
    230       1.23    simonb static char	db_print_format = 'x';
    231        1.1       cgd 
    232        1.1       cgd /*ARGSUSED*/
    233        1.1       cgd void
    234       1.31      matt db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    235       1.29  christos     const char *modif)
    236        1.1       cgd {
    237        1.1       cgd 	db_expr_t	value;
    238        1.1       cgd 
    239        1.1       cgd 	if (modif[0] != '\0')
    240        1.7   mycroft 		db_print_format = modif[0];
    241        1.1       cgd 
    242        1.1       cgd 	switch (db_print_format) {
    243        1.7   mycroft 	case 'a':
    244       1.18     jhawk 		db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
    245        1.1       cgd 		break;
    246        1.7   mycroft 	case 'r':
    247       1.19        tv 		{
    248       1.19        tv 			char tbuf[24];
    249       1.19        tv 
    250       1.32   thorpej 			db_format_radix(tbuf, 24, addr, false);
    251       1.19        tv 			db_printf("%11s", tbuf);
    252       1.19        tv 			break;
    253       1.19        tv 		}
    254        1.7   mycroft 	case 'x':
    255       1.25       scw 		db_printf(DB_EXPR_T_IS_QUAD ? "%16qx" : "%8lx", addr);
    256        1.1       cgd 		break;
    257        1.7   mycroft 	case 'z':
    258       1.19        tv 		{
    259       1.19        tv 			char tbuf[24];
    260       1.19        tv 
    261       1.32   thorpej 			db_format_hex(tbuf, 24, addr, false);
    262       1.19        tv 			db_printf("%8s", tbuf);
    263       1.19        tv 			break;
    264       1.19        tv 		}
    265        1.7   mycroft 	case 'd':
    266       1.25       scw 		db_printf(DB_EXPR_T_IS_QUAD ? "%11qd" : "%11ld", addr);
    267        1.1       cgd 		break;
    268        1.7   mycroft 	case 'u':
    269       1.25       scw 		db_printf(DB_EXPR_T_IS_QUAD ? "%11qu" : "%11lu", addr);
    270        1.1       cgd 		break;
    271        1.7   mycroft 	case 'o':
    272       1.25       scw 		db_printf(DB_EXPR_T_IS_QUAD ? "%15qo" : "%16lo", addr);
    273        1.1       cgd 		break;
    274        1.7   mycroft 	case 'c':
    275        1.1       cgd 		value = addr & 0xFF;
    276        1.1       cgd 		if (value >= ' ' && value <= '~')
    277       1.13   mycroft 			db_printf("%c", (char)value);
    278        1.1       cgd 		else
    279       1.25       scw 			db_printf("\\%03o", (int)value);
    280        1.1       cgd 		break;
    281        1.1       cgd 	}
    282        1.1       cgd 	db_printf("\n");
    283        1.1       cgd }
    284        1.1       cgd 
    285       1.10  christos void
    286       1.23    simonb db_print_loc_and_inst(db_addr_t loc)
    287        1.1       cgd {
    288       1.23    simonb 
    289       1.18     jhawk 	db_printsym(loc, DB_STGY_PROC, db_printf);
    290        1.1       cgd 	db_printf(":\t");
    291       1.32   thorpej 	(void) db_disasm(loc, false);
    292        1.1       cgd }
    293        1.1       cgd 
    294        1.1       cgd /*
    295        1.1       cgd  * Search for a value in memory.
    296        1.1       cgd  * Syntax: search [/bhl] addr value [mask] [,count]
    297        1.1       cgd  */
    298       1.10  christos /*ARGSUSED*/
    299        1.1       cgd void
    300       1.31      matt db_search_cmd(db_expr_t daddr, bool have_addr,
    301       1.30  christos     db_expr_t dcount, const char *modif)
    302        1.1       cgd {
    303        1.1       cgd 	int		t;
    304        1.1       cgd 	db_addr_t	addr;
    305        1.1       cgd 	int		size;
    306        1.1       cgd 	db_expr_t	value;
    307        1.1       cgd 	db_expr_t	mask;
    308       1.12       cgd 	db_expr_t	count;
    309        1.1       cgd 
    310        1.1       cgd 	t = db_read_token();
    311        1.1       cgd 	if (t == tSLASH) {
    312        1.7   mycroft 		t = db_read_token();
    313        1.7   mycroft 		if (t != tIDENT) {
    314        1.7   mycroft 			bad_modifier:
    315        1.7   mycroft 			db_printf("Bad modifier\n");
    316        1.7   mycroft 			db_flush_lex();
    317        1.7   mycroft 			return;
    318        1.7   mycroft 		}
    319        1.7   mycroft 
    320        1.7   mycroft 		if (!strcmp(db_tok_string, "b"))
    321        1.7   mycroft 			size = 1;
    322        1.7   mycroft 		else if (!strcmp(db_tok_string, "h"))
    323        1.7   mycroft 			size = 2;
    324        1.7   mycroft 		else if (!strcmp(db_tok_string, "l"))
    325        1.7   mycroft 			size = 4;
    326        1.7   mycroft 		else
    327        1.7   mycroft 			goto bad_modifier;
    328        1.7   mycroft 	} else {
    329        1.7   mycroft 		db_unread_token(t);
    330        1.1       cgd 		size = 4;
    331        1.1       cgd 	}
    332        1.1       cgd 
    333       1.10  christos 	if (!db_expression(&value)) {
    334        1.7   mycroft 		db_printf("Address missing\n");
    335        1.7   mycroft 		db_flush_lex();
    336        1.7   mycroft 		return;
    337        1.1       cgd 	}
    338       1.10  christos 	addr = (db_addr_t) value;
    339        1.1       cgd 
    340        1.1       cgd 	if (!db_expression(&value)) {
    341        1.7   mycroft 		db_printf("Value missing\n");
    342        1.7   mycroft 		db_flush_lex();
    343        1.7   mycroft 		return;
    344        1.1       cgd 	}
    345        1.1       cgd 
    346        1.1       cgd 	if (!db_expression(&mask))
    347       1.10  christos 		mask = (int) ~0;
    348        1.1       cgd 
    349        1.1       cgd 	t = db_read_token();
    350        1.1       cgd 	if (t == tCOMMA) {
    351        1.7   mycroft 		if (!db_expression(&count)) {
    352        1.7   mycroft 			db_printf("Count missing\n");
    353        1.7   mycroft 			db_flush_lex();
    354        1.7   mycroft 			return;
    355        1.7   mycroft 		}
    356        1.1       cgd 	} else {
    357        1.7   mycroft 		db_unread_token(t);
    358        1.7   mycroft 		count = -1;		/* effectively forever */
    359        1.1       cgd 	}
    360        1.1       cgd 	db_skip_to_eol();
    361        1.1       cgd 
    362        1.1       cgd 	db_search(addr, size, value, mask, count);
    363        1.1       cgd }
    364        1.1       cgd 
    365       1.23    simonb static void
    366       1.23    simonb db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
    367       1.23    simonb     unsigned int count)
    368        1.1       cgd {
    369        1.1       cgd 	while (count-- != 0) {
    370        1.1       cgd 		db_prev = addr;
    371       1.32   thorpej 		if ((db_get_value(addr, size, false) & mask) == value)
    372        1.1       cgd 			break;
    373        1.1       cgd 		addr += size;
    374        1.1       cgd 	}
    375        1.1       cgd 	db_next = addr;
    376        1.1       cgd }
    377