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 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"
50 #include "parser-defs.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 printouts */
73 /* Last specified output format. */
75 static char last_format = 'x';
77 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
79 static char last_size = 'w';
81 /* Default address to examine next. */
83 static CORE_ADDR next_address;
85 /* Number of delay instructions following current disassembled insn. */
87 static int branch_delay_insns;
89 /* Last address examined. */
91 static CORE_ADDR last_examine_address;
93 /* Contents of last address examined.
94 This is not valid past the end of the `x' command! */
96 static struct value *last_examine_value;
98 /* Largest offset between a symbolic value and an address, that will be
99 printed as `0x1234 <symbol+offset>'. */
101 static unsigned int max_symbolic_offset = UINT_MAX;
103 show_max_symbolic_offset (struct ui_file *file, int from_tty,
104 struct cmd_list_element *c, const char *value)
106 fprintf_filtered (file, _("\
107 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
111 /* Append the source filename and linenumber of the symbol when
112 printing a symbolic value as `<symbol at filename:linenum>' if set. */
113 static int print_symbol_filename = 0;
115 show_print_symbol_filename (struct ui_file *file, int from_tty,
116 struct cmd_list_element *c, const char *value)
118 fprintf_filtered (file, _("\
119 Printing of source filename and line number with <symbol> is %s.\n"),
123 /* Number of auto-display expression currently being displayed.
124 So that we can disable it if we get an error or a signal within it.
125 -1 when not doing one. */
127 int current_display_number;
131 /* Chain link to next auto-display item. */
132 struct display *next;
133 /* The expression as the user typed it. */
135 /* Expression to be evaluated and displayed. */
136 struct expression *exp;
137 /* Item number of this auto-display item. */
139 /* Display format specified. */
140 struct format_data format;
141 /* Innermost block required by this expression when evaluated */
143 /* Status of this display (enabled or disabled) */
147 /* Chain of expressions whose values should be displayed
148 automatically each time the program stops. */
150 static struct display *display_chain;
152 static int display_number;
154 /* Prototypes for exported functions. */
156 void output_command (char *, int);
158 void _initialize_printcmd (void);
160 /* Prototypes for local functions. */
162 static void do_one_display (struct display *);
165 /* Decode a format specification. *STRING_PTR should point to it.
166 OFORMAT and OSIZE are used as defaults for the format and size
167 if none are given in the format specification.
168 If OSIZE is zero, then the size field of the returned value
169 should be set only if a size is explicitly specified by the
171 The structure returned describes all the data
172 found in the specification. In addition, *STRING_PTR is advanced
173 past the specification and past all whitespace following it. */
175 static struct format_data
176 decode_format (char **string_ptr, int oformat, int osize)
178 struct format_data val;
179 char *p = *string_ptr;
185 if (*p >= '0' && *p <= '9')
186 val.count = atoi (p);
187 while (*p >= '0' && *p <= '9')
190 /* Now process size or format letters that follow. */
194 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
196 else if (*p >= 'a' && *p <= 'z')
202 while (*p == ' ' || *p == '\t')
206 /* Set defaults for format and size if not specified. */
207 if (val.format == '?')
211 /* Neither has been specified. */
212 val.format = oformat;
216 /* If a size is specified, any format makes a reasonable
217 default except 'i'. */
218 val.format = oformat == 'i' ? 'x' : oformat;
220 else if (val.size == '?')
225 /* Pick the appropriate size for an address. */
226 if (gdbarch_ptr_bit (current_gdbarch) == 64)
227 val.size = osize ? 'g' : osize;
228 else if (gdbarch_ptr_bit (current_gdbarch) == 32)
229 val.size = osize ? 'w' : osize;
230 else if (gdbarch_ptr_bit (current_gdbarch) == 16)
231 val.size = osize ? 'h' : osize;
233 /* Bad value for gdbarch_ptr_bit. */
234 internal_error (__FILE__, __LINE__,
235 _("failed internal consistency check"));
238 /* Floating point has to be word or giantword. */
239 if (osize == 'w' || osize == 'g')
242 /* Default it to giantword if the last used size is not
244 val.size = osize ? 'g' : osize;
247 /* Characters default to one byte. */
248 val.size = osize ? 'b' : osize;
251 /* The default is the size most recently specified. */
258 /* Print value VAL on stream according to OPTIONS.
259 Do not end with a newline.
260 SIZE is the letter for the size of datum being printed.
261 This is used to pad hex numbers so they line up. SIZE is 0
262 for print / output and set for examine. */
265 print_formatted (struct value *val, int size,
266 const struct value_print_options *options,
267 struct ui_file *stream)
269 struct type *type = check_typedef (value_type (val));
270 int len = TYPE_LENGTH (type);
272 if (VALUE_LVAL (val) == lval_memory)
273 next_address = VALUE_ADDRESS (val) + len;
277 switch (options->format)
281 struct type *elttype = value_type (val);
282 next_address = (VALUE_ADDRESS (val)
283 + val_print_string (elttype,
284 VALUE_ADDRESS (val), -1,
290 /* We often wrap here if there are long symbolic names. */
292 next_address = (VALUE_ADDRESS (val)
293 + gdb_print_insn (VALUE_ADDRESS (val), stream,
294 &branch_delay_insns));
299 if (options->format == 0 || options->format == 's'
300 || TYPE_CODE (type) == TYPE_CODE_REF
301 || TYPE_CODE (type) == TYPE_CODE_ARRAY
302 || TYPE_CODE (type) == TYPE_CODE_STRING
303 || TYPE_CODE (type) == TYPE_CODE_STRUCT
304 || TYPE_CODE (type) == TYPE_CODE_UNION
305 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
306 value_print (val, stream, options);
308 /* User specified format, so don't look to the the type to
309 tell us what to do. */
310 print_scalar_formatted (value_contents (val), type,
311 options, size, stream);
314 /* Return builtin floating point type of same length as TYPE.
315 If no such type is found, return TYPE itself. */
317 float_type_from_length (struct gdbarch *gdbarch, struct type *type)
319 const struct builtin_type *builtin = builtin_type (gdbarch);
320 unsigned int len = TYPE_LENGTH (type);
322 if (len == TYPE_LENGTH (builtin->builtin_float))
323 type = builtin->builtin_float;
324 else if (len == TYPE_LENGTH (builtin->builtin_double))
325 type = builtin->builtin_double;
326 else if (len == TYPE_LENGTH (builtin->builtin_long_double))
327 type = builtin->builtin_long_double;
332 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
333 according to OPTIONS and SIZE on STREAM.
334 Formats s and i are not supported at this level.
336 This is how the elements of an array or structure are printed
340 print_scalar_formatted (const void *valaddr, struct type *type,
341 const struct value_print_options *options,
342 int size, struct ui_file *stream)
344 LONGEST val_long = 0;
345 unsigned int len = TYPE_LENGTH (type);
346 enum bfd_endian byte_order = gdbarch_byte_order (current_gdbarch);
348 /* If we get here with a string format, try again without it. Go
349 all the way back to the language printers, which may call us
351 if (options->format == 's')
353 struct value_print_options opts = *options;
356 val_print (type, valaddr, 0, 0, stream, 0, &opts,
361 if (len > sizeof(LONGEST) &&
362 (TYPE_CODE (type) == TYPE_CODE_INT
363 || TYPE_CODE (type) == TYPE_CODE_ENUM))
365 switch (options->format)
368 print_octal_chars (stream, valaddr, len, byte_order);
372 print_decimal_chars (stream, valaddr, len, byte_order);
375 print_binary_chars (stream, valaddr, len, byte_order);
378 print_hex_chars (stream, valaddr, len, byte_order);
381 print_char_chars (stream, type, valaddr, len, byte_order);
388 if (options->format != 'f')
389 val_long = unpack_long (type, valaddr);
391 /* If the value is a pointer, and pointers and addresses are not the
392 same, then at this point, the value's length (in target bytes) is
393 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
394 if (TYPE_CODE (type) == TYPE_CODE_PTR)
395 len = gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT;
397 /* If we are printing it as unsigned, truncate it in case it is actually
398 a negative signed value (e.g. "print/u (short)-1" should print 65535
399 (if shorts are 16 bits) instead of 4294967295). */
400 if (options->format != 'd')
402 if (len < sizeof (LONGEST))
403 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
406 switch (options->format)
411 /* No size specified, like in print. Print varying # of digits. */
412 print_longest (stream, 'x', 1, val_long);
421 print_longest (stream, size, 1, val_long);
424 error (_("Undefined output size \"%c\"."), size);
429 print_longest (stream, 'd', 1, val_long);
433 print_longest (stream, 'u', 0, val_long);
438 print_longest (stream, 'o', 1, val_long);
440 fprintf_filtered (stream, "0");
445 CORE_ADDR addr = unpack_pointer (type, valaddr);
446 print_address (addr, stream);
452 struct value_print_options opts = *options;
454 if (TYPE_UNSIGNED (type))
455 value_print (value_from_longest (builtin_type_true_unsigned_char,
459 value_print (value_from_longest (builtin_type_true_char, val_long),
465 type = float_type_from_length (current_gdbarch, type);
466 print_floating (valaddr, type, stream);
470 internal_error (__FILE__, __LINE__,
471 _("failed internal consistency check"));
474 /* Binary; 't' stands for "two". */
476 char bits[8 * (sizeof val_long) + 1];
477 char buf[8 * (sizeof val_long) + 32];
482 width = 8 * (sizeof val_long);
499 error (_("Undefined output size \"%c\"."), size);
505 bits[width] = (val_long & 1) ? '1' : '0';
510 while (*cp && *cp == '0')
516 fputs_filtered (buf, stream);
521 error (_("Undefined output format \"%c\"."), options->format);
525 /* Specify default address for `x' command.
526 The `info lines' command uses this. */
529 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
531 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
535 /* Make address available to the user as $_. */
536 set_internalvar (lookup_internalvar ("_"),
537 value_from_pointer (ptr_type, addr));
540 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
541 after LEADIN. Print nothing if no symbolic name is found nearby.
542 Optionally also print source file and line number, if available.
543 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
544 or to interpret it as a possible C++ name and convert it back to source
545 form. However note that DO_DEMANGLE can be overridden by the specific
546 settings of the demangle and asm_demangle variables. */
549 print_address_symbolic (CORE_ADDR addr, struct ui_file *stream,
550 int do_demangle, char *leadin)
553 char *filename = NULL;
558 /* Throw away both name and filename. */
559 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
560 make_cleanup (free_current_contents, &filename);
562 if (build_address_symbolic (addr, do_demangle, &name, &offset,
563 &filename, &line, &unmapped))
565 do_cleanups (cleanup_chain);
569 fputs_filtered (leadin, stream);
571 fputs_filtered ("<*", stream);
573 fputs_filtered ("<", stream);
574 fputs_filtered (name, stream);
576 fprintf_filtered (stream, "+%u", (unsigned int) offset);
578 /* Append source filename and line number if desired. Give specific
579 line # of this addr, if we have it; else line # of the nearest symbol. */
580 if (print_symbol_filename && filename != NULL)
583 fprintf_filtered (stream, " at %s:%d", filename, line);
585 fprintf_filtered (stream, " in %s", filename);
588 fputs_filtered ("*>", stream);
590 fputs_filtered (">", stream);
592 do_cleanups (cleanup_chain);
595 /* Given an address ADDR return all the elements needed to print the
596 address in a symbolic form. NAME can be mangled or not depending
597 on DO_DEMANGLE (and also on the asm_demangle global variable,
598 manipulated via ''set print asm-demangle''). Return 0 in case of
599 success, when all the info in the OUT paramters is valid. Return 1
602 build_address_symbolic (CORE_ADDR addr, /* IN */
603 int do_demangle, /* IN */
604 char **name, /* OUT */
605 int *offset, /* OUT */
606 char **filename, /* OUT */
608 int *unmapped) /* OUT */
610 struct minimal_symbol *msymbol;
611 struct symbol *symbol;
612 CORE_ADDR name_location = 0;
613 struct obj_section *section = NULL;
614 char *name_temp = "";
616 /* Let's say it is mapped (not unmapped). */
619 /* Determine if the address is in an overlay, and whether it is
621 if (overlay_debugging)
623 section = find_pc_overlay (addr);
624 if (pc_in_unmapped_range (addr, section))
627 addr = overlay_mapped_address (addr, section);
631 /* First try to find the address in the symbol table, then
632 in the minsyms. Take the closest one. */
634 /* This is defective in the sense that it only finds text symbols. So
635 really this is kind of pointless--we should make sure that the
636 minimal symbols have everything we need (by changing that we could
637 save some memory, but for many debug format--ELF/DWARF or
638 anything/stabs--it would be inconvenient to eliminate those minimal
640 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
641 symbol = find_pc_sect_function (addr, section);
645 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
646 if (do_demangle || asm_demangle)
647 name_temp = SYMBOL_PRINT_NAME (symbol);
649 name_temp = SYMBOL_LINKAGE_NAME (symbol);
654 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
656 /* The msymbol is closer to the address than the symbol;
657 use the msymbol instead. */
659 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
660 if (do_demangle || asm_demangle)
661 name_temp = SYMBOL_PRINT_NAME (msymbol);
663 name_temp = SYMBOL_LINKAGE_NAME (msymbol);
666 if (symbol == NULL && msymbol == NULL)
669 /* If the nearest symbol is too far away, don't print anything symbolic. */
671 /* For when CORE_ADDR is larger than unsigned int, we do math in
672 CORE_ADDR. But when we detect unsigned wraparound in the
673 CORE_ADDR math, we ignore this test and print the offset,
674 because addr+max_symbolic_offset has wrapped through the end
675 of the address space back to the beginning, giving bogus comparison. */
676 if (addr > name_location + max_symbolic_offset
677 && name_location + max_symbolic_offset > name_location)
680 *offset = addr - name_location;
682 *name = xstrdup (name_temp);
684 if (print_symbol_filename)
686 struct symtab_and_line sal;
688 sal = find_pc_sect_line (addr, section, 0);
692 *filename = xstrdup (sal.symtab->filename);
700 /* Print address ADDR symbolically on STREAM.
701 First print it as a number. Then perhaps print
702 <SYMBOL + OFFSET> after the number. */
705 print_address (CORE_ADDR addr, struct ui_file *stream)
707 fputs_filtered (paddress (addr), stream);
708 print_address_symbolic (addr, stream, asm_demangle, " ");
711 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
712 controls whether to print the symbolic name "raw" or demangled.
713 Global setting "addressprint" controls whether to print hex address
717 print_address_demangle (CORE_ADDR addr, struct ui_file *stream,
720 struct value_print_options opts;
721 get_user_print_options (&opts);
724 fprintf_filtered (stream, "0");
726 else if (opts.addressprint)
728 fputs_filtered (paddress (addr), stream);
729 print_address_symbolic (addr, stream, do_demangle, " ");
733 print_address_symbolic (addr, stream, do_demangle, "");
738 /* These are the types that $__ will get after an examine command of one
741 static struct type *examine_i_type;
743 static struct type *examine_b_type;
744 static struct type *examine_h_type;
745 static struct type *examine_w_type;
746 static struct type *examine_g_type;
748 /* Examine data at address ADDR in format FMT.
749 Fetch it from memory and print on gdb_stdout. */
752 do_examine (struct format_data fmt, CORE_ADDR addr)
757 struct type *val_type = NULL;
760 struct value_print_options opts;
767 /* String or instruction format implies fetch single bytes
768 regardless of the specified size. */
769 if (format == 's' || format == 'i')
773 val_type = examine_i_type;
774 else if (size == 'b')
775 val_type = examine_b_type;
776 else if (size == 'h')
777 val_type = examine_h_type;
778 else if (size == 'w')
779 val_type = examine_w_type;
780 else if (size == 'g')
781 val_type = examine_g_type;
788 if (format == 's' || format == 'i')
791 get_formatted_print_options (&opts, format);
793 /* Print as many objects as specified in COUNT, at most maxelts per line,
794 with the address of the next one at the start of each line. */
799 print_address (next_address, gdb_stdout);
800 printf_filtered (":");
805 printf_filtered ("\t");
806 /* Note that print_formatted sets next_address for the next
808 last_examine_address = next_address;
810 if (last_examine_value)
811 value_free (last_examine_value);
813 /* The value to be displayed is not fetched greedily.
814 Instead, to avoid the possibility of a fetched value not
815 being used, its retrieval is delayed until the print code
816 uses it. When examining an instruction stream, the
817 disassembler will perform its own memory fetch using just
818 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
819 the disassembler be modified so that LAST_EXAMINE_VALUE
820 is left with the byte sequence from the last complete
821 instruction fetched from memory? */
822 last_examine_value = value_at_lazy (val_type, next_address);
824 if (last_examine_value)
825 release_value (last_examine_value);
827 print_formatted (last_examine_value, size, &opts, gdb_stdout);
829 /* Display any branch delay slots following the final insn. */
830 if (format == 'i' && count == 1)
831 count += branch_delay_insns;
833 printf_filtered ("\n");
834 gdb_flush (gdb_stdout);
839 validate_format (struct format_data fmt, char *cmdname)
842 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
844 error (_("Item count other than 1 is meaningless in \"%s\" command."),
846 if (fmt.format == 'i')
847 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
848 fmt.format, cmdname);
851 /* Evaluate string EXP as an expression in the current language and
852 print the resulting value. EXP may contain a format specifier as the
853 first argument ("/x myvar" for example, to print myvar in hex). */
856 print_command_1 (char *exp, int inspect, int voidprint)
858 struct expression *expr;
859 struct cleanup *old_chain = 0;
862 struct format_data fmt;
865 if (exp && *exp == '/')
868 fmt = decode_format (&exp, last_format, 0);
869 validate_format (fmt, "print");
870 last_format = format = fmt.format;
882 expr = parse_expression (exp);
883 old_chain = make_cleanup (free_current_contents, &expr);
885 val = evaluate_expression (expr);
888 val = access_value_history (0);
890 if (voidprint || (val && value_type (val) &&
891 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
893 struct value_print_options opts;
894 int histindex = record_latest_value (val);
897 annotate_value_history_begin (histindex, value_type (val));
899 annotate_value_begin (value_type (val));
902 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
904 else if (histindex >= 0)
905 printf_filtered ("$%d = ", histindex);
908 annotate_value_history_value ();
910 get_formatted_print_options (&opts, format);
911 opts.inspect_it = inspect;
913 print_formatted (val, fmt.size, &opts, gdb_stdout);
914 printf_filtered ("\n");
917 annotate_value_history_end ();
919 annotate_value_end ();
922 printf_unfiltered ("\") )\030");
926 do_cleanups (old_chain);
930 print_command (char *exp, int from_tty)
932 print_command_1 (exp, 0, 1);
935 /* Same as print, except in epoch, it gets its own window. */
937 inspect_command (char *exp, int from_tty)
939 extern int epoch_interface;
941 print_command_1 (exp, epoch_interface, 1);
944 /* Same as print, except it doesn't print void results. */
946 call_command (char *exp, int from_tty)
948 print_command_1 (exp, 0, 0);
952 output_command (char *exp, int from_tty)
954 struct expression *expr;
955 struct cleanup *old_chain;
958 struct format_data fmt;
959 struct value_print_options opts;
963 if (exp && *exp == '/')
966 fmt = decode_format (&exp, 0, 0);
967 validate_format (fmt, "output");
971 expr = parse_expression (exp);
972 old_chain = make_cleanup (free_current_contents, &expr);
974 val = evaluate_expression (expr);
976 annotate_value_begin (value_type (val));
978 get_formatted_print_options (&opts, format);
979 print_formatted (val, fmt.size, &opts, gdb_stdout);
981 annotate_value_end ();
984 gdb_flush (gdb_stdout);
986 do_cleanups (old_chain);
990 set_command (char *exp, int from_tty)
992 struct expression *expr = parse_expression (exp);
993 struct cleanup *old_chain =
994 make_cleanup (free_current_contents, &expr);
995 evaluate_expression (expr);
996 do_cleanups (old_chain);
1000 sym_info (char *arg, int from_tty)
1002 struct minimal_symbol *msymbol;
1003 struct objfile *objfile;
1004 struct obj_section *osect;
1005 CORE_ADDR addr, sect_addr;
1007 unsigned int offset;
1010 error_no_arg (_("address"));
1012 addr = parse_and_eval_address (arg);
1013 ALL_OBJSECTIONS (objfile, osect)
1015 /* Only process each object file once, even if there's a separate
1017 if (objfile->separate_debug_objfile_backlink)
1020 sect_addr = overlay_mapped_address (addr, osect);
1022 if (obj_section_addr (osect) <= sect_addr
1023 && sect_addr < obj_section_endaddr (osect)
1024 && (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, osect)))
1026 const char *obj_name, *mapped, *sec_name, *msym_name;
1028 struct cleanup *old_chain;
1031 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1032 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1033 sec_name = osect->the_bfd_section->name;
1034 msym_name = SYMBOL_PRINT_NAME (msymbol);
1036 /* Don't print the offset if it is zero.
1037 We assume there's no need to handle i18n of "sym + offset". */
1039 loc_string = xstrprintf ("%s + %u", msym_name, offset);
1041 loc_string = xstrprintf ("%s", msym_name);
1043 /* Use a cleanup to free loc_string in case the user quits
1044 a pagination request inside printf_filtered. */
1045 old_chain = make_cleanup (xfree, loc_string);
1047 gdb_assert (osect->objfile && osect->objfile->name);
1048 obj_name = osect->objfile->name;
1050 if (MULTI_OBJFILE_P ())
1051 if (pc_in_unmapped_range (addr, osect))
1052 if (section_is_overlay (osect))
1053 printf_filtered (_("%s in load address range of "
1054 "%s overlay section %s of %s\n"),
1055 loc_string, mapped, sec_name, obj_name);
1057 printf_filtered (_("%s in load address range of "
1058 "section %s of %s\n"),
1059 loc_string, sec_name, obj_name);
1061 if (section_is_overlay (osect))
1062 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1063 loc_string, mapped, sec_name, obj_name);
1065 printf_filtered (_("%s in section %s of %s\n"),
1066 loc_string, sec_name, obj_name);
1068 if (pc_in_unmapped_range (addr, osect))
1069 if (section_is_overlay (osect))
1070 printf_filtered (_("%s in load address range of %s overlay "
1072 loc_string, mapped, sec_name);
1074 printf_filtered (_("%s in load address range of section %s\n"),
1075 loc_string, sec_name);
1077 if (section_is_overlay (osect))
1078 printf_filtered (_("%s in %s overlay section %s\n"),
1079 loc_string, mapped, sec_name);
1081 printf_filtered (_("%s in section %s\n"),
1082 loc_string, sec_name);
1084 do_cleanups (old_chain);
1088 printf_filtered (_("No symbol matches %s.\n"), arg);
1092 address_info (char *exp, int from_tty)
1095 struct minimal_symbol *msymbol;
1097 struct obj_section *section;
1098 CORE_ADDR load_addr;
1099 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1100 if exp is a field of `this'. */
1103 error (_("Argument required."));
1105 sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
1106 &is_a_field_of_this);
1109 if (is_a_field_of_this)
1111 printf_filtered ("Symbol \"");
1112 fprintf_symbol_filtered (gdb_stdout, exp,
1113 current_language->la_language, DMGL_ANSI);
1114 printf_filtered ("\" is a field of the local class variable ");
1115 if (current_language->la_language == language_objc)
1116 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1118 printf_filtered ("`this'\n");
1122 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1124 if (msymbol != NULL)
1126 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1128 printf_filtered ("Symbol \"");
1129 fprintf_symbol_filtered (gdb_stdout, exp,
1130 current_language->la_language, DMGL_ANSI);
1131 printf_filtered ("\" is at ");
1132 fputs_filtered (paddress (load_addr), gdb_stdout);
1133 printf_filtered (" in a file compiled without debugging");
1134 section = SYMBOL_OBJ_SECTION (msymbol);
1135 if (section_is_overlay (section))
1137 load_addr = overlay_unmapped_address (load_addr, section);
1138 printf_filtered (",\n -- loaded at ");
1139 fputs_filtered (paddress (load_addr), gdb_stdout);
1140 printf_filtered (" in overlay section %s",
1141 section->the_bfd_section->name);
1143 printf_filtered (".\n");
1146 error (_("No symbol \"%s\" in current context."), exp);
1150 printf_filtered ("Symbol \"");
1151 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
1152 current_language->la_language, DMGL_ANSI);
1153 printf_filtered ("\" is ");
1154 val = SYMBOL_VALUE (sym);
1155 section = SYMBOL_OBJ_SECTION (sym);
1157 switch (SYMBOL_CLASS (sym))
1160 case LOC_CONST_BYTES:
1161 printf_filtered ("constant");
1165 printf_filtered ("a label at address ");
1166 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1168 if (section_is_overlay (section))
1170 load_addr = overlay_unmapped_address (load_addr, section);
1171 printf_filtered (",\n -- loaded at ");
1172 fputs_filtered (paddress (load_addr), gdb_stdout);
1173 printf_filtered (" in overlay section %s",
1174 section->the_bfd_section->name);
1179 /* FIXME: cagney/2004-01-26: It should be possible to
1180 unconditionally call the SYMBOL_OPS method when available.
1181 Unfortunately DWARF 2 stores the frame-base (instead of the
1182 function) location in a function's symbol. Oops! For the
1183 moment enable this when/where applicable. */
1184 SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
1188 if (SYMBOL_IS_ARGUMENT (sym))
1189 printf_filtered (_("an argument in register %s"),
1190 gdbarch_register_name (current_gdbarch, val));
1192 printf_filtered (_("a variable in register %s"),
1193 gdbarch_register_name (current_gdbarch, val));
1197 printf_filtered (_("static storage at address "));
1198 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1200 if (section_is_overlay (section))
1202 load_addr = overlay_unmapped_address (load_addr, section);
1203 printf_filtered (_(",\n -- loaded at "));
1204 fputs_filtered (paddress (load_addr), gdb_stdout);
1205 printf_filtered (_(" in overlay section %s"),
1206 section->the_bfd_section->name);
1210 case LOC_REGPARM_ADDR:
1211 printf_filtered (_("address of an argument in register %s"),
1212 gdbarch_register_name (current_gdbarch, val));
1216 printf_filtered (_("an argument at offset %ld"), val);
1220 printf_filtered (_("a local variable at frame offset %ld"), val);
1224 printf_filtered (_("a reference argument at offset %ld"), val);
1228 printf_filtered (_("a typedef"));
1232 printf_filtered (_("a function at address "));
1233 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1234 fputs_filtered (paddress (load_addr), gdb_stdout);
1235 if (section_is_overlay (section))
1237 load_addr = overlay_unmapped_address (load_addr, section);
1238 printf_filtered (_(",\n -- loaded at "));
1239 fputs_filtered (paddress (load_addr), gdb_stdout);
1240 printf_filtered (_(" in overlay section %s"),
1241 section->the_bfd_section->name);
1245 case LOC_UNRESOLVED:
1247 struct minimal_symbol *msym;
1249 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym), NULL, NULL);
1251 printf_filtered ("unresolved");
1254 section = SYMBOL_OBJ_SECTION (msym);
1255 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1258 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1259 printf_filtered (_("a thread-local variable at offset %s "
1260 "in the thread-local storage for `%s'"),
1261 paddr_nz (load_addr), section->objfile->name);
1264 printf_filtered (_("static storage at address "));
1265 fputs_filtered (paddress (load_addr), gdb_stdout);
1266 if (section_is_overlay (section))
1268 load_addr = overlay_unmapped_address (load_addr, section);
1269 printf_filtered (_(",\n -- loaded at "));
1270 fputs_filtered (paddress (load_addr), gdb_stdout);
1271 printf_filtered (_(" in overlay section %s"),
1272 section->the_bfd_section->name);
1279 case LOC_OPTIMIZED_OUT:
1280 printf_filtered (_("optimized out"));
1284 printf_filtered (_("of unknown (botched) type"));
1287 printf_filtered (".\n");
1292 x_command (char *exp, int from_tty)
1294 struct expression *expr;
1295 struct format_data fmt;
1296 struct cleanup *old_chain;
1299 fmt.format = last_format;
1300 fmt.size = last_size;
1303 if (exp && *exp == '/')
1306 fmt = decode_format (&exp, last_format, last_size);
1309 /* If we have an expression, evaluate it and use it as the address. */
1311 if (exp != 0 && *exp != 0)
1313 expr = parse_expression (exp);
1314 /* Cause expression not to be there any more if this command is
1315 repeated with Newline. But don't clobber a user-defined
1316 command's definition. */
1319 old_chain = make_cleanup (free_current_contents, &expr);
1320 val = evaluate_expression (expr);
1321 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1322 val = value_ind (val);
1323 /* In rvalue contexts, such as this, functions are coerced into
1324 pointers to functions. This makes "x/i main" work. */
1325 if (/* last_format == 'i' && */
1326 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1327 && VALUE_LVAL (val) == lval_memory)
1328 next_address = VALUE_ADDRESS (val);
1330 next_address = value_as_address (val);
1331 do_cleanups (old_chain);
1334 do_examine (fmt, next_address);
1336 /* If the examine succeeds, we remember its size and format for next
1338 last_size = fmt.size;
1339 last_format = fmt.format;
1341 /* Set a couple of internal variables if appropriate. */
1342 if (last_examine_value)
1344 /* Make last address examined available to the user as $_. Use
1345 the correct pointer type. */
1346 struct type *pointer_type
1347 = lookup_pointer_type (value_type (last_examine_value));
1348 set_internalvar (lookup_internalvar ("_"),
1349 value_from_pointer (pointer_type,
1350 last_examine_address));
1352 /* Make contents of last address examined available to the user
1353 as $__. If the last value has not been fetched from memory
1354 then don't fetch it now; instead mark it by voiding the $__
1356 if (value_lazy (last_examine_value))
1357 set_internalvar (lookup_internalvar ("__"),
1358 allocate_value (builtin_type_void));
1360 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1365 /* Add an expression to the auto-display chain.
1366 Specify the expression. */
1369 display_command (char *exp, int from_tty)
1371 struct format_data fmt;
1372 struct expression *expr;
1373 struct display *new;
1377 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1379 if (tui_active && exp != NULL && *exp == '$')
1380 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1394 fmt = decode_format (&exp, 0, 0);
1395 if (fmt.size && fmt.format == 0)
1397 if (fmt.format == 'i' || fmt.format == 's')
1407 innermost_block = NULL;
1408 expr = parse_expression (exp);
1410 new = (struct display *) xmalloc (sizeof (struct display));
1412 new->exp_string = xstrdup (exp);
1414 new->block = innermost_block;
1415 new->next = display_chain;
1416 new->number = ++display_number;
1419 display_chain = new;
1421 if (from_tty && target_has_execution)
1422 do_one_display (new);
1429 free_display (struct display *d)
1431 xfree (d->exp_string);
1436 /* Clear out the display_chain. Done when new symtabs are loaded,
1437 since this invalidates the types stored in many expressions. */
1440 clear_displays (void)
1444 while ((d = display_chain) != NULL)
1446 display_chain = d->next;
1451 /* Delete the auto-display number NUM. */
1454 delete_display (int num)
1456 struct display *d1, *d;
1459 error (_("No display number %d."), num);
1461 if (display_chain->number == num)
1464 display_chain = d1->next;
1468 for (d = display_chain;; d = d->next)
1471 error (_("No display number %d."), num);
1472 if (d->next->number == num)
1482 /* Delete some values from the auto-display chain.
1483 Specify the element numbers. */
1486 undisplay_command (char *args, int from_tty)
1494 if (query (_("Delete all auto-display expressions? ")))
1503 while (*p1 >= '0' && *p1 <= '9')
1505 if (*p1 && *p1 != ' ' && *p1 != '\t')
1506 error (_("Arguments must be display numbers."));
1510 delete_display (num);
1513 while (*p == ' ' || *p == '\t')
1519 /* Display a single auto-display.
1520 Do nothing if the display cannot be printed in the current context,
1521 or if the display is disabled. */
1524 do_one_display (struct display *d)
1526 int within_current_scope;
1528 if (d->enabled_p == 0)
1533 volatile struct gdb_exception ex;
1534 TRY_CATCH (ex, RETURN_MASK_ALL)
1536 innermost_block = NULL;
1537 d->exp = parse_expression (d->exp_string);
1538 d->block = innermost_block;
1542 /* Can't re-parse the expression. Disable this display item. */
1544 warning (_("Unable to display \"%s\": %s"),
1545 d->exp_string, ex.message);
1551 within_current_scope = contained_in (get_selected_block (0), d->block);
1553 within_current_scope = 1;
1554 if (!within_current_scope)
1557 current_display_number = d->number;
1559 annotate_display_begin ();
1560 printf_filtered ("%d", d->number);
1561 annotate_display_number_end ();
1562 printf_filtered (": ");
1568 annotate_display_format ();
1570 printf_filtered ("x/");
1571 if (d->format.count != 1)
1572 printf_filtered ("%d", d->format.count);
1573 printf_filtered ("%c", d->format.format);
1574 if (d->format.format != 'i' && d->format.format != 's')
1575 printf_filtered ("%c", d->format.size);
1576 printf_filtered (" ");
1578 annotate_display_expression ();
1580 puts_filtered (d->exp_string);
1581 annotate_display_expression_end ();
1583 if (d->format.count != 1 || d->format.format == 'i')
1584 printf_filtered ("\n");
1586 printf_filtered (" ");
1588 val = evaluate_expression (d->exp);
1589 addr = value_as_address (val);
1590 if (d->format.format == 'i')
1591 addr = gdbarch_addr_bits_remove (current_gdbarch, addr);
1593 annotate_display_value ();
1595 do_examine (d->format, addr);
1599 struct value_print_options opts;
1601 annotate_display_format ();
1603 if (d->format.format)
1604 printf_filtered ("/%c ", d->format.format);
1606 annotate_display_expression ();
1608 puts_filtered (d->exp_string);
1609 annotate_display_expression_end ();
1611 printf_filtered (" = ");
1613 annotate_display_expression ();
1615 get_formatted_print_options (&opts, d->format.format);
1616 print_formatted (evaluate_expression (d->exp),
1617 d->format.size, &opts, gdb_stdout);
1618 printf_filtered ("\n");
1621 annotate_display_end ();
1623 gdb_flush (gdb_stdout);
1624 current_display_number = -1;
1627 /* Display all of the values on the auto-display chain which can be
1628 evaluated in the current scope. */
1635 for (d = display_chain; d; d = d->next)
1639 /* Delete the auto-display which we were in the process of displaying.
1640 This is done when there is an error or a signal. */
1643 disable_display (int num)
1647 for (d = display_chain; d; d = d->next)
1648 if (d->number == num)
1653 printf_unfiltered (_("No display number %d.\n"), num);
1657 disable_current_display (void)
1659 if (current_display_number >= 0)
1661 disable_display (current_display_number);
1662 fprintf_unfiltered (gdb_stderr, _("\
1663 Disabling display %d to avoid infinite recursion.\n"),
1664 current_display_number);
1666 current_display_number = -1;
1670 display_info (char *ignore, int from_tty)
1675 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1677 printf_filtered (_("Auto-display expressions now in effect:\n\
1678 Num Enb Expression\n"));
1680 for (d = display_chain; d; d = d->next)
1682 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1684 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1686 else if (d->format.format)
1687 printf_filtered ("/%c ", d->format.format);
1688 puts_filtered (d->exp_string);
1689 if (d->block && !contained_in (get_selected_block (0), d->block))
1690 printf_filtered (_(" (cannot be evaluated in the current context)"));
1691 printf_filtered ("\n");
1692 gdb_flush (gdb_stdout);
1697 enable_display (char *args, int from_tty)
1706 for (d = display_chain; d; d = d->next)
1713 while (*p1 >= '0' && *p1 <= '9')
1715 if (*p1 && *p1 != ' ' && *p1 != '\t')
1716 error (_("Arguments must be display numbers."));
1720 for (d = display_chain; d; d = d->next)
1721 if (d->number == num)
1726 printf_unfiltered (_("No display number %d.\n"), num);
1729 while (*p == ' ' || *p == '\t')
1735 disable_display_command (char *args, int from_tty)
1743 for (d = display_chain; d; d = d->next)
1750 while (*p1 >= '0' && *p1 <= '9')
1752 if (*p1 && *p1 != ' ' && *p1 != '\t')
1753 error (_("Arguments must be display numbers."));
1755 disable_display (atoi (p));
1758 while (*p == ' ' || *p == '\t')
1763 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1764 is unloaded), otherwise return 0. */
1767 display_uses_solib_p (const struct display *d,
1768 const struct so_list *solib)
1771 struct expression *const exp = d->exp;
1772 const union exp_element *const elts = exp->elts;
1774 if (d->block != NULL
1775 && solib_contains_address_p (solib, d->block->startaddr))
1778 for (endpos = exp->nelts; endpos > 0; )
1780 int i, args, oplen = 0;
1782 exp->language_defn->la_exp_desc->operator_length (exp, endpos,
1784 gdb_assert (oplen > 0);
1787 if (elts[i].opcode == OP_VAR_VALUE)
1789 const struct block *const block = elts[i + 1].block;
1790 const struct symbol *const symbol = elts[i + 2].symbol;
1791 const struct obj_section *const section =
1792 SYMBOL_OBJ_SECTION (symbol);
1795 && solib_contains_address_p (solib, block->startaddr))
1798 if (section && section->objfile == solib->objfile)
1807 /* display_chain items point to blocks and expressions. Some expressions in
1808 turn may point to symbols.
1809 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1810 obstack_free'd when a shared library is unloaded.
1811 Clear pointers that are about to become dangling.
1812 Both .exp and .block fields will be restored next time we need to display
1813 an item by re-parsing .exp_string field in the new execution context. */
1816 clear_dangling_display_expressions (struct so_list *solib)
1819 struct objfile *objfile = NULL;
1821 for (d = display_chain; d; d = d->next)
1823 if (d->exp && display_uses_solib_p (d, solib))
1833 /* Print the value in stack frame FRAME of a variable specified by a
1834 struct symbol. NAME is the name to print; if NULL then VAR's print
1835 name will be used. STREAM is the ui_file on which to print the
1836 value. INDENT specifies the number of indent levels to print
1837 before printing the variable name. */
1840 print_variable_and_value (const char *name, struct symbol *var,
1841 struct frame_info *frame,
1842 struct ui_file *stream, int indent)
1845 struct value_print_options opts;
1848 name = SYMBOL_PRINT_NAME (var);
1850 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1852 val = read_var_value (var, frame);
1853 get_user_print_options (&opts);
1854 common_val_print (val, stream, indent, &opts, current_language);
1855 fprintf_filtered (stream, "\n");
1859 printf_command (char *arg, int from_tty)
1863 char *string = NULL;
1864 struct value **val_args;
1866 char *current_substring;
1868 int allocated_args = 20;
1869 struct cleanup *old_cleanups;
1871 val_args = xmalloc (allocated_args * sizeof (struct value *));
1872 old_cleanups = make_cleanup (free_current_contents, &val_args);
1875 error_no_arg (_("format-control string and values to print"));
1877 /* Skip white space before format string */
1878 while (*s == ' ' || *s == '\t')
1881 /* A format string should follow, enveloped in double quotes. */
1883 error (_("Bad format string, missing '\"'."));
1885 /* Parse the format-control string and copy it into the string STRING,
1886 processing some kinds of escape sequence. */
1888 f = string = (char *) alloca (strlen (s) + 1);
1896 error (_("Bad format string, non-terminated '\"'."));
1929 /* ??? TODO: handle other escape sequences */
1930 error (_("Unrecognized escape character \\%c in format string."),
1940 /* Skip over " and following space and comma. */
1943 while (*s == ' ' || *s == '\t')
1946 if (*s != ',' && *s != 0)
1947 error (_("Invalid argument syntax"));
1951 while (*s == ' ' || *s == '\t')
1954 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1955 substrings = alloca (strlen (string) * 2);
1956 current_substring = substrings;
1959 /* Now scan the string for %-specs and see what kinds of args they want.
1960 argclass[I] classifies the %-specs so we can give printf_filtered
1961 something of the right size. */
1965 int_arg, long_arg, long_long_arg, ptr_arg,
1966 string_arg, wide_string_arg, wide_char_arg,
1967 double_arg, long_double_arg, decfloat_arg
1969 enum argclass *argclass;
1970 enum argclass this_argclass;
1975 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1982 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
1983 int seen_space = 0, seen_plus = 0;
1984 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
1985 int seen_big_d = 0, seen_double_big_d = 0;
1988 /* Check the validity of the format specifier, and work
1989 out what argument it expects. We only accept C89
1990 format strings, with the exception of long long (which
1991 we autoconf for). */
1993 /* Skip over "%%". */
2000 /* The first part of a format specifier is a set of flag
2002 while (strchr ("0-+ #", *f))
2015 /* The next part of a format specifier is a width. */
2016 while (strchr ("0123456789", *f))
2019 /* The next part of a format specifier is a precision. */
2024 while (strchr ("0123456789", *f))
2028 /* The next part of a format specifier is a length modifier. */
2049 /* Decimal32 modifier. */
2055 /* Decimal64 and Decimal128 modifiers. */
2060 /* Check for a Decimal128. */
2064 seen_double_big_d = 1;
2080 if (seen_space || seen_plus)
2087 this_argclass = int_arg;
2088 else if (lcount == 1)
2089 this_argclass = long_arg;
2091 this_argclass = long_long_arg;
2098 this_argclass = lcount == 0 ? int_arg : wide_char_arg;
2099 if (lcount > 1 || seen_h || seen_big_l)
2101 if (seen_prec || seen_zero || seen_space || seen_plus)
2106 this_argclass = ptr_arg;
2107 if (lcount || seen_h || seen_big_l)
2109 if (seen_prec || seen_zero || seen_space || seen_plus)
2114 this_argclass = lcount == 0 ? string_arg : wide_string_arg;
2115 if (lcount > 1 || seen_h || seen_big_l)
2117 if (seen_zero || seen_space || seen_plus)
2126 if (seen_big_h || seen_big_d || seen_double_big_d)
2127 this_argclass = decfloat_arg;
2128 else if (seen_big_l)
2129 this_argclass = long_double_arg;
2131 this_argclass = double_arg;
2133 if (lcount || seen_h)
2138 error (_("`*' not supported for precision or width in printf"));
2141 error (_("Format specifier `n' not supported in printf"));
2144 error (_("Incomplete format specifier at end of format string"));
2147 error (_("Unrecognized format specifier '%c' in printf"), *f);
2151 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2156 if (lcount > 1 && USE_PRINTF_I64)
2158 /* Windows' printf does support long long, but not the usual way.
2159 Convert %lld to %I64d. */
2160 int length_before_ll = f - last_arg - 1 - lcount;
2161 strncpy (current_substring, last_arg, length_before_ll);
2162 strcpy (current_substring + length_before_ll, "I64");
2163 current_substring[length_before_ll + 3] =
2164 last_arg[length_before_ll + lcount];
2165 current_substring += length_before_ll + 4;
2167 else if (this_argclass == wide_string_arg
2168 || this_argclass == wide_char_arg)
2170 /* Convert %ls or %lc to %s. */
2171 int length_before_ls = f - last_arg - 2;
2172 strncpy (current_substring, last_arg, length_before_ls);
2173 strcpy (current_substring + length_before_ls, "s");
2174 current_substring += length_before_ls + 2;
2178 strncpy (current_substring, last_arg, f - last_arg);
2179 current_substring += f - last_arg;
2181 *current_substring++ = '\0';
2183 argclass[nargs_wanted++] = this_argclass;
2186 /* Now, parse all arguments and evaluate them.
2187 Store the VALUEs in VAL_ARGS. */
2192 if (nargs == allocated_args)
2193 val_args = (struct value **) xrealloc ((char *) val_args,
2194 (allocated_args *= 2)
2195 * sizeof (struct value *));
2197 val_args[nargs] = parse_to_comma_and_eval (&s1);
2205 if (nargs != nargs_wanted)
2206 error (_("Wrong number of arguments for specified format-string"));
2208 /* Now actually print them. */
2209 current_substring = substrings;
2210 for (i = 0; i < nargs; i++)
2212 switch (argclass[i])
2219 tem = value_as_address (val_args[i]);
2221 /* This is a %s argument. Find the length of the string. */
2226 read_memory (tem + j, &c, 1);
2231 /* Copy the string contents into a string inside GDB. */
2232 str = (gdb_byte *) alloca (j + 1);
2234 read_memory (tem, str, j);
2237 printf_filtered (current_substring, (char *) str);
2240 case wide_string_arg:
2245 struct type *wctype = lookup_typename ("wchar_t", NULL, 0);
2246 int wcwidth = TYPE_LENGTH (wctype);
2247 gdb_byte *buf = alloca (wcwidth);
2248 struct obstack output;
2249 struct cleanup *inner_cleanup;
2251 tem = value_as_address (val_args[i]);
2253 /* This is a %s argument. Find the length of the string. */
2254 for (j = 0;; j += wcwidth)
2257 read_memory (tem + j, buf, wcwidth);
2258 if (extract_unsigned_integer (buf, wcwidth) == 0)
2262 /* Copy the string contents into a string inside GDB. */
2263 str = (gdb_byte *) alloca (j + wcwidth);
2265 read_memory (tem, str, j);
2266 memset (&str[j], 0, wcwidth);
2268 obstack_init (&output);
2269 inner_cleanup = make_cleanup_obstack_free (&output);
2271 convert_between_encodings (target_wide_charset (),
2274 &output, translit_char);
2275 obstack_grow_str0 (&output, "");
2277 printf_filtered (current_substring, obstack_base (&output));
2278 do_cleanups (inner_cleanup);
2283 struct type *wctype = lookup_typename ("wchar_t", NULL, 0);
2284 struct type *valtype;
2285 struct obstack output;
2286 struct cleanup *inner_cleanup;
2287 const gdb_byte *bytes;
2289 valtype = value_type (val_args[i]);
2290 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2291 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2292 error (_("expected wchar_t argument for %%lc"));
2294 bytes = value_contents (val_args[i]);
2296 obstack_init (&output);
2297 inner_cleanup = make_cleanup_obstack_free (&output);
2299 convert_between_encodings (target_wide_charset (),
2301 bytes, TYPE_LENGTH (valtype),
2302 TYPE_LENGTH (valtype),
2303 &output, translit_char);
2304 obstack_grow_str0 (&output, "");
2306 printf_filtered (current_substring, obstack_base (&output));
2307 do_cleanups (inner_cleanup);
2312 struct type *type = value_type (val_args[i]);
2316 /* If format string wants a float, unchecked-convert the value
2317 to floating point of the same size. */
2318 type = float_type_from_length (current_gdbarch, type);
2319 val = unpack_double (type, value_contents (val_args[i]), &inv);
2321 error (_("Invalid floating value found in program."));
2323 printf_filtered (current_substring, (double) val);
2326 case long_double_arg:
2327 #ifdef HAVE_LONG_DOUBLE
2329 struct type *type = value_type (val_args[i]);
2333 /* If format string wants a float, unchecked-convert the value
2334 to floating point of the same size. */
2335 type = float_type_from_length (current_gdbarch, type);
2336 val = unpack_double (type, value_contents (val_args[i]), &inv);
2338 error (_("Invalid floating value found in program."));
2340 printf_filtered (current_substring, (long double) val);
2344 error (_("long double not supported in printf"));
2347 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2349 long long val = value_as_long (val_args[i]);
2350 printf_filtered (current_substring, val);
2354 error (_("long long not supported in printf"));
2358 int val = value_as_long (val_args[i]);
2359 printf_filtered (current_substring, val);
2364 long val = value_as_long (val_args[i]);
2365 printf_filtered (current_substring, val);
2369 /* Handles decimal floating values. */
2372 const gdb_byte *param_ptr = value_contents (val_args[i]);
2373 #if defined (PRINTF_HAS_DECFLOAT)
2374 /* If we have native support for Decimal floating
2375 printing, handle it here. */
2376 printf_filtered (current_substring, param_ptr);
2379 /* As a workaround until vasprintf has native support for DFP
2380 we convert the DFP values to string and print them using
2381 the %s format specifier. */
2384 int nnull_chars = 0;
2386 /* Parameter data. */
2387 struct type *param_type = value_type (val_args[i]);
2388 unsigned int param_len = TYPE_LENGTH (param_type);
2390 /* DFP output data. */
2391 struct value *dfp_value = NULL;
2395 struct type *dfp_type = NULL;
2396 char decstr[MAX_DECIMAL_STRING];
2398 /* Points to the end of the string so that we can go back
2399 and check for DFP length modifiers. */
2400 eos = current_substring + strlen (current_substring);
2402 /* Look for the float/double format specifier. */
2403 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2404 && *eos != 'g' && *eos != 'G')
2409 /* Search for the '%' char and extract the size and type of
2410 the output decimal value based on its modifiers
2411 (%Hf, %Df, %DDf). */
2412 while (*--sos != '%')
2417 dfp_type = builtin_type (current_gdbarch)->builtin_decfloat;
2419 else if (*sos == 'D' && *(sos - 1) == 'D')
2422 dfp_type = builtin_type (current_gdbarch)->builtin_declong;
2428 dfp_type = builtin_type (current_gdbarch)->builtin_decdouble;
2432 /* Replace %Hf, %Df and %DDf with %s's. */
2435 /* Go through the whole format string and pull the correct
2436 number of chars back to compensate for the change in the
2437 format specifier. */
2438 while (nnull_chars < nargs - i)
2446 /* Conversion between different DFP types. */
2447 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2448 decimal_convert (param_ptr, param_len, dec, dfp_len);
2450 /* If this is a non-trivial conversion, just output 0.
2451 A correct converted value can be displayed by explicitly
2452 casting to a DFP type. */
2453 decimal_from_string (dec, dfp_len, "0");
2455 dfp_value = value_from_decfloat (dfp_type, dec);
2457 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2459 decimal_to_string (dfp_ptr, dfp_len, decstr);
2461 /* Print the DFP value. */
2462 printf_filtered (current_substring, decstr);
2470 /* We avoid the host's %p because pointers are too
2471 likely to be the wrong size. The only interesting
2472 modifier for %p is a width; extract that, and then
2473 handle %p as glibc would: %#x or a literal "(nil)". */
2475 char *p, *fmt, *fmt_p;
2476 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2477 long long val = value_as_long (val_args[i]);
2479 long val = value_as_long (val_args[i]);
2482 fmt = alloca (strlen (current_substring) + 5);
2484 /* Copy up to the leading %. */
2485 p = current_substring;
2489 int is_percent = (*p == '%');
2503 /* Copy any width. */
2504 while (*p >= '0' && *p < '9')
2507 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2510 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2516 printf_filtered (fmt, val);
2522 printf_filtered (fmt, "(nil)");
2528 internal_error (__FILE__, __LINE__,
2529 _("failed internal consistency check"));
2531 /* Skip to the next substring. */
2532 current_substring += strlen (current_substring) + 1;
2534 /* Print the portion of the format string after the last argument. */
2535 puts_filtered (last_arg);
2537 do_cleanups (old_cleanups);
2541 _initialize_printcmd (void)
2543 struct cmd_list_element *c;
2545 current_display_number = -1;
2547 observer_attach_solib_unloaded (clear_dangling_display_expressions);
2549 add_info ("address", address_info,
2550 _("Describe where symbol SYM is stored."));
2552 add_info ("symbol", sym_info, _("\
2553 Describe what symbol is at location ADDR.\n\
2554 Only for symbols with fixed locations (global or static scope)."));
2556 add_com ("x", class_vars, x_command, _("\
2557 Examine memory: x/FMT ADDRESS.\n\
2558 ADDRESS is an expression for the memory address to examine.\n\
2559 FMT is a repeat count followed by a format letter and a size letter.\n\
2560 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2561 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2562 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2563 The specified number of objects of the specified size are printed\n\
2564 according to the format.\n\n\
2565 Defaults for format and size letters are those previously used.\n\
2566 Default count is 1. Default address is following last thing printed\n\
2567 with this command or \"print\"."));
2570 add_com ("whereis", class_vars, whereis_command,
2571 _("Print line number and file of definition of variable."));
2574 add_info ("display", display_info, _("\
2575 Expressions to display when program stops, with code numbers."));
2577 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2578 Cancel some expressions to be displayed when program stops.\n\
2579 Arguments are the code numbers of the expressions to stop displaying.\n\
2580 No argument means cancel all automatic-display expressions.\n\
2581 \"delete display\" has the same effect as this command.\n\
2582 Do \"info display\" to see current list of code numbers."),
2585 add_com ("display", class_vars, display_command, _("\
2586 Print value of expression EXP each time the program stops.\n\
2587 /FMT may be used before EXP as in the \"print\" command.\n\
2588 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2589 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2590 and examining is done as in the \"x\" command.\n\n\
2591 With no argument, display all currently requested auto-display expressions.\n\
2592 Use \"undisplay\" to cancel display requests previously made."));
2594 add_cmd ("display", class_vars, enable_display, _("\
2595 Enable some expressions to be displayed when program stops.\n\
2596 Arguments are the code numbers of the expressions to resume displaying.\n\
2597 No argument means enable all automatic-display expressions.\n\
2598 Do \"info display\" to see current list of code numbers."), &enablelist);
2600 add_cmd ("display", class_vars, disable_display_command, _("\
2601 Disable some expressions to be displayed when program stops.\n\
2602 Arguments are the code numbers of the expressions to stop displaying.\n\
2603 No argument means disable all automatic-display expressions.\n\
2604 Do \"info display\" to see current list of code numbers."), &disablelist);
2606 add_cmd ("display", class_vars, undisplay_command, _("\
2607 Cancel some expressions to be displayed when program stops.\n\
2608 Arguments are the code numbers of the expressions to stop displaying.\n\
2609 No argument means cancel all automatic-display expressions.\n\
2610 Do \"info display\" to see current list of code numbers."), &deletelist);
2612 add_com ("printf", class_vars, printf_command, _("\
2613 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2614 This is useful for formatted output in user-defined commands."));
2616 add_com ("output", class_vars, output_command, _("\
2617 Like \"print\" but don't put in value history and don't print newline.\n\
2618 This is useful in user-defined commands."));
2620 add_prefix_cmd ("set", class_vars, set_command, _("\
2621 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2622 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2623 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2624 with $), a register (a few standard names starting with $), or an actual\n\
2625 variable in the program being debugged. EXP is any valid expression.\n\
2626 Use \"set variable\" for variables with names identical to set subcommands.\n\
2628 With a subcommand, this command modifies parts of the gdb environment.\n\
2629 You can see these environment settings with the \"show\" command."),
2630 &setlist, "set ", 1, &cmdlist);
2632 add_com ("assign", class_vars, set_command, _("\
2633 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2634 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2635 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2636 with $), a register (a few standard names starting with $), or an actual\n\
2637 variable in the program being debugged. EXP is any valid expression.\n\
2638 Use \"set variable\" for variables with names identical to set subcommands.\n\
2639 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2640 You can see these environment settings with the \"show\" command."));
2642 /* "call" is the same as "set", but handy for dbx users to call fns. */
2643 c = add_com ("call", class_vars, call_command, _("\
2644 Call a function in the program.\n\
2645 The argument is the function name and arguments, in the notation of the\n\
2646 current working language. The result is printed and saved in the value\n\
2647 history, if it is not void."));
2648 set_cmd_completer (c, expression_completer);
2650 add_cmd ("variable", class_vars, set_command, _("\
2651 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2652 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2653 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2654 with $), a register (a few standard names starting with $), or an actual\n\
2655 variable in the program being debugged. EXP is any valid expression.\n\
2656 This may usually be abbreviated to simply \"set\"."),
2659 c = add_com ("print", class_vars, print_command, _("\
2660 Print value of expression EXP.\n\
2661 Variables accessible are those of the lexical environment of the selected\n\
2662 stack frame, plus all those whose scope is global or an entire file.\n\
2664 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2665 $$NUM refers to NUM'th value back from the last one.\n\
2666 Names starting with $ refer to registers (with the values they would have\n\
2667 if the program were to return to the stack frame now selected, restoring\n\
2668 all registers saved by frames farther in) or else to debugger\n\
2669 \"convenience\" variables (any such name not a known register).\n\
2670 Use assignment expressions to give values to convenience variables.\n\
2672 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2673 @ is a binary operator for treating consecutive data objects\n\
2674 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2675 element is FOO, whose second element is stored in the space following\n\
2676 where FOO is stored, etc. FOO must be an expression whose value\n\
2677 resides in memory.\n\
2679 EXP may be preceded with /FMT, where FMT is a format letter\n\
2680 but no count or size letter (see \"x\" command)."));
2681 set_cmd_completer (c, expression_completer);
2682 add_com_alias ("p", "print", class_vars, 1);
2684 c = add_com ("inspect", class_vars, inspect_command, _("\
2685 Same as \"print\" command, except that if you are running in the epoch\n\
2686 environment, the value is printed in its own window."));
2687 set_cmd_completer (c, expression_completer);
2689 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2690 &max_symbolic_offset, _("\
2691 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2692 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2694 show_max_symbolic_offset,
2695 &setprintlist, &showprintlist);
2696 add_setshow_boolean_cmd ("symbol-filename", no_class,
2697 &print_symbol_filename, _("\
2698 Set printing of source filename and line number with <symbol>."), _("\
2699 Show printing of source filename and line number with <symbol>."), NULL,
2701 show_print_symbol_filename,
2702 &setprintlist, &showprintlist);
2704 /* For examine/instruction a single byte quantity is specified as
2705 the data. This avoids problems with value_at_lazy() requiring a
2706 valid data type (and rejecting VOID). */
2707 examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2709 examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2710 examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2711 examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2712 examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);