1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdb_string.h"
27 #include "expression.h"
31 #include "breakpoint.h"
33 #include "gdb-demangle.h"
36 #include "symfile.h" /* for overlay functions */
37 #include "objfiles.h" /* ditto */
38 #include "completer.h" /* for completion functions */
40 #include "gdb_assert.h"
45 #include "exceptions.h"
48 #include "parser-defs.h"
50 #include "arch-utils.h"
51 #include "cli/cli-utils.h"
55 #include "tui/tui.h" /* For tui_active et al. */
64 /* True if the value should be printed raw -- that is, bypassing
65 python-based formatters. */
69 /* Last specified output format. */
71 static char last_format
= 0;
73 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
75 static char last_size
= 'w';
77 /* Default address to examine next, and associated architecture. */
79 static struct gdbarch
*next_gdbarch
;
80 static CORE_ADDR next_address
;
82 /* Number of delay instructions following current disassembled insn. */
84 static int branch_delay_insns
;
86 /* Last address examined. */
88 static CORE_ADDR last_examine_address
;
90 /* Contents of last address examined.
91 This is not valid past the end of the `x' command! */
93 static struct value
*last_examine_value
;
95 /* Largest offset between a symbolic value and an address, that will be
96 printed as `0x1234 <symbol+offset>'. */
98 static unsigned int max_symbolic_offset
= UINT_MAX
;
100 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
101 struct cmd_list_element
*c
, const char *value
)
103 fprintf_filtered (file
,
104 _("The largest offset that will be "
105 "printed in <symbol+1234> form is %s.\n"),
109 /* Append the source filename and linenumber of the symbol when
110 printing a symbolic value as `<symbol at filename:linenum>' if set. */
111 static int print_symbol_filename
= 0;
113 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
114 struct cmd_list_element
*c
, const char *value
)
116 fprintf_filtered (file
, _("Printing of source filename and "
117 "line number with <symbol> is %s.\n"),
121 /* Number of auto-display expression currently being displayed.
122 So that we can disable it if we get a signal within it.
123 -1 when not doing one. */
125 static int current_display_number
;
129 /* Chain link to next auto-display item. */
130 struct display
*next
;
132 /* The expression as the user typed it. */
135 /* Expression to be evaluated and displayed. */
136 struct expression
*exp
;
138 /* Item number of this auto-display item. */
141 /* Display format specified. */
142 struct format_data format
;
144 /* Program space associated with `block'. */
145 struct program_space
*pspace
;
147 /* Innermost block required by this expression when evaluated. */
148 const struct block
*block
;
150 /* Status of this display (enabled or disabled). */
154 /* Chain of expressions whose values should be displayed
155 automatically each time the program stops. */
157 static struct display
*display_chain
;
159 static int display_number
;
161 /* Walk the following statement or block through all displays.
162 ALL_DISPLAYS_SAFE does so even if the statement deletes the current
165 #define ALL_DISPLAYS(B) \
166 for (B = display_chain; B; B = B->next)
168 #define ALL_DISPLAYS_SAFE(B,TMP) \
169 for (B = display_chain; \
170 B ? (TMP = B->next, 1): 0; \
173 /* Prototypes for exported functions. */
175 void _initialize_printcmd (void);
177 /* Prototypes for local functions. */
179 static void do_one_display (struct display
*);
182 /* Decode a format specification. *STRING_PTR should point to it.
183 OFORMAT and OSIZE are used as defaults for the format and size
184 if none are given in the format specification.
185 If OSIZE is zero, then the size field of the returned value
186 should be set only if a size is explicitly specified by the
188 The structure returned describes all the data
189 found in the specification. In addition, *STRING_PTR is advanced
190 past the specification and past all whitespace following it. */
192 static struct format_data
193 decode_format (char **string_ptr
, int oformat
, int osize
)
195 struct format_data val
;
196 char *p
= *string_ptr
;
203 if (*p
>= '0' && *p
<= '9')
204 val
.count
= atoi (p
);
205 while (*p
>= '0' && *p
<= '9')
208 /* Now process size or format letters that follow. */
212 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
219 else if (*p
>= 'a' && *p
<= 'z')
225 while (*p
== ' ' || *p
== '\t')
229 /* Set defaults for format and size if not specified. */
230 if (val
.format
== '?')
234 /* Neither has been specified. */
235 val
.format
= oformat
;
239 /* If a size is specified, any format makes a reasonable
240 default except 'i'. */
241 val
.format
= oformat
== 'i' ? 'x' : oformat
;
243 else if (val
.size
== '?')
247 /* Pick the appropriate size for an address. This is deferred
248 until do_examine when we know the actual architecture to use.
249 A special size value of 'a' is used to indicate this case. */
250 val
.size
= osize
? 'a' : osize
;
253 /* Floating point has to be word or giantword. */
254 if (osize
== 'w' || osize
== 'g')
257 /* Default it to giantword if the last used size is not
259 val
.size
= osize
? 'g' : osize
;
262 /* Characters default to one byte. */
263 val
.size
= osize
? 'b' : osize
;
266 /* Display strings with byte size chars unless explicitly
272 /* The default is the size most recently specified. */
279 /* Print value VAL on stream according to OPTIONS.
280 Do not end with a newline.
281 SIZE is the letter for the size of datum being printed.
282 This is used to pad hex numbers so they line up. SIZE is 0
283 for print / output and set for examine. */
286 print_formatted (struct value
*val
, int size
,
287 const struct value_print_options
*options
,
288 struct ui_file
*stream
)
290 struct type
*type
= check_typedef (value_type (val
));
291 int len
= TYPE_LENGTH (type
);
293 if (VALUE_LVAL (val
) == lval_memory
)
294 next_address
= value_address (val
) + len
;
298 switch (options
->format
)
302 struct type
*elttype
= value_type (val
);
304 next_address
= (value_address (val
)
305 + val_print_string (elttype
, NULL
,
306 value_address (val
), -1,
307 stream
, options
) * len
);
312 /* We often wrap here if there are long symbolic names. */
314 next_address
= (value_address (val
)
315 + gdb_print_insn (get_type_arch (type
),
316 value_address (val
), stream
,
317 &branch_delay_insns
));
322 if (options
->format
== 0 || options
->format
== 's'
323 || TYPE_CODE (type
) == TYPE_CODE_REF
324 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
325 || TYPE_CODE (type
) == TYPE_CODE_STRING
326 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
327 || TYPE_CODE (type
) == TYPE_CODE_UNION
328 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
329 value_print (val
, stream
, options
);
331 /* User specified format, so don't look to the type to tell us
333 val_print_scalar_formatted (type
,
334 value_contents_for_printing (val
),
335 value_embedded_offset (val
),
337 options
, size
, stream
);
340 /* Return builtin floating point type of same length as TYPE.
341 If no such type is found, return TYPE itself. */
343 float_type_from_length (struct type
*type
)
345 struct gdbarch
*gdbarch
= get_type_arch (type
);
346 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
348 if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_float
))
349 type
= builtin
->builtin_float
;
350 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_double
))
351 type
= builtin
->builtin_double
;
352 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_long_double
))
353 type
= builtin
->builtin_long_double
;
358 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
359 according to OPTIONS and SIZE on STREAM. Formats s and i are not
360 supported at this level. */
363 print_scalar_formatted (const void *valaddr
, struct type
*type
,
364 const struct value_print_options
*options
,
365 int size
, struct ui_file
*stream
)
367 struct gdbarch
*gdbarch
= get_type_arch (type
);
368 LONGEST val_long
= 0;
369 unsigned int len
= TYPE_LENGTH (type
);
370 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
372 /* String printing should go through val_print_scalar_formatted. */
373 gdb_assert (options
->format
!= 's');
375 if (len
> sizeof(LONGEST
) &&
376 (TYPE_CODE (type
) == TYPE_CODE_INT
377 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
379 switch (options
->format
)
382 print_octal_chars (stream
, valaddr
, len
, byte_order
);
386 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
389 print_binary_chars (stream
, valaddr
, len
, byte_order
);
392 print_hex_chars (stream
, valaddr
, len
, byte_order
);
395 print_char_chars (stream
, type
, valaddr
, len
, byte_order
);
402 if (options
->format
!= 'f')
403 val_long
= unpack_long (type
, valaddr
);
405 /* If the value is a pointer, and pointers and addresses are not the
406 same, then at this point, the value's length (in target bytes) is
407 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
408 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
409 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
411 /* If we are printing it as unsigned, truncate it in case it is actually
412 a negative signed value (e.g. "print/u (short)-1" should print 65535
413 (if shorts are 16 bits) instead of 4294967295). */
414 if (options
->format
!= 'd' || TYPE_UNSIGNED (type
))
416 if (len
< sizeof (LONGEST
))
417 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
420 switch (options
->format
)
425 /* No size specified, like in print. Print varying # of digits. */
426 print_longest (stream
, 'x', 1, val_long
);
435 print_longest (stream
, size
, 1, val_long
);
438 error (_("Undefined output size \"%c\"."), size
);
443 print_longest (stream
, 'd', 1, val_long
);
447 print_longest (stream
, 'u', 0, val_long
);
452 print_longest (stream
, 'o', 1, val_long
);
454 fprintf_filtered (stream
, "0");
459 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
461 print_address (gdbarch
, addr
, stream
);
467 struct value_print_options opts
= *options
;
470 if (TYPE_UNSIGNED (type
))
471 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
473 type
= builtin_type (gdbarch
)->builtin_true_char
;
475 value_print (value_from_longest (type
, val_long
), stream
, &opts
);
480 type
= float_type_from_length (type
);
481 print_floating (valaddr
, type
, stream
);
485 internal_error (__FILE__
, __LINE__
,
486 _("failed internal consistency check"));
489 /* Binary; 't' stands for "two". */
491 char bits
[8 * (sizeof val_long
) + 1];
492 char buf
[8 * (sizeof val_long
) + 32];
497 width
= 8 * (sizeof val_long
);
514 error (_("Undefined output size \"%c\"."), size
);
520 bits
[width
] = (val_long
& 1) ? '1' : '0';
525 while (*cp
&& *cp
== '0')
530 strncpy (buf
, cp
, sizeof (bits
));
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. Returns
563 non-zero if anything was printed; zero otherwise. */
566 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
567 struct ui_file
*stream
,
568 int do_demangle
, char *leadin
)
571 char *filename
= NULL
;
576 /* Throw away both name and filename. */
577 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
578 make_cleanup (free_current_contents
, &filename
);
580 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, &name
, &offset
,
581 &filename
, &line
, &unmapped
))
583 do_cleanups (cleanup_chain
);
587 fputs_filtered (leadin
, stream
);
589 fputs_filtered ("<*", stream
);
591 fputs_filtered ("<", stream
);
592 fputs_filtered (name
, stream
);
594 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
596 /* Append source filename and line number if desired. Give specific
597 line # of this addr, if we have it; else line # of the nearest symbol. */
598 if (print_symbol_filename
&& filename
!= NULL
)
601 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
603 fprintf_filtered (stream
, " in %s", filename
);
606 fputs_filtered ("*>", stream
);
608 fputs_filtered (">", stream
);
610 do_cleanups (cleanup_chain
);
614 /* Given an address ADDR return all the elements needed to print the
615 address in a symbolic form. NAME can be mangled or not depending
616 on DO_DEMANGLE (and also on the asm_demangle global variable,
617 manipulated via ''set print asm-demangle''). Return 0 in case of
618 success, when all the info in the OUT paramters is valid. Return 1
621 build_address_symbolic (struct gdbarch
*gdbarch
,
622 CORE_ADDR addr
, /* IN */
623 int do_demangle
, /* IN */
624 char **name
, /* OUT */
625 int *offset
, /* OUT */
626 char **filename
, /* OUT */
628 int *unmapped
) /* OUT */
630 struct minimal_symbol
*msymbol
;
631 struct symbol
*symbol
;
632 CORE_ADDR name_location
= 0;
633 struct obj_section
*section
= NULL
;
634 const char *name_temp
= "";
636 /* Let's say it is mapped (not unmapped). */
639 /* Determine if the address is in an overlay, and whether it is
641 if (overlay_debugging
)
643 section
= find_pc_overlay (addr
);
644 if (pc_in_unmapped_range (addr
, section
))
647 addr
= overlay_mapped_address (addr
, section
);
651 /* First try to find the address in the symbol table, then
652 in the minsyms. Take the closest one. */
654 /* This is defective in the sense that it only finds text symbols. So
655 really this is kind of pointless--we should make sure that the
656 minimal symbols have everything we need (by changing that we could
657 save some memory, but for many debug format--ELF/DWARF or
658 anything/stabs--it would be inconvenient to eliminate those minimal
660 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
661 symbol
= find_pc_sect_function (addr
, section
);
665 /* If this is a function (i.e. a code address), strip out any
666 non-address bits. For instance, display a pointer to the
667 first instruction of a Thumb function as <function>; the
668 second instruction will be <function+2>, even though the
669 pointer is <function+3>. This matches the ISA behavior. */
670 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
672 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
673 if (do_demangle
|| asm_demangle
)
674 name_temp
= SYMBOL_PRINT_NAME (symbol
);
676 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
680 && MSYMBOL_HAS_SIZE (msymbol
)
681 && MSYMBOL_SIZE (msymbol
) == 0
682 && MSYMBOL_TYPE (msymbol
) != mst_text
683 && MSYMBOL_TYPE (msymbol
) != mst_text_gnu_ifunc
684 && MSYMBOL_TYPE (msymbol
) != mst_file_text
)
689 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
691 /* The msymbol is closer to the address than the symbol;
692 use the msymbol instead. */
694 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
695 if (do_demangle
|| asm_demangle
)
696 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
698 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
701 if (symbol
== NULL
&& msymbol
== NULL
)
704 /* If the nearest symbol is too far away, don't print anything symbolic. */
706 /* For when CORE_ADDR is larger than unsigned int, we do math in
707 CORE_ADDR. But when we detect unsigned wraparound in the
708 CORE_ADDR math, we ignore this test and print the offset,
709 because addr+max_symbolic_offset has wrapped through the end
710 of the address space back to the beginning, giving bogus comparison. */
711 if (addr
> name_location
+ max_symbolic_offset
712 && name_location
+ max_symbolic_offset
> name_location
)
715 *offset
= addr
- name_location
;
717 *name
= xstrdup (name_temp
);
719 if (print_symbol_filename
)
721 struct symtab_and_line sal
;
723 sal
= find_pc_sect_line (addr
, section
, 0);
727 *filename
= xstrdup (sal
.symtab
->filename
);
735 /* Print address ADDR symbolically on STREAM.
736 First print it as a number. Then perhaps print
737 <SYMBOL + OFFSET> after the number. */
740 print_address (struct gdbarch
*gdbarch
,
741 CORE_ADDR addr
, struct ui_file
*stream
)
743 fputs_filtered (paddress (gdbarch
, addr
), stream
);
744 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
747 /* Return a prefix for instruction address:
748 "=> " for current instruction, else " ". */
751 pc_prefix (CORE_ADDR addr
)
753 if (has_stack_frames ())
755 struct frame_info
*frame
;
758 frame
= get_selected_frame (NULL
);
759 if (get_frame_pc_if_available (frame
, &pc
) && pc
== addr
)
765 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
766 controls whether to print the symbolic name "raw" or demangled.
767 Return non-zero if anything was printed; zero otherwise. */
770 print_address_demangle (const struct value_print_options
*opts
,
771 struct gdbarch
*gdbarch
, CORE_ADDR addr
,
772 struct ui_file
*stream
, int do_demangle
)
774 if (opts
->addressprint
)
776 fputs_filtered (paddress (gdbarch
, addr
), stream
);
777 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
781 return print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
787 /* Examine data at address ADDR in format FMT.
788 Fetch it from memory and print on gdb_stdout. */
791 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
796 struct type
*val_type
= NULL
;
799 struct value_print_options opts
;
804 next_gdbarch
= gdbarch
;
807 /* Instruction format implies fetch single bytes
808 regardless of the specified size.
809 The case of strings is handled in decode_format, only explicit
810 size operator are not changed to 'b'. */
816 /* Pick the appropriate size for an address. */
817 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
819 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
821 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
824 /* Bad value for gdbarch_ptr_bit. */
825 internal_error (__FILE__
, __LINE__
,
826 _("failed internal consistency check"));
830 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
831 else if (size
== 'h')
832 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
833 else if (size
== 'w')
834 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
835 else if (size
== 'g')
836 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
840 struct type
*char_type
= NULL
;
842 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
843 if type is not found. */
845 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
846 else if (size
== 'w')
847 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
849 val_type
= char_type
;
852 if (size
!= '\0' && size
!= 'b')
853 warning (_("Unable to display strings with "
854 "size '%c', using 'b' instead."), size
);
856 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
865 if (format
== 's' || format
== 'i')
868 get_formatted_print_options (&opts
, format
);
870 /* Print as many objects as specified in COUNT, at most maxelts per line,
871 with the address of the next one at the start of each line. */
877 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
878 print_address (next_gdbarch
, next_address
, gdb_stdout
);
879 printf_filtered (":");
884 printf_filtered ("\t");
885 /* Note that print_formatted sets next_address for the next
887 last_examine_address
= next_address
;
889 if (last_examine_value
)
890 value_free (last_examine_value
);
892 /* The value to be displayed is not fetched greedily.
893 Instead, to avoid the possibility of a fetched value not
894 being used, its retrieval is delayed until the print code
895 uses it. When examining an instruction stream, the
896 disassembler will perform its own memory fetch using just
897 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
898 the disassembler be modified so that LAST_EXAMINE_VALUE
899 is left with the byte sequence from the last complete
900 instruction fetched from memory? */
901 last_examine_value
= value_at_lazy (val_type
, next_address
);
903 if (last_examine_value
)
904 release_value (last_examine_value
);
906 print_formatted (last_examine_value
, size
, &opts
, gdb_stdout
);
908 /* Display any branch delay slots following the final insn. */
909 if (format
== 'i' && count
== 1)
910 count
+= branch_delay_insns
;
912 printf_filtered ("\n");
913 gdb_flush (gdb_stdout
);
918 validate_format (struct format_data fmt
, char *cmdname
)
921 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
923 error (_("Item count other than 1 is meaningless in \"%s\" command."),
925 if (fmt
.format
== 'i')
926 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
927 fmt
.format
, cmdname
);
930 /* Evaluate string EXP as an expression in the current language and
931 print the resulting value. EXP may contain a format specifier as the
932 first argument ("/x myvar" for example, to print myvar in hex). */
935 print_command_1 (char *exp
, int voidprint
)
937 struct expression
*expr
;
938 struct cleanup
*old_chain
= 0;
941 struct format_data fmt
;
944 if (exp
&& *exp
== '/')
947 fmt
= decode_format (&exp
, last_format
, 0);
948 validate_format (fmt
, "print");
949 last_format
= format
= fmt
.format
;
961 expr
= parse_expression (exp
);
962 old_chain
= make_cleanup (free_current_contents
, &expr
);
964 val
= evaluate_expression (expr
);
967 val
= access_value_history (0);
969 if (voidprint
|| (val
&& value_type (val
) &&
970 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
972 struct value_print_options opts
;
973 int histindex
= record_latest_value (val
);
976 annotate_value_history_begin (histindex
, value_type (val
));
978 annotate_value_begin (value_type (val
));
981 printf_filtered ("$%d = ", histindex
);
984 annotate_value_history_value ();
986 get_formatted_print_options (&opts
, format
);
989 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
990 printf_filtered ("\n");
993 annotate_value_history_end ();
995 annotate_value_end ();
999 do_cleanups (old_chain
);
1003 print_command (char *exp
, int from_tty
)
1005 print_command_1 (exp
, 1);
1008 /* Same as print, except it doesn't print void results. */
1010 call_command (char *exp
, int from_tty
)
1012 print_command_1 (exp
, 0);
1016 output_command (char *exp
, int from_tty
)
1018 struct expression
*expr
;
1019 struct cleanup
*old_chain
;
1022 struct format_data fmt
;
1023 struct value_print_options opts
;
1028 if (exp
&& *exp
== '/')
1031 fmt
= decode_format (&exp
, 0, 0);
1032 validate_format (fmt
, "output");
1033 format
= fmt
.format
;
1036 expr
= parse_expression (exp
);
1037 old_chain
= make_cleanup (free_current_contents
, &expr
);
1039 val
= evaluate_expression (expr
);
1041 annotate_value_begin (value_type (val
));
1043 get_formatted_print_options (&opts
, format
);
1045 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1047 annotate_value_end ();
1050 gdb_flush (gdb_stdout
);
1052 do_cleanups (old_chain
);
1056 set_command (char *exp
, int from_tty
)
1058 struct expression
*expr
= parse_expression (exp
);
1059 struct cleanup
*old_chain
=
1060 make_cleanup (free_current_contents
, &expr
);
1062 if (expr
->nelts
>= 1)
1063 switch (expr
->elts
[0].opcode
)
1065 case UNOP_PREINCREMENT
:
1066 case UNOP_POSTINCREMENT
:
1067 case UNOP_PREDECREMENT
:
1068 case UNOP_POSTDECREMENT
:
1070 case BINOP_ASSIGN_MODIFY
:
1075 (_("Expression is not an assignment (and might have no effect)"));
1078 evaluate_expression (expr
);
1079 do_cleanups (old_chain
);
1083 sym_info (char *arg
, int from_tty
)
1085 struct minimal_symbol
*msymbol
;
1086 struct objfile
*objfile
;
1087 struct obj_section
*osect
;
1088 CORE_ADDR addr
, sect_addr
;
1090 unsigned int offset
;
1093 error_no_arg (_("address"));
1095 addr
= parse_and_eval_address (arg
);
1096 ALL_OBJSECTIONS (objfile
, osect
)
1098 /* Only process each object file once, even if there's a separate
1100 if (objfile
->separate_debug_objfile_backlink
)
1103 sect_addr
= overlay_mapped_address (addr
, osect
);
1105 if (obj_section_addr (osect
) <= sect_addr
1106 && sect_addr
< obj_section_endaddr (osect
)
1107 && (msymbol
= lookup_minimal_symbol_by_pc_section (sect_addr
, osect
)))
1109 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1111 struct cleanup
*old_chain
;
1114 offset
= sect_addr
- SYMBOL_VALUE_ADDRESS (msymbol
);
1115 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1116 sec_name
= osect
->the_bfd_section
->name
;
1117 msym_name
= SYMBOL_PRINT_NAME (msymbol
);
1119 /* Don't print the offset if it is zero.
1120 We assume there's no need to handle i18n of "sym + offset". */
1122 loc_string
= xstrprintf ("%s + %u", msym_name
, offset
);
1124 loc_string
= xstrprintf ("%s", msym_name
);
1126 /* Use a cleanup to free loc_string in case the user quits
1127 a pagination request inside printf_filtered. */
1128 old_chain
= make_cleanup (xfree
, loc_string
);
1130 gdb_assert (osect
->objfile
&& osect
->objfile
->name
);
1131 obj_name
= osect
->objfile
->name
;
1133 if (MULTI_OBJFILE_P ())
1134 if (pc_in_unmapped_range (addr
, osect
))
1135 if (section_is_overlay (osect
))
1136 printf_filtered (_("%s in load address range of "
1137 "%s overlay section %s of %s\n"),
1138 loc_string
, mapped
, sec_name
, obj_name
);
1140 printf_filtered (_("%s in load address range of "
1141 "section %s of %s\n"),
1142 loc_string
, sec_name
, obj_name
);
1144 if (section_is_overlay (osect
))
1145 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1146 loc_string
, mapped
, sec_name
, obj_name
);
1148 printf_filtered (_("%s in section %s of %s\n"),
1149 loc_string
, sec_name
, obj_name
);
1151 if (pc_in_unmapped_range (addr
, osect
))
1152 if (section_is_overlay (osect
))
1153 printf_filtered (_("%s in load address range of %s overlay "
1155 loc_string
, mapped
, sec_name
);
1157 printf_filtered (_("%s in load address range of section %s\n"),
1158 loc_string
, sec_name
);
1160 if (section_is_overlay (osect
))
1161 printf_filtered (_("%s in %s overlay section %s\n"),
1162 loc_string
, mapped
, sec_name
);
1164 printf_filtered (_("%s in section %s\n"),
1165 loc_string
, sec_name
);
1167 do_cleanups (old_chain
);
1171 printf_filtered (_("No symbol matches %s.\n"), arg
);
1175 address_info (char *exp
, int from_tty
)
1177 struct gdbarch
*gdbarch
;
1180 struct minimal_symbol
*msymbol
;
1182 struct obj_section
*section
;
1183 CORE_ADDR load_addr
, context_pc
= 0;
1184 struct field_of_this_result is_a_field_of_this
;
1187 error (_("Argument required."));
1189 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1190 &is_a_field_of_this
);
1193 if (is_a_field_of_this
.type
!= NULL
)
1195 printf_filtered ("Symbol \"");
1196 fprintf_symbol_filtered (gdb_stdout
, exp
,
1197 current_language
->la_language
, DMGL_ANSI
);
1198 printf_filtered ("\" is a field of the local class variable ");
1199 if (current_language
->la_language
== language_objc
)
1200 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1202 printf_filtered ("`this'\n");
1206 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1208 if (msymbol
!= NULL
)
1210 gdbarch
= get_objfile_arch (msymbol_objfile (msymbol
));
1211 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1213 printf_filtered ("Symbol \"");
1214 fprintf_symbol_filtered (gdb_stdout
, exp
,
1215 current_language
->la_language
, DMGL_ANSI
);
1216 printf_filtered ("\" is at ");
1217 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1218 printf_filtered (" in a file compiled without debugging");
1219 section
= SYMBOL_OBJ_SECTION (msymbol
);
1220 if (section_is_overlay (section
))
1222 load_addr
= overlay_unmapped_address (load_addr
, section
);
1223 printf_filtered (",\n -- loaded at ");
1224 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1225 printf_filtered (" in overlay section %s",
1226 section
->the_bfd_section
->name
);
1228 printf_filtered (".\n");
1231 error (_("No symbol \"%s\" in current context."), exp
);
1235 printf_filtered ("Symbol \"");
1236 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1237 current_language
->la_language
, DMGL_ANSI
);
1238 printf_filtered ("\" is ");
1239 val
= SYMBOL_VALUE (sym
);
1240 section
= SYMBOL_OBJ_SECTION (sym
);
1241 gdbarch
= get_objfile_arch (SYMBOL_SYMTAB (sym
)->objfile
);
1243 switch (SYMBOL_CLASS (sym
))
1246 case LOC_CONST_BYTES
:
1247 printf_filtered ("constant");
1251 printf_filtered ("a label at address ");
1252 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1253 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1254 if (section_is_overlay (section
))
1256 load_addr
= overlay_unmapped_address (load_addr
, section
);
1257 printf_filtered (",\n -- loaded at ");
1258 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1259 printf_filtered (" in overlay section %s",
1260 section
->the_bfd_section
->name
);
1265 /* FIXME: cagney/2004-01-26: It should be possible to
1266 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1267 Unfortunately DWARF 2 stores the frame-base (instead of the
1268 function) location in a function's symbol. Oops! For the
1269 moment enable this when/where applicable. */
1270 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
,
1275 /* GDBARCH is the architecture associated with the objfile the symbol
1276 is defined in; the target architecture may be different, and may
1277 provide additional registers. However, we do not know the target
1278 architecture at this point. We assume the objfile architecture
1279 will contain all the standard registers that occur in debug info
1281 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1283 if (SYMBOL_IS_ARGUMENT (sym
))
1284 printf_filtered (_("an argument in register %s"),
1285 gdbarch_register_name (gdbarch
, regno
));
1287 printf_filtered (_("a variable in register %s"),
1288 gdbarch_register_name (gdbarch
, regno
));
1292 printf_filtered (_("static storage at address "));
1293 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1294 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1295 if (section_is_overlay (section
))
1297 load_addr
= overlay_unmapped_address (load_addr
, section
);
1298 printf_filtered (_(",\n -- loaded at "));
1299 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1300 printf_filtered (_(" in overlay section %s"),
1301 section
->the_bfd_section
->name
);
1305 case LOC_REGPARM_ADDR
:
1306 /* Note comment at LOC_REGISTER. */
1307 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1308 printf_filtered (_("address of an argument in register %s"),
1309 gdbarch_register_name (gdbarch
, regno
));
1313 printf_filtered (_("an argument at offset %ld"), val
);
1317 printf_filtered (_("a local variable at frame offset %ld"), val
);
1321 printf_filtered (_("a reference argument at offset %ld"), val
);
1325 printf_filtered (_("a typedef"));
1329 printf_filtered (_("a function at address "));
1330 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1331 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1332 if (section_is_overlay (section
))
1334 load_addr
= overlay_unmapped_address (load_addr
, section
);
1335 printf_filtered (_(",\n -- loaded at "));
1336 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1337 printf_filtered (_(" in overlay section %s"),
1338 section
->the_bfd_section
->name
);
1342 case LOC_UNRESOLVED
:
1344 struct minimal_symbol
*msym
;
1346 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1348 printf_filtered ("unresolved");
1351 section
= SYMBOL_OBJ_SECTION (msym
);
1352 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1355 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1356 printf_filtered (_("a thread-local variable at offset %s "
1357 "in the thread-local storage for `%s'"),
1358 paddress (gdbarch
, load_addr
),
1359 section
->objfile
->name
);
1362 printf_filtered (_("static storage at address "));
1363 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1364 if (section_is_overlay (section
))
1366 load_addr
= overlay_unmapped_address (load_addr
, section
);
1367 printf_filtered (_(",\n -- loaded at "));
1368 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1369 printf_filtered (_(" in overlay section %s"),
1370 section
->the_bfd_section
->name
);
1377 case LOC_OPTIMIZED_OUT
:
1378 printf_filtered (_("optimized out"));
1382 printf_filtered (_("of unknown (botched) type"));
1385 printf_filtered (".\n");
1390 x_command (char *exp
, int from_tty
)
1392 struct expression
*expr
;
1393 struct format_data fmt
;
1394 struct cleanup
*old_chain
;
1397 fmt
.format
= last_format
? last_format
: 'x';
1398 fmt
.size
= last_size
;
1402 if (exp
&& *exp
== '/')
1405 fmt
= decode_format (&exp
, last_format
, last_size
);
1408 /* If we have an expression, evaluate it and use it as the address. */
1410 if (exp
!= 0 && *exp
!= 0)
1412 expr
= parse_expression (exp
);
1413 /* Cause expression not to be there any more if this command is
1414 repeated with Newline. But don't clobber a user-defined
1415 command's definition. */
1418 old_chain
= make_cleanup (free_current_contents
, &expr
);
1419 val
= evaluate_expression (expr
);
1420 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1421 val
= coerce_ref (val
);
1422 /* In rvalue contexts, such as this, functions are coerced into
1423 pointers to functions. This makes "x/i main" work. */
1424 if (/* last_format == 'i' && */
1425 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1426 && VALUE_LVAL (val
) == lval_memory
)
1427 next_address
= value_address (val
);
1429 next_address
= value_as_address (val
);
1431 next_gdbarch
= expr
->gdbarch
;
1432 do_cleanups (old_chain
);
1436 error_no_arg (_("starting display address"));
1438 do_examine (fmt
, next_gdbarch
, next_address
);
1440 /* If the examine succeeds, we remember its size and format for next
1441 time. Set last_size to 'b' for strings. */
1442 if (fmt
.format
== 's')
1445 last_size
= fmt
.size
;
1446 last_format
= fmt
.format
;
1448 /* Set a couple of internal variables if appropriate. */
1449 if (last_examine_value
)
1451 /* Make last address examined available to the user as $_. Use
1452 the correct pointer type. */
1453 struct type
*pointer_type
1454 = lookup_pointer_type (value_type (last_examine_value
));
1455 set_internalvar (lookup_internalvar ("_"),
1456 value_from_pointer (pointer_type
,
1457 last_examine_address
));
1459 /* Make contents of last address examined available to the user
1460 as $__. If the last value has not been fetched from memory
1461 then don't fetch it now; instead mark it by voiding the $__
1463 if (value_lazy (last_examine_value
))
1464 clear_internalvar (lookup_internalvar ("__"));
1466 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1471 /* Add an expression to the auto-display chain.
1472 Specify the expression. */
1475 display_command (char *exp
, int from_tty
)
1477 struct format_data fmt
;
1478 struct expression
*expr
;
1479 struct display
*new;
1483 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1485 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1486 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1500 fmt
= decode_format (&exp
, 0, 0);
1501 if (fmt
.size
&& fmt
.format
== 0)
1503 if (fmt
.format
== 'i' || fmt
.format
== 's')
1514 innermost_block
= NULL
;
1515 expr
= parse_expression (exp
);
1517 new = (struct display
*) xmalloc (sizeof (struct display
));
1519 new->exp_string
= xstrdup (exp
);
1521 new->block
= innermost_block
;
1522 new->pspace
= current_program_space
;
1523 new->next
= display_chain
;
1524 new->number
= ++display_number
;
1527 display_chain
= new;
1529 if (from_tty
&& target_has_execution
)
1530 do_one_display (new);
1537 free_display (struct display
*d
)
1539 xfree (d
->exp_string
);
1544 /* Clear out the display_chain. Done when new symtabs are loaded,
1545 since this invalidates the types stored in many expressions. */
1548 clear_displays (void)
1552 while ((d
= display_chain
) != NULL
)
1554 display_chain
= d
->next
;
1559 /* Delete the auto-display DISPLAY. */
1562 delete_display (struct display
*display
)
1566 gdb_assert (display
!= NULL
);
1568 if (display_chain
== display
)
1569 display_chain
= display
->next
;
1572 if (d
->next
== display
)
1574 d
->next
= display
->next
;
1578 free_display (display
);
1581 /* Call FUNCTION on each of the displays whose numbers are given in
1582 ARGS. DATA is passed unmodified to FUNCTION. */
1585 map_display_numbers (char *args
,
1586 void (*function
) (struct display
*,
1590 struct get_number_or_range_state state
;
1594 error_no_arg (_("one or more display numbers"));
1596 init_number_or_range (&state
, args
);
1598 while (!state
.finished
)
1600 char *p
= state
.string
;
1602 num
= get_number_or_range (&state
);
1604 warning (_("bad display number at or near '%s'"), p
);
1607 struct display
*d
, *tmp
;
1609 ALL_DISPLAYS_SAFE (d
, tmp
)
1610 if (d
->number
== num
)
1613 printf_unfiltered (_("No display number %d.\n"), num
);
1620 /* Callback for map_display_numbers, that deletes a display. */
1623 do_delete_display (struct display
*d
, void *data
)
1628 /* "undisplay" command. */
1631 undisplay_command (char *args
, int from_tty
)
1635 if (query (_("Delete all auto-display expressions? ")))
1641 map_display_numbers (args
, do_delete_display
, NULL
);
1645 /* Display a single auto-display.
1646 Do nothing if the display cannot be printed in the current context,
1647 or if the display is disabled. */
1650 do_one_display (struct display
*d
)
1652 struct cleanup
*old_chain
;
1653 int within_current_scope
;
1655 if (d
->enabled_p
== 0)
1658 /* The expression carries the architecture that was used at parse time.
1659 This is a problem if the expression depends on architecture features
1660 (e.g. register numbers), and the current architecture is now different.
1661 For example, a display statement like "display/i $pc" is expected to
1662 display the PC register of the current architecture, not the arch at
1663 the time the display command was given. Therefore, we re-parse the
1664 expression if the current architecture has changed. */
1665 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1674 volatile struct gdb_exception ex
;
1676 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1678 innermost_block
= NULL
;
1679 d
->exp
= parse_expression (d
->exp_string
);
1680 d
->block
= innermost_block
;
1684 /* Can't re-parse the expression. Disable this display item. */
1686 warning (_("Unable to display \"%s\": %s"),
1687 d
->exp_string
, ex
.message
);
1694 if (d
->pspace
== current_program_space
)
1695 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1697 within_current_scope
= 0;
1700 within_current_scope
= 1;
1701 if (!within_current_scope
)
1704 old_chain
= make_cleanup_restore_integer (¤t_display_number
);
1705 current_display_number
= d
->number
;
1707 annotate_display_begin ();
1708 printf_filtered ("%d", d
->number
);
1709 annotate_display_number_end ();
1710 printf_filtered (": ");
1713 volatile struct gdb_exception ex
;
1715 annotate_display_format ();
1717 printf_filtered ("x/");
1718 if (d
->format
.count
!= 1)
1719 printf_filtered ("%d", d
->format
.count
);
1720 printf_filtered ("%c", d
->format
.format
);
1721 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1722 printf_filtered ("%c", d
->format
.size
);
1723 printf_filtered (" ");
1725 annotate_display_expression ();
1727 puts_filtered (d
->exp_string
);
1728 annotate_display_expression_end ();
1730 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1731 printf_filtered ("\n");
1733 printf_filtered (" ");
1735 annotate_display_value ();
1737 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1742 val
= evaluate_expression (d
->exp
);
1743 addr
= value_as_address (val
);
1744 if (d
->format
.format
== 'i')
1745 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1746 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1749 fprintf_filtered (gdb_stdout
, _("<error: %s>\n"), ex
.message
);
1753 struct value_print_options opts
;
1754 volatile struct gdb_exception ex
;
1756 annotate_display_format ();
1758 if (d
->format
.format
)
1759 printf_filtered ("/%c ", d
->format
.format
);
1761 annotate_display_expression ();
1763 puts_filtered (d
->exp_string
);
1764 annotate_display_expression_end ();
1766 printf_filtered (" = ");
1768 annotate_display_expression ();
1770 get_formatted_print_options (&opts
, d
->format
.format
);
1771 opts
.raw
= d
->format
.raw
;
1773 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1777 val
= evaluate_expression (d
->exp
);
1778 print_formatted (val
, d
->format
.size
, &opts
, gdb_stdout
);
1781 fprintf_filtered (gdb_stdout
, _("<error: %s>"), ex
.message
);
1782 printf_filtered ("\n");
1785 annotate_display_end ();
1787 gdb_flush (gdb_stdout
);
1788 do_cleanups (old_chain
);
1791 /* Display all of the values on the auto-display chain which can be
1792 evaluated in the current scope. */
1799 for (d
= display_chain
; d
; d
= d
->next
)
1803 /* Delete the auto-display which we were in the process of displaying.
1804 This is done when there is an error or a signal. */
1807 disable_display (int num
)
1811 for (d
= display_chain
; d
; d
= d
->next
)
1812 if (d
->number
== num
)
1817 printf_unfiltered (_("No display number %d.\n"), num
);
1821 disable_current_display (void)
1823 if (current_display_number
>= 0)
1825 disable_display (current_display_number
);
1826 fprintf_unfiltered (gdb_stderr
,
1827 _("Disabling display %d to "
1828 "avoid infinite recursion.\n"),
1829 current_display_number
);
1831 current_display_number
= -1;
1835 display_info (char *ignore
, int from_tty
)
1840 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1842 printf_filtered (_("Auto-display expressions now in effect:\n\
1843 Num Enb Expression\n"));
1845 for (d
= display_chain
; d
; d
= d
->next
)
1847 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1849 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1851 else if (d
->format
.format
)
1852 printf_filtered ("/%c ", d
->format
.format
);
1853 puts_filtered (d
->exp_string
);
1854 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1855 printf_filtered (_(" (cannot be evaluated in the current context)"));
1856 printf_filtered ("\n");
1857 gdb_flush (gdb_stdout
);
1861 /* Callback fo map_display_numbers, that enables or disables the
1862 passed in display D. */
1865 do_enable_disable_display (struct display
*d
, void *data
)
1867 d
->enabled_p
= *(int *) data
;
1870 /* Implamentation of both the "disable display" and "enable display"
1871 commands. ENABLE decides what to do. */
1874 enable_disable_display_command (char *args
, int from_tty
, int enable
)
1881 d
->enabled_p
= enable
;
1885 map_display_numbers (args
, do_enable_disable_display
, &enable
);
1888 /* The "enable display" command. */
1891 enable_display_command (char *args
, int from_tty
)
1893 enable_disable_display_command (args
, from_tty
, 1);
1896 /* The "disable display" command. */
1899 disable_display_command (char *args
, int from_tty
)
1901 enable_disable_display_command (args
, from_tty
, 0);
1904 /* display_chain items point to blocks and expressions. Some expressions in
1905 turn may point to symbols.
1906 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1907 obstack_free'd when a shared library is unloaded.
1908 Clear pointers that are about to become dangling.
1909 Both .exp and .block fields will be restored next time we need to display
1910 an item by re-parsing .exp_string field in the new execution context. */
1913 clear_dangling_display_expressions (struct so_list
*solib
)
1915 struct objfile
*objfile
= solib
->objfile
;
1918 /* With no symbol file we cannot have a block or expression from it. */
1919 if (objfile
== NULL
)
1921 if (objfile
->separate_debug_objfile_backlink
)
1922 objfile
= objfile
->separate_debug_objfile_backlink
;
1923 gdb_assert (objfile
->pspace
== solib
->pspace
);
1925 for (d
= display_chain
; d
!= NULL
; d
= d
->next
)
1927 if (d
->pspace
!= solib
->pspace
)
1930 if (lookup_objfile_from_block (d
->block
) == objfile
1931 || (d
->exp
&& exp_uses_objfile (d
->exp
, objfile
)))
1941 /* Print the value in stack frame FRAME of a variable specified by a
1942 struct symbol. NAME is the name to print; if NULL then VAR's print
1943 name will be used. STREAM is the ui_file on which to print the
1944 value. INDENT specifies the number of indent levels to print
1945 before printing the variable name.
1947 This function invalidates FRAME. */
1950 print_variable_and_value (const char *name
, struct symbol
*var
,
1951 struct frame_info
*frame
,
1952 struct ui_file
*stream
, int indent
)
1954 volatile struct gdb_exception except
;
1957 name
= SYMBOL_PRINT_NAME (var
);
1959 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1960 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1963 struct value_print_options opts
;
1965 val
= read_var_value (var
, frame
);
1966 get_user_print_options (&opts
);
1968 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1970 /* common_val_print invalidates FRAME when a pretty printer calls inferior
1974 if (except
.reason
< 0)
1975 fprintf_filtered(stream
, "<error reading variable %s (%s)>", name
,
1977 fprintf_filtered (stream
, "\n");
1980 /* printf "printf format string" ARG to STREAM. */
1983 ui_printf (char *arg
, struct ui_file
*stream
)
1985 struct format_piece
*fpieces
;
1987 struct value
**val_args
;
1988 int allocated_args
= 20;
1989 struct cleanup
*old_cleanups
;
1991 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1992 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1995 error_no_arg (_("format-control string and values to print"));
1997 s
= skip_spaces (s
);
1999 /* A format string should follow, enveloped in double quotes. */
2001 error (_("Bad format string, missing '\"'."));
2003 fpieces
= parse_format_string (&s
);
2005 make_cleanup (free_format_pieces_cleanup
, &fpieces
);
2008 error (_("Bad format string, non-terminated '\"'."));
2010 s
= skip_spaces (s
);
2012 if (*s
!= ',' && *s
!= 0)
2013 error (_("Invalid argument syntax"));
2017 s
= skip_spaces (s
);
2023 char *current_substring
;
2026 for (fr
= 0; fpieces
[fr
].string
!= NULL
; fr
++)
2027 if (fpieces
[fr
].argclass
!= literal_piece
)
2030 /* Now, parse all arguments and evaluate them.
2031 Store the VALUEs in VAL_ARGS. */
2037 if (nargs
== allocated_args
)
2038 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2039 (allocated_args
*= 2)
2040 * sizeof (struct value
*));
2042 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2050 if (nargs
!= nargs_wanted
)
2051 error (_("Wrong number of arguments for specified format-string"));
2053 /* Now actually print them. */
2055 for (fr
= 0; fpieces
[fr
].string
!= NULL
; fr
++)
2057 current_substring
= fpieces
[fr
].string
;
2058 switch (fpieces
[fr
].argclass
)
2066 tem
= value_as_address (val_args
[i
]);
2068 /* This is a %s argument. Find the length of the string. */
2074 read_memory (tem
+ j
, &c
, 1);
2079 /* Copy the string contents into a string inside GDB. */
2080 str
= (gdb_byte
*) alloca (j
+ 1);
2082 read_memory (tem
, str
, j
);
2085 fprintf_filtered (stream
, current_substring
, (char *) str
);
2088 case wide_string_arg
:
2093 struct gdbarch
*gdbarch
2094 = get_type_arch (value_type (val_args
[i
]));
2095 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2096 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2097 "wchar_t", NULL
, 0);
2098 int wcwidth
= TYPE_LENGTH (wctype
);
2099 gdb_byte
*buf
= alloca (wcwidth
);
2100 struct obstack output
;
2101 struct cleanup
*inner_cleanup
;
2103 tem
= value_as_address (val_args
[i
]);
2105 /* This is a %s argument. Find the length of the string. */
2106 for (j
= 0;; j
+= wcwidth
)
2109 read_memory (tem
+ j
, buf
, wcwidth
);
2110 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2114 /* Copy the string contents into a string inside GDB. */
2115 str
= (gdb_byte
*) alloca (j
+ wcwidth
);
2117 read_memory (tem
, str
, j
);
2118 memset (&str
[j
], 0, wcwidth
);
2120 obstack_init (&output
);
2121 inner_cleanup
= make_cleanup_obstack_free (&output
);
2123 convert_between_encodings (target_wide_charset (gdbarch
),
2126 &output
, translit_char
);
2127 obstack_grow_str0 (&output
, "");
2129 fprintf_filtered (stream
, current_substring
,
2130 obstack_base (&output
));
2131 do_cleanups (inner_cleanup
);
2136 struct gdbarch
*gdbarch
2137 = get_type_arch (value_type (val_args
[i
]));
2138 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2139 "wchar_t", NULL
, 0);
2140 struct type
*valtype
;
2141 struct obstack output
;
2142 struct cleanup
*inner_cleanup
;
2143 const gdb_byte
*bytes
;
2145 valtype
= value_type (val_args
[i
]);
2146 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2147 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2148 error (_("expected wchar_t argument for %%lc"));
2150 bytes
= value_contents (val_args
[i
]);
2152 obstack_init (&output
);
2153 inner_cleanup
= make_cleanup_obstack_free (&output
);
2155 convert_between_encodings (target_wide_charset (gdbarch
),
2157 bytes
, TYPE_LENGTH (valtype
),
2158 TYPE_LENGTH (valtype
),
2159 &output
, translit_char
);
2160 obstack_grow_str0 (&output
, "");
2162 fprintf_filtered (stream
, current_substring
,
2163 obstack_base (&output
));
2164 do_cleanups (inner_cleanup
);
2169 struct type
*type
= value_type (val_args
[i
]);
2173 /* If format string wants a float, unchecked-convert the value
2174 to floating point of the same size. */
2175 type
= float_type_from_length (type
);
2176 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2178 error (_("Invalid floating value found in program."));
2180 fprintf_filtered (stream
, current_substring
, (double) val
);
2183 case long_double_arg
:
2184 #ifdef HAVE_LONG_DOUBLE
2186 struct type
*type
= value_type (val_args
[i
]);
2190 /* If format string wants a float, unchecked-convert the value
2191 to floating point of the same size. */
2192 type
= float_type_from_length (type
);
2193 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2195 error (_("Invalid floating value found in program."));
2197 fprintf_filtered (stream
, current_substring
,
2202 error (_("long double not supported in printf"));
2205 #ifdef PRINTF_HAS_LONG_LONG
2207 long long val
= value_as_long (val_args
[i
]);
2209 fprintf_filtered (stream
, current_substring
, val
);
2213 error (_("long long not supported in printf"));
2217 int val
= value_as_long (val_args
[i
]);
2219 fprintf_filtered (stream
, current_substring
, val
);
2224 long val
= value_as_long (val_args
[i
]);
2226 fprintf_filtered (stream
, current_substring
, val
);
2230 /* Handles decimal floating values. */
2233 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2235 #if defined (PRINTF_HAS_DECFLOAT)
2236 /* If we have native support for Decimal floating
2237 printing, handle it here. */
2238 fprintf_filtered (stream
, current_substring
, param_ptr
);
2241 /* As a workaround until vasprintf has native support for DFP
2242 we convert the DFP values to string and print them using
2243 the %s format specifier. */
2246 int nnull_chars
= 0;
2248 /* Parameter data. */
2249 struct type
*param_type
= value_type (val_args
[i
]);
2250 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2251 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2253 /* DFP output data. */
2254 struct value
*dfp_value
= NULL
;
2258 struct type
*dfp_type
= NULL
;
2259 char decstr
[MAX_DECIMAL_STRING
];
2261 /* Points to the end of the string so that we can go back
2262 and check for DFP length modifiers. */
2263 eos
= current_substring
+ strlen (current_substring
);
2265 /* Look for the float/double format specifier. */
2266 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2267 && *eos
!= 'g' && *eos
!= 'G')
2272 /* Search for the '%' char and extract the size and type of
2273 the output decimal value based on its modifiers
2274 (%Hf, %Df, %DDf). */
2275 while (*--sos
!= '%')
2280 dfp_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2282 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2285 dfp_type
= builtin_type (gdbarch
)->builtin_declong
;
2291 dfp_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2295 /* Replace %Hf, %Df and %DDf with %s's. */
2298 /* Go through the whole format string and pull the correct
2299 number of chars back to compensate for the change in the
2300 format specifier. */
2301 while (nnull_chars
< nargs
- i
)
2309 /* Conversion between different DFP types. */
2310 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2311 decimal_convert (param_ptr
, TYPE_LENGTH (param_type
),
2312 byte_order
, dec
, dfp_len
, byte_order
);
2314 /* If this is a non-trivial conversion, just output 0.
2315 A correct converted value can be displayed by explicitly
2316 casting to a DFP type. */
2317 decimal_from_string (dec
, dfp_len
, byte_order
, "0");
2319 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2321 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2323 decimal_to_string (dfp_ptr
, dfp_len
, byte_order
, decstr
);
2325 /* Print the DFP value. */
2326 fprintf_filtered (stream
, current_substring
, decstr
);
2334 /* We avoid the host's %p because pointers are too
2335 likely to be the wrong size. The only interesting
2336 modifier for %p is a width; extract that, and then
2337 handle %p as glibc would: %#x or a literal "(nil)". */
2339 char *p
, *fmt
, *fmt_p
;
2340 #ifdef PRINTF_HAS_LONG_LONG
2341 long long val
= value_as_long (val_args
[i
]);
2343 long val
= value_as_long (val_args
[i
]);
2346 fmt
= alloca (strlen (current_substring
) + 5);
2348 /* Copy up to the leading %. */
2349 p
= current_substring
;
2353 int is_percent
= (*p
== '%');
2368 /* Copy any width. */
2369 while (*p
>= '0' && *p
< '9')
2372 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2375 #ifdef PRINTF_HAS_LONG_LONG
2381 fprintf_filtered (stream
, fmt
, val
);
2387 fprintf_filtered (stream
, fmt
, "(nil)");
2393 /* Print a portion of the format string that has no
2394 directives. Note that this will not include any
2395 ordinary %-specs, but it might include "%%". That is
2396 why we use printf_filtered and not puts_filtered here.
2397 Also, we pass a dummy argument because some platforms
2398 have modified GCC to include -Wformat-security by
2399 default, which will warn here if there is no
2401 fprintf_filtered (stream
, current_substring
, 0);
2404 internal_error (__FILE__
, __LINE__
,
2405 _("failed internal consistency check"));
2407 /* Maybe advance to the next argument. */
2408 if (fpieces
[fr
].argclass
!= literal_piece
)
2412 do_cleanups (old_cleanups
);
2415 /* Implement the "printf" command. */
2418 printf_command (char *arg
, int from_tty
)
2420 ui_printf (arg
, gdb_stdout
);
2423 /* Implement the "eval" command. */
2426 eval_command (char *arg
, int from_tty
)
2428 struct ui_file
*ui_out
= mem_fileopen ();
2429 struct cleanup
*cleanups
= make_cleanup_ui_file_delete (ui_out
);
2432 ui_printf (arg
, ui_out
);
2434 expanded
= ui_file_xstrdup (ui_out
, NULL
);
2435 make_cleanup (xfree
, expanded
);
2437 execute_command (expanded
, from_tty
);
2439 do_cleanups (cleanups
);
2443 _initialize_printcmd (void)
2445 struct cmd_list_element
*c
;
2447 current_display_number
= -1;
2449 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2451 add_info ("address", address_info
,
2452 _("Describe where symbol SYM is stored."));
2454 add_info ("symbol", sym_info
, _("\
2455 Describe what symbol is at location ADDR.\n\
2456 Only for symbols with fixed locations (global or static scope)."));
2458 add_com ("x", class_vars
, x_command
, _("\
2459 Examine memory: x/FMT ADDRESS.\n\
2460 ADDRESS is an expression for the memory address to examine.\n\
2461 FMT is a repeat count followed by a format letter and a size letter.\n\
2462 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2463 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2464 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2465 The specified number of objects of the specified size are printed\n\
2466 according to the format.\n\n\
2467 Defaults for format and size letters are those previously used.\n\
2468 Default count is 1. Default address is following last thing printed\n\
2469 with this command or \"print\"."));
2472 add_com ("whereis", class_vars
, whereis_command
,
2473 _("Print line number and file of definition of variable."));
2476 add_info ("display", display_info
, _("\
2477 Expressions to display when program stops, with code numbers."));
2479 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2480 Cancel some expressions to be displayed when program stops.\n\
2481 Arguments are the code numbers of the expressions to stop displaying.\n\
2482 No argument means cancel all automatic-display expressions.\n\
2483 \"delete display\" has the same effect as this command.\n\
2484 Do \"info display\" to see current list of code numbers."),
2487 add_com ("display", class_vars
, display_command
, _("\
2488 Print value of expression EXP each time the program stops.\n\
2489 /FMT may be used before EXP as in the \"print\" command.\n\
2490 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2491 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2492 and examining is done as in the \"x\" command.\n\n\
2493 With no argument, display all currently requested auto-display expressions.\n\
2494 Use \"undisplay\" to cancel display requests previously made."));
2496 add_cmd ("display", class_vars
, enable_display_command
, _("\
2497 Enable some expressions to be displayed when program stops.\n\
2498 Arguments are the code numbers of the expressions to resume displaying.\n\
2499 No argument means enable all automatic-display expressions.\n\
2500 Do \"info display\" to see current list of code numbers."), &enablelist
);
2502 add_cmd ("display", class_vars
, disable_display_command
, _("\
2503 Disable some expressions to be displayed when program stops.\n\
2504 Arguments are the code numbers of the expressions to stop displaying.\n\
2505 No argument means disable all automatic-display expressions.\n\
2506 Do \"info display\" to see current list of code numbers."), &disablelist
);
2508 add_cmd ("display", class_vars
, undisplay_command
, _("\
2509 Cancel some expressions to be displayed when program stops.\n\
2510 Arguments are the code numbers of the expressions to stop displaying.\n\
2511 No argument means cancel all automatic-display expressions.\n\
2512 Do \"info display\" to see current list of code numbers."), &deletelist
);
2514 add_com ("printf", class_vars
, printf_command
, _("\
2515 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2516 This is useful for formatted output in user-defined commands."));
2518 add_com ("output", class_vars
, output_command
, _("\
2519 Like \"print\" but don't put in value history and don't print newline.\n\
2520 This is useful in user-defined commands."));
2522 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2523 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2524 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2525 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2526 with $), a register (a few standard names starting with $), or an actual\n\
2527 variable in the program being debugged. EXP is any valid expression.\n\
2528 Use \"set variable\" for variables with names identical to set subcommands.\n\
2530 With a subcommand, this command modifies parts of the gdb environment.\n\
2531 You can see these environment settings with the \"show\" command."),
2532 &setlist
, "set ", 1, &cmdlist
);
2534 add_com ("assign", class_vars
, set_command
, _("\
2535 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2536 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2537 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2538 with $), a register (a few standard names starting with $), or an actual\n\
2539 variable in the program being debugged. EXP is any valid expression.\n\
2540 Use \"set variable\" for variables with names identical to set subcommands.\n\
2541 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2542 You can see these environment settings with the \"show\" command."));
2544 /* "call" is the same as "set", but handy for dbx users to call fns. */
2545 c
= add_com ("call", class_vars
, call_command
, _("\
2546 Call a function in the program.\n\
2547 The argument is the function name and arguments, in the notation of the\n\
2548 current working language. The result is printed and saved in the value\n\
2549 history, if it is not void."));
2550 set_cmd_completer (c
, expression_completer
);
2552 add_cmd ("variable", class_vars
, set_command
, _("\
2553 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2554 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2555 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2556 with $), a register (a few standard names starting with $), or an actual\n\
2557 variable in the program being debugged. EXP is any valid expression.\n\
2558 This may usually be abbreviated to simply \"set\"."),
2561 c
= add_com ("print", class_vars
, print_command
, _("\
2562 Print value of expression EXP.\n\
2563 Variables accessible are those of the lexical environment of the selected\n\
2564 stack frame, plus all those whose scope is global or an entire file.\n\
2566 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2567 $$NUM refers to NUM'th value back from the last one.\n\
2568 Names starting with $ refer to registers (with the values they would have\n\
2569 if the program were to return to the stack frame now selected, restoring\n\
2570 all registers saved by frames farther in) or else to debugger\n\
2571 \"convenience\" variables (any such name not a known register).\n\
2572 Use assignment expressions to give values to convenience variables.\n\
2574 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2575 @ is a binary operator for treating consecutive data objects\n\
2576 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2577 element is FOO, whose second element is stored in the space following\n\
2578 where FOO is stored, etc. FOO must be an expression whose value\n\
2579 resides in memory.\n\
2581 EXP may be preceded with /FMT, where FMT is a format letter\n\
2582 but no count or size letter (see \"x\" command)."));
2583 set_cmd_completer (c
, expression_completer
);
2584 add_com_alias ("p", "print", class_vars
, 1);
2585 add_com_alias ("inspect", "print", class_vars
, 1);
2587 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2588 &max_symbolic_offset
, _("\
2589 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2590 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2592 show_max_symbolic_offset
,
2593 &setprintlist
, &showprintlist
);
2594 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2595 &print_symbol_filename
, _("\
2596 Set printing of source filename and line number with <symbol>."), _("\
2597 Show printing of source filename and line number with <symbol>."), NULL
,
2599 show_print_symbol_filename
,
2600 &setprintlist
, &showprintlist
);
2602 add_com ("eval", no_class
, eval_command
, _("\
2603 Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2604 a command line, and call it."));