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