1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986-2020 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/>. */
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 */
42 #include "target-float.h"
43 #include "observable.h"
45 #include "parser-defs.h"
47 #include "arch-utils.h"
48 #include "cli/cli-utils.h"
49 #include "cli/cli-option.h"
50 #include "cli/cli-script.h"
51 #include "cli/cli-style.h"
52 #include "gdbsupport/format.h"
54 #include "gdbsupport/byte-vector.h"
55 #include "gdbsupport/gdb_optional.h"
57 /* Last specified output format. */
59 static char last_format
= 0;
61 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
63 static char last_size
= 'w';
65 /* Last specified count for the 'x' command. */
67 static int last_count
;
69 /* Default address to examine next, and associated architecture. */
71 static struct gdbarch
*next_gdbarch
;
72 static CORE_ADDR next_address
;
74 /* Number of delay instructions following current disassembled insn. */
76 static int branch_delay_insns
;
78 /* Last address examined. */
80 static CORE_ADDR last_examine_address
;
82 /* Contents of last address examined.
83 This is not valid past the end of the `x' command! */
85 static value_ref_ptr last_examine_value
;
87 /* Largest offset between a symbolic value and an address, that will be
88 printed as `0x1234 <symbol+offset>'. */
90 static unsigned int max_symbolic_offset
= UINT_MAX
;
92 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
93 struct cmd_list_element
*c
, const char *value
)
95 fprintf_filtered (file
,
96 _("The largest offset that will be "
97 "printed in <symbol+1234> form is %s.\n"),
101 /* Append the source filename and linenumber of the symbol when
102 printing a symbolic value as `<symbol at filename:linenum>' if set. */
103 static bool print_symbol_filename
= false;
105 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
106 struct cmd_list_element
*c
, const char *value
)
108 fprintf_filtered (file
, _("Printing of source filename and "
109 "line number with <symbol> is %s.\n"),
113 /* Number of auto-display expression currently being displayed.
114 So that we can disable it if we get a signal within it.
115 -1 when not doing one. */
117 static int current_display_number
;
119 /* Last allocated display number. */
121 static int display_number
;
125 display (const char *exp_string_
, expression_up
&&exp_
,
126 const struct format_data
&format_
, struct program_space
*pspace_
,
127 const struct block
*block_
)
128 : exp_string (exp_string_
),
129 exp (std::move (exp_
)),
130 number (++display_number
),
138 /* The expression as the user typed it. */
139 std::string exp_string
;
141 /* Expression to be evaluated and displayed. */
144 /* Item number of this auto-display item. */
147 /* Display format specified. */
148 struct format_data format
;
150 /* Program space associated with `block'. */
151 struct program_space
*pspace
;
153 /* Innermost block required by this expression when evaluated. */
154 const struct block
*block
;
156 /* Status of this display (enabled or disabled). */
160 /* Expressions whose values should be displayed automatically each
161 time the program stops. */
163 static std::vector
<std::unique_ptr
<struct display
>> all_displays
;
165 /* Prototypes for local functions. */
167 static void do_one_display (struct display
*);
170 /* Decode a format specification. *STRING_PTR should point to it.
171 OFORMAT and OSIZE are used as defaults for the format and size
172 if none are given in the format specification.
173 If OSIZE is zero, then the size field of the returned value
174 should be set only if a size is explicitly specified by the
176 The structure returned describes all the data
177 found in the specification. In addition, *STRING_PTR is advanced
178 past the specification and past all whitespace following it. */
180 static struct format_data
181 decode_format (const char **string_ptr
, int oformat
, int osize
)
183 struct format_data val
;
184 const char *p
= *string_ptr
;
196 if (*p
>= '0' && *p
<= '9')
197 val
.count
*= atoi (p
);
198 while (*p
>= '0' && *p
<= '9')
201 /* Now process size or format letters that follow. */
205 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
212 else if (*p
>= 'a' && *p
<= 'z')
218 *string_ptr
= skip_spaces (p
);
220 /* Set defaults for format and size if not specified. */
221 if (val
.format
== '?')
225 /* Neither has been specified. */
226 val
.format
= oformat
;
230 /* If a size is specified, any format makes a reasonable
231 default except 'i'. */
232 val
.format
= oformat
== 'i' ? 'x' : oformat
;
234 else if (val
.size
== '?')
238 /* Pick the appropriate size for an address. This is deferred
239 until do_examine when we know the actual architecture to use.
240 A special size value of 'a' is used to indicate this case. */
241 val
.size
= osize
? 'a' : osize
;
244 /* Floating point has to be word or giantword. */
245 if (osize
== 'w' || osize
== 'g')
248 /* Default it to giantword if the last used size is not
250 val
.size
= osize
? 'g' : osize
;
253 /* Characters default to one byte. */
254 val
.size
= osize
? 'b' : osize
;
257 /* Display strings with byte size chars unless explicitly
263 /* The default is the size most recently specified. */
270 /* Print value VAL on stream according to OPTIONS.
271 Do not end with a newline.
272 SIZE is the letter for the size of datum being printed.
273 This is used to pad hex numbers so they line up. SIZE is 0
274 for print / output and set for examine. */
277 print_formatted (struct value
*val
, int size
,
278 const struct value_print_options
*options
,
279 struct ui_file
*stream
)
281 struct type
*type
= check_typedef (value_type (val
));
282 int len
= TYPE_LENGTH (type
);
284 if (VALUE_LVAL (val
) == lval_memory
)
285 next_address
= value_address (val
) + len
;
289 switch (options
->format
)
293 struct type
*elttype
= value_type (val
);
295 next_address
= (value_address (val
)
296 + val_print_string (elttype
, NULL
,
297 value_address (val
), -1,
298 stream
, options
) * len
);
303 /* We often wrap here if there are long symbolic names. */
305 next_address
= (value_address (val
)
306 + gdb_print_insn (get_type_arch (type
),
307 value_address (val
), stream
,
308 &branch_delay_insns
));
313 if (options
->format
== 0 || options
->format
== 's'
314 || type
->code () == TYPE_CODE_VOID
315 || type
->code () == TYPE_CODE_REF
316 || type
->code () == TYPE_CODE_ARRAY
317 || type
->code () == TYPE_CODE_STRING
318 || type
->code () == TYPE_CODE_STRUCT
319 || type
->code () == TYPE_CODE_UNION
320 || type
->code () == TYPE_CODE_NAMESPACE
)
321 value_print (val
, stream
, options
);
323 /* User specified format, so don't look to the type to tell us
325 value_print_scalar_formatted (val
, options
, size
, stream
);
328 /* Return builtin floating point type of same length as TYPE.
329 If no such type is found, return TYPE itself. */
331 float_type_from_length (struct type
*type
)
333 struct gdbarch
*gdbarch
= get_type_arch (type
);
334 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
336 if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_float
))
337 type
= builtin
->builtin_float
;
338 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_double
))
339 type
= builtin
->builtin_double
;
340 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_long_double
))
341 type
= builtin
->builtin_long_double
;
346 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
347 according to OPTIONS and SIZE on STREAM. Formats s and i are not
348 supported at this level. */
351 print_scalar_formatted (const gdb_byte
*valaddr
, struct type
*type
,
352 const struct value_print_options
*options
,
353 int size
, struct ui_file
*stream
)
355 struct gdbarch
*gdbarch
= get_type_arch (type
);
356 unsigned int len
= TYPE_LENGTH (type
);
357 enum bfd_endian byte_order
= type_byte_order (type
);
359 /* String printing should go through val_print_scalar_formatted. */
360 gdb_assert (options
->format
!= 's');
362 /* If the value is a pointer, and pointers and addresses are not the
363 same, then at this point, the value's length (in target bytes) is
364 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
365 if (type
->code () == TYPE_CODE_PTR
)
366 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
368 /* If we are printing it as unsigned, truncate it in case it is actually
369 a negative signed value (e.g. "print/u (short)-1" should print 65535
370 (if shorts are 16 bits) instead of 4294967295). */
371 if (options
->format
!= 'c'
372 && (options
->format
!= 'd' || type
->is_unsigned ()))
374 if (len
< TYPE_LENGTH (type
) && byte_order
== BFD_ENDIAN_BIG
)
375 valaddr
+= TYPE_LENGTH (type
) - len
;
378 /* Allow LEN == 0, and in this case, don't assume that VALADDR is
380 const gdb_byte zero
= 0;
387 if (size
!= 0 && (options
->format
== 'x' || options
->format
== 't'))
389 /* Truncate to fit. */
406 error (_("Undefined output size \"%c\"."), size
);
408 if (newlen
< len
&& byte_order
== BFD_ENDIAN_BIG
)
409 valaddr
+= len
- newlen
;
413 /* Historically gdb has printed floats by first casting them to a
414 long, and then printing the long. PR cli/16242 suggests changing
415 this to using C-style hex float format.
417 Biased range types and sub-word scalar types must also be handled
418 here; the value is correctly computed by unpack_long. */
419 gdb::byte_vector converted_bytes
;
420 /* Some cases below will unpack the value again. In the biased
421 range case, we want to avoid this, so we store the unpacked value
422 here for possible use later. */
423 gdb::optional
<LONGEST
> val_long
;
424 if (((type
->code () == TYPE_CODE_FLT
425 || is_fixed_point_type (type
))
426 && (options
->format
== 'o'
427 || options
->format
== 'x'
428 || options
->format
== 't'
429 || options
->format
== 'z'
430 || options
->format
== 'd'
431 || options
->format
== 'u'))
432 || (type
->code () == TYPE_CODE_RANGE
&& type
->bounds ()->bias
!= 0)
433 || type
->bit_size_differs_p ())
435 val_long
.emplace (unpack_long (type
, valaddr
));
436 converted_bytes
.resize (TYPE_LENGTH (type
));
437 store_signed_integer (converted_bytes
.data (), TYPE_LENGTH (type
),
438 byte_order
, *val_long
);
439 valaddr
= converted_bytes
.data ();
442 /* Printing a non-float type as 'f' will interpret the data as if it were
443 of a floating-point type of the same length, if that exists. Otherwise,
444 the data is printed as integer. */
445 char format
= options
->format
;
446 if (format
== 'f' && type
->code () != TYPE_CODE_FLT
)
448 type
= float_type_from_length (type
);
449 if (type
->code () != TYPE_CODE_FLT
)
456 print_octal_chars (stream
, valaddr
, len
, byte_order
);
459 print_decimal_chars (stream
, valaddr
, len
, true, byte_order
);
462 print_decimal_chars (stream
, valaddr
, len
, false, byte_order
);
465 if (type
->code () != TYPE_CODE_FLT
)
467 print_decimal_chars (stream
, valaddr
, len
, !type
->is_unsigned (),
473 print_floating (valaddr
, type
, stream
);
477 print_binary_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
480 print_hex_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
483 print_hex_chars (stream
, valaddr
, len
, byte_order
, true);
487 struct value_print_options opts
= *options
;
489 if (!val_long
.has_value ())
490 val_long
.emplace (unpack_long (type
, valaddr
));
493 if (type
->is_unsigned ())
494 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
496 type
= builtin_type (gdbarch
)->builtin_true_char
;
498 value_print (value_from_longest (type
, *val_long
), stream
, &opts
);
504 if (!val_long
.has_value ())
505 val_long
.emplace (unpack_long (type
, valaddr
));
506 print_address (gdbarch
, *val_long
, stream
);
511 error (_("Undefined output format \"%c\"."), format
);
515 /* Specify default address for `x' command.
516 The `info lines' command uses this. */
519 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
521 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
523 next_gdbarch
= gdbarch
;
526 /* Make address available to the user as $_. */
527 set_internalvar (lookup_internalvar ("_"),
528 value_from_pointer (ptr_type
, addr
));
531 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
532 after LEADIN. Print nothing if no symbolic name is found nearby.
533 Optionally also print source file and line number, if available.
534 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
535 or to interpret it as a possible C++ name and convert it back to source
536 form. However note that DO_DEMANGLE can be overridden by the specific
537 settings of the demangle and asm_demangle variables. Returns
538 non-zero if anything was printed; zero otherwise. */
541 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
542 struct ui_file
*stream
,
543 int do_demangle
, const char *leadin
)
545 std::string name
, filename
;
550 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, false, &name
,
551 &offset
, &filename
, &line
, &unmapped
))
554 fputs_filtered (leadin
, stream
);
556 fputs_filtered ("<*", stream
);
558 fputs_filtered ("<", stream
);
559 fputs_styled (name
.c_str (), function_name_style
.style (), stream
);
561 fprintf_filtered (stream
, "%+d", offset
);
563 /* Append source filename and line number if desired. Give specific
564 line # of this addr, if we have it; else line # of the nearest symbol. */
565 if (print_symbol_filename
&& !filename
.empty ())
567 fputs_filtered (line
== -1 ? " in " : " at ", stream
);
568 fputs_styled (filename
.c_str (), file_name_style
.style (), stream
);
570 fprintf_filtered (stream
, ":%d", line
);
573 fputs_filtered ("*>", stream
);
575 fputs_filtered (">", stream
);
580 /* See valprint.h. */
583 build_address_symbolic (struct gdbarch
*gdbarch
,
584 CORE_ADDR addr
, /* IN */
585 bool do_demangle
, /* IN */
586 bool prefer_sym_over_minsym
, /* IN */
587 std::string
*name
, /* OUT */
588 int *offset
, /* OUT */
589 std::string
*filename
, /* OUT */
591 int *unmapped
) /* OUT */
593 struct bound_minimal_symbol msymbol
;
594 struct symbol
*symbol
;
595 CORE_ADDR name_location
= 0;
596 struct obj_section
*section
= NULL
;
597 const char *name_temp
= "";
599 /* Let's say it is mapped (not unmapped). */
602 /* Determine if the address is in an overlay, and whether it is
604 if (overlay_debugging
)
606 section
= find_pc_overlay (addr
);
607 if (pc_in_unmapped_range (addr
, section
))
610 addr
= overlay_mapped_address (addr
, section
);
614 /* Try to find the address in both the symbol table and the minsyms.
615 In most cases, we'll prefer to use the symbol instead of the
616 minsym. However, there are cases (see below) where we'll choose
617 to use the minsym instead. */
619 /* This is defective in the sense that it only finds text symbols. So
620 really this is kind of pointless--we should make sure that the
621 minimal symbols have everything we need (by changing that we could
622 save some memory, but for many debug format--ELF/DWARF or
623 anything/stabs--it would be inconvenient to eliminate those minimal
625 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
626 symbol
= find_pc_sect_function (addr
, section
);
630 /* If this is a function (i.e. a code address), strip out any
631 non-address bits. For instance, display a pointer to the
632 first instruction of a Thumb function as <function>; the
633 second instruction will be <function+2>, even though the
634 pointer is <function+3>. This matches the ISA behavior. */
635 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
637 name_location
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol
));
638 if (do_demangle
|| asm_demangle
)
639 name_temp
= symbol
->print_name ();
641 name_temp
= symbol
->linkage_name ();
644 if (msymbol
.minsym
!= NULL
645 && MSYMBOL_HAS_SIZE (msymbol
.minsym
)
646 && MSYMBOL_SIZE (msymbol
.minsym
) == 0
647 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text
648 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text_gnu_ifunc
649 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_file_text
)
650 msymbol
.minsym
= NULL
;
652 if (msymbol
.minsym
!= NULL
)
654 /* Use the minsym if no symbol is found.
656 Additionally, use the minsym instead of a (found) symbol if
657 the following conditions all hold:
658 1) The prefer_sym_over_minsym flag is false.
659 2) The minsym address is identical to that of the address under
661 3) The symbol address is not identical to that of the address
662 under consideration. */
663 if (symbol
== NULL
||
664 (!prefer_sym_over_minsym
665 && BMSYMBOL_VALUE_ADDRESS (msymbol
) == addr
666 && name_location
!= addr
))
668 /* If this is a function (i.e. a code address), strip out any
669 non-address bits. For instance, display a pointer to the
670 first instruction of a Thumb function as <function>; the
671 second instruction will be <function+2>, even though the
672 pointer is <function+3>. This matches the ISA behavior. */
673 if (MSYMBOL_TYPE (msymbol
.minsym
) == mst_text
674 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_text_gnu_ifunc
675 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_file_text
676 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_solib_trampoline
)
677 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
680 name_location
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
681 if (do_demangle
|| asm_demangle
)
682 name_temp
= msymbol
.minsym
->print_name ();
684 name_temp
= msymbol
.minsym
->linkage_name ();
687 if (symbol
== NULL
&& msymbol
.minsym
== NULL
)
690 /* If the nearest symbol is too far away, don't print anything symbolic. */
692 /* For when CORE_ADDR is larger than unsigned int, we do math in
693 CORE_ADDR. But when we detect unsigned wraparound in the
694 CORE_ADDR math, we ignore this test and print the offset,
695 because addr+max_symbolic_offset has wrapped through the end
696 of the address space back to the beginning, giving bogus comparison. */
697 if (addr
> name_location
+ max_symbolic_offset
698 && name_location
+ max_symbolic_offset
> name_location
)
701 *offset
= (LONGEST
) addr
- name_location
;
705 if (print_symbol_filename
)
707 struct symtab_and_line sal
;
709 sal
= find_pc_sect_line (addr
, section
, 0);
713 *filename
= symtab_to_filename_for_display (sal
.symtab
);
721 /* Print address ADDR symbolically on STREAM.
722 First print it as a number. Then perhaps print
723 <SYMBOL + OFFSET> after the number. */
726 print_address (struct gdbarch
*gdbarch
,
727 CORE_ADDR addr
, struct ui_file
*stream
)
729 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
730 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
733 /* Return a prefix for instruction address:
734 "=> " for current instruction, else " ". */
737 pc_prefix (CORE_ADDR addr
)
739 if (has_stack_frames ())
741 struct frame_info
*frame
;
744 frame
= get_selected_frame (NULL
);
745 if (get_frame_pc_if_available (frame
, &pc
) && pc
== addr
)
751 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
752 controls whether to print the symbolic name "raw" or demangled.
753 Return non-zero if anything was printed; zero otherwise. */
756 print_address_demangle (const struct value_print_options
*opts
,
757 struct gdbarch
*gdbarch
, CORE_ADDR addr
,
758 struct ui_file
*stream
, int do_demangle
)
760 if (opts
->addressprint
)
762 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
763 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
767 return print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
773 /* Find the address of the instruction that is INST_COUNT instructions before
774 the instruction at ADDR.
775 Since some architectures have variable-length instructions, we can't just
776 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
777 number information to locate the nearest known instruction boundary,
778 and disassemble forward from there. If we go out of the symbol range
779 during disassembling, we return the lowest address we've got so far and
780 set the number of instructions read to INST_READ. */
783 find_instruction_backward (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
784 int inst_count
, int *inst_read
)
786 /* The vector PCS is used to store instruction addresses within
788 CORE_ADDR loop_start
, loop_end
, p
;
789 std::vector
<CORE_ADDR
> pcs
;
790 struct symtab_and_line sal
;
793 loop_start
= loop_end
= addr
;
795 /* In each iteration of the outer loop, we get a pc range that ends before
796 LOOP_START, then we count and store every instruction address of the range
797 iterated in the loop.
798 If the number of instructions counted reaches INST_COUNT, return the
799 stored address that is located INST_COUNT instructions back from ADDR.
800 If INST_COUNT is not reached, we subtract the number of counted
801 instructions from INST_COUNT, and go to the next iteration. */
805 sal
= find_pc_sect_line (loop_start
, NULL
, 1);
808 /* We reach here when line info is not available. In this case,
809 we print a message and just exit the loop. The return value
810 is calculated after the loop. */
811 printf_filtered (_("No line number information available "
814 print_address (gdbarch
, loop_start
- 1, gdb_stdout
);
815 printf_filtered ("\n");
819 loop_end
= loop_start
;
822 /* This loop pushes instruction addresses in the range from
823 LOOP_START to LOOP_END. */
824 for (p
= loop_start
; p
< loop_end
;)
827 p
+= gdb_insn_length (gdbarch
, p
);
830 inst_count
-= pcs
.size ();
831 *inst_read
+= pcs
.size ();
833 while (inst_count
> 0);
835 /* After the loop, the vector PCS has instruction addresses of the last
836 source line we processed, and INST_COUNT has a negative value.
837 We return the address at the index of -INST_COUNT in the vector for
839 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
849 find_instruction_backward is called with INST_COUNT = 4 and expected to
850 return 0x4001. When we reach here, INST_COUNT is set to -1 because
851 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
852 4001 is located at the index 1 of the last iterated line (= Line X),
853 which is simply calculated by -INST_COUNT.
854 The case when the length of PCS is 0 means that we reached an area for
855 which line info is not available. In such case, we return LOOP_START,
856 which was the lowest instruction address that had line info. */
857 p
= pcs
.size () > 0 ? pcs
[-inst_count
] : loop_start
;
859 /* INST_READ includes all instruction addresses in a pc range. Need to
860 exclude the beginning part up to the address we're returning. That
861 is, exclude {0x4000} in the example above. */
863 *inst_read
+= inst_count
;
868 /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
869 placing the results in GDB's memory from MYADDR + LEN. Returns
870 a count of the bytes actually read. */
873 read_memory_backward (struct gdbarch
*gdbarch
,
874 CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
877 int nread
; /* Number of bytes actually read. */
879 /* First try a complete read. */
880 errcode
= target_read_memory (memaddr
, myaddr
, len
);
888 /* Loop, reading one byte at a time until we get as much as we can. */
891 for (nread
= 0; nread
< len
; ++nread
)
893 errcode
= target_read_memory (--memaddr
, --myaddr
, 1);
896 /* The read was unsuccessful, so exit the loop. */
897 printf_filtered (_("Cannot access memory at address %s\n"),
898 paddress (gdbarch
, memaddr
));
906 /* Returns true if X (which is LEN bytes wide) is the number zero. */
909 integer_is_zero (const gdb_byte
*x
, int len
)
913 while (i
< len
&& x
[i
] == 0)
918 /* Find the start address of a string in which ADDR is included.
919 Basically we search for '\0' and return the next address,
920 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
921 we stop searching and return the address to print characters as many as
922 PRINT_MAX from the string. */
925 find_string_backward (struct gdbarch
*gdbarch
,
926 CORE_ADDR addr
, int count
, int char_size
,
927 const struct value_print_options
*options
,
928 int *strings_counted
)
930 const int chunk_size
= 0x20;
933 int chars_to_read
= chunk_size
;
934 int chars_counted
= 0;
935 int count_original
= count
;
936 CORE_ADDR string_start_addr
= addr
;
938 gdb_assert (char_size
== 1 || char_size
== 2 || char_size
== 4);
939 gdb::byte_vector
buffer (chars_to_read
* char_size
);
940 while (count
> 0 && read_error
== 0)
944 addr
-= chars_to_read
* char_size
;
945 chars_read
= read_memory_backward (gdbarch
, addr
, buffer
.data (),
946 chars_to_read
* char_size
);
947 chars_read
/= char_size
;
948 read_error
= (chars_read
== chars_to_read
) ? 0 : 1;
949 /* Searching for '\0' from the end of buffer in backward direction. */
950 for (i
= 0; i
< chars_read
&& count
> 0 ; ++i
, ++chars_counted
)
952 int offset
= (chars_to_read
- i
- 1) * char_size
;
954 if (integer_is_zero (&buffer
[offset
], char_size
)
955 || chars_counted
== options
->print_max
)
957 /* Found '\0' or reached print_max. As OFFSET is the offset to
958 '\0', we add CHAR_SIZE to return the start address of
961 string_start_addr
= addr
+ offset
+ char_size
;
967 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
968 *strings_counted
= count_original
- count
;
972 /* In error case, STRING_START_ADDR is pointing to the string that
973 was last successfully loaded. Rewind the partially loaded string. */
974 string_start_addr
-= chars_counted
* char_size
;
977 return string_start_addr
;
980 /* Examine data at address ADDR in format FMT.
981 Fetch it from memory and print on gdb_stdout. */
984 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
989 struct type
*val_type
= NULL
;
992 struct value_print_options opts
;
993 int need_to_update_next_address
= 0;
994 CORE_ADDR addr_rewound
= 0;
999 next_gdbarch
= gdbarch
;
1000 next_address
= addr
;
1002 /* Instruction format implies fetch single bytes
1003 regardless of the specified size.
1004 The case of strings is handled in decode_format, only explicit
1005 size operator are not changed to 'b'. */
1011 /* Pick the appropriate size for an address. */
1012 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
1014 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
1016 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
1019 /* Bad value for gdbarch_ptr_bit. */
1020 internal_error (__FILE__
, __LINE__
,
1021 _("failed internal consistency check"));
1025 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1026 else if (size
== 'h')
1027 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
1028 else if (size
== 'w')
1029 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
1030 else if (size
== 'g')
1031 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
1035 struct type
*char_type
= NULL
;
1037 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1038 if type is not found. */
1040 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
1041 else if (size
== 'w')
1042 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
1044 val_type
= char_type
;
1047 if (size
!= '\0' && size
!= 'b')
1048 warning (_("Unable to display strings with "
1049 "size '%c', using 'b' instead."), size
);
1051 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1060 if (format
== 's' || format
== 'i')
1063 get_formatted_print_options (&opts
, format
);
1067 /* This is the negative repeat count case.
1068 We rewind the address based on the given repeat count and format,
1069 then examine memory from there in forward direction. */
1074 next_address
= find_instruction_backward (gdbarch
, addr
, count
,
1077 else if (format
== 's')
1079 next_address
= find_string_backward (gdbarch
, addr
, count
,
1080 TYPE_LENGTH (val_type
),
1085 next_address
= addr
- count
* TYPE_LENGTH (val_type
);
1088 /* The following call to print_formatted updates next_address in every
1089 iteration. In backward case, we store the start address here
1090 and update next_address with it before exiting the function. */
1091 addr_rewound
= (format
== 's'
1092 ? next_address
- TYPE_LENGTH (val_type
)
1094 need_to_update_next_address
= 1;
1097 /* Print as many objects as specified in COUNT, at most maxelts per line,
1098 with the address of the next one at the start of each line. */
1104 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
1105 print_address (next_gdbarch
, next_address
, gdb_stdout
);
1106 printf_filtered (":");
1111 printf_filtered ("\t");
1112 /* Note that print_formatted sets next_address for the next
1114 last_examine_address
= next_address
;
1116 /* The value to be displayed is not fetched greedily.
1117 Instead, to avoid the possibility of a fetched value not
1118 being used, its retrieval is delayed until the print code
1119 uses it. When examining an instruction stream, the
1120 disassembler will perform its own memory fetch using just
1121 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1122 the disassembler be modified so that LAST_EXAMINE_VALUE
1123 is left with the byte sequence from the last complete
1124 instruction fetched from memory? */
1126 = release_value (value_at_lazy (val_type
, next_address
));
1128 print_formatted (last_examine_value
.get (), size
, &opts
, gdb_stdout
);
1130 /* Display any branch delay slots following the final insn. */
1131 if (format
== 'i' && count
== 1)
1132 count
+= branch_delay_insns
;
1134 printf_filtered ("\n");
1137 if (need_to_update_next_address
)
1138 next_address
= addr_rewound
;
1142 validate_format (struct format_data fmt
, const char *cmdname
)
1145 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
1147 error (_("Item count other than 1 is meaningless in \"%s\" command."),
1149 if (fmt
.format
== 'i')
1150 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
1151 fmt
.format
, cmdname
);
1154 /* Parse print command format string into *OPTS and update *EXPP.
1155 CMDNAME should name the current command. */
1158 print_command_parse_format (const char **expp
, const char *cmdname
,
1159 value_print_options
*opts
)
1161 const char *exp
= *expp
;
1163 /* opts->raw value might already have been set by 'set print raw-values'
1164 or by using 'print -raw-values'.
1165 So, do not set opts->raw to 0, only set it to 1 if /r is given. */
1166 if (exp
&& *exp
== '/')
1171 fmt
= decode_format (&exp
, last_format
, 0);
1172 validate_format (fmt
, cmdname
);
1173 last_format
= fmt
.format
;
1175 opts
->format
= fmt
.format
;
1176 opts
->raw
= opts
->raw
|| fmt
.raw
;
1186 /* See valprint.h. */
1189 print_value (value
*val
, const value_print_options
&opts
)
1191 int histindex
= record_latest_value (val
);
1193 annotate_value_history_begin (histindex
, value_type (val
));
1195 printf_filtered ("$%d = ", histindex
);
1197 annotate_value_history_value ();
1199 print_formatted (val
, 0, &opts
, gdb_stdout
);
1200 printf_filtered ("\n");
1202 annotate_value_history_end ();
1205 /* Implementation of the "print" and "call" commands. */
1208 print_command_1 (const char *args
, int voidprint
)
1211 value_print_options print_opts
;
1213 get_user_print_options (&print_opts
);
1214 /* Override global settings with explicit options, if any. */
1215 auto group
= make_value_print_options_def_group (&print_opts
);
1216 gdb::option::process_options
1217 (&args
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
1219 print_command_parse_format (&args
, "print", &print_opts
);
1221 const char *exp
= args
;
1223 if (exp
!= nullptr && *exp
)
1225 expression_up expr
= parse_expression (exp
);
1226 val
= evaluate_expression (expr
.get ());
1229 val
= access_value_history (0);
1231 if (voidprint
|| (val
&& value_type (val
) &&
1232 value_type (val
)->code () != TYPE_CODE_VOID
))
1233 print_value (val
, print_opts
);
1236 /* See valprint.h. */
1239 print_command_completer (struct cmd_list_element
*ignore
,
1240 completion_tracker
&tracker
,
1241 const char *text
, const char * /*word*/)
1243 const auto group
= make_value_print_options_def_group (nullptr);
1244 if (gdb::option::complete_options
1245 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
))
1248 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1249 expression_completer (ignore
, tracker
, text
, word
);
1253 print_command (const char *exp
, int from_tty
)
1255 print_command_1 (exp
, 1);
1258 /* Same as print, except it doesn't print void results. */
1260 call_command (const char *exp
, int from_tty
)
1262 print_command_1 (exp
, 0);
1265 /* Implementation of the "output" command. */
1268 output_command (const char *exp
, int from_tty
)
1272 struct format_data fmt
;
1273 struct value_print_options opts
;
1278 if (exp
&& *exp
== '/')
1281 fmt
= decode_format (&exp
, 0, 0);
1282 validate_format (fmt
, "output");
1283 format
= fmt
.format
;
1286 expression_up expr
= parse_expression (exp
);
1288 val
= evaluate_expression (expr
.get ());
1290 annotate_value_begin (value_type (val
));
1292 get_formatted_print_options (&opts
, format
);
1294 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1296 annotate_value_end ();
1299 gdb_flush (gdb_stdout
);
1303 set_command (const char *exp
, int from_tty
)
1305 expression_up expr
= parse_expression (exp
);
1307 if (expr
->nelts
>= 1)
1308 switch (expr
->elts
[0].opcode
)
1310 case UNOP_PREINCREMENT
:
1311 case UNOP_POSTINCREMENT
:
1312 case UNOP_PREDECREMENT
:
1313 case UNOP_POSTDECREMENT
:
1315 case BINOP_ASSIGN_MODIFY
:
1320 (_("Expression is not an assignment (and might have no effect)"));
1323 evaluate_expression (expr
.get ());
1327 info_symbol_command (const char *arg
, int from_tty
)
1329 struct minimal_symbol
*msymbol
;
1330 struct obj_section
*osect
;
1331 CORE_ADDR addr
, sect_addr
;
1333 unsigned int offset
;
1336 error_no_arg (_("address"));
1338 addr
= parse_and_eval_address (arg
);
1339 for (objfile
*objfile
: current_program_space
->objfiles ())
1340 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
1342 /* Only process each object file once, even if there's a separate
1344 if (objfile
->separate_debug_objfile_backlink
)
1347 sect_addr
= overlay_mapped_address (addr
, osect
);
1349 if (obj_section_addr (osect
) <= sect_addr
1350 && sect_addr
< obj_section_endaddr (osect
)
1352 = lookup_minimal_symbol_by_pc_section (sect_addr
,
1355 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1356 const char *loc_string
;
1359 offset
= sect_addr
- MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
1360 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1361 sec_name
= osect
->the_bfd_section
->name
;
1362 msym_name
= msymbol
->print_name ();
1364 /* Don't print the offset if it is zero.
1365 We assume there's no need to handle i18n of "sym + offset". */
1366 std::string string_holder
;
1369 string_holder
= string_printf ("%s + %u", msym_name
, offset
);
1370 loc_string
= string_holder
.c_str ();
1373 loc_string
= msym_name
;
1375 gdb_assert (osect
->objfile
&& objfile_name (osect
->objfile
));
1376 obj_name
= objfile_name (osect
->objfile
);
1378 if (current_program_space
->multi_objfile_p ())
1379 if (pc_in_unmapped_range (addr
, osect
))
1380 if (section_is_overlay (osect
))
1381 printf_filtered (_("%s in load address range of "
1382 "%s overlay section %s of %s\n"),
1383 loc_string
, mapped
, sec_name
, obj_name
);
1385 printf_filtered (_("%s in load address range of "
1386 "section %s of %s\n"),
1387 loc_string
, sec_name
, obj_name
);
1389 if (section_is_overlay (osect
))
1390 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1391 loc_string
, mapped
, sec_name
, obj_name
);
1393 printf_filtered (_("%s in section %s of %s\n"),
1394 loc_string
, sec_name
, obj_name
);
1396 if (pc_in_unmapped_range (addr
, osect
))
1397 if (section_is_overlay (osect
))
1398 printf_filtered (_("%s in load address range of %s overlay "
1400 loc_string
, mapped
, sec_name
);
1403 (_("%s in load address range of section %s\n"),
1404 loc_string
, sec_name
);
1406 if (section_is_overlay (osect
))
1407 printf_filtered (_("%s in %s overlay section %s\n"),
1408 loc_string
, mapped
, sec_name
);
1410 printf_filtered (_("%s in section %s\n"),
1411 loc_string
, sec_name
);
1415 printf_filtered (_("No symbol matches %s.\n"), arg
);
1419 info_address_command (const char *exp
, int from_tty
)
1421 struct gdbarch
*gdbarch
;
1424 struct bound_minimal_symbol msymbol
;
1426 struct obj_section
*section
;
1427 CORE_ADDR load_addr
, context_pc
= 0;
1428 struct field_of_this_result is_a_field_of_this
;
1431 error (_("Argument required."));
1433 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1434 &is_a_field_of_this
).symbol
;
1437 if (is_a_field_of_this
.type
!= NULL
)
1439 printf_filtered ("Symbol \"");
1440 fprintf_symbol_filtered (gdb_stdout
, exp
,
1441 current_language
->la_language
, DMGL_ANSI
);
1442 printf_filtered ("\" is a field of the local class variable ");
1443 if (current_language
->la_language
== language_objc
)
1444 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1446 printf_filtered ("`this'\n");
1450 msymbol
= lookup_bound_minimal_symbol (exp
);
1452 if (msymbol
.minsym
!= NULL
)
1454 struct objfile
*objfile
= msymbol
.objfile
;
1456 gdbarch
= objfile
->arch ();
1457 load_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1459 printf_filtered ("Symbol \"");
1460 fprintf_symbol_filtered (gdb_stdout
, exp
,
1461 current_language
->la_language
, DMGL_ANSI
);
1462 printf_filtered ("\" is at ");
1463 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1465 printf_filtered (" in a file compiled without debugging");
1466 section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
.minsym
);
1467 if (section_is_overlay (section
))
1469 load_addr
= overlay_unmapped_address (load_addr
, section
);
1470 printf_filtered (",\n -- loaded at ");
1471 fputs_styled (paddress (gdbarch
, load_addr
),
1472 address_style
.style (),
1474 printf_filtered (" in overlay section %s",
1475 section
->the_bfd_section
->name
);
1477 printf_filtered (".\n");
1480 error (_("No symbol \"%s\" in current context."), exp
);
1484 printf_filtered ("Symbol \"");
1485 fprintf_symbol_filtered (gdb_stdout
, sym
->print_name (),
1486 current_language
->la_language
, DMGL_ANSI
);
1487 printf_filtered ("\" is ");
1488 val
= SYMBOL_VALUE (sym
);
1489 if (SYMBOL_OBJFILE_OWNED (sym
))
1490 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sym
), sym
);
1493 gdbarch
= symbol_arch (sym
);
1495 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
1497 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
,
1499 printf_filtered (".\n");
1503 switch (SYMBOL_CLASS (sym
))
1506 case LOC_CONST_BYTES
:
1507 printf_filtered ("constant");
1511 printf_filtered ("a label at address ");
1512 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1513 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1515 if (section_is_overlay (section
))
1517 load_addr
= overlay_unmapped_address (load_addr
, section
);
1518 printf_filtered (",\n -- loaded at ");
1519 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1521 printf_filtered (" in overlay section %s",
1522 section
->the_bfd_section
->name
);
1527 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
1530 /* GDBARCH is the architecture associated with the objfile the symbol
1531 is defined in; the target architecture may be different, and may
1532 provide additional registers. However, we do not know the target
1533 architecture at this point. We assume the objfile architecture
1534 will contain all the standard registers that occur in debug info
1536 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1538 if (SYMBOL_IS_ARGUMENT (sym
))
1539 printf_filtered (_("an argument in register %s"),
1540 gdbarch_register_name (gdbarch
, regno
));
1542 printf_filtered (_("a variable in register %s"),
1543 gdbarch_register_name (gdbarch
, regno
));
1547 printf_filtered (_("static storage at address "));
1548 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1549 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1551 if (section_is_overlay (section
))
1553 load_addr
= overlay_unmapped_address (load_addr
, section
);
1554 printf_filtered (_(",\n -- loaded at "));
1555 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1557 printf_filtered (_(" in overlay section %s"),
1558 section
->the_bfd_section
->name
);
1562 case LOC_REGPARM_ADDR
:
1563 /* Note comment at LOC_REGISTER. */
1564 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1565 printf_filtered (_("address of an argument in register %s"),
1566 gdbarch_register_name (gdbarch
, regno
));
1570 printf_filtered (_("an argument at offset %ld"), val
);
1574 printf_filtered (_("a local variable at frame offset %ld"), val
);
1578 printf_filtered (_("a reference argument at offset %ld"), val
);
1582 printf_filtered (_("a typedef"));
1586 printf_filtered (_("a function at address "));
1587 load_addr
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
));
1588 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1590 if (section_is_overlay (section
))
1592 load_addr
= overlay_unmapped_address (load_addr
, section
);
1593 printf_filtered (_(",\n -- loaded at "));
1594 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1596 printf_filtered (_(" in overlay section %s"),
1597 section
->the_bfd_section
->name
);
1601 case LOC_UNRESOLVED
:
1603 struct bound_minimal_symbol msym
;
1605 msym
= lookup_bound_minimal_symbol (sym
->linkage_name ());
1606 if (msym
.minsym
== NULL
)
1607 printf_filtered ("unresolved");
1610 section
= MSYMBOL_OBJ_SECTION (msym
.objfile
, msym
.minsym
);
1613 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1615 load_addr
= MSYMBOL_VALUE_RAW_ADDRESS (msym
.minsym
);
1616 printf_filtered (_("a thread-local variable at offset %s "
1617 "in the thread-local storage for `%s'"),
1618 paddress (gdbarch
, load_addr
),
1619 objfile_name (section
->objfile
));
1623 load_addr
= BMSYMBOL_VALUE_ADDRESS (msym
);
1624 printf_filtered (_("static storage at address "));
1625 fputs_styled (paddress (gdbarch
, load_addr
),
1626 address_style
.style (), gdb_stdout
);
1627 if (section_is_overlay (section
))
1629 load_addr
= overlay_unmapped_address (load_addr
, section
);
1630 printf_filtered (_(",\n -- loaded at "));
1631 fputs_styled (paddress (gdbarch
, load_addr
),
1632 address_style
.style (),
1634 printf_filtered (_(" in overlay section %s"),
1635 section
->the_bfd_section
->name
);
1642 case LOC_OPTIMIZED_OUT
:
1643 printf_filtered (_("optimized out"));
1647 printf_filtered (_("of unknown (botched) type"));
1650 printf_filtered (".\n");
1655 x_command (const char *exp
, int from_tty
)
1657 struct format_data fmt
;
1660 fmt
.format
= last_format
? last_format
: 'x';
1661 fmt
.size
= last_size
;
1665 /* If there is no expression and no format, use the most recent
1667 if (exp
== nullptr && last_count
> 0)
1668 fmt
.count
= last_count
;
1670 if (exp
&& *exp
== '/')
1672 const char *tmp
= exp
+ 1;
1674 fmt
= decode_format (&tmp
, last_format
, last_size
);
1678 last_count
= fmt
.count
;
1680 /* If we have an expression, evaluate it and use it as the address. */
1682 if (exp
!= 0 && *exp
!= 0)
1684 expression_up expr
= parse_expression (exp
);
1685 /* Cause expression not to be there any more if this command is
1686 repeated with Newline. But don't clobber a user-defined
1687 command's definition. */
1689 set_repeat_arguments ("");
1690 val
= evaluate_expression (expr
.get ());
1691 if (TYPE_IS_REFERENCE (value_type (val
)))
1692 val
= coerce_ref (val
);
1693 /* In rvalue contexts, such as this, functions are coerced into
1694 pointers to functions. This makes "x/i main" work. */
1695 if (value_type (val
)->code () == TYPE_CODE_FUNC
1696 && VALUE_LVAL (val
) == lval_memory
)
1697 next_address
= value_address (val
);
1699 next_address
= value_as_address (val
);
1701 next_gdbarch
= expr
->gdbarch
;
1705 error_no_arg (_("starting display address"));
1707 do_examine (fmt
, next_gdbarch
, next_address
);
1709 /* If the examine succeeds, we remember its size and format for next
1710 time. Set last_size to 'b' for strings. */
1711 if (fmt
.format
== 's')
1714 last_size
= fmt
.size
;
1715 last_format
= fmt
.format
;
1717 /* Set a couple of internal variables if appropriate. */
1718 if (last_examine_value
!= nullptr)
1720 /* Make last address examined available to the user as $_. Use
1721 the correct pointer type. */
1722 struct type
*pointer_type
1723 = lookup_pointer_type (value_type (last_examine_value
.get ()));
1724 set_internalvar (lookup_internalvar ("_"),
1725 value_from_pointer (pointer_type
,
1726 last_examine_address
));
1728 /* Make contents of last address examined available to the user
1729 as $__. If the last value has not been fetched from memory
1730 then don't fetch it now; instead mark it by voiding the $__
1732 if (value_lazy (last_examine_value
.get ()))
1733 clear_internalvar (lookup_internalvar ("__"));
1735 set_internalvar (lookup_internalvar ("__"), last_examine_value
.get ());
1740 /* Add an expression to the auto-display chain.
1741 Specify the expression. */
1744 display_command (const char *arg
, int from_tty
)
1746 struct format_data fmt
;
1747 struct display
*newobj
;
1748 const char *exp
= arg
;
1759 fmt
= decode_format (&exp
, 0, 0);
1760 if (fmt
.size
&& fmt
.format
== 0)
1762 if (fmt
.format
== 'i' || fmt
.format
== 's')
1773 innermost_block_tracker tracker
;
1774 expression_up expr
= parse_expression (exp
, &tracker
);
1776 newobj
= new display (exp
, std::move (expr
), fmt
,
1777 current_program_space
, tracker
.block ());
1778 all_displays
.emplace_back (newobj
);
1781 do_one_display (newobj
);
1786 /* Clear out the display_chain. Done when new symtabs are loaded,
1787 since this invalidates the types stored in many expressions. */
1792 all_displays
.clear ();
1795 /* Delete the auto-display DISPLAY. */
1798 delete_display (struct display
*display
)
1800 gdb_assert (display
!= NULL
);
1802 auto iter
= std::find_if (all_displays
.begin (),
1803 all_displays
.end (),
1804 [=] (const std::unique_ptr
<struct display
> &item
)
1806 return item
.get () == display
;
1808 gdb_assert (iter
!= all_displays
.end ());
1809 all_displays
.erase (iter
);
1812 /* Call FUNCTION on each of the displays whose numbers are given in
1813 ARGS. DATA is passed unmodified to FUNCTION. */
1816 map_display_numbers (const char *args
,
1817 gdb::function_view
<void (struct display
*)> function
)
1822 error_no_arg (_("one or more display numbers"));
1824 number_or_range_parser
parser (args
);
1826 while (!parser
.finished ())
1828 const char *p
= parser
.cur_tok ();
1830 num
= parser
.get_number ();
1832 warning (_("bad display number at or near '%s'"), p
);
1835 auto iter
= std::find_if (all_displays
.begin (),
1836 all_displays
.end (),
1837 [=] (const std::unique_ptr
<display
> &item
)
1839 return item
->number
== num
;
1841 if (iter
== all_displays
.end ())
1842 printf_unfiltered (_("No display number %d.\n"), num
);
1844 function (iter
->get ());
1849 /* "undisplay" command. */
1852 undisplay_command (const char *args
, int from_tty
)
1856 if (query (_("Delete all auto-display expressions? ")))
1862 map_display_numbers (args
, delete_display
);
1866 /* Display a single auto-display.
1867 Do nothing if the display cannot be printed in the current context,
1868 or if the display is disabled. */
1871 do_one_display (struct display
*d
)
1873 int within_current_scope
;
1878 /* The expression carries the architecture that was used at parse time.
1879 This is a problem if the expression depends on architecture features
1880 (e.g. register numbers), and the current architecture is now different.
1881 For example, a display statement like "display/i $pc" is expected to
1882 display the PC register of the current architecture, not the arch at
1883 the time the display command was given. Therefore, we re-parse the
1884 expression if the current architecture has changed. */
1885 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1896 innermost_block_tracker tracker
;
1897 d
->exp
= parse_expression (d
->exp_string
.c_str (), &tracker
);
1898 d
->block
= tracker
.block ();
1900 catch (const gdb_exception
&ex
)
1902 /* Can't re-parse the expression. Disable this display item. */
1903 d
->enabled_p
= false;
1904 warning (_("Unable to display \"%s\": %s"),
1905 d
->exp_string
.c_str (), ex
.what ());
1912 if (d
->pspace
== current_program_space
)
1913 within_current_scope
= contained_in (get_selected_block (0), d
->block
,
1916 within_current_scope
= 0;
1919 within_current_scope
= 1;
1920 if (!within_current_scope
)
1923 scoped_restore save_display_number
1924 = make_scoped_restore (¤t_display_number
, d
->number
);
1926 annotate_display_begin ();
1927 printf_filtered ("%d", d
->number
);
1928 annotate_display_number_end ();
1929 printf_filtered (": ");
1933 annotate_display_format ();
1935 printf_filtered ("x/");
1936 if (d
->format
.count
!= 1)
1937 printf_filtered ("%d", d
->format
.count
);
1938 printf_filtered ("%c", d
->format
.format
);
1939 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1940 printf_filtered ("%c", d
->format
.size
);
1941 printf_filtered (" ");
1943 annotate_display_expression ();
1945 puts_filtered (d
->exp_string
.c_str ());
1946 annotate_display_expression_end ();
1948 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1949 printf_filtered ("\n");
1951 printf_filtered (" ");
1953 annotate_display_value ();
1960 val
= evaluate_expression (d
->exp
.get ());
1961 addr
= value_as_address (val
);
1962 if (d
->format
.format
== 'i')
1963 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1964 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1966 catch (const gdb_exception_error
&ex
)
1968 fprintf_filtered (gdb_stdout
, _("%p[<error: %s>%p]\n"),
1969 metadata_style
.style ().ptr (), ex
.what (),
1975 struct value_print_options opts
;
1977 annotate_display_format ();
1979 if (d
->format
.format
)
1980 printf_filtered ("/%c ", d
->format
.format
);
1982 annotate_display_expression ();
1984 puts_filtered (d
->exp_string
.c_str ());
1985 annotate_display_expression_end ();
1987 printf_filtered (" = ");
1989 annotate_display_expression ();
1991 get_formatted_print_options (&opts
, d
->format
.format
);
1992 opts
.raw
= d
->format
.raw
;
1998 val
= evaluate_expression (d
->exp
.get ());
1999 print_formatted (val
, d
->format
.size
, &opts
, gdb_stdout
);
2001 catch (const gdb_exception_error
&ex
)
2003 fprintf_styled (gdb_stdout
, metadata_style
.style (),
2004 _("<error: %s>"), ex
.what ());
2007 printf_filtered ("\n");
2010 annotate_display_end ();
2012 gdb_flush (gdb_stdout
);
2015 /* Display all of the values on the auto-display chain which can be
2016 evaluated in the current scope. */
2021 for (auto &d
: all_displays
)
2022 do_one_display (d
.get ());
2025 /* Delete the auto-display which we were in the process of displaying.
2026 This is done when there is an error or a signal. */
2029 disable_display (int num
)
2031 for (auto &d
: all_displays
)
2032 if (d
->number
== num
)
2034 d
->enabled_p
= false;
2037 printf_unfiltered (_("No display number %d.\n"), num
);
2041 disable_current_display (void)
2043 if (current_display_number
>= 0)
2045 disable_display (current_display_number
);
2046 fprintf_unfiltered (gdb_stderr
,
2047 _("Disabling display %d to "
2048 "avoid infinite recursion.\n"),
2049 current_display_number
);
2051 current_display_number
= -1;
2055 info_display_command (const char *ignore
, int from_tty
)
2057 if (all_displays
.empty ())
2058 printf_unfiltered (_("There are no auto-display expressions now.\n"));
2060 printf_filtered (_("Auto-display expressions now in effect:\n\
2061 Num Enb Expression\n"));
2063 for (auto &d
: all_displays
)
2065 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
2067 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
2069 else if (d
->format
.format
)
2070 printf_filtered ("/%c ", d
->format
.format
);
2071 puts_filtered (d
->exp_string
.c_str ());
2072 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
, true))
2073 printf_filtered (_(" (cannot be evaluated in the current context)"));
2074 printf_filtered ("\n");
2078 /* Implementation of both the "disable display" and "enable display"
2079 commands. ENABLE decides what to do. */
2082 enable_disable_display_command (const char *args
, int from_tty
, bool enable
)
2086 for (auto &d
: all_displays
)
2087 d
->enabled_p
= enable
;
2091 map_display_numbers (args
,
2092 [=] (struct display
*d
)
2094 d
->enabled_p
= enable
;
2098 /* The "enable display" command. */
2101 enable_display_command (const char *args
, int from_tty
)
2103 enable_disable_display_command (args
, from_tty
, true);
2106 /* The "disable display" command. */
2109 disable_display_command (const char *args
, int from_tty
)
2111 enable_disable_display_command (args
, from_tty
, false);
2114 /* display_chain items point to blocks and expressions. Some expressions in
2115 turn may point to symbols.
2116 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2117 obstack_free'd when a shared library is unloaded.
2118 Clear pointers that are about to become dangling.
2119 Both .exp and .block fields will be restored next time we need to display
2120 an item by re-parsing .exp_string field in the new execution context. */
2123 clear_dangling_display_expressions (struct objfile
*objfile
)
2125 struct program_space
*pspace
;
2127 /* With no symbol file we cannot have a block or expression from it. */
2128 if (objfile
== NULL
)
2130 pspace
= objfile
->pspace
;
2131 if (objfile
->separate_debug_objfile_backlink
)
2133 objfile
= objfile
->separate_debug_objfile_backlink
;
2134 gdb_assert (objfile
->pspace
== pspace
);
2137 for (auto &d
: all_displays
)
2139 if (d
->pspace
!= pspace
)
2142 struct objfile
*bl_objf
= nullptr;
2143 if (d
->block
!= nullptr)
2145 bl_objf
= block_objfile (d
->block
);
2146 if (bl_objf
->separate_debug_objfile_backlink
!= nullptr)
2147 bl_objf
= bl_objf
->separate_debug_objfile_backlink
;
2150 if (bl_objf
== objfile
2151 || (d
->exp
!= NULL
&& exp_uses_objfile (d
->exp
.get (), objfile
)))
2160 /* Print the value in stack frame FRAME of a variable specified by a
2161 struct symbol. NAME is the name to print; if NULL then VAR's print
2162 name will be used. STREAM is the ui_file on which to print the
2163 value. INDENT specifies the number of indent levels to print
2164 before printing the variable name.
2166 This function invalidates FRAME. */
2169 print_variable_and_value (const char *name
, struct symbol
*var
,
2170 struct frame_info
*frame
,
2171 struct ui_file
*stream
, int indent
)
2175 name
= var
->print_name ();
2177 fprintf_filtered (stream
, "%s%ps = ", n_spaces (2 * indent
),
2178 styled_string (variable_name_style
.style (), name
));
2183 struct value_print_options opts
;
2185 /* READ_VAR_VALUE needs a block in order to deal with non-local
2186 references (i.e. to handle nested functions). In this context, we
2187 print variables that are local to this frame, so we can avoid passing
2189 val
= read_var_value (var
, NULL
, frame
);
2190 get_user_print_options (&opts
);
2192 common_val_print (val
, stream
, indent
, &opts
, current_language
);
2194 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2198 catch (const gdb_exception_error
&except
)
2200 fprintf_styled (stream
, metadata_style
.style (),
2201 "<error reading variable %s (%s)>", name
,
2205 fprintf_filtered (stream
, "\n");
2208 /* Subroutine of ui_printf to simplify it.
2209 Print VALUE to STREAM using FORMAT.
2210 VALUE is a C-style string either on the target or
2211 in a GDB internal variable. */
2214 printf_c_string (struct ui_file
*stream
, const char *format
,
2215 struct value
*value
)
2217 const gdb_byte
*str
;
2219 if (value_type (value
)->code () != TYPE_CODE_PTR
2220 && VALUE_LVAL (value
) == lval_internalvar
2221 && c_is_string_type_p (value_type (value
)))
2223 size_t len
= TYPE_LENGTH (value_type (value
));
2225 /* Copy the internal var value to TEM_STR and append a terminating null
2226 character. This protects against corrupted C-style strings that lack
2227 the terminating null char. It also allows Ada-style strings (not
2228 null terminated) to be printed without problems. */
2229 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2231 memcpy (tem_str
, value_contents (value
), len
);
2237 CORE_ADDR tem
= value_as_address (value
);;
2242 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2243 fprintf_filtered (stream
, format
, "(null)");
2248 /* This is a %s argument. Find the length of the string. */
2251 for (len
= 0;; len
++)
2256 read_memory (tem
+ len
, &c
, 1);
2261 /* Copy the string contents into a string inside GDB. */
2262 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2265 read_memory (tem
, tem_str
, len
);
2271 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2272 fprintf_filtered (stream
, format
, (char *) str
);
2276 /* Subroutine of ui_printf to simplify it.
2277 Print VALUE to STREAM using FORMAT.
2278 VALUE is a wide C-style string on the target or
2279 in a GDB internal variable. */
2282 printf_wide_c_string (struct ui_file
*stream
, const char *format
,
2283 struct value
*value
)
2285 const gdb_byte
*str
;
2287 struct gdbarch
*gdbarch
= get_type_arch (value_type (value
));
2288 struct type
*wctype
= lookup_typename (current_language
,
2289 "wchar_t", NULL
, 0);
2290 int wcwidth
= TYPE_LENGTH (wctype
);
2292 if (VALUE_LVAL (value
) == lval_internalvar
2293 && c_is_string_type_p (value_type (value
)))
2295 str
= value_contents (value
);
2296 len
= TYPE_LENGTH (value_type (value
));
2300 CORE_ADDR tem
= value_as_address (value
);
2305 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2306 fprintf_filtered (stream
, format
, "(null)");
2311 /* This is a %s argument. Find the length of the string. */
2312 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2313 gdb_byte
*buf
= (gdb_byte
*) alloca (wcwidth
);
2315 for (len
= 0;; len
+= wcwidth
)
2318 read_memory (tem
+ len
, buf
, wcwidth
);
2319 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2323 /* Copy the string contents into a string inside GDB. */
2324 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ wcwidth
);
2327 read_memory (tem
, tem_str
, len
);
2328 memset (&tem_str
[len
], 0, wcwidth
);
2332 auto_obstack output
;
2334 convert_between_encodings (target_wide_charset (gdbarch
),
2337 &output
, translit_char
);
2338 obstack_grow_str0 (&output
, "");
2341 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2342 fprintf_filtered (stream
, format
, obstack_base (&output
));
2346 /* Subroutine of ui_printf to simplify it.
2347 Print VALUE, a floating point value, to STREAM using FORMAT. */
2350 printf_floating (struct ui_file
*stream
, const char *format
,
2351 struct value
*value
, enum argclass argclass
)
2353 /* Parameter data. */
2354 struct type
*param_type
= value_type (value
);
2355 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2357 /* Determine target type corresponding to the format string. */
2358 struct type
*fmt_type
;
2362 fmt_type
= builtin_type (gdbarch
)->builtin_double
;
2364 case long_double_arg
:
2365 fmt_type
= builtin_type (gdbarch
)->builtin_long_double
;
2367 case dec32float_arg
:
2368 fmt_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2370 case dec64float_arg
:
2371 fmt_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2373 case dec128float_arg
:
2374 fmt_type
= builtin_type (gdbarch
)->builtin_declong
;
2377 gdb_assert_not_reached ("unexpected argument class");
2380 /* To match the traditional GDB behavior, the conversion is
2381 done differently depending on the type of the parameter:
2383 - if the parameter has floating-point type, it's value
2384 is converted to the target type;
2386 - otherwise, if the parameter has a type that is of the
2387 same size as a built-in floating-point type, the value
2388 bytes are interpreted as if they were of that type, and
2389 then converted to the target type (this is not done for
2390 decimal floating-point argument classes);
2392 - otherwise, if the source value has an integer value,
2393 it's value is converted to the target type;
2395 - otherwise, an error is raised.
2397 In either case, the result of the conversion is a byte buffer
2398 formatted in the target format for the target type. */
2400 if (fmt_type
->code () == TYPE_CODE_FLT
)
2402 param_type
= float_type_from_length (param_type
);
2403 if (param_type
!= value_type (value
))
2404 value
= value_from_contents (param_type
, value_contents (value
));
2407 value
= value_cast (fmt_type
, value
);
2409 /* Convert the value to a string and print it. */
2411 = target_float_to_string (value_contents (value
), fmt_type
, format
);
2412 fputs_filtered (str
.c_str (), stream
);
2415 /* Subroutine of ui_printf to simplify it.
2416 Print VALUE, a target pointer, to STREAM using FORMAT. */
2419 printf_pointer (struct ui_file
*stream
, const char *format
,
2420 struct value
*value
)
2422 /* We avoid the host's %p because pointers are too
2423 likely to be the wrong size. The only interesting
2424 modifier for %p is a width; extract that, and then
2425 handle %p as glibc would: %#x or a literal "(nil)". */
2429 #ifdef PRINTF_HAS_LONG_LONG
2430 long long val
= value_as_long (value
);
2432 long val
= value_as_long (value
);
2435 fmt
= (char *) alloca (strlen (format
) + 5);
2437 /* Copy up to the leading %. */
2442 int is_percent
= (*p
== '%');
2457 /* Copy any width or flags. Only the "-" flag is valid for pointers
2458 -- see the format_pieces constructor. */
2459 while (*p
== '-' || (*p
>= '0' && *p
< '9'))
2462 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2465 #ifdef PRINTF_HAS_LONG_LONG
2472 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2473 fprintf_filtered (stream
, fmt
, val
);
2481 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2482 fprintf_filtered (stream
, fmt
, "(nil)");
2487 /* printf "printf format string" ARG to STREAM. */
2490 ui_printf (const char *arg
, struct ui_file
*stream
)
2492 const char *s
= arg
;
2493 std::vector
<struct value
*> val_args
;
2496 error_no_arg (_("format-control string and values to print"));
2498 s
= skip_spaces (s
);
2500 /* A format string should follow, enveloped in double quotes. */
2502 error (_("Bad format string, missing '\"'."));
2504 format_pieces
fpieces (&s
);
2507 error (_("Bad format string, non-terminated '\"'."));
2509 s
= skip_spaces (s
);
2511 if (*s
!= ',' && *s
!= 0)
2512 error (_("Invalid argument syntax"));
2516 s
= skip_spaces (s
);
2521 const char *current_substring
;
2524 for (auto &&piece
: fpieces
)
2525 if (piece
.argclass
!= literal_piece
)
2528 /* Now, parse all arguments and evaluate them.
2529 Store the VALUEs in VAL_ARGS. */
2536 val_args
.push_back (parse_to_comma_and_eval (&s1
));
2543 if (val_args
.size () != nargs_wanted
)
2544 error (_("Wrong number of arguments for specified format-string"));
2546 /* Now actually print them. */
2548 for (auto &&piece
: fpieces
)
2550 current_substring
= piece
.string
;
2551 switch (piece
.argclass
)
2554 printf_c_string (stream
, current_substring
, val_args
[i
]);
2556 case wide_string_arg
:
2557 printf_wide_c_string (stream
, current_substring
, val_args
[i
]);
2561 struct gdbarch
*gdbarch
2562 = get_type_arch (value_type (val_args
[i
]));
2563 struct type
*wctype
= lookup_typename (current_language
,
2564 "wchar_t", NULL
, 0);
2565 struct type
*valtype
;
2566 const gdb_byte
*bytes
;
2568 valtype
= value_type (val_args
[i
]);
2569 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2570 || valtype
->code () != TYPE_CODE_INT
)
2571 error (_("expected wchar_t argument for %%lc"));
2573 bytes
= value_contents (val_args
[i
]);
2575 auto_obstack output
;
2577 convert_between_encodings (target_wide_charset (gdbarch
),
2579 bytes
, TYPE_LENGTH (valtype
),
2580 TYPE_LENGTH (valtype
),
2581 &output
, translit_char
);
2582 obstack_grow_str0 (&output
, "");
2585 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2586 fprintf_filtered (stream
, current_substring
,
2587 obstack_base (&output
));
2592 #ifdef PRINTF_HAS_LONG_LONG
2594 long long val
= value_as_long (val_args
[i
]);
2597 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2598 fprintf_filtered (stream
, current_substring
, val
);
2603 error (_("long long not supported in printf"));
2607 int val
= value_as_long (val_args
[i
]);
2610 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2611 fprintf_filtered (stream
, current_substring
, val
);
2617 long val
= value_as_long (val_args
[i
]);
2620 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2621 fprintf_filtered (stream
, current_substring
, val
);
2627 size_t val
= value_as_long (val_args
[i
]);
2630 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2631 fprintf_filtered (stream
, current_substring
, val
);
2635 /* Handles floating-point values. */
2637 case long_double_arg
:
2638 case dec32float_arg
:
2639 case dec64float_arg
:
2640 case dec128float_arg
:
2641 printf_floating (stream
, current_substring
, val_args
[i
],
2645 printf_pointer (stream
, current_substring
, val_args
[i
]);
2648 /* Print a portion of the format string that has no
2649 directives. Note that this will not include any
2650 ordinary %-specs, but it might include "%%". That is
2651 why we use printf_filtered and not puts_filtered here.
2652 Also, we pass a dummy argument because some platforms
2653 have modified GCC to include -Wformat-security by
2654 default, which will warn here if there is no
2657 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2658 fprintf_filtered (stream
, current_substring
, 0);
2662 internal_error (__FILE__
, __LINE__
,
2663 _("failed internal consistency check"));
2665 /* Maybe advance to the next argument. */
2666 if (piece
.argclass
!= literal_piece
)
2672 /* Implement the "printf" command. */
2675 printf_command (const char *arg
, int from_tty
)
2677 ui_printf (arg
, gdb_stdout
);
2678 reset_terminal_style (gdb_stdout
);
2680 gdb_stdout
->flush ();
2683 /* Implement the "eval" command. */
2686 eval_command (const char *arg
, int from_tty
)
2690 ui_printf (arg
, &stb
);
2692 std::string expanded
= insert_user_defined_cmd_args (stb
.c_str ());
2694 execute_command (expanded
.c_str (), from_tty
);
2697 void _initialize_printcmd ();
2699 _initialize_printcmd ()
2701 struct cmd_list_element
*c
;
2703 current_display_number
= -1;
2705 gdb::observers::free_objfile
.attach (clear_dangling_display_expressions
);
2707 add_info ("address", info_address_command
,
2708 _("Describe where symbol SYM is stored.\n\
2709 Usage: info address SYM"));
2711 add_info ("symbol", info_symbol_command
, _("\
2712 Describe what symbol is at location ADDR.\n\
2713 Usage: info symbol ADDR\n\
2714 Only for symbols with fixed locations (global or static scope)."));
2716 add_com ("x", class_vars
, x_command
, _("\
2717 Examine memory: x/FMT ADDRESS.\n\
2718 ADDRESS is an expression for the memory address to examine.\n\
2719 FMT is a repeat count followed by a format letter and a size letter.\n\
2720 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2721 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2722 and z(hex, zero padded on the left).\n\
2723 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2724 The specified number of objects of the specified size are printed\n\
2725 according to the format. If a negative number is specified, memory is\n\
2726 examined backward from the address.\n\n\
2727 Defaults for format and size letters are those previously used.\n\
2728 Default count is 1. Default address is following last thing printed\n\
2729 with this command or \"print\"."));
2731 add_info ("display", info_display_command
, _("\
2732 Expressions to display when program stops, with code numbers.\n\
2733 Usage: info display"));
2735 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2736 Cancel some expressions to be displayed when program stops.\n\
2737 Usage: undisplay [NUM]...\n\
2738 Arguments are the code numbers of the expressions to stop displaying.\n\
2739 No argument means cancel all automatic-display expressions.\n\
2740 \"delete display\" has the same effect as this command.\n\
2741 Do \"info display\" to see current list of code numbers."),
2744 add_com ("display", class_vars
, display_command
, _("\
2745 Print value of expression EXP each time the program stops.\n\
2746 Usage: display[/FMT] EXP\n\
2747 /FMT may be used before EXP as in the \"print\" command.\n\
2748 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2749 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2750 and examining is done as in the \"x\" command.\n\n\
2751 With no argument, display all currently requested auto-display expressions.\n\
2752 Use \"undisplay\" to cancel display requests previously made."));
2754 add_cmd ("display", class_vars
, enable_display_command
, _("\
2755 Enable some expressions to be displayed when program stops.\n\
2756 Usage: enable display [NUM]...\n\
2757 Arguments are the code numbers of the expressions to resume displaying.\n\
2758 No argument means enable all automatic-display expressions.\n\
2759 Do \"info display\" to see current list of code numbers."), &enablelist
);
2761 add_cmd ("display", class_vars
, disable_display_command
, _("\
2762 Disable some expressions to be displayed when program stops.\n\
2763 Usage: disable display [NUM]...\n\
2764 Arguments are the code numbers of the expressions to stop displaying.\n\
2765 No argument means disable all automatic-display expressions.\n\
2766 Do \"info display\" to see current list of code numbers."), &disablelist
);
2768 add_cmd ("display", class_vars
, undisplay_command
, _("\
2769 Cancel some expressions to be displayed when program stops.\n\
2770 Usage: delete display [NUM]...\n\
2771 Arguments are the code numbers of the expressions to stop displaying.\n\
2772 No argument means cancel all automatic-display expressions.\n\
2773 Do \"info display\" to see current list of code numbers."), &deletelist
);
2775 add_com ("printf", class_vars
, printf_command
, _("\
2776 Formatted printing, like the C \"printf\" function.\n\
2777 Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2778 This supports most C printf format specifications, like %s, %d, etc."));
2780 add_com ("output", class_vars
, output_command
, _("\
2781 Like \"print\" but don't put in value history and don't print newline.\n\
2782 Usage: output EXP\n\
2783 This is useful in user-defined commands."));
2785 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2786 Evaluate expression EXP and assign result to variable VAR.\n\
2787 Usage: set VAR = EXP\n\
2788 This uses assignment syntax appropriate for the current language\n\
2789 (VAR = EXP or VAR := EXP for example).\n\
2790 VAR may be a debugger \"convenience\" variable (names starting\n\
2791 with $), a register (a few standard names starting with $), or an actual\n\
2792 variable in the program being debugged. EXP is any valid expression.\n\
2793 Use \"set variable\" for variables with names identical to set subcommands.\n\
2795 With a subcommand, this command modifies parts of the gdb environment.\n\
2796 You can see these environment settings with the \"show\" command."),
2797 &setlist
, "set ", 1, &cmdlist
);
2799 add_com ("assign", class_vars
, set_command
, _("\
2800 Evaluate expression EXP and assign result to variable VAR.\n\
2801 Usage: assign VAR = EXP\n\
2802 This uses assignment syntax appropriate for the current language\n\
2803 (VAR = EXP or VAR := EXP for example).\n\
2804 VAR may be a debugger \"convenience\" variable (names starting\n\
2805 with $), a register (a few standard names starting with $), or an actual\n\
2806 variable in the program being debugged. EXP is any valid expression.\n\
2807 Use \"set variable\" for variables with names identical to set subcommands.\n\
2808 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2809 You can see these environment settings with the \"show\" command."));
2811 /* "call" is the same as "set", but handy for dbx users to call fns. */
2812 c
= add_com ("call", class_vars
, call_command
, _("\
2813 Call a function in the program.\n\
2815 The argument is the function name and arguments, in the notation of the\n\
2816 current working language. The result is printed and saved in the value\n\
2817 history, if it is not void."));
2818 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2820 add_cmd ("variable", class_vars
, set_command
, _("\
2821 Evaluate expression EXP and assign result to variable VAR.\n\
2822 Usage: set variable VAR = EXP\n\
2823 This uses assignment syntax appropriate for the current language\n\
2824 (VAR = EXP or VAR := EXP for example).\n\
2825 VAR may be a debugger \"convenience\" variable (names starting\n\
2826 with $), a register (a few standard names starting with $), or an actual\n\
2827 variable in the program being debugged. EXP is any valid expression.\n\
2828 This may usually be abbreviated to simply \"set\"."),
2830 add_alias_cmd ("var", "variable", class_vars
, 0, &setlist
);
2832 const auto print_opts
= make_value_print_options_def_group (nullptr);
2834 static const std::string print_help
= gdb::option::build_help (_("\
2835 Print value of expression EXP.\n\
2836 Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
2841 Note: because this command accepts arbitrary expressions, if you\n\
2842 specify any command option, you must use a double dash (\"--\")\n\
2843 to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
2845 Variables accessible are those of the lexical environment of the selected\n\
2846 stack frame, plus all those whose scope is global or an entire file.\n\
2848 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2849 $$NUM refers to NUM'th value back from the last one.\n\
2850 Names starting with $ refer to registers (with the values they would have\n\
2851 if the program were to return to the stack frame now selected, restoring\n\
2852 all registers saved by frames farther in) or else to debugger\n\
2853 \"convenience\" variables (any such name not a known register).\n\
2854 Use assignment expressions to give values to convenience variables.\n\
2856 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2857 @ is a binary operator for treating consecutive data objects\n\
2858 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2859 element is FOO, whose second element is stored in the space following\n\
2860 where FOO is stored, etc. FOO must be an expression whose value\n\
2861 resides in memory.\n\
2863 EXP may be preceded with /FMT, where FMT is a format letter\n\
2864 but no count or size letter (see \"x\" command)."),
2867 c
= add_com ("print", class_vars
, print_command
, print_help
.c_str ());
2868 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2869 add_com_alias ("p", "print", class_vars
, 1);
2870 add_com_alias ("inspect", "print", class_vars
, 1);
2872 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2873 &max_symbolic_offset
, _("\
2874 Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2875 Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2876 Tell GDB to only display the symbolic form of an address if the\n\
2877 offset between the closest earlier symbol and the address is less than\n\
2878 the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2879 to always print the symbolic form of an address if any symbol precedes\n\
2880 it. Zero is equivalent to \"unlimited\"."),
2882 show_max_symbolic_offset
,
2883 &setprintlist
, &showprintlist
);
2884 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2885 &print_symbol_filename
, _("\
2886 Set printing of source filename and line number with <SYMBOL>."), _("\
2887 Show printing of source filename and line number with <SYMBOL>."), NULL
,
2889 show_print_symbol_filename
,
2890 &setprintlist
, &showprintlist
);
2892 add_com ("eval", no_class
, eval_command
, _("\
2893 Construct a GDB command and then evaluate it.\n\
2894 Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2895 Convert the arguments to a string as \"printf\" would, but then\n\
2896 treat this string as a command line, and evaluate it."));