db_examine.c revision 8876
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.7 1995/05/22 13:07:11 davidg Exp $
27 */
28
29/*
30 *	Author: David B. Golub, Carnegie Mellon University
31 *	Date:	7/90
32 */
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/proc.h>
36
37#include <ddb/ddb.h>
38
39#include <ddb/db_lex.h>
40#include <ddb/db_output.h>
41#include <ddb/db_command.h>
42#include <ddb/db_sym.h>
43#include <ddb/db_access.h>
44
45char	db_examine_format[TOK_STRING_SIZE] = "x";
46
47static void db_examine(db_addr_t, char *, int);
48static void db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int);
49
50/*
51 * Examine (print) data.
52 */
53/*ARGSUSED*/
54void
55db_examine_cmd(addr, have_addr, count, modif)
56	db_expr_t	addr;
57	int		have_addr;
58	db_expr_t	count;
59	char *		modif;
60{
61	if (modif[0] != '\0')
62	    db_strcpy(db_examine_format, modif);
63
64	if (count == -1)
65	    count = 1;
66
67	db_examine((db_addr_t) addr, db_examine_format, count);
68}
69
70static void
71db_examine(addr, fmt, count)
72	register
73	db_addr_t	addr;
74	char *		fmt;	/* format string */
75	int		count;	/* repeat count */
76{
77	int		c;
78	db_expr_t	value;
79	int		size;
80	int		width;
81	char *		fp;
82
83	while (--count >= 0) {
84	    fp = fmt;
85	    size = 4;
86	    width = 16;
87	    while ((c = *fp++) != 0) {
88		switch (c) {
89		    case 'b':
90			size = 1;
91			width = 4;
92			break;
93		    case 'h':
94			size = 2;
95			width = 8;
96			break;
97		    case 'l':
98			size = 4;
99			width = 16;
100			break;
101		    case 'a':	/* address */
102			/* always forces a new line */
103			if (db_print_position() != 0)
104			    db_printf("\n");
105			db_prev = addr;
106			db_printsym(addr, DB_STGY_ANY);
107			db_printf(":\t");
108			break;
109		    default:
110			if (db_print_position() == 0) {
111			    /* Print the address. */
112			    db_printsym(addr, DB_STGY_ANY);
113			    db_printf(":\t");
114			    db_prev = addr;
115			}
116
117			switch (c) {
118			    case 'r':	/* signed, current radix */
119				value = db_get_value(addr, size, TRUE);
120				addr += size;
121				db_printf("%-*r", width, value);
122				break;
123			    case 'x':	/* unsigned hex */
124				value = db_get_value(addr, size, FALSE);
125				addr += size;
126				db_printf("%-*x", width, value);
127				break;
128			    case 'z':	/* signed hex */
129				value = db_get_value(addr, size, TRUE);
130				addr += size;
131				db_printf("%-*z", width, value);
132				break;
133			    case 'd':	/* signed decimal */
134				value = db_get_value(addr, size, TRUE);
135				addr += size;
136				db_printf("%-*d", width, value);
137				break;
138			    case 'u':	/* unsigned decimal */
139				value = db_get_value(addr, size, FALSE);
140				addr += size;
141				db_printf("%-*u", width, value);
142				break;
143			    case 'o':	/* unsigned octal */
144				value = db_get_value(addr, size, FALSE);
145				addr += size;
146				db_printf("%-*o", width, value);
147				break;
148			    case 'c':	/* character */
149				value = db_get_value(addr, 1, FALSE);
150				addr += 1;
151				if (value >= ' ' && value <= '~')
152				    db_printf("%c", value);
153				else
154				    db_printf("\\%03o", value);
155				break;
156			    case 's':	/* null-terminated string */
157				for (;;) {
158				    value = db_get_value(addr, 1, FALSE);
159				    addr += 1;
160				    if (value == 0)
161					break;
162				    if (value >= ' ' && value <= '~')
163					db_printf("%c", value);
164				    else
165					db_printf("\\%03o", value);
166				}
167				break;
168			    case 'i':	/* instruction */
169				addr = db_disasm(addr, FALSE);
170				break;
171			    case 'I':	/* instruction, alternate form */
172				addr = db_disasm(addr, TRUE);
173				break;
174			    default:
175				break;
176			}
177			if (db_print_position() != 0)
178			    db_end_line();
179			break;
180		}
181	    }
182	}
183	db_next = addr;
184}
185
186/*
187 * Print value.
188 */
189char	db_print_format = 'x';
190
191/*ARGSUSED*/
192void
193db_print_cmd(addr, have_addr, count, modif)
194	db_expr_t	addr;
195	int		have_addr;
196	db_expr_t	count;
197	char *		modif;
198{
199	db_expr_t	value;
200
201	if (modif[0] != '\0')
202	    db_print_format = modif[0];
203
204	switch (db_print_format) {
205	    case 'a':
206		db_printsym((db_addr_t)addr, DB_STGY_ANY);
207		break;
208	    case 'r':
209		db_printf("%11r", addr);
210		break;
211	    case 'x':
212		db_printf("%8x", addr);
213		break;
214	    case 'z':
215		db_printf("%8z", addr);
216		break;
217	    case 'd':
218		db_printf("%11d", addr);
219		break;
220	    case 'u':
221		db_printf("%11u", addr);
222		break;
223	    case 'o':
224		db_printf("%16o", addr);
225		break;
226	    case 'c':
227		value = addr & 0xFF;
228		if (value >= ' ' && value <= '~')
229		    db_printf("%c", value);
230		else
231		    db_printf("\\%03o", value);
232		break;
233	}
234	db_printf("\n");
235}
236
237void
238db_print_loc_and_inst(loc)
239	db_addr_t	loc;
240{
241	db_printsym(loc, DB_STGY_PROC);
242	db_printf(":\t");
243	(void) db_disasm(loc, TRUE);
244}
245
246/*
247 * Search for a value in memory.
248 * Syntax: search [/bhl] addr value [mask] [,count]
249 */
250void
251db_search_cmd(db_expr_t dummy1, int dummy2, db_expr_t dummy3, char *dummy4)
252{
253	int		t;
254	db_addr_t	addr;
255	int		size;
256	db_expr_t	value;
257	db_expr_t	mask;
258	unsigned int	count;
259
260	t = db_read_token();
261	if (t == tSLASH) {
262	    t = db_read_token();
263	    if (t != tIDENT) {
264	      bad_modifier:
265		db_printf("Bad modifier\n");
266		db_flush_lex();
267		return;
268	    }
269
270	    if (!strcmp(db_tok_string, "b"))
271		size = 1;
272	    else if (!strcmp(db_tok_string, "h"))
273		size = 2;
274	    else if (!strcmp(db_tok_string, "l"))
275		size = 4;
276	    else
277		goto bad_modifier;
278	} else {
279	    db_unread_token(t);
280	    size = 4;
281	}
282
283	if (!db_expression((db_expr_t *)&addr)) {
284	    db_printf("Address missing\n");
285	    db_flush_lex();
286	    return;
287	}
288
289	if (!db_expression(&value)) {
290	    db_printf("Value missing\n");
291	    db_flush_lex();
292	    return;
293	}
294
295	if (!db_expression(&mask))
296	    mask = 0xffffffffUL;
297
298	t = db_read_token();
299	if (t == tCOMMA) {
300	    if (!db_expression(&count)) {
301		db_printf("Count missing\n");
302		db_flush_lex();
303		return;
304	    }
305	} else {
306	    db_unread_token(t);
307	    count = -1;		/* effectively forever */
308	}
309	db_skip_to_eol();
310
311	db_search(addr, size, value, mask, count);
312}
313
314static void
315db_search(addr, size, value, mask, count)
316	register
317	db_addr_t	addr;
318	int		size;
319	db_expr_t	value;
320	db_expr_t	mask;
321	unsigned int	count;
322{
323	while (count-- != 0) {
324		db_prev = addr;
325		if ((db_get_value(addr, size, FALSE) & mask) == value)
326			break;
327		addr += size;
328	}
329	db_next = addr;
330}
331