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