xref: /freebsd/sys/ddb/db_examine.c (revision cd508278c11d046bf7ce79ca39f6aabeb8c77f36)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
3007f6cad7SBruce Evans 
31753960f7SDavid E. O'Brien #include <sys/cdefs.h>
32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
33753960f7SDavid E. O'Brien 
34f540b106SGarrett Wollman #include <sys/param.h>
3507f6cad7SBruce Evans #include <sys/systm.h>
36381fe1aaSGarrett Wollman 
37f540b106SGarrett Wollman #include <ddb/ddb.h>
385b81b6b3SRodney W. Grimes 
39f540b106SGarrett Wollman #include <ddb/db_lex.h>
40f540b106SGarrett Wollman #include <ddb/db_output.h>
41f540b106SGarrett Wollman #include <ddb/db_command.h>
42f540b106SGarrett Wollman #include <ddb/db_sym.h>
43f540b106SGarrett Wollman #include <ddb/db_access.h>
445b81b6b3SRodney W. Grimes 
45f73a856dSPoul-Henning Kamp static char	db_examine_format[TOK_STRING_SIZE] = "x";
465b81b6b3SRodney W. Grimes 
4714e10f99SAlfred Perlstein static void	db_examine(db_addr_t, char *, int);
4814e10f99SAlfred Perlstein static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int);
49381fe1aaSGarrett Wollman 
505b81b6b3SRodney W. Grimes /*
515b81b6b3SRodney W. Grimes  * Examine (print) data.
525b81b6b3SRodney W. Grimes  */
535b81b6b3SRodney W. Grimes /*ARGSUSED*/
545b81b6b3SRodney W. Grimes void
55*cd508278SPedro F. Giffuni db_examine_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
565b81b6b3SRodney W. Grimes {
575b81b6b3SRodney W. Grimes 	if (modif[0] != '\0')
585b81b6b3SRodney W. Grimes 	    db_strcpy(db_examine_format, modif);
595b81b6b3SRodney W. Grimes 
605b81b6b3SRodney W. Grimes 	if (count == -1)
615b81b6b3SRodney W. Grimes 	    count = 1;
625b81b6b3SRodney W. Grimes 
635b81b6b3SRodney W. Grimes 	db_examine((db_addr_t) addr, db_examine_format, count);
645b81b6b3SRodney W. Grimes }
655b81b6b3SRodney W. Grimes 
66381fe1aaSGarrett Wollman static void
67a41dd031SPedro F. Giffuni db_examine(db_addr_t addr, char *fmt, int count)
685b81b6b3SRodney W. Grimes {
695b81b6b3SRodney W. Grimes 	int		c;
705b81b6b3SRodney W. Grimes 	db_expr_t	value;
715b81b6b3SRodney W. Grimes 	int		size;
725b81b6b3SRodney W. Grimes 	int		width;
735b81b6b3SRodney W. Grimes 	char *		fp;
745b81b6b3SRodney W. Grimes 
756b76a4c7SJohn Baldwin 	while (--count >= 0 && !db_pager_quit) {
765b81b6b3SRodney W. Grimes 	    fp = fmt;
775b81b6b3SRodney W. Grimes 	    size = 4;
785b81b6b3SRodney W. Grimes 	    while ((c = *fp++) != 0) {
795b81b6b3SRodney W. Grimes 		switch (c) {
805b81b6b3SRodney W. Grimes 		    case 'b':
815b81b6b3SRodney W. Grimes 			size = 1;
825b81b6b3SRodney W. Grimes 			break;
835b81b6b3SRodney W. Grimes 		    case 'h':
845b81b6b3SRodney W. Grimes 			size = 2;
855b81b6b3SRodney W. Grimes 			break;
865b81b6b3SRodney W. Grimes 		    case 'l':
875b81b6b3SRodney W. Grimes 			size = 4;
885b81b6b3SRodney W. Grimes 			break;
891bfc653bSDoug Rabson 		    case 'g':
901bfc653bSDoug Rabson 			size = 8;
911bfc653bSDoug Rabson 			break;
925b81b6b3SRodney W. Grimes 		    case 'a':	/* address */
93358ad31dSThomas Moestl 			size = sizeof(void *);
945b81b6b3SRodney W. Grimes 			/* always forces a new line */
955b81b6b3SRodney W. Grimes 			if (db_print_position() != 0)
965b81b6b3SRodney W. Grimes 			    db_printf("\n");
975b81b6b3SRodney W. Grimes 			db_prev = addr;
985b81b6b3SRodney W. Grimes 			db_printsym(addr, DB_STGY_ANY);
995b81b6b3SRodney W. Grimes 			db_printf(":\t");
1005b81b6b3SRodney W. Grimes 			break;
1015b81b6b3SRodney W. Grimes 		    default:
1025b81b6b3SRodney W. Grimes 			if (db_print_position() == 0) {
103751b0b8eSDavid Greenman 			    /* Print the address. */
104751b0b8eSDavid Greenman 			    db_printsym(addr, DB_STGY_ANY);
105751b0b8eSDavid Greenman 			    db_printf(":\t");
1065b81b6b3SRodney W. Grimes 			    db_prev = addr;
1075b81b6b3SRodney W. Grimes 			}
1085b81b6b3SRodney W. Grimes 
109358ad31dSThomas Moestl 			width = size * 4;
1105b81b6b3SRodney W. Grimes 			switch (c) {
1115b81b6b3SRodney W. Grimes 			    case 'r':	/* signed, current radix */
1122b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1135b81b6b3SRodney W. Grimes 				addr += size;
1141c6989faSPeter Wemm 				db_printf("%+-*lr", width, (long)value);
1155b81b6b3SRodney W. Grimes 				break;
1165b81b6b3SRodney W. Grimes 			    case 'x':	/* unsigned hex */
1172b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1185b81b6b3SRodney W. Grimes 				addr += size;
1191c6989faSPeter Wemm 				db_printf("%-*lx", width, (long)value);
1205b81b6b3SRodney W. Grimes 				break;
1215b81b6b3SRodney W. Grimes 			    case 'z':	/* signed hex */
1222b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1235b81b6b3SRodney W. Grimes 				addr += size;
1244578a2e6SMaxime Henrion 				db_printf("%-*ly", width, (long)value);
1255b81b6b3SRodney W. Grimes 				break;
1265b81b6b3SRodney W. Grimes 			    case 'd':	/* signed decimal */
1272b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1285b81b6b3SRodney W. Grimes 				addr += size;
1291c6989faSPeter Wemm 				db_printf("%-*ld", width, (long)value);
1305b81b6b3SRodney W. Grimes 				break;
1315b81b6b3SRodney W. Grimes 			    case 'u':	/* unsigned decimal */
1322b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1335b81b6b3SRodney W. Grimes 				addr += size;
1341c6989faSPeter Wemm 				db_printf("%-*lu", width, (long)value);
1355b81b6b3SRodney W. Grimes 				break;
1365b81b6b3SRodney W. Grimes 			    case 'o':	/* unsigned octal */
1372b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1385b81b6b3SRodney W. Grimes 				addr += size;
1391c6989faSPeter Wemm 				db_printf("%-*lo", width, (long)value);
1405b81b6b3SRodney W. Grimes 				break;
1415b81b6b3SRodney W. Grimes 			    case 'c':	/* character */
1422b490bc7SPedro F. Giffuni 				value = db_get_value(addr, 1, false);
1435b81b6b3SRodney W. Grimes 				addr += 1;
1445b81b6b3SRodney W. Grimes 				if (value >= ' ' && value <= '~')
1451c6989faSPeter Wemm 				    db_printf("%c", (int)value);
1465b81b6b3SRodney W. Grimes 				else
1471c6989faSPeter Wemm 				    db_printf("\\%03o", (int)value);
1485b81b6b3SRodney W. Grimes 				break;
1495b81b6b3SRodney W. Grimes 			    case 's':	/* null-terminated string */
1505b81b6b3SRodney W. Grimes 				for (;;) {
1512b490bc7SPedro F. Giffuni 				    value = db_get_value(addr, 1, false);
1525b81b6b3SRodney W. Grimes 				    addr += 1;
1535b81b6b3SRodney W. Grimes 				    if (value == 0)
1545b81b6b3SRodney W. Grimes 					break;
1555b81b6b3SRodney W. Grimes 				    if (value >= ' ' && value <= '~')
1561c6989faSPeter Wemm 					db_printf("%c", (int)value);
1575b81b6b3SRodney W. Grimes 				    else
1581c6989faSPeter Wemm 					db_printf("\\%03o", (int)value);
1595b81b6b3SRodney W. Grimes 				}
1605b81b6b3SRodney W. Grimes 				break;
1617c7b7f8eSRobert Watson 			    case 'S':	/* symbol */
1627c7b7f8eSRobert Watson 				value = db_get_value(addr, sizeof(void *),
1632b490bc7SPedro F. Giffuni 				    false);
1647c7b7f8eSRobert Watson 				addr += sizeof(void *);
1657c7b7f8eSRobert Watson 				db_printsym(value, DB_STGY_ANY);
1667c7b7f8eSRobert Watson 				break;
1675b81b6b3SRodney W. Grimes 			    case 'i':	/* instruction */
1682b490bc7SPedro F. Giffuni 				addr = db_disasm(addr, false);
1695b81b6b3SRodney W. Grimes 				break;
1705b81b6b3SRodney W. Grimes 			    case 'I':	/* instruction, alternate form */
1712b490bc7SPedro F. Giffuni 				addr = db_disasm(addr, true);
1725b81b6b3SRodney W. Grimes 				break;
1735b81b6b3SRodney W. Grimes 			    default:
1745b81b6b3SRodney W. Grimes 				break;
1755b81b6b3SRodney W. Grimes 			}
1765b81b6b3SRodney W. Grimes 			if (db_print_position() != 0)
1772481da74SBruce Evans 			    db_end_line(1);
1785b81b6b3SRodney W. Grimes 			break;
1795b81b6b3SRodney W. Grimes 		}
1805b81b6b3SRodney W. Grimes 	    }
1815b81b6b3SRodney W. Grimes 	}
1825b81b6b3SRodney W. Grimes 	db_next = addr;
1835b81b6b3SRodney W. Grimes }
1845b81b6b3SRodney W. Grimes 
1855b81b6b3SRodney W. Grimes /*
1865b81b6b3SRodney W. Grimes  * Print value.
1875b81b6b3SRodney W. Grimes  */
188f73a856dSPoul-Henning Kamp static char	db_print_format = 'x';
1895b81b6b3SRodney W. Grimes 
1905b81b6b3SRodney W. Grimes /*ARGSUSED*/
1915b81b6b3SRodney W. Grimes void
192*cd508278SPedro F. Giffuni db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
1935b81b6b3SRodney W. Grimes {
1945b81b6b3SRodney W. Grimes 	db_expr_t	value;
1955b81b6b3SRodney W. Grimes 
1965b81b6b3SRodney W. Grimes 	if (modif[0] != '\0')
1975b81b6b3SRodney W. Grimes 	    db_print_format = modif[0];
1985b81b6b3SRodney W. Grimes 
1995b81b6b3SRodney W. Grimes 	switch (db_print_format) {
2005b81b6b3SRodney W. Grimes 	    case 'a':
2015b81b6b3SRodney W. Grimes 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
2025b81b6b3SRodney W. Grimes 		break;
2035b81b6b3SRodney W. Grimes 	    case 'r':
204596dfc04SBruce Evans 		db_printf("%+11lr", (long)addr);
2055b81b6b3SRodney W. Grimes 		break;
2065b81b6b3SRodney W. Grimes 	    case 'x':
207b1bf7bc6SBruce Evans 		db_printf("%8lx", (unsigned long)addr);
2085b81b6b3SRodney W. Grimes 		break;
2095b81b6b3SRodney W. Grimes 	    case 'z':
2104578a2e6SMaxime Henrion 		db_printf("%8ly", (long)addr);
2115b81b6b3SRodney W. Grimes 		break;
2125b81b6b3SRodney W. Grimes 	    case 'd':
213b1bf7bc6SBruce Evans 		db_printf("%11ld", (long)addr);
2145b81b6b3SRodney W. Grimes 		break;
2155b81b6b3SRodney W. Grimes 	    case 'u':
216b1bf7bc6SBruce Evans 		db_printf("%11lu", (unsigned long)addr);
2175b81b6b3SRodney W. Grimes 		break;
2185b81b6b3SRodney W. Grimes 	    case 'o':
219b1bf7bc6SBruce Evans 		db_printf("%16lo", (unsigned long)addr);
2205b81b6b3SRodney W. Grimes 		break;
2215b81b6b3SRodney W. Grimes 	    case 'c':
2225b81b6b3SRodney W. Grimes 		value = addr & 0xFF;
2235b81b6b3SRodney W. Grimes 		if (value >= ' ' && value <= '~')
2241c6989faSPeter Wemm 		    db_printf("%c", (int)value);
2255b81b6b3SRodney W. Grimes 		else
2261c6989faSPeter Wemm 		    db_printf("\\%03o", (int)value);
2275b81b6b3SRodney W. Grimes 		break;
2285b81b6b3SRodney W. Grimes 	}
2295b81b6b3SRodney W. Grimes 	db_printf("\n");
2305b81b6b3SRodney W. Grimes }
2315b81b6b3SRodney W. Grimes 
232381fe1aaSGarrett Wollman void
233a41dd031SPedro F. Giffuni db_print_loc_and_inst(db_addr_t loc)
2345b81b6b3SRodney W. Grimes {
2355b81b6b3SRodney W. Grimes 	db_printsym(loc, DB_STGY_PROC);
2365b81b6b3SRodney W. Grimes 	db_printf(":\t");
2372b490bc7SPedro F. Giffuni 	(void) db_disasm(loc, true);
2385b81b6b3SRodney W. Grimes }
2395b81b6b3SRodney W. Grimes 
2405b81b6b3SRodney W. Grimes /*
2415b81b6b3SRodney W. Grimes  * Search for a value in memory.
2425b81b6b3SRodney W. Grimes  * Syntax: search [/bhl] addr value [mask] [,count]
2435b81b6b3SRodney W. Grimes  */
2445b81b6b3SRodney W. Grimes void
245*cd508278SPedro F. Giffuni db_search_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
2465b81b6b3SRodney W. Grimes {
2475b81b6b3SRodney W. Grimes 	int		t;
2485b81b6b3SRodney W. Grimes 	db_addr_t	addr;
2495b81b6b3SRodney W. Grimes 	int		size;
2505b81b6b3SRodney W. Grimes 	db_expr_t	value;
2515b81b6b3SRodney W. Grimes 	db_expr_t	mask;
252897cd717SDoug Rabson 	db_expr_t	count;
2535b81b6b3SRodney W. Grimes 
2545b81b6b3SRodney W. Grimes 	t = db_read_token();
2555b81b6b3SRodney W. Grimes 	if (t == tSLASH) {
2565b81b6b3SRodney W. Grimes 	    t = db_read_token();
2575b81b6b3SRodney W. Grimes 	    if (t != tIDENT) {
2585b81b6b3SRodney W. Grimes 	      bad_modifier:
2595b81b6b3SRodney W. Grimes 		db_printf("Bad modifier\n");
2605b81b6b3SRodney W. Grimes 		db_flush_lex();
2615b81b6b3SRodney W. Grimes 		return;
2625b81b6b3SRodney W. Grimes 	    }
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes 	    if (!strcmp(db_tok_string, "b"))
2655b81b6b3SRodney W. Grimes 		size = 1;
2665b81b6b3SRodney W. Grimes 	    else if (!strcmp(db_tok_string, "h"))
2675b81b6b3SRodney W. Grimes 		size = 2;
2685b81b6b3SRodney W. Grimes 	    else if (!strcmp(db_tok_string, "l"))
2695b81b6b3SRodney W. Grimes 		size = 4;
2705b81b6b3SRodney W. Grimes 	    else
2715b81b6b3SRodney W. Grimes 		goto bad_modifier;
2725b81b6b3SRodney W. Grimes 	} else {
2735b81b6b3SRodney W. Grimes 	    db_unread_token(t);
2745b81b6b3SRodney W. Grimes 	    size = 4;
2755b81b6b3SRodney W. Grimes 	}
2765b81b6b3SRodney W. Grimes 
277381fe1aaSGarrett Wollman 	if (!db_expression((db_expr_t *)&addr)) {
2785b81b6b3SRodney W. Grimes 	    db_printf("Address missing\n");
2795b81b6b3SRodney W. Grimes 	    db_flush_lex();
2805b81b6b3SRodney W. Grimes 	    return;
2815b81b6b3SRodney W. Grimes 	}
2825b81b6b3SRodney W. Grimes 
2835b81b6b3SRodney W. Grimes 	if (!db_expression(&value)) {
2845b81b6b3SRodney W. Grimes 	    db_printf("Value missing\n");
2855b81b6b3SRodney W. Grimes 	    db_flush_lex();
2865b81b6b3SRodney W. Grimes 	    return;
2875b81b6b3SRodney W. Grimes 	}
2885b81b6b3SRodney W. Grimes 
2895b81b6b3SRodney W. Grimes 	if (!db_expression(&mask))
290aaf08d94SGarrett Wollman 	    mask = 0xffffffffUL;
2915b81b6b3SRodney W. Grimes 
2925b81b6b3SRodney W. Grimes 	t = db_read_token();
2935b81b6b3SRodney W. Grimes 	if (t == tCOMMA) {
2945b81b6b3SRodney W. Grimes 	    if (!db_expression(&count)) {
2955b81b6b3SRodney W. Grimes 		db_printf("Count missing\n");
2965b81b6b3SRodney W. Grimes 		db_flush_lex();
2975b81b6b3SRodney W. Grimes 		return;
2985b81b6b3SRodney W. Grimes 	    }
2995b81b6b3SRodney W. Grimes 	} else {
3005b81b6b3SRodney W. Grimes 	    db_unread_token(t);
3015b81b6b3SRodney W. Grimes 	    count = -1;		/* effectively forever */
3025b81b6b3SRodney W. Grimes 	}
3035b81b6b3SRodney W. Grimes 	db_skip_to_eol();
3045b81b6b3SRodney W. Grimes 
3055b81b6b3SRodney W. Grimes 	db_search(addr, size, value, mask, count);
3065b81b6b3SRodney W. Grimes }
3075b81b6b3SRodney W. Grimes 
308381fe1aaSGarrett Wollman static void
309a41dd031SPedro F. Giffuni db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
310a41dd031SPedro F. Giffuni     unsigned int count)
3115b81b6b3SRodney W. Grimes {
3125b81b6b3SRodney W. Grimes 	while (count-- != 0) {
3135b81b6b3SRodney W. Grimes 		db_prev = addr;
3142b490bc7SPedro F. Giffuni 		if ((db_get_value(addr, size, false) & mask) == value)
3155b81b6b3SRodney W. Grimes 			break;
3165b81b6b3SRodney W. Grimes 		addr += size;
3175b81b6b3SRodney W. Grimes 	}
3185b81b6b3SRodney W. Grimes 	db_next = addr;
3195b81b6b3SRodney W. Grimes }
320