1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
29 #include "expression.h"
33 #include "breakpoint.h"
37 #include "symfile.h" /* for overlay functions */
38 #include "objfiles.h" /* ditto */
39 #include "completer.h" /* for completion functions */
41 #include "gdb_assert.h"
46 #include "exceptions.h"
49 #include "parser-defs.h"
51 #include "arch-utils.h"
55 #include "tui/tui.h" /* For tui_active et al. */
58 #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
59 # define USE_PRINTF_I64 1
60 # define PRINTF_HAS_LONG_LONG
62 # define USE_PRINTF_I64 0
65 extern int asm_demangle; /* Whether to demangle syms in asm
74 /* True if the value should be printed raw -- that is, bypassing
75 python-based formatters. */
79 /* Last specified output format. */
81 static char last_format = 0;
83 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
85 static char last_size = 'w';
87 /* Default address to examine next, and associated architecture. */
89 static struct gdbarch *next_gdbarch;
90 static CORE_ADDR next_address;
92 /* Number of delay instructions following current disassembled insn. */
94 static int branch_delay_insns;
96 /* Last address examined. */
98 static CORE_ADDR last_examine_address;
100 /* Contents of last address examined.
101 This is not valid past the end of the `x' command! */
103 static struct value *last_examine_value;
105 /* Largest offset between a symbolic value and an address, that will be
106 printed as `0x1234 <symbol+offset>'. */
108 static unsigned int max_symbolic_offset = UINT_MAX;
110 show_max_symbolic_offset (struct ui_file *file, int from_tty,
111 struct cmd_list_element *c, const char *value)
113 fprintf_filtered (file,
114 _("The largest offset that will be "
115 "printed in <symbol+1234> form is %s.\n"),
119 /* Append the source filename and linenumber of the symbol when
120 printing a symbolic value as `<symbol at filename:linenum>' if set. */
121 static int print_symbol_filename = 0;
123 show_print_symbol_filename (struct ui_file *file, int from_tty,
124 struct cmd_list_element *c, const char *value)
126 fprintf_filtered (file, _("Printing of source filename and "
127 "line number with <symbol> is %s.\n"),
131 /* Number of auto-display expression currently being displayed.
132 So that we can disable it if we get an error or a signal within it.
133 -1 when not doing one. */
135 int current_display_number;
139 /* Chain link to next auto-display item. */
140 struct display *next;
142 /* The expression as the user typed it. */
145 /* Expression to be evaluated and displayed. */
146 struct expression *exp;
148 /* Item number of this auto-display item. */
151 /* Display format specified. */
152 struct format_data format;
154 /* Program space associated with `block'. */
155 struct program_space *pspace;
157 /* Innermost block required by this expression when evaluated. */
160 /* Status of this display (enabled or disabled). */
164 /* Chain of expressions whose values should be displayed
165 automatically each time the program stops. */
167 static struct display *display_chain;
169 static int display_number;
171 /* Walk the following statement or block through all displays. */
173 #define ALL_DISPLAYS(B) \
174 for (B = display_chain; B; B = B->next)
176 /* Prototypes for exported functions. */
178 void output_command (char *, int);
180 void _initialize_printcmd (void);
182 /* Prototypes for local functions. */
184 static void do_one_display (struct display *);
187 /* Decode a format specification. *STRING_PTR should point to it.
188 OFORMAT and OSIZE are used as defaults for the format and size
189 if none are given in the format specification.
190 If OSIZE is zero, then the size field of the returned value
191 should be set only if a size is explicitly specified by the
193 The structure returned describes all the data
194 found in the specification. In addition, *STRING_PTR is advanced
195 past the specification and past all whitespace following it. */
197 static struct format_data
198 decode_format (char **string_ptr, int oformat, int osize)
200 struct format_data val;
201 char *p = *string_ptr;
208 if (*p >= '0' && *p <= '9')
209 val.count = atoi (p);
210 while (*p >= '0' && *p <= '9')
213 /* Now process size or format letters that follow. */
217 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
224 else if (*p >= 'a' && *p <= 'z')
230 while (*p == ' ' || *p == '\t')
234 /* Set defaults for format and size if not specified. */
235 if (val.format == '?')
239 /* Neither has been specified. */
240 val.format = oformat;
244 /* If a size is specified, any format makes a reasonable
245 default except 'i'. */
246 val.format = oformat == 'i' ? 'x' : oformat;
248 else if (val.size == '?')
252 /* Pick the appropriate size for an address. This is deferred
253 until do_examine when we know the actual architecture to use.
254 A special size value of 'a' is used to indicate this case. */
255 val.size = osize ? 'a' : osize;
258 /* Floating point has to be word or giantword. */
259 if (osize == 'w' || osize == 'g')
262 /* Default it to giantword if the last used size is not
264 val.size = osize ? 'g' : osize;
267 /* Characters default to one byte. */
268 val.size = osize ? 'b' : osize;
271 /* Display strings with byte size chars unless explicitly
277 /* The default is the size most recently specified. */
284 /* Print value VAL on stream according to OPTIONS.
285 Do not end with a newline.
286 SIZE is the letter for the size of datum being printed.
287 This is used to pad hex numbers so they line up. SIZE is 0
288 for print / output and set for examine. */
291 print_formatted (struct value *val, int size,
292 const struct value_print_options *options,
293 struct ui_file *stream)
295 struct type *type = check_typedef (value_type (val));
296 int len = TYPE_LENGTH (type);
298 if (VALUE_LVAL (val) == lval_memory)
299 next_address = value_address (val) + len;
303 switch (options->format)
307 struct type *elttype = value_type (val);
309 next_address = (value_address (val)
310 + val_print_string (elttype, NULL,
311 value_address (val), -1,
312 stream, options) * len);
317 /* We often wrap here if there are long symbolic names. */
319 next_address = (value_address (val)
320 + gdb_print_insn (get_type_arch (type),
321 value_address (val), stream,
322 &branch_delay_insns));
327 if (options->format == 0 || options->format == 's'
328 || TYPE_CODE (type) == TYPE_CODE_REF
329 || TYPE_CODE (type) == TYPE_CODE_ARRAY
330 || TYPE_CODE (type) == TYPE_CODE_STRING
331 || TYPE_CODE (type) == TYPE_CODE_STRUCT
332 || TYPE_CODE (type) == TYPE_CODE_UNION
333 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
334 value_print (val, stream, options);
336 /* User specified format, so don't look to the the type to
337 tell us what to do. */
338 val_print_scalar_formatted (type,
339 value_contents_for_printing (val),
340 value_embedded_offset (val),
342 options, size, stream);
345 /* Return builtin floating point type of same length as TYPE.
346 If no such type is found, return TYPE itself. */
348 float_type_from_length (struct type *type)
350 struct gdbarch *gdbarch = get_type_arch (type);
351 const struct builtin_type *builtin = builtin_type (gdbarch);
352 unsigned int len = TYPE_LENGTH (type);
354 if (len == TYPE_LENGTH (builtin->builtin_float))
355 type = builtin->builtin_float;
356 else if (len == TYPE_LENGTH (builtin->builtin_double))
357 type = builtin->builtin_double;
358 else if (len == TYPE_LENGTH (builtin->builtin_long_double))
359 type = builtin->builtin_long_double;
364 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
365 according to OPTIONS and SIZE on STREAM. Formats s and i are not
366 supported at this level. */
369 print_scalar_formatted (const void *valaddr, struct type *type,
370 const struct value_print_options *options,
371 int size, struct ui_file *stream)
373 struct gdbarch *gdbarch = get_type_arch (type);
374 LONGEST val_long = 0;
375 unsigned int len = TYPE_LENGTH (type);
376 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
378 /* String printing should go through val_print_scalar_formatted. */
379 gdb_assert (options->format != 's');
381 if (len > sizeof(LONGEST) &&
382 (TYPE_CODE (type) == TYPE_CODE_INT
383 || TYPE_CODE (type) == TYPE_CODE_ENUM))
385 switch (options->format)
388 print_octal_chars (stream, valaddr, len, byte_order);
392 print_decimal_chars (stream, valaddr, len, byte_order);
395 print_binary_chars (stream, valaddr, len, byte_order);
398 print_hex_chars (stream, valaddr, len, byte_order);
401 print_char_chars (stream, type, valaddr, len, byte_order);
408 if (options->format != 'f')
409 val_long = unpack_long (type, valaddr);
411 /* If the value is a pointer, and pointers and addresses are not the
412 same, then at this point, the value's length (in target bytes) is
413 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
414 if (TYPE_CODE (type) == TYPE_CODE_PTR)
415 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
417 /* If we are printing it as unsigned, truncate it in case it is actually
418 a negative signed value (e.g. "print/u (short)-1" should print 65535
419 (if shorts are 16 bits) instead of 4294967295). */
420 if (options->format != 'd' || TYPE_UNSIGNED (type))
422 if (len < sizeof (LONGEST))
423 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
426 switch (options->format)
431 /* No size specified, like in print. Print varying # of digits. */
432 print_longest (stream, 'x', 1, val_long);
441 print_longest (stream, size, 1, val_long);
444 error (_("Undefined output size \"%c\"."), size);
449 print_longest (stream, 'd', 1, val_long);
453 print_longest (stream, 'u', 0, val_long);
458 print_longest (stream, 'o', 1, val_long);
460 fprintf_filtered (stream, "0");
465 CORE_ADDR addr = unpack_pointer (type, valaddr);
467 print_address (gdbarch, addr, stream);
473 struct value_print_options opts = *options;
476 if (TYPE_UNSIGNED (type))
477 type = builtin_type (gdbarch)->builtin_true_unsigned_char;
479 type = builtin_type (gdbarch)->builtin_true_char;
481 value_print (value_from_longest (type, val_long), stream, &opts);
486 type = float_type_from_length (type);
487 print_floating (valaddr, type, stream);
491 internal_error (__FILE__, __LINE__,
492 _("failed internal consistency check"));
495 /* Binary; 't' stands for "two". */
497 char bits[8 * (sizeof val_long) + 1];
498 char buf[8 * (sizeof val_long) + 32];
503 width = 8 * (sizeof val_long);
520 error (_("Undefined output size \"%c\"."), size);
526 bits[width] = (val_long & 1) ? '1' : '0';
531 while (*cp && *cp == '0')
537 fputs_filtered (buf, stream);
542 error (_("Undefined output format \"%c\"."), options->format);
546 /* Specify default address for `x' command.
547 The `info lines' command uses this. */
550 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
552 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
554 next_gdbarch = gdbarch;
557 /* Make address available to the user as $_. */
558 set_internalvar (lookup_internalvar ("_"),
559 value_from_pointer (ptr_type, addr));
562 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
563 after LEADIN. Print nothing if no symbolic name is found nearby.
564 Optionally also print source file and line number, if available.
565 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
566 or to interpret it as a possible C++ name and convert it back to source
567 form. However note that DO_DEMANGLE can be overridden by the specific
568 settings of the demangle and asm_demangle variables. */
571 print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
572 struct ui_file *stream,
573 int do_demangle, char *leadin)
576 char *filename = NULL;
581 /* Throw away both name and filename. */
582 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
583 make_cleanup (free_current_contents, &filename);
585 if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
586 &filename, &line, &unmapped))
588 do_cleanups (cleanup_chain);
592 fputs_filtered (leadin, stream);
594 fputs_filtered ("<*", stream);
596 fputs_filtered ("<", stream);
597 fputs_filtered (name, stream);
599 fprintf_filtered (stream, "+%u", (unsigned int) offset);
601 /* Append source filename and line number if desired. Give specific
602 line # of this addr, if we have it; else line # of the nearest symbol. */
603 if (print_symbol_filename && filename != NULL)
606 fprintf_filtered (stream, " at %s:%d", filename, line);
608 fprintf_filtered (stream, " in %s", filename);
611 fputs_filtered ("*>", stream);
613 fputs_filtered (">", stream);
615 do_cleanups (cleanup_chain);
618 /* Given an address ADDR return all the elements needed to print the
619 address in a symbolic form. NAME can be mangled or not depending
620 on DO_DEMANGLE (and also on the asm_demangle global variable,
621 manipulated via ''set print asm-demangle''). Return 0 in case of
622 success, when all the info in the OUT paramters is valid. Return 1
625 build_address_symbolic (struct gdbarch *gdbarch,
626 CORE_ADDR addr, /* IN */
627 int do_demangle, /* IN */
628 char **name, /* OUT */
629 int *offset, /* OUT */
630 char **filename, /* OUT */
632 int *unmapped) /* OUT */
634 struct minimal_symbol *msymbol;
635 struct symbol *symbol;
636 CORE_ADDR name_location = 0;
637 struct obj_section *section = NULL;
638 char *name_temp = "";
640 /* Let's say it is mapped (not unmapped). */
643 /* Determine if the address is in an overlay, and whether it is
645 if (overlay_debugging)
647 section = find_pc_overlay (addr);
648 if (pc_in_unmapped_range (addr, section))
651 addr = overlay_mapped_address (addr, section);
655 /* First try to find the address in the symbol table, then
656 in the minsyms. Take the closest one. */
658 /* This is defective in the sense that it only finds text symbols. So
659 really this is kind of pointless--we should make sure that the
660 minimal symbols have everything we need (by changing that we could
661 save some memory, but for many debug format--ELF/DWARF or
662 anything/stabs--it would be inconvenient to eliminate those minimal
664 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
665 symbol = find_pc_sect_function (addr, section);
669 /* If this is a function (i.e. a code address), strip out any
670 non-address bits. For instance, display a pointer to the
671 first instruction of a Thumb function as <function>; the
672 second instruction will be <function+2>, even though the
673 pointer is <function+3>. This matches the ISA behavior. */
674 addr = gdbarch_addr_bits_remove (gdbarch, addr);
676 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
677 if (do_demangle || asm_demangle)
678 name_temp = SYMBOL_PRINT_NAME (symbol);
680 name_temp = SYMBOL_LINKAGE_NAME (symbol);
685 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
687 /* The msymbol is closer to the address than the symbol;
688 use the msymbol instead. */
690 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
691 if (do_demangle || asm_demangle)
692 name_temp = SYMBOL_PRINT_NAME (msymbol);
694 name_temp = SYMBOL_LINKAGE_NAME (msymbol);
697 if (symbol == NULL && msymbol == NULL)
700 /* If the nearest symbol is too far away, don't print anything symbolic. */
702 /* For when CORE_ADDR is larger than unsigned int, we do math in
703 CORE_ADDR. But when we detect unsigned wraparound in the
704 CORE_ADDR math, we ignore this test and print the offset,
705 because addr+max_symbolic_offset has wrapped through the end
706 of the address space back to the beginning, giving bogus comparison. */
707 if (addr > name_location + max_symbolic_offset
708 && name_location + max_symbolic_offset > name_location)
711 *offset = addr - name_location;
713 *name = xstrdup (name_temp);
715 if (print_symbol_filename)
717 struct symtab_and_line sal;
719 sal = find_pc_sect_line (addr, section, 0);
723 *filename = xstrdup (sal.symtab->filename);
731 /* Print address ADDR symbolically on STREAM.
732 First print it as a number. Then perhaps print
733 <SYMBOL + OFFSET> after the number. */
736 print_address (struct gdbarch *gdbarch,
737 CORE_ADDR addr, struct ui_file *stream)
739 fputs_filtered (paddress (gdbarch, addr), stream);
740 print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
743 /* Return a prefix for instruction address:
744 "=> " for current instruction, else " ". */
747 pc_prefix (CORE_ADDR addr)
749 if (has_stack_frames ())
751 struct frame_info *frame;
754 frame = get_selected_frame (NULL);
755 pc = get_frame_pc (frame);
763 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
764 controls whether to print the symbolic name "raw" or demangled.
765 Global setting "addressprint" controls whether to print hex address
769 print_address_demangle (struct gdbarch *gdbarch, CORE_ADDR addr,
770 struct ui_file *stream, int do_demangle)
772 struct value_print_options opts;
774 get_user_print_options (&opts);
777 fprintf_filtered (stream, "0");
779 else if (opts.addressprint)
781 fputs_filtered (paddress (gdbarch, addr), stream);
782 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
786 print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
791 /* Examine data at address ADDR in format FMT.
792 Fetch it from memory and print on gdb_stdout. */
795 do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
800 struct type *val_type = NULL;
803 struct value_print_options opts;
808 next_gdbarch = gdbarch;
811 /* Instruction format implies fetch single bytes
812 regardless of the specified size.
813 The case of strings is handled in decode_format, only explicit
814 size operator are not changed to 'b'. */
820 /* Pick the appropriate size for an address. */
821 if (gdbarch_ptr_bit (next_gdbarch) == 64)
823 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
825 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
828 /* Bad value for gdbarch_ptr_bit. */
829 internal_error (__FILE__, __LINE__,
830 _("failed internal consistency check"));
834 val_type = builtin_type (next_gdbarch)->builtin_int8;
835 else if (size == 'h')
836 val_type = builtin_type (next_gdbarch)->builtin_int16;
837 else if (size == 'w')
838 val_type = builtin_type (next_gdbarch)->builtin_int32;
839 else if (size == 'g')
840 val_type = builtin_type (next_gdbarch)->builtin_int64;
844 struct type *char_type = NULL;
846 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
847 if type is not found. */
849 char_type = builtin_type (next_gdbarch)->builtin_char16;
850 else if (size == 'w')
851 char_type = builtin_type (next_gdbarch)->builtin_char32;
853 val_type = char_type;
856 if (size != '\0' && size != 'b')
857 warning (_("Unable to display strings with "
858 "size '%c', using 'b' instead."), size);
860 val_type = builtin_type (next_gdbarch)->builtin_int8;
869 if (format == 's' || format == 'i')
872 get_formatted_print_options (&opts, format);
874 /* Print as many objects as specified in COUNT, at most maxelts per line,
875 with the address of the next one at the start of each line. */
881 fputs_filtered (pc_prefix (next_address), gdb_stdout);
882 print_address (next_gdbarch, next_address, gdb_stdout);
883 printf_filtered (":");
888 printf_filtered ("\t");
889 /* Note that print_formatted sets next_address for the next
891 last_examine_address = next_address;
893 if (last_examine_value)
894 value_free (last_examine_value);
896 /* The value to be displayed is not fetched greedily.
897 Instead, to avoid the possibility of a fetched value not
898 being used, its retrieval is delayed until the print code
899 uses it. When examining an instruction stream, the
900 disassembler will perform its own memory fetch using just
901 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
902 the disassembler be modified so that LAST_EXAMINE_VALUE
903 is left with the byte sequence from the last complete
904 instruction fetched from memory? */
905 last_examine_value = value_at_lazy (val_type, next_address);
907 if (last_examine_value)
908 release_value (last_examine_value);
910 print_formatted (last_examine_value, size, &opts, gdb_stdout);
912 /* Display any branch delay slots following the final insn. */
913 if (format == 'i' && count == 1)
914 count += branch_delay_insns;
916 printf_filtered ("\n");
917 gdb_flush (gdb_stdout);
922 validate_format (struct format_data fmt, char *cmdname)
925 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
927 error (_("Item count other than 1 is meaningless in \"%s\" command."),
929 if (fmt.format == 'i')
930 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
931 fmt.format, cmdname);
934 /* Evaluate string EXP as an expression in the current language and
935 print the resulting value. EXP may contain a format specifier as the
936 first argument ("/x myvar" for example, to print myvar in hex). */
939 print_command_1 (char *exp, int inspect, int voidprint)
941 struct expression *expr;
942 struct cleanup *old_chain = 0;
945 struct format_data fmt;
948 if (exp && *exp == '/')
951 fmt = decode_format (&exp, last_format, 0);
952 validate_format (fmt, "print");
953 last_format = format = fmt.format;
965 expr = parse_expression (exp);
966 old_chain = make_cleanup (free_current_contents, &expr);
968 val = evaluate_expression (expr);
971 val = access_value_history (0);
973 if (voidprint || (val && value_type (val) &&
974 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
976 struct value_print_options opts;
977 int histindex = record_latest_value (val);
980 annotate_value_history_begin (histindex, value_type (val));
982 annotate_value_begin (value_type (val));
985 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
987 else if (histindex >= 0)
988 printf_filtered ("$%d = ", histindex);
991 annotate_value_history_value ();
993 get_formatted_print_options (&opts, format);
994 opts.inspect_it = inspect;
997 print_formatted (val, fmt.size, &opts, gdb_stdout);
998 printf_filtered ("\n");
1001 annotate_value_history_end ();
1003 annotate_value_end ();
1006 printf_unfiltered ("\") )\030");
1010 do_cleanups (old_chain);
1014 print_command (char *exp, int from_tty)
1016 print_command_1 (exp, 0, 1);
1019 /* Same as print, except in epoch, it gets its own window. */
1021 inspect_command (char *exp, int from_tty)
1023 extern int epoch_interface;
1025 print_command_1 (exp, epoch_interface, 1);
1028 /* Same as print, except it doesn't print void results. */
1030 call_command (char *exp, int from_tty)
1032 print_command_1 (exp, 0, 0);
1036 output_command (char *exp, int from_tty)
1038 struct expression *expr;
1039 struct cleanup *old_chain;
1042 struct format_data fmt;
1043 struct value_print_options opts;
1048 if (exp && *exp == '/')
1051 fmt = decode_format (&exp, 0, 0);
1052 validate_format (fmt, "output");
1053 format = fmt.format;
1056 expr = parse_expression (exp);
1057 old_chain = make_cleanup (free_current_contents, &expr);
1059 val = evaluate_expression (expr);
1061 annotate_value_begin (value_type (val));
1063 get_formatted_print_options (&opts, format);
1065 print_formatted (val, fmt.size, &opts, gdb_stdout);
1067 annotate_value_end ();
1070 gdb_flush (gdb_stdout);
1072 do_cleanups (old_chain);
1076 set_command (char *exp, int from_tty)
1078 struct expression *expr = parse_expression (exp);
1079 struct cleanup *old_chain =
1080 make_cleanup (free_current_contents, &expr);
1082 evaluate_expression (expr);
1083 do_cleanups (old_chain);
1087 sym_info (char *arg, int from_tty)
1089 struct minimal_symbol *msymbol;
1090 struct objfile *objfile;
1091 struct obj_section *osect;
1092 CORE_ADDR addr, sect_addr;
1094 unsigned int offset;
1097 error_no_arg (_("address"));
1099 addr = parse_and_eval_address (arg);
1100 ALL_OBJSECTIONS (objfile, osect)
1102 /* Only process each object file once, even if there's a separate
1104 if (objfile->separate_debug_objfile_backlink)
1107 sect_addr = overlay_mapped_address (addr, osect);
1109 if (obj_section_addr (osect) <= sect_addr
1110 && sect_addr < obj_section_endaddr (osect)
1111 && (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, osect)))
1113 const char *obj_name, *mapped, *sec_name, *msym_name;
1115 struct cleanup *old_chain;
1118 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1119 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1120 sec_name = osect->the_bfd_section->name;
1121 msym_name = SYMBOL_PRINT_NAME (msymbol);
1123 /* Don't print the offset if it is zero.
1124 We assume there's no need to handle i18n of "sym + offset". */
1126 loc_string = xstrprintf ("%s + %u", msym_name, offset);
1128 loc_string = xstrprintf ("%s", msym_name);
1130 /* Use a cleanup to free loc_string in case the user quits
1131 a pagination request inside printf_filtered. */
1132 old_chain = make_cleanup (xfree, loc_string);
1134 gdb_assert (osect->objfile && osect->objfile->name);
1135 obj_name = osect->objfile->name;
1137 if (MULTI_OBJFILE_P ())
1138 if (pc_in_unmapped_range (addr, osect))
1139 if (section_is_overlay (osect))
1140 printf_filtered (_("%s in load address range of "
1141 "%s overlay section %s of %s\n"),
1142 loc_string, mapped, sec_name, obj_name);
1144 printf_filtered (_("%s in load address range of "
1145 "section %s of %s\n"),
1146 loc_string, sec_name, obj_name);
1148 if (section_is_overlay (osect))
1149 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1150 loc_string, mapped, sec_name, obj_name);
1152 printf_filtered (_("%s in section %s of %s\n"),
1153 loc_string, sec_name, obj_name);
1155 if (pc_in_unmapped_range (addr, osect))
1156 if (section_is_overlay (osect))
1157 printf_filtered (_("%s in load address range of %s overlay "
1159 loc_string, mapped, sec_name);
1161 printf_filtered (_("%s in load address range of section %s\n"),
1162 loc_string, sec_name);
1164 if (section_is_overlay (osect))
1165 printf_filtered (_("%s in %s overlay section %s\n"),
1166 loc_string, mapped, sec_name);
1168 printf_filtered (_("%s in section %s\n"),
1169 loc_string, sec_name);
1171 do_cleanups (old_chain);
1175 printf_filtered (_("No symbol matches %s.\n"), arg);
1179 address_info (char *exp, int from_tty)
1181 struct gdbarch *gdbarch;
1184 struct minimal_symbol *msymbol;
1186 struct obj_section *section;
1187 CORE_ADDR load_addr, context_pc = 0;
1188 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1189 if exp is a field of `this'. */
1192 error (_("Argument required."));
1194 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
1195 &is_a_field_of_this);
1198 if (is_a_field_of_this)
1200 printf_filtered ("Symbol \"");
1201 fprintf_symbol_filtered (gdb_stdout, exp,
1202 current_language->la_language, DMGL_ANSI);
1203 printf_filtered ("\" is a field of the local class variable ");
1204 if (current_language->la_language == language_objc)
1205 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1207 printf_filtered ("`this'\n");
1211 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1213 if (msymbol != NULL)
1215 gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
1216 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1218 printf_filtered ("Symbol \"");
1219 fprintf_symbol_filtered (gdb_stdout, exp,
1220 current_language->la_language, DMGL_ANSI);
1221 printf_filtered ("\" is at ");
1222 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1223 printf_filtered (" in a file compiled without debugging");
1224 section = SYMBOL_OBJ_SECTION (msymbol);
1225 if (section_is_overlay (section))
1227 load_addr = overlay_unmapped_address (load_addr, section);
1228 printf_filtered (",\n -- loaded at ");
1229 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1230 printf_filtered (" in overlay section %s",
1231 section->the_bfd_section->name);
1233 printf_filtered (".\n");
1236 error (_("No symbol \"%s\" in current context."), exp);
1240 printf_filtered ("Symbol \"");
1241 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
1242 current_language->la_language, DMGL_ANSI);
1243 printf_filtered ("\" is ");
1244 val = SYMBOL_VALUE (sym);
1245 section = SYMBOL_OBJ_SECTION (sym);
1246 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
1248 switch (SYMBOL_CLASS (sym))
1251 case LOC_CONST_BYTES:
1252 printf_filtered ("constant");
1256 printf_filtered ("a label at address ");
1257 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1258 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1259 if (section_is_overlay (section))
1261 load_addr = overlay_unmapped_address (load_addr, section);
1262 printf_filtered (",\n -- loaded at ");
1263 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1264 printf_filtered (" in overlay section %s",
1265 section->the_bfd_section->name);
1270 /* FIXME: cagney/2004-01-26: It should be possible to
1271 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1272 Unfortunately DWARF 2 stores the frame-base (instead of the
1273 function) location in a function's symbol. Oops! For the
1274 moment enable this when/where applicable. */
1275 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1280 /* GDBARCH is the architecture associated with the objfile the symbol
1281 is defined in; the target architecture may be different, and may
1282 provide additional registers. However, we do not know the target
1283 architecture at this point. We assume the objfile architecture
1284 will contain all the standard registers that occur in debug info
1286 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1288 if (SYMBOL_IS_ARGUMENT (sym))
1289 printf_filtered (_("an argument in register %s"),
1290 gdbarch_register_name (gdbarch, regno));
1292 printf_filtered (_("a variable in register %s"),
1293 gdbarch_register_name (gdbarch, regno));
1297 printf_filtered (_("static storage at address "));
1298 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1299 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1300 if (section_is_overlay (section))
1302 load_addr = overlay_unmapped_address (load_addr, section);
1303 printf_filtered (_(",\n -- loaded at "));
1304 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1305 printf_filtered (_(" in overlay section %s"),
1306 section->the_bfd_section->name);
1310 case LOC_REGPARM_ADDR:
1311 /* Note comment at LOC_REGISTER. */
1312 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1313 printf_filtered (_("address of an argument in register %s"),
1314 gdbarch_register_name (gdbarch, regno));
1318 printf_filtered (_("an argument at offset %ld"), val);
1322 printf_filtered (_("a local variable at frame offset %ld"), val);
1326 printf_filtered (_("a reference argument at offset %ld"), val);
1330 printf_filtered (_("a typedef"));
1334 printf_filtered (_("a function at address "));
1335 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1336 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1337 if (section_is_overlay (section))
1339 load_addr = overlay_unmapped_address (load_addr, section);
1340 printf_filtered (_(",\n -- loaded at "));
1341 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1342 printf_filtered (_(" in overlay section %s"),
1343 section->the_bfd_section->name);
1347 case LOC_UNRESOLVED:
1349 struct minimal_symbol *msym;
1351 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym), NULL, NULL);
1353 printf_filtered ("unresolved");
1356 section = SYMBOL_OBJ_SECTION (msym);
1357 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1360 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1361 printf_filtered (_("a thread-local variable at offset %s "
1362 "in the thread-local storage for `%s'"),
1363 paddress (gdbarch, load_addr),
1364 section->objfile->name);
1367 printf_filtered (_("static storage at address "));
1368 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1369 if (section_is_overlay (section))
1371 load_addr = overlay_unmapped_address (load_addr, section);
1372 printf_filtered (_(",\n -- loaded at "));
1373 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1374 printf_filtered (_(" in overlay section %s"),
1375 section->the_bfd_section->name);
1382 case LOC_OPTIMIZED_OUT:
1383 printf_filtered (_("optimized out"));
1387 printf_filtered (_("of unknown (botched) type"));
1390 printf_filtered (".\n");
1395 x_command (char *exp, int from_tty)
1397 struct expression *expr;
1398 struct format_data fmt;
1399 struct cleanup *old_chain;
1402 fmt.format = last_format ? last_format : 'x';
1403 fmt.size = last_size;
1407 if (exp && *exp == '/')
1410 fmt = decode_format (&exp, last_format, last_size);
1413 /* If we have an expression, evaluate it and use it as the address. */
1415 if (exp != 0 && *exp != 0)
1417 expr = parse_expression (exp);
1418 /* Cause expression not to be there any more if this command is
1419 repeated with Newline. But don't clobber a user-defined
1420 command's definition. */
1423 old_chain = make_cleanup (free_current_contents, &expr);
1424 val = evaluate_expression (expr);
1425 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1426 val = coerce_ref (val);
1427 /* In rvalue contexts, such as this, functions are coerced into
1428 pointers to functions. This makes "x/i main" work. */
1429 if (/* last_format == 'i' && */
1430 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1431 && VALUE_LVAL (val) == lval_memory)
1432 next_address = value_address (val);
1434 next_address = value_as_address (val);
1436 next_gdbarch = expr->gdbarch;
1437 do_cleanups (old_chain);
1441 error_no_arg (_("starting display address"));
1443 do_examine (fmt, next_gdbarch, next_address);
1445 /* If the examine succeeds, we remember its size and format for next
1446 time. Set last_size to 'b' for strings. */
1447 if (fmt.format == 's')
1450 last_size = fmt.size;
1451 last_format = fmt.format;
1453 /* Set a couple of internal variables if appropriate. */
1454 if (last_examine_value)
1456 /* Make last address examined available to the user as $_. Use
1457 the correct pointer type. */
1458 struct type *pointer_type
1459 = lookup_pointer_type (value_type (last_examine_value));
1460 set_internalvar (lookup_internalvar ("_"),
1461 value_from_pointer (pointer_type,
1462 last_examine_address));
1464 /* Make contents of last address examined available to the user
1465 as $__. If the last value has not been fetched from memory
1466 then don't fetch it now; instead mark it by voiding the $__
1468 if (value_lazy (last_examine_value))
1469 clear_internalvar (lookup_internalvar ("__"));
1471 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1476 /* Add an expression to the auto-display chain.
1477 Specify the expression. */
1480 display_command (char *exp, int from_tty)
1482 struct format_data fmt;
1483 struct expression *expr;
1484 struct display *new;
1488 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1490 if (tui_active && exp != NULL && *exp == '$')
1491 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1505 fmt = decode_format (&exp, 0, 0);
1506 if (fmt.size && fmt.format == 0)
1508 if (fmt.format == 'i' || fmt.format == 's')
1519 innermost_block = NULL;
1520 expr = parse_expression (exp);
1522 new = (struct display *) xmalloc (sizeof (struct display));
1524 new->exp_string = xstrdup (exp);
1526 new->block = innermost_block;
1527 new->pspace = current_program_space;
1528 new->next = display_chain;
1529 new->number = ++display_number;
1532 display_chain = new;
1534 if (from_tty && target_has_execution)
1535 do_one_display (new);
1542 free_display (struct display *d)
1544 xfree (d->exp_string);
1549 /* Clear out the display_chain. Done when new symtabs are loaded,
1550 since this invalidates the types stored in many expressions. */
1553 clear_displays (void)
1557 while ((d = display_chain) != NULL)
1559 display_chain = d->next;
1564 /* Delete the auto-display DISPLAY. */
1567 delete_display (struct display *display)
1571 gdb_assert (display != NULL);
1573 if (display_chain == display)
1574 display_chain = display->next;
1577 if (d->next == display)
1579 d->next = display->next;
1583 free_display (display);
1586 /* Delete some values from the auto-display chain.
1587 Specify the element numbers. */
1590 undisplay_command (char *args, int from_tty)
1598 if (query (_("Delete all auto-display expressions? ")))
1608 num = get_number_or_range (&p1);
1610 warning (_("bad display number at or near '%s'"), p);
1616 if (d->number == num)
1619 printf_unfiltered (_("No display number %d.\n"), num);
1629 /* Display a single auto-display.
1630 Do nothing if the display cannot be printed in the current context,
1631 or if the display is disabled. */
1634 do_one_display (struct display *d)
1636 int within_current_scope;
1638 if (d->enabled_p == 0)
1641 /* The expression carries the architecture that was used at parse time.
1642 This is a problem if the expression depends on architecture features
1643 (e.g. register numbers), and the current architecture is now different.
1644 For example, a display statement like "display/i $pc" is expected to
1645 display the PC register of the current architecture, not the arch at
1646 the time the display command was given. Therefore, we re-parse the
1647 expression if the current architecture has changed. */
1648 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
1657 volatile struct gdb_exception ex;
1659 TRY_CATCH (ex, RETURN_MASK_ALL)
1661 innermost_block = NULL;
1662 d->exp = parse_expression (d->exp_string);
1663 d->block = innermost_block;
1667 /* Can't re-parse the expression. Disable this display item. */
1669 warning (_("Unable to display \"%s\": %s"),
1670 d->exp_string, ex.message);
1677 if (d->pspace == current_program_space)
1678 within_current_scope = contained_in (get_selected_block (0), d->block);
1680 within_current_scope = 0;
1683 within_current_scope = 1;
1684 if (!within_current_scope)
1687 current_display_number = d->number;
1689 annotate_display_begin ();
1690 printf_filtered ("%d", d->number);
1691 annotate_display_number_end ();
1692 printf_filtered (": ");
1698 annotate_display_format ();
1700 printf_filtered ("x/");
1701 if (d->format.count != 1)
1702 printf_filtered ("%d", d->format.count);
1703 printf_filtered ("%c", d->format.format);
1704 if (d->format.format != 'i' && d->format.format != 's')
1705 printf_filtered ("%c", d->format.size);
1706 printf_filtered (" ");
1708 annotate_display_expression ();
1710 puts_filtered (d->exp_string);
1711 annotate_display_expression_end ();
1713 if (d->format.count != 1 || d->format.format == 'i')
1714 printf_filtered ("\n");
1716 printf_filtered (" ");
1718 val = evaluate_expression (d->exp);
1719 addr = value_as_address (val);
1720 if (d->format.format == 'i')
1721 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
1723 annotate_display_value ();
1725 do_examine (d->format, d->exp->gdbarch, addr);
1729 struct value_print_options opts;
1731 annotate_display_format ();
1733 if (d->format.format)
1734 printf_filtered ("/%c ", d->format.format);
1736 annotate_display_expression ();
1738 puts_filtered (d->exp_string);
1739 annotate_display_expression_end ();
1741 printf_filtered (" = ");
1743 annotate_display_expression ();
1745 get_formatted_print_options (&opts, d->format.format);
1746 opts.raw = d->format.raw;
1747 print_formatted (evaluate_expression (d->exp),
1748 d->format.size, &opts, gdb_stdout);
1749 printf_filtered ("\n");
1752 annotate_display_end ();
1754 gdb_flush (gdb_stdout);
1755 current_display_number = -1;
1758 /* Display all of the values on the auto-display chain which can be
1759 evaluated in the current scope. */
1766 for (d = display_chain; d; d = d->next)
1770 /* Delete the auto-display which we were in the process of displaying.
1771 This is done when there is an error or a signal. */
1774 disable_display (int num)
1778 for (d = display_chain; d; d = d->next)
1779 if (d->number == num)
1784 printf_unfiltered (_("No display number %d.\n"), num);
1788 disable_current_display (void)
1790 if (current_display_number >= 0)
1792 disable_display (current_display_number);
1793 fprintf_unfiltered (gdb_stderr,
1794 _("Disabling display %d to "
1795 "avoid infinite recursion.\n"),
1796 current_display_number);
1798 current_display_number = -1;
1802 display_info (char *ignore, int from_tty)
1807 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1809 printf_filtered (_("Auto-display expressions now in effect:\n\
1810 Num Enb Expression\n"));
1812 for (d = display_chain; d; d = d->next)
1814 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1816 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1818 else if (d->format.format)
1819 printf_filtered ("/%c ", d->format.format);
1820 puts_filtered (d->exp_string);
1821 if (d->block && !contained_in (get_selected_block (0), d->block))
1822 printf_filtered (_(" (cannot be evaluated in the current context)"));
1823 printf_filtered ("\n");
1824 gdb_flush (gdb_stdout);
1829 enable_display (char *args, int from_tty)
1838 for (d = display_chain; d; d = d->next)
1845 while (*p1 >= '0' && *p1 <= '9')
1847 if (*p1 && *p1 != ' ' && *p1 != '\t')
1848 error (_("Arguments must be display numbers."));
1852 for (d = display_chain; d; d = d->next)
1853 if (d->number == num)
1858 printf_unfiltered (_("No display number %d.\n"), num);
1861 while (*p == ' ' || *p == '\t')
1867 disable_display_command (char *args, int from_tty)
1875 for (d = display_chain; d; d = d->next)
1882 while (*p1 >= '0' && *p1 <= '9')
1884 if (*p1 && *p1 != ' ' && *p1 != '\t')
1885 error (_("Arguments must be display numbers."));
1887 disable_display (atoi (p));
1890 while (*p == ' ' || *p == '\t')
1895 /* display_chain items point to blocks and expressions. Some expressions in
1896 turn may point to symbols.
1897 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1898 obstack_free'd when a shared library is unloaded.
1899 Clear pointers that are about to become dangling.
1900 Both .exp and .block fields will be restored next time we need to display
1901 an item by re-parsing .exp_string field in the new execution context. */
1904 clear_dangling_display_expressions (struct so_list *solib)
1906 struct objfile *objfile = solib->objfile;
1909 /* With no symbol file we cannot have a block or expression from it. */
1910 if (objfile == NULL)
1912 if (objfile->separate_debug_objfile_backlink)
1913 objfile = objfile->separate_debug_objfile_backlink;
1914 gdb_assert (objfile->pspace == solib->pspace);
1916 for (d = display_chain; d != NULL; d = d->next)
1918 if (d->pspace != solib->pspace)
1921 if (lookup_objfile_from_block (d->block) == objfile
1922 || (d->exp && exp_uses_objfile (d->exp, objfile)))
1932 /* Print the value in stack frame FRAME of a variable specified by a
1933 struct symbol. NAME is the name to print; if NULL then VAR's print
1934 name will be used. STREAM is the ui_file on which to print the
1935 value. INDENT specifies the number of indent levels to print
1936 before printing the variable name. */
1939 print_variable_and_value (const char *name, struct symbol *var,
1940 struct frame_info *frame,
1941 struct ui_file *stream, int indent)
1943 volatile struct gdb_exception except;
1946 name = SYMBOL_PRINT_NAME (var);
1948 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1949 TRY_CATCH (except, RETURN_MASK_ERROR)
1952 struct value_print_options opts;
1954 val = read_var_value (var, frame);
1955 get_user_print_options (&opts);
1956 common_val_print (val, stream, indent, &opts, current_language);
1958 if (except.reason < 0)
1959 fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
1961 fprintf_filtered (stream, "\n");
1965 string_printf (char *arg, struct ui_file *stream, printf_callback callback,
1966 void *loc_v, void *aexpr_v)
1970 char *string = NULL;
1971 struct value **val_args;
1973 char *current_substring;
1975 int allocated_args = 20;
1976 struct cleanup *old_cleanups;
1977 struct bp_location *loc = loc_v;
1978 struct agent_expr *aexpr = aexpr_v;
1980 val_args = xmalloc (allocated_args * sizeof (struct value *));
1981 old_cleanups = make_cleanup (free_current_contents, &val_args);
1984 error_no_arg (_("format-control string and values to print"));
1986 /* Skip white space before format string. */
1987 while (*s == ' ' || *s == '\t')
1990 /* A format string should follow, enveloped in double quotes. */
1992 error (_("Bad format string, missing '\"'."));
1994 /* Parse the format-control string and copy it into the string STRING,
1995 processing some kinds of escape sequence. */
1997 f = string = (char *) alloca (strlen (s) + 1);
2005 error (_("Bad format string, non-terminated '\"'."));
2038 /* ??? TODO: handle other escape sequences. */
2039 error (_("Unrecognized escape character \\%c in format string."),
2049 /* Skip over " and following space and comma. */
2052 while (*s == ' ' || *s == '\t')
2055 if (*s != ',' && *s != 0)
2056 error (_("Invalid argument syntax"));
2060 while (*s == ' ' || *s == '\t')
2063 /* Need extra space for the '\0's. Doubling the size is sufficient. */
2064 substrings = alloca (strlen (string) * 2);
2065 current_substring = substrings;
2068 /* Now scan the string for %-specs and see what kinds of args they want.
2069 argclass[I] classifies the %-specs so we can give printf_filtered
2070 something of the right size. */
2074 int_arg, long_arg, long_long_arg, ptr_arg,
2075 string_arg, wide_string_arg, wide_char_arg,
2076 double_arg, long_double_arg, decfloat_arg
2078 enum argclass *argclass;
2079 enum argclass this_argclass;
2084 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
2091 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
2092 int seen_space = 0, seen_plus = 0;
2093 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
2094 int seen_big_d = 0, seen_double_big_d = 0;
2097 /* Check the validity of the format specifier, and work
2098 out what argument it expects. We only accept C89
2099 format strings, with the exception of long long (which
2100 we autoconf for). */
2102 /* Skip over "%%". */
2109 /* The first part of a format specifier is a set of flag
2111 while (strchr ("0-+ #", *f))
2124 /* The next part of a format specifier is a width. */
2125 while (strchr ("0123456789", *f))
2128 /* The next part of a format specifier is a precision. */
2133 while (strchr ("0123456789", *f))
2137 /* The next part of a format specifier is a length modifier. */
2158 /* Decimal32 modifier. */
2164 /* Decimal64 and Decimal128 modifiers. */
2169 /* Check for a Decimal128. */
2173 seen_double_big_d = 1;
2189 if (seen_space || seen_plus)
2196 this_argclass = int_arg;
2197 else if (lcount == 1)
2198 this_argclass = long_arg;
2200 this_argclass = long_long_arg;
2207 this_argclass = lcount == 0 ? int_arg : wide_char_arg;
2208 if (lcount > 1 || seen_h || seen_big_l)
2210 if (seen_prec || seen_zero || seen_space || seen_plus)
2215 this_argclass = ptr_arg;
2216 if (lcount || seen_h || seen_big_l)
2218 if (seen_prec || seen_zero || seen_space || seen_plus)
2223 this_argclass = lcount == 0 ? string_arg : wide_string_arg;
2224 if (lcount > 1 || seen_h || seen_big_l)
2226 if (seen_zero || seen_space || seen_plus)
2235 if (seen_big_h || seen_big_d || seen_double_big_d)
2236 this_argclass = decfloat_arg;
2237 else if (seen_big_l)
2238 this_argclass = long_double_arg;
2240 this_argclass = double_arg;
2242 if (lcount || seen_h)
2247 error (_("`*' not supported for precision or width in printf"));
2250 error (_("Format specifier `n' not supported in printf"));
2253 error (_("Incomplete format specifier at end of format string"));
2256 error (_("Unrecognized format specifier '%c' in printf"), *f);
2260 error (_("Inappropriate modifiers to "
2261 "format specifier '%c' in printf"),
2266 if (lcount > 1 && USE_PRINTF_I64)
2268 /* Windows' printf does support long long, but not the usual way.
2269 Convert %lld to %I64d. */
2270 int length_before_ll = f - last_arg - 1 - lcount;
2272 strncpy (current_substring, last_arg, length_before_ll);
2273 strcpy (current_substring + length_before_ll, "I64");
2274 current_substring[length_before_ll + 3] =
2275 last_arg[length_before_ll + lcount];
2276 current_substring += length_before_ll + 4;
2278 else if (this_argclass == wide_string_arg
2279 || this_argclass == wide_char_arg)
2281 /* Convert %ls or %lc to %s. */
2282 int length_before_ls = f - last_arg - 2;
2284 strncpy (current_substring, last_arg, length_before_ls);
2285 strcpy (current_substring + length_before_ls, "s");
2286 current_substring += length_before_ls + 2;
2290 strncpy (current_substring, last_arg, f - last_arg);
2291 current_substring += f - last_arg;
2293 *current_substring++ = '\0';
2295 argclass[nargs_wanted++] = this_argclass;
2298 /* Now, parse all arguments and evaluate them.
2299 Store the VALUEs in VAL_ARGS. */
2302 current_substring = substrings;
2308 if (nargs == allocated_args)
2309 val_args = (struct value **) xrealloc ((char *) val_args,
2310 (allocated_args *= 2)
2311 * sizeof (struct value *));
2314 if (nargs >= nargs_wanted)
2315 error (_("Wrong number of arguments for specified "
2317 callback (current_substring, &s1, loc, aexpr);
2318 current_substring += strlen (current_substring) + 1;
2321 val_args[nargs] = parse_to_comma_and_eval (&s1);
2329 callback (last_arg, NULL, loc, aexpr);
2331 if (nargs != nargs_wanted)
2332 error (_("Wrong number of arguments for specified format-string"));
2337 /* Now actually print them. */
2338 current_substring = substrings;
2339 for (i = 0; i < nargs; i++)
2341 switch (argclass[i])
2349 tem = value_as_address (val_args[i]);
2351 /* This is a %s argument. Find the length of the string. */
2357 read_memory (tem + j, &c, 1);
2362 /* Copy the string contents into a string inside GDB. */
2363 str = (gdb_byte *) alloca (j + 1);
2365 read_memory (tem, str, j);
2368 fprintf_filtered (stream, current_substring, (char *) str);
2371 case wide_string_arg:
2376 struct gdbarch *gdbarch
2377 = get_type_arch (value_type (val_args[i]));
2378 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2379 struct type *wctype = lookup_typename (current_language, gdbarch,
2380 "wchar_t", NULL, 0);
2381 int wcwidth = TYPE_LENGTH (wctype);
2382 gdb_byte *buf = alloca (wcwidth);
2383 struct obstack output;
2384 struct cleanup *inner_cleanup;
2386 tem = value_as_address (val_args[i]);
2388 /* This is a %s argument. Find the length of the string. */
2389 for (j = 0;; j += wcwidth)
2392 read_memory (tem + j, buf, wcwidth);
2393 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2397 /* Copy the string contents into a string inside GDB. */
2398 str = (gdb_byte *) alloca (j + wcwidth);
2400 read_memory (tem, str, j);
2401 memset (&str[j], 0, wcwidth);
2403 obstack_init (&output);
2404 inner_cleanup = make_cleanup_obstack_free (&output);
2406 convert_between_encodings (target_wide_charset (gdbarch),
2409 &output, translit_char);
2410 obstack_grow_str0 (&output, "");
2412 fprintf_filtered (stream, current_substring,
2413 obstack_base (&output));
2414 do_cleanups (inner_cleanup);
2419 struct gdbarch *gdbarch
2420 = get_type_arch (value_type (val_args[i]));
2421 struct type *wctype = lookup_typename (current_language, gdbarch,
2422 "wchar_t", NULL, 0);
2423 struct type *valtype;
2424 struct obstack output;
2425 struct cleanup *inner_cleanup;
2426 const gdb_byte *bytes;
2428 valtype = value_type (val_args[i]);
2429 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2430 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2431 error (_("expected wchar_t argument for %%lc"));
2433 bytes = value_contents (val_args[i]);
2435 obstack_init (&output);
2436 inner_cleanup = make_cleanup_obstack_free (&output);
2438 convert_between_encodings (target_wide_charset (gdbarch),
2440 bytes, TYPE_LENGTH (valtype),
2441 TYPE_LENGTH (valtype),
2442 &output, translit_char);
2443 obstack_grow_str0 (&output, "");
2445 fprintf_filtered (stream, current_substring,
2446 obstack_base (&output));
2447 do_cleanups (inner_cleanup);
2452 struct type *type = value_type (val_args[i]);
2456 /* If format string wants a float, unchecked-convert the value
2457 to floating point of the same size. */
2458 type = float_type_from_length (type);
2459 val = unpack_double (type, value_contents (val_args[i]), &inv);
2461 error (_("Invalid floating value found in program."));
2463 fprintf_filtered (stream, current_substring, (double) val);
2466 case long_double_arg:
2467 #ifdef HAVE_LONG_DOUBLE
2469 struct type *type = value_type (val_args[i]);
2473 /* If format string wants a float, unchecked-convert the value
2474 to floating point of the same size. */
2475 type = float_type_from_length (type);
2476 val = unpack_double (type, value_contents (val_args[i]), &inv);
2478 error (_("Invalid floating value found in program."));
2480 fprintf_filtered (stream, current_substring,
2485 error (_("long double not supported in printf"));
2488 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2490 long long val = value_as_long (val_args[i]);
2492 fprintf_filtered (stream, current_substring, val);
2496 error (_("long long not supported in printf"));
2500 int val = value_as_long (val_args[i]);
2502 fprintf_filtered (stream, current_substring, val);
2507 long val = value_as_long (val_args[i]);
2509 fprintf_filtered (stream, current_substring, val);
2513 /* Handles decimal floating values. */
2516 const gdb_byte *param_ptr = value_contents (val_args[i]);
2518 #if defined (PRINTF_HAS_DECFLOAT)
2519 /* If we have native support for Decimal floating
2520 printing, handle it here. */
2521 fprintf_filtered (stream, current_substring, param_ptr);
2524 /* As a workaround until vasprintf has native support for DFP
2525 we convert the DFP values to string and print them using
2526 the %s format specifier. */
2529 int nnull_chars = 0;
2531 /* Parameter data. */
2532 struct type *param_type = value_type (val_args[i]);
2533 unsigned int param_len = TYPE_LENGTH (param_type);
2534 struct gdbarch *gdbarch = get_type_arch (param_type);
2535 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2537 /* DFP output data. */
2538 struct value *dfp_value = NULL;
2542 struct type *dfp_type = NULL;
2543 char decstr[MAX_DECIMAL_STRING];
2545 /* Points to the end of the string so that we can go back
2546 and check for DFP length modifiers. */
2547 eos = current_substring + strlen (current_substring);
2549 /* Look for the float/double format specifier. */
2550 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2551 && *eos != 'g' && *eos != 'G')
2556 /* Search for the '%' char and extract the size and type of
2557 the output decimal value based on its modifiers
2558 (%Hf, %Df, %DDf). */
2559 while (*--sos != '%')
2564 dfp_type = builtin_type (gdbarch)->builtin_decfloat;
2566 else if (*sos == 'D' && *(sos - 1) == 'D')
2569 dfp_type = builtin_type (gdbarch)->builtin_declong;
2575 dfp_type = builtin_type (gdbarch)->builtin_decdouble;
2579 /* Replace %Hf, %Df and %DDf with %s's. */
2582 /* Go through the whole format string and pull the correct
2583 number of chars back to compensate for the change in the
2584 format specifier. */
2585 while (nnull_chars < nargs - i)
2593 /* Conversion between different DFP types. */
2594 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2595 decimal_convert (param_ptr, param_len, byte_order,
2596 dec, dfp_len, byte_order);
2598 /* If this is a non-trivial conversion, just output 0.
2599 A correct converted value can be displayed by explicitly
2600 casting to a DFP type. */
2601 decimal_from_string (dec, dfp_len, byte_order, "0");
2603 dfp_value = value_from_decfloat (dfp_type, dec);
2605 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2607 decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
2609 /* Print the DFP value. */
2610 fprintf_filtered (stream, current_substring, decstr);
2618 /* We avoid the host's %p because pointers are too
2619 likely to be the wrong size. The only interesting
2620 modifier for %p is a width; extract that, and then
2621 handle %p as glibc would: %#x or a literal "(nil)". */
2623 char *p, *fmt, *fmt_p;
2624 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2625 long long val = value_as_long (val_args[i]);
2627 long val = value_as_long (val_args[i]);
2630 fmt = alloca (strlen (current_substring) + 5);
2632 /* Copy up to the leading %. */
2633 p = current_substring;
2637 int is_percent = (*p == '%');
2652 /* Copy any width. */
2653 while (*p >= '0' && *p < '9')
2656 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2659 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2665 fprintf_filtered (stream, fmt, val);
2671 fprintf_filtered (stream, fmt, "(nil)");
2677 internal_error (__FILE__, __LINE__,
2678 _("failed internal consistency check"));
2680 /* Skip to the next substring. */
2681 current_substring += strlen (current_substring) + 1;
2683 /* Print the portion of the format string after the last argument.
2684 Note that this will not include any ordinary %-specs, but it
2685 might include "%%". That is why we use printf_filtered and not
2686 puts_filtered here. Also, we pass a dummy argument because
2687 some platforms have modified GCC to include -Wformat-security
2688 by default, which will warn here if there is no argument. */
2689 fprintf_filtered (stream, last_arg, 0);
2693 do_cleanups (old_cleanups);
2696 /* Implement the "printf" command. */
2699 printf_command (char *arg, int from_tty)
2701 string_printf (arg, gdb_stdout, NULL, NULL, NULL);
2704 /* Implement the "eval" command. */
2707 eval_command (char *arg, int from_tty)
2709 struct ui_file *ui_out = mem_fileopen ();
2710 struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
2713 string_printf (arg, ui_out, NULL, NULL, NULL);
2715 expanded = ui_file_xstrdup (ui_out, NULL);
2716 make_cleanup (xfree, expanded);
2718 execute_command (expanded, from_tty);
2720 do_cleanups (cleanups);
2724 _initialize_printcmd (void)
2726 struct cmd_list_element *c;
2728 current_display_number = -1;
2730 observer_attach_solib_unloaded (clear_dangling_display_expressions);
2732 add_info ("address", address_info,
2733 _("Describe where symbol SYM is stored."));
2735 add_info ("symbol", sym_info, _("\
2736 Describe what symbol is at location ADDR.\n\
2737 Only for symbols with fixed locations (global or static scope)."));
2739 add_com ("x", class_vars, x_command, _("\
2740 Examine memory: x/FMT ADDRESS.\n\
2741 ADDRESS is an expression for the memory address to examine.\n\
2742 FMT is a repeat count followed by a format letter and a size letter.\n\
2743 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2744 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2745 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2746 The specified number of objects of the specified size are printed\n\
2747 according to the format.\n\n\
2748 Defaults for format and size letters are those previously used.\n\
2749 Default count is 1. Default address is following last thing printed\n\
2750 with this command or \"print\"."));
2753 add_com ("whereis", class_vars, whereis_command,
2754 _("Print line number and file of definition of variable."));
2757 add_info ("display", display_info, _("\
2758 Expressions to display when program stops, with code numbers."));
2760 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2761 Cancel some expressions to be displayed when program stops.\n\
2762 Arguments are the code numbers of the expressions to stop displaying.\n\
2763 No argument means cancel all automatic-display expressions.\n\
2764 \"delete display\" has the same effect as this command.\n\
2765 Do \"info display\" to see current list of code numbers."),
2768 add_com ("display", class_vars, display_command, _("\
2769 Print value of expression EXP each time the program stops.\n\
2770 /FMT may be used before EXP as in the \"print\" command.\n\
2771 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2772 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2773 and examining is done as in the \"x\" command.\n\n\
2774 With no argument, display all currently requested auto-display expressions.\n\
2775 Use \"undisplay\" to cancel display requests previously made."));
2777 add_cmd ("display", class_vars, enable_display, _("\
2778 Enable some expressions to be displayed when program stops.\n\
2779 Arguments are the code numbers of the expressions to resume displaying.\n\
2780 No argument means enable all automatic-display expressions.\n\
2781 Do \"info display\" to see current list of code numbers."), &enablelist);
2783 add_cmd ("display", class_vars, disable_display_command, _("\
2784 Disable some expressions to be displayed when program stops.\n\
2785 Arguments are the code numbers of the expressions to stop displaying.\n\
2786 No argument means disable all automatic-display expressions.\n\
2787 Do \"info display\" to see current list of code numbers."), &disablelist);
2789 add_cmd ("display", class_vars, undisplay_command, _("\
2790 Cancel some expressions to be displayed when program stops.\n\
2791 Arguments are the code numbers of the expressions to stop displaying.\n\
2792 No argument means cancel all automatic-display expressions.\n\
2793 Do \"info display\" to see current list of code numbers."), &deletelist);
2795 add_com ("printf", class_vars, printf_command, _("\
2796 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2797 This is useful for formatted output in user-defined commands."));
2799 add_com ("output", class_vars, output_command, _("\
2800 Like \"print\" but don't put in value history and don't print newline.\n\
2801 This is useful in user-defined commands."));
2803 add_prefix_cmd ("set", class_vars, set_command, _("\
2804 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2805 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2806 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2807 with $), a register (a few standard names starting with $), or an actual\n\
2808 variable in the program being debugged. EXP is any valid expression.\n\
2809 Use \"set variable\" for variables with names identical to set subcommands.\n\
2811 With a subcommand, this command modifies parts of the gdb environment.\n\
2812 You can see these environment settings with the \"show\" command."),
2813 &setlist, "set ", 1, &cmdlist);
2815 add_com ("assign", class_vars, set_command, _("\
2816 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2817 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2818 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2819 with $), a register (a few standard names starting with $), or an actual\n\
2820 variable in the program being debugged. EXP is any valid expression.\n\
2821 Use \"set variable\" for variables with names identical to set subcommands.\n\
2822 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2823 You can see these environment settings with the \"show\" command."));
2825 /* "call" is the same as "set", but handy for dbx users to call fns. */
2826 c = add_com ("call", class_vars, call_command, _("\
2827 Call a function in the program.\n\
2828 The argument is the function name and arguments, in the notation of the\n\
2829 current working language. The result is printed and saved in the value\n\
2830 history, if it is not void."));
2831 set_cmd_completer (c, expression_completer);
2833 add_cmd ("variable", class_vars, set_command, _("\
2834 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2835 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2836 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2837 with $), a register (a few standard names starting with $), or an actual\n\
2838 variable in the program being debugged. EXP is any valid expression.\n\
2839 This may usually be abbreviated to simply \"set\"."),
2842 c = add_com ("print", class_vars, print_command, _("\
2843 Print value of expression EXP.\n\
2844 Variables accessible are those of the lexical environment of the selected\n\
2845 stack frame, plus all those whose scope is global or an entire file.\n\
2847 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2848 $$NUM refers to NUM'th value back from the last one.\n\
2849 Names starting with $ refer to registers (with the values they would have\n\
2850 if the program were to return to the stack frame now selected, restoring\n\
2851 all registers saved by frames farther in) or else to debugger\n\
2852 \"convenience\" variables (any such name not a known register).\n\
2853 Use assignment expressions to give values to convenience variables.\n\
2855 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2856 @ is a binary operator for treating consecutive data objects\n\
2857 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2858 element is FOO, whose second element is stored in the space following\n\
2859 where FOO is stored, etc. FOO must be an expression whose value\n\
2860 resides in memory.\n\
2862 EXP may be preceded with /FMT, where FMT is a format letter\n\
2863 but no count or size letter (see \"x\" command)."));
2864 set_cmd_completer (c, expression_completer);
2865 add_com_alias ("p", "print", class_vars, 1);
2867 c = add_com ("inspect", class_vars, inspect_command, _("\
2868 Same as \"print\" command, except that if you are running in the epoch\n\
2869 environment, the value is printed in its own window."));
2870 set_cmd_completer (c, expression_completer);
2872 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2873 &max_symbolic_offset, _("\
2874 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2875 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2877 show_max_symbolic_offset,
2878 &setprintlist, &showprintlist);
2879 add_setshow_boolean_cmd ("symbol-filename", no_class,
2880 &print_symbol_filename, _("\
2881 Set printing of source filename and line number with <symbol>."), _("\
2882 Show printing of source filename and line number with <symbol>."), NULL,
2884 show_print_symbol_filename,
2885 &setprintlist, &showprintlist);
2887 add_com ("eval", no_class, eval_command, _("\
2888 Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2889 a command line, and call it."));