xref: /freebsd/sys/ddb/db_examine.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 /*
27  *	Author: David B. Golub, Carnegie Mellon University
28  *	Date:	7/90
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.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 
45 static char	db_examine_format[TOK_STRING_SIZE] = "x";
46 
47 static void	db_examine(db_addr_t, char *, int);
48 static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int);
49 
50 /*
51  * Examine (print) data.
52  */
53 /*ARGSUSED*/
54 void
55 db_examine_cmd(addr, have_addr, count, modif)
56 	db_expr_t	addr;
57 	boolean_t	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 
70 static void
71 db_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 && !db_pager_quit) {
84 	    fp = fmt;
85 	    size = 4;
86 	    while ((c = *fp++) != 0) {
87 		switch (c) {
88 		    case 'b':
89 			size = 1;
90 			break;
91 		    case 'h':
92 			size = 2;
93 			break;
94 		    case 'l':
95 			size = 4;
96 			break;
97 		    case 'g':
98 			size = 8;
99 			break;
100 		    case 'a':	/* address */
101 			size = sizeof(void *);
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 			width = size * 4;
118 			switch (c) {
119 			    case 'r':	/* signed, current radix */
120 				value = db_get_value(addr, size, TRUE);
121 				addr += size;
122 				db_printf("%+-*lr", width, (long)value);
123 				break;
124 			    case 'x':	/* unsigned hex */
125 				value = db_get_value(addr, size, FALSE);
126 				addr += size;
127 				db_printf("%-*lx", width, (long)value);
128 				break;
129 			    case 'z':	/* signed hex */
130 				value = db_get_value(addr, size, TRUE);
131 				addr += size;
132 				db_printf("%-*ly", width, (long)value);
133 				break;
134 			    case 'd':	/* signed decimal */
135 				value = db_get_value(addr, size, TRUE);
136 				addr += size;
137 				db_printf("%-*ld", width, (long)value);
138 				break;
139 			    case 'u':	/* unsigned decimal */
140 				value = db_get_value(addr, size, FALSE);
141 				addr += size;
142 				db_printf("%-*lu", width, (long)value);
143 				break;
144 			    case 'o':	/* unsigned octal */
145 				value = db_get_value(addr, size, FALSE);
146 				addr += size;
147 				db_printf("%-*lo", width, (long)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", (int)value);
154 				else
155 				    db_printf("\\%03o", (int)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", (int)value);
165 				    else
166 					db_printf("\\%03o", (int)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(1);
180 			break;
181 		}
182 	    }
183 	}
184 	db_next = addr;
185 }
186 
187 /*
188  * Print value.
189  */
190 static char	db_print_format = 'x';
191 
192 /*ARGSUSED*/
193 void
194 db_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("%+11lr", (long)addr);
211 		break;
212 	    case 'x':
213 		db_printf("%8lx", (unsigned long)addr);
214 		break;
215 	    case 'z':
216 		db_printf("%8ly", (long)addr);
217 		break;
218 	    case 'd':
219 		db_printf("%11ld", (long)addr);
220 		break;
221 	    case 'u':
222 		db_printf("%11lu", (unsigned long)addr);
223 		break;
224 	    case 'o':
225 		db_printf("%16lo", (unsigned long)addr);
226 		break;
227 	    case 'c':
228 		value = addr & 0xFF;
229 		if (value >= ' ' && value <= '~')
230 		    db_printf("%c", (int)value);
231 		else
232 		    db_printf("\\%03o", (int)value);
233 		break;
234 	}
235 	db_printf("\n");
236 }
237 
238 void
239 db_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  */
251 void
252 db_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 	db_expr_t	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 
319 static void
320 db_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