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 (value_address (val
), stream
,
297 &branch_delay_insns
));
302 if (options
->format
== 0 || options
->format
== 's'
303 || TYPE_CODE (type
) == TYPE_CODE_REF
304 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
305 || TYPE_CODE (type
) == TYPE_CODE_STRING
306 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
307 || TYPE_CODE (type
) == TYPE_CODE_UNION
308 || TYPE_CODE (type
) == TYPE_CODE_NAMESPACE
)
309 value_print (val
, stream
, options
);
311 /* User specified format, so don't look to the the type to
312 tell us what to do. */
313 print_scalar_formatted (value_contents (val
), type
,
314 options
, size
, stream
);
317 /* Return builtin floating point type of same length as TYPE.
318 If no such type is found, return TYPE itself. */
320 float_type_from_length (struct type
*type
)
322 struct gdbarch
*gdbarch
= get_type_arch (type
);
323 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
324 unsigned int len
= TYPE_LENGTH (type
);
326 if (len
== TYPE_LENGTH (builtin
->builtin_float
))
327 type
= builtin
->builtin_float
;
328 else if (len
== TYPE_LENGTH (builtin
->builtin_double
))
329 type
= builtin
->builtin_double
;
330 else if (len
== TYPE_LENGTH (builtin
->builtin_long_double
))
331 type
= builtin
->builtin_long_double
;
336 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
337 according to OPTIONS and SIZE on STREAM.
338 Formats s and i are not supported at this level.
340 This is how the elements of an array or structure are printed
344 print_scalar_formatted (const void *valaddr
, struct type
*type
,
345 const struct value_print_options
*options
,
346 int size
, struct ui_file
*stream
)
348 struct gdbarch
*gdbarch
= get_type_arch (type
);
349 LONGEST val_long
= 0;
350 unsigned int len
= TYPE_LENGTH (type
);
351 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
353 /* If we get here with a string format, try again without it. Go
354 all the way back to the language printers, which may call us
356 if (options
->format
== 's')
358 struct value_print_options opts
= *options
;
361 val_print (type
, valaddr
, 0, 0, stream
, 0, &opts
,
366 if (len
> sizeof(LONGEST
) &&
367 (TYPE_CODE (type
) == TYPE_CODE_INT
368 || TYPE_CODE (type
) == TYPE_CODE_ENUM
))
370 switch (options
->format
)
373 print_octal_chars (stream
, valaddr
, len
, byte_order
);
377 print_decimal_chars (stream
, valaddr
, len
, byte_order
);
380 print_binary_chars (stream
, valaddr
, len
, byte_order
);
383 print_hex_chars (stream
, valaddr
, len
, byte_order
);
386 print_char_chars (stream
, type
, valaddr
, len
, byte_order
);
393 if (options
->format
!= 'f')
394 val_long
= unpack_long (type
, valaddr
);
396 /* If the value is a pointer, and pointers and addresses are not the
397 same, then at this point, the value's length (in target bytes) is
398 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
399 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
400 len
= gdbarch_addr_bit (gdbarch
) / TARGET_CHAR_BIT
;
402 /* If we are printing it as unsigned, truncate it in case it is actually
403 a negative signed value (e.g. "print/u (short)-1" should print 65535
404 (if shorts are 16 bits) instead of 4294967295). */
405 if (options
->format
!= 'd' || TYPE_UNSIGNED (type
))
407 if (len
< sizeof (LONGEST
))
408 val_long
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* len
) - 1;
411 switch (options
->format
)
416 /* No size specified, like in print. Print varying # of digits. */
417 print_longest (stream
, 'x', 1, val_long
);
426 print_longest (stream
, size
, 1, val_long
);
429 error (_("Undefined output size \"%c\"."), size
);
434 print_longest (stream
, 'd', 1, val_long
);
438 print_longest (stream
, 'u', 0, val_long
);
443 print_longest (stream
, 'o', 1, val_long
);
445 fprintf_filtered (stream
, "0");
450 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
451 print_address (addr
, stream
);
457 struct value_print_options opts
= *options
;
460 if (TYPE_UNSIGNED (type
))
461 type
= builtin_type (gdbarch
)->builtin_true_unsigned_char
;
463 type
= builtin_type (gdbarch
)->builtin_true_char
;
465 value_print (value_from_longest (type
, val_long
), stream
, &opts
);
470 type
= float_type_from_length (type
);
471 print_floating (valaddr
, type
, stream
);
475 internal_error (__FILE__
, __LINE__
,
476 _("failed internal consistency check"));
479 /* Binary; 't' stands for "two". */
481 char bits
[8 * (sizeof val_long
) + 1];
482 char buf
[8 * (sizeof val_long
) + 32];
487 width
= 8 * (sizeof val_long
);
504 error (_("Undefined output size \"%c\"."), size
);
510 bits
[width
] = (val_long
& 1) ? '1' : '0';
515 while (*cp
&& *cp
== '0')
521 fputs_filtered (buf
, stream
);
526 error (_("Undefined output format \"%c\"."), options
->format
);
530 /* Specify default address for `x' command.
531 The `info lines' command uses this. */
534 set_next_address (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
536 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
538 next_gdbarch
= gdbarch
;
541 /* Make address available to the user as $_. */
542 set_internalvar (lookup_internalvar ("_"),
543 value_from_pointer (ptr_type
, addr
));
546 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
547 after LEADIN. Print nothing if no symbolic name is found nearby.
548 Optionally also print source file and line number, if available.
549 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
550 or to interpret it as a possible C++ name and convert it back to source
551 form. However note that DO_DEMANGLE can be overridden by the specific
552 settings of the demangle and asm_demangle variables. */
555 print_address_symbolic (CORE_ADDR addr
, struct ui_file
*stream
,
556 int do_demangle
, char *leadin
)
559 char *filename
= NULL
;
564 /* Throw away both name and filename. */
565 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &name
);
566 make_cleanup (free_current_contents
, &filename
);
568 if (build_address_symbolic (addr
, do_demangle
, &name
, &offset
,
569 &filename
, &line
, &unmapped
))
571 do_cleanups (cleanup_chain
);
575 fputs_filtered (leadin
, stream
);
577 fputs_filtered ("<*", stream
);
579 fputs_filtered ("<", stream
);
580 fputs_filtered (name
, stream
);
582 fprintf_filtered (stream
, "+%u", (unsigned int) offset
);
584 /* Append source filename and line number if desired. Give specific
585 line # of this addr, if we have it; else line # of the nearest symbol. */
586 if (print_symbol_filename
&& filename
!= NULL
)
589 fprintf_filtered (stream
, " at %s:%d", filename
, line
);
591 fprintf_filtered (stream
, " in %s", filename
);
594 fputs_filtered ("*>", stream
);
596 fputs_filtered (">", stream
);
598 do_cleanups (cleanup_chain
);
601 /* Given an address ADDR return all the elements needed to print the
602 address in a symbolic form. NAME can be mangled or not depending
603 on DO_DEMANGLE (and also on the asm_demangle global variable,
604 manipulated via ''set print asm-demangle''). Return 0 in case of
605 success, when all the info in the OUT paramters is valid. Return 1
608 build_address_symbolic (CORE_ADDR addr
, /* IN */
609 int do_demangle
, /* IN */
610 char **name
, /* OUT */
611 int *offset
, /* OUT */
612 char **filename
, /* OUT */
614 int *unmapped
) /* OUT */
616 struct minimal_symbol
*msymbol
;
617 struct symbol
*symbol
;
618 CORE_ADDR name_location
= 0;
619 struct obj_section
*section
= NULL
;
620 char *name_temp
= "";
622 /* Let's say it is mapped (not unmapped). */
625 /* Determine if the address is in an overlay, and whether it is
627 if (overlay_debugging
)
629 section
= find_pc_overlay (addr
);
630 if (pc_in_unmapped_range (addr
, section
))
633 addr
= overlay_mapped_address (addr
, section
);
637 /* First try to find the address in the symbol table, then
638 in the minsyms. Take the closest one. */
640 /* This is defective in the sense that it only finds text symbols. So
641 really this is kind of pointless--we should make sure that the
642 minimal symbols have everything we need (by changing that we could
643 save some memory, but for many debug format--ELF/DWARF or
644 anything/stabs--it would be inconvenient to eliminate those minimal
646 msymbol
= lookup_minimal_symbol_by_pc_section (addr
, section
);
647 symbol
= find_pc_sect_function (addr
, section
);
651 name_location
= BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
));
652 if (do_demangle
|| asm_demangle
)
653 name_temp
= SYMBOL_PRINT_NAME (symbol
);
655 name_temp
= SYMBOL_LINKAGE_NAME (symbol
);
660 if (SYMBOL_VALUE_ADDRESS (msymbol
) > name_location
|| symbol
== NULL
)
662 /* The msymbol is closer to the address than the symbol;
663 use the msymbol instead. */
665 name_location
= SYMBOL_VALUE_ADDRESS (msymbol
);
666 if (do_demangle
|| asm_demangle
)
667 name_temp
= SYMBOL_PRINT_NAME (msymbol
);
669 name_temp
= SYMBOL_LINKAGE_NAME (msymbol
);
672 if (symbol
== NULL
&& msymbol
== NULL
)
675 /* If the nearest symbol is too far away, don't print anything symbolic. */
677 /* For when CORE_ADDR is larger than unsigned int, we do math in
678 CORE_ADDR. But when we detect unsigned wraparound in the
679 CORE_ADDR math, we ignore this test and print the offset,
680 because addr+max_symbolic_offset has wrapped through the end
681 of the address space back to the beginning, giving bogus comparison. */
682 if (addr
> name_location
+ max_symbolic_offset
683 && name_location
+ max_symbolic_offset
> name_location
)
686 *offset
= addr
- name_location
;
688 *name
= xstrdup (name_temp
);
690 if (print_symbol_filename
)
692 struct symtab_and_line sal
;
694 sal
= find_pc_sect_line (addr
, section
, 0);
698 *filename
= xstrdup (sal
.symtab
->filename
);
706 /* Print address ADDR symbolically on STREAM.
707 First print it as a number. Then perhaps print
708 <SYMBOL + OFFSET> after the number. */
711 print_address (CORE_ADDR addr
, struct ui_file
*stream
)
713 fputs_filtered (paddress (addr
), stream
);
714 print_address_symbolic (addr
, stream
, asm_demangle
, " ");
717 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
718 controls whether to print the symbolic name "raw" or demangled.
719 Global setting "addressprint" controls whether to print hex address
723 print_address_demangle (CORE_ADDR addr
, struct ui_file
*stream
,
726 struct value_print_options opts
;
727 get_user_print_options (&opts
);
730 fprintf_filtered (stream
, "0");
732 else if (opts
.addressprint
)
734 fputs_filtered (paddress (addr
), stream
);
735 print_address_symbolic (addr
, stream
, do_demangle
, " ");
739 print_address_symbolic (addr
, stream
, do_demangle
, "");
744 /* Examine data at address ADDR in format FMT.
745 Fetch it from memory and print on gdb_stdout. */
748 do_examine (struct format_data fmt
, struct gdbarch
*gdbarch
, CORE_ADDR addr
)
753 struct type
*val_type
= NULL
;
756 struct value_print_options opts
;
761 next_gdbarch
= gdbarch
;
764 /* String or instruction format implies fetch single bytes
765 regardless of the specified size. */
766 if (format
== 's' || format
== 'i')
771 /* Pick the appropriate size for an address. */
772 if (gdbarch_ptr_bit (next_gdbarch
) == 64)
774 else if (gdbarch_ptr_bit (next_gdbarch
) == 32)
776 else if (gdbarch_ptr_bit (next_gdbarch
) == 16)
779 /* Bad value for gdbarch_ptr_bit. */
780 internal_error (__FILE__
, __LINE__
,
781 _("failed internal consistency check"));
785 val_type
= builtin_type (next_gdbarch
)->builtin_int8
;
786 else if (size
== 'h')
787 val_type
= builtin_type (next_gdbarch
)->builtin_int16
;
788 else if (size
== 'w')
789 val_type
= builtin_type (next_gdbarch
)->builtin_int32
;
790 else if (size
== 'g')
791 val_type
= builtin_type (next_gdbarch
)->builtin_int64
;
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
)
1108 struct gdbarch
*gdbarch
;
1111 struct minimal_symbol
*msymbol
;
1113 struct obj_section
*section
;
1114 CORE_ADDR load_addr
;
1115 int is_a_field_of_this
; /* C++: lookup_symbol sets this to nonzero
1116 if exp is a field of `this'. */
1119 error (_("Argument required."));
1121 sym
= lookup_symbol (exp
, get_selected_block (0), VAR_DOMAIN
,
1122 &is_a_field_of_this
);
1125 if (is_a_field_of_this
)
1127 printf_filtered ("Symbol \"");
1128 fprintf_symbol_filtered (gdb_stdout
, exp
,
1129 current_language
->la_language
, DMGL_ANSI
);
1130 printf_filtered ("\" is a field of the local class variable ");
1131 if (current_language
->la_language
== language_objc
)
1132 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1134 printf_filtered ("`this'\n");
1138 msymbol
= lookup_minimal_symbol (exp
, NULL
, NULL
);
1140 if (msymbol
!= NULL
)
1142 load_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1144 printf_filtered ("Symbol \"");
1145 fprintf_symbol_filtered (gdb_stdout
, exp
,
1146 current_language
->la_language
, DMGL_ANSI
);
1147 printf_filtered ("\" is at ");
1148 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1149 printf_filtered (" in a file compiled without debugging");
1150 section
= SYMBOL_OBJ_SECTION (msymbol
);
1151 if (section_is_overlay (section
))
1153 load_addr
= overlay_unmapped_address (load_addr
, section
);
1154 printf_filtered (",\n -- loaded at ");
1155 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1156 printf_filtered (" in overlay section %s",
1157 section
->the_bfd_section
->name
);
1159 printf_filtered (".\n");
1162 error (_("No symbol \"%s\" in current context."), exp
);
1166 printf_filtered ("Symbol \"");
1167 fprintf_symbol_filtered (gdb_stdout
, SYMBOL_PRINT_NAME (sym
),
1168 current_language
->la_language
, DMGL_ANSI
);
1169 printf_filtered ("\" is ");
1170 val
= SYMBOL_VALUE (sym
);
1171 section
= SYMBOL_OBJ_SECTION (sym
);
1172 gdbarch
= get_objfile_arch (SYMBOL_SYMTAB (sym
)->objfile
);
1174 switch (SYMBOL_CLASS (sym
))
1177 case LOC_CONST_BYTES
:
1178 printf_filtered ("constant");
1182 printf_filtered ("a label at address ");
1183 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1185 if (section_is_overlay (section
))
1187 load_addr
= overlay_unmapped_address (load_addr
, section
);
1188 printf_filtered (",\n -- loaded at ");
1189 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1190 printf_filtered (" in overlay section %s",
1191 section
->the_bfd_section
->name
);
1196 /* FIXME: cagney/2004-01-26: It should be possible to
1197 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
1198 Unfortunately DWARF 2 stores the frame-base (instead of the
1199 function) location in a function's symbol. Oops! For the
1200 moment enable this when/where applicable. */
1201 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
, gdb_stdout
);
1205 /* GDBARCH is the architecture associated with the objfile the symbol
1206 is defined in; the target architecture may be different, and may
1207 provide additional registers. However, we do not know the target
1208 architecture at this point. We assume the objfile architecture
1209 will contain all the standard registers that occur in debug info
1211 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1213 if (SYMBOL_IS_ARGUMENT (sym
))
1214 printf_filtered (_("an argument in register %s"),
1215 gdbarch_register_name (gdbarch
, regno
));
1217 printf_filtered (_("a variable in register %s"),
1218 gdbarch_register_name (gdbarch
, regno
));
1222 printf_filtered (_("static storage at address "));
1223 fputs_filtered (paddress (load_addr
= SYMBOL_VALUE_ADDRESS (sym
)),
1225 if (section_is_overlay (section
))
1227 load_addr
= overlay_unmapped_address (load_addr
, section
);
1228 printf_filtered (_(",\n -- loaded at "));
1229 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1230 printf_filtered (_(" in overlay section %s"),
1231 section
->the_bfd_section
->name
);
1235 case LOC_REGPARM_ADDR
:
1236 /* Note comment at LOC_REGISTER. */
1237 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
1238 printf_filtered (_("address of an argument in register %s"),
1239 gdbarch_register_name (gdbarch
, regno
));
1243 printf_filtered (_("an argument at offset %ld"), val
);
1247 printf_filtered (_("a local variable at frame offset %ld"), val
);
1251 printf_filtered (_("a reference argument at offset %ld"), val
);
1255 printf_filtered (_("a typedef"));
1259 printf_filtered (_("a function at address "));
1260 load_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1261 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1262 if (section_is_overlay (section
))
1264 load_addr
= overlay_unmapped_address (load_addr
, section
);
1265 printf_filtered (_(",\n -- loaded at "));
1266 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1267 printf_filtered (_(" in overlay section %s"),
1268 section
->the_bfd_section
->name
);
1272 case LOC_UNRESOLVED
:
1274 struct minimal_symbol
*msym
;
1276 msym
= lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym
), NULL
, NULL
);
1278 printf_filtered ("unresolved");
1281 section
= SYMBOL_OBJ_SECTION (msym
);
1282 load_addr
= SYMBOL_VALUE_ADDRESS (msym
);
1285 && (section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
1286 printf_filtered (_("a thread-local variable at offset %s "
1287 "in the thread-local storage for `%s'"),
1288 paddr_nz (load_addr
), section
->objfile
->name
);
1291 printf_filtered (_("static storage at address "));
1292 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1293 if (section_is_overlay (section
))
1295 load_addr
= overlay_unmapped_address (load_addr
, section
);
1296 printf_filtered (_(",\n -- loaded at "));
1297 fputs_filtered (paddress (load_addr
), gdb_stdout
);
1298 printf_filtered (_(" in overlay section %s"),
1299 section
->the_bfd_section
->name
);
1306 case LOC_OPTIMIZED_OUT
:
1307 printf_filtered (_("optimized out"));
1311 printf_filtered (_("of unknown (botched) type"));
1314 printf_filtered (".\n");
1319 x_command (char *exp
, int from_tty
)
1321 struct expression
*expr
;
1322 struct format_data fmt
;
1323 struct cleanup
*old_chain
;
1326 fmt
.format
= last_format
? last_format
: 'x';
1327 fmt
.size
= last_size
;
1331 if (exp
&& *exp
== '/')
1334 fmt
= decode_format (&exp
, last_format
, last_size
);
1337 /* If we have an expression, evaluate it and use it as the address. */
1339 if (exp
!= 0 && *exp
!= 0)
1341 expr
= parse_expression (exp
);
1342 /* Cause expression not to be there any more if this command is
1343 repeated with Newline. But don't clobber a user-defined
1344 command's definition. */
1347 old_chain
= make_cleanup (free_current_contents
, &expr
);
1348 val
= evaluate_expression (expr
);
1349 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_REF
)
1350 val
= value_ind (val
);
1351 /* In rvalue contexts, such as this, functions are coerced into
1352 pointers to functions. This makes "x/i main" work. */
1353 if (/* last_format == 'i' && */
1354 TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1355 && VALUE_LVAL (val
) == lval_memory
)
1356 next_address
= value_address (val
);
1358 next_address
= value_as_address (val
);
1360 next_gdbarch
= expr
->gdbarch
;
1361 do_cleanups (old_chain
);
1365 error_no_arg (_("starting display address"));
1367 do_examine (fmt
, next_gdbarch
, next_address
);
1369 /* If the examine succeeds, we remember its size and format for next
1371 last_size
= fmt
.size
;
1372 last_format
= fmt
.format
;
1374 /* Set a couple of internal variables if appropriate. */
1375 if (last_examine_value
)
1377 /* Make last address examined available to the user as $_. Use
1378 the correct pointer type. */
1379 struct type
*pointer_type
1380 = lookup_pointer_type (value_type (last_examine_value
));
1381 set_internalvar (lookup_internalvar ("_"),
1382 value_from_pointer (pointer_type
,
1383 last_examine_address
));
1385 /* Make contents of last address examined available to the user
1386 as $__. If the last value has not been fetched from memory
1387 then don't fetch it now; instead mark it by voiding the $__
1389 if (value_lazy (last_examine_value
))
1390 clear_internalvar (lookup_internalvar ("__"));
1392 set_internalvar (lookup_internalvar ("__"), last_examine_value
);
1397 /* Add an expression to the auto-display chain.
1398 Specify the expression. */
1401 display_command (char *exp
, int from_tty
)
1403 struct format_data fmt
;
1404 struct expression
*expr
;
1405 struct display
*new;
1409 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1411 if (tui_active
&& exp
!= NULL
&& *exp
== '$')
1412 display_it
= (tui_set_layout_for_display_command (exp
) == TUI_FAILURE
);
1426 fmt
= decode_format (&exp
, 0, 0);
1427 if (fmt
.size
&& fmt
.format
== 0)
1429 if (fmt
.format
== 'i' || fmt
.format
== 's')
1440 innermost_block
= NULL
;
1441 expr
= parse_expression (exp
);
1443 new = (struct display
*) xmalloc (sizeof (struct display
));
1445 new->exp_string
= xstrdup (exp
);
1447 new->block
= innermost_block
;
1448 new->next
= display_chain
;
1449 new->number
= ++display_number
;
1452 display_chain
= new;
1454 if (from_tty
&& target_has_execution
)
1455 do_one_display (new);
1462 free_display (struct display
*d
)
1464 xfree (d
->exp_string
);
1469 /* Clear out the display_chain. Done when new symtabs are loaded,
1470 since this invalidates the types stored in many expressions. */
1473 clear_displays (void)
1477 while ((d
= display_chain
) != NULL
)
1479 display_chain
= d
->next
;
1484 /* Delete the auto-display number NUM. */
1487 delete_display (int num
)
1489 struct display
*d1
, *d
;
1492 error (_("No display number %d."), num
);
1494 if (display_chain
->number
== num
)
1497 display_chain
= d1
->next
;
1501 for (d
= display_chain
;; d
= d
->next
)
1504 error (_("No display number %d."), num
);
1505 if (d
->next
->number
== num
)
1515 /* Delete some values from the auto-display chain.
1516 Specify the element numbers. */
1519 undisplay_command (char *args
, int from_tty
)
1527 if (query (_("Delete all auto-display expressions? ")))
1536 while (*p1
>= '0' && *p1
<= '9')
1538 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1539 error (_("Arguments must be display numbers."));
1543 delete_display (num
);
1546 while (*p
== ' ' || *p
== '\t')
1552 /* Display a single auto-display.
1553 Do nothing if the display cannot be printed in the current context,
1554 or if the display is disabled. */
1557 do_one_display (struct display
*d
)
1559 int within_current_scope
;
1561 if (d
->enabled_p
== 0)
1566 volatile struct gdb_exception ex
;
1567 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1569 innermost_block
= NULL
;
1570 d
->exp
= parse_expression (d
->exp_string
);
1571 d
->block
= innermost_block
;
1575 /* Can't re-parse the expression. Disable this display item. */
1577 warning (_("Unable to display \"%s\": %s"),
1578 d
->exp_string
, ex
.message
);
1584 within_current_scope
= contained_in (get_selected_block (0), d
->block
);
1586 within_current_scope
= 1;
1587 if (!within_current_scope
)
1590 current_display_number
= d
->number
;
1592 annotate_display_begin ();
1593 printf_filtered ("%d", d
->number
);
1594 annotate_display_number_end ();
1595 printf_filtered (": ");
1601 annotate_display_format ();
1603 printf_filtered ("x/");
1604 if (d
->format
.count
!= 1)
1605 printf_filtered ("%d", d
->format
.count
);
1606 printf_filtered ("%c", d
->format
.format
);
1607 if (d
->format
.format
!= 'i' && d
->format
.format
!= 's')
1608 printf_filtered ("%c", d
->format
.size
);
1609 printf_filtered (" ");
1611 annotate_display_expression ();
1613 puts_filtered (d
->exp_string
);
1614 annotate_display_expression_end ();
1616 if (d
->format
.count
!= 1 || d
->format
.format
== 'i')
1617 printf_filtered ("\n");
1619 printf_filtered (" ");
1621 val
= evaluate_expression (d
->exp
);
1622 addr
= value_as_address (val
);
1623 if (d
->format
.format
== 'i')
1624 addr
= gdbarch_addr_bits_remove (d
->exp
->gdbarch
, addr
);
1626 annotate_display_value ();
1628 do_examine (d
->format
, d
->exp
->gdbarch
, addr
);
1632 struct value_print_options opts
;
1634 annotate_display_format ();
1636 if (d
->format
.format
)
1637 printf_filtered ("/%c ", d
->format
.format
);
1639 annotate_display_expression ();
1641 puts_filtered (d
->exp_string
);
1642 annotate_display_expression_end ();
1644 printf_filtered (" = ");
1646 annotate_display_expression ();
1648 get_formatted_print_options (&opts
, d
->format
.format
);
1649 opts
.raw
= d
->format
.raw
;
1650 print_formatted (evaluate_expression (d
->exp
),
1651 d
->format
.size
, &opts
, gdb_stdout
);
1652 printf_filtered ("\n");
1655 annotate_display_end ();
1657 gdb_flush (gdb_stdout
);
1658 current_display_number
= -1;
1661 /* Display all of the values on the auto-display chain which can be
1662 evaluated in the current scope. */
1669 for (d
= display_chain
; d
; d
= d
->next
)
1673 /* Delete the auto-display which we were in the process of displaying.
1674 This is done when there is an error or a signal. */
1677 disable_display (int num
)
1681 for (d
= display_chain
; d
; d
= d
->next
)
1682 if (d
->number
== num
)
1687 printf_unfiltered (_("No display number %d.\n"), num
);
1691 disable_current_display (void)
1693 if (current_display_number
>= 0)
1695 disable_display (current_display_number
);
1696 fprintf_unfiltered (gdb_stderr
, _("\
1697 Disabling display %d to avoid infinite recursion.\n"),
1698 current_display_number
);
1700 current_display_number
= -1;
1704 display_info (char *ignore
, int from_tty
)
1709 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1711 printf_filtered (_("Auto-display expressions now in effect:\n\
1712 Num Enb Expression\n"));
1714 for (d
= display_chain
; d
; d
= d
->next
)
1716 printf_filtered ("%d: %c ", d
->number
, "ny"[(int) d
->enabled_p
]);
1718 printf_filtered ("/%d%c%c ", d
->format
.count
, d
->format
.size
,
1720 else if (d
->format
.format
)
1721 printf_filtered ("/%c ", d
->format
.format
);
1722 puts_filtered (d
->exp_string
);
1723 if (d
->block
&& !contained_in (get_selected_block (0), d
->block
))
1724 printf_filtered (_(" (cannot be evaluated in the current context)"));
1725 printf_filtered ("\n");
1726 gdb_flush (gdb_stdout
);
1731 enable_display (char *args
, int from_tty
)
1740 for (d
= display_chain
; d
; d
= d
->next
)
1747 while (*p1
>= '0' && *p1
<= '9')
1749 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1750 error (_("Arguments must be display numbers."));
1754 for (d
= display_chain
; d
; d
= d
->next
)
1755 if (d
->number
== num
)
1760 printf_unfiltered (_("No display number %d.\n"), num
);
1763 while (*p
== ' ' || *p
== '\t')
1769 disable_display_command (char *args
, int from_tty
)
1777 for (d
= display_chain
; d
; d
= d
->next
)
1784 while (*p1
>= '0' && *p1
<= '9')
1786 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
1787 error (_("Arguments must be display numbers."));
1789 disable_display (atoi (p
));
1792 while (*p
== ' ' || *p
== '\t')
1797 /* Return 1 if D uses SOLIB (and will become dangling when SOLIB
1798 is unloaded), otherwise return 0. */
1801 display_uses_solib_p (const struct display
*d
,
1802 const struct so_list
*solib
)
1805 struct expression
*const exp
= d
->exp
;
1806 const union exp_element
*const elts
= exp
->elts
;
1808 if (d
->block
!= NULL
1809 && solib_contains_address_p (solib
, d
->block
->startaddr
))
1812 for (endpos
= exp
->nelts
; endpos
> 0; )
1814 int i
, args
, oplen
= 0;
1816 exp
->language_defn
->la_exp_desc
->operator_length (exp
, endpos
,
1818 gdb_assert (oplen
> 0);
1821 if (elts
[i
].opcode
== OP_VAR_VALUE
)
1823 const struct block
*const block
= elts
[i
+ 1].block
;
1824 const struct symbol
*const symbol
= elts
[i
+ 2].symbol
;
1825 const struct obj_section
*const section
=
1826 SYMBOL_OBJ_SECTION (symbol
);
1829 && solib_contains_address_p (solib
, block
->startaddr
))
1832 if (section
&& section
->objfile
== solib
->objfile
)
1841 /* display_chain items point to blocks and expressions. Some expressions in
1842 turn may point to symbols.
1843 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1844 obstack_free'd when a shared library is unloaded.
1845 Clear pointers that are about to become dangling.
1846 Both .exp and .block fields will be restored next time we need to display
1847 an item by re-parsing .exp_string field in the new execution context. */
1850 clear_dangling_display_expressions (struct so_list
*solib
)
1853 struct objfile
*objfile
= NULL
;
1855 for (d
= display_chain
; d
; d
= d
->next
)
1857 if (d
->exp
&& display_uses_solib_p (d
, solib
))
1867 /* Print the value in stack frame FRAME of a variable specified by a
1868 struct symbol. NAME is the name to print; if NULL then VAR's print
1869 name will be used. STREAM is the ui_file on which to print the
1870 value. INDENT specifies the number of indent levels to print
1871 before printing the variable name. */
1874 print_variable_and_value (const char *name
, struct symbol
*var
,
1875 struct frame_info
*frame
,
1876 struct ui_file
*stream
, int indent
)
1879 struct value_print_options opts
;
1882 name
= SYMBOL_PRINT_NAME (var
);
1884 fprintf_filtered (stream
, "%s%s = ", n_spaces (2 * indent
), name
);
1886 val
= read_var_value (var
, frame
);
1887 get_user_print_options (&opts
);
1888 common_val_print (val
, stream
, indent
, &opts
, current_language
);
1889 fprintf_filtered (stream
, "\n");
1893 printf_command (char *arg
, int from_tty
)
1897 char *string
= NULL
;
1898 struct value
**val_args
;
1900 char *current_substring
;
1902 int allocated_args
= 20;
1903 struct cleanup
*old_cleanups
;
1905 val_args
= xmalloc (allocated_args
* sizeof (struct value
*));
1906 old_cleanups
= make_cleanup (free_current_contents
, &val_args
);
1909 error_no_arg (_("format-control string and values to print"));
1911 /* Skip white space before format string */
1912 while (*s
== ' ' || *s
== '\t')
1915 /* A format string should follow, enveloped in double quotes. */
1917 error (_("Bad format string, missing '\"'."));
1919 /* Parse the format-control string and copy it into the string STRING,
1920 processing some kinds of escape sequence. */
1922 f
= string
= (char *) alloca (strlen (s
) + 1);
1930 error (_("Bad format string, non-terminated '\"'."));
1963 /* ??? TODO: handle other escape sequences */
1964 error (_("Unrecognized escape character \\%c in format string."),
1974 /* Skip over " and following space and comma. */
1977 while (*s
== ' ' || *s
== '\t')
1980 if (*s
!= ',' && *s
!= 0)
1981 error (_("Invalid argument syntax"));
1985 while (*s
== ' ' || *s
== '\t')
1988 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1989 substrings
= alloca (strlen (string
) * 2);
1990 current_substring
= substrings
;
1993 /* Now scan the string for %-specs and see what kinds of args they want.
1994 argclass[I] classifies the %-specs so we can give printf_filtered
1995 something of the right size. */
1999 int_arg
, long_arg
, long_long_arg
, ptr_arg
,
2000 string_arg
, wide_string_arg
, wide_char_arg
,
2001 double_arg
, long_double_arg
, decfloat_arg
2003 enum argclass
*argclass
;
2004 enum argclass this_argclass
;
2009 argclass
= (enum argclass
*) alloca (strlen (s
) * sizeof *argclass
);
2016 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
2017 int seen_space
= 0, seen_plus
= 0;
2018 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
2019 int seen_big_d
= 0, seen_double_big_d
= 0;
2022 /* Check the validity of the format specifier, and work
2023 out what argument it expects. We only accept C89
2024 format strings, with the exception of long long (which
2025 we autoconf for). */
2027 /* Skip over "%%". */
2034 /* The first part of a format specifier is a set of flag
2036 while (strchr ("0-+ #", *f
))
2049 /* The next part of a format specifier is a width. */
2050 while (strchr ("0123456789", *f
))
2053 /* The next part of a format specifier is a precision. */
2058 while (strchr ("0123456789", *f
))
2062 /* The next part of a format specifier is a length modifier. */
2083 /* Decimal32 modifier. */
2089 /* Decimal64 and Decimal128 modifiers. */
2094 /* Check for a Decimal128. */
2098 seen_double_big_d
= 1;
2114 if (seen_space
|| seen_plus
)
2121 this_argclass
= int_arg
;
2122 else if (lcount
== 1)
2123 this_argclass
= long_arg
;
2125 this_argclass
= long_long_arg
;
2132 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
2133 if (lcount
> 1 || seen_h
|| seen_big_l
)
2135 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2140 this_argclass
= ptr_arg
;
2141 if (lcount
|| seen_h
|| seen_big_l
)
2143 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
2148 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
2149 if (lcount
> 1 || seen_h
|| seen_big_l
)
2151 if (seen_zero
|| seen_space
|| seen_plus
)
2160 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
2161 this_argclass
= decfloat_arg
;
2162 else if (seen_big_l
)
2163 this_argclass
= long_double_arg
;
2165 this_argclass
= double_arg
;
2167 if (lcount
|| seen_h
)
2172 error (_("`*' not supported for precision or width in printf"));
2175 error (_("Format specifier `n' not supported in printf"));
2178 error (_("Incomplete format specifier at end of format string"));
2181 error (_("Unrecognized format specifier '%c' in printf"), *f
);
2185 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2190 if (lcount
> 1 && USE_PRINTF_I64
)
2192 /* Windows' printf does support long long, but not the usual way.
2193 Convert %lld to %I64d. */
2194 int length_before_ll
= f
- last_arg
- 1 - lcount
;
2195 strncpy (current_substring
, last_arg
, length_before_ll
);
2196 strcpy (current_substring
+ length_before_ll
, "I64");
2197 current_substring
[length_before_ll
+ 3] =
2198 last_arg
[length_before_ll
+ lcount
];
2199 current_substring
+= length_before_ll
+ 4;
2201 else if (this_argclass
== wide_string_arg
2202 || this_argclass
== wide_char_arg
)
2204 /* Convert %ls or %lc to %s. */
2205 int length_before_ls
= f
- last_arg
- 2;
2206 strncpy (current_substring
, last_arg
, length_before_ls
);
2207 strcpy (current_substring
+ length_before_ls
, "s");
2208 current_substring
+= length_before_ls
+ 2;
2212 strncpy (current_substring
, last_arg
, f
- last_arg
);
2213 current_substring
+= f
- last_arg
;
2215 *current_substring
++ = '\0';
2217 argclass
[nargs_wanted
++] = this_argclass
;
2220 /* Now, parse all arguments and evaluate them.
2221 Store the VALUEs in VAL_ARGS. */
2226 if (nargs
== allocated_args
)
2227 val_args
= (struct value
**) xrealloc ((char *) val_args
,
2228 (allocated_args
*= 2)
2229 * sizeof (struct value
*));
2231 val_args
[nargs
] = parse_to_comma_and_eval (&s1
);
2239 if (nargs
!= nargs_wanted
)
2240 error (_("Wrong number of arguments for specified format-string"));
2242 /* Now actually print them. */
2243 current_substring
= substrings
;
2244 for (i
= 0; i
< nargs
; i
++)
2246 switch (argclass
[i
])
2253 tem
= value_as_address (val_args
[i
]);
2255 /* This is a %s argument. Find the length of the string. */
2260 read_memory (tem
+ j
, &c
, 1);
2265 /* Copy the string contents into a string inside GDB. */
2266 str
= (gdb_byte
*) alloca (j
+ 1);
2268 read_memory (tem
, str
, j
);
2271 printf_filtered (current_substring
, (char *) str
);
2274 case wide_string_arg
:
2279 struct gdbarch
*gdbarch
2280 = get_type_arch (value_type (val_args
[i
]));
2281 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2282 "wchar_t", NULL
, 0);
2283 int wcwidth
= TYPE_LENGTH (wctype
);
2284 gdb_byte
*buf
= alloca (wcwidth
);
2285 struct obstack output
;
2286 struct cleanup
*inner_cleanup
;
2288 tem
= value_as_address (val_args
[i
]);
2290 /* This is a %s argument. Find the length of the string. */
2291 for (j
= 0;; j
+= wcwidth
)
2294 read_memory (tem
+ j
, buf
, wcwidth
);
2295 if (extract_unsigned_integer (buf
, wcwidth
) == 0)
2299 /* Copy the string contents into a string inside GDB. */
2300 str
= (gdb_byte
*) alloca (j
+ wcwidth
);
2302 read_memory (tem
, str
, j
);
2303 memset (&str
[j
], 0, wcwidth
);
2305 obstack_init (&output
);
2306 inner_cleanup
= make_cleanup_obstack_free (&output
);
2308 convert_between_encodings (target_wide_charset (),
2311 &output
, translit_char
);
2312 obstack_grow_str0 (&output
, "");
2314 printf_filtered (current_substring
, obstack_base (&output
));
2315 do_cleanups (inner_cleanup
);
2320 struct gdbarch
*gdbarch
2321 = get_type_arch (value_type (val_args
[i
]));
2322 struct type
*wctype
= lookup_typename (current_language
, gdbarch
,
2323 "wchar_t", NULL
, 0);
2324 struct type
*valtype
;
2325 struct obstack output
;
2326 struct cleanup
*inner_cleanup
;
2327 const gdb_byte
*bytes
;
2329 valtype
= value_type (val_args
[i
]);
2330 if (TYPE_LENGTH (valtype
) != TYPE_LENGTH (wctype
)
2331 || TYPE_CODE (valtype
) != TYPE_CODE_INT
)
2332 error (_("expected wchar_t argument for %%lc"));
2334 bytes
= value_contents (val_args
[i
]);
2336 obstack_init (&output
);
2337 inner_cleanup
= make_cleanup_obstack_free (&output
);
2339 convert_between_encodings (target_wide_charset (),
2341 bytes
, TYPE_LENGTH (valtype
),
2342 TYPE_LENGTH (valtype
),
2343 &output
, translit_char
);
2344 obstack_grow_str0 (&output
, "");
2346 printf_filtered (current_substring
, obstack_base (&output
));
2347 do_cleanups (inner_cleanup
);
2352 struct type
*type
= value_type (val_args
[i
]);
2356 /* If format string wants a float, unchecked-convert the value
2357 to floating point of the same size. */
2358 type
= float_type_from_length (type
);
2359 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2361 error (_("Invalid floating value found in program."));
2363 printf_filtered (current_substring
, (double) val
);
2366 case long_double_arg
:
2367 #ifdef HAVE_LONG_DOUBLE
2369 struct type
*type
= value_type (val_args
[i
]);
2373 /* If format string wants a float, unchecked-convert the value
2374 to floating point of the same size. */
2375 type
= float_type_from_length (type
);
2376 val
= unpack_double (type
, value_contents (val_args
[i
]), &inv
);
2378 error (_("Invalid floating value found in program."));
2380 printf_filtered (current_substring
, (long double) val
);
2384 error (_("long double not supported in printf"));
2387 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2389 long long val
= value_as_long (val_args
[i
]);
2390 printf_filtered (current_substring
, val
);
2394 error (_("long long not supported in printf"));
2398 int val
= value_as_long (val_args
[i
]);
2399 printf_filtered (current_substring
, val
);
2404 long val
= value_as_long (val_args
[i
]);
2405 printf_filtered (current_substring
, val
);
2409 /* Handles decimal floating values. */
2412 const gdb_byte
*param_ptr
= value_contents (val_args
[i
]);
2413 #if defined (PRINTF_HAS_DECFLOAT)
2414 /* If we have native support for Decimal floating
2415 printing, handle it here. */
2416 printf_filtered (current_substring
, param_ptr
);
2419 /* As a workaround until vasprintf has native support for DFP
2420 we convert the DFP values to string and print them using
2421 the %s format specifier. */
2424 int nnull_chars
= 0;
2426 /* Parameter data. */
2427 struct type
*param_type
= value_type (val_args
[i
]);
2428 unsigned int param_len
= TYPE_LENGTH (param_type
);
2429 struct gdbarch
*gdbarch
= get_type_arch (param_type
);
2431 /* DFP output data. */
2432 struct value
*dfp_value
= NULL
;
2436 struct type
*dfp_type
= NULL
;
2437 char decstr
[MAX_DECIMAL_STRING
];
2439 /* Points to the end of the string so that we can go back
2440 and check for DFP length modifiers. */
2441 eos
= current_substring
+ strlen (current_substring
);
2443 /* Look for the float/double format specifier. */
2444 while (*eos
!= 'f' && *eos
!= 'e' && *eos
!= 'E'
2445 && *eos
!= 'g' && *eos
!= 'G')
2450 /* Search for the '%' char and extract the size and type of
2451 the output decimal value based on its modifiers
2452 (%Hf, %Df, %DDf). */
2453 while (*--sos
!= '%')
2458 dfp_type
= builtin_type (gdbarch
)->builtin_decfloat
;
2460 else if (*sos
== 'D' && *(sos
- 1) == 'D')
2463 dfp_type
= builtin_type (gdbarch
)->builtin_declong
;
2469 dfp_type
= builtin_type (gdbarch
)->builtin_decdouble
;
2473 /* Replace %Hf, %Df and %DDf with %s's. */
2476 /* Go through the whole format string and pull the correct
2477 number of chars back to compensate for the change in the
2478 format specifier. */
2479 while (nnull_chars
< nargs
- i
)
2487 /* Conversion between different DFP types. */
2488 if (TYPE_CODE (param_type
) == TYPE_CODE_DECFLOAT
)
2489 decimal_convert (param_ptr
, param_len
, dec
, dfp_len
);
2491 /* If this is a non-trivial conversion, just output 0.
2492 A correct converted value can be displayed by explicitly
2493 casting to a DFP type. */
2494 decimal_from_string (dec
, dfp_len
, "0");
2496 dfp_value
= value_from_decfloat (dfp_type
, dec
);
2498 dfp_ptr
= (gdb_byte
*) value_contents (dfp_value
);
2500 decimal_to_string (dfp_ptr
, dfp_len
, decstr
);
2502 /* Print the DFP value. */
2503 printf_filtered (current_substring
, decstr
);
2511 /* We avoid the host's %p because pointers are too
2512 likely to be the wrong size. The only interesting
2513 modifier for %p is a width; extract that, and then
2514 handle %p as glibc would: %#x or a literal "(nil)". */
2516 char *p
, *fmt
, *fmt_p
;
2517 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2518 long long val
= value_as_long (val_args
[i
]);
2520 long val
= value_as_long (val_args
[i
]);
2523 fmt
= alloca (strlen (current_substring
) + 5);
2525 /* Copy up to the leading %. */
2526 p
= current_substring
;
2530 int is_percent
= (*p
== '%');
2544 /* Copy any width. */
2545 while (*p
>= '0' && *p
< '9')
2548 gdb_assert (*p
== 'p' && *(p
+ 1) == '\0');
2551 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2557 printf_filtered (fmt
, val
);
2563 printf_filtered (fmt
, "(nil)");
2569 internal_error (__FILE__
, __LINE__
,
2570 _("failed internal consistency check"));
2572 /* Skip to the next substring. */
2573 current_substring
+= strlen (current_substring
) + 1;
2575 /* Print the portion of the format string after the last argument. */
2576 puts_filtered (last_arg
);
2578 do_cleanups (old_cleanups
);
2582 _initialize_printcmd (void)
2584 struct cmd_list_element
*c
;
2586 current_display_number
= -1;
2588 observer_attach_solib_unloaded (clear_dangling_display_expressions
);
2590 add_info ("address", address_info
,
2591 _("Describe where symbol SYM is stored."));
2593 add_info ("symbol", sym_info
, _("\
2594 Describe what symbol is at location ADDR.\n\
2595 Only for symbols with fixed locations (global or static scope)."));
2597 add_com ("x", class_vars
, x_command
, _("\
2598 Examine memory: x/FMT ADDRESS.\n\
2599 ADDRESS is an expression for the memory address to examine.\n\
2600 FMT is a repeat count followed by a format letter and a size letter.\n\
2601 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2602 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2603 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2604 The specified number of objects of the specified size are printed\n\
2605 according to the format.\n\n\
2606 Defaults for format and size letters are those previously used.\n\
2607 Default count is 1. Default address is following last thing printed\n\
2608 with this command or \"print\"."));
2611 add_com ("whereis", class_vars
, whereis_command
,
2612 _("Print line number and file of definition of variable."));
2615 add_info ("display", display_info
, _("\
2616 Expressions to display when program stops, with code numbers."));
2618 add_cmd ("undisplay", class_vars
, undisplay_command
, _("\
2619 Cancel some expressions to be displayed when program stops.\n\
2620 Arguments are the code numbers of the expressions to stop displaying.\n\
2621 No argument means cancel all automatic-display expressions.\n\
2622 \"delete display\" has the same effect as this command.\n\
2623 Do \"info display\" to see current list of code numbers."),
2626 add_com ("display", class_vars
, display_command
, _("\
2627 Print value of expression EXP each time the program stops.\n\
2628 /FMT may be used before EXP as in the \"print\" command.\n\
2629 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2630 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2631 and examining is done as in the \"x\" command.\n\n\
2632 With no argument, display all currently requested auto-display expressions.\n\
2633 Use \"undisplay\" to cancel display requests previously made."));
2635 add_cmd ("display", class_vars
, enable_display
, _("\
2636 Enable some expressions to be displayed when program stops.\n\
2637 Arguments are the code numbers of the expressions to resume displaying.\n\
2638 No argument means enable all automatic-display expressions.\n\
2639 Do \"info display\" to see current list of code numbers."), &enablelist
);
2641 add_cmd ("display", class_vars
, disable_display_command
, _("\
2642 Disable some expressions to be displayed when program stops.\n\
2643 Arguments are the code numbers of the expressions to stop displaying.\n\
2644 No argument means disable all automatic-display expressions.\n\
2645 Do \"info display\" to see current list of code numbers."), &disablelist
);
2647 add_cmd ("display", class_vars
, undisplay_command
, _("\
2648 Cancel some expressions to be displayed when program stops.\n\
2649 Arguments are the code numbers of the expressions to stop displaying.\n\
2650 No argument means cancel all automatic-display expressions.\n\
2651 Do \"info display\" to see current list of code numbers."), &deletelist
);
2653 add_com ("printf", class_vars
, printf_command
, _("\
2654 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2655 This is useful for formatted output in user-defined commands."));
2657 add_com ("output", class_vars
, output_command
, _("\
2658 Like \"print\" but don't put in value history and don't print newline.\n\
2659 This is useful in user-defined commands."));
2661 add_prefix_cmd ("set", class_vars
, set_command
, _("\
2662 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2663 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2664 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2665 with $), a register (a few standard names starting with $), or an actual\n\
2666 variable in the program being debugged. EXP is any valid expression.\n\
2667 Use \"set variable\" for variables with names identical to set subcommands.\n\
2669 With a subcommand, this command modifies parts of the gdb environment.\n\
2670 You can see these environment settings with the \"show\" command."),
2671 &setlist
, "set ", 1, &cmdlist
);
2673 add_com ("assign", class_vars
, set_command
, _("\
2674 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2675 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2676 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2677 with $), a register (a few standard names starting with $), or an actual\n\
2678 variable in the program being debugged. EXP is any valid expression.\n\
2679 Use \"set variable\" for variables with names identical to set subcommands.\n\
2680 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2681 You can see these environment settings with the \"show\" command."));
2683 /* "call" is the same as "set", but handy for dbx users to call fns. */
2684 c
= add_com ("call", class_vars
, call_command
, _("\
2685 Call a function in the program.\n\
2686 The argument is the function name and arguments, in the notation of the\n\
2687 current working language. The result is printed and saved in the value\n\
2688 history, if it is not void."));
2689 set_cmd_completer (c
, expression_completer
);
2691 add_cmd ("variable", class_vars
, set_command
, _("\
2692 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2693 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2694 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2695 with $), a register (a few standard names starting with $), or an actual\n\
2696 variable in the program being debugged. EXP is any valid expression.\n\
2697 This may usually be abbreviated to simply \"set\"."),
2700 c
= add_com ("print", class_vars
, print_command
, _("\
2701 Print value of expression EXP.\n\
2702 Variables accessible are those of the lexical environment of the selected\n\
2703 stack frame, plus all those whose scope is global or an entire file.\n\
2705 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2706 $$NUM refers to NUM'th value back from the last one.\n\
2707 Names starting with $ refer to registers (with the values they would have\n\
2708 if the program were to return to the stack frame now selected, restoring\n\
2709 all registers saved by frames farther in) or else to debugger\n\
2710 \"convenience\" variables (any such name not a known register).\n\
2711 Use assignment expressions to give values to convenience variables.\n\
2713 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2714 @ is a binary operator for treating consecutive data objects\n\
2715 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2716 element is FOO, whose second element is stored in the space following\n\
2717 where FOO is stored, etc. FOO must be an expression whose value\n\
2718 resides in memory.\n\
2720 EXP may be preceded with /FMT, where FMT is a format letter\n\
2721 but no count or size letter (see \"x\" command)."));
2722 set_cmd_completer (c
, expression_completer
);
2723 add_com_alias ("p", "print", class_vars
, 1);
2725 c
= add_com ("inspect", class_vars
, inspect_command
, _("\
2726 Same as \"print\" command, except that if you are running in the epoch\n\
2727 environment, the value is printed in its own window."));
2728 set_cmd_completer (c
, expression_completer
);
2730 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class
,
2731 &max_symbolic_offset
, _("\
2732 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2733 Show the largest offset that will be printed in <symbol+1234> form."), NULL
,
2735 show_max_symbolic_offset
,
2736 &setprintlist
, &showprintlist
);
2737 add_setshow_boolean_cmd ("symbol-filename", no_class
,
2738 &print_symbol_filename
, _("\
2739 Set printing of source filename and line number with <symbol>."), _("\
2740 Show printing of source filename and line number with <symbol>."), NULL
,
2742 show_print_symbol_filename
,
2743 &setprintlist
, &showprintlist
);