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