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 */
72 /* True if the value should be printed raw -- that is, bypassing
73 python-based formatters. */
77 /* Last specified output format. */
79 static char last_format = 0;
81 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
83 static char last_size = 'w';
85 /* Default address to examine next, and associated architecture. */
87 static struct gdbarch *next_gdbarch;
88 static CORE_ADDR next_address;
90 /* Number of delay instructions following current disassembled insn. */
92 static int branch_delay_insns;
94 /* Last address examined. */
96 static CORE_ADDR last_examine_address;
98 /* Contents of last address examined.
99 This is not valid past the end of the `x' command! */
101 static struct value *last_examine_value;
103 /* Largest offset between a symbolic value and an address, that will be
104 printed as `0x1234 <symbol+offset>'. */
106 static unsigned int max_symbolic_offset = UINT_MAX;
108 show_max_symbolic_offset (struct ui_file *file, int from_tty,
109 struct cmd_list_element *c, const char *value)
111 fprintf_filtered (file, _("\
112 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
116 /* Append the source filename and linenumber of the symbol when
117 printing a symbolic value as `<symbol at filename:linenum>' if set. */
118 static int print_symbol_filename = 0;
120 show_print_symbol_filename (struct ui_file *file, int from_tty,
121 struct cmd_list_element *c, const char *value)
123 fprintf_filtered (file, _("\
124 Printing of source filename and line number with <symbol> is %s.\n"),
128 /* Number of auto-display expression currently being displayed.
129 So that we can disable it if we get an error or a signal within it.
130 -1 when not doing one. */
132 int current_display_number;
136 /* Chain link to next auto-display item. */
137 struct display *next;
139 /* The expression as the user typed it. */
142 /* Expression to be evaluated and displayed. */
143 struct expression *exp;
145 /* Item number of this auto-display item. */
148 /* Display format specified. */
149 struct format_data format;
151 /* Program space associated with `block'. */
152 struct program_space *pspace;
154 /* Innermost block required by this expression when evaluated */
157 /* Status of this display (enabled or disabled) */
161 /* Chain of expressions whose values should be displayed
162 automatically each time the program stops. */
164 static struct display *display_chain;
166 static int display_number;
168 /* Prototypes for exported functions. */
170 void output_command (char *, int);
172 void _initialize_printcmd (void);
174 /* Prototypes for local functions. */
176 static void do_one_display (struct display *);
179 /* Decode a format specification. *STRING_PTR should point to it.
180 OFORMAT and OSIZE are used as defaults for the format and size
181 if none are given in the format specification.
182 If OSIZE is zero, then the size field of the returned value
183 should be set only if a size is explicitly specified by the
185 The structure returned describes all the data
186 found in the specification. In addition, *STRING_PTR is advanced
187 past the specification and past all whitespace following it. */
189 static struct format_data
190 decode_format (char **string_ptr, int oformat, int osize)
192 struct format_data val;
193 char *p = *string_ptr;
200 if (*p >= '0' && *p <= '9')
201 val.count = atoi (p);
202 while (*p >= '0' && *p <= '9')
205 /* Now process size or format letters that follow. */
209 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
216 else if (*p >= 'a' && *p <= 'z')
222 while (*p == ' ' || *p == '\t')
226 /* Set defaults for format and size if not specified. */
227 if (val.format == '?')
231 /* Neither has been specified. */
232 val.format = oformat;
236 /* If a size is specified, any format makes a reasonable
237 default except 'i'. */
238 val.format = oformat == 'i' ? 'x' : oformat;
240 else if (val.size == '?')
244 /* Pick the appropriate size for an address. This is deferred
245 until do_examine when we know the actual architecture to use.
246 A special size value of 'a' is used to indicate this case. */
247 val.size = osize ? 'a' : osize;
250 /* Floating point has to be word or giantword. */
251 if (osize == 'w' || osize == 'g')
254 /* Default it to giantword if the last used size is not
256 val.size = osize ? 'g' : osize;
259 /* Characters default to one byte. */
260 val.size = osize ? 'b' : osize;
263 /* The default is the size most recently specified. */
270 /* Print value VAL on stream according to OPTIONS.
271 Do not end with a newline.
272 SIZE is the letter for the size of datum being printed.
273 This is used to pad hex numbers so they line up. SIZE is 0
274 for print / output and set for examine. */
277 print_formatted (struct value *val, int size,
278 const struct value_print_options *options,
279 struct ui_file *stream)
281 struct type *type = check_typedef (value_type (val));
282 int len = TYPE_LENGTH (type);
284 if (VALUE_LVAL (val) == lval_memory)
285 next_address = value_address (val) + len;
289 switch (options->format)
293 struct type *elttype = value_type (val);
294 next_address = (value_address (val)
295 + val_print_string (elttype,
296 value_address (val), -1,
302 /* We often wrap here if there are long symbolic names. */
304 next_address = (value_address (val)
305 + gdb_print_insn (get_type_arch (type),
306 value_address (val), stream,
307 &branch_delay_insns));
312 if (options->format == 0 || options->format == 's'
313 || TYPE_CODE (type) == TYPE_CODE_REF
314 || TYPE_CODE (type) == TYPE_CODE_ARRAY
315 || TYPE_CODE (type) == TYPE_CODE_STRING
316 || TYPE_CODE (type) == TYPE_CODE_STRUCT
317 || TYPE_CODE (type) == TYPE_CODE_UNION
318 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
319 value_print (val, stream, options);
321 /* User specified format, so don't look to the the type to
322 tell us what to do. */
323 print_scalar_formatted (value_contents (val), type,
324 options, size, stream);
327 /* Return builtin floating point type of same length as TYPE.
328 If no such type is found, return TYPE itself. */
330 float_type_from_length (struct type *type)
332 struct gdbarch *gdbarch = get_type_arch (type);
333 const struct builtin_type *builtin = builtin_type (gdbarch);
334 unsigned int len = TYPE_LENGTH (type);
336 if (len == TYPE_LENGTH (builtin->builtin_float))
337 type = builtin->builtin_float;
338 else if (len == TYPE_LENGTH (builtin->builtin_double))
339 type = builtin->builtin_double;
340 else if (len == TYPE_LENGTH (builtin->builtin_long_double))
341 type = builtin->builtin_long_double;
346 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
347 according to OPTIONS and SIZE on STREAM.
348 Formats s and i are not supported at this level.
350 This is how the elements of an array or structure are printed
354 print_scalar_formatted (const void *valaddr, struct type *type,
355 const struct value_print_options *options,
356 int size, struct ui_file *stream)
358 struct gdbarch *gdbarch = get_type_arch (type);
359 LONGEST val_long = 0;
360 unsigned int len = TYPE_LENGTH (type);
361 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
363 /* If we get here with a string format, try again without it. Go
364 all the way back to the language printers, which may call us
366 if (options->format == 's')
368 struct value_print_options opts = *options;
371 val_print (type, valaddr, 0, 0, stream, 0, &opts,
376 if (len > sizeof(LONGEST) &&
377 (TYPE_CODE (type) == TYPE_CODE_INT
378 || TYPE_CODE (type) == TYPE_CODE_ENUM))
380 switch (options->format)
383 print_octal_chars (stream, valaddr, len, byte_order);
387 print_decimal_chars (stream, valaddr, len, byte_order);
390 print_binary_chars (stream, valaddr, len, byte_order);
393 print_hex_chars (stream, valaddr, len, byte_order);
396 print_char_chars (stream, type, valaddr, len, byte_order);
403 if (options->format != 'f')
404 val_long = unpack_long (type, valaddr);
406 /* If the value is a pointer, and pointers and addresses are not the
407 same, then at this point, the value's length (in target bytes) is
408 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
409 if (TYPE_CODE (type) == TYPE_CODE_PTR)
410 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
412 /* If we are printing it as unsigned, truncate it in case it is actually
413 a negative signed value (e.g. "print/u (short)-1" should print 65535
414 (if shorts are 16 bits) instead of 4294967295). */
415 if (options->format != 'd' || TYPE_UNSIGNED (type))
417 if (len < sizeof (LONGEST))
418 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
421 switch (options->format)
426 /* No size specified, like in print. Print varying # of digits. */
427 print_longest (stream, 'x', 1, val_long);
436 print_longest (stream, size, 1, val_long);
439 error (_("Undefined output size \"%c\"."), size);
444 print_longest (stream, 'd', 1, val_long);
448 print_longest (stream, 'u', 0, val_long);
453 print_longest (stream, 'o', 1, val_long);
455 fprintf_filtered (stream, "0");
460 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;
767 get_user_print_options (&opts);
770 fprintf_filtered (stream, "0");
772 else if (opts.addressprint)
774 fputs_filtered (paddress (gdbarch, addr), stream);
775 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
779 print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
784 /* Examine data at address ADDR in format FMT.
785 Fetch it from memory and print on gdb_stdout. */
788 do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
793 struct type *val_type = NULL;
796 struct value_print_options opts;
801 next_gdbarch = gdbarch;
804 /* String or instruction format implies fetch single bytes
805 regardless of the specified size. */
806 if (format == 's' || format == 'i')
811 /* Pick the appropriate size for an address. */
812 if (gdbarch_ptr_bit (next_gdbarch) == 64)
814 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
816 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
819 /* Bad value for gdbarch_ptr_bit. */
820 internal_error (__FILE__, __LINE__,
821 _("failed internal consistency check"));
825 val_type = builtin_type (next_gdbarch)->builtin_int8;
826 else if (size == 'h')
827 val_type = builtin_type (next_gdbarch)->builtin_int16;
828 else if (size == 'w')
829 val_type = builtin_type (next_gdbarch)->builtin_int32;
830 else if (size == 'g')
831 val_type = builtin_type (next_gdbarch)->builtin_int64;
838 if (format == 's' || format == 'i')
841 get_formatted_print_options (&opts, format);
843 /* Print as many objects as specified in COUNT, at most maxelts per line,
844 with the address of the next one at the start of each line. */
850 fputs_filtered (pc_prefix (next_address), gdb_stdout);
851 print_address (next_gdbarch, next_address, gdb_stdout);
852 printf_filtered (":");
857 printf_filtered ("\t");
858 /* Note that print_formatted sets next_address for the next
860 last_examine_address = next_address;
862 if (last_examine_value)
863 value_free (last_examine_value);
865 /* The value to be displayed is not fetched greedily.
866 Instead, to avoid the possibility of a fetched value not
867 being used, its retrieval is delayed until the print code
868 uses it. When examining an instruction stream, the
869 disassembler will perform its own memory fetch using just
870 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
871 the disassembler be modified so that LAST_EXAMINE_VALUE
872 is left with the byte sequence from the last complete
873 instruction fetched from memory? */
874 last_examine_value = value_at_lazy (val_type, next_address);
876 if (last_examine_value)
877 release_value (last_examine_value);
879 print_formatted (last_examine_value, size, &opts, gdb_stdout);
881 /* Display any branch delay slots following the final insn. */
882 if (format == 'i' && count == 1)
883 count += branch_delay_insns;
885 printf_filtered ("\n");
886 gdb_flush (gdb_stdout);
891 validate_format (struct format_data fmt, char *cmdname)
894 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
896 error (_("Item count other than 1 is meaningless in \"%s\" command."),
898 if (fmt.format == 'i')
899 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
900 fmt.format, cmdname);
903 /* Evaluate string EXP as an expression in the current language and
904 print the resulting value. EXP may contain a format specifier as the
905 first argument ("/x myvar" for example, to print myvar in hex). */
908 print_command_1 (char *exp, int inspect, int voidprint)
910 struct expression *expr;
911 struct cleanup *old_chain = 0;
914 struct format_data fmt;
917 if (exp && *exp == '/')
920 fmt = decode_format (&exp, last_format, 0);
921 validate_format (fmt, "print");
922 last_format = format = fmt.format;
935 expr = parse_expression (exp);
936 old_chain = make_cleanup (free_current_contents, &expr);
938 val = evaluate_expression (expr);
941 val = access_value_history (0);
943 if (voidprint || (val && value_type (val) &&
944 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
946 struct value_print_options opts;
947 int histindex = record_latest_value (val);
950 annotate_value_history_begin (histindex, value_type (val));
952 annotate_value_begin (value_type (val));
955 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
957 else if (histindex >= 0)
958 printf_filtered ("$%d = ", histindex);
961 annotate_value_history_value ();
963 get_formatted_print_options (&opts, format);
964 opts.inspect_it = inspect;
967 print_formatted (val, fmt.size, &opts, gdb_stdout);
968 printf_filtered ("\n");
971 annotate_value_history_end ();
973 annotate_value_end ();
976 printf_unfiltered ("\") )\030");
980 do_cleanups (old_chain);
984 print_command (char *exp, int from_tty)
986 print_command_1 (exp, 0, 1);
989 /* Same as print, except in epoch, it gets its own window. */
991 inspect_command (char *exp, int from_tty)
993 extern int epoch_interface;
995 print_command_1 (exp, epoch_interface, 1);
998 /* Same as print, except it doesn't print void results. */
1000 call_command (char *exp, int from_tty)
1002 print_command_1 (exp, 0, 0);
1006 output_command (char *exp, int from_tty)
1008 struct expression *expr;
1009 struct cleanup *old_chain;
1012 struct format_data fmt;
1013 struct value_print_options opts;
1018 if (exp && *exp == '/')
1021 fmt = decode_format (&exp, 0, 0);
1022 validate_format (fmt, "output");
1023 format = fmt.format;
1026 expr = parse_expression (exp);
1027 old_chain = make_cleanup (free_current_contents, &expr);
1029 val = evaluate_expression (expr);
1031 annotate_value_begin (value_type (val));
1033 get_formatted_print_options (&opts, format);
1035 print_formatted (val, fmt.size, &opts, gdb_stdout);
1037 annotate_value_end ();
1040 gdb_flush (gdb_stdout);
1042 do_cleanups (old_chain);
1046 set_command (char *exp, int from_tty)
1048 struct expression *expr = parse_expression (exp);
1049 struct cleanup *old_chain =
1050 make_cleanup (free_current_contents, &expr);
1051 evaluate_expression (expr);
1052 do_cleanups (old_chain);
1056 sym_info (char *arg, int from_tty)
1058 struct minimal_symbol *msymbol;
1059 struct objfile *objfile;
1060 struct obj_section *osect;
1061 CORE_ADDR addr, sect_addr;
1063 unsigned int offset;
1066 error_no_arg (_("address"));
1068 addr = parse_and_eval_address (arg);
1069 ALL_OBJSECTIONS (objfile, osect)
1071 /* Only process each object file once, even if there's a separate
1073 if (objfile->separate_debug_objfile_backlink)
1076 sect_addr = overlay_mapped_address (addr, osect);
1078 if (obj_section_addr (osect) <= sect_addr
1079 && sect_addr < obj_section_endaddr (osect)
1080 && (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, osect)))
1082 const char *obj_name, *mapped, *sec_name, *msym_name;
1084 struct cleanup *old_chain;
1087 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1088 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1089 sec_name = osect->the_bfd_section->name;
1090 msym_name = SYMBOL_PRINT_NAME (msymbol);
1092 /* Don't print the offset if it is zero.
1093 We assume there's no need to handle i18n of "sym + offset". */
1095 loc_string = xstrprintf ("%s + %u", msym_name, offset);
1097 loc_string = xstrprintf ("%s", msym_name);
1099 /* Use a cleanup to free loc_string in case the user quits
1100 a pagination request inside printf_filtered. */
1101 old_chain = make_cleanup (xfree, loc_string);
1103 gdb_assert (osect->objfile && osect->objfile->name);
1104 obj_name = osect->objfile->name;
1106 if (MULTI_OBJFILE_P ())
1107 if (pc_in_unmapped_range (addr, osect))
1108 if (section_is_overlay (osect))
1109 printf_filtered (_("%s in load address range of "
1110 "%s overlay section %s of %s\n"),
1111 loc_string, mapped, sec_name, obj_name);
1113 printf_filtered (_("%s in load address range of "
1114 "section %s of %s\n"),
1115 loc_string, sec_name, obj_name);
1117 if (section_is_overlay (osect))
1118 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1119 loc_string, mapped, sec_name, obj_name);
1121 printf_filtered (_("%s in section %s of %s\n"),
1122 loc_string, sec_name, obj_name);
1124 if (pc_in_unmapped_range (addr, osect))
1125 if (section_is_overlay (osect))
1126 printf_filtered (_("%s in load address range of %s overlay "
1128 loc_string, mapped, sec_name);
1130 printf_filtered (_("%s in load address range of section %s\n"),
1131 loc_string, sec_name);
1133 if (section_is_overlay (osect))
1134 printf_filtered (_("%s in %s overlay section %s\n"),
1135 loc_string, mapped, sec_name);
1137 printf_filtered (_("%s in section %s\n"),
1138 loc_string, sec_name);
1140 do_cleanups (old_chain);
1144 printf_filtered (_("No symbol matches %s.\n"), arg);
1148 address_info (char *exp, int from_tty)
1150 struct gdbarch *gdbarch;
1153 struct minimal_symbol *msymbol;
1155 struct obj_section *section;
1156 CORE_ADDR load_addr;
1157 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1158 if exp is a field of `this'. */
1161 error (_("Argument required."));
1163 sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
1164 &is_a_field_of_this);
1167 if (is_a_field_of_this)
1169 printf_filtered ("Symbol \"");
1170 fprintf_symbol_filtered (gdb_stdout, exp,
1171 current_language->la_language, DMGL_ANSI);
1172 printf_filtered ("\" is a field of the local class variable ");
1173 if (current_language->la_language == language_objc)
1174 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1176 printf_filtered ("`this'\n");
1180 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1182 if (msymbol != NULL)
1184 gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
1185 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1187 printf_filtered ("Symbol \"");
1188 fprintf_symbol_filtered (gdb_stdout, exp,
1189 current_language->la_language, DMGL_ANSI);
1190 printf_filtered ("\" is at ");
1191 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1192 printf_filtered (" in a file compiled without debugging");
1193 section = SYMBOL_OBJ_SECTION (msymbol);
1194 if (section_is_overlay (section))
1196 load_addr = overlay_unmapped_address (load_addr, section);
1197 printf_filtered (",\n -- loaded at ");
1198 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1199 printf_filtered (" in overlay section %s",
1200 section->the_bfd_section->name);
1202 printf_filtered (".\n");
1205 error (_("No symbol \"%s\" in current context."), exp);
1209 printf_filtered ("Symbol \"");
1210 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
1211 current_language->la_language, DMGL_ANSI);
1212 printf_filtered ("\" is ");
1213 val = SYMBOL_VALUE (sym);
1214 section = SYMBOL_OBJ_SECTION (sym);
1215 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
1217 switch (SYMBOL_CLASS (sym))
1220 case LOC_CONST_BYTES:
1221 printf_filtered ("constant");
1225 printf_filtered ("a label at address ");
1226 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1227 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1228 if (section_is_overlay (section))
1230 load_addr = overlay_unmapped_address (load_addr, section);
1231 printf_filtered (",\n -- loaded at ");
1232 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1233 printf_filtered (" in overlay section %s",
1234 section->the_bfd_section->name);
1239 /* FIXME: cagney/2004-01-26: It should be possible to
1240 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1241 Unfortunately DWARF 2 stores the frame-base (instead of the
1242 function) location in a function's symbol. Oops! For the
1243 moment enable this when/where applicable. */
1244 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, gdb_stdout);
1248 /* GDBARCH is the architecture associated with the objfile the symbol
1249 is defined in; the target architecture may be different, and may
1250 provide additional registers. However, we do not know the target
1251 architecture at this point. We assume the objfile architecture
1252 will contain all the standard registers that occur in debug info
1254 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1256 if (SYMBOL_IS_ARGUMENT (sym))
1257 printf_filtered (_("an argument in register %s"),
1258 gdbarch_register_name (gdbarch, regno));
1260 printf_filtered (_("a variable in register %s"),
1261 gdbarch_register_name (gdbarch, regno));
1265 printf_filtered (_("static storage at address "));
1266 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1267 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1268 if (section_is_overlay (section))
1270 load_addr = overlay_unmapped_address (load_addr, section);
1271 printf_filtered (_(",\n -- loaded at "));
1272 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1273 printf_filtered (_(" in overlay section %s"),
1274 section->the_bfd_section->name);
1278 case LOC_REGPARM_ADDR:
1279 /* Note comment at LOC_REGISTER. */
1280 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1281 printf_filtered (_("address of an argument in register %s"),
1282 gdbarch_register_name (gdbarch, regno));
1286 printf_filtered (_("an argument at offset %ld"), val);
1290 printf_filtered (_("a local variable at frame offset %ld"), val);
1294 printf_filtered (_("a reference argument at offset %ld"), val);
1298 printf_filtered (_("a typedef"));
1302 printf_filtered (_("a function at address "));
1303 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1304 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1305 if (section_is_overlay (section))
1307 load_addr = overlay_unmapped_address (load_addr, section);
1308 printf_filtered (_(",\n -- loaded at "));
1309 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1310 printf_filtered (_(" in overlay section %s"),
1311 section->the_bfd_section->name);
1315 case LOC_UNRESOLVED:
1317 struct minimal_symbol *msym;
1319 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym), NULL, NULL);
1321 printf_filtered ("unresolved");
1324 section = SYMBOL_OBJ_SECTION (msym);
1325 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1328 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1329 printf_filtered (_("a thread-local variable at offset %s "
1330 "in the thread-local storage for `%s'"),
1331 paddress (gdbarch, load_addr),
1332 section->objfile->name);
1335 printf_filtered (_("static storage at address "));
1336 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1337 if (section_is_overlay (section))
1339 load_addr = overlay_unmapped_address (load_addr, section);
1340 printf_filtered (_(",\n -- loaded at "));
1341 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1342 printf_filtered (_(" in overlay section %s"),
1343 section->the_bfd_section->name);
1350 case LOC_OPTIMIZED_OUT:
1351 printf_filtered (_("optimized out"));
1355 printf_filtered (_("of unknown (botched) type"));
1358 printf_filtered (".\n");
1363 x_command (char *exp, int from_tty)
1365 struct expression *expr;
1366 struct format_data fmt;
1367 struct cleanup *old_chain;
1370 fmt.format = last_format ? last_format : 'x';
1371 fmt.size = last_size;
1375 if (exp && *exp == '/')
1378 fmt = decode_format (&exp, last_format, last_size);
1381 /* If we have an expression, evaluate it and use it as the address. */
1383 if (exp != 0 && *exp != 0)
1385 expr = parse_expression (exp);
1386 /* Cause expression not to be there any more if this command is
1387 repeated with Newline. But don't clobber a user-defined
1388 command's definition. */
1391 old_chain = make_cleanup (free_current_contents, &expr);
1392 val = evaluate_expression (expr);
1393 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1394 val = value_ind (val);
1395 /* In rvalue contexts, such as this, functions are coerced into
1396 pointers to functions. This makes "x/i main" work. */
1397 if (/* last_format == 'i' && */
1398 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1399 && VALUE_LVAL (val) == lval_memory)
1400 next_address = value_address (val);
1402 next_address = value_as_address (val);
1404 next_gdbarch = expr->gdbarch;
1405 do_cleanups (old_chain);
1409 error_no_arg (_("starting display address"));
1411 do_examine (fmt, next_gdbarch, next_address);
1413 /* If the examine succeeds, we remember its size and format for next
1415 last_size = fmt.size;
1416 last_format = fmt.format;
1418 /* Set a couple of internal variables if appropriate. */
1419 if (last_examine_value)
1421 /* Make last address examined available to the user as $_. Use
1422 the correct pointer type. */
1423 struct type *pointer_type
1424 = lookup_pointer_type (value_type (last_examine_value));
1425 set_internalvar (lookup_internalvar ("_"),
1426 value_from_pointer (pointer_type,
1427 last_examine_address));
1429 /* Make contents of last address examined available to the user
1430 as $__. If the last value has not been fetched from memory
1431 then don't fetch it now; instead mark it by voiding the $__
1433 if (value_lazy (last_examine_value))
1434 clear_internalvar (lookup_internalvar ("__"));
1436 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1441 /* Add an expression to the auto-display chain.
1442 Specify the expression. */
1445 display_command (char *exp, int from_tty)
1447 struct format_data fmt;
1448 struct expression *expr;
1449 struct display *new;
1453 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1455 if (tui_active && exp != NULL && *exp == '$')
1456 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1470 fmt = decode_format (&exp, 0, 0);
1471 if (fmt.size && fmt.format == 0)
1473 if (fmt.format == 'i' || fmt.format == 's')
1484 innermost_block = NULL;
1485 expr = parse_expression (exp);
1487 new = (struct display *) xmalloc (sizeof (struct display));
1489 new->exp_string = xstrdup (exp);
1491 new->block = innermost_block;
1492 new->pspace = current_program_space;
1493 new->next = display_chain;
1494 new->number = ++display_number;
1497 display_chain = new;
1499 if (from_tty && target_has_execution)
1500 do_one_display (new);
1507 free_display (struct display *d)
1509 xfree (d->exp_string);
1514 /* Clear out the display_chain. Done when new symtabs are loaded,
1515 since this invalidates the types stored in many expressions. */
1518 clear_displays (void)
1522 while ((d = display_chain) != NULL)
1524 display_chain = d->next;
1529 /* Delete the auto-display number NUM. */
1532 delete_display (int num)
1534 struct display *d1, *d;
1537 error (_("No display number %d."), num);
1539 if (display_chain->number == num)
1542 display_chain = d1->next;
1546 for (d = display_chain;; d = d->next)
1549 error (_("No display number %d."), num);
1550 if (d->next->number == num)
1560 /* Delete some values from the auto-display chain.
1561 Specify the element numbers. */
1564 undisplay_command (char *args, int from_tty)
1572 if (query (_("Delete all auto-display expressions? ")))
1581 while (*p1 >= '0' && *p1 <= '9')
1583 if (*p1 && *p1 != ' ' && *p1 != '\t')
1584 error (_("Arguments must be display numbers."));
1588 delete_display (num);
1591 while (*p == ' ' || *p == '\t')
1597 /* Display a single auto-display.
1598 Do nothing if the display cannot be printed in the current context,
1599 or if the display is disabled. */
1602 do_one_display (struct display *d)
1604 int within_current_scope;
1606 if (d->enabled_p == 0)
1611 volatile struct gdb_exception ex;
1612 TRY_CATCH (ex, RETURN_MASK_ALL)
1614 innermost_block = NULL;
1615 d->exp = parse_expression (d->exp_string);
1616 d->block = innermost_block;
1620 /* Can't re-parse the expression. Disable this display item. */
1622 warning (_("Unable to display \"%s\": %s"),
1623 d->exp_string, ex.message);
1630 if (d->pspace == current_program_space)
1631 within_current_scope = contained_in (get_selected_block (0), d->block);
1633 within_current_scope = 0;
1636 within_current_scope = 1;
1637 if (!within_current_scope)
1640 current_display_number = d->number;
1642 annotate_display_begin ();
1643 printf_filtered ("%d", d->number);
1644 annotate_display_number_end ();
1645 printf_filtered (": ");
1651 annotate_display_format ();
1653 printf_filtered ("x/");
1654 if (d->format.count != 1)
1655 printf_filtered ("%d", d->format.count);
1656 printf_filtered ("%c", d->format.format);
1657 if (d->format.format != 'i' && d->format.format != 's')
1658 printf_filtered ("%c", d->format.size);
1659 printf_filtered (" ");
1661 annotate_display_expression ();
1663 puts_filtered (d->exp_string);
1664 annotate_display_expression_end ();
1666 if (d->format.count != 1 || d->format.format == 'i')
1667 printf_filtered ("\n");
1669 printf_filtered (" ");
1671 val = evaluate_expression (d->exp);
1672 addr = value_as_address (val);
1673 if (d->format.format == 'i')
1674 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
1676 annotate_display_value ();
1678 do_examine (d->format, d->exp->gdbarch, addr);
1682 struct value_print_options opts;
1684 annotate_display_format ();
1686 if (d->format.format)
1687 printf_filtered ("/%c ", d->format.format);
1689 annotate_display_expression ();
1691 puts_filtered (d->exp_string);
1692 annotate_display_expression_end ();
1694 printf_filtered (" = ");
1696 annotate_display_expression ();
1698 get_formatted_print_options (&opts, d->format.format);
1699 opts.raw = d->format.raw;
1700 print_formatted (evaluate_expression (d->exp),
1701 d->format.size, &opts, gdb_stdout);
1702 printf_filtered ("\n");
1705 annotate_display_end ();
1707 gdb_flush (gdb_stdout);
1708 current_display_number = -1;
1711 /* Display all of the values on the auto-display chain which can be
1712 evaluated in the current scope. */
1719 for (d = display_chain; d; d = d->next)
1723 /* Delete the auto-display which we were in the process of displaying.
1724 This is done when there is an error or a signal. */
1727 disable_display (int num)
1731 for (d = display_chain; d; d = d->next)
1732 if (d->number == num)
1737 printf_unfiltered (_("No display number %d.\n"), num);
1741 disable_current_display (void)
1743 if (current_display_number >= 0)
1745 disable_display (current_display_number);
1746 fprintf_unfiltered (gdb_stderr, _("\
1747 Disabling display %d to avoid infinite recursion.\n"),
1748 current_display_number);
1750 current_display_number = -1;
1754 display_info (char *ignore, int from_tty)
1759 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1761 printf_filtered (_("Auto-display expressions now in effect:\n\
1762 Num Enb Expression\n"));
1764 for (d = display_chain; d; d = d->next)
1766 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1768 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1770 else if (d->format.format)
1771 printf_filtered ("/%c ", d->format.format);
1772 puts_filtered (d->exp_string);
1773 if (d->block && !contained_in (get_selected_block (0), d->block))
1774 printf_filtered (_(" (cannot be evaluated in the current context)"));
1775 printf_filtered ("\n");
1776 gdb_flush (gdb_stdout);
1781 enable_display (char *args, int from_tty)
1790 for (d = display_chain; d; d = d->next)
1797 while (*p1 >= '0' && *p1 <= '9')
1799 if (*p1 && *p1 != ' ' && *p1 != '\t')
1800 error (_("Arguments must be display numbers."));
1804 for (d = display_chain; d; d = d->next)
1805 if (d->number == num)
1810 printf_unfiltered (_("No display number %d.\n"), num);
1813 while (*p == ' ' || *p == '\t')
1819 disable_display_command (char *args, int from_tty)
1827 for (d = display_chain; d; d = d->next)
1834 while (*p1 >= '0' && *p1 <= '9')
1836 if (*p1 && *p1 != ' ' && *p1 != '\t')
1837 error (_("Arguments must be display numbers."));
1839 disable_display (atoi (p));
1842 while (*p == ' ' || *p == '\t')
1847 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1848 is unloaded), otherwise return 0. */
1851 display_uses_solib_p (const struct display *d,
1852 const struct so_list *solib)
1855 struct expression *const exp = d->exp;
1856 const union exp_element *const elts = exp->elts;
1858 if (d->block != NULL
1859 && d->pspace == solib->pspace
1860 && solib_contains_address_p (solib, d->block->startaddr))
1863 for (endpos = exp->nelts; endpos > 0; )
1865 int i, args, oplen = 0;
1867 exp->language_defn->la_exp_desc->operator_length (exp, endpos,
1869 gdb_assert (oplen > 0);
1872 if (elts[i].opcode == OP_VAR_VALUE)
1874 const struct block *const block = elts[i + 1].block;
1875 const struct symbol *const symbol = elts[i + 2].symbol;
1876 const struct obj_section *const section =
1877 SYMBOL_OBJ_SECTION (symbol);
1880 && solib_contains_address_p (solib,
1884 if (section && section->objfile == solib->objfile)
1893 /* display_chain items point to blocks and expressions. Some expressions in
1894 turn may point to symbols.
1895 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1896 obstack_free'd when a shared library is unloaded.
1897 Clear pointers that are about to become dangling.
1898 Both .exp and .block fields will be restored next time we need to display
1899 an item by re-parsing .exp_string field in the new execution context. */
1902 clear_dangling_display_expressions (struct so_list *solib)
1905 struct objfile *objfile = NULL;
1907 for (d = display_chain; d; d = d->next)
1909 if (d->exp && display_uses_solib_p (d, solib))
1919 /* Print the value in stack frame FRAME of a variable specified by a
1920 struct symbol. NAME is the name to print; if NULL then VAR's print
1921 name will be used. STREAM is the ui_file on which to print the
1922 value. INDENT specifies the number of indent levels to print
1923 before printing the variable name. */
1926 print_variable_and_value (const char *name, struct symbol *var,
1927 struct frame_info *frame,
1928 struct ui_file *stream, int indent)
1931 struct value_print_options opts;
1934 name = SYMBOL_PRINT_NAME (var);
1936 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1938 val = read_var_value (var, frame);
1939 get_user_print_options (&opts);
1940 common_val_print (val, stream, indent, &opts, current_language);
1941 fprintf_filtered (stream, "\n");
1945 printf_command (char *arg, int from_tty)
1949 char *string = NULL;
1950 struct value **val_args;
1952 char *current_substring;
1954 int allocated_args = 20;
1955 struct cleanup *old_cleanups;
1957 val_args = xmalloc (allocated_args * sizeof (struct value *));
1958 old_cleanups = make_cleanup (free_current_contents, &val_args);
1961 error_no_arg (_("format-control string and values to print"));
1963 /* Skip white space before format string */
1964 while (*s == ' ' || *s == '\t')
1967 /* A format string should follow, enveloped in double quotes. */
1969 error (_("Bad format string, missing '\"'."));
1971 /* Parse the format-control string and copy it into the string STRING,
1972 processing some kinds of escape sequence. */
1974 f = string = (char *) alloca (strlen (s) + 1);
1982 error (_("Bad format string, non-terminated '\"'."));
2015 /* ??? TODO: handle other escape sequences */
2016 error (_("Unrecognized escape character \\%c in format string."),
2026 /* Skip over " and following space and comma. */
2029 while (*s == ' ' || *s == '\t')
2032 if (*s != ',' && *s != 0)
2033 error (_("Invalid argument syntax"));
2037 while (*s == ' ' || *s == '\t')
2040 /* Need extra space for the '\0's. Doubling the size is sufficient. */
2041 substrings = alloca (strlen (string) * 2);
2042 current_substring = substrings;
2045 /* Now scan the string for %-specs and see what kinds of args they want.
2046 argclass[I] classifies the %-specs so we can give printf_filtered
2047 something of the right size. */
2051 int_arg, long_arg, long_long_arg, ptr_arg,
2052 string_arg, wide_string_arg, wide_char_arg,
2053 double_arg, long_double_arg, decfloat_arg
2055 enum argclass *argclass;
2056 enum argclass this_argclass;
2061 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
2068 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
2069 int seen_space = 0, seen_plus = 0;
2070 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
2071 int seen_big_d = 0, seen_double_big_d = 0;
2074 /* Check the validity of the format specifier, and work
2075 out what argument it expects. We only accept C89
2076 format strings, with the exception of long long (which
2077 we autoconf for). */
2079 /* Skip over "%%". */
2086 /* The first part of a format specifier is a set of flag
2088 while (strchr ("0-+ #", *f))
2101 /* The next part of a format specifier is a width. */
2102 while (strchr ("0123456789", *f))
2105 /* The next part of a format specifier is a precision. */
2110 while (strchr ("0123456789", *f))
2114 /* The next part of a format specifier is a length modifier. */
2135 /* Decimal32 modifier. */
2141 /* Decimal64 and Decimal128 modifiers. */
2146 /* Check for a Decimal128. */
2150 seen_double_big_d = 1;
2166 if (seen_space || seen_plus)
2173 this_argclass = int_arg;
2174 else if (lcount == 1)
2175 this_argclass = long_arg;
2177 this_argclass = long_long_arg;
2184 this_argclass = lcount == 0 ? int_arg : wide_char_arg;
2185 if (lcount > 1 || seen_h || seen_big_l)
2187 if (seen_prec || seen_zero || seen_space || seen_plus)
2192 this_argclass = ptr_arg;
2193 if (lcount || seen_h || seen_big_l)
2195 if (seen_prec || seen_zero || seen_space || seen_plus)
2200 this_argclass = lcount == 0 ? string_arg : wide_string_arg;
2201 if (lcount > 1 || seen_h || seen_big_l)
2203 if (seen_zero || seen_space || seen_plus)
2212 if (seen_big_h || seen_big_d || seen_double_big_d)
2213 this_argclass = decfloat_arg;
2214 else if (seen_big_l)
2215 this_argclass = long_double_arg;
2217 this_argclass = double_arg;
2219 if (lcount || seen_h)
2224 error (_("`*' not supported for precision or width in printf"));
2227 error (_("Format specifier `n' not supported in printf"));
2230 error (_("Incomplete format specifier at end of format string"));
2233 error (_("Unrecognized format specifier '%c' in printf"), *f);
2237 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2242 if (lcount > 1 && USE_PRINTF_I64)
2244 /* Windows' printf does support long long, but not the usual way.
2245 Convert %lld to %I64d. */
2246 int length_before_ll = f - last_arg - 1 - lcount;
2247 strncpy (current_substring, last_arg, length_before_ll);
2248 strcpy (current_substring + length_before_ll, "I64");
2249 current_substring[length_before_ll + 3] =
2250 last_arg[length_before_ll + lcount];
2251 current_substring += length_before_ll + 4;
2253 else if (this_argclass == wide_string_arg
2254 || this_argclass == wide_char_arg)
2256 /* Convert %ls or %lc to %s. */
2257 int length_before_ls = f - last_arg - 2;
2258 strncpy (current_substring, last_arg, length_before_ls);
2259 strcpy (current_substring + length_before_ls, "s");
2260 current_substring += length_before_ls + 2;
2264 strncpy (current_substring, last_arg, f - last_arg);
2265 current_substring += f - last_arg;
2267 *current_substring++ = '\0';
2269 argclass[nargs_wanted++] = this_argclass;
2272 /* Now, parse all arguments and evaluate them.
2273 Store the VALUEs in VAL_ARGS. */
2278 if (nargs == allocated_args)
2279 val_args = (struct value **) xrealloc ((char *) val_args,
2280 (allocated_args *= 2)
2281 * sizeof (struct value *));
2283 val_args[nargs] = parse_to_comma_and_eval (&s1);
2291 if (nargs != nargs_wanted)
2292 error (_("Wrong number of arguments for specified format-string"));
2294 /* Now actually print them. */
2295 current_substring = substrings;
2296 for (i = 0; i < nargs; i++)
2298 switch (argclass[i])
2305 tem = value_as_address (val_args[i]);
2307 /* This is a %s argument. Find the length of the string. */
2312 read_memory (tem + j, &c, 1);
2317 /* Copy the string contents into a string inside GDB. */
2318 str = (gdb_byte *) alloca (j + 1);
2320 read_memory (tem, str, j);
2323 printf_filtered (current_substring, (char *) str);
2326 case wide_string_arg:
2331 struct gdbarch *gdbarch
2332 = get_type_arch (value_type (val_args[i]));
2333 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2334 struct type *wctype = lookup_typename (current_language, gdbarch,
2335 "wchar_t", NULL, 0);
2336 int wcwidth = TYPE_LENGTH (wctype);
2337 gdb_byte *buf = alloca (wcwidth);
2338 struct obstack output;
2339 struct cleanup *inner_cleanup;
2341 tem = value_as_address (val_args[i]);
2343 /* This is a %s argument. Find the length of the string. */
2344 for (j = 0;; j += wcwidth)
2347 read_memory (tem + j, buf, wcwidth);
2348 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2352 /* Copy the string contents into a string inside GDB. */
2353 str = (gdb_byte *) alloca (j + wcwidth);
2355 read_memory (tem, str, j);
2356 memset (&str[j], 0, wcwidth);
2358 obstack_init (&output);
2359 inner_cleanup = make_cleanup_obstack_free (&output);
2361 convert_between_encodings (target_wide_charset (byte_order),
2364 &output, translit_char);
2365 obstack_grow_str0 (&output, "");
2367 printf_filtered (current_substring, obstack_base (&output));
2368 do_cleanups (inner_cleanup);
2373 struct gdbarch *gdbarch
2374 = get_type_arch (value_type (val_args[i]));
2375 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2376 struct type *wctype = lookup_typename (current_language, gdbarch,
2377 "wchar_t", NULL, 0);
2378 struct type *valtype;
2379 struct obstack output;
2380 struct cleanup *inner_cleanup;
2381 const gdb_byte *bytes;
2383 valtype = value_type (val_args[i]);
2384 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2385 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2386 error (_("expected wchar_t argument for %%lc"));
2388 bytes = value_contents (val_args[i]);
2390 obstack_init (&output);
2391 inner_cleanup = make_cleanup_obstack_free (&output);
2393 convert_between_encodings (target_wide_charset (byte_order),
2395 bytes, TYPE_LENGTH (valtype),
2396 TYPE_LENGTH (valtype),
2397 &output, translit_char);
2398 obstack_grow_str0 (&output, "");
2400 printf_filtered (current_substring, obstack_base (&output));
2401 do_cleanups (inner_cleanup);
2406 struct type *type = value_type (val_args[i]);
2410 /* If format string wants a float, unchecked-convert the value
2411 to floating point of the same size. */
2412 type = float_type_from_length (type);
2413 val = unpack_double (type, value_contents (val_args[i]), &inv);
2415 error (_("Invalid floating value found in program."));
2417 printf_filtered (current_substring, (double) val);
2420 case long_double_arg:
2421 #ifdef HAVE_LONG_DOUBLE
2423 struct type *type = value_type (val_args[i]);
2427 /* If format string wants a float, unchecked-convert the value
2428 to floating point of the same size. */
2429 type = float_type_from_length (type);
2430 val = unpack_double (type, value_contents (val_args[i]), &inv);
2432 error (_("Invalid floating value found in program."));
2434 printf_filtered (current_substring, (long double) val);
2438 error (_("long double not supported in printf"));
2441 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2443 long long val = value_as_long (val_args[i]);
2444 printf_filtered (current_substring, val);
2448 error (_("long long not supported in printf"));
2452 int val = value_as_long (val_args[i]);
2453 printf_filtered (current_substring, val);
2458 long val = value_as_long (val_args[i]);
2459 printf_filtered (current_substring, val);
2463 /* Handles decimal floating values. */
2466 const gdb_byte *param_ptr = value_contents (val_args[i]);
2467 #if defined (PRINTF_HAS_DECFLOAT)
2468 /* If we have native support for Decimal floating
2469 printing, handle it here. */
2470 printf_filtered (current_substring, param_ptr);
2473 /* As a workaround until vasprintf has native support for DFP
2474 we convert the DFP values to string and print them using
2475 the %s format specifier. */
2478 int nnull_chars = 0;
2480 /* Parameter data. */
2481 struct type *param_type = value_type (val_args[i]);
2482 unsigned int param_len = TYPE_LENGTH (param_type);
2483 struct gdbarch *gdbarch = get_type_arch (param_type);
2484 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2486 /* DFP output data. */
2487 struct value *dfp_value = NULL;
2491 struct type *dfp_type = NULL;
2492 char decstr[MAX_DECIMAL_STRING];
2494 /* Points to the end of the string so that we can go back
2495 and check for DFP length modifiers. */
2496 eos = current_substring + strlen (current_substring);
2498 /* Look for the float/double format specifier. */
2499 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2500 && *eos != 'g' && *eos != 'G')
2505 /* Search for the '%' char and extract the size and type of
2506 the output decimal value based on its modifiers
2507 (%Hf, %Df, %DDf). */
2508 while (*--sos != '%')
2513 dfp_type = builtin_type (gdbarch)->builtin_decfloat;
2515 else if (*sos == 'D' && *(sos - 1) == 'D')
2518 dfp_type = builtin_type (gdbarch)->builtin_declong;
2524 dfp_type = builtin_type (gdbarch)->builtin_decdouble;
2528 /* Replace %Hf, %Df and %DDf with %s's. */
2531 /* Go through the whole format string and pull the correct
2532 number of chars back to compensate for the change in the
2533 format specifier. */
2534 while (nnull_chars < nargs - i)
2542 /* Conversion between different DFP types. */
2543 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2544 decimal_convert (param_ptr, param_len, byte_order,
2545 dec, dfp_len, byte_order);
2547 /* If this is a non-trivial conversion, just output 0.
2548 A correct converted value can be displayed by explicitly
2549 casting to a DFP type. */
2550 decimal_from_string (dec, dfp_len, byte_order, "0");
2552 dfp_value = value_from_decfloat (dfp_type, dec);
2554 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2556 decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
2558 /* Print the DFP value. */
2559 printf_filtered (current_substring, decstr);
2567 /* We avoid the host's %p because pointers are too
2568 likely to be the wrong size. The only interesting
2569 modifier for %p is a width; extract that, and then
2570 handle %p as glibc would: %#x or a literal "(nil)". */
2572 char *p, *fmt, *fmt_p;
2573 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2574 long long val = value_as_long (val_args[i]);
2576 long val = value_as_long (val_args[i]);
2579 fmt = alloca (strlen (current_substring) + 5);
2581 /* Copy up to the leading %. */
2582 p = current_substring;
2586 int is_percent = (*p == '%');
2600 /* Copy any width. */
2601 while (*p >= '0' && *p < '9')
2604 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2607 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2613 printf_filtered (fmt, val);
2619 printf_filtered (fmt, "(nil)");
2625 internal_error (__FILE__, __LINE__,
2626 _("failed internal consistency check"));
2628 /* Skip to the next substring. */
2629 current_substring += strlen (current_substring) + 1;
2631 /* Print the portion of the format string after the last argument. */
2632 puts_filtered (last_arg);
2634 do_cleanups (old_cleanups);
2638 _initialize_printcmd (void)
2640 struct cmd_list_element *c;
2642 current_display_number = -1;
2644 observer_attach_solib_unloaded (clear_dangling_display_expressions);
2646 add_info ("address", address_info,
2647 _("Describe where symbol SYM is stored."));
2649 add_info ("symbol", sym_info, _("\
2650 Describe what symbol is at location ADDR.\n\
2651 Only for symbols with fixed locations (global or static scope)."));
2653 add_com ("x", class_vars, x_command, _("\
2654 Examine memory: x/FMT ADDRESS.\n\
2655 ADDRESS is an expression for the memory address to examine.\n\
2656 FMT is a repeat count followed by a format letter and a size letter.\n\
2657 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2658 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2659 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2660 The specified number of objects of the specified size are printed\n\
2661 according to the format.\n\n\
2662 Defaults for format and size letters are those previously used.\n\
2663 Default count is 1. Default address is following last thing printed\n\
2664 with this command or \"print\"."));
2667 add_com ("whereis", class_vars, whereis_command,
2668 _("Print line number and file of definition of variable."));
2671 add_info ("display", display_info, _("\
2672 Expressions to display when program stops, with code numbers."));
2674 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2675 Cancel some expressions to be displayed when program stops.\n\
2676 Arguments are the code numbers of the expressions to stop displaying.\n\
2677 No argument means cancel all automatic-display expressions.\n\
2678 \"delete display\" has the same effect as this command.\n\
2679 Do \"info display\" to see current list of code numbers."),
2682 add_com ("display", class_vars, display_command, _("\
2683 Print value of expression EXP each time the program stops.\n\
2684 /FMT may be used before EXP as in the \"print\" command.\n\
2685 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2686 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2687 and examining is done as in the \"x\" command.\n\n\
2688 With no argument, display all currently requested auto-display expressions.\n\
2689 Use \"undisplay\" to cancel display requests previously made."));
2691 add_cmd ("display", class_vars, enable_display, _("\
2692 Enable some expressions to be displayed when program stops.\n\
2693 Arguments are the code numbers of the expressions to resume displaying.\n\
2694 No argument means enable all automatic-display expressions.\n\
2695 Do \"info display\" to see current list of code numbers."), &enablelist);
2697 add_cmd ("display", class_vars, disable_display_command, _("\
2698 Disable some expressions to be displayed when program stops.\n\
2699 Arguments are the code numbers of the expressions to stop displaying.\n\
2700 No argument means disable all automatic-display expressions.\n\
2701 Do \"info display\" to see current list of code numbers."), &disablelist);
2703 add_cmd ("display", class_vars, undisplay_command, _("\
2704 Cancel some expressions to be displayed when program stops.\n\
2705 Arguments are the code numbers of the expressions to stop displaying.\n\
2706 No argument means cancel all automatic-display expressions.\n\
2707 Do \"info display\" to see current list of code numbers."), &deletelist);
2709 add_com ("printf", class_vars, printf_command, _("\
2710 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2711 This is useful for formatted output in user-defined commands."));
2713 add_com ("output", class_vars, output_command, _("\
2714 Like \"print\" but don't put in value history and don't print newline.\n\
2715 This is useful in user-defined commands."));
2717 add_prefix_cmd ("set", class_vars, set_command, _("\
2718 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2719 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2720 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2721 with $), a register (a few standard names starting with $), or an actual\n\
2722 variable in the program being debugged. EXP is any valid expression.\n\
2723 Use \"set variable\" for variables with names identical to set subcommands.\n\
2725 With a subcommand, this command modifies parts of the gdb environment.\n\
2726 You can see these environment settings with the \"show\" command."),
2727 &setlist, "set ", 1, &cmdlist);
2729 add_com ("assign", class_vars, set_command, _("\
2730 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2731 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2732 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2733 with $), a register (a few standard names starting with $), or an actual\n\
2734 variable in the program being debugged. EXP is any valid expression.\n\
2735 Use \"set variable\" for variables with names identical to set subcommands.\n\
2736 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2737 You can see these environment settings with the \"show\" command."));
2739 /* "call" is the same as "set", but handy for dbx users to call fns. */
2740 c = add_com ("call", class_vars, call_command, _("\
2741 Call a function in the program.\n\
2742 The argument is the function name and arguments, in the notation of the\n\
2743 current working language. The result is printed and saved in the value\n\
2744 history, if it is not void."));
2745 set_cmd_completer (c, expression_completer);
2747 add_cmd ("variable", class_vars, set_command, _("\
2748 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2749 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2750 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2751 with $), a register (a few standard names starting with $), or an actual\n\
2752 variable in the program being debugged. EXP is any valid expression.\n\
2753 This may usually be abbreviated to simply \"set\"."),
2756 c = add_com ("print", class_vars, print_command, _("\
2757 Print value of expression EXP.\n\
2758 Variables accessible are those of the lexical environment of the selected\n\
2759 stack frame, plus all those whose scope is global or an entire file.\n\
2761 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2762 $$NUM refers to NUM'th value back from the last one.\n\
2763 Names starting with $ refer to registers (with the values they would have\n\
2764 if the program were to return to the stack frame now selected, restoring\n\
2765 all registers saved by frames farther in) or else to debugger\n\
2766 \"convenience\" variables (any such name not a known register).\n\
2767 Use assignment expressions to give values to convenience variables.\n\
2769 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2770 @ is a binary operator for treating consecutive data objects\n\
2771 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2772 element is FOO, whose second element is stored in the space following\n\
2773 where FOO is stored, etc. FOO must be an expression whose value\n\
2774 resides in memory.\n\
2776 EXP may be preceded with /FMT, where FMT is a format letter\n\
2777 but no count or size letter (see \"x\" command)."));
2778 set_cmd_completer (c, expression_completer);
2779 add_com_alias ("p", "print", class_vars, 1);
2781 c = add_com ("inspect", class_vars, inspect_command, _("\
2782 Same as \"print\" command, except that if you are running in the epoch\n\
2783 environment, the value is printed in its own window."));
2784 set_cmd_completer (c, expression_completer);
2786 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2787 &max_symbolic_offset, _("\
2788 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2789 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2791 show_max_symbolic_offset,
2792 &setprintlist, &showprintlist);
2793 add_setshow_boolean_cmd ("symbol-filename", no_class,
2794 &print_symbol_filename, _("\
2795 Set printing of source filename and line number with <symbol>."), _("\
2796 Show printing of source filename and line number with <symbol>."), NULL,
2798 show_print_symbol_filename,
2799 &setprintlist, &showprintlist);