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