xref: /freebsd/sys/ddb/db_examine.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1dd3cb568SWarner Losh /*-
2*796df753SPedro F. Giffuni  * SPDX-License-Identifier: MIT-CMU
3*796df753SPedro F. Giffuni  *
45b81b6b3SRodney W. Grimes  * Mach Operating System
55b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
65b81b6b3SRodney W. Grimes  * All Rights Reserved.
75b81b6b3SRodney W. Grimes  *
85b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
95b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
105b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
115b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
125b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
135b81b6b3SRodney W. Grimes  *
145b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
155b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
165b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
195b81b6b3SRodney W. Grimes  *
205b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
215b81b6b3SRodney W. Grimes  *  School of Computer Science
225b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
235b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
245b81b6b3SRodney W. Grimes  *
255b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
265b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
275b81b6b3SRodney W. Grimes  */
285b81b6b3SRodney W. Grimes /*
295b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
305b81b6b3SRodney W. Grimes  *	Date:	7/90
315b81b6b3SRodney W. Grimes  */
3207f6cad7SBruce Evans 
33f540b106SGarrett Wollman #include <sys/param.h>
3407f6cad7SBruce Evans #include <sys/systm.h>
35381fe1aaSGarrett Wollman 
36f540b106SGarrett Wollman #include <ddb/ddb.h>
375b81b6b3SRodney W. Grimes 
38f540b106SGarrett Wollman #include <ddb/db_lex.h>
39f540b106SGarrett Wollman #include <ddb/db_output.h>
40f540b106SGarrett Wollman #include <ddb/db_command.h>
41f540b106SGarrett Wollman #include <ddb/db_sym.h>
42f540b106SGarrett Wollman #include <ddb/db_access.h>
435b81b6b3SRodney W. Grimes 
44f73a856dSPoul-Henning Kamp static char	db_examine_format[TOK_STRING_SIZE] = "x";
455b81b6b3SRodney W. Grimes 
4614e10f99SAlfred Perlstein static void	db_examine(db_addr_t, char *, int);
4714e10f99SAlfred Perlstein static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int);
48381fe1aaSGarrett Wollman 
495b81b6b3SRodney W. Grimes /*
505b81b6b3SRodney W. Grimes  * Examine (print) data.
515b81b6b3SRodney W. Grimes  */
525b81b6b3SRodney W. Grimes /*ARGSUSED*/
535b81b6b3SRodney W. Grimes void
db_examine_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)54cd508278SPedro F. Giffuni db_examine_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
555b81b6b3SRodney W. Grimes {
565b81b6b3SRodney W. Grimes 	if (modif[0] != '\0')
575b81b6b3SRodney W. Grimes 	    db_strcpy(db_examine_format, modif);
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes 	if (count == -1)
605b81b6b3SRodney W. Grimes 	    count = 1;
615b81b6b3SRodney W. Grimes 
625b81b6b3SRodney W. Grimes 	db_examine((db_addr_t) addr, db_examine_format, count);
635b81b6b3SRodney W. Grimes }
645b81b6b3SRodney W. Grimes 
65381fe1aaSGarrett Wollman static void
db_examine(db_addr_t addr,char * fmt,int count)66a41dd031SPedro F. Giffuni db_examine(db_addr_t addr, char *fmt, int count)
675b81b6b3SRodney W. Grimes {
685b81b6b3SRodney W. Grimes 	int		c;
695b81b6b3SRodney W. Grimes 	db_expr_t	value;
705b81b6b3SRodney W. Grimes 	int		size;
715b81b6b3SRodney W. Grimes 	int		width;
725b81b6b3SRodney W. Grimes 	char *		fp;
735b81b6b3SRodney W. Grimes 
746b76a4c7SJohn Baldwin 	while (--count >= 0 && !db_pager_quit) {
755b81b6b3SRodney W. Grimes 	    fp = fmt;
765b81b6b3SRodney W. Grimes 	    size = 4;
775b81b6b3SRodney W. Grimes 	    while ((c = *fp++) != 0) {
785b81b6b3SRodney W. Grimes 		switch (c) {
795b81b6b3SRodney W. Grimes 		    case 'b':
805b81b6b3SRodney W. Grimes 			size = 1;
815b81b6b3SRodney W. Grimes 			break;
825b81b6b3SRodney W. Grimes 		    case 'h':
835b81b6b3SRodney W. Grimes 			size = 2;
845b81b6b3SRodney W. Grimes 			break;
855b81b6b3SRodney W. Grimes 		    case 'l':
865b81b6b3SRodney W. Grimes 			size = 4;
875b81b6b3SRodney W. Grimes 			break;
881bfc653bSDoug Rabson 		    case 'g':
891bfc653bSDoug Rabson 			size = 8;
901bfc653bSDoug Rabson 			break;
918c6a0d07SPedro F. Giffuni 		    case 'a':	/* address */
928c6a0d07SPedro F. Giffuni 			size = sizeof(void *);
938c6a0d07SPedro F. Giffuni 			/* always forces a new line */
948c6a0d07SPedro F. Giffuni 			if (db_print_position() != 0)
958c6a0d07SPedro F. Giffuni 			    db_printf("\n");
968c6a0d07SPedro F. Giffuni 			db_prev = addr;
978c6a0d07SPedro F. Giffuni 			db_printsym(addr, DB_STGY_ANY);
988c6a0d07SPedro F. Giffuni 			db_printf(":\t");
998c6a0d07SPedro F. Giffuni 			break;
1005b81b6b3SRodney W. Grimes 		    default:
1015b81b6b3SRodney W. Grimes 			if (db_print_position() == 0) {
102751b0b8eSDavid Greenman 			    /* Print the address. */
103751b0b8eSDavid Greenman 			    db_printsym(addr, DB_STGY_ANY);
104751b0b8eSDavid Greenman 			    db_printf(":\t");
1055b81b6b3SRodney W. Grimes 			    db_prev = addr;
1065b81b6b3SRodney W. Grimes 			}
1075b81b6b3SRodney W. Grimes 
108358ad31dSThomas Moestl 			width = size * 4;
1095b81b6b3SRodney W. Grimes 			switch (c) {
1105b81b6b3SRodney W. Grimes 			    case 'r':	/* signed, current radix */
1112b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1125b81b6b3SRodney W. Grimes 				addr += size;
1131c6989faSPeter Wemm 				db_printf("%+-*lr", width, (long)value);
1145b81b6b3SRodney W. Grimes 				break;
1155b81b6b3SRodney W. Grimes 			    case 'x':	/* unsigned hex */
1162b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1175b81b6b3SRodney W. Grimes 				addr += size;
1181c6989faSPeter Wemm 				db_printf("%-*lx", width, (long)value);
1195b81b6b3SRodney W. Grimes 				break;
1205b81b6b3SRodney W. Grimes 			    case 'z':	/* signed hex */
1212b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1225b81b6b3SRodney W. Grimes 				addr += size;
1234578a2e6SMaxime Henrion 				db_printf("%-*ly", width, (long)value);
1245b81b6b3SRodney W. Grimes 				break;
1255b81b6b3SRodney W. Grimes 			    case 'd':	/* signed decimal */
1262b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, true);
1275b81b6b3SRodney W. Grimes 				addr += size;
1281c6989faSPeter Wemm 				db_printf("%-*ld", width, (long)value);
1295b81b6b3SRodney W. Grimes 				break;
1305b81b6b3SRodney W. Grimes 			    case 'u':	/* unsigned decimal */
1312b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1325b81b6b3SRodney W. Grimes 				addr += size;
1331c6989faSPeter Wemm 				db_printf("%-*lu", width, (long)value);
1345b81b6b3SRodney W. Grimes 				break;
1355b81b6b3SRodney W. Grimes 			    case 'o':	/* unsigned octal */
1362b490bc7SPedro F. Giffuni 				value = db_get_value(addr, size, false);
1375b81b6b3SRodney W. Grimes 				addr += size;
1381c6989faSPeter Wemm 				db_printf("%-*lo", width, (long)value);
1395b81b6b3SRodney W. Grimes 				break;
1405b81b6b3SRodney W. Grimes 			    case 'c':	/* character */
1412b490bc7SPedro F. Giffuni 				value = db_get_value(addr, 1, false);
1425b81b6b3SRodney W. Grimes 				addr += 1;
1435b81b6b3SRodney W. Grimes 				if (value >= ' ' && value <= '~')
1441c6989faSPeter Wemm 				    db_printf("%c", (int)value);
1455b81b6b3SRodney W. Grimes 				else
1461c6989faSPeter Wemm 				    db_printf("\\%03o", (int)value);
1475b81b6b3SRodney W. Grimes 				break;
1485b81b6b3SRodney W. Grimes 			    case 's':	/* null-terminated string */
1495b81b6b3SRodney W. Grimes 				for (;;) {
1502b490bc7SPedro F. Giffuni 				    value = db_get_value(addr, 1, false);
1515b81b6b3SRodney W. Grimes 				    addr += 1;
1525b81b6b3SRodney W. Grimes 				    if (value == 0)
1535b81b6b3SRodney W. Grimes 					break;
1545b81b6b3SRodney W. Grimes 				    if (value >= ' ' && value <= '~')
1551c6989faSPeter Wemm 					db_printf("%c", (int)value);
1565b81b6b3SRodney W. Grimes 				    else
1571c6989faSPeter Wemm 					db_printf("\\%03o", (int)value);
1585b81b6b3SRodney W. Grimes 				}
1595b81b6b3SRodney W. Grimes 				break;
1607c7b7f8eSRobert Watson 			    case 'S':	/* symbol */
1617c7b7f8eSRobert Watson 				value = db_get_value(addr, sizeof(void *),
1622b490bc7SPedro F. Giffuni 				    false);
1637c7b7f8eSRobert Watson 				addr += sizeof(void *);
1647c7b7f8eSRobert Watson 				db_printsym(value, DB_STGY_ANY);
1657c7b7f8eSRobert Watson 				break;
1665b81b6b3SRodney W. Grimes 			    case 'i':	/* instruction */
1672b490bc7SPedro F. Giffuni 				addr = db_disasm(addr, false);
1685b81b6b3SRodney W. Grimes 				break;
1695b81b6b3SRodney W. Grimes 			    case 'I':	/* instruction, alternate form */
1702b490bc7SPedro F. Giffuni 				addr = db_disasm(addr, true);
1715b81b6b3SRodney W. Grimes 				break;
1725b81b6b3SRodney W. Grimes 			    default:
1735b81b6b3SRodney W. Grimes 				break;
1745b81b6b3SRodney W. Grimes 			}
1758c6a0d07SPedro F. Giffuni 			if (db_print_position() != 0)
1762481da74SBruce Evans 			    db_end_line(1);
1775b81b6b3SRodney W. Grimes 			break;
1785b81b6b3SRodney W. Grimes 		}
1795b81b6b3SRodney W. Grimes 	    }
1805b81b6b3SRodney W. Grimes 	}
1815b81b6b3SRodney W. Grimes 	db_next = addr;
1825b81b6b3SRodney W. Grimes }
1835b81b6b3SRodney W. Grimes 
1845b81b6b3SRodney W. Grimes /*
1855b81b6b3SRodney W. Grimes  * Print value.
1865b81b6b3SRodney W. Grimes  */
187f73a856dSPoul-Henning Kamp static char	db_print_format = 'x';
1885b81b6b3SRodney W. Grimes 
1895b81b6b3SRodney W. Grimes /*ARGSUSED*/
1905b81b6b3SRodney W. Grimes void
db_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)191cd508278SPedro F. Giffuni db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
1925b81b6b3SRodney W. Grimes {
1935b81b6b3SRodney W. Grimes 	db_expr_t	value;
1945b81b6b3SRodney W. Grimes 
1955b81b6b3SRodney W. Grimes 	if (modif[0] != '\0')
1965b81b6b3SRodney W. Grimes 	    db_print_format = modif[0];
1975b81b6b3SRodney W. Grimes 
1985b81b6b3SRodney W. Grimes 	switch (db_print_format) {
1995b81b6b3SRodney W. Grimes 	    case 'a':
2005b81b6b3SRodney W. Grimes 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
2015b81b6b3SRodney W. Grimes 		break;
2025b81b6b3SRodney W. Grimes 	    case 'r':
203596dfc04SBruce Evans 		db_printf("%+11lr", (long)addr);
2045b81b6b3SRodney W. Grimes 		break;
2055b81b6b3SRodney W. Grimes 	    case 'x':
206b1bf7bc6SBruce Evans 		db_printf("%8lx", (unsigned long)addr);
2075b81b6b3SRodney W. Grimes 		break;
2085b81b6b3SRodney W. Grimes 	    case 'z':
2094578a2e6SMaxime Henrion 		db_printf("%8ly", (long)addr);
2105b81b6b3SRodney W. Grimes 		break;
2115b81b6b3SRodney W. Grimes 	    case 'd':
212b1bf7bc6SBruce Evans 		db_printf("%11ld", (long)addr);
2135b81b6b3SRodney W. Grimes 		break;
2145b81b6b3SRodney W. Grimes 	    case 'u':
215b1bf7bc6SBruce Evans 		db_printf("%11lu", (unsigned long)addr);
2165b81b6b3SRodney W. Grimes 		break;
2175b81b6b3SRodney W. Grimes 	    case 'o':
218b1bf7bc6SBruce Evans 		db_printf("%16lo", (unsigned long)addr);
2195b81b6b3SRodney W. Grimes 		break;
2205b81b6b3SRodney W. Grimes 	    case 'c':
2215b81b6b3SRodney W. Grimes 		value = addr & 0xFF;
2225b81b6b3SRodney W. Grimes 		if (value >= ' ' && value <= '~')
2231c6989faSPeter Wemm 		    db_printf("%c", (int)value);
2245b81b6b3SRodney W. Grimes 		else
2251c6989faSPeter Wemm 		    db_printf("\\%03o", (int)value);
2265b81b6b3SRodney W. Grimes 		break;
227c69cee69SPedro F. Giffuni 	    default:
228c69cee69SPedro F. Giffuni 		db_print_format = 'x';
229c69cee69SPedro F. Giffuni 		db_error("Syntax error: unsupported print modifier\n");
230c69cee69SPedro F. Giffuni 		/*NOTREACHED*/
2315b81b6b3SRodney W. Grimes 	}
2325b81b6b3SRodney W. Grimes 	db_printf("\n");
2335b81b6b3SRodney W. Grimes }
2345b81b6b3SRodney W. Grimes 
235381fe1aaSGarrett Wollman void
db_print_loc_and_inst(db_addr_t loc)236a41dd031SPedro F. Giffuni db_print_loc_and_inst(db_addr_t loc)
2375b81b6b3SRodney W. Grimes {
238e31a60b4SMark Johnston 	db_expr_t off;
239e31a60b4SMark Johnston 
2405b81b6b3SRodney W. Grimes 	db_printsym(loc, DB_STGY_PROC);
241e31a60b4SMark Johnston 	if (db_search_symbol(loc, DB_STGY_PROC, &off) != C_DB_SYM_NULL) {
2425b81b6b3SRodney W. Grimes 		db_printf(":\t");
243808cf02cSBruce Evans 		(void)db_disasm(loc, false);
2445b81b6b3SRodney W. Grimes 	}
245e31a60b4SMark Johnston }
2465b81b6b3SRodney W. Grimes 
2475b81b6b3SRodney W. Grimes /*
2485b81b6b3SRodney W. Grimes  * Search for a value in memory.
2495b81b6b3SRodney W. Grimes  * Syntax: search [/bhl] addr value [mask] [,count]
2505b81b6b3SRodney W. Grimes  */
2515b81b6b3SRodney W. Grimes void
db_search_cmd(db_expr_t dummy1,bool dummy2,db_expr_t dummy3,char * dummy4)252cd508278SPedro F. Giffuni db_search_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
2535b81b6b3SRodney W. Grimes {
2545b81b6b3SRodney W. Grimes 	int		t;
2555b81b6b3SRodney W. Grimes 	db_addr_t	addr;
2565b81b6b3SRodney W. Grimes 	int		size;
2575b81b6b3SRodney W. Grimes 	db_expr_t	value;
2585b81b6b3SRodney W. Grimes 	db_expr_t	mask;
259897cd717SDoug Rabson 	db_expr_t	count;
2605b81b6b3SRodney W. Grimes 
2615b81b6b3SRodney W. Grimes 	t = db_read_token();
2625b81b6b3SRodney W. Grimes 	if (t == tSLASH) {
2635b81b6b3SRodney W. Grimes 	    t = db_read_token();
2645b81b6b3SRodney W. Grimes 	    if (t != tIDENT) {
2655b81b6b3SRodney W. Grimes 	      bad_modifier:
2665b81b6b3SRodney W. Grimes 		db_printf("Bad modifier\n");
2675b81b6b3SRodney W. Grimes 		db_flush_lex();
2685b81b6b3SRodney W. Grimes 		return;
2695b81b6b3SRodney W. Grimes 	    }
2705b81b6b3SRodney W. Grimes 
2715b81b6b3SRodney W. Grimes 	    if (!strcmp(db_tok_string, "b"))
2725b81b6b3SRodney W. Grimes 		size = 1;
2735b81b6b3SRodney W. Grimes 	    else if (!strcmp(db_tok_string, "h"))
2745b81b6b3SRodney W. Grimes 		size = 2;
2755b81b6b3SRodney W. Grimes 	    else if (!strcmp(db_tok_string, "l"))
2765b81b6b3SRodney W. Grimes 		size = 4;
2775b81b6b3SRodney W. Grimes 	    else
2785b81b6b3SRodney W. Grimes 		goto bad_modifier;
2795b81b6b3SRodney W. Grimes 	} else {
2805b81b6b3SRodney W. Grimes 	    db_unread_token(t);
2815b81b6b3SRodney W. Grimes 	    size = 4;
2825b81b6b3SRodney W. Grimes 	}
2835b81b6b3SRodney W. Grimes 
284381fe1aaSGarrett Wollman 	if (!db_expression((db_expr_t *)&addr)) {
2855b81b6b3SRodney W. Grimes 	    db_printf("Address missing\n");
2865b81b6b3SRodney W. Grimes 	    db_flush_lex();
2875b81b6b3SRodney W. Grimes 	    return;
2885b81b6b3SRodney W. Grimes 	}
2895b81b6b3SRodney W. Grimes 
2905b81b6b3SRodney W. Grimes 	if (!db_expression(&value)) {
2915b81b6b3SRodney W. Grimes 	    db_printf("Value missing\n");
2925b81b6b3SRodney W. Grimes 	    db_flush_lex();
2935b81b6b3SRodney W. Grimes 	    return;
2945b81b6b3SRodney W. Grimes 	}
2955b81b6b3SRodney W. Grimes 
2965b81b6b3SRodney W. Grimes 	if (!db_expression(&mask))
297aaf08d94SGarrett Wollman 	    mask = 0xffffffffUL;
2985b81b6b3SRodney W. Grimes 
2995b81b6b3SRodney W. Grimes 	t = db_read_token();
3005b81b6b3SRodney W. Grimes 	if (t == tCOMMA) {
3015b81b6b3SRodney W. Grimes 	    if (!db_expression(&count)) {
3025b81b6b3SRodney W. Grimes 		db_printf("Count missing\n");
3035b81b6b3SRodney W. Grimes 		db_flush_lex();
3045b81b6b3SRodney W. Grimes 		return;
3055b81b6b3SRodney W. Grimes 	    }
3065b81b6b3SRodney W. Grimes 	} else {
3075b81b6b3SRodney W. Grimes 	    db_unread_token(t);
3085b81b6b3SRodney W. Grimes 	    count = -1;		/* effectively forever */
3095b81b6b3SRodney W. Grimes 	}
3105b81b6b3SRodney W. Grimes 	db_skip_to_eol();
3115b81b6b3SRodney W. Grimes 
3125b81b6b3SRodney W. Grimes 	db_search(addr, size, value, mask, count);
3135b81b6b3SRodney W. Grimes }
3145b81b6b3SRodney W. Grimes 
315381fe1aaSGarrett Wollman static void
db_search(db_addr_t addr,int size,db_expr_t value,db_expr_t mask,unsigned int count)316a41dd031SPedro F. Giffuni db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
317a41dd031SPedro F. Giffuni     unsigned int count)
3185b81b6b3SRodney W. Grimes {
3195b81b6b3SRodney W. Grimes 	while (count-- != 0) {
3205b81b6b3SRodney W. Grimes 		db_prev = addr;
3212b490bc7SPedro F. Giffuni 		if ((db_get_value(addr, size, false) & mask) == value)
3225b81b6b3SRodney W. Grimes 			break;
3235b81b6b3SRodney W. Grimes 		addr += size;
3245b81b6b3SRodney W. Grimes 	}
3255b81b6b3SRodney W. Grimes 	db_next = addr;
3265b81b6b3SRodney W. Grimes }
327