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