1 /* Print values for GNU debugger GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
29 #include "expression.h"
33 #include "breakpoint.h"
37 #include "symfile.h" /* for overlay functions */
38 #include "objfiles.h" /* ditto */
39 #include "completer.h" /* for completion functions */
41 #include "gdb_assert.h"
46 #include "exceptions.h"
50 #include "parser-defs.h"
53 #include "tui/tui.h" /* For tui_active et.al. */
56 #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
57 # define USE_PRINTF_I64 1
58 # define PRINTF_HAS_LONG_LONG
60 # define USE_PRINTF_I64 0
63 extern int asm_demangle
; /* Whether to demangle syms in asm printouts */
72 /* Last specified output format. */
74 static char last_format
= 'x';
76 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
78 static char last_size
= 'w';
80 /* Default address to examine next. */
82 static CORE_ADDR next_address
;
84 /* Number of delay instructions following current disassembled insn. */
86 static int branch_delay_insns
;
88 /* Last address examined. */
90 static CORE_ADDR last_examine_address
;
92 /* Contents of last address examined.
93 This is not valid past the end of the `x' command! */
95 static struct value
*last_examine_value
;
97 /* Largest offset between a symbolic value and an address, that will be
98 printed as `0x1234 <symbol+offset>'. */
100 static unsigned int max_symbolic_offset
= UINT_MAX
;
102 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
103 struct cmd_list_element
*c
, const char *value
)
105 fprintf_filtered (file
, _("\
106 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
110 /* Append the source filename and linenumber of the symbol when
111 printing a symbolic value as `<symbol at filename:linenum>' if set. */
112 static int print_symbol_filename
= 0;
114 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
115 struct cmd_list_element
*c
, const char *value
)
117 fprintf_filtered (file
, _("\
118 Printing of source filename and line number with <symbol> is %s.\n"),
122 /* Number of auto-display expression currently being displayed.
123 So that we can disable it if we get an error or a signal within it.
124 -1 when not doing one. */
126 int current_display_number
;
130 /* Chain link to next auto-display item. */
131 struct display
*next
;
132 /* The expression as the user typed it. */
134 /* Expression to be evaluated and displayed. */
135 struct expression
*exp
;
136 /* Item number of this auto-display item. */
138 /* Display format specified. */
139 struct format_data format
;
140 /* Innermost block required by this expression when evaluated */
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 /* Prototypes for exported functions. */
155 void output_command (char *, int);
157 void _initialize_printcmd (void);
159 /* Prototypes for local functions. */
161 static void do_one_display (struct display
*);
164 /* Decode a format specification. *STRING_PTR should point to it.
165 OFORMAT and OSIZE are used as defaults for the format and size
166 if none are given in the format specification.
167 If OSIZE is zero, then the size field of the returned value
168 should be set only if a size is explicitly specified by the
170 The structure returned describes all the data
171 found in the specification. In addition, *STRING_PTR is advanced
172 past the specification and past all whitespace following it. */
174 static struct format_data
175 decode_format (char **string_ptr
, int oformat
, int osize
)
177 struct format_data val
;
178 char *p
= *string_ptr
;
184 if (*p
>= '0' && *p
<= '9')
185 val
.count
= atoi (p
);
186 while (*p
>= '0' && *p
<= '9')
189 /* Now process size or format letters that follow. */
193 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
195 else if (*p
>= 'a' && *p
<= 'z')
201 while (*p
== ' ' || *p
== '\t')
205 /* Set defaults for format and size if not specified. */
206 if (val
.format
== '?')
210 /* Neither has been specified. */
211 val
.format
= oformat
;
215 /* If a size is specified, any format makes a reasonable
216 default except 'i'. */
217 val
.format
= oformat
== 'i' ? 'x' : oformat
;
219 else if (val
.size
== '?')
224 /* Pick the appropriate size for an address. */
225 if (gdbarch_ptr_bit (current_gdbarch
) == 64)
226 val
.size
= osize
? 'g' : osize
;
227 else if (gdbarch_ptr_bit (current_gdbarch
) == 32)
228 val
.size
= osize
? 'w' : osize
;
229 else if (gdbarch_ptr_bit (current_gdbarch
) == 16)
230 val
.size
= osize
? 'h' : osize
;
232 /* Bad value for gdbarch_ptr_bit. */
233 internal_error (__FILE__
, __LINE__
,
234 _("failed internal consistency check"));
237 /* Floating point has to be word or giantword. */
238 if (osize
== 'w' || osize
== 'g')
241 /* Default it to giantword if the last used size is not
243 val
.size
= osize
? 'g' : osize
;
246 /* Characters default to one byte. */
247 val
.size
= osize
? 'b' : osize
;
250 /* The default is the size most recently specified. */
257 /* Print value VAL on stream according to OPTIONS.
258 Do not end with a newline.
259 SIZE is the letter for the size of datum being printed.
260 This is used to pad hex numbers so they line up. SIZE is 0
261 for print / output and set for examine. */
264 print_formatted (struct value
*val
, int size
,
265 const struct value_print_options
*options
,
266 struct ui_file
*stream
)
268 struct type
*type
= check_typedef (value_type (val
));
269 int len
= TYPE_LENGTH (type
);
271 if (VALUE_LVAL (val
) == lval_memory
)
272 next_address
= VALUE_ADDRESS (val
) + len
;
276 switch (options
->format
)
279 /* FIXME: Need to handle wchar_t's here... */
280 next_address
= VALUE_ADDRESS (val
)
281 + val_print_string (VALUE_ADDRESS (val
), -1, 1, stream
,
286 /* We often wrap here if there are long symbolic names. */
288 next_address
= (VALUE_ADDRESS (val
)
289 + gdb_print_insn (VALUE_ADDRESS (val
), stream
,
290 &branch_delay_insns
));
295 if (options
->format
== 0 || options
->format
== 's'
296 || TYPE_CODE (type
) == TYPE_CODE_REF
297 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
298 || TYPE_CODE (type
) == TYPE_CODE_STRING
299 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
300 || TYPE_CODE (type
) == TYPE_CODE_UNION
301 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
302 value_print (val
, stream
, options
);
304 /* User specified format, so don't look to the the type to
305 tell us what to do. */
306 print_scalar_formatted (value_contents (val
), type
,
307 options
, size
, stream
);
310 /* Return builtin floating point type of same length as TYPE.
311 If no such type is found, return TYPE itself. */
313 float_type_from_length (struct gdbarch
*gdbarch
, struct type
*type
)
315 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
316 unsigned int len
= TYPE_LENGTH (type
);
318 if (len
== TYPE_LENGTH (builtin
->builtin_float
))
319 type
= builtin
->builtin_float
;
320 else if (len
== TYPE_LENGTH (builtin
->builtin_double
))
321 type
= builtin
->builtin_double
;
322 else if (len
== TYPE_LENGTH (builtin
->builtin_long_double
))
323 type
= builtin
->builtin_long_double
;
328 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
329 according to OPTIONS and SIZE on STREAM.
330 Formats s and i are not supported at this level.
332 This is how the elements of an array or structure are printed
336 print_scalar_formatted (const void *valaddr
, struct type
*type
,
337 const struct value_print_options
*options
,
338 int size
, struct ui_file
*stream
)
340 LONGEST val_long
= 0;
341 unsigned int len
= TYPE_LENGTH (type
);
342 enum bfd_endian byte_order
= gdbarch_byte_order (current_gdbarch
);
344 /* If we get here with a string format, try again without it. Go
345 all the way back to the language printers, which may call us
347 if (options
->format
== 's')
349 struct value_print_options opts
= *options
;
352 val_print (type
, valaddr
, 0, 0, stream
, 0, &opts
,
357 if (len
> sizeof(LONGEST
) &&
358 (TYPE_CODE (type
) == TYPE_CODE_INT
359 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
361 switch (options
->format
)
364 print_octal_chars (stream
, valaddr
, len
, byte_order
);
368 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
371 print_binary_chars (stream
, valaddr
, len
, byte_order
);
374 print_hex_chars (stream
, valaddr
, len
, byte_order
);
377 print_char_chars (stream
, valaddr
, len
, byte_order
);
384 if (options
->format
!= 'f')
385 val_long
= unpack_long (type
, valaddr
);
387 /* If the value is a pointer, and pointers and addresses are not the
388 same, then at this point, the value's length (in target bytes) is
389 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
390 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
391 len
= gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
;
393 /* If we are printing it as unsigned, truncate it in case it is actually
394 a negative signed value (e.g. "print/u (short)-1" should print 65535
395 (if shorts are 16 bits) instead of 4294967295). */
396 if (options
->format
!= 'd')
398 if (len
< sizeof (LONGEST
))
399 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
402 switch (options
->format
)
407 /* No size specified, like in print. Print varying # of digits. */
408 print_longest (stream
, 'x', 1, val_long
);
417 print_longest (stream
, size
, 1, val_long
);
420 error (_("Undefined output size \"%c\"."), size
);
425 print_longest (stream
, 'd', 1, val_long
);
429 print_longest (stream
, 'u', 0, val_long
);
434 print_longest (stream
, 'o', 1, val_long
);
436 fprintf_filtered (stream
, "0");
441 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
442 print_address (addr
, stream
);
448 struct value_print_options opts
= *options
;
450 if (TYPE_UNSIGNED (type
))
451 value_print (value_from_longest (builtin_type_true_unsigned_char
,
455 value_print (value_from_longest (builtin_type_true_char
, val_long
),
461 type
= float_type_from_length (current_gdbarch
, type
);
462 print_floating (valaddr
, type
, stream
);
466 internal_error (__FILE__
, __LINE__
,
467 _("failed internal consistency check"));
470 /* Binary; 't' stands for "two". */
472 char bits
[8 * (sizeof val_long
) + 1];
473 char buf
[8 * (sizeof val_long
) + 32];
478 width
= 8 * (sizeof val_long
);
495 error (_("Undefined output size \"%c\"."), size
);
501 bits
[width
] = (val_long
& 1) ? '1' : '0';
506 while (*cp
&& *cp
== '0')
512 fputs_filtered (buf
, stream
);
517 error (_("Undefined output format \"%c\"."), options
->format
);
521 /* Specify default address for `x' command.
522 The `info lines' command uses this. */
525 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
527 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
531 /* Make address available to the user as $_. */
532 set_internalvar (lookup_internalvar ("_"),
533 value_from_pointer (ptr_type
, addr
));
536 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
537 after LEADIN. Print nothing if no symbolic name is found nearby.
538 Optionally also print source file and line number, if available.
539 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
540 or to interpret it as a possible C++ name and convert it back to source
541 form. However note that DO_DEMANGLE can be overridden by the specific
542 settings of the demangle and asm_demangle variables. */
545 print_address_symbolic (CORE_ADDR addr
, struct ui_file
*stream
,
546 int do_demangle
, char *leadin
)
549 char *filename
= NULL
;
554 /* Throw away both name and filename. */
555 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
556 make_cleanup (free_current_contents
, &filename
);
558 if (build_address_symbolic (addr
, do_demangle
, &name
, &offset
,
559 &filename
, &line
, &unmapped
))
561 do_cleanups (cleanup_chain
);
565 fputs_filtered (leadin
, stream
);
567 fputs_filtered ("<*", stream
);
569 fputs_filtered ("<", stream
);
570 fputs_filtered (name
, stream
);
572 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
574 /* Append source filename and line number if desired. Give specific
575 line # of this addr, if we have it; else line # of the nearest symbol. */
576 if (print_symbol_filename
&& filename
!= NULL
)
579 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
581 fprintf_filtered (stream
, " in %s", filename
);
584 fputs_filtered ("*>", stream
);
586 fputs_filtered (">", stream
);
588 do_cleanups (cleanup_chain
);
591 /* Given an address ADDR return all the elements needed to print the
592 address in a symbolic form. NAME can be mangled or not depending
593 on DO_DEMANGLE (and also on the asm_demangle global variable,
594 manipulated via ''set print asm-demangle''). Return 0 in case of
595 success, when all the info in the OUT paramters is valid. Return 1
598 build_address_symbolic (CORE_ADDR addr
, /* IN */
599 int do_demangle
, /* IN */
600 char **name
, /* OUT */
601 int *offset
, /* OUT */
602 char **filename
, /* OUT */
604 int *unmapped
) /* OUT */
606 struct minimal_symbol
*msymbol
;
607 struct symbol
*symbol
;
608 CORE_ADDR name_location
= 0;
609 struct obj_section
*section
= NULL
;
610 char *name_temp
= "";
612 /* Let's say it is mapped (not unmapped). */
615 /* Determine if the address is in an overlay, and whether it is
617 if (overlay_debugging
)
619 section
= find_pc_overlay (addr
);
620 if (pc_in_unmapped_range (addr
, section
))
623 addr
= overlay_mapped_address (addr
, section
);
627 /* First try to find the address in the symbol table, then
628 in the minsyms. Take the closest one. */
630 /* This is defective in the sense that it only finds text symbols. So
631 really this is kind of pointless--we should make sure that the
632 minimal symbols have everything we need (by changing that we could
633 save some memory, but for many debug format--ELF/DWARF or
634 anything/stabs--it would be inconvenient to eliminate those minimal
636 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
637 symbol
= find_pc_sect_function (addr
, section
);
641 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
642 if (do_demangle
|| asm_demangle
)
643 name_temp
= SYMBOL_PRINT_NAME (symbol
);
645 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
650 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
652 /* The msymbol is closer to the address than the symbol;
653 use the msymbol instead. */
655 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
656 if (do_demangle
|| asm_demangle
)
657 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
659 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
662 if (symbol
== NULL
&& msymbol
== NULL
)
665 /* If the nearest symbol is too far away, don't print anything symbolic. */
667 /* For when CORE_ADDR is larger than unsigned int, we do math in
668 CORE_ADDR. But when we detect unsigned wraparound in the
669 CORE_ADDR math, we ignore this test and print the offset,
670 because addr+max_symbolic_offset has wrapped through the end
671 of the address space back to the beginning, giving bogus comparison. */
672 if (addr
> name_location
+ max_symbolic_offset
673 && name_location
+ max_symbolic_offset
> name_location
)
676 *offset
= addr
- name_location
;
678 *name
= xstrdup (name_temp
);
680 if (print_symbol_filename
)
682 struct symtab_and_line sal
;
684 sal
= find_pc_sect_line (addr
, section
, 0);
688 *filename
= xstrdup (sal
.symtab
->filename
);
696 /* Print address ADDR symbolically on STREAM.
697 First print it as a number. Then perhaps print
698 <SYMBOL + OFFSET> after the number. */
701 print_address (CORE_ADDR addr
, struct ui_file
*stream
)
703 fputs_filtered (paddress (addr
), stream
);
704 print_address_symbolic (addr
, stream
, asm_demangle
, " ");
707 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
708 controls whether to print the symbolic name "raw" or demangled.
709 Global setting "addressprint" controls whether to print hex address
713 print_address_demangle (CORE_ADDR addr
, struct ui_file
*stream
,
716 struct value_print_options opts
;
717 get_user_print_options (&opts
);
720 fprintf_filtered (stream
, "0");
722 else if (opts
.addressprint
)
724 fputs_filtered (paddress (addr
), stream
);
725 print_address_symbolic (addr
, stream
, do_demangle
, " ");
729 print_address_symbolic (addr
, stream
, do_demangle
, "");
734 /* These are the types that $__ will get after an examine command of one
737 static struct type
*examine_i_type
;
739 static struct type
*examine_b_type
;
740 static struct type
*examine_h_type
;
741 static struct type
*examine_w_type
;
742 static struct type
*examine_g_type
;
744 /* Examine data at address ADDR in format FMT.
745 Fetch it from memory and print on gdb_stdout. */
748 do_examine (struct format_data fmt
, CORE_ADDR addr
)
753 struct type
*val_type
= NULL
;
756 struct value_print_options opts
;
763 /* String or instruction format implies fetch single bytes
764 regardless of the specified size. */
765 if (format
== 's' || format
== 'i')
769 val_type
= examine_i_type
;
770 else if (size
== 'b')
771 val_type
= examine_b_type
;
772 else if (size
== 'h')
773 val_type
= examine_h_type
;
774 else if (size
== 'w')
775 val_type
= examine_w_type
;
776 else if (size
== 'g')
777 val_type
= examine_g_type
;
784 if (format
== 's' || format
== 'i')
787 get_formatted_print_options (&opts
, format
);
789 /* Print as many objects as specified in COUNT, at most maxelts per line,
790 with the address of the next one at the start of each line. */
795 print_address (next_address
, gdb_stdout
);
796 printf_filtered (":");
801 printf_filtered ("\t");
802 /* Note that print_formatted sets next_address for the next
804 last_examine_address
= next_address
;
806 if (last_examine_value
)
807 value_free (last_examine_value
);
809 /* The value to be displayed is not fetched greedily.
810 Instead, to avoid the possibility of a fetched value not
811 being used, its retrieval is delayed until the print code
812 uses it. When examining an instruction stream, the
813 disassembler will perform its own memory fetch using just
814 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
815 the disassembler be modified so that LAST_EXAMINE_VALUE
816 is left with the byte sequence from the last complete
817 instruction fetched from memory? */
818 last_examine_value
= value_at_lazy (val_type
, next_address
);
820 if (last_examine_value
)
821 release_value (last_examine_value
);
823 print_formatted (last_examine_value
, size
, &opts
, gdb_stdout
);
825 /* Display any branch delay slots following the final insn. */
826 if (format
== 'i' && count
== 1)
827 count
+= branch_delay_insns
;
829 printf_filtered ("\n");
830 gdb_flush (gdb_stdout
);
835 validate_format (struct format_data fmt
, char *cmdname
)
838 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
840 error (_("Item count other than 1 is meaningless in \"%s\" command."),
842 if (fmt
.format
== 'i')
843 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
844 fmt
.format
, cmdname
);
847 /* Evaluate string EXP as an expression in the current language and
848 print the resulting value. EXP may contain a format specifier as the
849 first argument ("/x myvar" for example, to print myvar in hex). */
852 print_command_1 (char *exp
, int inspect
, int voidprint
)
854 struct expression
*expr
;
855 struct cleanup
*old_chain
= 0;
858 struct format_data fmt
;
861 if (exp
&& *exp
== '/')
864 fmt
= decode_format (&exp
, last_format
, 0);
865 validate_format (fmt
, "print");
866 last_format
= format
= fmt
.format
;
878 expr
= parse_expression (exp
);
879 old_chain
= make_cleanup (free_current_contents
, &expr
);
881 val
= evaluate_expression (expr
);
884 val
= access_value_history (0);
886 if (voidprint
|| (val
&& value_type (val
) &&
887 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
889 struct value_print_options opts
;
890 int histindex
= record_latest_value (val
);
893 annotate_value_history_begin (histindex
, value_type (val
));
895 annotate_value_begin (value_type (val
));
898 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
900 else if (histindex
>= 0)
901 printf_filtered ("$%d = ", histindex
);
904 annotate_value_history_value ();
906 get_formatted_print_options (&opts
, format
);
907 opts
.inspect_it
= inspect
;
909 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
910 printf_filtered ("\n");
913 annotate_value_history_end ();
915 annotate_value_end ();
918 printf_unfiltered ("\") )\030");
922 do_cleanups (old_chain
);
926 print_command (char *exp
, int from_tty
)
928 print_command_1 (exp
, 0, 1);
931 /* Same as print, except in epoch, it gets its own window. */
933 inspect_command (char *exp
, int from_tty
)
935 extern int epoch_interface
;
937 print_command_1 (exp
, epoch_interface
, 1);
940 /* Same as print, except it doesn't print void results. */
942 call_command (char *exp
, int from_tty
)
944 print_command_1 (exp
, 0, 0);
948 output_command (char *exp
, int from_tty
)
950 struct expression
*expr
;
951 struct cleanup
*old_chain
;
954 struct format_data fmt
;
955 struct value_print_options opts
;
959 if (exp
&& *exp
== '/')
962 fmt
= decode_format (&exp
, 0, 0);
963 validate_format (fmt
, "output");
967 expr
= parse_expression (exp
);
968 old_chain
= make_cleanup (free_current_contents
, &expr
);
970 val
= evaluate_expression (expr
);
972 annotate_value_begin (value_type (val
));
974 get_formatted_print_options (&opts
, format
);
975 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
977 annotate_value_end ();
980 gdb_flush (gdb_stdout
);
982 do_cleanups (old_chain
);
986 set_command (char *exp
, int from_tty
)
988 struct expression
*expr
= parse_expression (exp
);
989 struct cleanup
*old_chain
=
990 make_cleanup (free_current_contents
, &expr
);
991 evaluate_expression (expr
);
992 do_cleanups (old_chain
);
996 sym_info (char *arg
, int from_tty
)
998 struct minimal_symbol
*msymbol
;
999 struct objfile
*objfile
;
1000 struct obj_section
*osect
;
1001 CORE_ADDR addr
, sect_addr
;
1003 unsigned int offset
;
1006 error_no_arg (_("address"));
1008 addr
= parse_and_eval_address (arg
);
1009 ALL_OBJSECTIONS (objfile
, osect
)
1011 /* Only process each object file once, even if there's a separate
1013 if (objfile
->separate_debug_objfile_backlink
)
1016 sect_addr
= overlay_mapped_address (addr
, osect
);
1018 if (obj_section_addr (osect
) <= sect_addr
1019 && sect_addr
< obj_section_endaddr (osect
)
1020 && (msymbol
= lookup_minimal_symbol_by_pc_section (sect_addr
, osect
)))
1022 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1024 struct cleanup
*old_chain
;
1027 offset
= sect_addr
- SYMBOL_VALUE_ADDRESS (msymbol
);
1028 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1029 sec_name
= osect
->the_bfd_section
->name
;
1030 msym_name
= SYMBOL_PRINT_NAME (msymbol
);
1032 /* Don't print the offset if it is zero.
1033 We assume there's no need to handle i18n of "sym + offset". */
1035 xasprintf (&loc_string
, "%s + %u", msym_name
, offset
);
1037 xasprintf (&loc_string
, "%s", msym_name
);
1039 /* Use a cleanup to free loc_string in case the user quits
1040 a pagination request inside printf_filtered. */
1041 old_chain
= make_cleanup (xfree
, loc_string
);
1043 gdb_assert (osect
->objfile
&& osect
->objfile
->name
);
1044 obj_name
= osect
->objfile
->name
;
1046 if (MULTI_OBJFILE_P ())
1047 if (pc_in_unmapped_range (addr
, osect
))
1048 if (section_is_overlay (osect
))
1049 printf_filtered (_("%s in load address range of "
1050 "%s overlay section %s of %s\n"),
1051 loc_string
, mapped
, sec_name
, obj_name
);
1053 printf_filtered (_("%s in load address range of "
1054 "section %s of %s\n"),
1055 loc_string
, sec_name
, obj_name
);
1057 if (section_is_overlay (osect
))
1058 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1059 loc_string
, mapped
, sec_name
, obj_name
);
1061 printf_filtered (_("%s in section %s of %s\n"),
1062 loc_string
, sec_name
, obj_name
);
1064 if (pc_in_unmapped_range (addr
, osect
))
1065 if (section_is_overlay (osect
))
1066 printf_filtered (_("%s in load address range of %s overlay "
1068 loc_string
, mapped
, sec_name
);
1070 printf_filtered (_("%s in load address range of section %s\n"),
1071 loc_string
, sec_name
);
1073 if (section_is_overlay (osect
))
1074 printf_filtered (_("%s in %s overlay section %s\n"),
1075 loc_string
, mapped
, sec_name
);
1077 printf_filtered (_("%s in section %s\n"),
1078 loc_string
, sec_name
);
1080 do_cleanups (old_chain
);
1084 printf_filtered (_("No symbol matches %s.\n"), arg
);
1088 address_info (char *exp
, int from_tty
)
1091 struct minimal_symbol
*msymbol
;
1093 struct obj_section
*section
;
1094 CORE_ADDR load_addr
;
1095 int is_a_field_of_this
; /* C++: lookup_symbol sets this to nonzero
1096 if exp is a field of `this'. */
1099 error (_("Argument required."));
1101 sym
= lookup_symbol (exp
, get_selected_block (0), VAR_DOMAIN
,
1102 &is_a_field_of_this
);
1105 if (is_a_field_of_this
)
1107 printf_filtered ("Symbol \"");
1108 fprintf_symbol_filtered (gdb_stdout
, exp
,
1109 current_language
->la_language
, DMGL_ANSI
);
1110 printf_filtered ("\" is a field of the local class variable ");
1111 if (current_language
->la_language
== language_objc
)
1112 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1114 printf_filtered ("`this'\n");
1118 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1120 if (msymbol
!= NULL
)
1122 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1124 printf_filtered ("Symbol \"");
1125 fprintf_symbol_filtered (gdb_stdout
, exp
,
1126 current_language
->la_language
, DMGL_ANSI
);
1127 printf_filtered ("\" is at ");
1128 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1129 printf_filtered (" in a file compiled without debugging");
1130 section
= SYMBOL_OBJ_SECTION (msymbol
);
1131 if (section_is_overlay (section
))
1133 load_addr
= overlay_unmapped_address (load_addr
, section
);
1134 printf_filtered (",\n -- loaded at ");
1135 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1136 printf_filtered (" in overlay section %s",
1137 section
->the_bfd_section
->name
);
1139 printf_filtered (".\n");
1142 error (_("No symbol \"%s\" in current context."), exp
);
1146 printf_filtered ("Symbol \"");
1147 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1148 current_language
->la_language
, DMGL_ANSI
);
1149 printf_filtered ("\" is ");
1150 val
= SYMBOL_VALUE (sym
);
1151 section
= SYMBOL_OBJ_SECTION (sym
);
1153 switch (SYMBOL_CLASS (sym
))
1156 case LOC_CONST_BYTES
:
1157 printf_filtered ("constant");
1161 printf_filtered ("a label at address ");
1162 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1164 if (section_is_overlay (section
))
1166 load_addr
= overlay_unmapped_address (load_addr
, section
);
1167 printf_filtered (",\n -- loaded at ");
1168 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1169 printf_filtered (" in overlay section %s",
1170 section
->the_bfd_section
->name
);
1175 /* FIXME: cagney/2004-01-26: It should be possible to
1176 unconditionally call the SYMBOL_OPS method when available.
1177 Unfortunately DWARF 2 stores the frame-base (instead of the
1178 function) location in a function's symbol. Oops! For the
1179 moment enable this when/where applicable. */
1180 SYMBOL_OPS (sym
)->describe_location (sym
, gdb_stdout
);
1184 if (SYMBOL_IS_ARGUMENT (sym
))
1185 printf_filtered (_("an argument in register %s"),
1186 gdbarch_register_name (current_gdbarch
, val
));
1188 printf_filtered (_("a variable in register %s"),
1189 gdbarch_register_name (current_gdbarch
, val
));
1193 printf_filtered (_("static storage at address "));
1194 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1196 if (section_is_overlay (section
))
1198 load_addr
= overlay_unmapped_address (load_addr
, section
);
1199 printf_filtered (_(",\n -- loaded at "));
1200 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1201 printf_filtered (_(" in overlay section %s"),
1202 section
->the_bfd_section
->name
);
1206 case LOC_REGPARM_ADDR
:
1207 printf_filtered (_("address of an argument in register %s"),
1208 gdbarch_register_name (current_gdbarch
, val
));
1212 printf_filtered (_("an argument at offset %ld"), val
);
1216 printf_filtered (_("a local variable at frame offset %ld"), val
);
1220 printf_filtered (_("a reference argument at offset %ld"), val
);
1224 printf_filtered (_("a typedef"));
1228 printf_filtered (_("a function at address "));
1229 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1230 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1231 if (section_is_overlay (section
))
1233 load_addr
= overlay_unmapped_address (load_addr
, section
);
1234 printf_filtered (_(",\n -- loaded at "));
1235 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1236 printf_filtered (_(" in overlay section %s"),
1237 section
->the_bfd_section
->name
);
1241 case LOC_UNRESOLVED
:
1243 struct minimal_symbol
*msym
;
1245 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1247 printf_filtered ("unresolved");
1250 section
= SYMBOL_OBJ_SECTION (msym
);
1251 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1254 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1255 printf_filtered (_("a thread-local variable at offset %s "
1256 "in the thread-local storage for `%s'"),
1257 paddr_nz (load_addr
), section
->objfile
->name
);
1260 printf_filtered (_("static storage at address "));
1261 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1262 if (section_is_overlay (section
))
1264 load_addr
= overlay_unmapped_address (load_addr
, section
);
1265 printf_filtered (_(",\n -- loaded at "));
1266 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1267 printf_filtered (_(" in overlay section %s"),
1268 section
->the_bfd_section
->name
);
1275 case LOC_OPTIMIZED_OUT
:
1276 printf_filtered (_("optimized out"));
1280 printf_filtered (_("of unknown (botched) type"));
1283 printf_filtered (".\n");
1288 x_command (char *exp
, int from_tty
)
1290 struct expression
*expr
;
1291 struct format_data fmt
;
1292 struct cleanup
*old_chain
;
1295 fmt
.format
= last_format
;
1296 fmt
.size
= last_size
;
1299 if (exp
&& *exp
== '/')
1302 fmt
= decode_format (&exp
, last_format
, last_size
);
1305 /* If we have an expression, evaluate it and use it as the address. */
1307 if (exp
!= 0 && *exp
!= 0)
1309 expr
= parse_expression (exp
);
1310 /* Cause expression not to be there any more if this command is
1311 repeated with Newline. But don't clobber a user-defined
1312 command's definition. */
1315 old_chain
= make_cleanup (free_current_contents
, &expr
);
1316 val
= evaluate_expression (expr
);
1317 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1318 val
= value_ind (val
);
1319 /* In rvalue contexts, such as this, functions are coerced into
1320 pointers to functions. This makes "x/i main" work. */
1321 if (/* last_format == 'i' && */
1322 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1323 && VALUE_LVAL (val
) == lval_memory
)
1324 next_address
= VALUE_ADDRESS (val
);
1326 next_address
= value_as_address (val
);
1327 do_cleanups (old_chain
);
1330 do_examine (fmt
, next_address
);
1332 /* If the examine succeeds, we remember its size and format for next
1334 last_size
= fmt
.size
;
1335 last_format
= fmt
.format
;
1337 /* Set a couple of internal variables if appropriate. */
1338 if (last_examine_value
)
1340 /* Make last address examined available to the user as $_. Use
1341 the correct pointer type. */
1342 struct type
*pointer_type
1343 = lookup_pointer_type (value_type (last_examine_value
));
1344 set_internalvar (lookup_internalvar ("_"),
1345 value_from_pointer (pointer_type
,
1346 last_examine_address
));
1348 /* Make contents of last address examined available to the user
1349 as $__. If the last value has not been fetched from memory
1350 then don't fetch it now; instead mark it by voiding the $__
1352 if (value_lazy (last_examine_value
))
1353 set_internalvar (lookup_internalvar ("__"),
1354 allocate_value (builtin_type_void
));
1356 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1361 /* Add an expression to the auto-display chain.
1362 Specify the expression. */
1365 display_command (char *exp
, int from_tty
)
1367 struct format_data fmt
;
1368 struct expression
*expr
;
1369 struct display
*new;
1373 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1375 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1376 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1390 fmt
= decode_format (&exp
, 0, 0);
1391 if (fmt
.size
&& fmt
.format
== 0)
1393 if (fmt
.format
== 'i' || fmt
.format
== 's')
1403 innermost_block
= NULL
;
1404 expr
= parse_expression (exp
);
1406 new = (struct display
*) xmalloc (sizeof (struct display
));
1408 new->exp_string
= xstrdup (exp
);
1410 new->block
= innermost_block
;
1411 new->next
= display_chain
;
1412 new->number
= ++display_number
;
1415 display_chain
= new;
1417 if (from_tty
&& target_has_execution
)
1418 do_one_display (new);
1425 free_display (struct display
*d
)
1427 xfree (d
->exp_string
);
1432 /* Clear out the display_chain. Done when new symtabs are loaded,
1433 since this invalidates the types stored in many expressions. */
1436 clear_displays (void)
1440 while ((d
= display_chain
) != NULL
)
1442 display_chain
= d
->next
;
1447 /* Delete the auto-display number NUM. */
1450 delete_display (int num
)
1452 struct display
*d1
, *d
;
1455 error (_("No display number %d."), num
);
1457 if (display_chain
->number
== num
)
1460 display_chain
= d1
->next
;
1464 for (d
= display_chain
;; d
= d
->next
)
1467 error (_("No display number %d."), num
);
1468 if (d
->next
->number
== num
)
1478 /* Delete some values from the auto-display chain.
1479 Specify the element numbers. */
1482 undisplay_command (char *args
, int from_tty
)
1490 if (query (_("Delete all auto-display expressions? ")))
1499 while (*p1
>= '0' && *p1
<= '9')
1501 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1502 error (_("Arguments must be display numbers."));
1506 delete_display (num
);
1509 while (*p
== ' ' || *p
== '\t')
1515 /* Display a single auto-display.
1516 Do nothing if the display cannot be printed in the current context,
1517 or if the display is disabled. */
1520 do_one_display (struct display
*d
)
1522 int within_current_scope
;
1524 if (d
->enabled_p
== 0)
1529 volatile struct gdb_exception ex
;
1530 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1532 innermost_block
= NULL
;
1533 d
->exp
= parse_expression (d
->exp_string
);
1534 d
->block
= innermost_block
;
1538 /* Can't re-parse the expression. Disable this display item. */
1540 warning (_("Unable to display \"%s\": %s"),
1541 d
->exp_string
, ex
.message
);
1547 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1549 within_current_scope
= 1;
1550 if (!within_current_scope
)
1553 current_display_number
= d
->number
;
1555 annotate_display_begin ();
1556 printf_filtered ("%d", d
->number
);
1557 annotate_display_number_end ();
1558 printf_filtered (": ");
1564 annotate_display_format ();
1566 printf_filtered ("x/");
1567 if (d
->format
.count
!= 1)
1568 printf_filtered ("%d", d
->format
.count
);
1569 printf_filtered ("%c", d
->format
.format
);
1570 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1571 printf_filtered ("%c", d
->format
.size
);
1572 printf_filtered (" ");
1574 annotate_display_expression ();
1576 puts_filtered (d
->exp_string
);
1577 annotate_display_expression_end ();
1579 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1580 printf_filtered ("\n");
1582 printf_filtered (" ");
1584 val
= evaluate_expression (d
->exp
);
1585 addr
= value_as_address (val
);
1586 if (d
->format
.format
== 'i')
1587 addr
= gdbarch_addr_bits_remove (current_gdbarch
, addr
);
1589 annotate_display_value ();
1591 do_examine (d
->format
, addr
);
1595 struct value_print_options opts
;
1597 annotate_display_format ();
1599 if (d
->format
.format
)
1600 printf_filtered ("/%c ", d
->format
.format
);
1602 annotate_display_expression ();
1604 puts_filtered (d
->exp_string
);
1605 annotate_display_expression_end ();
1607 printf_filtered (" = ");
1609 annotate_display_expression ();
1611 get_formatted_print_options (&opts
, d
->format
.format
);
1612 print_formatted (evaluate_expression (d
->exp
),
1613 d
->format
.size
, &opts
, gdb_stdout
);
1614 printf_filtered ("\n");
1617 annotate_display_end ();
1619 gdb_flush (gdb_stdout
);
1620 current_display_number
= -1;
1623 /* Display all of the values on the auto-display chain which can be
1624 evaluated in the current scope. */
1631 for (d
= display_chain
; d
; d
= d
->next
)
1635 /* Delete the auto-display which we were in the process of displaying.
1636 This is done when there is an error or a signal. */
1639 disable_display (int num
)
1643 for (d
= display_chain
; d
; d
= d
->next
)
1644 if (d
->number
== num
)
1649 printf_unfiltered (_("No display number %d.\n"), num
);
1653 disable_current_display (void)
1655 if (current_display_number
>= 0)
1657 disable_display (current_display_number
);
1658 fprintf_unfiltered (gdb_stderr
, _("\
1659 Disabling display %d to avoid infinite recursion.\n"),
1660 current_display_number
);
1662 current_display_number
= -1;
1666 display_info (char *ignore
, int from_tty
)
1671 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1673 printf_filtered (_("Auto-display expressions now in effect:\n\
1674 Num Enb Expression\n"));
1676 for (d
= display_chain
; d
; d
= d
->next
)
1678 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1680 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1682 else if (d
->format
.format
)
1683 printf_filtered ("/%c ", d
->format
.format
);
1684 puts_filtered (d
->exp_string
);
1685 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1686 printf_filtered (_(" (cannot be evaluated in the current context)"));
1687 printf_filtered ("\n");
1688 gdb_flush (gdb_stdout
);
1693 enable_display (char *args
, int from_tty
)
1702 for (d
= display_chain
; d
; d
= d
->next
)
1709 while (*p1
>= '0' && *p1
<= '9')
1711 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1712 error (_("Arguments must be display numbers."));
1716 for (d
= display_chain
; d
; d
= d
->next
)
1717 if (d
->number
== num
)
1722 printf_unfiltered (_("No display number %d.\n"), num
);
1725 while (*p
== ' ' || *p
== '\t')
1731 disable_display_command (char *args
, int from_tty
)
1739 for (d
= display_chain
; d
; d
= d
->next
)
1746 while (*p1
>= '0' && *p1
<= '9')
1748 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1749 error (_("Arguments must be display numbers."));
1751 disable_display (atoi (p
));
1754 while (*p
== ' ' || *p
== '\t')
1759 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1760 is unloaded), otherwise return 0. */
1763 display_uses_solib_p (const struct display
*d
,
1764 const struct so_list
*solib
)
1767 struct expression
*const exp
= d
->exp
;
1769 if (d
->block
!= NULL
1770 && solib_name_from_address (d
->block
->startaddr
) == solib
->so_name
)
1773 for (i
= 0; i
< exp
->nelts
; )
1775 int args
, oplen
= 0;
1776 const union exp_element
*const elts
= exp
->elts
;
1778 if (elts
[i
].opcode
== OP_VAR_VALUE
)
1780 const struct block
*const block
= elts
[i
+ 1].block
;
1781 const struct symbol
*const symbol
= elts
[i
+ 2].symbol
;
1782 const struct obj_section
*const section
=
1783 SYMBOL_OBJ_SECTION (symbol
);
1786 && solib_name_from_address (block
->startaddr
) == solib
->so_name
)
1789 if (section
&& section
->objfile
== solib
->objfile
)
1792 exp
->language_defn
->la_exp_desc
->operator_length (exp
, i
+ 1,
1794 gdb_assert (oplen
> 0);
1800 /* display_chain items point to blocks and expressions. Some expressions in
1801 turn may point to symbols.
1802 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1803 obstack_free'd when a shared library is unloaded.
1804 Clear pointers that are about to become dangling.
1805 Both .exp and .block fields will be restored next time we need to display
1806 an item by re-parsing .exp_string field in the new execution context. */
1809 clear_dangling_display_expressions (struct so_list
*solib
)
1812 struct objfile
*objfile
= NULL
;
1814 for (d
= display_chain
; d
; d
= d
->next
)
1816 if (d
->exp
&& display_uses_solib_p (d
, solib
))
1826 /* Print the value in stack frame FRAME of a variable specified by a
1827 struct symbol. NAME is the name to print; if NULL then VAR's print
1828 name will be used. STREAM is the ui_file on which to print the
1829 value. INDENT specifies the number of indent levels to print
1830 before printing the variable name. */
1833 print_variable_and_value (const char *name
, struct symbol
*var
,
1834 struct frame_info
*frame
,
1835 struct ui_file
*stream
, int indent
)
1838 struct value_print_options opts
;
1841 name
= SYMBOL_PRINT_NAME (var
);
1843 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1845 val
= read_var_value (var
, frame
);
1846 get_user_print_options (&opts
);
1847 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1848 fprintf_filtered (stream
, "\n");
1852 printf_command (char *arg
, int from_tty
)
1856 char *string
= NULL
;
1857 struct value
**val_args
;
1859 char *current_substring
;
1861 int allocated_args
= 20;
1862 struct cleanup
*old_cleanups
;
1864 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1865 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1868 error_no_arg (_("format-control string and values to print"));
1870 /* Skip white space before format string */
1871 while (*s
== ' ' || *s
== '\t')
1874 /* A format string should follow, enveloped in double quotes. */
1876 error (_("Bad format string, missing '\"'."));
1878 /* Parse the format-control string and copy it into the string STRING,
1879 processing some kinds of escape sequence. */
1881 f
= string
= (char *) alloca (strlen (s
) + 1);
1889 error (_("Bad format string, non-terminated '\"'."));
1922 /* ??? TODO: handle other escape sequences */
1923 error (_("Unrecognized escape character \\%c in format string."),
1933 /* Skip over " and following space and comma. */
1936 while (*s
== ' ' || *s
== '\t')
1939 if (*s
!= ',' && *s
!= 0)
1940 error (_("Invalid argument syntax"));
1944 while (*s
== ' ' || *s
== '\t')
1947 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1948 substrings
= alloca (strlen (string
) * 2);
1949 current_substring
= substrings
;
1952 /* Now scan the string for %-specs and see what kinds of args they want.
1953 argclass[I] classifies the %-specs so we can give printf_filtered
1954 something of the right size. */
1958 int_arg
, long_arg
, long_long_arg
, ptr_arg
, string_arg
,
1959 double_arg
, long_double_arg
, decfloat_arg
1961 enum argclass
*argclass
;
1962 enum argclass this_argclass
;
1967 argclass
= (enum argclass
*) alloca (strlen (s
) * sizeof *argclass
);
1974 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
1975 int seen_space
= 0, seen_plus
= 0;
1976 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
1977 int seen_big_d
= 0, seen_double_big_d
= 0;
1980 /* Check the validity of the format specifier, and work
1981 out what argument it expects. We only accept C89
1982 format strings, with the exception of long long (which
1983 we autoconf for). */
1985 /* Skip over "%%". */
1992 /* The first part of a format specifier is a set of flag
1994 while (strchr ("0-+ #", *f
))
2007 /* The next part of a format specifier is a width. */
2008 while (strchr ("0123456789", *f
))
2011 /* The next part of a format specifier is a precision. */
2016 while (strchr ("0123456789", *f
))
2020 /* The next part of a format specifier is a length modifier. */
2041 /* Decimal32 modifier. */
2047 /* Decimal64 and Decimal128 modifiers. */
2052 /* Check for a Decimal128. */
2056 seen_double_big_d
= 1;
2072 if (seen_space
|| seen_plus
)
2079 this_argclass
= int_arg
;
2080 else if (lcount
== 1)
2081 this_argclass
= long_arg
;
2083 this_argclass
= long_long_arg
;
2090 this_argclass
= int_arg
;
2091 if (lcount
|| seen_h
|| seen_big_l
)
2093 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2098 this_argclass
= ptr_arg
;
2099 if (lcount
|| seen_h
|| seen_big_l
)
2101 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2106 this_argclass
= string_arg
;
2107 if (lcount
|| seen_h
|| seen_big_l
)
2109 if (seen_zero
|| seen_space
|| seen_plus
)
2118 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
2119 this_argclass
= decfloat_arg
;
2120 else if (seen_big_l
)
2121 this_argclass
= long_double_arg
;
2123 this_argclass
= double_arg
;
2125 if (lcount
|| seen_h
)
2130 error (_("`*' not supported for precision or width in printf"));
2133 error (_("Format specifier `n' not supported in printf"));
2136 error (_("Incomplete format specifier at end of format string"));
2139 error (_("Unrecognized format specifier '%c' in printf"), *f
);
2143 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2148 if (lcount
> 1 && USE_PRINTF_I64
)
2150 /* Windows' printf does support long long, but not the usual way.
2151 Convert %lld to %I64d. */
2152 int length_before_ll
= f
- last_arg
- 1 - lcount
;
2153 strncpy (current_substring
, last_arg
, length_before_ll
);
2154 strcpy (current_substring
+ length_before_ll
, "I64");
2155 current_substring
[length_before_ll
+ 3] =
2156 last_arg
[length_before_ll
+ lcount
];
2157 current_substring
+= length_before_ll
+ 4;
2161 strncpy (current_substring
, last_arg
, f
- last_arg
);
2162 current_substring
+= f
- last_arg
;
2164 *current_substring
++ = '\0';
2166 argclass
[nargs_wanted
++] = this_argclass
;
2169 /* Now, parse all arguments and evaluate them.
2170 Store the VALUEs in VAL_ARGS. */
2175 if (nargs
== allocated_args
)
2176 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2177 (allocated_args
*= 2)
2178 * sizeof (struct value
*));
2180 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2188 if (nargs
!= nargs_wanted
)
2189 error (_("Wrong number of arguments for specified format-string"));
2191 /* Now actually print them. */
2192 current_substring
= substrings
;
2193 for (i
= 0; i
< nargs
; i
++)
2195 switch (argclass
[i
])
2202 tem
= value_as_address (val_args
[i
]);
2204 /* This is a %s argument. Find the length of the string. */
2209 read_memory (tem
+ j
, &c
, 1);
2214 /* Copy the string contents into a string inside GDB. */
2215 str
= (gdb_byte
*) alloca (j
+ 1);
2217 read_memory (tem
, str
, j
);
2220 printf_filtered (current_substring
, (char *) str
);
2225 struct type
*type
= value_type (val_args
[i
]);
2229 /* If format string wants a float, unchecked-convert the value
2230 to floating point of the same size. */
2231 type
= float_type_from_length (current_gdbarch
, type
);
2232 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2234 error (_("Invalid floating value found in program."));
2236 printf_filtered (current_substring
, (double) val
);
2239 case long_double_arg
:
2240 #ifdef HAVE_LONG_DOUBLE
2242 struct type
*type
= value_type (val_args
[i
]);
2246 /* If format string wants a float, unchecked-convert the value
2247 to floating point of the same size. */
2248 type
= float_type_from_length (current_gdbarch
, type
);
2249 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2251 error (_("Invalid floating value found in program."));
2253 printf_filtered (current_substring
, (long double) val
);
2257 error (_("long double not supported in printf"));
2260 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2262 long long val
= value_as_long (val_args
[i
]);
2263 printf_filtered (current_substring
, val
);
2267 error (_("long long not supported in printf"));
2271 int val
= value_as_long (val_args
[i
]);
2272 printf_filtered (current_substring
, val
);
2277 long val
= value_as_long (val_args
[i
]);
2278 printf_filtered (current_substring
, val
);
2282 /* Handles decimal floating values. */
2285 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2286 #if defined (PRINTF_HAS_DECFLOAT)
2287 /* If we have native support for Decimal floating
2288 printing, handle it here. */
2289 printf_filtered (current_substring
, param_ptr
);
2292 /* As a workaround until vasprintf has native support for DFP
2293 we convert the DFP values to string and print them using
2294 the %s format specifier. */
2297 int nnull_chars
= 0;
2299 /* Parameter data. */
2300 struct type
*param_type
= value_type (val_args
[i
]);
2301 unsigned int param_len
= TYPE_LENGTH (param_type
);
2303 /* DFP output data. */
2304 struct value
*dfp_value
= NULL
;
2308 struct type
*dfp_type
= NULL
;
2309 char decstr
[MAX_DECIMAL_STRING
];
2311 /* Points to the end of the string so that we can go back
2312 and check for DFP length modifiers. */
2313 eos
= current_substring
+ strlen (current_substring
);
2315 /* Look for the float/double format specifier. */
2316 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2317 && *eos
!= 'g' && *eos
!= 'G')
2322 /* Search for the '%' char and extract the size and type of
2323 the output decimal value based on its modifiers
2324 (%Hf, %Df, %DDf). */
2325 while (*--sos
!= '%')
2330 dfp_type
= builtin_type (current_gdbarch
)->builtin_decfloat
;
2332 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2335 dfp_type
= builtin_type (current_gdbarch
)->builtin_declong
;
2341 dfp_type
= builtin_type (current_gdbarch
)->builtin_decdouble
;
2345 /* Replace %Hf, %Df and %DDf with %s's. */
2348 /* Go through the whole format string and pull the correct
2349 number of chars back to compensate for the change in the
2350 format specifier. */
2351 while (nnull_chars
< nargs
- i
)
2359 /* Conversion between different DFP types. */
2360 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2361 decimal_convert (param_ptr
, param_len
, dec
, dfp_len
);
2363 /* If this is a non-trivial conversion, just output 0.
2364 A correct converted value can be displayed by explicitly
2365 casting to a DFP type. */
2366 decimal_from_string (dec
, dfp_len
, "0");
2368 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2370 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2372 decimal_to_string (dfp_ptr
, dfp_len
, decstr
);
2374 /* Print the DFP value. */
2375 printf_filtered (current_substring
, decstr
);
2383 /* We avoid the host's %p because pointers are too
2384 likely to be the wrong size. The only interesting
2385 modifier for %p is a width; extract that, and then
2386 handle %p as glibc would: %#x or a literal "(nil)". */
2388 char *p
, *fmt
, *fmt_p
;
2389 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2390 long long val
= value_as_long (val_args
[i
]);
2392 long val
= value_as_long (val_args
[i
]);
2395 fmt
= alloca (strlen (current_substring
) + 5);
2397 /* Copy up to the leading %. */
2398 p
= current_substring
;
2402 int is_percent
= (*p
== '%');
2416 /* Copy any width. */
2417 while (*p
>= '0' && *p
< '9')
2420 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2423 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2429 printf_filtered (fmt
, val
);
2435 printf_filtered (fmt
, "(nil)");
2441 internal_error (__FILE__
, __LINE__
,
2442 _("failed internal consistency check"));
2444 /* Skip to the next substring. */
2445 current_substring
+= strlen (current_substring
) + 1;
2447 /* Print the portion of the format string after the last argument. */
2448 puts_filtered (last_arg
);
2450 do_cleanups (old_cleanups
);
2454 _initialize_printcmd (void)
2456 struct cmd_list_element
*c
;
2458 current_display_number
= -1;
2460 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2462 add_info ("address", address_info
,
2463 _("Describe where symbol SYM is stored."));
2465 add_info ("symbol", sym_info
, _("\
2466 Describe what symbol is at location ADDR.\n\
2467 Only for symbols with fixed locations (global or static scope)."));
2469 add_com ("x", class_vars
, x_command
, _("\
2470 Examine memory: x/FMT ADDRESS.\n\
2471 ADDRESS is an expression for the memory address to examine.\n\
2472 FMT is a repeat count followed by a format letter and a size letter.\n\
2473 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2474 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2475 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2476 The specified number of objects of the specified size are printed\n\
2477 according to the format.\n\n\
2478 Defaults for format and size letters are those previously used.\n\
2479 Default count is 1. Default address is following last thing printed\n\
2480 with this command or \"print\"."));
2483 add_com ("whereis", class_vars
, whereis_command
,
2484 _("Print line number and file of definition of variable."));
2487 add_info ("display", display_info
, _("\
2488 Expressions to display when program stops, with code numbers."));
2490 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2491 Cancel some expressions to be displayed when program stops.\n\
2492 Arguments are the code numbers of the expressions to stop displaying.\n\
2493 No argument means cancel all automatic-display expressions.\n\
2494 \"delete display\" has the same effect as this command.\n\
2495 Do \"info display\" to see current list of code numbers."),
2498 add_com ("display", class_vars
, display_command
, _("\
2499 Print value of expression EXP each time the program stops.\n\
2500 /FMT may be used before EXP as in the \"print\" command.\n\
2501 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2502 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2503 and examining is done as in the \"x\" command.\n\n\
2504 With no argument, display all currently requested auto-display expressions.\n\
2505 Use \"undisplay\" to cancel display requests previously made."));
2507 add_cmd ("display", class_vars
, enable_display
, _("\
2508 Enable some expressions to be displayed when program stops.\n\
2509 Arguments are the code numbers of the expressions to resume displaying.\n\
2510 No argument means enable all automatic-display expressions.\n\
2511 Do \"info display\" to see current list of code numbers."), &enablelist
);
2513 add_cmd ("display", class_vars
, disable_display_command
, _("\
2514 Disable some expressions to be displayed when program stops.\n\
2515 Arguments are the code numbers of the expressions to stop displaying.\n\
2516 No argument means disable all automatic-display expressions.\n\
2517 Do \"info display\" to see current list of code numbers."), &disablelist
);
2519 add_cmd ("display", class_vars
, undisplay_command
, _("\
2520 Cancel some expressions to be displayed when program stops.\n\
2521 Arguments are the code numbers of the expressions to stop displaying.\n\
2522 No argument means cancel all automatic-display expressions.\n\
2523 Do \"info display\" to see current list of code numbers."), &deletelist
);
2525 add_com ("printf", class_vars
, printf_command
, _("\
2526 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2527 This is useful for formatted output in user-defined commands."));
2529 add_com ("output", class_vars
, output_command
, _("\
2530 Like \"print\" but don't put in value history and don't print newline.\n\
2531 This is useful in user-defined commands."));
2533 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2534 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2535 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2536 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2537 with $), a register (a few standard names starting with $), or an actual\n\
2538 variable in the program being debugged. EXP is any valid expression.\n\
2539 Use \"set variable\" for variables with names identical to set subcommands.\n\
2541 With a subcommand, this command modifies parts of the gdb environment.\n\
2542 You can see these environment settings with the \"show\" command."),
2543 &setlist
, "set ", 1, &cmdlist
);
2545 add_com ("assign", class_vars
, set_command
, _("\
2546 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2547 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2548 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2549 with $), a register (a few standard names starting with $), or an actual\n\
2550 variable in the program being debugged. EXP is any valid expression.\n\
2551 Use \"set variable\" for variables with names identical to set subcommands.\n\
2552 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2553 You can see these environment settings with the \"show\" command."));
2555 /* "call" is the same as "set", but handy for dbx users to call fns. */
2556 c
= add_com ("call", class_vars
, call_command
, _("\
2557 Call a function in the program.\n\
2558 The argument is the function name and arguments, in the notation of the\n\
2559 current working language. The result is printed and saved in the value\n\
2560 history, if it is not void."));
2561 set_cmd_completer (c
, expression_completer
);
2563 add_cmd ("variable", class_vars
, set_command
, _("\
2564 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2565 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2566 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2567 with $), a register (a few standard names starting with $), or an actual\n\
2568 variable in the program being debugged. EXP is any valid expression.\n\
2569 This may usually be abbreviated to simply \"set\"."),
2572 c
= add_com ("print", class_vars
, print_command
, _("\
2573 Print value of expression EXP.\n\
2574 Variables accessible are those of the lexical environment of the selected\n\
2575 stack frame, plus all those whose scope is global or an entire file.\n\
2577 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2578 $$NUM refers to NUM'th value back from the last one.\n\
2579 Names starting with $ refer to registers (with the values they would have\n\
2580 if the program were to return to the stack frame now selected, restoring\n\
2581 all registers saved by frames farther in) or else to debugger\n\
2582 \"convenience\" variables (any such name not a known register).\n\
2583 Use assignment expressions to give values to convenience variables.\n\
2585 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2586 @ is a binary operator for treating consecutive data objects\n\
2587 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2588 element is FOO, whose second element is stored in the space following\n\
2589 where FOO is stored, etc. FOO must be an expression whose value\n\
2590 resides in memory.\n\
2592 EXP may be preceded with /FMT, where FMT is a format letter\n\
2593 but no count or size letter (see \"x\" command)."));
2594 set_cmd_completer (c
, expression_completer
);
2595 add_com_alias ("p", "print", class_vars
, 1);
2597 c
= add_com ("inspect", class_vars
, inspect_command
, _("\
2598 Same as \"print\" command, except that if you are running in the epoch\n\
2599 environment, the value is printed in its own window."));
2600 set_cmd_completer (c
, expression_completer
);
2602 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2603 &max_symbolic_offset
, _("\
2604 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2605 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2607 show_max_symbolic_offset
,
2608 &setprintlist
, &showprintlist
);
2609 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2610 &print_symbol_filename
, _("\
2611 Set printing of source filename and line number with <symbol>."), _("\
2612 Show printing of source filename and line number with <symbol>."), NULL
,
2614 show_print_symbol_filename
,
2615 &setprintlist
, &showprintlist
);
2617 /* For examine/instruction a single byte quantity is specified as
2618 the data. This avoids problems with value_at_lazy() requiring a
2619 valid data type (and rejecting VOID). */
2620 examine_i_type
= init_type (TYPE_CODE_INT
, 1, 0, "examine_i_type", NULL
);
2622 examine_b_type
= init_type (TYPE_CODE_INT
, 1, 0, "examine_b_type", NULL
);
2623 examine_h_type
= init_type (TYPE_CODE_INT
, 2, 0, "examine_h_type", NULL
);
2624 examine_w_type
= init_type (TYPE_CODE_INT
, 4, 0, "examine_w_type", NULL
);
2625 examine_g_type
= init_type (TYPE_CODE_INT
, 8, 0, "examine_g_type", NULL
);