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