db_examine.c revision 1.38 1 /* $NetBSD: db_examine.c,v 1.38 2019/09/10 08:16:05 ryo 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.38 2019/09/10 08:16:05 ryo 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 'q':
105 if (sizeof(db_expr_t) != sizeof(uint64_t)) {
106 size = -1;
107 db_error("q not supported\n");
108 /*NOTREACHED*/
109 }
110 /* FALLTHROUGH */
111 case 'L': /* implementation maximum */
112 size = sizeof value;
113 width = 12 * (sizeof value / 4);
114 break;
115 case 'a': /* address */
116 db_printf("= 0x%lx\n", (long)addr);
117 break;
118 case 'p':
119 size = sizeof(void *);
120 value = db_get_value(addr, size, true);
121 addr += size;
122 db_printf("= 0x%lx ", (long)value);
123 db_printsym((db_addr_t)value, DB_STGY_ANY, db_printf);
124 db_printf("\n");
125 break;
126 case 'r': /* signed, current radix */
127 value = db_get_value(addr, size, true);
128 addr += size;
129 db_format_radix(tbuf, 24, value, false);
130 db_printf("%-*s", width, tbuf);
131 break;
132 case 'x': /* unsigned hex */
133 value = db_get_value(addr, size, false);
134 addr += size;
135 db_printf("%-*" DDB_EXPR_FMT "x", width, value);
136 break;
137 case 'm': /* hex dump */
138 /*
139 * Print off in chunks of size. Try to print 16
140 * bytes at a time into 4 columns. This
141 * loops modify's count extra times in order
142 * to get the nicely formatted lines.
143 */
144
145 bytes = 0;
146 do {
147 for (i = 0; i < size; i++) {
148 value =
149 db_get_value(addr+bytes, 1,
150 false);
151 db_printf(
152 "%02" DDB_EXPR_FMT "x",
153 value);
154 bytes++;
155 if (!(bytes % 4))
156 db_printf(" ");
157 }
158 } while ((bytes != 16) && count--);
159 /* True up the columns before continuing */
160 for (i = 4; i >= (bytes / 4); i--)
161 db_printf ("\t");
162 /* Print chars, use . for non-printable's. */
163 while (bytes--) {
164 value = db_get_value(addr, 1, false);
165 addr += 1;
166 if (value >= ' ' && value <= '~')
167 db_printf("%c", (char)value);
168 else
169 db_printf(".");
170 }
171 db_printf("\n");
172 break;
173 case 'z': /* signed hex */
174 value = db_get_value(addr, size, true);
175 addr += size;
176 db_format_hex(tbuf, 24, value, false);
177 db_printf("%-*s", width, tbuf);
178 break;
179 case 'd': /* signed decimal */
180 value = db_get_value(addr, size, true);
181 addr += size;
182 db_printf("%-*" DDB_EXPR_FMT "d", width, value);
183 break;
184 case 'u': /* unsigned decimal */
185 value = db_get_value(addr, size, false);
186 addr += size;
187 db_printf("%-*" DDB_EXPR_FMT "u", width, value);
188 break;
189 case 'o': /* unsigned octal */
190 value = db_get_value(addr, size, false);
191 addr += size;
192 db_printf("%-*" DDB_EXPR_FMT "o", width, value);
193 break;
194 case 'c': /* character */
195 value = db_get_value(addr, 1, false);
196 addr += 1;
197 if (value >= ' ' && value <= '~')
198 db_printf("%c", (char)value);
199 else
200 db_printf("\\%03o", (int)value);
201 break;
202 case 's': /* null-terminated string */
203 for (;;) {
204 value = db_get_value(addr, 1, false);
205 addr += 1;
206 if (value == 0)
207 break;
208 if (value >= ' ' && value <= '~')
209 db_printf("%c", (char)value);
210 else
211 db_printf("\\%03o", (int)value);
212 }
213 break;
214 case 'i': /* instruction */
215 addr = db_disasm(addr, false);
216 break;
217 case 'I': /* instruction, alternate form */
218 addr = db_disasm(addr, true);
219 break;
220 default:
221 break;
222 }
223 if (db_print_position() != 0)
224 db_end_line();
225 }
226 }
227 db_next = addr;
228 }
229
230 /*
231 * Print value.
232 */
233 static char db_print_format = 'x';
234
235 /*ARGSUSED*/
236 void
237 db_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
238 const char *modif)
239 {
240 db_expr_t value;
241
242 if (modif[0] != '\0')
243 db_print_format = modif[0];
244
245 switch (db_print_format) {
246 case 'a':
247 db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
248 break;
249 case 'r':
250 {
251 char tbuf[24];
252
253 db_format_radix(tbuf, 24, addr, false);
254 db_printf("%11s", tbuf);
255 break;
256 }
257 case 'x':
258 db_printf("%16" DDB_EXPR_FMT "x", addr);
259 break;
260 case 'z':
261 {
262 char tbuf[24];
263
264 db_format_hex(tbuf, 24, addr, false);
265 db_printf("%8s", tbuf);
266 break;
267 }
268 case 'd':
269 db_printf("%11" DDB_EXPR_FMT "d", addr);
270 break;
271 case 'u':
272 db_printf("%11" DDB_EXPR_FMT "u", addr);
273 break;
274 case 'o':
275 db_printf("%15" DDB_EXPR_FMT "o", addr);
276 break;
277 case 'c':
278 value = addr & 0xFF;
279 if (value >= ' ' && value <= '~')
280 db_printf("%c", (char)value);
281 else
282 db_printf("\\%03o", (int)value);
283 break;
284 }
285 db_printf("\n");
286 }
287
288 void
289 db_print_loc_and_inst(db_addr_t loc)
290 {
291
292 db_printsym(loc, DB_STGY_PROC, db_printf);
293 db_printf(":\t");
294 (void) db_disasm(loc, false);
295 }
296
297 /*
298 * Search for a value in memory.
299 * Syntax: search [/bhl] addr value [mask] [,count]
300 */
301 /*ARGSUSED*/
302 void
303 db_search_cmd(db_expr_t daddr, bool have_addr,
304 db_expr_t dcount, const char *modif)
305 {
306 int t;
307 db_addr_t addr;
308 int size;
309 db_expr_t value;
310 db_expr_t mask;
311 db_expr_t count;
312
313 t = db_read_token();
314 if (t == tSLASH) {
315 t = db_read_token();
316 if (t != tIDENT) {
317 bad_modifier:
318 db_printf("Bad modifier\n");
319 db_flush_lex();
320 return;
321 }
322
323 if (!strcmp(db_tok_string, "b"))
324 size = 1;
325 else if (!strcmp(db_tok_string, "h"))
326 size = 2;
327 else if (!strcmp(db_tok_string, "l"))
328 size = 4;
329 else
330 goto bad_modifier;
331 } else {
332 db_unread_token(t);
333 size = 4;
334 }
335
336 if (!db_expression(&value)) {
337 db_printf("Address missing\n");
338 db_flush_lex();
339 return;
340 }
341 addr = (db_addr_t) value;
342
343 if (!db_expression(&value)) {
344 db_printf("Value missing\n");
345 db_flush_lex();
346 return;
347 }
348
349 if (!db_expression(&mask))
350 mask = (int) ~0;
351
352 t = db_read_token();
353 if (t == tCOMMA) {
354 if (!db_expression(&count)) {
355 db_printf("Count missing\n");
356 db_flush_lex();
357 return;
358 }
359 } else {
360 db_unread_token(t);
361 count = -1; /* effectively forever */
362 }
363 db_skip_to_eol();
364
365 db_search(addr, size, value, mask, count);
366 }
367
368 static void
369 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
370 unsigned int count)
371 {
372 while (count-- != 0) {
373 db_prev = addr;
374 if ((db_get_value(addr, size, false) & mask) == value)
375 break;
376 addr += size;
377 }
378 db_next = addr;
379 }
380