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, and associated architecture. */
87 static struct gdbarch
*next_gdbarch
;
88 static CORE_ADDR next_address
;
90 /* Number of delay instructions following current disassembled insn. */
92 static int branch_delay_insns
;
94 /* Last address examined. */
96 static CORE_ADDR last_examine_address
;
98 /* Contents of last address examined.
99 This is not valid past the end of the `x' command! */
101 static struct value
*last_examine_value
;
103 /* Largest offset between a symbolic value and an address, that will be
104 printed as `0x1234 <symbol+offset>'. */
106 static unsigned int max_symbolic_offset
= UINT_MAX
;
108 show_max_symbolic_offset (struct ui_file
*file
, int from_tty
,
109 struct cmd_list_element
*c
, const char *value
)
111 fprintf_filtered (file
, _("\
112 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
116 /* Append the source filename and linenumber of the symbol when
117 printing a symbolic value as `<symbol at filename:linenum>' if set. */
118 static int print_symbol_filename
= 0;
120 show_print_symbol_filename (struct ui_file
*file
, int from_tty
,
121 struct cmd_list_element
*c
, const char *value
)
123 fprintf_filtered (file
, _("\
124 Printing of source filename and line number with <symbol> is %s.\n"),
128 /* Number of auto-display expression currently being displayed.
129 So that we can disable it if we get an error or a signal within it.
130 -1 when not doing one. */
132 int current_display_number
;
136 /* Chain link to next auto-display item. */
137 struct display
*next
;
138 /* The expression as the user typed it. */
140 /* Expression to be evaluated and displayed. */
141 struct expression
*exp
;
142 /* Item number of this auto-display item. */
144 /* Display format specified. */
145 struct format_data format
;
146 /* Innermost block required by this expression when evaluated */
148 /* Status of this display (enabled or disabled) */
152 /* Chain of expressions whose values should be displayed
153 automatically each time the program stops. */
155 static struct display
*display_chain
;
157 static int display_number
;
159 /* Prototypes for exported functions. */
161 void output_command (char *, int);
163 void _initialize_printcmd (void);
165 /* Prototypes for local functions. */
167 static void do_one_display (struct display
*);
170 /* Decode a format specification. *STRING_PTR should point to it.
171 OFORMAT and OSIZE are used as defaults for the format and size
172 if none are given in the format specification.
173 If OSIZE is zero, then the size field of the returned value
174 should be set only if a size is explicitly specified by the
176 The structure returned describes all the data
177 found in the specification. In addition, *STRING_PTR is advanced
178 past the specification and past all whitespace following it. */
180 static struct format_data
181 decode_format (char **string_ptr
, int oformat
, int osize
)
183 struct format_data val
;
184 char *p
= *string_ptr
;
191 if (*p
>= '0' && *p
<= '9')
192 val
.count
= atoi (p
);
193 while (*p
>= '0' && *p
<= '9')
196 /* Now process size or format letters that follow. */
200 if (*p
== 'b' || *p
== 'h' || *p
== 'w' || *p
== 'g')
207 else if (*p
>= 'a' && *p
<= 'z')
213 while (*p
== ' ' || *p
== '\t')
217 /* Set defaults for format and size if not specified. */
218 if (val
.format
== '?')
222 /* Neither has been specified. */
223 val
.format
= oformat
;
227 /* If a size is specified, any format makes a reasonable
228 default except 'i'. */
229 val
.format
= oformat
== 'i' ? 'x' : oformat
;
231 else if (val
.size
== '?')
235 /* Pick the appropriate size for an address. This is deferred
236 until do_examine when we know the actual architecture to use.
237 A special size value of 'a' is used to indicate this case. */
238 val
.size
= osize
? 'a' : osize
;
241 /* Floating point has to be word or giantword. */
242 if (osize
== 'w' || osize
== 'g')
245 /* Default it to giantword if the last used size is not
247 val
.size
= osize
? 'g' : osize
;
250 /* Characters default to one byte. */
251 val
.size
= osize
? 'b' : osize
;
254 /* The default is the size most recently specified. */
261 /* Print value VAL on stream according to OPTIONS.
262 Do not end with a newline.
263 SIZE is the letter for the size of datum being printed.
264 This is used to pad hex numbers so they line up. SIZE is 0
265 for print / output and set for examine. */
268 print_formatted (struct value
*val
, int size
,
269 const struct value_print_options
*options
,
270 struct ui_file
*stream
)
272 struct type
*type
= check_typedef (value_type (val
));
273 int len
= TYPE_LENGTH (type
);
275 if (VALUE_LVAL (val
) == lval_memory
)
276 next_address
= value_address (val
) + len
;
280 switch (options
->format
)
284 struct type
*elttype
= value_type (val
);
285 next_address
= (value_address (val
)
286 + val_print_string (elttype
,
287 value_address (val
), -1,
293 /* We often wrap here if there are long symbolic names. */
295 next_address
= (value_address (val
)
296 + gdb_print_insn (get_type_arch (type
),
297 value_address (val
), stream
,
298 &branch_delay_insns
));
303 if (options
->format
== 0 || options
->format
== 's'
304 || TYPE_CODE (type
) == TYPE_CODE_REF
305 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
306 || TYPE_CODE (type
) == TYPE_CODE_STRING
307 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
308 || TYPE_CODE (type
) == TYPE_CODE_UNION
309 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
310 value_print (val
, stream
, options
);
312 /* User specified format, so don't look to the the type to
313 tell us what to do. */
314 print_scalar_formatted (value_contents (val
), type
,
315 options
, size
, stream
);
318 /* Return builtin floating point type of same length as TYPE.
319 If no such type is found, return TYPE itself. */
321 float_type_from_length (struct type
*type
)
323 struct gdbarch
*gdbarch
= get_type_arch (type
);
324 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
325 unsigned int len
= TYPE_LENGTH (type
);
327 if (len
== TYPE_LENGTH (builtin
->builtin_float
))
328 type
= builtin
->builtin_float
;
329 else if (len
== TYPE_LENGTH (builtin
->builtin_double
))
330 type
= builtin
->builtin_double
;
331 else if (len
== TYPE_LENGTH (builtin
->builtin_long_double
))
332 type
= builtin
->builtin_long_double
;
337 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
338 according to OPTIONS and SIZE on STREAM.
339 Formats s and i are not supported at this level.
341 This is how the elements of an array or structure are printed
345 print_scalar_formatted (const void *valaddr
, struct type
*type
,
346 const struct value_print_options
*options
,
347 int size
, struct ui_file
*stream
)
349 struct gdbarch
*gdbarch
= get_type_arch (type
);
350 LONGEST val_long
= 0;
351 unsigned int len
= TYPE_LENGTH (type
);
352 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
354 /* If we get here with a string format, try again without it. Go
355 all the way back to the language printers, which may call us
357 if (options
->format
== 's')
359 struct value_print_options opts
= *options
;
362 val_print (type
, valaddr
, 0, 0, stream
, 0, &opts
,
367 if (len
> sizeof(LONGEST
) &&
368 (TYPE_CODE (type
) == TYPE_CODE_INT
369 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
371 switch (options
->format
)
374 print_octal_chars (stream
, valaddr
, len
, byte_order
);
378 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
381 print_binary_chars (stream
, valaddr
, len
, byte_order
);
384 print_hex_chars (stream
, valaddr
, len
, byte_order
);
387 print_char_chars (stream
, type
, valaddr
, len
, byte_order
);
394 if (options
->format
!= 'f')
395 val_long
= unpack_long (type
, valaddr
);
397 /* If the value is a pointer, and pointers and addresses are not the
398 same, then at this point, the value's length (in target bytes) is
399 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
400 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
401 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
403 /* If we are printing it as unsigned, truncate it in case it is actually
404 a negative signed value (e.g. "print/u (short)-1" should print 65535
405 (if shorts are 16 bits) instead of 4294967295). */
406 if (options
->format
!= 'd' || TYPE_UNSIGNED (type
))
408 if (len
< sizeof (LONGEST
))
409 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
412 switch (options
->format
)
417 /* No size specified, like in print. Print varying # of digits. */
418 print_longest (stream
, 'x', 1, val_long
);
427 print_longest (stream
, size
, 1, val_long
);
430 error (_("Undefined output size \"%c\"."), size
);
435 print_longest (stream
, 'd', 1, val_long
);
439 print_longest (stream
, 'u', 0, val_long
);
444 print_longest (stream
, 'o', 1, val_long
);
446 fprintf_filtered (stream
, "0");
451 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
452 print_address (gdbarch
, addr
, stream
);
458 struct value_print_options opts
= *options
;
461 if (TYPE_UNSIGNED (type
))
462 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
464 type
= builtin_type (gdbarch
)->builtin_true_char
;
466 value_print (value_from_longest (type
, val_long
), stream
, &opts
);
471 type
= float_type_from_length (type
);
472 print_floating (valaddr
, type
, stream
);
476 internal_error (__FILE__
, __LINE__
,
477 _("failed internal consistency check"));
480 /* Binary; 't' stands for "two". */
482 char bits
[8 * (sizeof val_long
) + 1];
483 char buf
[8 * (sizeof val_long
) + 32];
488 width
= 8 * (sizeof val_long
);
505 error (_("Undefined output size \"%c\"."), size
);
511 bits
[width
] = (val_long
& 1) ? '1' : '0';
516 while (*cp
&& *cp
== '0')
522 fputs_filtered (buf
, stream
);
527 error (_("Undefined output format \"%c\"."), options
->format
);
531 /* Specify default address for `x' command.
532 The `info lines' command uses this. */
535 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
537 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
539 next_gdbarch
= gdbarch
;
542 /* Make address available to the user as $_. */
543 set_internalvar (lookup_internalvar ("_"),
544 value_from_pointer (ptr_type
, addr
));
547 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
548 after LEADIN. Print nothing if no symbolic name is found nearby.
549 Optionally also print source file and line number, if available.
550 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
551 or to interpret it as a possible C++ name and convert it back to source
552 form. However note that DO_DEMANGLE can be overridden by the specific
553 settings of the demangle and asm_demangle variables. */
556 print_address_symbolic (CORE_ADDR addr
, struct ui_file
*stream
,
557 int do_demangle
, char *leadin
)
560 char *filename
= NULL
;
565 /* Throw away both name and filename. */
566 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
567 make_cleanup (free_current_contents
, &filename
);
569 if (build_address_symbolic (addr
, do_demangle
, &name
, &offset
,
570 &filename
, &line
, &unmapped
))
572 do_cleanups (cleanup_chain
);
576 fputs_filtered (leadin
, stream
);
578 fputs_filtered ("<*", stream
);
580 fputs_filtered ("<", stream
);
581 fputs_filtered (name
, stream
);
583 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
585 /* Append source filename and line number if desired. Give specific
586 line # of this addr, if we have it; else line # of the nearest symbol. */
587 if (print_symbol_filename
&& filename
!= NULL
)
590 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
592 fprintf_filtered (stream
, " in %s", filename
);
595 fputs_filtered ("*>", stream
);
597 fputs_filtered (">", stream
);
599 do_cleanups (cleanup_chain
);
602 /* Given an address ADDR return all the elements needed to print the
603 address in a symbolic form. NAME can be mangled or not depending
604 on DO_DEMANGLE (and also on the asm_demangle global variable,
605 manipulated via ''set print asm-demangle''). Return 0 in case of
606 success, when all the info in the OUT paramters is valid. Return 1
609 build_address_symbolic (CORE_ADDR addr
, /* IN */
610 int do_demangle
, /* IN */
611 char **name
, /* OUT */
612 int *offset
, /* OUT */
613 char **filename
, /* OUT */
615 int *unmapped
) /* OUT */
617 struct minimal_symbol
*msymbol
;
618 struct symbol
*symbol
;
619 CORE_ADDR name_location
= 0;
620 struct obj_section
*section
= NULL
;
621 char *name_temp
= "";
623 /* Let's say it is mapped (not unmapped). */
626 /* Determine if the address is in an overlay, and whether it is
628 if (overlay_debugging
)
630 section
= find_pc_overlay (addr
);
631 if (pc_in_unmapped_range (addr
, section
))
634 addr
= overlay_mapped_address (addr
, section
);
638 /* First try to find the address in the symbol table, then
639 in the minsyms. Take the closest one. */
641 /* This is defective in the sense that it only finds text symbols. So
642 really this is kind of pointless--we should make sure that the
643 minimal symbols have everything we need (by changing that we could
644 save some memory, but for many debug format--ELF/DWARF or
645 anything/stabs--it would be inconvenient to eliminate those minimal
647 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
648 symbol
= find_pc_sect_function (addr
, section
);
652 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
653 if (do_demangle
|| asm_demangle
)
654 name_temp
= SYMBOL_PRINT_NAME (symbol
);
656 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
661 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
663 /* The msymbol is closer to the address than the symbol;
664 use the msymbol instead. */
666 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
667 if (do_demangle
|| asm_demangle
)
668 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
670 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
673 if (symbol
== NULL
&& msymbol
== NULL
)
676 /* If the nearest symbol is too far away, don't print anything symbolic. */
678 /* For when CORE_ADDR is larger than unsigned int, we do math in
679 CORE_ADDR. But when we detect unsigned wraparound in the
680 CORE_ADDR math, we ignore this test and print the offset,
681 because addr+max_symbolic_offset has wrapped through the end
682 of the address space back to the beginning, giving bogus comparison. */
683 if (addr
> name_location
+ max_symbolic_offset
684 && name_location
+ max_symbolic_offset
> name_location
)
687 *offset
= addr
- name_location
;
689 *name
= xstrdup (name_temp
);
691 if (print_symbol_filename
)
693 struct symtab_and_line sal
;
695 sal
= find_pc_sect_line (addr
, section
, 0);
699 *filename
= xstrdup (sal
.symtab
->filename
);
707 /* Print address ADDR symbolically on STREAM.
708 First print it as a number. Then perhaps print
709 <SYMBOL + OFFSET> after the number. */
712 print_address (struct gdbarch
*gdbarch
,
713 CORE_ADDR addr
, struct ui_file
*stream
)
715 fputs_filtered (paddress (gdbarch
, addr
), stream
);
716 print_address_symbolic (addr
, stream
, asm_demangle
, " ");
719 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
720 controls whether to print the symbolic name "raw" or demangled.
721 Global setting "addressprint" controls whether to print hex address
725 print_address_demangle (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
726 struct ui_file
*stream
, int do_demangle
)
728 struct value_print_options opts
;
729 get_user_print_options (&opts
);
732 fprintf_filtered (stream
, "0");
734 else if (opts
.addressprint
)
736 fputs_filtered (paddress (gdbarch
, addr
), stream
);
737 print_address_symbolic (addr
, stream
, do_demangle
, " ");
741 print_address_symbolic (addr
, stream
, do_demangle
, "");
746 /* Examine data at address ADDR in format FMT.
747 Fetch it from memory and print on gdb_stdout. */
750 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
755 struct type
*val_type
= NULL
;
758 struct value_print_options opts
;
763 next_gdbarch
= gdbarch
;
766 /* String or instruction format implies fetch single bytes
767 regardless of the specified size. */
768 if (format
== 's' || format
== 'i')
773 /* Pick the appropriate size for an address. */
774 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
776 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
778 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
781 /* Bad value for gdbarch_ptr_bit. */
782 internal_error (__FILE__
, __LINE__
,
783 _("failed internal consistency check"));
787 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
788 else if (size
== 'h')
789 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
790 else if (size
== 'w')
791 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
792 else if (size
== 'g')
793 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
800 if (format
== 's' || format
== 'i')
803 get_formatted_print_options (&opts
, format
);
805 /* Print as many objects as specified in COUNT, at most maxelts per line,
806 with the address of the next one at the start of each line. */
811 print_address (next_gdbarch
, next_address
, gdb_stdout
);
812 printf_filtered (":");
817 printf_filtered ("\t");
818 /* Note that print_formatted sets next_address for the next
820 last_examine_address
= next_address
;
822 if (last_examine_value
)
823 value_free (last_examine_value
);
825 /* The value to be displayed is not fetched greedily.
826 Instead, to avoid the possibility of a fetched value not
827 being used, its retrieval is delayed until the print code
828 uses it. When examining an instruction stream, the
829 disassembler will perform its own memory fetch using just
830 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
831 the disassembler be modified so that LAST_EXAMINE_VALUE
832 is left with the byte sequence from the last complete
833 instruction fetched from memory? */
834 last_examine_value
= value_at_lazy (val_type
, next_address
);
836 if (last_examine_value
)
837 release_value (last_examine_value
);
839 print_formatted (last_examine_value
, size
, &opts
, gdb_stdout
);
841 /* Display any branch delay slots following the final insn. */
842 if (format
== 'i' && count
== 1)
843 count
+= branch_delay_insns
;
845 printf_filtered ("\n");
846 gdb_flush (gdb_stdout
);
851 validate_format (struct format_data fmt
, char *cmdname
)
854 error (_("Size letters are meaningless in \"%s\" command."), cmdname
);
856 error (_("Item count other than 1 is meaningless in \"%s\" command."),
858 if (fmt
.format
== 'i')
859 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
860 fmt
.format
, cmdname
);
863 /* Evaluate string EXP as an expression in the current language and
864 print the resulting value. EXP may contain a format specifier as the
865 first argument ("/x myvar" for example, to print myvar in hex). */
868 print_command_1 (char *exp
, int inspect
, int voidprint
)
870 struct expression
*expr
;
871 struct cleanup
*old_chain
= 0;
874 struct format_data fmt
;
877 if (exp
&& *exp
== '/')
880 fmt
= decode_format (&exp
, last_format
, 0);
881 validate_format (fmt
, "print");
882 last_format
= format
= fmt
.format
;
895 expr
= parse_expression (exp
);
896 old_chain
= make_cleanup (free_current_contents
, &expr
);
898 val
= evaluate_expression (expr
);
901 val
= access_value_history (0);
903 if (voidprint
|| (val
&& value_type (val
) &&
904 TYPE_CODE (value_type (val
)) != TYPE_CODE_VOID
))
906 struct value_print_options opts
;
907 int histindex
= record_latest_value (val
);
910 annotate_value_history_begin (histindex
, value_type (val
));
912 annotate_value_begin (value_type (val
));
915 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
917 else if (histindex
>= 0)
918 printf_filtered ("$%d = ", histindex
);
921 annotate_value_history_value ();
923 get_formatted_print_options (&opts
, format
);
924 opts
.inspect_it
= inspect
;
927 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
928 printf_filtered ("\n");
931 annotate_value_history_end ();
933 annotate_value_end ();
936 printf_unfiltered ("\") )\030");
940 do_cleanups (old_chain
);
944 print_command (char *exp
, int from_tty
)
946 print_command_1 (exp
, 0, 1);
949 /* Same as print, except in epoch, it gets its own window. */
951 inspect_command (char *exp
, int from_tty
)
953 extern int epoch_interface
;
955 print_command_1 (exp
, epoch_interface
, 1);
958 /* Same as print, except it doesn't print void results. */
960 call_command (char *exp
, int from_tty
)
962 print_command_1 (exp
, 0, 0);
966 output_command (char *exp
, int from_tty
)
968 struct expression
*expr
;
969 struct cleanup
*old_chain
;
972 struct format_data fmt
;
973 struct value_print_options opts
;
978 if (exp
&& *exp
== '/')
981 fmt
= decode_format (&exp
, 0, 0);
982 validate_format (fmt
, "output");
986 expr
= parse_expression (exp
);
987 old_chain
= make_cleanup (free_current_contents
, &expr
);
989 val
= evaluate_expression (expr
);
991 annotate_value_begin (value_type (val
));
993 get_formatted_print_options (&opts
, format
);
995 print_formatted (val
, fmt
.size
, &opts
, gdb_stdout
);
997 annotate_value_end ();
1000 gdb_flush (gdb_stdout
);
1002 do_cleanups (old_chain
);
1006 set_command (char *exp
, int from_tty
)
1008 struct expression
*expr
= parse_expression (exp
);
1009 struct cleanup
*old_chain
=
1010 make_cleanup (free_current_contents
, &expr
);
1011 evaluate_expression (expr
);
1012 do_cleanups (old_chain
);
1016 sym_info (char *arg
, int from_tty
)
1018 struct minimal_symbol
*msymbol
;
1019 struct objfile
*objfile
;
1020 struct obj_section
*osect
;
1021 CORE_ADDR addr
, sect_addr
;
1023 unsigned int offset
;
1026 error_no_arg (_("address"));
1028 addr
= parse_and_eval_address (arg
);
1029 ALL_OBJSECTIONS (objfile
, osect
)
1031 /* Only process each object file once, even if there's a separate
1033 if (objfile
->separate_debug_objfile_backlink
)
1036 sect_addr
= overlay_mapped_address (addr
, osect
);
1038 if (obj_section_addr (osect
) <= sect_addr
1039 && sect_addr
< obj_section_endaddr (osect
)
1040 && (msymbol
= lookup_minimal_symbol_by_pc_section (sect_addr
, osect
)))
1042 const char *obj_name
, *mapped
, *sec_name
, *msym_name
;
1044 struct cleanup
*old_chain
;
1047 offset
= sect_addr
- SYMBOL_VALUE_ADDRESS (msymbol
);
1048 mapped
= section_is_mapped (osect
) ? _("mapped") : _("unmapped");
1049 sec_name
= osect
->the_bfd_section
->name
;
1050 msym_name
= SYMBOL_PRINT_NAME (msymbol
);
1052 /* Don't print the offset if it is zero.
1053 We assume there's no need to handle i18n of "sym + offset". */
1055 loc_string
= xstrprintf ("%s + %u", msym_name
, offset
);
1057 loc_string
= xstrprintf ("%s", msym_name
);
1059 /* Use a cleanup to free loc_string in case the user quits
1060 a pagination request inside printf_filtered. */
1061 old_chain
= make_cleanup (xfree
, loc_string
);
1063 gdb_assert (osect
->objfile
&& osect
->objfile
->name
);
1064 obj_name
= osect
->objfile
->name
;
1066 if (MULTI_OBJFILE_P ())
1067 if (pc_in_unmapped_range (addr
, osect
))
1068 if (section_is_overlay (osect
))
1069 printf_filtered (_("%s in load address range of "
1070 "%s overlay section %s of %s\n"),
1071 loc_string
, mapped
, sec_name
, obj_name
);
1073 printf_filtered (_("%s in load address range of "
1074 "section %s of %s\n"),
1075 loc_string
, sec_name
, obj_name
);
1077 if (section_is_overlay (osect
))
1078 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1079 loc_string
, mapped
, sec_name
, obj_name
);
1081 printf_filtered (_("%s in section %s of %s\n"),
1082 loc_string
, sec_name
, obj_name
);
1084 if (pc_in_unmapped_range (addr
, osect
))
1085 if (section_is_overlay (osect
))
1086 printf_filtered (_("%s in load address range of %s overlay "
1088 loc_string
, mapped
, sec_name
);
1090 printf_filtered (_("%s in load address range of section %s\n"),
1091 loc_string
, sec_name
);
1093 if (section_is_overlay (osect
))
1094 printf_filtered (_("%s in %s overlay section %s\n"),
1095 loc_string
, mapped
, sec_name
);
1097 printf_filtered (_("%s in section %s\n"),
1098 loc_string
, sec_name
);
1100 do_cleanups (old_chain
);
1104 printf_filtered (_("No symbol matches %s.\n"), arg
);
1108 address_info (char *exp
, int from_tty
)
1110 struct gdbarch
*gdbarch
;
1113 struct minimal_symbol
*msymbol
;
1115 struct obj_section
*section
;
1116 CORE_ADDR load_addr
;
1117 int is_a_field_of_this
; /* C++: lookup_symbol sets this to nonzero
1118 if exp is a field of `this'. */
1121 error (_("Argument required."));
1123 sym
= lookup_symbol (exp
, get_selected_block (0), VAR_DOMAIN
,
1124 &is_a_field_of_this
);
1127 if (is_a_field_of_this
)
1129 printf_filtered ("Symbol \"");
1130 fprintf_symbol_filtered (gdb_stdout
, exp
,
1131 current_language
->la_language
, DMGL_ANSI
);
1132 printf_filtered ("\" is a field of the local class variable ");
1133 if (current_language
->la_language
== language_objc
)
1134 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1136 printf_filtered ("`this'\n");
1140 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1142 if (msymbol
!= NULL
)
1144 gdbarch
= get_objfile_arch (msymbol_objfile (msymbol
));
1145 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1147 printf_filtered ("Symbol \"");
1148 fprintf_symbol_filtered (gdb_stdout
, exp
,
1149 current_language
->la_language
, DMGL_ANSI
);
1150 printf_filtered ("\" is at ");
1151 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1152 printf_filtered (" in a file compiled without debugging");
1153 section
= SYMBOL_OBJ_SECTION (msymbol
);
1154 if (section_is_overlay (section
))
1156 load_addr
= overlay_unmapped_address (load_addr
, section
);
1157 printf_filtered (",\n -- loaded at ");
1158 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1159 printf_filtered (" in overlay section %s",
1160 section
->the_bfd_section
->name
);
1162 printf_filtered (".\n");
1165 error (_("No symbol \"%s\" in current context."), exp
);
1169 printf_filtered ("Symbol \"");
1170 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1171 current_language
->la_language
, DMGL_ANSI
);
1172 printf_filtered ("\" is ");
1173 val
= SYMBOL_VALUE (sym
);
1174 section
= SYMBOL_OBJ_SECTION (sym
);
1175 gdbarch
= get_objfile_arch (SYMBOL_SYMTAB (sym
)->objfile
);
1177 switch (SYMBOL_CLASS (sym
))
1180 case LOC_CONST_BYTES
:
1181 printf_filtered ("constant");
1185 printf_filtered ("a label at address ");
1186 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1187 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1188 if (section_is_overlay (section
))
1190 load_addr
= overlay_unmapped_address (load_addr
, section
);
1191 printf_filtered (",\n -- loaded at ");
1192 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1193 printf_filtered (" in overlay section %s",
1194 section
->the_bfd_section
->name
);
1199 /* FIXME: cagney/2004-01-26: It should be possible to
1200 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1201 Unfortunately DWARF 2 stores the frame-base (instead of the
1202 function) location in a function's symbol. Oops! For the
1203 moment enable this when/where applicable. */
1204 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, gdb_stdout
);
1208 /* GDBARCH is the architecture associated with the objfile the symbol
1209 is defined in; the target architecture may be different, and may
1210 provide additional registers. However, we do not know the target
1211 architecture at this point. We assume the objfile architecture
1212 will contain all the standard registers that occur in debug info
1214 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1216 if (SYMBOL_IS_ARGUMENT (sym
))
1217 printf_filtered (_("an argument in register %s"),
1218 gdbarch_register_name (gdbarch
, regno
));
1220 printf_filtered (_("a variable in register %s"),
1221 gdbarch_register_name (gdbarch
, regno
));
1225 printf_filtered (_("static storage at address "));
1226 load_addr
= SYMBOL_VALUE_ADDRESS (sym
);
1227 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1228 if (section_is_overlay (section
))
1230 load_addr
= overlay_unmapped_address (load_addr
, section
);
1231 printf_filtered (_(",\n -- loaded at "));
1232 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1233 printf_filtered (_(" in overlay section %s"),
1234 section
->the_bfd_section
->name
);
1238 case LOC_REGPARM_ADDR
:
1239 /* Note comment at LOC_REGISTER. */
1240 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1241 printf_filtered (_("address of an argument in register %s"),
1242 gdbarch_register_name (gdbarch
, regno
));
1246 printf_filtered (_("an argument at offset %ld"), val
);
1250 printf_filtered (_("a local variable at frame offset %ld"), val
);
1254 printf_filtered (_("a reference argument at offset %ld"), val
);
1258 printf_filtered (_("a typedef"));
1262 printf_filtered (_("a function at address "));
1263 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1264 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1265 if (section_is_overlay (section
))
1267 load_addr
= overlay_unmapped_address (load_addr
, section
);
1268 printf_filtered (_(",\n -- loaded at "));
1269 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1270 printf_filtered (_(" in overlay section %s"),
1271 section
->the_bfd_section
->name
);
1275 case LOC_UNRESOLVED
:
1277 struct minimal_symbol
*msym
;
1279 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1281 printf_filtered ("unresolved");
1284 section
= SYMBOL_OBJ_SECTION (msym
);
1285 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1288 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1289 printf_filtered (_("a thread-local variable at offset %s "
1290 "in the thread-local storage for `%s'"),
1291 paddress (gdbarch
, load_addr
),
1292 section
->objfile
->name
);
1295 printf_filtered (_("static storage at address "));
1296 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1297 if (section_is_overlay (section
))
1299 load_addr
= overlay_unmapped_address (load_addr
, section
);
1300 printf_filtered (_(",\n -- loaded at "));
1301 fputs_filtered (paddress (gdbarch
, load_addr
), gdb_stdout
);
1302 printf_filtered (_(" in overlay section %s"),
1303 section
->the_bfd_section
->name
);
1310 case LOC_OPTIMIZED_OUT
:
1311 printf_filtered (_("optimized out"));
1315 printf_filtered (_("of unknown (botched) type"));
1318 printf_filtered (".\n");
1323 x_command (char *exp
, int from_tty
)
1325 struct expression
*expr
;
1326 struct format_data fmt
;
1327 struct cleanup
*old_chain
;
1330 fmt
.format
= last_format
? last_format
: 'x';
1331 fmt
.size
= last_size
;
1335 if (exp
&& *exp
== '/')
1338 fmt
= decode_format (&exp
, last_format
, last_size
);
1341 /* If we have an expression, evaluate it and use it as the address. */
1343 if (exp
!= 0 && *exp
!= 0)
1345 expr
= parse_expression (exp
);
1346 /* Cause expression not to be there any more if this command is
1347 repeated with Newline. But don't clobber a user-defined
1348 command's definition. */
1351 old_chain
= make_cleanup (free_current_contents
, &expr
);
1352 val
= evaluate_expression (expr
);
1353 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1354 val
= value_ind (val
);
1355 /* In rvalue contexts, such as this, functions are coerced into
1356 pointers to functions. This makes "x/i main" work. */
1357 if (/* last_format == 'i' && */
1358 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1359 && VALUE_LVAL (val
) == lval_memory
)
1360 next_address
= value_address (val
);
1362 next_address
= value_as_address (val
);
1364 next_gdbarch
= expr
->gdbarch
;
1365 do_cleanups (old_chain
);
1369 error_no_arg (_("starting display address"));
1371 do_examine (fmt
, next_gdbarch
, next_address
);
1373 /* If the examine succeeds, we remember its size and format for next
1375 last_size
= fmt
.size
;
1376 last_format
= fmt
.format
;
1378 /* Set a couple of internal variables if appropriate. */
1379 if (last_examine_value
)
1381 /* Make last address examined available to the user as $_. Use
1382 the correct pointer type. */
1383 struct type
*pointer_type
1384 = lookup_pointer_type (value_type (last_examine_value
));
1385 set_internalvar (lookup_internalvar ("_"),
1386 value_from_pointer (pointer_type
,
1387 last_examine_address
));
1389 /* Make contents of last address examined available to the user
1390 as $__. If the last value has not been fetched from memory
1391 then don't fetch it now; instead mark it by voiding the $__
1393 if (value_lazy (last_examine_value
))
1394 clear_internalvar (lookup_internalvar ("__"));
1396 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1401 /* Add an expression to the auto-display chain.
1402 Specify the expression. */
1405 display_command (char *exp
, int from_tty
)
1407 struct format_data fmt
;
1408 struct expression
*expr
;
1409 struct display
*new;
1413 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1415 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1416 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1430 fmt
= decode_format (&exp
, 0, 0);
1431 if (fmt
.size
&& fmt
.format
== 0)
1433 if (fmt
.format
== 'i' || fmt
.format
== 's')
1444 innermost_block
= NULL
;
1445 expr
= parse_expression (exp
);
1447 new = (struct display
*) xmalloc (sizeof (struct display
));
1449 new->exp_string
= xstrdup (exp
);
1451 new->block
= innermost_block
;
1452 new->next
= display_chain
;
1453 new->number
= ++display_number
;
1456 display_chain
= new;
1458 if (from_tty
&& target_has_execution
)
1459 do_one_display (new);
1466 free_display (struct display
*d
)
1468 xfree (d
->exp_string
);
1473 /* Clear out the display_chain. Done when new symtabs are loaded,
1474 since this invalidates the types stored in many expressions. */
1477 clear_displays (void)
1481 while ((d
= display_chain
) != NULL
)
1483 display_chain
= d
->next
;
1488 /* Delete the auto-display number NUM. */
1491 delete_display (int num
)
1493 struct display
*d1
, *d
;
1496 error (_("No display number %d."), num
);
1498 if (display_chain
->number
== num
)
1501 display_chain
= d1
->next
;
1505 for (d
= display_chain
;; d
= d
->next
)
1508 error (_("No display number %d."), num
);
1509 if (d
->next
->number
== num
)
1519 /* Delete some values from the auto-display chain.
1520 Specify the element numbers. */
1523 undisplay_command (char *args
, int from_tty
)
1531 if (query (_("Delete all auto-display expressions? ")))
1540 while (*p1
>= '0' && *p1
<= '9')
1542 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1543 error (_("Arguments must be display numbers."));
1547 delete_display (num
);
1550 while (*p
== ' ' || *p
== '\t')
1556 /* Display a single auto-display.
1557 Do nothing if the display cannot be printed in the current context,
1558 or if the display is disabled. */
1561 do_one_display (struct display
*d
)
1563 int within_current_scope
;
1565 if (d
->enabled_p
== 0)
1570 volatile struct gdb_exception ex
;
1571 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1573 innermost_block
= NULL
;
1574 d
->exp
= parse_expression (d
->exp_string
);
1575 d
->block
= innermost_block
;
1579 /* Can't re-parse the expression. Disable this display item. */
1581 warning (_("Unable to display \"%s\": %s"),
1582 d
->exp_string
, ex
.message
);
1588 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1590 within_current_scope
= 1;
1591 if (!within_current_scope
)
1594 current_display_number
= d
->number
;
1596 annotate_display_begin ();
1597 printf_filtered ("%d", d
->number
);
1598 annotate_display_number_end ();
1599 printf_filtered (": ");
1605 annotate_display_format ();
1607 printf_filtered ("x/");
1608 if (d
->format
.count
!= 1)
1609 printf_filtered ("%d", d
->format
.count
);
1610 printf_filtered ("%c", d
->format
.format
);
1611 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1612 printf_filtered ("%c", d
->format
.size
);
1613 printf_filtered (" ");
1615 annotate_display_expression ();
1617 puts_filtered (d
->exp_string
);
1618 annotate_display_expression_end ();
1620 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1621 printf_filtered ("\n");
1623 printf_filtered (" ");
1625 val
= evaluate_expression (d
->exp
);
1626 addr
= value_as_address (val
);
1627 if (d
->format
.format
== 'i')
1628 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1630 annotate_display_value ();
1632 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1636 struct value_print_options opts
;
1638 annotate_display_format ();
1640 if (d
->format
.format
)
1641 printf_filtered ("/%c ", d
->format
.format
);
1643 annotate_display_expression ();
1645 puts_filtered (d
->exp_string
);
1646 annotate_display_expression_end ();
1648 printf_filtered (" = ");
1650 annotate_display_expression ();
1652 get_formatted_print_options (&opts
, d
->format
.format
);
1653 opts
.raw
= d
->format
.raw
;
1654 print_formatted (evaluate_expression (d
->exp
),
1655 d
->format
.size
, &opts
, gdb_stdout
);
1656 printf_filtered ("\n");
1659 annotate_display_end ();
1661 gdb_flush (gdb_stdout
);
1662 current_display_number
= -1;
1665 /* Display all of the values on the auto-display chain which can be
1666 evaluated in the current scope. */
1673 for (d
= display_chain
; d
; d
= d
->next
)
1677 /* Delete the auto-display which we were in the process of displaying.
1678 This is done when there is an error or a signal. */
1681 disable_display (int num
)
1685 for (d
= display_chain
; d
; d
= d
->next
)
1686 if (d
->number
== num
)
1691 printf_unfiltered (_("No display number %d.\n"), num
);
1695 disable_current_display (void)
1697 if (current_display_number
>= 0)
1699 disable_display (current_display_number
);
1700 fprintf_unfiltered (gdb_stderr
, _("\
1701 Disabling display %d to avoid infinite recursion.\n"),
1702 current_display_number
);
1704 current_display_number
= -1;
1708 display_info (char *ignore
, int from_tty
)
1713 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1715 printf_filtered (_("Auto-display expressions now in effect:\n\
1716 Num Enb Expression\n"));
1718 for (d
= display_chain
; d
; d
= d
->next
)
1720 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1722 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1724 else if (d
->format
.format
)
1725 printf_filtered ("/%c ", d
->format
.format
);
1726 puts_filtered (d
->exp_string
);
1727 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1728 printf_filtered (_(" (cannot be evaluated in the current context)"));
1729 printf_filtered ("\n");
1730 gdb_flush (gdb_stdout
);
1735 enable_display (char *args
, int from_tty
)
1744 for (d
= display_chain
; d
; d
= d
->next
)
1751 while (*p1
>= '0' && *p1
<= '9')
1753 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1754 error (_("Arguments must be display numbers."));
1758 for (d
= display_chain
; d
; d
= d
->next
)
1759 if (d
->number
== num
)
1764 printf_unfiltered (_("No display number %d.\n"), num
);
1767 while (*p
== ' ' || *p
== '\t')
1773 disable_display_command (char *args
, int from_tty
)
1781 for (d
= display_chain
; d
; d
= d
->next
)
1788 while (*p1
>= '0' && *p1
<= '9')
1790 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1791 error (_("Arguments must be display numbers."));
1793 disable_display (atoi (p
));
1796 while (*p
== ' ' || *p
== '\t')
1801 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1802 is unloaded), otherwise return 0. */
1805 display_uses_solib_p (const struct display
*d
,
1806 const struct so_list
*solib
)
1809 struct expression
*const exp
= d
->exp
;
1810 const union exp_element
*const elts
= exp
->elts
;
1812 if (d
->block
!= NULL
1813 && solib_contains_address_p (solib
, d
->block
->startaddr
))
1816 for (endpos
= exp
->nelts
; endpos
> 0; )
1818 int i
, args
, oplen
= 0;
1820 exp
->language_defn
->la_exp_desc
->operator_length (exp
, endpos
,
1822 gdb_assert (oplen
> 0);
1825 if (elts
[i
].opcode
== OP_VAR_VALUE
)
1827 const struct block
*const block
= elts
[i
+ 1].block
;
1828 const struct symbol
*const symbol
= elts
[i
+ 2].symbol
;
1829 const struct obj_section
*const section
=
1830 SYMBOL_OBJ_SECTION (symbol
);
1833 && solib_contains_address_p (solib
, block
->startaddr
))
1836 if (section
&& section
->objfile
== solib
->objfile
)
1845 /* display_chain items point to blocks and expressions. Some expressions in
1846 turn may point to symbols.
1847 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1848 obstack_free'd when a shared library is unloaded.
1849 Clear pointers that are about to become dangling.
1850 Both .exp and .block fields will be restored next time we need to display
1851 an item by re-parsing .exp_string field in the new execution context. */
1854 clear_dangling_display_expressions (struct so_list
*solib
)
1857 struct objfile
*objfile
= NULL
;
1859 for (d
= display_chain
; d
; d
= d
->next
)
1861 if (d
->exp
&& display_uses_solib_p (d
, solib
))
1871 /* Print the value in stack frame FRAME of a variable specified by a
1872 struct symbol. NAME is the name to print; if NULL then VAR's print
1873 name will be used. STREAM is the ui_file on which to print the
1874 value. INDENT specifies the number of indent levels to print
1875 before printing the variable name. */
1878 print_variable_and_value (const char *name
, struct symbol
*var
,
1879 struct frame_info
*frame
,
1880 struct ui_file
*stream
, int indent
)
1883 struct value_print_options opts
;
1886 name
= SYMBOL_PRINT_NAME (var
);
1888 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1890 val
= read_var_value (var
, frame
);
1891 get_user_print_options (&opts
);
1892 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1893 fprintf_filtered (stream
, "\n");
1897 printf_command (char *arg
, int from_tty
)
1901 char *string
= NULL
;
1902 struct value
**val_args
;
1904 char *current_substring
;
1906 int allocated_args
= 20;
1907 struct cleanup
*old_cleanups
;
1909 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1910 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1913 error_no_arg (_("format-control string and values to print"));
1915 /* Skip white space before format string */
1916 while (*s
== ' ' || *s
== '\t')
1919 /* A format string should follow, enveloped in double quotes. */
1921 error (_("Bad format string, missing '\"'."));
1923 /* Parse the format-control string and copy it into the string STRING,
1924 processing some kinds of escape sequence. */
1926 f
= string
= (char *) alloca (strlen (s
) + 1);
1934 error (_("Bad format string, non-terminated '\"'."));
1967 /* ??? TODO: handle other escape sequences */
1968 error (_("Unrecognized escape character \\%c in format string."),
1978 /* Skip over " and following space and comma. */
1981 while (*s
== ' ' || *s
== '\t')
1984 if (*s
!= ',' && *s
!= 0)
1985 error (_("Invalid argument syntax"));
1989 while (*s
== ' ' || *s
== '\t')
1992 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1993 substrings
= alloca (strlen (string
) * 2);
1994 current_substring
= substrings
;
1997 /* Now scan the string for %-specs and see what kinds of args they want.
1998 argclass[I] classifies the %-specs so we can give printf_filtered
1999 something of the right size. */
2003 int_arg
, long_arg
, long_long_arg
, ptr_arg
,
2004 string_arg
, wide_string_arg
, wide_char_arg
,
2005 double_arg
, long_double_arg
, decfloat_arg
2007 enum argclass
*argclass
;
2008 enum argclass this_argclass
;
2013 argclass
= (enum argclass
*) alloca (strlen (s
) * sizeof *argclass
);
2020 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
2021 int seen_space
= 0, seen_plus
= 0;
2022 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
2023 int seen_big_d
= 0, seen_double_big_d
= 0;
2026 /* Check the validity of the format specifier, and work
2027 out what argument it expects. We only accept C89
2028 format strings, with the exception of long long (which
2029 we autoconf for). */
2031 /* Skip over "%%". */
2038 /* The first part of a format specifier is a set of flag
2040 while (strchr ("0-+ #", *f
))
2053 /* The next part of a format specifier is a width. */
2054 while (strchr ("0123456789", *f
))
2057 /* The next part of a format specifier is a precision. */
2062 while (strchr ("0123456789", *f
))
2066 /* The next part of a format specifier is a length modifier. */
2087 /* Decimal32 modifier. */
2093 /* Decimal64 and Decimal128 modifiers. */
2098 /* Check for a Decimal128. */
2102 seen_double_big_d
= 1;
2118 if (seen_space
|| seen_plus
)
2125 this_argclass
= int_arg
;
2126 else if (lcount
== 1)
2127 this_argclass
= long_arg
;
2129 this_argclass
= long_long_arg
;
2136 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
2137 if (lcount
> 1 || seen_h
|| seen_big_l
)
2139 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2144 this_argclass
= ptr_arg
;
2145 if (lcount
|| seen_h
|| seen_big_l
)
2147 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2152 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
2153 if (lcount
> 1 || seen_h
|| seen_big_l
)
2155 if (seen_zero
|| seen_space
|| seen_plus
)
2164 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
2165 this_argclass
= decfloat_arg
;
2166 else if (seen_big_l
)
2167 this_argclass
= long_double_arg
;
2169 this_argclass
= double_arg
;
2171 if (lcount
|| seen_h
)
2176 error (_("`*' not supported for precision or width in printf"));
2179 error (_("Format specifier `n' not supported in printf"));
2182 error (_("Incomplete format specifier at end of format string"));
2185 error (_("Unrecognized format specifier '%c' in printf"), *f
);
2189 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2194 if (lcount
> 1 && USE_PRINTF_I64
)
2196 /* Windows' printf does support long long, but not the usual way.
2197 Convert %lld to %I64d. */
2198 int length_before_ll
= f
- last_arg
- 1 - lcount
;
2199 strncpy (current_substring
, last_arg
, length_before_ll
);
2200 strcpy (current_substring
+ length_before_ll
, "I64");
2201 current_substring
[length_before_ll
+ 3] =
2202 last_arg
[length_before_ll
+ lcount
];
2203 current_substring
+= length_before_ll
+ 4;
2205 else if (this_argclass
== wide_string_arg
2206 || this_argclass
== wide_char_arg
)
2208 /* Convert %ls or %lc to %s. */
2209 int length_before_ls
= f
- last_arg
- 2;
2210 strncpy (current_substring
, last_arg
, length_before_ls
);
2211 strcpy (current_substring
+ length_before_ls
, "s");
2212 current_substring
+= length_before_ls
+ 2;
2216 strncpy (current_substring
, last_arg
, f
- last_arg
);
2217 current_substring
+= f
- last_arg
;
2219 *current_substring
++ = '\0';
2221 argclass
[nargs_wanted
++] = this_argclass
;
2224 /* Now, parse all arguments and evaluate them.
2225 Store the VALUEs in VAL_ARGS. */
2230 if (nargs
== allocated_args
)
2231 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2232 (allocated_args
*= 2)
2233 * sizeof (struct value
*));
2235 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2243 if (nargs
!= nargs_wanted
)
2244 error (_("Wrong number of arguments for specified format-string"));
2246 /* Now actually print them. */
2247 current_substring
= substrings
;
2248 for (i
= 0; i
< nargs
; i
++)
2250 switch (argclass
[i
])
2257 tem
= value_as_address (val_args
[i
]);
2259 /* This is a %s argument. Find the length of the string. */
2264 read_memory (tem
+ j
, &c
, 1);
2269 /* Copy the string contents into a string inside GDB. */
2270 str
= (gdb_byte
*) alloca (j
+ 1);
2272 read_memory (tem
, str
, j
);
2275 printf_filtered (current_substring
, (char *) str
);
2278 case wide_string_arg
:
2283 struct gdbarch
*gdbarch
2284 = get_type_arch (value_type (val_args
[i
]));
2285 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2286 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2287 "wchar_t", NULL
, 0);
2288 int wcwidth
= TYPE_LENGTH (wctype
);
2289 gdb_byte
*buf
= alloca (wcwidth
);
2290 struct obstack output
;
2291 struct cleanup
*inner_cleanup
;
2293 tem
= value_as_address (val_args
[i
]);
2295 /* This is a %s argument. Find the length of the string. */
2296 for (j
= 0;; j
+= wcwidth
)
2299 read_memory (tem
+ j
, buf
, wcwidth
);
2300 if (extract_unsigned_integer (buf
, wcwidth
, byte_order
) == 0)
2304 /* Copy the string contents into a string inside GDB. */
2305 str
= (gdb_byte
*) alloca (j
+ wcwidth
);
2307 read_memory (tem
, str
, j
);
2308 memset (&str
[j
], 0, wcwidth
);
2310 obstack_init (&output
);
2311 inner_cleanup
= make_cleanup_obstack_free (&output
);
2313 convert_between_encodings (target_wide_charset (byte_order
),
2316 &output
, translit_char
);
2317 obstack_grow_str0 (&output
, "");
2319 printf_filtered (current_substring
, obstack_base (&output
));
2320 do_cleanups (inner_cleanup
);
2325 struct gdbarch
*gdbarch
2326 = get_type_arch (value_type (val_args
[i
]));
2327 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2328 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2329 "wchar_t", NULL
, 0);
2330 struct type
*valtype
;
2331 struct obstack output
;
2332 struct cleanup
*inner_cleanup
;
2333 const gdb_byte
*bytes
;
2335 valtype
= value_type (val_args
[i
]);
2336 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2337 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2338 error (_("expected wchar_t argument for %%lc"));
2340 bytes
= value_contents (val_args
[i
]);
2342 obstack_init (&output
);
2343 inner_cleanup
= make_cleanup_obstack_free (&output
);
2345 convert_between_encodings (target_wide_charset (byte_order
),
2347 bytes
, TYPE_LENGTH (valtype
),
2348 TYPE_LENGTH (valtype
),
2349 &output
, translit_char
);
2350 obstack_grow_str0 (&output
, "");
2352 printf_filtered (current_substring
, obstack_base (&output
));
2353 do_cleanups (inner_cleanup
);
2358 struct type
*type
= value_type (val_args
[i
]);
2362 /* If format string wants a float, unchecked-convert the value
2363 to floating point of the same size. */
2364 type
= float_type_from_length (type
);
2365 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2367 error (_("Invalid floating value found in program."));
2369 printf_filtered (current_substring
, (double) val
);
2372 case long_double_arg
:
2373 #ifdef HAVE_LONG_DOUBLE
2375 struct type
*type
= value_type (val_args
[i
]);
2379 /* If format string wants a float, unchecked-convert the value
2380 to floating point of the same size. */
2381 type
= float_type_from_length (type
);
2382 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2384 error (_("Invalid floating value found in program."));
2386 printf_filtered (current_substring
, (long double) val
);
2390 error (_("long double not supported in printf"));
2393 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2395 long long val
= value_as_long (val_args
[i
]);
2396 printf_filtered (current_substring
, val
);
2400 error (_("long long not supported in printf"));
2404 int val
= value_as_long (val_args
[i
]);
2405 printf_filtered (current_substring
, val
);
2410 long val
= value_as_long (val_args
[i
]);
2411 printf_filtered (current_substring
, val
);
2415 /* Handles decimal floating values. */
2418 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2419 #if defined (PRINTF_HAS_DECFLOAT)
2420 /* If we have native support for Decimal floating
2421 printing, handle it here. */
2422 printf_filtered (current_substring
, param_ptr
);
2425 /* As a workaround until vasprintf has native support for DFP
2426 we convert the DFP values to string and print them using
2427 the %s format specifier. */
2430 int nnull_chars
= 0;
2432 /* Parameter data. */
2433 struct type
*param_type
= value_type (val_args
[i
]);
2434 unsigned int param_len
= TYPE_LENGTH (param_type
);
2435 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2436 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2438 /* DFP output data. */
2439 struct value
*dfp_value
= NULL
;
2443 struct type
*dfp_type
= NULL
;
2444 char decstr
[MAX_DECIMAL_STRING
];
2446 /* Points to the end of the string so that we can go back
2447 and check for DFP length modifiers. */
2448 eos
= current_substring
+ strlen (current_substring
);
2450 /* Look for the float/double format specifier. */
2451 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2452 && *eos
!= 'g' && *eos
!= 'G')
2457 /* Search for the '%' char and extract the size and type of
2458 the output decimal value based on its modifiers
2459 (%Hf, %Df, %DDf). */
2460 while (*--sos
!= '%')
2465 dfp_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2467 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2470 dfp_type
= builtin_type (gdbarch
)->builtin_declong
;
2476 dfp_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2480 /* Replace %Hf, %Df and %DDf with %s's. */
2483 /* Go through the whole format string and pull the correct
2484 number of chars back to compensate for the change in the
2485 format specifier. */
2486 while (nnull_chars
< nargs
- i
)
2494 /* Conversion between different DFP types. */
2495 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2496 decimal_convert (param_ptr
, param_len
, byte_order
,
2497 dec
, dfp_len
, byte_order
);
2499 /* If this is a non-trivial conversion, just output 0.
2500 A correct converted value can be displayed by explicitly
2501 casting to a DFP type. */
2502 decimal_from_string (dec
, dfp_len
, byte_order
, "0");
2504 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2506 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2508 decimal_to_string (dfp_ptr
, dfp_len
, byte_order
, decstr
);
2510 /* Print the DFP value. */
2511 printf_filtered (current_substring
, decstr
);
2519 /* We avoid the host's %p because pointers are too
2520 likely to be the wrong size. The only interesting
2521 modifier for %p is a width; extract that, and then
2522 handle %p as glibc would: %#x or a literal "(nil)". */
2524 char *p
, *fmt
, *fmt_p
;
2525 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2526 long long val
= value_as_long (val_args
[i
]);
2528 long val
= value_as_long (val_args
[i
]);
2531 fmt
= alloca (strlen (current_substring
) + 5);
2533 /* Copy up to the leading %. */
2534 p
= current_substring
;
2538 int is_percent
= (*p
== '%');
2552 /* Copy any width. */
2553 while (*p
>= '0' && *p
< '9')
2556 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2559 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2565 printf_filtered (fmt
, val
);
2571 printf_filtered (fmt
, "(nil)");
2577 internal_error (__FILE__
, __LINE__
,
2578 _("failed internal consistency check"));
2580 /* Skip to the next substring. */
2581 current_substring
+= strlen (current_substring
) + 1;
2583 /* Print the portion of the format string after the last argument. */
2584 puts_filtered (last_arg
);
2586 do_cleanups (old_cleanups
);
2590 _initialize_printcmd (void)
2592 struct cmd_list_element
*c
;
2594 current_display_number
= -1;
2596 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2598 add_info ("address", address_info
,
2599 _("Describe where symbol SYM is stored."));
2601 add_info ("symbol", sym_info
, _("\
2602 Describe what symbol is at location ADDR.\n\
2603 Only for symbols with fixed locations (global or static scope)."));
2605 add_com ("x", class_vars
, x_command
, _("\
2606 Examine memory: x/FMT ADDRESS.\n\
2607 ADDRESS is an expression for the memory address to examine.\n\
2608 FMT is a repeat count followed by a format letter and a size letter.\n\
2609 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2610 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2611 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2612 The specified number of objects of the specified size are printed\n\
2613 according to the format.\n\n\
2614 Defaults for format and size letters are those previously used.\n\
2615 Default count is 1. Default address is following last thing printed\n\
2616 with this command or \"print\"."));
2619 add_com ("whereis", class_vars
, whereis_command
,
2620 _("Print line number and file of definition of variable."));
2623 add_info ("display", display_info
, _("\
2624 Expressions to display when program stops, with code numbers."));
2626 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2627 Cancel some expressions to be displayed when program stops.\n\
2628 Arguments are the code numbers of the expressions to stop displaying.\n\
2629 No argument means cancel all automatic-display expressions.\n\
2630 \"delete display\" has the same effect as this command.\n\
2631 Do \"info display\" to see current list of code numbers."),
2634 add_com ("display", class_vars
, display_command
, _("\
2635 Print value of expression EXP each time the program stops.\n\
2636 /FMT may be used before EXP as in the \"print\" command.\n\
2637 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2638 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2639 and examining is done as in the \"x\" command.\n\n\
2640 With no argument, display all currently requested auto-display expressions.\n\
2641 Use \"undisplay\" to cancel display requests previously made."));
2643 add_cmd ("display", class_vars
, enable_display
, _("\
2644 Enable some expressions to be displayed when program stops.\n\
2645 Arguments are the code numbers of the expressions to resume displaying.\n\
2646 No argument means enable all automatic-display expressions.\n\
2647 Do \"info display\" to see current list of code numbers."), &enablelist
);
2649 add_cmd ("display", class_vars
, disable_display_command
, _("\
2650 Disable some expressions to be displayed when program stops.\n\
2651 Arguments are the code numbers of the expressions to stop displaying.\n\
2652 No argument means disable all automatic-display expressions.\n\
2653 Do \"info display\" to see current list of code numbers."), &disablelist
);
2655 add_cmd ("display", class_vars
, undisplay_command
, _("\
2656 Cancel some expressions to be displayed when program stops.\n\
2657 Arguments are the code numbers of the expressions to stop displaying.\n\
2658 No argument means cancel all automatic-display expressions.\n\
2659 Do \"info display\" to see current list of code numbers."), &deletelist
);
2661 add_com ("printf", class_vars
, printf_command
, _("\
2662 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2663 This is useful for formatted output in user-defined commands."));
2665 add_com ("output", class_vars
, output_command
, _("\
2666 Like \"print\" but don't put in value history and don't print newline.\n\
2667 This is useful in user-defined commands."));
2669 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2670 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2671 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2672 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2673 with $), a register (a few standard names starting with $), or an actual\n\
2674 variable in the program being debugged. EXP is any valid expression.\n\
2675 Use \"set variable\" for variables with names identical to set subcommands.\n\
2677 With a subcommand, this command modifies parts of the gdb environment.\n\
2678 You can see these environment settings with the \"show\" command."),
2679 &setlist
, "set ", 1, &cmdlist
);
2681 add_com ("assign", class_vars
, set_command
, _("\
2682 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2683 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2684 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2685 with $), a register (a few standard names starting with $), or an actual\n\
2686 variable in the program being debugged. EXP is any valid expression.\n\
2687 Use \"set variable\" for variables with names identical to set subcommands.\n\
2688 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2689 You can see these environment settings with the \"show\" command."));
2691 /* "call" is the same as "set", but handy for dbx users to call fns. */
2692 c
= add_com ("call", class_vars
, call_command
, _("\
2693 Call a function in the program.\n\
2694 The argument is the function name and arguments, in the notation of the\n\
2695 current working language. The result is printed and saved in the value\n\
2696 history, if it is not void."));
2697 set_cmd_completer (c
, expression_completer
);
2699 add_cmd ("variable", class_vars
, set_command
, _("\
2700 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2701 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2702 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2703 with $), a register (a few standard names starting with $), or an actual\n\
2704 variable in the program being debugged. EXP is any valid expression.\n\
2705 This may usually be abbreviated to simply \"set\"."),
2708 c
= add_com ("print", class_vars
, print_command
, _("\
2709 Print value of expression EXP.\n\
2710 Variables accessible are those of the lexical environment of the selected\n\
2711 stack frame, plus all those whose scope is global or an entire file.\n\
2713 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2714 $$NUM refers to NUM'th value back from the last one.\n\
2715 Names starting with $ refer to registers (with the values they would have\n\
2716 if the program were to return to the stack frame now selected, restoring\n\
2717 all registers saved by frames farther in) or else to debugger\n\
2718 \"convenience\" variables (any such name not a known register).\n\
2719 Use assignment expressions to give values to convenience variables.\n\
2721 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2722 @ is a binary operator for treating consecutive data objects\n\
2723 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2724 element is FOO, whose second element is stored in the space following\n\
2725 where FOO is stored, etc. FOO must be an expression whose value\n\
2726 resides in memory.\n\
2728 EXP may be preceded with /FMT, where FMT is a format letter\n\
2729 but no count or size letter (see \"x\" command)."));
2730 set_cmd_completer (c
, expression_completer
);
2731 add_com_alias ("p", "print", class_vars
, 1);
2733 c
= add_com ("inspect", class_vars
, inspect_command
, _("\
2734 Same as \"print\" command, except that if you are running in the epoch\n\
2735 environment, the value is printed in its own window."));
2736 set_cmd_completer (c
, expression_completer
);
2738 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2739 &max_symbolic_offset
, _("\
2740 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2741 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2743 show_max_symbolic_offset
,
2744 &setprintlist
, &showprintlist
);
2745 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2746 &print_symbol_filename
, _("\
2747 Set printing of source filename and line number with <symbol>."), _("\
2748 Show printing of source filename and line number with <symbol>."), NULL
,
2750 show_print_symbol_filename
,
2751 &setprintlist
, &showprintlist
);