2009-03-09 Paul Pluzhnikov <ppluzhnikov@google.com>
[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 i;
1767   struct expression *const exp = d->exp;
1768
1769   if (d->block != NULL
1770       && solib_contains_address_p (solib, d->block->startaddr))
1771     return 1;
1772
1773   for (i = 0; i < exp->nelts; )
1774     {
1775       int args, oplen = 0;
1776       const union exp_element *const elts = exp->elts;
1777
1778       if (elts[i].opcode == OP_VAR_VALUE)
1779         {
1780           const struct block *const block = elts[i + 1].block;
1781           const struct symbol *const symbol = elts[i + 2].symbol;
1782           const struct obj_section *const section =
1783             SYMBOL_OBJ_SECTION (symbol);
1784
1785           if (block != NULL
1786               && solib_contains_address_p (solib, block->startaddr))
1787             return 1;
1788
1789           if (section && section->objfile == solib->objfile)
1790             return 1;
1791         }
1792       exp->language_defn->la_exp_desc->operator_length (exp, i + 1,
1793                                                         &oplen, &args);
1794       gdb_assert (oplen > 0);
1795       i += oplen;
1796     }
1797   return 0;
1798 }
1799
1800 /* display_chain items point to blocks and expressions.  Some expressions in
1801    turn may point to symbols.
1802    Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
1803    obstack_free'd when a shared library is unloaded.
1804    Clear pointers that are about to become dangling.
1805    Both .exp and .block fields will be restored next time we need to display
1806    an item by re-parsing .exp_string field in the new execution context.  */
1807
1808 static void
1809 clear_dangling_display_expressions (struct so_list *solib)
1810 {
1811   struct display *d;
1812   struct objfile *objfile = NULL;
1813
1814   for (d = display_chain; d; d = d->next)
1815     {
1816       if (d->exp && display_uses_solib_p (d, solib))
1817         {
1818           xfree (d->exp);
1819           d->exp = NULL;
1820           d->block = NULL;
1821         }
1822     }
1823 }
1824 \f
1825
1826 /* Print the value in stack frame FRAME of a variable specified by a
1827    struct symbol.  NAME is the name to print; if NULL then VAR's print
1828    name will be used.  STREAM is the ui_file on which to print the
1829    value.  INDENT specifies the number of indent levels to print
1830    before printing the variable name.  */
1831
1832 void
1833 print_variable_and_value (const char *name, struct symbol *var,
1834                           struct frame_info *frame,
1835                           struct ui_file *stream, int indent)
1836 {
1837   struct value *val;
1838   struct value_print_options opts;
1839
1840   if (!name)
1841     name = SYMBOL_PRINT_NAME (var);
1842
1843   fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1844
1845   val = read_var_value (var, frame);
1846   get_user_print_options (&opts);
1847   common_val_print (val, stream, indent, &opts, current_language);
1848   fprintf_filtered (stream, "\n");
1849 }
1850
1851 static void
1852 printf_command (char *arg, int from_tty)
1853 {
1854   char *f = NULL;
1855   char *s = arg;
1856   char *string = NULL;
1857   struct value **val_args;
1858   char *substrings;
1859   char *current_substring;
1860   int nargs = 0;
1861   int allocated_args = 20;
1862   struct cleanup *old_cleanups;
1863
1864   val_args = xmalloc (allocated_args * sizeof (struct value *));
1865   old_cleanups = make_cleanup (free_current_contents, &val_args);
1866
1867   if (s == 0)
1868     error_no_arg (_("format-control string and values to print"));
1869
1870   /* Skip white space before format string */
1871   while (*s == ' ' || *s == '\t')
1872     s++;
1873
1874   /* A format string should follow, enveloped in double quotes.  */
1875   if (*s++ != '"')
1876     error (_("Bad format string, missing '\"'."));
1877
1878   /* Parse the format-control string and copy it into the string STRING,
1879      processing some kinds of escape sequence.  */
1880
1881   f = string = (char *) alloca (strlen (s) + 1);
1882
1883   while (*s != '"')
1884     {
1885       int c = *s++;
1886       switch (c)
1887         {
1888         case '\0':
1889           error (_("Bad format string, non-terminated '\"'."));
1890
1891         case '\\':
1892           switch (c = *s++)
1893             {
1894             case '\\':
1895               *f++ = '\\';
1896               break;
1897             case 'a':
1898               *f++ = '\a';
1899               break;
1900             case 'b':
1901               *f++ = '\b';
1902               break;
1903             case 'f':
1904               *f++ = '\f';
1905               break;
1906             case 'n':
1907               *f++ = '\n';
1908               break;
1909             case 'r':
1910               *f++ = '\r';
1911               break;
1912             case 't':
1913               *f++ = '\t';
1914               break;
1915             case 'v':
1916               *f++ = '\v';
1917               break;
1918             case '"':
1919               *f++ = '"';
1920               break;
1921             default:
1922               /* ??? TODO: handle other escape sequences */
1923               error (_("Unrecognized escape character \\%c in format string."),
1924                      c);
1925             }
1926           break;
1927
1928         default:
1929           *f++ = c;
1930         }
1931     }
1932
1933   /* Skip over " and following space and comma.  */
1934   s++;
1935   *f++ = '\0';
1936   while (*s == ' ' || *s == '\t')
1937     s++;
1938
1939   if (*s != ',' && *s != 0)
1940     error (_("Invalid argument syntax"));
1941
1942   if (*s == ',')
1943     s++;
1944   while (*s == ' ' || *s == '\t')
1945     s++;
1946
1947   /* Need extra space for the '\0's.  Doubling the size is sufficient.  */
1948   substrings = alloca (strlen (string) * 2);
1949   current_substring = substrings;
1950
1951   {
1952     /* Now scan the string for %-specs and see what kinds of args they want.
1953        argclass[I] classifies the %-specs so we can give printf_filtered
1954        something of the right size.  */
1955
1956     enum argclass
1957       {
1958         int_arg, long_arg, long_long_arg, ptr_arg, string_arg,
1959         double_arg, long_double_arg, decfloat_arg
1960       };
1961     enum argclass *argclass;
1962     enum argclass this_argclass;
1963     char *last_arg;
1964     int nargs_wanted;
1965     int i;
1966
1967     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1968     nargs_wanted = 0;
1969     f = string;
1970     last_arg = string;
1971     while (*f)
1972       if (*f++ == '%')
1973         {
1974           int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
1975           int seen_space = 0, seen_plus = 0;
1976           int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
1977           int seen_big_d = 0, seen_double_big_d = 0;
1978           int bad = 0;
1979
1980           /* Check the validity of the format specifier, and work
1981              out what argument it expects.  We only accept C89
1982              format strings, with the exception of long long (which
1983              we autoconf for).  */
1984
1985           /* Skip over "%%".  */
1986           if (*f == '%')
1987             {
1988               f++;
1989               continue;
1990             }
1991
1992           /* The first part of a format specifier is a set of flag
1993              characters.  */
1994           while (strchr ("0-+ #", *f))
1995             {
1996               if (*f == '#')
1997                 seen_hash = 1;
1998               else if (*f == '0')
1999                 seen_zero = 1;
2000               else if (*f == ' ')
2001                 seen_space = 1;
2002               else if (*f == '+')
2003                 seen_plus = 1;
2004               f++;
2005             }
2006
2007           /* The next part of a format specifier is a width.  */
2008           while (strchr ("0123456789", *f))
2009             f++;
2010
2011           /* The next part of a format specifier is a precision.  */
2012           if (*f == '.')
2013             {
2014               seen_prec = 1;
2015               f++;
2016               while (strchr ("0123456789", *f))
2017                 f++;
2018             }
2019
2020           /* The next part of a format specifier is a length modifier.  */
2021           if (*f == 'h')
2022             {
2023               seen_h = 1;
2024               f++;
2025             }
2026           else if (*f == 'l')
2027             {
2028               f++;
2029               lcount++;
2030               if (*f == 'l')
2031                 {
2032                   f++;
2033                   lcount++;
2034                 }
2035             }
2036           else if (*f == 'L')
2037             {
2038               seen_big_l = 1;
2039               f++;
2040             }
2041           /* Decimal32 modifier.  */
2042           else if (*f == 'H')
2043             {
2044               seen_big_h = 1;
2045               f++;
2046             }
2047           /* Decimal64 and Decimal128 modifiers.  */
2048           else if (*f == 'D')
2049             {
2050               f++;
2051
2052               /* Check for a Decimal128.  */
2053               if (*f == 'D')
2054                 {
2055                   f++;
2056                   seen_double_big_d = 1;
2057                 }
2058               else
2059                 seen_big_d = 1;
2060             }
2061
2062           switch (*f)
2063             {
2064             case 'u':
2065               if (seen_hash)
2066                 bad = 1;
2067               /* FALLTHROUGH */
2068
2069             case 'o':
2070             case 'x':
2071             case 'X':
2072               if (seen_space || seen_plus)
2073                 bad = 1;
2074               /* FALLTHROUGH */
2075
2076             case 'd':
2077             case 'i':
2078               if (lcount == 0)
2079                 this_argclass = int_arg;
2080               else if (lcount == 1)
2081                 this_argclass = long_arg;
2082               else
2083                 this_argclass = long_long_arg;
2084
2085               if (seen_big_l)
2086                 bad = 1;
2087               break;
2088
2089             case 'c':
2090               this_argclass = int_arg;
2091               if (lcount || seen_h || seen_big_l)
2092                 bad = 1;
2093               if (seen_prec || seen_zero || seen_space || seen_plus)
2094                 bad = 1;
2095               break;
2096
2097             case 'p':
2098               this_argclass = ptr_arg;
2099               if (lcount || seen_h || seen_big_l)
2100                 bad = 1;
2101               if (seen_prec || seen_zero || seen_space || seen_plus)
2102                 bad = 1;
2103               break;
2104
2105             case 's':
2106               this_argclass = string_arg;
2107               if (lcount || seen_h || seen_big_l)
2108                 bad = 1;
2109               if (seen_zero || seen_space || seen_plus)
2110                 bad = 1;
2111               break;
2112
2113             case 'e':
2114             case 'f':
2115             case 'g':
2116             case 'E':
2117             case 'G':
2118               if (seen_big_h || seen_big_d || seen_double_big_d)
2119                 this_argclass = decfloat_arg;
2120               else if (seen_big_l)
2121                 this_argclass = long_double_arg;
2122               else
2123                 this_argclass = double_arg;
2124
2125               if (lcount || seen_h)
2126                 bad = 1;
2127               break;
2128
2129             case '*':
2130               error (_("`*' not supported for precision or width in printf"));
2131
2132             case 'n':
2133               error (_("Format specifier `n' not supported in printf"));
2134
2135             case '\0':
2136               error (_("Incomplete format specifier at end of format string"));
2137
2138             default:
2139               error (_("Unrecognized format specifier '%c' in printf"), *f);
2140             }
2141
2142           if (bad)
2143             error (_("Inappropriate modifiers to format specifier '%c' in printf"),
2144                    *f);
2145
2146           f++;
2147
2148           if (lcount > 1 && USE_PRINTF_I64)
2149             {
2150               /* Windows' printf does support long long, but not the usual way.
2151                  Convert %lld to %I64d.  */
2152               int length_before_ll = f - last_arg - 1 - lcount;
2153               strncpy (current_substring, last_arg, length_before_ll);
2154               strcpy (current_substring + length_before_ll, "I64");
2155               current_substring[length_before_ll + 3] =
2156                 last_arg[length_before_ll + lcount];
2157               current_substring += length_before_ll + 4;
2158             }
2159           else
2160             {
2161               strncpy (current_substring, last_arg, f - last_arg);
2162               current_substring += f - last_arg;
2163             }
2164           *current_substring++ = '\0';
2165           last_arg = f;
2166           argclass[nargs_wanted++] = this_argclass;
2167         }
2168
2169     /* Now, parse all arguments and evaluate them.
2170        Store the VALUEs in VAL_ARGS.  */
2171
2172     while (*s != '\0')
2173       {
2174         char *s1;
2175         if (nargs == allocated_args)
2176           val_args = (struct value **) xrealloc ((char *) val_args,
2177                                                  (allocated_args *= 2)
2178                                                  * sizeof (struct value *));
2179         s1 = s;
2180         val_args[nargs] = parse_to_comma_and_eval (&s1);
2181
2182         nargs++;
2183         s = s1;
2184         if (*s == ',')
2185           s++;
2186       }
2187
2188     if (nargs != nargs_wanted)
2189       error (_("Wrong number of arguments for specified format-string"));
2190
2191     /* Now actually print them.  */
2192     current_substring = substrings;
2193     for (i = 0; i < nargs; i++)
2194       {
2195         switch (argclass[i])
2196           {
2197           case string_arg:
2198             {
2199               gdb_byte *str;
2200               CORE_ADDR tem;
2201               int j;
2202               tem = value_as_address (val_args[i]);
2203
2204               /* This is a %s argument.  Find the length of the string.  */
2205               for (j = 0;; j++)
2206                 {
2207                   gdb_byte c;
2208                   QUIT;
2209                   read_memory (tem + j, &c, 1);
2210                   if (c == 0)
2211                     break;
2212                 }
2213
2214               /* Copy the string contents into a string inside GDB.  */
2215               str = (gdb_byte *) alloca (j + 1);
2216               if (j != 0)
2217                 read_memory (tem, str, j);
2218               str[j] = 0;
2219
2220               printf_filtered (current_substring, (char *) str);
2221             }
2222             break;
2223           case double_arg:
2224             {
2225               struct type *type = value_type (val_args[i]);
2226               DOUBLEST val;
2227               int inv;
2228
2229               /* If format string wants a float, unchecked-convert the value
2230                  to floating point of the same size.  */
2231               type = float_type_from_length (current_gdbarch, type);
2232               val = unpack_double (type, value_contents (val_args[i]), &inv);
2233               if (inv)
2234                 error (_("Invalid floating value found in program."));
2235
2236               printf_filtered (current_substring, (double) val);
2237               break;
2238             }
2239           case long_double_arg:
2240 #ifdef HAVE_LONG_DOUBLE
2241             {
2242               struct type *type = value_type (val_args[i]);
2243               DOUBLEST val;
2244               int inv;
2245
2246               /* If format string wants a float, unchecked-convert the value
2247                  to floating point of the same size.  */
2248               type = float_type_from_length (current_gdbarch, type);
2249               val = unpack_double (type, value_contents (val_args[i]), &inv);
2250               if (inv)
2251                 error (_("Invalid floating value found in program."));
2252
2253               printf_filtered (current_substring, (long double) val);
2254               break;
2255             }
2256 #else
2257             error (_("long double not supported in printf"));
2258 #endif
2259           case long_long_arg:
2260 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2261             {
2262               long long val = value_as_long (val_args[i]);
2263               printf_filtered (current_substring, val);
2264               break;
2265             }
2266 #else
2267             error (_("long long not supported in printf"));
2268 #endif
2269           case int_arg:
2270             {
2271               int val = value_as_long (val_args[i]);
2272               printf_filtered (current_substring, val);
2273               break;
2274             }
2275           case long_arg:
2276             {
2277               long val = value_as_long (val_args[i]);
2278               printf_filtered (current_substring, val);
2279               break;
2280             }
2281
2282           /* Handles decimal floating values.  */
2283         case decfloat_arg:
2284             {
2285               const gdb_byte *param_ptr = value_contents (val_args[i]);
2286 #if defined (PRINTF_HAS_DECFLOAT)
2287               /* If we have native support for Decimal floating
2288                  printing, handle it here.  */
2289               printf_filtered (current_substring, param_ptr);
2290 #else
2291
2292               /* As a workaround until vasprintf has native support for DFP
2293                we convert the DFP values to string and print them using
2294                the %s format specifier.  */
2295
2296               char *eos, *sos;
2297               int nnull_chars = 0;
2298
2299               /* Parameter data.  */
2300               struct type *param_type = value_type (val_args[i]);
2301               unsigned int param_len = TYPE_LENGTH (param_type);
2302
2303               /* DFP output data.  */
2304               struct value *dfp_value = NULL;
2305               gdb_byte *dfp_ptr;
2306               int dfp_len = 16;
2307               gdb_byte dec[16];
2308               struct type *dfp_type = NULL;
2309               char decstr[MAX_DECIMAL_STRING];
2310
2311               /* Points to the end of the string so that we can go back
2312                  and check for DFP length modifiers.  */
2313               eos = current_substring + strlen (current_substring);
2314
2315               /* Look for the float/double format specifier.  */
2316               while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2317                      && *eos != 'g' && *eos != 'G')
2318                   eos--;
2319
2320               sos = eos;
2321
2322               /* Search for the '%' char and extract the size and type of
2323                  the output decimal value based on its modifiers
2324                  (%Hf, %Df, %DDf).  */
2325               while (*--sos != '%')
2326                 {
2327                   if (*sos == 'H')
2328                     {
2329                       dfp_len = 4;
2330                       dfp_type = builtin_type (current_gdbarch)->builtin_decfloat;
2331                     }
2332                   else if (*sos == 'D' && *(sos - 1) == 'D')
2333                     {
2334                       dfp_len = 16;
2335                       dfp_type = builtin_type (current_gdbarch)->builtin_declong;
2336                       sos--;
2337                     }
2338                   else
2339                     {
2340                       dfp_len = 8;
2341                       dfp_type = builtin_type (current_gdbarch)->builtin_decdouble;
2342                     }
2343                 }
2344
2345               /* Replace %Hf, %Df and %DDf with %s's.  */
2346               *++sos = 's';
2347
2348               /* Go through the whole format string and pull the correct
2349                  number of chars back to compensate for the change in the
2350                  format specifier.  */
2351               while (nnull_chars < nargs - i)
2352                 {
2353                   if (*eos == '\0')
2354                     nnull_chars++;
2355
2356                   *++sos = *++eos;
2357                 }
2358
2359               /* Conversion between different DFP types.  */
2360               if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2361                 decimal_convert (param_ptr, param_len, dec, dfp_len);
2362               else
2363                 /* If this is a non-trivial conversion, just output 0.
2364                    A correct converted value can be displayed by explicitly
2365                    casting to a DFP type.  */
2366                 decimal_from_string (dec, dfp_len, "0");
2367
2368               dfp_value = value_from_decfloat (dfp_type, dec);
2369
2370               dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2371
2372               decimal_to_string (dfp_ptr, dfp_len, decstr);
2373
2374               /* Print the DFP value.  */
2375               printf_filtered (current_substring, decstr);
2376
2377               break;
2378 #endif
2379             }
2380
2381           case ptr_arg:
2382             {
2383               /* We avoid the host's %p because pointers are too
2384                  likely to be the wrong size.  The only interesting
2385                  modifier for %p is a width; extract that, and then
2386                  handle %p as glibc would: %#x or a literal "(nil)".  */
2387
2388               char *p, *fmt, *fmt_p;
2389 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2390               long long val = value_as_long (val_args[i]);
2391 #else
2392               long val = value_as_long (val_args[i]);
2393 #endif
2394
2395               fmt = alloca (strlen (current_substring) + 5);
2396
2397               /* Copy up to the leading %.  */
2398               p = current_substring;
2399               fmt_p = fmt;
2400               while (*p)
2401                 {
2402                   int is_percent = (*p == '%');
2403                   *fmt_p++ = *p++;
2404                   if (is_percent)
2405                     {
2406                       if (*p == '%')
2407                         *fmt_p++ = *p++;
2408                       else
2409                         break;
2410                     }
2411                 }
2412
2413               if (val != 0)
2414                 *fmt_p++ = '#';
2415
2416               /* Copy any width.  */
2417               while (*p >= '0' && *p < '9')
2418                 *fmt_p++ = *p++;
2419
2420               gdb_assert (*p == 'p' && *(p + 1) == '\0');
2421               if (val != 0)
2422                 {
2423 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2424                   *fmt_p++ = 'l';
2425 #endif
2426                   *fmt_p++ = 'l';
2427                   *fmt_p++ = 'x';
2428                   *fmt_p++ = '\0';
2429                   printf_filtered (fmt, val);
2430                 }
2431               else
2432                 {
2433                   *fmt_p++ = 's';
2434                   *fmt_p++ = '\0';
2435                   printf_filtered (fmt, "(nil)");
2436                 }
2437
2438               break;
2439             }
2440           default:
2441             internal_error (__FILE__, __LINE__,
2442                             _("failed internal consistency check"));
2443           }
2444         /* Skip to the next substring.  */
2445         current_substring += strlen (current_substring) + 1;
2446       }
2447     /* Print the portion of the format string after the last argument.  */
2448     puts_filtered (last_arg);
2449   }
2450   do_cleanups (old_cleanups);
2451 }
2452
2453 void
2454 _initialize_printcmd (void)
2455 {
2456   struct cmd_list_element *c;
2457
2458   current_display_number = -1;
2459
2460   observer_attach_solib_unloaded (clear_dangling_display_expressions);
2461
2462   add_info ("address", address_info,
2463             _("Describe where symbol SYM is stored."));
2464
2465   add_info ("symbol", sym_info, _("\
2466 Describe what symbol is at location ADDR.\n\
2467 Only for symbols with fixed locations (global or static scope)."));
2468
2469   add_com ("x", class_vars, x_command, _("\
2470 Examine memory: x/FMT ADDRESS.\n\
2471 ADDRESS is an expression for the memory address to examine.\n\
2472 FMT is a repeat count followed by a format letter and a size letter.\n\
2473 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2474   t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2475 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2476 The specified number of objects of the specified size are printed\n\
2477 according to the format.\n\n\
2478 Defaults for format and size letters are those previously used.\n\
2479 Default count is 1.  Default address is following last thing printed\n\
2480 with this command or \"print\"."));
2481
2482 #if 0
2483   add_com ("whereis", class_vars, whereis_command,
2484            _("Print line number and file of definition of variable."));
2485 #endif
2486
2487   add_info ("display", display_info, _("\
2488 Expressions to display when program stops, with code numbers."));
2489
2490   add_cmd ("undisplay", class_vars, undisplay_command, _("\
2491 Cancel some expressions to be displayed when program stops.\n\
2492 Arguments are the code numbers of the expressions to stop displaying.\n\
2493 No argument means cancel all automatic-display expressions.\n\
2494 \"delete display\" has the same effect as this command.\n\
2495 Do \"info display\" to see current list of code numbers."),
2496            &cmdlist);
2497
2498   add_com ("display", class_vars, display_command, _("\
2499 Print value of expression EXP each time the program stops.\n\
2500 /FMT may be used before EXP as in the \"print\" command.\n\
2501 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2502 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2503 and examining is done as in the \"x\" command.\n\n\
2504 With no argument, display all currently requested auto-display expressions.\n\
2505 Use \"undisplay\" to cancel display requests previously made."));
2506
2507   add_cmd ("display", class_vars, enable_display, _("\
2508 Enable some expressions to be displayed when program stops.\n\
2509 Arguments are the code numbers of the expressions to resume displaying.\n\
2510 No argument means enable all automatic-display expressions.\n\
2511 Do \"info display\" to see current list of code numbers."), &enablelist);
2512
2513   add_cmd ("display", class_vars, disable_display_command, _("\
2514 Disable some expressions to be displayed when program stops.\n\
2515 Arguments are the code numbers of the expressions to stop displaying.\n\
2516 No argument means disable all automatic-display expressions.\n\
2517 Do \"info display\" to see current list of code numbers."), &disablelist);
2518
2519   add_cmd ("display", class_vars, undisplay_command, _("\
2520 Cancel some expressions to be displayed when program stops.\n\
2521 Arguments are the code numbers of the expressions to stop displaying.\n\
2522 No argument means cancel all automatic-display expressions.\n\
2523 Do \"info display\" to see current list of code numbers."), &deletelist);
2524
2525   add_com ("printf", class_vars, printf_command, _("\
2526 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2527 This is useful for formatted output in user-defined commands."));
2528
2529   add_com ("output", class_vars, output_command, _("\
2530 Like \"print\" but don't put in value history and don't print newline.\n\
2531 This is useful in user-defined commands."));
2532
2533   add_prefix_cmd ("set", class_vars, set_command, _("\
2534 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2535 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2536 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2537 with $), a register (a few standard names starting with $), or an actual\n\
2538 variable in the program being debugged.  EXP is any valid expression.\n\
2539 Use \"set variable\" for variables with names identical to set subcommands.\n\
2540 \n\
2541 With a subcommand, this command modifies parts of the gdb environment.\n\
2542 You can see these environment settings with the \"show\" command."),
2543                   &setlist, "set ", 1, &cmdlist);
2544   if (dbx_commands)
2545     add_com ("assign", class_vars, set_command, _("\
2546 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2547 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2548 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2549 with $), a register (a few standard names starting with $), or an actual\n\
2550 variable in the program being debugged.  EXP is any valid expression.\n\
2551 Use \"set variable\" for variables with names identical to set subcommands.\n\
2552 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2553 You can see these environment settings with the \"show\" command."));
2554
2555   /* "call" is the same as "set", but handy for dbx users to call fns. */
2556   c = add_com ("call", class_vars, call_command, _("\
2557 Call a function in the program.\n\
2558 The argument is the function name and arguments, in the notation of the\n\
2559 current working language.  The result is printed and saved in the value\n\
2560 history, if it is not void."));
2561   set_cmd_completer (c, expression_completer);
2562
2563   add_cmd ("variable", class_vars, set_command, _("\
2564 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2565 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2566 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2567 with $), a register (a few standard names starting with $), or an actual\n\
2568 variable in the program being debugged.  EXP is any valid expression.\n\
2569 This may usually be abbreviated to simply \"set\"."),
2570            &setlist);
2571
2572   c = add_com ("print", class_vars, print_command, _("\
2573 Print value of expression EXP.\n\
2574 Variables accessible are those of the lexical environment of the selected\n\
2575 stack frame, plus all those whose scope is global or an entire file.\n\
2576 \n\
2577 $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
2578 $$NUM refers to NUM'th value back from the last one.\n\
2579 Names starting with $ refer to registers (with the values they would have\n\
2580 if the program were to return to the stack frame now selected, restoring\n\
2581 all registers saved by frames farther in) or else to debugger\n\
2582 \"convenience\" variables (any such name not a known register).\n\
2583 Use assignment expressions to give values to convenience variables.\n\
2584 \n\
2585 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2586 @ is a binary operator for treating consecutive data objects\n\
2587 anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
2588 element is FOO, whose second element is stored in the space following\n\
2589 where FOO is stored, etc.  FOO must be an expression whose value\n\
2590 resides in memory.\n\
2591 \n\
2592 EXP may be preceded with /FMT, where FMT is a format letter\n\
2593 but no count or size letter (see \"x\" command)."));
2594   set_cmd_completer (c, expression_completer);
2595   add_com_alias ("p", "print", class_vars, 1);
2596
2597   c = add_com ("inspect", class_vars, inspect_command, _("\
2598 Same as \"print\" command, except that if you are running in the epoch\n\
2599 environment, the value is printed in its own window."));
2600   set_cmd_completer (c, expression_completer);
2601
2602   add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2603                             &max_symbolic_offset, _("\
2604 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2605 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2606                             NULL,
2607                             show_max_symbolic_offset,
2608                             &setprintlist, &showprintlist);
2609   add_setshow_boolean_cmd ("symbol-filename", no_class,
2610                            &print_symbol_filename, _("\
2611 Set printing of source filename and line number with <symbol>."), _("\
2612 Show printing of source filename and line number with <symbol>."), NULL,
2613                            NULL,
2614                            show_print_symbol_filename,
2615                            &setprintlist, &showprintlist);
2616
2617   /* For examine/instruction a single byte quantity is specified as
2618      the data.  This avoids problems with value_at_lazy() requiring a
2619      valid data type (and rejecting VOID). */
2620   examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2621
2622   examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2623   examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2624   examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2625   examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2626
2627 }