1 /*- 2 * SPDX-License-Identifier: MIT-CMU 3 * 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 /* 29 * Author: David B. Golub, Carnegie Mellon University 30 * Date: 7/90 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 36 #include <ddb/ddb.h> 37 38 #include <ddb/db_lex.h> 39 #include <ddb/db_output.h> 40 #include <ddb/db_command.h> 41 #include <ddb/db_sym.h> 42 #include <ddb/db_access.h> 43 44 static char db_examine_format[TOK_STRING_SIZE] = "x"; 45 46 static void db_examine(db_addr_t, char *, int); 47 static void db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int); 48 49 /* 50 * Examine (print) data. 51 */ 52 /*ARGSUSED*/ 53 void 54 db_examine_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 55 { 56 if (modif[0] != '\0') 57 db_strcpy(db_examine_format, modif); 58 59 if (count == -1) 60 count = 1; 61 62 db_examine((db_addr_t) addr, db_examine_format, count); 63 } 64 65 static void 66 db_examine(db_addr_t addr, char *fmt, int count) 67 { 68 int c; 69 db_expr_t value; 70 int size; 71 int width; 72 char * fp; 73 74 while (--count >= 0 && !db_pager_quit) { 75 fp = fmt; 76 size = 4; 77 while ((c = *fp++) != 0) { 78 switch (c) { 79 case 'b': 80 size = 1; 81 break; 82 case 'h': 83 size = 2; 84 break; 85 case 'l': 86 size = 4; 87 break; 88 case 'g': 89 size = 8; 90 break; 91 case 'a': /* address */ 92 size = sizeof(void *); 93 /* always forces a new line */ 94 if (db_print_position() != 0) 95 db_printf("\n"); 96 db_prev = addr; 97 db_printsym(addr, DB_STGY_ANY); 98 db_printf(":\t"); 99 break; 100 default: 101 if (db_print_position() == 0) { 102 /* Print the address. */ 103 db_printsym(addr, DB_STGY_ANY); 104 db_printf(":\t"); 105 db_prev = addr; 106 } 107 108 width = size * 4; 109 switch (c) { 110 case 'r': /* signed, current radix */ 111 value = db_get_value(addr, size, true); 112 addr += size; 113 db_printf("%+-*lr", width, (long)value); 114 break; 115 case 'x': /* unsigned hex */ 116 value = db_get_value(addr, size, false); 117 addr += size; 118 db_printf("%-*lx", width, (long)value); 119 break; 120 case 'z': /* signed hex */ 121 value = db_get_value(addr, size, true); 122 addr += size; 123 db_printf("%-*ly", width, (long)value); 124 break; 125 case 'd': /* signed decimal */ 126 value = db_get_value(addr, size, true); 127 addr += size; 128 db_printf("%-*ld", width, (long)value); 129 break; 130 case 'u': /* unsigned decimal */ 131 value = db_get_value(addr, size, false); 132 addr += size; 133 db_printf("%-*lu", width, (long)value); 134 break; 135 case 'o': /* unsigned octal */ 136 value = db_get_value(addr, size, false); 137 addr += size; 138 db_printf("%-*lo", width, (long)value); 139 break; 140 case 'c': /* character */ 141 value = db_get_value(addr, 1, false); 142 addr += 1; 143 if (value >= ' ' && value <= '~') 144 db_printf("%c", (int)value); 145 else 146 db_printf("\\%03o", (int)value); 147 break; 148 case 's': /* null-terminated string */ 149 for (;;) { 150 value = db_get_value(addr, 1, false); 151 addr += 1; 152 if (value == 0) 153 break; 154 if (value >= ' ' && value <= '~') 155 db_printf("%c", (int)value); 156 else 157 db_printf("\\%03o", (int)value); 158 } 159 break; 160 case 'S': /* symbol */ 161 value = db_get_value(addr, sizeof(void *), 162 false); 163 addr += sizeof(void *); 164 db_printsym(value, DB_STGY_ANY); 165 break; 166 case 'i': /* instruction */ 167 addr = db_disasm(addr, false); 168 break; 169 case 'I': /* instruction, alternate form */ 170 addr = db_disasm(addr, true); 171 break; 172 default: 173 break; 174 } 175 if (db_print_position() != 0) 176 db_end_line(1); 177 break; 178 } 179 } 180 } 181 db_next = addr; 182 } 183 184 /* 185 * Print value. 186 */ 187 static char db_print_format = 'x'; 188 189 /*ARGSUSED*/ 190 void 191 db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 192 { 193 db_expr_t value; 194 195 if (modif[0] != '\0') 196 db_print_format = modif[0]; 197 198 switch (db_print_format) { 199 case 'a': 200 db_printsym((db_addr_t)addr, DB_STGY_ANY); 201 break; 202 case 'r': 203 db_printf("%+11lr", (long)addr); 204 break; 205 case 'x': 206 db_printf("%8lx", (unsigned long)addr); 207 break; 208 case 'z': 209 db_printf("%8ly", (long)addr); 210 break; 211 case 'd': 212 db_printf("%11ld", (long)addr); 213 break; 214 case 'u': 215 db_printf("%11lu", (unsigned long)addr); 216 break; 217 case 'o': 218 db_printf("%16lo", (unsigned long)addr); 219 break; 220 case 'c': 221 value = addr & 0xFF; 222 if (value >= ' ' && value <= '~') 223 db_printf("%c", (int)value); 224 else 225 db_printf("\\%03o", (int)value); 226 break; 227 default: 228 db_print_format = 'x'; 229 db_error("Syntax error: unsupported print modifier\n"); 230 /*NOTREACHED*/ 231 } 232 db_printf("\n"); 233 } 234 235 void 236 db_print_loc_and_inst(db_addr_t loc) 237 { 238 db_expr_t off; 239 240 db_printsym(loc, DB_STGY_PROC); 241 if (db_search_symbol(loc, DB_STGY_PROC, &off) != C_DB_SYM_NULL) { 242 db_printf(":\t"); 243 (void)db_disasm(loc, false); 244 } 245 } 246 247 /* 248 * Search for a value in memory. 249 * Syntax: search [/bhl] addr value [mask] [,count] 250 */ 251 void 252 db_search_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 253 { 254 int t; 255 db_addr_t addr; 256 int size; 257 db_expr_t value; 258 db_expr_t mask; 259 db_expr_t count; 260 261 t = db_read_token(); 262 if (t == tSLASH) { 263 t = db_read_token(); 264 if (t != tIDENT) { 265 bad_modifier: 266 db_printf("Bad modifier\n"); 267 db_flush_lex(); 268 return; 269 } 270 271 if (!strcmp(db_tok_string, "b")) 272 size = 1; 273 else if (!strcmp(db_tok_string, "h")) 274 size = 2; 275 else if (!strcmp(db_tok_string, "l")) 276 size = 4; 277 else 278 goto bad_modifier; 279 } else { 280 db_unread_token(t); 281 size = 4; 282 } 283 284 if (!db_expression((db_expr_t *)&addr)) { 285 db_printf("Address missing\n"); 286 db_flush_lex(); 287 return; 288 } 289 290 if (!db_expression(&value)) { 291 db_printf("Value missing\n"); 292 db_flush_lex(); 293 return; 294 } 295 296 if (!db_expression(&mask)) 297 mask = 0xffffffffUL; 298 299 t = db_read_token(); 300 if (t == tCOMMA) { 301 if (!db_expression(&count)) { 302 db_printf("Count missing\n"); 303 db_flush_lex(); 304 return; 305 } 306 } else { 307 db_unread_token(t); 308 count = -1; /* effectively forever */ 309 } 310 db_skip_to_eol(); 311 312 db_search(addr, size, value, mask, count); 313 } 314 315 static void 316 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask, 317 unsigned int count) 318 { 319 while (count-- != 0) { 320 db_prev = addr; 321 if ((db_get_value(addr, size, false) & mask) == value) 322 break; 323 addr += size; 324 } 325 db_next = addr; 326 } 327