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