Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Print values for GNU debugger GDB. |
e2ad119d | 2 | |
8926118c | 3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, |
95051d27 | 4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software |
8926118c | 5 | Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "gdb_string.h" | |
26 | #include "frame.h" | |
27 | #include "symtab.h" | |
28 | #include "gdbtypes.h" | |
29 | #include "value.h" | |
30 | #include "language.h" | |
31 | #include "expression.h" | |
32 | #include "gdbcore.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "target.h" | |
35 | #include "breakpoint.h" | |
36 | #include "demangle.h" | |
37 | #include "valprint.h" | |
38 | #include "annotate.h" | |
c5aa993b JM |
39 | #include "symfile.h" /* for overlay functions */ |
40 | #include "objfiles.h" /* ditto */ | |
c94fdfd0 | 41 | #include "completer.h" /* for completion functions */ |
8b93c638 | 42 | #include "ui-out.h" |
261397f8 | 43 | #include "gdb_assert.h" |
fe898f56 | 44 | #include "block.h" |
92bf2b80 | 45 | #include "disasm.h" |
c906108c | 46 | |
6a83354a AC |
47 | #ifdef TUI |
48 | #include "tui/tui.h" /* For tui_active et.al. */ | |
49 | #endif | |
50 | ||
c906108c SS |
51 | extern int asm_demangle; /* Whether to demangle syms in asm printouts */ |
52 | extern int addressprint; /* Whether to print hex addresses in HLL " */ | |
53 | ||
54 | struct format_data | |
c5aa993b JM |
55 | { |
56 | int count; | |
57 | char format; | |
58 | char size; | |
59 | }; | |
c906108c SS |
60 | |
61 | /* Last specified output format. */ | |
62 | ||
63 | static char last_format = 'x'; | |
64 | ||
65 | /* Last specified examination size. 'b', 'h', 'w' or `q'. */ | |
66 | ||
67 | static char last_size = 'w'; | |
68 | ||
69 | /* Default address to examine next. */ | |
70 | ||
71 | static CORE_ADDR next_address; | |
72 | ||
73 | /* Default section to examine next. */ | |
74 | ||
75 | static asection *next_section; | |
76 | ||
77 | /* Last address examined. */ | |
78 | ||
79 | static CORE_ADDR last_examine_address; | |
80 | ||
81 | /* Contents of last address examined. | |
82 | This is not valid past the end of the `x' command! */ | |
83 | ||
3d6d86c6 | 84 | static struct value *last_examine_value; |
c906108c SS |
85 | |
86 | /* Largest offset between a symbolic value and an address, that will be | |
87 | printed as `0x1234 <symbol+offset>'. */ | |
88 | ||
89 | static unsigned int max_symbolic_offset = UINT_MAX; | |
90 | ||
91 | /* Append the source filename and linenumber of the symbol when | |
92 | printing a symbolic value as `<symbol at filename:linenum>' if set. */ | |
93 | static int print_symbol_filename = 0; | |
94 | ||
95 | /* Number of auto-display expression currently being displayed. | |
96 | So that we can disable it if we get an error or a signal within it. | |
97 | -1 when not doing one. */ | |
98 | ||
99 | int current_display_number; | |
100 | ||
101 | /* Flag to low-level print routines that this value is being printed | |
102 | in an epoch window. We'd like to pass this as a parameter, but | |
103 | every routine would need to take it. Perhaps we can encapsulate | |
104 | this in the I/O stream once we have GNU stdio. */ | |
105 | ||
106 | int inspect_it = 0; | |
107 | ||
108 | struct display | |
c5aa993b JM |
109 | { |
110 | /* Chain link to next auto-display item. */ | |
111 | struct display *next; | |
112 | /* Expression to be evaluated and displayed. */ | |
113 | struct expression *exp; | |
114 | /* Item number of this auto-display item. */ | |
115 | int number; | |
116 | /* Display format specified. */ | |
117 | struct format_data format; | |
118 | /* Innermost block required by this expression when evaluated */ | |
119 | struct block *block; | |
120 | /* Status of this display (enabled or disabled) */ | |
b5de0fa7 | 121 | int enabled_p; |
c5aa993b | 122 | }; |
c906108c SS |
123 | |
124 | /* Chain of expressions whose values should be displayed | |
125 | automatically each time the program stops. */ | |
126 | ||
127 | static struct display *display_chain; | |
128 | ||
129 | static int display_number; | |
130 | ||
131 | /* Prototypes for exported functions. */ | |
132 | ||
a14ed312 | 133 | void output_command (char *, int); |
c906108c | 134 | |
a14ed312 | 135 | void _initialize_printcmd (void); |
c906108c SS |
136 | |
137 | /* Prototypes for local functions. */ | |
138 | ||
a14ed312 | 139 | static void delete_display (int); |
c906108c | 140 | |
a14ed312 | 141 | static void enable_display (char *, int); |
c906108c | 142 | |
a14ed312 | 143 | static void disable_display_command (char *, int); |
c906108c | 144 | |
a14ed312 | 145 | static void printf_command (char *, int); |
c906108c | 146 | |
a14ed312 | 147 | static void display_info (char *, int); |
c906108c | 148 | |
a14ed312 | 149 | static void do_one_display (struct display *); |
c906108c | 150 | |
a14ed312 | 151 | static void undisplay_command (char *, int); |
c906108c | 152 | |
a14ed312 | 153 | static void free_display (struct display *); |
c906108c | 154 | |
a14ed312 | 155 | static void display_command (char *, int); |
c906108c | 156 | |
a14ed312 | 157 | void x_command (char *, int); |
c906108c | 158 | |
a14ed312 | 159 | static void address_info (char *, int); |
c906108c | 160 | |
a14ed312 | 161 | static void set_command (char *, int); |
c906108c | 162 | |
a14ed312 | 163 | static void call_command (char *, int); |
c906108c | 164 | |
a14ed312 | 165 | static void inspect_command (char *, int); |
c906108c | 166 | |
a14ed312 | 167 | static void print_command (char *, int); |
c906108c | 168 | |
a14ed312 | 169 | static void print_command_1 (char *, int, int); |
c906108c | 170 | |
a14ed312 | 171 | static void validate_format (struct format_data, char *); |
c906108c | 172 | |
a14ed312 KB |
173 | static void do_examine (struct format_data, CORE_ADDR addr, |
174 | asection * section); | |
c906108c | 175 | |
3d6d86c6 | 176 | static void print_formatted (struct value *, int, int, struct ui_file *); |
c906108c | 177 | |
a14ed312 | 178 | static struct format_data decode_format (char **, int, int); |
c906108c | 179 | |
a14ed312 | 180 | static void sym_info (char *, int); |
c906108c | 181 | \f |
c5aa993b | 182 | |
c906108c SS |
183 | /* Decode a format specification. *STRING_PTR should point to it. |
184 | OFORMAT and OSIZE are used as defaults for the format and size | |
185 | if none are given in the format specification. | |
186 | If OSIZE is zero, then the size field of the returned value | |
187 | should be set only if a size is explicitly specified by the | |
188 | user. | |
189 | The structure returned describes all the data | |
190 | found in the specification. In addition, *STRING_PTR is advanced | |
191 | past the specification and past all whitespace following it. */ | |
192 | ||
193 | static struct format_data | |
fba45db2 | 194 | decode_format (char **string_ptr, int oformat, int osize) |
c906108c SS |
195 | { |
196 | struct format_data val; | |
52f0bd74 | 197 | char *p = *string_ptr; |
c906108c SS |
198 | |
199 | val.format = '?'; | |
200 | val.size = '?'; | |
201 | val.count = 1; | |
202 | ||
203 | if (*p >= '0' && *p <= '9') | |
204 | val.count = atoi (p); | |
c5aa993b JM |
205 | while (*p >= '0' && *p <= '9') |
206 | p++; | |
c906108c SS |
207 | |
208 | /* Now process size or format letters that follow. */ | |
209 | ||
210 | while (1) | |
211 | { | |
212 | if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g') | |
213 | val.size = *p++; | |
214 | else if (*p >= 'a' && *p <= 'z') | |
215 | val.format = *p++; | |
216 | else | |
217 | break; | |
218 | } | |
219 | ||
c5aa993b JM |
220 | while (*p == ' ' || *p == '\t') |
221 | p++; | |
c906108c SS |
222 | *string_ptr = p; |
223 | ||
224 | /* Set defaults for format and size if not specified. */ | |
225 | if (val.format == '?') | |
226 | { | |
227 | if (val.size == '?') | |
228 | { | |
229 | /* Neither has been specified. */ | |
230 | val.format = oformat; | |
231 | val.size = osize; | |
232 | } | |
233 | else | |
234 | /* If a size is specified, any format makes a reasonable | |
235 | default except 'i'. */ | |
236 | val.format = oformat == 'i' ? 'x' : oformat; | |
237 | } | |
238 | else if (val.size == '?') | |
239 | switch (val.format) | |
240 | { | |
241 | case 'a': | |
242 | case 's': | |
243 | /* Pick the appropriate size for an address. */ | |
244 | if (TARGET_PTR_BIT == 64) | |
245 | val.size = osize ? 'g' : osize; | |
246 | else if (TARGET_PTR_BIT == 32) | |
247 | val.size = osize ? 'w' : osize; | |
248 | else if (TARGET_PTR_BIT == 16) | |
249 | val.size = osize ? 'h' : osize; | |
250 | else | |
251 | /* Bad value for TARGET_PTR_BIT */ | |
e1e9e218 | 252 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
253 | break; |
254 | case 'f': | |
255 | /* Floating point has to be word or giantword. */ | |
256 | if (osize == 'w' || osize == 'g') | |
257 | val.size = osize; | |
258 | else | |
259 | /* Default it to giantword if the last used size is not | |
260 | appropriate. */ | |
261 | val.size = osize ? 'g' : osize; | |
262 | break; | |
263 | case 'c': | |
264 | /* Characters default to one byte. */ | |
265 | val.size = osize ? 'b' : osize; | |
266 | break; | |
267 | default: | |
268 | /* The default is the size most recently specified. */ | |
269 | val.size = osize; | |
270 | } | |
271 | ||
272 | return val; | |
273 | } | |
274 | \f | |
2acceee2 | 275 | /* Print value VAL on stream according to FORMAT, a letter or 0. |
c906108c SS |
276 | Do not end with a newline. |
277 | 0 means print VAL according to its own type. | |
278 | SIZE is the letter for the size of datum being printed. | |
279 | This is used to pad hex numbers so they line up. */ | |
280 | ||
281 | static void | |
aa1ee363 | 282 | print_formatted (struct value *val, int format, int size, |
fba45db2 | 283 | struct ui_file *stream) |
c906108c SS |
284 | { |
285 | struct type *type = check_typedef (VALUE_TYPE (val)); | |
286 | int len = TYPE_LENGTH (type); | |
287 | ||
288 | if (VALUE_LVAL (val) == lval_memory) | |
289 | { | |
290 | next_address = VALUE_ADDRESS (val) + len; | |
291 | next_section = VALUE_BFD_SECTION (val); | |
292 | } | |
293 | ||
294 | switch (format) | |
295 | { | |
296 | case 's': | |
297 | /* FIXME: Need to handle wchar_t's here... */ | |
298 | next_address = VALUE_ADDRESS (val) | |
2acceee2 | 299 | + val_print_string (VALUE_ADDRESS (val), -1, 1, stream); |
c906108c SS |
300 | next_section = VALUE_BFD_SECTION (val); |
301 | break; | |
302 | ||
303 | case 'i': | |
304 | /* The old comment says | |
c5aa993b JM |
305 | "Force output out, print_insn not using _filtered". |
306 | I'm not completely sure what that means, I suspect most print_insn | |
307 | now do use _filtered, so I guess it's obsolete. | |
308 | --Yes, it does filter now, and so this is obsolete. -JB */ | |
c906108c SS |
309 | |
310 | /* We often wrap here if there are long symbolic names. */ | |
311 | wrap_here (" "); | |
312 | next_address = VALUE_ADDRESS (val) | |
92bf2b80 | 313 | + gdb_print_insn (VALUE_ADDRESS (val), stream); |
c906108c SS |
314 | next_section = VALUE_BFD_SECTION (val); |
315 | break; | |
316 | ||
317 | default: | |
318 | if (format == 0 | |
319 | || TYPE_CODE (type) == TYPE_CODE_ARRAY | |
320 | || TYPE_CODE (type) == TYPE_CODE_STRING | |
321 | || TYPE_CODE (type) == TYPE_CODE_STRUCT | |
5c4e30ca DC |
322 | || TYPE_CODE (type) == TYPE_CODE_UNION |
323 | || TYPE_CODE (type) == TYPE_CODE_NAMESPACE) | |
c5aa993b JM |
324 | /* If format is 0, use the 'natural' format for |
325 | * that type of value. If the type is non-scalar, | |
326 | * we have to use language rules to print it as | |
327 | * a series of scalars. | |
328 | */ | |
2acceee2 | 329 | value_print (val, stream, format, Val_pretty_default); |
c906108c | 330 | else |
c5aa993b JM |
331 | /* User specified format, so don't look to the |
332 | * the type to tell us what to do. | |
333 | */ | |
c906108c | 334 | print_scalar_formatted (VALUE_CONTENTS (val), type, |
2acceee2 | 335 | format, size, stream); |
c906108c SS |
336 | } |
337 | } | |
338 | ||
339 | /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR, | |
340 | according to letters FORMAT and SIZE on STREAM. | |
341 | FORMAT may not be zero. Formats s and i are not supported at this level. | |
342 | ||
343 | This is how the elements of an array or structure are printed | |
344 | with a format. */ | |
345 | ||
346 | void | |
a2867626 | 347 | print_scalar_formatted (void *valaddr, struct type *type, int format, int size, |
fba45db2 | 348 | struct ui_file *stream) |
c906108c | 349 | { |
81cb7cc9 | 350 | LONGEST val_long = 0; |
c906108c SS |
351 | unsigned int len = TYPE_LENGTH (type); |
352 | ||
6b9acc27 JJ |
353 | if (len > sizeof(LONGEST) && |
354 | (TYPE_CODE (type) == TYPE_CODE_INT | |
355 | || TYPE_CODE (type) == TYPE_CODE_ENUM)) | |
356 | { | |
357 | switch (format) | |
358 | { | |
359 | case 'o': | |
360 | print_octal_chars (stream, valaddr, len); | |
361 | return; | |
362 | case 'u': | |
363 | case 'd': | |
364 | print_decimal_chars (stream, valaddr, len); | |
365 | return; | |
366 | case 't': | |
367 | print_binary_chars (stream, valaddr, len); | |
368 | return; | |
369 | case 'x': | |
370 | print_hex_chars (stream, valaddr, len); | |
371 | return; | |
372 | case 'c': | |
373 | print_char_chars (stream, valaddr, len); | |
374 | return; | |
375 | default: | |
376 | break; | |
377 | }; | |
378 | } | |
379 | ||
95051d27 | 380 | if (format != 'f') |
c906108c SS |
381 | val_long = unpack_long (type, valaddr); |
382 | ||
ef166cf4 | 383 | /* If the value is a pointer, and pointers and addresses are not the |
d0aee0c4 FF |
384 | same, then at this point, the value's length (in target bytes) is |
385 | TARGET_ADDR_BIT/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */ | |
ef166cf4 | 386 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
d0aee0c4 | 387 | len = TARGET_ADDR_BIT / TARGET_CHAR_BIT; |
ef166cf4 | 388 | |
c906108c SS |
389 | /* If we are printing it as unsigned, truncate it in case it is actually |
390 | a negative signed value (e.g. "print/u (short)-1" should print 65535 | |
391 | (if shorts are 16 bits) instead of 4294967295). */ | |
392 | if (format != 'd') | |
393 | { | |
394 | if (len < sizeof (LONGEST)) | |
395 | val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1; | |
396 | } | |
397 | ||
398 | switch (format) | |
399 | { | |
400 | case 'x': | |
401 | if (!size) | |
402 | { | |
403 | /* no size specified, like in print. Print varying # of digits. */ | |
404 | print_longest (stream, 'x', 1, val_long); | |
405 | } | |
406 | else | |
407 | switch (size) | |
408 | { | |
409 | case 'b': | |
410 | case 'h': | |
411 | case 'w': | |
412 | case 'g': | |
413 | print_longest (stream, size, 1, val_long); | |
414 | break; | |
415 | default: | |
416 | error ("Undefined output size \"%c\".", size); | |
417 | } | |
418 | break; | |
419 | ||
420 | case 'd': | |
421 | print_longest (stream, 'd', 1, val_long); | |
422 | break; | |
423 | ||
424 | case 'u': | |
425 | print_longest (stream, 'u', 0, val_long); | |
426 | break; | |
427 | ||
428 | case 'o': | |
429 | if (val_long) | |
430 | print_longest (stream, 'o', 1, val_long); | |
431 | else | |
432 | fprintf_filtered (stream, "0"); | |
433 | break; | |
434 | ||
435 | case 'a': | |
593de6a6 | 436 | { |
593de6a6 | 437 | CORE_ADDR addr = unpack_pointer (type, valaddr); |
593de6a6 PS |
438 | print_address (addr, stream); |
439 | } | |
c906108c SS |
440 | break; |
441 | ||
442 | case 'c': | |
9e0b60a8 JM |
443 | value_print (value_from_longest (builtin_type_true_char, val_long), |
444 | stream, 0, Val_pretty_default); | |
c906108c SS |
445 | break; |
446 | ||
447 | case 'f': | |
f4697836 | 448 | if (len == TYPE_LENGTH (builtin_type_float)) |
664cccae | 449 | type = builtin_type_float; |
f4697836 | 450 | else if (len == TYPE_LENGTH (builtin_type_double)) |
664cccae | 451 | type = builtin_type_double; |
f4697836 JB |
452 | else if (len == TYPE_LENGTH (builtin_type_long_double)) |
453 | type = builtin_type_long_double; | |
c906108c SS |
454 | print_floating (valaddr, type, stream); |
455 | break; | |
456 | ||
457 | case 0: | |
e1e9e218 | 458 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
459 | |
460 | case 't': | |
461 | /* Binary; 't' stands for "two". */ | |
462 | { | |
c5aa993b JM |
463 | char bits[8 * (sizeof val_long) + 1]; |
464 | char buf[8 * (sizeof val_long) + 32]; | |
c906108c SS |
465 | char *cp = bits; |
466 | int width; | |
467 | ||
c5aa993b JM |
468 | if (!size) |
469 | width = 8 * (sizeof val_long); | |
470 | else | |
471 | switch (size) | |
c906108c SS |
472 | { |
473 | case 'b': | |
474 | width = 8; | |
475 | break; | |
476 | case 'h': | |
477 | width = 16; | |
478 | break; | |
479 | case 'w': | |
480 | width = 32; | |
481 | break; | |
482 | case 'g': | |
483 | width = 64; | |
484 | break; | |
485 | default: | |
486 | error ("Undefined output size \"%c\".", size); | |
487 | } | |
488 | ||
c5aa993b JM |
489 | bits[width] = '\0'; |
490 | while (width-- > 0) | |
491 | { | |
492 | bits[width] = (val_long & 1) ? '1' : '0'; | |
493 | val_long >>= 1; | |
494 | } | |
c906108c SS |
495 | if (!size) |
496 | { | |
497 | while (*cp && *cp == '0') | |
498 | cp++; | |
499 | if (*cp == '\0') | |
500 | cp--; | |
501 | } | |
bb599908 | 502 | strcpy (buf, cp); |
306d9ac5 | 503 | fputs_filtered (buf, stream); |
c906108c SS |
504 | } |
505 | break; | |
506 | ||
507 | default: | |
508 | error ("Undefined output format \"%c\".", format); | |
509 | } | |
510 | } | |
511 | ||
512 | /* Specify default address for `x' command. | |
513 | `info lines' uses this. */ | |
514 | ||
515 | void | |
fba45db2 | 516 | set_next_address (CORE_ADDR addr) |
c906108c SS |
517 | { |
518 | next_address = addr; | |
519 | ||
520 | /* Make address available to the user as $_. */ | |
521 | set_internalvar (lookup_internalvar ("_"), | |
4478b372 JB |
522 | value_from_pointer (lookup_pointer_type (builtin_type_void), |
523 | addr)); | |
c906108c SS |
524 | } |
525 | ||
526 | /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM, | |
527 | after LEADIN. Print nothing if no symbolic name is found nearby. | |
528 | Optionally also print source file and line number, if available. | |
529 | DO_DEMANGLE controls whether to print a symbol in its native "raw" form, | |
530 | or to interpret it as a possible C++ name and convert it back to source | |
531 | form. However note that DO_DEMANGLE can be overridden by the specific | |
532 | settings of the demangle and asm_demangle variables. */ | |
533 | ||
534 | void | |
fba45db2 KB |
535 | print_address_symbolic (CORE_ADDR addr, struct ui_file *stream, int do_demangle, |
536 | char *leadin) | |
dfcd3bfb JM |
537 | { |
538 | char *name = NULL; | |
539 | char *filename = NULL; | |
540 | int unmapped = 0; | |
541 | int offset = 0; | |
542 | int line = 0; | |
543 | ||
2f9429ae AC |
544 | /* throw away both name and filename */ |
545 | struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name); | |
546 | make_cleanup (free_current_contents, &filename); | |
dfcd3bfb JM |
547 | |
548 | if (build_address_symbolic (addr, do_demangle, &name, &offset, &filename, &line, &unmapped)) | |
2f9429ae AC |
549 | { |
550 | do_cleanups (cleanup_chain); | |
551 | return; | |
552 | } | |
dfcd3bfb JM |
553 | |
554 | fputs_filtered (leadin, stream); | |
555 | if (unmapped) | |
556 | fputs_filtered ("<*", stream); | |
557 | else | |
558 | fputs_filtered ("<", stream); | |
559 | fputs_filtered (name, stream); | |
560 | if (offset != 0) | |
561 | fprintf_filtered (stream, "+%u", (unsigned int) offset); | |
562 | ||
563 | /* Append source filename and line number if desired. Give specific | |
564 | line # of this addr, if we have it; else line # of the nearest symbol. */ | |
565 | if (print_symbol_filename && filename != NULL) | |
566 | { | |
567 | if (line != -1) | |
568 | fprintf_filtered (stream, " at %s:%d", filename, line); | |
569 | else | |
570 | fprintf_filtered (stream, " in %s", filename); | |
571 | } | |
572 | if (unmapped) | |
573 | fputs_filtered ("*>", stream); | |
574 | else | |
575 | fputs_filtered (">", stream); | |
576 | ||
577 | do_cleanups (cleanup_chain); | |
578 | } | |
579 | ||
580 | /* Given an address ADDR return all the elements needed to print the | |
581 | address in a symbolic form. NAME can be mangled or not depending | |
582 | on DO_DEMANGLE (and also on the asm_demangle global variable, | |
583 | manipulated via ''set print asm-demangle''). Return 0 in case of | |
584 | success, when all the info in the OUT paramters is valid. Return 1 | |
585 | otherwise. */ | |
586 | int | |
587 | build_address_symbolic (CORE_ADDR addr, /* IN */ | |
588 | int do_demangle, /* IN */ | |
589 | char **name, /* OUT */ | |
590 | int *offset, /* OUT */ | |
591 | char **filename, /* OUT */ | |
592 | int *line, /* OUT */ | |
593 | int *unmapped) /* OUT */ | |
c906108c SS |
594 | { |
595 | struct minimal_symbol *msymbol; | |
596 | struct symbol *symbol; | |
597 | struct symtab *symtab = 0; | |
598 | CORE_ADDR name_location = 0; | |
c906108c | 599 | asection *section = 0; |
dfcd3bfb JM |
600 | char *name_temp = ""; |
601 | ||
602 | /* Let's say it is unmapped. */ | |
603 | *unmapped = 0; | |
c906108c | 604 | |
dfcd3bfb JM |
605 | /* Determine if the address is in an overlay, and whether it is |
606 | mapped. */ | |
c906108c SS |
607 | if (overlay_debugging) |
608 | { | |
609 | section = find_pc_overlay (addr); | |
610 | if (pc_in_unmapped_range (addr, section)) | |
611 | { | |
dfcd3bfb | 612 | *unmapped = 1; |
c906108c SS |
613 | addr = overlay_mapped_address (addr, section); |
614 | } | |
615 | } | |
616 | ||
c906108c SS |
617 | /* First try to find the address in the symbol table, then |
618 | in the minsyms. Take the closest one. */ | |
619 | ||
620 | /* This is defective in the sense that it only finds text symbols. So | |
621 | really this is kind of pointless--we should make sure that the | |
622 | minimal symbols have everything we need (by changing that we could | |
623 | save some memory, but for many debug format--ELF/DWARF or | |
624 | anything/stabs--it would be inconvenient to eliminate those minimal | |
625 | symbols anyway). */ | |
626 | msymbol = lookup_minimal_symbol_by_pc_section (addr, section); | |
627 | symbol = find_pc_sect_function (addr, section); | |
628 | ||
629 | if (symbol) | |
630 | { | |
631 | name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)); | |
406fc7fb | 632 | if (do_demangle || asm_demangle) |
de5ad195 | 633 | name_temp = SYMBOL_PRINT_NAME (symbol); |
c906108c | 634 | else |
22abf04a | 635 | name_temp = DEPRECATED_SYMBOL_NAME (symbol); |
c906108c SS |
636 | } |
637 | ||
638 | if (msymbol != NULL) | |
639 | { | |
640 | if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL) | |
641 | { | |
642 | /* The msymbol is closer to the address than the symbol; | |
643 | use the msymbol instead. */ | |
644 | symbol = 0; | |
645 | symtab = 0; | |
646 | name_location = SYMBOL_VALUE_ADDRESS (msymbol); | |
406fc7fb | 647 | if (do_demangle || asm_demangle) |
de5ad195 | 648 | name_temp = SYMBOL_PRINT_NAME (msymbol); |
c906108c | 649 | else |
22abf04a | 650 | name_temp = DEPRECATED_SYMBOL_NAME (msymbol); |
c906108c SS |
651 | } |
652 | } | |
653 | if (symbol == NULL && msymbol == NULL) | |
dfcd3bfb | 654 | return 1; |
c906108c | 655 | |
c906108c SS |
656 | /* If the nearest symbol is too far away, don't print anything symbolic. */ |
657 | ||
658 | /* For when CORE_ADDR is larger than unsigned int, we do math in | |
659 | CORE_ADDR. But when we detect unsigned wraparound in the | |
660 | CORE_ADDR math, we ignore this test and print the offset, | |
661 | because addr+max_symbolic_offset has wrapped through the end | |
662 | of the address space back to the beginning, giving bogus comparison. */ | |
663 | if (addr > name_location + max_symbolic_offset | |
664 | && name_location + max_symbolic_offset > name_location) | |
dfcd3bfb | 665 | return 1; |
c906108c | 666 | |
dfcd3bfb JM |
667 | *offset = addr - name_location; |
668 | ||
669 | *name = xstrdup (name_temp); | |
c906108c | 670 | |
c906108c SS |
671 | if (print_symbol_filename) |
672 | { | |
673 | struct symtab_and_line sal; | |
674 | ||
675 | sal = find_pc_sect_line (addr, section, 0); | |
676 | ||
677 | if (sal.symtab) | |
dfcd3bfb JM |
678 | { |
679 | *filename = xstrdup (sal.symtab->filename); | |
680 | *line = sal.line; | |
681 | } | |
c906108c | 682 | else if (symtab && symbol && symbol->line) |
dfcd3bfb JM |
683 | { |
684 | *filename = xstrdup (symtab->filename); | |
685 | *line = symbol->line; | |
686 | } | |
c906108c | 687 | else if (symtab) |
dfcd3bfb JM |
688 | { |
689 | *filename = xstrdup (symtab->filename); | |
690 | *line = -1; | |
691 | } | |
c906108c | 692 | } |
dfcd3bfb | 693 | return 0; |
c906108c SS |
694 | } |
695 | ||
c906108c SS |
696 | /* Print address ADDR on STREAM. USE_LOCAL means the same thing as for |
697 | print_longest. */ | |
698 | void | |
fba45db2 | 699 | print_address_numeric (CORE_ADDR addr, int use_local, struct ui_file *stream) |
c906108c | 700 | { |
c0d8fd9a | 701 | /* Truncate address to the size of a target address, avoiding shifts |
e2ad119d | 702 | larger or equal than the width of a CORE_ADDR. The local |
c0d8fd9a MS |
703 | variable ADDR_BIT stops the compiler reporting a shift overflow |
704 | when it won't occur. */ | |
e2ad119d AC |
705 | /* NOTE: This assumes that the significant address information is |
706 | kept in the least significant bits of ADDR - the upper bits were | |
707 | either zero or sign extended. Should ADDRESS_TO_POINTER() or | |
708 | some ADDRESS_TO_PRINTABLE() be used to do the conversion? */ | |
c0d8fd9a | 709 | |
52204a0b | 710 | int addr_bit = TARGET_ADDR_BIT; |
c0d8fd9a | 711 | |
52204a0b DT |
712 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) |
713 | addr &= ((CORE_ADDR) 1 << addr_bit) - 1; | |
c906108c SS |
714 | print_longest (stream, 'x', use_local, (ULONGEST) addr); |
715 | } | |
716 | ||
717 | /* Print address ADDR symbolically on STREAM. | |
718 | First print it as a number. Then perhaps print | |
719 | <SYMBOL + OFFSET> after the number. */ | |
720 | ||
721 | void | |
fba45db2 | 722 | print_address (CORE_ADDR addr, struct ui_file *stream) |
c906108c SS |
723 | { |
724 | print_address_numeric (addr, 1, stream); | |
725 | print_address_symbolic (addr, stream, asm_demangle, " "); | |
726 | } | |
727 | ||
728 | /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE | |
729 | controls whether to print the symbolic name "raw" or demangled. | |
730 | Global setting "addressprint" controls whether to print hex address | |
731 | or not. */ | |
732 | ||
733 | void | |
fba45db2 | 734 | print_address_demangle (CORE_ADDR addr, struct ui_file *stream, int do_demangle) |
c906108c SS |
735 | { |
736 | if (addr == 0) | |
737 | { | |
738 | fprintf_filtered (stream, "0"); | |
739 | } | |
740 | else if (addressprint) | |
741 | { | |
742 | print_address_numeric (addr, 1, stream); | |
743 | print_address_symbolic (addr, stream, do_demangle, " "); | |
744 | } | |
745 | else | |
746 | { | |
747 | print_address_symbolic (addr, stream, do_demangle, ""); | |
748 | } | |
749 | } | |
750 | \f | |
751 | ||
752 | /* These are the types that $__ will get after an examine command of one | |
753 | of these sizes. */ | |
754 | ||
755 | static struct type *examine_i_type; | |
756 | ||
757 | static struct type *examine_b_type; | |
758 | static struct type *examine_h_type; | |
759 | static struct type *examine_w_type; | |
760 | static struct type *examine_g_type; | |
761 | ||
762 | /* Examine data at address ADDR in format FMT. | |
763 | Fetch it from memory and print on gdb_stdout. */ | |
764 | ||
765 | static void | |
fba45db2 | 766 | do_examine (struct format_data fmt, CORE_ADDR addr, asection *sect) |
c906108c | 767 | { |
52f0bd74 AC |
768 | char format = 0; |
769 | char size; | |
770 | int count = 1; | |
c906108c | 771 | struct type *val_type = NULL; |
52f0bd74 AC |
772 | int i; |
773 | int maxelts; | |
c906108c SS |
774 | |
775 | format = fmt.format; | |
776 | size = fmt.size; | |
777 | count = fmt.count; | |
778 | next_address = addr; | |
779 | next_section = sect; | |
780 | ||
781 | /* String or instruction format implies fetch single bytes | |
782 | regardless of the specified size. */ | |
783 | if (format == 's' || format == 'i') | |
784 | size = 'b'; | |
785 | ||
786 | if (format == 'i') | |
787 | val_type = examine_i_type; | |
788 | else if (size == 'b') | |
789 | val_type = examine_b_type; | |
790 | else if (size == 'h') | |
791 | val_type = examine_h_type; | |
792 | else if (size == 'w') | |
793 | val_type = examine_w_type; | |
794 | else if (size == 'g') | |
795 | val_type = examine_g_type; | |
796 | ||
797 | maxelts = 8; | |
798 | if (size == 'w') | |
799 | maxelts = 4; | |
800 | if (size == 'g') | |
801 | maxelts = 2; | |
802 | if (format == 's' || format == 'i') | |
803 | maxelts = 1; | |
804 | ||
805 | /* Print as many objects as specified in COUNT, at most maxelts per line, | |
806 | with the address of the next one at the start of each line. */ | |
807 | ||
808 | while (count > 0) | |
809 | { | |
810 | QUIT; | |
811 | print_address (next_address, gdb_stdout); | |
812 | printf_filtered (":"); | |
813 | for (i = maxelts; | |
814 | i > 0 && count > 0; | |
815 | i--, count--) | |
816 | { | |
817 | printf_filtered ("\t"); | |
818 | /* Note that print_formatted sets next_address for the next | |
819 | object. */ | |
820 | last_examine_address = next_address; | |
821 | ||
822 | if (last_examine_value) | |
823 | value_free (last_examine_value); | |
824 | ||
825 | /* The value to be displayed is not fetched greedily. | |
c5aa993b JM |
826 | Instead, to avoid the posibility of a fetched value not |
827 | being used, its retreval is delayed until the print code | |
828 | uses it. When examining an instruction stream, the | |
829 | disassembler will perform its own memory fetch using just | |
830 | the address stored in LAST_EXAMINE_VALUE. FIXME: Should | |
831 | the disassembler be modified so that LAST_EXAMINE_VALUE | |
832 | is left with the byte sequence from the last complete | |
833 | instruction fetched from memory? */ | |
c906108c SS |
834 | last_examine_value = value_at_lazy (val_type, next_address, sect); |
835 | ||
836 | if (last_examine_value) | |
837 | release_value (last_examine_value); | |
838 | ||
2acceee2 | 839 | print_formatted (last_examine_value, format, size, gdb_stdout); |
c906108c SS |
840 | } |
841 | printf_filtered ("\n"); | |
842 | gdb_flush (gdb_stdout); | |
843 | } | |
844 | } | |
845 | \f | |
846 | static void | |
fba45db2 | 847 | validate_format (struct format_data fmt, char *cmdname) |
c906108c SS |
848 | { |
849 | if (fmt.size != 0) | |
850 | error ("Size letters are meaningless in \"%s\" command.", cmdname); | |
851 | if (fmt.count != 1) | |
852 | error ("Item count other than 1 is meaningless in \"%s\" command.", | |
853 | cmdname); | |
854 | if (fmt.format == 'i' || fmt.format == 's') | |
855 | error ("Format letter \"%c\" is meaningless in \"%s\" command.", | |
856 | fmt.format, cmdname); | |
857 | } | |
858 | ||
859 | /* Evaluate string EXP as an expression in the current language and | |
c5aa993b JM |
860 | print the resulting value. EXP may contain a format specifier as the |
861 | first argument ("/x myvar" for example, to print myvar in hex). | |
862 | */ | |
c906108c SS |
863 | |
864 | static void | |
fba45db2 | 865 | print_command_1 (char *exp, int inspect, int voidprint) |
c906108c SS |
866 | { |
867 | struct expression *expr; | |
52f0bd74 AC |
868 | struct cleanup *old_chain = 0; |
869 | char format = 0; | |
3d6d86c6 | 870 | struct value *val; |
c906108c SS |
871 | struct format_data fmt; |
872 | int cleanup = 0; | |
873 | ||
874 | /* Pass inspect flag to the rest of the print routines in a global (sigh). */ | |
875 | inspect_it = inspect; | |
876 | ||
877 | if (exp && *exp == '/') | |
878 | { | |
879 | exp++; | |
880 | fmt = decode_format (&exp, last_format, 0); | |
881 | validate_format (fmt, "print"); | |
882 | last_format = format = fmt.format; | |
883 | } | |
884 | else | |
885 | { | |
886 | fmt.count = 1; | |
887 | fmt.format = 0; | |
888 | fmt.size = 0; | |
889 | } | |
890 | ||
891 | if (exp && *exp) | |
892 | { | |
c906108c SS |
893 | struct type *type; |
894 | expr = parse_expression (exp); | |
c13c43fd | 895 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
896 | cleanup = 1; |
897 | val = evaluate_expression (expr); | |
c906108c SS |
898 | } |
899 | else | |
900 | val = access_value_history (0); | |
901 | ||
902 | if (voidprint || (val && VALUE_TYPE (val) && | |
c5aa993b | 903 | TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_VOID)) |
c906108c SS |
904 | { |
905 | int histindex = record_latest_value (val); | |
906 | ||
907 | if (histindex >= 0) | |
908 | annotate_value_history_begin (histindex, VALUE_TYPE (val)); | |
909 | else | |
910 | annotate_value_begin (VALUE_TYPE (val)); | |
911 | ||
912 | if (inspect) | |
913 | printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"", exp, histindex); | |
c5aa993b JM |
914 | else if (histindex >= 0) |
915 | printf_filtered ("$%d = ", histindex); | |
c906108c SS |
916 | |
917 | if (histindex >= 0) | |
918 | annotate_value_history_value (); | |
919 | ||
2acceee2 | 920 | print_formatted (val, format, fmt.size, gdb_stdout); |
c906108c SS |
921 | printf_filtered ("\n"); |
922 | ||
923 | if (histindex >= 0) | |
924 | annotate_value_history_end (); | |
925 | else | |
926 | annotate_value_end (); | |
927 | ||
928 | if (inspect) | |
c5aa993b | 929 | printf_unfiltered ("\") )\030"); |
c906108c SS |
930 | } |
931 | ||
932 | if (cleanup) | |
933 | do_cleanups (old_chain); | |
c5aa993b | 934 | inspect_it = 0; /* Reset print routines to normal */ |
c906108c SS |
935 | } |
936 | ||
c906108c | 937 | static void |
fba45db2 | 938 | print_command (char *exp, int from_tty) |
c906108c SS |
939 | { |
940 | print_command_1 (exp, 0, 1); | |
941 | } | |
942 | ||
943 | /* Same as print, except in epoch, it gets its own window */ | |
c906108c | 944 | static void |
fba45db2 | 945 | inspect_command (char *exp, int from_tty) |
c906108c SS |
946 | { |
947 | extern int epoch_interface; | |
948 | ||
949 | print_command_1 (exp, epoch_interface, 1); | |
950 | } | |
951 | ||
952 | /* Same as print, except it doesn't print void results. */ | |
c906108c | 953 | static void |
fba45db2 | 954 | call_command (char *exp, int from_tty) |
c906108c SS |
955 | { |
956 | print_command_1 (exp, 0, 0); | |
957 | } | |
958 | ||
c906108c | 959 | void |
fba45db2 | 960 | output_command (char *exp, int from_tty) |
c906108c SS |
961 | { |
962 | struct expression *expr; | |
52f0bd74 AC |
963 | struct cleanup *old_chain; |
964 | char format = 0; | |
3d6d86c6 | 965 | struct value *val; |
c906108c SS |
966 | struct format_data fmt; |
967 | ||
968 | if (exp && *exp == '/') | |
969 | { | |
970 | exp++; | |
971 | fmt = decode_format (&exp, 0, 0); | |
972 | validate_format (fmt, "output"); | |
973 | format = fmt.format; | |
974 | } | |
975 | ||
976 | expr = parse_expression (exp); | |
c13c43fd | 977 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
978 | |
979 | val = evaluate_expression (expr); | |
980 | ||
981 | annotate_value_begin (VALUE_TYPE (val)); | |
982 | ||
2acceee2 | 983 | print_formatted (val, format, fmt.size, gdb_stdout); |
c906108c SS |
984 | |
985 | annotate_value_end (); | |
986 | ||
2acceee2 JM |
987 | wrap_here (""); |
988 | gdb_flush (gdb_stdout); | |
989 | ||
c906108c SS |
990 | do_cleanups (old_chain); |
991 | } | |
992 | ||
c906108c | 993 | static void |
fba45db2 | 994 | set_command (char *exp, int from_tty) |
c906108c SS |
995 | { |
996 | struct expression *expr = parse_expression (exp); | |
52f0bd74 | 997 | struct cleanup *old_chain = |
c13c43fd | 998 | make_cleanup (free_current_contents, &expr); |
c906108c SS |
999 | evaluate_expression (expr); |
1000 | do_cleanups (old_chain); | |
1001 | } | |
1002 | ||
c906108c | 1003 | static void |
fba45db2 | 1004 | sym_info (char *arg, int from_tty) |
c906108c SS |
1005 | { |
1006 | struct minimal_symbol *msymbol; | |
c5aa993b JM |
1007 | struct objfile *objfile; |
1008 | struct obj_section *osect; | |
1009 | asection *sect; | |
1010 | CORE_ADDR addr, sect_addr; | |
1011 | int matches = 0; | |
1012 | unsigned int offset; | |
c906108c SS |
1013 | |
1014 | if (!arg) | |
1015 | error_no_arg ("address"); | |
1016 | ||
1017 | addr = parse_and_eval_address (arg); | |
1018 | ALL_OBJSECTIONS (objfile, osect) | |
c5aa993b JM |
1019 | { |
1020 | sect = osect->the_bfd_section; | |
1021 | sect_addr = overlay_mapped_address (addr, sect); | |
c906108c | 1022 | |
c5aa993b JM |
1023 | if (osect->addr <= sect_addr && sect_addr < osect->endaddr && |
1024 | (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect))) | |
1025 | { | |
1026 | matches = 1; | |
1027 | offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol); | |
1028 | if (offset) | |
1029 | printf_filtered ("%s + %u in ", | |
de5ad195 | 1030 | SYMBOL_PRINT_NAME (msymbol), offset); |
c5aa993b JM |
1031 | else |
1032 | printf_filtered ("%s in ", | |
de5ad195 | 1033 | SYMBOL_PRINT_NAME (msymbol)); |
c5aa993b JM |
1034 | if (pc_in_unmapped_range (addr, sect)) |
1035 | printf_filtered ("load address range of "); | |
1036 | if (section_is_overlay (sect)) | |
1037 | printf_filtered ("%s overlay ", | |
1038 | section_is_mapped (sect) ? "mapped" : "unmapped"); | |
1039 | printf_filtered ("section %s", sect->name); | |
1040 | printf_filtered ("\n"); | |
1041 | } | |
1042 | } | |
c906108c SS |
1043 | if (matches == 0) |
1044 | printf_filtered ("No symbol matches %s.\n", arg); | |
1045 | } | |
1046 | ||
c906108c | 1047 | static void |
fba45db2 | 1048 | address_info (char *exp, int from_tty) |
c906108c | 1049 | { |
52f0bd74 AC |
1050 | struct symbol *sym; |
1051 | struct minimal_symbol *msymbol; | |
1052 | long val; | |
1053 | long basereg; | |
c906108c SS |
1054 | asection *section; |
1055 | CORE_ADDR load_addr; | |
1056 | int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero | |
1057 | if exp is a field of `this'. */ | |
1058 | ||
1059 | if (exp == 0) | |
1060 | error ("Argument required."); | |
1061 | ||
176620f1 | 1062 | sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN, |
c5aa993b | 1063 | &is_a_field_of_this, (struct symtab **) NULL); |
c906108c SS |
1064 | if (sym == NULL) |
1065 | { | |
1066 | if (is_a_field_of_this) | |
1067 | { | |
1068 | printf_filtered ("Symbol \""); | |
1069 | fprintf_symbol_filtered (gdb_stdout, exp, | |
1070 | current_language->la_language, DMGL_ANSI); | |
e2b23ee9 AF |
1071 | printf_filtered ("\" is a field of the local class variable "); |
1072 | if (current_language->la_language == language_objc) | |
2625d86c | 1073 | printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */ |
e2b23ee9 | 1074 | else |
2625d86c | 1075 | printf_filtered ("`this'\n"); |
c906108c SS |
1076 | return; |
1077 | } | |
1078 | ||
1079 | msymbol = lookup_minimal_symbol (exp, NULL, NULL); | |
1080 | ||
1081 | if (msymbol != NULL) | |
1082 | { | |
1083 | load_addr = SYMBOL_VALUE_ADDRESS (msymbol); | |
1084 | ||
1085 | printf_filtered ("Symbol \""); | |
1086 | fprintf_symbol_filtered (gdb_stdout, exp, | |
1087 | current_language->la_language, DMGL_ANSI); | |
1088 | printf_filtered ("\" is at "); | |
1089 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1090 | printf_filtered (" in a file compiled without debugging"); | |
1091 | section = SYMBOL_BFD_SECTION (msymbol); | |
1092 | if (section_is_overlay (section)) | |
1093 | { | |
1094 | load_addr = overlay_unmapped_address (load_addr, section); | |
1095 | printf_filtered (",\n -- loaded at "); | |
1096 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1097 | printf_filtered (" in overlay section %s", section->name); | |
1098 | } | |
1099 | printf_filtered (".\n"); | |
1100 | } | |
1101 | else | |
1102 | error ("No symbol \"%s\" in current context.", exp); | |
1103 | return; | |
1104 | } | |
1105 | ||
1106 | printf_filtered ("Symbol \""); | |
22abf04a | 1107 | fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym), |
c906108c SS |
1108 | current_language->la_language, DMGL_ANSI); |
1109 | printf_filtered ("\" is "); | |
c5aa993b | 1110 | val = SYMBOL_VALUE (sym); |
c906108c SS |
1111 | basereg = SYMBOL_BASEREG (sym); |
1112 | section = SYMBOL_BFD_SECTION (sym); | |
1113 | ||
1114 | switch (SYMBOL_CLASS (sym)) | |
1115 | { | |
1116 | case LOC_CONST: | |
1117 | case LOC_CONST_BYTES: | |
1118 | printf_filtered ("constant"); | |
1119 | break; | |
1120 | ||
1121 | case LOC_LABEL: | |
1122 | printf_filtered ("a label at address "); | |
c5aa993b | 1123 | print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym), |
c906108c SS |
1124 | 1, gdb_stdout); |
1125 | if (section_is_overlay (section)) | |
1126 | { | |
1127 | load_addr = overlay_unmapped_address (load_addr, section); | |
1128 | printf_filtered (",\n -- loaded at "); | |
1129 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1130 | printf_filtered (" in overlay section %s", section->name); | |
1131 | } | |
1132 | break; | |
1133 | ||
4c2df51b DJ |
1134 | case LOC_COMPUTED: |
1135 | case LOC_COMPUTED_ARG: | |
a67af2b9 AC |
1136 | /* FIXME: cagney/2004-01-26: It should be possible to |
1137 | unconditionally call the SYMBOL_OPS method when available. | |
d3efc286 | 1138 | Unfortunately DWARF 2 stores the frame-base (instead of the |
a67af2b9 AC |
1139 | function) location in a function's symbol. Oops! For the |
1140 | moment enable this when/where applicable. */ | |
1141 | SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout); | |
4c2df51b DJ |
1142 | break; |
1143 | ||
c906108c SS |
1144 | case LOC_REGISTER: |
1145 | printf_filtered ("a variable in register %s", REGISTER_NAME (val)); | |
1146 | break; | |
1147 | ||
1148 | case LOC_STATIC: | |
1149 | printf_filtered ("static storage at address "); | |
c5aa993b | 1150 | print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym), |
c906108c SS |
1151 | 1, gdb_stdout); |
1152 | if (section_is_overlay (section)) | |
1153 | { | |
1154 | load_addr = overlay_unmapped_address (load_addr, section); | |
1155 | printf_filtered (",\n -- loaded at "); | |
1156 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1157 | printf_filtered (" in overlay section %s", section->name); | |
1158 | } | |
1159 | break; | |
1160 | ||
1161 | case LOC_INDIRECT: | |
1162 | printf_filtered ("external global (indirect addressing), at address *("); | |
1163 | print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym), | |
1164 | 1, gdb_stdout); | |
1165 | printf_filtered (")"); | |
1166 | if (section_is_overlay (section)) | |
1167 | { | |
1168 | load_addr = overlay_unmapped_address (load_addr, section); | |
1169 | printf_filtered (",\n -- loaded at "); | |
1170 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1171 | printf_filtered (" in overlay section %s", section->name); | |
1172 | } | |
1173 | break; | |
1174 | ||
1175 | case LOC_REGPARM: | |
1176 | printf_filtered ("an argument in register %s", REGISTER_NAME (val)); | |
1177 | break; | |
1178 | ||
1179 | case LOC_REGPARM_ADDR: | |
1180 | printf_filtered ("address of an argument in register %s", REGISTER_NAME (val)); | |
1181 | break; | |
1182 | ||
1183 | case LOC_ARG: | |
1184 | printf_filtered ("an argument at offset %ld", val); | |
1185 | break; | |
1186 | ||
1187 | case LOC_LOCAL_ARG: | |
1188 | printf_filtered ("an argument at frame offset %ld", val); | |
1189 | break; | |
1190 | ||
1191 | case LOC_LOCAL: | |
1192 | printf_filtered ("a local variable at frame offset %ld", val); | |
1193 | break; | |
1194 | ||
1195 | case LOC_REF_ARG: | |
1196 | printf_filtered ("a reference argument at offset %ld", val); | |
1197 | break; | |
1198 | ||
1199 | case LOC_BASEREG: | |
1200 | printf_filtered ("a variable at offset %ld from register %s", | |
c5aa993b | 1201 | val, REGISTER_NAME (basereg)); |
c906108c SS |
1202 | break; |
1203 | ||
1204 | case LOC_BASEREG_ARG: | |
1205 | printf_filtered ("an argument at offset %ld from register %s", | |
c5aa993b | 1206 | val, REGISTER_NAME (basereg)); |
c906108c SS |
1207 | break; |
1208 | ||
1209 | case LOC_TYPEDEF: | |
1210 | printf_filtered ("a typedef"); | |
1211 | break; | |
1212 | ||
1213 | case LOC_BLOCK: | |
1214 | printf_filtered ("a function at address "); | |
c5aa993b | 1215 | print_address_numeric (load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)), |
c906108c | 1216 | 1, gdb_stdout); |
c906108c SS |
1217 | if (section_is_overlay (section)) |
1218 | { | |
1219 | load_addr = overlay_unmapped_address (load_addr, section); | |
1220 | printf_filtered (",\n -- loaded at "); | |
1221 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1222 | printf_filtered (" in overlay section %s", section->name); | |
1223 | } | |
1224 | break; | |
1225 | ||
1226 | case LOC_UNRESOLVED: | |
1227 | { | |
1228 | struct minimal_symbol *msym; | |
1229 | ||
22abf04a | 1230 | msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL); |
c906108c SS |
1231 | if (msym == NULL) |
1232 | printf_filtered ("unresolved"); | |
1233 | else | |
1234 | { | |
1235 | section = SYMBOL_BFD_SECTION (msym); | |
1236 | printf_filtered ("static storage at address "); | |
c5aa993b | 1237 | print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (msym), |
c906108c SS |
1238 | 1, gdb_stdout); |
1239 | if (section_is_overlay (section)) | |
1240 | { | |
1241 | load_addr = overlay_unmapped_address (load_addr, section); | |
1242 | printf_filtered (",\n -- loaded at "); | |
1243 | print_address_numeric (load_addr, 1, gdb_stdout); | |
1244 | printf_filtered (" in overlay section %s", section->name); | |
1245 | } | |
1246 | } | |
1247 | } | |
1248 | break; | |
1249 | ||
407caf07 | 1250 | case LOC_HP_THREAD_LOCAL_STATIC: |
c906108c | 1251 | printf_filtered ( |
c5aa993b JM |
1252 | "a thread-local variable at offset %ld from the thread base register %s", |
1253 | val, REGISTER_NAME (basereg)); | |
c906108c SS |
1254 | break; |
1255 | ||
1256 | case LOC_OPTIMIZED_OUT: | |
1257 | printf_filtered ("optimized out"); | |
1258 | break; | |
c5aa993b | 1259 | |
c906108c SS |
1260 | default: |
1261 | printf_filtered ("of unknown (botched) type"); | |
1262 | break; | |
1263 | } | |
1264 | printf_filtered (".\n"); | |
1265 | } | |
1266 | \f | |
1267 | void | |
fba45db2 | 1268 | x_command (char *exp, int from_tty) |
c906108c SS |
1269 | { |
1270 | struct expression *expr; | |
1271 | struct format_data fmt; | |
1272 | struct cleanup *old_chain; | |
1273 | struct value *val; | |
1274 | ||
1275 | fmt.format = last_format; | |
1276 | fmt.size = last_size; | |
1277 | fmt.count = 1; | |
1278 | ||
1279 | if (exp && *exp == '/') | |
1280 | { | |
1281 | exp++; | |
1282 | fmt = decode_format (&exp, last_format, last_size); | |
1283 | } | |
1284 | ||
1285 | /* If we have an expression, evaluate it and use it as the address. */ | |
1286 | ||
1287 | if (exp != 0 && *exp != 0) | |
1288 | { | |
1289 | expr = parse_expression (exp); | |
1290 | /* Cause expression not to be there any more | |
c5aa993b JM |
1291 | if this command is repeated with Newline. |
1292 | But don't clobber a user-defined command's definition. */ | |
c906108c SS |
1293 | if (from_tty) |
1294 | *exp = 0; | |
c13c43fd | 1295 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
1296 | val = evaluate_expression (expr); |
1297 | if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF) | |
1298 | val = value_ind (val); | |
1299 | /* In rvalue contexts, such as this, functions are coerced into | |
c5aa993b | 1300 | pointers to functions. This makes "x/i main" work. */ |
c0d8fd9a MS |
1301 | if (/* last_format == 'i' && */ |
1302 | TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC | |
c5aa993b | 1303 | && VALUE_LVAL (val) == lval_memory) |
c906108c SS |
1304 | next_address = VALUE_ADDRESS (val); |
1305 | else | |
1aa20aa8 | 1306 | next_address = value_as_address (val); |
c906108c SS |
1307 | if (VALUE_BFD_SECTION (val)) |
1308 | next_section = VALUE_BFD_SECTION (val); | |
1309 | do_cleanups (old_chain); | |
1310 | } | |
1311 | ||
1312 | do_examine (fmt, next_address, next_section); | |
1313 | ||
1314 | /* If the examine succeeds, we remember its size and format for next time. */ | |
1315 | last_size = fmt.size; | |
1316 | last_format = fmt.format; | |
1317 | ||
1318 | /* Set a couple of internal variables if appropriate. */ | |
1319 | if (last_examine_value) | |
1320 | { | |
1321 | /* Make last address examined available to the user as $_. Use | |
c5aa993b | 1322 | the correct pointer type. */ |
4478b372 JB |
1323 | struct type *pointer_type |
1324 | = lookup_pointer_type (VALUE_TYPE (last_examine_value)); | |
c906108c | 1325 | set_internalvar (lookup_internalvar ("_"), |
4478b372 JB |
1326 | value_from_pointer (pointer_type, |
1327 | last_examine_address)); | |
c5aa993b JM |
1328 | |
1329 | /* Make contents of last address examined available to the user as $__. */ | |
c906108c SS |
1330 | /* If the last value has not been fetched from memory then don't |
1331 | fetch it now - instead mark it by voiding the $__ variable. */ | |
1332 | if (VALUE_LAZY (last_examine_value)) | |
1333 | set_internalvar (lookup_internalvar ("__"), | |
1334 | allocate_value (builtin_type_void)); | |
1335 | else | |
1336 | set_internalvar (lookup_internalvar ("__"), last_examine_value); | |
1337 | } | |
1338 | } | |
c906108c | 1339 | \f |
c5aa993b | 1340 | |
c906108c SS |
1341 | /* Add an expression to the auto-display chain. |
1342 | Specify the expression. */ | |
1343 | ||
1344 | static void | |
fba45db2 | 1345 | display_command (char *exp, int from_tty) |
c906108c SS |
1346 | { |
1347 | struct format_data fmt; | |
52f0bd74 AC |
1348 | struct expression *expr; |
1349 | struct display *new; | |
c906108c SS |
1350 | int display_it = 1; |
1351 | ||
1352 | #if defined(TUI) | |
021e7609 AC |
1353 | /* NOTE: cagney/2003-02-13 The `tui_active' was previously |
1354 | `tui_version'. */ | |
fd33e6cb | 1355 | if (tui_active && exp != NULL && *exp == '$') |
080ce8c0 | 1356 | display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE); |
c906108c SS |
1357 | #endif |
1358 | ||
1359 | if (display_it) | |
1360 | { | |
1361 | if (exp == 0) | |
1362 | { | |
1363 | do_displays (); | |
1364 | return; | |
1365 | } | |
1366 | ||
1367 | if (*exp == '/') | |
1368 | { | |
1369 | exp++; | |
1370 | fmt = decode_format (&exp, 0, 0); | |
1371 | if (fmt.size && fmt.format == 0) | |
1372 | fmt.format = 'x'; | |
1373 | if (fmt.format == 'i' || fmt.format == 's') | |
1374 | fmt.size = 'b'; | |
1375 | } | |
1376 | else | |
1377 | { | |
1378 | fmt.format = 0; | |
1379 | fmt.size = 0; | |
1380 | fmt.count = 0; | |
1381 | } | |
1382 | ||
1383 | innermost_block = 0; | |
1384 | expr = parse_expression (exp); | |
1385 | ||
1386 | new = (struct display *) xmalloc (sizeof (struct display)); | |
1387 | ||
1388 | new->exp = expr; | |
1389 | new->block = innermost_block; | |
1390 | new->next = display_chain; | |
1391 | new->number = ++display_number; | |
1392 | new->format = fmt; | |
b5de0fa7 | 1393 | new->enabled_p = 1; |
c906108c SS |
1394 | display_chain = new; |
1395 | ||
1396 | if (from_tty && target_has_execution) | |
1397 | do_one_display (new); | |
1398 | ||
1399 | dont_repeat (); | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | static void | |
fba45db2 | 1404 | free_display (struct display *d) |
c906108c | 1405 | { |
b8c9b27d KB |
1406 | xfree (d->exp); |
1407 | xfree (d); | |
c906108c SS |
1408 | } |
1409 | ||
1410 | /* Clear out the display_chain. | |
1411 | Done when new symtabs are loaded, since this invalidates | |
1412 | the types stored in many expressions. */ | |
1413 | ||
1414 | void | |
fba45db2 | 1415 | clear_displays (void) |
c906108c | 1416 | { |
52f0bd74 | 1417 | struct display *d; |
c906108c SS |
1418 | |
1419 | while ((d = display_chain) != NULL) | |
1420 | { | |
b8c9b27d | 1421 | xfree (d->exp); |
c906108c | 1422 | display_chain = d->next; |
b8c9b27d | 1423 | xfree (d); |
c906108c SS |
1424 | } |
1425 | } | |
1426 | ||
1427 | /* Delete the auto-display number NUM. */ | |
1428 | ||
1429 | static void | |
fba45db2 | 1430 | delete_display (int num) |
c906108c | 1431 | { |
52f0bd74 | 1432 | struct display *d1, *d; |
c906108c SS |
1433 | |
1434 | if (!display_chain) | |
1435 | error ("No display number %d.", num); | |
1436 | ||
1437 | if (display_chain->number == num) | |
1438 | { | |
1439 | d1 = display_chain; | |
1440 | display_chain = d1->next; | |
1441 | free_display (d1); | |
1442 | } | |
1443 | else | |
c5aa993b | 1444 | for (d = display_chain;; d = d->next) |
c906108c SS |
1445 | { |
1446 | if (d->next == 0) | |
1447 | error ("No display number %d.", num); | |
1448 | if (d->next->number == num) | |
1449 | { | |
1450 | d1 = d->next; | |
1451 | d->next = d1->next; | |
1452 | free_display (d1); | |
1453 | break; | |
1454 | } | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | /* Delete some values from the auto-display chain. | |
1459 | Specify the element numbers. */ | |
1460 | ||
1461 | static void | |
fba45db2 | 1462 | undisplay_command (char *args, int from_tty) |
c906108c | 1463 | { |
52f0bd74 AC |
1464 | char *p = args; |
1465 | char *p1; | |
1466 | int num; | |
c906108c SS |
1467 | |
1468 | if (args == 0) | |
1469 | { | |
1470 | if (query ("Delete all auto-display expressions? ")) | |
1471 | clear_displays (); | |
1472 | dont_repeat (); | |
1473 | return; | |
1474 | } | |
1475 | ||
1476 | while (*p) | |
1477 | { | |
1478 | p1 = p; | |
c5aa993b JM |
1479 | while (*p1 >= '0' && *p1 <= '9') |
1480 | p1++; | |
c906108c SS |
1481 | if (*p1 && *p1 != ' ' && *p1 != '\t') |
1482 | error ("Arguments must be display numbers."); | |
1483 | ||
1484 | num = atoi (p); | |
1485 | ||
1486 | delete_display (num); | |
1487 | ||
1488 | p = p1; | |
c5aa993b JM |
1489 | while (*p == ' ' || *p == '\t') |
1490 | p++; | |
c906108c SS |
1491 | } |
1492 | dont_repeat (); | |
1493 | } | |
1494 | ||
1495 | /* Display a single auto-display. | |
1496 | Do nothing if the display cannot be printed in the current context, | |
1497 | or if the display is disabled. */ | |
1498 | ||
1499 | static void | |
fba45db2 | 1500 | do_one_display (struct display *d) |
c906108c SS |
1501 | { |
1502 | int within_current_scope; | |
1503 | ||
b5de0fa7 | 1504 | if (d->enabled_p == 0) |
c906108c SS |
1505 | return; |
1506 | ||
1507 | if (d->block) | |
ae767bfb | 1508 | within_current_scope = contained_in (get_selected_block (0), d->block); |
c906108c SS |
1509 | else |
1510 | within_current_scope = 1; | |
1511 | if (!within_current_scope) | |
1512 | return; | |
1513 | ||
1514 | current_display_number = d->number; | |
1515 | ||
1516 | annotate_display_begin (); | |
1517 | printf_filtered ("%d", d->number); | |
1518 | annotate_display_number_end (); | |
1519 | printf_filtered (": "); | |
1520 | if (d->format.size) | |
1521 | { | |
1522 | CORE_ADDR addr; | |
3d6d86c6 | 1523 | struct value *val; |
c906108c SS |
1524 | |
1525 | annotate_display_format (); | |
1526 | ||
1527 | printf_filtered ("x/"); | |
1528 | if (d->format.count != 1) | |
1529 | printf_filtered ("%d", d->format.count); | |
1530 | printf_filtered ("%c", d->format.format); | |
1531 | if (d->format.format != 'i' && d->format.format != 's') | |
1532 | printf_filtered ("%c", d->format.size); | |
1533 | printf_filtered (" "); | |
1534 | ||
1535 | annotate_display_expression (); | |
1536 | ||
1537 | print_expression (d->exp, gdb_stdout); | |
1538 | annotate_display_expression_end (); | |
1539 | ||
1540 | if (d->format.count != 1) | |
1541 | printf_filtered ("\n"); | |
1542 | else | |
1543 | printf_filtered (" "); | |
c5aa993b | 1544 | |
c906108c | 1545 | val = evaluate_expression (d->exp); |
1aa20aa8 | 1546 | addr = value_as_address (val); |
c906108c SS |
1547 | if (d->format.format == 'i') |
1548 | addr = ADDR_BITS_REMOVE (addr); | |
1549 | ||
1550 | annotate_display_value (); | |
1551 | ||
1552 | do_examine (d->format, addr, VALUE_BFD_SECTION (val)); | |
1553 | } | |
1554 | else | |
1555 | { | |
1556 | annotate_display_format (); | |
1557 | ||
1558 | if (d->format.format) | |
1559 | printf_filtered ("/%c ", d->format.format); | |
1560 | ||
1561 | annotate_display_expression (); | |
1562 | ||
1563 | print_expression (d->exp, gdb_stdout); | |
1564 | annotate_display_expression_end (); | |
1565 | ||
1566 | printf_filtered (" = "); | |
1567 | ||
1568 | annotate_display_expression (); | |
1569 | ||
1570 | print_formatted (evaluate_expression (d->exp), | |
2acceee2 | 1571 | d->format.format, d->format.size, gdb_stdout); |
c906108c SS |
1572 | printf_filtered ("\n"); |
1573 | } | |
1574 | ||
1575 | annotate_display_end (); | |
1576 | ||
1577 | gdb_flush (gdb_stdout); | |
1578 | current_display_number = -1; | |
1579 | } | |
1580 | ||
1581 | /* Display all of the values on the auto-display chain which can be | |
1582 | evaluated in the current scope. */ | |
1583 | ||
1584 | void | |
fba45db2 | 1585 | do_displays (void) |
c906108c | 1586 | { |
52f0bd74 | 1587 | struct display *d; |
c906108c SS |
1588 | |
1589 | for (d = display_chain; d; d = d->next) | |
1590 | do_one_display (d); | |
1591 | } | |
1592 | ||
1593 | /* Delete the auto-display which we were in the process of displaying. | |
1594 | This is done when there is an error or a signal. */ | |
1595 | ||
1596 | void | |
fba45db2 | 1597 | disable_display (int num) |
c906108c | 1598 | { |
52f0bd74 | 1599 | struct display *d; |
c906108c SS |
1600 | |
1601 | for (d = display_chain; d; d = d->next) | |
1602 | if (d->number == num) | |
1603 | { | |
b5de0fa7 | 1604 | d->enabled_p = 0; |
c906108c SS |
1605 | return; |
1606 | } | |
1607 | printf_unfiltered ("No display number %d.\n", num); | |
1608 | } | |
c5aa993b | 1609 | |
c906108c | 1610 | void |
fba45db2 | 1611 | disable_current_display (void) |
c906108c SS |
1612 | { |
1613 | if (current_display_number >= 0) | |
1614 | { | |
1615 | disable_display (current_display_number); | |
1616 | fprintf_unfiltered (gdb_stderr, "Disabling display %d to avoid infinite recursion.\n", | |
c5aa993b | 1617 | current_display_number); |
c906108c SS |
1618 | } |
1619 | current_display_number = -1; | |
1620 | } | |
1621 | ||
1622 | static void | |
fba45db2 | 1623 | display_info (char *ignore, int from_tty) |
c906108c | 1624 | { |
52f0bd74 | 1625 | struct display *d; |
c906108c SS |
1626 | |
1627 | if (!display_chain) | |
1628 | printf_unfiltered ("There are no auto-display expressions now.\n"); | |
1629 | else | |
c5aa993b | 1630 | printf_filtered ("Auto-display expressions now in effect:\n\ |
c906108c SS |
1631 | Num Enb Expression\n"); |
1632 | ||
1633 | for (d = display_chain; d; d = d->next) | |
1634 | { | |
b5de0fa7 | 1635 | printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]); |
c906108c SS |
1636 | if (d->format.size) |
1637 | printf_filtered ("/%d%c%c ", d->format.count, d->format.size, | |
c5aa993b | 1638 | d->format.format); |
c906108c SS |
1639 | else if (d->format.format) |
1640 | printf_filtered ("/%c ", d->format.format); | |
1641 | print_expression (d->exp, gdb_stdout); | |
ae767bfb | 1642 | if (d->block && !contained_in (get_selected_block (0), d->block)) |
c906108c SS |
1643 | printf_filtered (" (cannot be evaluated in the current context)"); |
1644 | printf_filtered ("\n"); | |
1645 | gdb_flush (gdb_stdout); | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | static void | |
fba45db2 | 1650 | enable_display (char *args, int from_tty) |
c906108c | 1651 | { |
52f0bd74 AC |
1652 | char *p = args; |
1653 | char *p1; | |
1654 | int num; | |
1655 | struct display *d; | |
c906108c SS |
1656 | |
1657 | if (p == 0) | |
1658 | { | |
1659 | for (d = display_chain; d; d = d->next) | |
b5de0fa7 | 1660 | d->enabled_p = 1; |
c906108c SS |
1661 | } |
1662 | else | |
1663 | while (*p) | |
1664 | { | |
1665 | p1 = p; | |
1666 | while (*p1 >= '0' && *p1 <= '9') | |
1667 | p1++; | |
1668 | if (*p1 && *p1 != ' ' && *p1 != '\t') | |
1669 | error ("Arguments must be display numbers."); | |
c5aa993b | 1670 | |
c906108c | 1671 | num = atoi (p); |
c5aa993b | 1672 | |
c906108c SS |
1673 | for (d = display_chain; d; d = d->next) |
1674 | if (d->number == num) | |
1675 | { | |
b5de0fa7 | 1676 | d->enabled_p = 1; |
c906108c SS |
1677 | goto win; |
1678 | } | |
1679 | printf_unfiltered ("No display number %d.\n", num); | |
1680 | win: | |
1681 | p = p1; | |
1682 | while (*p == ' ' || *p == '\t') | |
1683 | p++; | |
1684 | } | |
1685 | } | |
1686 | ||
c906108c | 1687 | static void |
fba45db2 | 1688 | disable_display_command (char *args, int from_tty) |
c906108c | 1689 | { |
52f0bd74 AC |
1690 | char *p = args; |
1691 | char *p1; | |
1692 | struct display *d; | |
c906108c SS |
1693 | |
1694 | if (p == 0) | |
1695 | { | |
1696 | for (d = display_chain; d; d = d->next) | |
b5de0fa7 | 1697 | d->enabled_p = 0; |
c906108c SS |
1698 | } |
1699 | else | |
1700 | while (*p) | |
1701 | { | |
1702 | p1 = p; | |
1703 | while (*p1 >= '0' && *p1 <= '9') | |
1704 | p1++; | |
1705 | if (*p1 && *p1 != ' ' && *p1 != '\t') | |
1706 | error ("Arguments must be display numbers."); | |
c5aa993b | 1707 | |
c906108c SS |
1708 | disable_display (atoi (p)); |
1709 | ||
1710 | p = p1; | |
1711 | while (*p == ' ' || *p == '\t') | |
1712 | p++; | |
1713 | } | |
1714 | } | |
c906108c | 1715 | \f |
c5aa993b | 1716 | |
c906108c SS |
1717 | /* Print the value in stack frame FRAME of a variable |
1718 | specified by a struct symbol. */ | |
1719 | ||
1720 | void | |
fba45db2 KB |
1721 | print_variable_value (struct symbol *var, struct frame_info *frame, |
1722 | struct ui_file *stream) | |
c906108c | 1723 | { |
3d6d86c6 | 1724 | struct value *val = read_var_value (var, frame); |
c906108c SS |
1725 | |
1726 | value_print (val, stream, 0, Val_pretty_default); | |
1727 | } | |
1728 | ||
c906108c | 1729 | static void |
fba45db2 | 1730 | printf_command (char *arg, int from_tty) |
c906108c | 1731 | { |
52f0bd74 AC |
1732 | char *f = NULL; |
1733 | char *s = arg; | |
c906108c | 1734 | char *string = NULL; |
3d6d86c6 | 1735 | struct value **val_args; |
c906108c SS |
1736 | char *substrings; |
1737 | char *current_substring; | |
1738 | int nargs = 0; | |
1739 | int allocated_args = 20; | |
1740 | struct cleanup *old_cleanups; | |
1741 | ||
f976f6d4 AC |
1742 | val_args = (struct value **) xmalloc (allocated_args |
1743 | * sizeof (struct value *)); | |
c13c43fd | 1744 | old_cleanups = make_cleanup (free_current_contents, &val_args); |
c906108c SS |
1745 | |
1746 | if (s == 0) | |
1747 | error_no_arg ("format-control string and values to print"); | |
1748 | ||
1749 | /* Skip white space before format string */ | |
c5aa993b JM |
1750 | while (*s == ' ' || *s == '\t') |
1751 | s++; | |
c906108c SS |
1752 | |
1753 | /* A format string should follow, enveloped in double quotes */ | |
1754 | if (*s++ != '"') | |
1755 | error ("Bad format string, missing '\"'."); | |
1756 | ||
1757 | /* Parse the format-control string and copy it into the string STRING, | |
1758 | processing some kinds of escape sequence. */ | |
1759 | ||
1760 | f = string = (char *) alloca (strlen (s) + 1); | |
1761 | ||
1762 | while (*s != '"') | |
1763 | { | |
1764 | int c = *s++; | |
1765 | switch (c) | |
1766 | { | |
1767 | case '\0': | |
1768 | error ("Bad format string, non-terminated '\"'."); | |
1769 | ||
1770 | case '\\': | |
1771 | switch (c = *s++) | |
1772 | { | |
1773 | case '\\': | |
1774 | *f++ = '\\'; | |
1775 | break; | |
1776 | case 'a': | |
c906108c | 1777 | *f++ = '\a'; |
c906108c SS |
1778 | break; |
1779 | case 'b': | |
1780 | *f++ = '\b'; | |
1781 | break; | |
1782 | case 'f': | |
1783 | *f++ = '\f'; | |
1784 | break; | |
1785 | case 'n': | |
1786 | *f++ = '\n'; | |
1787 | break; | |
1788 | case 'r': | |
1789 | *f++ = '\r'; | |
1790 | break; | |
1791 | case 't': | |
1792 | *f++ = '\t'; | |
1793 | break; | |
1794 | case 'v': | |
1795 | *f++ = '\v'; | |
1796 | break; | |
1797 | case '"': | |
1798 | *f++ = '"'; | |
1799 | break; | |
1800 | default: | |
1801 | /* ??? TODO: handle other escape sequences */ | |
1802 | error ("Unrecognized escape character \\%c in format string.", | |
1803 | c); | |
1804 | } | |
1805 | break; | |
1806 | ||
1807 | default: | |
1808 | *f++ = c; | |
1809 | } | |
1810 | } | |
1811 | ||
1812 | /* Skip over " and following space and comma. */ | |
1813 | s++; | |
1814 | *f++ = '\0'; | |
c5aa993b JM |
1815 | while (*s == ' ' || *s == '\t') |
1816 | s++; | |
c906108c SS |
1817 | |
1818 | if (*s != ',' && *s != 0) | |
1819 | error ("Invalid argument syntax"); | |
1820 | ||
c5aa993b JM |
1821 | if (*s == ',') |
1822 | s++; | |
1823 | while (*s == ' ' || *s == '\t') | |
1824 | s++; | |
c906108c SS |
1825 | |
1826 | /* Need extra space for the '\0's. Doubling the size is sufficient. */ | |
1827 | substrings = alloca (strlen (string) * 2); | |
1828 | current_substring = substrings; | |
1829 | ||
1830 | { | |
1831 | /* Now scan the string for %-specs and see what kinds of args they want. | |
1832 | argclass[I] classifies the %-specs so we can give printf_filtered | |
1833 | something of the right size. */ | |
1834 | ||
c5aa993b JM |
1835 | enum argclass |
1836 | { | |
1837 | no_arg, int_arg, string_arg, double_arg, long_long_arg | |
1838 | }; | |
c906108c SS |
1839 | enum argclass *argclass; |
1840 | enum argclass this_argclass; | |
1841 | char *last_arg; | |
1842 | int nargs_wanted; | |
1843 | int lcount; | |
1844 | int i; | |
1845 | ||
1846 | argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass); | |
1847 | nargs_wanted = 0; | |
1848 | f = string; | |
1849 | last_arg = string; | |
1850 | while (*f) | |
1851 | if (*f++ == '%') | |
1852 | { | |
1853 | lcount = 0; | |
c5aa993b | 1854 | while (strchr ("0123456789.hlL-+ #", *f)) |
c906108c SS |
1855 | { |
1856 | if (*f == 'l' || *f == 'L') | |
1857 | lcount++; | |
1858 | f++; | |
1859 | } | |
1860 | switch (*f) | |
1861 | { | |
1862 | case 's': | |
1863 | this_argclass = string_arg; | |
1864 | break; | |
1865 | ||
1866 | case 'e': | |
1867 | case 'f': | |
1868 | case 'g': | |
1869 | this_argclass = double_arg; | |
1870 | break; | |
1871 | ||
1872 | case '*': | |
1873 | error ("`*' not supported for precision or width in printf"); | |
1874 | ||
1875 | case 'n': | |
1876 | error ("Format specifier `n' not supported in printf"); | |
1877 | ||
1878 | case '%': | |
1879 | this_argclass = no_arg; | |
1880 | break; | |
1881 | ||
1882 | default: | |
1883 | if (lcount > 1) | |
1884 | this_argclass = long_long_arg; | |
1885 | else | |
1886 | this_argclass = int_arg; | |
1887 | break; | |
1888 | } | |
1889 | f++; | |
1890 | if (this_argclass != no_arg) | |
1891 | { | |
1892 | strncpy (current_substring, last_arg, f - last_arg); | |
1893 | current_substring += f - last_arg; | |
1894 | *current_substring++ = '\0'; | |
1895 | last_arg = f; | |
1896 | argclass[nargs_wanted++] = this_argclass; | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | /* Now, parse all arguments and evaluate them. | |
1901 | Store the VALUEs in VAL_ARGS. */ | |
1902 | ||
1903 | while (*s != '\0') | |
1904 | { | |
1905 | char *s1; | |
1906 | if (nargs == allocated_args) | |
f976f6d4 AC |
1907 | val_args = (struct value **) xrealloc ((char *) val_args, |
1908 | (allocated_args *= 2) | |
1909 | * sizeof (struct value *)); | |
c906108c SS |
1910 | s1 = s; |
1911 | val_args[nargs] = parse_to_comma_and_eval (&s1); | |
c5aa993b | 1912 | |
c906108c SS |
1913 | /* If format string wants a float, unchecked-convert the value to |
1914 | floating point of the same size */ | |
c5aa993b | 1915 | |
c906108c SS |
1916 | if (argclass[nargs] == double_arg) |
1917 | { | |
1918 | struct type *type = VALUE_TYPE (val_args[nargs]); | |
1919 | if (TYPE_LENGTH (type) == sizeof (float)) | |
c5aa993b | 1920 | VALUE_TYPE (val_args[nargs]) = builtin_type_float; |
c906108c | 1921 | if (TYPE_LENGTH (type) == sizeof (double)) |
c5aa993b | 1922 | VALUE_TYPE (val_args[nargs]) = builtin_type_double; |
c906108c SS |
1923 | } |
1924 | nargs++; | |
1925 | s = s1; | |
1926 | if (*s == ',') | |
1927 | s++; | |
1928 | } | |
c5aa993b | 1929 | |
c906108c SS |
1930 | if (nargs != nargs_wanted) |
1931 | error ("Wrong number of arguments for specified format-string"); | |
1932 | ||
1933 | /* Now actually print them. */ | |
1934 | current_substring = substrings; | |
1935 | for (i = 0; i < nargs; i++) | |
1936 | { | |
1937 | switch (argclass[i]) | |
1938 | { | |
1939 | case string_arg: | |
1940 | { | |
1941 | char *str; | |
1942 | CORE_ADDR tem; | |
1943 | int j; | |
1aa20aa8 | 1944 | tem = value_as_address (val_args[i]); |
c906108c SS |
1945 | |
1946 | /* This is a %s argument. Find the length of the string. */ | |
c5aa993b | 1947 | for (j = 0;; j++) |
c906108c SS |
1948 | { |
1949 | char c; | |
1950 | QUIT; | |
d4b2399a | 1951 | read_memory (tem + j, &c, 1); |
c906108c SS |
1952 | if (c == 0) |
1953 | break; | |
1954 | } | |
1955 | ||
1956 | /* Copy the string contents into a string inside GDB. */ | |
1957 | str = (char *) alloca (j + 1); | |
7b92f6e1 MS |
1958 | if (j != 0) |
1959 | read_memory (tem, str, j); | |
c906108c SS |
1960 | str[j] = 0; |
1961 | ||
1962 | printf_filtered (current_substring, str); | |
1963 | } | |
1964 | break; | |
1965 | case double_arg: | |
1966 | { | |
1967 | double val = value_as_double (val_args[i]); | |
1968 | printf_filtered (current_substring, val); | |
1969 | break; | |
1970 | } | |
1971 | case long_long_arg: | |
1972 | #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG) | |
1973 | { | |
1974 | long long val = value_as_long (val_args[i]); | |
1975 | printf_filtered (current_substring, val); | |
1976 | break; | |
1977 | } | |
1978 | #else | |
1979 | error ("long long not supported in printf"); | |
1980 | #endif | |
1981 | case int_arg: | |
1982 | { | |
1983 | /* FIXME: there should be separate int_arg and long_arg. */ | |
1984 | long val = value_as_long (val_args[i]); | |
1985 | printf_filtered (current_substring, val); | |
1986 | break; | |
1987 | } | |
c5aa993b JM |
1988 | default: /* purecov: deadcode */ |
1989 | error ("internal error in printf_command"); /* purecov: deadcode */ | |
c906108c SS |
1990 | } |
1991 | /* Skip to the next substring. */ | |
1992 | current_substring += strlen (current_substring) + 1; | |
1993 | } | |
1994 | /* Print the portion of the format string after the last argument. */ | |
306d9ac5 | 1995 | puts_filtered (last_arg); |
c906108c SS |
1996 | } |
1997 | do_cleanups (old_cleanups); | |
1998 | } | |
c906108c | 1999 | |
c906108c | 2000 | void |
fba45db2 | 2001 | _initialize_printcmd (void) |
c906108c | 2002 | { |
c94fdfd0 EZ |
2003 | struct cmd_list_element *c; |
2004 | ||
c906108c SS |
2005 | current_display_number = -1; |
2006 | ||
2007 | add_info ("address", address_info, | |
c5aa993b | 2008 | "Describe where symbol SYM is stored."); |
c906108c | 2009 | |
c5aa993b | 2010 | add_info ("symbol", sym_info, |
c906108c SS |
2011 | "Describe what symbol is at location ADDR.\n\ |
2012 | Only for symbols with fixed locations (global or static scope)."); | |
2013 | ||
2014 | add_com ("x", class_vars, x_command, | |
2015 | concat ("Examine memory: x/FMT ADDRESS.\n\ | |
2016 | ADDRESS is an expression for the memory address to examine.\n\ | |
2017 | FMT is a repeat count followed by a format letter and a size letter.\n\ | |
2018 | Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\ | |
2019 | t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n", | |
c5aa993b | 2020 | "Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\ |
c906108c SS |
2021 | The specified number of objects of the specified size are printed\n\ |
2022 | according to the format.\n\n\ | |
2023 | Defaults for format and size letters are those previously used.\n\ | |
2024 | Default count is 1. Default address is following last thing printed\n\ | |
2025 | with this command or \"print\".", NULL)); | |
2026 | ||
c906108c SS |
2027 | #if 0 |
2028 | add_com ("whereis", class_vars, whereis_command, | |
2029 | "Print line number and file of definition of variable."); | |
2030 | #endif | |
c5aa993b | 2031 | |
c906108c SS |
2032 | add_info ("display", display_info, |
2033 | "Expressions to display when program stops, with code numbers."); | |
2034 | ||
2035 | add_cmd ("undisplay", class_vars, undisplay_command, | |
2036 | "Cancel some expressions to be displayed when program stops.\n\ | |
2037 | Arguments are the code numbers of the expressions to stop displaying.\n\ | |
2038 | No argument means cancel all automatic-display expressions.\n\ | |
2039 | \"delete display\" has the same effect as this command.\n\ | |
2040 | Do \"info display\" to see current list of code numbers.", | |
c5aa993b | 2041 | &cmdlist); |
c906108c SS |
2042 | |
2043 | add_com ("display", class_vars, display_command, | |
2044 | "Print value of expression EXP each time the program stops.\n\ | |
2045 | /FMT may be used before EXP as in the \"print\" command.\n\ | |
2046 | /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\ | |
2047 | as in the \"x\" command, and then EXP is used to get the address to examine\n\ | |
2048 | and examining is done as in the \"x\" command.\n\n\ | |
2049 | With no argument, display all currently requested auto-display expressions.\n\ | |
2050 | Use \"undisplay\" to cancel display requests previously made." | |
c5aa993b | 2051 | ); |
c906108c | 2052 | |
c5aa993b | 2053 | add_cmd ("display", class_vars, enable_display, |
c906108c SS |
2054 | "Enable some expressions to be displayed when program stops.\n\ |
2055 | Arguments are the code numbers of the expressions to resume displaying.\n\ | |
2056 | No argument means enable all automatic-display expressions.\n\ | |
2057 | Do \"info display\" to see current list of code numbers.", &enablelist); | |
2058 | ||
c5aa993b | 2059 | add_cmd ("display", class_vars, disable_display_command, |
c906108c SS |
2060 | "Disable some expressions to be displayed when program stops.\n\ |
2061 | Arguments are the code numbers of the expressions to stop displaying.\n\ | |
2062 | No argument means disable all automatic-display expressions.\n\ | |
2063 | Do \"info display\" to see current list of code numbers.", &disablelist); | |
2064 | ||
c5aa993b | 2065 | add_cmd ("display", class_vars, undisplay_command, |
c906108c SS |
2066 | "Cancel some expressions to be displayed when program stops.\n\ |
2067 | Arguments are the code numbers of the expressions to stop displaying.\n\ | |
2068 | No argument means cancel all automatic-display expressions.\n\ | |
2069 | Do \"info display\" to see current list of code numbers.", &deletelist); | |
2070 | ||
2071 | add_com ("printf", class_vars, printf_command, | |
c5aa993b | 2072 | "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\ |
c906108c SS |
2073 | This is useful for formatted output in user-defined commands."); |
2074 | ||
2075 | add_com ("output", class_vars, output_command, | |
2076 | "Like \"print\" but don't put in value history and don't print newline.\n\ | |
2077 | This is useful in user-defined commands."); | |
2078 | ||
2079 | add_prefix_cmd ("set", class_vars, set_command, | |
c5aa993b | 2080 | concat ("Evaluate expression EXP and assign result to variable VAR, using assignment\n\ |
c906108c SS |
2081 | syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\ |
2082 | example). VAR may be a debugger \"convenience\" variable (names starting\n\ | |
2083 | with $), a register (a few standard names starting with $), or an actual\n\ | |
2084 | variable in the program being debugged. EXP is any valid expression.\n", | |
c5aa993b | 2085 | "Use \"set variable\" for variables with names identical to set subcommands.\n\ |
c906108c SS |
2086 | \nWith a subcommand, this command modifies parts of the gdb environment.\n\ |
2087 | You can see these environment settings with the \"show\" command.", NULL), | |
c5aa993b | 2088 | &setlist, "set ", 1, &cmdlist); |
c906108c | 2089 | if (dbx_commands) |
c5aa993b | 2090 | add_com ("assign", class_vars, set_command, concat ("Evaluate expression \ |
c906108c SS |
2091 | EXP and assign result to variable VAR, using assignment\n\ |
2092 | syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\ | |
2093 | example). VAR may be a debugger \"convenience\" variable (names starting\n\ | |
2094 | with $), a register (a few standard names starting with $), or an actual\n\ | |
2095 | variable in the program being debugged. EXP is any valid expression.\n", | |
c5aa993b | 2096 | "Use \"set variable\" for variables with names identical to set subcommands.\n\ |
c906108c SS |
2097 | \nWith a subcommand, this command modifies parts of the gdb environment.\n\ |
2098 | You can see these environment settings with the \"show\" command.", NULL)); | |
2099 | ||
2100 | /* "call" is the same as "set", but handy for dbx users to call fns. */ | |
c94fdfd0 EZ |
2101 | c = add_com ("call", class_vars, call_command, |
2102 | "Call a function in the program.\n\ | |
c906108c SS |
2103 | The argument is the function name and arguments, in the notation of the\n\ |
2104 | current working language. The result is printed and saved in the value\n\ | |
2105 | history, if it is not void."); | |
5ba2abeb | 2106 | set_cmd_completer (c, location_completer); |
c906108c SS |
2107 | |
2108 | add_cmd ("variable", class_vars, set_command, | |
c5aa993b | 2109 | "Evaluate expression EXP and assign result to variable VAR, using assignment\n\ |
c906108c SS |
2110 | syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\ |
2111 | example). VAR may be a debugger \"convenience\" variable (names starting\n\ | |
2112 | with $), a register (a few standard names starting with $), or an actual\n\ | |
2113 | variable in the program being debugged. EXP is any valid expression.\n\ | |
2114 | This may usually be abbreviated to simply \"set\".", | |
c5aa993b | 2115 | &setlist); |
c906108c | 2116 | |
c94fdfd0 | 2117 | c = add_com ("print", class_vars, print_command, |
c906108c SS |
2118 | concat ("Print value of expression EXP.\n\ |
2119 | Variables accessible are those of the lexical environment of the selected\n\ | |
2120 | stack frame, plus all those whose scope is global or an entire file.\n\ | |
2121 | \n\ | |
2122 | $NUM gets previous value number NUM. $ and $$ are the last two values.\n\ | |
2123 | $$NUM refers to NUM'th value back from the last one.\n\ | |
2124 | Names starting with $ refer to registers (with the values they would have\n", | |
c5aa993b | 2125 | "if the program were to return to the stack frame now selected, restoring\n\ |
c906108c SS |
2126 | all registers saved by frames farther in) or else to debugger\n\ |
2127 | \"convenience\" variables (any such name not a known register).\n\ | |
2128 | Use assignment expressions to give values to convenience variables.\n", | |
2129 | "\n\ | |
2130 | {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\ | |
2131 | @ is a binary operator for treating consecutive data objects\n\ | |
2132 | anywhere in memory as an array. FOO@NUM gives an array whose first\n\ | |
2133 | element is FOO, whose second element is stored in the space following\n\ | |
2134 | where FOO is stored, etc. FOO must be an expression whose value\n\ | |
2135 | resides in memory.\n", | |
2136 | "\n\ | |
2137 | EXP may be preceded with /FMT, where FMT is a format letter\n\ | |
2138 | but no count or size letter (see \"x\" command).", NULL)); | |
5ba2abeb | 2139 | set_cmd_completer (c, location_completer); |
c906108c SS |
2140 | add_com_alias ("p", "print", class_vars, 1); |
2141 | ||
c94fdfd0 | 2142 | c = add_com ("inspect", class_vars, inspect_command, |
c5aa993b | 2143 | "Same as \"print\" command, except that if you are running in the epoch\n\ |
c906108c | 2144 | environment, the value is printed in its own window."); |
5ba2abeb | 2145 | set_cmd_completer (c, location_completer); |
c906108c | 2146 | |
cb1a6d5f AC |
2147 | deprecated_add_show_from_set |
2148 | (add_set_cmd ("max-symbolic-offset", no_class, var_uinteger, | |
2149 | (char *) &max_symbolic_offset, | |
c5aa993b | 2150 | "Set the largest offset that will be printed in <symbol+1234> form.", |
cb1a6d5f AC |
2151 | &setprintlist), |
2152 | &showprintlist); | |
2153 | deprecated_add_show_from_set | |
2154 | (add_set_cmd ("symbol-filename", no_class, var_boolean, | |
2155 | (char *) &print_symbol_filename, "\ | |
2156 | Set printing of source filename and line number with <symbol>.", | |
2157 | &setprintlist), | |
2158 | &showprintlist); | |
c906108c SS |
2159 | |
2160 | /* For examine/instruction a single byte quantity is specified as | |
2161 | the data. This avoids problems with value_at_lazy() requiring a | |
2162 | valid data type (and rejecting VOID). */ | |
2163 | examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL); | |
2164 | ||
2165 | examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL); | |
2166 | examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL); | |
2167 | examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL); | |
2168 | examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL); | |
2169 | ||
2170 | } |