xref: /freebsd/sys/ddb/db_examine.c (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
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(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
56 {
57 	if (modif[0] != '\0')
58 	    db_strcpy(db_examine_format, modif);
59 
60 	if (count == -1)
61 	    count = 1;
62 
63 	db_examine((db_addr_t) addr, db_examine_format, count);
64 }
65 
66 static void
67 db_examine(db_addr_t addr, char *fmt, int count)
68 {
69 	int		c;
70 	db_expr_t	value;
71 	int		size;
72 	int		width;
73 	char *		fp;
74 
75 	while (--count >= 0 && !db_pager_quit) {
76 	    fp = fmt;
77 	    size = 4;
78 	    while ((c = *fp++) != 0) {
79 		switch (c) {
80 		    case 'b':
81 			size = 1;
82 			break;
83 		    case 'h':
84 			size = 2;
85 			break;
86 		    case 'l':
87 			size = 4;
88 			break;
89 		    case 'g':
90 			size = 8;
91 			break;
92 		    case 'a':	/* address */
93 			size = sizeof(void *);
94 			/* always forces a new line */
95 			if (db_print_position() != 0)
96 			    db_printf("\n");
97 			db_prev = addr;
98 			db_printsym(addr, DB_STGY_ANY);
99 			db_printf(":\t");
100 			break;
101 		    default:
102 			if (db_print_position() == 0) {
103 			    /* Print the address. */
104 			    db_printsym(addr, DB_STGY_ANY);
105 			    db_printf(":\t");
106 			    db_prev = addr;
107 			}
108 
109 			width = size * 4;
110 			switch (c) {
111 			    case 'r':	/* signed, current radix */
112 				value = db_get_value(addr, size, true);
113 				addr += size;
114 				db_printf("%+-*lr", width, (long)value);
115 				break;
116 			    case 'x':	/* unsigned hex */
117 				value = db_get_value(addr, size, false);
118 				addr += size;
119 				db_printf("%-*lx", width, (long)value);
120 				break;
121 			    case 'z':	/* signed hex */
122 				value = db_get_value(addr, size, true);
123 				addr += size;
124 				db_printf("%-*ly", width, (long)value);
125 				break;
126 			    case 'd':	/* signed decimal */
127 				value = db_get_value(addr, size, true);
128 				addr += size;
129 				db_printf("%-*ld", width, (long)value);
130 				break;
131 			    case 'u':	/* unsigned decimal */
132 				value = db_get_value(addr, size, false);
133 				addr += size;
134 				db_printf("%-*lu", width, (long)value);
135 				break;
136 			    case 'o':	/* unsigned octal */
137 				value = db_get_value(addr, size, false);
138 				addr += size;
139 				db_printf("%-*lo", width, (long)value);
140 				break;
141 			    case 'c':	/* character */
142 				value = db_get_value(addr, 1, false);
143 				addr += 1;
144 				if (value >= ' ' && value <= '~')
145 				    db_printf("%c", (int)value);
146 				else
147 				    db_printf("\\%03o", (int)value);
148 				break;
149 			    case 's':	/* null-terminated string */
150 				for (;;) {
151 				    value = db_get_value(addr, 1, false);
152 				    addr += 1;
153 				    if (value == 0)
154 					break;
155 				    if (value >= ' ' && value <= '~')
156 					db_printf("%c", (int)value);
157 				    else
158 					db_printf("\\%03o", (int)value);
159 				}
160 				break;
161 			    case 'S':	/* symbol */
162 				value = db_get_value(addr, sizeof(void *),
163 				    false);
164 				addr += sizeof(void *);
165 				db_printsym(value, DB_STGY_ANY);
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(1);
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(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
193 {
194 	db_expr_t	value;
195 
196 	if (modif[0] != '\0')
197 	    db_print_format = modif[0];
198 
199 	switch (db_print_format) {
200 	    case 'a':
201 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
202 		break;
203 	    case 'r':
204 		db_printf("%+11lr", (long)addr);
205 		break;
206 	    case 'x':
207 		db_printf("%8lx", (unsigned long)addr);
208 		break;
209 	    case 'z':
210 		db_printf("%8ly", (long)addr);
211 		break;
212 	    case 'd':
213 		db_printf("%11ld", (long)addr);
214 		break;
215 	    case 'u':
216 		db_printf("%11lu", (unsigned long)addr);
217 		break;
218 	    case 'o':
219 		db_printf("%16lo", (unsigned long)addr);
220 		break;
221 	    case 'c':
222 		value = addr & 0xFF;
223 		if (value >= ' ' && value <= '~')
224 		    db_printf("%c", (int)value);
225 		else
226 		    db_printf("\\%03o", (int)value);
227 		break;
228 	    default:
229 		db_print_format = 'x';
230 		db_error("Syntax error: unsupported print modifier\n");
231 		/*NOTREACHED*/
232 	}
233 	db_printf("\n");
234 }
235 
236 void
237 db_print_loc_and_inst(db_addr_t loc)
238 {
239 	db_expr_t off;
240 
241 	db_printsym(loc, DB_STGY_PROC);
242 	if (db_search_symbol(loc, DB_STGY_PROC, &off) != C_DB_SYM_NULL) {
243 		db_printf(":\t");
244 		(void)db_disasm(loc, false);
245 	}
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(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
254 {
255 	int		t;
256 	db_addr_t	addr;
257 	int		size;
258 	db_expr_t	value;
259 	db_expr_t	mask;
260 	db_expr_t	count;
261 
262 	t = db_read_token();
263 	if (t == tSLASH) {
264 	    t = db_read_token();
265 	    if (t != tIDENT) {
266 	      bad_modifier:
267 		db_printf("Bad modifier\n");
268 		db_flush_lex();
269 		return;
270 	    }
271 
272 	    if (!strcmp(db_tok_string, "b"))
273 		size = 1;
274 	    else if (!strcmp(db_tok_string, "h"))
275 		size = 2;
276 	    else if (!strcmp(db_tok_string, "l"))
277 		size = 4;
278 	    else
279 		goto bad_modifier;
280 	} else {
281 	    db_unread_token(t);
282 	    size = 4;
283 	}
284 
285 	if (!db_expression((db_expr_t *)&addr)) {
286 	    db_printf("Address missing\n");
287 	    db_flush_lex();
288 	    return;
289 	}
290 
291 	if (!db_expression(&value)) {
292 	    db_printf("Value missing\n");
293 	    db_flush_lex();
294 	    return;
295 	}
296 
297 	if (!db_expression(&mask))
298 	    mask = 0xffffffffUL;
299 
300 	t = db_read_token();
301 	if (t == tCOMMA) {
302 	    if (!db_expression(&count)) {
303 		db_printf("Count missing\n");
304 		db_flush_lex();
305 		return;
306 	    }
307 	} else {
308 	    db_unread_token(t);
309 	    count = -1;		/* effectively forever */
310 	}
311 	db_skip_to_eol();
312 
313 	db_search(addr, size, value, mask, count);
314 }
315 
316 static void
317 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
318     unsigned int count)
319 {
320 	while (count-- != 0) {
321 		db_prev = addr;
322 		if ((db_get_value(addr, size, false) & mask) == value)
323 			break;
324 		addr += size;
325 	}
326 	db_next = addr;
327 }
328