1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986-2021 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"
56 #include "safe-ctype.h"
58 /* Last specified output format. */
60 static char last_format
= 0;
62 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
64 static char last_size
= 'w';
66 /* Last specified count for the 'x' command. */
68 static int last_count
;
70 /* Default address to examine next, and associated architecture. */
72 static struct gdbarch
*next_gdbarch
;
73 static CORE_ADDR next_address
;
75 /* Number of delay instructions following current disassembled insn. */
77 static int branch_delay_insns
;
79 /* Last address examined. */
81 static CORE_ADDR last_examine_address
;
83 /* Contents of last address examined.
84 This is not valid past the end of the `x' command! */
86 static value_ref_ptr last_examine_value
;
88 /* Largest offset between a symbolic value and an address, that will be
89 printed as `0x1234 <symbol+offset>'. */
91 static unsigned int max_symbolic_offset
= UINT_MAX
;
93 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
94 struct cmd_list_element
*c
, const char *value
)
96 fprintf_filtered (file
,
97 _("The largest offset that will be "
98 "printed in <symbol+1234> form is %s.\n"),
102 /* Append the source filename and linenumber of the symbol when
103 printing a symbolic value as `<symbol at filename:linenum>' if set. */
104 static bool print_symbol_filename
= false;
106 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
107 struct cmd_list_element
*c
, const char *value
)
109 fprintf_filtered (file
, _("Printing of source filename and "
110 "line number with <symbol> is %s.\n"),
114 /* Number of auto-display expression currently being displayed.
115 So that we can disable it if we get a signal within it.
116 -1 when not doing one. */
118 static int current_display_number
;
120 /* Last allocated display number. */
122 static int display_number
;
126 display (const char *exp_string_
, expression_up
&&exp_
,
127 const struct format_data
&format_
, struct program_space
*pspace_
,
128 const struct block
*block_
)
129 : exp_string (exp_string_
),
130 exp (std::move (exp_
)),
131 number (++display_number
),
139 /* The expression as the user typed it. */
140 std::string exp_string
;
142 /* Expression to be evaluated and displayed. */
145 /* Item number of this auto-display item. */
148 /* Display format specified. */
149 struct format_data format
;
151 /* Program space associated with `block'. */
152 struct program_space
*pspace
;
154 /* Innermost block required by this expression when evaluated. */
155 const struct block
*block
;
157 /* Status of this display (enabled or disabled). */
161 /* Expressions whose values should be displayed automatically each
162 time the program stops. */
164 static std::vector
<std::unique_ptr
<struct display
>> all_displays
;
166 /* Prototypes for local functions. */
168 static void do_one_display (struct display
*);
171 /* Decode a format specification. *STRING_PTR should point to it.
172 OFORMAT and OSIZE are used as defaults for the format and size
173 if none are given in the format specification.
174 If OSIZE is zero, then the size field of the returned value
175 should be set only if a size is explicitly specified by the
177 The structure returned describes all the data
178 found in the specification. In addition, *STRING_PTR is advanced
179 past the specification and past all whitespace following it. */
181 static struct format_data
182 decode_format (const char **string_ptr
, int oformat
, int osize
)
184 struct format_data val
;
185 const char *p
= *string_ptr
;
197 if (*p
>= '0' && *p
<= '9')
198 val
.count
*= atoi (p
);
199 while (*p
>= '0' && *p
<= '9')
202 /* Now process size or format letters that follow. */
206 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
213 else if (*p
>= 'a' && *p
<= 'z')
219 *string_ptr
= skip_spaces (p
);
221 /* Set defaults for format and size if not specified. */
222 if (val
.format
== '?')
226 /* Neither has been specified. */
227 val
.format
= oformat
;
231 /* If a size is specified, any format makes a reasonable
232 default except 'i'. */
233 val
.format
= oformat
== 'i' ? 'x' : oformat
;
235 else if (val
.size
== '?')
239 /* Pick the appropriate size for an address. This is deferred
240 until do_examine when we know the actual architecture to use.
241 A special size value of 'a' is used to indicate this case. */
242 val
.size
= osize
? 'a' : osize
;
245 /* Floating point has to be word or giantword. */
246 if (osize
== 'w' || osize
== 'g')
249 /* Default it to giantword if the last used size is not
251 val
.size
= osize
? 'g' : osize
;
254 /* Characters default to one byte. */
255 val
.size
= osize
? 'b' : osize
;
258 /* Display strings with byte size chars unless explicitly
264 /* The default is the size most recently specified. */
271 /* Print value VAL on stream according to OPTIONS.
272 Do not end with a newline.
273 SIZE is the letter for the size of datum being printed.
274 This is used to pad hex numbers so they line up. SIZE is 0
275 for print / output and set for examine. */
278 print_formatted (struct value
*val
, int size
,
279 const struct value_print_options
*options
,
280 struct ui_file
*stream
)
282 struct type
*type
= check_typedef (value_type (val
));
283 int len
= TYPE_LENGTH (type
);
285 if (VALUE_LVAL (val
) == lval_memory
)
286 next_address
= value_address (val
) + len
;
290 switch (options
->format
)
294 struct type
*elttype
= value_type (val
);
296 next_address
= (value_address (val
)
297 + val_print_string (elttype
, NULL
,
298 value_address (val
), -1,
299 stream
, options
) * len
);
304 /* We often wrap here if there are long symbolic names. */
306 next_address
= (value_address (val
)
307 + gdb_print_insn (type
->arch (),
308 value_address (val
), stream
,
309 &branch_delay_insns
));
314 if (options
->format
== 0 || options
->format
== 's'
315 || type
->code () == TYPE_CODE_VOID
316 || type
->code () == TYPE_CODE_REF
317 || type
->code () == TYPE_CODE_ARRAY
318 || type
->code () == TYPE_CODE_STRING
319 || type
->code () == TYPE_CODE_STRUCT
320 || type
->code () == TYPE_CODE_UNION
321 || type
->code () == TYPE_CODE_NAMESPACE
)
322 value_print (val
, stream
, options
);
324 /* User specified format, so don't look to the type to tell us
326 value_print_scalar_formatted (val
, options
, size
, stream
);
329 /* Return builtin floating point type of same length as TYPE.
330 If no such type is found, return TYPE itself. */
332 float_type_from_length (struct type
*type
)
334 struct gdbarch
*gdbarch
= type
->arch ();
335 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
337 if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_float
))
338 type
= builtin
->builtin_float
;
339 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_double
))
340 type
= builtin
->builtin_double
;
341 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_long_double
))
342 type
= builtin
->builtin_long_double
;
347 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
348 according to OPTIONS and SIZE on STREAM. Formats s and i are not
349 supported at this level. */
352 print_scalar_formatted (const gdb_byte
*valaddr
, struct type
*type
,
353 const struct value_print_options
*options
,
354 int size
, struct ui_file
*stream
)
356 struct gdbarch
*gdbarch
= type
->arch ();
357 unsigned int len
= TYPE_LENGTH (type
);
358 enum bfd_endian byte_order
= type_byte_order (type
);
360 /* String printing should go through val_print_scalar_formatted. */
361 gdb_assert (options
->format
!= 's');
363 /* If the value is a pointer, and pointers and addresses are not the
364 same, then at this point, the value's length (in target bytes) is
365 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
366 if (type
->code () == TYPE_CODE_PTR
)
367 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
369 /* If we are printing it as unsigned, truncate it in case it is actually
370 a negative signed value (e.g. "print/u (short)-1" should print 65535
371 (if shorts are 16 bits) instead of 4294967295). */
372 if (options
->format
!= 'c'
373 && (options
->format
!= 'd' || type
->is_unsigned ()))
375 if (len
< TYPE_LENGTH (type
) && byte_order
== BFD_ENDIAN_BIG
)
376 valaddr
+= TYPE_LENGTH (type
) - len
;
379 /* Allow LEN == 0, and in this case, don't assume that VALADDR is
381 const gdb_byte zero
= 0;
388 if (size
!= 0 && (options
->format
== 'x' || options
->format
== 't'))
390 /* Truncate to fit. */
407 error (_("Undefined output size \"%c\"."), size
);
409 if (newlen
< len
&& byte_order
== BFD_ENDIAN_BIG
)
410 valaddr
+= len
- newlen
;
414 /* Historically gdb has printed floats by first casting them to a
415 long, and then printing the long. PR cli/16242 suggests changing
416 this to using C-style hex float format.
418 Biased range types and sub-word scalar types must also be handled
419 here; the value is correctly computed by unpack_long. */
420 gdb::byte_vector converted_bytes
;
421 /* Some cases below will unpack the value again. In the biased
422 range case, we want to avoid this, so we store the unpacked value
423 here for possible use later. */
424 gdb::optional
<LONGEST
> val_long
;
425 if (((type
->code () == TYPE_CODE_FLT
426 || is_fixed_point_type (type
))
427 && (options
->format
== 'o'
428 || options
->format
== 'x'
429 || options
->format
== 't'
430 || options
->format
== 'z'
431 || options
->format
== 'd'
432 || options
->format
== 'u'))
433 || (type
->code () == TYPE_CODE_RANGE
&& type
->bounds ()->bias
!= 0)
434 || type
->bit_size_differs_p ())
436 val_long
.emplace (unpack_long (type
, valaddr
));
437 converted_bytes
.resize (TYPE_LENGTH (type
));
438 store_signed_integer (converted_bytes
.data (), TYPE_LENGTH (type
),
439 byte_order
, *val_long
);
440 valaddr
= converted_bytes
.data ();
443 /* Printing a non-float type as 'f' will interpret the data as if it were
444 of a floating-point type of the same length, if that exists. Otherwise,
445 the data is printed as integer. */
446 char format
= options
->format
;
447 if (format
== 'f' && type
->code () != TYPE_CODE_FLT
)
449 type
= float_type_from_length (type
);
450 if (type
->code () != TYPE_CODE_FLT
)
457 print_octal_chars (stream
, valaddr
, len
, byte_order
);
460 print_decimal_chars (stream
, valaddr
, len
, true, byte_order
);
463 print_decimal_chars (stream
, valaddr
, len
, false, byte_order
);
466 if (type
->code () != TYPE_CODE_FLT
)
468 print_decimal_chars (stream
, valaddr
, len
, !type
->is_unsigned (),
474 print_floating (valaddr
, type
, stream
);
478 print_binary_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
481 print_hex_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
484 print_hex_chars (stream
, valaddr
, len
, byte_order
, true);
488 struct value_print_options opts
= *options
;
490 if (!val_long
.has_value ())
491 val_long
.emplace (unpack_long (type
, valaddr
));
494 if (type
->is_unsigned ())
495 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
497 type
= builtin_type (gdbarch
)->builtin_true_char
;
499 value_print (value_from_longest (type
, *val_long
), stream
, &opts
);
505 if (!val_long
.has_value ())
506 val_long
.emplace (unpack_long (type
, valaddr
));
507 print_address (gdbarch
, *val_long
, stream
);
512 error (_("Undefined output format \"%c\"."), format
);
516 /* Specify default address for `x' command.
517 The `info lines' command uses this. */
520 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
522 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
524 next_gdbarch
= gdbarch
;
527 /* Make address available to the user as $_. */
528 set_internalvar (lookup_internalvar ("_"),
529 value_from_pointer (ptr_type
, addr
));
532 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
533 after LEADIN. Print nothing if no symbolic name is found nearby.
534 Optionally also print source file and line number, if available.
535 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
536 or to interpret it as a possible C++ name and convert it back to source
537 form. However note that DO_DEMANGLE can be overridden by the specific
538 settings of the demangle and asm_demangle variables. Returns
539 non-zero if anything was printed; zero otherwise. */
542 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
543 struct ui_file
*stream
,
544 int do_demangle
, const char *leadin
)
546 std::string name
, filename
;
551 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, false, &name
,
552 &offset
, &filename
, &line
, &unmapped
))
555 fputs_filtered (leadin
, stream
);
557 fputs_filtered ("<*", stream
);
559 fputs_filtered ("<", stream
);
560 fputs_styled (name
.c_str (), function_name_style
.style (), stream
);
562 fprintf_filtered (stream
, "%+d", offset
);
564 /* Append source filename and line number if desired. Give specific
565 line # of this addr, if we have it; else line # of the nearest symbol. */
566 if (print_symbol_filename
&& !filename
.empty ())
568 fputs_filtered (line
== -1 ? " in " : " at ", stream
);
569 fputs_styled (filename
.c_str (), file_name_style
.style (), stream
);
571 fprintf_filtered (stream
, ":%d", line
);
574 fputs_filtered ("*>", stream
);
576 fputs_filtered (">", stream
);
581 /* See valprint.h. */
584 build_address_symbolic (struct gdbarch
*gdbarch
,
585 CORE_ADDR addr
, /* IN */
586 bool do_demangle
, /* IN */
587 bool prefer_sym_over_minsym
, /* IN */
588 std::string
*name
, /* OUT */
589 int *offset
, /* OUT */
590 std::string
*filename
, /* OUT */
592 int *unmapped
) /* OUT */
594 struct bound_minimal_symbol msymbol
;
595 struct symbol
*symbol
;
596 CORE_ADDR name_location
= 0;
597 struct obj_section
*section
= NULL
;
598 const char *name_temp
= "";
600 /* Let's say it is mapped (not unmapped). */
603 /* Determine if the address is in an overlay, and whether it is
605 if (overlay_debugging
)
607 section
= find_pc_overlay (addr
);
608 if (pc_in_unmapped_range (addr
, section
))
611 addr
= overlay_mapped_address (addr
, section
);
615 /* Try to find the address in both the symbol table and the minsyms.
616 In most cases, we'll prefer to use the symbol instead of the
617 minsym. However, there are cases (see below) where we'll choose
618 to use the minsym instead. */
620 /* This is defective in the sense that it only finds text symbols. So
621 really this is kind of pointless--we should make sure that the
622 minimal symbols have everything we need (by changing that we could
623 save some memory, but for many debug format--ELF/DWARF or
624 anything/stabs--it would be inconvenient to eliminate those minimal
626 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
627 symbol
= find_pc_sect_function (addr
, section
);
631 /* If this is a function (i.e. a code address), strip out any
632 non-address bits. For instance, display a pointer to the
633 first instruction of a Thumb function as <function>; the
634 second instruction will be <function+2>, even though the
635 pointer is <function+3>. This matches the ISA behavior. */
636 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
638 name_location
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol
));
639 if (do_demangle
|| asm_demangle
)
640 name_temp
= symbol
->print_name ();
642 name_temp
= symbol
->linkage_name ();
645 if (msymbol
.minsym
!= NULL
646 && MSYMBOL_HAS_SIZE (msymbol
.minsym
)
647 && MSYMBOL_SIZE (msymbol
.minsym
) == 0
648 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text
649 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text_gnu_ifunc
650 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_file_text
)
651 msymbol
.minsym
= NULL
;
653 if (msymbol
.minsym
!= NULL
)
655 /* Use the minsym if no symbol is found.
657 Additionally, use the minsym instead of a (found) symbol if
658 the following conditions all hold:
659 1) The prefer_sym_over_minsym flag is false.
660 2) The minsym address is identical to that of the address under
662 3) The symbol address is not identical to that of the address
663 under consideration. */
664 if (symbol
== NULL
||
665 (!prefer_sym_over_minsym
666 && BMSYMBOL_VALUE_ADDRESS (msymbol
) == addr
667 && name_location
!= addr
))
669 /* If this is a function (i.e. a code address), strip out any
670 non-address bits. For instance, display a pointer to the
671 first instruction of a Thumb function as <function>; the
672 second instruction will be <function+2>, even though the
673 pointer is <function+3>. This matches the ISA behavior. */
674 if (MSYMBOL_TYPE (msymbol
.minsym
) == mst_text
675 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_text_gnu_ifunc
676 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_file_text
677 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_solib_trampoline
)
678 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
681 name_location
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
682 if (do_demangle
|| asm_demangle
)
683 name_temp
= msymbol
.minsym
->print_name ();
685 name_temp
= msymbol
.minsym
->linkage_name ();
688 if (symbol
== NULL
&& msymbol
.minsym
== NULL
)
691 /* If the nearest symbol is too far away, don't print anything symbolic. */
693 /* For when CORE_ADDR is larger than unsigned int, we do math in
694 CORE_ADDR. But when we detect unsigned wraparound in the
695 CORE_ADDR math, we ignore this test and print the offset,
696 because addr+max_symbolic_offset has wrapped through the end
697 of the address space back to the beginning, giving bogus comparison. */
698 if (addr
> name_location
+ max_symbolic_offset
699 && name_location
+ max_symbolic_offset
> name_location
)
702 *offset
= (LONGEST
) addr
- name_location
;
706 if (print_symbol_filename
)
708 struct symtab_and_line sal
;
710 sal
= find_pc_sect_line (addr
, section
, 0);
714 *filename
= symtab_to_filename_for_display (sal
.symtab
);
722 /* Print address ADDR symbolically on STREAM.
723 First print it as a number. Then perhaps print
724 <SYMBOL + OFFSET> after the number. */
727 print_address (struct gdbarch
*gdbarch
,
728 CORE_ADDR addr
, struct ui_file
*stream
)
730 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
731 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
734 /* Return a prefix for instruction address:
735 "=> " for current instruction, else " ". */
738 pc_prefix (CORE_ADDR addr
)
740 if (has_stack_frames ())
742 struct frame_info
*frame
;
745 frame
= get_selected_frame (NULL
);
746 if (get_frame_pc_if_available (frame
, &pc
) && pc
== addr
)
752 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
753 controls whether to print the symbolic name "raw" or demangled.
754 Return non-zero if anything was printed; zero otherwise. */
757 print_address_demangle (const struct value_print_options
*opts
,
758 struct gdbarch
*gdbarch
, CORE_ADDR addr
,
759 struct ui_file
*stream
, int do_demangle
)
761 if (opts
->addressprint
)
763 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
764 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
768 return print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
774 /* Find the address of the instruction that is INST_COUNT instructions before
775 the instruction at ADDR.
776 Since some architectures have variable-length instructions, we can't just
777 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
778 number information to locate the nearest known instruction boundary,
779 and disassemble forward from there. If we go out of the symbol range
780 during disassembling, we return the lowest address we've got so far and
781 set the number of instructions read to INST_READ. */
784 find_instruction_backward (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
785 int inst_count
, int *inst_read
)
787 /* The vector PCS is used to store instruction addresses within
789 CORE_ADDR loop_start
, loop_end
, p
;
790 std::vector
<CORE_ADDR
> pcs
;
791 struct symtab_and_line sal
;
794 loop_start
= loop_end
= addr
;
796 /* In each iteration of the outer loop, we get a pc range that ends before
797 LOOP_START, then we count and store every instruction address of the range
798 iterated in the loop.
799 If the number of instructions counted reaches INST_COUNT, return the
800 stored address that is located INST_COUNT instructions back from ADDR.
801 If INST_COUNT is not reached, we subtract the number of counted
802 instructions from INST_COUNT, and go to the next iteration. */
806 sal
= find_pc_sect_line (loop_start
, NULL
, 1);
809 /* We reach here when line info is not available. In this case,
810 we print a message and just exit the loop. The return value
811 is calculated after the loop. */
812 printf_filtered (_("No line number information available "
815 print_address (gdbarch
, loop_start
- 1, gdb_stdout
);
816 printf_filtered ("\n");
820 loop_end
= loop_start
;
823 /* This loop pushes instruction addresses in the range from
824 LOOP_START to LOOP_END. */
825 for (p
= loop_start
; p
< loop_end
;)
828 p
+= gdb_insn_length (gdbarch
, p
);
831 inst_count
-= pcs
.size ();
832 *inst_read
+= pcs
.size ();
834 while (inst_count
> 0);
836 /* After the loop, the vector PCS has instruction addresses of the last
837 source line we processed, and INST_COUNT has a negative value.
838 We return the address at the index of -INST_COUNT in the vector for
840 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
850 find_instruction_backward is called with INST_COUNT = 4 and expected to
851 return 0x4001. When we reach here, INST_COUNT is set to -1 because
852 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
853 4001 is located at the index 1 of the last iterated line (= Line X),
854 which is simply calculated by -INST_COUNT.
855 The case when the length of PCS is 0 means that we reached an area for
856 which line info is not available. In such case, we return LOOP_START,
857 which was the lowest instruction address that had line info. */
858 p
= pcs
.size () > 0 ? pcs
[-inst_count
] : loop_start
;
860 /* INST_READ includes all instruction addresses in a pc range. Need to
861 exclude the beginning part up to the address we're returning. That
862 is, exclude {0x4000} in the example above. */
864 *inst_read
+= inst_count
;
869 /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
870 placing the results in GDB's memory from MYADDR + LEN. Returns
871 a count of the bytes actually read. */
874 read_memory_backward (struct gdbarch
*gdbarch
,
875 CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
878 int nread
; /* Number of bytes actually read. */
880 /* First try a complete read. */
881 errcode
= target_read_memory (memaddr
, myaddr
, len
);
889 /* Loop, reading one byte at a time until we get as much as we can. */
892 for (nread
= 0; nread
< len
; ++nread
)
894 errcode
= target_read_memory (--memaddr
, --myaddr
, 1);
897 /* The read was unsuccessful, so exit the loop. */
898 printf_filtered (_("Cannot access memory at address %s\n"),
899 paddress (gdbarch
, memaddr
));
907 /* Returns true if X (which is LEN bytes wide) is the number zero. */
910 integer_is_zero (const gdb_byte
*x
, int len
)
914 while (i
< len
&& x
[i
] == 0)
919 /* Find the start address of a string in which ADDR is included.
920 Basically we search for '\0' and return the next address,
921 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
922 we stop searching and return the address to print characters as many as
923 PRINT_MAX from the string. */
926 find_string_backward (struct gdbarch
*gdbarch
,
927 CORE_ADDR addr
, int count
, int char_size
,
928 const struct value_print_options
*options
,
929 int *strings_counted
)
931 const int chunk_size
= 0x20;
934 int chars_to_read
= chunk_size
;
935 int chars_counted
= 0;
936 int count_original
= count
;
937 CORE_ADDR string_start_addr
= addr
;
939 gdb_assert (char_size
== 1 || char_size
== 2 || char_size
== 4);
940 gdb::byte_vector
buffer (chars_to_read
* char_size
);
941 while (count
> 0 && read_error
== 0)
945 addr
-= chars_to_read
* char_size
;
946 chars_read
= read_memory_backward (gdbarch
, addr
, buffer
.data (),
947 chars_to_read
* char_size
);
948 chars_read
/= char_size
;
949 read_error
= (chars_read
== chars_to_read
) ? 0 : 1;
950 /* Searching for '\0' from the end of buffer in backward direction. */
951 for (i
= 0; i
< chars_read
&& count
> 0 ; ++i
, ++chars_counted
)
953 int offset
= (chars_to_read
- i
- 1) * char_size
;
955 if (integer_is_zero (&buffer
[offset
], char_size
)
956 || chars_counted
== options
->print_max
)
958 /* Found '\0' or reached print_max. As OFFSET is the offset to
959 '\0', we add CHAR_SIZE to return the start address of
962 string_start_addr
= addr
+ offset
+ char_size
;
968 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
969 *strings_counted
= count_original
- count
;
973 /* In error case, STRING_START_ADDR is pointing to the string that
974 was last successfully loaded. Rewind the partially loaded string. */
975 string_start_addr
-= chars_counted
* char_size
;
978 return string_start_addr
;
981 /* Examine data at address ADDR in format FMT.
982 Fetch it from memory and print on gdb_stdout. */
985 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
990 struct type
*val_type
= NULL
;
993 struct value_print_options opts
;
994 int need_to_update_next_address
= 0;
995 CORE_ADDR addr_rewound
= 0;
1000 next_gdbarch
= gdbarch
;
1001 next_address
= addr
;
1003 /* Instruction format implies fetch single bytes
1004 regardless of the specified size.
1005 The case of strings is handled in decode_format, only explicit
1006 size operator are not changed to 'b'. */
1012 /* Pick the appropriate size for an address. */
1013 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
1015 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
1017 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
1020 /* Bad value for gdbarch_ptr_bit. */
1021 internal_error (__FILE__
, __LINE__
,
1022 _("failed internal consistency check"));
1026 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1027 else if (size
== 'h')
1028 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
1029 else if (size
== 'w')
1030 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
1031 else if (size
== 'g')
1032 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
1036 struct type
*char_type
= NULL
;
1038 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1039 if type is not found. */
1041 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
1042 else if (size
== 'w')
1043 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
1045 val_type
= char_type
;
1048 if (size
!= '\0' && size
!= 'b')
1049 warning (_("Unable to display strings with "
1050 "size '%c', using 'b' instead."), size
);
1052 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1061 if (format
== 's' || format
== 'i')
1064 get_formatted_print_options (&opts
, format
);
1068 /* This is the negative repeat count case.
1069 We rewind the address based on the given repeat count and format,
1070 then examine memory from there in forward direction. */
1075 next_address
= find_instruction_backward (gdbarch
, addr
, count
,
1078 else if (format
== 's')
1080 next_address
= find_string_backward (gdbarch
, addr
, count
,
1081 TYPE_LENGTH (val_type
),
1086 next_address
= addr
- count
* TYPE_LENGTH (val_type
);
1089 /* The following call to print_formatted updates next_address in every
1090 iteration. In backward case, we store the start address here
1091 and update next_address with it before exiting the function. */
1092 addr_rewound
= (format
== 's'
1093 ? next_address
- TYPE_LENGTH (val_type
)
1095 need_to_update_next_address
= 1;
1098 /* Print as many objects as specified in COUNT, at most maxelts per line,
1099 with the address of the next one at the start of each line. */
1105 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
1106 print_address (next_gdbarch
, next_address
, gdb_stdout
);
1107 printf_filtered (":");
1112 printf_filtered ("\t");
1113 /* Note that print_formatted sets next_address for the next
1115 last_examine_address
= next_address
;
1117 /* The value to be displayed is not fetched greedily.
1118 Instead, to avoid the possibility of a fetched value not
1119 being used, its retrieval is delayed until the print code
1120 uses it. When examining an instruction stream, the
1121 disassembler will perform its own memory fetch using just
1122 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1123 the disassembler be modified so that LAST_EXAMINE_VALUE
1124 is left with the byte sequence from the last complete
1125 instruction fetched from memory? */
1127 = release_value (value_at_lazy (val_type
, next_address
));
1129 print_formatted (last_examine_value
.get (), size
, &opts
, gdb_stdout
);
1131 /* Display any branch delay slots following the final insn. */
1132 if (format
== 'i' && count
== 1)
1133 count
+= branch_delay_insns
;
1135 printf_filtered ("\n");
1138 if (need_to_update_next_address
)
1139 next_address
= addr_rewound
;
1143 validate_format (struct format_data fmt
, const char *cmdname
)
1146 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
1148 error (_("Item count other than 1 is meaningless in \"%s\" command."),
1150 if (fmt
.format
== 'i')
1151 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
1152 fmt
.format
, cmdname
);
1155 /* Parse print command format string into *OPTS and update *EXPP.
1156 CMDNAME should name the current command. */
1159 print_command_parse_format (const char **expp
, const char *cmdname
,
1160 value_print_options
*opts
)
1162 const char *exp
= *expp
;
1164 /* opts->raw value might already have been set by 'set print raw-values'
1165 or by using 'print -raw-values'.
1166 So, do not set opts->raw to 0, only set it to 1 if /r is given. */
1167 if (exp
&& *exp
== '/')
1172 fmt
= decode_format (&exp
, last_format
, 0);
1173 validate_format (fmt
, cmdname
);
1174 last_format
= fmt
.format
;
1176 opts
->format
= fmt
.format
;
1177 opts
->raw
= opts
->raw
|| fmt
.raw
;
1187 /* See valprint.h. */
1190 print_value (value
*val
, const value_print_options
&opts
)
1192 int histindex
= record_latest_value (val
);
1194 annotate_value_history_begin (histindex
, value_type (val
));
1196 printf_filtered ("$%d = ", histindex
);
1198 annotate_value_history_value ();
1200 print_formatted (val
, 0, &opts
, gdb_stdout
);
1201 printf_filtered ("\n");
1203 annotate_value_history_end ();
1206 /* Implementation of the "print" and "call" commands. */
1209 print_command_1 (const char *args
, bool voidprint
)
1212 value_print_options print_opts
;
1214 get_user_print_options (&print_opts
);
1215 /* Override global settings with explicit options, if any. */
1216 auto group
= make_value_print_options_def_group (&print_opts
);
1217 gdb::option::process_options
1218 (&args
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
1220 print_command_parse_format (&args
, "print", &print_opts
);
1222 const char *exp
= args
;
1224 if (exp
!= nullptr && *exp
)
1226 /* VOIDPRINT is true to indicate that we do want to print a void
1227 value, so invert it for parse_expression. */
1228 expression_up expr
= parse_expression (exp
, nullptr, !voidprint
);
1229 val
= evaluate_expression (expr
.get ());
1232 val
= access_value_history (0);
1234 if (voidprint
|| (val
&& value_type (val
) &&
1235 value_type (val
)->code () != TYPE_CODE_VOID
))
1236 print_value (val
, print_opts
);
1239 /* Called from command completion function to skip over /FMT
1240 specifications, allowing the rest of the line to be completed. Returns
1241 true if the /FMT is at the end of the current line and there is nothing
1242 left to complete, otherwise false is returned.
1244 In either case *ARGS can be updated to point after any part of /FMT that
1247 This function is designed so that trying to complete '/' will offer no
1248 completions, the user needs to insert the format specification
1249 themselves. Trying to complete '/FMT' (where FMT is any non-empty set
1250 of alpha-numeric characters) will cause readline to insert a single
1251 space, setting the user up to enter the expression. */
1254 skip_over_slash_fmt (completion_tracker
&tracker
, const char **args
)
1256 const char *text
= *args
;
1261 tracker
.set_use_custom_word_point (true);
1263 if (text
[1] == '\0')
1265 /* The user tried to complete after typing just the '/' character
1266 of the /FMT string. Step the completer past the '/', but we
1267 don't offer any completions. */
1273 /* The user has typed some characters after the '/', we assume
1274 this is a complete /FMT string, first skip over it. */
1275 text
= skip_to_space (text
);
1279 /* We're at the end of the input string. The user has typed
1280 '/FMT' and asked for a completion. Push an empty
1281 completion string, this will cause readline to insert a
1282 space so the user now has '/FMT '. */
1284 tracker
.add_completion (make_unique_xstrdup (text
));
1288 /* The user has already typed things after the /FMT, skip the
1289 whitespace and return false. Whoever called this function
1290 should then try to complete what comes next. */
1292 text
= skip_spaces (text
);
1296 tracker
.advance_custom_word_point_by (text
- *args
);
1304 /* See valprint.h. */
1307 print_command_completer (struct cmd_list_element
*ignore
,
1308 completion_tracker
&tracker
,
1309 const char *text
, const char * /*word*/)
1311 const auto group
= make_value_print_options_def_group (nullptr);
1312 if (gdb::option::complete_options
1313 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
))
1316 if (skip_over_slash_fmt (tracker
, &text
))
1319 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1320 expression_completer (ignore
, tracker
, text
, word
);
1324 print_command (const char *exp
, int from_tty
)
1326 print_command_1 (exp
, true);
1329 /* Same as print, except it doesn't print void results. */
1331 call_command (const char *exp
, int from_tty
)
1333 print_command_1 (exp
, false);
1336 /* Implementation of the "output" command. */
1339 output_command (const char *exp
, int from_tty
)
1343 struct format_data fmt
;
1344 struct value_print_options opts
;
1349 if (exp
&& *exp
== '/')
1352 fmt
= decode_format (&exp
, 0, 0);
1353 validate_format (fmt
, "output");
1354 format
= fmt
.format
;
1357 expression_up expr
= parse_expression (exp
);
1359 val
= evaluate_expression (expr
.get ());
1361 annotate_value_begin (value_type (val
));
1363 get_formatted_print_options (&opts
, format
);
1365 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1367 annotate_value_end ();
1370 gdb_flush (gdb_stdout
);
1374 set_command (const char *exp
, int from_tty
)
1376 expression_up expr
= parse_expression (exp
);
1378 enum exp_opcode opcode
= OP_NULL
;
1379 if (expr
->op
!= nullptr)
1380 opcode
= expr
->op
->opcode ();
1381 else if (expr
->nelts
>= 1)
1382 opcode
= expr
->elts
[0].opcode
;
1384 if (opcode
!= OP_NULL
)
1387 case UNOP_PREINCREMENT
:
1388 case UNOP_POSTINCREMENT
:
1389 case UNOP_PREDECREMENT
:
1390 case UNOP_POSTDECREMENT
:
1392 case BINOP_ASSIGN_MODIFY
:
1397 (_("Expression is not an assignment (and might have no effect)"));
1400 evaluate_expression (expr
.get ());
1404 info_symbol_command (const char *arg
, int from_tty
)
1406 struct minimal_symbol
*msymbol
;
1407 struct obj_section
*osect
;
1408 CORE_ADDR addr
, sect_addr
;
1410 unsigned int offset
;
1413 error_no_arg (_("address"));
1415 addr
= parse_and_eval_address (arg
);
1416 for (objfile
*objfile
: current_program_space
->objfiles ())
1417 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
1419 /* Only process each object file once, even if there's a separate
1421 if (objfile
->separate_debug_objfile_backlink
)
1424 sect_addr
= overlay_mapped_address (addr
, osect
);
1426 if (obj_section_addr (osect
) <= sect_addr
1427 && sect_addr
< obj_section_endaddr (osect
)
1429 = lookup_minimal_symbol_by_pc_section (sect_addr
,
1432 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1433 const char *loc_string
;
1436 offset
= sect_addr
- MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
1437 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1438 sec_name
= osect
->the_bfd_section
->name
;
1439 msym_name
= msymbol
->print_name ();
1441 /* Don't print the offset if it is zero.
1442 We assume there's no need to handle i18n of "sym + offset". */
1443 std::string string_holder
;
1446 string_holder
= string_printf ("%s + %u", msym_name
, offset
);
1447 loc_string
= string_holder
.c_str ();
1450 loc_string
= msym_name
;
1452 gdb_assert (osect
->objfile
&& objfile_name (osect
->objfile
));
1453 obj_name
= objfile_name (osect
->objfile
);
1455 if (current_program_space
->multi_objfile_p ())
1456 if (pc_in_unmapped_range (addr
, osect
))
1457 if (section_is_overlay (osect
))
1458 printf_filtered (_("%s in load address range of "
1459 "%s overlay section %s of %s\n"),
1460 loc_string
, mapped
, sec_name
, obj_name
);
1462 printf_filtered (_("%s in load address range of "
1463 "section %s of %s\n"),
1464 loc_string
, sec_name
, obj_name
);
1466 if (section_is_overlay (osect
))
1467 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1468 loc_string
, mapped
, sec_name
, obj_name
);
1470 printf_filtered (_("%s in section %s of %s\n"),
1471 loc_string
, sec_name
, obj_name
);
1473 if (pc_in_unmapped_range (addr
, osect
))
1474 if (section_is_overlay (osect
))
1475 printf_filtered (_("%s in load address range of %s overlay "
1477 loc_string
, mapped
, sec_name
);
1480 (_("%s in load address range of section %s\n"),
1481 loc_string
, sec_name
);
1483 if (section_is_overlay (osect
))
1484 printf_filtered (_("%s in %s overlay section %s\n"),
1485 loc_string
, mapped
, sec_name
);
1487 printf_filtered (_("%s in section %s\n"),
1488 loc_string
, sec_name
);
1492 printf_filtered (_("No symbol matches %s.\n"), arg
);
1496 info_address_command (const char *exp
, int from_tty
)
1498 struct gdbarch
*gdbarch
;
1501 struct bound_minimal_symbol msymbol
;
1503 struct obj_section
*section
;
1504 CORE_ADDR load_addr
, context_pc
= 0;
1505 struct field_of_this_result is_a_field_of_this
;
1508 error (_("Argument required."));
1510 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1511 &is_a_field_of_this
).symbol
;
1514 if (is_a_field_of_this
.type
!= NULL
)
1516 printf_filtered ("Symbol \"");
1517 fprintf_symbol_filtered (gdb_stdout
, exp
,
1518 current_language
->la_language
, DMGL_ANSI
);
1519 printf_filtered ("\" is a field of the local class variable ");
1520 if (current_language
->la_language
== language_objc
)
1521 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1523 printf_filtered ("`this'\n");
1527 msymbol
= lookup_bound_minimal_symbol (exp
);
1529 if (msymbol
.minsym
!= NULL
)
1531 struct objfile
*objfile
= msymbol
.objfile
;
1533 gdbarch
= objfile
->arch ();
1534 load_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1536 printf_filtered ("Symbol \"");
1537 fprintf_symbol_filtered (gdb_stdout
, exp
,
1538 current_language
->la_language
, DMGL_ANSI
);
1539 printf_filtered ("\" is at ");
1540 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1542 printf_filtered (" in a file compiled without debugging");
1543 section
= msymbol
.minsym
->obj_section (objfile
);
1544 if (section_is_overlay (section
))
1546 load_addr
= overlay_unmapped_address (load_addr
, section
);
1547 printf_filtered (",\n -- loaded at ");
1548 fputs_styled (paddress (gdbarch
, load_addr
),
1549 address_style
.style (),
1551 printf_filtered (" in overlay section %s",
1552 section
->the_bfd_section
->name
);
1554 printf_filtered (".\n");
1557 error (_("No symbol \"%s\" in current context."), exp
);
1561 printf_filtered ("Symbol \"");
1562 fprintf_symbol_filtered (gdb_stdout
, sym
->print_name (),
1563 current_language
->la_language
, DMGL_ANSI
);
1564 printf_filtered ("\" is ");
1565 val
= SYMBOL_VALUE (sym
);
1566 if (SYMBOL_OBJFILE_OWNED (sym
))
1567 section
= sym
->obj_section (symbol_objfile (sym
));
1570 gdbarch
= symbol_arch (sym
);
1572 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
1574 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
,
1576 printf_filtered (".\n");
1580 switch (SYMBOL_CLASS (sym
))
1583 case LOC_CONST_BYTES
:
1584 printf_filtered ("constant");
1588 printf_filtered ("a label at address ");
1589 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1590 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1592 if (section_is_overlay (section
))
1594 load_addr
= overlay_unmapped_address (load_addr
, section
);
1595 printf_filtered (",\n -- loaded at ");
1596 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1598 printf_filtered (" in overlay section %s",
1599 section
->the_bfd_section
->name
);
1604 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
1607 /* GDBARCH is the architecture associated with the objfile the symbol
1608 is defined in; the target architecture may be different, and may
1609 provide additional registers. However, we do not know the target
1610 architecture at this point. We assume the objfile architecture
1611 will contain all the standard registers that occur in debug info
1613 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1615 if (SYMBOL_IS_ARGUMENT (sym
))
1616 printf_filtered (_("an argument in register %s"),
1617 gdbarch_register_name (gdbarch
, regno
));
1619 printf_filtered (_("a variable in register %s"),
1620 gdbarch_register_name (gdbarch
, regno
));
1624 printf_filtered (_("static storage at address "));
1625 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1626 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1628 if (section_is_overlay (section
))
1630 load_addr
= overlay_unmapped_address (load_addr
, section
);
1631 printf_filtered (_(",\n -- loaded at "));
1632 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1634 printf_filtered (_(" in overlay section %s"),
1635 section
->the_bfd_section
->name
);
1639 case LOC_REGPARM_ADDR
:
1640 /* Note comment at LOC_REGISTER. */
1641 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1642 printf_filtered (_("address of an argument in register %s"),
1643 gdbarch_register_name (gdbarch
, regno
));
1647 printf_filtered (_("an argument at offset %ld"), val
);
1651 printf_filtered (_("a local variable at frame offset %ld"), val
);
1655 printf_filtered (_("a reference argument at offset %ld"), val
);
1659 printf_filtered (_("a typedef"));
1663 printf_filtered (_("a function at address "));
1664 load_addr
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
));
1665 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1667 if (section_is_overlay (section
))
1669 load_addr
= overlay_unmapped_address (load_addr
, section
);
1670 printf_filtered (_(",\n -- loaded at "));
1671 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1673 printf_filtered (_(" in overlay section %s"),
1674 section
->the_bfd_section
->name
);
1678 case LOC_UNRESOLVED
:
1680 struct bound_minimal_symbol msym
;
1682 msym
= lookup_bound_minimal_symbol (sym
->linkage_name ());
1683 if (msym
.minsym
== NULL
)
1684 printf_filtered ("unresolved");
1687 section
= msym
.obj_section ();
1690 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1692 load_addr
= MSYMBOL_VALUE_RAW_ADDRESS (msym
.minsym
);
1693 printf_filtered (_("a thread-local variable at offset %s "
1694 "in the thread-local storage for `%s'"),
1695 paddress (gdbarch
, load_addr
),
1696 objfile_name (section
->objfile
));
1700 load_addr
= BMSYMBOL_VALUE_ADDRESS (msym
);
1701 printf_filtered (_("static storage at address "));
1702 fputs_styled (paddress (gdbarch
, load_addr
),
1703 address_style
.style (), gdb_stdout
);
1704 if (section_is_overlay (section
))
1706 load_addr
= overlay_unmapped_address (load_addr
, section
);
1707 printf_filtered (_(",\n -- loaded at "));
1708 fputs_styled (paddress (gdbarch
, load_addr
),
1709 address_style
.style (),
1711 printf_filtered (_(" in overlay section %s"),
1712 section
->the_bfd_section
->name
);
1719 case LOC_OPTIMIZED_OUT
:
1720 printf_filtered (_("optimized out"));
1724 printf_filtered (_("of unknown (botched) type"));
1727 printf_filtered (".\n");
1732 x_command (const char *exp
, int from_tty
)
1734 struct format_data fmt
;
1737 fmt
.format
= last_format
? last_format
: 'x';
1738 fmt
.size
= last_size
;
1742 /* If there is no expression and no format, use the most recent
1744 if (exp
== nullptr && last_count
> 0)
1745 fmt
.count
= last_count
;
1747 if (exp
&& *exp
== '/')
1749 const char *tmp
= exp
+ 1;
1751 fmt
= decode_format (&tmp
, last_format
, last_size
);
1755 last_count
= fmt
.count
;
1757 /* If we have an expression, evaluate it and use it as the address. */
1759 if (exp
!= 0 && *exp
!= 0)
1761 expression_up expr
= parse_expression (exp
);
1762 /* Cause expression not to be there any more if this command is
1763 repeated with Newline. But don't clobber a user-defined
1764 command's definition. */
1766 set_repeat_arguments ("");
1767 val
= evaluate_expression (expr
.get ());
1768 if (TYPE_IS_REFERENCE (value_type (val
)))
1769 val
= coerce_ref (val
);
1770 /* In rvalue contexts, such as this, functions are coerced into
1771 pointers to functions. This makes "x/i main" work. */
1772 if (value_type (val
)->code () == TYPE_CODE_FUNC
1773 && VALUE_LVAL (val
) == lval_memory
)
1774 next_address
= value_address (val
);
1776 next_address
= value_as_address (val
);
1778 next_gdbarch
= expr
->gdbarch
;
1782 error_no_arg (_("starting display address"));
1784 do_examine (fmt
, next_gdbarch
, next_address
);
1786 /* If the examine succeeds, we remember its size and format for next
1787 time. Set last_size to 'b' for strings. */
1788 if (fmt
.format
== 's')
1791 last_size
= fmt
.size
;
1792 last_format
= fmt
.format
;
1794 /* Set a couple of internal variables if appropriate. */
1795 if (last_examine_value
!= nullptr)
1797 /* Make last address examined available to the user as $_. Use
1798 the correct pointer type. */
1799 struct type
*pointer_type
1800 = lookup_pointer_type (value_type (last_examine_value
.get ()));
1801 set_internalvar (lookup_internalvar ("_"),
1802 value_from_pointer (pointer_type
,
1803 last_examine_address
));
1805 /* Make contents of last address examined available to the user
1806 as $__. If the last value has not been fetched from memory
1807 then don't fetch it now; instead mark it by voiding the $__
1809 if (value_lazy (last_examine_value
.get ()))
1810 clear_internalvar (lookup_internalvar ("__"));
1812 set_internalvar (lookup_internalvar ("__"), last_examine_value
.get ());
1816 /* Command completion for the 'display' and 'x' commands. */
1819 display_and_x_command_completer (struct cmd_list_element
*ignore
,
1820 completion_tracker
&tracker
,
1821 const char *text
, const char * /*word*/)
1823 if (skip_over_slash_fmt (tracker
, &text
))
1826 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1827 expression_completer (ignore
, tracker
, text
, word
);
1832 /* Add an expression to the auto-display chain.
1833 Specify the expression. */
1836 display_command (const char *arg
, int from_tty
)
1838 struct format_data fmt
;
1839 struct display
*newobj
;
1840 const char *exp
= arg
;
1851 fmt
= decode_format (&exp
, 0, 0);
1852 if (fmt
.size
&& fmt
.format
== 0)
1854 if (fmt
.format
== 'i' || fmt
.format
== 's')
1865 innermost_block_tracker tracker
;
1866 expression_up expr
= parse_expression (exp
, &tracker
);
1868 newobj
= new display (exp
, std::move (expr
), fmt
,
1869 current_program_space
, tracker
.block ());
1870 all_displays
.emplace_back (newobj
);
1873 do_one_display (newobj
);
1878 /* Clear out the display_chain. Done when new symtabs are loaded,
1879 since this invalidates the types stored in many expressions. */
1884 all_displays
.clear ();
1887 /* Delete the auto-display DISPLAY. */
1890 delete_display (struct display
*display
)
1892 gdb_assert (display
!= NULL
);
1894 auto iter
= std::find_if (all_displays
.begin (),
1895 all_displays
.end (),
1896 [=] (const std::unique_ptr
<struct display
> &item
)
1898 return item
.get () == display
;
1900 gdb_assert (iter
!= all_displays
.end ());
1901 all_displays
.erase (iter
);
1904 /* Call FUNCTION on each of the displays whose numbers are given in
1905 ARGS. DATA is passed unmodified to FUNCTION. */
1908 map_display_numbers (const char *args
,
1909 gdb::function_view
<void (struct display
*)> function
)
1914 error_no_arg (_("one or more display numbers"));
1916 number_or_range_parser
parser (args
);
1918 while (!parser
.finished ())
1920 const char *p
= parser
.cur_tok ();
1922 num
= parser
.get_number ();
1924 warning (_("bad display number at or near '%s'"), p
);
1927 auto iter
= std::find_if (all_displays
.begin (),
1928 all_displays
.end (),
1929 [=] (const std::unique_ptr
<display
> &item
)
1931 return item
->number
== num
;
1933 if (iter
== all_displays
.end ())
1934 printf_unfiltered (_("No display number %d.\n"), num
);
1936 function (iter
->get ());
1941 /* "undisplay" command. */
1944 undisplay_command (const char *args
, int from_tty
)
1948 if (query (_("Delete all auto-display expressions? ")))
1954 map_display_numbers (args
, delete_display
);
1958 /* Display a single auto-display.
1959 Do nothing if the display cannot be printed in the current context,
1960 or if the display is disabled. */
1963 do_one_display (struct display
*d
)
1965 int within_current_scope
;
1970 /* The expression carries the architecture that was used at parse time.
1971 This is a problem if the expression depends on architecture features
1972 (e.g. register numbers), and the current architecture is now different.
1973 For example, a display statement like "display/i $pc" is expected to
1974 display the PC register of the current architecture, not the arch at
1975 the time the display command was given. Therefore, we re-parse the
1976 expression if the current architecture has changed. */
1977 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1988 innermost_block_tracker tracker
;
1989 d
->exp
= parse_expression (d
->exp_string
.c_str (), &tracker
);
1990 d
->block
= tracker
.block ();
1992 catch (const gdb_exception
&ex
)
1994 /* Can't re-parse the expression. Disable this display item. */
1995 d
->enabled_p
= false;
1996 warning (_("Unable to display \"%s\": %s"),
1997 d
->exp_string
.c_str (), ex
.what ());
2004 if (d
->pspace
== current_program_space
)
2005 within_current_scope
= contained_in (get_selected_block (0), d
->block
,
2008 within_current_scope
= 0;
2011 within_current_scope
= 1;
2012 if (!within_current_scope
)
2015 scoped_restore save_display_number
2016 = make_scoped_restore (¤t_display_number
, d
->number
);
2018 annotate_display_begin ();
2019 printf_filtered ("%d", d
->number
);
2020 annotate_display_number_end ();
2021 printf_filtered (": ");
2025 annotate_display_format ();
2027 printf_filtered ("x/");
2028 if (d
->format
.count
!= 1)
2029 printf_filtered ("%d", d
->format
.count
);
2030 printf_filtered ("%c", d
->format
.format
);
2031 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
2032 printf_filtered ("%c", d
->format
.size
);
2033 printf_filtered (" ");
2035 annotate_display_expression ();
2037 puts_filtered (d
->exp_string
.c_str ());
2038 annotate_display_expression_end ();
2040 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
2041 printf_filtered ("\n");
2043 printf_filtered (" ");
2045 annotate_display_value ();
2052 val
= evaluate_expression (d
->exp
.get ());
2053 addr
= value_as_address (val
);
2054 if (d
->format
.format
== 'i')
2055 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
2056 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
2058 catch (const gdb_exception_error
&ex
)
2060 fprintf_filtered (gdb_stdout
, _("%p[<error: %s>%p]\n"),
2061 metadata_style
.style ().ptr (), ex
.what (),
2067 struct value_print_options opts
;
2069 annotate_display_format ();
2071 if (d
->format
.format
)
2072 printf_filtered ("/%c ", d
->format
.format
);
2074 annotate_display_expression ();
2076 puts_filtered (d
->exp_string
.c_str ());
2077 annotate_display_expression_end ();
2079 printf_filtered (" = ");
2081 annotate_display_expression ();
2083 get_formatted_print_options (&opts
, d
->format
.format
);
2084 opts
.raw
= d
->format
.raw
;
2090 val
= evaluate_expression (d
->exp
.get ());
2091 print_formatted (val
, d
->format
.size
, &opts
, gdb_stdout
);
2093 catch (const gdb_exception_error
&ex
)
2095 fprintf_styled (gdb_stdout
, metadata_style
.style (),
2096 _("<error: %s>"), ex
.what ());
2099 printf_filtered ("\n");
2102 annotate_display_end ();
2104 gdb_flush (gdb_stdout
);
2107 /* Display all of the values on the auto-display chain which can be
2108 evaluated in the current scope. */
2113 for (auto &d
: all_displays
)
2114 do_one_display (d
.get ());
2117 /* Delete the auto-display which we were in the process of displaying.
2118 This is done when there is an error or a signal. */
2121 disable_display (int num
)
2123 for (auto &d
: all_displays
)
2124 if (d
->number
== num
)
2126 d
->enabled_p
= false;
2129 printf_unfiltered (_("No display number %d.\n"), num
);
2133 disable_current_display (void)
2135 if (current_display_number
>= 0)
2137 disable_display (current_display_number
);
2138 fprintf_unfiltered (gdb_stderr
,
2139 _("Disabling display %d to "
2140 "avoid infinite recursion.\n"),
2141 current_display_number
);
2143 current_display_number
= -1;
2147 info_display_command (const char *ignore
, int from_tty
)
2149 if (all_displays
.empty ())
2150 printf_unfiltered (_("There are no auto-display expressions now.\n"));
2152 printf_filtered (_("Auto-display expressions now in effect:\n\
2153 Num Enb Expression\n"));
2155 for (auto &d
: all_displays
)
2157 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
2159 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
2161 else if (d
->format
.format
)
2162 printf_filtered ("/%c ", d
->format
.format
);
2163 puts_filtered (d
->exp_string
.c_str ());
2164 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
, true))
2165 printf_filtered (_(" (cannot be evaluated in the current context)"));
2166 printf_filtered ("\n");
2170 /* Implementation of both the "disable display" and "enable display"
2171 commands. ENABLE decides what to do. */
2174 enable_disable_display_command (const char *args
, int from_tty
, bool enable
)
2178 for (auto &d
: all_displays
)
2179 d
->enabled_p
= enable
;
2183 map_display_numbers (args
,
2184 [=] (struct display
*d
)
2186 d
->enabled_p
= enable
;
2190 /* The "enable display" command. */
2193 enable_display_command (const char *args
, int from_tty
)
2195 enable_disable_display_command (args
, from_tty
, true);
2198 /* The "disable display" command. */
2201 disable_display_command (const char *args
, int from_tty
)
2203 enable_disable_display_command (args
, from_tty
, false);
2206 /* display_chain items point to blocks and expressions. Some expressions in
2207 turn may point to symbols.
2208 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2209 obstack_free'd when a shared library is unloaded.
2210 Clear pointers that are about to become dangling.
2211 Both .exp and .block fields will be restored next time we need to display
2212 an item by re-parsing .exp_string field in the new execution context. */
2215 clear_dangling_display_expressions (struct objfile
*objfile
)
2217 struct program_space
*pspace
;
2219 /* With no symbol file we cannot have a block or expression from it. */
2220 if (objfile
== NULL
)
2222 pspace
= objfile
->pspace
;
2223 if (objfile
->separate_debug_objfile_backlink
)
2225 objfile
= objfile
->separate_debug_objfile_backlink
;
2226 gdb_assert (objfile
->pspace
== pspace
);
2229 for (auto &d
: all_displays
)
2231 if (d
->pspace
!= pspace
)
2234 struct objfile
*bl_objf
= nullptr;
2235 if (d
->block
!= nullptr)
2237 bl_objf
= block_objfile (d
->block
);
2238 if (bl_objf
->separate_debug_objfile_backlink
!= nullptr)
2239 bl_objf
= bl_objf
->separate_debug_objfile_backlink
;
2242 if (bl_objf
== objfile
2243 || (d
->exp
!= NULL
&& exp_uses_objfile (d
->exp
.get (), objfile
)))
2252 /* Print the value in stack frame FRAME of a variable specified by a
2253 struct symbol. NAME is the name to print; if NULL then VAR's print
2254 name will be used. STREAM is the ui_file on which to print the
2255 value. INDENT specifies the number of indent levels to print
2256 before printing the variable name.
2258 This function invalidates FRAME. */
2261 print_variable_and_value (const char *name
, struct symbol
*var
,
2262 struct frame_info
*frame
,
2263 struct ui_file
*stream
, int indent
)
2267 name
= var
->print_name ();
2269 fprintf_filtered (stream
, "%*s%ps = ", 2 * indent
, "",
2270 styled_string (variable_name_style
.style (), name
));
2275 struct value_print_options opts
;
2277 /* READ_VAR_VALUE needs a block in order to deal with non-local
2278 references (i.e. to handle nested functions). In this context, we
2279 print variables that are local to this frame, so we can avoid passing
2281 val
= read_var_value (var
, NULL
, frame
);
2282 get_user_print_options (&opts
);
2284 common_val_print (val
, stream
, indent
, &opts
, current_language
);
2286 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2290 catch (const gdb_exception_error
&except
)
2292 fprintf_styled (stream
, metadata_style
.style (),
2293 "<error reading variable %s (%s)>", name
,
2297 fprintf_filtered (stream
, "\n");
2300 /* Subroutine of ui_printf to simplify it.
2301 Print VALUE to STREAM using FORMAT.
2302 VALUE is a C-style string either on the target or
2303 in a GDB internal variable. */
2306 printf_c_string (struct ui_file
*stream
, const char *format
,
2307 struct value
*value
)
2309 const gdb_byte
*str
;
2311 if (value_type (value
)->code () != TYPE_CODE_PTR
2312 && VALUE_LVAL (value
) == lval_internalvar
2313 && c_is_string_type_p (value_type (value
)))
2315 size_t len
= TYPE_LENGTH (value_type (value
));
2317 /* Copy the internal var value to TEM_STR and append a terminating null
2318 character. This protects against corrupted C-style strings that lack
2319 the terminating null char. It also allows Ada-style strings (not
2320 null terminated) to be printed without problems. */
2321 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2323 memcpy (tem_str
, value_contents (value
), len
);
2329 CORE_ADDR tem
= value_as_address (value
);;
2334 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2335 fprintf_filtered (stream
, format
, "(null)");
2340 /* This is a %s argument. Find the length of the string. */
2343 for (len
= 0;; len
++)
2348 read_memory (tem
+ len
, &c
, 1);
2353 /* Copy the string contents into a string inside GDB. */
2354 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2357 read_memory (tem
, tem_str
, len
);
2363 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2364 fprintf_filtered (stream
, format
, (char *) str
);
2368 /* Subroutine of ui_printf to simplify it.
2369 Print VALUE to STREAM using FORMAT.
2370 VALUE is a wide C-style string on the target or
2371 in a GDB internal variable. */
2374 printf_wide_c_string (struct ui_file
*stream
, const char *format
,
2375 struct value
*value
)
2377 const gdb_byte
*str
;
2379 struct gdbarch
*gdbarch
= value_type (value
)->arch ();
2380 struct type
*wctype
= lookup_typename (current_language
,
2381 "wchar_t", NULL
, 0);
2382 int wcwidth
= TYPE_LENGTH (wctype
);
2384 if (VALUE_LVAL (value
) == lval_internalvar
2385 && c_is_string_type_p (value_type (value
)))
2387 str
= value_contents (value
);
2388 len
= TYPE_LENGTH (value_type (value
));
2392 CORE_ADDR tem
= value_as_address (value
);
2397 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2398 fprintf_filtered (stream
, format
, "(null)");
2403 /* This is a %s argument. Find the length of the string. */
2404 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2405 gdb_byte
*buf
= (gdb_byte
*) alloca (wcwidth
);
2407 for (len
= 0;; len
+= wcwidth
)
2410 read_memory (tem
+ len
, buf
, wcwidth
);
2411 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2415 /* Copy the string contents into a string inside GDB. */
2416 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ wcwidth
);
2419 read_memory (tem
, tem_str
, len
);
2420 memset (&tem_str
[len
], 0, wcwidth
);
2424 auto_obstack output
;
2426 convert_between_encodings (target_wide_charset (gdbarch
),
2429 &output
, translit_char
);
2430 obstack_grow_str0 (&output
, "");
2433 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2434 fprintf_filtered (stream
, format
, obstack_base (&output
));
2438 /* Subroutine of ui_printf to simplify it.
2439 Print VALUE, a floating point value, to STREAM using FORMAT. */
2442 printf_floating (struct ui_file
*stream
, const char *format
,
2443 struct value
*value
, enum argclass argclass
)
2445 /* Parameter data. */
2446 struct type
*param_type
= value_type (value
);
2447 struct gdbarch
*gdbarch
= param_type
->arch ();
2449 /* Determine target type corresponding to the format string. */
2450 struct type
*fmt_type
;
2454 fmt_type
= builtin_type (gdbarch
)->builtin_double
;
2456 case long_double_arg
:
2457 fmt_type
= builtin_type (gdbarch
)->builtin_long_double
;
2459 case dec32float_arg
:
2460 fmt_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2462 case dec64float_arg
:
2463 fmt_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2465 case dec128float_arg
:
2466 fmt_type
= builtin_type (gdbarch
)->builtin_declong
;
2469 gdb_assert_not_reached ("unexpected argument class");
2472 /* To match the traditional GDB behavior, the conversion is
2473 done differently depending on the type of the parameter:
2475 - if the parameter has floating-point type, it's value
2476 is converted to the target type;
2478 - otherwise, if the parameter has a type that is of the
2479 same size as a built-in floating-point type, the value
2480 bytes are interpreted as if they were of that type, and
2481 then converted to the target type (this is not done for
2482 decimal floating-point argument classes);
2484 - otherwise, if the source value has an integer value,
2485 it's value is converted to the target type;
2487 - otherwise, an error is raised.
2489 In either case, the result of the conversion is a byte buffer
2490 formatted in the target format for the target type. */
2492 if (fmt_type
->code () == TYPE_CODE_FLT
)
2494 param_type
= float_type_from_length (param_type
);
2495 if (param_type
!= value_type (value
))
2496 value
= value_from_contents (param_type
, value_contents (value
));
2499 value
= value_cast (fmt_type
, value
);
2501 /* Convert the value to a string and print it. */
2503 = target_float_to_string (value_contents (value
), fmt_type
, format
);
2504 fputs_filtered (str
.c_str (), stream
);
2507 /* Subroutine of ui_printf to simplify it.
2508 Print VALUE, a target pointer, to STREAM using FORMAT. */
2511 printf_pointer (struct ui_file
*stream
, const char *format
,
2512 struct value
*value
)
2514 /* We avoid the host's %p because pointers are too
2515 likely to be the wrong size. The only interesting
2516 modifier for %p is a width; extract that, and then
2517 handle %p as glibc would: %#x or a literal "(nil)". */
2521 #ifdef PRINTF_HAS_LONG_LONG
2522 long long val
= value_as_long (value
);
2524 long val
= value_as_long (value
);
2527 fmt
= (char *) alloca (strlen (format
) + 5);
2529 /* Copy up to the leading %. */
2534 int is_percent
= (*p
== '%');
2549 /* Copy any width or flags. Only the "-" flag is valid for pointers
2550 -- see the format_pieces constructor. */
2551 while (*p
== '-' || (*p
>= '0' && *p
< '9'))
2554 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2557 #ifdef PRINTF_HAS_LONG_LONG
2564 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2565 fprintf_filtered (stream
, fmt
, val
);
2573 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2574 fprintf_filtered (stream
, fmt
, "(nil)");
2579 /* printf "printf format string" ARG to STREAM. */
2582 ui_printf (const char *arg
, struct ui_file
*stream
)
2584 const char *s
= arg
;
2585 std::vector
<struct value
*> val_args
;
2588 error_no_arg (_("format-control string and values to print"));
2590 s
= skip_spaces (s
);
2592 /* A format string should follow, enveloped in double quotes. */
2594 error (_("Bad format string, missing '\"'."));
2596 format_pieces
fpieces (&s
);
2599 error (_("Bad format string, non-terminated '\"'."));
2601 s
= skip_spaces (s
);
2603 if (*s
!= ',' && *s
!= 0)
2604 error (_("Invalid argument syntax"));
2608 s
= skip_spaces (s
);
2613 const char *current_substring
;
2616 for (auto &&piece
: fpieces
)
2617 if (piece
.argclass
!= literal_piece
)
2620 /* Now, parse all arguments and evaluate them.
2621 Store the VALUEs in VAL_ARGS. */
2628 val_args
.push_back (parse_to_comma_and_eval (&s1
));
2635 if (val_args
.size () != nargs_wanted
)
2636 error (_("Wrong number of arguments for specified format-string"));
2638 /* Now actually print them. */
2640 for (auto &&piece
: fpieces
)
2642 current_substring
= piece
.string
;
2643 switch (piece
.argclass
)
2646 printf_c_string (stream
, current_substring
, val_args
[i
]);
2648 case wide_string_arg
:
2649 printf_wide_c_string (stream
, current_substring
, val_args
[i
]);
2653 struct gdbarch
*gdbarch
= value_type (val_args
[i
])->arch ();
2654 struct type
*wctype
= lookup_typename (current_language
,
2655 "wchar_t", NULL
, 0);
2656 struct type
*valtype
;
2657 const gdb_byte
*bytes
;
2659 valtype
= value_type (val_args
[i
]);
2660 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2661 || valtype
->code () != TYPE_CODE_INT
)
2662 error (_("expected wchar_t argument for %%lc"));
2664 bytes
= value_contents (val_args
[i
]);
2666 auto_obstack output
;
2668 convert_between_encodings (target_wide_charset (gdbarch
),
2670 bytes
, TYPE_LENGTH (valtype
),
2671 TYPE_LENGTH (valtype
),
2672 &output
, translit_char
);
2673 obstack_grow_str0 (&output
, "");
2676 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2677 fprintf_filtered (stream
, current_substring
,
2678 obstack_base (&output
));
2683 #ifdef PRINTF_HAS_LONG_LONG
2685 long long val
= value_as_long (val_args
[i
]);
2688 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2689 fprintf_filtered (stream
, current_substring
, val
);
2694 error (_("long long not supported in printf"));
2698 int val
= value_as_long (val_args
[i
]);
2701 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2702 fprintf_filtered (stream
, current_substring
, val
);
2708 long val
= value_as_long (val_args
[i
]);
2711 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2712 fprintf_filtered (stream
, current_substring
, val
);
2718 size_t val
= value_as_long (val_args
[i
]);
2721 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2722 fprintf_filtered (stream
, current_substring
, val
);
2726 /* Handles floating-point values. */
2728 case long_double_arg
:
2729 case dec32float_arg
:
2730 case dec64float_arg
:
2731 case dec128float_arg
:
2732 printf_floating (stream
, current_substring
, val_args
[i
],
2736 printf_pointer (stream
, current_substring
, val_args
[i
]);
2739 /* Print a portion of the format string that has no
2740 directives. Note that this will not include any
2741 ordinary %-specs, but it might include "%%". That is
2742 why we use printf_filtered and not puts_filtered here.
2743 Also, we pass a dummy argument because some platforms
2744 have modified GCC to include -Wformat-security by
2745 default, which will warn here if there is no
2748 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2749 fprintf_filtered (stream
, current_substring
, 0);
2753 internal_error (__FILE__
, __LINE__
,
2754 _("failed internal consistency check"));
2756 /* Maybe advance to the next argument. */
2757 if (piece
.argclass
!= literal_piece
)
2763 /* Implement the "printf" command. */
2766 printf_command (const char *arg
, int from_tty
)
2768 ui_printf (arg
, gdb_stdout
);
2769 reset_terminal_style (gdb_stdout
);
2771 gdb_stdout
->flush ();
2774 /* Implement the "eval" command. */
2777 eval_command (const char *arg
, int from_tty
)
2781 ui_printf (arg
, &stb
);
2783 std::string expanded
= insert_user_defined_cmd_args (stb
.c_str ());
2785 execute_command (expanded
.c_str (), from_tty
);
2788 void _initialize_printcmd ();
2790 _initialize_printcmd ()
2792 struct cmd_list_element
*c
;
2794 current_display_number
= -1;
2796 gdb::observers::free_objfile
.attach (clear_dangling_display_expressions
);
2798 add_info ("address", info_address_command
,
2799 _("Describe where symbol SYM is stored.\n\
2800 Usage: info address SYM"));
2802 add_info ("symbol", info_symbol_command
, _("\
2803 Describe what symbol is at location ADDR.\n\
2804 Usage: info symbol ADDR\n\
2805 Only for symbols with fixed locations (global or static scope)."));
2807 c
= add_com ("x", class_vars
, x_command
, _("\
2808 Examine memory: x/FMT ADDRESS.\n\
2809 ADDRESS is an expression for the memory address to examine.\n\
2810 FMT is a repeat count followed by a format letter and a size letter.\n\
2811 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2812 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2813 and z(hex, zero padded on the left).\n\
2814 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2815 The specified number of objects of the specified size are printed\n\
2816 according to the format. If a negative number is specified, memory is\n\
2817 examined backward from the address.\n\n\
2818 Defaults for format and size letters are those previously used.\n\
2819 Default count is 1. Default address is following last thing printed\n\
2820 with this command or \"print\"."));
2821 set_cmd_completer_handle_brkchars (c
, display_and_x_command_completer
);
2823 add_info ("display", info_display_command
, _("\
2824 Expressions to display when program stops, with code numbers.\n\
2825 Usage: info display"));
2827 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2828 Cancel some expressions to be displayed when program stops.\n\
2829 Usage: undisplay [NUM]...\n\
2830 Arguments are the code numbers of the expressions to stop displaying.\n\
2831 No argument means cancel all automatic-display expressions.\n\
2832 \"delete display\" has the same effect as this command.\n\
2833 Do \"info display\" to see current list of code numbers."),
2836 c
= add_com ("display", class_vars
, display_command
, _("\
2837 Print value of expression EXP each time the program stops.\n\
2838 Usage: display[/FMT] EXP\n\
2839 /FMT may be used before EXP as in the \"print\" command.\n\
2840 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2841 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2842 and examining is done as in the \"x\" command.\n\n\
2843 With no argument, display all currently requested auto-display expressions.\n\
2844 Use \"undisplay\" to cancel display requests previously made."));
2845 set_cmd_completer_handle_brkchars (c
, display_and_x_command_completer
);
2847 add_cmd ("display", class_vars
, enable_display_command
, _("\
2848 Enable some expressions to be displayed when program stops.\n\
2849 Usage: enable display [NUM]...\n\
2850 Arguments are the code numbers of the expressions to resume displaying.\n\
2851 No argument means enable all automatic-display expressions.\n\
2852 Do \"info display\" to see current list of code numbers."), &enablelist
);
2854 add_cmd ("display", class_vars
, disable_display_command
, _("\
2855 Disable some expressions to be displayed when program stops.\n\
2856 Usage: disable display [NUM]...\n\
2857 Arguments are the code numbers of the expressions to stop displaying.\n\
2858 No argument means disable all automatic-display expressions.\n\
2859 Do \"info display\" to see current list of code numbers."), &disablelist
);
2861 add_cmd ("display", class_vars
, undisplay_command
, _("\
2862 Cancel some expressions to be displayed when program stops.\n\
2863 Usage: delete display [NUM]...\n\
2864 Arguments are the code numbers of the expressions to stop displaying.\n\
2865 No argument means cancel all automatic-display expressions.\n\
2866 Do \"info display\" to see current list of code numbers."), &deletelist
);
2868 add_com ("printf", class_vars
, printf_command
, _("\
2869 Formatted printing, like the C \"printf\" function.\n\
2870 Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2871 This supports most C printf format specifications, like %s, %d, etc."));
2873 add_com ("output", class_vars
, output_command
, _("\
2874 Like \"print\" but don't put in value history and don't print newline.\n\
2875 Usage: output EXP\n\
2876 This is useful in user-defined commands."));
2878 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2879 Evaluate expression EXP and assign result to variable VAR.\n\
2880 Usage: set VAR = EXP\n\
2881 This uses assignment syntax appropriate for the current language\n\
2882 (VAR = EXP or VAR := EXP for example).\n\
2883 VAR may be a debugger \"convenience\" variable (names starting\n\
2884 with $), a register (a few standard names starting with $), or an actual\n\
2885 variable in the program being debugged. EXP is any valid expression.\n\
2886 Use \"set variable\" for variables with names identical to set subcommands.\n\
2888 With a subcommand, this command modifies parts of the gdb environment.\n\
2889 You can see these environment settings with the \"show\" command."),
2890 &setlist
, "set ", 1, &cmdlist
);
2892 add_com ("assign", class_vars
, set_command
, _("\
2893 Evaluate expression EXP and assign result to variable VAR.\n\
2894 Usage: assign VAR = EXP\n\
2895 This uses assignment syntax appropriate for the current language\n\
2896 (VAR = EXP or VAR := EXP for example).\n\
2897 VAR may be a debugger \"convenience\" variable (names starting\n\
2898 with $), a register (a few standard names starting with $), or an actual\n\
2899 variable in the program being debugged. EXP is any valid expression.\n\
2900 Use \"set variable\" for variables with names identical to set subcommands.\n\
2901 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2902 You can see these environment settings with the \"show\" command."));
2904 /* "call" is the same as "set", but handy for dbx users to call fns. */
2905 c
= add_com ("call", class_vars
, call_command
, _("\
2906 Call a function in the program.\n\
2908 The argument is the function name and arguments, in the notation of the\n\
2909 current working language. The result is printed and saved in the value\n\
2910 history, if it is not void."));
2911 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2913 add_cmd ("variable", class_vars
, set_command
, _("\
2914 Evaluate expression EXP and assign result to variable VAR.\n\
2915 Usage: set variable VAR = EXP\n\
2916 This uses assignment syntax appropriate for the current language\n\
2917 (VAR = EXP or VAR := EXP for example).\n\
2918 VAR may be a debugger \"convenience\" variable (names starting\n\
2919 with $), a register (a few standard names starting with $), or an actual\n\
2920 variable in the program being debugged. EXP is any valid expression.\n\
2921 This may usually be abbreviated to simply \"set\"."),
2923 add_alias_cmd ("var", "variable", class_vars
, 0, &setlist
);
2925 const auto print_opts
= make_value_print_options_def_group (nullptr);
2927 static const std::string print_help
= gdb::option::build_help (_("\
2928 Print value of expression EXP.\n\
2929 Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
2934 Note: because this command accepts arbitrary expressions, if you\n\
2935 specify any command option, you must use a double dash (\"--\")\n\
2936 to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
2938 Variables accessible are those of the lexical environment of the selected\n\
2939 stack frame, plus all those whose scope is global or an entire file.\n\
2941 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2942 $$NUM refers to NUM'th value back from the last one.\n\
2943 Names starting with $ refer to registers (with the values they would have\n\
2944 if the program were to return to the stack frame now selected, restoring\n\
2945 all registers saved by frames farther in) or else to debugger\n\
2946 \"convenience\" variables (any such name not a known register).\n\
2947 Use assignment expressions to give values to convenience variables.\n\
2949 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2950 @ is a binary operator for treating consecutive data objects\n\
2951 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2952 element is FOO, whose second element is stored in the space following\n\
2953 where FOO is stored, etc. FOO must be an expression whose value\n\
2954 resides in memory.\n\
2956 EXP may be preceded with /FMT, where FMT is a format letter\n\
2957 but no count or size letter (see \"x\" command)."),
2960 c
= add_com ("print", class_vars
, print_command
, print_help
.c_str ());
2961 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2962 add_com_alias ("p", "print", class_vars
, 1);
2963 add_com_alias ("inspect", "print", class_vars
, 1);
2965 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2966 &max_symbolic_offset
, _("\
2967 Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2968 Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2969 Tell GDB to only display the symbolic form of an address if the\n\
2970 offset between the closest earlier symbol and the address is less than\n\
2971 the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2972 to always print the symbolic form of an address if any symbol precedes\n\
2973 it. Zero is equivalent to \"unlimited\"."),
2975 show_max_symbolic_offset
,
2976 &setprintlist
, &showprintlist
);
2977 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2978 &print_symbol_filename
, _("\
2979 Set printing of source filename and line number with <SYMBOL>."), _("\
2980 Show printing of source filename and line number with <SYMBOL>."), NULL
,
2982 show_print_symbol_filename
,
2983 &setprintlist
, &showprintlist
);
2985 add_com ("eval", no_class
, eval_command
, _("\
2986 Construct a GDB command and then evaluate it.\n\
2987 Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2988 Convert the arguments to a string as \"printf\" would, but then\n\
2989 treat this string as a command line, and evaluate it."));