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