xref: /freebsd/sys/ddb/db_examine.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
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 'S':	/* symbol */
170 				value = db_get_value(addr, sizeof(void *),
171 				    FALSE);
172 				addr += sizeof(void *);
173 				db_printsym(value, DB_STGY_ANY);
174 				break;
175 			    case 'i':	/* instruction */
176 				addr = db_disasm(addr, FALSE);
177 				break;
178 			    case 'I':	/* instruction, alternate form */
179 				addr = db_disasm(addr, TRUE);
180 				break;
181 			    default:
182 				break;
183 			}
184 			if (db_print_position() != 0)
185 			    db_end_line(1);
186 			break;
187 		}
188 	    }
189 	}
190 	db_next = addr;
191 }
192 
193 /*
194  * Print value.
195  */
196 static char	db_print_format = 'x';
197 
198 /*ARGSUSED*/
199 void
200 db_print_cmd(addr, have_addr, count, modif)
201 	db_expr_t	addr;
202 	boolean_t	have_addr;
203 	db_expr_t	count;
204 	char *		modif;
205 {
206 	db_expr_t	value;
207 
208 	if (modif[0] != '\0')
209 	    db_print_format = modif[0];
210 
211 	switch (db_print_format) {
212 	    case 'a':
213 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
214 		break;
215 	    case 'r':
216 		db_printf("%+11lr", (long)addr);
217 		break;
218 	    case 'x':
219 		db_printf("%8lx", (unsigned long)addr);
220 		break;
221 	    case 'z':
222 		db_printf("%8ly", (long)addr);
223 		break;
224 	    case 'd':
225 		db_printf("%11ld", (long)addr);
226 		break;
227 	    case 'u':
228 		db_printf("%11lu", (unsigned long)addr);
229 		break;
230 	    case 'o':
231 		db_printf("%16lo", (unsigned long)addr);
232 		break;
233 	    case 'c':
234 		value = addr & 0xFF;
235 		if (value >= ' ' && value <= '~')
236 		    db_printf("%c", (int)value);
237 		else
238 		    db_printf("\\%03o", (int)value);
239 		break;
240 	}
241 	db_printf("\n");
242 }
243 
244 void
245 db_print_loc_and_inst(loc)
246 	db_addr_t	loc;
247 {
248 	db_printsym(loc, DB_STGY_PROC);
249 	db_printf(":\t");
250 	(void) db_disasm(loc, TRUE);
251 }
252 
253 /*
254  * Search for a value in memory.
255  * Syntax: search [/bhl] addr value [mask] [,count]
256  */
257 void
258 db_search_cmd(dummy1, dummy2, dummy3, dummy4)
259 	db_expr_t	dummy1;
260 	boolean_t	dummy2;
261 	db_expr_t	dummy3;
262 	char *		dummy4;
263 {
264 	int		t;
265 	db_addr_t	addr;
266 	int		size;
267 	db_expr_t	value;
268 	db_expr_t	mask;
269 	db_expr_t	count;
270 
271 	t = db_read_token();
272 	if (t == tSLASH) {
273 	    t = db_read_token();
274 	    if (t != tIDENT) {
275 	      bad_modifier:
276 		db_printf("Bad modifier\n");
277 		db_flush_lex();
278 		return;
279 	    }
280 
281 	    if (!strcmp(db_tok_string, "b"))
282 		size = 1;
283 	    else if (!strcmp(db_tok_string, "h"))
284 		size = 2;
285 	    else if (!strcmp(db_tok_string, "l"))
286 		size = 4;
287 	    else
288 		goto bad_modifier;
289 	} else {
290 	    db_unread_token(t);
291 	    size = 4;
292 	}
293 
294 	if (!db_expression((db_expr_t *)&addr)) {
295 	    db_printf("Address missing\n");
296 	    db_flush_lex();
297 	    return;
298 	}
299 
300 	if (!db_expression(&value)) {
301 	    db_printf("Value missing\n");
302 	    db_flush_lex();
303 	    return;
304 	}
305 
306 	if (!db_expression(&mask))
307 	    mask = 0xffffffffUL;
308 
309 	t = db_read_token();
310 	if (t == tCOMMA) {
311 	    if (!db_expression(&count)) {
312 		db_printf("Count missing\n");
313 		db_flush_lex();
314 		return;
315 	    }
316 	} else {
317 	    db_unread_token(t);
318 	    count = -1;		/* effectively forever */
319 	}
320 	db_skip_to_eol();
321 
322 	db_search(addr, size, value, mask, count);
323 }
324 
325 static void
326 db_search(addr, size, value, mask, count)
327 	register
328 	db_addr_t	addr;
329 	int		size;
330 	db_expr_t	value;
331 	db_expr_t	mask;
332 	unsigned int	count;
333 {
334 	while (count-- != 0) {
335 		db_prev = addr;
336 		if ((db_get_value(addr, size, FALSE) & mask) == value)
337 			break;
338 		addr += size;
339 	}
340 	db_next = addr;
341 }
342