xref: /openbsd-src/sys/ddb/db_examine.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: db_examine.c,v 1.18 2011/11/07 20:29:56 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 	db_addr_t	incr;
80 	int		dis;
81 	char		tmpfmt[28];
82 
83 	while (--count >= 0) {
84 		fp = fmt;
85 
86 		/* defaults */
87 		size = 4;
88 		width = 12;
89 		incr = 0;
90 		dis = 0;
91 
92 		while ((c = *fp++) != 0) {
93 			if (db_print_position() == 0) {
94 				/* Always print the address. */
95 				db_printsym(addr, DB_STGY_ANY, db_printf);
96 				db_printf(":\t");
97 				db_prev = addr;
98 			}
99 			incr = size;
100 			switch (c) {
101 			case 'b':	/* byte */
102 				size = 1;
103 				width = 4;
104 				break;
105 			case 'h':	/* half-word */
106 				size = 2;
107 				width = 8;
108 				break;
109 			case 'l':	/* long-word */
110 				size = 4;
111 				width = 12;
112 				break;
113 #ifdef __LP64__
114 			case 'q':	/* quad-word */
115 				size = 8;
116 				width = 20;
117 				break;
118 #endif
119 			case 'a':	/* address */
120 				db_printf("= 0x%lx\n", (long)addr);
121 				incr = 0;
122 				break;
123 			case 'r':	/* signed, current radix */
124 				value = db_get_value(addr, size, TRUE);
125 				db_format(tmpfmt, sizeof tmpfmt,
126 				    (long)value, DB_FORMAT_R, 0, width);
127 				db_printf("%-*s", width, tmpfmt);
128 				break;
129 			case 'x':	/* unsigned hex */
130 				value = db_get_value(addr, size, FALSE);
131 				db_printf("%-*lx", width, (long)value);
132 				break;
133 			case 'z':	/* signed hex */
134 				value = db_get_value(addr, size, TRUE);
135 				db_format(tmpfmt, sizeof tmpfmt,
136 				    (long)value, DB_FORMAT_Z, 0, width);
137 				db_printf("%-*s", width, tmpfmt);
138 				break;
139 			case 'd':	/* signed decimal */
140 				value = db_get_value(addr, size, TRUE);
141 				db_printf("%-*ld", width, (long)value);
142 				break;
143 			case 'u':	/* unsigned decimal */
144 				value = db_get_value(addr, size, FALSE);
145 				db_printf("%-*lu", width, (long)value);
146 				break;
147 			case 'o':	/* unsigned octal */
148 				value = db_get_value(addr, size, FALSE);
149 				db_printf("%-*lo", width, value);
150 				break;
151 			case 'c':	/* character */
152 				value = db_get_value(addr, 1, FALSE);
153 				incr = 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 				incr = 0;
161 				for (;;) {
162 					value = db_get_value(addr + incr, 1,
163 					    FALSE);
164 					incr++;
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 			case 'I':	/* instruction, alternate form */
175 				dis = c;
176 				break;
177 			default:
178 				incr = 0;
179 				break;
180 			}
181 		}
182 		/* if we had a disassembly modifier, do it last */
183 		switch (dis) {
184 		case 'i':	/* instruction */
185 			addr = db_disasm(addr, FALSE);
186 			break;
187 		case 'I':	/* instruction, alternate form */
188 			addr = db_disasm(addr, TRUE);
189 			break;
190 		default:
191 			addr += incr;
192 			break;
193 		}
194 		if (db_print_position() != 0)
195 			db_printf("\n");
196 	}
197 	db_next = addr;
198 }
199 
200 /*
201  * Print value.
202  */
203 char	db_print_format = 'x';
204 
205 /*ARGSUSED*/
206 void
207 db_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
208 {
209 	db_expr_t	value;
210 	char		tmpfmt[28];
211 
212 	if (modif[0] != '\0')
213 		db_print_format = modif[0];
214 
215 	switch (db_print_format) {
216 	case 'a':
217 		db_printsym((db_addr_t)addr, DB_STGY_ANY, db_printf);
218 		break;
219 	case 'r':
220 		db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
221 		    DB_FORMAT_R, 0, sizeof(db_expr_t) * 2 * 6 / 5));
222 		break;
223 	case 'x':
224 		db_printf("%*lx", (uint)sizeof(db_expr_t) * 2, addr);
225 		break;
226 	case 'z':
227 		db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
228 		    DB_FORMAT_Z, 0, sizeof(db_expr_t) * 2));
229 		break;
230 	case 'd':
231 		db_printf("%*ld", (uint)sizeof(db_expr_t) * 2 * 6 / 5, addr);
232 		break;
233 	case 'u':
234 		db_printf("%*lu", (uint)sizeof(db_expr_t) * 2 * 6 / 5, addr);
235 		break;
236 	case 'o':
237 		db_printf("%*lo", (uint)sizeof(db_expr_t) * 2 * 4 / 3, addr);
238 		break;
239 	case 'c':
240 		value = addr & 0xFF;
241 		if (value >= ' ' && value <= '~')
242 			db_printf("%c", (int)value);
243 		else
244 			db_printf("\\%03o", (int)value);
245 		break;
246 	}
247 	db_printf("\n");
248 }
249 
250 void
251 db_print_loc_and_inst(db_addr_t loc)
252 {
253 	db_printsym(loc, DB_STGY_PROC, db_printf);
254 	db_printf(":\t");
255 	(void) db_disasm(loc, FALSE);
256 }
257 
258 /* local copy is needed here so that we can trace strlcpy() in libkern */
259 size_t
260 db_strlcpy(char *dst, const char *src, size_t siz)
261 {
262 	char *d = dst;
263 	const char *s = src;
264 	size_t n = siz;
265 
266 	/* Copy as many bytes as will fit */
267 	if (n != 0 && --n != 0) {
268 		do {
269 			if ((*d++ = *s++) == 0)
270 				break;
271 		} while (--n != 0);
272 	}
273 
274 	/* Not enough room in dst, add NUL and traverse rest of src */
275 	if (n == 0) {
276 		if (siz != 0)
277 			*d = '\0';		/* NUL-terminate dst */
278 		while (*s++)
279 			;
280 	}
281 
282 	return(s - src - 1);	/* count does not include NUL */
283 }
284 
285 /*
286  * Search for a value in memory.
287  * Syntax: search [/bhl] addr value [mask] [,count]
288  */
289 /*ARGSUSED*/
290 void
291 db_search_cmd(db_expr_t daddr, int have_addr, db_expr_t dcount, char *modif)
292 {
293 	int		t;
294 	db_addr_t	addr;
295 	int		size;
296 	db_expr_t	value;
297 	db_expr_t	mask;
298 	db_expr_t	count;
299 
300 	t = db_read_token();
301 	if (t == tSLASH) {
302 		t = db_read_token();
303 		if (t != tIDENT) {
304 			bad_modifier:
305 			db_printf("Bad modifier\n");
306 			db_flush_lex();
307 			return;
308 		}
309 
310 		if (!strcmp(db_tok_string, "b"))
311 			size = 1;
312 		else if (!strcmp(db_tok_string, "h"))
313 			size = 2;
314 		else if (!strcmp(db_tok_string, "l"))
315 			size = 4;
316 		else
317 			goto bad_modifier;
318 	} else {
319 		db_unread_token(t);
320 		size = 4;
321 	}
322 
323 	if (!db_expression(&value)) {
324 		db_printf("Address missing\n");
325 		db_flush_lex();
326 		return;
327 	}
328 	addr = (db_addr_t) value;
329 
330 	if (!db_expression(&value)) {
331 		db_printf("Value missing\n");
332 		db_flush_lex();
333 		return;
334 	}
335 
336 	if (!db_expression(&mask))
337 		mask = (int) ~0;
338 
339 	t = db_read_token();
340 	if (t == tCOMMA) {
341 		if (!db_expression(&count)) {
342 			db_printf("Count missing\n");
343 			db_flush_lex();
344 			return;
345 		}
346 	} else {
347 		db_unread_token(t);
348 		count = -1;		/* forever */
349 	}
350 	db_skip_to_eol();
351 
352 	db_search(addr, size, value, mask, count);
353 }
354 
355 void
356 db_search(db_addr_t addr, int size, db_expr_t value, db_expr_t mask,
357     db_expr_t count)
358 {
359 	/* Negative counts means forever.  */
360 	while (count < 0 || count-- != 0) {
361 		db_prev = addr;
362 		if ((db_get_value(addr, size, FALSE) & mask) == value)
363 			break;
364 		addr += size;
365 	}
366 	db_next = addr;
367 }
368