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