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 555b81b6b3SRodney W. Grimes db_examine_cmd(addr, have_addr, count, modif) 565b81b6b3SRodney W. Grimes db_expr_t addr; 57058284fcSBruce Evans boolean_t have_addr; 585b81b6b3SRodney W. Grimes db_expr_t count; 595b81b6b3SRodney W. Grimes char * modif; 605b81b6b3SRodney W. Grimes { 615b81b6b3SRodney W. Grimes if (modif[0] != '\0') 625b81b6b3SRodney W. Grimes db_strcpy(db_examine_format, modif); 635b81b6b3SRodney W. Grimes 645b81b6b3SRodney W. Grimes if (count == -1) 655b81b6b3SRodney W. Grimes count = 1; 665b81b6b3SRodney W. Grimes 675b81b6b3SRodney W. Grimes db_examine((db_addr_t) addr, db_examine_format, count); 685b81b6b3SRodney W. Grimes } 695b81b6b3SRodney W. Grimes 70381fe1aaSGarrett Wollman static void 715b81b6b3SRodney W. Grimes db_examine(addr, fmt, count) 725b81b6b3SRodney W. Grimes register 735b81b6b3SRodney W. Grimes db_addr_t addr; 745b81b6b3SRodney W. Grimes char * fmt; /* format string */ 755b81b6b3SRodney W. Grimes int count; /* repeat count */ 765b81b6b3SRodney W. Grimes { 775b81b6b3SRodney W. Grimes int c; 785b81b6b3SRodney W. Grimes db_expr_t value; 795b81b6b3SRodney W. Grimes int size; 805b81b6b3SRodney W. Grimes int width; 815b81b6b3SRodney W. Grimes char * fp; 825b81b6b3SRodney W. Grimes 836b76a4c7SJohn Baldwin while (--count >= 0 && !db_pager_quit) { 845b81b6b3SRodney W. Grimes fp = fmt; 855b81b6b3SRodney W. Grimes size = 4; 865b81b6b3SRodney W. Grimes while ((c = *fp++) != 0) { 875b81b6b3SRodney W. Grimes switch (c) { 885b81b6b3SRodney W. Grimes case 'b': 895b81b6b3SRodney W. Grimes size = 1; 905b81b6b3SRodney W. Grimes break; 915b81b6b3SRodney W. Grimes case 'h': 925b81b6b3SRodney W. Grimes size = 2; 935b81b6b3SRodney W. Grimes break; 945b81b6b3SRodney W. Grimes case 'l': 955b81b6b3SRodney W. Grimes size = 4; 965b81b6b3SRodney W. Grimes break; 971bfc653bSDoug Rabson case 'g': 981bfc653bSDoug Rabson size = 8; 991bfc653bSDoug Rabson break; 1005b81b6b3SRodney W. Grimes case 'a': /* address */ 101358ad31dSThomas Moestl size = sizeof(void *); 1025b81b6b3SRodney W. Grimes /* always forces a new line */ 1035b81b6b3SRodney W. Grimes if (db_print_position() != 0) 1045b81b6b3SRodney W. Grimes db_printf("\n"); 1055b81b6b3SRodney W. Grimes db_prev = addr; 1065b81b6b3SRodney W. Grimes db_printsym(addr, DB_STGY_ANY); 1075b81b6b3SRodney W. Grimes db_printf(":\t"); 1085b81b6b3SRodney W. Grimes break; 1095b81b6b3SRodney W. Grimes default: 1105b81b6b3SRodney W. Grimes if (db_print_position() == 0) { 111751b0b8eSDavid Greenman /* Print the address. */ 112751b0b8eSDavid Greenman db_printsym(addr, DB_STGY_ANY); 113751b0b8eSDavid Greenman db_printf(":\t"); 1145b81b6b3SRodney W. Grimes db_prev = addr; 1155b81b6b3SRodney W. Grimes } 1165b81b6b3SRodney W. Grimes 117358ad31dSThomas Moestl width = size * 4; 1185b81b6b3SRodney W. Grimes switch (c) { 1195b81b6b3SRodney W. Grimes case 'r': /* signed, current radix */ 1205b81b6b3SRodney W. Grimes value = db_get_value(addr, size, TRUE); 1215b81b6b3SRodney W. Grimes addr += size; 1221c6989faSPeter Wemm db_printf("%+-*lr", width, (long)value); 1235b81b6b3SRodney W. Grimes break; 1245b81b6b3SRodney W. Grimes case 'x': /* unsigned hex */ 1255b81b6b3SRodney W. Grimes value = db_get_value(addr, size, FALSE); 1265b81b6b3SRodney W. Grimes addr += size; 1271c6989faSPeter Wemm db_printf("%-*lx", width, (long)value); 1285b81b6b3SRodney W. Grimes break; 1295b81b6b3SRodney W. Grimes case 'z': /* signed hex */ 1305b81b6b3SRodney W. Grimes value = db_get_value(addr, size, TRUE); 1315b81b6b3SRodney W. Grimes addr += size; 1324578a2e6SMaxime Henrion db_printf("%-*ly", width, (long)value); 1335b81b6b3SRodney W. Grimes break; 1345b81b6b3SRodney W. Grimes case 'd': /* signed decimal */ 1355b81b6b3SRodney W. Grimes value = db_get_value(addr, size, TRUE); 1365b81b6b3SRodney W. Grimes addr += size; 1371c6989faSPeter Wemm db_printf("%-*ld", width, (long)value); 1385b81b6b3SRodney W. Grimes break; 1395b81b6b3SRodney W. Grimes case 'u': /* unsigned decimal */ 1405b81b6b3SRodney W. Grimes value = db_get_value(addr, size, FALSE); 1415b81b6b3SRodney W. Grimes addr += size; 1421c6989faSPeter Wemm db_printf("%-*lu", width, (long)value); 1435b81b6b3SRodney W. Grimes break; 1445b81b6b3SRodney W. Grimes case 'o': /* unsigned octal */ 1455b81b6b3SRodney W. Grimes value = db_get_value(addr, size, FALSE); 1465b81b6b3SRodney W. Grimes addr += size; 1471c6989faSPeter Wemm db_printf("%-*lo", width, (long)value); 1485b81b6b3SRodney W. Grimes break; 1495b81b6b3SRodney W. Grimes case 'c': /* character */ 1505b81b6b3SRodney W. Grimes value = db_get_value(addr, 1, FALSE); 1515b81b6b3SRodney W. Grimes addr += 1; 1525b81b6b3SRodney W. Grimes if (value >= ' ' && value <= '~') 1531c6989faSPeter Wemm db_printf("%c", (int)value); 1545b81b6b3SRodney W. Grimes else 1551c6989faSPeter Wemm db_printf("\\%03o", (int)value); 1565b81b6b3SRodney W. Grimes break; 1575b81b6b3SRodney W. Grimes case 's': /* null-terminated string */ 1585b81b6b3SRodney W. Grimes for (;;) { 1595b81b6b3SRodney W. Grimes value = db_get_value(addr, 1, FALSE); 1605b81b6b3SRodney W. Grimes addr += 1; 1615b81b6b3SRodney W. Grimes if (value == 0) 1625b81b6b3SRodney W. Grimes break; 1635b81b6b3SRodney W. Grimes if (value >= ' ' && value <= '~') 1641c6989faSPeter Wemm db_printf("%c", (int)value); 1655b81b6b3SRodney W. Grimes else 1661c6989faSPeter Wemm db_printf("\\%03o", (int)value); 1675b81b6b3SRodney W. Grimes } 1685b81b6b3SRodney W. Grimes break; 1697c7b7f8eSRobert Watson case 'S': /* symbol */ 1707c7b7f8eSRobert Watson value = db_get_value(addr, sizeof(void *), 1717c7b7f8eSRobert Watson FALSE); 1727c7b7f8eSRobert Watson addr += sizeof(void *); 1737c7b7f8eSRobert Watson db_printsym(value, DB_STGY_ANY); 1747c7b7f8eSRobert Watson break; 1755b81b6b3SRodney W. Grimes case 'i': /* instruction */ 1765b81b6b3SRodney W. Grimes addr = db_disasm(addr, FALSE); 1775b81b6b3SRodney W. Grimes break; 1785b81b6b3SRodney W. Grimes case 'I': /* instruction, alternate form */ 1795b81b6b3SRodney W. Grimes addr = db_disasm(addr, TRUE); 1805b81b6b3SRodney W. Grimes break; 1815b81b6b3SRodney W. Grimes default: 1825b81b6b3SRodney W. Grimes break; 1835b81b6b3SRodney W. Grimes } 1845b81b6b3SRodney W. Grimes if (db_print_position() != 0) 1852481da74SBruce Evans db_end_line(1); 1865b81b6b3SRodney W. Grimes break; 1875b81b6b3SRodney W. Grimes } 1885b81b6b3SRodney W. Grimes } 1895b81b6b3SRodney W. Grimes } 1905b81b6b3SRodney W. Grimes db_next = addr; 1915b81b6b3SRodney W. Grimes } 1925b81b6b3SRodney W. Grimes 1935b81b6b3SRodney W. Grimes /* 1945b81b6b3SRodney W. Grimes * Print value. 1955b81b6b3SRodney W. Grimes */ 196f73a856dSPoul-Henning Kamp static char db_print_format = 'x'; 1975b81b6b3SRodney W. Grimes 1985b81b6b3SRodney W. Grimes /*ARGSUSED*/ 1995b81b6b3SRodney W. Grimes void 2005b81b6b3SRodney W. Grimes db_print_cmd(addr, have_addr, count, modif) 2015b81b6b3SRodney W. Grimes db_expr_t addr; 202058284fcSBruce Evans boolean_t have_addr; 2035b81b6b3SRodney W. Grimes db_expr_t count; 2045b81b6b3SRodney W. Grimes char * modif; 2055b81b6b3SRodney W. Grimes { 2065b81b6b3SRodney W. Grimes db_expr_t value; 2075b81b6b3SRodney W. Grimes 2085b81b6b3SRodney W. Grimes if (modif[0] != '\0') 2095b81b6b3SRodney W. Grimes db_print_format = modif[0]; 2105b81b6b3SRodney W. Grimes 2115b81b6b3SRodney W. Grimes switch (db_print_format) { 2125b81b6b3SRodney W. Grimes case 'a': 2135b81b6b3SRodney W. Grimes db_printsym((db_addr_t)addr, DB_STGY_ANY); 2145b81b6b3SRodney W. Grimes break; 2155b81b6b3SRodney W. Grimes case 'r': 216596dfc04SBruce Evans db_printf("%+11lr", (long)addr); 2175b81b6b3SRodney W. Grimes break; 2185b81b6b3SRodney W. Grimes case 'x': 219b1bf7bc6SBruce Evans db_printf("%8lx", (unsigned long)addr); 2205b81b6b3SRodney W. Grimes break; 2215b81b6b3SRodney W. Grimes case 'z': 2224578a2e6SMaxime Henrion db_printf("%8ly", (long)addr); 2235b81b6b3SRodney W. Grimes break; 2245b81b6b3SRodney W. Grimes case 'd': 225b1bf7bc6SBruce Evans db_printf("%11ld", (long)addr); 2265b81b6b3SRodney W. Grimes break; 2275b81b6b3SRodney W. Grimes case 'u': 228b1bf7bc6SBruce Evans db_printf("%11lu", (unsigned long)addr); 2295b81b6b3SRodney W. Grimes break; 2305b81b6b3SRodney W. Grimes case 'o': 231b1bf7bc6SBruce Evans db_printf("%16lo", (unsigned long)addr); 2325b81b6b3SRodney W. Grimes break; 2335b81b6b3SRodney W. Grimes case 'c': 2345b81b6b3SRodney W. Grimes value = addr & 0xFF; 2355b81b6b3SRodney W. Grimes if (value >= ' ' && value <= '~') 2361c6989faSPeter Wemm db_printf("%c", (int)value); 2375b81b6b3SRodney W. Grimes else 2381c6989faSPeter Wemm db_printf("\\%03o", (int)value); 2395b81b6b3SRodney W. Grimes break; 2405b81b6b3SRodney W. Grimes } 2415b81b6b3SRodney W. Grimes db_printf("\n"); 2425b81b6b3SRodney W. Grimes } 2435b81b6b3SRodney W. Grimes 244381fe1aaSGarrett Wollman void 2455b81b6b3SRodney W. Grimes db_print_loc_and_inst(loc) 2465b81b6b3SRodney W. Grimes db_addr_t loc; 2475b81b6b3SRodney W. Grimes { 2485b81b6b3SRodney W. Grimes db_printsym(loc, DB_STGY_PROC); 2495b81b6b3SRodney W. Grimes db_printf(":\t"); 2505b81b6b3SRodney W. Grimes (void) db_disasm(loc, TRUE); 2515b81b6b3SRodney W. Grimes } 2525b81b6b3SRodney W. Grimes 2535b81b6b3SRodney W. Grimes /* 2545b81b6b3SRodney W. Grimes * Search for a value in memory. 2555b81b6b3SRodney W. Grimes * Syntax: search [/bhl] addr value [mask] [,count] 2565b81b6b3SRodney W. Grimes */ 2575b81b6b3SRodney W. Grimes void 258058284fcSBruce Evans db_search_cmd(dummy1, dummy2, dummy3, dummy4) 259058284fcSBruce Evans db_expr_t dummy1; 260058284fcSBruce Evans boolean_t dummy2; 261058284fcSBruce Evans db_expr_t dummy3; 262058284fcSBruce Evans char * dummy4; 2635b81b6b3SRodney W. Grimes { 2645b81b6b3SRodney W. Grimes int t; 2655b81b6b3SRodney W. Grimes db_addr_t addr; 2665b81b6b3SRodney W. Grimes int size; 2675b81b6b3SRodney W. Grimes db_expr_t value; 2685b81b6b3SRodney W. Grimes db_expr_t mask; 269897cd717SDoug Rabson db_expr_t count; 2705b81b6b3SRodney W. Grimes 2715b81b6b3SRodney W. Grimes t = db_read_token(); 2725b81b6b3SRodney W. Grimes if (t == tSLASH) { 2735b81b6b3SRodney W. Grimes t = db_read_token(); 2745b81b6b3SRodney W. Grimes if (t != tIDENT) { 2755b81b6b3SRodney W. Grimes bad_modifier: 2765b81b6b3SRodney W. Grimes db_printf("Bad modifier\n"); 2775b81b6b3SRodney W. Grimes db_flush_lex(); 2785b81b6b3SRodney W. Grimes return; 2795b81b6b3SRodney W. Grimes } 2805b81b6b3SRodney W. Grimes 2815b81b6b3SRodney W. Grimes if (!strcmp(db_tok_string, "b")) 2825b81b6b3SRodney W. Grimes size = 1; 2835b81b6b3SRodney W. Grimes else if (!strcmp(db_tok_string, "h")) 2845b81b6b3SRodney W. Grimes size = 2; 2855b81b6b3SRodney W. Grimes else if (!strcmp(db_tok_string, "l")) 2865b81b6b3SRodney W. Grimes size = 4; 2875b81b6b3SRodney W. Grimes else 2885b81b6b3SRodney W. Grimes goto bad_modifier; 2895b81b6b3SRodney W. Grimes } else { 2905b81b6b3SRodney W. Grimes db_unread_token(t); 2915b81b6b3SRodney W. Grimes size = 4; 2925b81b6b3SRodney W. Grimes } 2935b81b6b3SRodney W. Grimes 294381fe1aaSGarrett Wollman if (!db_expression((db_expr_t *)&addr)) { 2955b81b6b3SRodney W. Grimes db_printf("Address missing\n"); 2965b81b6b3SRodney W. Grimes db_flush_lex(); 2975b81b6b3SRodney W. Grimes return; 2985b81b6b3SRodney W. Grimes } 2995b81b6b3SRodney W. Grimes 3005b81b6b3SRodney W. Grimes if (!db_expression(&value)) { 3015b81b6b3SRodney W. Grimes db_printf("Value missing\n"); 3025b81b6b3SRodney W. Grimes db_flush_lex(); 3035b81b6b3SRodney W. Grimes return; 3045b81b6b3SRodney W. Grimes } 3055b81b6b3SRodney W. Grimes 3065b81b6b3SRodney W. Grimes if (!db_expression(&mask)) 307aaf08d94SGarrett Wollman mask = 0xffffffffUL; 3085b81b6b3SRodney W. Grimes 3095b81b6b3SRodney W. Grimes t = db_read_token(); 3105b81b6b3SRodney W. Grimes if (t == tCOMMA) { 3115b81b6b3SRodney W. Grimes if (!db_expression(&count)) { 3125b81b6b3SRodney W. Grimes db_printf("Count missing\n"); 3135b81b6b3SRodney W. Grimes db_flush_lex(); 3145b81b6b3SRodney W. Grimes return; 3155b81b6b3SRodney W. Grimes } 3165b81b6b3SRodney W. Grimes } else { 3175b81b6b3SRodney W. Grimes db_unread_token(t); 3185b81b6b3SRodney W. Grimes count = -1; /* effectively forever */ 3195b81b6b3SRodney W. Grimes } 3205b81b6b3SRodney W. Grimes db_skip_to_eol(); 3215b81b6b3SRodney W. Grimes 3225b81b6b3SRodney W. Grimes db_search(addr, size, value, mask, count); 3235b81b6b3SRodney W. Grimes } 3245b81b6b3SRodney W. Grimes 325381fe1aaSGarrett Wollman static void 3265b81b6b3SRodney W. Grimes db_search(addr, size, value, mask, count) 3275b81b6b3SRodney W. Grimes register 3285b81b6b3SRodney W. Grimes db_addr_t addr; 3295b81b6b3SRodney W. Grimes int size; 3305b81b6b3SRodney W. Grimes db_expr_t value; 3315b81b6b3SRodney W. Grimes db_expr_t mask; 3325b81b6b3SRodney W. Grimes unsigned int count; 3335b81b6b3SRodney W. Grimes { 3345b81b6b3SRodney W. Grimes while (count-- != 0) { 3355b81b6b3SRodney W. Grimes db_prev = addr; 3365b81b6b3SRodney W. Grimes if ((db_get_value(addr, size, FALSE) & mask) == value) 3375b81b6b3SRodney W. Grimes break; 3385b81b6b3SRodney W. Grimes addr += size; 3395b81b6b3SRodney W. Grimes } 3405b81b6b3SRodney W. Grimes db_next = addr; 3415b81b6b3SRodney W. Grimes } 342