db_examine.c revision 272461
155682Smarkm/*- 2233294Sstas * Mach Operating System 3233294Sstas * Copyright (c) 1991,1990 Carnegie Mellon University 4233294Sstas * All Rights Reserved. 555682Smarkm * 6233294Sstas * Permission to use, copy, modify and distribute this software and its 7233294Sstas * documentation is hereby granted, provided that both the copyright 8233294Sstas * notice and this permission notice appear in all copies of the 955682Smarkm * software, derivative works or modified versions, and any portions 10233294Sstas * thereof, and that both notices appear in supporting documentation. 11233294Sstas * 1255682Smarkm * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13233294Sstas * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14233294Sstas * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15233294Sstas * 1655682Smarkm * Carnegie Mellon requests users of this software to return to 17233294Sstas * 18233294Sstas * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19233294Sstas * School of Computer Science 2055682Smarkm * Carnegie Mellon University 21233294Sstas * Pittsburgh PA 15213-3890 22233294Sstas * 23233294Sstas * any improvements or extensions that they make and grant Carnegie the 24233294Sstas * rights to redistribute these changes. 25233294Sstas */ 26233294Sstas/* 27233294Sstas * Author: David B. Golub, Carnegie Mellon University 28233294Sstas * Date: 7/90 29233294Sstas */ 30233294Sstas 31233294Sstas#include <sys/cdefs.h> 3255682Smarkm__FBSDID("$FreeBSD: releng/10.1/sys/ddb/db_examine.c 176914 2008-03-07 18:09:07Z rwatson $"); 3355682Smarkm 3455682Smarkm#include <sys/param.h> 35233294Sstas#include <sys/systm.h> 36233294Sstas 37233294Sstas#include <ddb/ddb.h> 3855682Smarkm 39233294Sstas#include <ddb/db_lex.h> 40233294Sstas#include <ddb/db_output.h> 41233294Sstas#include <ddb/db_command.h> 42178825Sdfr#include <ddb/db_sym.h> 43178825Sdfr#include <ddb/db_access.h> 44178825Sdfr 45178825Sdfrstatic char db_examine_format[TOK_STRING_SIZE] = "x"; 46178825Sdfr 47178825Sdfrstatic void db_examine(db_addr_t, char *, int); 48233294Sstasstatic void db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int); 49178825Sdfr 50178825Sdfr/* 51178825Sdfr * Examine (print) data. 52233294Sstas */ 53178825Sdfr/*ARGSUSED*/ 54178825Sdfrvoid 55178825Sdfrdb_examine_cmd(addr, have_addr, count, modif) 56178825Sdfr db_expr_t addr; 57178825Sdfr boolean_t have_addr; 58178825Sdfr db_expr_t count; 59178825Sdfr char * modif; 60178825Sdfr{ 61178825Sdfr if (modif[0] != '\0') 62233294Sstas db_strcpy(db_examine_format, modif); 63178825Sdfr 64178825Sdfr if (count == -1) 65178825Sdfr count = 1; 66178825Sdfr 67178825Sdfr db_examine((db_addr_t) addr, db_examine_format, count); 68178825Sdfr} 69233294Sstas 70178825Sdfrstatic void 71178825Sdfrdb_examine(addr, fmt, count) 72178825Sdfr register 73178825Sdfr db_addr_t addr; 74178825Sdfr char * fmt; /* format string */ 75233294Sstas int count; /* repeat count */ 76178825Sdfr{ 77178825Sdfr int c; 78178825Sdfr db_expr_t value; 79178825Sdfr int size; 80178825Sdfr int width; 81178825Sdfr char * fp; 82178825Sdfr 83178825Sdfr while (--count >= 0 && !db_pager_quit) { 84178825Sdfr fp = fmt; 85233294Sstas size = 4; 86178825Sdfr while ((c = *fp++) != 0) { 87178825Sdfr switch (c) { 88233294Sstas case 'b': 89233294Sstas size = 1; 90233294Sstas break; 91233294Sstas case 'h': 92233294Sstas size = 2; 93233294Sstas break; 94233294Sstas case 'l': 95233294Sstas size = 4; 96233294Sstas break; 97233294Sstas case 'g': 98233294Sstas size = 8; 99233294Sstas break; 100233294Sstas case 'a': /* address */ 101233294Sstas size = sizeof(void *); 102233294Sstas /* always forces a new line */ 103233294Sstas if (db_print_position() != 0) 104233294Sstas db_printf("\n"); 105233294Sstas db_prev = addr; 106233294Sstas db_printsym(addr, DB_STGY_ANY); 107233294Sstas db_printf(":\t"); 108233294Sstas break; 109233294Sstas default: 110233294Sstas if (db_print_position() == 0) { 111233294Sstas /* Print the address. */ 112233294Sstas db_printsym(addr, DB_STGY_ANY); 113233294Sstas db_printf(":\t"); 114233294Sstas db_prev = addr; 115233294Sstas } 116233294Sstas 117233294Sstas width = size * 4; 118233294Sstas switch (c) { 119233294Sstas case 'r': /* signed, current radix */ 120233294Sstas value = db_get_value(addr, size, TRUE); 121233294Sstas addr += size; 122233294Sstas db_printf("%+-*lr", width, (long)value); 123233294Sstas break; 124233294Sstas case 'x': /* unsigned hex */ 125233294Sstas value = db_get_value(addr, size, FALSE); 126233294Sstas addr += size; 127233294Sstas db_printf("%-*lx", width, (long)value); 128233294Sstas break; 129 case 'z': /* signed hex */ 130 value = db_get_value(addr, size, TRUE); 131 addr += size; 132 db_printf("%-*ly", width, (long)value); 133 break; 134 case 'd': /* signed decimal */ 135 value = db_get_value(addr, size, TRUE); 136 addr += size; 137 db_printf("%-*ld", width, (long)value); 138 break; 139 case 'u': /* unsigned decimal */ 140 value = db_get_value(addr, size, FALSE); 141 addr += size; 142 db_printf("%-*lu", width, (long)value); 143 break; 144 case 'o': /* unsigned octal */ 145 value = db_get_value(addr, size, FALSE); 146 addr += size; 147 db_printf("%-*lo", width, (long)value); 148 break; 149 case 'c': /* character */ 150 value = db_get_value(addr, 1, FALSE); 151 addr += 1; 152 if (value >= ' ' && value <= '~') 153 db_printf("%c", (int)value); 154 else 155 db_printf("\\%03o", (int)value); 156 break; 157 case 's': /* null-terminated string */ 158 for (;;) { 159 value = db_get_value(addr, 1, FALSE); 160 addr += 1; 161 if (value == 0) 162 break; 163 if (value >= ' ' && value <= '~') 164 db_printf("%c", (int)value); 165 else 166 db_printf("\\%03o", (int)value); 167 } 168 break; 169 case 'S': /* symbol */ 170 value = db_get_value(addr, sizeof(void *), 171 FALSE); 172 addr += sizeof(void *); 173 db_printsym(value, DB_STGY_ANY); 174 break; 175 case 'i': /* instruction */ 176 addr = db_disasm(addr, FALSE); 177 break; 178 case 'I': /* instruction, alternate form */ 179 addr = db_disasm(addr, TRUE); 180 break; 181 default: 182 break; 183 } 184 if (db_print_position() != 0) 185 db_end_line(1); 186 break; 187 } 188 } 189 } 190 db_next = addr; 191} 192 193/* 194 * Print value. 195 */ 196static char db_print_format = 'x'; 197 198/*ARGSUSED*/ 199void 200db_print_cmd(addr, have_addr, count, modif) 201 db_expr_t addr; 202 boolean_t have_addr; 203 db_expr_t count; 204 char * modif; 205{ 206 db_expr_t value; 207 208 if (modif[0] != '\0') 209 db_print_format = modif[0]; 210 211 switch (db_print_format) { 212 case 'a': 213 db_printsym((db_addr_t)addr, DB_STGY_ANY); 214 break; 215 case 'r': 216 db_printf("%+11lr", (long)addr); 217 break; 218 case 'x': 219 db_printf("%8lx", (unsigned long)addr); 220 break; 221 case 'z': 222 db_printf("%8ly", (long)addr); 223 break; 224 case 'd': 225 db_printf("%11ld", (long)addr); 226 break; 227 case 'u': 228 db_printf("%11lu", (unsigned long)addr); 229 break; 230 case 'o': 231 db_printf("%16lo", (unsigned long)addr); 232 break; 233 case 'c': 234 value = addr & 0xFF; 235 if (value >= ' ' && value <= '~') 236 db_printf("%c", (int)value); 237 else 238 db_printf("\\%03o", (int)value); 239 break; 240 } 241 db_printf("\n"); 242} 243 244void 245db_print_loc_and_inst(loc) 246 db_addr_t loc; 247{ 248 db_printsym(loc, DB_STGY_PROC); 249 db_printf(":\t"); 250 (void) db_disasm(loc, TRUE); 251} 252 253/* 254 * Search for a value in memory. 255 * Syntax: search [/bhl] addr value [mask] [,count] 256 */ 257void 258db_search_cmd(dummy1, dummy2, dummy3, dummy4) 259 db_expr_t dummy1; 260 boolean_t dummy2; 261 db_expr_t dummy3; 262 char * dummy4; 263{ 264 int t; 265 db_addr_t addr; 266 int size; 267 db_expr_t value; 268 db_expr_t mask; 269 db_expr_t count; 270 271 t = db_read_token(); 272 if (t == tSLASH) { 273 t = db_read_token(); 274 if (t != tIDENT) { 275 bad_modifier: 276 db_printf("Bad modifier\n"); 277 db_flush_lex(); 278 return; 279 } 280 281 if (!strcmp(db_tok_string, "b")) 282 size = 1; 283 else if (!strcmp(db_tok_string, "h")) 284 size = 2; 285 else if (!strcmp(db_tok_string, "l")) 286 size = 4; 287 else 288 goto bad_modifier; 289 } else { 290 db_unread_token(t); 291 size = 4; 292 } 293 294 if (!db_expression((db_expr_t *)&addr)) { 295 db_printf("Address missing\n"); 296 db_flush_lex(); 297 return; 298 } 299 300 if (!db_expression(&value)) { 301 db_printf("Value missing\n"); 302 db_flush_lex(); 303 return; 304 } 305 306 if (!db_expression(&mask)) 307 mask = 0xffffffffUL; 308 309 t = db_read_token(); 310 if (t == tCOMMA) { 311 if (!db_expression(&count)) { 312 db_printf("Count missing\n"); 313 db_flush_lex(); 314 return; 315 } 316 } else { 317 db_unread_token(t); 318 count = -1; /* effectively forever */ 319 } 320 db_skip_to_eol(); 321 322 db_search(addr, size, value, mask, count); 323} 324 325static void 326db_search(addr, size, value, mask, count) 327 register 328 db_addr_t addr; 329 int size; 330 db_expr_t value; 331 db_expr_t mask; 332 unsigned int count; 333{ 334 while (count-- != 0) { 335 db_prev = addr; 336 if ((db_get_value(addr, size, FALSE) & mask) == value) 337 break; 338 addr += size; 339 } 340 db_next = addr; 341} 342