* ld-elf/warn1.d: Do not run on sparc64-*-solaris2*.
[external/binutils.git] / gdb / printcmd.c
1 /* Print values for GNU debugger GDB.
2
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5    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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "frame.h"
27 #include "symtab.h"
28 #include "gdbtypes.h"
29 #include "value.h"
30 #include "language.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "target.h"
35 #include "breakpoint.h"
36 #include "demangle.h"
37 #include "valprint.h"
38 #include "annotate.h"
39 #include "symfile.h"            /* for overlay functions */
40 #include "objfiles.h"           /* ditto */
41 #include "completer.h"          /* for completion functions */
42 #include "ui-out.h"
43 #include "gdb_assert.h"
44 #include "block.h"
45 #include "disasm.h"
46
47 #ifdef TUI
48 #include "tui/tui.h"            /* For tui_active et.al.   */
49 #endif
50
51 extern int asm_demangle;        /* Whether to demangle syms in asm printouts */
52 extern int addressprint;        /* Whether to print hex addresses in HLL " */
53
54 struct format_data
55   {
56     int count;
57     char format;
58     char size;
59   };
60
61 /* Last specified output format.  */
62
63 static char last_format = 'x';
64
65 /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
66
67 static char last_size = 'w';
68
69 /* Default address to examine next.  */
70
71 static CORE_ADDR next_address;
72
73 /* Last address examined.  */
74
75 static CORE_ADDR last_examine_address;
76
77 /* Contents of last address examined.
78    This is not valid past the end of the `x' command!  */
79
80 static struct value *last_examine_value;
81
82 /* Largest offset between a symbolic value and an address, that will be
83    printed as `0x1234 <symbol+offset>'.  */
84
85 static unsigned int max_symbolic_offset = UINT_MAX;
86
87 /* Append the source filename and linenumber of the symbol when
88    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
89 static int print_symbol_filename = 0;
90
91 /* Number of auto-display expression currently being displayed.
92    So that we can disable it if we get an error or a signal within it.
93    -1 when not doing one.  */
94
95 int current_display_number;
96
97 /* Flag to low-level print routines that this value is being printed
98    in an epoch window.  We'd like to pass this as a parameter, but
99    every routine would need to take it.  Perhaps we can encapsulate
100    this in the I/O stream once we have GNU stdio. */
101
102 int inspect_it = 0;
103
104 struct display
105   {
106     /* Chain link to next auto-display item.  */
107     struct display *next;
108     /* Expression to be evaluated and displayed.  */
109     struct expression *exp;
110     /* Item number of this auto-display item.  */
111     int number;
112     /* Display format specified.  */
113     struct format_data format;
114     /* Innermost block required by this expression when evaluated */
115     struct block *block;
116     /* Status of this display (enabled or disabled) */
117     int enabled_p;
118   };
119
120 /* Chain of expressions whose values should be displayed
121    automatically each time the program stops.  */
122
123 static struct display *display_chain;
124
125 static int display_number;
126
127 /* Prototypes for exported functions. */
128
129 void output_command (char *, int);
130
131 void _initialize_printcmd (void);
132
133 /* Prototypes for local functions. */
134
135 static void delete_display (int);
136
137 static void enable_display (char *, int);
138
139 static void disable_display_command (char *, int);
140
141 static void printf_command (char *, int);
142
143 static void display_info (char *, int);
144
145 static void do_one_display (struct display *);
146
147 static void undisplay_command (char *, int);
148
149 static void free_display (struct display *);
150
151 static void display_command (char *, int);
152
153 void x_command (char *, int);
154
155 static void address_info (char *, int);
156
157 static void set_command (char *, int);
158
159 static void call_command (char *, int);
160
161 static void inspect_command (char *, int);
162
163 static void print_command (char *, int);
164
165 static void print_command_1 (char *, int, int);
166
167 static void validate_format (struct format_data, char *);
168
169 static void print_formatted (struct value *, int, int, struct ui_file *);
170
171 static struct format_data decode_format (char **, int, int);
172
173 static void sym_info (char *, int);
174 \f
175
176 /* Decode a format specification.  *STRING_PTR should point to it.
177    OFORMAT and OSIZE are used as defaults for the format and size
178    if none are given in the format specification.
179    If OSIZE is zero, then the size field of the returned value
180    should be set only if a size is explicitly specified by the
181    user.
182    The structure returned describes all the data
183    found in the specification.  In addition, *STRING_PTR is advanced
184    past the specification and past all whitespace following it.  */
185
186 static struct format_data
187 decode_format (char **string_ptr, int oformat, int osize)
188 {
189   struct format_data val;
190   char *p = *string_ptr;
191
192   val.format = '?';
193   val.size = '?';
194   val.count = 1;
195
196   if (*p >= '0' && *p <= '9')
197     val.count = atoi (p);
198   while (*p >= '0' && *p <= '9')
199     p++;
200
201   /* Now process size or format letters that follow.  */
202
203   while (1)
204     {
205       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
206         val.size = *p++;
207       else if (*p >= 'a' && *p <= 'z')
208         val.format = *p++;
209       else
210         break;
211     }
212
213   while (*p == ' ' || *p == '\t')
214     p++;
215   *string_ptr = p;
216
217   /* Set defaults for format and size if not specified.  */
218   if (val.format == '?')
219     {
220       if (val.size == '?')
221         {
222           /* Neither has been specified.  */
223           val.format = oformat;
224           val.size = osize;
225         }
226       else
227         /* If a size is specified, any format makes a reasonable
228            default except 'i'.  */
229         val.format = oformat == 'i' ? 'x' : oformat;
230     }
231   else if (val.size == '?')
232     switch (val.format)
233       {
234       case 'a':
235       case 's':
236         /* Pick the appropriate size for an address.  */
237         if (TARGET_PTR_BIT == 64)
238           val.size = osize ? 'g' : osize;
239         else if (TARGET_PTR_BIT == 32)
240           val.size = osize ? 'w' : osize;
241         else if (TARGET_PTR_BIT == 16)
242           val.size = osize ? 'h' : osize;
243         else
244           /* Bad value for TARGET_PTR_BIT */
245           internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
246         break;
247       case 'f':
248         /* Floating point has to be word or giantword.  */
249         if (osize == 'w' || osize == 'g')
250           val.size = osize;
251         else
252           /* Default it to giantword if the last used size is not
253              appropriate.  */
254           val.size = osize ? 'g' : osize;
255         break;
256       case 'c':
257         /* Characters default to one byte.  */
258         val.size = osize ? 'b' : osize;
259         break;
260       default:
261         /* The default is the size most recently specified.  */
262         val.size = osize;
263       }
264
265   return val;
266 }
267 \f
268 /* Print value VAL on stream according to FORMAT, a letter or 0.
269    Do not end with a newline.
270    0 means print VAL according to its own type.
271    SIZE is the letter for the size of datum being printed.
272    This is used to pad hex numbers so they line up.  */
273
274 static void
275 print_formatted (struct value *val, int format, int size,
276                  struct ui_file *stream)
277 {
278   struct type *type = check_typedef (value_type (val));
279   int len = TYPE_LENGTH (type);
280
281   if (VALUE_LVAL (val) == lval_memory)
282     {
283       next_address = VALUE_ADDRESS (val) + len;
284     }
285
286   switch (format)
287     {
288     case 's':
289       /* FIXME: Need to handle wchar_t's here... */
290       next_address = VALUE_ADDRESS (val)
291         + val_print_string (VALUE_ADDRESS (val), -1, 1, stream);
292       break;
293
294     case 'i':
295       /* The old comment says
296          "Force output out, print_insn not using _filtered".
297          I'm not completely sure what that means, I suspect most print_insn
298          now do use _filtered, so I guess it's obsolete.
299          --Yes, it does filter now, and so this is obsolete.  -JB  */
300
301       /* We often wrap here if there are long symbolic names.  */
302       wrap_here ("    ");
303       next_address = VALUE_ADDRESS (val)
304         + gdb_print_insn (VALUE_ADDRESS (val), stream);
305       break;
306
307     default:
308       if (format == 0
309           || TYPE_CODE (type) == TYPE_CODE_ARRAY
310           || TYPE_CODE (type) == TYPE_CODE_STRING
311           || TYPE_CODE (type) == TYPE_CODE_STRUCT
312           || TYPE_CODE (type) == TYPE_CODE_UNION
313           || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
314         /* If format is 0, use the 'natural' format for
315          * that type of value.  If the type is non-scalar,
316          * we have to use language rules to print it as
317          * a series of scalars.
318          */
319         value_print (val, stream, format, Val_pretty_default);
320       else
321         /* User specified format, so don't look to the
322          * the type to tell us what to do.
323          */
324         print_scalar_formatted (value_contents (val), type,
325                                 format, size, stream);
326     }
327 }
328
329 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
330    according to letters FORMAT and SIZE on STREAM.
331    FORMAT may not be zero.  Formats s and i are not supported at this level.
332
333    This is how the elements of an array or structure are printed
334    with a format.  */
335
336 void
337 print_scalar_formatted (const void *valaddr, struct type *type,
338                         int format, int size, struct ui_file *stream)
339 {
340   LONGEST val_long = 0;
341   unsigned int len = TYPE_LENGTH (type);
342
343   if (len > sizeof(LONGEST) &&
344       (TYPE_CODE (type) == TYPE_CODE_INT
345        || TYPE_CODE (type) == TYPE_CODE_ENUM))
346     {
347       switch (format)
348         {
349         case 'o':
350           print_octal_chars (stream, valaddr, len);
351           return;
352         case 'u':
353         case 'd':
354           print_decimal_chars (stream, valaddr, len);
355           return;
356         case 't':
357           print_binary_chars (stream, valaddr, len);
358           return;
359         case 'x':
360           print_hex_chars (stream, valaddr, len);
361           return;
362         case 'c':
363           print_char_chars (stream, valaddr, len);
364           return;
365         default:
366           break;
367         };
368     }
369
370   if (format != 'f')
371     val_long = unpack_long (type, valaddr);
372
373   /* If the value is a pointer, and pointers and addresses are not the
374      same, then at this point, the value's length (in target bytes) is
375      TARGET_ADDR_BIT/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
376   if (TYPE_CODE (type) == TYPE_CODE_PTR)
377     len = TARGET_ADDR_BIT / TARGET_CHAR_BIT;
378
379   /* If we are printing it as unsigned, truncate it in case it is actually
380      a negative signed value (e.g. "print/u (short)-1" should print 65535
381      (if shorts are 16 bits) instead of 4294967295).  */
382   if (format != 'd')
383     {
384       if (len < sizeof (LONGEST))
385         val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
386     }
387
388   switch (format)
389     {
390     case 'x':
391       if (!size)
392         {
393           /* no size specified, like in print.  Print varying # of digits. */
394           print_longest (stream, 'x', 1, val_long);
395         }
396       else
397         switch (size)
398           {
399           case 'b':
400           case 'h':
401           case 'w':
402           case 'g':
403             print_longest (stream, size, 1, val_long);
404             break;
405           default:
406             error (_("Undefined output size \"%c\"."), size);
407           }
408       break;
409
410     case 'd':
411       print_longest (stream, 'd', 1, val_long);
412       break;
413
414     case 'u':
415       print_longest (stream, 'u', 0, val_long);
416       break;
417
418     case 'o':
419       if (val_long)
420         print_longest (stream, 'o', 1, val_long);
421       else
422         fprintf_filtered (stream, "0");
423       break;
424
425     case 'a':
426       {
427         CORE_ADDR addr = unpack_pointer (type, valaddr);
428         print_address (addr, stream);
429       }
430       break;
431
432     case 'c':
433       value_print (value_from_longest (builtin_type_true_char, val_long),
434                    stream, 0, Val_pretty_default);
435       break;
436
437     case 'f':
438       if (len == TYPE_LENGTH (builtin_type_float))
439         type = builtin_type_float;
440       else if (len == TYPE_LENGTH (builtin_type_double))
441         type = builtin_type_double;
442       else if (len == TYPE_LENGTH (builtin_type_long_double))
443         type = builtin_type_long_double;
444       print_floating (valaddr, type, stream);
445       break;
446
447     case 0:
448       internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
449
450     case 't':
451       /* Binary; 't' stands for "two".  */
452       {
453         char bits[8 * (sizeof val_long) + 1];
454         char buf[8 * (sizeof val_long) + 32];
455         char *cp = bits;
456         int width;
457
458         if (!size)
459           width = 8 * (sizeof val_long);
460         else
461           switch (size)
462             {
463             case 'b':
464               width = 8;
465               break;
466             case 'h':
467               width = 16;
468               break;
469             case 'w':
470               width = 32;
471               break;
472             case 'g':
473               width = 64;
474               break;
475             default:
476               error (_("Undefined output size \"%c\"."), size);
477             }
478
479         bits[width] = '\0';
480         while (width-- > 0)
481           {
482             bits[width] = (val_long & 1) ? '1' : '0';
483             val_long >>= 1;
484           }
485         if (!size)
486           {
487             while (*cp && *cp == '0')
488               cp++;
489             if (*cp == '\0')
490               cp--;
491           }
492         strcpy (buf, cp);
493         fputs_filtered (buf, stream);
494       }
495       break;
496
497     default:
498       error (_("Undefined output format \"%c\"."), format);
499     }
500 }
501
502 /* Specify default address for `x' command.
503    `info lines' uses this.  */
504
505 void
506 set_next_address (CORE_ADDR addr)
507 {
508   next_address = addr;
509
510   /* Make address available to the user as $_.  */
511   set_internalvar (lookup_internalvar ("_"),
512                    value_from_pointer (lookup_pointer_type (builtin_type_void),
513                                        addr));
514 }
515
516 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
517    after LEADIN.  Print nothing if no symbolic name is found nearby.
518    Optionally also print source file and line number, if available.
519    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
520    or to interpret it as a possible C++ name and convert it back to source
521    form.  However note that DO_DEMANGLE can be overridden by the specific
522    settings of the demangle and asm_demangle variables.  */
523
524 void
525 print_address_symbolic (CORE_ADDR addr, struct ui_file *stream, int do_demangle,
526                         char *leadin)
527 {
528   char *name = NULL;
529   char *filename = NULL;
530   int unmapped = 0;
531   int offset = 0;
532   int line = 0;
533
534   /* throw away both name and filename */
535   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
536   make_cleanup (free_current_contents, &filename);
537
538   if (build_address_symbolic (addr, do_demangle, &name, &offset, &filename, &line, &unmapped))
539     {
540       do_cleanups (cleanup_chain);
541       return;
542     }
543
544   fputs_filtered (leadin, stream);
545   if (unmapped)
546     fputs_filtered ("<*", stream);
547   else
548     fputs_filtered ("<", stream);
549   fputs_filtered (name, stream);
550   if (offset != 0)
551     fprintf_filtered (stream, "+%u", (unsigned int) offset);
552
553   /* Append source filename and line number if desired.  Give specific
554      line # of this addr, if we have it; else line # of the nearest symbol.  */
555   if (print_symbol_filename && filename != NULL)
556     {
557       if (line != -1)
558         fprintf_filtered (stream, " at %s:%d", filename, line);
559       else
560         fprintf_filtered (stream, " in %s", filename);
561     }
562   if (unmapped)
563     fputs_filtered ("*>", stream);
564   else
565     fputs_filtered (">", stream);
566
567   do_cleanups (cleanup_chain);
568 }
569
570 /* Given an address ADDR return all the elements needed to print the
571    address in a symbolic form. NAME can be mangled or not depending
572    on DO_DEMANGLE (and also on the asm_demangle global variable,
573    manipulated via ''set print asm-demangle''). Return 0 in case of
574    success, when all the info in the OUT paramters is valid. Return 1
575    otherwise. */
576 int
577 build_address_symbolic (CORE_ADDR addr,  /* IN */
578                         int do_demangle, /* IN */
579                         char **name,     /* OUT */
580                         int *offset,     /* OUT */
581                         char **filename, /* OUT */
582                         int *line,       /* OUT */
583                         int *unmapped)   /* OUT */
584 {
585   struct minimal_symbol *msymbol;
586   struct symbol *symbol;
587   struct symtab *symtab = 0;
588   CORE_ADDR name_location = 0;
589   asection *section = 0;
590   char *name_temp = "";
591   
592   /* Let's say it is unmapped. */
593   *unmapped = 0;
594
595   /* Determine if the address is in an overlay, and whether it is
596      mapped. */
597   if (overlay_debugging)
598     {
599       section = find_pc_overlay (addr);
600       if (pc_in_unmapped_range (addr, section))
601         {
602           *unmapped = 1;
603           addr = overlay_mapped_address (addr, section);
604         }
605     }
606
607   /* First try to find the address in the symbol table, then
608      in the minsyms.  Take the closest one.  */
609
610   /* This is defective in the sense that it only finds text symbols.  So
611      really this is kind of pointless--we should make sure that the
612      minimal symbols have everything we need (by changing that we could
613      save some memory, but for many debug format--ELF/DWARF or
614      anything/stabs--it would be inconvenient to eliminate those minimal
615      symbols anyway).  */
616   msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
617   symbol = find_pc_sect_function (addr, section);
618
619   if (symbol)
620     {
621       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
622       if (do_demangle || asm_demangle)
623         name_temp = SYMBOL_PRINT_NAME (symbol);
624       else
625         name_temp = DEPRECATED_SYMBOL_NAME (symbol);
626     }
627
628   if (msymbol != NULL)
629     {
630       if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
631         {
632           /* The msymbol is closer to the address than the symbol;
633              use the msymbol instead.  */
634           symbol = 0;
635           symtab = 0;
636           name_location = SYMBOL_VALUE_ADDRESS (msymbol);
637           if (do_demangle || asm_demangle)
638             name_temp = SYMBOL_PRINT_NAME (msymbol);
639           else
640             name_temp = DEPRECATED_SYMBOL_NAME (msymbol);
641         }
642     }
643   if (symbol == NULL && msymbol == NULL)
644     return 1;
645
646   /* If the nearest symbol is too far away, don't print anything symbolic.  */
647
648   /* For when CORE_ADDR is larger than unsigned int, we do math in
649      CORE_ADDR.  But when we detect unsigned wraparound in the
650      CORE_ADDR math, we ignore this test and print the offset,
651      because addr+max_symbolic_offset has wrapped through the end
652      of the address space back to the beginning, giving bogus comparison.  */
653   if (addr > name_location + max_symbolic_offset
654       && name_location + max_symbolic_offset > name_location)
655     return 1;
656
657   *offset = addr - name_location;
658
659   *name = xstrdup (name_temp);
660
661   if (print_symbol_filename)
662     {
663       struct symtab_and_line sal;
664
665       sal = find_pc_sect_line (addr, section, 0);
666
667       if (sal.symtab)
668         {
669           *filename = xstrdup (sal.symtab->filename);
670           *line = sal.line;
671         }
672       else if (symtab && symbol && symbol->line)
673         {
674           *filename = xstrdup (symtab->filename);
675           *line = symbol->line;
676         }
677       else if (symtab)
678         {
679           *filename = xstrdup (symtab->filename);
680           *line = -1;
681         }
682     }
683   return 0;
684 }
685
686 /* Print address ADDR on STREAM.  USE_LOCAL means the same thing as for
687    print_longest.  */
688 void
689 print_address_numeric (CORE_ADDR addr, int use_local, struct ui_file *stream)
690 {
691   /* Truncate address to the size of a target address, avoiding shifts
692      larger or equal than the width of a CORE_ADDR.  The local
693      variable ADDR_BIT stops the compiler reporting a shift overflow
694      when it won't occur. */
695   /* NOTE: This assumes that the significant address information is
696      kept in the least significant bits of ADDR - the upper bits were
697      either zero or sign extended.  Should ADDRESS_TO_POINTER() or
698      some ADDRESS_TO_PRINTABLE() be used to do the conversion?  */
699
700   int addr_bit = TARGET_ADDR_BIT;
701
702   if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
703     addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
704   print_longest (stream, 'x', use_local, (ULONGEST) addr);
705 }
706
707 /* Print address ADDR symbolically on STREAM.
708    First print it as a number.  Then perhaps print
709    <SYMBOL + OFFSET> after the number.  */
710
711 void
712 print_address (CORE_ADDR addr, struct ui_file *stream)
713 {
714   print_address_numeric (addr, 1, stream);
715   print_address_symbolic (addr, stream, asm_demangle, " ");
716 }
717
718 /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
719    controls whether to print the symbolic name "raw" or demangled.
720    Global setting "addressprint" controls whether to print hex address
721    or not.  */
722
723 void
724 print_address_demangle (CORE_ADDR addr, struct ui_file *stream, int do_demangle)
725 {
726   if (addr == 0)
727     {
728       fprintf_filtered (stream, "0");
729     }
730   else if (addressprint)
731     {
732       print_address_numeric (addr, 1, stream);
733       print_address_symbolic (addr, stream, do_demangle, " ");
734     }
735   else
736     {
737       print_address_symbolic (addr, stream, do_demangle, "");
738     }
739 }
740 \f
741
742 /* These are the types that $__ will get after an examine command of one
743    of these sizes.  */
744
745 static struct type *examine_i_type;
746
747 static struct type *examine_b_type;
748 static struct type *examine_h_type;
749 static struct type *examine_w_type;
750 static struct type *examine_g_type;
751
752 /* Examine data at address ADDR in format FMT.
753    Fetch it from memory and print on gdb_stdout.  */
754
755 static void
756 do_examine (struct format_data fmt, CORE_ADDR addr)
757 {
758   char format = 0;
759   char size;
760   int count = 1;
761   struct type *val_type = NULL;
762   int i;
763   int maxelts;
764
765   format = fmt.format;
766   size = fmt.size;
767   count = fmt.count;
768   next_address = addr;
769
770   /* String or instruction format implies fetch single bytes
771      regardless of the specified size.  */
772   if (format == 's' || format == 'i')
773     size = 'b';
774
775   if (format == 'i')
776     val_type = examine_i_type;
777   else if (size == 'b')
778     val_type = examine_b_type;
779   else if (size == 'h')
780     val_type = examine_h_type;
781   else if (size == 'w')
782     val_type = examine_w_type;
783   else if (size == 'g')
784     val_type = examine_g_type;
785
786   maxelts = 8;
787   if (size == 'w')
788     maxelts = 4;
789   if (size == 'g')
790     maxelts = 2;
791   if (format == 's' || format == 'i')
792     maxelts = 1;
793
794   /* Print as many objects as specified in COUNT, at most maxelts per line,
795      with the address of the next one at the start of each line.  */
796
797   while (count > 0)
798     {
799       QUIT;
800       print_address (next_address, gdb_stdout);
801       printf_filtered (":");
802       for (i = maxelts;
803            i > 0 && count > 0;
804            i--, count--)
805         {
806           printf_filtered ("\t");
807           /* Note that print_formatted sets next_address for the next
808              object.  */
809           last_examine_address = next_address;
810
811           if (last_examine_value)
812             value_free (last_examine_value);
813
814           /* The value to be displayed is not fetched greedily.
815              Instead, to avoid the posibility of a fetched value not
816              being used, its retreval is delayed until the print code
817              uses it.  When examining an instruction stream, the
818              disassembler will perform its own memory fetch using just
819              the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
820              the disassembler be modified so that LAST_EXAMINE_VALUE
821              is left with the byte sequence from the last complete
822              instruction fetched from memory? */
823           last_examine_value = value_at_lazy (val_type, next_address);
824
825           if (last_examine_value)
826             release_value (last_examine_value);
827
828           print_formatted (last_examine_value, format, size, gdb_stdout);
829         }
830       printf_filtered ("\n");
831       gdb_flush (gdb_stdout);
832     }
833 }
834 \f
835 static void
836 validate_format (struct format_data fmt, char *cmdname)
837 {
838   if (fmt.size != 0)
839     error (_("Size letters are meaningless in \"%s\" command."), cmdname);
840   if (fmt.count != 1)
841     error (_("Item count other than 1 is meaningless in \"%s\" command."),
842            cmdname);
843   if (fmt.format == 'i' || fmt.format == 's')
844     error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
845            fmt.format, cmdname);
846 }
847
848 /*  Evaluate string EXP as an expression in the current language and
849    print the resulting value.  EXP may contain a format specifier as the
850    first argument ("/x myvar" for example, to print myvar in hex).
851  */
852
853 static void
854 print_command_1 (char *exp, int inspect, int voidprint)
855 {
856   struct expression *expr;
857   struct cleanup *old_chain = 0;
858   char format = 0;
859   struct value *val;
860   struct format_data fmt;
861   int cleanup = 0;
862
863   /* Pass inspect flag to the rest of the print routines in a global (sigh). */
864   inspect_it = inspect;
865
866   if (exp && *exp == '/')
867     {
868       exp++;
869       fmt = decode_format (&exp, last_format, 0);
870       validate_format (fmt, "print");
871       last_format = format = fmt.format;
872     }
873   else
874     {
875       fmt.count = 1;
876       fmt.format = 0;
877       fmt.size = 0;
878     }
879
880   if (exp && *exp)
881     {
882       struct type *type;
883       expr = parse_expression (exp);
884       old_chain = make_cleanup (free_current_contents, &expr);
885       cleanup = 1;
886       val = evaluate_expression (expr);
887     }
888   else
889     val = access_value_history (0);
890
891   if (voidprint || (val && value_type (val) &&
892                     TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
893     {
894       int histindex = record_latest_value (val);
895
896       if (histindex >= 0)
897         annotate_value_history_begin (histindex, value_type (val));
898       else
899         annotate_value_begin (value_type (val));
900
901       if (inspect)
902         printf_unfiltered ("\031(gdb-makebuffer \"%s\"  %d '(\"", exp, histindex);
903       else if (histindex >= 0)
904         printf_filtered ("$%d = ", histindex);
905
906       if (histindex >= 0)
907         annotate_value_history_value ();
908
909       print_formatted (val, format, fmt.size, 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   inspect_it = 0;               /* Reset print routines to normal */
924 }
925
926 static void
927 print_command (char *exp, int from_tty)
928 {
929   print_command_1 (exp, 0, 1);
930 }
931
932 /* Same as print, except in epoch, it gets its own window */
933 static void
934 inspect_command (char *exp, int from_tty)
935 {
936   extern int epoch_interface;
937
938   print_command_1 (exp, epoch_interface, 1);
939 }
940
941 /* Same as print, except it doesn't print void results. */
942 static void
943 call_command (char *exp, int from_tty)
944 {
945   print_command_1 (exp, 0, 0);
946 }
947
948 void
949 output_command (char *exp, int from_tty)
950 {
951   struct expression *expr;
952   struct cleanup *old_chain;
953   char format = 0;
954   struct value *val;
955   struct format_data fmt;
956
957   if (exp && *exp == '/')
958     {
959       exp++;
960       fmt = decode_format (&exp, 0, 0);
961       validate_format (fmt, "output");
962       format = fmt.format;
963     }
964
965   expr = parse_expression (exp);
966   old_chain = make_cleanup (free_current_contents, &expr);
967
968   val = evaluate_expression (expr);
969
970   annotate_value_begin (value_type (val));
971
972   print_formatted (val, format, fmt.size, gdb_stdout);
973
974   annotate_value_end ();
975
976   wrap_here ("");
977   gdb_flush (gdb_stdout);
978
979   do_cleanups (old_chain);
980 }
981
982 static void
983 set_command (char *exp, int from_tty)
984 {
985   struct expression *expr = parse_expression (exp);
986   struct cleanup *old_chain =
987     make_cleanup (free_current_contents, &expr);
988   evaluate_expression (expr);
989   do_cleanups (old_chain);
990 }
991
992 static void
993 sym_info (char *arg, int from_tty)
994 {
995   struct minimal_symbol *msymbol;
996   struct objfile *objfile;
997   struct obj_section *osect;
998   asection *sect;
999   CORE_ADDR addr, sect_addr;
1000   int matches = 0;
1001   unsigned int offset;
1002
1003   if (!arg)
1004     error_no_arg (_("address"));
1005
1006   addr = parse_and_eval_address (arg);
1007   ALL_OBJSECTIONS (objfile, osect)
1008   {
1009     sect = osect->the_bfd_section;
1010     sect_addr = overlay_mapped_address (addr, sect);
1011
1012     if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
1013         (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
1014       {
1015         matches = 1;
1016         offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1017         if (offset)
1018           printf_filtered ("%s + %u in ",
1019                            SYMBOL_PRINT_NAME (msymbol), offset);
1020         else
1021           printf_filtered ("%s in ",
1022                            SYMBOL_PRINT_NAME (msymbol));
1023         if (pc_in_unmapped_range (addr, sect))
1024           printf_filtered (_("load address range of "));
1025         if (section_is_overlay (sect))
1026           printf_filtered (_("%s overlay "),
1027                            section_is_mapped (sect) ? "mapped" : "unmapped");
1028         printf_filtered (_("section %s"), sect->name);
1029         printf_filtered ("\n");
1030       }
1031   }
1032   if (matches == 0)
1033     printf_filtered (_("No symbol matches %s.\n"), arg);
1034 }
1035
1036 static void
1037 address_info (char *exp, int from_tty)
1038 {
1039   struct symbol *sym;
1040   struct minimal_symbol *msymbol;
1041   long val;
1042   long basereg;
1043   asection *section;
1044   CORE_ADDR load_addr;
1045   int is_a_field_of_this;       /* C++: lookup_symbol sets this to nonzero
1046                                    if exp is a field of `this'. */
1047
1048   if (exp == 0)
1049     error (_("Argument required."));
1050
1051   sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
1052                        &is_a_field_of_this, (struct symtab **) NULL);
1053   if (sym == NULL)
1054     {
1055       if (is_a_field_of_this)
1056         {
1057           printf_filtered ("Symbol \"");
1058           fprintf_symbol_filtered (gdb_stdout, exp,
1059                                    current_language->la_language, DMGL_ANSI);
1060           printf_filtered ("\" is a field of the local class variable ");
1061           if (current_language->la_language == language_objc)
1062             printf_filtered ("`self'\n");       /* ObjC equivalent of "this" */
1063           else
1064             printf_filtered ("`this'\n");
1065           return;
1066         }
1067
1068       msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1069
1070       if (msymbol != NULL)
1071         {
1072           load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1073
1074           printf_filtered ("Symbol \"");
1075           fprintf_symbol_filtered (gdb_stdout, exp,
1076                                    current_language->la_language, DMGL_ANSI);
1077           printf_filtered ("\" is at ");
1078           print_address_numeric (load_addr, 1, gdb_stdout);
1079           printf_filtered (" in a file compiled without debugging");
1080           section = SYMBOL_BFD_SECTION (msymbol);
1081           if (section_is_overlay (section))
1082             {
1083               load_addr = overlay_unmapped_address (load_addr, section);
1084               printf_filtered (",\n -- loaded at ");
1085               print_address_numeric (load_addr, 1, gdb_stdout);
1086               printf_filtered (" in overlay section %s", section->name);
1087             }
1088           printf_filtered (".\n");
1089         }
1090       else
1091         error (_("No symbol \"%s\" in current context."), exp);
1092       return;
1093     }
1094
1095   printf_filtered ("Symbol \"");
1096   fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym),
1097                            current_language->la_language, DMGL_ANSI);
1098   printf_filtered ("\" is ");
1099   val = SYMBOL_VALUE (sym);
1100   basereg = SYMBOL_BASEREG (sym);
1101   section = SYMBOL_BFD_SECTION (sym);
1102
1103   switch (SYMBOL_CLASS (sym))
1104     {
1105     case LOC_CONST:
1106     case LOC_CONST_BYTES:
1107       printf_filtered ("constant");
1108       break;
1109
1110     case LOC_LABEL:
1111       printf_filtered ("a label at address ");
1112       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1113                              1, gdb_stdout);
1114       if (section_is_overlay (section))
1115         {
1116           load_addr = overlay_unmapped_address (load_addr, section);
1117           printf_filtered (",\n -- loaded at ");
1118           print_address_numeric (load_addr, 1, gdb_stdout);
1119           printf_filtered (" in overlay section %s", section->name);
1120         }
1121       break;
1122
1123     case LOC_COMPUTED:
1124     case LOC_COMPUTED_ARG:
1125       /* FIXME: cagney/2004-01-26: It should be possible to
1126          unconditionally call the SYMBOL_OPS method when available.
1127          Unfortunately DWARF 2 stores the frame-base (instead of the
1128          function) location in a function's symbol.  Oops!  For the
1129          moment enable this when/where applicable.  */
1130       SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
1131       break;
1132
1133     case LOC_REGISTER:
1134       printf_filtered (_("a variable in register %s"), REGISTER_NAME (val));
1135       break;
1136
1137     case LOC_STATIC:
1138       printf_filtered (_("static storage at address "));
1139       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1140                              1, gdb_stdout);
1141       if (section_is_overlay (section))
1142         {
1143           load_addr = overlay_unmapped_address (load_addr, section);
1144           printf_filtered (_(",\n -- loaded at "));
1145           print_address_numeric (load_addr, 1, gdb_stdout);
1146           printf_filtered (_(" in overlay section %s"), section->name);
1147         }
1148       break;
1149
1150     case LOC_INDIRECT:
1151       printf_filtered (_("external global (indirect addressing), at address *("));
1152       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1153                              1, gdb_stdout);
1154       printf_filtered (")");
1155       if (section_is_overlay (section))
1156         {
1157           load_addr = overlay_unmapped_address (load_addr, section);
1158           printf_filtered (_(",\n -- loaded at "));
1159           print_address_numeric (load_addr, 1, gdb_stdout);
1160           printf_filtered (_(" in overlay section %s"), section->name);
1161         }
1162       break;
1163
1164     case LOC_REGPARM:
1165       printf_filtered (_("an argument in register %s"), REGISTER_NAME (val));
1166       break;
1167
1168     case LOC_REGPARM_ADDR:
1169       printf_filtered (_("address of an argument in register %s"), REGISTER_NAME (val));
1170       break;
1171
1172     case LOC_ARG:
1173       printf_filtered (_("an argument at offset %ld"), val);
1174       break;
1175
1176     case LOC_LOCAL_ARG:
1177       printf_filtered (_("an argument at frame offset %ld"), val);
1178       break;
1179
1180     case LOC_LOCAL:
1181       printf_filtered (_("a local variable at frame offset %ld"), val);
1182       break;
1183
1184     case LOC_REF_ARG:
1185       printf_filtered (_("a reference argument at offset %ld"), val);
1186       break;
1187
1188     case LOC_BASEREG:
1189       printf_filtered (_("a variable at offset %ld from register %s"),
1190                        val, REGISTER_NAME (basereg));
1191       break;
1192
1193     case LOC_BASEREG_ARG:
1194       printf_filtered (_("an argument at offset %ld from register %s"),
1195                        val, REGISTER_NAME (basereg));
1196       break;
1197
1198     case LOC_TYPEDEF:
1199       printf_filtered (_("a typedef"));
1200       break;
1201
1202     case LOC_BLOCK:
1203       printf_filtered (_("a function at address "));
1204       print_address_numeric (load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
1205                              1, gdb_stdout);
1206       if (section_is_overlay (section))
1207         {
1208           load_addr = overlay_unmapped_address (load_addr, section);
1209           printf_filtered (_(",\n -- loaded at "));
1210           print_address_numeric (load_addr, 1, gdb_stdout);
1211           printf_filtered (_(" in overlay section %s"), section->name);
1212         }
1213       break;
1214
1215     case LOC_UNRESOLVED:
1216       {
1217         struct minimal_symbol *msym;
1218
1219         msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL);
1220         if (msym == NULL)
1221           printf_filtered ("unresolved");
1222         else
1223           {
1224             section = SYMBOL_BFD_SECTION (msym);
1225             printf_filtered (_("static storage at address "));
1226             print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (msym),
1227                                    1, gdb_stdout);
1228             if (section_is_overlay (section))
1229               {
1230                 load_addr = overlay_unmapped_address (load_addr, section);
1231                 printf_filtered (_(",\n -- loaded at "));
1232                 print_address_numeric (load_addr, 1, gdb_stdout);
1233                 printf_filtered (_(" in overlay section %s"), section->name);
1234               }
1235           }
1236       }
1237       break;
1238
1239     case LOC_HP_THREAD_LOCAL_STATIC:
1240       printf_filtered (
1241                         "a thread-local variable at offset %ld from the thread base register %s",
1242                         val, REGISTER_NAME (basereg));
1243       break;
1244
1245     case LOC_OPTIMIZED_OUT:
1246       printf_filtered (_("optimized out"));
1247       break;
1248
1249     default:
1250       printf_filtered (_("of unknown (botched) type"));
1251       break;
1252     }
1253   printf_filtered (".\n");
1254 }
1255 \f
1256 void
1257 x_command (char *exp, int from_tty)
1258 {
1259   struct expression *expr;
1260   struct format_data fmt;
1261   struct cleanup *old_chain;
1262   struct value *val;
1263
1264   fmt.format = last_format;
1265   fmt.size = last_size;
1266   fmt.count = 1;
1267
1268   if (exp && *exp == '/')
1269     {
1270       exp++;
1271       fmt = decode_format (&exp, last_format, last_size);
1272     }
1273
1274   /* If we have an expression, evaluate it and use it as the address.  */
1275
1276   if (exp != 0 && *exp != 0)
1277     {
1278       expr = parse_expression (exp);
1279       /* Cause expression not to be there any more
1280          if this command is repeated with Newline.
1281          But don't clobber a user-defined command's definition.  */
1282       if (from_tty)
1283         *exp = 0;
1284       old_chain = make_cleanup (free_current_contents, &expr);
1285       val = evaluate_expression (expr);
1286       if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1287         val = value_ind (val);
1288       /* In rvalue contexts, such as this, functions are coerced into
1289          pointers to functions.  This makes "x/i main" work.  */
1290       if (/* last_format == 'i'  && */ 
1291           TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1292            && VALUE_LVAL (val) == lval_memory)
1293         next_address = VALUE_ADDRESS (val);
1294       else
1295         next_address = value_as_address (val);
1296       do_cleanups (old_chain);
1297     }
1298
1299   do_examine (fmt, next_address);
1300
1301   /* If the examine succeeds, we remember its size and format for next time.  */
1302   last_size = fmt.size;
1303   last_format = fmt.format;
1304
1305   /* Set a couple of internal variables if appropriate. */
1306   if (last_examine_value)
1307     {
1308       /* Make last address examined available to the user as $_.  Use
1309          the correct pointer type.  */
1310       struct type *pointer_type
1311         = lookup_pointer_type (value_type (last_examine_value));
1312       set_internalvar (lookup_internalvar ("_"),
1313                        value_from_pointer (pointer_type,
1314                                            last_examine_address));
1315
1316       /* Make contents of last address examined available to the user as $__. */
1317       /* If the last value has not been fetched from memory then don't
1318          fetch it now - instead mark it by voiding the $__ variable. */
1319       if (value_lazy (last_examine_value))
1320         set_internalvar (lookup_internalvar ("__"),
1321                          allocate_value (builtin_type_void));
1322       else
1323         set_internalvar (lookup_internalvar ("__"), last_examine_value);
1324     }
1325 }
1326 \f
1327
1328 /* Add an expression to the auto-display chain.
1329    Specify the expression.  */
1330
1331 static void
1332 display_command (char *exp, int from_tty)
1333 {
1334   struct format_data fmt;
1335   struct expression *expr;
1336   struct display *new;
1337   int display_it = 1;
1338
1339 #if defined(TUI)
1340   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1341      `tui_version'.  */
1342   if (tui_active && exp != NULL && *exp == '$')
1343     display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1344 #endif
1345
1346   if (display_it)
1347     {
1348       if (exp == 0)
1349         {
1350           do_displays ();
1351           return;
1352         }
1353
1354       if (*exp == '/')
1355         {
1356           exp++;
1357           fmt = decode_format (&exp, 0, 0);
1358           if (fmt.size && fmt.format == 0)
1359             fmt.format = 'x';
1360           if (fmt.format == 'i' || fmt.format == 's')
1361             fmt.size = 'b';
1362         }
1363       else
1364         {
1365           fmt.format = 0;
1366           fmt.size = 0;
1367           fmt.count = 0;
1368         }
1369
1370       innermost_block = 0;
1371       expr = parse_expression (exp);
1372
1373       new = (struct display *) xmalloc (sizeof (struct display));
1374
1375       new->exp = expr;
1376       new->block = innermost_block;
1377       new->next = display_chain;
1378       new->number = ++display_number;
1379       new->format = fmt;
1380       new->enabled_p = 1;
1381       display_chain = new;
1382
1383       if (from_tty && target_has_execution)
1384         do_one_display (new);
1385
1386       dont_repeat ();
1387     }
1388 }
1389
1390 static void
1391 free_display (struct display *d)
1392 {
1393   xfree (d->exp);
1394   xfree (d);
1395 }
1396
1397 /* Clear out the display_chain.
1398    Done when new symtabs are loaded, since this invalidates
1399    the types stored in many expressions.  */
1400
1401 void
1402 clear_displays (void)
1403 {
1404   struct display *d;
1405
1406   while ((d = display_chain) != NULL)
1407     {
1408       xfree (d->exp);
1409       display_chain = d->next;
1410       xfree (d);
1411     }
1412 }
1413
1414 /* Delete the auto-display number NUM.  */
1415
1416 static void
1417 delete_display (int num)
1418 {
1419   struct display *d1, *d;
1420
1421   if (!display_chain)
1422     error (_("No display number %d."), num);
1423
1424   if (display_chain->number == num)
1425     {
1426       d1 = display_chain;
1427       display_chain = d1->next;
1428       free_display (d1);
1429     }
1430   else
1431     for (d = display_chain;; d = d->next)
1432       {
1433         if (d->next == 0)
1434           error (_("No display number %d."), num);
1435         if (d->next->number == num)
1436           {
1437             d1 = d->next;
1438             d->next = d1->next;
1439             free_display (d1);
1440             break;
1441           }
1442       }
1443 }
1444
1445 /* Delete some values from the auto-display chain.
1446    Specify the element numbers.  */
1447
1448 static void
1449 undisplay_command (char *args, int from_tty)
1450 {
1451   char *p = args;
1452   char *p1;
1453   int num;
1454
1455   if (args == 0)
1456     {
1457       if (query ("Delete all auto-display expressions? "))
1458         clear_displays ();
1459       dont_repeat ();
1460       return;
1461     }
1462
1463   while (*p)
1464     {
1465       p1 = p;
1466       while (*p1 >= '0' && *p1 <= '9')
1467         p1++;
1468       if (*p1 && *p1 != ' ' && *p1 != '\t')
1469         error (_("Arguments must be display numbers."));
1470
1471       num = atoi (p);
1472
1473       delete_display (num);
1474
1475       p = p1;
1476       while (*p == ' ' || *p == '\t')
1477         p++;
1478     }
1479   dont_repeat ();
1480 }
1481
1482 /* Display a single auto-display.  
1483    Do nothing if the display cannot be printed in the current context,
1484    or if the display is disabled. */
1485
1486 static void
1487 do_one_display (struct display *d)
1488 {
1489   int within_current_scope;
1490
1491   if (d->enabled_p == 0)
1492     return;
1493
1494   if (d->block)
1495     within_current_scope = contained_in (get_selected_block (0), d->block);
1496   else
1497     within_current_scope = 1;
1498   if (!within_current_scope)
1499     return;
1500
1501   current_display_number = d->number;
1502
1503   annotate_display_begin ();
1504   printf_filtered ("%d", d->number);
1505   annotate_display_number_end ();
1506   printf_filtered (": ");
1507   if (d->format.size)
1508     {
1509       CORE_ADDR addr;
1510       struct value *val;
1511
1512       annotate_display_format ();
1513
1514       printf_filtered ("x/");
1515       if (d->format.count != 1)
1516         printf_filtered ("%d", d->format.count);
1517       printf_filtered ("%c", d->format.format);
1518       if (d->format.format != 'i' && d->format.format != 's')
1519         printf_filtered ("%c", d->format.size);
1520       printf_filtered (" ");
1521
1522       annotate_display_expression ();
1523
1524       print_expression (d->exp, gdb_stdout);
1525       annotate_display_expression_end ();
1526
1527       if (d->format.count != 1)
1528         printf_filtered ("\n");
1529       else
1530         printf_filtered ("  ");
1531
1532       val = evaluate_expression (d->exp);
1533       addr = value_as_address (val);
1534       if (d->format.format == 'i')
1535         addr = ADDR_BITS_REMOVE (addr);
1536
1537       annotate_display_value ();
1538
1539       do_examine (d->format, addr);
1540     }
1541   else
1542     {
1543       annotate_display_format ();
1544
1545       if (d->format.format)
1546         printf_filtered ("/%c ", d->format.format);
1547
1548       annotate_display_expression ();
1549
1550       print_expression (d->exp, gdb_stdout);
1551       annotate_display_expression_end ();
1552
1553       printf_filtered (" = ");
1554
1555       annotate_display_expression ();
1556
1557       print_formatted (evaluate_expression (d->exp),
1558                        d->format.format, d->format.size, gdb_stdout);
1559       printf_filtered ("\n");
1560     }
1561
1562   annotate_display_end ();
1563
1564   gdb_flush (gdb_stdout);
1565   current_display_number = -1;
1566 }
1567
1568 /* Display all of the values on the auto-display chain which can be
1569    evaluated in the current scope.  */
1570
1571 void
1572 do_displays (void)
1573 {
1574   struct display *d;
1575
1576   for (d = display_chain; d; d = d->next)
1577     do_one_display (d);
1578 }
1579
1580 /* Delete the auto-display which we were in the process of displaying.
1581    This is done when there is an error or a signal.  */
1582
1583 void
1584 disable_display (int num)
1585 {
1586   struct display *d;
1587
1588   for (d = display_chain; d; d = d->next)
1589     if (d->number == num)
1590       {
1591         d->enabled_p = 0;
1592         return;
1593       }
1594   printf_unfiltered (_("No display number %d.\n"), num);
1595 }
1596
1597 void
1598 disable_current_display (void)
1599 {
1600   if (current_display_number >= 0)
1601     {
1602       disable_display (current_display_number);
1603       fprintf_unfiltered (gdb_stderr, "Disabling display %d to avoid infinite recursion.\n",
1604                           current_display_number);
1605     }
1606   current_display_number = -1;
1607 }
1608
1609 static void
1610 display_info (char *ignore, int from_tty)
1611 {
1612   struct display *d;
1613
1614   if (!display_chain)
1615     printf_unfiltered (_("There are no auto-display expressions now.\n"));
1616   else
1617     printf_filtered (_("Auto-display expressions now in effect:\n\
1618 Num Enb Expression\n"));
1619
1620   for (d = display_chain; d; d = d->next)
1621     {
1622       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
1623       if (d->format.size)
1624         printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1625                          d->format.format);
1626       else if (d->format.format)
1627         printf_filtered ("/%c ", d->format.format);
1628       print_expression (d->exp, gdb_stdout);
1629       if (d->block && !contained_in (get_selected_block (0), d->block))
1630         printf_filtered (_(" (cannot be evaluated in the current context)"));
1631       printf_filtered ("\n");
1632       gdb_flush (gdb_stdout);
1633     }
1634 }
1635
1636 static void
1637 enable_display (char *args, int from_tty)
1638 {
1639   char *p = args;
1640   char *p1;
1641   int num;
1642   struct display *d;
1643
1644   if (p == 0)
1645     {
1646       for (d = display_chain; d; d = d->next)
1647         d->enabled_p = 1;
1648     }
1649   else
1650     while (*p)
1651       {
1652         p1 = p;
1653         while (*p1 >= '0' && *p1 <= '9')
1654           p1++;
1655         if (*p1 && *p1 != ' ' && *p1 != '\t')
1656           error (_("Arguments must be display numbers."));
1657
1658         num = atoi (p);
1659
1660         for (d = display_chain; d; d = d->next)
1661           if (d->number == num)
1662             {
1663               d->enabled_p = 1;
1664               goto win;
1665             }
1666         printf_unfiltered (_("No display number %d.\n"), num);
1667       win:
1668         p = p1;
1669         while (*p == ' ' || *p == '\t')
1670           p++;
1671       }
1672 }
1673
1674 static void
1675 disable_display_command (char *args, int from_tty)
1676 {
1677   char *p = args;
1678   char *p1;
1679   struct display *d;
1680
1681   if (p == 0)
1682     {
1683       for (d = display_chain; d; d = d->next)
1684         d->enabled_p = 0;
1685     }
1686   else
1687     while (*p)
1688       {
1689         p1 = p;
1690         while (*p1 >= '0' && *p1 <= '9')
1691           p1++;
1692         if (*p1 && *p1 != ' ' && *p1 != '\t')
1693           error (_("Arguments must be display numbers."));
1694
1695         disable_display (atoi (p));
1696
1697         p = p1;
1698         while (*p == ' ' || *p == '\t')
1699           p++;
1700       }
1701 }
1702 \f
1703
1704 /* Print the value in stack frame FRAME of a variable
1705    specified by a struct symbol.  */
1706
1707 void
1708 print_variable_value (struct symbol *var, struct frame_info *frame,
1709                       struct ui_file *stream)
1710 {
1711   struct value *val = read_var_value (var, frame);
1712
1713   value_print (val, stream, 0, Val_pretty_default);
1714 }
1715
1716 static void
1717 printf_command (char *arg, int from_tty)
1718 {
1719   char *f = NULL;
1720   char *s = arg;
1721   char *string = NULL;
1722   struct value **val_args;
1723   char *substrings;
1724   char *current_substring;
1725   int nargs = 0;
1726   int allocated_args = 20;
1727   struct cleanup *old_cleanups;
1728
1729   val_args = (struct value **) xmalloc (allocated_args
1730                                         * sizeof (struct value *));
1731   old_cleanups = make_cleanup (free_current_contents, &val_args);
1732
1733   if (s == 0)
1734     error_no_arg (_("format-control string and values to print"));
1735
1736   /* Skip white space before format string */
1737   while (*s == ' ' || *s == '\t')
1738     s++;
1739
1740   /* A format string should follow, enveloped in double quotes */
1741   if (*s++ != '"')
1742     error (_("Bad format string, missing '\"'."));
1743
1744   /* Parse the format-control string and copy it into the string STRING,
1745      processing some kinds of escape sequence.  */
1746
1747   f = string = (char *) alloca (strlen (s) + 1);
1748
1749   while (*s != '"')
1750     {
1751       int c = *s++;
1752       switch (c)
1753         {
1754         case '\0':
1755           error (_("Bad format string, non-terminated '\"'."));
1756
1757         case '\\':
1758           switch (c = *s++)
1759             {
1760             case '\\':
1761               *f++ = '\\';
1762               break;
1763             case 'a':
1764               *f++ = '\a';
1765               break;
1766             case 'b':
1767               *f++ = '\b';
1768               break;
1769             case 'f':
1770               *f++ = '\f';
1771               break;
1772             case 'n':
1773               *f++ = '\n';
1774               break;
1775             case 'r':
1776               *f++ = '\r';
1777               break;
1778             case 't':
1779               *f++ = '\t';
1780               break;
1781             case 'v':
1782               *f++ = '\v';
1783               break;
1784             case '"':
1785               *f++ = '"';
1786               break;
1787             default:
1788               /* ??? TODO: handle other escape sequences */
1789               error (_("Unrecognized escape character \\%c in format string."),
1790                      c);
1791             }
1792           break;
1793
1794         default:
1795           *f++ = c;
1796         }
1797     }
1798
1799   /* Skip over " and following space and comma.  */
1800   s++;
1801   *f++ = '\0';
1802   while (*s == ' ' || *s == '\t')
1803     s++;
1804
1805   if (*s != ',' && *s != 0)
1806     error (_("Invalid argument syntax"));
1807
1808   if (*s == ',')
1809     s++;
1810   while (*s == ' ' || *s == '\t')
1811     s++;
1812
1813   /* Need extra space for the '\0's.  Doubling the size is sufficient.  */
1814   substrings = alloca (strlen (string) * 2);
1815   current_substring = substrings;
1816
1817   {
1818     /* Now scan the string for %-specs and see what kinds of args they want.
1819        argclass[I] classifies the %-specs so we can give printf_filtered
1820        something of the right size.  */
1821
1822     enum argclass
1823       {
1824         no_arg, int_arg, string_arg, double_arg, long_long_arg
1825       };
1826     enum argclass *argclass;
1827     enum argclass this_argclass;
1828     char *last_arg;
1829     int nargs_wanted;
1830     int lcount;
1831     int i;
1832
1833     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1834     nargs_wanted = 0;
1835     f = string;
1836     last_arg = string;
1837     while (*f)
1838       if (*f++ == '%')
1839         {
1840           lcount = 0;
1841           while (strchr ("0123456789.hlL-+ #", *f))
1842             {
1843               if (*f == 'l' || *f == 'L')
1844                 lcount++;
1845               f++;
1846             }
1847           switch (*f)
1848             {
1849             case 's':
1850               this_argclass = string_arg;
1851               break;
1852
1853             case 'e':
1854             case 'f':
1855             case 'g':
1856               this_argclass = double_arg;
1857               break;
1858
1859             case '*':
1860               error (_("`*' not supported for precision or width in printf"));
1861
1862             case 'n':
1863               error (_("Format specifier `n' not supported in printf"));
1864
1865             case '%':
1866               this_argclass = no_arg;
1867               break;
1868
1869             default:
1870               if (lcount > 1)
1871                 this_argclass = long_long_arg;
1872               else
1873                 this_argclass = int_arg;
1874               break;
1875             }
1876           f++;
1877           if (this_argclass != no_arg)
1878             {
1879               strncpy (current_substring, last_arg, f - last_arg);
1880               current_substring += f - last_arg;
1881               *current_substring++ = '\0';
1882               last_arg = f;
1883               argclass[nargs_wanted++] = this_argclass;
1884             }
1885         }
1886
1887     /* Now, parse all arguments and evaluate them.
1888        Store the VALUEs in VAL_ARGS.  */
1889
1890     while (*s != '\0')
1891       {
1892         char *s1;
1893         if (nargs == allocated_args)
1894           val_args = (struct value **) xrealloc ((char *) val_args,
1895                                                  (allocated_args *= 2)
1896                                                  * sizeof (struct value *));
1897         s1 = s;
1898         val_args[nargs] = parse_to_comma_and_eval (&s1);
1899
1900         /* If format string wants a float, unchecked-convert the value to
1901            floating point of the same size */
1902
1903         if (argclass[nargs] == double_arg)
1904           {
1905             struct type *type = value_type (val_args[nargs]);
1906             if (TYPE_LENGTH (type) == sizeof (float))
1907               deprecated_set_value_type (val_args[nargs], builtin_type_float);
1908             if (TYPE_LENGTH (type) == sizeof (double))
1909               deprecated_set_value_type (val_args[nargs], builtin_type_double);
1910           }
1911         nargs++;
1912         s = s1;
1913         if (*s == ',')
1914           s++;
1915       }
1916
1917     if (nargs != nargs_wanted)
1918       error (_("Wrong number of arguments for specified format-string"));
1919
1920     /* Now actually print them.  */
1921     current_substring = substrings;
1922     for (i = 0; i < nargs; i++)
1923       {
1924         switch (argclass[i])
1925           {
1926           case string_arg:
1927             {
1928               char *str;
1929               CORE_ADDR tem;
1930               int j;
1931               tem = value_as_address (val_args[i]);
1932
1933               /* This is a %s argument.  Find the length of the string.  */
1934               for (j = 0;; j++)
1935                 {
1936                   char c;
1937                   QUIT;
1938                   read_memory (tem + j, &c, 1);
1939                   if (c == 0)
1940                     break;
1941                 }
1942
1943               /* Copy the string contents into a string inside GDB.  */
1944               str = (char *) alloca (j + 1);
1945               if (j != 0)
1946                 read_memory (tem, str, j);
1947               str[j] = 0;
1948
1949               printf_filtered (current_substring, str);
1950             }
1951             break;
1952           case double_arg:
1953             {
1954               double val = value_as_double (val_args[i]);
1955               printf_filtered (current_substring, val);
1956               break;
1957             }
1958           case long_long_arg:
1959 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
1960             {
1961               long long val = value_as_long (val_args[i]);
1962               printf_filtered (current_substring, val);
1963               break;
1964             }
1965 #else
1966             error (_("long long not supported in printf"));
1967 #endif
1968           case int_arg:
1969             {
1970               /* FIXME: there should be separate int_arg and long_arg.  */
1971               long val = value_as_long (val_args[i]);
1972               printf_filtered (current_substring, val);
1973               break;
1974             }
1975           default:              /* purecov: deadcode */
1976             error (_("internal error in printf_command"));              /* purecov: deadcode */
1977           }
1978         /* Skip to the next substring.  */
1979         current_substring += strlen (current_substring) + 1;
1980       }
1981     /* Print the portion of the format string after the last argument.  */
1982     puts_filtered (last_arg);
1983   }
1984   do_cleanups (old_cleanups);
1985 }
1986
1987 void
1988 _initialize_printcmd (void)
1989 {
1990   struct cmd_list_element *c;
1991
1992   current_display_number = -1;
1993
1994   add_info ("address", address_info,
1995             "Describe where symbol SYM is stored.");
1996
1997   add_info ("symbol", sym_info,
1998             "Describe what symbol is at location ADDR.\n\
1999 Only for symbols with fixed locations (global or static scope).");
2000
2001   add_com ("x", class_vars, x_command,
2002            concat ("Examine memory: x/FMT ADDRESS.\n\
2003 ADDRESS is an expression for the memory address to examine.\n\
2004 FMT is a repeat count followed by a format letter and a size letter.\n\
2005 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2006   t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n",
2007                    "Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2008 The specified number of objects of the specified size are printed\n\
2009 according to the format.\n\n\
2010 Defaults for format and size letters are those previously used.\n\
2011 Default count is 1.  Default address is following last thing printed\n\
2012 with this command or \"print\".", NULL));
2013
2014 #if 0
2015   add_com ("whereis", class_vars, whereis_command,
2016            "Print line number and file of definition of variable.");
2017 #endif
2018
2019   add_info ("display", display_info,
2020             "Expressions to display when program stops, with code numbers.");
2021
2022   add_cmd ("undisplay", class_vars, undisplay_command,
2023            "Cancel some expressions to be displayed when program stops.\n\
2024 Arguments are the code numbers of the expressions to stop displaying.\n\
2025 No argument means cancel all automatic-display expressions.\n\
2026 \"delete display\" has the same effect as this command.\n\
2027 Do \"info display\" to see current list of code numbers.",
2028            &cmdlist);
2029
2030   add_com ("display", class_vars, display_command,
2031            "Print value of expression EXP each time the program stops.\n\
2032 /FMT may be used before EXP as in the \"print\" command.\n\
2033 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2034 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2035 and examining is done as in the \"x\" command.\n\n\
2036 With no argument, display all currently requested auto-display expressions.\n\
2037 Use \"undisplay\" to cancel display requests previously made."
2038     );
2039
2040   add_cmd ("display", class_vars, enable_display,
2041            "Enable some expressions to be displayed when program stops.\n\
2042 Arguments are the code numbers of the expressions to resume displaying.\n\
2043 No argument means enable all automatic-display expressions.\n\
2044 Do \"info display\" to see current list of code numbers.", &enablelist);
2045
2046   add_cmd ("display", class_vars, disable_display_command,
2047            "Disable some expressions to be displayed when program stops.\n\
2048 Arguments are the code numbers of the expressions to stop displaying.\n\
2049 No argument means disable all automatic-display expressions.\n\
2050 Do \"info display\" to see current list of code numbers.", &disablelist);
2051
2052   add_cmd ("display", class_vars, undisplay_command,
2053            "Cancel some expressions to be displayed when program stops.\n\
2054 Arguments are the code numbers of the expressions to stop displaying.\n\
2055 No argument means cancel all automatic-display expressions.\n\
2056 Do \"info display\" to see current list of code numbers.", &deletelist);
2057
2058   add_com ("printf", class_vars, printf_command,
2059            "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2060 This is useful for formatted output in user-defined commands.");
2061
2062   add_com ("output", class_vars, output_command,
2063            "Like \"print\" but don't put in value history and don't print newline.\n\
2064 This is useful in user-defined commands.");
2065
2066   add_prefix_cmd ("set", class_vars, set_command,
2067                   concat ("Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2068 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2069 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2070 with $), a register (a few standard names starting with $), or an actual\n\
2071 variable in the program being debugged.  EXP is any valid expression.\n",
2072                           "Use \"set variable\" for variables with names identical to set subcommands.\n\
2073 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2074 You can see these environment settings with the \"show\" command.", NULL),
2075                   &setlist, "set ", 1, &cmdlist);
2076   if (dbx_commands)
2077     add_com ("assign", class_vars, set_command, concat ("Evaluate expression \
2078 EXP and assign result to variable VAR, using assignment\n\
2079 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2080 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2081 with $), a register (a few standard names starting with $), or an actual\n\
2082 variable in the program being debugged.  EXP is any valid expression.\n",
2083                                                         "Use \"set variable\" for variables with names identical to set subcommands.\n\
2084 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2085 You can see these environment settings with the \"show\" command.", NULL));
2086
2087   /* "call" is the same as "set", but handy for dbx users to call fns. */
2088   c = add_com ("call", class_vars, call_command,
2089                "Call a function in the program.\n\
2090 The argument is the function name and arguments, in the notation of the\n\
2091 current working language.  The result is printed and saved in the value\n\
2092 history, if it is not void.");
2093   set_cmd_completer (c, location_completer);
2094
2095   add_cmd ("variable", class_vars, set_command,
2096            "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2097 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2098 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2099 with $), a register (a few standard names starting with $), or an actual\n\
2100 variable in the program being debugged.  EXP is any valid expression.\n\
2101 This may usually be abbreviated to simply \"set\".",
2102            &setlist);
2103
2104   c = add_com ("print", class_vars, print_command,
2105            concat ("Print value of expression EXP.\n\
2106 Variables accessible are those of the lexical environment of the selected\n\
2107 stack frame, plus all those whose scope is global or an entire file.\n\
2108 \n\
2109 $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
2110 $$NUM refers to NUM'th value back from the last one.\n\
2111 Names starting with $ refer to registers (with the values they would have\n",
2112                    "if the program were to return to the stack frame now selected, restoring\n\
2113 all registers saved by frames farther in) or else to debugger\n\
2114 \"convenience\" variables (any such name not a known register).\n\
2115 Use assignment expressions to give values to convenience variables.\n",
2116                    "\n\
2117 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2118 @ is a binary operator for treating consecutive data objects\n\
2119 anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
2120 element is FOO, whose second element is stored in the space following\n\
2121 where FOO is stored, etc.  FOO must be an expression whose value\n\
2122 resides in memory.\n",
2123                    "\n\
2124 EXP may be preceded with /FMT, where FMT is a format letter\n\
2125 but no count or size letter (see \"x\" command).", NULL));
2126   set_cmd_completer (c, location_completer);
2127   add_com_alias ("p", "print", class_vars, 1);
2128
2129   c = add_com ("inspect", class_vars, inspect_command,
2130            "Same as \"print\" command, except that if you are running in the epoch\n\
2131 environment, the value is printed in its own window.");
2132   set_cmd_completer (c, location_completer);
2133
2134   deprecated_add_show_from_set
2135     (add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,
2136                   (char *) &max_symbolic_offset,
2137        "Set the largest offset that will be printed in <symbol+1234> form.",
2138                   &setprintlist),
2139      &showprintlist);
2140   deprecated_add_show_from_set
2141     (add_set_cmd ("symbol-filename", no_class, var_boolean,
2142                   (char *) &print_symbol_filename, "\
2143 Set printing of source filename and line number with <symbol>.",
2144                   &setprintlist),
2145      &showprintlist);
2146
2147   /* For examine/instruction a single byte quantity is specified as
2148      the data.  This avoids problems with value_at_lazy() requiring a
2149      valid data type (and rejecting VOID). */
2150   examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2151
2152   examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2153   examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2154   examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2155   examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2156
2157 }