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"
54 #include "tui/tui.h" /* For tui_active et.al. */
57 #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
58 # define USE_PRINTF_I64 1
59 # define PRINTF_HAS_LONG_LONG
61 # define USE_PRINTF_I64 0
64 extern int asm_demangle
; /* Whether to demangle syms in asm printouts */
72 /* True if the value should be printed raw -- that is, bypassing
73 python-based formatters. */
77 /* Last specified output format. */
79 static char last_format
= 0;
81 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
83 static char last_size
= 'w';
85 /* Default address to examine next. */
87 static CORE_ADDR next_address
;
89 /* Number of delay instructions following current disassembled insn. */
91 static int branch_delay_insns
;
93 /* Last address examined. */
95 static CORE_ADDR last_examine_address
;
97 /* Contents of last address examined.
98 This is not valid past the end of the `x' command! */
100 static struct value
*last_examine_value
;
102 /* Largest offset between a symbolic value and an address, that will be
103 printed as `0x1234 <symbol+offset>'. */
105 static unsigned int max_symbolic_offset
= UINT_MAX
;
107 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
108 struct cmd_list_element
*c
, const char *value
)
110 fprintf_filtered (file
, _("\
111 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
115 /* Append the source filename and linenumber of the symbol when
116 printing a symbolic value as `<symbol at filename:linenum>' if set. */
117 static int print_symbol_filename
= 0;
119 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
120 struct cmd_list_element
*c
, const char *value
)
122 fprintf_filtered (file
, _("\
123 Printing of source filename and line number with <symbol> is %s.\n"),
127 /* Number of auto-display expression currently being displayed.
128 So that we can disable it if we get an error or a signal within it.
129 -1 when not doing one. */
131 int current_display_number
;
135 /* Chain link to next auto-display item. */
136 struct display
*next
;
137 /* The expression as the user typed it. */
139 /* Expression to be evaluated and displayed. */
140 struct expression
*exp
;
141 /* Item number of this auto-display item. */
143 /* Display format specified. */
144 struct format_data format
;
145 /* Innermost block required by this expression when evaluated */
147 /* Status of this display (enabled or disabled) */
151 /* Chain of expressions whose values should be displayed
152 automatically each time the program stops. */
154 static struct display
*display_chain
;
156 static int display_number
;
158 /* Prototypes for exported functions. */
160 void output_command (char *, int);
162 void _initialize_printcmd (void);
164 /* Prototypes for local functions. */
166 static void do_one_display (struct display
*);
169 /* Decode a format specification. *STRING_PTR should point to it.
170 OFORMAT and OSIZE are used as defaults for the format and size
171 if none are given in the format specification.
172 If OSIZE is zero, then the size field of the returned value
173 should be set only if a size is explicitly specified by the
175 The structure returned describes all the data
176 found in the specification. In addition, *STRING_PTR is advanced
177 past the specification and past all whitespace following it. */
179 static struct format_data
180 decode_format (char **string_ptr
, int oformat
, int osize
)
182 struct format_data val
;
183 char *p
= *string_ptr
;
190 if (*p
>= '0' && *p
<= '9')
191 val
.count
= atoi (p
);
192 while (*p
>= '0' && *p
<= '9')
195 /* Now process size or format letters that follow. */
199 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
206 else if (*p
>= 'a' && *p
<= 'z')
212 while (*p
== ' ' || *p
== '\t')
216 /* Set defaults for format and size if not specified. */
217 if (val
.format
== '?')
221 /* Neither has been specified. */
222 val
.format
= oformat
;
226 /* If a size is specified, any format makes a reasonable
227 default except 'i'. */
228 val
.format
= oformat
== 'i' ? 'x' : oformat
;
230 else if (val
.size
== '?')
235 /* Pick the appropriate size for an address. */
236 if (gdbarch_ptr_bit (current_gdbarch
) == 64)
237 val
.size
= osize
? 'g' : osize
;
238 else if (gdbarch_ptr_bit (current_gdbarch
) == 32)
239 val
.size
= osize
? 'w' : osize
;
240 else if (gdbarch_ptr_bit (current_gdbarch
) == 16)
241 val
.size
= osize
? 'h' : osize
;
243 /* Bad value for gdbarch_ptr_bit. */
244 internal_error (__FILE__
, __LINE__
,
245 _("failed internal consistency check"));
248 /* Floating point has to be word or giantword. */
249 if (osize
== 'w' || osize
== 'g')
252 /* Default it to giantword if the last used size is not
254 val
.size
= osize
? 'g' : osize
;
257 /* Characters default to one byte. */
258 val
.size
= osize
? 'b' : osize
;
261 /* The default is the size most recently specified. */
268 /* Print value VAL on stream according to OPTIONS.
269 Do not end with a newline.
270 SIZE is the letter for the size of datum being printed.
271 This is used to pad hex numbers so they line up. SIZE is 0
272 for print / output and set for examine. */
275 print_formatted (struct value
*val
, int size
,
276 const struct value_print_options
*options
,
277 struct ui_file
*stream
)
279 struct type
*type
= check_typedef (value_type (val
));
280 int len
= TYPE_LENGTH (type
);
282 if (VALUE_LVAL (val
) == lval_memory
)
283 next_address
= value_address (val
) + len
;
287 switch (options
->format
)
291 struct type
*elttype
= value_type (val
);
292 next_address
= (value_address (val
)
293 + val_print_string (elttype
,
294 value_address (val
), -1,
300 /* We often wrap here if there are long symbolic names. */
302 next_address
= (value_address (val
)
303 + gdb_print_insn (value_address (val
), stream
,
304 &branch_delay_insns
));
309 if (options
->format
== 0 || options
->format
== 's'
310 || TYPE_CODE (type
) == TYPE_CODE_REF
311 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
312 || TYPE_CODE (type
) == TYPE_CODE_STRING
313 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
314 || TYPE_CODE (type
) == TYPE_CODE_UNION
315 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
316 value_print (val
, stream
, options
);
318 /* User specified format, so don't look to the the type to
319 tell us what to do. */
320 print_scalar_formatted (value_contents (val
), type
,
321 options
, size
, stream
);
324 /* Return builtin floating point type of same length as TYPE.
325 If no such type is found, return TYPE itself. */
327 float_type_from_length (struct gdbarch
*gdbarch
, struct type
*type
)
329 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
330 unsigned int len
= TYPE_LENGTH (type
);
332 if (len
== TYPE_LENGTH (builtin
->builtin_float
))
333 type
= builtin
->builtin_float
;
334 else if (len
== TYPE_LENGTH (builtin
->builtin_double
))
335 type
= builtin
->builtin_double
;
336 else if (len
== TYPE_LENGTH (builtin
->builtin_long_double
))
337 type
= builtin
->builtin_long_double
;
342 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
343 according to OPTIONS and SIZE on STREAM.
344 Formats s and i are not supported at this level.
346 This is how the elements of an array or structure are printed
350 print_scalar_formatted (const void *valaddr
, struct type
*type
,
351 const struct value_print_options
*options
,
352 int size
, struct ui_file
*stream
)
354 LONGEST val_long
= 0;
355 unsigned int len
= TYPE_LENGTH (type
);
356 enum bfd_endian byte_order
= gdbarch_byte_order (current_gdbarch
);
358 /* If we get here with a string format, try again without it. Go
359 all the way back to the language printers, which may call us
361 if (options
->format
== 's')
363 struct value_print_options opts
= *options
;
366 val_print (type
, valaddr
, 0, 0, stream
, 0, &opts
,
371 if (len
> sizeof(LONGEST
) &&
372 (TYPE_CODE (type
) == TYPE_CODE_INT
373 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
375 switch (options
->format
)
378 print_octal_chars (stream
, valaddr
, len
, byte_order
);
382 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
385 print_binary_chars (stream
, valaddr
, len
, byte_order
);
388 print_hex_chars (stream
, valaddr
, len
, byte_order
);
391 print_char_chars (stream
, type
, valaddr
, len
, byte_order
);
398 if (options
->format
!= 'f')
399 val_long
= unpack_long (type
, valaddr
);
401 /* If the value is a pointer, and pointers and addresses are not the
402 same, then at this point, the value's length (in target bytes) is
403 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
404 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
405 len
= gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
;
407 /* If we are printing it as unsigned, truncate it in case it is actually
408 a negative signed value (e.g. "print/u (short)-1" should print 65535
409 (if shorts are 16 bits) instead of 4294967295). */
410 if (options
->format
!= 'd')
412 if (len
< sizeof (LONGEST
))
413 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
416 switch (options
->format
)
421 /* No size specified, like in print. Print varying # of digits. */
422 print_longest (stream
, 'x', 1, val_long
);
431 print_longest (stream
, size
, 1, val_long
);
434 error (_("Undefined output size \"%c\"."), size
);
439 print_longest (stream
, 'd', 1, val_long
);
443 print_longest (stream
, 'u', 0, val_long
);
448 print_longest (stream
, 'o', 1, val_long
);
450 fprintf_filtered (stream
, "0");
455 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
456 print_address (addr
, stream
);
462 struct value_print_options opts
= *options
;
464 if (TYPE_UNSIGNED (type
))
465 value_print (value_from_longest (builtin_type_true_unsigned_char
,
469 value_print (value_from_longest (builtin_type_true_char
, val_long
),
475 type
= float_type_from_length (current_gdbarch
, type
);
476 print_floating (valaddr
, type
, stream
);
480 internal_error (__FILE__
, __LINE__
,
481 _("failed internal consistency check"));
484 /* Binary; 't' stands for "two". */
486 char bits
[8 * (sizeof val_long
) + 1];
487 char buf
[8 * (sizeof val_long
) + 32];
492 width
= 8 * (sizeof val_long
);
509 error (_("Undefined output size \"%c\"."), size
);
515 bits
[width
] = (val_long
& 1) ? '1' : '0';
520 while (*cp
&& *cp
== '0')
526 fputs_filtered (buf
, stream
);
531 error (_("Undefined output format \"%c\"."), options
->format
);
535 /* Specify default address for `x' command.
536 The `info lines' command uses this. */
539 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
541 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
545 /* Make address available to the user as $_. */
546 set_internalvar (lookup_internalvar ("_"),
547 value_from_pointer (ptr_type
, addr
));
550 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
551 after LEADIN. Print nothing if no symbolic name is found nearby.
552 Optionally also print source file and line number, if available.
553 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
554 or to interpret it as a possible C++ name and convert it back to source
555 form. However note that DO_DEMANGLE can be overridden by the specific
556 settings of the demangle and asm_demangle variables. */
559 print_address_symbolic (CORE_ADDR addr
, struct ui_file
*stream
,
560 int do_demangle
, char *leadin
)
563 char *filename
= NULL
;
568 /* Throw away both name and filename. */
569 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
570 make_cleanup (free_current_contents
, &filename
);
572 if (build_address_symbolic (addr
, do_demangle
, &name
, &offset
,
573 &filename
, &line
, &unmapped
))
575 do_cleanups (cleanup_chain
);
579 fputs_filtered (leadin
, stream
);
581 fputs_filtered ("<*", stream
);
583 fputs_filtered ("<", stream
);
584 fputs_filtered (name
, stream
);
586 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
588 /* Append source filename and line number if desired. Give specific
589 line # of this addr, if we have it; else line # of the nearest symbol. */
590 if (print_symbol_filename
&& filename
!= NULL
)
593 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
595 fprintf_filtered (stream
, " in %s", filename
);
598 fputs_filtered ("*>", stream
);
600 fputs_filtered (">", stream
);
602 do_cleanups (cleanup_chain
);
605 /* Given an address ADDR return all the elements needed to print the
606 address in a symbolic form. NAME can be mangled or not depending
607 on DO_DEMANGLE (and also on the asm_demangle global variable,
608 manipulated via ''set print asm-demangle''). Return 0 in case of
609 success, when all the info in the OUT paramters is valid. Return 1
612 build_address_symbolic (CORE_ADDR addr
, /* IN */
613 int do_demangle
, /* IN */
614 char **name
, /* OUT */
615 int *offset
, /* OUT */
616 char **filename
, /* OUT */
618 int *unmapped
) /* OUT */
620 struct minimal_symbol
*msymbol
;
621 struct symbol
*symbol
;
622 CORE_ADDR name_location
= 0;
623 struct obj_section
*section
= NULL
;
624 char *name_temp
= "";
626 /* Let's say it is mapped (not unmapped). */
629 /* Determine if the address is in an overlay, and whether it is
631 if (overlay_debugging
)
633 section
= find_pc_overlay (addr
);
634 if (pc_in_unmapped_range (addr
, section
))
637 addr
= overlay_mapped_address (addr
, section
);
641 /* First try to find the address in the symbol table, then
642 in the minsyms. Take the closest one. */
644 /* This is defective in the sense that it only finds text symbols. So
645 really this is kind of pointless--we should make sure that the
646 minimal symbols have everything we need (by changing that we could
647 save some memory, but for many debug format--ELF/DWARF or
648 anything/stabs--it would be inconvenient to eliminate those minimal
650 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
651 symbol
= find_pc_sect_function (addr
, section
);
655 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
656 if (do_demangle
|| asm_demangle
)
657 name_temp
= SYMBOL_PRINT_NAME (symbol
);
659 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
664 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
666 /* The msymbol is closer to the address than the symbol;
667 use the msymbol instead. */
669 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
670 if (do_demangle
|| asm_demangle
)
671 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
673 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
676 if (symbol
== NULL
&& msymbol
== NULL
)
679 /* If the nearest symbol is too far away, don't print anything symbolic. */
681 /* For when CORE_ADDR is larger than unsigned int, we do math in
682 CORE_ADDR. But when we detect unsigned wraparound in the
683 CORE_ADDR math, we ignore this test and print the offset,
684 because addr+max_symbolic_offset has wrapped through the end
685 of the address space back to the beginning, giving bogus comparison. */
686 if (addr
> name_location
+ max_symbolic_offset
687 && name_location
+ max_symbolic_offset
> name_location
)
690 *offset
= addr
- name_location
;
692 *name
= xstrdup (name_temp
);
694 if (print_symbol_filename
)
696 struct symtab_and_line sal
;
698 sal
= find_pc_sect_line (addr
, section
, 0);
702 *filename
= xstrdup (sal
.symtab
->filename
);
710 /* Print address ADDR symbolically on STREAM.
711 First print it as a number. Then perhaps print
712 <SYMBOL + OFFSET> after the number. */
715 print_address (CORE_ADDR addr
, struct ui_file
*stream
)
717 fputs_filtered (paddress (addr
), stream
);
718 print_address_symbolic (addr
, stream
, asm_demangle
, " ");
721 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
722 controls whether to print the symbolic name "raw" or demangled.
723 Global setting "addressprint" controls whether to print hex address
727 print_address_demangle (CORE_ADDR addr
, struct ui_file
*stream
,
730 struct value_print_options opts
;
731 get_user_print_options (&opts
);
734 fprintf_filtered (stream
, "0");
736 else if (opts
.addressprint
)
738 fputs_filtered (paddress (addr
), stream
);
739 print_address_symbolic (addr
, stream
, do_demangle
, " ");
743 print_address_symbolic (addr
, stream
, do_demangle
, "");
748 /* These are the types that $__ will get after an examine command of one
751 static struct type
*examine_i_type
;
753 static struct type
*examine_b_type
;
754 static struct type
*examine_h_type
;
755 static struct type
*examine_w_type
;
756 static struct type
*examine_g_type
;
758 /* Examine data at address ADDR in format FMT.
759 Fetch it from memory and print on gdb_stdout. */
762 do_examine (struct format_data fmt
, CORE_ADDR addr
)
767 struct type
*val_type
= NULL
;
770 struct value_print_options opts
;
777 /* String or instruction format implies fetch single bytes
778 regardless of the specified size. */
779 if (format
== 's' || format
== 'i')
783 val_type
= examine_i_type
;
784 else if (size
== 'b')
785 val_type
= examine_b_type
;
786 else if (size
== 'h')
787 val_type
= examine_h_type
;
788 else if (size
== 'w')
789 val_type
= examine_w_type
;
790 else if (size
== 'g')
791 val_type
= examine_g_type
;
798 if (format
== 's' || format
== 'i')
801 get_formatted_print_options (&opts
, format
);
803 /* Print as many objects as specified in COUNT, at most maxelts per line,
804 with the address of the next one at the start of each line. */
809 print_address (next_address
, gdb_stdout
);
810 printf_filtered (":");
815 printf_filtered ("\t");
816 /* Note that print_formatted sets next_address for the next
818 last_examine_address
= next_address
;
820 if (last_examine_value
)
821 value_free (last_examine_value
);
823 /* The value to be displayed is not fetched greedily.
824 Instead, to avoid the possibility of a fetched value not
825 being used, its retrieval is delayed until the print code
826 uses it. When examining an instruction stream, the
827 disassembler will perform its own memory fetch using just
828 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
829 the disassembler be modified so that LAST_EXAMINE_VALUE
830 is left with the byte sequence from the last complete
831 instruction fetched from memory? */
832 last_examine_value
= value_at_lazy (val_type
, next_address
);
834 if (last_examine_value
)
835 release_value (last_examine_value
);
837 print_formatted (last_examine_value
, size
, &opts
, gdb_stdout
);
839 /* Display any branch delay slots following the final insn. */
840 if (format
== 'i' && count
== 1)
841 count
+= branch_delay_insns
;
843 printf_filtered ("\n");
844 gdb_flush (gdb_stdout
);
849 validate_format (struct format_data fmt
, char *cmdname
)
852 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
854 error (_("Item count other than 1 is meaningless in \"%s\" command."),
856 if (fmt
.format
== 'i')
857 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
858 fmt
.format
, cmdname
);
861 /* Evaluate string EXP as an expression in the current language and
862 print the resulting value. EXP may contain a format specifier as the
863 first argument ("/x myvar" for example, to print myvar in hex). */
866 print_command_1 (char *exp
, int inspect
, int voidprint
)
868 struct expression
*expr
;
869 struct cleanup
*old_chain
= 0;
872 struct format_data fmt
;
875 if (exp
&& *exp
== '/')
878 fmt
= decode_format (&exp
, last_format
, 0);
879 validate_format (fmt
, "print");
880 last_format
= format
= fmt
.format
;
893 expr
= parse_expression (exp
);
894 old_chain
= make_cleanup (free_current_contents
, &expr
);
896 val
= evaluate_expression (expr
);
899 val
= access_value_history (0);
901 if (voidprint
|| (val
&& value_type (val
) &&
902 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
904 struct value_print_options opts
;
905 int histindex
= record_latest_value (val
);
908 annotate_value_history_begin (histindex
, value_type (val
));
910 annotate_value_begin (value_type (val
));
913 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
915 else if (histindex
>= 0)
916 printf_filtered ("$%d = ", histindex
);
919 annotate_value_history_value ();
921 get_formatted_print_options (&opts
, format
);
922 opts
.inspect_it
= inspect
;
925 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
926 printf_filtered ("\n");
929 annotate_value_history_end ();
931 annotate_value_end ();
934 printf_unfiltered ("\") )\030");
938 do_cleanups (old_chain
);
942 print_command (char *exp
, int from_tty
)
944 print_command_1 (exp
, 0, 1);
947 /* Same as print, except in epoch, it gets its own window. */
949 inspect_command (char *exp
, int from_tty
)
951 extern int epoch_interface
;
953 print_command_1 (exp
, epoch_interface
, 1);
956 /* Same as print, except it doesn't print void results. */
958 call_command (char *exp
, int from_tty
)
960 print_command_1 (exp
, 0, 0);
964 output_command (char *exp
, int from_tty
)
966 struct expression
*expr
;
967 struct cleanup
*old_chain
;
970 struct format_data fmt
;
971 struct value_print_options opts
;
976 if (exp
&& *exp
== '/')
979 fmt
= decode_format (&exp
, 0, 0);
980 validate_format (fmt
, "output");
984 expr
= parse_expression (exp
);
985 old_chain
= make_cleanup (free_current_contents
, &expr
);
987 val
= evaluate_expression (expr
);
989 annotate_value_begin (value_type (val
));
991 get_formatted_print_options (&opts
, format
);
993 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
995 annotate_value_end ();
998 gdb_flush (gdb_stdout
);
1000 do_cleanups (old_chain
);
1004 set_command (char *exp
, int from_tty
)
1006 struct expression
*expr
= parse_expression (exp
);
1007 struct cleanup
*old_chain
=
1008 make_cleanup (free_current_contents
, &expr
);
1009 evaluate_expression (expr
);
1010 do_cleanups (old_chain
);
1014 sym_info (char *arg
, int from_tty
)
1016 struct minimal_symbol
*msymbol
;
1017 struct objfile
*objfile
;
1018 struct obj_section
*osect
;
1019 CORE_ADDR addr
, sect_addr
;
1021 unsigned int offset
;
1024 error_no_arg (_("address"));
1026 addr
= parse_and_eval_address (arg
);
1027 ALL_OBJSECTIONS (objfile
, osect
)
1029 /* Only process each object file once, even if there's a separate
1031 if (objfile
->separate_debug_objfile_backlink
)
1034 sect_addr
= overlay_mapped_address (addr
, osect
);
1036 if (obj_section_addr (osect
) <= sect_addr
1037 && sect_addr
< obj_section_endaddr (osect
)
1038 && (msymbol
= lookup_minimal_symbol_by_pc_section (sect_addr
, osect
)))
1040 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1042 struct cleanup
*old_chain
;
1045 offset
= sect_addr
- SYMBOL_VALUE_ADDRESS (msymbol
);
1046 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1047 sec_name
= osect
->the_bfd_section
->name
;
1048 msym_name
= SYMBOL_PRINT_NAME (msymbol
);
1050 /* Don't print the offset if it is zero.
1051 We assume there's no need to handle i18n of "sym + offset". */
1053 loc_string
= xstrprintf ("%s + %u", msym_name
, offset
);
1055 loc_string
= xstrprintf ("%s", msym_name
);
1057 /* Use a cleanup to free loc_string in case the user quits
1058 a pagination request inside printf_filtered. */
1059 old_chain
= make_cleanup (xfree
, loc_string
);
1061 gdb_assert (osect
->objfile
&& osect
->objfile
->name
);
1062 obj_name
= osect
->objfile
->name
;
1064 if (MULTI_OBJFILE_P ())
1065 if (pc_in_unmapped_range (addr
, osect
))
1066 if (section_is_overlay (osect
))
1067 printf_filtered (_("%s in load address range of "
1068 "%s overlay section %s of %s\n"),
1069 loc_string
, mapped
, sec_name
, obj_name
);
1071 printf_filtered (_("%s in load address range of "
1072 "section %s of %s\n"),
1073 loc_string
, sec_name
, obj_name
);
1075 if (section_is_overlay (osect
))
1076 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1077 loc_string
, mapped
, sec_name
, obj_name
);
1079 printf_filtered (_("%s in section %s of %s\n"),
1080 loc_string
, sec_name
, obj_name
);
1082 if (pc_in_unmapped_range (addr
, osect
))
1083 if (section_is_overlay (osect
))
1084 printf_filtered (_("%s in load address range of %s overlay "
1086 loc_string
, mapped
, sec_name
);
1088 printf_filtered (_("%s in load address range of section %s\n"),
1089 loc_string
, sec_name
);
1091 if (section_is_overlay (osect
))
1092 printf_filtered (_("%s in %s overlay section %s\n"),
1093 loc_string
, mapped
, sec_name
);
1095 printf_filtered (_("%s in section %s\n"),
1096 loc_string
, sec_name
);
1098 do_cleanups (old_chain
);
1102 printf_filtered (_("No symbol matches %s.\n"), arg
);
1106 address_info (char *exp
, int from_tty
)
1109 struct minimal_symbol
*msymbol
;
1111 struct obj_section
*section
;
1112 CORE_ADDR load_addr
;
1113 int is_a_field_of_this
; /* C++: lookup_symbol sets this to nonzero
1114 if exp is a field of `this'. */
1117 error (_("Argument required."));
1119 sym
= lookup_symbol (exp
, get_selected_block (0), VAR_DOMAIN
,
1120 &is_a_field_of_this
);
1123 if (is_a_field_of_this
)
1125 printf_filtered ("Symbol \"");
1126 fprintf_symbol_filtered (gdb_stdout
, exp
,
1127 current_language
->la_language
, DMGL_ANSI
);
1128 printf_filtered ("\" is a field of the local class variable ");
1129 if (current_language
->la_language
== language_objc
)
1130 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1132 printf_filtered ("`this'\n");
1136 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1138 if (msymbol
!= NULL
)
1140 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1142 printf_filtered ("Symbol \"");
1143 fprintf_symbol_filtered (gdb_stdout
, exp
,
1144 current_language
->la_language
, DMGL_ANSI
);
1145 printf_filtered ("\" is at ");
1146 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1147 printf_filtered (" in a file compiled without debugging");
1148 section
= SYMBOL_OBJ_SECTION (msymbol
);
1149 if (section_is_overlay (section
))
1151 load_addr
= overlay_unmapped_address (load_addr
, section
);
1152 printf_filtered (",\n -- loaded at ");
1153 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1154 printf_filtered (" in overlay section %s",
1155 section
->the_bfd_section
->name
);
1157 printf_filtered (".\n");
1160 error (_("No symbol \"%s\" in current context."), exp
);
1164 printf_filtered ("Symbol \"");
1165 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1166 current_language
->la_language
, DMGL_ANSI
);
1167 printf_filtered ("\" is ");
1168 val
= SYMBOL_VALUE (sym
);
1169 section
= SYMBOL_OBJ_SECTION (sym
);
1171 switch (SYMBOL_CLASS (sym
))
1174 case LOC_CONST_BYTES
:
1175 printf_filtered ("constant");
1179 printf_filtered ("a label at address ");
1180 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1182 if (section_is_overlay (section
))
1184 load_addr
= overlay_unmapped_address (load_addr
, section
);
1185 printf_filtered (",\n -- loaded at ");
1186 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1187 printf_filtered (" in overlay section %s",
1188 section
->the_bfd_section
->name
);
1193 /* FIXME: cagney/2004-01-26: It should be possible to
1194 unconditionally call the SYMBOL_OPS method when available.
1195 Unfortunately DWARF 2 stores the frame-base (instead of the
1196 function) location in a function's symbol. Oops! For the
1197 moment enable this when/where applicable. */
1198 SYMBOL_OPS (sym
)->describe_location (sym
, gdb_stdout
);
1202 if (SYMBOL_IS_ARGUMENT (sym
))
1203 printf_filtered (_("an argument in register %s"),
1204 gdbarch_register_name (current_gdbarch
, val
));
1206 printf_filtered (_("a variable in register %s"),
1207 gdbarch_register_name (current_gdbarch
, val
));
1211 printf_filtered (_("static storage at address "));
1212 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1214 if (section_is_overlay (section
))
1216 load_addr
= overlay_unmapped_address (load_addr
, section
);
1217 printf_filtered (_(",\n -- loaded at "));
1218 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1219 printf_filtered (_(" in overlay section %s"),
1220 section
->the_bfd_section
->name
);
1224 case LOC_REGPARM_ADDR
:
1225 printf_filtered (_("address of an argument in register %s"),
1226 gdbarch_register_name (current_gdbarch
, val
));
1230 printf_filtered (_("an argument at offset %ld"), val
);
1234 printf_filtered (_("a local variable at frame offset %ld"), val
);
1238 printf_filtered (_("a reference argument at offset %ld"), val
);
1242 printf_filtered (_("a typedef"));
1246 printf_filtered (_("a function at address "));
1247 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1248 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1249 if (section_is_overlay (section
))
1251 load_addr
= overlay_unmapped_address (load_addr
, section
);
1252 printf_filtered (_(",\n -- loaded at "));
1253 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1254 printf_filtered (_(" in overlay section %s"),
1255 section
->the_bfd_section
->name
);
1259 case LOC_UNRESOLVED
:
1261 struct minimal_symbol
*msym
;
1263 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1265 printf_filtered ("unresolved");
1268 section
= SYMBOL_OBJ_SECTION (msym
);
1269 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1272 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1273 printf_filtered (_("a thread-local variable at offset %s "
1274 "in the thread-local storage for `%s'"),
1275 paddr_nz (load_addr
), section
->objfile
->name
);
1278 printf_filtered (_("static storage at address "));
1279 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1280 if (section_is_overlay (section
))
1282 load_addr
= overlay_unmapped_address (load_addr
, section
);
1283 printf_filtered (_(",\n -- loaded at "));
1284 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1285 printf_filtered (_(" in overlay section %s"),
1286 section
->the_bfd_section
->name
);
1293 case LOC_OPTIMIZED_OUT
:
1294 printf_filtered (_("optimized out"));
1298 printf_filtered (_("of unknown (botched) type"));
1301 printf_filtered (".\n");
1306 x_command (char *exp
, int from_tty
)
1308 struct expression
*expr
;
1309 struct format_data fmt
;
1310 struct cleanup
*old_chain
;
1313 fmt
.format
= last_format
? last_format
: 'x';
1314 fmt
.size
= last_size
;
1318 if (exp
&& *exp
== '/')
1321 fmt
= decode_format (&exp
, last_format
, last_size
);
1324 /* If we have an expression, evaluate it and use it as the address. */
1326 if (exp
!= 0 && *exp
!= 0)
1328 expr
= parse_expression (exp
);
1329 /* Cause expression not to be there any more if this command is
1330 repeated with Newline. But don't clobber a user-defined
1331 command's definition. */
1334 old_chain
= make_cleanup (free_current_contents
, &expr
);
1335 val
= evaluate_expression (expr
);
1336 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1337 val
= value_ind (val
);
1338 /* In rvalue contexts, such as this, functions are coerced into
1339 pointers to functions. This makes "x/i main" work. */
1340 if (/* last_format == 'i' && */
1341 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1342 && VALUE_LVAL (val
) == lval_memory
)
1343 next_address
= value_address (val
);
1345 next_address
= value_as_address (val
);
1346 do_cleanups (old_chain
);
1349 do_examine (fmt
, next_address
);
1351 /* If the examine succeeds, we remember its size and format for next
1353 last_size
= fmt
.size
;
1354 last_format
= fmt
.format
;
1356 /* Set a couple of internal variables if appropriate. */
1357 if (last_examine_value
)
1359 /* Make last address examined available to the user as $_. Use
1360 the correct pointer type. */
1361 struct type
*pointer_type
1362 = lookup_pointer_type (value_type (last_examine_value
));
1363 set_internalvar (lookup_internalvar ("_"),
1364 value_from_pointer (pointer_type
,
1365 last_examine_address
));
1367 /* Make contents of last address examined available to the user
1368 as $__. If the last value has not been fetched from memory
1369 then don't fetch it now; instead mark it by voiding the $__
1371 if (value_lazy (last_examine_value
))
1372 clear_internalvar (lookup_internalvar ("__"));
1374 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1379 /* Add an expression to the auto-display chain.
1380 Specify the expression. */
1383 display_command (char *exp
, int from_tty
)
1385 struct format_data fmt
;
1386 struct expression
*expr
;
1387 struct display
*new;
1391 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1393 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1394 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1408 fmt
= decode_format (&exp
, 0, 0);
1409 if (fmt
.size
&& fmt
.format
== 0)
1411 if (fmt
.format
== 'i' || fmt
.format
== 's')
1422 innermost_block
= NULL
;
1423 expr
= parse_expression (exp
);
1425 new = (struct display
*) xmalloc (sizeof (struct display
));
1427 new->exp_string
= xstrdup (exp
);
1429 new->block
= innermost_block
;
1430 new->next
= display_chain
;
1431 new->number
= ++display_number
;
1434 display_chain
= new;
1436 if (from_tty
&& target_has_execution
)
1437 do_one_display (new);
1444 free_display (struct display
*d
)
1446 xfree (d
->exp_string
);
1451 /* Clear out the display_chain. Done when new symtabs are loaded,
1452 since this invalidates the types stored in many expressions. */
1455 clear_displays (void)
1459 while ((d
= display_chain
) != NULL
)
1461 display_chain
= d
->next
;
1466 /* Delete the auto-display number NUM. */
1469 delete_display (int num
)
1471 struct display
*d1
, *d
;
1474 error (_("No display number %d."), num
);
1476 if (display_chain
->number
== num
)
1479 display_chain
= d1
->next
;
1483 for (d
= display_chain
;; d
= d
->next
)
1486 error (_("No display number %d."), num
);
1487 if (d
->next
->number
== num
)
1497 /* Delete some values from the auto-display chain.
1498 Specify the element numbers. */
1501 undisplay_command (char *args
, int from_tty
)
1509 if (query (_("Delete all auto-display expressions? ")))
1518 while (*p1
>= '0' && *p1
<= '9')
1520 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1521 error (_("Arguments must be display numbers."));
1525 delete_display (num
);
1528 while (*p
== ' ' || *p
== '\t')
1534 /* Display a single auto-display.
1535 Do nothing if the display cannot be printed in the current context,
1536 or if the display is disabled. */
1539 do_one_display (struct display
*d
)
1541 int within_current_scope
;
1543 if (d
->enabled_p
== 0)
1548 volatile struct gdb_exception ex
;
1549 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1551 innermost_block
= NULL
;
1552 d
->exp
= parse_expression (d
->exp_string
);
1553 d
->block
= innermost_block
;
1557 /* Can't re-parse the expression. Disable this display item. */
1559 warning (_("Unable to display \"%s\": %s"),
1560 d
->exp_string
, ex
.message
);
1566 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1568 within_current_scope
= 1;
1569 if (!within_current_scope
)
1572 current_display_number
= d
->number
;
1574 annotate_display_begin ();
1575 printf_filtered ("%d", d
->number
);
1576 annotate_display_number_end ();
1577 printf_filtered (": ");
1583 annotate_display_format ();
1585 printf_filtered ("x/");
1586 if (d
->format
.count
!= 1)
1587 printf_filtered ("%d", d
->format
.count
);
1588 printf_filtered ("%c", d
->format
.format
);
1589 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1590 printf_filtered ("%c", d
->format
.size
);
1591 printf_filtered (" ");
1593 annotate_display_expression ();
1595 puts_filtered (d
->exp_string
);
1596 annotate_display_expression_end ();
1598 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1599 printf_filtered ("\n");
1601 printf_filtered (" ");
1603 val
= evaluate_expression (d
->exp
);
1604 addr
= value_as_address (val
);
1605 if (d
->format
.format
== 'i')
1606 addr
= gdbarch_addr_bits_remove (current_gdbarch
, addr
);
1608 annotate_display_value ();
1610 do_examine (d
->format
, addr
);
1614 struct value_print_options opts
;
1616 annotate_display_format ();
1618 if (d
->format
.format
)
1619 printf_filtered ("/%c ", d
->format
.format
);
1621 annotate_display_expression ();
1623 puts_filtered (d
->exp_string
);
1624 annotate_display_expression_end ();
1626 printf_filtered (" = ");
1628 annotate_display_expression ();
1630 get_formatted_print_options (&opts
, d
->format
.format
);
1631 opts
.raw
= d
->format
.raw
;
1632 print_formatted (evaluate_expression (d
->exp
),
1633 d
->format
.size
, &opts
, gdb_stdout
);
1634 printf_filtered ("\n");
1637 annotate_display_end ();
1639 gdb_flush (gdb_stdout
);
1640 current_display_number
= -1;
1643 /* Display all of the values on the auto-display chain which can be
1644 evaluated in the current scope. */
1651 for (d
= display_chain
; d
; d
= d
->next
)
1655 /* Delete the auto-display which we were in the process of displaying.
1656 This is done when there is an error or a signal. */
1659 disable_display (int num
)
1663 for (d
= display_chain
; d
; d
= d
->next
)
1664 if (d
->number
== num
)
1669 printf_unfiltered (_("No display number %d.\n"), num
);
1673 disable_current_display (void)
1675 if (current_display_number
>= 0)
1677 disable_display (current_display_number
);
1678 fprintf_unfiltered (gdb_stderr
, _("\
1679 Disabling display %d to avoid infinite recursion.\n"),
1680 current_display_number
);
1682 current_display_number
= -1;
1686 display_info (char *ignore
, int from_tty
)
1691 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1693 printf_filtered (_("Auto-display expressions now in effect:\n\
1694 Num Enb Expression\n"));
1696 for (d
= display_chain
; d
; d
= d
->next
)
1698 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1700 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1702 else if (d
->format
.format
)
1703 printf_filtered ("/%c ", d
->format
.format
);
1704 puts_filtered (d
->exp_string
);
1705 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1706 printf_filtered (_(" (cannot be evaluated in the current context)"));
1707 printf_filtered ("\n");
1708 gdb_flush (gdb_stdout
);
1713 enable_display (char *args
, int from_tty
)
1722 for (d
= display_chain
; d
; d
= d
->next
)
1729 while (*p1
>= '0' && *p1
<= '9')
1731 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1732 error (_("Arguments must be display numbers."));
1736 for (d
= display_chain
; d
; d
= d
->next
)
1737 if (d
->number
== num
)
1742 printf_unfiltered (_("No display number %d.\n"), num
);
1745 while (*p
== ' ' || *p
== '\t')
1751 disable_display_command (char *args
, int from_tty
)
1759 for (d
= display_chain
; d
; d
= d
->next
)
1766 while (*p1
>= '0' && *p1
<= '9')
1768 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1769 error (_("Arguments must be display numbers."));
1771 disable_display (atoi (p
));
1774 while (*p
== ' ' || *p
== '\t')
1779 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1780 is unloaded), otherwise return 0. */
1783 display_uses_solib_p (const struct display
*d
,
1784 const struct so_list
*solib
)
1787 struct expression
*const exp
= d
->exp
;
1788 const union exp_element
*const elts
= exp
->elts
;
1790 if (d
->block
!= NULL
1791 && solib_contains_address_p (solib
, d
->block
->startaddr
))
1794 for (endpos
= exp
->nelts
; endpos
> 0; )
1796 int i
, args
, oplen
= 0;
1798 exp
->language_defn
->la_exp_desc
->operator_length (exp
, endpos
,
1800 gdb_assert (oplen
> 0);
1803 if (elts
[i
].opcode
== OP_VAR_VALUE
)
1805 const struct block
*const block
= elts
[i
+ 1].block
;
1806 const struct symbol
*const symbol
= elts
[i
+ 2].symbol
;
1807 const struct obj_section
*const section
=
1808 SYMBOL_OBJ_SECTION (symbol
);
1811 && solib_contains_address_p (solib
, block
->startaddr
))
1814 if (section
&& section
->objfile
== solib
->objfile
)
1823 /* display_chain items point to blocks and expressions. Some expressions in
1824 turn may point to symbols.
1825 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1826 obstack_free'd when a shared library is unloaded.
1827 Clear pointers that are about to become dangling.
1828 Both .exp and .block fields will be restored next time we need to display
1829 an item by re-parsing .exp_string field in the new execution context. */
1832 clear_dangling_display_expressions (struct so_list
*solib
)
1835 struct objfile
*objfile
= NULL
;
1837 for (d
= display_chain
; d
; d
= d
->next
)
1839 if (d
->exp
&& display_uses_solib_p (d
, solib
))
1849 /* Print the value in stack frame FRAME of a variable specified by a
1850 struct symbol. NAME is the name to print; if NULL then VAR's print
1851 name will be used. STREAM is the ui_file on which to print the
1852 value. INDENT specifies the number of indent levels to print
1853 before printing the variable name. */
1856 print_variable_and_value (const char *name
, struct symbol
*var
,
1857 struct frame_info
*frame
,
1858 struct ui_file
*stream
, int indent
)
1861 struct value_print_options opts
;
1864 name
= SYMBOL_PRINT_NAME (var
);
1866 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1868 val
= read_var_value (var
, frame
);
1869 get_user_print_options (&opts
);
1870 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1871 fprintf_filtered (stream
, "\n");
1875 printf_command (char *arg
, int from_tty
)
1879 char *string
= NULL
;
1880 struct value
**val_args
;
1882 char *current_substring
;
1884 int allocated_args
= 20;
1885 struct cleanup
*old_cleanups
;
1887 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1888 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1891 error_no_arg (_("format-control string and values to print"));
1893 /* Skip white space before format string */
1894 while (*s
== ' ' || *s
== '\t')
1897 /* A format string should follow, enveloped in double quotes. */
1899 error (_("Bad format string, missing '\"'."));
1901 /* Parse the format-control string and copy it into the string STRING,
1902 processing some kinds of escape sequence. */
1904 f
= string
= (char *) alloca (strlen (s
) + 1);
1912 error (_("Bad format string, non-terminated '\"'."));
1945 /* ??? TODO: handle other escape sequences */
1946 error (_("Unrecognized escape character \\%c in format string."),
1956 /* Skip over " and following space and comma. */
1959 while (*s
== ' ' || *s
== '\t')
1962 if (*s
!= ',' && *s
!= 0)
1963 error (_("Invalid argument syntax"));
1967 while (*s
== ' ' || *s
== '\t')
1970 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1971 substrings
= alloca (strlen (string
) * 2);
1972 current_substring
= substrings
;
1975 /* Now scan the string for %-specs and see what kinds of args they want.
1976 argclass[I] classifies the %-specs so we can give printf_filtered
1977 something of the right size. */
1981 int_arg
, long_arg
, long_long_arg
, ptr_arg
,
1982 string_arg
, wide_string_arg
, wide_char_arg
,
1983 double_arg
, long_double_arg
, decfloat_arg
1985 enum argclass
*argclass
;
1986 enum argclass this_argclass
;
1991 argclass
= (enum argclass
*) alloca (strlen (s
) * sizeof *argclass
);
1998 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
1999 int seen_space
= 0, seen_plus
= 0;
2000 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
2001 int seen_big_d
= 0, seen_double_big_d
= 0;
2004 /* Check the validity of the format specifier, and work
2005 out what argument it expects. We only accept C89
2006 format strings, with the exception of long long (which
2007 we autoconf for). */
2009 /* Skip over "%%". */
2016 /* The first part of a format specifier is a set of flag
2018 while (strchr ("0-+ #", *f
))
2031 /* The next part of a format specifier is a width. */
2032 while (strchr ("0123456789", *f
))
2035 /* The next part of a format specifier is a precision. */
2040 while (strchr ("0123456789", *f
))
2044 /* The next part of a format specifier is a length modifier. */
2065 /* Decimal32 modifier. */
2071 /* Decimal64 and Decimal128 modifiers. */
2076 /* Check for a Decimal128. */
2080 seen_double_big_d
= 1;
2096 if (seen_space
|| seen_plus
)
2103 this_argclass
= int_arg
;
2104 else if (lcount
== 1)
2105 this_argclass
= long_arg
;
2107 this_argclass
= long_long_arg
;
2114 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
2115 if (lcount
> 1 || seen_h
|| seen_big_l
)
2117 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2122 this_argclass
= ptr_arg
;
2123 if (lcount
|| seen_h
|| seen_big_l
)
2125 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2130 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
2131 if (lcount
> 1 || seen_h
|| seen_big_l
)
2133 if (seen_zero
|| seen_space
|| seen_plus
)
2142 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
2143 this_argclass
= decfloat_arg
;
2144 else if (seen_big_l
)
2145 this_argclass
= long_double_arg
;
2147 this_argclass
= double_arg
;
2149 if (lcount
|| seen_h
)
2154 error (_("`*' not supported for precision or width in printf"));
2157 error (_("Format specifier `n' not supported in printf"));
2160 error (_("Incomplete format specifier at end of format string"));
2163 error (_("Unrecognized format specifier '%c' in printf"), *f
);
2167 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2172 if (lcount
> 1 && USE_PRINTF_I64
)
2174 /* Windows' printf does support long long, but not the usual way.
2175 Convert %lld to %I64d. */
2176 int length_before_ll
= f
- last_arg
- 1 - lcount
;
2177 strncpy (current_substring
, last_arg
, length_before_ll
);
2178 strcpy (current_substring
+ length_before_ll
, "I64");
2179 current_substring
[length_before_ll
+ 3] =
2180 last_arg
[length_before_ll
+ lcount
];
2181 current_substring
+= length_before_ll
+ 4;
2183 else if (this_argclass
== wide_string_arg
2184 || this_argclass
== wide_char_arg
)
2186 /* Convert %ls or %lc to %s. */
2187 int length_before_ls
= f
- last_arg
- 2;
2188 strncpy (current_substring
, last_arg
, length_before_ls
);
2189 strcpy (current_substring
+ length_before_ls
, "s");
2190 current_substring
+= length_before_ls
+ 2;
2194 strncpy (current_substring
, last_arg
, f
- last_arg
);
2195 current_substring
+= f
- last_arg
;
2197 *current_substring
++ = '\0';
2199 argclass
[nargs_wanted
++] = this_argclass
;
2202 /* Now, parse all arguments and evaluate them.
2203 Store the VALUEs in VAL_ARGS. */
2208 if (nargs
== allocated_args
)
2209 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2210 (allocated_args
*= 2)
2211 * sizeof (struct value
*));
2213 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2221 if (nargs
!= nargs_wanted
)
2222 error (_("Wrong number of arguments for specified format-string"));
2224 /* Now actually print them. */
2225 current_substring
= substrings
;
2226 for (i
= 0; i
< nargs
; i
++)
2228 switch (argclass
[i
])
2235 tem
= value_as_address (val_args
[i
]);
2237 /* This is a %s argument. Find the length of the string. */
2242 read_memory (tem
+ j
, &c
, 1);
2247 /* Copy the string contents into a string inside GDB. */
2248 str
= (gdb_byte
*) alloca (j
+ 1);
2250 read_memory (tem
, str
, j
);
2253 printf_filtered (current_substring
, (char *) str
);
2256 case wide_string_arg
:
2261 struct type
*wctype
= lookup_typename ("wchar_t", NULL
, 0);
2262 int wcwidth
= TYPE_LENGTH (wctype
);
2263 gdb_byte
*buf
= alloca (wcwidth
);
2264 struct obstack output
;
2265 struct cleanup
*inner_cleanup
;
2267 tem
= value_as_address (val_args
[i
]);
2269 /* This is a %s argument. Find the length of the string. */
2270 for (j
= 0;; j
+= wcwidth
)
2273 read_memory (tem
+ j
, buf
, wcwidth
);
2274 if (extract_unsigned_integer (buf
, wcwidth
) == 0)
2278 /* Copy the string contents into a string inside GDB. */
2279 str
= (gdb_byte
*) alloca (j
+ wcwidth
);
2281 read_memory (tem
, str
, j
);
2282 memset (&str
[j
], 0, wcwidth
);
2284 obstack_init (&output
);
2285 inner_cleanup
= make_cleanup_obstack_free (&output
);
2287 convert_between_encodings (target_wide_charset (),
2290 &output
, translit_char
);
2291 obstack_grow_str0 (&output
, "");
2293 printf_filtered (current_substring
, obstack_base (&output
));
2294 do_cleanups (inner_cleanup
);
2299 struct type
*wctype
= lookup_typename ("wchar_t", NULL
, 0);
2300 struct type
*valtype
;
2301 struct obstack output
;
2302 struct cleanup
*inner_cleanup
;
2303 const gdb_byte
*bytes
;
2305 valtype
= value_type (val_args
[i
]);
2306 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2307 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2308 error (_("expected wchar_t argument for %%lc"));
2310 bytes
= value_contents (val_args
[i
]);
2312 obstack_init (&output
);
2313 inner_cleanup
= make_cleanup_obstack_free (&output
);
2315 convert_between_encodings (target_wide_charset (),
2317 bytes
, TYPE_LENGTH (valtype
),
2318 TYPE_LENGTH (valtype
),
2319 &output
, translit_char
);
2320 obstack_grow_str0 (&output
, "");
2322 printf_filtered (current_substring
, obstack_base (&output
));
2323 do_cleanups (inner_cleanup
);
2328 struct type
*type
= value_type (val_args
[i
]);
2332 /* If format string wants a float, unchecked-convert the value
2333 to floating point of the same size. */
2334 type
= float_type_from_length (current_gdbarch
, type
);
2335 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2337 error (_("Invalid floating value found in program."));
2339 printf_filtered (current_substring
, (double) val
);
2342 case long_double_arg
:
2343 #ifdef HAVE_LONG_DOUBLE
2345 struct type
*type
= value_type (val_args
[i
]);
2349 /* If format string wants a float, unchecked-convert the value
2350 to floating point of the same size. */
2351 type
= float_type_from_length (current_gdbarch
, type
);
2352 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2354 error (_("Invalid floating value found in program."));
2356 printf_filtered (current_substring
, (long double) val
);
2360 error (_("long double not supported in printf"));
2363 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2365 long long val
= value_as_long (val_args
[i
]);
2366 printf_filtered (current_substring
, val
);
2370 error (_("long long not supported in printf"));
2374 int val
= value_as_long (val_args
[i
]);
2375 printf_filtered (current_substring
, val
);
2380 long val
= value_as_long (val_args
[i
]);
2381 printf_filtered (current_substring
, val
);
2385 /* Handles decimal floating values. */
2388 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2389 #if defined (PRINTF_HAS_DECFLOAT)
2390 /* If we have native support for Decimal floating
2391 printing, handle it here. */
2392 printf_filtered (current_substring
, param_ptr
);
2395 /* As a workaround until vasprintf has native support for DFP
2396 we convert the DFP values to string and print them using
2397 the %s format specifier. */
2400 int nnull_chars
= 0;
2402 /* Parameter data. */
2403 struct type
*param_type
= value_type (val_args
[i
]);
2404 unsigned int param_len
= TYPE_LENGTH (param_type
);
2406 /* DFP output data. */
2407 struct value
*dfp_value
= NULL
;
2411 struct type
*dfp_type
= NULL
;
2412 char decstr
[MAX_DECIMAL_STRING
];
2414 /* Points to the end of the string so that we can go back
2415 and check for DFP length modifiers. */
2416 eos
= current_substring
+ strlen (current_substring
);
2418 /* Look for the float/double format specifier. */
2419 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2420 && *eos
!= 'g' && *eos
!= 'G')
2425 /* Search for the '%' char and extract the size and type of
2426 the output decimal value based on its modifiers
2427 (%Hf, %Df, %DDf). */
2428 while (*--sos
!= '%')
2433 dfp_type
= builtin_type (current_gdbarch
)->builtin_decfloat
;
2435 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2438 dfp_type
= builtin_type (current_gdbarch
)->builtin_declong
;
2444 dfp_type
= builtin_type (current_gdbarch
)->builtin_decdouble
;
2448 /* Replace %Hf, %Df and %DDf with %s's. */
2451 /* Go through the whole format string and pull the correct
2452 number of chars back to compensate for the change in the
2453 format specifier. */
2454 while (nnull_chars
< nargs
- i
)
2462 /* Conversion between different DFP types. */
2463 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2464 decimal_convert (param_ptr
, param_len
, dec
, dfp_len
);
2466 /* If this is a non-trivial conversion, just output 0.
2467 A correct converted value can be displayed by explicitly
2468 casting to a DFP type. */
2469 decimal_from_string (dec
, dfp_len
, "0");
2471 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2473 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2475 decimal_to_string (dfp_ptr
, dfp_len
, decstr
);
2477 /* Print the DFP value. */
2478 printf_filtered (current_substring
, decstr
);
2486 /* We avoid the host's %p because pointers are too
2487 likely to be the wrong size. The only interesting
2488 modifier for %p is a width; extract that, and then
2489 handle %p as glibc would: %#x or a literal "(nil)". */
2491 char *p
, *fmt
, *fmt_p
;
2492 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2493 long long val
= value_as_long (val_args
[i
]);
2495 long val
= value_as_long (val_args
[i
]);
2498 fmt
= alloca (strlen (current_substring
) + 5);
2500 /* Copy up to the leading %. */
2501 p
= current_substring
;
2505 int is_percent
= (*p
== '%');
2519 /* Copy any width. */
2520 while (*p
>= '0' && *p
< '9')
2523 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2526 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2532 printf_filtered (fmt
, val
);
2538 printf_filtered (fmt
, "(nil)");
2544 internal_error (__FILE__
, __LINE__
,
2545 _("failed internal consistency check"));
2547 /* Skip to the next substring. */
2548 current_substring
+= strlen (current_substring
) + 1;
2550 /* Print the portion of the format string after the last argument. */
2551 puts_filtered (last_arg
);
2553 do_cleanups (old_cleanups
);
2557 _initialize_printcmd (void)
2559 struct cmd_list_element
*c
;
2561 current_display_number
= -1;
2563 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2565 add_info ("address", address_info
,
2566 _("Describe where symbol SYM is stored."));
2568 add_info ("symbol", sym_info
, _("\
2569 Describe what symbol is at location ADDR.\n\
2570 Only for symbols with fixed locations (global or static scope)."));
2572 add_com ("x", class_vars
, x_command
, _("\
2573 Examine memory: x/FMT ADDRESS.\n\
2574 ADDRESS is an expression for the memory address to examine.\n\
2575 FMT is a repeat count followed by a format letter and a size letter.\n\
2576 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2577 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2578 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2579 The specified number of objects of the specified size are printed\n\
2580 according to the format.\n\n\
2581 Defaults for format and size letters are those previously used.\n\
2582 Default count is 1. Default address is following last thing printed\n\
2583 with this command or \"print\"."));
2586 add_com ("whereis", class_vars
, whereis_command
,
2587 _("Print line number and file of definition of variable."));
2590 add_info ("display", display_info
, _("\
2591 Expressions to display when program stops, with code numbers."));
2593 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2594 Cancel some expressions to be displayed when program stops.\n\
2595 Arguments are the code numbers of the expressions to stop displaying.\n\
2596 No argument means cancel all automatic-display expressions.\n\
2597 \"delete display\" has the same effect as this command.\n\
2598 Do \"info display\" to see current list of code numbers."),
2601 add_com ("display", class_vars
, display_command
, _("\
2602 Print value of expression EXP each time the program stops.\n\
2603 /FMT may be used before EXP as in the \"print\" command.\n\
2604 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2605 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2606 and examining is done as in the \"x\" command.\n\n\
2607 With no argument, display all currently requested auto-display expressions.\n\
2608 Use \"undisplay\" to cancel display requests previously made."));
2610 add_cmd ("display", class_vars
, enable_display
, _("\
2611 Enable some expressions to be displayed when program stops.\n\
2612 Arguments are the code numbers of the expressions to resume displaying.\n\
2613 No argument means enable all automatic-display expressions.\n\
2614 Do \"info display\" to see current list of code numbers."), &enablelist
);
2616 add_cmd ("display", class_vars
, disable_display_command
, _("\
2617 Disable some expressions to be displayed when program stops.\n\
2618 Arguments are the code numbers of the expressions to stop displaying.\n\
2619 No argument means disable all automatic-display expressions.\n\
2620 Do \"info display\" to see current list of code numbers."), &disablelist
);
2622 add_cmd ("display", class_vars
, undisplay_command
, _("\
2623 Cancel some expressions to be displayed when program stops.\n\
2624 Arguments are the code numbers of the expressions to stop displaying.\n\
2625 No argument means cancel all automatic-display expressions.\n\
2626 Do \"info display\" to see current list of code numbers."), &deletelist
);
2628 add_com ("printf", class_vars
, printf_command
, _("\
2629 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2630 This is useful for formatted output in user-defined commands."));
2632 add_com ("output", class_vars
, output_command
, _("\
2633 Like \"print\" but don't put in value history and don't print newline.\n\
2634 This is useful in user-defined commands."));
2636 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2637 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2638 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2639 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2640 with $), a register (a few standard names starting with $), or an actual\n\
2641 variable in the program being debugged. EXP is any valid expression.\n\
2642 Use \"set variable\" for variables with names identical to set subcommands.\n\
2644 With a subcommand, this command modifies parts of the gdb environment.\n\
2645 You can see these environment settings with the \"show\" command."),
2646 &setlist
, "set ", 1, &cmdlist
);
2648 add_com ("assign", class_vars
, set_command
, _("\
2649 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2650 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2651 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2652 with $), a register (a few standard names starting with $), or an actual\n\
2653 variable in the program being debugged. EXP is any valid expression.\n\
2654 Use \"set variable\" for variables with names identical to set subcommands.\n\
2655 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2656 You can see these environment settings with the \"show\" command."));
2658 /* "call" is the same as "set", but handy for dbx users to call fns. */
2659 c
= add_com ("call", class_vars
, call_command
, _("\
2660 Call a function in the program.\n\
2661 The argument is the function name and arguments, in the notation of the\n\
2662 current working language. The result is printed and saved in the value\n\
2663 history, if it is not void."));
2664 set_cmd_completer (c
, expression_completer
);
2666 add_cmd ("variable", class_vars
, set_command
, _("\
2667 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2668 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2669 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2670 with $), a register (a few standard names starting with $), or an actual\n\
2671 variable in the program being debugged. EXP is any valid expression.\n\
2672 This may usually be abbreviated to simply \"set\"."),
2675 c
= add_com ("print", class_vars
, print_command
, _("\
2676 Print value of expression EXP.\n\
2677 Variables accessible are those of the lexical environment of the selected\n\
2678 stack frame, plus all those whose scope is global or an entire file.\n\
2680 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2681 $$NUM refers to NUM'th value back from the last one.\n\
2682 Names starting with $ refer to registers (with the values they would have\n\
2683 if the program were to return to the stack frame now selected, restoring\n\
2684 all registers saved by frames farther in) or else to debugger\n\
2685 \"convenience\" variables (any such name not a known register).\n\
2686 Use assignment expressions to give values to convenience variables.\n\
2688 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2689 @ is a binary operator for treating consecutive data objects\n\
2690 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2691 element is FOO, whose second element is stored in the space following\n\
2692 where FOO is stored, etc. FOO must be an expression whose value\n\
2693 resides in memory.\n\
2695 EXP may be preceded with /FMT, where FMT is a format letter\n\
2696 but no count or size letter (see \"x\" command)."));
2697 set_cmd_completer (c
, expression_completer
);
2698 add_com_alias ("p", "print", class_vars
, 1);
2700 c
= add_com ("inspect", class_vars
, inspect_command
, _("\
2701 Same as \"print\" command, except that if you are running in the epoch\n\
2702 environment, the value is printed in its own window."));
2703 set_cmd_completer (c
, expression_completer
);
2705 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2706 &max_symbolic_offset
, _("\
2707 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2708 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2710 show_max_symbolic_offset
,
2711 &setprintlist
, &showprintlist
);
2712 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2713 &print_symbol_filename
, _("\
2714 Set printing of source filename and line number with <symbol>."), _("\
2715 Show printing of source filename and line number with <symbol>."), NULL
,
2717 show_print_symbol_filename
,
2718 &setprintlist
, &showprintlist
);
2720 /* For examine/instruction a single byte quantity is specified as
2721 the data. This avoids problems with value_at_lazy() requiring a
2722 valid data type (and rejecting VOID). */
2723 examine_i_type
= init_type (TYPE_CODE_INT
, 1, 0, "examine_i_type", NULL
);
2725 examine_b_type
= init_type (TYPE_CODE_INT
, 1, 0, "examine_b_type", NULL
);
2726 examine_h_type
= init_type (TYPE_CODE_INT
, 2, 0, "examine_h_type", NULL
);
2727 examine_w_type
= init_type (TYPE_CODE_INT
, 4, 0, "examine_w_type", NULL
);
2728 examine_g_type
= init_type (TYPE_CODE_INT
, 8, 0, "examine_g_type", NULL
);