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