xref: /dragonfly/sys/ddb/db_examine.c (revision 9dbf638f)
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  * $FreeBSD: src/sys/ddb/db_examine.c,v 1.27 1999/08/28 00:41:07 peter Exp $
27  * $DragonFly: src/sys/ddb/db_examine.c,v 1.4 2003/08/27 10:47:13 rob Exp $
28  */
29 
30 /*
31  *	Author: David B. Golub, Carnegie Mellon University
32  *	Date:	7/90
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 
38 #include <ddb/ddb.h>
39 
40 #include <ddb/db_lex.h>
41 #include <ddb/db_output.h>
42 #include <ddb/db_command.h>
43 #include <ddb/db_sym.h>
44 #include <ddb/db_access.h>
45 
46 static char	db_examine_format[TOK_STRING_SIZE] = "x";
47 
48 static void	db_examine (db_addr_t, char *, int);
49 static void	db_search (db_addr_t, int, db_expr_t, db_expr_t, u_int);
50 
51 /*
52  * Examine (print) data.
53  */
54 /*ARGSUSED*/
55 void
56 db_examine_cmd(addr, have_addr, count, modif)
57 	db_expr_t	addr;
58 	boolean_t	have_addr;
59 	db_expr_t	count;
60 	char *		modif;
61 {
62 	if (modif[0] != '\0')
63 	    db_strcpy(db_examine_format, modif);
64 
65 	if (count == -1)
66 	    count = 1;
67 
68 	db_examine((db_addr_t) addr, db_examine_format, count);
69 }
70 
71 static void
72 db_examine(addr, fmt, count)
73 
74 	db_addr_t	addr;
75 	char *		fmt;	/* format string */
76 	int		count;	/* repeat count */
77 {
78 	int		c;
79 	db_expr_t	value;
80 	int		size;
81 	int		width;
82 	char *		fp;
83 
84 	while (--count >= 0) {
85 	    fp = fmt;
86 	    size = 4;
87 	    width = 16;
88 	    while ((c = *fp++) != 0) {
89 		switch (c) {
90 		    case 'b':
91 			size = 1;
92 			width = 4;
93 			break;
94 		    case 'h':
95 			size = 2;
96 			width = 8;
97 			break;
98 		    case 'l':
99 			size = 4;
100 			width = 16;
101 			break;
102 		    case 'g':
103 			size = 8;
104 			width = 32;
105 			break;
106 		    case 'a':	/* address */
107 			/* always forces a new line */
108 			if (db_print_position() != 0)
109 			    db_printf("\n");
110 			db_prev = addr;
111 			db_printsym(addr, DB_STGY_ANY);
112 			db_printf(":\t");
113 			break;
114 		    default:
115 			if (db_print_position() == 0) {
116 			    /* Print the address. */
117 			    db_printsym(addr, DB_STGY_ANY);
118 			    db_printf(":\t");
119 			    db_prev = addr;
120 			}
121 
122 			switch (c) {
123 			    case 'r':	/* signed, current radix */
124 				value = db_get_value(addr, size, TRUE);
125 				addr += size;
126 				db_printf("%+-*lr", width, (long)value);
127 				break;
128 			    case 'x':	/* unsigned hex */
129 				value = db_get_value(addr, size, FALSE);
130 				addr += size;
131 				db_printf("%-*lx", width, (long)value);
132 				break;
133 			    case 'z':	/* signed hex */
134 				value = db_get_value(addr, size, TRUE);
135 				addr += size;
136 				db_printf("%-*lz", width, (long)value);
137 				break;
138 			    case 'd':	/* signed decimal */
139 				value = db_get_value(addr, size, TRUE);
140 				addr += size;
141 				db_printf("%-*ld", width, (long)value);
142 				break;
143 			    case 'u':	/* unsigned decimal */
144 				value = db_get_value(addr, size, FALSE);
145 				addr += size;
146 				db_printf("%-*lu", width, (long)value);
147 				break;
148 			    case 'o':	/* unsigned octal */
149 				value = db_get_value(addr, size, FALSE);
150 				addr += size;
151 				db_printf("%-*lo", width, (long)value);
152 				break;
153 			    case 'c':	/* character */
154 				value = db_get_value(addr, 1, FALSE);
155 				addr += 1;
156 				if (value >= ' ' && value <= '~')
157 				    db_printf("%c", (int)value);
158 				else
159 				    db_printf("\\%03o", (int)value);
160 				break;
161 			    case 's':	/* null-terminated string */
162 				for (;;) {
163 				    value = db_get_value(addr, 1, FALSE);
164 				    addr += 1;
165 				    if (value == 0)
166 					break;
167 				    if (value >= ' ' && value <= '~')
168 					db_printf("%c", (int)value);
169 				    else
170 					db_printf("\\%03o", (int)value);
171 				}
172 				break;
173 			    case 'i':	/* instruction */
174 				addr = db_disasm(addr, FALSE);
175 				break;
176 			    case 'I':	/* instruction, alternate form */
177 				addr = db_disasm(addr, TRUE);
178 				break;
179 			    default:
180 				break;
181 			}
182 			if (db_print_position() != 0)
183 			    db_end_line();
184 			break;
185 		}
186 	    }
187 	}
188 	db_next = addr;
189 }
190 
191 /*
192  * Print value.
193  */
194 static char	db_print_format = 'x';
195 
196 /*ARGSUSED*/
197 void
198 db_print_cmd(addr, have_addr, count, modif)
199 	db_expr_t	addr;
200 	boolean_t	have_addr;
201 	db_expr_t	count;
202 	char *		modif;
203 {
204 	db_expr_t	value;
205 
206 	if (modif[0] != '\0')
207 	    db_print_format = modif[0];
208 
209 	switch (db_print_format) {
210 	    case 'a':
211 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
212 		break;
213 	    case 'r':
214 		db_printf("%+11lr", (long)addr);
215 		break;
216 	    case 'x':
217 		db_printf("%8lx", (unsigned long)addr);
218 		break;
219 	    case 'z':
220 		db_printf("%8lz", (long)addr);
221 		break;
222 	    case 'd':
223 		db_printf("%11ld", (long)addr);
224 		break;
225 	    case 'u':
226 		db_printf("%11lu", (unsigned long)addr);
227 		break;
228 	    case 'o':
229 		db_printf("%16lo", (unsigned long)addr);
230 		break;
231 	    case 'c':
232 		value = addr & 0xFF;
233 		if (value >= ' ' && value <= '~')
234 		    db_printf("%c", (int)value);
235 		else
236 		    db_printf("\\%03o", (int)value);
237 		break;
238 	}
239 	db_printf("\n");
240 }
241 
242 void
243 db_print_loc_and_inst(loc)
244 	db_addr_t	loc;
245 {
246 	db_printsym(loc, DB_STGY_PROC);
247 	db_printf(":\t");
248 	(void) db_disasm(loc, TRUE);
249 }
250 
251 /*
252  * Search for a value in memory.
253  * Syntax: search [/bhl] addr value [mask] [,count]
254  */
255 void
256 db_search_cmd(dummy1, dummy2, dummy3, dummy4)
257 	db_expr_t	dummy1;
258 	boolean_t	dummy2;
259 	db_expr_t	dummy3;
260 	char *		dummy4;
261 {
262 	int		t;
263 	db_addr_t	addr;
264 	int		size;
265 	db_expr_t	value;
266 	db_expr_t	mask;
267 	db_expr_t	count;
268 
269 	t = db_read_token();
270 	if (t == tSLASH) {
271 	    t = db_read_token();
272 	    if (t != tIDENT) {
273 	      bad_modifier:
274 		db_printf("Bad modifier\n");
275 		db_flush_lex();
276 		return;
277 	    }
278 
279 	    if (!strcmp(db_tok_string, "b"))
280 		size = 1;
281 	    else if (!strcmp(db_tok_string, "h"))
282 		size = 2;
283 	    else if (!strcmp(db_tok_string, "l"))
284 		size = 4;
285 	    else
286 		goto bad_modifier;
287 	} else {
288 	    db_unread_token(t);
289 	    size = 4;
290 	}
291 
292 	if (!db_expression((db_expr_t *)&addr)) {
293 	    db_printf("Address missing\n");
294 	    db_flush_lex();
295 	    return;
296 	}
297 
298 	if (!db_expression(&value)) {
299 	    db_printf("Value missing\n");
300 	    db_flush_lex();
301 	    return;
302 	}
303 
304 	if (!db_expression(&mask))
305 	    mask = 0xffffffffUL;
306 
307 	t = db_read_token();
308 	if (t == tCOMMA) {
309 	    if (!db_expression(&count)) {
310 		db_printf("Count missing\n");
311 		db_flush_lex();
312 		return;
313 	    }
314 	} else {
315 	    db_unread_token(t);
316 	    count = -1;		/* effectively forever */
317 	}
318 	db_skip_to_eol();
319 
320 	db_search(addr, size, value, mask, count);
321 }
322 
323 static void
324 db_search(addr, size, value, mask, count)
325 
326 	db_addr_t	addr;
327 	int		size;
328 	db_expr_t	value;
329 	db_expr_t	mask;
330 	unsigned int	count;
331 {
332 	while (count-- != 0) {
333 		db_prev = addr;
334 		if ((db_get_value(addr, size, FALSE) & mask) == value)
335 			break;
336 		addr += size;
337 	}
338 	db_next = addr;
339 }
340