1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009, 2010 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
29 #include "expression.h"
33 #include "breakpoint.h"
37 #include "symfile.h" /* for overlay functions */
38 #include "objfiles.h" /* ditto */
39 #include "completer.h" /* for completion functions */
41 #include "gdb_assert.h"
46 #include "exceptions.h"
49 #include "parser-defs.h"
51 #include "arch-utils.h"
54 #include "tui/tui.h" /* For tui_active et.al. */
57 #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
58 # define USE_PRINTF_I64 1
59 # define PRINTF_HAS_LONG_LONG
61 # define USE_PRINTF_I64 0
64 extern int asm_demangle
; /* Whether to demangle syms in asm 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 /* Display strings with byte size chars unless explicitly specified. */
268 /* The default is the size most recently specified. */
275 /* Print value VAL on stream according to OPTIONS.
276 Do not end with a newline.
277 SIZE is the letter for the size of datum being printed.
278 This is used to pad hex numbers so they line up. SIZE is 0
279 for print / output and set for examine. */
282 print_formatted (struct value
*val
, int size
,
283 const struct value_print_options
*options
,
284 struct ui_file
*stream
)
286 struct type
*type
= check_typedef (value_type (val
));
287 int len
= TYPE_LENGTH (type
);
289 if (VALUE_LVAL (val
) == lval_memory
)
290 next_address
= value_address (val
) + len
;
294 switch (options
->format
)
298 struct type
*elttype
= value_type (val
);
300 next_address
= (value_address (val
)
301 + val_print_string (elttype
,
302 value_address (val
), -1,
303 stream
, options
) * len
);
308 /* We often wrap here if there are long symbolic names. */
310 next_address
= (value_address (val
)
311 + gdb_print_insn (get_type_arch (type
),
312 value_address (val
), stream
,
313 &branch_delay_insns
));
318 if (options
->format
== 0 || options
->format
== 's'
319 || TYPE_CODE (type
) == TYPE_CODE_REF
320 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
321 || TYPE_CODE (type
) == TYPE_CODE_STRING
322 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
323 || TYPE_CODE (type
) == TYPE_CODE_UNION
324 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
325 value_print (val
, stream
, options
);
327 /* User specified format, so don't look to the the type to
328 tell us what to do. */
329 print_scalar_formatted (value_contents (val
), type
,
330 options
, size
, stream
);
333 /* Return builtin floating point type of same length as TYPE.
334 If no such type is found, return TYPE itself. */
336 float_type_from_length (struct type
*type
)
338 struct gdbarch
*gdbarch
= get_type_arch (type
);
339 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
340 unsigned int len
= TYPE_LENGTH (type
);
342 if (len
== TYPE_LENGTH (builtin
->builtin_float
))
343 type
= builtin
->builtin_float
;
344 else if (len
== TYPE_LENGTH (builtin
->builtin_double
))
345 type
= builtin
->builtin_double
;
346 else if (len
== TYPE_LENGTH (builtin
->builtin_long_double
))
347 type
= builtin
->builtin_long_double
;
352 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
353 according to OPTIONS and SIZE on STREAM.
354 Formats s and i are not supported at this level.
356 This is how the elements of an array or structure are printed
360 print_scalar_formatted (const void *valaddr
, struct type
*type
,
361 const struct value_print_options
*options
,
362 int size
, struct ui_file
*stream
)
364 struct gdbarch
*gdbarch
= get_type_arch (type
);
365 LONGEST val_long
= 0;
366 unsigned int len
= TYPE_LENGTH (type
);
367 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
369 /* If we get here with a string format, try again without it. Go
370 all the way back to the language printers, which may call us
372 if (options
->format
== 's')
374 struct value_print_options opts
= *options
;
377 val_print (type
, valaddr
, 0, 0, stream
, 0, &opts
,
382 if (len
> sizeof(LONGEST
) &&
383 (TYPE_CODE (type
) == TYPE_CODE_INT
384 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
386 switch (options
->format
)
389 print_octal_chars (stream
, valaddr
, len
, byte_order
);
393 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
396 print_binary_chars (stream
, valaddr
, len
, byte_order
);
399 print_hex_chars (stream
, valaddr
, len
, byte_order
);
402 print_char_chars (stream
, type
, valaddr
, len
, byte_order
);
409 if (options
->format
!= 'f')
410 val_long
= unpack_long (type
, valaddr
);
412 /* If the value is a pointer, and pointers and addresses are not the
413 same, then at this point, the value's length (in target bytes) is
414 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
415 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
416 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
418 /* If we are printing it as unsigned, truncate it in case it is actually
419 a negative signed value (e.g. "print/u (short)-1" should print 65535
420 (if shorts are 16 bits) instead of 4294967295). */
421 if (options
->format
!= 'd' || TYPE_UNSIGNED (type
))
423 if (len
< sizeof (LONGEST
))
424 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
427 switch (options
->format
)
432 /* No size specified, like in print. Print varying # of digits. */
433 print_longest (stream
, 'x', 1, val_long
);
442 print_longest (stream
, size
, 1, val_long
);
445 error (_("Undefined output size \"%c\"."), size
);
450 print_longest (stream
, 'd', 1, val_long
);
454 print_longest (stream
, 'u', 0, val_long
);
459 print_longest (stream
, 'o', 1, val_long
);
461 fprintf_filtered (stream
, "0");
466 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
468 print_address (gdbarch
, addr
, stream
);
474 struct value_print_options opts
= *options
;
477 if (TYPE_UNSIGNED (type
))
478 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
480 type
= builtin_type (gdbarch
)->builtin_true_char
;
482 value_print (value_from_longest (type
, val_long
), stream
, &opts
);
487 type
= float_type_from_length (type
);
488 print_floating (valaddr
, type
, stream
);
492 internal_error (__FILE__
, __LINE__
,
493 _("failed internal consistency check"));
496 /* Binary; 't' stands for "two". */
498 char bits
[8 * (sizeof val_long
) + 1];
499 char buf
[8 * (sizeof val_long
) + 32];
504 width
= 8 * (sizeof val_long
);
521 error (_("Undefined output size \"%c\"."), size
);
527 bits
[width
] = (val_long
& 1) ? '1' : '0';
532 while (*cp
&& *cp
== '0')
538 fputs_filtered (buf
, stream
);
543 error (_("Undefined output format \"%c\"."), options
->format
);
547 /* Specify default address for `x' command.
548 The `info lines' command uses this. */
551 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
553 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
555 next_gdbarch
= gdbarch
;
558 /* Make address available to the user as $_. */
559 set_internalvar (lookup_internalvar ("_"),
560 value_from_pointer (ptr_type
, addr
));
563 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
564 after LEADIN. Print nothing if no symbolic name is found nearby.
565 Optionally also print source file and line number, if available.
566 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
567 or to interpret it as a possible C++ name and convert it back to source
568 form. However note that DO_DEMANGLE can be overridden by the specific
569 settings of the demangle and asm_demangle variables. */
572 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
573 struct ui_file
*stream
,
574 int do_demangle
, char *leadin
)
577 char *filename
= NULL
;
582 /* Throw away both name and filename. */
583 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
584 make_cleanup (free_current_contents
, &filename
);
586 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, &name
, &offset
,
587 &filename
, &line
, &unmapped
))
589 do_cleanups (cleanup_chain
);
593 fputs_filtered (leadin
, stream
);
595 fputs_filtered ("<*", stream
);
597 fputs_filtered ("<", stream
);
598 fputs_filtered (name
, stream
);
600 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
602 /* Append source filename and line number if desired. Give specific
603 line # of this addr, if we have it; else line # of the nearest symbol. */
604 if (print_symbol_filename
&& filename
!= NULL
)
607 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
609 fprintf_filtered (stream
, " in %s", filename
);
612 fputs_filtered ("*>", stream
);
614 fputs_filtered (">", stream
);
616 do_cleanups (cleanup_chain
);
619 /* Given an address ADDR return all the elements needed to print the
620 address in a symbolic form. NAME can be mangled or not depending
621 on DO_DEMANGLE (and also on the asm_demangle global variable,
622 manipulated via ''set print asm-demangle''). Return 0 in case of
623 success, when all the info in the OUT paramters is valid. Return 1
626 build_address_symbolic (struct gdbarch
*gdbarch
,
627 CORE_ADDR addr
, /* IN */
628 int do_demangle
, /* IN */
629 char **name
, /* OUT */
630 int *offset
, /* OUT */
631 char **filename
, /* OUT */
633 int *unmapped
) /* OUT */
635 struct minimal_symbol
*msymbol
;
636 struct symbol
*symbol
;
637 CORE_ADDR name_location
= 0;
638 struct obj_section
*section
= NULL
;
639 char *name_temp
= "";
641 /* Let's say it is mapped (not unmapped). */
644 /* Determine if the address is in an overlay, and whether it is
646 if (overlay_debugging
)
648 section
= find_pc_overlay (addr
);
649 if (pc_in_unmapped_range (addr
, section
))
652 addr
= overlay_mapped_address (addr
, section
);
656 /* First try to find the address in the symbol table, then
657 in the minsyms. Take the closest one. */
659 /* This is defective in the sense that it only finds text symbols. So
660 really this is kind of pointless--we should make sure that the
661 minimal symbols have everything we need (by changing that we could
662 save some memory, but for many debug format--ELF/DWARF or
663 anything/stabs--it would be inconvenient to eliminate those minimal
665 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
666 symbol
= find_pc_sect_function (addr
, section
);
670 /* If this is a function (i.e. a code address), strip out any
671 non-address bits. For instance, display a pointer to the
672 first instruction of a Thumb function as <function>; the
673 second instruction will be <function+2>, even though the
674 pointer is <function+3>. This matches the ISA behavior. */
675 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
677 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
678 if (do_demangle
|| asm_demangle
)
679 name_temp
= SYMBOL_PRINT_NAME (symbol
);
681 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
686 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
688 /* The msymbol is closer to the address than the symbol;
689 use the msymbol instead. */
691 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
692 if (do_demangle
|| asm_demangle
)
693 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
695 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
698 if (symbol
== NULL
&& msymbol
== NULL
)
701 /* If the nearest symbol is too far away, don't print anything symbolic. */
703 /* For when CORE_ADDR is larger than unsigned int, we do math in
704 CORE_ADDR. But when we detect unsigned wraparound in the
705 CORE_ADDR math, we ignore this test and print the offset,
706 because addr+max_symbolic_offset has wrapped through the end
707 of the address space back to the beginning, giving bogus comparison. */
708 if (addr
> name_location
+ max_symbolic_offset
709 && name_location
+ max_symbolic_offset
> name_location
)
712 *offset
= addr
- name_location
;
714 *name
= xstrdup (name_temp
);
716 if (print_symbol_filename
)
718 struct symtab_and_line sal
;
720 sal
= find_pc_sect_line (addr
, section
, 0);
724 *filename
= xstrdup (sal
.symtab
->filename
);
732 /* Print address ADDR symbolically on STREAM.
733 First print it as a number. Then perhaps print
734 <SYMBOL + OFFSET> after the number. */
737 print_address (struct gdbarch
*gdbarch
,
738 CORE_ADDR addr
, struct ui_file
*stream
)
740 fputs_filtered (paddress (gdbarch
, addr
), stream
);
741 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
744 /* Return a prefix for instruction address:
745 "=> " for current instruction, else " ". */
748 pc_prefix (CORE_ADDR addr
)
750 if (has_stack_frames ())
752 struct frame_info
*frame
;
755 frame
= get_selected_frame (NULL
);
756 pc
= get_frame_pc (frame
);
764 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
765 controls whether to print the symbolic name "raw" or demangled.
766 Global setting "addressprint" controls whether to print hex address
770 print_address_demangle (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
771 struct ui_file
*stream
, int do_demangle
)
773 struct value_print_options opts
;
775 get_user_print_options (&opts
);
778 fprintf_filtered (stream
, "0");
780 else if (opts
.addressprint
)
782 fputs_filtered (paddress (gdbarch
, addr
), stream
);
783 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
787 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
792 /* Examine data at address ADDR in format FMT.
793 Fetch it from memory and print on gdb_stdout. */
796 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
801 struct type
*val_type
= NULL
;
804 struct value_print_options opts
;
809 next_gdbarch
= gdbarch
;
812 /* Instruction format implies fetch single bytes
813 regardless of the specified size.
814 The case of strings is handled in decode_format, only explicit
815 size operator are not changed to 'b'. */
821 /* Pick the appropriate size for an address. */
822 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
824 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
826 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
829 /* Bad value for gdbarch_ptr_bit. */
830 internal_error (__FILE__
, __LINE__
,
831 _("failed internal consistency check"));
835 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
836 else if (size
== 'h')
837 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
838 else if (size
== 'w')
839 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
840 else if (size
== 'g')
841 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
845 struct type
*char_type
= NULL
;
847 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
848 if type is not found. */
850 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
851 else if (size
== 'w')
852 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
854 val_type
= char_type
;
857 if (size
!= '\0' && size
!= 'b')
858 warning (_("Unable to display strings with size '%c', using 'b' \
861 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
870 if (format
== 's' || format
== 'i')
873 get_formatted_print_options (&opts
, format
);
875 /* Print as many objects as specified in COUNT, at most maxelts per line,
876 with the address of the next one at the start of each line. */
882 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
883 print_address (next_gdbarch
, next_address
, gdb_stdout
);
884 printf_filtered (":");
889 printf_filtered ("\t");
890 /* Note that print_formatted sets next_address for the next
892 last_examine_address
= next_address
;
894 if (last_examine_value
)
895 value_free (last_examine_value
);
897 /* The value to be displayed is not fetched greedily.
898 Instead, to avoid the possibility of a fetched value not
899 being used, its retrieval is delayed until the print code
900 uses it. When examining an instruction stream, the
901 disassembler will perform its own memory fetch using just
902 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
903 the disassembler be modified so that LAST_EXAMINE_VALUE
904 is left with the byte sequence from the last complete
905 instruction fetched from memory? */
906 last_examine_value
= value_at_lazy (val_type
, next_address
);
908 if (last_examine_value
)
909 release_value (last_examine_value
);
911 print_formatted (last_examine_value
, size
, &opts
, gdb_stdout
);
913 /* Display any branch delay slots following the final insn. */
914 if (format
== 'i' && count
== 1)
915 count
+= branch_delay_insns
;
917 printf_filtered ("\n");
918 gdb_flush (gdb_stdout
);
923 validate_format (struct format_data fmt
, char *cmdname
)
926 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
928 error (_("Item count other than 1 is meaningless in \"%s\" command."),
930 if (fmt
.format
== 'i')
931 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
932 fmt
.format
, cmdname
);
935 /* Evaluate string EXP as an expression in the current language and
936 print the resulting value. EXP may contain a format specifier as the
937 first argument ("/x myvar" for example, to print myvar in hex). */
940 print_command_1 (char *exp
, int inspect
, int voidprint
)
942 struct expression
*expr
;
943 struct cleanup
*old_chain
= 0;
946 struct format_data fmt
;
949 if (exp
&& *exp
== '/')
952 fmt
= decode_format (&exp
, last_format
, 0);
953 validate_format (fmt
, "print");
954 last_format
= format
= fmt
.format
;
966 expr
= parse_expression (exp
);
967 old_chain
= make_cleanup (free_current_contents
, &expr
);
969 val
= evaluate_expression (expr
);
972 val
= access_value_history (0);
974 if (voidprint
|| (val
&& value_type (val
) &&
975 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
977 struct value_print_options opts
;
978 int histindex
= record_latest_value (val
);
981 annotate_value_history_begin (histindex
, value_type (val
));
983 annotate_value_begin (value_type (val
));
986 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
988 else if (histindex
>= 0)
989 printf_filtered ("$%d = ", histindex
);
992 annotate_value_history_value ();
994 get_formatted_print_options (&opts
, format
);
995 opts
.inspect_it
= inspect
;
998 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
999 printf_filtered ("\n");
1002 annotate_value_history_end ();
1004 annotate_value_end ();
1007 printf_unfiltered ("\") )\030");
1011 do_cleanups (old_chain
);
1015 print_command (char *exp
, int from_tty
)
1017 print_command_1 (exp
, 0, 1);
1020 /* Same as print, except in epoch, it gets its own window. */
1022 inspect_command (char *exp
, int from_tty
)
1024 extern int epoch_interface
;
1026 print_command_1 (exp
, epoch_interface
, 1);
1029 /* Same as print, except it doesn't print void results. */
1031 call_command (char *exp
, int from_tty
)
1033 print_command_1 (exp
, 0, 0);
1037 output_command (char *exp
, int from_tty
)
1039 struct expression
*expr
;
1040 struct cleanup
*old_chain
;
1043 struct format_data fmt
;
1044 struct value_print_options opts
;
1049 if (exp
&& *exp
== '/')
1052 fmt
= decode_format (&exp
, 0, 0);
1053 validate_format (fmt
, "output");
1054 format
= fmt
.format
;
1057 expr
= parse_expression (exp
);
1058 old_chain
= make_cleanup (free_current_contents
, &expr
);
1060 val
= evaluate_expression (expr
);
1062 annotate_value_begin (value_type (val
));
1064 get_formatted_print_options (&opts
, format
);
1066 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1068 annotate_value_end ();
1071 gdb_flush (gdb_stdout
);
1073 do_cleanups (old_chain
);
1077 set_command (char *exp
, int from_tty
)
1079 struct expression
*expr
= parse_expression (exp
);
1080 struct cleanup
*old_chain
=
1081 make_cleanup (free_current_contents
, &expr
);
1083 evaluate_expression (expr
);
1084 do_cleanups (old_chain
);
1088 sym_info (char *arg
, int from_tty
)
1090 struct minimal_symbol
*msymbol
;
1091 struct objfile
*objfile
;
1092 struct obj_section
*osect
;
1093 CORE_ADDR addr
, sect_addr
;
1095 unsigned int offset
;
1098 error_no_arg (_("address"));
1100 addr
= parse_and_eval_address (arg
);
1101 ALL_OBJSECTIONS (objfile
, osect
)
1103 /* Only process each object file once, even if there's a separate
1105 if (objfile
->separate_debug_objfile_backlink
)
1108 sect_addr
= overlay_mapped_address (addr
, osect
);
1110 if (obj_section_addr (osect
) <= sect_addr
1111 && sect_addr
< obj_section_endaddr (osect
)
1112 && (msymbol
= lookup_minimal_symbol_by_pc_section (sect_addr
, osect
)))
1114 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1116 struct cleanup
*old_chain
;
1119 offset
= sect_addr
- SYMBOL_VALUE_ADDRESS (msymbol
);
1120 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1121 sec_name
= osect
->the_bfd_section
->name
;
1122 msym_name
= SYMBOL_PRINT_NAME (msymbol
);
1124 /* Don't print the offset if it is zero.
1125 We assume there's no need to handle i18n of "sym + offset". */
1127 loc_string
= xstrprintf ("%s + %u", msym_name
, offset
);
1129 loc_string
= xstrprintf ("%s", msym_name
);
1131 /* Use a cleanup to free loc_string in case the user quits
1132 a pagination request inside printf_filtered. */
1133 old_chain
= make_cleanup (xfree
, loc_string
);
1135 gdb_assert (osect
->objfile
&& osect
->objfile
->name
);
1136 obj_name
= osect
->objfile
->name
;
1138 if (MULTI_OBJFILE_P ())
1139 if (pc_in_unmapped_range (addr
, osect
))
1140 if (section_is_overlay (osect
))
1141 printf_filtered (_("%s in load address range of "
1142 "%s overlay section %s of %s\n"),
1143 loc_string
, mapped
, sec_name
, obj_name
);
1145 printf_filtered (_("%s in load address range of "
1146 "section %s of %s\n"),
1147 loc_string
, sec_name
, obj_name
);
1149 if (section_is_overlay (osect
))
1150 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1151 loc_string
, mapped
, sec_name
, obj_name
);
1153 printf_filtered (_("%s in section %s of %s\n"),
1154 loc_string
, sec_name
, obj_name
);
1156 if (pc_in_unmapped_range (addr
, osect
))
1157 if (section_is_overlay (osect
))
1158 printf_filtered (_("%s in load address range of %s overlay "
1160 loc_string
, mapped
, sec_name
);
1162 printf_filtered (_("%s in load address range of section %s\n"),
1163 loc_string
, sec_name
);
1165 if (section_is_overlay (osect
))
1166 printf_filtered (_("%s in %s overlay section %s\n"),
1167 loc_string
, mapped
, sec_name
);
1169 printf_filtered (_("%s in section %s\n"),
1170 loc_string
, sec_name
);
1172 do_cleanups (old_chain
);
1176 printf_filtered (_("No symbol matches %s.\n"), arg
);
1180 address_info (char *exp
, int from_tty
)
1182 struct gdbarch
*gdbarch
;
1185 struct minimal_symbol
*msymbol
;
1187 struct obj_section
*section
;
1188 CORE_ADDR load_addr
, context_pc
= 0;
1189 int is_a_field_of_this
; /* C++: lookup_symbol sets this to nonzero
1190 if exp is a field of `this'. */
1193 error (_("Argument required."));
1195 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1196 &is_a_field_of_this
);
1199 if (is_a_field_of_this
)
1201 printf_filtered ("Symbol \"");
1202 fprintf_symbol_filtered (gdb_stdout
, exp
,
1203 current_language
->la_language
, DMGL_ANSI
);
1204 printf_filtered ("\" is a field of the local class variable ");
1205 if (current_language
->la_language
== language_objc
)
1206 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1208 printf_filtered ("`this'\n");
1212 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1214 if (msymbol
!= NULL
)
1216 gdbarch
= get_objfile_arch (msymbol_objfile (msymbol
));
1217 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1219 printf_filtered ("Symbol \"");
1220 fprintf_symbol_filtered (gdb_stdout
, exp
,
1221 current_language
->la_language
, DMGL_ANSI
);
1222 printf_filtered ("\" is at ");
1223 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1224 printf_filtered (" in a file compiled without debugging");
1225 section
= SYMBOL_OBJ_SECTION (msymbol
);
1226 if (section_is_overlay (section
))
1228 load_addr
= overlay_unmapped_address (load_addr
, section
);
1229 printf_filtered (",\n -- loaded at ");
1230 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1231 printf_filtered (" in overlay section %s",
1232 section
->the_bfd_section
->name
);
1234 printf_filtered (".\n");
1237 error (_("No symbol \"%s\" in current context."), exp
);
1241 printf_filtered ("Symbol \"");
1242 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1243 current_language
->la_language
, DMGL_ANSI
);
1244 printf_filtered ("\" is ");
1245 val
= SYMBOL_VALUE (sym
);
1246 section
= SYMBOL_OBJ_SECTION (sym
);
1247 gdbarch
= get_objfile_arch (SYMBOL_SYMTAB (sym
)->objfile
);
1249 switch (SYMBOL_CLASS (sym
))
1252 case LOC_CONST_BYTES
:
1253 printf_filtered ("constant");
1257 printf_filtered ("a label at address ");
1258 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1259 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1260 if (section_is_overlay (section
))
1262 load_addr
= overlay_unmapped_address (load_addr
, section
);
1263 printf_filtered (",\n -- loaded at ");
1264 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1265 printf_filtered (" in overlay section %s",
1266 section
->the_bfd_section
->name
);
1271 /* FIXME: cagney/2004-01-26: It should be possible to
1272 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1273 Unfortunately DWARF 2 stores the frame-base (instead of the
1274 function) location in a function's symbol. Oops! For the
1275 moment enable this when/where applicable. */
1276 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
, gdb_stdout
);
1280 /* GDBARCH is the architecture associated with the objfile the symbol
1281 is defined in; the target architecture may be different, and may
1282 provide additional registers. However, we do not know the target
1283 architecture at this point. We assume the objfile architecture
1284 will contain all the standard registers that occur in debug info
1286 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1288 if (SYMBOL_IS_ARGUMENT (sym
))
1289 printf_filtered (_("an argument in register %s"),
1290 gdbarch_register_name (gdbarch
, regno
));
1292 printf_filtered (_("a variable in register %s"),
1293 gdbarch_register_name (gdbarch
, regno
));
1297 printf_filtered (_("static storage at address "));
1298 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1299 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1300 if (section_is_overlay (section
))
1302 load_addr
= overlay_unmapped_address (load_addr
, section
);
1303 printf_filtered (_(",\n -- loaded at "));
1304 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1305 printf_filtered (_(" in overlay section %s"),
1306 section
->the_bfd_section
->name
);
1310 case LOC_REGPARM_ADDR
:
1311 /* Note comment at LOC_REGISTER. */
1312 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1313 printf_filtered (_("address of an argument in register %s"),
1314 gdbarch_register_name (gdbarch
, regno
));
1318 printf_filtered (_("an argument at offset %ld"), val
);
1322 printf_filtered (_("a local variable at frame offset %ld"), val
);
1326 printf_filtered (_("a reference argument at offset %ld"), val
);
1330 printf_filtered (_("a typedef"));
1334 printf_filtered (_("a function at address "));
1335 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1336 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1337 if (section_is_overlay (section
))
1339 load_addr
= overlay_unmapped_address (load_addr
, section
);
1340 printf_filtered (_(",\n -- loaded at "));
1341 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1342 printf_filtered (_(" in overlay section %s"),
1343 section
->the_bfd_section
->name
);
1347 case LOC_UNRESOLVED
:
1349 struct minimal_symbol
*msym
;
1351 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1353 printf_filtered ("unresolved");
1356 section
= SYMBOL_OBJ_SECTION (msym
);
1357 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1360 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1361 printf_filtered (_("a thread-local variable at offset %s "
1362 "in the thread-local storage for `%s'"),
1363 paddress (gdbarch
, load_addr
),
1364 section
->objfile
->name
);
1367 printf_filtered (_("static storage at address "));
1368 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1369 if (section_is_overlay (section
))
1371 load_addr
= overlay_unmapped_address (load_addr
, section
);
1372 printf_filtered (_(",\n -- loaded at "));
1373 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1374 printf_filtered (_(" in overlay section %s"),
1375 section
->the_bfd_section
->name
);
1382 case LOC_OPTIMIZED_OUT
:
1383 printf_filtered (_("optimized out"));
1387 printf_filtered (_("of unknown (botched) type"));
1390 printf_filtered (".\n");
1395 x_command (char *exp
, int from_tty
)
1397 struct expression
*expr
;
1398 struct format_data fmt
;
1399 struct cleanup
*old_chain
;
1402 fmt
.format
= last_format
? last_format
: 'x';
1403 fmt
.size
= last_size
;
1407 if (exp
&& *exp
== '/')
1410 fmt
= decode_format (&exp
, last_format
, last_size
);
1413 /* If we have an expression, evaluate it and use it as the address. */
1415 if (exp
!= 0 && *exp
!= 0)
1417 expr
= parse_expression (exp
);
1418 /* Cause expression not to be there any more if this command is
1419 repeated with Newline. But don't clobber a user-defined
1420 command's definition. */
1423 old_chain
= make_cleanup (free_current_contents
, &expr
);
1424 val
= evaluate_expression (expr
);
1425 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1426 val
= coerce_ref (val
);
1427 /* In rvalue contexts, such as this, functions are coerced into
1428 pointers to functions. This makes "x/i main" work. */
1429 if (/* last_format == 'i' && */
1430 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1431 && VALUE_LVAL (val
) == lval_memory
)
1432 next_address
= value_address (val
);
1434 next_address
= value_as_address (val
);
1436 next_gdbarch
= expr
->gdbarch
;
1437 do_cleanups (old_chain
);
1441 error_no_arg (_("starting display address"));
1443 do_examine (fmt
, next_gdbarch
, next_address
);
1445 /* If the examine succeeds, we remember its size and format for next
1446 time. Set last_size to 'b' for strings. */
1447 if (fmt
.format
== 's')
1450 last_size
= fmt
.size
;
1451 last_format
= fmt
.format
;
1453 /* Set a couple of internal variables if appropriate. */
1454 if (last_examine_value
)
1456 /* Make last address examined available to the user as $_. Use
1457 the correct pointer type. */
1458 struct type
*pointer_type
1459 = lookup_pointer_type (value_type (last_examine_value
));
1460 set_internalvar (lookup_internalvar ("_"),
1461 value_from_pointer (pointer_type
,
1462 last_examine_address
));
1464 /* Make contents of last address examined available to the user
1465 as $__. If the last value has not been fetched from memory
1466 then don't fetch it now; instead mark it by voiding the $__
1468 if (value_lazy (last_examine_value
))
1469 clear_internalvar (lookup_internalvar ("__"));
1471 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1476 /* Add an expression to the auto-display chain.
1477 Specify the expression. */
1480 display_command (char *exp
, int from_tty
)
1482 struct format_data fmt
;
1483 struct expression
*expr
;
1484 struct display
*new;
1488 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1490 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1491 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1505 fmt
= decode_format (&exp
, 0, 0);
1506 if (fmt
.size
&& fmt
.format
== 0)
1508 if (fmt
.format
== 'i' || fmt
.format
== 's')
1519 innermost_block
= NULL
;
1520 expr
= parse_expression (exp
);
1522 new = (struct display
*) xmalloc (sizeof (struct display
));
1524 new->exp_string
= xstrdup (exp
);
1526 new->block
= innermost_block
;
1527 new->pspace
= current_program_space
;
1528 new->next
= display_chain
;
1529 new->number
= ++display_number
;
1532 display_chain
= new;
1534 if (from_tty
&& target_has_execution
)
1535 do_one_display (new);
1542 free_display (struct display
*d
)
1544 xfree (d
->exp_string
);
1549 /* Clear out the display_chain. Done when new symtabs are loaded,
1550 since this invalidates the types stored in many expressions. */
1553 clear_displays (void)
1557 while ((d
= display_chain
) != NULL
)
1559 display_chain
= d
->next
;
1564 /* Delete the auto-display number NUM. */
1567 delete_display (int num
)
1569 struct display
*d1
, *d
;
1572 error (_("No display number %d."), num
);
1574 if (display_chain
->number
== num
)
1577 display_chain
= d1
->next
;
1581 for (d
= display_chain
;; d
= d
->next
)
1584 error (_("No display number %d."), num
);
1585 if (d
->next
->number
== num
)
1595 /* Delete some values from the auto-display chain.
1596 Specify the element numbers. */
1599 undisplay_command (char *args
, int from_tty
)
1607 if (query (_("Delete all auto-display expressions? ")))
1616 while (*p1
>= '0' && *p1
<= '9')
1618 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1619 error (_("Arguments must be display numbers."));
1623 delete_display (num
);
1626 while (*p
== ' ' || *p
== '\t')
1632 /* Display a single auto-display.
1633 Do nothing if the display cannot be printed in the current context,
1634 or if the display is disabled. */
1637 do_one_display (struct display
*d
)
1639 int within_current_scope
;
1641 if (d
->enabled_p
== 0)
1644 /* The expression carries the architecture that was used at parse time.
1645 This is a problem if the expression depends on architecture features
1646 (e.g. register numbers), and the current architecture is now different.
1647 For example, a display statement like "display/i $pc" is expected to
1648 display the PC register of the current architecture, not the arch at
1649 the time the display command was given. Therefore, we re-parse the
1650 expression if the current architecture has changed. */
1651 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1660 volatile struct gdb_exception ex
;
1662 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1664 innermost_block
= NULL
;
1665 d
->exp
= parse_expression (d
->exp_string
);
1666 d
->block
= innermost_block
;
1670 /* Can't re-parse the expression. Disable this display item. */
1672 warning (_("Unable to display \"%s\": %s"),
1673 d
->exp_string
, ex
.message
);
1680 if (d
->pspace
== current_program_space
)
1681 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1683 within_current_scope
= 0;
1686 within_current_scope
= 1;
1687 if (!within_current_scope
)
1690 current_display_number
= d
->number
;
1692 annotate_display_begin ();
1693 printf_filtered ("%d", d
->number
);
1694 annotate_display_number_end ();
1695 printf_filtered (": ");
1701 annotate_display_format ();
1703 printf_filtered ("x/");
1704 if (d
->format
.count
!= 1)
1705 printf_filtered ("%d", d
->format
.count
);
1706 printf_filtered ("%c", d
->format
.format
);
1707 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1708 printf_filtered ("%c", d
->format
.size
);
1709 printf_filtered (" ");
1711 annotate_display_expression ();
1713 puts_filtered (d
->exp_string
);
1714 annotate_display_expression_end ();
1716 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1717 printf_filtered ("\n");
1719 printf_filtered (" ");
1721 val
= evaluate_expression (d
->exp
);
1722 addr
= value_as_address (val
);
1723 if (d
->format
.format
== 'i')
1724 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1726 annotate_display_value ();
1728 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1732 struct value_print_options opts
;
1734 annotate_display_format ();
1736 if (d
->format
.format
)
1737 printf_filtered ("/%c ", d
->format
.format
);
1739 annotate_display_expression ();
1741 puts_filtered (d
->exp_string
);
1742 annotate_display_expression_end ();
1744 printf_filtered (" = ");
1746 annotate_display_expression ();
1748 get_formatted_print_options (&opts
, d
->format
.format
);
1749 opts
.raw
= d
->format
.raw
;
1750 print_formatted (evaluate_expression (d
->exp
),
1751 d
->format
.size
, &opts
, gdb_stdout
);
1752 printf_filtered ("\n");
1755 annotate_display_end ();
1757 gdb_flush (gdb_stdout
);
1758 current_display_number
= -1;
1761 /* Display all of the values on the auto-display chain which can be
1762 evaluated in the current scope. */
1769 for (d
= display_chain
; d
; d
= d
->next
)
1773 /* Delete the auto-display which we were in the process of displaying.
1774 This is done when there is an error or a signal. */
1777 disable_display (int num
)
1781 for (d
= display_chain
; d
; d
= d
->next
)
1782 if (d
->number
== num
)
1787 printf_unfiltered (_("No display number %d.\n"), num
);
1791 disable_current_display (void)
1793 if (current_display_number
>= 0)
1795 disable_display (current_display_number
);
1796 fprintf_unfiltered (gdb_stderr
, _("\
1797 Disabling display %d to avoid infinite recursion.\n"),
1798 current_display_number
);
1800 current_display_number
= -1;
1804 display_info (char *ignore
, int from_tty
)
1809 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1811 printf_filtered (_("Auto-display expressions now in effect:\n\
1812 Num Enb Expression\n"));
1814 for (d
= display_chain
; d
; d
= d
->next
)
1816 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1818 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1820 else if (d
->format
.format
)
1821 printf_filtered ("/%c ", d
->format
.format
);
1822 puts_filtered (d
->exp_string
);
1823 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1824 printf_filtered (_(" (cannot be evaluated in the current context)"));
1825 printf_filtered ("\n");
1826 gdb_flush (gdb_stdout
);
1831 enable_display (char *args
, int from_tty
)
1840 for (d
= display_chain
; d
; d
= d
->next
)
1847 while (*p1
>= '0' && *p1
<= '9')
1849 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1850 error (_("Arguments must be display numbers."));
1854 for (d
= display_chain
; d
; d
= d
->next
)
1855 if (d
->number
== num
)
1860 printf_unfiltered (_("No display number %d.\n"), num
);
1863 while (*p
== ' ' || *p
== '\t')
1869 disable_display_command (char *args
, int from_tty
)
1877 for (d
= display_chain
; d
; d
= d
->next
)
1884 while (*p1
>= '0' && *p1
<= '9')
1886 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1887 error (_("Arguments must be display numbers."));
1889 disable_display (atoi (p
));
1892 while (*p
== ' ' || *p
== '\t')
1897 /* display_chain items point to blocks and expressions. Some expressions in
1898 turn may point to symbols.
1899 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1900 obstack_free'd when a shared library is unloaded.
1901 Clear pointers that are about to become dangling.
1902 Both .exp and .block fields will be restored next time we need to display
1903 an item by re-parsing .exp_string field in the new execution context. */
1906 clear_dangling_display_expressions (struct so_list
*solib
)
1908 struct objfile
*objfile
= solib
->objfile
;
1911 /* With no symbol file we cannot have a block or expression from it. */
1912 if (objfile
== NULL
)
1914 if (objfile
->separate_debug_objfile_backlink
)
1915 objfile
= objfile
->separate_debug_objfile_backlink
;
1916 gdb_assert (objfile
->pspace
== solib
->pspace
);
1918 for (d
= display_chain
; d
!= NULL
; d
= d
->next
)
1920 if (d
->pspace
!= solib
->pspace
)
1923 if (lookup_objfile_from_block (d
->block
) == objfile
1924 || (d
->exp
&& exp_uses_objfile (d
->exp
, objfile
)))
1934 /* Print the value in stack frame FRAME of a variable specified by a
1935 struct symbol. NAME is the name to print; if NULL then VAR's print
1936 name will be used. STREAM is the ui_file on which to print the
1937 value. INDENT specifies the number of indent levels to print
1938 before printing the variable name. */
1941 print_variable_and_value (const char *name
, struct symbol
*var
,
1942 struct frame_info
*frame
,
1943 struct ui_file
*stream
, int indent
)
1946 struct value_print_options opts
;
1949 name
= SYMBOL_PRINT_NAME (var
);
1951 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1953 val
= read_var_value (var
, frame
);
1954 get_user_print_options (&opts
);
1955 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1956 fprintf_filtered (stream
, "\n");
1960 printf_command (char *arg
, int from_tty
)
1964 char *string
= NULL
;
1965 struct value
**val_args
;
1967 char *current_substring
;
1969 int allocated_args
= 20;
1970 struct cleanup
*old_cleanups
;
1972 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1973 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1976 error_no_arg (_("format-control string and values to print"));
1978 /* Skip white space before format string */
1979 while (*s
== ' ' || *s
== '\t')
1982 /* A format string should follow, enveloped in double quotes. */
1984 error (_("Bad format string, missing '\"'."));
1986 /* Parse the format-control string and copy it into the string STRING,
1987 processing some kinds of escape sequence. */
1989 f
= string
= (char *) alloca (strlen (s
) + 1);
1997 error (_("Bad format string, non-terminated '\"'."));
2030 /* ??? TODO: handle other escape sequences */
2031 error (_("Unrecognized escape character \\%c in format string."),
2041 /* Skip over " and following space and comma. */
2044 while (*s
== ' ' || *s
== '\t')
2047 if (*s
!= ',' && *s
!= 0)
2048 error (_("Invalid argument syntax"));
2052 while (*s
== ' ' || *s
== '\t')
2055 /* Need extra space for the '\0's. Doubling the size is sufficient. */
2056 substrings
= alloca (strlen (string
) * 2);
2057 current_substring
= substrings
;
2060 /* Now scan the string for %-specs and see what kinds of args they want.
2061 argclass[I] classifies the %-specs so we can give printf_filtered
2062 something of the right size. */
2066 int_arg
, long_arg
, long_long_arg
, ptr_arg
,
2067 string_arg
, wide_string_arg
, wide_char_arg
,
2068 double_arg
, long_double_arg
, decfloat_arg
2070 enum argclass
*argclass
;
2071 enum argclass this_argclass
;
2076 argclass
= (enum argclass
*) alloca (strlen (s
) * sizeof *argclass
);
2083 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
2084 int seen_space
= 0, seen_plus
= 0;
2085 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
2086 int seen_big_d
= 0, seen_double_big_d
= 0;
2089 /* Check the validity of the format specifier, and work
2090 out what argument it expects. We only accept C89
2091 format strings, with the exception of long long (which
2092 we autoconf for). */
2094 /* Skip over "%%". */
2101 /* The first part of a format specifier is a set of flag
2103 while (strchr ("0-+ #", *f
))
2116 /* The next part of a format specifier is a width. */
2117 while (strchr ("0123456789", *f
))
2120 /* The next part of a format specifier is a precision. */
2125 while (strchr ("0123456789", *f
))
2129 /* The next part of a format specifier is a length modifier. */
2150 /* Decimal32 modifier. */
2156 /* Decimal64 and Decimal128 modifiers. */
2161 /* Check for a Decimal128. */
2165 seen_double_big_d
= 1;
2181 if (seen_space
|| seen_plus
)
2188 this_argclass
= int_arg
;
2189 else if (lcount
== 1)
2190 this_argclass
= long_arg
;
2192 this_argclass
= long_long_arg
;
2199 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
2200 if (lcount
> 1 || seen_h
|| seen_big_l
)
2202 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2207 this_argclass
= ptr_arg
;
2208 if (lcount
|| seen_h
|| seen_big_l
)
2210 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2215 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
2216 if (lcount
> 1 || seen_h
|| seen_big_l
)
2218 if (seen_zero
|| seen_space
|| seen_plus
)
2227 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
2228 this_argclass
= decfloat_arg
;
2229 else if (seen_big_l
)
2230 this_argclass
= long_double_arg
;
2232 this_argclass
= double_arg
;
2234 if (lcount
|| seen_h
)
2239 error (_("`*' not supported for precision or width in printf"));
2242 error (_("Format specifier `n' not supported in printf"));
2245 error (_("Incomplete format specifier at end of format string"));
2248 error (_("Unrecognized format specifier '%c' in printf"), *f
);
2252 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2257 if (lcount
> 1 && USE_PRINTF_I64
)
2259 /* Windows' printf does support long long, but not the usual way.
2260 Convert %lld to %I64d. */
2261 int length_before_ll
= f
- last_arg
- 1 - lcount
;
2263 strncpy (current_substring
, last_arg
, length_before_ll
);
2264 strcpy (current_substring
+ length_before_ll
, "I64");
2265 current_substring
[length_before_ll
+ 3] =
2266 last_arg
[length_before_ll
+ lcount
];
2267 current_substring
+= length_before_ll
+ 4;
2269 else if (this_argclass
== wide_string_arg
2270 || this_argclass
== wide_char_arg
)
2272 /* Convert %ls or %lc to %s. */
2273 int length_before_ls
= f
- last_arg
- 2;
2275 strncpy (current_substring
, last_arg
, length_before_ls
);
2276 strcpy (current_substring
+ length_before_ls
, "s");
2277 current_substring
+= length_before_ls
+ 2;
2281 strncpy (current_substring
, last_arg
, f
- last_arg
);
2282 current_substring
+= f
- last_arg
;
2284 *current_substring
++ = '\0';
2286 argclass
[nargs_wanted
++] = this_argclass
;
2289 /* Now, parse all arguments and evaluate them.
2290 Store the VALUEs in VAL_ARGS. */
2296 if (nargs
== allocated_args
)
2297 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2298 (allocated_args
*= 2)
2299 * sizeof (struct value
*));
2301 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2309 if (nargs
!= nargs_wanted
)
2310 error (_("Wrong number of arguments for specified format-string"));
2312 /* Now actually print them. */
2313 current_substring
= substrings
;
2314 for (i
= 0; i
< nargs
; i
++)
2316 switch (argclass
[i
])
2324 tem
= value_as_address (val_args
[i
]);
2326 /* This is a %s argument. Find the length of the string. */
2332 read_memory (tem
+ j
, &c
, 1);
2337 /* Copy the string contents into a string inside GDB. */
2338 str
= (gdb_byte
*) alloca (j
+ 1);
2340 read_memory (tem
, str
, j
);
2343 printf_filtered (current_substring
, (char *) str
);
2346 case wide_string_arg
:
2351 struct gdbarch
*gdbarch
2352 = get_type_arch (value_type (val_args
[i
]));
2353 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2354 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2355 "wchar_t", NULL
, 0);
2356 int wcwidth
= TYPE_LENGTH (wctype
);
2357 gdb_byte
*buf
= alloca (wcwidth
);
2358 struct obstack output
;
2359 struct cleanup
*inner_cleanup
;
2361 tem
= value_as_address (val_args
[i
]);
2363 /* This is a %s argument. Find the length of the string. */
2364 for (j
= 0;; j
+= wcwidth
)
2367 read_memory (tem
+ j
, buf
, wcwidth
);
2368 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2372 /* Copy the string contents into a string inside GDB. */
2373 str
= (gdb_byte
*) alloca (j
+ wcwidth
);
2375 read_memory (tem
, str
, j
);
2376 memset (&str
[j
], 0, wcwidth
);
2378 obstack_init (&output
);
2379 inner_cleanup
= make_cleanup_obstack_free (&output
);
2381 convert_between_encodings (target_wide_charset (gdbarch
),
2384 &output
, translit_char
);
2385 obstack_grow_str0 (&output
, "");
2387 printf_filtered (current_substring
, obstack_base (&output
));
2388 do_cleanups (inner_cleanup
);
2393 struct gdbarch
*gdbarch
2394 = get_type_arch (value_type (val_args
[i
]));
2395 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2396 "wchar_t", NULL
, 0);
2397 struct type
*valtype
;
2398 struct obstack output
;
2399 struct cleanup
*inner_cleanup
;
2400 const gdb_byte
*bytes
;
2402 valtype
= value_type (val_args
[i
]);
2403 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2404 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2405 error (_("expected wchar_t argument for %%lc"));
2407 bytes
= value_contents (val_args
[i
]);
2409 obstack_init (&output
);
2410 inner_cleanup
= make_cleanup_obstack_free (&output
);
2412 convert_between_encodings (target_wide_charset (gdbarch
),
2414 bytes
, TYPE_LENGTH (valtype
),
2415 TYPE_LENGTH (valtype
),
2416 &output
, translit_char
);
2417 obstack_grow_str0 (&output
, "");
2419 printf_filtered (current_substring
, obstack_base (&output
));
2420 do_cleanups (inner_cleanup
);
2425 struct type
*type
= value_type (val_args
[i
]);
2429 /* If format string wants a float, unchecked-convert the value
2430 to floating point of the same size. */
2431 type
= float_type_from_length (type
);
2432 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2434 error (_("Invalid floating value found in program."));
2436 printf_filtered (current_substring
, (double) val
);
2439 case long_double_arg
:
2440 #ifdef HAVE_LONG_DOUBLE
2442 struct type
*type
= value_type (val_args
[i
]);
2446 /* If format string wants a float, unchecked-convert the value
2447 to floating point of the same size. */
2448 type
= float_type_from_length (type
);
2449 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2451 error (_("Invalid floating value found in program."));
2453 printf_filtered (current_substring
, (long double) val
);
2457 error (_("long double not supported in printf"));
2460 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2462 long long val
= value_as_long (val_args
[i
]);
2464 printf_filtered (current_substring
, val
);
2468 error (_("long long not supported in printf"));
2472 int val
= value_as_long (val_args
[i
]);
2474 printf_filtered (current_substring
, val
);
2479 long val
= value_as_long (val_args
[i
]);
2481 printf_filtered (current_substring
, val
);
2485 /* Handles decimal floating values. */
2488 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2490 #if defined (PRINTF_HAS_DECFLOAT)
2491 /* If we have native support for Decimal floating
2492 printing, handle it here. */
2493 printf_filtered (current_substring
, param_ptr
);
2496 /* As a workaround until vasprintf has native support for DFP
2497 we convert the DFP values to string and print them using
2498 the %s format specifier. */
2501 int nnull_chars
= 0;
2503 /* Parameter data. */
2504 struct type
*param_type
= value_type (val_args
[i
]);
2505 unsigned int param_len
= TYPE_LENGTH (param_type
);
2506 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2507 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2509 /* DFP output data. */
2510 struct value
*dfp_value
= NULL
;
2514 struct type
*dfp_type
= NULL
;
2515 char decstr
[MAX_DECIMAL_STRING
];
2517 /* Points to the end of the string so that we can go back
2518 and check for DFP length modifiers. */
2519 eos
= current_substring
+ strlen (current_substring
);
2521 /* Look for the float/double format specifier. */
2522 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2523 && *eos
!= 'g' && *eos
!= 'G')
2528 /* Search for the '%' char and extract the size and type of
2529 the output decimal value based on its modifiers
2530 (%Hf, %Df, %DDf). */
2531 while (*--sos
!= '%')
2536 dfp_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2538 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2541 dfp_type
= builtin_type (gdbarch
)->builtin_declong
;
2547 dfp_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2551 /* Replace %Hf, %Df and %DDf with %s's. */
2554 /* Go through the whole format string and pull the correct
2555 number of chars back to compensate for the change in the
2556 format specifier. */
2557 while (nnull_chars
< nargs
- i
)
2565 /* Conversion between different DFP types. */
2566 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2567 decimal_convert (param_ptr
, param_len
, byte_order
,
2568 dec
, dfp_len
, byte_order
);
2570 /* If this is a non-trivial conversion, just output 0.
2571 A correct converted value can be displayed by explicitly
2572 casting to a DFP type. */
2573 decimal_from_string (dec
, dfp_len
, byte_order
, "0");
2575 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2577 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2579 decimal_to_string (dfp_ptr
, dfp_len
, byte_order
, decstr
);
2581 /* Print the DFP value. */
2582 printf_filtered (current_substring
, decstr
);
2590 /* We avoid the host's %p because pointers are too
2591 likely to be the wrong size. The only interesting
2592 modifier for %p is a width; extract that, and then
2593 handle %p as glibc would: %#x or a literal "(nil)". */
2595 char *p
, *fmt
, *fmt_p
;
2596 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2597 long long val
= value_as_long (val_args
[i
]);
2599 long val
= value_as_long (val_args
[i
]);
2602 fmt
= alloca (strlen (current_substring
) + 5);
2604 /* Copy up to the leading %. */
2605 p
= current_substring
;
2609 int is_percent
= (*p
== '%');
2624 /* Copy any width. */
2625 while (*p
>= '0' && *p
< '9')
2628 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2631 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2637 printf_filtered (fmt
, val
);
2643 printf_filtered (fmt
, "(nil)");
2649 internal_error (__FILE__
, __LINE__
,
2650 _("failed internal consistency check"));
2652 /* Skip to the next substring. */
2653 current_substring
+= strlen (current_substring
) + 1;
2655 /* Print the portion of the format string after the last argument.
2656 Note that this will not include any ordinary %-specs, but it
2657 might include "%%". That is why we use printf_filtered and not
2658 puts_filtered here. Also, we pass a dummy argument because
2659 some platforms have modified GCC to include -Wformat-security
2660 by default, which will warn here if there is no argument. */
2661 printf_filtered (last_arg
, 0);
2663 do_cleanups (old_cleanups
);
2667 _initialize_printcmd (void)
2669 struct cmd_list_element
*c
;
2671 current_display_number
= -1;
2673 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2675 add_info ("address", address_info
,
2676 _("Describe where symbol SYM is stored."));
2678 add_info ("symbol", sym_info
, _("\
2679 Describe what symbol is at location ADDR.\n\
2680 Only for symbols with fixed locations (global or static scope)."));
2682 add_com ("x", class_vars
, x_command
, _("\
2683 Examine memory: x/FMT ADDRESS.\n\
2684 ADDRESS is an expression for the memory address to examine.\n\
2685 FMT is a repeat count followed by a format letter and a size letter.\n\
2686 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2687 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2688 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2689 The specified number of objects of the specified size are printed\n\
2690 according to the format.\n\n\
2691 Defaults for format and size letters are those previously used.\n\
2692 Default count is 1. Default address is following last thing printed\n\
2693 with this command or \"print\"."));
2696 add_com ("whereis", class_vars
, whereis_command
,
2697 _("Print line number and file of definition of variable."));
2700 add_info ("display", display_info
, _("\
2701 Expressions to display when program stops, with code numbers."));
2703 add_cmd ("undisplay", 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 \"delete display\" has the same effect as this command.\n\
2708 Do \"info display\" to see current list of code numbers."),
2711 add_com ("display", class_vars
, display_command
, _("\
2712 Print value of expression EXP each time the program stops.\n\
2713 /FMT may be used before EXP as in the \"print\" command.\n\
2714 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2715 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2716 and examining is done as in the \"x\" command.\n\n\
2717 With no argument, display all currently requested auto-display expressions.\n\
2718 Use \"undisplay\" to cancel display requests previously made."));
2720 add_cmd ("display", class_vars
, enable_display
, _("\
2721 Enable some expressions to be displayed when program stops.\n\
2722 Arguments are the code numbers of the expressions to resume displaying.\n\
2723 No argument means enable all automatic-display expressions.\n\
2724 Do \"info display\" to see current list of code numbers."), &enablelist
);
2726 add_cmd ("display", class_vars
, disable_display_command
, _("\
2727 Disable some expressions to be displayed when program stops.\n\
2728 Arguments are the code numbers of the expressions to stop displaying.\n\
2729 No argument means disable all automatic-display expressions.\n\
2730 Do \"info display\" to see current list of code numbers."), &disablelist
);
2732 add_cmd ("display", class_vars
, undisplay_command
, _("\
2733 Cancel some expressions to be displayed when program stops.\n\
2734 Arguments are the code numbers of the expressions to stop displaying.\n\
2735 No argument means cancel all automatic-display expressions.\n\
2736 Do \"info display\" to see current list of code numbers."), &deletelist
);
2738 add_com ("printf", class_vars
, printf_command
, _("\
2739 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2740 This is useful for formatted output in user-defined commands."));
2742 add_com ("output", class_vars
, output_command
, _("\
2743 Like \"print\" but don't put in value history and don't print newline.\n\
2744 This is useful in user-defined commands."));
2746 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2747 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2748 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2749 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2750 with $), a register (a few standard names starting with $), or an actual\n\
2751 variable in the program being debugged. EXP is any valid expression.\n\
2752 Use \"set variable\" for variables with names identical to set subcommands.\n\
2754 With a subcommand, this command modifies parts of the gdb environment.\n\
2755 You can see these environment settings with the \"show\" command."),
2756 &setlist
, "set ", 1, &cmdlist
);
2758 add_com ("assign", class_vars
, set_command
, _("\
2759 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2760 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2761 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2762 with $), a register (a few standard names starting with $), or an actual\n\
2763 variable in the program being debugged. EXP is any valid expression.\n\
2764 Use \"set variable\" for variables with names identical to set subcommands.\n\
2765 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2766 You can see these environment settings with the \"show\" command."));
2768 /* "call" is the same as "set", but handy for dbx users to call fns. */
2769 c
= add_com ("call", class_vars
, call_command
, _("\
2770 Call a function in the program.\n\
2771 The argument is the function name and arguments, in the notation of the\n\
2772 current working language. The result is printed and saved in the value\n\
2773 history, if it is not void."));
2774 set_cmd_completer (c
, expression_completer
);
2776 add_cmd ("variable", class_vars
, set_command
, _("\
2777 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2778 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2779 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2780 with $), a register (a few standard names starting with $), or an actual\n\
2781 variable in the program being debugged. EXP is any valid expression.\n\
2782 This may usually be abbreviated to simply \"set\"."),
2785 c
= add_com ("print", class_vars
, print_command
, _("\
2786 Print value of expression EXP.\n\
2787 Variables accessible are those of the lexical environment of the selected\n\
2788 stack frame, plus all those whose scope is global or an entire file.\n\
2790 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2791 $$NUM refers to NUM'th value back from the last one.\n\
2792 Names starting with $ refer to registers (with the values they would have\n\
2793 if the program were to return to the stack frame now selected, restoring\n\
2794 all registers saved by frames farther in) or else to debugger\n\
2795 \"convenience\" variables (any such name not a known register).\n\
2796 Use assignment expressions to give values to convenience variables.\n\
2798 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2799 @ is a binary operator for treating consecutive data objects\n\
2800 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2801 element is FOO, whose second element is stored in the space following\n\
2802 where FOO is stored, etc. FOO must be an expression whose value\n\
2803 resides in memory.\n\
2805 EXP may be preceded with /FMT, where FMT is a format letter\n\
2806 but no count or size letter (see \"x\" command)."));
2807 set_cmd_completer (c
, expression_completer
);
2808 add_com_alias ("p", "print", class_vars
, 1);
2810 c
= add_com ("inspect", class_vars
, inspect_command
, _("\
2811 Same as \"print\" command, except that if you are running in the epoch\n\
2812 environment, the value is printed in its own window."));
2813 set_cmd_completer (c
, expression_completer
);
2815 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2816 &max_symbolic_offset
, _("\
2817 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2818 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2820 show_max_symbolic_offset
,
2821 &setprintlist
, &showprintlist
);
2822 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2823 &print_symbol_filename
, _("\
2824 Set printing of source filename and line number with <symbol>."), _("\
2825 Show printing of source filename and line number with <symbol>."), NULL
,
2827 show_print_symbol_filename
,
2828 &setprintlist
, &showprintlist
);