xref: /openbsd-src/sys/ddb/db_examine.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: db_examine.c,v 1.16 2007/11/05 19:23:24 miod Exp $	*/
2 /*	$NetBSD: db_examine.c,v 1.11 1996/03/30 22:30:07 christos Exp $	*/
3 
4 /*
5  * Mach Operating System
6  * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
7  * All Rights Reserved.
8  *
9  * Permission to use, copy, modify and distribute this software and its
10  * documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie Mellon
27  * the rights to redistribute these changes.
28  *
29  *	Author: David B. Golub, Carnegie Mellon University
30  *	Date:	7/90
31  */
32 
33 #include <sys/param.h>
34 #include <sys/proc.h>
35 
36 #include <uvm/uvm_extern.h>
37 
38 #include <machine/db_machdep.h>		/* type definitions */
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 #include <ddb/db_extern.h>
46 #include <ddb/db_interface.h>
47 
48 char	db_examine_format[TOK_STRING_SIZE] = "x";
49 
50 /*
51  * Examine (print) data.  Syntax is:
52  *		x/[bhlq][cdiorsuxz]*
53  * For example, the command:
54  *  	x/bxxxx
55  * should print:
56  *  	address:  01  23  45  67
57  */
58 /*ARGSUSED*/
59 void
60 db_examine_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
61 {
62 	if (modif[0] != '\0')
63 		db_strlcpy(db_examine_format, modif, sizeof(db_examine_format));
64 
65 	if (count == -1)
66 		count = 1;
67 
68 	db_examine((db_addr_t)addr, db_examine_format, count);
69 }
70 
71 void
72 db_examine(db_addr_t addr, char *fmt, int count)
73 {
74 	int		c;
75 	db_expr_t	value;
76 	int		size;
77 	int		width;
78 	char *		fp;
79 	char		tmpfmt[28];
80 
81 	while (--count >= 0) {
82 		fp = fmt;
83 		size = 4;
84 		width = 12;
85 		while ((c = *fp++) != 0) {
86 			if (db_print_position() == 0) {
87 				/* Always print the address. */
88 				db_printsym(addr, DB_STGY_ANY, db_printf);
89 				db_printf(":\t");
90 				db_prev = addr;
91 			}
92 			switch (c) {
93 			case 'b':	/* byte */
94 				size = 1;
95 				width = 4;
96 				break;
97 			case 'h':	/* half-word */
98 				size = 2;
99 				width = 8;
100 				break;
101 			case 'l':	/* long-word */
102 				size = 4;
103 				width = 12;
104 				break;
105 #ifdef __LP64__
106 			case 'q':	/* quad-word */
107 				size = 8;
108 				width = 20;
109 				break;
110 #endif
111 			case 'a':	/* address */
112 				db_printf("= 0x%lx\n", (long)addr);
113 				break;
114 			case 'r':	/* signed, current radix */
115 				value = db_get_value(addr, size, TRUE);
116 				addr += size;
117 				db_format(tmpfmt, sizeof tmpfmt,
118 				    (long)value, DB_FORMAT_R, 0, width);
119 				db_printf("%-*s", width, tmpfmt);
120 				break;
121 			case 'x':	/* unsigned hex */
122 				value = db_get_value(addr, size, FALSE);
123 				addr += size;
124 				db_printf("%-*lx", width, (long)value);
125 				break;
126 			case 'z':	/* signed hex */
127 				value = db_get_value(addr, size, TRUE);
128 				addr += size;
129 				db_format(tmpfmt, sizeof tmpfmt,
130 				    (long)value, DB_FORMAT_Z, 0, width);
131 				db_printf("%-*s", width, tmpfmt);
132 				break;
133 			case 'd':	/* signed decimal */
134 				value = db_get_value(addr, size, TRUE);
135 				addr += size;
136 				db_printf("%-*ld", width, (long)value);
137 				break;
138 			case 'u':	/* unsigned decimal */
139 				value = db_get_value(addr, size, FALSE);
140 				addr += size;
141 				db_printf("%-*lu", width, (long)value);
142 				break;
143 			case 'o':	/* unsigned octal */
144 				value = db_get_value(addr, size, FALSE);
145 				addr += size;
146 				db_printf("%-*lo", width, value);
147 				break;
148 			case 'c':	/* character */
149 				value = db_get_value(addr, 1, FALSE);
150 				addr += 1;
151 				if (value >= ' ' && value <= '~')
152 					db_printf("%c", value);
153 				else
154 					db_printf("\\%03o", value);
155 				break;
156 			case 's':	/* null-terminated string */
157 				for (;;) {
158 					value = db_get_value(addr, 1, FALSE);
159 					addr += 1;
160 					if (value == 0)
161 						break;
162 					if (value >= ' ' && value <= '~')
163 						db_printf("%c", value);
164 					else
165 						db_printf("\\%03o", value);
166 				}
167 				break;
168 			case 'i':	/* instruction */
169 				addr = db_disasm(addr, FALSE);
170 				break;
171 			case 'I':	/* instruction, alternate form */
172 				addr = db_disasm(addr, TRUE);
173 				break;
174 			default:
175 				break;
176 			}
177 			if (db_print_position() != 0)
178 				db_end_line(width);
179 		}
180 	}
181 	db_next = addr;
182 }
183 
184 /*
185  * Print value.
186  */
187 char	db_print_format = 'x';
188 
189 /*ARGSUSED*/
190 void
191 db_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
192 {
193 	db_expr_t	value;
194 	char		tmpfmt[28];
195 
196 	if (modif[0] != '\0')
197 		db_print_format = modif[0];
198 
199 	switch (db_print_format) {
200 	case 'a':
201 		db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
202 		break;
203 	case 'r':
204 		db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
205 		    DB_FORMAT_R, 0, sizeof(db_expr_t) * 2 * 6 / 5));
206 		break;
207 	case 'x':
208 		db_printf("%*x", sizeof(db_expr_t) * 2, addr);
209 		break;
210 	case 'z':
211 		db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
212 		    DB_FORMAT_Z, 0, sizeof(db_expr_t) * 2));
213 		break;
214 	case 'd':
215 		db_printf("%*d", sizeof(db_expr_t) * 2 * 6 / 5, addr);
216 		break;
217 	case 'u':
218 		db_printf("%*u", sizeof(db_expr_t) * 2 * 6 / 5, addr);
219 		break;
220 	case 'o':
221 		db_printf("%*o", sizeof(db_expr_t) * 2 * 4 / 3, addr);
222 		break;
223 	case 'c':
224 		value = addr & 0xFF;
225 		if (value >= ' ' && value <= '~')
226 			db_printf("%c", value);
227 		else
228 			db_printf("\\%03o", value);
229 		break;
230 	}
231 	db_printf("\n");
232 }
233 
234 void
235 db_print_loc_and_inst(db_addr_t loc)
236 {
237 	db_printsym(loc, DB_STGY_PROC, db_printf);
238 	db_printf(":\t");
239 	(void) db_disasm(loc, FALSE);
240 }
241 
242 /* local copy is needed here so that we can trace strlcpy() in libkern */
243 size_t
244 db_strlcpy(char *dst, const char *src, size_t siz)
245 {
246 	char *d = dst;
247 	const char *s = src;
248 	size_t n = siz;
249 
250 	/* Copy as many bytes as will fit */
251 	if (n != 0 && --n != 0) {
252 		do {
253 			if ((*d++ = *s++) == 0)
254 				break;
255 		} while (--n != 0);
256 	}
257 
258 	/* Not enough room in dst, add NUL and traverse rest of src */
259 	if (n == 0) {
260 		if (siz != 0)
261 			*d = '\0';		/* NUL-terminate dst */
262 		while (*s++)
263 			;
264 	}
265 
266 	return(s - src - 1);	/* count does not include NUL */
267 }
268 
269 /*
270  * Search for a value in memory.
271  * Syntax: search [/bhl] addr value [mask] [,count]
272  */
273 /*ARGSUSED*/
274 void
275 db_search_cmd(db_expr_t daddr, int have_addr, db_expr_t dcount, char *modif)
276 {
277 	int		t;
278 	db_addr_t	addr;
279 	int		size;
280 	db_expr_t	value;
281 	db_expr_t	mask;
282 	db_expr_t	count;
283 
284 	t = db_read_token();
285 	if (t == tSLASH) {
286 		t = db_read_token();
287 		if (t != tIDENT) {
288 			bad_modifier:
289 			db_printf("Bad modifier\n");
290 			db_flush_lex();
291 			return;
292 		}
293 
294 		if (!strcmp(db_tok_string, "b"))
295 			size = 1;
296 		else if (!strcmp(db_tok_string, "h"))
297 			size = 2;
298 		else if (!strcmp(db_tok_string, "l"))
299 			size = 4;
300 		else
301 			goto bad_modifier;
302 	} else {
303 		db_unread_token(t);
304 		size = 4;
305 	}
306 
307 	if (!db_expression(&value)) {
308 		db_printf("Address missing\n");
309 		db_flush_lex();
310 		return;
311 	}
312 	addr = (db_addr_t) value;
313 
314 	if (!db_expression(&value)) {
315 		db_printf("Value missing\n");
316 		db_flush_lex();
317 		return;
318 	}
319 
320 	if (!db_expression(&mask))
321 		mask = (int) ~0;
322 
323 	t = db_read_token();
324 	if (t == tCOMMA) {
325 		if (!db_expression(&count)) {
326 			db_printf("Count missing\n");
327 			db_flush_lex();
328 			return;
329 		}
330 	} else {
331 		db_unread_token(t);
332 		count = -1;		/* forever */
333 	}
334 	db_skip_to_eol();
335 
336 	db_search(addr, size, value, mask, count);
337 }
338 
339 void
340 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
341     db_expr_t count)
342 {
343 	/* Negative counts means forever.  */
344 	while (count < 0 || count-- != 0) {
345 		db_prev = addr;
346 		if ((db_get_value(addr, size, FALSE) & mask) == value)
347 			break;
348 		addr += size;
349 	}
350 	db_next = addr;
351 }
352