xref: /netbsd-src/sys/ddb/db_examine.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
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  *	Author: David B. Golub, Carnegie Mellon University
27  *	Date:	7/90
28  *	$Id: db_examine.c,v 1.3 1993/12/18 04:46:31 mycroft Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/proc.h>
33 
34 #include <machine/db_machdep.h>		/* type definitions */
35 
36 #include <ddb/db_lex.h>
37 #include <ddb/db_output.h>
38 #include <ddb/db_command.h>
39 #include <ddb/db_sym.h>
40 
41 char	db_examine_format[TOK_STRING_SIZE] = "x";
42 
43 extern	db_addr_t db_disasm(/* db_addr_t, boolean_t */);
44 			/* instruction disassembler */
45 
46 /*
47  * Examine (print) data.
48  */
49 /*ARGSUSED*/
50 void
51 db_examine_cmd(addr, have_addr, count, modif)
52 	db_expr_t	addr;
53 	int		have_addr;
54 	db_expr_t	count;
55 	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 db_examine(addr, fmt, count)
67 	register
68 	db_addr_t	addr;
69 	char *		fmt;	/* format string */
70 	int		count;	/* repeat count */
71 {
72 	int		c;
73 	db_expr_t	value;
74 	int		size;
75 	int		width;
76 	char *		fp;
77 
78 	while (--count >= 0) {
79 	    fp = fmt;
80 	    size = 4;
81 	    width = 16;
82 	    while ((c = *fp++) != 0) {
83 		switch (c) {
84 		    case 'b':
85 			size = 1;
86 			width = 4;
87 			break;
88 		    case 'h':
89 			size = 2;
90 			width = 8;
91 			break;
92 		    case 'l':
93 			size = 4;
94 			width = 16;
95 			break;
96 		    case 'a':	/* address */
97 			/* always forces a new line */
98 			if (db_print_position() != 0)
99 			    db_printf("\n");
100 			db_prev = addr;
101 			db_printsym(addr, DB_STGY_ANY);
102 			db_printf(":\t");
103 			break;
104 		    default:
105 			if (db_print_position() == 0) {
106 			    /* If we hit a new symbol, print it */
107 			    char *	name;
108 			    db_expr_t	off;
109 
110 			    db_find_sym_and_offset(addr, &name, &off);
111 			    if (off == 0)
112 				db_printf("%s:\t", name);
113 			    else
114 				db_printf("\t\t");
115 
116 			    db_prev = addr;
117 			}
118 
119 			switch (c) {
120 			    case 'r':	/* signed, current radix */
121 				value = db_get_value(addr, size, TRUE);
122 				addr += size;
123 				db_printf("%-*r", width, value);
124 				break;
125 			    case 'x':	/* unsigned hex */
126 				value = db_get_value(addr, size, FALSE);
127 				addr += size;
128 				db_printf("%-*x", width, value);
129 				break;
130 			    case 'z':	/* signed hex */
131 				value = db_get_value(addr, size, TRUE);
132 				addr += size;
133 				db_printf("%-*z", width, value);
134 				break;
135 			    case 'd':	/* signed decimal */
136 				value = db_get_value(addr, size, TRUE);
137 				addr += size;
138 				db_printf("%-*d", width, value);
139 				break;
140 			    case 'u':	/* unsigned decimal */
141 				value = db_get_value(addr, size, FALSE);
142 				addr += size;
143 				db_printf("%-*u", width, value);
144 				break;
145 			    case 'o':	/* unsigned octal */
146 				value = db_get_value(addr, size, FALSE);
147 				addr += size;
148 				db_printf("%-*o", width, value);
149 				break;
150 			    case 'c':	/* character */
151 				value = db_get_value(addr, 1, FALSE);
152 				addr += 1;
153 				if (value >= ' ' && value <= '~')
154 				    db_printf("%c", value);
155 				else
156 				    db_printf("\\%03o", value);
157 				break;
158 			    case 's':	/* null-terminated string */
159 				for (;;) {
160 				    value = db_get_value(addr, 1, FALSE);
161 				    addr += 1;
162 				    if (value == 0)
163 					break;
164 				    if (value >= ' ' && value <= '~')
165 					db_printf("%c", value);
166 				    else
167 					db_printf("\\%03o", value);
168 				}
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)
180 			    db_end_line();
181 			break;
182 		}
183 	    }
184 	}
185 	db_next = addr;
186 }
187 
188 /*
189  * Print value.
190  */
191 char	db_print_format = 'x';
192 
193 /*ARGSUSED*/
194 void
195 db_print_cmd(addr, have_addr, count, modif)
196 	db_expr_t	addr;
197 	int		have_addr;
198 	db_expr_t	count;
199 	char *		modif;
200 {
201 	db_expr_t	value;
202 
203 	if (modif[0] != '\0')
204 	    db_print_format = modif[0];
205 
206 	switch (db_print_format) {
207 	    case 'a':
208 		db_printsym((db_addr_t)addr, DB_STGY_ANY);
209 		break;
210 	    case 'r':
211 		db_printf("%11r", addr);
212 		break;
213 	    case 'x':
214 		db_printf("%8x", addr);
215 		break;
216 	    case 'z':
217 		db_printf("%8z", addr);
218 		break;
219 	    case 'd':
220 		db_printf("%11d", addr);
221 		break;
222 	    case 'u':
223 		db_printf("%11u", addr);
224 		break;
225 	    case 'o':
226 		db_printf("%16o", addr);
227 		break;
228 	    case 'c':
229 		value = addr & 0xFF;
230 		if (value >= ' ' && value <= '~')
231 		    db_printf("%c", value);
232 		else
233 		    db_printf("\\%03o", value);
234 		break;
235 	}
236 	db_printf("\n");
237 }
238 
239 db_print_loc_and_inst(loc)
240 	db_addr_t	loc;
241 {
242 	db_printsym(loc, DB_STGY_PROC);
243 	db_printf(":\t");
244 	(void) db_disasm(loc, TRUE);
245 }
246 
247 db_strcpy(dst, src)
248 	register char *dst;
249 	register char *src;
250 {
251 	while (*dst++ = *src++)
252 	    ;
253 }
254 
255 /*
256  * Search for a value in memory.
257  * Syntax: search [/bhl] addr value [mask] [,count]
258  */
259 void
260 db_search_cmd()
261 {
262 	int		t;
263 	db_addr_t	addr;
264 	int		size;
265 	db_expr_t	value;
266 	db_expr_t	mask;
267 	unsigned int	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(&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 = 0xffffffff;
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 db_search(addr, size, value, mask, count)
324 	register
325 	db_addr_t	addr;
326 	int		size;
327 	db_expr_t	value;
328 	db_expr_t	mask;
329 	unsigned int	count;
330 {
331 	while (count-- != 0) {
332 		db_prev = addr;
333 		if ((db_get_value(addr, size, FALSE) & mask) == value)
334 			break;
335 		addr += size;
336 	}
337 	db_next = addr;
338 }
339