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