Update
[gdb.git] / gdb / printcmd.c
blobe73c511e673626a739e4bc0eba6e7bff62356c5b
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 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/>. */
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "frame.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "value.h"
28 #include "language.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "target.h"
33 #include "breakpoint.h"
34 #include "demangle.h"
35 #include "valprint.h"
36 #include "annotate.h"
37 #include "symfile.h" /* for overlay functions */
38 #include "objfiles.h" /* ditto */
39 #include "completer.h" /* for completion functions */
40 #include "ui-out.h"
41 #include "gdb_assert.h"
42 #include "block.h"
43 #include "disasm.h"
44 #include "dfp.h"
46 #ifdef TUI
47 #include "tui/tui.h" /* For tui_active et.al. */
48 #endif
50 extern int asm_demangle; /* Whether to demangle syms in asm printouts */
51 extern int addressprint; /* Whether to print hex addresses in HLL " */
53 struct format_data
55 int count;
56 char format;
57 char size;
60 /* Last specified output format. */
62 static char last_format = 'x';
64 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
66 static char last_size = 'w';
68 /* Default address to examine next. */
70 static CORE_ADDR next_address;
72 /* Number of delay instructions following current disassembled insn. */
74 static int branch_delay_insns;
76 /* Last address examined. */
78 static CORE_ADDR last_examine_address;
80 /* Contents of last address examined.
81 This is not valid past the end of the `x' command! */
83 static struct value *last_examine_value;
85 /* Largest offset between a symbolic value and an address, that will be
86 printed as `0x1234 <symbol+offset>'. */
88 static unsigned int max_symbolic_offset = UINT_MAX;
89 static void
90 show_max_symbolic_offset (struct ui_file *file, int from_tty,
91 struct cmd_list_element *c, const char *value)
93 fprintf_filtered (file, _("\
94 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
95 value);
98 /* Append the source filename and linenumber of the symbol when
99 printing a symbolic value as `<symbol at filename:linenum>' if set. */
100 static int print_symbol_filename = 0;
101 static void
102 show_print_symbol_filename (struct ui_file *file, int from_tty,
103 struct cmd_list_element *c, const char *value)
105 fprintf_filtered (file, _("\
106 Printing of source filename and line number with <symbol> is %s.\n"),
107 value);
110 /* Number of auto-display expression currently being displayed.
111 So that we can disable it if we get an error or a signal within it.
112 -1 when not doing one. */
114 int current_display_number;
116 /* Flag to low-level print routines that this value is being printed
117 in an epoch window. We'd like to pass this as a parameter, but
118 every routine would need to take it. Perhaps we can encapsulate
119 this in the I/O stream once we have GNU stdio. */
121 int inspect_it = 0;
123 struct display
125 /* Chain link to next auto-display item. */
126 struct display *next;
127 /* Expression to be evaluated and displayed. */
128 struct expression *exp;
129 /* Item number of this auto-display item. */
130 int number;
131 /* Display format specified. */
132 struct format_data format;
133 /* Innermost block required by this expression when evaluated */
134 struct block *block;
135 /* Status of this display (enabled or disabled) */
136 int enabled_p;
139 /* Chain of expressions whose values should be displayed
140 automatically each time the program stops. */
142 static struct display *display_chain;
144 static int display_number;
146 /* Prototypes for exported functions. */
148 void output_command (char *, int);
150 void _initialize_printcmd (void);
152 /* Prototypes for local functions. */
154 static void do_one_display (struct display *);
157 /* Decode a format specification. *STRING_PTR should point to it.
158 OFORMAT and OSIZE are used as defaults for the format and size
159 if none are given in the format specification.
160 If OSIZE is zero, then the size field of the returned value
161 should be set only if a size is explicitly specified by the
162 user.
163 The structure returned describes all the data
164 found in the specification. In addition, *STRING_PTR is advanced
165 past the specification and past all whitespace following it. */
167 static struct format_data
168 decode_format (char **string_ptr, int oformat, int osize)
170 struct format_data val;
171 char *p = *string_ptr;
173 val.format = '?';
174 val.size = '?';
175 val.count = 1;
177 if (*p >= '0' && *p <= '9')
178 val.count = atoi (p);
179 while (*p >= '0' && *p <= '9')
180 p++;
182 /* Now process size or format letters that follow. */
184 while (1)
186 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
187 val.size = *p++;
188 else if (*p >= 'a' && *p <= 'z')
189 val.format = *p++;
190 else
191 break;
194 while (*p == ' ' || *p == '\t')
195 p++;
196 *string_ptr = p;
198 /* Set defaults for format and size if not specified. */
199 if (val.format == '?')
201 if (val.size == '?')
203 /* Neither has been specified. */
204 val.format = oformat;
205 val.size = osize;
207 else
208 /* If a size is specified, any format makes a reasonable
209 default except 'i'. */
210 val.format = oformat == 'i' ? 'x' : oformat;
212 else if (val.size == '?')
213 switch (val.format)
215 case 'a':
216 case 's':
217 /* Pick the appropriate size for an address. */
218 if (gdbarch_ptr_bit (current_gdbarch) == 64)
219 val.size = osize ? 'g' : osize;
220 else if (gdbarch_ptr_bit (current_gdbarch) == 32)
221 val.size = osize ? 'w' : osize;
222 else if (gdbarch_ptr_bit (current_gdbarch) == 16)
223 val.size = osize ? 'h' : osize;
224 else
225 /* Bad value for gdbarch_ptr_bit. */
226 internal_error (__FILE__, __LINE__,
227 _("failed internal consistency check"));
228 break;
229 case 'f':
230 /* Floating point has to be word or giantword. */
231 if (osize == 'w' || osize == 'g')
232 val.size = osize;
233 else
234 /* Default it to giantword if the last used size is not
235 appropriate. */
236 val.size = osize ? 'g' : osize;
237 break;
238 case 'c':
239 /* Characters default to one byte. */
240 val.size = osize ? 'b' : osize;
241 break;
242 default:
243 /* The default is the size most recently specified. */
244 val.size = osize;
247 return val;
250 /* Print value VAL on stream according to FORMAT, a letter or 0.
251 Do not end with a newline.
252 0 means print VAL according to its own type.
253 SIZE is the letter for the size of datum being printed.
254 This is used to pad hex numbers so they line up. SIZE is 0
255 for print / output and set for examine. */
257 static void
258 print_formatted (struct value *val, int format, int size,
259 struct ui_file *stream)
261 struct type *type = check_typedef (value_type (val));
262 int len = TYPE_LENGTH (type);
264 if (VALUE_LVAL (val) == lval_memory)
265 next_address = VALUE_ADDRESS (val) + len;
267 if (size)
269 switch (format)
271 case 's':
272 /* FIXME: Need to handle wchar_t's here... */
273 next_address = VALUE_ADDRESS (val)
274 + val_print_string (VALUE_ADDRESS (val), -1, 1, stream);
275 return;
277 case 'i':
278 /* We often wrap here if there are long symbolic names. */
279 wrap_here (" ");
280 next_address = (VALUE_ADDRESS (val)
281 + gdb_print_insn (VALUE_ADDRESS (val), stream,
282 &branch_delay_insns));
283 return;
287 if (format == 0 || format == 's'
288 || TYPE_CODE (type) == TYPE_CODE_REF
289 || TYPE_CODE (type) == TYPE_CODE_ARRAY
290 || TYPE_CODE (type) == TYPE_CODE_STRING
291 || TYPE_CODE (type) == TYPE_CODE_STRUCT
292 || TYPE_CODE (type) == TYPE_CODE_UNION
293 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
294 /* If format is 0, use the 'natural' format for that type of
295 value. If the type is non-scalar, we have to use language
296 rules to print it as a series of scalars. */
297 value_print (val, stream, format, Val_pretty_default);
298 else
299 /* User specified format, so don't look to the the type to
300 tell us what to do. */
301 print_scalar_formatted (value_contents (val), type,
302 format, size, stream);
305 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
306 according to letters FORMAT and SIZE on STREAM.
307 FORMAT may not be zero. Formats s and i are not supported at this level.
309 This is how the elements of an array or structure are printed
310 with a format. */
312 void
313 print_scalar_formatted (const void *valaddr, struct type *type,
314 int format, int size, struct ui_file *stream)
316 LONGEST val_long = 0;
317 unsigned int len = TYPE_LENGTH (type);
319 /* If we get here with a string format, try again without it. Go
320 all the way back to the language printers, which may call us
321 again. */
322 if (format == 's')
324 val_print (type, valaddr, 0, 0, stream, 0, 0, 0, Val_pretty_default);
325 return;
328 if (len > sizeof(LONGEST) &&
329 (TYPE_CODE (type) == TYPE_CODE_INT
330 || TYPE_CODE (type) == TYPE_CODE_ENUM))
332 switch (format)
334 case 'o':
335 print_octal_chars (stream, valaddr, len);
336 return;
337 case 'u':
338 case 'd':
339 print_decimal_chars (stream, valaddr, len);
340 return;
341 case 't':
342 print_binary_chars (stream, valaddr, len);
343 return;
344 case 'x':
345 print_hex_chars (stream, valaddr, len);
346 return;
347 case 'c':
348 print_char_chars (stream, valaddr, len);
349 return;
350 default:
351 break;
355 if (format != 'f')
356 val_long = unpack_long (type, valaddr);
358 /* If the value is a pointer, and pointers and addresses are not the
359 same, then at this point, the value's length (in target bytes) is
360 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
361 if (TYPE_CODE (type) == TYPE_CODE_PTR)
362 len = gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT;
364 /* If we are printing it as unsigned, truncate it in case it is actually
365 a negative signed value (e.g. "print/u (short)-1" should print 65535
366 (if shorts are 16 bits) instead of 4294967295). */
367 if (format != 'd')
369 if (len < sizeof (LONGEST))
370 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
373 switch (format)
375 case 'x':
376 if (!size)
378 /* No size specified, like in print. Print varying # of digits. */
379 print_longest (stream, 'x', 1, val_long);
381 else
382 switch (size)
384 case 'b':
385 case 'h':
386 case 'w':
387 case 'g':
388 print_longest (stream, size, 1, val_long);
389 break;
390 default:
391 error (_("Undefined output size \"%c\"."), size);
393 break;
395 case 'd':
396 print_longest (stream, 'd', 1, val_long);
397 break;
399 case 'u':
400 print_longest (stream, 'u', 0, val_long);
401 break;
403 case 'o':
404 if (val_long)
405 print_longest (stream, 'o', 1, val_long);
406 else
407 fprintf_filtered (stream, "0");
408 break;
410 case 'a':
412 CORE_ADDR addr = unpack_pointer (type, valaddr);
413 print_address (addr, stream);
415 break;
417 case 'c':
418 if (TYPE_UNSIGNED (type))
420 struct type *utype;
422 utype = builtin_type (current_gdbarch)->builtin_true_unsigned_char;
423 value_print (value_from_longest (utype, val_long),
424 stream, 0, Val_pretty_default);
426 else
427 value_print (value_from_longest (builtin_type_true_char, val_long),
428 stream, 0, Val_pretty_default);
429 break;
431 case 'f':
432 if (len == TYPE_LENGTH (builtin_type_float))
433 type = builtin_type_float;
434 else if (len == TYPE_LENGTH (builtin_type_double))
435 type = builtin_type_double;
436 else if (len == TYPE_LENGTH (builtin_type_long_double))
437 type = builtin_type_long_double;
438 print_floating (valaddr, type, stream);
439 break;
441 case 0:
442 internal_error (__FILE__, __LINE__,
443 _("failed internal consistency check"));
445 case 't':
446 /* Binary; 't' stands for "two". */
448 char bits[8 * (sizeof val_long) + 1];
449 char buf[8 * (sizeof val_long) + 32];
450 char *cp = bits;
451 int width;
453 if (!size)
454 width = 8 * (sizeof val_long);
455 else
456 switch (size)
458 case 'b':
459 width = 8;
460 break;
461 case 'h':
462 width = 16;
463 break;
464 case 'w':
465 width = 32;
466 break;
467 case 'g':
468 width = 64;
469 break;
470 default:
471 error (_("Undefined output size \"%c\"."), size);
474 bits[width] = '\0';
475 while (width-- > 0)
477 bits[width] = (val_long & 1) ? '1' : '0';
478 val_long >>= 1;
480 if (!size)
482 while (*cp && *cp == '0')
483 cp++;
484 if (*cp == '\0')
485 cp--;
487 strcpy (buf, cp);
488 fputs_filtered (buf, stream);
490 break;
492 default:
493 error (_("Undefined output format \"%c\"."), format);
497 /* Specify default address for `x' command.
498 The `info lines' command uses this. */
500 void
501 set_next_address (CORE_ADDR addr)
503 next_address = addr;
505 /* Make address available to the user as $_. */
506 set_internalvar (lookup_internalvar ("_"),
507 value_from_pointer (lookup_pointer_type (builtin_type_void),
508 addr));
511 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
512 after LEADIN. Print nothing if no symbolic name is found nearby.
513 Optionally also print source file and line number, if available.
514 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
515 or to interpret it as a possible C++ name and convert it back to source
516 form. However note that DO_DEMANGLE can be overridden by the specific
517 settings of the demangle and asm_demangle variables. */
519 void
520 print_address_symbolic (CORE_ADDR addr, struct ui_file *stream,
521 int do_demangle, char *leadin)
523 char *name = NULL;
524 char *filename = NULL;
525 int unmapped = 0;
526 int offset = 0;
527 int line = 0;
529 /* Throw away both name and filename. */
530 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
531 make_cleanup (free_current_contents, &filename);
533 if (build_address_symbolic (addr, do_demangle, &name, &offset,
534 &filename, &line, &unmapped))
536 do_cleanups (cleanup_chain);
537 return;
540 fputs_filtered (leadin, stream);
541 if (unmapped)
542 fputs_filtered ("<*", stream);
543 else
544 fputs_filtered ("<", stream);
545 fputs_filtered (name, stream);
546 if (offset != 0)
547 fprintf_filtered (stream, "+%u", (unsigned int) offset);
549 /* Append source filename and line number if desired. Give specific
550 line # of this addr, if we have it; else line # of the nearest symbol. */
551 if (print_symbol_filename && filename != NULL)
553 if (line != -1)
554 fprintf_filtered (stream, " at %s:%d", filename, line);
555 else
556 fprintf_filtered (stream, " in %s", filename);
558 if (unmapped)
559 fputs_filtered ("*>", stream);
560 else
561 fputs_filtered (">", stream);
563 do_cleanups (cleanup_chain);
566 /* Given an address ADDR return all the elements needed to print the
567 address in a symbolic form. NAME can be mangled or not depending
568 on DO_DEMANGLE (and also on the asm_demangle global variable,
569 manipulated via ''set print asm-demangle''). Return 0 in case of
570 success, when all the info in the OUT paramters is valid. Return 1
571 otherwise. */
573 build_address_symbolic (CORE_ADDR addr, /* IN */
574 int do_demangle, /* IN */
575 char **name, /* OUT */
576 int *offset, /* OUT */
577 char **filename, /* OUT */
578 int *line, /* OUT */
579 int *unmapped) /* OUT */
581 struct minimal_symbol *msymbol;
582 struct symbol *symbol;
583 CORE_ADDR name_location = 0;
584 asection *section = 0;
585 char *name_temp = "";
587 /* Let's say it is unmapped. */
588 *unmapped = 0;
590 /* Determine if the address is in an overlay, and whether it is
591 mapped. */
592 if (overlay_debugging)
594 section = find_pc_overlay (addr);
595 if (pc_in_unmapped_range (addr, section))
597 *unmapped = 1;
598 addr = overlay_mapped_address (addr, section);
602 /* First try to find the address in the symbol table, then
603 in the minsyms. Take the closest one. */
605 /* This is defective in the sense that it only finds text symbols. So
606 really this is kind of pointless--we should make sure that the
607 minimal symbols have everything we need (by changing that we could
608 save some memory, but for many debug format--ELF/DWARF or
609 anything/stabs--it would be inconvenient to eliminate those minimal
610 symbols anyway). */
611 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
612 symbol = find_pc_sect_function (addr, section);
614 if (symbol)
616 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
617 if (do_demangle || asm_demangle)
618 name_temp = SYMBOL_PRINT_NAME (symbol);
619 else
620 name_temp = DEPRECATED_SYMBOL_NAME (symbol);
623 if (msymbol != NULL)
625 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
627 /* The msymbol is closer to the address than the symbol;
628 use the msymbol instead. */
629 symbol = 0;
630 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
631 if (do_demangle || asm_demangle)
632 name_temp = SYMBOL_PRINT_NAME (msymbol);
633 else
634 name_temp = DEPRECATED_SYMBOL_NAME (msymbol);
637 if (symbol == NULL && msymbol == NULL)
638 return 1;
640 /* If the nearest symbol is too far away, don't print anything symbolic. */
642 /* For when CORE_ADDR is larger than unsigned int, we do math in
643 CORE_ADDR. But when we detect unsigned wraparound in the
644 CORE_ADDR math, we ignore this test and print the offset,
645 because addr+max_symbolic_offset has wrapped through the end
646 of the address space back to the beginning, giving bogus comparison. */
647 if (addr > name_location + max_symbolic_offset
648 && name_location + max_symbolic_offset > name_location)
649 return 1;
651 *offset = addr - name_location;
653 *name = xstrdup (name_temp);
655 if (print_symbol_filename)
657 struct symtab_and_line sal;
659 sal = find_pc_sect_line (addr, section, 0);
661 if (sal.symtab)
663 *filename = xstrdup (sal.symtab->filename);
664 *line = sal.line;
667 return 0;
670 /* Print address ADDR on STREAM. USE_LOCAL means the same thing as for
671 print_longest. */
672 void
673 deprecated_print_address_numeric (CORE_ADDR addr, int use_local,
674 struct ui_file *stream)
676 if (use_local)
677 fputs_filtered (paddress (addr), stream);
678 else
680 int addr_bit = gdbarch_addr_bit (current_gdbarch);
682 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
683 addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
684 print_longest (stream, 'x', 0, (ULONGEST) addr);
688 /* Print address ADDR symbolically on STREAM.
689 First print it as a number. Then perhaps print
690 <SYMBOL + OFFSET> after the number. */
692 void
693 print_address (CORE_ADDR addr, struct ui_file *stream)
695 fputs_filtered (paddress (addr), stream);
696 print_address_symbolic (addr, stream, asm_demangle, " ");
699 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
700 controls whether to print the symbolic name "raw" or demangled.
701 Global setting "addressprint" controls whether to print hex address
702 or not. */
704 void
705 print_address_demangle (CORE_ADDR addr, struct ui_file *stream,
706 int do_demangle)
708 if (addr == 0)
710 fprintf_filtered (stream, "0");
712 else if (addressprint)
714 fputs_filtered (paddress (addr), stream);
715 print_address_symbolic (addr, stream, do_demangle, " ");
717 else
719 print_address_symbolic (addr, stream, do_demangle, "");
724 /* These are the types that $__ will get after an examine command of one
725 of these sizes. */
727 static struct type *examine_i_type;
729 static struct type *examine_b_type;
730 static struct type *examine_h_type;
731 static struct type *examine_w_type;
732 static struct type *examine_g_type;
734 /* Examine data at address ADDR in format FMT.
735 Fetch it from memory and print on gdb_stdout. */
737 static void
738 do_examine (struct format_data fmt, CORE_ADDR addr)
740 char format = 0;
741 char size;
742 int count = 1;
743 struct type *val_type = NULL;
744 int i;
745 int maxelts;
747 format = fmt.format;
748 size = fmt.size;
749 count = fmt.count;
750 next_address = addr;
752 /* String or instruction format implies fetch single bytes
753 regardless of the specified size. */
754 if (format == 's' || format == 'i')
755 size = 'b';
757 if (format == 'i')
758 val_type = examine_i_type;
759 else if (size == 'b')
760 val_type = examine_b_type;
761 else if (size == 'h')
762 val_type = examine_h_type;
763 else if (size == 'w')
764 val_type = examine_w_type;
765 else if (size == 'g')
766 val_type = examine_g_type;
768 maxelts = 8;
769 if (size == 'w')
770 maxelts = 4;
771 if (size == 'g')
772 maxelts = 2;
773 if (format == 's' || format == 'i')
774 maxelts = 1;
776 /* Print as many objects as specified in COUNT, at most maxelts per line,
777 with the address of the next one at the start of each line. */
779 while (count > 0)
781 QUIT;
782 print_address (next_address, gdb_stdout);
783 printf_filtered (":");
784 for (i = maxelts;
785 i > 0 && count > 0;
786 i--, count--)
788 printf_filtered ("\t");
789 /* Note that print_formatted sets next_address for the next
790 object. */
791 last_examine_address = next_address;
793 if (last_examine_value)
794 value_free (last_examine_value);
796 /* The value to be displayed is not fetched greedily.
797 Instead, to avoid the possibility of a fetched value not
798 being used, its retrieval is delayed until the print code
799 uses it. When examining an instruction stream, the
800 disassembler will perform its own memory fetch using just
801 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
802 the disassembler be modified so that LAST_EXAMINE_VALUE
803 is left with the byte sequence from the last complete
804 instruction fetched from memory? */
805 last_examine_value = value_at_lazy (val_type, next_address);
807 if (last_examine_value)
808 release_value (last_examine_value);
810 print_formatted (last_examine_value, format, size, gdb_stdout);
812 /* Display any branch delay slots following the final insn. */
813 if (format == 'i' && count == 1)
814 count += branch_delay_insns;
816 printf_filtered ("\n");
817 gdb_flush (gdb_stdout);
821 static void
822 validate_format (struct format_data fmt, char *cmdname)
824 if (fmt.size != 0)
825 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
826 if (fmt.count != 1)
827 error (_("Item count other than 1 is meaningless in \"%s\" command."),
828 cmdname);
829 if (fmt.format == 'i')
830 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
831 fmt.format, cmdname);
834 /* Evaluate string EXP as an expression in the current language and
835 print the resulting value. EXP may contain a format specifier as the
836 first argument ("/x myvar" for example, to print myvar in hex). */
838 static void
839 print_command_1 (char *exp, int inspect, int voidprint)
841 struct expression *expr;
842 struct cleanup *old_chain = 0;
843 char format = 0;
844 struct value *val;
845 struct format_data fmt;
846 int cleanup = 0;
848 /* Pass inspect flag to the rest of the print routines in a global
849 (sigh). */
850 inspect_it = inspect;
852 if (exp && *exp == '/')
854 exp++;
855 fmt = decode_format (&exp, last_format, 0);
856 validate_format (fmt, "print");
857 last_format = format = fmt.format;
859 else
861 fmt.count = 1;
862 fmt.format = 0;
863 fmt.size = 0;
866 if (exp && *exp)
868 struct type *type;
869 expr = parse_expression (exp);
870 old_chain = make_cleanup (free_current_contents, &expr);
871 cleanup = 1;
872 val = evaluate_expression (expr);
874 else
875 val = access_value_history (0);
877 if (voidprint || (val && value_type (val) &&
878 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
880 int histindex = record_latest_value (val);
882 if (histindex >= 0)
883 annotate_value_history_begin (histindex, value_type (val));
884 else
885 annotate_value_begin (value_type (val));
887 if (inspect)
888 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
889 exp, histindex);
890 else if (histindex >= 0)
891 printf_filtered ("$%d = ", histindex);
893 if (histindex >= 0)
894 annotate_value_history_value ();
896 print_formatted (val, format, fmt.size, gdb_stdout);
897 printf_filtered ("\n");
899 if (histindex >= 0)
900 annotate_value_history_end ();
901 else
902 annotate_value_end ();
904 if (inspect)
905 printf_unfiltered ("\") )\030");
908 if (cleanup)
909 do_cleanups (old_chain);
910 inspect_it = 0; /* Reset print routines to normal. */
913 static void
914 print_command (char *exp, int from_tty)
916 print_command_1 (exp, 0, 1);
919 /* Same as print, except in epoch, it gets its own window. */
920 static void
921 inspect_command (char *exp, int from_tty)
923 extern int epoch_interface;
925 print_command_1 (exp, epoch_interface, 1);
928 /* Same as print, except it doesn't print void results. */
929 static void
930 call_command (char *exp, int from_tty)
932 print_command_1 (exp, 0, 0);
935 void
936 output_command (char *exp, int from_tty)
938 struct expression *expr;
939 struct cleanup *old_chain;
940 char format = 0;
941 struct value *val;
942 struct format_data fmt;
944 fmt.size = 0;
946 if (exp && *exp == '/')
948 exp++;
949 fmt = decode_format (&exp, 0, 0);
950 validate_format (fmt, "output");
951 format = fmt.format;
954 expr = parse_expression (exp);
955 old_chain = make_cleanup (free_current_contents, &expr);
957 val = evaluate_expression (expr);
959 annotate_value_begin (value_type (val));
961 print_formatted (val, format, fmt.size, gdb_stdout);
963 annotate_value_end ();
965 wrap_here ("");
966 gdb_flush (gdb_stdout);
968 do_cleanups (old_chain);
971 static void
972 set_command (char *exp, int from_tty)
974 struct expression *expr = parse_expression (exp);
975 struct cleanup *old_chain =
976 make_cleanup (free_current_contents, &expr);
977 evaluate_expression (expr);
978 do_cleanups (old_chain);
981 static void
982 sym_info (char *arg, int from_tty)
984 struct minimal_symbol *msymbol;
985 struct objfile *objfile;
986 struct obj_section *osect;
987 asection *sect;
988 CORE_ADDR addr, sect_addr;
989 int matches = 0;
990 unsigned int offset;
992 if (!arg)
993 error_no_arg (_("address"));
995 addr = parse_and_eval_address (arg);
996 ALL_OBJSECTIONS (objfile, osect)
998 /* Only process each object file once, even if there's a separate
999 debug file. */
1000 if (objfile->separate_debug_objfile_backlink)
1001 continue;
1003 sect = osect->the_bfd_section;
1004 sect_addr = overlay_mapped_address (addr, sect);
1006 if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
1007 (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
1009 matches = 1;
1010 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1011 if (offset)
1012 printf_filtered ("%s + %u in ",
1013 SYMBOL_PRINT_NAME (msymbol), offset);
1014 else
1015 printf_filtered ("%s in ",
1016 SYMBOL_PRINT_NAME (msymbol));
1017 if (pc_in_unmapped_range (addr, sect))
1018 printf_filtered (_("load address range of "));
1019 if (section_is_overlay (sect))
1020 printf_filtered (_("%s overlay "),
1021 section_is_mapped (sect) ? "mapped" : "unmapped");
1022 printf_filtered (_("section %s"), sect->name);
1023 printf_filtered ("\n");
1026 if (matches == 0)
1027 printf_filtered (_("No symbol matches %s.\n"), arg);
1030 static void
1031 address_info (char *exp, int from_tty)
1033 struct symbol *sym;
1034 struct minimal_symbol *msymbol;
1035 long val;
1036 long basereg;
1037 asection *section;
1038 CORE_ADDR load_addr;
1039 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1040 if exp is a field of `this'. */
1042 if (exp == 0)
1043 error (_("Argument required."));
1045 sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
1046 &is_a_field_of_this, (struct symtab **) NULL);
1047 if (sym == NULL)
1049 if (is_a_field_of_this)
1051 printf_filtered ("Symbol \"");
1052 fprintf_symbol_filtered (gdb_stdout, exp,
1053 current_language->la_language, DMGL_ANSI);
1054 printf_filtered ("\" is a field of the local class variable ");
1055 if (current_language->la_language == language_objc)
1056 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1057 else
1058 printf_filtered ("`this'\n");
1059 return;
1062 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1064 if (msymbol != NULL)
1066 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1068 printf_filtered ("Symbol \"");
1069 fprintf_symbol_filtered (gdb_stdout, exp,
1070 current_language->la_language, DMGL_ANSI);
1071 printf_filtered ("\" is at ");
1072 fputs_filtered (paddress (load_addr), gdb_stdout);
1073 printf_filtered (" in a file compiled without debugging");
1074 section = SYMBOL_BFD_SECTION (msymbol);
1075 if (section_is_overlay (section))
1077 load_addr = overlay_unmapped_address (load_addr, section);
1078 printf_filtered (",\n -- loaded at ");
1079 fputs_filtered (paddress (load_addr), gdb_stdout);
1080 printf_filtered (" in overlay section %s", section->name);
1082 printf_filtered (".\n");
1084 else
1085 error (_("No symbol \"%s\" in current context."), exp);
1086 return;
1089 printf_filtered ("Symbol \"");
1090 fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym),
1091 current_language->la_language, DMGL_ANSI);
1092 printf_filtered ("\" is ");
1093 val = SYMBOL_VALUE (sym);
1094 basereg = SYMBOL_BASEREG (sym);
1095 section = SYMBOL_BFD_SECTION (sym);
1097 switch (SYMBOL_CLASS (sym))
1099 case LOC_CONST:
1100 case LOC_CONST_BYTES:
1101 printf_filtered ("constant");
1102 break;
1104 case LOC_LABEL:
1105 printf_filtered ("a label at address ");
1106 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1107 gdb_stdout);
1108 if (section_is_overlay (section))
1110 load_addr = overlay_unmapped_address (load_addr, section);
1111 printf_filtered (",\n -- loaded at ");
1112 fputs_filtered (paddress (load_addr), gdb_stdout);
1113 printf_filtered (" in overlay section %s", section->name);
1115 break;
1117 case LOC_COMPUTED:
1118 case LOC_COMPUTED_ARG:
1119 /* FIXME: cagney/2004-01-26: It should be possible to
1120 unconditionally call the SYMBOL_OPS method when available.
1121 Unfortunately DWARF 2 stores the frame-base (instead of the
1122 function) location in a function's symbol. Oops! For the
1123 moment enable this when/where applicable. */
1124 SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
1125 break;
1127 case LOC_REGISTER:
1128 printf_filtered (_("a variable in register %s"),
1129 gdbarch_register_name (current_gdbarch, val));
1130 break;
1132 case LOC_STATIC:
1133 printf_filtered (_("static storage at address "));
1134 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1135 gdb_stdout);
1136 if (section_is_overlay (section))
1138 load_addr = overlay_unmapped_address (load_addr, section);
1139 printf_filtered (_(",\n -- loaded at "));
1140 fputs_filtered (paddress (load_addr), gdb_stdout);
1141 printf_filtered (_(" in overlay section %s"), section->name);
1143 break;
1145 case LOC_INDIRECT:
1146 printf_filtered (_("external global (indirect addressing), at address *("));
1147 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1148 gdb_stdout);
1149 printf_filtered (")");
1150 if (section_is_overlay (section))
1152 load_addr = overlay_unmapped_address (load_addr, section);
1153 printf_filtered (_(",\n -- loaded at "));
1154 fputs_filtered (paddress (load_addr), gdb_stdout);
1155 printf_filtered (_(" in overlay section %s"), section->name);
1157 break;
1159 case LOC_REGPARM:
1160 printf_filtered (_("an argument in register %s"),
1161 gdbarch_register_name (current_gdbarch, val));
1162 break;
1164 case LOC_REGPARM_ADDR:
1165 printf_filtered (_("address of an argument in register %s"),
1166 gdbarch_register_name (current_gdbarch, val));
1167 break;
1169 case LOC_ARG:
1170 printf_filtered (_("an argument at offset %ld"), val);
1171 break;
1173 case LOC_LOCAL_ARG:
1174 printf_filtered (_("an argument at frame offset %ld"), val);
1175 break;
1177 case LOC_LOCAL:
1178 printf_filtered (_("a local variable at frame offset %ld"), val);
1179 break;
1181 case LOC_REF_ARG:
1182 printf_filtered (_("a reference argument at offset %ld"), val);
1183 break;
1185 case LOC_BASEREG:
1186 printf_filtered (_("a variable at offset %ld from register %s"),
1187 val, gdbarch_register_name (current_gdbarch, basereg));
1188 break;
1190 case LOC_BASEREG_ARG:
1191 printf_filtered (_("an argument at offset %ld from register %s"),
1192 val, gdbarch_register_name (current_gdbarch, basereg));
1193 break;
1195 case LOC_TYPEDEF:
1196 printf_filtered (_("a typedef"));
1197 break;
1199 case LOC_BLOCK:
1200 printf_filtered (_("a function at address "));
1201 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1202 fputs_filtered (paddress (load_addr), gdb_stdout);
1203 if (section_is_overlay (section))
1205 load_addr = overlay_unmapped_address (load_addr, section);
1206 printf_filtered (_(",\n -- loaded at "));
1207 fputs_filtered (paddress (load_addr), gdb_stdout);
1208 printf_filtered (_(" in overlay section %s"), section->name);
1210 break;
1212 case LOC_UNRESOLVED:
1214 struct minimal_symbol *msym;
1216 msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL);
1217 if (msym == NULL)
1218 printf_filtered ("unresolved");
1219 else
1221 section = SYMBOL_BFD_SECTION (msym);
1222 printf_filtered (_("static storage at address "));
1223 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1224 fputs_filtered (paddress (load_addr), gdb_stdout);
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"), section->name);
1234 break;
1236 case LOC_HP_THREAD_LOCAL_STATIC:
1237 printf_filtered (_("\
1238 a thread-local variable at offset %ld from the thread base register %s"),
1239 val, gdbarch_register_name (current_gdbarch, basereg));
1240 break;
1242 case LOC_OPTIMIZED_OUT:
1243 printf_filtered (_("optimized out"));
1244 break;
1246 default:
1247 printf_filtered (_("of unknown (botched) type"));
1248 break;
1250 printf_filtered (".\n");
1254 static void
1255 x_command (char *exp, int from_tty)
1257 struct expression *expr;
1258 struct format_data fmt;
1259 struct cleanup *old_chain;
1260 struct value *val;
1262 fmt.format = last_format;
1263 fmt.size = last_size;
1264 fmt.count = 1;
1266 if (exp && *exp == '/')
1268 exp++;
1269 fmt = decode_format (&exp, last_format, last_size);
1272 /* If we have an expression, evaluate it and use it as the address. */
1274 if (exp != 0 && *exp != 0)
1276 expr = parse_expression (exp);
1277 /* Cause expression not to be there any more if this command is
1278 repeated with Newline. But don't clobber a user-defined
1279 command's definition. */
1280 if (from_tty)
1281 *exp = 0;
1282 old_chain = make_cleanup (free_current_contents, &expr);
1283 val = evaluate_expression (expr);
1284 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1285 val = value_ind (val);
1286 /* In rvalue contexts, such as this, functions are coerced into
1287 pointers to functions. This makes "x/i main" work. */
1288 if (/* last_format == 'i' && */
1289 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1290 && VALUE_LVAL (val) == lval_memory)
1291 next_address = VALUE_ADDRESS (val);
1292 else
1293 next_address = value_as_address (val);
1294 do_cleanups (old_chain);
1297 do_examine (fmt, next_address);
1299 /* If the examine succeeds, we remember its size and format for next
1300 time. */
1301 last_size = fmt.size;
1302 last_format = fmt.format;
1304 /* Set a couple of internal variables if appropriate. */
1305 if (last_examine_value)
1307 /* Make last address examined available to the user as $_. Use
1308 the correct pointer type. */
1309 struct type *pointer_type
1310 = lookup_pointer_type (value_type (last_examine_value));
1311 set_internalvar (lookup_internalvar ("_"),
1312 value_from_pointer (pointer_type,
1313 last_examine_address));
1315 /* Make contents of last address examined available to the user
1316 as $__. If the last value has not been fetched from memory
1317 then don't fetch it now; instead mark it by voiding the $__
1318 variable. */
1319 if (value_lazy (last_examine_value))
1320 set_internalvar (lookup_internalvar ("__"),
1321 allocate_value (builtin_type_void));
1322 else
1323 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1328 /* Add an expression to the auto-display chain.
1329 Specify the expression. */
1331 static void
1332 display_command (char *exp, int from_tty)
1334 struct format_data fmt;
1335 struct expression *expr;
1336 struct display *new;
1337 int display_it = 1;
1339 #if defined(TUI)
1340 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1341 `tui_version'. */
1342 if (tui_active && exp != NULL && *exp == '$')
1343 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1344 #endif
1346 if (display_it)
1348 if (exp == 0)
1350 do_displays ();
1351 return;
1354 if (*exp == '/')
1356 exp++;
1357 fmt = decode_format (&exp, 0, 0);
1358 if (fmt.size && fmt.format == 0)
1359 fmt.format = 'x';
1360 if (fmt.format == 'i' || fmt.format == 's')
1361 fmt.size = 'b';
1363 else
1365 fmt.format = 0;
1366 fmt.size = 0;
1367 fmt.count = 0;
1370 innermost_block = 0;
1371 expr = parse_expression (exp);
1373 new = (struct display *) xmalloc (sizeof (struct display));
1375 new->exp = expr;
1376 new->block = innermost_block;
1377 new->next = display_chain;
1378 new->number = ++display_number;
1379 new->format = fmt;
1380 new->enabled_p = 1;
1381 display_chain = new;
1383 if (from_tty && target_has_execution)
1384 do_one_display (new);
1386 dont_repeat ();
1390 static void
1391 free_display (struct display *d)
1393 xfree (d->exp);
1394 xfree (d);
1397 /* Clear out the display_chain. Done when new symtabs are loaded,
1398 since this invalidates the types stored in many expressions. */
1400 void
1401 clear_displays (void)
1403 struct display *d;
1405 while ((d = display_chain) != NULL)
1407 xfree (d->exp);
1408 display_chain = d->next;
1409 xfree (d);
1413 /* Delete the auto-display number NUM. */
1415 static void
1416 delete_display (int num)
1418 struct display *d1, *d;
1420 if (!display_chain)
1421 error (_("No display number %d."), num);
1423 if (display_chain->number == num)
1425 d1 = display_chain;
1426 display_chain = d1->next;
1427 free_display (d1);
1429 else
1430 for (d = display_chain;; d = d->next)
1432 if (d->next == 0)
1433 error (_("No display number %d."), num);
1434 if (d->next->number == num)
1436 d1 = d->next;
1437 d->next = d1->next;
1438 free_display (d1);
1439 break;
1444 /* Delete some values from the auto-display chain.
1445 Specify the element numbers. */
1447 static void
1448 undisplay_command (char *args, int from_tty)
1450 char *p = args;
1451 char *p1;
1452 int num;
1454 if (args == 0)
1456 if (query ("Delete all auto-display expressions? "))
1457 clear_displays ();
1458 dont_repeat ();
1459 return;
1462 while (*p)
1464 p1 = p;
1465 while (*p1 >= '0' && *p1 <= '9')
1466 p1++;
1467 if (*p1 && *p1 != ' ' && *p1 != '\t')
1468 error (_("Arguments must be display numbers."));
1470 num = atoi (p);
1472 delete_display (num);
1474 p = p1;
1475 while (*p == ' ' || *p == '\t')
1476 p++;
1478 dont_repeat ();
1481 /* Display a single auto-display.
1482 Do nothing if the display cannot be printed in the current context,
1483 or if the display is disabled. */
1485 static void
1486 do_one_display (struct display *d)
1488 int within_current_scope;
1490 if (d->enabled_p == 0)
1491 return;
1493 if (d->block)
1494 within_current_scope = contained_in (get_selected_block (0), d->block);
1495 else
1496 within_current_scope = 1;
1497 if (!within_current_scope)
1498 return;
1500 current_display_number = d->number;
1502 annotate_display_begin ();
1503 printf_filtered ("%d", d->number);
1504 annotate_display_number_end ();
1505 printf_filtered (": ");
1506 if (d->format.size)
1508 CORE_ADDR addr;
1509 struct value *val;
1511 annotate_display_format ();
1513 printf_filtered ("x/");
1514 if (d->format.count != 1)
1515 printf_filtered ("%d", d->format.count);
1516 printf_filtered ("%c", d->format.format);
1517 if (d->format.format != 'i' && d->format.format != 's')
1518 printf_filtered ("%c", d->format.size);
1519 printf_filtered (" ");
1521 annotate_display_expression ();
1523 print_expression (d->exp, gdb_stdout);
1524 annotate_display_expression_end ();
1526 if (d->format.count != 1 || d->format.format == 'i')
1527 printf_filtered ("\n");
1528 else
1529 printf_filtered (" ");
1531 val = evaluate_expression (d->exp);
1532 addr = value_as_address (val);
1533 if (d->format.format == 'i')
1534 addr = gdbarch_addr_bits_remove (current_gdbarch, addr);
1536 annotate_display_value ();
1538 do_examine (d->format, addr);
1540 else
1542 annotate_display_format ();
1544 if (d->format.format)
1545 printf_filtered ("/%c ", d->format.format);
1547 annotate_display_expression ();
1549 print_expression (d->exp, gdb_stdout);
1550 annotate_display_expression_end ();
1552 printf_filtered (" = ");
1554 annotate_display_expression ();
1556 print_formatted (evaluate_expression (d->exp),
1557 d->format.format, d->format.size, gdb_stdout);
1558 printf_filtered ("\n");
1561 annotate_display_end ();
1563 gdb_flush (gdb_stdout);
1564 current_display_number = -1;
1567 /* Display all of the values on the auto-display chain which can be
1568 evaluated in the current scope. */
1570 void
1571 do_displays (void)
1573 struct display *d;
1575 for (d = display_chain; d; d = d->next)
1576 do_one_display (d);
1579 /* Delete the auto-display which we were in the process of displaying.
1580 This is done when there is an error or a signal. */
1582 void
1583 disable_display (int num)
1585 struct display *d;
1587 for (d = display_chain; d; d = d->next)
1588 if (d->number == num)
1590 d->enabled_p = 0;
1591 return;
1593 printf_unfiltered (_("No display number %d.\n"), num);
1596 void
1597 disable_current_display (void)
1599 if (current_display_number >= 0)
1601 disable_display (current_display_number);
1602 fprintf_unfiltered (gdb_stderr, _("\
1603 Disabling display %d to avoid infinite recursion.\n"),
1604 current_display_number);
1606 current_display_number = -1;
1609 static void
1610 display_info (char *ignore, int from_tty)
1612 struct display *d;
1614 if (!display_chain)
1615 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1616 else
1617 printf_filtered (_("Auto-display expressions now in effect:\n\
1618 Num Enb Expression\n"));
1620 for (d = display_chain; d; d = d->next)
1622 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1623 if (d->format.size)
1624 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1625 d->format.format);
1626 else if (d->format.format)
1627 printf_filtered ("/%c ", d->format.format);
1628 print_expression (d->exp, gdb_stdout);
1629 if (d->block && !contained_in (get_selected_block (0), d->block))
1630 printf_filtered (_(" (cannot be evaluated in the current context)"));
1631 printf_filtered ("\n");
1632 gdb_flush (gdb_stdout);
1636 static void
1637 enable_display (char *args, int from_tty)
1639 char *p = args;
1640 char *p1;
1641 int num;
1642 struct display *d;
1644 if (p == 0)
1646 for (d = display_chain; d; d = d->next)
1647 d->enabled_p = 1;
1649 else
1650 while (*p)
1652 p1 = p;
1653 while (*p1 >= '0' && *p1 <= '9')
1654 p1++;
1655 if (*p1 && *p1 != ' ' && *p1 != '\t')
1656 error (_("Arguments must be display numbers."));
1658 num = atoi (p);
1660 for (d = display_chain; d; d = d->next)
1661 if (d->number == num)
1663 d->enabled_p = 1;
1664 goto win;
1666 printf_unfiltered (_("No display number %d.\n"), num);
1667 win:
1668 p = p1;
1669 while (*p == ' ' || *p == '\t')
1670 p++;
1674 static void
1675 disable_display_command (char *args, int from_tty)
1677 char *p = args;
1678 char *p1;
1679 struct display *d;
1681 if (p == 0)
1683 for (d = display_chain; d; d = d->next)
1684 d->enabled_p = 0;
1686 else
1687 while (*p)
1689 p1 = p;
1690 while (*p1 >= '0' && *p1 <= '9')
1691 p1++;
1692 if (*p1 && *p1 != ' ' && *p1 != '\t')
1693 error (_("Arguments must be display numbers."));
1695 disable_display (atoi (p));
1697 p = p1;
1698 while (*p == ' ' || *p == '\t')
1699 p++;
1704 /* Print the value in stack frame FRAME of a variable specified by a
1705 struct symbol. */
1707 void
1708 print_variable_value (struct symbol *var, struct frame_info *frame,
1709 struct ui_file *stream)
1711 struct value *val = read_var_value (var, frame);
1713 value_print (val, stream, 0, Val_pretty_default);
1716 static void
1717 printf_command (char *arg, int from_tty)
1719 char *f = NULL;
1720 char *s = arg;
1721 char *string = NULL;
1722 struct value **val_args;
1723 char *substrings;
1724 char *current_substring;
1725 int nargs = 0;
1726 int allocated_args = 20;
1727 struct cleanup *old_cleanups;
1729 val_args = xmalloc (allocated_args * sizeof (struct value *));
1730 old_cleanups = make_cleanup (free_current_contents, &val_args);
1732 if (s == 0)
1733 error_no_arg (_("format-control string and values to print"));
1735 /* Skip white space before format string */
1736 while (*s == ' ' || *s == '\t')
1737 s++;
1739 /* A format string should follow, enveloped in double quotes. */
1740 if (*s++ != '"')
1741 error (_("Bad format string, missing '\"'."));
1743 /* Parse the format-control string and copy it into the string STRING,
1744 processing some kinds of escape sequence. */
1746 f = string = (char *) alloca (strlen (s) + 1);
1748 while (*s != '"')
1750 int c = *s++;
1751 switch (c)
1753 case '\0':
1754 error (_("Bad format string, non-terminated '\"'."));
1756 case '\\':
1757 switch (c = *s++)
1759 case '\\':
1760 *f++ = '\\';
1761 break;
1762 case 'a':
1763 *f++ = '\a';
1764 break;
1765 case 'b':
1766 *f++ = '\b';
1767 break;
1768 case 'f':
1769 *f++ = '\f';
1770 break;
1771 case 'n':
1772 *f++ = '\n';
1773 break;
1774 case 'r':
1775 *f++ = '\r';
1776 break;
1777 case 't':
1778 *f++ = '\t';
1779 break;
1780 case 'v':
1781 *f++ = '\v';
1782 break;
1783 case '"':
1784 *f++ = '"';
1785 break;
1786 default:
1787 /* ??? TODO: handle other escape sequences */
1788 error (_("Unrecognized escape character \\%c in format string."),
1791 break;
1793 default:
1794 *f++ = c;
1798 /* Skip over " and following space and comma. */
1799 s++;
1800 *f++ = '\0';
1801 while (*s == ' ' || *s == '\t')
1802 s++;
1804 if (*s != ',' && *s != 0)
1805 error (_("Invalid argument syntax"));
1807 if (*s == ',')
1808 s++;
1809 while (*s == ' ' || *s == '\t')
1810 s++;
1812 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1813 substrings = alloca (strlen (string) * 2);
1814 current_substring = substrings;
1817 /* Now scan the string for %-specs and see what kinds of args they want.
1818 argclass[I] classifies the %-specs so we can give printf_filtered
1819 something of the right size. */
1821 enum argclass
1823 int_arg, long_arg, long_long_arg, ptr_arg, string_arg,
1824 double_arg, long_double_arg, decfloat_arg
1826 enum argclass *argclass;
1827 enum argclass this_argclass;
1828 char *last_arg;
1829 int nargs_wanted;
1830 int i;
1832 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1833 nargs_wanted = 0;
1834 f = string;
1835 last_arg = string;
1836 while (*f)
1837 if (*f++ == '%')
1839 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
1840 int seen_space = 0, seen_plus = 0;
1841 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
1842 int seen_big_d = 0, seen_double_big_d = 0;
1843 int bad = 0;
1845 /* Check the validity of the format specifier, and work
1846 out what argument it expects. We only accept C89
1847 format strings, with the exception of long long (which
1848 we autoconf for). */
1850 /* Skip over "%%". */
1851 if (*f == '%')
1853 f++;
1854 continue;
1857 /* The first part of a format specifier is a set of flag
1858 characters. */
1859 while (strchr ("0-+ #", *f))
1861 if (*f == '#')
1862 seen_hash = 1;
1863 else if (*f == '0')
1864 seen_zero = 1;
1865 else if (*f == ' ')
1866 seen_space = 1;
1867 else if (*f == '+')
1868 seen_plus = 1;
1869 f++;
1872 /* The next part of a format specifier is a width. */
1873 while (strchr ("0123456789", *f))
1874 f++;
1876 /* The next part of a format specifier is a precision. */
1877 if (*f == '.')
1879 seen_prec = 1;
1880 f++;
1881 while (strchr ("0123456789", *f))
1882 f++;
1885 /* The next part of a format specifier is a length modifier. */
1886 if (*f == 'h')
1888 seen_h = 1;
1889 f++;
1891 else if (*f == 'l')
1893 f++;
1894 lcount++;
1895 if (*f == 'l')
1897 f++;
1898 lcount++;
1901 else if (*f == 'L')
1903 seen_big_l = 1;
1904 f++;
1906 /* Decimal32 modifier. */
1907 else if (*f == 'H')
1909 seen_big_h = 1;
1910 f++;
1912 /* Decimal64 and Decimal128 modifiers. */
1913 else if (*f == 'D')
1915 f++;
1917 /* Check for a Decimal128. */
1918 if (*f == 'D')
1920 f++;
1921 seen_double_big_d = 1;
1923 else
1924 seen_big_d = 1;
1927 switch (*f)
1929 case 'u':
1930 if (seen_hash)
1931 bad = 1;
1932 /* FALLTHROUGH */
1934 case 'o':
1935 case 'x':
1936 case 'X':
1937 if (seen_space || seen_plus)
1938 bad = 1;
1939 /* FALLTHROUGH */
1941 case 'd':
1942 case 'i':
1943 if (lcount == 0)
1944 this_argclass = int_arg;
1945 else if (lcount == 1)
1946 this_argclass = long_arg;
1947 else
1948 this_argclass = long_long_arg;
1950 if (seen_big_l)
1951 bad = 1;
1952 break;
1954 case 'c':
1955 this_argclass = int_arg;
1956 if (lcount || seen_h || seen_big_l)
1957 bad = 1;
1958 if (seen_prec || seen_zero || seen_space || seen_plus)
1959 bad = 1;
1960 break;
1962 case 'p':
1963 this_argclass = ptr_arg;
1964 if (lcount || seen_h || seen_big_l)
1965 bad = 1;
1966 if (seen_prec || seen_zero || seen_space || seen_plus)
1967 bad = 1;
1968 break;
1970 case 's':
1971 this_argclass = string_arg;
1972 if (lcount || seen_h || seen_big_l)
1973 bad = 1;
1974 if (seen_zero || seen_space || seen_plus)
1975 bad = 1;
1976 break;
1978 case 'e':
1979 case 'f':
1980 case 'g':
1981 case 'E':
1982 case 'G':
1983 if (seen_big_h || seen_big_d || seen_double_big_d)
1984 this_argclass = decfloat_arg;
1985 else if (seen_big_l)
1986 this_argclass = long_double_arg;
1987 else
1988 this_argclass = double_arg;
1990 if (lcount || seen_h)
1991 bad = 1;
1992 break;
1994 case '*':
1995 error (_("`*' not supported for precision or width in printf"));
1997 case 'n':
1998 error (_("Format specifier `n' not supported in printf"));
2000 case '\0':
2001 error (_("Incomplete format specifier at end of format string"));
2003 default:
2004 error (_("Unrecognized format specifier '%c' in printf"), *f);
2007 if (bad)
2008 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2009 *f);
2011 f++;
2012 strncpy (current_substring, last_arg, f - last_arg);
2013 current_substring += f - last_arg;
2014 *current_substring++ = '\0';
2015 last_arg = f;
2016 argclass[nargs_wanted++] = this_argclass;
2019 /* Now, parse all arguments and evaluate them.
2020 Store the VALUEs in VAL_ARGS. */
2022 while (*s != '\0')
2024 char *s1;
2025 if (nargs == allocated_args)
2026 val_args = (struct value **) xrealloc ((char *) val_args,
2027 (allocated_args *= 2)
2028 * sizeof (struct value *));
2029 s1 = s;
2030 val_args[nargs] = parse_to_comma_and_eval (&s1);
2032 /* If format string wants a float, unchecked-convert the value to
2033 floating point of the same size */
2035 if (argclass[nargs] == double_arg)
2037 struct type *type = value_type (val_args[nargs]);
2038 if (TYPE_LENGTH (type) == sizeof (float))
2039 deprecated_set_value_type (val_args[nargs], builtin_type_float);
2040 if (TYPE_LENGTH (type) == sizeof (double))
2041 deprecated_set_value_type (val_args[nargs], builtin_type_double);
2043 nargs++;
2044 s = s1;
2045 if (*s == ',')
2046 s++;
2049 if (nargs != nargs_wanted)
2050 error (_("Wrong number of arguments for specified format-string"));
2052 /* Now actually print them. */
2053 current_substring = substrings;
2054 for (i = 0; i < nargs; i++)
2056 switch (argclass[i])
2058 case string_arg:
2060 gdb_byte *str;
2061 CORE_ADDR tem;
2062 int j;
2063 tem = value_as_address (val_args[i]);
2065 /* This is a %s argument. Find the length of the string. */
2066 for (j = 0;; j++)
2068 gdb_byte c;
2069 QUIT;
2070 read_memory (tem + j, &c, 1);
2071 if (c == 0)
2072 break;
2075 /* Copy the string contents into a string inside GDB. */
2076 str = (gdb_byte *) alloca (j + 1);
2077 if (j != 0)
2078 read_memory (tem, str, j);
2079 str[j] = 0;
2081 printf_filtered (current_substring, (char *) str);
2083 break;
2084 case double_arg:
2086 double val = value_as_double (val_args[i]);
2087 printf_filtered (current_substring, val);
2088 break;
2090 case long_double_arg:
2091 #ifdef HAVE_LONG_DOUBLE
2093 long double val = value_as_double (val_args[i]);
2094 printf_filtered (current_substring, val);
2095 break;
2097 #else
2098 error (_("long double not supported in printf"));
2099 #endif
2100 case long_long_arg:
2101 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2103 long long val = value_as_long (val_args[i]);
2104 printf_filtered (current_substring, val);
2105 break;
2107 #else
2108 error (_("long long not supported in printf"));
2109 #endif
2110 case int_arg:
2112 int val = value_as_long (val_args[i]);
2113 printf_filtered (current_substring, val);
2114 break;
2116 case long_arg:
2118 long val = value_as_long (val_args[i]);
2119 printf_filtered (current_substring, val);
2120 break;
2123 /* Handles decimal floating values. */
2124 case decfloat_arg:
2126 const gdb_byte *param_ptr = value_contents (val_args[i]);
2127 #if defined (PRINTF_HAS_DECFLOAT)
2128 /* If we have native support for Decimal floating
2129 printing, handle it here. */
2130 printf_filtered (current_substring, param_ptr);
2131 #else
2133 /* As a workaround until vasprintf has native support for DFP
2134 we convert the DFP values to string and print them using
2135 the %s format specifier. */
2137 char *eos, *sos;
2138 int nnull_chars = 0;
2140 /* Parameter data. */
2141 struct type *param_type = value_type (val_args[i]);
2142 unsigned int param_len = TYPE_LENGTH (param_type);
2144 /* DFP output data. */
2145 struct value *dfp_value = NULL;
2146 gdb_byte *dfp_ptr;
2147 int dfp_len = 16;
2148 gdb_byte dec[16];
2149 struct type *dfp_type = NULL;
2150 char decstr[MAX_DECIMAL_STRING];
2152 /* Points to the end of the string so that we can go back
2153 and check for DFP length modifiers. */
2154 eos = current_substring + strlen (current_substring);
2156 /* Look for the float/double format specifier. */
2157 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2158 && *eos != 'g' && *eos != 'G')
2159 eos--;
2161 sos = eos;
2163 /* Search for the '%' char and extract the size and type of
2164 the output decimal value based on its modifiers
2165 (%Hf, %Df, %DDf). */
2166 while (*--sos != '%')
2168 if (*sos == 'H')
2170 dfp_len = 4;
2171 dfp_type = builtin_type (current_gdbarch)->builtin_decfloat;
2173 else if (*sos == 'D' && *(sos - 1) == 'D')
2175 dfp_len = 16;
2176 dfp_type = builtin_type (current_gdbarch)->builtin_declong;
2177 sos--;
2179 else
2181 dfp_len = 8;
2182 dfp_type = builtin_type (current_gdbarch)->builtin_decdouble;
2186 /* Replace %Hf, %Df and %DDf with %s's. */
2187 *++sos = 's';
2189 /* Go through the whole format string and pull the correct
2190 number of chars back to compensate for the change in the
2191 format specifier. */
2192 while (nnull_chars < nargs - i)
2194 if (*eos == '\0')
2195 nnull_chars++;
2197 *++sos = *++eos;
2200 /* Conversion between different DFP types. */
2201 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2202 decimal_convert (param_ptr, param_len, dec, dfp_len);
2203 else
2204 /* If this is a non-trivial conversion, just output 0.
2205 A correct converted value can be displayed by explicitly
2206 casting to a DFP type. */
2207 decimal_from_string (dec, dfp_len, "0");
2209 dfp_value = value_from_decfloat (dfp_type, dec);
2211 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2213 decimal_to_string (dfp_ptr, dfp_len, decstr);
2215 /* Print the DFP value. */
2216 printf_filtered (current_substring, decstr);
2218 break;
2219 #endif
2222 case ptr_arg:
2224 /* We avoid the host's %p because pointers are too
2225 likely to be the wrong size. The only interesting
2226 modifier for %p is a width; extract that, and then
2227 handle %p as glibc would: %#x or a literal "(nil)". */
2229 char *p, *fmt, *fmt_p;
2230 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2231 long long val = value_as_long (val_args[i]);
2232 #else
2233 long val = value_as_long (val_args[i]);
2234 #endif
2236 fmt = alloca (strlen (current_substring) + 5);
2238 /* Copy up to the leading %. */
2239 p = current_substring;
2240 fmt_p = fmt;
2241 while (*p)
2243 int is_percent = (*p == '%');
2244 *fmt_p++ = *p++;
2245 if (is_percent)
2247 if (*p == '%')
2248 *fmt_p++ = *p++;
2249 else
2250 break;
2254 if (val != 0)
2255 *fmt_p++ = '#';
2257 /* Copy any width. */
2258 while (*p >= '0' && *p < '9')
2259 *fmt_p++ = *p++;
2261 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2262 if (val != 0)
2264 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2265 *fmt_p++ = 'l';
2266 #endif
2267 *fmt_p++ = 'l';
2268 *fmt_p++ = 'x';
2269 *fmt_p++ = '\0';
2270 printf_filtered (fmt, val);
2272 else
2274 *fmt_p++ = 's';
2275 *fmt_p++ = '\0';
2276 printf_filtered (fmt, "(nil)");
2279 break;
2281 default:
2282 internal_error (__FILE__, __LINE__,
2283 _("failed internal consistency check"));
2285 /* Skip to the next substring. */
2286 current_substring += strlen (current_substring) + 1;
2288 /* Print the portion of the format string after the last argument. */
2289 puts_filtered (last_arg);
2291 do_cleanups (old_cleanups);
2294 void
2295 _initialize_printcmd (void)
2297 struct cmd_list_element *c;
2299 current_display_number = -1;
2301 add_info ("address", address_info,
2302 _("Describe where symbol SYM is stored."));
2304 add_info ("symbol", sym_info, _("\
2305 Describe what symbol is at location ADDR.\n\
2306 Only for symbols with fixed locations (global or static scope)."));
2308 add_com ("x", class_vars, x_command, _("\
2309 Examine memory: x/FMT ADDRESS.\n\
2310 ADDRESS is an expression for the memory address to examine.\n\
2311 FMT is a repeat count followed by a format letter and a size letter.\n\
2312 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2313 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2314 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2315 The specified number of objects of the specified size are printed\n\
2316 according to the format.\n\n\
2317 Defaults for format and size letters are those previously used.\n\
2318 Default count is 1. Default address is following last thing printed\n\
2319 with this command or \"print\"."));
2321 #if 0
2322 add_com ("whereis", class_vars, whereis_command,
2323 _("Print line number and file of definition of variable."));
2324 #endif
2326 add_info ("display", display_info, _("\
2327 Expressions to display when program stops, with code numbers."));
2329 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2330 Cancel some expressions to be displayed when program stops.\n\
2331 Arguments are the code numbers of the expressions to stop displaying.\n\
2332 No argument means cancel all automatic-display expressions.\n\
2333 \"delete display\" has the same effect as this command.\n\
2334 Do \"info display\" to see current list of code numbers."),
2335 &cmdlist);
2337 add_com ("display", class_vars, display_command, _("\
2338 Print value of expression EXP each time the program stops.\n\
2339 /FMT may be used before EXP as in the \"print\" command.\n\
2340 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2341 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2342 and examining is done as in the \"x\" command.\n\n\
2343 With no argument, display all currently requested auto-display expressions.\n\
2344 Use \"undisplay\" to cancel display requests previously made."));
2346 add_cmd ("display", class_vars, enable_display, _("\
2347 Enable some expressions to be displayed when program stops.\n\
2348 Arguments are the code numbers of the expressions to resume displaying.\n\
2349 No argument means enable all automatic-display expressions.\n\
2350 Do \"info display\" to see current list of code numbers."), &enablelist);
2352 add_cmd ("display", class_vars, disable_display_command, _("\
2353 Disable some expressions to be displayed when program stops.\n\
2354 Arguments are the code numbers of the expressions to stop displaying.\n\
2355 No argument means disable all automatic-display expressions.\n\
2356 Do \"info display\" to see current list of code numbers."), &disablelist);
2358 add_cmd ("display", class_vars, undisplay_command, _("\
2359 Cancel some expressions to be displayed when program stops.\n\
2360 Arguments are the code numbers of the expressions to stop displaying.\n\
2361 No argument means cancel all automatic-display expressions.\n\
2362 Do \"info display\" to see current list of code numbers."), &deletelist);
2364 add_com ("printf", class_vars, printf_command, _("\
2365 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2366 This is useful for formatted output in user-defined commands."));
2368 add_com ("output", class_vars, output_command, _("\
2369 Like \"print\" but don't put in value history and don't print newline.\n\
2370 This is useful in user-defined commands."));
2372 add_prefix_cmd ("set", class_vars, set_command, _("\
2373 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2374 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2375 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2376 with $), a register (a few standard names starting with $), or an actual\n\
2377 variable in the program being debugged. EXP is any valid expression.\n\
2378 Use \"set variable\" for variables with names identical to set subcommands.\n\
2380 With a subcommand, this command modifies parts of the gdb environment.\n\
2381 You can see these environment settings with the \"show\" command."),
2382 &setlist, "set ", 1, &cmdlist);
2383 if (dbx_commands)
2384 add_com ("assign", class_vars, set_command, _("\
2385 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2386 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2387 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2388 with $), a register (a few standard names starting with $), or an actual\n\
2389 variable in the program being debugged. EXP is any valid expression.\n\
2390 Use \"set variable\" for variables with names identical to set subcommands.\n\
2391 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2392 You can see these environment settings with the \"show\" command."));
2394 /* "call" is the same as "set", but handy for dbx users to call fns. */
2395 c = add_com ("call", class_vars, call_command, _("\
2396 Call a function in the program.\n\
2397 The argument is the function name and arguments, in the notation of the\n\
2398 current working language. The result is printed and saved in the value\n\
2399 history, if it is not void."));
2400 set_cmd_completer (c, location_completer);
2402 add_cmd ("variable", class_vars, set_command, _("\
2403 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2404 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2405 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2406 with $), a register (a few standard names starting with $), or an actual\n\
2407 variable in the program being debugged. EXP is any valid expression.\n\
2408 This may usually be abbreviated to simply \"set\"."),
2409 &setlist);
2411 c = add_com ("print", class_vars, print_command, _("\
2412 Print value of expression EXP.\n\
2413 Variables accessible are those of the lexical environment of the selected\n\
2414 stack frame, plus all those whose scope is global or an entire file.\n\
2416 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2417 $$NUM refers to NUM'th value back from the last one.\n\
2418 Names starting with $ refer to registers (with the values they would have\n\
2419 if the program were to return to the stack frame now selected, restoring\n\
2420 all registers saved by frames farther in) or else to debugger\n\
2421 \"convenience\" variables (any such name not a known register).\n\
2422 Use assignment expressions to give values to convenience variables.\n\
2424 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2425 @ is a binary operator for treating consecutive data objects\n\
2426 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2427 element is FOO, whose second element is stored in the space following\n\
2428 where FOO is stored, etc. FOO must be an expression whose value\n\
2429 resides in memory.\n\
2431 EXP may be preceded with /FMT, where FMT is a format letter\n\
2432 but no count or size letter (see \"x\" command)."));
2433 set_cmd_completer (c, location_completer);
2434 add_com_alias ("p", "print", class_vars, 1);
2436 c = add_com ("inspect", class_vars, inspect_command, _("\
2437 Same as \"print\" command, except that if you are running in the epoch\n\
2438 environment, the value is printed in its own window."));
2439 set_cmd_completer (c, location_completer);
2441 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2442 &max_symbolic_offset, _("\
2443 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2444 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2445 NULL,
2446 show_max_symbolic_offset,
2447 &setprintlist, &showprintlist);
2448 add_setshow_boolean_cmd ("symbol-filename", no_class,
2449 &print_symbol_filename, _("\
2450 Set printing of source filename and line number with <symbol>."), _("\
2451 Show printing of source filename and line number with <symbol>."), NULL,
2452 NULL,
2453 show_print_symbol_filename,
2454 &setprintlist, &showprintlist);
2456 /* For examine/instruction a single byte quantity is specified as
2457 the data. This avoids problems with value_at_lazy() requiring a
2458 valid data type (and rejecting VOID). */
2459 examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2461 examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2462 examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2463 examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2464 examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);