xref: /freebsd/sys/ddb/db_examine.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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.5 1994/08/13 03:49:17 wollman Exp $
27  */
28 
29 /*
30  *	Author: David B. Golub, Carnegie Mellon University
31  *	Date:	7/90
32  */
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.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 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 	int		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) {
84 	    fp = fmt;
85 	    size = 4;
86 	    width = 16;
87 	    while ((c = *fp++) != 0) {
88 		switch (c) {
89 		    case 'b':
90 			size = 1;
91 			width = 4;
92 			break;
93 		    case 'h':
94 			size = 2;
95 			width = 8;
96 			break;
97 		    case 'l':
98 			size = 4;
99 			width = 16;
100 			break;
101 		    case 'a':	/* address */
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 			    /* If we hit a new symbol, print it */
112 			    char *	name;
113 			    db_expr_t	off;
114 
115 			    db_find_sym_and_offset(addr, &name, &off);
116 			    if (off == 0)
117 				db_printf("%s:\t", name);
118 			    else
119 				db_printf("\t\t");
120 
121 			    db_prev = addr;
122 			}
123 
124 			switch (c) {
125 			    case 'r':	/* signed, current radix */
126 				value = db_get_value(addr, size, TRUE);
127 				addr += size;
128 				db_printf("%-*r", width, value);
129 				break;
130 			    case 'x':	/* unsigned hex */
131 				value = db_get_value(addr, size, FALSE);
132 				addr += size;
133 				db_printf("%-*x", width, value);
134 				break;
135 			    case 'z':	/* signed hex */
136 				value = db_get_value(addr, size, TRUE);
137 				addr += size;
138 				db_printf("%-*z", width, value);
139 				break;
140 			    case 'd':	/* signed decimal */
141 				value = db_get_value(addr, size, TRUE);
142 				addr += size;
143 				db_printf("%-*d", width, value);
144 				break;
145 			    case 'u':	/* unsigned decimal */
146 				value = db_get_value(addr, size, FALSE);
147 				addr += size;
148 				db_printf("%-*u", width, value);
149 				break;
150 			    case 'o':	/* unsigned octal */
151 				value = db_get_value(addr, size, FALSE);
152 				addr += size;
153 				db_printf("%-*o", width, value);
154 				break;
155 			    case 'c':	/* character */
156 				value = db_get_value(addr, 1, FALSE);
157 				addr += 1;
158 				if (value >= ' ' && value <= '~')
159 				    db_printf("%c", value);
160 				else
161 				    db_printf("\\%03o", value);
162 				break;
163 			    case 's':	/* null-terminated string */
164 				for (;;) {
165 				    value = db_get_value(addr, 1, FALSE);
166 				    addr += 1;
167 				    if (value == 0)
168 					break;
169 				    if (value >= ' ' && value <= '~')
170 					db_printf("%c", value);
171 				    else
172 					db_printf("\\%03o", value);
173 				}
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();
186 			break;
187 		}
188 	    }
189 	}
190 	db_next = addr;
191 }
192 
193 /*
194  * Print value.
195  */
196 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 	int		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("%11r", addr);
217 		break;
218 	    case 'x':
219 		db_printf("%8x", addr);
220 		break;
221 	    case 'z':
222 		db_printf("%8z", addr);
223 		break;
224 	    case 'd':
225 		db_printf("%11d", addr);
226 		break;
227 	    case 'u':
228 		db_printf("%11u", addr);
229 		break;
230 	    case 'o':
231 		db_printf("%16o", addr);
232 		break;
233 	    case 'c':
234 		value = addr & 0xFF;
235 		if (value >= ' ' && value <= '~')
236 		    db_printf("%c", value);
237 		else
238 		    db_printf("\\%03o", 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(db_expr_t dummy1, int dummy2, db_expr_t dummy3, char *dummy4)
259 {
260 	int		t;
261 	db_addr_t	addr;
262 	int		size;
263 	db_expr_t	value;
264 	db_expr_t	mask;
265 	unsigned int	count;
266 
267 	t = db_read_token();
268 	if (t == tSLASH) {
269 	    t = db_read_token();
270 	    if (t != tIDENT) {
271 	      bad_modifier:
272 		db_printf("Bad modifier\n");
273 		db_flush_lex();
274 		return;
275 	    }
276 
277 	    if (!strcmp(db_tok_string, "b"))
278 		size = 1;
279 	    else if (!strcmp(db_tok_string, "h"))
280 		size = 2;
281 	    else if (!strcmp(db_tok_string, "l"))
282 		size = 4;
283 	    else
284 		goto bad_modifier;
285 	} else {
286 	    db_unread_token(t);
287 	    size = 4;
288 	}
289 
290 	if (!db_expression((db_expr_t *)&addr)) {
291 	    db_printf("Address missing\n");
292 	    db_flush_lex();
293 	    return;
294 	}
295 
296 	if (!db_expression(&value)) {
297 	    db_printf("Value missing\n");
298 	    db_flush_lex();
299 	    return;
300 	}
301 
302 	if (!db_expression(&mask))
303 	    mask = 0xffffffffUL;
304 
305 	t = db_read_token();
306 	if (t == tCOMMA) {
307 	    if (!db_expression(&count)) {
308 		db_printf("Count missing\n");
309 		db_flush_lex();
310 		return;
311 	    }
312 	} else {
313 	    db_unread_token(t);
314 	    count = -1;		/* effectively forever */
315 	}
316 	db_skip_to_eol();
317 
318 	db_search(addr, size, value, mask, count);
319 }
320 
321 static void
322 db_search(addr, size, value, mask, count)
323 	register
324 	db_addr_t	addr;
325 	int		size;
326 	db_expr_t	value;
327 	db_expr_t	mask;
328 	unsigned int	count;
329 {
330 	while (count-- != 0) {
331 		db_prev = addr;
332 		if ((db_get_value(addr, size, FALSE) & mask) == value)
333 			break;
334 		addr += size;
335 	}
336 	db_next = addr;
337 }
338