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