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