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