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