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