printcmd.c revision 1.1.1.1 1 /* Print values for GNU debugger GDB.
2
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include <string.h>
22 #include "frame.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "language.h"
27 #include "expression.h"
28 #include "gdbcore.h"
29 #include "gdbcmd.h"
30 #include "target.h"
31 #include "breakpoint.h"
32 #include "demangle.h"
33 #include "gdb-demangle.h"
34 #include "valprint.h"
35 #include "annotate.h"
36 #include "symfile.h" /* for overlay functions */
37 #include "objfiles.h" /* ditto */
38 #include "completer.h" /* for completion functions */
39 #include "ui-out.h"
40 #include "gdb_assert.h"
41 #include "block.h"
42 #include "disasm.h"
43 #include "dfp.h"
44 #include "exceptions.h"
45 #include "observer.h"
46 #include "solist.h"
47 #include "parser-defs.h"
48 #include "charset.h"
49 #include "arch-utils.h"
50 #include "cli/cli-utils.h"
51 #include "format.h"
52 #include "source.h"
53
54 #ifdef TUI
55 #include "tui/tui.h" /* For tui_active et al. */
56 #endif
57
58 struct format_data
59 {
60 int count;
61 char format;
62 char size;
63
64 /* True if the value should be printed raw -- that is, bypassing
65 python-based formatters. */
66 unsigned char raw;
67 };
68
69 /* Last specified output format. */
70
71 static char last_format = 0;
72
73 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
74
75 static char last_size = 'w';
76
77 /* Default address to examine next, and associated architecture. */
78
79 static struct gdbarch *next_gdbarch;
80 static CORE_ADDR next_address;
81
82 /* Number of delay instructions following current disassembled insn. */
83
84 static int branch_delay_insns;
85
86 /* Last address examined. */
87
88 static CORE_ADDR last_examine_address;
89
90 /* Contents of last address examined.
91 This is not valid past the end of the `x' command! */
92
93 static struct value *last_examine_value;
94
95 /* Largest offset between a symbolic value and an address, that will be
96 printed as `0x1234 <symbol+offset>'. */
97
98 static unsigned int max_symbolic_offset = UINT_MAX;
99 static void
100 show_max_symbolic_offset (struct ui_file *file, int from_tty,
101 struct cmd_list_element *c, const char *value)
102 {
103 fprintf_filtered (file,
104 _("The largest offset that will be "
105 "printed in <symbol+1234> form is %s.\n"),
106 value);
107 }
108
109 /* Append the source filename and linenumber of the symbol when
110 printing a symbolic value as `<symbol at filename:linenum>' if set. */
111 static int print_symbol_filename = 0;
112 static void
113 show_print_symbol_filename (struct ui_file *file, int from_tty,
114 struct cmd_list_element *c, const char *value)
115 {
116 fprintf_filtered (file, _("Printing of source filename and "
117 "line number with <symbol> is %s.\n"),
118 value);
119 }
120
121 /* Number of auto-display expression currently being displayed.
122 So that we can disable it if we get a signal within it.
123 -1 when not doing one. */
124
125 static int current_display_number;
126
127 struct display
128 {
129 /* Chain link to next auto-display item. */
130 struct display *next;
131
132 /* The expression as the user typed it. */
133 char *exp_string;
134
135 /* Expression to be evaluated and displayed. */
136 struct expression *exp;
137
138 /* Item number of this auto-display item. */
139 int number;
140
141 /* Display format specified. */
142 struct format_data format;
143
144 /* Program space associated with `block'. */
145 struct program_space *pspace;
146
147 /* Innermost block required by this expression when evaluated. */
148 const struct block *block;
149
150 /* Status of this display (enabled or disabled). */
151 int enabled_p;
152 };
153
154 /* Chain of expressions whose values should be displayed
155 automatically each time the program stops. */
156
157 static struct display *display_chain;
158
159 static int display_number;
160
161 /* Walk the following statement or block through all displays.
162 ALL_DISPLAYS_SAFE does so even if the statement deletes the current
163 display. */
164
165 #define ALL_DISPLAYS(B) \
166 for (B = display_chain; B; B = B->next)
167
168 #define ALL_DISPLAYS_SAFE(B,TMP) \
169 for (B = display_chain; \
170 B ? (TMP = B->next, 1): 0; \
171 B = TMP)
172
173 /* Prototypes for exported functions. */
174
175 void _initialize_printcmd (void);
176
177 /* Prototypes for local functions. */
178
179 static void do_one_display (struct display *);
180
181
183 /* Decode a format specification. *STRING_PTR should point to it.
184 OFORMAT and OSIZE are used as defaults for the format and size
185 if none are given in the format specification.
186 If OSIZE is zero, then the size field of the returned value
187 should be set only if a size is explicitly specified by the
188 user.
189 The structure returned describes all the data
190 found in the specification. In addition, *STRING_PTR is advanced
191 past the specification and past all whitespace following it. */
192
193 static struct format_data
194 decode_format (const char **string_ptr, int oformat, int osize)
195 {
196 struct format_data val;
197 const char *p = *string_ptr;
198
199 val.format = '?';
200 val.size = '?';
201 val.count = 1;
202 val.raw = 0;
203
204 if (*p >= '0' && *p <= '9')
205 val.count = atoi (p);
206 while (*p >= '0' && *p <= '9')
207 p++;
208
209 /* Now process size or format letters that follow. */
210
211 while (1)
212 {
213 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
214 val.size = *p++;
215 else if (*p == 'r')
216 {
217 val.raw = 1;
218 p++;
219 }
220 else if (*p >= 'a' && *p <= 'z')
221 val.format = *p++;
222 else
223 break;
224 }
225
226 while (*p == ' ' || *p == '\t')
227 p++;
228 *string_ptr = p;
229
230 /* Set defaults for format and size if not specified. */
231 if (val.format == '?')
232 {
233 if (val.size == '?')
234 {
235 /* Neither has been specified. */
236 val.format = oformat;
237 val.size = osize;
238 }
239 else
240 /* If a size is specified, any format makes a reasonable
241 default except 'i'. */
242 val.format = oformat == 'i' ? 'x' : oformat;
243 }
244 else if (val.size == '?')
245 switch (val.format)
246 {
247 case 'a':
248 /* Pick the appropriate size for an address. This is deferred
249 until do_examine when we know the actual architecture to use.
250 A special size value of 'a' is used to indicate this case. */
251 val.size = osize ? 'a' : osize;
252 break;
253 case 'f':
254 /* Floating point has to be word or giantword. */
255 if (osize == 'w' || osize == 'g')
256 val.size = osize;
257 else
258 /* Default it to giantword if the last used size is not
259 appropriate. */
260 val.size = osize ? 'g' : osize;
261 break;
262 case 'c':
263 /* Characters default to one byte. */
264 val.size = osize ? 'b' : osize;
265 break;
266 case 's':
267 /* Display strings with byte size chars unless explicitly
268 specified. */
269 val.size = '\0';
270 break;
271
272 default:
273 /* The default is the size most recently specified. */
274 val.size = osize;
275 }
276
277 return val;
278 }
279
280 /* Print value VAL on stream according to OPTIONS.
282 Do not end with a newline.
283 SIZE is the letter for the size of datum being printed.
284 This is used to pad hex numbers so they line up. SIZE is 0
285 for print / output and set for examine. */
286
287 static void
288 print_formatted (struct value *val, int size,
289 const struct value_print_options *options,
290 struct ui_file *stream)
291 {
292 struct type *type = check_typedef (value_type (val));
293 int len = TYPE_LENGTH (type);
294
295 if (VALUE_LVAL (val) == lval_memory)
296 next_address = value_address (val) + len;
297
298 if (size)
299 {
300 switch (options->format)
301 {
302 case 's':
303 {
304 struct type *elttype = value_type (val);
305
306 next_address = (value_address (val)
307 + val_print_string (elttype, NULL,
308 value_address (val), -1,
309 stream, options) * len);
310 }
311 return;
312
313 case 'i':
314 /* We often wrap here if there are long symbolic names. */
315 wrap_here (" ");
316 next_address = (value_address (val)
317 + gdb_print_insn (get_type_arch (type),
318 value_address (val), stream,
319 &branch_delay_insns));
320 return;
321 }
322 }
323
324 if (options->format == 0 || options->format == 's'
325 || TYPE_CODE (type) == TYPE_CODE_REF
326 || TYPE_CODE (type) == TYPE_CODE_ARRAY
327 || TYPE_CODE (type) == TYPE_CODE_STRING
328 || TYPE_CODE (type) == TYPE_CODE_STRUCT
329 || TYPE_CODE (type) == TYPE_CODE_UNION
330 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
331 value_print (val, stream, options);
332 else
333 /* User specified format, so don't look to the type to tell us
334 what to do. */
335 val_print_scalar_formatted (type,
336 value_contents_for_printing (val),
337 value_embedded_offset (val),
338 val,
339 options, size, stream);
340 }
341
342 /* Return builtin floating point type of same length as TYPE.
343 If no such type is found, return TYPE itself. */
344 static struct type *
345 float_type_from_length (struct type *type)
346 {
347 struct gdbarch *gdbarch = get_type_arch (type);
348 const struct builtin_type *builtin = builtin_type (gdbarch);
349
350 if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
351 type = builtin->builtin_float;
352 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
353 type = builtin->builtin_double;
354 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
355 type = builtin->builtin_long_double;
356
357 return type;
358 }
359
360 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
361 according to OPTIONS and SIZE on STREAM. Formats s and i are not
362 supported at this level. */
363
364 void
365 print_scalar_formatted (const void *valaddr, struct type *type,
366 const struct value_print_options *options,
367 int size, struct ui_file *stream)
368 {
369 struct gdbarch *gdbarch = get_type_arch (type);
370 LONGEST val_long = 0;
371 unsigned int len = TYPE_LENGTH (type);
372 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
373
374 /* String printing should go through val_print_scalar_formatted. */
375 gdb_assert (options->format != 's');
376
377 if (len > sizeof(LONGEST) &&
378 (TYPE_CODE (type) == TYPE_CODE_INT
379 || TYPE_CODE (type) == TYPE_CODE_ENUM))
380 {
381 switch (options->format)
382 {
383 case 'o':
384 print_octal_chars (stream, valaddr, len, byte_order);
385 return;
386 case 'u':
387 case 'd':
388 print_decimal_chars (stream, valaddr, len, byte_order);
389 return;
390 case 't':
391 print_binary_chars (stream, valaddr, len, byte_order);
392 return;
393 case 'x':
394 print_hex_chars (stream, valaddr, len, byte_order);
395 return;
396 case 'c':
397 print_char_chars (stream, type, valaddr, len, byte_order);
398 return;
399 default:
400 break;
401 };
402 }
403
404 if (options->format != 'f')
405 val_long = unpack_long (type, valaddr);
406
407 /* If the value is a pointer, and pointers and addresses are not the
408 same, then at this point, the value's length (in target bytes) is
409 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
410 if (TYPE_CODE (type) == TYPE_CODE_PTR)
411 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
412
413 /* If we are printing it as unsigned, truncate it in case it is actually
414 a negative signed value (e.g. "print/u (short)-1" should print 65535
415 (if shorts are 16 bits) instead of 4294967295). */
416 if (options->format != 'd' || TYPE_UNSIGNED (type))
417 {
418 if (len < sizeof (LONGEST))
419 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
420 }
421
422 switch (options->format)
423 {
424 case 'x':
425 if (!size)
426 {
427 /* No size specified, like in print. Print varying # of digits. */
428 print_longest (stream, 'x', 1, val_long);
429 }
430 else
431 switch (size)
432 {
433 case 'b':
434 case 'h':
435 case 'w':
436 case 'g':
437 print_longest (stream, size, 1, val_long);
438 break;
439 default:
440 error (_("Undefined output size \"%c\"."), size);
441 }
442 break;
443
444 case 'd':
445 print_longest (stream, 'd', 1, val_long);
446 break;
447
448 case 'u':
449 print_longest (stream, 'u', 0, val_long);
450 break;
451
452 case 'o':
453 if (val_long)
454 print_longest (stream, 'o', 1, val_long);
455 else
456 fprintf_filtered (stream, "0");
457 break;
458
459 case 'a':
460 {
461 CORE_ADDR addr = unpack_pointer (type, valaddr);
462
463 print_address (gdbarch, addr, stream);
464 }
465 break;
466
467 case 'c':
468 {
469 struct value_print_options opts = *options;
470
471 opts.format = 0;
472 if (TYPE_UNSIGNED (type))
473 type = builtin_type (gdbarch)->builtin_true_unsigned_char;
474 else
475 type = builtin_type (gdbarch)->builtin_true_char;
476
477 value_print (value_from_longest (type, val_long), stream, &opts);
478 }
479 break;
480
481 case 'f':
482 type = float_type_from_length (type);
483 print_floating (valaddr, type, stream);
484 break;
485
486 case 0:
487 internal_error (__FILE__, __LINE__,
488 _("failed internal consistency check"));
489
490 case 't':
491 /* Binary; 't' stands for "two". */
492 {
493 char bits[8 * (sizeof val_long) + 1];
494 char buf[8 * (sizeof val_long) + 32];
495 char *cp = bits;
496 int width;
497
498 if (!size)
499 width = 8 * (sizeof val_long);
500 else
501 switch (size)
502 {
503 case 'b':
504 width = 8;
505 break;
506 case 'h':
507 width = 16;
508 break;
509 case 'w':
510 width = 32;
511 break;
512 case 'g':
513 width = 64;
514 break;
515 default:
516 error (_("Undefined output size \"%c\"."), size);
517 }
518
519 bits[width] = '\0';
520 while (width-- > 0)
521 {
522 bits[width] = (val_long & 1) ? '1' : '0';
523 val_long >>= 1;
524 }
525 if (!size)
526 {
527 while (*cp && *cp == '0')
528 cp++;
529 if (*cp == '\0')
530 cp--;
531 }
532 strncpy (buf, cp, sizeof (bits));
533 fputs_filtered (buf, stream);
534 }
535 break;
536
537 case 'z':
538 print_hex_chars (stream, valaddr, len, byte_order);
539 break;
540
541 default:
542 error (_("Undefined output format \"%c\"."), options->format);
543 }
544 }
545
546 /* Specify default address for `x' command.
547 The `info lines' command uses this. */
548
549 void
550 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
551 {
552 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
553
554 next_gdbarch = gdbarch;
555 next_address = addr;
556
557 /* Make address available to the user as $_. */
558 set_internalvar (lookup_internalvar ("_"),
559 value_from_pointer (ptr_type, addr));
560 }
561
562 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
563 after LEADIN. Print nothing if no symbolic name is found nearby.
564 Optionally also print source file and line number, if available.
565 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
566 or to interpret it as a possible C++ name and convert it back to source
567 form. However note that DO_DEMANGLE can be overridden by the specific
568 settings of the demangle and asm_demangle variables. Returns
569 non-zero if anything was printed; zero otherwise. */
570
571 int
572 print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
573 struct ui_file *stream,
574 int do_demangle, char *leadin)
575 {
576 char *name = NULL;
577 char *filename = NULL;
578 int unmapped = 0;
579 int offset = 0;
580 int line = 0;
581
582 /* Throw away both name and filename. */
583 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
584 make_cleanup (free_current_contents, &filename);
585
586 if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
587 &filename, &line, &unmapped))
588 {
589 do_cleanups (cleanup_chain);
590 return 0;
591 }
592
593 fputs_filtered (leadin, stream);
594 if (unmapped)
595 fputs_filtered ("<*", stream);
596 else
597 fputs_filtered ("<", stream);
598 fputs_filtered (name, stream);
599 if (offset != 0)
600 fprintf_filtered (stream, "+%u", (unsigned int) offset);
601
602 /* Append source filename and line number if desired. Give specific
603 line # of this addr, if we have it; else line # of the nearest symbol. */
604 if (print_symbol_filename && filename != NULL)
605 {
606 if (line != -1)
607 fprintf_filtered (stream, " at %s:%d", filename, line);
608 else
609 fprintf_filtered (stream, " in %s", filename);
610 }
611 if (unmapped)
612 fputs_filtered ("*>", stream);
613 else
614 fputs_filtered (">", stream);
615
616 do_cleanups (cleanup_chain);
617 return 1;
618 }
619
620 /* Given an address ADDR return all the elements needed to print the
621 address in a symbolic form. NAME can be mangled or not depending
622 on DO_DEMANGLE (and also on the asm_demangle global variable,
623 manipulated via ''set print asm-demangle''). Return 0 in case of
624 success, when all the info in the OUT paramters is valid. Return 1
625 otherwise. */
626 int
627 build_address_symbolic (struct gdbarch *gdbarch,
628 CORE_ADDR addr, /* IN */
629 int do_demangle, /* IN */
630 char **name, /* OUT */
631 int *offset, /* OUT */
632 char **filename, /* OUT */
633 int *line, /* OUT */
634 int *unmapped) /* OUT */
635 {
636 struct minimal_symbol *msymbol;
637 struct symbol *symbol;
638 CORE_ADDR name_location = 0;
639 struct obj_section *section = NULL;
640 const char *name_temp = "";
641
642 /* Let's say it is mapped (not unmapped). */
643 *unmapped = 0;
644
645 /* Determine if the address is in an overlay, and whether it is
646 mapped. */
647 if (overlay_debugging)
648 {
649 section = find_pc_overlay (addr);
650 if (pc_in_unmapped_range (addr, section))
651 {
652 *unmapped = 1;
653 addr = overlay_mapped_address (addr, section);
654 }
655 }
656
657 /* First try to find the address in the symbol table, then
658 in the minsyms. Take the closest one. */
659
660 /* This is defective in the sense that it only finds text symbols. So
661 really this is kind of pointless--we should make sure that the
662 minimal symbols have everything we need (by changing that we could
663 save some memory, but for many debug format--ELF/DWARF or
664 anything/stabs--it would be inconvenient to eliminate those minimal
665 symbols anyway). */
666 msymbol = lookup_minimal_symbol_by_pc_section (addr, section).minsym;
667 symbol = find_pc_sect_function (addr, section);
668
669 if (symbol)
670 {
671 /* If this is a function (i.e. a code address), strip out any
672 non-address bits. For instance, display a pointer to the
673 first instruction of a Thumb function as <function>; the
674 second instruction will be <function+2>, even though the
675 pointer is <function+3>. This matches the ISA behavior. */
676 addr = gdbarch_addr_bits_remove (gdbarch, addr);
677
678 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
679 if (do_demangle || asm_demangle)
680 name_temp = SYMBOL_PRINT_NAME (symbol);
681 else
682 name_temp = SYMBOL_LINKAGE_NAME (symbol);
683 }
684
685 if (msymbol != NULL
686 && MSYMBOL_HAS_SIZE (msymbol)
687 && MSYMBOL_SIZE (msymbol) == 0
688 && MSYMBOL_TYPE (msymbol) != mst_text
689 && MSYMBOL_TYPE (msymbol) != mst_text_gnu_ifunc
690 && MSYMBOL_TYPE (msymbol) != mst_file_text)
691 msymbol = NULL;
692
693 if (msymbol != NULL)
694 {
695 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
696 {
697 /* If this is a function (i.e. a code address), strip out any
698 non-address bits. For instance, display a pointer to the
699 first instruction of a Thumb function as <function>; the
700 second instruction will be <function+2>, even though the
701 pointer is <function+3>. This matches the ISA behavior. */
702 if (MSYMBOL_TYPE (msymbol) == mst_text
703 || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
704 || MSYMBOL_TYPE (msymbol) == mst_file_text
705 || MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
706 addr = gdbarch_addr_bits_remove (gdbarch, addr);
707
708 /* The msymbol is closer to the address than the symbol;
709 use the msymbol instead. */
710 symbol = 0;
711 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
712 if (do_demangle || asm_demangle)
713 name_temp = SYMBOL_PRINT_NAME (msymbol);
714 else
715 name_temp = SYMBOL_LINKAGE_NAME (msymbol);
716 }
717 }
718 if (symbol == NULL && msymbol == NULL)
719 return 1;
720
721 /* If the nearest symbol is too far away, don't print anything symbolic. */
722
723 /* For when CORE_ADDR is larger than unsigned int, we do math in
724 CORE_ADDR. But when we detect unsigned wraparound in the
725 CORE_ADDR math, we ignore this test and print the offset,
726 because addr+max_symbolic_offset has wrapped through the end
727 of the address space back to the beginning, giving bogus comparison. */
728 if (addr > name_location + max_symbolic_offset
729 && name_location + max_symbolic_offset > name_location)
730 return 1;
731
732 *offset = addr - name_location;
733
734 *name = xstrdup (name_temp);
735
736 if (print_symbol_filename)
737 {
738 struct symtab_and_line sal;
739
740 sal = find_pc_sect_line (addr, section, 0);
741
742 if (sal.symtab)
743 {
744 *filename = xstrdup (symtab_to_filename_for_display (sal.symtab));
745 *line = sal.line;
746 }
747 }
748 return 0;
749 }
750
751
752 /* Print address ADDR symbolically on STREAM.
753 First print it as a number. Then perhaps print
754 <SYMBOL + OFFSET> after the number. */
755
756 void
757 print_address (struct gdbarch *gdbarch,
758 CORE_ADDR addr, struct ui_file *stream)
759 {
760 fputs_filtered (paddress (gdbarch, addr), stream);
761 print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
762 }
763
764 /* Return a prefix for instruction address:
765 "=> " for current instruction, else " ". */
766
767 const char *
768 pc_prefix (CORE_ADDR addr)
769 {
770 if (has_stack_frames ())
771 {
772 struct frame_info *frame;
773 CORE_ADDR pc;
774
775 frame = get_selected_frame (NULL);
776 if (get_frame_pc_if_available (frame, &pc) && pc == addr)
777 return "=> ";
778 }
779 return " ";
780 }
781
782 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
783 controls whether to print the symbolic name "raw" or demangled.
784 Return non-zero if anything was printed; zero otherwise. */
785
786 int
787 print_address_demangle (const struct value_print_options *opts,
788 struct gdbarch *gdbarch, CORE_ADDR addr,
789 struct ui_file *stream, int do_demangle)
790 {
791 if (opts->addressprint)
792 {
793 fputs_filtered (paddress (gdbarch, addr), stream);
794 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
795 }
796 else
797 {
798 return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
799 }
800 return 1;
801 }
802
803
805 /* Examine data at address ADDR in format FMT.
806 Fetch it from memory and print on gdb_stdout. */
807
808 static void
809 do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
810 {
811 char format = 0;
812 char size;
813 int count = 1;
814 struct type *val_type = NULL;
815 int i;
816 int maxelts;
817 struct value_print_options opts;
818
819 format = fmt.format;
820 size = fmt.size;
821 count = fmt.count;
822 next_gdbarch = gdbarch;
823 next_address = addr;
824
825 /* Instruction format implies fetch single bytes
826 regardless of the specified size.
827 The case of strings is handled in decode_format, only explicit
828 size operator are not changed to 'b'. */
829 if (format == 'i')
830 size = 'b';
831
832 if (size == 'a')
833 {
834 /* Pick the appropriate size for an address. */
835 if (gdbarch_ptr_bit (next_gdbarch) == 64)
836 size = 'g';
837 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
838 size = 'w';
839 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
840 size = 'h';
841 else
842 /* Bad value for gdbarch_ptr_bit. */
843 internal_error (__FILE__, __LINE__,
844 _("failed internal consistency check"));
845 }
846
847 if (size == 'b')
848 val_type = builtin_type (next_gdbarch)->builtin_int8;
849 else if (size == 'h')
850 val_type = builtin_type (next_gdbarch)->builtin_int16;
851 else if (size == 'w')
852 val_type = builtin_type (next_gdbarch)->builtin_int32;
853 else if (size == 'g')
854 val_type = builtin_type (next_gdbarch)->builtin_int64;
855
856 if (format == 's')
857 {
858 struct type *char_type = NULL;
859
860 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
861 if type is not found. */
862 if (size == 'h')
863 char_type = builtin_type (next_gdbarch)->builtin_char16;
864 else if (size == 'w')
865 char_type = builtin_type (next_gdbarch)->builtin_char32;
866 if (char_type)
867 val_type = char_type;
868 else
869 {
870 if (size != '\0' && size != 'b')
871 warning (_("Unable to display strings with "
872 "size '%c', using 'b' instead."), size);
873 size = 'b';
874 val_type = builtin_type (next_gdbarch)->builtin_int8;
875 }
876 }
877
878 maxelts = 8;
879 if (size == 'w')
880 maxelts = 4;
881 if (size == 'g')
882 maxelts = 2;
883 if (format == 's' || format == 'i')
884 maxelts = 1;
885
886 get_formatted_print_options (&opts, format);
887
888 /* Print as many objects as specified in COUNT, at most maxelts per line,
889 with the address of the next one at the start of each line. */
890
891 while (count > 0)
892 {
893 QUIT;
894 if (format == 'i')
895 fputs_filtered (pc_prefix (next_address), gdb_stdout);
896 print_address (next_gdbarch, next_address, gdb_stdout);
897 printf_filtered (":");
898 for (i = maxelts;
899 i > 0 && count > 0;
900 i--, count--)
901 {
902 printf_filtered ("\t");
903 /* Note that print_formatted sets next_address for the next
904 object. */
905 last_examine_address = next_address;
906
907 if (last_examine_value)
908 value_free (last_examine_value);
909
910 /* The value to be displayed is not fetched greedily.
911 Instead, to avoid the possibility of a fetched value not
912 being used, its retrieval is delayed until the print code
913 uses it. When examining an instruction stream, the
914 disassembler will perform its own memory fetch using just
915 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
916 the disassembler be modified so that LAST_EXAMINE_VALUE
917 is left with the byte sequence from the last complete
918 instruction fetched from memory? */
919 last_examine_value = value_at_lazy (val_type, next_address);
920
921 if (last_examine_value)
922 release_value (last_examine_value);
923
924 print_formatted (last_examine_value, size, &opts, gdb_stdout);
925
926 /* Display any branch delay slots following the final insn. */
927 if (format == 'i' && count == 1)
928 count += branch_delay_insns;
929 }
930 printf_filtered ("\n");
931 gdb_flush (gdb_stdout);
932 }
933 }
934
935 static void
937 validate_format (struct format_data fmt, char *cmdname)
938 {
939 if (fmt.size != 0)
940 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
941 if (fmt.count != 1)
942 error (_("Item count other than 1 is meaningless in \"%s\" command."),
943 cmdname);
944 if (fmt.format == 'i')
945 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
946 fmt.format, cmdname);
947 }
948
949 /* Evaluate string EXP as an expression in the current language and
950 print the resulting value. EXP may contain a format specifier as the
951 first argument ("/x myvar" for example, to print myvar in hex). */
952
953 static void
954 print_command_1 (const char *exp, int voidprint)
955 {
956 struct expression *expr;
957 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
958 char format = 0;
959 struct value *val;
960 struct format_data fmt;
961
962 if (exp && *exp == '/')
963 {
964 exp++;
965 fmt = decode_format (&exp, last_format, 0);
966 validate_format (fmt, "print");
967 last_format = format = fmt.format;
968 }
969 else
970 {
971 fmt.count = 1;
972 fmt.format = 0;
973 fmt.size = 0;
974 fmt.raw = 0;
975 }
976
977 if (exp && *exp)
978 {
979 expr = parse_expression (exp);
980 make_cleanup (free_current_contents, &expr);
981 val = evaluate_expression (expr);
982 }
983 else
984 val = access_value_history (0);
985
986 if (voidprint || (val && value_type (val) &&
987 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
988 {
989 struct value_print_options opts;
990 int histindex = record_latest_value (val);
991
992 if (histindex >= 0)
993 annotate_value_history_begin (histindex, value_type (val));
994 else
995 annotate_value_begin (value_type (val));
996
997 if (histindex >= 0)
998 printf_filtered ("$%d = ", histindex);
999
1000 if (histindex >= 0)
1001 annotate_value_history_value ();
1002
1003 get_formatted_print_options (&opts, format);
1004 opts.raw = fmt.raw;
1005
1006 print_formatted (val, fmt.size, &opts, gdb_stdout);
1007 printf_filtered ("\n");
1008
1009 if (histindex >= 0)
1010 annotate_value_history_end ();
1011 else
1012 annotate_value_end ();
1013 }
1014
1015 do_cleanups (old_chain);
1016 }
1017
1018 static void
1019 print_command (char *exp, int from_tty)
1020 {
1021 print_command_1 (exp, 1);
1022 }
1023
1024 /* Same as print, except it doesn't print void results. */
1025 static void
1026 call_command (char *exp, int from_tty)
1027 {
1028 print_command_1 (exp, 0);
1029 }
1030
1031 /* Implementation of the "output" command. */
1032
1033 static void
1034 output_command (char *exp, int from_tty)
1035 {
1036 output_command_const (exp, from_tty);
1037 }
1038
1039 /* Like output_command, but takes a const string as argument. */
1040
1041 void
1042 output_command_const (const char *exp, int from_tty)
1043 {
1044 struct expression *expr;
1045 struct cleanup *old_chain;
1046 char format = 0;
1047 struct value *val;
1048 struct format_data fmt;
1049 struct value_print_options opts;
1050
1051 fmt.size = 0;
1052 fmt.raw = 0;
1053
1054 if (exp && *exp == '/')
1055 {
1056 exp++;
1057 fmt = decode_format (&exp, 0, 0);
1058 validate_format (fmt, "output");
1059 format = fmt.format;
1060 }
1061
1062 expr = parse_expression (exp);
1063 old_chain = make_cleanup (free_current_contents, &expr);
1064
1065 val = evaluate_expression (expr);
1066
1067 annotate_value_begin (value_type (val));
1068
1069 get_formatted_print_options (&opts, format);
1070 opts.raw = fmt.raw;
1071 print_formatted (val, fmt.size, &opts, gdb_stdout);
1072
1073 annotate_value_end ();
1074
1075 wrap_here ("");
1076 gdb_flush (gdb_stdout);
1077
1078 do_cleanups (old_chain);
1079 }
1080
1081 static void
1082 set_command (char *exp, int from_tty)
1083 {
1084 struct expression *expr = parse_expression (exp);
1085 struct cleanup *old_chain =
1086 make_cleanup (free_current_contents, &expr);
1087
1088 if (expr->nelts >= 1)
1089 switch (expr->elts[0].opcode)
1090 {
1091 case UNOP_PREINCREMENT:
1092 case UNOP_POSTINCREMENT:
1093 case UNOP_PREDECREMENT:
1094 case UNOP_POSTDECREMENT:
1095 case BINOP_ASSIGN:
1096 case BINOP_ASSIGN_MODIFY:
1097 case BINOP_COMMA:
1098 break;
1099 default:
1100 warning
1101 (_("Expression is not an assignment (and might have no effect)"));
1102 }
1103
1104 evaluate_expression (expr);
1105 do_cleanups (old_chain);
1106 }
1107
1108 static void
1109 sym_info (char *arg, int from_tty)
1110 {
1111 struct minimal_symbol *msymbol;
1112 struct objfile *objfile;
1113 struct obj_section *osect;
1114 CORE_ADDR addr, sect_addr;
1115 int matches = 0;
1116 unsigned int offset;
1117
1118 if (!arg)
1119 error_no_arg (_("address"));
1120
1121 addr = parse_and_eval_address (arg);
1122 ALL_OBJSECTIONS (objfile, osect)
1123 {
1124 /* Only process each object file once, even if there's a separate
1125 debug file. */
1126 if (objfile->separate_debug_objfile_backlink)
1127 continue;
1128
1129 sect_addr = overlay_mapped_address (addr, osect);
1130
1131 if (obj_section_addr (osect) <= sect_addr
1132 && sect_addr < obj_section_endaddr (osect)
1133 && (msymbol
1134 = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
1135 {
1136 const char *obj_name, *mapped, *sec_name, *msym_name;
1137 char *loc_string;
1138 struct cleanup *old_chain;
1139
1140 matches = 1;
1141 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1142 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1143 sec_name = osect->the_bfd_section->name;
1144 msym_name = SYMBOL_PRINT_NAME (msymbol);
1145
1146 /* Don't print the offset if it is zero.
1147 We assume there's no need to handle i18n of "sym + offset". */
1148 if (offset)
1149 loc_string = xstrprintf ("%s + %u", msym_name, offset);
1150 else
1151 loc_string = xstrprintf ("%s", msym_name);
1152
1153 /* Use a cleanup to free loc_string in case the user quits
1154 a pagination request inside printf_filtered. */
1155 old_chain = make_cleanup (xfree, loc_string);
1156
1157 gdb_assert (osect->objfile && objfile_name (osect->objfile));
1158 obj_name = objfile_name (osect->objfile);
1159
1160 if (MULTI_OBJFILE_P ())
1161 if (pc_in_unmapped_range (addr, osect))
1162 if (section_is_overlay (osect))
1163 printf_filtered (_("%s in load address range of "
1164 "%s overlay section %s of %s\n"),
1165 loc_string, mapped, sec_name, obj_name);
1166 else
1167 printf_filtered (_("%s in load address range of "
1168 "section %s of %s\n"),
1169 loc_string, sec_name, obj_name);
1170 else
1171 if (section_is_overlay (osect))
1172 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1173 loc_string, mapped, sec_name, obj_name);
1174 else
1175 printf_filtered (_("%s in section %s of %s\n"),
1176 loc_string, sec_name, obj_name);
1177 else
1178 if (pc_in_unmapped_range (addr, osect))
1179 if (section_is_overlay (osect))
1180 printf_filtered (_("%s in load address range of %s overlay "
1181 "section %s\n"),
1182 loc_string, mapped, sec_name);
1183 else
1184 printf_filtered (_("%s in load address range of section %s\n"),
1185 loc_string, sec_name);
1186 else
1187 if (section_is_overlay (osect))
1188 printf_filtered (_("%s in %s overlay section %s\n"),
1189 loc_string, mapped, sec_name);
1190 else
1191 printf_filtered (_("%s in section %s\n"),
1192 loc_string, sec_name);
1193
1194 do_cleanups (old_chain);
1195 }
1196 }
1197 if (matches == 0)
1198 printf_filtered (_("No symbol matches %s.\n"), arg);
1199 }
1200
1201 static void
1202 address_info (char *exp, int from_tty)
1203 {
1204 struct gdbarch *gdbarch;
1205 int regno;
1206 struct symbol *sym;
1207 struct bound_minimal_symbol msymbol;
1208 long val;
1209 struct obj_section *section;
1210 CORE_ADDR load_addr, context_pc = 0;
1211 struct field_of_this_result is_a_field_of_this;
1212
1213 if (exp == 0)
1214 error (_("Argument required."));
1215
1216 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
1217 &is_a_field_of_this);
1218 if (sym == NULL)
1219 {
1220 if (is_a_field_of_this.type != NULL)
1221 {
1222 printf_filtered ("Symbol \"");
1223 fprintf_symbol_filtered (gdb_stdout, exp,
1224 current_language->la_language, DMGL_ANSI);
1225 printf_filtered ("\" is a field of the local class variable ");
1226 if (current_language->la_language == language_objc)
1227 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1228 else
1229 printf_filtered ("`this'\n");
1230 return;
1231 }
1232
1233 msymbol = lookup_bound_minimal_symbol (exp);
1234
1235 if (msymbol.minsym != NULL)
1236 {
1237 struct objfile *objfile = msymbol.objfile;
1238
1239 gdbarch = get_objfile_arch (objfile);
1240 load_addr = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
1241
1242 printf_filtered ("Symbol \"");
1243 fprintf_symbol_filtered (gdb_stdout, exp,
1244 current_language->la_language, DMGL_ANSI);
1245 printf_filtered ("\" is at ");
1246 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1247 printf_filtered (" in a file compiled without debugging");
1248 section = SYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
1249 if (section_is_overlay (section))
1250 {
1251 load_addr = overlay_unmapped_address (load_addr, section);
1252 printf_filtered (",\n -- loaded at ");
1253 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1254 printf_filtered (" in overlay section %s",
1255 section->the_bfd_section->name);
1256 }
1257 printf_filtered (".\n");
1258 }
1259 else
1260 error (_("No symbol \"%s\" in current context."), exp);
1261 return;
1262 }
1263
1264 printf_filtered ("Symbol \"");
1265 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
1266 current_language->la_language, DMGL_ANSI);
1267 printf_filtered ("\" is ");
1268 val = SYMBOL_VALUE (sym);
1269 section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (sym), sym);
1270 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
1271
1272 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
1273 {
1274 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1275 gdb_stdout);
1276 printf_filtered (".\n");
1277 return;
1278 }
1279
1280 switch (SYMBOL_CLASS (sym))
1281 {
1282 case LOC_CONST:
1283 case LOC_CONST_BYTES:
1284 printf_filtered ("constant");
1285 break;
1286
1287 case LOC_LABEL:
1288 printf_filtered ("a label at address ");
1289 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1290 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1291 if (section_is_overlay (section))
1292 {
1293 load_addr = overlay_unmapped_address (load_addr, section);
1294 printf_filtered (",\n -- loaded at ");
1295 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1296 printf_filtered (" in overlay section %s",
1297 section->the_bfd_section->name);
1298 }
1299 break;
1300
1301 case LOC_COMPUTED:
1302 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
1303
1304 case LOC_REGISTER:
1305 /* GDBARCH is the architecture associated with the objfile the symbol
1306 is defined in; the target architecture may be different, and may
1307 provide additional registers. However, we do not know the target
1308 architecture at this point. We assume the objfile architecture
1309 will contain all the standard registers that occur in debug info
1310 in that objfile. */
1311 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1312
1313 if (SYMBOL_IS_ARGUMENT (sym))
1314 printf_filtered (_("an argument in register %s"),
1315 gdbarch_register_name (gdbarch, regno));
1316 else
1317 printf_filtered (_("a variable in register %s"),
1318 gdbarch_register_name (gdbarch, regno));
1319 break;
1320
1321 case LOC_STATIC:
1322 printf_filtered (_("static storage at address "));
1323 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1324 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1325 if (section_is_overlay (section))
1326 {
1327 load_addr = overlay_unmapped_address (load_addr, section);
1328 printf_filtered (_(",\n -- loaded at "));
1329 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1330 printf_filtered (_(" in overlay section %s"),
1331 section->the_bfd_section->name);
1332 }
1333 break;
1334
1335 case LOC_REGPARM_ADDR:
1336 /* Note comment at LOC_REGISTER. */
1337 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1338 printf_filtered (_("address of an argument in register %s"),
1339 gdbarch_register_name (gdbarch, regno));
1340 break;
1341
1342 case LOC_ARG:
1343 printf_filtered (_("an argument at offset %ld"), val);
1344 break;
1345
1346 case LOC_LOCAL:
1347 printf_filtered (_("a local variable at frame offset %ld"), val);
1348 break;
1349
1350 case LOC_REF_ARG:
1351 printf_filtered (_("a reference argument at offset %ld"), val);
1352 break;
1353
1354 case LOC_TYPEDEF:
1355 printf_filtered (_("a typedef"));
1356 break;
1357
1358 case LOC_BLOCK:
1359 printf_filtered (_("a function at address "));
1360 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1361 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1362 if (section_is_overlay (section))
1363 {
1364 load_addr = overlay_unmapped_address (load_addr, section);
1365 printf_filtered (_(",\n -- loaded at "));
1366 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1367 printf_filtered (_(" in overlay section %s"),
1368 section->the_bfd_section->name);
1369 }
1370 break;
1371
1372 case LOC_UNRESOLVED:
1373 {
1374 struct bound_minimal_symbol msym;
1375
1376 msym = lookup_minimal_symbol_and_objfile (SYMBOL_LINKAGE_NAME (sym));
1377 if (msym.minsym == NULL)
1378 printf_filtered ("unresolved");
1379 else
1380 {
1381 section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
1382 load_addr = SYMBOL_VALUE_ADDRESS (msym.minsym);
1383
1384 if (section
1385 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1386 printf_filtered (_("a thread-local variable at offset %s "
1387 "in the thread-local storage for `%s'"),
1388 paddress (gdbarch, load_addr),
1389 objfile_name (section->objfile));
1390 else
1391 {
1392 printf_filtered (_("static storage at address "));
1393 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1394 if (section_is_overlay (section))
1395 {
1396 load_addr = overlay_unmapped_address (load_addr, section);
1397 printf_filtered (_(",\n -- loaded at "));
1398 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
1399 printf_filtered (_(" in overlay section %s"),
1400 section->the_bfd_section->name);
1401 }
1402 }
1403 }
1404 }
1405 break;
1406
1407 case LOC_OPTIMIZED_OUT:
1408 printf_filtered (_("optimized out"));
1409 break;
1410
1411 default:
1412 printf_filtered (_("of unknown (botched) type"));
1413 break;
1414 }
1415 printf_filtered (".\n");
1416 }
1417
1418
1420 static void
1421 x_command (char *exp, int from_tty)
1422 {
1423 struct expression *expr;
1424 struct format_data fmt;
1425 struct cleanup *old_chain;
1426 struct value *val;
1427
1428 fmt.format = last_format ? last_format : 'x';
1429 fmt.size = last_size;
1430 fmt.count = 1;
1431 fmt.raw = 0;
1432
1433 if (exp && *exp == '/')
1434 {
1435 const char *tmp = exp + 1;
1436
1437 fmt = decode_format (&tmp, last_format, last_size);
1438 exp = (char *) tmp;
1439 }
1440
1441 /* If we have an expression, evaluate it and use it as the address. */
1442
1443 if (exp != 0 && *exp != 0)
1444 {
1445 expr = parse_expression (exp);
1446 /* Cause expression not to be there any more if this command is
1447 repeated with Newline. But don't clobber a user-defined
1448 command's definition. */
1449 if (from_tty)
1450 *exp = 0;
1451 old_chain = make_cleanup (free_current_contents, &expr);
1452 val = evaluate_expression (expr);
1453 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1454 val = coerce_ref (val);
1455 /* In rvalue contexts, such as this, functions are coerced into
1456 pointers to functions. This makes "x/i main" work. */
1457 if (/* last_format == 'i' && */
1458 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1459 && VALUE_LVAL (val) == lval_memory)
1460 next_address = value_address (val);
1461 else
1462 next_address = value_as_address (val);
1463
1464 next_gdbarch = expr->gdbarch;
1465 do_cleanups (old_chain);
1466 }
1467
1468 if (!next_gdbarch)
1469 error_no_arg (_("starting display address"));
1470
1471 do_examine (fmt, next_gdbarch, next_address);
1472
1473 /* If the examine succeeds, we remember its size and format for next
1474 time. Set last_size to 'b' for strings. */
1475 if (fmt.format == 's')
1476 last_size = 'b';
1477 else
1478 last_size = fmt.size;
1479 last_format = fmt.format;
1480
1481 /* Set a couple of internal variables if appropriate. */
1482 if (last_examine_value)
1483 {
1484 /* Make last address examined available to the user as $_. Use
1485 the correct pointer type. */
1486 struct type *pointer_type
1487 = lookup_pointer_type (value_type (last_examine_value));
1488 set_internalvar (lookup_internalvar ("_"),
1489 value_from_pointer (pointer_type,
1490 last_examine_address));
1491
1492 /* Make contents of last address examined available to the user
1493 as $__. If the last value has not been fetched from memory
1494 then don't fetch it now; instead mark it by voiding the $__
1495 variable. */
1496 if (value_lazy (last_examine_value))
1497 clear_internalvar (lookup_internalvar ("__"));
1498 else
1499 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1500 }
1501 }
1502
1503
1505 /* Add an expression to the auto-display chain.
1506 Specify the expression. */
1507
1508 static void
1509 display_command (char *arg, int from_tty)
1510 {
1511 struct format_data fmt;
1512 struct expression *expr;
1513 struct display *new;
1514 int display_it = 1;
1515 const char *exp = arg;
1516
1517 #if defined(TUI)
1518 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1519 `tui_version'. */
1520 if (tui_active && exp != NULL && *exp == '$')
1521 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1522 #endif
1523
1524 if (display_it)
1525 {
1526 if (exp == 0)
1527 {
1528 do_displays ();
1529 return;
1530 }
1531
1532 if (*exp == '/')
1533 {
1534 exp++;
1535 fmt = decode_format (&exp, 0, 0);
1536 if (fmt.size && fmt.format == 0)
1537 fmt.format = 'x';
1538 if (fmt.format == 'i' || fmt.format == 's')
1539 fmt.size = 'b';
1540 }
1541 else
1542 {
1543 fmt.format = 0;
1544 fmt.size = 0;
1545 fmt.count = 0;
1546 fmt.raw = 0;
1547 }
1548
1549 innermost_block = NULL;
1550 expr = parse_expression (exp);
1551
1552 new = (struct display *) xmalloc (sizeof (struct display));
1553
1554 new->exp_string = xstrdup (exp);
1555 new->exp = expr;
1556 new->block = innermost_block;
1557 new->pspace = current_program_space;
1558 new->next = display_chain;
1559 new->number = ++display_number;
1560 new->format = fmt;
1561 new->enabled_p = 1;
1562 display_chain = new;
1563
1564 if (from_tty && target_has_execution)
1565 do_one_display (new);
1566
1567 dont_repeat ();
1568 }
1569 }
1570
1571 static void
1572 free_display (struct display *d)
1573 {
1574 xfree (d->exp_string);
1575 xfree (d->exp);
1576 xfree (d);
1577 }
1578
1579 /* Clear out the display_chain. Done when new symtabs are loaded,
1580 since this invalidates the types stored in many expressions. */
1581
1582 void
1583 clear_displays (void)
1584 {
1585 struct display *d;
1586
1587 while ((d = display_chain) != NULL)
1588 {
1589 display_chain = d->next;
1590 free_display (d);
1591 }
1592 }
1593
1594 /* Delete the auto-display DISPLAY. */
1595
1596 static void
1597 delete_display (struct display *display)
1598 {
1599 struct display *d;
1600
1601 gdb_assert (display != NULL);
1602
1603 if (display_chain == display)
1604 display_chain = display->next;
1605
1606 ALL_DISPLAYS (d)
1607 if (d->next == display)
1608 {
1609 d->next = display->next;
1610 break;
1611 }
1612
1613 free_display (display);
1614 }
1615
1616 /* Call FUNCTION on each of the displays whose numbers are given in
1617 ARGS. DATA is passed unmodified to FUNCTION. */
1618
1619 static void
1620 map_display_numbers (char *args,
1621 void (*function) (struct display *,
1622 void *),
1623 void *data)
1624 {
1625 struct get_number_or_range_state state;
1626 int num;
1627
1628 if (args == NULL)
1629 error_no_arg (_("one or more display numbers"));
1630
1631 init_number_or_range (&state, args);
1632
1633 while (!state.finished)
1634 {
1635 char *p = state.string;
1636
1637 num = get_number_or_range (&state);
1638 if (num == 0)
1639 warning (_("bad display number at or near '%s'"), p);
1640 else
1641 {
1642 struct display *d, *tmp;
1643
1644 ALL_DISPLAYS_SAFE (d, tmp)
1645 if (d->number == num)
1646 break;
1647 if (d == NULL)
1648 printf_unfiltered (_("No display number %d.\n"), num);
1649 else
1650 function (d, data);
1651 }
1652 }
1653 }
1654
1655 /* Callback for map_display_numbers, that deletes a display. */
1656
1657 static void
1658 do_delete_display (struct display *d, void *data)
1659 {
1660 delete_display (d);
1661 }
1662
1663 /* "undisplay" command. */
1664
1665 static void
1666 undisplay_command (char *args, int from_tty)
1667 {
1668 if (args == NULL)
1669 {
1670 if (query (_("Delete all auto-display expressions? ")))
1671 clear_displays ();
1672 dont_repeat ();
1673 return;
1674 }
1675
1676 map_display_numbers (args, do_delete_display, NULL);
1677 dont_repeat ();
1678 }
1679
1680 /* Display a single auto-display.
1681 Do nothing if the display cannot be printed in the current context,
1682 or if the display is disabled. */
1683
1684 static void
1685 do_one_display (struct display *d)
1686 {
1687 struct cleanup *old_chain;
1688 int within_current_scope;
1689
1690 if (d->enabled_p == 0)
1691 return;
1692
1693 /* The expression carries the architecture that was used at parse time.
1694 This is a problem if the expression depends on architecture features
1695 (e.g. register numbers), and the current architecture is now different.
1696 For example, a display statement like "display/i $pc" is expected to
1697 display the PC register of the current architecture, not the arch at
1698 the time the display command was given. Therefore, we re-parse the
1699 expression if the current architecture has changed. */
1700 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
1701 {
1702 xfree (d->exp);
1703 d->exp = NULL;
1704 d->block = NULL;
1705 }
1706
1707 if (d->exp == NULL)
1708 {
1709 volatile struct gdb_exception ex;
1710
1711 TRY_CATCH (ex, RETURN_MASK_ALL)
1712 {
1713 innermost_block = NULL;
1714 d->exp = parse_expression (d->exp_string);
1715 d->block = innermost_block;
1716 }
1717 if (ex.reason < 0)
1718 {
1719 /* Can't re-parse the expression. Disable this display item. */
1720 d->enabled_p = 0;
1721 warning (_("Unable to display \"%s\": %s"),
1722 d->exp_string, ex.message);
1723 return;
1724 }
1725 }
1726
1727 if (d->block)
1728 {
1729 if (d->pspace == current_program_space)
1730 within_current_scope = contained_in (get_selected_block (0), d->block);
1731 else
1732 within_current_scope = 0;
1733 }
1734 else
1735 within_current_scope = 1;
1736 if (!within_current_scope)
1737 return;
1738
1739 old_chain = make_cleanup_restore_integer (¤t_display_number);
1740 current_display_number = d->number;
1741
1742 annotate_display_begin ();
1743 printf_filtered ("%d", d->number);
1744 annotate_display_number_end ();
1745 printf_filtered (": ");
1746 if (d->format.size)
1747 {
1748 volatile struct gdb_exception ex;
1749
1750 annotate_display_format ();
1751
1752 printf_filtered ("x/");
1753 if (d->format.count != 1)
1754 printf_filtered ("%d", d->format.count);
1755 printf_filtered ("%c", d->format.format);
1756 if (d->format.format != 'i' && d->format.format != 's')
1757 printf_filtered ("%c", d->format.size);
1758 printf_filtered (" ");
1759
1760 annotate_display_expression ();
1761
1762 puts_filtered (d->exp_string);
1763 annotate_display_expression_end ();
1764
1765 if (d->format.count != 1 || d->format.format == 'i')
1766 printf_filtered ("\n");
1767 else
1768 printf_filtered (" ");
1769
1770 annotate_display_value ();
1771
1772 TRY_CATCH (ex, RETURN_MASK_ERROR)
1773 {
1774 struct value *val;
1775 CORE_ADDR addr;
1776
1777 val = evaluate_expression (d->exp);
1778 addr = value_as_address (val);
1779 if (d->format.format == 'i')
1780 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
1781 do_examine (d->format, d->exp->gdbarch, addr);
1782 }
1783 if (ex.reason < 0)
1784 fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
1785 }
1786 else
1787 {
1788 struct value_print_options opts;
1789 volatile struct gdb_exception ex;
1790
1791 annotate_display_format ();
1792
1793 if (d->format.format)
1794 printf_filtered ("/%c ", d->format.format);
1795
1796 annotate_display_expression ();
1797
1798 puts_filtered (d->exp_string);
1799 annotate_display_expression_end ();
1800
1801 printf_filtered (" = ");
1802
1803 annotate_display_expression ();
1804
1805 get_formatted_print_options (&opts, d->format.format);
1806 opts.raw = d->format.raw;
1807
1808 TRY_CATCH (ex, RETURN_MASK_ERROR)
1809 {
1810 struct value *val;
1811
1812 val = evaluate_expression (d->exp);
1813 print_formatted (val, d->format.size, &opts, gdb_stdout);
1814 }
1815 if (ex.reason < 0)
1816 fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
1817 printf_filtered ("\n");
1818 }
1819
1820 annotate_display_end ();
1821
1822 gdb_flush (gdb_stdout);
1823 do_cleanups (old_chain);
1824 }
1825
1826 /* Display all of the values on the auto-display chain which can be
1827 evaluated in the current scope. */
1828
1829 void
1830 do_displays (void)
1831 {
1832 struct display *d;
1833
1834 for (d = display_chain; d; d = d->next)
1835 do_one_display (d);
1836 }
1837
1838 /* Delete the auto-display which we were in the process of displaying.
1839 This is done when there is an error or a signal. */
1840
1841 void
1842 disable_display (int num)
1843 {
1844 struct display *d;
1845
1846 for (d = display_chain; d; d = d->next)
1847 if (d->number == num)
1848 {
1849 d->enabled_p = 0;
1850 return;
1851 }
1852 printf_unfiltered (_("No display number %d.\n"), num);
1853 }
1854
1855 void
1856 disable_current_display (void)
1857 {
1858 if (current_display_number >= 0)
1859 {
1860 disable_display (current_display_number);
1861 fprintf_unfiltered (gdb_stderr,
1862 _("Disabling display %d to "
1863 "avoid infinite recursion.\n"),
1864 current_display_number);
1865 }
1866 current_display_number = -1;
1867 }
1868
1869 static void
1870 display_info (char *ignore, int from_tty)
1871 {
1872 struct display *d;
1873
1874 if (!display_chain)
1875 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1876 else
1877 printf_filtered (_("Auto-display expressions now in effect:\n\
1878 Num Enb Expression\n"));
1879
1880 for (d = display_chain; d; d = d->next)
1881 {
1882 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1883 if (d->format.size)
1884 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1885 d->format.format);
1886 else if (d->format.format)
1887 printf_filtered ("/%c ", d->format.format);
1888 puts_filtered (d->exp_string);
1889 if (d->block && !contained_in (get_selected_block (0), d->block))
1890 printf_filtered (_(" (cannot be evaluated in the current context)"));
1891 printf_filtered ("\n");
1892 gdb_flush (gdb_stdout);
1893 }
1894 }
1895
1896 /* Callback fo map_display_numbers, that enables or disables the
1897 passed in display D. */
1898
1899 static void
1900 do_enable_disable_display (struct display *d, void *data)
1901 {
1902 d->enabled_p = *(int *) data;
1903 }
1904
1905 /* Implamentation of both the "disable display" and "enable display"
1906 commands. ENABLE decides what to do. */
1907
1908 static void
1909 enable_disable_display_command (char *args, int from_tty, int enable)
1910 {
1911 if (args == NULL)
1912 {
1913 struct display *d;
1914
1915 ALL_DISPLAYS (d)
1916 d->enabled_p = enable;
1917 return;
1918 }
1919
1920 map_display_numbers (args, do_enable_disable_display, &enable);
1921 }
1922
1923 /* The "enable display" command. */
1924
1925 static void
1926 enable_display_command (char *args, int from_tty)
1927 {
1928 enable_disable_display_command (args, from_tty, 1);
1929 }
1930
1931 /* The "disable display" command. */
1932
1933 static void
1934 disable_display_command (char *args, int from_tty)
1935 {
1936 enable_disable_display_command (args, from_tty, 0);
1937 }
1938
1939 /* display_chain items point to blocks and expressions. Some expressions in
1940 turn may point to symbols.
1941 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1942 obstack_free'd when a shared library is unloaded.
1943 Clear pointers that are about to become dangling.
1944 Both .exp and .block fields will be restored next time we need to display
1945 an item by re-parsing .exp_string field in the new execution context. */
1946
1947 static void
1948 clear_dangling_display_expressions (struct objfile *objfile)
1949 {
1950 struct display *d;
1951 struct program_space *pspace;
1952
1953 /* With no symbol file we cannot have a block or expression from it. */
1954 if (objfile == NULL)
1955 return;
1956 pspace = objfile->pspace;
1957 if (objfile->separate_debug_objfile_backlink)
1958 {
1959 objfile = objfile->separate_debug_objfile_backlink;
1960 gdb_assert (objfile->pspace == pspace);
1961 }
1962
1963 for (d = display_chain; d != NULL; d = d->next)
1964 {
1965 if (d->pspace != pspace)
1966 continue;
1967
1968 if (lookup_objfile_from_block (d->block) == objfile
1969 || (d->exp && exp_uses_objfile (d->exp, objfile)))
1970 {
1971 xfree (d->exp);
1972 d->exp = NULL;
1973 d->block = NULL;
1974 }
1975 }
1976 }
1977
1978
1980 /* Print the value in stack frame FRAME of a variable specified by a
1981 struct symbol. NAME is the name to print; if NULL then VAR's print
1982 name will be used. STREAM is the ui_file on which to print the
1983 value. INDENT specifies the number of indent levels to print
1984 before printing the variable name.
1985
1986 This function invalidates FRAME. */
1987
1988 void
1989 print_variable_and_value (const char *name, struct symbol *var,
1990 struct frame_info *frame,
1991 struct ui_file *stream, int indent)
1992 {
1993 volatile struct gdb_exception except;
1994
1995 if (!name)
1996 name = SYMBOL_PRINT_NAME (var);
1997
1998 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1999 TRY_CATCH (except, RETURN_MASK_ERROR)
2000 {
2001 struct value *val;
2002 struct value_print_options opts;
2003
2004 val = read_var_value (var, frame);
2005 get_user_print_options (&opts);
2006 opts.deref_ref = 1;
2007 common_val_print (val, stream, indent, &opts, current_language);
2008
2009 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2010 function. */
2011 frame = NULL;
2012 }
2013 if (except.reason < 0)
2014 fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
2015 except.message);
2016 fprintf_filtered (stream, "\n");
2017 }
2018
2019 /* Subroutine of ui_printf to simplify it.
2020 Print VALUE to STREAM using FORMAT.
2021 VALUE is a C-style string on the target. */
2022
2023 static void
2024 printf_c_string (struct ui_file *stream, const char *format,
2025 struct value *value)
2026 {
2027 gdb_byte *str;
2028 CORE_ADDR tem;
2029 int j;
2030
2031 tem = value_as_address (value);
2032
2033 /* This is a %s argument. Find the length of the string. */
2034 for (j = 0;; j++)
2035 {
2036 gdb_byte c;
2037
2038 QUIT;
2039 read_memory (tem + j, &c, 1);
2040 if (c == 0)
2041 break;
2042 }
2043
2044 /* Copy the string contents into a string inside GDB. */
2045 str = (gdb_byte *) alloca (j + 1);
2046 if (j != 0)
2047 read_memory (tem, str, j);
2048 str[j] = 0;
2049
2050 fprintf_filtered (stream, format, (char *) str);
2051 }
2052
2053 /* Subroutine of ui_printf to simplify it.
2054 Print VALUE to STREAM using FORMAT.
2055 VALUE is a wide C-style string on the target. */
2056
2057 static void
2058 printf_wide_c_string (struct ui_file *stream, const char *format,
2059 struct value *value)
2060 {
2061 gdb_byte *str;
2062 CORE_ADDR tem;
2063 int j;
2064 struct gdbarch *gdbarch = get_type_arch (value_type (value));
2065 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2066 struct type *wctype = lookup_typename (current_language, gdbarch,
2067 "wchar_t", NULL, 0);
2068 int wcwidth = TYPE_LENGTH (wctype);
2069 gdb_byte *buf = alloca (wcwidth);
2070 struct obstack output;
2071 struct cleanup *inner_cleanup;
2072
2073 tem = value_as_address (value);
2074
2075 /* This is a %s argument. Find the length of the string. */
2076 for (j = 0;; j += wcwidth)
2077 {
2078 QUIT;
2079 read_memory (tem + j, buf, wcwidth);
2080 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2081 break;
2082 }
2083
2084 /* Copy the string contents into a string inside GDB. */
2085 str = (gdb_byte *) alloca (j + wcwidth);
2086 if (j != 0)
2087 read_memory (tem, str, j);
2088 memset (&str[j], 0, wcwidth);
2089
2090 obstack_init (&output);
2091 inner_cleanup = make_cleanup_obstack_free (&output);
2092
2093 convert_between_encodings (target_wide_charset (gdbarch),
2094 host_charset (),
2095 str, j, wcwidth,
2096 &output, translit_char);
2097 obstack_grow_str0 (&output, "");
2098
2099 fprintf_filtered (stream, format, obstack_base (&output));
2100 do_cleanups (inner_cleanup);
2101 }
2102
2103 /* Subroutine of ui_printf to simplify it.
2104 Print VALUE, a decimal floating point value, to STREAM using FORMAT. */
2105
2106 static void
2107 printf_decfloat (struct ui_file *stream, const char *format,
2108 struct value *value)
2109 {
2110 const gdb_byte *param_ptr = value_contents (value);
2111
2112 #if defined (PRINTF_HAS_DECFLOAT)
2113 /* If we have native support for Decimal floating
2114 printing, handle it here. */
2115 fprintf_filtered (stream, format, param_ptr);
2116 #else
2117 /* As a workaround until vasprintf has native support for DFP
2118 we convert the DFP values to string and print them using
2119 the %s format specifier. */
2120 const char *p;
2121
2122 /* Parameter data. */
2123 struct type *param_type = value_type (value);
2124 struct gdbarch *gdbarch = get_type_arch (param_type);
2125 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2126
2127 /* DFP output data. */
2128 struct value *dfp_value = NULL;
2129 gdb_byte *dfp_ptr;
2130 int dfp_len = 16;
2131 gdb_byte dec[16];
2132 struct type *dfp_type = NULL;
2133 char decstr[MAX_DECIMAL_STRING];
2134
2135 /* Points to the end of the string so that we can go back
2136 and check for DFP length modifiers. */
2137 p = format + strlen (format);
2138
2139 /* Look for the float/double format specifier. */
2140 while (*p != 'f' && *p != 'e' && *p != 'E'
2141 && *p != 'g' && *p != 'G')
2142 p--;
2143
2144 /* Search for the '%' char and extract the size and type of
2145 the output decimal value based on its modifiers
2146 (%Hf, %Df, %DDf). */
2147 while (*--p != '%')
2148 {
2149 if (*p == 'H')
2150 {
2151 dfp_len = 4;
2152 dfp_type = builtin_type (gdbarch)->builtin_decfloat;
2153 }
2154 else if (*p == 'D' && *(p - 1) == 'D')
2155 {
2156 dfp_len = 16;
2157 dfp_type = builtin_type (gdbarch)->builtin_declong;
2158 p--;
2159 }
2160 else
2161 {
2162 dfp_len = 8;
2163 dfp_type = builtin_type (gdbarch)->builtin_decdouble;
2164 }
2165 }
2166
2167 /* Conversion between different DFP types. */
2168 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2169 decimal_convert (param_ptr, TYPE_LENGTH (param_type),
2170 byte_order, dec, dfp_len, byte_order);
2171 else
2172 /* If this is a non-trivial conversion, just output 0.
2173 A correct converted value can be displayed by explicitly
2174 casting to a DFP type. */
2175 decimal_from_string (dec, dfp_len, byte_order, "0");
2176
2177 dfp_value = value_from_decfloat (dfp_type, dec);
2178
2179 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2180
2181 decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
2182
2183 /* Print the DFP value. */
2184 fprintf_filtered (stream, "%s", decstr);
2185 #endif
2186 }
2187
2188 /* Subroutine of ui_printf to simplify it.
2189 Print VALUE, a target pointer, to STREAM using FORMAT. */
2190
2191 static void
2192 printf_pointer (struct ui_file *stream, const char *format,
2193 struct value *value)
2194 {
2195 /* We avoid the host's %p because pointers are too
2196 likely to be the wrong size. The only interesting
2197 modifier for %p is a width; extract that, and then
2198 handle %p as glibc would: %#x or a literal "(nil)". */
2199
2200 const char *p;
2201 char *fmt, *fmt_p;
2202 #ifdef PRINTF_HAS_LONG_LONG
2203 long long val = value_as_long (value);
2204 #else
2205 long val = value_as_long (value);
2206 #endif
2207
2208 fmt = alloca (strlen (format) + 5);
2209
2210 /* Copy up to the leading %. */
2211 p = format;
2212 fmt_p = fmt;
2213 while (*p)
2214 {
2215 int is_percent = (*p == '%');
2216
2217 *fmt_p++ = *p++;
2218 if (is_percent)
2219 {
2220 if (*p == '%')
2221 *fmt_p++ = *p++;
2222 else
2223 break;
2224 }
2225 }
2226
2227 if (val != 0)
2228 *fmt_p++ = '#';
2229
2230 /* Copy any width. */
2231 while (*p >= '0' && *p < '9')
2232 *fmt_p++ = *p++;
2233
2234 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2235 if (val != 0)
2236 {
2237 #ifdef PRINTF_HAS_LONG_LONG
2238 *fmt_p++ = 'l';
2239 #endif
2240 *fmt_p++ = 'l';
2241 *fmt_p++ = 'x';
2242 *fmt_p++ = '\0';
2243 fprintf_filtered (stream, fmt, val);
2244 }
2245 else
2246 {
2247 *fmt_p++ = 's';
2248 *fmt_p++ = '\0';
2249 fprintf_filtered (stream, fmt, "(nil)");
2250 }
2251 }
2252
2253 /* printf "printf format string" ARG to STREAM. */
2254
2255 static void
2256 ui_printf (const char *arg, struct ui_file *stream)
2257 {
2258 struct format_piece *fpieces;
2259 const char *s = arg;
2260 struct value **val_args;
2261 int allocated_args = 20;
2262 struct cleanup *old_cleanups;
2263
2264 val_args = xmalloc (allocated_args * sizeof (struct value *));
2265 old_cleanups = make_cleanup (free_current_contents, &val_args);
2266
2267 if (s == 0)
2268 error_no_arg (_("format-control string and values to print"));
2269
2270 s = skip_spaces_const (s);
2271
2272 /* A format string should follow, enveloped in double quotes. */
2273 if (*s++ != '"')
2274 error (_("Bad format string, missing '\"'."));
2275
2276 fpieces = parse_format_string (&s);
2277
2278 make_cleanup (free_format_pieces_cleanup, &fpieces);
2279
2280 if (*s++ != '"')
2281 error (_("Bad format string, non-terminated '\"'."));
2282
2283 s = skip_spaces_const (s);
2284
2285 if (*s != ',' && *s != 0)
2286 error (_("Invalid argument syntax"));
2287
2288 if (*s == ',')
2289 s++;
2290 s = skip_spaces_const (s);
2291
2292 {
2293 int nargs = 0;
2294 int nargs_wanted;
2295 int i, fr;
2296 char *current_substring;
2297
2298 nargs_wanted = 0;
2299 for (fr = 0; fpieces[fr].string != NULL; fr++)
2300 if (fpieces[fr].argclass != literal_piece)
2301 ++nargs_wanted;
2302
2303 /* Now, parse all arguments and evaluate them.
2304 Store the VALUEs in VAL_ARGS. */
2305
2306 while (*s != '\0')
2307 {
2308 const char *s1;
2309
2310 if (nargs == allocated_args)
2311 val_args = (struct value **) xrealloc ((char *) val_args,
2312 (allocated_args *= 2)
2313 * sizeof (struct value *));
2314 s1 = s;
2315 val_args[nargs] = parse_to_comma_and_eval (&s1);
2316
2317 nargs++;
2318 s = s1;
2319 if (*s == ',')
2320 s++;
2321 }
2322
2323 if (nargs != nargs_wanted)
2324 error (_("Wrong number of arguments for specified format-string"));
2325
2326 /* Now actually print them. */
2327 i = 0;
2328 for (fr = 0; fpieces[fr].string != NULL; fr++)
2329 {
2330 current_substring = fpieces[fr].string;
2331 switch (fpieces[fr].argclass)
2332 {
2333 case string_arg:
2334 printf_c_string (stream, current_substring, val_args[i]);
2335 break;
2336 case wide_string_arg:
2337 printf_wide_c_string (stream, current_substring, val_args[i]);
2338 break;
2339 case wide_char_arg:
2340 {
2341 struct gdbarch *gdbarch
2342 = get_type_arch (value_type (val_args[i]));
2343 struct type *wctype = lookup_typename (current_language, gdbarch,
2344 "wchar_t", NULL, 0);
2345 struct type *valtype;
2346 struct obstack output;
2347 struct cleanup *inner_cleanup;
2348 const gdb_byte *bytes;
2349
2350 valtype = value_type (val_args[i]);
2351 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2352 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2353 error (_("expected wchar_t argument for %%lc"));
2354
2355 bytes = value_contents (val_args[i]);
2356
2357 obstack_init (&output);
2358 inner_cleanup = make_cleanup_obstack_free (&output);
2359
2360 convert_between_encodings (target_wide_charset (gdbarch),
2361 host_charset (),
2362 bytes, TYPE_LENGTH (valtype),
2363 TYPE_LENGTH (valtype),
2364 &output, translit_char);
2365 obstack_grow_str0 (&output, "");
2366
2367 fprintf_filtered (stream, current_substring,
2368 obstack_base (&output));
2369 do_cleanups (inner_cleanup);
2370 }
2371 break;
2372 case double_arg:
2373 {
2374 struct type *type = value_type (val_args[i]);
2375 DOUBLEST val;
2376 int inv;
2377
2378 /* If format string wants a float, unchecked-convert the value
2379 to floating point of the same size. */
2380 type = float_type_from_length (type);
2381 val = unpack_double (type, value_contents (val_args[i]), &inv);
2382 if (inv)
2383 error (_("Invalid floating value found in program."));
2384
2385 fprintf_filtered (stream, current_substring, (double) val);
2386 break;
2387 }
2388 case long_double_arg:
2389 #ifdef HAVE_LONG_DOUBLE
2390 {
2391 struct type *type = value_type (val_args[i]);
2392 DOUBLEST val;
2393 int inv;
2394
2395 /* If format string wants a float, unchecked-convert the value
2396 to floating point of the same size. */
2397 type = float_type_from_length (type);
2398 val = unpack_double (type, value_contents (val_args[i]), &inv);
2399 if (inv)
2400 error (_("Invalid floating value found in program."));
2401
2402 fprintf_filtered (stream, current_substring,
2403 (long double) val);
2404 break;
2405 }
2406 #else
2407 error (_("long double not supported in printf"));
2408 #endif
2409 case long_long_arg:
2410 #ifdef PRINTF_HAS_LONG_LONG
2411 {
2412 long long val = value_as_long (val_args[i]);
2413
2414 fprintf_filtered (stream, current_substring, val);
2415 break;
2416 }
2417 #else
2418 error (_("long long not supported in printf"));
2419 #endif
2420 case int_arg:
2421 {
2422 int val = value_as_long (val_args[i]);
2423
2424 fprintf_filtered (stream, current_substring, val);
2425 break;
2426 }
2427 case long_arg:
2428 {
2429 long val = value_as_long (val_args[i]);
2430
2431 fprintf_filtered (stream, current_substring, val);
2432 break;
2433 }
2434 /* Handles decimal floating values. */
2435 case decfloat_arg:
2436 printf_decfloat (stream, current_substring, val_args[i]);
2437 break;
2438 case ptr_arg:
2439 printf_pointer (stream, current_substring, val_args[i]);
2440 break;
2441 case literal_piece:
2442 /* Print a portion of the format string that has no
2443 directives. Note that this will not include any
2444 ordinary %-specs, but it might include "%%". That is
2445 why we use printf_filtered and not puts_filtered here.
2446 Also, we pass a dummy argument because some platforms
2447 have modified GCC to include -Wformat-security by
2448 default, which will warn here if there is no
2449 argument. */
2450 fprintf_filtered (stream, current_substring, 0);
2451 break;
2452 default:
2453 internal_error (__FILE__, __LINE__,
2454 _("failed internal consistency check"));
2455 }
2456 /* Maybe advance to the next argument. */
2457 if (fpieces[fr].argclass != literal_piece)
2458 ++i;
2459 }
2460 }
2461 do_cleanups (old_cleanups);
2462 }
2463
2464 /* Implement the "printf" command. */
2465
2466 static void
2467 printf_command (char *arg, int from_tty)
2468 {
2469 ui_printf (arg, gdb_stdout);
2470 gdb_flush (gdb_stdout);
2471 }
2472
2473 /* Implement the "eval" command. */
2474
2475 static void
2476 eval_command (char *arg, int from_tty)
2477 {
2478 struct ui_file *ui_out = mem_fileopen ();
2479 struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
2480 char *expanded;
2481
2482 ui_printf (arg, ui_out);
2483
2484 expanded = ui_file_xstrdup (ui_out, NULL);
2485 make_cleanup (xfree, expanded);
2486
2487 execute_command (expanded, from_tty);
2488
2489 do_cleanups (cleanups);
2490 }
2491
2492 void
2493 _initialize_printcmd (void)
2494 {
2495 struct cmd_list_element *c;
2496
2497 current_display_number = -1;
2498
2499 observer_attach_free_objfile (clear_dangling_display_expressions);
2500
2501 add_info ("address", address_info,
2502 _("Describe where symbol SYM is stored."));
2503
2504 add_info ("symbol", sym_info, _("\
2505 Describe what symbol is at location ADDR.\n\
2506 Only for symbols with fixed locations (global or static scope)."));
2507
2508 add_com ("x", class_vars, x_command, _("\
2509 Examine memory: x/FMT ADDRESS.\n\
2510 ADDRESS is an expression for the memory address to examine.\n\
2511 FMT is a repeat count followed by a format letter and a size letter.\n\
2512 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2513 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2514 and z(hex, zero padded on the left).\n\
2515 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2516 The specified number of objects of the specified size are printed\n\
2517 according to the format.\n\n\
2518 Defaults for format and size letters are those previously used.\n\
2519 Default count is 1. Default address is following last thing printed\n\
2520 with this command or \"print\"."));
2521
2522 #if 0
2523 add_com ("whereis", class_vars, whereis_command,
2524 _("Print line number and file of definition of variable."));
2525 #endif
2526
2527 add_info ("display", display_info, _("\
2528 Expressions to display when program stops, with code numbers."));
2529
2530 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2531 Cancel some expressions to be displayed when program stops.\n\
2532 Arguments are the code numbers of the expressions to stop displaying.\n\
2533 No argument means cancel all automatic-display expressions.\n\
2534 \"delete display\" has the same effect as this command.\n\
2535 Do \"info display\" to see current list of code numbers."),
2536 &cmdlist);
2537
2538 add_com ("display", class_vars, display_command, _("\
2539 Print value of expression EXP each time the program stops.\n\
2540 /FMT may be used before EXP as in the \"print\" command.\n\
2541 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2542 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2543 and examining is done as in the \"x\" command.\n\n\
2544 With no argument, display all currently requested auto-display expressions.\n\
2545 Use \"undisplay\" to cancel display requests previously made."));
2546
2547 add_cmd ("display", class_vars, enable_display_command, _("\
2548 Enable some expressions to be displayed when program stops.\n\
2549 Arguments are the code numbers of the expressions to resume displaying.\n\
2550 No argument means enable all automatic-display expressions.\n\
2551 Do \"info display\" to see current list of code numbers."), &enablelist);
2552
2553 add_cmd ("display", class_vars, disable_display_command, _("\
2554 Disable some expressions to be displayed when program stops.\n\
2555 Arguments are the code numbers of the expressions to stop displaying.\n\
2556 No argument means disable all automatic-display expressions.\n\
2557 Do \"info display\" to see current list of code numbers."), &disablelist);
2558
2559 add_cmd ("display", class_vars, undisplay_command, _("\
2560 Cancel some expressions to be displayed when program stops.\n\
2561 Arguments are the code numbers of the expressions to stop displaying.\n\
2562 No argument means cancel all automatic-display expressions.\n\
2563 Do \"info display\" to see current list of code numbers."), &deletelist);
2564
2565 add_com ("printf", class_vars, printf_command, _("\
2566 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2567 This is useful for formatted output in user-defined commands."));
2568
2569 add_com ("output", class_vars, output_command, _("\
2570 Like \"print\" but don't put in value history and don't print newline.\n\
2571 This is useful in user-defined commands."));
2572
2573 add_prefix_cmd ("set", class_vars, set_command, _("\
2574 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2575 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2576 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2577 with $), a register (a few standard names starting with $), or an actual\n\
2578 variable in the program being debugged. EXP is any valid expression.\n\
2579 Use \"set variable\" for variables with names identical to set subcommands.\n\
2580 \n\
2581 With a subcommand, this command modifies parts of the gdb environment.\n\
2582 You can see these environment settings with the \"show\" command."),
2583 &setlist, "set ", 1, &cmdlist);
2584 if (dbx_commands)
2585 add_com ("assign", class_vars, set_command, _("\
2586 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2587 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2588 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2589 with $), a register (a few standard names starting with $), or an actual\n\
2590 variable in the program being debugged. EXP is any valid expression.\n\
2591 Use \"set variable\" for variables with names identical to set subcommands.\n\
2592 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2593 You can see these environment settings with the \"show\" command."));
2594
2595 /* "call" is the same as "set", but handy for dbx users to call fns. */
2596 c = add_com ("call", class_vars, call_command, _("\
2597 Call a function in the program.\n\
2598 The argument is the function name and arguments, in the notation of the\n\
2599 current working language. The result is printed and saved in the value\n\
2600 history, if it is not void."));
2601 set_cmd_completer (c, expression_completer);
2602
2603 add_cmd ("variable", class_vars, set_command, _("\
2604 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2605 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2606 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2607 with $), a register (a few standard names starting with $), or an actual\n\
2608 variable in the program being debugged. EXP is any valid expression.\n\
2609 This may usually be abbreviated to simply \"set\"."),
2610 &setlist);
2611
2612 c = add_com ("print", class_vars, print_command, _("\
2613 Print value of expression EXP.\n\
2614 Variables accessible are those of the lexical environment of the selected\n\
2615 stack frame, plus all those whose scope is global or an entire file.\n\
2616 \n\
2617 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2618 $$NUM refers to NUM'th value back from the last one.\n\
2619 Names starting with $ refer to registers (with the values they would have\n\
2620 if the program were to return to the stack frame now selected, restoring\n\
2621 all registers saved by frames farther in) or else to debugger\n\
2622 \"convenience\" variables (any such name not a known register).\n\
2623 Use assignment expressions to give values to convenience variables.\n\
2624 \n\
2625 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2626 @ is a binary operator for treating consecutive data objects\n\
2627 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2628 element is FOO, whose second element is stored in the space following\n\
2629 where FOO is stored, etc. FOO must be an expression whose value\n\
2630 resides in memory.\n\
2631 \n\
2632 EXP may be preceded with /FMT, where FMT is a format letter\n\
2633 but no count or size letter (see \"x\" command)."));
2634 set_cmd_completer (c, expression_completer);
2635 add_com_alias ("p", "print", class_vars, 1);
2636 add_com_alias ("inspect", "print", class_vars, 1);
2637
2638 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2639 &max_symbolic_offset, _("\
2640 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2641 Show the largest offset that will be printed in <symbol+1234> form."), _("\
2642 Tell GDB to only display the symbolic form of an address if the\n\
2643 offset between the closest earlier symbol and the address is less than\n\
2644 the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2645 to always print the symbolic form of an address if any symbol precedes\n\
2646 it. Zero is equivalent to \"unlimited\"."),
2647 NULL,
2648 show_max_symbolic_offset,
2649 &setprintlist, &showprintlist);
2650 add_setshow_boolean_cmd ("symbol-filename", no_class,
2651 &print_symbol_filename, _("\
2652 Set printing of source filename and line number with <symbol>."), _("\
2653 Show printing of source filename and line number with <symbol>."), NULL,
2654 NULL,
2655 show_print_symbol_filename,
2656 &setprintlist, &showprintlist);
2657
2658 add_com ("eval", no_class, eval_command, _("\
2659 Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2660 a command line, and call it."));
2661 }
2662