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