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"
54 #include "tui/tui.h" /* For tui_active et al. */
57 #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
58 # define USE_PRINTF_I64 1
59 # define PRINTF_HAS_LONG_LONG
61 # define USE_PRINTF_I64 0
64 extern int asm_demangle; /* Whether to demangle syms in asm
73 /* True if the value should be printed raw -- that is, bypassing
74 python-based formatters. */
78 /* Last specified output format. */
80 static char last_format = 0;
82 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
84 static char last_size = 'w';
86 /* Default address to examine next, and associated architecture. */
88 static struct gdbarch *next_gdbarch;
89 static CORE_ADDR next_address;
91 /* Number of delay instructions following current disassembled insn. */
93 static int branch_delay_insns;
95 /* Last address examined. */
97 static CORE_ADDR last_examine_address;
99 /* Contents of last address examined.
100 This is not valid past the end of the `x' command! */
102 static struct value *last_examine_value;
104 /* Largest offset between a symbolic value and an address, that will be
105 printed as `0x1234 <symbol+offset>'. */
107 static unsigned int max_symbolic_offset = UINT_MAX;
109 show_max_symbolic_offset (struct ui_file *file, int from_tty,
110 struct cmd_list_element *c, const char *value)
112 fprintf_filtered (file,
113 _("The largest offset that will be "
114 "printed in <symbol+1234> form is %s.\n"),
118 /* Append the source filename and linenumber of the symbol when
119 printing a symbolic value as `<symbol at filename:linenum>' if set. */
120 static int print_symbol_filename = 0;
122 show_print_symbol_filename (struct ui_file *file, int from_tty,
123 struct cmd_list_element *c, const char *value)
125 fprintf_filtered (file, _("Printing of source filename and "
126 "line number with <symbol> is %s.\n"),
130 /* Number of auto-display expression currently being displayed.
131 So that we can disable it if we get an error or a signal within it.
132 -1 when not doing one. */
134 int current_display_number;
138 /* Chain link to next auto-display item. */
139 struct display *next;
141 /* The expression as the user typed it. */
144 /* Expression to be evaluated and displayed. */
145 struct expression *exp;
147 /* Item number of this auto-display item. */
150 /* Display format specified. */
151 struct format_data format;
153 /* Program space associated with `block'. */
154 struct program_space *pspace;
156 /* Innermost block required by this expression when evaluated. */
159 /* Status of this display (enabled or disabled). */
163 /* Chain of expressions whose values should be displayed
164 automatically each time the program stops. */
166 static struct display *display_chain;
168 static int display_number;
170 /* Prototypes for exported functions. */
172 void output_command (char *, int);
174 void _initialize_printcmd (void);
176 /* Prototypes for local functions. */
178 static void do_one_display (struct display *);
181 /* Decode a format specification. *STRING_PTR should point to it.
182 OFORMAT and OSIZE are used as defaults for the format and size
183 if none are given in the format specification.
184 If OSIZE is zero, then the size field of the returned value
185 should be set only if a size is explicitly specified by the
187 The structure returned describes all the data
188 found in the specification. In addition, *STRING_PTR is advanced
189 past the specification and past all whitespace following it. */
191 static struct format_data
192 decode_format (char **string_ptr, int oformat, int osize)
194 struct format_data val;
195 char *p = *string_ptr;
202 if (*p >= '0' && *p <= '9')
203 val.count = atoi (p);
204 while (*p >= '0' && *p <= '9')
207 /* Now process size or format letters that follow. */
211 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
218 else if (*p >= 'a' && *p <= 'z')
224 while (*p == ' ' || *p == '\t')
228 /* Set defaults for format and size if not specified. */
229 if (val.format == '?')
233 /* Neither has been specified. */
234 val.format = oformat;
238 /* If a size is specified, any format makes a reasonable
239 default except 'i'. */
240 val.format = oformat == 'i' ? 'x' : oformat;
242 else if (val.size == '?')
246 /* Pick the appropriate size for an address. This is deferred
247 until do_examine when we know the actual architecture to use.
248 A special size value of 'a' is used to indicate this case. */
249 val.size = osize ? 'a' : osize;
252 /* Floating point has to be word or giantword. */
253 if (osize == 'w' || osize == 'g')
256 /* Default it to giantword if the last used size is not
258 val.size = osize ? 'g' : osize;
261 /* Characters default to one byte. */
262 val.size = osize ? 'b' : osize;
265 /* Display strings with byte size chars unless explicitly
271 /* The default is the size most recently specified. */
278 /* Print value VAL on stream according to OPTIONS.
279 Do not end with a newline.
280 SIZE is the letter for the size of datum being printed.
281 This is used to pad hex numbers so they line up. SIZE is 0
282 for print / output and set for examine. */
285 print_formatted (struct value *val, int size,
286 const struct value_print_options *options,
287 struct ui_file *stream)
289 struct type *type = check_typedef (value_type (val));
290 int len = TYPE_LENGTH (type);
292 if (VALUE_LVAL (val) == lval_memory)
293 next_address = value_address (val) + len;
297 switch (options->format)
301 struct type *elttype = value_type (val);
303 next_address = (value_address (val)
304 + val_print_string (elttype, NULL,
305 value_address (val), -1,
306 stream, options) * len);
311 /* We often wrap here if there are long symbolic names. */
313 next_address = (value_address (val)
314 + gdb_print_insn (get_type_arch (type),
315 value_address (val), stream,
316 &branch_delay_insns));
321 if (options->format == 0 || options->format == 's'
322 || TYPE_CODE (type) == TYPE_CODE_REF
323 || TYPE_CODE (type) == TYPE_CODE_ARRAY
324 || TYPE_CODE (type) == TYPE_CODE_STRING
325 || TYPE_CODE (type) == TYPE_CODE_STRUCT
326 || TYPE_CODE (type) == TYPE_CODE_UNION
327 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
328 value_print (val, stream, options);
330 /* User specified format, so don't look to the the type to
331 tell us what to do. */
332 val_print_scalar_formatted (type,
333 value_contents_for_printing (val),
334 value_embedded_offset (val),
336 options, size, stream);
339 /* Return builtin floating point type of same length as TYPE.
340 If no such type is found, return TYPE itself. */
342 float_type_from_length (struct type *type)
344 struct gdbarch *gdbarch = get_type_arch (type);
345 const struct builtin_type *builtin = builtin_type (gdbarch);
346 unsigned int len = TYPE_LENGTH (type);
348 if (len == TYPE_LENGTH (builtin->builtin_float))
349 type = builtin->builtin_float;
350 else if (len == TYPE_LENGTH (builtin->builtin_double))
351 type = builtin->builtin_double;
352 else if (len == TYPE_LENGTH (builtin->builtin_long_double))
353 type = builtin->builtin_long_double;
358 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
359 according to OPTIONS and SIZE on STREAM. Formats s and i are not
360 supported at this level. */
363 print_scalar_formatted (const void *valaddr, struct type *type,
364 const struct value_print_options *options,
365 int size, struct ui_file *stream)
367 struct gdbarch *gdbarch = get_type_arch (type);
368 LONGEST val_long = 0;
369 unsigned int len = TYPE_LENGTH (type);
370 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
372 /* String printing should go through val_print_scalar_formatted. */
373 gdb_assert (options->format != 's');
375 if (len > sizeof(LONGEST) &&
376 (TYPE_CODE (type) == TYPE_CODE_INT
377 || TYPE_CODE (type) == TYPE_CODE_ENUM))
379 switch (options->format)
382 print_octal_chars (stream, valaddr, len, byte_order);
386 print_decimal_chars (stream, valaddr, len, byte_order);
389 print_binary_chars (stream, valaddr, len, byte_order);
392 print_hex_chars (stream, valaddr, len, byte_order);
395 print_char_chars (stream, type, valaddr, len, byte_order);
402 if (options->format != 'f')
403 val_long = unpack_long (type, valaddr);
405 /* If the value is a pointer, and pointers and addresses are not the
406 same, then at this point, the value's length (in target bytes) is
407 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
408 if (TYPE_CODE (type) == TYPE_CODE_PTR)
409 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
411 /* If we are printing it as unsigned, truncate it in case it is actually
412 a negative signed value (e.g. "print/u (short)-1" should print 65535
413 (if shorts are 16 bits) instead of 4294967295). */
414 if (options->format != 'd' || TYPE_UNSIGNED (type))
416 if (len < sizeof (LONGEST))
417 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
420 switch (options->format)
425 /* No size specified, like in print. Print varying # of digits. */
426 print_longest (stream, 'x', 1, val_long);
435 print_longest (stream, size, 1, val_long);
438 error (_("Undefined output size \"%c\"."), size);
443 print_longest (stream, 'd', 1, val_long);
447 print_longest (stream, 'u', 0, val_long);
452 print_longest (stream, 'o', 1, val_long);
454 fprintf_filtered (stream, "0");
459 CORE_ADDR addr = unpack_pointer (type, valaddr);
461 print_address (gdbarch, addr, stream);
467 struct value_print_options opts = *options;
470 if (TYPE_UNSIGNED (type))
471 type = builtin_type (gdbarch)->builtin_true_unsigned_char;
473 type = builtin_type (gdbarch)->builtin_true_char;
475 value_print (value_from_longest (type, val_long), stream, &opts);
480 type = float_type_from_length (type);
481 print_floating (valaddr, type, stream);
485 internal_error (__FILE__, __LINE__,
486 _("failed internal consistency check"));
489 /* Binary; 't' stands for "two". */
491 char bits[8 * (sizeof val_long) + 1];
492 char buf[8 * (sizeof val_long) + 32];
497 width = 8 * (sizeof val_long);
514 error (_("Undefined output size \"%c\"."), size);
520 bits[width] = (val_long & 1) ? '1' : '0';
525 while (*cp && *cp == '0')
531 fputs_filtered (buf, stream);
536 error (_("Undefined output format \"%c\"."), options->format);
540 /* Specify default address for `x' command.
541 The `info lines' command uses this. */
544 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
546 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
548 next_gdbarch = gdbarch;
551 /* Make address available to the user as $_. */
552 set_internalvar (lookup_internalvar ("_"),
553 value_from_pointer (ptr_type, addr));
556 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
557 after LEADIN. Print nothing if no symbolic name is found nearby.
558 Optionally also print source file and line number, if available.
559 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
560 or to interpret it as a possible C++ name and convert it back to source
561 form. However note that DO_DEMANGLE can be overridden by the specific
562 settings of the demangle and asm_demangle variables. */
565 print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
566 struct ui_file *stream,
567 int do_demangle, char *leadin)
570 char *filename = NULL;
575 /* Throw away both name and filename. */
576 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
577 make_cleanup (free_current_contents, &filename);
579 if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
580 &filename, &line, &unmapped))
582 do_cleanups (cleanup_chain);
586 fputs_filtered (leadin, stream);
588 fputs_filtered ("<*", stream);
590 fputs_filtered ("<", stream);
591 fputs_filtered (name, stream);
593 fprintf_filtered (stream, "+%u", (unsigned int) offset);
595 /* Append source filename and line number if desired. Give specific
596 line # of this addr, if we have it; else line # of the nearest symbol. */
597 if (print_symbol_filename && filename != NULL)
600 fprintf_filtered (stream, " at %s:%d", filename, line);
602 fprintf_filtered (stream, " in %s", filename);
605 fputs_filtered ("*>", stream);
607 fputs_filtered (">", stream);
609 do_cleanups (cleanup_chain);
612 /* Given an address ADDR return all the elements needed to print the
613 address in a symbolic form. NAME can be mangled or not depending
614 on DO_DEMANGLE (and also on the asm_demangle global variable,
615 manipulated via ''set print asm-demangle''). Return 0 in case of
616 success, when all the info in the OUT paramters is valid. Return 1
619 build_address_symbolic (struct gdbarch *gdbarch,
620 CORE_ADDR addr, /* IN */
621 int do_demangle, /* IN */
622 char **name, /* OUT */
623 int *offset, /* OUT */
624 char **filename, /* OUT */
626 int *unmapped) /* OUT */
628 struct minimal_symbol *msymbol;
629 struct symbol *symbol;
630 CORE_ADDR name_location = 0;
631 struct obj_section *section = NULL;
632 char *name_temp = "";
634 /* Let's say it is mapped (not unmapped). */
637 /* Determine if the address is in an overlay, and whether it is
639 if (overlay_debugging)
641 section = find_pc_overlay (addr);
642 if (pc_in_unmapped_range (addr, section))
645 addr = overlay_mapped_address (addr, section);
649 /* First try to find the address in the symbol table, then
650 in the minsyms. Take the closest one. */
652 /* This is defective in the sense that it only finds text symbols. So
653 really this is kind of pointless--we should make sure that the
654 minimal symbols have everything we need (by changing that we could
655 save some memory, but for many debug format--ELF/DWARF or
656 anything/stabs--it would be inconvenient to eliminate those minimal
658 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
659 symbol = find_pc_sect_function (addr, section);
663 /* If this is a function (i.e. a code address), strip out any
664 non-address bits. For instance, display a pointer to the
665 first instruction of a Thumb function as <function>; the
666 second instruction will be <function+2>, even though the
667 pointer is <function+3>. This matches the ISA behavior. */
668 addr = gdbarch_addr_bits_remove (gdbarch, addr);
670 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
671 if (do_demangle || asm_demangle)
672 name_temp = SYMBOL_PRINT_NAME (symbol);
674 name_temp = SYMBOL_LINKAGE_NAME (symbol);
679 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
681 /* The msymbol is closer to the address than the symbol;
682 use the msymbol instead. */
684 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
685 if (do_demangle || asm_demangle)
686 name_temp = SYMBOL_PRINT_NAME (msymbol);
688 name_temp = SYMBOL_LINKAGE_NAME (msymbol);
691 if (symbol == NULL && msymbol == NULL)
694 /* If the nearest symbol is too far away, don't print anything symbolic. */
696 /* For when CORE_ADDR is larger than unsigned int, we do math in
697 CORE_ADDR. But when we detect unsigned wraparound in the
698 CORE_ADDR math, we ignore this test and print the offset,
699 because addr+max_symbolic_offset has wrapped through the end
700 of the address space back to the beginning, giving bogus comparison. */
701 if (addr > name_location + max_symbolic_offset
702 && name_location + max_symbolic_offset > name_location)
705 *offset = addr - name_location;
707 *name = xstrdup (name_temp);
709 if (print_symbol_filename)
711 struct symtab_and_line sal;
713 sal = find_pc_sect_line (addr, section, 0);
717 *filename = xstrdup (sal.symtab->filename);
725 /* Print address ADDR symbolically on STREAM.
726 First print it as a number. Then perhaps print
727 <SYMBOL + OFFSET> after the number. */
730 print_address (struct gdbarch *gdbarch,
731 CORE_ADDR addr, struct ui_file *stream)
733 fputs_filtered (paddress (gdbarch, addr), stream);
734 print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
737 /* Return a prefix for instruction address:
738 "=> " for current instruction, else " ". */
741 pc_prefix (CORE_ADDR addr)
743 if (has_stack_frames ())
745 struct frame_info *frame;
748 frame = get_selected_frame (NULL);
749 pc = get_frame_pc (frame);
757 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
758 controls whether to print the symbolic name "raw" or demangled.
759 Global setting "addressprint" controls whether to print hex address
763 print_address_demangle (struct gdbarch *gdbarch, CORE_ADDR addr,
764 struct ui_file *stream, int do_demangle)
766 struct value_print_options opts;
768 get_user_print_options (&opts);
771 fprintf_filtered (stream, "0");
773 else if (opts.addressprint)
775 fputs_filtered (paddress (gdbarch, addr), stream);
776 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
780 print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
785 /* Examine data at address ADDR in format FMT.
786 Fetch it from memory and print on gdb_stdout. */
789 do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
794 struct type *val_type = NULL;
797 struct value_print_options opts;
802 next_gdbarch = gdbarch;
805 /* Instruction format implies fetch single bytes
806 regardless of the specified size.
807 The case of strings is handled in decode_format, only explicit
808 size operator are not changed to 'b'. */
814 /* Pick the appropriate size for an address. */
815 if (gdbarch_ptr_bit (next_gdbarch) == 64)
817 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
819 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
822 /* Bad value for gdbarch_ptr_bit. */
823 internal_error (__FILE__, __LINE__,
824 _("failed internal consistency check"));
828 val_type = builtin_type (next_gdbarch)->builtin_int8;
829 else if (size == 'h')
830 val_type = builtin_type (next_gdbarch)->builtin_int16;
831 else if (size == 'w')
832 val_type = builtin_type (next_gdbarch)->builtin_int32;
833 else if (size == 'g')
834 val_type = builtin_type (next_gdbarch)->builtin_int64;
838 struct type *char_type = NULL;
840 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
841 if type is not found. */
843 char_type = builtin_type (next_gdbarch)->builtin_char16;
844 else if (size == 'w')
845 char_type = builtin_type (next_gdbarch)->builtin_char32;
847 val_type = char_type;
850 if (size != '\0' && size != 'b')
851 warning (_("Unable to display strings with "
852 "size '%c', using 'b' instead."), size);
854 val_type = builtin_type (next_gdbarch)->builtin_int8;
863 if (format == 's' || format == 'i')
866 get_formatted_print_options (&opts, format);
868 /* Print as many objects as specified in COUNT, at most maxelts per line,
869 with the address of the next one at the start of each line. */
875 fputs_filtered (pc_prefix (next_address), gdb_stdout);
876 print_address (next_gdbarch, next_address, gdb_stdout);
877 printf_filtered (":");
882 printf_filtered ("\t");
883 /* Note that print_formatted sets next_address for the next
885 last_examine_address = next_address;
887 if (last_examine_value)
888 value_free (last_examine_value);
890 /* The value to be displayed is not fetched greedily.
891 Instead, to avoid the possibility of a fetched value not
892 being used, its retrieval is delayed until the print code
893 uses it. When examining an instruction stream, the
894 disassembler will perform its own memory fetch using just
895 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
896 the disassembler be modified so that LAST_EXAMINE_VALUE
897 is left with the byte sequence from the last complete
898 instruction fetched from memory? */
899 last_examine_value = value_at_lazy (val_type, next_address);
901 if (last_examine_value)
902 release_value (last_examine_value);
904 print_formatted (last_examine_value, size, &opts, gdb_stdout);
906 /* Display any branch delay slots following the final insn. */
907 if (format == 'i' && count == 1)
908 count += branch_delay_insns;
910 printf_filtered ("\n");
911 gdb_flush (gdb_stdout);
916 validate_format (struct format_data fmt, char *cmdname)
919 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
921 error (_("Item count other than 1 is meaningless in \"%s\" command."),
923 if (fmt.format == 'i')
924 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
925 fmt.format, cmdname);
928 /* Evaluate string EXP as an expression in the current language and
929 print the resulting value. EXP may contain a format specifier as the
930 first argument ("/x myvar" for example, to print myvar in hex). */
933 print_command_1 (char *exp, int inspect, int voidprint)
935 struct expression *expr;
936 struct cleanup *old_chain = 0;
939 struct format_data fmt;
942 if (exp && *exp == '/')
945 fmt = decode_format (&exp, last_format, 0);
946 validate_format (fmt, "print");
947 last_format = format = fmt.format;
959 expr = parse_expression (exp);
960 old_chain = make_cleanup (free_current_contents, &expr);
962 val = evaluate_expression (expr);
965 val = access_value_history (0);
967 if (voidprint || (val && value_type (val) &&
968 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
970 struct value_print_options opts;
971 int histindex = record_latest_value (val);
974 annotate_value_history_begin (histindex, value_type (val));
976 annotate_value_begin (value_type (val));
979 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
981 else if (histindex >= 0)
982 printf_filtered ("$%d = ", histindex);
985 annotate_value_history_value ();
987 get_formatted_print_options (&opts, format);
988 opts.inspect_it = inspect;
991 print_formatted (val, fmt.size, &opts, gdb_stdout);
992 printf_filtered ("\n");
995 annotate_value_history_end ();
997 annotate_value_end ();
1000 printf_unfiltered ("\") )\030");
1004 do_cleanups (old_chain);
1008 print_command (char *exp, int from_tty)
1010 print_command_1 (exp, 0, 1);
1013 /* Same as print, except in epoch, it gets its own window. */
1015 inspect_command (char *exp, int from_tty)
1017 extern int epoch_interface;
1019 print_command_1 (exp, epoch_interface, 1);
1022 /* Same as print, except it doesn't print void results. */
1024 call_command (char *exp, int from_tty)
1026 print_command_1 (exp, 0, 0);
1030 output_command (char *exp, int from_tty)
1032 struct expression *expr;
1033 struct cleanup *old_chain;
1036 struct format_data fmt;
1037 struct value_print_options opts;
1042 if (exp && *exp == '/')
1045 fmt = decode_format (&exp, 0, 0);
1046 validate_format (fmt, "output");
1047 format = fmt.format;
1050 expr = parse_expression (exp);
1051 old_chain = make_cleanup (free_current_contents, &expr);
1053 val = evaluate_expression (expr);
1055 annotate_value_begin (value_type (val));
1057 get_formatted_print_options (&opts, format);
1059 print_formatted (val, fmt.size, &opts, gdb_stdout);
1061 annotate_value_end ();
1064 gdb_flush (gdb_stdout);
1066 do_cleanups (old_chain);
1070 set_command (char *exp, int from_tty)
1072 struct expression *expr = parse_expression (exp);
1073 struct cleanup *old_chain =
1074 make_cleanup (free_current_contents, &expr);
1076 evaluate_expression (expr);
1077 do_cleanups (old_chain);
1081 sym_info (char *arg, int from_tty)
1083 struct minimal_symbol *msymbol;
1084 struct objfile *objfile;
1085 struct obj_section *osect;
1086 CORE_ADDR addr, sect_addr;
1088 unsigned int offset;
1091 error_no_arg (_("address"));
1093 addr = parse_and_eval_address (arg);
1094 ALL_OBJSECTIONS (objfile, osect)
1096 /* Only process each object file once, even if there's a separate
1098 if (objfile->separate_debug_objfile_backlink)
1101 sect_addr = overlay_mapped_address (addr, osect);
1103 if (obj_section_addr (osect) <= sect_addr
1104 && sect_addr < obj_section_endaddr (osect)
1105 && (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, osect)))
1107 const char *obj_name, *mapped, *sec_name, *msym_name;
1109 struct cleanup *old_chain;
1112 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1113 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1114 sec_name = osect->the_bfd_section->name;
1115 msym_name = SYMBOL_PRINT_NAME (msymbol);
1117 /* Don't print the offset if it is zero.
1118 We assume there's no need to handle i18n of "sym + offset". */
1120 loc_string = xstrprintf ("%s + %u", msym_name, offset);
1122 loc_string = xstrprintf ("%s", msym_name);
1124 /* Use a cleanup to free loc_string in case the user quits
1125 a pagination request inside printf_filtered. */
1126 old_chain = make_cleanup (xfree, loc_string);
1128 gdb_assert (osect->objfile && osect->objfile->name);
1129 obj_name = osect->objfile->name;
1131 if (MULTI_OBJFILE_P ())
1132 if (pc_in_unmapped_range (addr, osect))
1133 if (section_is_overlay (osect))
1134 printf_filtered (_("%s in load address range of "
1135 "%s overlay section %s of %s\n"),
1136 loc_string, mapped, sec_name, obj_name);
1138 printf_filtered (_("%s in load address range of "
1139 "section %s of %s\n"),
1140 loc_string, sec_name, obj_name);
1142 if (section_is_overlay (osect))
1143 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1144 loc_string, mapped, sec_name, obj_name);
1146 printf_filtered (_("%s in section %s of %s\n"),
1147 loc_string, sec_name, obj_name);
1149 if (pc_in_unmapped_range (addr, osect))
1150 if (section_is_overlay (osect))
1151 printf_filtered (_("%s in load address range of %s overlay "
1153 loc_string, mapped, sec_name);
1155 printf_filtered (_("%s in load address range of section %s\n"),
1156 loc_string, sec_name);
1158 if (section_is_overlay (osect))
1159 printf_filtered (_("%s in %s overlay section %s\n"),
1160 loc_string, mapped, sec_name);
1162 printf_filtered (_("%s in section %s\n"),
1163 loc_string, sec_name);
1165 do_cleanups (old_chain);
1169 printf_filtered (_("No symbol matches %s.\n"), arg);
1173 address_info (char *exp, int from_tty)
1175 struct gdbarch *gdbarch;
1178 struct minimal_symbol *msymbol;
1180 struct obj_section *section;
1181 CORE_ADDR load_addr, context_pc = 0;
1182 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1183 if exp is a field of `this'. */
1186 error (_("Argument required."));
1188 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
1189 &is_a_field_of_this);
1192 if (is_a_field_of_this)
1194 printf_filtered ("Symbol \"");
1195 fprintf_symbol_filtered (gdb_stdout, exp,
1196 current_language->la_language, DMGL_ANSI);
1197 printf_filtered ("\" is a field of the local class variable ");
1198 if (current_language->la_language == language_objc)
1199 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1201 printf_filtered ("`this'\n");
1205 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1207 if (msymbol != NULL)
1209 gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
1210 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1212 printf_filtered ("Symbol \"");
1213 fprintf_symbol_filtered (gdb_stdout, exp,
1214 current_language->la_language, DMGL_ANSI);
1215 printf_filtered ("\" is at ");
1216 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1217 printf_filtered (" in a file compiled without debugging");
1218 section = SYMBOL_OBJ_SECTION (msymbol);
1219 if (section_is_overlay (section))
1221 load_addr = overlay_unmapped_address (load_addr, section);
1222 printf_filtered (",\n -- loaded at ");
1223 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1224 printf_filtered (" in overlay section %s",
1225 section->the_bfd_section->name);
1227 printf_filtered (".\n");
1230 error (_("No symbol \"%s\" in current context."), exp);
1234 printf_filtered ("Symbol \"");
1235 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
1236 current_language->la_language, DMGL_ANSI);
1237 printf_filtered ("\" is ");
1238 val = SYMBOL_VALUE (sym);
1239 section = SYMBOL_OBJ_SECTION (sym);
1240 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
1242 switch (SYMBOL_CLASS (sym))
1245 case LOC_CONST_BYTES:
1246 printf_filtered ("constant");
1250 printf_filtered ("a label at address ");
1251 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1252 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1253 if (section_is_overlay (section))
1255 load_addr = overlay_unmapped_address (load_addr, section);
1256 printf_filtered (",\n -- loaded at ");
1257 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1258 printf_filtered (" in overlay section %s",
1259 section->the_bfd_section->name);
1264 /* FIXME: cagney/2004-01-26: It should be possible to
1265 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1266 Unfortunately DWARF 2 stores the frame-base (instead of the
1267 function) location in a function's symbol. Oops! For the
1268 moment enable this when/where applicable. */
1269 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1274 /* GDBARCH is the architecture associated with the objfile the symbol
1275 is defined in; the target architecture may be different, and may
1276 provide additional registers. However, we do not know the target
1277 architecture at this point. We assume the objfile architecture
1278 will contain all the standard registers that occur in debug info
1280 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1282 if (SYMBOL_IS_ARGUMENT (sym))
1283 printf_filtered (_("an argument in register %s"),
1284 gdbarch_register_name (gdbarch, regno));
1286 printf_filtered (_("a variable in register %s"),
1287 gdbarch_register_name (gdbarch, regno));
1291 printf_filtered (_("static storage at address "));
1292 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1293 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1294 if (section_is_overlay (section))
1296 load_addr = overlay_unmapped_address (load_addr, section);
1297 printf_filtered (_(",\n -- loaded at "));
1298 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1299 printf_filtered (_(" in overlay section %s"),
1300 section->the_bfd_section->name);
1304 case LOC_REGPARM_ADDR:
1305 /* Note comment at LOC_REGISTER. */
1306 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1307 printf_filtered (_("address of an argument in register %s"),
1308 gdbarch_register_name (gdbarch, regno));
1312 printf_filtered (_("an argument at offset %ld"), val);
1316 printf_filtered (_("a local variable at frame offset %ld"), val);
1320 printf_filtered (_("a reference argument at offset %ld"), val);
1324 printf_filtered (_("a typedef"));
1328 printf_filtered (_("a function at address "));
1329 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1330 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1331 if (section_is_overlay (section))
1333 load_addr = overlay_unmapped_address (load_addr, section);
1334 printf_filtered (_(",\n -- loaded at "));
1335 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1336 printf_filtered (_(" in overlay section %s"),
1337 section->the_bfd_section->name);
1341 case LOC_UNRESOLVED:
1343 struct minimal_symbol *msym;
1345 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym), NULL, NULL);
1347 printf_filtered ("unresolved");
1350 section = SYMBOL_OBJ_SECTION (msym);
1351 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1354 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1355 printf_filtered (_("a thread-local variable at offset %s "
1356 "in the thread-local storage for `%s'"),
1357 paddress (gdbarch, load_addr),
1358 section->objfile->name);
1361 printf_filtered (_("static storage at address "));
1362 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1363 if (section_is_overlay (section))
1365 load_addr = overlay_unmapped_address (load_addr, section);
1366 printf_filtered (_(",\n -- loaded at "));
1367 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1368 printf_filtered (_(" in overlay section %s"),
1369 section->the_bfd_section->name);
1376 case LOC_OPTIMIZED_OUT:
1377 printf_filtered (_("optimized out"));
1381 printf_filtered (_("of unknown (botched) type"));
1384 printf_filtered (".\n");
1389 x_command (char *exp, int from_tty)
1391 struct expression *expr;
1392 struct format_data fmt;
1393 struct cleanup *old_chain;
1396 fmt.format = last_format ? last_format : 'x';
1397 fmt.size = last_size;
1401 if (exp && *exp == '/')
1404 fmt = decode_format (&exp, last_format, last_size);
1407 /* If we have an expression, evaluate it and use it as the address. */
1409 if (exp != 0 && *exp != 0)
1411 expr = parse_expression (exp);
1412 /* Cause expression not to be there any more if this command is
1413 repeated with Newline. But don't clobber a user-defined
1414 command's definition. */
1417 old_chain = make_cleanup (free_current_contents, &expr);
1418 val = evaluate_expression (expr);
1419 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1420 val = coerce_ref (val);
1421 /* In rvalue contexts, such as this, functions are coerced into
1422 pointers to functions. This makes "x/i main" work. */
1423 if (/* last_format == 'i' && */
1424 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1425 && VALUE_LVAL (val) == lval_memory)
1426 next_address = value_address (val);
1428 next_address = value_as_address (val);
1430 next_gdbarch = expr->gdbarch;
1431 do_cleanups (old_chain);
1435 error_no_arg (_("starting display address"));
1437 do_examine (fmt, next_gdbarch, next_address);
1439 /* If the examine succeeds, we remember its size and format for next
1440 time. Set last_size to 'b' for strings. */
1441 if (fmt.format == 's')
1444 last_size = fmt.size;
1445 last_format = fmt.format;
1447 /* Set a couple of internal variables if appropriate. */
1448 if (last_examine_value)
1450 /* Make last address examined available to the user as $_. Use
1451 the correct pointer type. */
1452 struct type *pointer_type
1453 = lookup_pointer_type (value_type (last_examine_value));
1454 set_internalvar (lookup_internalvar ("_"),
1455 value_from_pointer (pointer_type,
1456 last_examine_address));
1458 /* Make contents of last address examined available to the user
1459 as $__. If the last value has not been fetched from memory
1460 then don't fetch it now; instead mark it by voiding the $__
1462 if (value_lazy (last_examine_value))
1463 clear_internalvar (lookup_internalvar ("__"));
1465 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1470 /* Add an expression to the auto-display chain.
1471 Specify the expression. */
1474 display_command (char *exp, int from_tty)
1476 struct format_data fmt;
1477 struct expression *expr;
1478 struct display *new;
1482 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1484 if (tui_active && exp != NULL && *exp == '$')
1485 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1499 fmt = decode_format (&exp, 0, 0);
1500 if (fmt.size && fmt.format == 0)
1502 if (fmt.format == 'i' || fmt.format == 's')
1513 innermost_block = NULL;
1514 expr = parse_expression (exp);
1516 new = (struct display *) xmalloc (sizeof (struct display));
1518 new->exp_string = xstrdup (exp);
1520 new->block = innermost_block;
1521 new->pspace = current_program_space;
1522 new->next = display_chain;
1523 new->number = ++display_number;
1526 display_chain = new;
1528 if (from_tty && target_has_execution)
1529 do_one_display (new);
1536 free_display (struct display *d)
1538 xfree (d->exp_string);
1543 /* Clear out the display_chain. Done when new symtabs are loaded,
1544 since this invalidates the types stored in many expressions. */
1547 clear_displays (void)
1551 while ((d = display_chain) != NULL)
1553 display_chain = d->next;
1558 /* Delete the auto-display number NUM. */
1561 delete_display (int num)
1563 struct display *d1, *d;
1566 error (_("No display number %d."), num);
1568 if (display_chain->number == num)
1571 display_chain = d1->next;
1575 for (d = display_chain;; d = d->next)
1578 error (_("No display number %d."), num);
1579 if (d->next->number == num)
1589 /* Delete some values from the auto-display chain.
1590 Specify the element numbers. */
1593 undisplay_command (char *args, int from_tty)
1601 if (query (_("Delete all auto-display expressions? ")))
1610 while (*p1 >= '0' && *p1 <= '9')
1612 if (*p1 && *p1 != ' ' && *p1 != '\t')
1613 error (_("Arguments must be display numbers."));
1617 delete_display (num);
1620 while (*p == ' ' || *p == '\t')
1626 /* Display a single auto-display.
1627 Do nothing if the display cannot be printed in the current context,
1628 or if the display is disabled. */
1631 do_one_display (struct display *d)
1633 int within_current_scope;
1635 if (d->enabled_p == 0)
1638 /* The expression carries the architecture that was used at parse time.
1639 This is a problem if the expression depends on architecture features
1640 (e.g. register numbers), and the current architecture is now different.
1641 For example, a display statement like "display/i $pc" is expected to
1642 display the PC register of the current architecture, not the arch at
1643 the time the display command was given. Therefore, we re-parse the
1644 expression if the current architecture has changed. */
1645 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
1654 volatile struct gdb_exception ex;
1656 TRY_CATCH (ex, RETURN_MASK_ALL)
1658 innermost_block = NULL;
1659 d->exp = parse_expression (d->exp_string);
1660 d->block = innermost_block;
1664 /* Can't re-parse the expression. Disable this display item. */
1666 warning (_("Unable to display \"%s\": %s"),
1667 d->exp_string, ex.message);
1674 if (d->pspace == current_program_space)
1675 within_current_scope = contained_in (get_selected_block (0), d->block);
1677 within_current_scope = 0;
1680 within_current_scope = 1;
1681 if (!within_current_scope)
1684 current_display_number = d->number;
1686 annotate_display_begin ();
1687 printf_filtered ("%d", d->number);
1688 annotate_display_number_end ();
1689 printf_filtered (": ");
1695 annotate_display_format ();
1697 printf_filtered ("x/");
1698 if (d->format.count != 1)
1699 printf_filtered ("%d", d->format.count);
1700 printf_filtered ("%c", d->format.format);
1701 if (d->format.format != 'i' && d->format.format != 's')
1702 printf_filtered ("%c", d->format.size);
1703 printf_filtered (" ");
1705 annotate_display_expression ();
1707 puts_filtered (d->exp_string);
1708 annotate_display_expression_end ();
1710 if (d->format.count != 1 || d->format.format == 'i')
1711 printf_filtered ("\n");
1713 printf_filtered (" ");
1715 val = evaluate_expression (d->exp);
1716 addr = value_as_address (val);
1717 if (d->format.format == 'i')
1718 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
1720 annotate_display_value ();
1722 do_examine (d->format, d->exp->gdbarch, addr);
1726 struct value_print_options opts;
1728 annotate_display_format ();
1730 if (d->format.format)
1731 printf_filtered ("/%c ", d->format.format);
1733 annotate_display_expression ();
1735 puts_filtered (d->exp_string);
1736 annotate_display_expression_end ();
1738 printf_filtered (" = ");
1740 annotate_display_expression ();
1742 get_formatted_print_options (&opts, d->format.format);
1743 opts.raw = d->format.raw;
1744 print_formatted (evaluate_expression (d->exp),
1745 d->format.size, &opts, gdb_stdout);
1746 printf_filtered ("\n");
1749 annotate_display_end ();
1751 gdb_flush (gdb_stdout);
1752 current_display_number = -1;
1755 /* Display all of the values on the auto-display chain which can be
1756 evaluated in the current scope. */
1763 for (d = display_chain; d; d = d->next)
1767 /* Delete the auto-display which we were in the process of displaying.
1768 This is done when there is an error or a signal. */
1771 disable_display (int num)
1775 for (d = display_chain; d; d = d->next)
1776 if (d->number == num)
1781 printf_unfiltered (_("No display number %d.\n"), num);
1785 disable_current_display (void)
1787 if (current_display_number >= 0)
1789 disable_display (current_display_number);
1790 fprintf_unfiltered (gdb_stderr,
1791 _("Disabling display %d to "
1792 "avoid infinite recursion.\n"),
1793 current_display_number);
1795 current_display_number = -1;
1799 display_info (char *ignore, int from_tty)
1804 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1806 printf_filtered (_("Auto-display expressions now in effect:\n\
1807 Num Enb Expression\n"));
1809 for (d = display_chain; d; d = d->next)
1811 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1813 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1815 else if (d->format.format)
1816 printf_filtered ("/%c ", d->format.format);
1817 puts_filtered (d->exp_string);
1818 if (d->block && !contained_in (get_selected_block (0), d->block))
1819 printf_filtered (_(" (cannot be evaluated in the current context)"));
1820 printf_filtered ("\n");
1821 gdb_flush (gdb_stdout);
1826 enable_display (char *args, int from_tty)
1835 for (d = display_chain; d; d = d->next)
1842 while (*p1 >= '0' && *p1 <= '9')
1844 if (*p1 && *p1 != ' ' && *p1 != '\t')
1845 error (_("Arguments must be display numbers."));
1849 for (d = display_chain; d; d = d->next)
1850 if (d->number == num)
1855 printf_unfiltered (_("No display number %d.\n"), num);
1858 while (*p == ' ' || *p == '\t')
1864 disable_display_command (char *args, int from_tty)
1872 for (d = display_chain; d; d = d->next)
1879 while (*p1 >= '0' && *p1 <= '9')
1881 if (*p1 && *p1 != ' ' && *p1 != '\t')
1882 error (_("Arguments must be display numbers."));
1884 disable_display (atoi (p));
1887 while (*p == ' ' || *p == '\t')
1892 /* display_chain items point to blocks and expressions. Some expressions in
1893 turn may point to symbols.
1894 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1895 obstack_free'd when a shared library is unloaded.
1896 Clear pointers that are about to become dangling.
1897 Both .exp and .block fields will be restored next time we need to display
1898 an item by re-parsing .exp_string field in the new execution context. */
1901 clear_dangling_display_expressions (struct so_list *solib)
1903 struct objfile *objfile = solib->objfile;
1906 /* With no symbol file we cannot have a block or expression from it. */
1907 if (objfile == NULL)
1909 if (objfile->separate_debug_objfile_backlink)
1910 objfile = objfile->separate_debug_objfile_backlink;
1911 gdb_assert (objfile->pspace == solib->pspace);
1913 for (d = display_chain; d != NULL; d = d->next)
1915 if (d->pspace != solib->pspace)
1918 if (lookup_objfile_from_block (d->block) == objfile
1919 || (d->exp && exp_uses_objfile (d->exp, objfile)))
1929 /* Print the value in stack frame FRAME of a variable specified by a
1930 struct symbol. NAME is the name to print; if NULL then VAR's print
1931 name will be used. STREAM is the ui_file on which to print the
1932 value. INDENT specifies the number of indent levels to print
1933 before printing the variable name. */
1936 print_variable_and_value (const char *name, struct symbol *var,
1937 struct frame_info *frame,
1938 struct ui_file *stream, int indent)
1940 volatile struct gdb_exception except;
1943 name = SYMBOL_PRINT_NAME (var);
1945 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1946 TRY_CATCH (except, RETURN_MASK_ERROR)
1949 struct value_print_options opts;
1951 val = read_var_value (var, frame);
1952 get_user_print_options (&opts);
1953 common_val_print (val, stream, indent, &opts, current_language);
1955 if (except.reason < 0)
1956 fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
1958 fprintf_filtered (stream, "\n");
1961 /* printf "printf format string" ARG to STREAM. */
1964 ui_printf (char *arg, struct ui_file *stream)
1968 char *string = NULL;
1969 struct value **val_args;
1971 char *current_substring;
1973 int allocated_args = 20;
1974 struct cleanup *old_cleanups;
1976 val_args = xmalloc (allocated_args * sizeof (struct value *));
1977 old_cleanups = make_cleanup (free_current_contents, &val_args);
1980 error_no_arg (_("format-control string and values to print"));
1982 /* Skip white space before format string. */
1983 while (*s == ' ' || *s == '\t')
1986 /* A format string should follow, enveloped in double quotes. */
1988 error (_("Bad format string, missing '\"'."));
1990 /* Parse the format-control string and copy it into the string STRING,
1991 processing some kinds of escape sequence. */
1993 f = string = (char *) alloca (strlen (s) + 1);
2001 error (_("Bad format string, non-terminated '\"'."));
2034 /* ??? TODO: handle other escape sequences. */
2035 error (_("Unrecognized escape character \\%c in format string."),
2045 /* Skip over " and following space and comma. */
2048 while (*s == ' ' || *s == '\t')
2051 if (*s != ',' && *s != 0)
2052 error (_("Invalid argument syntax"));
2056 while (*s == ' ' || *s == '\t')
2059 /* Need extra space for the '\0's. Doubling the size is sufficient. */
2060 substrings = alloca (strlen (string) * 2);
2061 current_substring = substrings;
2064 /* Now scan the string for %-specs and see what kinds of args they want.
2065 argclass[I] classifies the %-specs so we can give printf_filtered
2066 something of the right size. */
2070 int_arg, long_arg, long_long_arg, ptr_arg,
2071 string_arg, wide_string_arg, wide_char_arg,
2072 double_arg, long_double_arg, decfloat_arg
2074 enum argclass *argclass;
2075 enum argclass this_argclass;
2080 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
2087 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
2088 int seen_space = 0, seen_plus = 0;
2089 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
2090 int seen_big_d = 0, seen_double_big_d = 0;
2093 /* Check the validity of the format specifier, and work
2094 out what argument it expects. We only accept C89
2095 format strings, with the exception of long long (which
2096 we autoconf for). */
2098 /* Skip over "%%". */
2105 /* The first part of a format specifier is a set of flag
2107 while (strchr ("0-+ #", *f))
2120 /* The next part of a format specifier is a width. */
2121 while (strchr ("0123456789", *f))
2124 /* The next part of a format specifier is a precision. */
2129 while (strchr ("0123456789", *f))
2133 /* The next part of a format specifier is a length modifier. */
2154 /* Decimal32 modifier. */
2160 /* Decimal64 and Decimal128 modifiers. */
2165 /* Check for a Decimal128. */
2169 seen_double_big_d = 1;
2185 if (seen_space || seen_plus)
2192 this_argclass = int_arg;
2193 else if (lcount == 1)
2194 this_argclass = long_arg;
2196 this_argclass = long_long_arg;
2203 this_argclass = lcount == 0 ? int_arg : wide_char_arg;
2204 if (lcount > 1 || seen_h || seen_big_l)
2206 if (seen_prec || seen_zero || seen_space || seen_plus)
2211 this_argclass = ptr_arg;
2212 if (lcount || seen_h || seen_big_l)
2214 if (seen_prec || seen_zero || seen_space || seen_plus)
2219 this_argclass = lcount == 0 ? string_arg : wide_string_arg;
2220 if (lcount > 1 || seen_h || seen_big_l)
2222 if (seen_zero || seen_space || seen_plus)
2231 if (seen_big_h || seen_big_d || seen_double_big_d)
2232 this_argclass = decfloat_arg;
2233 else if (seen_big_l)
2234 this_argclass = long_double_arg;
2236 this_argclass = double_arg;
2238 if (lcount || seen_h)
2243 error (_("`*' not supported for precision or width in printf"));
2246 error (_("Format specifier `n' not supported in printf"));
2249 error (_("Incomplete format specifier at end of format string"));
2252 error (_("Unrecognized format specifier '%c' in printf"), *f);
2256 error (_("Inappropriate modifiers to "
2257 "format specifier '%c' in printf"),
2262 if (lcount > 1 && USE_PRINTF_I64)
2264 /* Windows' printf does support long long, but not the usual way.
2265 Convert %lld to %I64d. */
2266 int length_before_ll = f - last_arg - 1 - lcount;
2268 strncpy (current_substring, last_arg, length_before_ll);
2269 strcpy (current_substring + length_before_ll, "I64");
2270 current_substring[length_before_ll + 3] =
2271 last_arg[length_before_ll + lcount];
2272 current_substring += length_before_ll + 4;
2274 else if (this_argclass == wide_string_arg
2275 || this_argclass == wide_char_arg)
2277 /* Convert %ls or %lc to %s. */
2278 int length_before_ls = f - last_arg - 2;
2280 strncpy (current_substring, last_arg, length_before_ls);
2281 strcpy (current_substring + length_before_ls, "s");
2282 current_substring += length_before_ls + 2;
2286 strncpy (current_substring, last_arg, f - last_arg);
2287 current_substring += f - last_arg;
2289 *current_substring++ = '\0';
2291 argclass[nargs_wanted++] = this_argclass;
2294 /* Now, parse all arguments and evaluate them.
2295 Store the VALUEs in VAL_ARGS. */
2301 if (nargs == allocated_args)
2302 val_args = (struct value **) xrealloc ((char *) val_args,
2303 (allocated_args *= 2)
2304 * sizeof (struct value *));
2306 val_args[nargs] = parse_to_comma_and_eval (&s1);
2314 if (nargs != nargs_wanted)
2315 error (_("Wrong number of arguments for specified format-string"));
2317 /* Now actually print them. */
2318 current_substring = substrings;
2319 for (i = 0; i < nargs; i++)
2321 switch (argclass[i])
2329 tem = value_as_address (val_args[i]);
2331 /* This is a %s argument. Find the length of the string. */
2337 read_memory (tem + j, &c, 1);
2342 /* Copy the string contents into a string inside GDB. */
2343 str = (gdb_byte *) alloca (j + 1);
2345 read_memory (tem, str, j);
2348 fprintf_filtered (stream, current_substring, (char *) str);
2351 case wide_string_arg:
2356 struct gdbarch *gdbarch
2357 = get_type_arch (value_type (val_args[i]));
2358 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2359 struct type *wctype = lookup_typename (current_language, gdbarch,
2360 "wchar_t", NULL, 0);
2361 int wcwidth = TYPE_LENGTH (wctype);
2362 gdb_byte *buf = alloca (wcwidth);
2363 struct obstack output;
2364 struct cleanup *inner_cleanup;
2366 tem = value_as_address (val_args[i]);
2368 /* This is a %s argument. Find the length of the string. */
2369 for (j = 0;; j += wcwidth)
2372 read_memory (tem + j, buf, wcwidth);
2373 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2377 /* Copy the string contents into a string inside GDB. */
2378 str = (gdb_byte *) alloca (j + wcwidth);
2380 read_memory (tem, str, j);
2381 memset (&str[j], 0, wcwidth);
2383 obstack_init (&output);
2384 inner_cleanup = make_cleanup_obstack_free (&output);
2386 convert_between_encodings (target_wide_charset (gdbarch),
2389 &output, translit_char);
2390 obstack_grow_str0 (&output, "");
2392 fprintf_filtered (stream, current_substring,
2393 obstack_base (&output));
2394 do_cleanups (inner_cleanup);
2399 struct gdbarch *gdbarch
2400 = get_type_arch (value_type (val_args[i]));
2401 struct type *wctype = lookup_typename (current_language, gdbarch,
2402 "wchar_t", NULL, 0);
2403 struct type *valtype;
2404 struct obstack output;
2405 struct cleanup *inner_cleanup;
2406 const gdb_byte *bytes;
2408 valtype = value_type (val_args[i]);
2409 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2410 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2411 error (_("expected wchar_t argument for %%lc"));
2413 bytes = value_contents (val_args[i]);
2415 obstack_init (&output);
2416 inner_cleanup = make_cleanup_obstack_free (&output);
2418 convert_between_encodings (target_wide_charset (gdbarch),
2420 bytes, TYPE_LENGTH (valtype),
2421 TYPE_LENGTH (valtype),
2422 &output, translit_char);
2423 obstack_grow_str0 (&output, "");
2425 fprintf_filtered (stream, current_substring,
2426 obstack_base (&output));
2427 do_cleanups (inner_cleanup);
2432 struct type *type = value_type (val_args[i]);
2436 /* If format string wants a float, unchecked-convert the value
2437 to floating point of the same size. */
2438 type = float_type_from_length (type);
2439 val = unpack_double (type, value_contents (val_args[i]), &inv);
2441 error (_("Invalid floating value found in program."));
2443 fprintf_filtered (stream, current_substring, (double) val);
2446 case long_double_arg:
2447 #ifdef HAVE_LONG_DOUBLE
2449 struct type *type = value_type (val_args[i]);
2453 /* If format string wants a float, unchecked-convert the value
2454 to floating point of the same size. */
2455 type = float_type_from_length (type);
2456 val = unpack_double (type, value_contents (val_args[i]), &inv);
2458 error (_("Invalid floating value found in program."));
2460 fprintf_filtered (stream, current_substring,
2465 error (_("long double not supported in printf"));
2468 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2470 long long val = value_as_long (val_args[i]);
2472 fprintf_filtered (stream, current_substring, val);
2476 error (_("long long not supported in printf"));
2480 int val = value_as_long (val_args[i]);
2482 fprintf_filtered (stream, current_substring, val);
2487 long val = value_as_long (val_args[i]);
2489 fprintf_filtered (stream, current_substring, val);
2493 /* Handles decimal floating values. */
2496 const gdb_byte *param_ptr = value_contents (val_args[i]);
2498 #if defined (PRINTF_HAS_DECFLOAT)
2499 /* If we have native support for Decimal floating
2500 printing, handle it here. */
2501 fprintf_filtered (stream, current_substring, param_ptr);
2504 /* As a workaround until vasprintf has native support for DFP
2505 we convert the DFP values to string and print them using
2506 the %s format specifier. */
2509 int nnull_chars = 0;
2511 /* Parameter data. */
2512 struct type *param_type = value_type (val_args[i]);
2513 unsigned int param_len = TYPE_LENGTH (param_type);
2514 struct gdbarch *gdbarch = get_type_arch (param_type);
2515 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2517 /* DFP output data. */
2518 struct value *dfp_value = NULL;
2522 struct type *dfp_type = NULL;
2523 char decstr[MAX_DECIMAL_STRING];
2525 /* Points to the end of the string so that we can go back
2526 and check for DFP length modifiers. */
2527 eos = current_substring + strlen (current_substring);
2529 /* Look for the float/double format specifier. */
2530 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2531 && *eos != 'g' && *eos != 'G')
2536 /* Search for the '%' char and extract the size and type of
2537 the output decimal value based on its modifiers
2538 (%Hf, %Df, %DDf). */
2539 while (*--sos != '%')
2544 dfp_type = builtin_type (gdbarch)->builtin_decfloat;
2546 else if (*sos == 'D' && *(sos - 1) == 'D')
2549 dfp_type = builtin_type (gdbarch)->builtin_declong;
2555 dfp_type = builtin_type (gdbarch)->builtin_decdouble;
2559 /* Replace %Hf, %Df and %DDf with %s's. */
2562 /* Go through the whole format string and pull the correct
2563 number of chars back to compensate for the change in the
2564 format specifier. */
2565 while (nnull_chars < nargs - i)
2573 /* Conversion between different DFP types. */
2574 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2575 decimal_convert (param_ptr, param_len, byte_order,
2576 dec, dfp_len, byte_order);
2578 /* If this is a non-trivial conversion, just output 0.
2579 A correct converted value can be displayed by explicitly
2580 casting to a DFP type. */
2581 decimal_from_string (dec, dfp_len, byte_order, "0");
2583 dfp_value = value_from_decfloat (dfp_type, dec);
2585 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2587 decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
2589 /* Print the DFP value. */
2590 fprintf_filtered (stream, current_substring, decstr);
2598 /* We avoid the host's %p because pointers are too
2599 likely to be the wrong size. The only interesting
2600 modifier for %p is a width; extract that, and then
2601 handle %p as glibc would: %#x or a literal "(nil)". */
2603 char *p, *fmt, *fmt_p;
2604 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2605 long long val = value_as_long (val_args[i]);
2607 long val = value_as_long (val_args[i]);
2610 fmt = alloca (strlen (current_substring) + 5);
2612 /* Copy up to the leading %. */
2613 p = current_substring;
2617 int is_percent = (*p == '%');
2632 /* Copy any width. */
2633 while (*p >= '0' && *p < '9')
2636 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2639 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2645 fprintf_filtered (stream, fmt, val);
2651 fprintf_filtered (stream, fmt, "(nil)");
2657 internal_error (__FILE__, __LINE__,
2658 _("failed internal consistency check"));
2660 /* Skip to the next substring. */
2661 current_substring += strlen (current_substring) + 1;
2663 /* Print the portion of the format string after the last argument.
2664 Note that this will not include any ordinary %-specs, but it
2665 might include "%%". That is why we use printf_filtered and not
2666 puts_filtered here. Also, we pass a dummy argument because
2667 some platforms have modified GCC to include -Wformat-security
2668 by default, which will warn here if there is no argument. */
2669 fprintf_filtered (stream, last_arg, 0);
2671 do_cleanups (old_cleanups);
2674 /* Implement the "printf" command. */
2677 printf_command (char *arg, int from_tty)
2679 ui_printf (arg, gdb_stdout);
2682 /* Implement the "eval" command. */
2685 eval_command (char *arg, int from_tty)
2687 struct ui_file *ui_out = mem_fileopen ();
2688 struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
2691 ui_printf (arg, ui_out);
2693 expanded = ui_file_xstrdup (ui_out, NULL);
2694 make_cleanup (xfree, expanded);
2696 execute_command (expanded, from_tty);
2698 do_cleanups (cleanups);
2702 _initialize_printcmd (void)
2704 struct cmd_list_element *c;
2706 current_display_number = -1;
2708 observer_attach_solib_unloaded (clear_dangling_display_expressions);
2710 add_info ("address", address_info,
2711 _("Describe where symbol SYM is stored."));
2713 add_info ("symbol", sym_info, _("\
2714 Describe what symbol is at location ADDR.\n\
2715 Only for symbols with fixed locations (global or static scope)."));
2717 add_com ("x", class_vars, x_command, _("\
2718 Examine memory: x/FMT ADDRESS.\n\
2719 ADDRESS is an expression for the memory address to examine.\n\
2720 FMT is a repeat count followed by a format letter and a size letter.\n\
2721 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2722 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2723 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2724 The specified number of objects of the specified size are printed\n\
2725 according to the format.\n\n\
2726 Defaults for format and size letters are those previously used.\n\
2727 Default count is 1. Default address is following last thing printed\n\
2728 with this command or \"print\"."));
2731 add_com ("whereis", class_vars, whereis_command,
2732 _("Print line number and file of definition of variable."));
2735 add_info ("display", display_info, _("\
2736 Expressions to display when program stops, with code numbers."));
2738 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2739 Cancel some expressions to be displayed when program stops.\n\
2740 Arguments are the code numbers of the expressions to stop displaying.\n\
2741 No argument means cancel all automatic-display expressions.\n\
2742 \"delete display\" has the same effect as this command.\n\
2743 Do \"info display\" to see current list of code numbers."),
2746 add_com ("display", class_vars, display_command, _("\
2747 Print value of expression EXP each time the program stops.\n\
2748 /FMT may be used before EXP as in the \"print\" command.\n\
2749 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2750 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2751 and examining is done as in the \"x\" command.\n\n\
2752 With no argument, display all currently requested auto-display expressions.\n\
2753 Use \"undisplay\" to cancel display requests previously made."));
2755 add_cmd ("display", class_vars, enable_display, _("\
2756 Enable some expressions to be displayed when program stops.\n\
2757 Arguments are the code numbers of the expressions to resume displaying.\n\
2758 No argument means enable all automatic-display expressions.\n\
2759 Do \"info display\" to see current list of code numbers."), &enablelist);
2761 add_cmd ("display", class_vars, disable_display_command, _("\
2762 Disable some expressions to be displayed when program stops.\n\
2763 Arguments are the code numbers of the expressions to stop displaying.\n\
2764 No argument means disable all automatic-display expressions.\n\
2765 Do \"info display\" to see current list of code numbers."), &disablelist);
2767 add_cmd ("display", class_vars, undisplay_command, _("\
2768 Cancel some expressions to be displayed when program stops.\n\
2769 Arguments are the code numbers of the expressions to stop displaying.\n\
2770 No argument means cancel all automatic-display expressions.\n\
2771 Do \"info display\" to see current list of code numbers."), &deletelist);
2773 add_com ("printf", class_vars, printf_command, _("\
2774 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2775 This is useful for formatted output in user-defined commands."));
2777 add_com ("output", class_vars, output_command, _("\
2778 Like \"print\" but don't put in value history and don't print newline.\n\
2779 This is useful in user-defined commands."));
2781 add_prefix_cmd ("set", class_vars, set_command, _("\
2782 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2783 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2784 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2785 with $), a register (a few standard names starting with $), or an actual\n\
2786 variable in the program being debugged. EXP is any valid expression.\n\
2787 Use \"set variable\" for variables with names identical to set subcommands.\n\
2789 With a subcommand, this command modifies parts of the gdb environment.\n\
2790 You can see these environment settings with the \"show\" command."),
2791 &setlist, "set ", 1, &cmdlist);
2793 add_com ("assign", class_vars, set_command, _("\
2794 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2795 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2796 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2797 with $), a register (a few standard names starting with $), or an actual\n\
2798 variable in the program being debugged. EXP is any valid expression.\n\
2799 Use \"set variable\" for variables with names identical to set subcommands.\n\
2800 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2801 You can see these environment settings with the \"show\" command."));
2803 /* "call" is the same as "set", but handy for dbx users to call fns. */
2804 c = add_com ("call", class_vars, call_command, _("\
2805 Call a function in the program.\n\
2806 The argument is the function name and arguments, in the notation of the\n\
2807 current working language. The result is printed and saved in the value\n\
2808 history, if it is not void."));
2809 set_cmd_completer (c, expression_completer);
2811 add_cmd ("variable", class_vars, set_command, _("\
2812 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2813 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2814 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2815 with $), a register (a few standard names starting with $), or an actual\n\
2816 variable in the program being debugged. EXP is any valid expression.\n\
2817 This may usually be abbreviated to simply \"set\"."),
2820 c = add_com ("print", class_vars, print_command, _("\
2821 Print value of expression EXP.\n\
2822 Variables accessible are those of the lexical environment of the selected\n\
2823 stack frame, plus all those whose scope is global or an entire file.\n\
2825 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2826 $$NUM refers to NUM'th value back from the last one.\n\
2827 Names starting with $ refer to registers (with the values they would have\n\
2828 if the program were to return to the stack frame now selected, restoring\n\
2829 all registers saved by frames farther in) or else to debugger\n\
2830 \"convenience\" variables (any such name not a known register).\n\
2831 Use assignment expressions to give values to convenience variables.\n\
2833 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2834 @ is a binary operator for treating consecutive data objects\n\
2835 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2836 element is FOO, whose second element is stored in the space following\n\
2837 where FOO is stored, etc. FOO must be an expression whose value\n\
2838 resides in memory.\n\
2840 EXP may be preceded with /FMT, where FMT is a format letter\n\
2841 but no count or size letter (see \"x\" command)."));
2842 set_cmd_completer (c, expression_completer);
2843 add_com_alias ("p", "print", class_vars, 1);
2845 c = add_com ("inspect", class_vars, inspect_command, _("\
2846 Same as \"print\" command, except that if you are running in the epoch\n\
2847 environment, the value is printed in its own window."));
2848 set_cmd_completer (c, expression_completer);
2850 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2851 &max_symbolic_offset, _("\
2852 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2853 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2855 show_max_symbolic_offset,
2856 &setprintlist, &showprintlist);
2857 add_setshow_boolean_cmd ("symbol-filename", no_class,
2858 &print_symbol_filename, _("\
2859 Set printing of source filename and line number with <symbol>."), _("\
2860 Show printing of source filename and line number with <symbol>."), NULL,
2862 show_print_symbol_filename,
2863 &setprintlist, &showprintlist);
2865 add_com ("eval", no_class, eval_command, _("\
2866 Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2867 a command line, and call it."));