1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986-2019 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 int print_symbol_filename
= 0;
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
;
121 /* Chain link to next auto-display item. */
122 struct display
*next
;
124 /* The expression as the user typed it. */
127 /* Expression to be evaluated and displayed. */
130 /* Item number of this auto-display item. */
133 /* Display format specified. */
134 struct format_data format
;
136 /* Program space associated with `block'. */
137 struct program_space
*pspace
;
139 /* Innermost block required by this expression when evaluated. */
140 const struct block
*block
;
142 /* Status of this display (enabled or disabled). */
146 /* Chain of expressions whose values should be displayed
147 automatically each time the program stops. */
149 static struct display
*display_chain
;
151 static int display_number
;
153 /* Walk the following statement or block through all displays.
154 ALL_DISPLAYS_SAFE does so even if the statement deletes the current
157 #define ALL_DISPLAYS(B) \
158 for (B = display_chain; B; B = B->next)
160 #define ALL_DISPLAYS_SAFE(B,TMP) \
161 for (B = display_chain; \
162 B ? (TMP = B->next, 1): 0; \
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
) == TYPE_CODE_REF
315 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
316 || TYPE_CODE (type
) == TYPE_CODE_STRING
317 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
318 || TYPE_CODE (type
) == TYPE_CODE_UNION
319 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
320 value_print (val
, stream
, options
);
322 /* User specified format, so don't look to the type to tell us
324 val_print_scalar_formatted (type
,
325 value_embedded_offset (val
),
327 options
, size
, stream
);
330 /* Return builtin floating point type of same length as TYPE.
331 If no such type is found, return TYPE itself. */
333 float_type_from_length (struct type
*type
)
335 struct gdbarch
*gdbarch
= get_type_arch (type
);
336 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
338 if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_float
))
339 type
= builtin
->builtin_float
;
340 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_double
))
341 type
= builtin
->builtin_double
;
342 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (builtin
->builtin_long_double
))
343 type
= builtin
->builtin_long_double
;
348 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
349 according to OPTIONS and SIZE on STREAM. Formats s and i are not
350 supported at this level. */
353 print_scalar_formatted (const gdb_byte
*valaddr
, struct type
*type
,
354 const struct value_print_options
*options
,
355 int size
, struct ui_file
*stream
)
357 struct gdbarch
*gdbarch
= get_type_arch (type
);
358 unsigned int len
= TYPE_LENGTH (type
);
359 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
361 /* String printing should go through val_print_scalar_formatted. */
362 gdb_assert (options
->format
!= 's');
364 /* If the value is a pointer, and pointers and addresses are not the
365 same, then at this point, the value's length (in target bytes) is
366 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
367 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
368 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
370 /* If we are printing it as unsigned, truncate it in case it is actually
371 a negative signed value (e.g. "print/u (short)-1" should print 65535
372 (if shorts are 16 bits) instead of 4294967295). */
373 if (options
->format
!= 'c'
374 && (options
->format
!= 'd' || TYPE_UNSIGNED (type
)))
376 if (len
< TYPE_LENGTH (type
) && byte_order
== BFD_ENDIAN_BIG
)
377 valaddr
+= TYPE_LENGTH (type
) - len
;
380 if (size
!= 0 && (options
->format
== 'x' || options
->format
== 't'))
382 /* Truncate to fit. */
399 error (_("Undefined output size \"%c\"."), size
);
401 if (newlen
< len
&& byte_order
== BFD_ENDIAN_BIG
)
402 valaddr
+= len
- newlen
;
406 /* Historically gdb has printed floats by first casting them to a
407 long, and then printing the long. PR cli/16242 suggests changing
408 this to using C-style hex float format.
410 Biased range types must also be unbiased here; the unbiasing is
411 done by unpack_long. */
412 gdb::byte_vector converted_bytes
;
413 /* Some cases below will unpack the value again. In the biased
414 range case, we want to avoid this, so we store the unpacked value
415 here for possible use later. */
416 gdb::optional
<LONGEST
> val_long
;
417 if ((TYPE_CODE (type
) == TYPE_CODE_FLT
418 && (options
->format
== 'o'
419 || options
->format
== 'x'
420 || options
->format
== 't'
421 || options
->format
== 'z'
422 || options
->format
== 'd'
423 || options
->format
== 'u'))
424 || (TYPE_CODE (type
) == TYPE_CODE_RANGE
425 && TYPE_RANGE_DATA (type
)->bias
!= 0))
427 val_long
.emplace (unpack_long (type
, valaddr
));
428 converted_bytes
.resize (TYPE_LENGTH (type
));
429 store_signed_integer (converted_bytes
.data (), TYPE_LENGTH (type
),
430 byte_order
, *val_long
);
431 valaddr
= converted_bytes
.data ();
434 /* Printing a non-float type as 'f' will interpret the data as if it were
435 of a floating-point type of the same length, if that exists. Otherwise,
436 the data is printed as integer. */
437 char format
= options
->format
;
438 if (format
== 'f' && TYPE_CODE (type
) != TYPE_CODE_FLT
)
440 type
= float_type_from_length (type
);
441 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
448 print_octal_chars (stream
, valaddr
, len
, byte_order
);
451 print_decimal_chars (stream
, valaddr
, len
, true, byte_order
);
454 print_decimal_chars (stream
, valaddr
, len
, false, byte_order
);
457 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
459 print_decimal_chars (stream
, valaddr
, len
, !TYPE_UNSIGNED (type
),
465 print_floating (valaddr
, type
, stream
);
469 print_binary_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
472 print_hex_chars (stream
, valaddr
, len
, byte_order
, size
> 0);
475 print_hex_chars (stream
, valaddr
, len
, byte_order
, true);
479 struct value_print_options opts
= *options
;
481 if (!val_long
.has_value ())
482 val_long
.emplace (unpack_long (type
, valaddr
));
485 if (TYPE_UNSIGNED (type
))
486 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
488 type
= builtin_type (gdbarch
)->builtin_true_char
;
490 value_print (value_from_longest (type
, *val_long
), stream
, &opts
);
496 if (!val_long
.has_value ())
497 val_long
.emplace (unpack_long (type
, valaddr
));
498 print_address (gdbarch
, *val_long
, stream
);
503 error (_("Undefined output format \"%c\"."), format
);
507 /* Specify default address for `x' command.
508 The `info lines' command uses this. */
511 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
513 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
515 next_gdbarch
= gdbarch
;
518 /* Make address available to the user as $_. */
519 set_internalvar (lookup_internalvar ("_"),
520 value_from_pointer (ptr_type
, addr
));
523 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
524 after LEADIN. Print nothing if no symbolic name is found nearby.
525 Optionally also print source file and line number, if available.
526 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
527 or to interpret it as a possible C++ name and convert it back to source
528 form. However note that DO_DEMANGLE can be overridden by the specific
529 settings of the demangle and asm_demangle variables. Returns
530 non-zero if anything was printed; zero otherwise. */
533 print_address_symbolic (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
534 struct ui_file
*stream
,
535 int do_demangle
, const char *leadin
)
537 std::string name
, filename
;
542 if (build_address_symbolic (gdbarch
, addr
, do_demangle
, false, &name
,
543 &offset
, &filename
, &line
, &unmapped
))
546 fputs_filtered (leadin
, stream
);
548 fputs_filtered ("<*", stream
);
550 fputs_filtered ("<", stream
);
551 fputs_styled (name
.c_str (), function_name_style
.style (), stream
);
553 fprintf_filtered (stream
, "%+d", offset
);
555 /* Append source filename and line number if desired. Give specific
556 line # of this addr, if we have it; else line # of the nearest symbol. */
557 if (print_symbol_filename
&& !filename
.empty ())
559 fputs_filtered (line
== -1 ? " in " : " at ", stream
);
560 fputs_styled (filename
.c_str (), file_name_style
.style (), stream
);
562 fprintf_filtered (stream
, ":%d", line
);
565 fputs_filtered ("*>", stream
);
567 fputs_filtered (">", stream
);
572 /* See valprint.h. */
575 build_address_symbolic (struct gdbarch
*gdbarch
,
576 CORE_ADDR addr
, /* IN */
577 bool do_demangle
, /* IN */
578 bool prefer_sym_over_minsym
, /* IN */
579 std::string
*name
, /* OUT */
580 int *offset
, /* OUT */
581 std::string
*filename
, /* OUT */
583 int *unmapped
) /* OUT */
585 struct bound_minimal_symbol msymbol
;
586 struct symbol
*symbol
;
587 CORE_ADDR name_location
= 0;
588 struct obj_section
*section
= NULL
;
589 const char *name_temp
= "";
591 /* Let's say it is mapped (not unmapped). */
594 /* Determine if the address is in an overlay, and whether it is
596 if (overlay_debugging
)
598 section
= find_pc_overlay (addr
);
599 if (pc_in_unmapped_range (addr
, section
))
602 addr
= overlay_mapped_address (addr
, section
);
606 /* Try to find the address in both the symbol table and the minsyms.
607 In most cases, we'll prefer to use the symbol instead of the
608 minsym. However, there are cases (see below) where we'll choose
609 to use the minsym instead. */
611 /* This is defective in the sense that it only finds text symbols. So
612 really this is kind of pointless--we should make sure that the
613 minimal symbols have everything we need (by changing that we could
614 save some memory, but for many debug format--ELF/DWARF or
615 anything/stabs--it would be inconvenient to eliminate those minimal
617 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
618 symbol
= find_pc_sect_function (addr
, section
);
622 /* If this is a function (i.e. a code address), strip out any
623 non-address bits. For instance, display a pointer to the
624 first instruction of a Thumb function as <function>; the
625 second instruction will be <function+2>, even though the
626 pointer is <function+3>. This matches the ISA behavior. */
627 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
629 name_location
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol
));
630 if (do_demangle
|| asm_demangle
)
631 name_temp
= SYMBOL_PRINT_NAME (symbol
);
633 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
636 if (msymbol
.minsym
!= NULL
637 && MSYMBOL_HAS_SIZE (msymbol
.minsym
)
638 && MSYMBOL_SIZE (msymbol
.minsym
) == 0
639 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text
640 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_text_gnu_ifunc
641 && MSYMBOL_TYPE (msymbol
.minsym
) != mst_file_text
)
642 msymbol
.minsym
= NULL
;
644 if (msymbol
.minsym
!= NULL
)
646 /* Use the minsym if no symbol is found.
648 Additionally, use the minsym instead of a (found) symbol if
649 the following conditions all hold:
650 1) The prefer_sym_over_minsym flag is false.
651 2) The minsym address is identical to that of the address under
653 3) The symbol address is not identical to that of the address
654 under consideration. */
655 if (symbol
== NULL
||
656 (!prefer_sym_over_minsym
657 && BMSYMBOL_VALUE_ADDRESS (msymbol
) == addr
658 && name_location
!= addr
))
660 /* If this is a function (i.e. a code address), strip out any
661 non-address bits. For instance, display a pointer to the
662 first instruction of a Thumb function as <function>; the
663 second instruction will be <function+2>, even though the
664 pointer is <function+3>. This matches the ISA behavior. */
665 if (MSYMBOL_TYPE (msymbol
.minsym
) == mst_text
666 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_text_gnu_ifunc
667 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_file_text
668 || MSYMBOL_TYPE (msymbol
.minsym
) == mst_solib_trampoline
)
669 addr
= gdbarch_addr_bits_remove (gdbarch
, addr
);
672 name_location
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
673 if (do_demangle
|| asm_demangle
)
674 name_temp
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
676 name_temp
= MSYMBOL_LINKAGE_NAME (msymbol
.minsym
);
679 if (symbol
== NULL
&& msymbol
.minsym
== NULL
)
682 /* If the nearest symbol is too far away, don't print anything symbolic. */
684 /* For when CORE_ADDR is larger than unsigned int, we do math in
685 CORE_ADDR. But when we detect unsigned wraparound in the
686 CORE_ADDR math, we ignore this test and print the offset,
687 because addr+max_symbolic_offset has wrapped through the end
688 of the address space back to the beginning, giving bogus comparison. */
689 if (addr
> name_location
+ max_symbolic_offset
690 && name_location
+ max_symbolic_offset
> name_location
)
693 *offset
= (LONGEST
) addr
- name_location
;
697 if (print_symbol_filename
)
699 struct symtab_and_line sal
;
701 sal
= find_pc_sect_line (addr
, section
, 0);
705 *filename
= symtab_to_filename_for_display (sal
.symtab
);
713 /* Print address ADDR symbolically on STREAM.
714 First print it as a number. Then perhaps print
715 <SYMBOL + OFFSET> after the number. */
718 print_address (struct gdbarch
*gdbarch
,
719 CORE_ADDR addr
, struct ui_file
*stream
)
721 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
722 print_address_symbolic (gdbarch
, addr
, stream
, asm_demangle
, " ");
725 /* Return a prefix for instruction address:
726 "=> " for current instruction, else " ". */
729 pc_prefix (CORE_ADDR addr
)
731 if (has_stack_frames ())
733 struct frame_info
*frame
;
736 frame
= get_selected_frame (NULL
);
737 if (get_frame_pc_if_available (frame
, &pc
) && pc
== addr
)
743 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
744 controls whether to print the symbolic name "raw" or demangled.
745 Return non-zero if anything was printed; zero otherwise. */
748 print_address_demangle (const struct value_print_options
*opts
,
749 struct gdbarch
*gdbarch
, CORE_ADDR addr
,
750 struct ui_file
*stream
, int do_demangle
)
752 if (opts
->addressprint
)
754 fputs_styled (paddress (gdbarch
, addr
), address_style
.style (), stream
);
755 print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, " ");
759 return print_address_symbolic (gdbarch
, addr
, stream
, do_demangle
, "");
765 /* Find the address of the instruction that is INST_COUNT instructions before
766 the instruction at ADDR.
767 Since some architectures have variable-length instructions, we can't just
768 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
769 number information to locate the nearest known instruction boundary,
770 and disassemble forward from there. If we go out of the symbol range
771 during disassembling, we return the lowest address we've got so far and
772 set the number of instructions read to INST_READ. */
775 find_instruction_backward (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
776 int inst_count
, int *inst_read
)
778 /* The vector PCS is used to store instruction addresses within
780 CORE_ADDR loop_start
, loop_end
, p
;
781 std::vector
<CORE_ADDR
> pcs
;
782 struct symtab_and_line sal
;
785 loop_start
= loop_end
= addr
;
787 /* In each iteration of the outer loop, we get a pc range that ends before
788 LOOP_START, then we count and store every instruction address of the range
789 iterated in the loop.
790 If the number of instructions counted reaches INST_COUNT, return the
791 stored address that is located INST_COUNT instructions back from ADDR.
792 If INST_COUNT is not reached, we subtract the number of counted
793 instructions from INST_COUNT, and go to the next iteration. */
797 sal
= find_pc_sect_line (loop_start
, NULL
, 1);
800 /* We reach here when line info is not available. In this case,
801 we print a message and just exit the loop. The return value
802 is calculated after the loop. */
803 printf_filtered (_("No line number information available "
806 print_address (gdbarch
, loop_start
- 1, gdb_stdout
);
807 printf_filtered ("\n");
811 loop_end
= loop_start
;
814 /* This loop pushes instruction addresses in the range from
815 LOOP_START to LOOP_END. */
816 for (p
= loop_start
; p
< loop_end
;)
819 p
+= gdb_insn_length (gdbarch
, p
);
822 inst_count
-= pcs
.size ();
823 *inst_read
+= pcs
.size ();
825 while (inst_count
> 0);
827 /* After the loop, the vector PCS has instruction addresses of the last
828 source line we processed, and INST_COUNT has a negative value.
829 We return the address at the index of -INST_COUNT in the vector for
831 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
841 find_instruction_backward is called with INST_COUNT = 4 and expected to
842 return 0x4001. When we reach here, INST_COUNT is set to -1 because
843 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
844 4001 is located at the index 1 of the last iterated line (= Line X),
845 which is simply calculated by -INST_COUNT.
846 The case when the length of PCS is 0 means that we reached an area for
847 which line info is not available. In such case, we return LOOP_START,
848 which was the lowest instruction address that had line info. */
849 p
= pcs
.size () > 0 ? pcs
[-inst_count
] : loop_start
;
851 /* INST_READ includes all instruction addresses in a pc range. Need to
852 exclude the beginning part up to the address we're returning. That
853 is, exclude {0x4000} in the example above. */
855 *inst_read
+= inst_count
;
860 /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
861 placing the results in GDB's memory from MYADDR + LEN. Returns
862 a count of the bytes actually read. */
865 read_memory_backward (struct gdbarch
*gdbarch
,
866 CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
869 int nread
; /* Number of bytes actually read. */
871 /* First try a complete read. */
872 errcode
= target_read_memory (memaddr
, myaddr
, len
);
880 /* Loop, reading one byte at a time until we get as much as we can. */
883 for (nread
= 0; nread
< len
; ++nread
)
885 errcode
= target_read_memory (--memaddr
, --myaddr
, 1);
888 /* The read was unsuccessful, so exit the loop. */
889 printf_filtered (_("Cannot access memory at address %s\n"),
890 paddress (gdbarch
, memaddr
));
898 /* Returns true if X (which is LEN bytes wide) is the number zero. */
901 integer_is_zero (const gdb_byte
*x
, int len
)
905 while (i
< len
&& x
[i
] == 0)
910 /* Find the start address of a string in which ADDR is included.
911 Basically we search for '\0' and return the next address,
912 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
913 we stop searching and return the address to print characters as many as
914 PRINT_MAX from the string. */
917 find_string_backward (struct gdbarch
*gdbarch
,
918 CORE_ADDR addr
, int count
, int char_size
,
919 const struct value_print_options
*options
,
920 int *strings_counted
)
922 const int chunk_size
= 0x20;
925 int chars_to_read
= chunk_size
;
926 int chars_counted
= 0;
927 int count_original
= count
;
928 CORE_ADDR string_start_addr
= addr
;
930 gdb_assert (char_size
== 1 || char_size
== 2 || char_size
== 4);
931 gdb::byte_vector
buffer (chars_to_read
* char_size
);
932 while (count
> 0 && read_error
== 0)
936 addr
-= chars_to_read
* char_size
;
937 chars_read
= read_memory_backward (gdbarch
, addr
, buffer
.data (),
938 chars_to_read
* char_size
);
939 chars_read
/= char_size
;
940 read_error
= (chars_read
== chars_to_read
) ? 0 : 1;
941 /* Searching for '\0' from the end of buffer in backward direction. */
942 for (i
= 0; i
< chars_read
&& count
> 0 ; ++i
, ++chars_counted
)
944 int offset
= (chars_to_read
- i
- 1) * char_size
;
946 if (integer_is_zero (&buffer
[offset
], char_size
)
947 || chars_counted
== options
->print_max
)
949 /* Found '\0' or reached print_max. As OFFSET is the offset to
950 '\0', we add CHAR_SIZE to return the start address of
953 string_start_addr
= addr
+ offset
+ char_size
;
959 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
960 *strings_counted
= count_original
- count
;
964 /* In error case, STRING_START_ADDR is pointing to the string that
965 was last successfully loaded. Rewind the partially loaded string. */
966 string_start_addr
-= chars_counted
* char_size
;
969 return string_start_addr
;
972 /* Examine data at address ADDR in format FMT.
973 Fetch it from memory and print on gdb_stdout. */
976 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
981 struct type
*val_type
= NULL
;
984 struct value_print_options opts
;
985 int need_to_update_next_address
= 0;
986 CORE_ADDR addr_rewound
= 0;
991 next_gdbarch
= gdbarch
;
994 /* Instruction format implies fetch single bytes
995 regardless of the specified size.
996 The case of strings is handled in decode_format, only explicit
997 size operator are not changed to 'b'. */
1003 /* Pick the appropriate size for an address. */
1004 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
1006 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
1008 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
1011 /* Bad value for gdbarch_ptr_bit. */
1012 internal_error (__FILE__
, __LINE__
,
1013 _("failed internal consistency check"));
1017 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1018 else if (size
== 'h')
1019 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
1020 else if (size
== 'w')
1021 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
1022 else if (size
== 'g')
1023 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
1027 struct type
*char_type
= NULL
;
1029 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1030 if type is not found. */
1032 char_type
= builtin_type (next_gdbarch
)->builtin_char16
;
1033 else if (size
== 'w')
1034 char_type
= builtin_type (next_gdbarch
)->builtin_char32
;
1036 val_type
= char_type
;
1039 if (size
!= '\0' && size
!= 'b')
1040 warning (_("Unable to display strings with "
1041 "size '%c', using 'b' instead."), size
);
1043 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
1052 if (format
== 's' || format
== 'i')
1055 get_formatted_print_options (&opts
, format
);
1059 /* This is the negative repeat count case.
1060 We rewind the address based on the given repeat count and format,
1061 then examine memory from there in forward direction. */
1066 next_address
= find_instruction_backward (gdbarch
, addr
, count
,
1069 else if (format
== 's')
1071 next_address
= find_string_backward (gdbarch
, addr
, count
,
1072 TYPE_LENGTH (val_type
),
1077 next_address
= addr
- count
* TYPE_LENGTH (val_type
);
1080 /* The following call to print_formatted updates next_address in every
1081 iteration. In backward case, we store the start address here
1082 and update next_address with it before exiting the function. */
1083 addr_rewound
= (format
== 's'
1084 ? next_address
- TYPE_LENGTH (val_type
)
1086 need_to_update_next_address
= 1;
1089 /* Print as many objects as specified in COUNT, at most maxelts per line,
1090 with the address of the next one at the start of each line. */
1096 fputs_filtered (pc_prefix (next_address
), gdb_stdout
);
1097 print_address (next_gdbarch
, next_address
, gdb_stdout
);
1098 printf_filtered (":");
1103 printf_filtered ("\t");
1104 /* Note that print_formatted sets next_address for the next
1106 last_examine_address
= next_address
;
1108 /* The value to be displayed is not fetched greedily.
1109 Instead, to avoid the possibility of a fetched value not
1110 being used, its retrieval is delayed until the print code
1111 uses it. When examining an instruction stream, the
1112 disassembler will perform its own memory fetch using just
1113 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1114 the disassembler be modified so that LAST_EXAMINE_VALUE
1115 is left with the byte sequence from the last complete
1116 instruction fetched from memory? */
1118 = release_value (value_at_lazy (val_type
, next_address
));
1120 print_formatted (last_examine_value
.get (), size
, &opts
, gdb_stdout
);
1122 /* Display any branch delay slots following the final insn. */
1123 if (format
== 'i' && count
== 1)
1124 count
+= branch_delay_insns
;
1126 printf_filtered ("\n");
1129 if (need_to_update_next_address
)
1130 next_address
= addr_rewound
;
1134 validate_format (struct format_data fmt
, const char *cmdname
)
1137 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
1139 error (_("Item count other than 1 is meaningless in \"%s\" command."),
1141 if (fmt
.format
== 'i')
1142 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
1143 fmt
.format
, cmdname
);
1146 /* Parse print command format string into *OPTS and update *EXPP.
1147 CMDNAME should name the current command. */
1150 print_command_parse_format (const char **expp
, const char *cmdname
,
1151 value_print_options
*opts
)
1153 const char *exp
= *expp
;
1155 if (exp
&& *exp
== '/')
1160 fmt
= decode_format (&exp
, last_format
, 0);
1161 validate_format (fmt
, cmdname
);
1162 last_format
= fmt
.format
;
1164 opts
->format
= fmt
.format
;
1165 opts
->raw
= fmt
.raw
;
1176 /* See valprint.h. */
1179 print_value (value
*val
, const value_print_options
&opts
)
1181 int histindex
= record_latest_value (val
);
1183 annotate_value_history_begin (histindex
, value_type (val
));
1185 printf_filtered ("$%d = ", histindex
);
1187 annotate_value_history_value ();
1189 print_formatted (val
, 0, &opts
, gdb_stdout
);
1190 printf_filtered ("\n");
1192 annotate_value_history_end ();
1195 /* Implementation of the "print" and "call" commands. */
1198 print_command_1 (const char *args
, int voidprint
)
1201 value_print_options print_opts
;
1203 get_user_print_options (&print_opts
);
1204 /* Override global settings with explicit options, if any. */
1205 auto group
= make_value_print_options_def_group (&print_opts
);
1206 gdb::option::process_options
1207 (&args
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
1209 print_command_parse_format (&args
, "print", &print_opts
);
1211 const char *exp
= args
;
1213 if (exp
!= nullptr && *exp
)
1215 expression_up expr
= parse_expression (exp
);
1216 val
= evaluate_expression (expr
.get ());
1219 val
= access_value_history (0);
1221 if (voidprint
|| (val
&& value_type (val
) &&
1222 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
1223 print_value (val
, print_opts
);
1226 /* See valprint.h. */
1229 print_command_completer (struct cmd_list_element
*ignore
,
1230 completion_tracker
&tracker
,
1231 const char *text
, const char * /*word*/)
1233 const auto group
= make_value_print_options_def_group (nullptr);
1234 if (gdb::option::complete_options
1235 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
))
1238 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1239 expression_completer (ignore
, tracker
, text
, word
);
1243 print_command (const char *exp
, int from_tty
)
1245 print_command_1 (exp
, 1);
1248 /* Same as print, except it doesn't print void results. */
1250 call_command (const char *exp
, int from_tty
)
1252 print_command_1 (exp
, 0);
1255 /* Implementation of the "output" command. */
1258 output_command (const char *exp
, int from_tty
)
1262 struct format_data fmt
;
1263 struct value_print_options opts
;
1268 if (exp
&& *exp
== '/')
1271 fmt
= decode_format (&exp
, 0, 0);
1272 validate_format (fmt
, "output");
1273 format
= fmt
.format
;
1276 expression_up expr
= parse_expression (exp
);
1278 val
= evaluate_expression (expr
.get ());
1280 annotate_value_begin (value_type (val
));
1282 get_formatted_print_options (&opts
, format
);
1284 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
1286 annotate_value_end ();
1289 gdb_flush (gdb_stdout
);
1293 set_command (const char *exp
, int from_tty
)
1295 expression_up expr
= parse_expression (exp
);
1297 if (expr
->nelts
>= 1)
1298 switch (expr
->elts
[0].opcode
)
1300 case UNOP_PREINCREMENT
:
1301 case UNOP_POSTINCREMENT
:
1302 case UNOP_PREDECREMENT
:
1303 case UNOP_POSTDECREMENT
:
1305 case BINOP_ASSIGN_MODIFY
:
1310 (_("Expression is not an assignment (and might have no effect)"));
1313 evaluate_expression (expr
.get ());
1317 info_symbol_command (const char *arg
, int from_tty
)
1319 struct minimal_symbol
*msymbol
;
1320 struct obj_section
*osect
;
1321 CORE_ADDR addr
, sect_addr
;
1323 unsigned int offset
;
1326 error_no_arg (_("address"));
1328 addr
= parse_and_eval_address (arg
);
1329 for (objfile
*objfile
: current_program_space
->objfiles ())
1330 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
1332 /* Only process each object file once, even if there's a separate
1334 if (objfile
->separate_debug_objfile_backlink
)
1337 sect_addr
= overlay_mapped_address (addr
, osect
);
1339 if (obj_section_addr (osect
) <= sect_addr
1340 && sect_addr
< obj_section_endaddr (osect
)
1342 = lookup_minimal_symbol_by_pc_section (sect_addr
,
1345 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1346 const char *loc_string
;
1349 offset
= sect_addr
- MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
1350 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1351 sec_name
= osect
->the_bfd_section
->name
;
1352 msym_name
= MSYMBOL_PRINT_NAME (msymbol
);
1354 /* Don't print the offset if it is zero.
1355 We assume there's no need to handle i18n of "sym + offset". */
1356 std::string string_holder
;
1359 string_holder
= string_printf ("%s + %u", msym_name
, offset
);
1360 loc_string
= string_holder
.c_str ();
1363 loc_string
= msym_name
;
1365 gdb_assert (osect
->objfile
&& objfile_name (osect
->objfile
));
1366 obj_name
= objfile_name (osect
->objfile
);
1368 if (MULTI_OBJFILE_P ())
1369 if (pc_in_unmapped_range (addr
, osect
))
1370 if (section_is_overlay (osect
))
1371 printf_filtered (_("%s in load address range of "
1372 "%s overlay section %s of %s\n"),
1373 loc_string
, mapped
, sec_name
, obj_name
);
1375 printf_filtered (_("%s in load address range of "
1376 "section %s of %s\n"),
1377 loc_string
, sec_name
, obj_name
);
1379 if (section_is_overlay (osect
))
1380 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1381 loc_string
, mapped
, sec_name
, obj_name
);
1383 printf_filtered (_("%s in section %s of %s\n"),
1384 loc_string
, sec_name
, obj_name
);
1386 if (pc_in_unmapped_range (addr
, osect
))
1387 if (section_is_overlay (osect
))
1388 printf_filtered (_("%s in load address range of %s overlay "
1390 loc_string
, mapped
, sec_name
);
1393 (_("%s in load address range of section %s\n"),
1394 loc_string
, sec_name
);
1396 if (section_is_overlay (osect
))
1397 printf_filtered (_("%s in %s overlay section %s\n"),
1398 loc_string
, mapped
, sec_name
);
1400 printf_filtered (_("%s in section %s\n"),
1401 loc_string
, sec_name
);
1405 printf_filtered (_("No symbol matches %s.\n"), arg
);
1409 info_address_command (const char *exp
, int from_tty
)
1411 struct gdbarch
*gdbarch
;
1414 struct bound_minimal_symbol msymbol
;
1416 struct obj_section
*section
;
1417 CORE_ADDR load_addr
, context_pc
= 0;
1418 struct field_of_this_result is_a_field_of_this
;
1421 error (_("Argument required."));
1423 sym
= lookup_symbol (exp
, get_selected_block (&context_pc
), VAR_DOMAIN
,
1424 &is_a_field_of_this
).symbol
;
1427 if (is_a_field_of_this
.type
!= NULL
)
1429 printf_filtered ("Symbol \"");
1430 fprintf_symbol_filtered (gdb_stdout
, exp
,
1431 current_language
->la_language
, DMGL_ANSI
);
1432 printf_filtered ("\" is a field of the local class variable ");
1433 if (current_language
->la_language
== language_objc
)
1434 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1436 printf_filtered ("`this'\n");
1440 msymbol
= lookup_bound_minimal_symbol (exp
);
1442 if (msymbol
.minsym
!= NULL
)
1444 struct objfile
*objfile
= msymbol
.objfile
;
1446 gdbarch
= get_objfile_arch (objfile
);
1447 load_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1449 printf_filtered ("Symbol \"");
1450 fprintf_symbol_filtered (gdb_stdout
, exp
,
1451 current_language
->la_language
, DMGL_ANSI
);
1452 printf_filtered ("\" is at ");
1453 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1455 printf_filtered (" in a file compiled without debugging");
1456 section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
.minsym
);
1457 if (section_is_overlay (section
))
1459 load_addr
= overlay_unmapped_address (load_addr
, section
);
1460 printf_filtered (",\n -- loaded at ");
1461 fputs_styled (paddress (gdbarch
, load_addr
),
1462 address_style
.style (),
1464 printf_filtered (" in overlay section %s",
1465 section
->the_bfd_section
->name
);
1467 printf_filtered (".\n");
1470 error (_("No symbol \"%s\" in current context."), exp
);
1474 printf_filtered ("Symbol \"");
1475 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1476 current_language
->la_language
, DMGL_ANSI
);
1477 printf_filtered ("\" is ");
1478 val
= SYMBOL_VALUE (sym
);
1479 if (SYMBOL_OBJFILE_OWNED (sym
))
1480 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sym
), sym
);
1483 gdbarch
= symbol_arch (sym
);
1485 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
1487 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, context_pc
,
1489 printf_filtered (".\n");
1493 switch (SYMBOL_CLASS (sym
))
1496 case LOC_CONST_BYTES
:
1497 printf_filtered ("constant");
1501 printf_filtered ("a label at address ");
1502 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1503 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1505 if (section_is_overlay (section
))
1507 load_addr
= overlay_unmapped_address (load_addr
, section
);
1508 printf_filtered (",\n -- loaded at ");
1509 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1511 printf_filtered (" in overlay section %s",
1512 section
->the_bfd_section
->name
);
1517 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
1520 /* GDBARCH is the architecture associated with the objfile the symbol
1521 is defined in; the target architecture may be different, and may
1522 provide additional registers. However, we do not know the target
1523 architecture at this point. We assume the objfile architecture
1524 will contain all the standard registers that occur in debug info
1526 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1528 if (SYMBOL_IS_ARGUMENT (sym
))
1529 printf_filtered (_("an argument in register %s"),
1530 gdbarch_register_name (gdbarch
, regno
));
1532 printf_filtered (_("a variable in register %s"),
1533 gdbarch_register_name (gdbarch
, regno
));
1537 printf_filtered (_("static storage at address "));
1538 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1539 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1541 if (section_is_overlay (section
))
1543 load_addr
= overlay_unmapped_address (load_addr
, section
);
1544 printf_filtered (_(",\n -- loaded at "));
1545 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1547 printf_filtered (_(" in overlay section %s"),
1548 section
->the_bfd_section
->name
);
1552 case LOC_REGPARM_ADDR
:
1553 /* Note comment at LOC_REGISTER. */
1554 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1555 printf_filtered (_("address of an argument in register %s"),
1556 gdbarch_register_name (gdbarch
, regno
));
1560 printf_filtered (_("an argument at offset %ld"), val
);
1564 printf_filtered (_("a local variable at frame offset %ld"), val
);
1568 printf_filtered (_("a reference argument at offset %ld"), val
);
1572 printf_filtered (_("a typedef"));
1576 printf_filtered (_("a function at address "));
1577 load_addr
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
));
1578 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1580 if (section_is_overlay (section
))
1582 load_addr
= overlay_unmapped_address (load_addr
, section
);
1583 printf_filtered (_(",\n -- loaded at "));
1584 fputs_styled (paddress (gdbarch
, load_addr
), address_style
.style (),
1586 printf_filtered (_(" in overlay section %s"),
1587 section
->the_bfd_section
->name
);
1591 case LOC_UNRESOLVED
:
1593 struct bound_minimal_symbol msym
;
1595 msym
= lookup_bound_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
));
1596 if (msym
.minsym
== NULL
)
1597 printf_filtered ("unresolved");
1600 section
= MSYMBOL_OBJ_SECTION (msym
.objfile
, msym
.minsym
);
1603 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1605 load_addr
= MSYMBOL_VALUE_RAW_ADDRESS (msym
.minsym
);
1606 printf_filtered (_("a thread-local variable at offset %s "
1607 "in the thread-local storage for `%s'"),
1608 paddress (gdbarch
, load_addr
),
1609 objfile_name (section
->objfile
));
1613 load_addr
= BMSYMBOL_VALUE_ADDRESS (msym
);
1614 printf_filtered (_("static storage at address "));
1615 fputs_styled (paddress (gdbarch
, load_addr
),
1616 address_style
.style (), gdb_stdout
);
1617 if (section_is_overlay (section
))
1619 load_addr
= overlay_unmapped_address (load_addr
, section
);
1620 printf_filtered (_(",\n -- loaded at "));
1621 fputs_styled (paddress (gdbarch
, load_addr
),
1622 address_style
.style (),
1624 printf_filtered (_(" in overlay section %s"),
1625 section
->the_bfd_section
->name
);
1632 case LOC_OPTIMIZED_OUT
:
1633 printf_filtered (_("optimized out"));
1637 printf_filtered (_("of unknown (botched) type"));
1640 printf_filtered (".\n");
1645 x_command (const char *exp
, int from_tty
)
1647 struct format_data fmt
;
1650 fmt
.format
= last_format
? last_format
: 'x';
1651 fmt
.size
= last_size
;
1655 /* If there is no expression and no format, use the most recent
1657 if (exp
== nullptr && last_count
> 0)
1658 fmt
.count
= last_count
;
1660 if (exp
&& *exp
== '/')
1662 const char *tmp
= exp
+ 1;
1664 fmt
= decode_format (&tmp
, last_format
, last_size
);
1668 last_count
= fmt
.count
;
1670 /* If we have an expression, evaluate it and use it as the address. */
1672 if (exp
!= 0 && *exp
!= 0)
1674 expression_up expr
= parse_expression (exp
);
1675 /* Cause expression not to be there any more if this command is
1676 repeated with Newline. But don't clobber a user-defined
1677 command's definition. */
1679 set_repeat_arguments ("");
1680 val
= evaluate_expression (expr
.get ());
1681 if (TYPE_IS_REFERENCE (value_type (val
)))
1682 val
= coerce_ref (val
);
1683 /* In rvalue contexts, such as this, functions are coerced into
1684 pointers to functions. This makes "x/i main" work. */
1685 if (/* last_format == 'i' && */
1686 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1687 && VALUE_LVAL (val
) == lval_memory
)
1688 next_address
= value_address (val
);
1690 next_address
= value_as_address (val
);
1692 next_gdbarch
= expr
->gdbarch
;
1696 error_no_arg (_("starting display address"));
1698 do_examine (fmt
, next_gdbarch
, next_address
);
1700 /* If the examine succeeds, we remember its size and format for next
1701 time. Set last_size to 'b' for strings. */
1702 if (fmt
.format
== 's')
1705 last_size
= fmt
.size
;
1706 last_format
= fmt
.format
;
1708 /* Set a couple of internal variables if appropriate. */
1709 if (last_examine_value
!= nullptr)
1711 /* Make last address examined available to the user as $_. Use
1712 the correct pointer type. */
1713 struct type
*pointer_type
1714 = lookup_pointer_type (value_type (last_examine_value
.get ()));
1715 set_internalvar (lookup_internalvar ("_"),
1716 value_from_pointer (pointer_type
,
1717 last_examine_address
));
1719 /* Make contents of last address examined available to the user
1720 as $__. If the last value has not been fetched from memory
1721 then don't fetch it now; instead mark it by voiding the $__
1723 if (value_lazy (last_examine_value
.get ()))
1724 clear_internalvar (lookup_internalvar ("__"));
1726 set_internalvar (lookup_internalvar ("__"), last_examine_value
.get ());
1731 /* Add an expression to the auto-display chain.
1732 Specify the expression. */
1735 display_command (const char *arg
, int from_tty
)
1737 struct format_data fmt
;
1738 struct display
*newobj
;
1739 const char *exp
= arg
;
1750 fmt
= decode_format (&exp
, 0, 0);
1751 if (fmt
.size
&& fmt
.format
== 0)
1753 if (fmt
.format
== 'i' || fmt
.format
== 's')
1764 innermost_block_tracker tracker
;
1765 expression_up expr
= parse_expression (exp
, &tracker
);
1767 newobj
= new display ();
1769 newobj
->exp_string
= xstrdup (exp
);
1770 newobj
->exp
= std::move (expr
);
1771 newobj
->block
= tracker
.block ();
1772 newobj
->pspace
= current_program_space
;
1773 newobj
->number
= ++display_number
;
1774 newobj
->format
= fmt
;
1775 newobj
->enabled_p
= 1;
1776 newobj
->next
= NULL
;
1778 if (display_chain
== NULL
)
1779 display_chain
= newobj
;
1782 struct display
*last
;
1784 for (last
= display_chain
; last
->next
!= NULL
; last
= last
->next
)
1786 last
->next
= newobj
;
1790 do_one_display (newobj
);
1796 free_display (struct display
*d
)
1798 xfree (d
->exp_string
);
1802 /* Clear out the display_chain. Done when new symtabs are loaded,
1803 since this invalidates the types stored in many expressions. */
1806 clear_displays (void)
1810 while ((d
= display_chain
) != NULL
)
1812 display_chain
= d
->next
;
1817 /* Delete the auto-display DISPLAY. */
1820 delete_display (struct display
*display
)
1824 gdb_assert (display
!= NULL
);
1826 if (display_chain
== display
)
1827 display_chain
= display
->next
;
1830 if (d
->next
== display
)
1832 d
->next
= display
->next
;
1836 free_display (display
);
1839 /* Call FUNCTION on each of the displays whose numbers are given in
1840 ARGS. DATA is passed unmodified to FUNCTION. */
1843 map_display_numbers (const char *args
,
1844 void (*function
) (struct display
*,
1851 error_no_arg (_("one or more display numbers"));
1853 number_or_range_parser
parser (args
);
1855 while (!parser
.finished ())
1857 const char *p
= parser
.cur_tok ();
1859 num
= parser
.get_number ();
1861 warning (_("bad display number at or near '%s'"), p
);
1864 struct display
*d
, *tmp
;
1866 ALL_DISPLAYS_SAFE (d
, tmp
)
1867 if (d
->number
== num
)
1870 printf_unfiltered (_("No display number %d.\n"), num
);
1877 /* Callback for map_display_numbers, that deletes a display. */
1880 do_delete_display (struct display
*d
, void *data
)
1885 /* "undisplay" command. */
1888 undisplay_command (const char *args
, int from_tty
)
1892 if (query (_("Delete all auto-display expressions? ")))
1898 map_display_numbers (args
, do_delete_display
, NULL
);
1902 /* Display a single auto-display.
1903 Do nothing if the display cannot be printed in the current context,
1904 or if the display is disabled. */
1907 do_one_display (struct display
*d
)
1909 int within_current_scope
;
1911 if (d
->enabled_p
== 0)
1914 /* The expression carries the architecture that was used at parse time.
1915 This is a problem if the expression depends on architecture features
1916 (e.g. register numbers), and the current architecture is now different.
1917 For example, a display statement like "display/i $pc" is expected to
1918 display the PC register of the current architecture, not the arch at
1919 the time the display command was given. Therefore, we re-parse the
1920 expression if the current architecture has changed. */
1921 if (d
->exp
!= NULL
&& d
->exp
->gdbarch
!= get_current_arch ())
1932 innermost_block_tracker tracker
;
1933 d
->exp
= parse_expression (d
->exp_string
, &tracker
);
1934 d
->block
= tracker
.block ();
1936 catch (const gdb_exception
&ex
)
1938 /* Can't re-parse the expression. Disable this display item. */
1940 warning (_("Unable to display \"%s\": %s"),
1941 d
->exp_string
, ex
.what ());
1948 if (d
->pspace
== current_program_space
)
1949 within_current_scope
= contained_in (get_selected_block (0), d
->block
,
1952 within_current_scope
= 0;
1955 within_current_scope
= 1;
1956 if (!within_current_scope
)
1959 scoped_restore save_display_number
1960 = make_scoped_restore (¤t_display_number
, d
->number
);
1962 annotate_display_begin ();
1963 printf_filtered ("%d", d
->number
);
1964 annotate_display_number_end ();
1965 printf_filtered (": ");
1969 annotate_display_format ();
1971 printf_filtered ("x/");
1972 if (d
->format
.count
!= 1)
1973 printf_filtered ("%d", d
->format
.count
);
1974 printf_filtered ("%c", d
->format
.format
);
1975 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1976 printf_filtered ("%c", d
->format
.size
);
1977 printf_filtered (" ");
1979 annotate_display_expression ();
1981 puts_filtered (d
->exp_string
);
1982 annotate_display_expression_end ();
1984 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1985 printf_filtered ("\n");
1987 printf_filtered (" ");
1989 annotate_display_value ();
1996 val
= evaluate_expression (d
->exp
.get ());
1997 addr
= value_as_address (val
);
1998 if (d
->format
.format
== 'i')
1999 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
2000 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
2002 catch (const gdb_exception_error
&ex
)
2004 fprintf_filtered (gdb_stdout
, _("<error: %s>\n"),
2010 struct value_print_options opts
;
2012 annotate_display_format ();
2014 if (d
->format
.format
)
2015 printf_filtered ("/%c ", d
->format
.format
);
2017 annotate_display_expression ();
2019 puts_filtered (d
->exp_string
);
2020 annotate_display_expression_end ();
2022 printf_filtered (" = ");
2024 annotate_display_expression ();
2026 get_formatted_print_options (&opts
, d
->format
.format
);
2027 opts
.raw
= d
->format
.raw
;
2033 val
= evaluate_expression (d
->exp
.get ());
2034 print_formatted (val
, d
->format
.size
, &opts
, gdb_stdout
);
2036 catch (const gdb_exception_error
&ex
)
2038 fprintf_filtered (gdb_stdout
, _("<error: %s>"), ex
.what ());
2041 printf_filtered ("\n");
2044 annotate_display_end ();
2046 gdb_flush (gdb_stdout
);
2049 /* Display all of the values on the auto-display chain which can be
2050 evaluated in the current scope. */
2057 for (d
= display_chain
; d
; d
= d
->next
)
2061 /* Delete the auto-display which we were in the process of displaying.
2062 This is done when there is an error or a signal. */
2065 disable_display (int num
)
2069 for (d
= display_chain
; d
; d
= d
->next
)
2070 if (d
->number
== num
)
2075 printf_unfiltered (_("No display number %d.\n"), num
);
2079 disable_current_display (void)
2081 if (current_display_number
>= 0)
2083 disable_display (current_display_number
);
2084 fprintf_unfiltered (gdb_stderr
,
2085 _("Disabling display %d to "
2086 "avoid infinite recursion.\n"),
2087 current_display_number
);
2089 current_display_number
= -1;
2093 info_display_command (const char *ignore
, int from_tty
)
2098 printf_unfiltered (_("There are no auto-display expressions now.\n"));
2100 printf_filtered (_("Auto-display expressions now in effect:\n\
2101 Num Enb Expression\n"));
2103 for (d
= display_chain
; d
; d
= d
->next
)
2105 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
2107 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
2109 else if (d
->format
.format
)
2110 printf_filtered ("/%c ", d
->format
.format
);
2111 puts_filtered (d
->exp_string
);
2112 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
, true))
2113 printf_filtered (_(" (cannot be evaluated in the current context)"));
2114 printf_filtered ("\n");
2118 /* Callback fo map_display_numbers, that enables or disables the
2119 passed in display D. */
2122 do_enable_disable_display (struct display
*d
, void *data
)
2124 d
->enabled_p
= *(int *) data
;
2127 /* Implamentation of both the "disable display" and "enable display"
2128 commands. ENABLE decides what to do. */
2131 enable_disable_display_command (const char *args
, int from_tty
, int enable
)
2138 d
->enabled_p
= enable
;
2142 map_display_numbers (args
, do_enable_disable_display
, &enable
);
2145 /* The "enable display" command. */
2148 enable_display_command (const char *args
, int from_tty
)
2150 enable_disable_display_command (args
, from_tty
, 1);
2153 /* The "disable display" command. */
2156 disable_display_command (const char *args
, int from_tty
)
2158 enable_disable_display_command (args
, from_tty
, 0);
2161 /* display_chain items point to blocks and expressions. Some expressions in
2162 turn may point to symbols.
2163 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2164 obstack_free'd when a shared library is unloaded.
2165 Clear pointers that are about to become dangling.
2166 Both .exp and .block fields will be restored next time we need to display
2167 an item by re-parsing .exp_string field in the new execution context. */
2170 clear_dangling_display_expressions (struct objfile
*objfile
)
2173 struct program_space
*pspace
;
2175 /* With no symbol file we cannot have a block or expression from it. */
2176 if (objfile
== NULL
)
2178 pspace
= objfile
->pspace
;
2179 if (objfile
->separate_debug_objfile_backlink
)
2181 objfile
= objfile
->separate_debug_objfile_backlink
;
2182 gdb_assert (objfile
->pspace
== pspace
);
2185 for (d
= display_chain
; d
!= NULL
; d
= d
->next
)
2187 if (d
->pspace
!= pspace
)
2190 if (lookup_objfile_from_block (d
->block
) == objfile
2191 || (d
->exp
!= NULL
&& exp_uses_objfile (d
->exp
.get (), objfile
)))
2200 /* Print the value in stack frame FRAME of a variable specified by a
2201 struct symbol. NAME is the name to print; if NULL then VAR's print
2202 name will be used. STREAM is the ui_file on which to print the
2203 value. INDENT specifies the number of indent levels to print
2204 before printing the variable name.
2206 This function invalidates FRAME. */
2209 print_variable_and_value (const char *name
, struct symbol
*var
,
2210 struct frame_info
*frame
,
2211 struct ui_file
*stream
, int indent
)
2215 name
= SYMBOL_PRINT_NAME (var
);
2217 fputs_filtered (n_spaces (2 * indent
), stream
);
2218 fputs_styled (name
, variable_name_style
.style (), stream
);
2219 fputs_filtered (" = ", stream
);
2224 struct value_print_options opts
;
2226 /* READ_VAR_VALUE needs a block in order to deal with non-local
2227 references (i.e. to handle nested functions). In this context, we
2228 print variables that are local to this frame, so we can avoid passing
2230 val
= read_var_value (var
, NULL
, frame
);
2231 get_user_print_options (&opts
);
2233 common_val_print (val
, stream
, indent
, &opts
, current_language
);
2235 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2239 catch (const gdb_exception_error
&except
)
2241 fprintf_filtered (stream
, "<error reading variable %s (%s)>", name
,
2245 fprintf_filtered (stream
, "\n");
2248 /* Subroutine of ui_printf to simplify it.
2249 Print VALUE to STREAM using FORMAT.
2250 VALUE is a C-style string either on the target or
2251 in a GDB internal variable. */
2254 printf_c_string (struct ui_file
*stream
, const char *format
,
2255 struct value
*value
)
2257 const gdb_byte
*str
;
2259 if (VALUE_LVAL (value
) == lval_internalvar
2260 && c_is_string_type_p (value_type (value
)))
2262 size_t len
= TYPE_LENGTH (value_type (value
));
2264 /* Copy the internal var value to TEM_STR and append a terminating null
2265 character. This protects against corrupted C-style strings that lack
2266 the terminating null char. It also allows Ada-style strings (not
2267 null terminated) to be printed without problems. */
2268 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2270 memcpy (tem_str
, value_contents (value
), len
);
2276 CORE_ADDR tem
= value_as_address (value
);;
2281 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2282 fprintf_filtered (stream
, format
, "(null)");
2287 /* This is a %s argument. Find the length of the string. */
2290 for (len
= 0;; len
++)
2295 read_memory (tem
+ len
, &c
, 1);
2300 /* Copy the string contents into a string inside GDB. */
2301 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ 1);
2304 read_memory (tem
, tem_str
, len
);
2310 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2311 fprintf_filtered (stream
, format
, (char *) str
);
2315 /* Subroutine of ui_printf to simplify it.
2316 Print VALUE to STREAM using FORMAT.
2317 VALUE is a wide C-style string on the target or
2318 in a GDB internal variable. */
2321 printf_wide_c_string (struct ui_file
*stream
, const char *format
,
2322 struct value
*value
)
2324 const gdb_byte
*str
;
2326 struct gdbarch
*gdbarch
= get_type_arch (value_type (value
));
2327 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2328 "wchar_t", NULL
, 0);
2329 int wcwidth
= TYPE_LENGTH (wctype
);
2331 if (VALUE_LVAL (value
) == lval_internalvar
2332 && c_is_string_type_p (value_type (value
)))
2334 str
= value_contents (value
);
2335 len
= TYPE_LENGTH (value_type (value
));
2339 CORE_ADDR tem
= value_as_address (value
);
2344 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2345 fprintf_filtered (stream
, format
, "(null)");
2350 /* This is a %s argument. Find the length of the string. */
2351 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2352 gdb_byte
*buf
= (gdb_byte
*) alloca (wcwidth
);
2354 for (len
= 0;; len
+= wcwidth
)
2357 read_memory (tem
+ len
, buf
, wcwidth
);
2358 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2362 /* Copy the string contents into a string inside GDB. */
2363 gdb_byte
*tem_str
= (gdb_byte
*) alloca (len
+ wcwidth
);
2366 read_memory (tem
, tem_str
, len
);
2367 memset (&tem_str
[len
], 0, wcwidth
);
2371 auto_obstack output
;
2373 convert_between_encodings (target_wide_charset (gdbarch
),
2376 &output
, translit_char
);
2377 obstack_grow_str0 (&output
, "");
2380 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2381 fprintf_filtered (stream
, format
, obstack_base (&output
));
2385 /* Subroutine of ui_printf to simplify it.
2386 Print VALUE, a floating point value, to STREAM using FORMAT. */
2389 printf_floating (struct ui_file
*stream
, const char *format
,
2390 struct value
*value
, enum argclass argclass
)
2392 /* Parameter data. */
2393 struct type
*param_type
= value_type (value
);
2394 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2396 /* Determine target type corresponding to the format string. */
2397 struct type
*fmt_type
;
2401 fmt_type
= builtin_type (gdbarch
)->builtin_double
;
2403 case long_double_arg
:
2404 fmt_type
= builtin_type (gdbarch
)->builtin_long_double
;
2406 case dec32float_arg
:
2407 fmt_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2409 case dec64float_arg
:
2410 fmt_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2412 case dec128float_arg
:
2413 fmt_type
= builtin_type (gdbarch
)->builtin_declong
;
2416 gdb_assert_not_reached ("unexpected argument class");
2419 /* To match the traditional GDB behavior, the conversion is
2420 done differently depending on the type of the parameter:
2422 - if the parameter has floating-point type, it's value
2423 is converted to the target type;
2425 - otherwise, if the parameter has a type that is of the
2426 same size as a built-in floating-point type, the value
2427 bytes are interpreted as if they were of that type, and
2428 then converted to the target type (this is not done for
2429 decimal floating-point argument classes);
2431 - otherwise, if the source value has an integer value,
2432 it's value is converted to the target type;
2434 - otherwise, an error is raised.
2436 In either case, the result of the conversion is a byte buffer
2437 formatted in the target format for the target type. */
2439 if (TYPE_CODE (fmt_type
) == TYPE_CODE_FLT
)
2441 param_type
= float_type_from_length (param_type
);
2442 if (param_type
!= value_type (value
))
2443 value
= value_from_contents (param_type
, value_contents (value
));
2446 value
= value_cast (fmt_type
, value
);
2448 /* Convert the value to a string and print it. */
2450 = target_float_to_string (value_contents (value
), fmt_type
, format
);
2451 fputs_filtered (str
.c_str (), stream
);
2454 /* Subroutine of ui_printf to simplify it.
2455 Print VALUE, a target pointer, to STREAM using FORMAT. */
2458 printf_pointer (struct ui_file
*stream
, const char *format
,
2459 struct value
*value
)
2461 /* We avoid the host's %p because pointers are too
2462 likely to be the wrong size. The only interesting
2463 modifier for %p is a width; extract that, and then
2464 handle %p as glibc would: %#x or a literal "(nil)". */
2468 #ifdef PRINTF_HAS_LONG_LONG
2469 long long val
= value_as_long (value
);
2471 long val
= value_as_long (value
);
2474 fmt
= (char *) alloca (strlen (format
) + 5);
2476 /* Copy up to the leading %. */
2481 int is_percent
= (*p
== '%');
2496 /* Copy any width or flags. Only the "-" flag is valid for pointers
2497 -- see the format_pieces constructor. */
2498 while (*p
== '-' || (*p
>= '0' && *p
< '9'))
2501 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2504 #ifdef PRINTF_HAS_LONG_LONG
2511 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2512 fprintf_filtered (stream
, fmt
, val
);
2520 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2521 fprintf_filtered (stream
, fmt
, "(nil)");
2526 /* printf "printf format string" ARG to STREAM. */
2529 ui_printf (const char *arg
, struct ui_file
*stream
)
2531 const char *s
= arg
;
2532 std::vector
<struct value
*> val_args
;
2535 error_no_arg (_("format-control string and values to print"));
2537 s
= skip_spaces (s
);
2539 /* A format string should follow, enveloped in double quotes. */
2541 error (_("Bad format string, missing '\"'."));
2543 format_pieces
fpieces (&s
);
2546 error (_("Bad format string, non-terminated '\"'."));
2548 s
= skip_spaces (s
);
2550 if (*s
!= ',' && *s
!= 0)
2551 error (_("Invalid argument syntax"));
2555 s
= skip_spaces (s
);
2560 const char *current_substring
;
2563 for (auto &&piece
: fpieces
)
2564 if (piece
.argclass
!= literal_piece
)
2567 /* Now, parse all arguments and evaluate them.
2568 Store the VALUEs in VAL_ARGS. */
2575 val_args
.push_back (parse_to_comma_and_eval (&s1
));
2582 if (val_args
.size () != nargs_wanted
)
2583 error (_("Wrong number of arguments for specified format-string"));
2585 /* Now actually print them. */
2587 for (auto &&piece
: fpieces
)
2589 current_substring
= piece
.string
;
2590 switch (piece
.argclass
)
2593 printf_c_string (stream
, current_substring
, val_args
[i
]);
2595 case wide_string_arg
:
2596 printf_wide_c_string (stream
, current_substring
, val_args
[i
]);
2600 struct gdbarch
*gdbarch
2601 = get_type_arch (value_type (val_args
[i
]));
2602 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2603 "wchar_t", NULL
, 0);
2604 struct type
*valtype
;
2605 const gdb_byte
*bytes
;
2607 valtype
= value_type (val_args
[i
]);
2608 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2609 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2610 error (_("expected wchar_t argument for %%lc"));
2612 bytes
= value_contents (val_args
[i
]);
2614 auto_obstack output
;
2616 convert_between_encodings (target_wide_charset (gdbarch
),
2618 bytes
, TYPE_LENGTH (valtype
),
2619 TYPE_LENGTH (valtype
),
2620 &output
, translit_char
);
2621 obstack_grow_str0 (&output
, "");
2624 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2625 fprintf_filtered (stream
, current_substring
,
2626 obstack_base (&output
));
2631 #ifdef PRINTF_HAS_LONG_LONG
2633 long long val
= value_as_long (val_args
[i
]);
2636 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2637 fprintf_filtered (stream
, current_substring
, val
);
2642 error (_("long long not supported in printf"));
2646 int val
= value_as_long (val_args
[i
]);
2649 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2650 fprintf_filtered (stream
, current_substring
, val
);
2656 long val
= value_as_long (val_args
[i
]);
2659 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2660 fprintf_filtered (stream
, current_substring
, val
);
2664 /* Handles floating-point values. */
2666 case long_double_arg
:
2667 case dec32float_arg
:
2668 case dec64float_arg
:
2669 case dec128float_arg
:
2670 printf_floating (stream
, current_substring
, val_args
[i
],
2674 printf_pointer (stream
, current_substring
, val_args
[i
]);
2677 /* Print a portion of the format string that has no
2678 directives. Note that this will not include any
2679 ordinary %-specs, but it might include "%%". That is
2680 why we use printf_filtered and not puts_filtered here.
2681 Also, we pass a dummy argument because some platforms
2682 have modified GCC to include -Wformat-security by
2683 default, which will warn here if there is no
2686 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2687 fprintf_filtered (stream
, current_substring
, 0);
2691 internal_error (__FILE__
, __LINE__
,
2692 _("failed internal consistency check"));
2694 /* Maybe advance to the next argument. */
2695 if (piece
.argclass
!= literal_piece
)
2701 /* Implement the "printf" command. */
2704 printf_command (const char *arg
, int from_tty
)
2706 ui_printf (arg
, gdb_stdout
);
2707 reset_terminal_style (gdb_stdout
);
2709 gdb_flush (gdb_stdout
);
2712 /* Implement the "eval" command. */
2715 eval_command (const char *arg
, int from_tty
)
2719 ui_printf (arg
, &stb
);
2721 std::string expanded
= insert_user_defined_cmd_args (stb
.c_str ());
2723 execute_command (expanded
.c_str (), from_tty
);
2727 _initialize_printcmd (void)
2729 struct cmd_list_element
*c
;
2731 current_display_number
= -1;
2733 gdb::observers::free_objfile
.attach (clear_dangling_display_expressions
);
2735 add_info ("address", info_address_command
,
2736 _("Describe where symbol SYM is stored.\n\
2737 Usage: info address SYM"));
2739 add_info ("symbol", info_symbol_command
, _("\
2740 Describe what symbol is at location ADDR.\n\
2741 Usage: info symbol ADDR\n\
2742 Only for symbols with fixed locations (global or static scope)."));
2744 add_com ("x", class_vars
, x_command
, _("\
2745 Examine memory: x/FMT ADDRESS.\n\
2746 ADDRESS is an expression for the memory address to examine.\n\
2747 FMT is a repeat count followed by a format letter and a size letter.\n\
2748 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2749 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2750 and z(hex, zero padded on the left).\n\
2751 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2752 The specified number of objects of the specified size are printed\n\
2753 according to the format. If a negative number is specified, memory is\n\
2754 examined backward from the address.\n\n\
2755 Defaults for format and size letters are those previously used.\n\
2756 Default count is 1. Default address is following last thing printed\n\
2757 with this command or \"print\"."));
2759 add_info ("display", info_display_command
, _("\
2760 Expressions to display when program stops, with code numbers.\n\
2761 Usage: info display"));
2763 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2764 Cancel some expressions to be displayed when program stops.\n\
2765 Usage: undisplay [NUM]...\n\
2766 Arguments are the code numbers of the expressions to stop displaying.\n\
2767 No argument means cancel all automatic-display expressions.\n\
2768 \"delete display\" has the same effect as this command.\n\
2769 Do \"info display\" to see current list of code numbers."),
2772 add_com ("display", class_vars
, display_command
, _("\
2773 Print value of expression EXP each time the program stops.\n\
2774 Usage: display[/FMT] EXP\n\
2775 /FMT may be used before EXP as in the \"print\" command.\n\
2776 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2777 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2778 and examining is done as in the \"x\" command.\n\n\
2779 With no argument, display all currently requested auto-display expressions.\n\
2780 Use \"undisplay\" to cancel display requests previously made."));
2782 add_cmd ("display", class_vars
, enable_display_command
, _("\
2783 Enable some expressions to be displayed when program stops.\n\
2784 Usage: enable display [NUM]...\n\
2785 Arguments are the code numbers of the expressions to resume displaying.\n\
2786 No argument means enable all automatic-display expressions.\n\
2787 Do \"info display\" to see current list of code numbers."), &enablelist
);
2789 add_cmd ("display", class_vars
, disable_display_command
, _("\
2790 Disable some expressions to be displayed when program stops.\n\
2791 Usage: disable display [NUM]...\n\
2792 Arguments are the code numbers of the expressions to stop displaying.\n\
2793 No argument means disable all automatic-display expressions.\n\
2794 Do \"info display\" to see current list of code numbers."), &disablelist
);
2796 add_cmd ("display", class_vars
, undisplay_command
, _("\
2797 Cancel some expressions to be displayed when program stops.\n\
2798 Usage: delete display [NUM]...\n\
2799 Arguments are the code numbers of the expressions to stop displaying.\n\
2800 No argument means cancel all automatic-display expressions.\n\
2801 Do \"info display\" to see current list of code numbers."), &deletelist
);
2803 add_com ("printf", class_vars
, printf_command
, _("\
2804 Formatted printing, like the C \"printf\" function.\n\
2805 Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2806 This supports most C printf format specifications, like %s, %d, etc."));
2808 add_com ("output", class_vars
, output_command
, _("\
2809 Like \"print\" but don't put in value history and don't print newline.\n\
2810 Usage: output EXP\n\
2811 This is useful in user-defined commands."));
2813 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2814 Evaluate expression EXP and assign result to variable VAR.\n\
2815 Usage: set VAR = EXP\n\
2816 This uses assignment syntax appropriate for the current language\n\
2817 (VAR = EXP or VAR := EXP for example).\n\
2818 VAR may be a debugger \"convenience\" variable (names starting\n\
2819 with $), a register (a few standard names starting with $), or an actual\n\
2820 variable in the program being debugged. EXP is any valid expression.\n\
2821 Use \"set variable\" for variables with names identical to set subcommands.\n\
2823 With a subcommand, this command modifies parts of the gdb environment.\n\
2824 You can see these environment settings with the \"show\" command."),
2825 &setlist
, "set ", 1, &cmdlist
);
2827 add_com ("assign", class_vars
, set_command
, _("\
2828 Evaluate expression EXP and assign result to variable VAR.\n\
2829 Usage: assign VAR = EXP\n\
2830 This uses assignment syntax appropriate for the current language\n\
2831 (VAR = EXP or VAR := EXP for example).\n\
2832 VAR may be a debugger \"convenience\" variable (names starting\n\
2833 with $), a register (a few standard names starting with $), or an actual\n\
2834 variable in the program being debugged. EXP is any valid expression.\n\
2835 Use \"set variable\" for variables with names identical to set subcommands.\n\
2836 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2837 You can see these environment settings with the \"show\" command."));
2839 /* "call" is the same as "set", but handy for dbx users to call fns. */
2840 c
= add_com ("call", class_vars
, call_command
, _("\
2841 Call a function in the program.\n\
2843 The argument is the function name and arguments, in the notation of the\n\
2844 current working language. The result is printed and saved in the value\n\
2845 history, if it is not void."));
2846 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2848 add_cmd ("variable", class_vars
, set_command
, _("\
2849 Evaluate expression EXP and assign result to variable VAR.\n\
2850 Usage: set variable VAR = EXP\n\
2851 This uses assignment syntax appropriate for the current language\n\
2852 (VAR = EXP or VAR := EXP for example).\n\
2853 VAR may be a debugger \"convenience\" variable (names starting\n\
2854 with $), a register (a few standard names starting with $), or an actual\n\
2855 variable in the program being debugged. EXP is any valid expression.\n\
2856 This may usually be abbreviated to simply \"set\"."),
2858 add_alias_cmd ("var", "variable", class_vars
, 0, &setlist
);
2860 const auto print_opts
= make_value_print_options_def_group (nullptr);
2862 static const std::string print_help
= gdb::option::build_help (_("\
2863 Print value of expression EXP.\n\
2864 Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
2869 Note: because this command accepts arbitrary expressions, if you\n\
2870 specify any command option, you must use a double dash (\"--\")\n\
2871 to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
2873 Variables accessible are those of the lexical environment of the selected\n\
2874 stack frame, plus all those whose scope is global or an entire file.\n\
2876 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2877 $$NUM refers to NUM'th value back from the last one.\n\
2878 Names starting with $ refer to registers (with the values they would have\n\
2879 if the program were to return to the stack frame now selected, restoring\n\
2880 all registers saved by frames farther in) or else to debugger\n\
2881 \"convenience\" variables (any such name not a known register).\n\
2882 Use assignment expressions to give values to convenience variables.\n\
2884 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2885 @ is a binary operator for treating consecutive data objects\n\
2886 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2887 element is FOO, whose second element is stored in the space following\n\
2888 where FOO is stored, etc. FOO must be an expression whose value\n\
2889 resides in memory.\n\
2891 EXP may be preceded with /FMT, where FMT is a format letter\n\
2892 but no count or size letter (see \"x\" command)."),
2895 c
= add_com ("print", class_vars
, print_command
, print_help
.c_str ());
2896 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
2897 add_com_alias ("p", "print", class_vars
, 1);
2898 add_com_alias ("inspect", "print", class_vars
, 1);
2900 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2901 &max_symbolic_offset
, _("\
2902 Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2903 Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
2904 Tell GDB to only display the symbolic form of an address if the\n\
2905 offset between the closest earlier symbol and the address is less than\n\
2906 the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2907 to always print the symbolic form of an address if any symbol precedes\n\
2908 it. Zero is equivalent to \"unlimited\"."),
2910 show_max_symbolic_offset
,
2911 &setprintlist
, &showprintlist
);
2912 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2913 &print_symbol_filename
, _("\
2914 Set printing of source filename and line number with <SYMBOL>."), _("\
2915 Show printing of source filename and line number with <SYMBOL>."), NULL
,
2917 show_print_symbol_filename
,
2918 &setprintlist
, &showprintlist
);
2920 add_com ("eval", no_class
, eval_command
, _("\
2921 Construct a GDB command and then evaluate it.\n\
2922 Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
2923 Convert the arguments to a string as \"printf\" would, but then\n\
2924 treat this string as a command line, and evaluate it."));