db_examine.c revision 1.34.6.1 1 /* $NetBSD: db_examine.c,v 1.34.6.1 2011/06/06 09:07:37 jruoho Exp $ */
2
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 "AS IS"
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 (at) 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 * Author: David B. Golub, Carnegie Mellon University
29 * Date: 7/90
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_examine.c,v 1.34.6.1 2011/06/06 09:07:37 jruoho Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/proc.h>
39
40 #include <ddb/ddb.h>
41
42 static char db_examine_format[TOK_STRING_SIZE] = "x";
43
44 static void db_examine(db_addr_t, char *, int);
45 static void db_search(db_addr_t, int, db_expr_t, db_expr_t, unsigned int);
46
47 /*
48 * Examine (print) data. Syntax is:
49 * x/[bhl][cdiorsuxz]*
50 * For example, the command:
51 * x/bxxxx
52 * should print:
53 * address: 01 23 45 67
54 */
55 /*ARGSUSED*/
56 void
57 db_examine_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
58 const char *modif)
59 {
60 if (modif[0] != '\0')
61 strlcpy(db_examine_format, modif, sizeof(db_examine_format));
62
63 if (count == -1)
64 count = 1;
65
66 db_examine((db_addr_t) addr, db_examine_format, count);
67 }
68
69 static void
70 db_examine(db_addr_t addr, char *fmt, int count)
71 {
72 int i, c;
73 db_expr_t value;
74 int size;
75 int width;
76 int bytes;
77 char * fp;
78 char tbuf[24];
79
80 while (--count >= 0) {
81 fp = fmt;
82 size = 4;
83 width = 12;
84 while ((c = *fp++) != 0) {
85 if (db_print_position() == 0) {
86 /* Always print the address. */
87 db_printsym(addr, DB_STGY_ANY, db_printf);
88 db_printf(":\t");
89 db_prev = addr;
90 }
91 switch (c) {
92 case 'b': /* byte */
93 size = 1;
94 width = 4;
95 break;
96 case 'h': /* half-word */
97 size = 2;
98 width = 8;
99 break;
100 case 'l': /* long-word */
101 size = 4;
102 width = 12;
103 break;
104 case 'L': /* implementation maximum */
105 size = sizeof value;
106 width = 12 * (sizeof value / 4);
107 break;
108 case 'a': /* address */
109 db_printf("= 0x%lx\n", (long)addr);
110 break;
111 case 'r': /* signed, current radix */
112 value = db_get_value(addr, size, true);
113 addr += size;
114 db_format_radix(tbuf, 24, value, false);
115 db_printf("%-*s", width, tbuf);
116 break;
117 case 'x': /* unsigned hex */
118 value = db_get_value(addr, size, false);
119 addr += size;
120 db_printf("%-*" DDB_EXPR_FMT "x", width, value);
121 break;
122 case 'm': /* hex dump */
123 /*
124 * Print off in chunks of size. Try to print 16
125 * bytes at a time into 4 columns. This
126 * loops modify's count extra times in order
127 * to get the nicely formatted lines.
128 */
129
130 bytes = 0;
131 do {
132 for (i = 0; i < size; i++) {
133 value =
134 db_get_value(addr+bytes, 1,
135 false);
136 db_printf(
137 "%02" DDB_EXPR_FMT "x",
138 value);
139 bytes++;
140 if (!(bytes % 4))
141 db_printf(" ");
142 }
143 } while ((bytes != 16) && count--);
144 /* True up the columns before continuing */
145 for (i = 4; i >= (bytes / 4); i--)
146 db_printf ("\t");
147 /* Print chars, use . for non-printable's. */
148 while (bytes--) {
149 value = db_get_value(addr, 1, false);
150 addr += 1;
151 if (value >= ' ' && value <= '~')
152 db_printf("%c", (char)value);
153 else
154 db_printf(".");
155 }
156 db_printf("\n");
157 break;
158 case 'z': /* signed hex */
159 value = db_get_value(addr, size, true);
160 addr += size;
161 db_format_hex(tbuf, 24, value, false);
162 db_printf("%-*s", width, tbuf);
163 break;
164 case 'd': /* signed decimal */
165 value = db_get_value(addr, size, true);
166 addr += size;
167 db_printf("%-*" DDB_EXPR_FMT "d", width, value);
168 break;
169 case 'u': /* unsigned decimal */
170 value = db_get_value(addr, size, false);
171 addr += size;
172 db_printf("%-*" DDB_EXPR_FMT "u", width, value);
173 break;
174 case 'o': /* unsigned octal */
175 value = db_get_value(addr, size, false);
176 addr += size;
177 db_printf("%-*" DDB_EXPR_FMT "o", width, value);
178 break;
179 case 'c': /* character */
180 value = db_get_value(addr, 1, false);
181 addr += 1;
182 if (value >= ' ' && value <= '~')
183 db_printf("%c", (char)value);
184 else
185 db_printf("\\%03o", (int)value);
186 break;
187 case 's': /* null-terminated string */
188 for (;;) {
189 value = db_get_value(addr, 1, false);
190 addr += 1;
191 if (value == 0)
192 break;
193 if (value >= ' ' && value <= '~')
194 db_printf("%c", (char)value);
195 else
196 db_printf("\\%03o", (int)value);
197 }
198 break;
199 case 'i': /* instruction */
200 addr = db_disasm(addr, false);
201 break;
202 case 'I': /* instruction, alternate form */
203 addr = db_disasm(addr, true);
204 break;
205 default:
206 break;
207 }
208 if (db_print_position() != 0)
209 db_end_line();
210 }
211 }
212 db_next = addr;
213 }
214
215 /*
216 * Print value.
217 */
218 static char db_print_format = 'x';
219
220 /*ARGSUSED*/
221 void
222 db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
223 const char *modif)
224 {
225 db_expr_t value;
226
227 if (modif[0] != '\0')
228 db_print_format = modif[0];
229
230 switch (db_print_format) {
231 case 'a':
232 db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
233 break;
234 case 'r':
235 {
236 char tbuf[24];
237
238 db_format_radix(tbuf, 24, addr, false);
239 db_printf("%11s", tbuf);
240 break;
241 }
242 case 'x':
243 db_printf("%16" DDB_EXPR_FMT "x", addr);
244 break;
245 case 'z':
246 {
247 char tbuf[24];
248
249 db_format_hex(tbuf, 24, addr, false);
250 db_printf("%8s", tbuf);
251 break;
252 }
253 case 'd':
254 db_printf("%11" DDB_EXPR_FMT "d", addr);
255 break;
256 case 'u':
257 db_printf("%11" DDB_EXPR_FMT "u", addr);
258 break;
259 case 'o':
260 db_printf("%15" DDB_EXPR_FMT "o", addr);
261 break;
262 case 'c':
263 value = addr & 0xFF;
264 if (value >= ' ' && value <= '~')
265 db_printf("%c", (char)value);
266 else
267 db_printf("\\%03o", (int)value);
268 break;
269 }
270 db_printf("\n");
271 }
272
273 void
274 db_print_loc_and_inst(db_addr_t loc)
275 {
276
277 db_printsym(loc, DB_STGY_PROC, db_printf);
278 db_printf(":\t");
279 (void) db_disasm(loc, false);
280 }
281
282 /*
283 * Search for a value in memory.
284 * Syntax: search [/bhl] addr value [mask] [,count]
285 */
286 /*ARGSUSED*/
287 void
288 db_search_cmd(db_expr_t daddr, bool have_addr,
289 db_expr_t dcount, const char *modif)
290 {
291 int t;
292 db_addr_t addr;
293 int size;
294 db_expr_t value;
295 db_expr_t mask;
296 db_expr_t count;
297
298 t = db_read_token();
299 if (t == tSLASH) {
300 t = db_read_token();
301 if (t != tIDENT) {
302 bad_modifier:
303 db_printf("Bad modifier\n");
304 db_flush_lex();
305 return;
306 }
307
308 if (!strcmp(db_tok_string, "b"))
309 size = 1;
310 else if (!strcmp(db_tok_string, "h"))
311 size = 2;
312 else if (!strcmp(db_tok_string, "l"))
313 size = 4;
314 else
315 goto bad_modifier;
316 } else {
317 db_unread_token(t);
318 size = 4;
319 }
320
321 if (!db_expression(&value)) {
322 db_printf("Address missing\n");
323 db_flush_lex();
324 return;
325 }
326 addr = (db_addr_t) value;
327
328 if (!db_expression(&value)) {
329 db_printf("Value missing\n");
330 db_flush_lex();
331 return;
332 }
333
334 if (!db_expression(&mask))
335 mask = (int) ~0;
336
337 t = db_read_token();
338 if (t == tCOMMA) {
339 if (!db_expression(&count)) {
340 db_printf("Count missing\n");
341 db_flush_lex();
342 return;
343 }
344 } else {
345 db_unread_token(t);
346 count = -1; /* effectively forever */
347 }
348 db_skip_to_eol();
349
350 db_search(addr, size, value, mask, count);
351 }
352
353 static void
354 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
355 unsigned int count)
356 {
357 while (count-- != 0) {
358 db_prev = addr;
359 if ((db_get_value(addr, size, false) & mask) == value)
360 break;
361 addr += size;
362 }
363 db_next = addr;
364 }
365