db_examine.c revision 1.21 1 1.21 cgd /* $NetBSD: db_examine.c,v 1.21 2001/02/24 00:00:12 cgd Exp $ */
2 1.4 cgd
3 1.7 mycroft /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1991,1990 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.7 mycroft *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.7 mycroft *
14 1.15 pk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.7 mycroft *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.7 mycroft *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.7 mycroft *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
26 1.1 cgd * rights to redistribute these changes.
27 1.2 cgd *
28 1.1 cgd * Author: David B. Golub, Carnegie Mellon University
29 1.1 cgd * Date: 7/90
30 1.1 cgd */
31 1.3 mycroft
32 1.3 mycroft #include <sys/param.h>
33 1.21 cgd #include <sys/systm.h>
34 1.3 mycroft #include <sys/proc.h>
35 1.3 mycroft
36 1.1 cgd #include <machine/db_machdep.h> /* type definitions */
37 1.1 cgd
38 1.1 cgd #include <ddb/db_lex.h>
39 1.1 cgd #include <ddb/db_output.h>
40 1.1 cgd #include <ddb/db_command.h>
41 1.1 cgd #include <ddb/db_sym.h>
42 1.10 christos #include <ddb/db_access.h>
43 1.10 christos #include <ddb/db_extern.h>
44 1.10 christos #include <ddb/db_interface.h>
45 1.1 cgd
46 1.1 cgd char db_examine_format[TOK_STRING_SIZE] = "x";
47 1.1 cgd
48 1.1 cgd /*
49 1.6 gwr * Examine (print) data. Syntax is:
50 1.6 gwr * x/[bhl][cdiorsuxz]*
51 1.6 gwr * For example, the command:
52 1.6 gwr * x/bxxxx
53 1.6 gwr * should print:
54 1.6 gwr * address: 01 23 45 67
55 1.1 cgd */
56 1.1 cgd /*ARGSUSED*/
57 1.1 cgd void
58 1.1 cgd db_examine_cmd(addr, have_addr, count, modif)
59 1.1 cgd db_expr_t addr;
60 1.1 cgd int have_addr;
61 1.1 cgd db_expr_t count;
62 1.1 cgd char * modif;
63 1.1 cgd {
64 1.1 cgd if (modif[0] != '\0')
65 1.7 mycroft db_strcpy(db_examine_format, modif);
66 1.1 cgd
67 1.1 cgd if (count == -1)
68 1.7 mycroft count = 1;
69 1.1 cgd
70 1.1 cgd db_examine((db_addr_t) addr, db_examine_format, count);
71 1.1 cgd }
72 1.1 cgd
73 1.10 christos void
74 1.1 cgd db_examine(addr, fmt, count)
75 1.17 augustss db_addr_t addr;
76 1.1 cgd char * fmt; /* format string */
77 1.1 cgd int count; /* repeat count */
78 1.1 cgd {
79 1.20 jmc int i, c;
80 1.1 cgd db_expr_t value;
81 1.1 cgd int size;
82 1.1 cgd int width;
83 1.20 jmc int bytes;
84 1.1 cgd char * fp;
85 1.19 tv char tbuf[24];
86 1.7 mycroft
87 1.1 cgd while (--count >= 0) {
88 1.7 mycroft fp = fmt;
89 1.7 mycroft size = 4;
90 1.8 gwr width = 12;
91 1.7 mycroft while ((c = *fp++) != 0) {
92 1.9 gwr if (db_print_position() == 0) {
93 1.9 gwr /* Always print the address. */
94 1.18 jhawk db_printsym(addr, DB_STGY_ANY, db_printf);
95 1.9 gwr db_printf(":\t");
96 1.9 gwr db_prev = addr;
97 1.9 gwr }
98 1.6 gwr switch (c) {
99 1.9 gwr case 'b': /* byte */
100 1.6 gwr size = 1;
101 1.6 gwr width = 4;
102 1.6 gwr break;
103 1.9 gwr case 'h': /* half-word */
104 1.6 gwr size = 2;
105 1.6 gwr width = 8;
106 1.6 gwr break;
107 1.9 gwr case 'l': /* long-word */
108 1.6 gwr size = 4;
109 1.8 gwr width = 12;
110 1.16 ross break;
111 1.16 ross case 'L': /* implementation maximum */
112 1.16 ross size = sizeof value;
113 1.16 ross width = 12 * (sizeof value / 4);
114 1.6 gwr break;
115 1.7 mycroft case 'a': /* address */
116 1.11 christos db_printf("= 0x%lx\n", addr);
117 1.6 gwr break;
118 1.6 gwr case 'r': /* signed, current radix */
119 1.1 cgd value = db_get_value(addr, size, TRUE);
120 1.1 cgd addr += size;
121 1.19 tv db_format_radix(tbuf, 24, value, FALSE);
122 1.19 tv db_printf("%-*s", width, tbuf);
123 1.1 cgd break;
124 1.6 gwr case 'x': /* unsigned hex */
125 1.1 cgd value = db_get_value(addr, size, FALSE);
126 1.1 cgd addr += size;
127 1.13 mycroft db_printf("%-*lx", width, value);
128 1.20 jmc break;
129 1.20 jmc case 'm': /* hex dump */
130 1.20 jmc /*
131 1.20 jmc * Print off in chunks of size. Try to print 16
132 1.20 jmc * bytes at a time into 4 columns. This
133 1.20 jmc * loops modify's count extra times in order
134 1.20 jmc * to get the nicely formatted lines.
135 1.20 jmc */
136 1.20 jmc
137 1.20 jmc bytes = 0;
138 1.20 jmc do {
139 1.20 jmc for (i = 0; i < size; i++) {
140 1.20 jmc value =
141 1.20 jmc db_get_value(addr+bytes, 1,
142 1.20 jmc FALSE);
143 1.20 jmc db_printf("%02lx", value);
144 1.20 jmc bytes++;
145 1.20 jmc if (!(bytes % 4))
146 1.20 jmc db_printf(" ");
147 1.20 jmc }
148 1.20 jmc } while ((bytes != 16) && count--);
149 1.20 jmc /* True up the columns before continuing */
150 1.20 jmc for (i = 4; i >= (bytes / 4); i--)
151 1.20 jmc db_printf ("\t");
152 1.20 jmc /* Print chars, use . for non-printable's. */
153 1.20 jmc while (bytes--) {
154 1.20 jmc value = db_get_value(addr, 1, FALSE);
155 1.20 jmc addr += 1;
156 1.20 jmc if (value >= ' ' && value <= '~')
157 1.20 jmc db_printf("%c", (char)value);
158 1.20 jmc else
159 1.20 jmc db_printf(".");
160 1.20 jmc }
161 1.20 jmc db_printf("\n");
162 1.1 cgd break;
163 1.6 gwr case 'z': /* signed hex */
164 1.1 cgd value = db_get_value(addr, size, TRUE);
165 1.1 cgd addr += size;
166 1.19 tv db_format_hex(tbuf, 24, value, FALSE);
167 1.19 tv db_printf("%-*s", width, tbuf);
168 1.1 cgd break;
169 1.6 gwr case 'd': /* signed decimal */
170 1.1 cgd value = db_get_value(addr, size, TRUE);
171 1.1 cgd addr += size;
172 1.13 mycroft db_printf("%-*ld", width, value);
173 1.1 cgd break;
174 1.6 gwr case 'u': /* unsigned decimal */
175 1.1 cgd value = db_get_value(addr, size, FALSE);
176 1.1 cgd addr += size;
177 1.13 mycroft db_printf("%-*lu", width, value);
178 1.1 cgd break;
179 1.6 gwr case 'o': /* unsigned octal */
180 1.1 cgd value = db_get_value(addr, size, FALSE);
181 1.1 cgd addr += size;
182 1.13 mycroft db_printf("%-*lo", width, value);
183 1.1 cgd break;
184 1.6 gwr case 'c': /* character */
185 1.1 cgd value = db_get_value(addr, 1, FALSE);
186 1.1 cgd addr += 1;
187 1.1 cgd if (value >= ' ' && value <= '~')
188 1.13 mycroft db_printf("%c", (char)value);
189 1.1 cgd else
190 1.13 mycroft db_printf("\\%03lo", value);
191 1.1 cgd break;
192 1.6 gwr case 's': /* null-terminated string */
193 1.1 cgd for (;;) {
194 1.7 mycroft value = db_get_value(addr, 1, FALSE);
195 1.7 mycroft addr += 1;
196 1.7 mycroft if (value == 0)
197 1.6 gwr break;
198 1.7 mycroft if (value >= ' ' && value <= '~')
199 1.13 mycroft db_printf("%c", (char)value);
200 1.7 mycroft else
201 1.13 mycroft db_printf("\\%03lo", value);
202 1.1 cgd }
203 1.1 cgd break;
204 1.6 gwr case 'i': /* instruction */
205 1.1 cgd addr = db_disasm(addr, FALSE);
206 1.1 cgd break;
207 1.6 gwr case 'I': /* instruction, alternate form */
208 1.1 cgd addr = db_disasm(addr, TRUE);
209 1.1 cgd break;
210 1.6 gwr default:
211 1.1 cgd break;
212 1.1 cgd }
213 1.1 cgd if (db_print_position() != 0)
214 1.6 gwr db_end_line();
215 1.7 mycroft }
216 1.1 cgd }
217 1.1 cgd db_next = addr;
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd /*
221 1.1 cgd * Print value.
222 1.1 cgd */
223 1.1 cgd char db_print_format = 'x';
224 1.1 cgd
225 1.1 cgd /*ARGSUSED*/
226 1.1 cgd void
227 1.1 cgd db_print_cmd(addr, have_addr, count, modif)
228 1.1 cgd db_expr_t addr;
229 1.1 cgd int have_addr;
230 1.1 cgd db_expr_t count;
231 1.1 cgd char * modif;
232 1.1 cgd {
233 1.1 cgd db_expr_t value;
234 1.1 cgd
235 1.1 cgd if (modif[0] != '\0')
236 1.7 mycroft db_print_format = modif[0];
237 1.1 cgd
238 1.1 cgd switch (db_print_format) {
239 1.7 mycroft case 'a':
240 1.18 jhawk db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
241 1.1 cgd break;
242 1.7 mycroft case 'r':
243 1.19 tv {
244 1.19 tv char tbuf[24];
245 1.19 tv
246 1.19 tv db_format_radix(tbuf, 24, addr, FALSE);
247 1.19 tv db_printf("%11s", tbuf);
248 1.19 tv break;
249 1.19 tv }
250 1.7 mycroft case 'x':
251 1.13 mycroft db_printf("%8lx", addr);
252 1.1 cgd break;
253 1.7 mycroft case 'z':
254 1.19 tv {
255 1.19 tv char tbuf[24];
256 1.19 tv
257 1.19 tv db_format_hex(tbuf, 24, addr, FALSE);
258 1.19 tv db_printf("%8s", tbuf);
259 1.19 tv break;
260 1.19 tv }
261 1.7 mycroft case 'd':
262 1.13 mycroft db_printf("%11ld", addr);
263 1.1 cgd break;
264 1.7 mycroft case 'u':
265 1.13 mycroft db_printf("%11lu", addr);
266 1.1 cgd break;
267 1.7 mycroft case 'o':
268 1.13 mycroft db_printf("%16lo", addr);
269 1.1 cgd break;
270 1.7 mycroft case 'c':
271 1.1 cgd value = addr & 0xFF;
272 1.1 cgd if (value >= ' ' && value <= '~')
273 1.13 mycroft db_printf("%c", (char)value);
274 1.1 cgd else
275 1.13 mycroft db_printf("\\%03lo", value);
276 1.1 cgd break;
277 1.1 cgd }
278 1.1 cgd db_printf("\n");
279 1.1 cgd }
280 1.1 cgd
281 1.10 christos void
282 1.1 cgd db_print_loc_and_inst(loc)
283 1.1 cgd db_addr_t loc;
284 1.1 cgd {
285 1.18 jhawk db_printsym(loc, DB_STGY_PROC, db_printf);
286 1.1 cgd db_printf(":\t");
287 1.8 gwr (void) db_disasm(loc, FALSE);
288 1.1 cgd }
289 1.1 cgd
290 1.10 christos void
291 1.1 cgd db_strcpy(dst, src)
292 1.17 augustss char *dst;
293 1.17 augustss char *src;
294 1.1 cgd {
295 1.10 christos while ((*dst++ = *src++) != '\0')
296 1.7 mycroft ;
297 1.1 cgd }
298 1.1 cgd
299 1.1 cgd /*
300 1.1 cgd * Search for a value in memory.
301 1.1 cgd * Syntax: search [/bhl] addr value [mask] [,count]
302 1.1 cgd */
303 1.10 christos /*ARGSUSED*/
304 1.1 cgd void
305 1.10 christos db_search_cmd(daddr, have_addr, dcount, modif)
306 1.10 christos db_expr_t daddr;
307 1.10 christos int have_addr;
308 1.10 christos db_expr_t dcount;
309 1.10 christos char * modif;
310 1.1 cgd {
311 1.1 cgd int t;
312 1.1 cgd db_addr_t addr;
313 1.1 cgd int size;
314 1.1 cgd db_expr_t value;
315 1.1 cgd db_expr_t mask;
316 1.12 cgd db_expr_t count;
317 1.1 cgd
318 1.1 cgd t = db_read_token();
319 1.1 cgd if (t == tSLASH) {
320 1.7 mycroft t = db_read_token();
321 1.7 mycroft if (t != tIDENT) {
322 1.7 mycroft bad_modifier:
323 1.7 mycroft db_printf("Bad modifier\n");
324 1.7 mycroft db_flush_lex();
325 1.7 mycroft return;
326 1.7 mycroft }
327 1.7 mycroft
328 1.7 mycroft if (!strcmp(db_tok_string, "b"))
329 1.7 mycroft size = 1;
330 1.7 mycroft else if (!strcmp(db_tok_string, "h"))
331 1.7 mycroft size = 2;
332 1.7 mycroft else if (!strcmp(db_tok_string, "l"))
333 1.7 mycroft size = 4;
334 1.7 mycroft else
335 1.7 mycroft goto bad_modifier;
336 1.7 mycroft } else {
337 1.7 mycroft db_unread_token(t);
338 1.1 cgd size = 4;
339 1.1 cgd }
340 1.1 cgd
341 1.10 christos if (!db_expression(&value)) {
342 1.7 mycroft db_printf("Address missing\n");
343 1.7 mycroft db_flush_lex();
344 1.7 mycroft return;
345 1.1 cgd }
346 1.10 christos addr = (db_addr_t) value;
347 1.1 cgd
348 1.1 cgd if (!db_expression(&value)) {
349 1.7 mycroft db_printf("Value missing\n");
350 1.7 mycroft db_flush_lex();
351 1.7 mycroft return;
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd if (!db_expression(&mask))
355 1.10 christos mask = (int) ~0;
356 1.1 cgd
357 1.1 cgd t = db_read_token();
358 1.1 cgd if (t == tCOMMA) {
359 1.7 mycroft if (!db_expression(&count)) {
360 1.7 mycroft db_printf("Count missing\n");
361 1.7 mycroft db_flush_lex();
362 1.7 mycroft return;
363 1.7 mycroft }
364 1.1 cgd } else {
365 1.7 mycroft db_unread_token(t);
366 1.7 mycroft count = -1; /* effectively forever */
367 1.1 cgd }
368 1.1 cgd db_skip_to_eol();
369 1.1 cgd
370 1.1 cgd db_search(addr, size, value, mask, count);
371 1.1 cgd }
372 1.1 cgd
373 1.10 christos void
374 1.1 cgd db_search(addr, size, value, mask, count)
375 1.1 cgd db_addr_t addr;
376 1.1 cgd int size;
377 1.1 cgd db_expr_t value;
378 1.1 cgd db_expr_t mask;
379 1.1 cgd unsigned int count;
380 1.1 cgd {
381 1.1 cgd while (count-- != 0) {
382 1.1 cgd db_prev = addr;
383 1.1 cgd if ((db_get_value(addr, size, FALSE) & mask) == value)
384 1.1 cgd break;
385 1.1 cgd addr += size;
386 1.1 cgd }
387 1.1 cgd db_next = addr;
388 1.1 cgd }
389