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 && (options
->format
== 'o'
426 || options
->format
== 'x'
427 || options
->format
== 't'
428 || options
->format
== 'z'
429 || options
->format
== 'd'
430 || options
->format
== 'u'))
431 || (type
->code () == TYPE_CODE_RANGE
&& type
->bounds ()->bias
!= 0)
432 || type
->bit_size_differs_p ())
434 val_long
.emplace (unpack_long (type
, valaddr
));
435 converted_bytes
.resize (TYPE_LENGTH (type
));
436 store_signed_integer (converted_bytes
.data (), TYPE_LENGTH (type
),
437 byte_order
, *val_long
);
438 valaddr
= converted_bytes
.data ();
441 /* Printing a non-float type as 'f' will interpret the data as if it were
442 of a floating-point type of the same length, if that exists. Otherwise,
443 the data is printed as integer. */
444 char format
= options
->format
;
445 if (format
== 'f' && type
->code () != TYPE_CODE_FLT
)
447 type
= float_type_from_length (type
);
448 if (type
->code () != TYPE_CODE_FLT
)
455 print_octal_chars (stream
, valaddr
, len
, byte_order
);
458 print_decimal_chars (stream
, valaddr
, len
, true, byte_order
);
461 print_decimal_chars (stream
, valaddr
, len
, false, byte_order
);
464 if (type
->code () != TYPE_CODE_FLT
)
466 print_decimal_chars (stream
, valaddr
, len
, !type
->is_unsigned (),
472 print_floating (valaddr
, type
, stream
);
476 print_binary_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
479 print_hex_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
482 print_hex_chars (stream
, valaddr
, len
, byte_order
, true);
486 struct value_print_options opts
= *options
;
488 if (!val_long
.has_value ())
489 val_long
.emplace (unpack_long (type
, valaddr
));
492 if (type
->is_unsigned ())
493 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
495 type
= builtin_type (gdbarch
)->builtin_true_char
;
497 value_print (value_from_longest (type
, *val_long
), stream
, &opts
);
503 if (!val_long
.has_value ())
504 val_long
.emplace (unpack_long (type
, valaddr
));
505 print_address (gdbarch
, *val_long
, stream
);
510 error (_("Undefined output format \"%c\"."), format
);
514 /* Specify default address for `x' command.
515 The `info lines' command uses this. */
518 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
520 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
522 next_gdbarch
= gdbarch
;
525 /* Make address available to the user as $_. */
526 set_internalvar (lookup_internalvar ("_"),
527 value_from_pointer (ptr_type
, addr
));
530 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
531 after LEADIN. Print nothing if no symbolic name is found nearby.
532 Optionally also print source file and line number, if available.
533 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
534 or to interpret it as a possible C++ name and convert it back to source
535 form. However note that DO_DEMANGLE can be overridden by the specific
536 settings of the demangle and asm_demangle variables. Returns
537 non-zero if anything was printed; zero otherwise. */
540 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
541 struct ui_file
*stream
,
542 int do_demangle
, const char *leadin
)
544 std::string name
, filename
;
549 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, false, &name
,
550 &offset
, &filename
, &line
, &unmapped
))
553 fputs_filtered (leadin
, stream
);
555 fputs_filtered ("<*", stream
);
557 fputs_filtered ("<", stream
);
558 fputs_styled (name
.c_str (), function_name_style
.style (), stream
);
560 fprintf_filtered (stream
, "%+d", offset
);
562 /* Append source filename and line number if desired. Give specific
563 line # of this addr, if we have it; else line # of the nearest symbol. */
564 if (print_symbol_filename
&& !filename
.empty ())
566 fputs_filtered (line
== -1 ? " in " : " at ", stream
);
567 fputs_styled (filename
.c_str (), file_name_style
.style (), stream
);
569 fprintf_filtered (stream
, ":%d", line
);
572 fputs_filtered ("*>", stream
);
574 fputs_filtered (">", stream
);
579 /* See valprint.h. */
582 build_address_symbolic (struct gdbarch
*gdbarch
,
583 CORE_ADDR addr
, /* IN */
584 bool do_demangle
, /* IN */
585 bool prefer_sym_over_minsym
, /* IN */
586 std::string
*name
, /* OUT */
587 int *offset
, /* OUT */
588 std::string
*filename
, /* OUT */
590 int *unmapped
) /* OUT */
592 struct bound_minimal_symbol msymbol
;
593 struct symbol
*symbol
;
594 CORE_ADDR name_location
= 0;
595 struct obj_section
*section
= NULL
;
596 const char *name_temp
= "";
598 /* Let's say it is mapped (not unmapped). */
601 /* Determine if the address is in an overlay, and whether it is
603 if (overlay_debugging
)
605 section
= find_pc_overlay (addr
);
606 if (pc_in_unmapped_range (addr
, section
))
609 addr
= overlay_mapped_address (addr
, section
);
613 /* Try to find the address in both the symbol table and the minsyms.
614 In most cases, we'll prefer to use the symbol instead of the
615 minsym. However, there are cases (see below) where we'll choose
616 to use the minsym instead. */
618 /* This is defective in the sense that it only finds text symbols. So
619 really this is kind of pointless--we should make sure that the
620 minimal symbols have everything we need (by changing that we could
621 save some memory, but for many debug format--ELF/DWARF or
622 anything/stabs--it would be inconvenient to eliminate those minimal
624 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
625 symbol
= find_pc_sect_function (addr
, section
);
629 /* If this is a function (i.e. a code address), strip out any
630 non-address bits. For instance, display a pointer to the
631 first instruction of a Thumb function as <function>; the
632 second instruction will be <function+2>, even though the
633 pointer is <function+3>. This matches the ISA behavior. */
634 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
636 name_location
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol
));
637 if (do_demangle
|| asm_demangle
)
638 name_temp
= symbol
->print_name ();
640 name_temp
= symbol
->linkage_name ();
643 if (msymbol
.minsym
!= NULL
644 && MSYMBOL_HAS_SIZE (msymbol
.minsym
)
645 && MSYMBOL_SIZE (msymbol
.minsym
) == 0
646 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text
647 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text_gnu_ifunc
648 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_file_text
)
649 msymbol
.minsym
= NULL
;
651 if (msymbol
.minsym
!= NULL
)
653 /* Use the minsym if no symbol is found.
655 Additionally, use the minsym instead of a (found) symbol if
656 the following conditions all hold:
657 1) The prefer_sym_over_minsym flag is false.
658 2) The minsym address is identical to that of the address under
660 3) The symbol address is not identical to that of the address
661 under consideration. */
662 if (symbol
== NULL
||
663 (!prefer_sym_over_minsym
664 && BMSYMBOL_VALUE_ADDRESS (msymbol
) == addr
665 && name_location
!= addr
))
667 /* If this is a function (i.e. a code address), strip out any
668 non-address bits. For instance, display a pointer to the
669 first instruction of a Thumb function as <function>; the
670 second instruction will be <function+2>, even though the
671 pointer is <function+3>. This matches the ISA behavior. */
672 if (MSYMBOL_TYPE (msymbol
.minsym
) == mst_text
673 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_text_gnu_ifunc
674 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_file_text
675 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_solib_trampoline
)
676 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
679 name_location
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
680 if (do_demangle
|| asm_demangle
)
681 name_temp
= msymbol
.minsym
->print_name ();
683 name_temp
= msymbol
.minsym
->linkage_name ();
686 if (symbol
== NULL
&& msymbol
.minsym
== NULL
)
689 /* If the nearest symbol is too far away, don't print anything symbolic. */
691 /* For when CORE_ADDR is larger than unsigned int, we do math in
692 CORE_ADDR. But when we detect unsigned wraparound in the
693 CORE_ADDR math, we ignore this test and print the offset,
694 because addr+max_symbolic_offset has wrapped through the end
695 of the address space back to the beginning, giving bogus comparison. */
696 if (addr
> name_location
+ max_symbolic_offset
697 && name_location
+ max_symbolic_offset
> name_location
)
700 *offset
= (LONGEST
) addr
- name_location
;
704 if (print_symbol_filename
)
706 struct symtab_and_line sal
;
708 sal
= find_pc_sect_line (addr
, section
, 0);
712 *filename
= symtab_to_filename_for_display (sal
.symtab
);
720 /* Print address ADDR symbolically on STREAM.
721 First print it as a number. Then perhaps print
722 <SYMBOL + OFFSET> after the number. */
725 print_address (struct gdbarch
*gdbarch
,
726 CORE_ADDR addr
, struct ui_file
*stream
)
728 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
729 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
732 /* Return a prefix for instruction address:
733 "=> " for current instruction, else " ". */
736 pc_prefix (CORE_ADDR addr
)
738 if (has_stack_frames ())
740 struct frame_info
*frame
;
743 frame
= get_selected_frame (NULL
);
744 if (get_frame_pc_if_available (frame
, &pc
) && pc
== addr
)
750 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
751 controls whether to print the symbolic name "raw" or demangled.
752 Return non-zero if anything was printed; zero otherwise. */
755 print_address_demangle (const struct value_print_options
*opts
,
756 struct gdbarch
*gdbarch
, CORE_ADDR addr
,
757 struct ui_file
*stream
, int do_demangle
)
759 if (opts
->addressprint
)
761 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
762 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
766 return print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
772 /* Find the address of the instruction that is INST_COUNT instructions before
773 the instruction at ADDR.
774 Since some architectures have variable-length instructions, we can't just
775 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
776 number information to locate the nearest known instruction boundary,
777 and disassemble forward from there. If we go out of the symbol range
778 during disassembling, we return the lowest address we've got so far and
779 set the number of instructions read to INST_READ. */
782 find_instruction_backward (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
783 int inst_count
, int *inst_read
)
785 /* The vector PCS is used to store instruction addresses within
787 CORE_ADDR loop_start
, loop_end
, p
;
788 std::vector
<CORE_ADDR
> pcs
;
789 struct symtab_and_line sal
;
792 loop_start
= loop_end
= addr
;
794 /* In each iteration of the outer loop, we get a pc range that ends before
795 LOOP_START, then we count and store every instruction address of the range
796 iterated in the loop.
797 If the number of instructions counted reaches INST_COUNT, return the
798 stored address that is located INST_COUNT instructions back from ADDR.
799 If INST_COUNT is not reached, we subtract the number of counted
800 instructions from INST_COUNT, and go to the next iteration. */
804 sal
= find_pc_sect_line (loop_start
, NULL
, 1);
807 /* We reach here when line info is not available. In this case,
808 we print a message and just exit the loop. The return value
809 is calculated after the loop. */
810 printf_filtered (_("No line number information available "
813 print_address (gdbarch
, loop_start
- 1, gdb_stdout
);
814 printf_filtered ("\n");
818 loop_end
= loop_start
;
821 /* This loop pushes instruction addresses in the range from
822 LOOP_START to LOOP_END. */
823 for (p
= loop_start
; p
< loop_end
;)
826 p
+= gdb_insn_length (gdbarch
, p
);
829 inst_count
-= pcs
.size ();
830 *inst_read
+= pcs
.size ();
832 while (inst_count
> 0);
834 /* After the loop, the vector PCS has instruction addresses of the last
835 source line we processed, and INST_COUNT has a negative value.
836 We return the address at the index of -INST_COUNT in the vector for
838 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
848 find_instruction_backward is called with INST_COUNT = 4 and expected to
849 return 0x4001. When we reach here, INST_COUNT is set to -1 because
850 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
851 4001 is located at the index 1 of the last iterated line (= Line X),
852 which is simply calculated by -INST_COUNT.
853 The case when the length of PCS is 0 means that we reached an area for
854 which line info is not available. In such case, we return LOOP_START,
855 which was the lowest instruction address that had line info. */
856 p
= pcs
.size () > 0 ? pcs
[-inst_count
] : loop_start
;
858 /* INST_READ includes all instruction addresses in a pc range. Need to
859 exclude the beginning part up to the address we're returning. That
860 is, exclude {0x4000} in the example above. */
862 *inst_read
+= inst_count
;
867 /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
868 placing the results in GDB's memory from MYADDR + LEN. Returns
869 a count of the bytes actually read. */
872 read_memory_backward (struct gdbarch
*gdbarch
,
873 CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
876 int nread
; /* Number of bytes actually read. */
878 /* First try a complete read. */
879 errcode
= target_read_memory (memaddr
, myaddr
, len
);
887 /* Loop, reading one byte at a time until we get as much as we can. */
890 for (nread
= 0; nread
< len
; ++nread
)
892 errcode
= target_read_memory (--memaddr
, --myaddr
, 1);
895 /* The read was unsuccessful, so exit the loop. */
896 printf_filtered (_("Cannot access memory at address %s\n"),
897 paddress (gdbarch
, memaddr
));
905 /* Returns true if X (which is LEN bytes wide) is the number zero. */
908 integer_is_zero (const gdb_byte
*x
, int len
)
912 while (i
< len
&& x
[i
] == 0)
917 /* Find the start address of a string in which ADDR is included.
918 Basically we search for '\0' and return the next address,
919 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
920 we stop searching and return the address to print characters as many as
921 PRINT_MAX from the string. */
924 find_string_backward (struct gdbarch
*gdbarch
,
925 CORE_ADDR addr
, int count
, int char_size
,
926 const struct value_print_options
*options
,
927 int *strings_counted
)
929 const int chunk_size
= 0x20;
932 int chars_to_read
= chunk_size
;
933 int chars_counted
= 0;
934 int count_original
= count
;
935 CORE_ADDR string_start_addr
= addr
;
937 gdb_assert (char_size
== 1 || char_size
== 2 || char_size
== 4);
938 gdb::byte_vector
buffer (chars_to_read
* char_size
);
939 while (count
> 0 && read_error
== 0)
943 addr
-= chars_to_read
* char_size
;
944 chars_read
= read_memory_backward (gdbarch
, addr
, buffer
.data (),
945 chars_to_read
* char_size
);
946 chars_read
/= char_size
;
947 read_error
= (chars_read
== chars_to_read
) ? 0 : 1;
948 /* Searching for '\0' from the end of buffer in backward direction. */
949 for (i
= 0; i
< chars_read
&& count
> 0 ; ++i
, ++chars_counted
)
951 int offset
= (chars_to_read
- i
- 1) * char_size
;
953 if (integer_is_zero (&buffer
[offset
], char_size
)
954 || chars_counted
== options
->print_max
)
956 /* Found '\0' or reached print_max. As OFFSET is the offset to
957 '\0', we add CHAR_SIZE to return the start address of
960 string_start_addr
= addr
+ offset
+ char_size
;
966 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
967 *strings_counted
= count_original
- count
;
971 /* In error case, STRING_START_ADDR is pointing to the string that
972 was last successfully loaded. Rewind the partially loaded string. */
973 string_start_addr
-= chars_counted
* char_size
;
976 return string_start_addr
;
979 /* Examine data at address ADDR in format FMT.
980 Fetch it from memory and print on gdb_stdout. */
983 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
988 struct type
*val_type
= NULL
;
991 struct value_print_options opts
;
992 int need_to_update_next_address
= 0;
993 CORE_ADDR addr_rewound
= 0;
998 next_gdbarch
= gdbarch
;
1001 /* Instruction format implies fetch single bytes
1002 regardless of the specified size.
1003 The case of strings is handled in decode_format, only explicit
1004 size operator are not changed to 'b'. */
1010 /* Pick the appropriate size for an address. */
1011 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
1013 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
1015 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
1018 /* Bad value for gdbarch_ptr_bit. */
1019 internal_error (__FILE__
, __LINE__
,
1020 _("failed internal consistency check"));
1024 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1025 else if (size
== 'h')
1026 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
1027 else if (size
== 'w')
1028 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
1029 else if (size
== 'g')
1030 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
1034 struct type
*char_type
= NULL
;
1036 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1037 if type is not found. */
1039 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
1040 else if (size
== 'w')
1041 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
1043 val_type
= char_type
;
1046 if (size
!= '\0' && size
!= 'b')
1047 warning (_("Unable to display strings with "
1048 "size '%c', using 'b' instead."), size
);
1050 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1059 if (format
== 's' || format
== 'i')
1062 get_formatted_print_options (&opts
, format
);
1066 /* This is the negative repeat count case.
1067 We rewind the address based on the given repeat count and format,
1068 then examine memory from there in forward direction. */
1073 next_address
= find_instruction_backward (gdbarch
, addr
, count
,
1076 else if (format
== 's')
1078 next_address
= find_string_backward (gdbarch
, addr
, count
,
1079 TYPE_LENGTH (val_type
),
1084 next_address
= addr
- count
* TYPE_LENGTH (val_type
);
1087 /* The following call to print_formatted updates next_address in every
1088 iteration. In backward case, we store the start address here
1089 and update next_address with it before exiting the function. */
1090 addr_rewound
= (format
== 's'
1091 ? next_address
- TYPE_LENGTH (val_type
)
1093 need_to_update_next_address
= 1;
1096 /* Print as many objects as specified in COUNT, at most maxelts per line,
1097 with the address of the next one at the start of each line. */
1103 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
1104 print_address (next_gdbarch
, next_address
, gdb_stdout
);
1105 printf_filtered (":");
1110 printf_filtered ("\t");
1111 /* Note that print_formatted sets next_address for the next
1113 last_examine_address
= next_address
;
1115 /* The value to be displayed is not fetched greedily.
1116 Instead, to avoid the possibility of a fetched value not
1117 being used, its retrieval is delayed until the print code
1118 uses it. When examining an instruction stream, the
1119 disassembler will perform its own memory fetch using just
1120 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1121 the disassembler be modified so that LAST_EXAMINE_VALUE
1122 is left with the byte sequence from the last complete
1123 instruction fetched from memory? */
1125 = release_value (value_at_lazy (val_type
, next_address
));
1127 print_formatted (last_examine_value
.get (), size
, &opts
, gdb_stdout
);
1129 /* Display any branch delay slots following the final insn. */
1130 if (format
== 'i' && count
== 1)
1131 count
+= branch_delay_insns
;
1133 printf_filtered ("\n");
1136 if (need_to_update_next_address
)
1137 next_address
= addr_rewound
;
1141 validate_format (struct format_data fmt
, const char *cmdname
)
1144 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
1146 error (_("Item count other than 1 is meaningless in \"%s\" command."),
1148 if (fmt
.format
== 'i')
1149 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
1150 fmt
.format
, cmdname
);
1153 /* Parse print command format string into *OPTS and update *EXPP.
1154 CMDNAME should name the current command. */
1157 print_command_parse_format (const char **expp
, const char *cmdname
,
1158 value_print_options
*opts
)
1160 const char *exp
= *expp
;
1162 /* opts->raw value might already have been set by 'set print raw-values'
1163 or by using 'print -raw-values'.
1164 So, do not set opts->raw to 0, only set it to 1 if /r is given. */
1165 if (exp
&& *exp
== '/')
1170 fmt
= decode_format (&exp
, last_format
, 0);
1171 validate_format (fmt
, cmdname
);
1172 last_format
= fmt
.format
;
1174 opts
->format
= fmt
.format
;
1175 opts
->raw
= opts
->raw
|| fmt
.raw
;
1185 /* See valprint.h. */
1188 print_value (value
*val
, const value_print_options
&opts
)
1190 int histindex
= record_latest_value (val
);
1192 annotate_value_history_begin (histindex
, value_type (val
));
1194 printf_filtered ("$%d = ", histindex
);
1196 annotate_value_history_value ();
1198 print_formatted (val
, 0, &opts
, gdb_stdout
);
1199 printf_filtered ("\n");
1201 annotate_value_history_end ();
1204 /* Implementation of the "print" and "call" commands. */
1207 print_command_1 (const char *args
, int voidprint
)
1210 value_print_options print_opts
;
1212 get_user_print_options (&print_opts
);
1213 /* Override global settings with explicit options, if any. */
1214 auto group
= make_value_print_options_def_group (&print_opts
);
1215 gdb::option::process_options
1216 (&args
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
1218 print_command_parse_format (&args
, "print", &print_opts
);
1220 const char *exp
= args
;
1222 if (exp
!= nullptr && *exp
)
1224 expression_up expr
= parse_expression (exp
);
1225 val
= evaluate_expression (expr
.get ());
1228 val
= access_value_history (0);
1230 if (voidprint
|| (val
&& value_type (val
) &&
1231 value_type (val
)->code () != TYPE_CODE_VOID
))
1232 print_value (val
, print_opts
);
1235 /* See valprint.h. */
1238 print_command_completer (struct cmd_list_element
*ignore
,
1239 completion_tracker
&tracker
,
1240 const char *text
, const char * /*word*/)
1242 const auto group
= make_value_print_options_def_group (nullptr);
1243 if (gdb::option::complete_options
1244 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
))
1247 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1248 expression_completer (ignore
, tracker
, text
, word
);
1252 print_command (const char *exp
, int from_tty
)
1254 print_command_1 (exp
, 1);
1257 /* Same as print, except it doesn't print void results. */
1259 call_command (const char *exp
, int from_tty
)
1261 print_command_1 (exp
, 0);
1264 /* Implementation of the "output" command. */
1267 output_command (const char *exp
, int from_tty
)
1271 struct format_data fmt
;
1272 struct value_print_options opts
;
1277 if (exp
&& *exp
== '/')
1280 fmt
= decode_format (&exp
, 0, 0);
1281 validate_format (fmt
, "output");
1282 format
= fmt
.format
;
1285 expression_up expr
= parse_expression (exp
);
1287 val
= evaluate_expression (expr
.get ());
1289 annotate_value_begin (value_type (val
));
1291 get_formatted_print_options (&opts
, format
);
1293 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1295 annotate_value_end ();
1298 gdb_flush (gdb_stdout
);
1302 set_command (const char *exp
, int from_tty
)
1304 expression_up expr
= parse_expression (exp
);
1306 if (expr
->nelts
>= 1)
1307 switch (expr
->elts
[0].opcode
)
1309 case UNOP_PREINCREMENT
:
1310 case UNOP_POSTINCREMENT
:
1311 case UNOP_PREDECREMENT
:
1312 case UNOP_POSTDECREMENT
:
1314 case BINOP_ASSIGN_MODIFY
:
1319 (_("Expression is not an assignment (and might have no effect)"));
1322 evaluate_expression (expr
.get ());
1326 info_symbol_command (const char *arg
, int from_tty
)
1328 struct minimal_symbol
*msymbol
;
1329 struct obj_section
*osect
;
1330 CORE_ADDR addr
, sect_addr
;
1332 unsigned int offset
;
1335 error_no_arg (_("address"));
1337 addr
= parse_and_eval_address (arg
);
1338 for (objfile
*objfile
: current_program_space
->objfiles ())
1339 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
1341 /* Only process each object file once, even if there's a separate
1343 if (objfile
->separate_debug_objfile_backlink
)
1346 sect_addr
= overlay_mapped_address (addr
, osect
);
1348 if (obj_section_addr (osect
) <= sect_addr
1349 && sect_addr
< obj_section_endaddr (osect
)
1351 = lookup_minimal_symbol_by_pc_section (sect_addr
,
1354 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1355 const char *loc_string
;
1358 offset
= sect_addr
- MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
1359 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1360 sec_name
= osect
->the_bfd_section
->name
;
1361 msym_name
= msymbol
->print_name ();
1363 /* Don't print the offset if it is zero.
1364 We assume there's no need to handle i18n of "sym + offset". */
1365 std::string string_holder
;
1368 string_holder
= string_printf ("%s + %u", msym_name
, offset
);
1369 loc_string
= string_holder
.c_str ();
1372 loc_string
= msym_name
;
1374 gdb_assert (osect
->objfile
&& objfile_name (osect
->objfile
));
1375 obj_name
= objfile_name (osect
->objfile
);
1377 if (current_program_space
->multi_objfile_p ())
1378 if (pc_in_unmapped_range (addr
, osect
))
1379 if (section_is_overlay (osect
))
1380 printf_filtered (_("%s in load address range of "
1381 "%s overlay section %s of %s\n"),
1382 loc_string
, mapped
, sec_name
, obj_name
);
1384 printf_filtered (_("%s in load address range of "
1385 "section %s of %s\n"),
1386 loc_string
, sec_name
, obj_name
);
1388 if (section_is_overlay (osect
))
1389 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1390 loc_string
, mapped
, sec_name
, obj_name
);
1392 printf_filtered (_("%s in section %s of %s\n"),
1393 loc_string
, sec_name
, obj_name
);
1395 if (pc_in_unmapped_range (addr
, osect
))
1396 if (section_is_overlay (osect
))
1397 printf_filtered (_("%s in load address range of %s overlay "
1399 loc_string
, mapped
, sec_name
);
1402 (_("%s in load address range of section %s\n"),
1403 loc_string
, sec_name
);
1405 if (section_is_overlay (osect
))
1406 printf_filtered (_("%s in %s overlay section %s\n"),
1407 loc_string
, mapped
, sec_name
);
1409 printf_filtered (_("%s in section %s\n"),
1410 loc_string
, sec_name
);
1414 printf_filtered (_("No symbol matches %s.\n"), arg
);
1418 info_address_command (const char *exp
, int from_tty
)
1420 struct gdbarch
*gdbarch
;
1423 struct bound_minimal_symbol msymbol
;
1425 struct obj_section
*section
;
1426 CORE_ADDR load_addr
, context_pc
= 0;
1427 struct field_of_this_result is_a_field_of_this
;
1430 error (_("Argument required."));
1432 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1433 &is_a_field_of_this
).symbol
;
1436 if (is_a_field_of_this
.type
!= NULL
)
1438 printf_filtered ("Symbol \"");
1439 fprintf_symbol_filtered (gdb_stdout
, exp
,
1440 current_language
->la_language
, DMGL_ANSI
);
1441 printf_filtered ("\" is a field of the local class variable ");
1442 if (current_language
->la_language
== language_objc
)
1443 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1445 printf_filtered ("`this'\n");
1449 msymbol
= lookup_bound_minimal_symbol (exp
);
1451 if (msymbol
.minsym
!= NULL
)
1453 struct objfile
*objfile
= msymbol
.objfile
;
1455 gdbarch
= objfile
->arch ();
1456 load_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1458 printf_filtered ("Symbol \"");
1459 fprintf_symbol_filtered (gdb_stdout
, exp
,
1460 current_language
->la_language
, DMGL_ANSI
);
1461 printf_filtered ("\" is at ");
1462 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1464 printf_filtered (" in a file compiled without debugging");
1465 section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
.minsym
);
1466 if (section_is_overlay (section
))
1468 load_addr
= overlay_unmapped_address (load_addr
, section
);
1469 printf_filtered (",\n -- loaded at ");
1470 fputs_styled (paddress (gdbarch
, load_addr
),
1471 address_style
.style (),
1473 printf_filtered (" in overlay section %s",
1474 section
->the_bfd_section
->name
);
1476 printf_filtered (".\n");
1479 error (_("No symbol \"%s\" in current context."), exp
);
1483 printf_filtered ("Symbol \"");
1484 fprintf_symbol_filtered (gdb_stdout
, sym
->print_name (),
1485 current_language
->la_language
, DMGL_ANSI
);
1486 printf_filtered ("\" is ");
1487 val
= SYMBOL_VALUE (sym
);
1488 if (SYMBOL_OBJFILE_OWNED (sym
))
1489 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sym
), sym
);
1492 gdbarch
= symbol_arch (sym
);
1494 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
1496 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
,
1498 printf_filtered (".\n");
1502 switch (SYMBOL_CLASS (sym
))
1505 case LOC_CONST_BYTES
:
1506 printf_filtered ("constant");
1510 printf_filtered ("a label at address ");
1511 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1512 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1514 if (section_is_overlay (section
))
1516 load_addr
= overlay_unmapped_address (load_addr
, section
);
1517 printf_filtered (",\n -- loaded at ");
1518 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1520 printf_filtered (" in overlay section %s",
1521 section
->the_bfd_section
->name
);
1526 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
1529 /* GDBARCH is the architecture associated with the objfile the symbol
1530 is defined in; the target architecture may be different, and may
1531 provide additional registers. However, we do not know the target
1532 architecture at this point. We assume the objfile architecture
1533 will contain all the standard registers that occur in debug info
1535 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1537 if (SYMBOL_IS_ARGUMENT (sym
))
1538 printf_filtered (_("an argument in register %s"),
1539 gdbarch_register_name (gdbarch
, regno
));
1541 printf_filtered (_("a variable in register %s"),
1542 gdbarch_register_name (gdbarch
, regno
));
1546 printf_filtered (_("static storage at address "));
1547 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1548 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1550 if (section_is_overlay (section
))
1552 load_addr
= overlay_unmapped_address (load_addr
, section
);
1553 printf_filtered (_(",\n -- loaded at "));
1554 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1556 printf_filtered (_(" in overlay section %s"),
1557 section
->the_bfd_section
->name
);
1561 case LOC_REGPARM_ADDR
:
1562 /* Note comment at LOC_REGISTER. */
1563 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1564 printf_filtered (_("address of an argument in register %s"),
1565 gdbarch_register_name (gdbarch
, regno
));
1569 printf_filtered (_("an argument at offset %ld"), val
);
1573 printf_filtered (_("a local variable at frame offset %ld"), val
);
1577 printf_filtered (_("a reference argument at offset %ld"), val
);
1581 printf_filtered (_("a typedef"));
1585 printf_filtered (_("a function at address "));
1586 load_addr
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
));
1587 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1589 if (section_is_overlay (section
))
1591 load_addr
= overlay_unmapped_address (load_addr
, section
);
1592 printf_filtered (_(",\n -- loaded at "));
1593 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1595 printf_filtered (_(" in overlay section %s"),
1596 section
->the_bfd_section
->name
);
1600 case LOC_UNRESOLVED
:
1602 struct bound_minimal_symbol msym
;
1604 msym
= lookup_bound_minimal_symbol (sym
->linkage_name ());
1605 if (msym
.minsym
== NULL
)
1606 printf_filtered ("unresolved");
1609 section
= MSYMBOL_OBJ_SECTION (msym
.objfile
, msym
.minsym
);
1612 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1614 load_addr
= MSYMBOL_VALUE_RAW_ADDRESS (msym
.minsym
);
1615 printf_filtered (_("a thread-local variable at offset %s "
1616 "in the thread-local storage for `%s'"),
1617 paddress (gdbarch
, load_addr
),
1618 objfile_name (section
->objfile
));
1622 load_addr
= BMSYMBOL_VALUE_ADDRESS (msym
);
1623 printf_filtered (_("static storage at address "));
1624 fputs_styled (paddress (gdbarch
, load_addr
),
1625 address_style
.style (), gdb_stdout
);
1626 if (section_is_overlay (section
))
1628 load_addr
= overlay_unmapped_address (load_addr
, section
);
1629 printf_filtered (_(",\n -- loaded at "));
1630 fputs_styled (paddress (gdbarch
, load_addr
),
1631 address_style
.style (),
1633 printf_filtered (_(" in overlay section %s"),
1634 section
->the_bfd_section
->name
);
1641 case LOC_OPTIMIZED_OUT
:
1642 printf_filtered (_("optimized out"));
1646 printf_filtered (_("of unknown (botched) type"));
1649 printf_filtered (".\n");
1654 x_command (const char *exp
, int from_tty
)
1656 struct format_data fmt
;
1659 fmt
.format
= last_format
? last_format
: 'x';
1660 fmt
.size
= last_size
;
1664 /* If there is no expression and no format, use the most recent
1666 if (exp
== nullptr && last_count
> 0)
1667 fmt
.count
= last_count
;
1669 if (exp
&& *exp
== '/')
1671 const char *tmp
= exp
+ 1;
1673 fmt
= decode_format (&tmp
, last_format
, last_size
);
1677 last_count
= fmt
.count
;
1679 /* If we have an expression, evaluate it and use it as the address. */
1681 if (exp
!= 0 && *exp
!= 0)
1683 expression_up expr
= parse_expression (exp
);
1684 /* Cause expression not to be there any more if this command is
1685 repeated with Newline. But don't clobber a user-defined
1686 command's definition. */
1688 set_repeat_arguments ("");
1689 val
= evaluate_expression (expr
.get ());
1690 if (TYPE_IS_REFERENCE (value_type (val
)))
1691 val
= coerce_ref (val
);
1692 /* In rvalue contexts, such as this, functions are coerced into
1693 pointers to functions. This makes "x/i main" work. */
1694 if (value_type (val
)->code () == TYPE_CODE_FUNC
1695 && VALUE_LVAL (val
) == lval_memory
)
1696 next_address
= value_address (val
);
1698 next_address
= value_as_address (val
);
1700 next_gdbarch
= expr
->gdbarch
;
1704 error_no_arg (_("starting display address"));
1706 do_examine (fmt
, next_gdbarch
, next_address
);
1708 /* If the examine succeeds, we remember its size and format for next
1709 time. Set last_size to 'b' for strings. */
1710 if (fmt
.format
== 's')
1713 last_size
= fmt
.size
;
1714 last_format
= fmt
.format
;
1716 /* Set a couple of internal variables if appropriate. */
1717 if (last_examine_value
!= nullptr)
1719 /* Make last address examined available to the user as $_. Use
1720 the correct pointer type. */
1721 struct type
*pointer_type
1722 = lookup_pointer_type (value_type (last_examine_value
.get ()));
1723 set_internalvar (lookup_internalvar ("_"),
1724 value_from_pointer (pointer_type
,
1725 last_examine_address
));
1727 /* Make contents of last address examined available to the user
1728 as $__. If the last value has not been fetched from memory
1729 then don't fetch it now; instead mark it by voiding the $__
1731 if (value_lazy (last_examine_value
.get ()))
1732 clear_internalvar (lookup_internalvar ("__"));
1734 set_internalvar (lookup_internalvar ("__"), last_examine_value
.get ());
1739 /* Add an expression to the auto-display chain.
1740 Specify the expression. */
1743 display_command (const char *arg
, int from_tty
)
1745 struct format_data fmt
;
1746 struct display
*newobj
;
1747 const char *exp
= arg
;
1758 fmt
= decode_format (&exp
, 0, 0);
1759 if (fmt
.size
&& fmt
.format
== 0)
1761 if (fmt
.format
== 'i' || fmt
.format
== 's')
1772 innermost_block_tracker tracker
;
1773 expression_up expr
= parse_expression (exp
, &tracker
);
1775 newobj
= new display (exp
, std::move (expr
), fmt
,
1776 current_program_space
, tracker
.block ());
1777 all_displays
.emplace_back (newobj
);
1780 do_one_display (newobj
);
1785 /* Clear out the display_chain. Done when new symtabs are loaded,
1786 since this invalidates the types stored in many expressions. */
1791 all_displays
.clear ();
1794 /* Delete the auto-display DISPLAY. */
1797 delete_display (struct display
*display
)
1799 gdb_assert (display
!= NULL
);
1801 auto iter
= std::find_if (all_displays
.begin (),
1802 all_displays
.end (),
1803 [=] (const std::unique_ptr
<struct display
> &item
)
1805 return item
.get () == display
;
1807 gdb_assert (iter
!= all_displays
.end ());
1808 all_displays
.erase (iter
);
1811 /* Call FUNCTION on each of the displays whose numbers are given in
1812 ARGS. DATA is passed unmodified to FUNCTION. */
1815 map_display_numbers (const char *args
,
1816 gdb::function_view
<void (struct display
*)> function
)
1821 error_no_arg (_("one or more display numbers"));
1823 number_or_range_parser
parser (args
);
1825 while (!parser
.finished ())
1827 const char *p
= parser
.cur_tok ();
1829 num
= parser
.get_number ();
1831 warning (_("bad display number at or near '%s'"), p
);
1834 auto iter
= std::find_if (all_displays
.begin (),
1835 all_displays
.end (),
1836 [=] (const std::unique_ptr
<display
> &item
)
1838 return item
->number
== num
;
1840 if (iter
== all_displays
.end ())
1841 printf_unfiltered (_("No display number %d.\n"), num
);
1843 function (iter
->get ());
1848 /* "undisplay" command. */
1851 undisplay_command (const char *args
, int from_tty
)
1855 if (query (_("Delete all auto-display expressions? ")))
1861 map_display_numbers (args
, delete_display
);
1865 /* Display a single auto-display.
1866 Do nothing if the display cannot be printed in the current context,
1867 or if the display is disabled. */
1870 do_one_display (struct display
*d
)
1872 int within_current_scope
;
1877 /* The expression carries the architecture that was used at parse time.
1878 This is a problem if the expression depends on architecture features
1879 (e.g. register numbers), and the current architecture is now different.
1880 For example, a display statement like "display/i $pc" is expected to
1881 display the PC register of the current architecture, not the arch at
1882 the time the display command was given. Therefore, we re-parse the
1883 expression if the current architecture has changed. */
1884 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1895 innermost_block_tracker tracker
;
1896 d
->exp
= parse_expression (d
->exp_string
.c_str (), &tracker
);
1897 d
->block
= tracker
.block ();
1899 catch (const gdb_exception
&ex
)
1901 /* Can't re-parse the expression. Disable this display item. */
1902 d
->enabled_p
= false;
1903 warning (_("Unable to display \"%s\": %s"),
1904 d
->exp_string
.c_str (), ex
.what ());
1911 if (d
->pspace
== current_program_space
)
1912 within_current_scope
= contained_in (get_selected_block (0), d
->block
,
1915 within_current_scope
= 0;
1918 within_current_scope
= 1;
1919 if (!within_current_scope
)
1922 scoped_restore save_display_number
1923 = make_scoped_restore (¤t_display_number
, d
->number
);
1925 annotate_display_begin ();
1926 printf_filtered ("%d", d
->number
);
1927 annotate_display_number_end ();
1928 printf_filtered (": ");
1932 annotate_display_format ();
1934 printf_filtered ("x/");
1935 if (d
->format
.count
!= 1)
1936 printf_filtered ("%d", d
->format
.count
);
1937 printf_filtered ("%c", d
->format
.format
);
1938 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1939 printf_filtered ("%c", d
->format
.size
);
1940 printf_filtered (" ");
1942 annotate_display_expression ();
1944 puts_filtered (d
->exp_string
.c_str ());
1945 annotate_display_expression_end ();
1947 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1948 printf_filtered ("\n");
1950 printf_filtered (" ");
1952 annotate_display_value ();
1959 val
= evaluate_expression (d
->exp
.get ());
1960 addr
= value_as_address (val
);
1961 if (d
->format
.format
== 'i')
1962 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1963 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1965 catch (const gdb_exception_error
&ex
)
1967 fprintf_filtered (gdb_stdout
, _("%p[<error: %s>%p]\n"),
1968 metadata_style
.style ().ptr (), ex
.what (),
1974 struct value_print_options opts
;
1976 annotate_display_format ();
1978 if (d
->format
.format
)
1979 printf_filtered ("/%c ", d
->format
.format
);
1981 annotate_display_expression ();
1983 puts_filtered (d
->exp_string
.c_str ());
1984 annotate_display_expression_end ();
1986 printf_filtered (" = ");
1988 annotate_display_expression ();
1990 get_formatted_print_options (&opts
, d
->format
.format
);
1991 opts
.raw
= d
->format
.raw
;
1997 val
= evaluate_expression (d
->exp
.get ());
1998 print_formatted (val
, d
->format
.size
, &opts
, gdb_stdout
);
2000 catch (const gdb_exception_error
&ex
)
2002 fprintf_styled (gdb_stdout
, metadata_style
.style (),
2003 _("<error: %s>"), ex
.what ());
2006 printf_filtered ("\n");
2009 annotate_display_end ();
2011 gdb_flush (gdb_stdout
);
2014 /* Display all of the values on the auto-display chain which can be
2015 evaluated in the current scope. */
2020 for (auto &d
: all_displays
)
2021 do_one_display (d
.get ());
2024 /* Delete the auto-display which we were in the process of displaying.
2025 This is done when there is an error or a signal. */
2028 disable_display (int num
)
2030 for (auto &d
: all_displays
)
2031 if (d
->number
== num
)
2033 d
->enabled_p
= false;
2036 printf_unfiltered (_("No display number %d.\n"), num
);
2040 disable_current_display (void)
2042 if (current_display_number
>= 0)
2044 disable_display (current_display_number
);
2045 fprintf_unfiltered (gdb_stderr
,
2046 _("Disabling display %d to "
2047 "avoid infinite recursion.\n"),
2048 current_display_number
);
2050 current_display_number
= -1;
2054 info_display_command (const char *ignore
, int from_tty
)
2056 if (all_displays
.empty ())
2057 printf_unfiltered (_("There are no auto-display expressions now.\n"));
2059 printf_filtered (_("Auto-display expressions now in effect:\n\
2060 Num Enb Expression\n"));
2062 for (auto &d
: all_displays
)
2064 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
2066 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
2068 else if (d
->format
.format
)
2069 printf_filtered ("/%c ", d
->format
.format
);
2070 puts_filtered (d
->exp_string
.c_str ());
2071 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
, true))
2072 printf_filtered (_(" (cannot be evaluated in the current context)"));
2073 printf_filtered ("\n");
2077 /* Implementation of both the "disable display" and "enable display"
2078 commands. ENABLE decides what to do. */
2081 enable_disable_display_command (const char *args
, int from_tty
, bool enable
)
2085 for (auto &d
: all_displays
)
2086 d
->enabled_p
= enable
;
2090 map_display_numbers (args
,
2091 [=] (struct display
*d
)
2093 d
->enabled_p
= enable
;
2097 /* The "enable display" command. */
2100 enable_display_command (const char *args
, int from_tty
)
2102 enable_disable_display_command (args
, from_tty
, true);
2105 /* The "disable display" command. */
2108 disable_display_command (const char *args
, int from_tty
)
2110 enable_disable_display_command (args
, from_tty
, false);
2113 /* display_chain items point to blocks and expressions. Some expressions in
2114 turn may point to symbols.
2115 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2116 obstack_free'd when a shared library is unloaded.
2117 Clear pointers that are about to become dangling.
2118 Both .exp and .block fields will be restored next time we need to display
2119 an item by re-parsing .exp_string field in the new execution context. */
2122 clear_dangling_display_expressions (struct objfile
*objfile
)
2124 struct program_space
*pspace
;
2126 /* With no symbol file we cannot have a block or expression from it. */
2127 if (objfile
== NULL
)
2129 pspace
= objfile
->pspace
;
2130 if (objfile
->separate_debug_objfile_backlink
)
2132 objfile
= objfile
->separate_debug_objfile_backlink
;
2133 gdb_assert (objfile
->pspace
== pspace
);
2136 for (auto &d
: all_displays
)
2138 if (d
->pspace
!= pspace
)
2141 struct objfile
*bl_objf
= nullptr;
2142 if (d
->block
!= nullptr)
2144 bl_objf
= block_objfile (d
->block
);
2145 if (bl_objf
->separate_debug_objfile_backlink
!= nullptr)
2146 bl_objf
= bl_objf
->separate_debug_objfile_backlink
;
2149 if (bl_objf
== objfile
2150 || (d
->exp
!= NULL
&& exp_uses_objfile (d
->exp
.get (), objfile
)))
2159 /* Print the value in stack frame FRAME of a variable specified by a
2160 struct symbol. NAME is the name to print; if NULL then VAR's print
2161 name will be used. STREAM is the ui_file on which to print the
2162 value. INDENT specifies the number of indent levels to print
2163 before printing the variable name.
2165 This function invalidates FRAME. */
2168 print_variable_and_value (const char *name
, struct symbol
*var
,
2169 struct frame_info
*frame
,
2170 struct ui_file
*stream
, int indent
)
2174 name
= var
->print_name ();
2176 fprintf_filtered (stream
, "%s%ps = ", n_spaces (2 * indent
),
2177 styled_string (variable_name_style
.style (), name
));
2182 struct value_print_options opts
;
2184 /* READ_VAR_VALUE needs a block in order to deal with non-local
2185 references (i.e. to handle nested functions). In this context, we
2186 print variables that are local to this frame, so we can avoid passing
2188 val
= read_var_value (var
, NULL
, frame
);
2189 get_user_print_options (&opts
);
2191 common_val_print (val
, stream
, indent
, &opts
, current_language
);
2193 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2197 catch (const gdb_exception_error
&except
)
2199 fprintf_styled (stream
, metadata_style
.style (),
2200 "<error reading variable %s (%s)>", name
,
2204 fprintf_filtered (stream
, "\n");
2207 /* Subroutine of ui_printf to simplify it.
2208 Print VALUE to STREAM using FORMAT.
2209 VALUE is a C-style string either on the target or
2210 in a GDB internal variable. */
2213 printf_c_string (struct ui_file
*stream
, const char *format
,
2214 struct value
*value
)
2216 const gdb_byte
*str
;
2218 if (value_type (value
)->code () != TYPE_CODE_PTR
2219 && VALUE_LVAL (value
) == lval_internalvar
2220 && c_is_string_type_p (value_type (value
)))
2222 size_t len
= TYPE_LENGTH (value_type (value
));
2224 /* Copy the internal var value to TEM_STR and append a terminating null
2225 character. This protects against corrupted C-style strings that lack
2226 the terminating null char. It also allows Ada-style strings (not
2227 null terminated) to be printed without problems. */
2228 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2230 memcpy (tem_str
, value_contents (value
), len
);
2236 CORE_ADDR tem
= value_as_address (value
);;
2241 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2242 fprintf_filtered (stream
, format
, "(null)");
2247 /* This is a %s argument. Find the length of the string. */
2250 for (len
= 0;; len
++)
2255 read_memory (tem
+ len
, &c
, 1);
2260 /* Copy the string contents into a string inside GDB. */
2261 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2264 read_memory (tem
, tem_str
, len
);
2270 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2271 fprintf_filtered (stream
, format
, (char *) str
);
2275 /* Subroutine of ui_printf to simplify it.
2276 Print VALUE to STREAM using FORMAT.
2277 VALUE is a wide C-style string on the target or
2278 in a GDB internal variable. */
2281 printf_wide_c_string (struct ui_file
*stream
, const char *format
,
2282 struct value
*value
)
2284 const gdb_byte
*str
;
2286 struct gdbarch
*gdbarch
= get_type_arch (value_type (value
));
2287 struct type
*wctype
= lookup_typename (current_language
,
2288 "wchar_t", NULL
, 0);
2289 int wcwidth
= TYPE_LENGTH (wctype
);
2291 if (VALUE_LVAL (value
) == lval_internalvar
2292 && c_is_string_type_p (value_type (value
)))
2294 str
= value_contents (value
);
2295 len
= TYPE_LENGTH (value_type (value
));
2299 CORE_ADDR tem
= value_as_address (value
);
2304 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2305 fprintf_filtered (stream
, format
, "(null)");
2310 /* This is a %s argument. Find the length of the string. */
2311 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2312 gdb_byte
*buf
= (gdb_byte
*) alloca (wcwidth
);
2314 for (len
= 0;; len
+= wcwidth
)
2317 read_memory (tem
+ len
, buf
, wcwidth
);
2318 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2322 /* Copy the string contents into a string inside GDB. */
2323 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ wcwidth
);
2326 read_memory (tem
, tem_str
, len
);
2327 memset (&tem_str
[len
], 0, wcwidth
);
2331 auto_obstack output
;
2333 convert_between_encodings (target_wide_charset (gdbarch
),
2336 &output
, translit_char
);
2337 obstack_grow_str0 (&output
, "");
2340 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2341 fprintf_filtered (stream
, format
, obstack_base (&output
));
2345 /* Subroutine of ui_printf to simplify it.
2346 Print VALUE, a floating point value, to STREAM using FORMAT. */
2349 printf_floating (struct ui_file
*stream
, const char *format
,
2350 struct value
*value
, enum argclass argclass
)
2352 /* Parameter data. */
2353 struct type
*param_type
= value_type (value
);
2354 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2356 /* Determine target type corresponding to the format string. */
2357 struct type
*fmt_type
;
2361 fmt_type
= builtin_type (gdbarch
)->builtin_double
;
2363 case long_double_arg
:
2364 fmt_type
= builtin_type (gdbarch
)->builtin_long_double
;
2366 case dec32float_arg
:
2367 fmt_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2369 case dec64float_arg
:
2370 fmt_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2372 case dec128float_arg
:
2373 fmt_type
= builtin_type (gdbarch
)->builtin_declong
;
2376 gdb_assert_not_reached ("unexpected argument class");
2379 /* To match the traditional GDB behavior, the conversion is
2380 done differently depending on the type of the parameter:
2382 - if the parameter has floating-point type, it's value
2383 is converted to the target type;
2385 - otherwise, if the parameter has a type that is of the
2386 same size as a built-in floating-point type, the value
2387 bytes are interpreted as if they were of that type, and
2388 then converted to the target type (this is not done for
2389 decimal floating-point argument classes);
2391 - otherwise, if the source value has an integer value,
2392 it's value is converted to the target type;
2394 - otherwise, an error is raised.
2396 In either case, the result of the conversion is a byte buffer
2397 formatted in the target format for the target type. */
2399 if (fmt_type
->code () == TYPE_CODE_FLT
)
2401 param_type
= float_type_from_length (param_type
);
2402 if (param_type
!= value_type (value
))
2403 value
= value_from_contents (param_type
, value_contents (value
));
2406 value
= value_cast (fmt_type
, value
);
2408 /* Convert the value to a string and print it. */
2410 = target_float_to_string (value_contents (value
), fmt_type
, format
);
2411 fputs_filtered (str
.c_str (), stream
);
2414 /* Subroutine of ui_printf to simplify it.
2415 Print VALUE, a target pointer, to STREAM using FORMAT. */
2418 printf_pointer (struct ui_file
*stream
, const char *format
,
2419 struct value
*value
)
2421 /* We avoid the host's %p because pointers are too
2422 likely to be the wrong size. The only interesting
2423 modifier for %p is a width; extract that, and then
2424 handle %p as glibc would: %#x or a literal "(nil)". */
2428 #ifdef PRINTF_HAS_LONG_LONG
2429 long long val
= value_as_long (value
);
2431 long val
= value_as_long (value
);
2434 fmt
= (char *) alloca (strlen (format
) + 5);
2436 /* Copy up to the leading %. */
2441 int is_percent
= (*p
== '%');
2456 /* Copy any width or flags. Only the "-" flag is valid for pointers
2457 -- see the format_pieces constructor. */
2458 while (*p
== '-' || (*p
>= '0' && *p
< '9'))
2461 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2464 #ifdef PRINTF_HAS_LONG_LONG
2471 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2472 fprintf_filtered (stream
, fmt
, val
);
2480 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2481 fprintf_filtered (stream
, fmt
, "(nil)");
2486 /* printf "printf format string" ARG to STREAM. */
2489 ui_printf (const char *arg
, struct ui_file
*stream
)
2491 const char *s
= arg
;
2492 std::vector
<struct value
*> val_args
;
2495 error_no_arg (_("format-control string and values to print"));
2497 s
= skip_spaces (s
);
2499 /* A format string should follow, enveloped in double quotes. */
2501 error (_("Bad format string, missing '\"'."));
2503 format_pieces
fpieces (&s
);
2506 error (_("Bad format string, non-terminated '\"'."));
2508 s
= skip_spaces (s
);
2510 if (*s
!= ',' && *s
!= 0)
2511 error (_("Invalid argument syntax"));
2515 s
= skip_spaces (s
);
2520 const char *current_substring
;
2523 for (auto &&piece
: fpieces
)
2524 if (piece
.argclass
!= literal_piece
)
2527 /* Now, parse all arguments and evaluate them.
2528 Store the VALUEs in VAL_ARGS. */
2535 val_args
.push_back (parse_to_comma_and_eval (&s1
));
2542 if (val_args
.size () != nargs_wanted
)
2543 error (_("Wrong number of arguments for specified format-string"));
2545 /* Now actually print them. */
2547 for (auto &&piece
: fpieces
)
2549 current_substring
= piece
.string
;
2550 switch (piece
.argclass
)
2553 printf_c_string (stream
, current_substring
, val_args
[i
]);
2555 case wide_string_arg
:
2556 printf_wide_c_string (stream
, current_substring
, val_args
[i
]);
2560 struct gdbarch
*gdbarch
2561 = get_type_arch (value_type (val_args
[i
]));
2562 struct type
*wctype
= lookup_typename (current_language
,
2563 "wchar_t", NULL
, 0);
2564 struct type
*valtype
;
2565 const gdb_byte
*bytes
;
2567 valtype
= value_type (val_args
[i
]);
2568 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2569 || valtype
->code () != TYPE_CODE_INT
)
2570 error (_("expected wchar_t argument for %%lc"));
2572 bytes
= value_contents (val_args
[i
]);
2574 auto_obstack output
;
2576 convert_between_encodings (target_wide_charset (gdbarch
),
2578 bytes
, TYPE_LENGTH (valtype
),
2579 TYPE_LENGTH (valtype
),
2580 &output
, translit_char
);
2581 obstack_grow_str0 (&output
, "");
2584 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2585 fprintf_filtered (stream
, current_substring
,
2586 obstack_base (&output
));
2591 #ifdef PRINTF_HAS_LONG_LONG
2593 long long val
= value_as_long (val_args
[i
]);
2596 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2597 fprintf_filtered (stream
, current_substring
, val
);
2602 error (_("long long not supported in printf"));
2606 int val
= value_as_long (val_args
[i
]);
2609 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2610 fprintf_filtered (stream
, current_substring
, val
);
2616 long val
= value_as_long (val_args
[i
]);
2619 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2620 fprintf_filtered (stream
, current_substring
, val
);
2626 size_t val
= value_as_long (val_args
[i
]);
2629 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2630 fprintf_filtered (stream
, current_substring
, val
);
2634 /* Handles floating-point values. */
2636 case long_double_arg
:
2637 case dec32float_arg
:
2638 case dec64float_arg
:
2639 case dec128float_arg
:
2640 printf_floating (stream
, current_substring
, val_args
[i
],
2644 printf_pointer (stream
, current_substring
, val_args
[i
]);
2647 /* Print a portion of the format string that has no
2648 directives. Note that this will not include any
2649 ordinary %-specs, but it might include "%%". That is
2650 why we use printf_filtered and not puts_filtered here.
2651 Also, we pass a dummy argument because some platforms
2652 have modified GCC to include -Wformat-security by
2653 default, which will warn here if there is no
2656 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2657 fprintf_filtered (stream
, current_substring
, 0);
2661 internal_error (__FILE__
, __LINE__
,
2662 _("failed internal consistency check"));
2664 /* Maybe advance to the next argument. */
2665 if (piece
.argclass
!= literal_piece
)
2671 /* Implement the "printf" command. */
2674 printf_command (const char *arg
, int from_tty
)
2676 ui_printf (arg
, gdb_stdout
);
2677 reset_terminal_style (gdb_stdout
);
2679 gdb_stdout
->flush ();
2682 /* Implement the "eval" command. */
2685 eval_command (const char *arg
, int from_tty
)
2689 ui_printf (arg
, &stb
);
2691 std::string expanded
= insert_user_defined_cmd_args (stb
.c_str ());
2693 execute_command (expanded
.c_str (), from_tty
);
2696 void _initialize_printcmd ();
2698 _initialize_printcmd ()
2700 struct cmd_list_element
*c
;
2702 current_display_number
= -1;
2704 gdb::observers::free_objfile
.attach (clear_dangling_display_expressions
);
2706 add_info ("address", info_address_command
,
2707 _("Describe where symbol SYM is stored.\n\
2708 Usage: info address SYM"));
2710 add_info ("symbol", info_symbol_command
, _("\
2711 Describe what symbol is at location ADDR.\n\
2712 Usage: info symbol ADDR\n\
2713 Only for symbols with fixed locations (global or static scope)."));
2715 add_com ("x", class_vars
, x_command
, _("\
2716 Examine memory: x/FMT ADDRESS.\n\
2717 ADDRESS is an expression for the memory address to examine.\n\
2718 FMT is a repeat count followed by a format letter and a size letter.\n\
2719 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2720 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2721 and z(hex, zero padded on the left).\n\
2722 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2723 The specified number of objects of the specified size are printed\n\
2724 according to the format. If a negative number is specified, memory is\n\
2725 examined backward from the address.\n\n\
2726 Defaults for format and size letters are those previously used.\n\
2727 Default count is 1. Default address is following last thing printed\n\
2728 with this command or \"print\"."));
2730 add_info ("display", info_display_command
, _("\
2731 Expressions to display when program stops, with code numbers.\n\
2732 Usage: info display"));
2734 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2735 Cancel some expressions to be displayed when program stops.\n\
2736 Usage: undisplay [NUM]...\n\
2737 Arguments are the code numbers of the expressions to stop displaying.\n\
2738 No argument means cancel all automatic-display expressions.\n\
2739 \"delete display\" has the same effect as this command.\n\
2740 Do \"info display\" to see current list of code numbers."),
2743 add_com ("display", class_vars
, display_command
, _("\
2744 Print value of expression EXP each time the program stops.\n\
2745 Usage: display[/FMT] EXP\n\
2746 /FMT may be used before EXP as in the \"print\" command.\n\
2747 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2748 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2749 and examining is done as in the \"x\" command.\n\n\
2750 With no argument, display all currently requested auto-display expressions.\n\
2751 Use \"undisplay\" to cancel display requests previously made."));
2753 add_cmd ("display", class_vars
, enable_display_command
, _("\
2754 Enable some expressions to be displayed when program stops.\n\
2755 Usage: enable display [NUM]...\n\
2756 Arguments are the code numbers of the expressions to resume displaying.\n\
2757 No argument means enable all automatic-display expressions.\n\
2758 Do \"info display\" to see current list of code numbers."), &enablelist
);
2760 add_cmd ("display", class_vars
, disable_display_command
, _("\
2761 Disable some expressions to be displayed when program stops.\n\
2762 Usage: disable display [NUM]...\n\
2763 Arguments are the code numbers of the expressions to stop displaying.\n\
2764 No argument means disable all automatic-display expressions.\n\
2765 Do \"info display\" to see current list of code numbers."), &disablelist
);
2767 add_cmd ("display", class_vars
, undisplay_command
, _("\
2768 Cancel some expressions to be displayed when program stops.\n\
2769 Usage: delete display [NUM]...\n\
2770 Arguments are the code numbers of the expressions to stop displaying.\n\
2771 No argument means cancel all automatic-display expressions.\n\
2772 Do \"info display\" to see current list of code numbers."), &deletelist
);
2774 add_com ("printf", class_vars
, printf_command
, _("\
2775 Formatted printing, like the C \"printf\" function.\n\
2776 Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2777 This supports most C printf format specifications, like %s, %d, etc."));
2779 add_com ("output", class_vars
, output_command
, _("\
2780 Like \"print\" but don't put in value history and don't print newline.\n\
2781 Usage: output EXP\n\
2782 This is useful in user-defined commands."));
2784 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2785 Evaluate expression EXP and assign result to variable VAR.\n\
2786 Usage: set VAR = EXP\n\
2787 This uses assignment syntax appropriate for the current language\n\
2788 (VAR = EXP or VAR := EXP for example).\n\
2789 VAR may be a debugger \"convenience\" variable (names starting\n\
2790 with $), a register (a few standard names starting with $), or an actual\n\
2791 variable in the program being debugged. EXP is any valid expression.\n\
2792 Use \"set variable\" for variables with names identical to set subcommands.\n\
2794 With a subcommand, this command modifies parts of the gdb environment.\n\
2795 You can see these environment settings with the \"show\" command."),
2796 &setlist
, "set ", 1, &cmdlist
);
2798 add_com ("assign", class_vars
, set_command
, _("\
2799 Evaluate expression EXP and assign result to variable VAR.\n\
2800 Usage: assign VAR = EXP\n\
2801 This uses assignment syntax appropriate for the current language\n\
2802 (VAR = EXP or VAR := EXP for example).\n\
2803 VAR may be a debugger \"convenience\" variable (names starting\n\
2804 with $), a register (a few standard names starting with $), or an actual\n\
2805 variable in the program being debugged. EXP is any valid expression.\n\
2806 Use \"set variable\" for variables with names identical to set subcommands.\n\
2807 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2808 You can see these environment settings with the \"show\" command."));
2810 /* "call" is the same as "set", but handy for dbx users to call fns. */
2811 c
= add_com ("call", class_vars
, call_command
, _("\
2812 Call a function in the program.\n\
2814 The argument is the function name and arguments, in the notation of the\n\
2815 current working language. The result is printed and saved in the value\n\
2816 history, if it is not void."));
2817 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2819 add_cmd ("variable", class_vars
, set_command
, _("\
2820 Evaluate expression EXP and assign result to variable VAR.\n\
2821 Usage: set variable VAR = EXP\n\
2822 This uses assignment syntax appropriate for the current language\n\
2823 (VAR = EXP or VAR := EXP for example).\n\
2824 VAR may be a debugger \"convenience\" variable (names starting\n\
2825 with $), a register (a few standard names starting with $), or an actual\n\
2826 variable in the program being debugged. EXP is any valid expression.\n\
2827 This may usually be abbreviated to simply \"set\"."),
2829 add_alias_cmd ("var", "variable", class_vars
, 0, &setlist
);
2831 const auto print_opts
= make_value_print_options_def_group (nullptr);
2833 static const std::string print_help
= gdb::option::build_help (_("\
2834 Print value of expression EXP.\n\
2835 Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
2840 Note: because this command accepts arbitrary expressions, if you\n\
2841 specify any command option, you must use a double dash (\"--\")\n\
2842 to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
2844 Variables accessible are those of the lexical environment of the selected\n\
2845 stack frame, plus all those whose scope is global or an entire file.\n\
2847 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2848 $$NUM refers to NUM'th value back from the last one.\n\
2849 Names starting with $ refer to registers (with the values they would have\n\
2850 if the program were to return to the stack frame now selected, restoring\n\
2851 all registers saved by frames farther in) or else to debugger\n\
2852 \"convenience\" variables (any such name not a known register).\n\
2853 Use assignment expressions to give values to convenience variables.\n\
2855 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2856 @ is a binary operator for treating consecutive data objects\n\
2857 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2858 element is FOO, whose second element is stored in the space following\n\
2859 where FOO is stored, etc. FOO must be an expression whose value\n\
2860 resides in memory.\n\
2862 EXP may be preceded with /FMT, where FMT is a format letter\n\
2863 but no count or size letter (see \"x\" command)."),
2866 c
= add_com ("print", class_vars
, print_command
, print_help
.c_str ());
2867 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2868 add_com_alias ("p", "print", class_vars
, 1);
2869 add_com_alias ("inspect", "print", class_vars
, 1);
2871 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2872 &max_symbolic_offset
, _("\
2873 Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2874 Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2875 Tell GDB to only display the symbolic form of an address if the\n\
2876 offset between the closest earlier symbol and the address is less than\n\
2877 the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2878 to always print the symbolic form of an address if any symbol precedes\n\
2879 it. Zero is equivalent to \"unlimited\"."),
2881 show_max_symbolic_offset
,
2882 &setprintlist
, &showprintlist
);
2883 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2884 &print_symbol_filename
, _("\
2885 Set printing of source filename and line number with <SYMBOL>."), _("\
2886 Show printing of source filename and line number with <SYMBOL>."), NULL
,
2888 show_print_symbol_filename
,
2889 &setprintlist
, &showprintlist
);
2891 add_com ("eval", no_class
, eval_command
, _("\
2892 Construct a GDB command and then evaluate it.\n\
2893 Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2894 Convert the arguments to a string as \"printf\" would, but then\n\
2895 treat this string as a command line, and evaluate it."));