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