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