Merge from vendor branch GCC:
[dragonfly.git] / contrib / gdb / gdb / printcmd.c
1 /* Print values for GNU debugger GDB.
2    Copyright 1986, 87, 88, 89, 90, 91, 93, 94, 95, 1998
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "frame.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "value.h"
27 #include "language.h"
28 #include "expression.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "target.h"
32 #include "breakpoint.h"
33 #include "demangle.h"
34 #include "valprint.h"
35 #include "annotate.h"
36 #include "symfile.h"    /* for overlay functions */
37 #include "objfiles.h"   /* ditto */
38
39 extern int asm_demangle;        /* Whether to demangle syms in asm printouts */
40 extern int addressprint;        /* Whether to print hex addresses in HLL " */
41
42 struct format_data
43 {
44   int count;
45   char format;
46   char size;
47 };
48
49 /* Last specified output format.  */
50
51 static char last_format = 'x';
52
53 /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
54
55 static char last_size = 'w';
56
57 /* Default address to examine next.  */
58
59 static CORE_ADDR next_address;
60
61 /* Default section to examine next. */
62
63 static asection *next_section;
64
65 /* Last address examined.  */
66
67 static CORE_ADDR last_examine_address;
68
69 /* Contents of last address examined.
70    This is not valid past the end of the `x' command!  */
71
72 static value_ptr last_examine_value;
73
74 /* Largest offset between a symbolic value and an address, that will be
75    printed as `0x1234 <symbol+offset>'.  */
76
77 static unsigned int max_symbolic_offset = UINT_MAX;
78
79 /* Append the source filename and linenumber of the symbol when
80    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
81 static int print_symbol_filename = 0;
82
83 /* Number of auto-display expression currently being displayed.
84    So that we can disable it if we get an error or a signal within it.
85    -1 when not doing one.  */
86
87 int current_display_number;
88
89 /* Flag to low-level print routines that this value is being printed
90    in an epoch window.  We'd like to pass this as a parameter, but
91    every routine would need to take it.  Perhaps we can encapsulate
92    this in the I/O stream once we have GNU stdio. */
93
94 int inspect_it = 0;
95
96 struct display
97 {
98   /* Chain link to next auto-display item.  */
99   struct display *next;
100   /* Expression to be evaluated and displayed.  */
101   struct expression *exp;
102   /* Item number of this auto-display item.  */
103   int number;
104   /* Display format specified.  */
105   struct format_data format;
106   /* Innermost block required by this expression when evaluated */
107   struct block *block;
108   /* Status of this display (enabled or disabled) */
109   enum enable status;
110 };
111
112 /* Chain of expressions whose values should be displayed
113    automatically each time the program stops.  */
114
115 static struct display *display_chain;
116
117 static int display_number;
118
119 /* Prototypes for exported functions. */
120
121 void output_command PARAMS ((char *, int));
122
123 void _initialize_printcmd PARAMS ((void));
124
125 /* Prototypes for local functions. */
126
127 static void delete_display PARAMS ((int));
128
129 static void enable_display PARAMS ((char *, int));
130
131 static void disable_display_command PARAMS ((char *, int));
132
133 static void disassemble_command PARAMS ((char *, int));
134
135 static void printf_command PARAMS ((char *, int));
136
137 static void print_frame_nameless_args PARAMS ((struct frame_info *, long,
138                                                int, int, GDB_FILE *));
139
140 static void display_info PARAMS ((char *, int));
141
142 static void do_one_display PARAMS ((struct display *));
143
144 static void undisplay_command PARAMS ((char *, int));
145
146 static void free_display PARAMS ((struct display *));
147
148 static void display_command PARAMS ((char *, int));
149
150 void x_command PARAMS ((char *, int));
151
152 static void address_info PARAMS ((char *, int));
153
154 static void set_command PARAMS ((char *, int));
155
156 static void call_command PARAMS ((char *, int));
157
158 static void inspect_command PARAMS ((char *, int));
159
160 static void print_command PARAMS ((char *, int));
161
162 static void print_command_1 PARAMS ((char *, int, int));
163
164 static void validate_format PARAMS ((struct format_data, char *));
165
166 static void do_examine PARAMS ((struct format_data, CORE_ADDR addr, asection *section));
167
168 static void print_formatted PARAMS ((value_ptr, int, int));
169
170 static struct format_data decode_format PARAMS ((char **, int, int));
171
172 static int print_insn PARAMS ((CORE_ADDR, GDB_FILE *));
173
174 static void sym_info PARAMS ((char *, int));
175
176 \f
177 /* Decode a format specification.  *STRING_PTR should point to it.
178    OFORMAT and OSIZE are used as defaults for the format and size
179    if none are given in the format specification.
180    If OSIZE is zero, then the size field of the returned value
181    should be set only if a size is explicitly specified by the
182    user.
183    The structure returned describes all the data
184    found in the specification.  In addition, *STRING_PTR is advanced
185    past the specification and past all whitespace following it.  */
186
187 static struct format_data
188 decode_format (string_ptr, oformat, osize)
189      char **string_ptr;
190      int oformat;
191      int osize;
192 {
193   struct format_data val;
194   register char *p = *string_ptr;
195
196   val.format = '?';
197   val.size = '?';
198   val.count = 1;
199
200   if (*p >= '0' && *p <= '9')
201     val.count = atoi (p);
202   while (*p >= '0' && *p <= '9') 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') p++;
217   *string_ptr = p;
218
219   /* Set defaults for format and size if not specified.  */
220   if (val.format == '?')
221     {
222       if (val.size == '?')
223         {
224           /* Neither has been specified.  */
225           val.format = oformat;
226           val.size = osize;
227         }
228       else
229         /* If a size is specified, any format makes a reasonable
230            default except 'i'.  */
231         val.format = oformat == 'i' ? 'x' : oformat;
232     }
233   else if (val.size == '?')
234     switch (val.format)
235       {
236       case 'a':
237       case 's':
238         /* Pick the appropriate size for an address.  */
239         if (TARGET_PTR_BIT == 64)
240           val.size = osize ? 'g' : osize;
241         else if (TARGET_PTR_BIT == 32)
242           val.size = osize ? 'w' : osize;
243         else if (TARGET_PTR_BIT == 16)
244           val.size = osize ? 'h' : osize;
245         else
246           /* Bad value for TARGET_PTR_BIT */
247           abort ();
248         break;
249       case 'f':
250         /* Floating point has to be word or giantword.  */
251         if (osize == 'w' || osize == 'g')
252           val.size = osize;
253         else
254           /* Default it to giantword if the last used size is not
255              appropriate.  */
256           val.size = osize ? 'g' : osize;
257         break;
258       case 'c':
259         /* Characters default to one byte.  */
260         val.size = osize ? 'b' : osize;
261         break;
262       default:
263         /* The default is the size most recently specified.  */
264         val.size = osize;
265       }
266
267   return val;
268 }
269 \f
270 /* Print value VAL on gdb_stdout according to FORMAT, a letter or 0.
271    Do not end with a newline.
272    0 means print VAL according to its own type.
273    SIZE is the letter for the size of datum being printed.
274    This is used to pad hex numbers so they line up.  */
275
276 static void
277 print_formatted (val, format, size)
278      register value_ptr val;
279      register int format;
280      int size;
281 {
282   struct type *type = check_typedef (VALUE_TYPE (val));
283   int len = TYPE_LENGTH (type);
284
285   if (VALUE_LVAL (val) == lval_memory)
286     {
287       next_address = VALUE_ADDRESS (val) + len;
288       next_section = VALUE_BFD_SECTION (val);
289     }
290
291   switch (format)
292     {
293     case 's':
294       /* FIXME: Need to handle wchar_t's here... */
295       next_address = VALUE_ADDRESS (val)
296         + val_print_string (VALUE_ADDRESS (val), -1, 1, gdb_stdout);
297       next_section = VALUE_BFD_SECTION (val);
298       break;
299
300     case 'i':
301       /* The old comment says
302          "Force output out, print_insn not using _filtered".
303          I'm not completely sure what that means, I suspect most print_insn
304          now do use _filtered, so I guess it's obsolete.
305            --Yes, it does filter now, and so this is obsolete.  -JB  */
306
307       /* We often wrap here if there are long symbolic names.  */
308       wrap_here ("    ");
309       next_address = VALUE_ADDRESS (val)
310         + print_insn (VALUE_ADDRESS (val), gdb_stdout);
311       next_section = VALUE_BFD_SECTION (val);
312       break;
313
314     default:
315       if (format == 0
316           || TYPE_CODE (type) == TYPE_CODE_ARRAY
317           || TYPE_CODE (type) == TYPE_CODE_STRING
318           || TYPE_CODE (type) == TYPE_CODE_STRUCT
319           || TYPE_CODE (type) == TYPE_CODE_UNION)
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, gdb_stdout, 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, gdb_stdout);
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 (valaddr, type, format, size, stream)
344      char *valaddr;
345      struct type *type;
346      int format;
347      int size;
348      GDB_FILE *stream;
349 {
350   LONGEST val_long;
351   unsigned int len = TYPE_LENGTH (type);
352
353   if (len > sizeof (LONGEST)
354       && (format == 't'
355           || format == 'c'
356           || format == 'o'
357           || format == 'u'
358           || format == 'd'
359           || format == 'x'))
360     {
361       if (! TYPE_UNSIGNED (type)
362           || ! extract_long_unsigned_integer (valaddr, len, &val_long))
363         {
364           /* We can't print it normally, but we can print it in hex.
365              Printing it in the wrong radix is more useful than saying
366              "use /x, you dummy".  */
367           /* FIXME:  we could also do octal or binary if that was the
368              desired format.  */
369           /* FIXME:  we should be using the size field to give us a
370              minimum field width to print.  */
371
372           if( format == 'o' )
373               print_octal_chars (stream, valaddr, len);
374           else if( format == 'd' )
375               print_decimal_chars (stream, valaddr, len );
376           else if( format == 't' )
377               print_binary_chars (stream, valaddr, len);
378           else
379
380               /* replace with call to print_hex_chars? Looks
381                  like val_print_type_code_int is redoing
382                  work.  - edie */
383
384               val_print_type_code_int (type, valaddr, stream);
385
386           return;
387         }
388
389       /* If we get here, extract_long_unsigned_integer set val_long.  */
390     }
391   else if (format != 'f')
392     val_long = unpack_long (type, valaddr);
393
394   /* If we are printing it as unsigned, truncate it in case it is actually
395      a negative signed value (e.g. "print/u (short)-1" should print 65535
396      (if shorts are 16 bits) instead of 4294967295).  */
397   if (format != 'd')
398     {
399       if (len < sizeof (LONGEST))
400         val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
401     }
402
403   switch (format)
404     {
405     case 'x':
406       if (!size)
407         {
408           /* no size specified, like in print.  Print varying # of digits. */
409           print_longest (stream, 'x', 1, val_long);
410         }
411       else
412         switch (size)
413           {
414           case 'b':
415           case 'h':
416           case 'w':
417           case 'g':
418             print_longest (stream, size, 1, val_long);
419             break;
420           default:
421             error ("Undefined output size \"%c\".", size);
422           }
423       break;
424
425     case 'd':
426       print_longest (stream, 'd', 1, val_long);
427       break;
428
429     case 'u':
430       print_longest (stream, 'u', 0, val_long);
431       break;
432
433     case 'o':
434       if (val_long)
435         print_longest (stream, 'o', 1, val_long);
436       else
437         fprintf_filtered (stream, "0");
438       break;
439
440     case 'a':
441       print_address (unpack_pointer (type, valaddr), stream);
442       break;
443
444     case 'c':
445       value_print (value_from_longest (builtin_type_char, val_long), stream, 0,
446                    Val_pretty_default);
447       break;
448
449     case 'f':
450       if (len == sizeof (float))
451         type = builtin_type_float;
452       else if (len == sizeof (double))
453         type = builtin_type_double;
454       print_floating (valaddr, type, stream);
455       break;
456
457     case 0:
458       abort ();
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         fprintf_filtered (stream, buf);
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 (addr)
519      CORE_ADDR addr;
520 {
521   next_address = addr;
522
523   /* Make address available to the user as $_.  */
524   set_internalvar (lookup_internalvar ("_"),
525                    value_from_longest (lookup_pointer_type (builtin_type_void),
526                                     (LONGEST) addr));
527 }
528
529 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
530    after LEADIN.  Print nothing if no symbolic name is found nearby.
531    Optionally also print source file and line number, if available.
532    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
533    or to interpret it as a possible C++ name and convert it back to source
534    form.  However note that DO_DEMANGLE can be overridden by the specific
535    settings of the demangle and asm_demangle variables.  */
536
537 void
538 print_address_symbolic (addr, stream, do_demangle, leadin)
539      CORE_ADDR addr;
540      GDB_FILE *stream;
541      int do_demangle;
542      char *leadin;
543 {
544   struct minimal_symbol *msymbol;
545   struct symbol *symbol;
546   struct symtab *symtab = 0;
547   CORE_ADDR name_location = 0;
548   char *name = "";
549   asection *section = 0;
550   int unmapped = 0;
551
552   /* Determine if the address is in an overlay, and whether it is mapped. */
553   if (overlay_debugging)
554     {
555       section = find_pc_overlay (addr);
556       if (pc_in_unmapped_range (addr, section))
557         {
558           unmapped = 1;
559           addr = overlay_mapped_address (addr, section);
560         }
561     }
562
563   /* On some targets, add in extra "flag" bits to PC for
564      disassembly.  This should ensure that "rounding errors" in
565      symbol addresses that are masked for disassembly favour the
566      the correct symbol. */
567
568 #ifdef GDB_TARGET_UNMASK_DISAS_PC
569   addr = GDB_TARGET_UNMASK_DISAS_PC (addr);
570 #endif
571
572   /* First try to find the address in the symbol table, then
573      in the minsyms.  Take the closest one.  */
574
575   /* This is defective in the sense that it only finds text symbols.  So
576      really this is kind of pointless--we should make sure that the
577      minimal symbols have everything we need (by changing that we could
578      save some memory, but for many debug format--ELF/DWARF or
579      anything/stabs--it would be inconvenient to eliminate those minimal
580      symbols anyway).  */
581   msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
582   symbol = find_pc_sect_function (addr, section);
583
584   if (symbol)
585     {
586       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
587       if (do_demangle)
588         name = SYMBOL_SOURCE_NAME (symbol);
589       else
590         name = SYMBOL_LINKAGE_NAME (symbol);
591     }
592
593   if (msymbol != NULL)
594     {
595       if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
596         {
597           /* The msymbol is closer to the address than the symbol;
598              use the msymbol instead.  */
599           symbol = 0;
600           symtab = 0;
601           name_location = SYMBOL_VALUE_ADDRESS (msymbol);
602           if (do_demangle)
603             name = SYMBOL_SOURCE_NAME (msymbol);
604           else
605             name = SYMBOL_LINKAGE_NAME (msymbol);
606         }
607     }
608   if (symbol == NULL && msymbol == NULL)
609     return;
610
611   /* On some targets, mask out extra "flag" bits from PC for handsome
612      disassembly. */
613
614 #ifdef GDB_TARGET_MASK_DISAS_PC
615   name_location = GDB_TARGET_MASK_DISAS_PC (name_location);
616   addr = GDB_TARGET_MASK_DISAS_PC (addr);
617 #endif
618
619   /* If the nearest symbol is too far away, don't print anything symbolic.  */
620
621   /* For when CORE_ADDR is larger than unsigned int, we do math in
622      CORE_ADDR.  But when we detect unsigned wraparound in the
623      CORE_ADDR math, we ignore this test and print the offset,
624      because addr+max_symbolic_offset has wrapped through the end
625      of the address space back to the beginning, giving bogus comparison.  */
626   if (addr > name_location + max_symbolic_offset
627       && name_location + max_symbolic_offset > name_location)
628     return;
629
630   fputs_filtered (leadin, stream);
631   if (unmapped)
632     fputs_filtered ("<*", stream);
633   else
634     fputs_filtered ("<", stream);
635   fputs_filtered (name, stream);
636   if (addr != name_location)
637     fprintf_filtered (stream, "+%u", (unsigned int)(addr - name_location));
638
639   /* Append source filename and line number if desired.  Give specific
640      line # of this addr, if we have it; else line # of the nearest symbol.  */
641   if (print_symbol_filename)
642     {
643       struct symtab_and_line sal;
644
645       sal = find_pc_sect_line (addr, section, 0);
646
647       if (sal.symtab)
648         fprintf_filtered (stream, " at %s:%d", sal.symtab->filename, sal.line);
649       else if (symtab && symbol && symbol->line)
650         fprintf_filtered (stream, " at %s:%d", symtab->filename, symbol->line);
651       else if (symtab)
652         fprintf_filtered (stream, " in %s", symtab->filename);
653     }
654   if (unmapped)
655     fputs_filtered ("*>", stream);
656   else
657     fputs_filtered (">", stream);
658 }
659
660
661 /* Print address ADDR on STREAM.  USE_LOCAL means the same thing as for
662    print_longest.  */
663 void
664 print_address_numeric (addr, use_local, stream)
665      CORE_ADDR addr;
666      int use_local;
667      GDB_FILE *stream;
668 {
669   /* This assumes a CORE_ADDR can fit in a LONGEST.  Probably a safe
670      assumption.  */
671   print_longest (stream, 'x', use_local, (ULONGEST) addr);
672 }
673
674 /* Print address ADDR symbolically on STREAM.
675    First print it as a number.  Then perhaps print
676    <SYMBOL + OFFSET> after the number.  */
677
678 void
679 print_address (addr, stream)
680      CORE_ADDR addr;
681      GDB_FILE *stream;
682 {
683   print_address_numeric (addr, 1, stream);
684   print_address_symbolic (addr, stream, asm_demangle, " ");
685 }
686
687 /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
688    controls whether to print the symbolic name "raw" or demangled.
689    Global setting "addressprint" controls whether to print hex address
690    or not.  */
691
692 void
693 print_address_demangle (addr, stream, do_demangle)
694      CORE_ADDR addr;
695      GDB_FILE *stream;
696      int do_demangle;
697 {
698   if (addr == 0)
699     {
700       fprintf_filtered (stream, "0");
701     }
702   else if (addressprint)
703     {
704       print_address_numeric (addr, 1, stream);
705       print_address_symbolic (addr, stream, do_demangle, " ");
706     }
707   else
708     {
709       print_address_symbolic (addr, stream, do_demangle, "");
710     }
711 }
712 \f
713
714 /* These are the types that $__ will get after an examine command of one
715    of these sizes.  */
716
717 static struct type *examine_i_type;
718
719 static struct type *examine_b_type;
720 static struct type *examine_h_type;
721 static struct type *examine_w_type;
722 static struct type *examine_g_type;
723
724 /* Examine data at address ADDR in format FMT.
725    Fetch it from memory and print on gdb_stdout.  */
726
727 static void
728 do_examine (fmt, addr, sect)
729      struct format_data fmt;
730      CORE_ADDR addr;
731      asection *sect;
732 {
733   register char format = 0;
734   register char size;
735   register int count = 1;
736   struct type *val_type = NULL;
737   register int i;
738   register int maxelts;
739
740   format = fmt.format;
741   size = fmt.size;
742   count = fmt.count;
743   next_address = addr;
744   next_section = sect;
745
746   /* String or instruction format implies fetch single bytes
747      regardless of the specified size.  */
748   if (format == 's' || format == 'i')
749     size = 'b';
750
751   if (format == 'i')
752     val_type = examine_i_type;
753   else if (size == 'b')
754     val_type = examine_b_type;
755   else if (size == 'h')
756     val_type = examine_h_type;
757   else if (size == 'w')
758     val_type = examine_w_type;
759   else if (size == 'g')
760     val_type = examine_g_type;
761
762   maxelts = 8;
763   if (size == 'w')
764     maxelts = 4;
765   if (size == 'g')
766     maxelts = 2;
767   if (format == 's' || format == 'i')
768     maxelts = 1;
769
770   /* Print as many objects as specified in COUNT, at most maxelts per line,
771      with the address of the next one at the start of each line.  */
772
773   while (count > 0)
774     {
775       QUIT;
776       print_address (next_address, gdb_stdout);
777       printf_filtered (":");
778       for (i = maxelts;
779            i > 0 && count > 0;
780            i--, count--)
781         {
782           printf_filtered ("\t");
783           /* Note that print_formatted sets next_address for the next
784              object.  */
785           last_examine_address = next_address;
786
787           if (last_examine_value)
788             value_free (last_examine_value);
789
790           /* The value to be displayed is not fetched greedily.
791              Instead, to avoid the posibility of a fetched value not
792              being used, its retreval is delayed until the print code
793              uses it.  When examining an instruction stream, the
794              disassembler will perform its own memory fetch using just
795              the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
796              the disassembler be modified so that LAST_EXAMINE_VALUE
797              is left with the byte sequence from the last complete
798              instruction fetched from memory? */
799           last_examine_value = value_at_lazy (val_type, next_address, sect);
800
801           if (last_examine_value)
802             release_value (last_examine_value);
803
804           print_formatted (last_examine_value, format, size);
805         }
806       printf_filtered ("\n");
807       gdb_flush (gdb_stdout);
808     }
809 }
810 \f
811 static void
812 validate_format (fmt, cmdname)
813      struct format_data fmt;
814      char *cmdname;
815 {
816   if (fmt.size != 0)
817     error ("Size letters are meaningless in \"%s\" command.", cmdname);
818   if (fmt.count != 1)
819     error ("Item count other than 1 is meaningless in \"%s\" command.",
820            cmdname);
821   if (fmt.format == 'i' || fmt.format == 's')
822     error ("Format letter \"%c\" is meaningless in \"%s\" command.",
823            fmt.format, cmdname);
824 }
825
826 /*  Evaluate string EXP as an expression in the current language and
827     print the resulting value.  EXP may contain a format specifier as the
828     first argument ("/x myvar" for example, to print myvar in hex).
829     */
830
831 static void
832 print_command_1 (exp, inspect, voidprint)
833      char *exp;
834      int inspect;
835      int voidprint;
836 {
837   struct expression *expr;
838   register struct cleanup *old_chain = 0;
839   register char format = 0;
840   register value_ptr val;
841   struct format_data fmt;
842   int cleanup = 0;
843
844   /* Pass inspect flag to the rest of the print routines in a global (sigh). */
845   inspect_it = inspect;
846
847   if (exp && *exp == '/')
848     {
849       exp++;
850       fmt = decode_format (&exp, last_format, 0);
851       validate_format (fmt, "print");
852       last_format = format = fmt.format;
853     }
854   else
855     {
856       fmt.count = 1;
857       fmt.format = 0;
858       fmt.size = 0;
859     }
860
861   if (exp && *exp)
862     {
863       extern int objectprint;
864       struct type *type;
865       expr = parse_expression (exp);
866       old_chain = make_cleanup ((make_cleanup_func) free_current_contents, 
867                                 &expr);
868       cleanup = 1;
869       val = evaluate_expression (expr);
870
871       /* C++: figure out what type we actually want to print it as.  */
872       type = VALUE_TYPE (val);
873
874       if (objectprint
875           && (   TYPE_CODE (type) == TYPE_CODE_PTR
876               || TYPE_CODE (type) == TYPE_CODE_REF)
877           && (   TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT
878               || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_UNION))
879         {
880           value_ptr v;
881
882           v = value_from_vtable_info (val, TYPE_TARGET_TYPE (type));
883           if (v != 0)
884             {
885               val = v;
886               type = VALUE_TYPE (val);
887             }
888         }
889     }
890   else
891     val = access_value_history (0);
892
893   if (voidprint || (val && VALUE_TYPE (val) &&
894                     TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_VOID))
895     {
896       int histindex = record_latest_value (val);
897
898       if (histindex >= 0)
899         annotate_value_history_begin (histindex, VALUE_TYPE (val));
900       else
901         annotate_value_begin (VALUE_TYPE (val));
902
903       if (inspect)
904         printf_unfiltered ("\031(gdb-makebuffer \"%s\"  %d '(\"", exp, histindex);
905       else
906         if (histindex >= 0) printf_filtered ("$%d = ", histindex);
907
908       if (histindex >= 0)
909         annotate_value_history_value ();
910
911       print_formatted (val, format, fmt.size);
912       printf_filtered ("\n");
913
914       if (histindex >= 0)
915         annotate_value_history_end ();
916       else
917         annotate_value_end ();
918
919       if (inspect)
920         printf_unfiltered("\") )\030");
921     }
922
923   if (cleanup)
924     do_cleanups (old_chain);
925   inspect_it = 0;       /* Reset print routines to normal */
926 }
927
928 /* ARGSUSED */
929 static void
930 print_command (exp, from_tty)
931      char *exp;
932      int from_tty;
933 {
934   print_command_1 (exp, 0, 1);
935 }
936
937 /* Same as print, except in epoch, it gets its own window */
938 /* ARGSUSED */
939 static void
940 inspect_command (exp, from_tty)
941      char *exp;
942      int from_tty;
943 {
944   extern int epoch_interface;
945
946   print_command_1 (exp, epoch_interface, 1);
947 }
948
949 /* Same as print, except it doesn't print void results. */
950 /* ARGSUSED */
951 static void
952 call_command (exp, from_tty)
953      char *exp;
954      int from_tty;
955 {
956   print_command_1 (exp, 0, 0);
957 }
958
959 /* ARGSUSED */
960 void
961 output_command (exp, from_tty)
962      char *exp;
963      int from_tty;
964 {
965   struct expression *expr;
966   register struct cleanup *old_chain;
967   register char format = 0;
968   register value_ptr val;
969   struct format_data fmt;
970
971   if (exp && *exp == '/')
972     {
973       exp++;
974       fmt = decode_format (&exp, 0, 0);
975       validate_format (fmt, "output");
976       format = fmt.format;
977     }
978
979   expr = parse_expression (exp);
980   old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
981
982   val = evaluate_expression (expr);
983
984   annotate_value_begin (VALUE_TYPE (val));
985
986   print_formatted (val, format, fmt.size);
987
988   annotate_value_end ();
989
990   do_cleanups (old_chain);
991 }
992
993 /* ARGSUSED */
994 static void
995 set_command (exp, from_tty)
996      char *exp;
997      int from_tty;
998 {
999   struct expression *expr = parse_expression (exp);
1000   register struct cleanup *old_chain
1001     = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1002   evaluate_expression (expr);
1003   do_cleanups (old_chain);
1004 }
1005
1006 /* ARGSUSED */
1007 static void
1008 sym_info (arg, from_tty)
1009      char *arg;
1010      int   from_tty;
1011 {
1012   struct minimal_symbol *msymbol;
1013   struct objfile        *objfile;
1014   struct obj_section    *osect;
1015   asection              *sect;
1016   CORE_ADDR              addr, sect_addr;
1017   int                    matches = 0;
1018   unsigned int           offset;
1019
1020   if (!arg)
1021     error_no_arg ("address");
1022
1023   addr = parse_and_eval_address (arg);
1024   ALL_OBJSECTIONS (objfile, osect)
1025     {
1026       sect = osect->the_bfd_section;
1027       sect_addr = overlay_mapped_address (addr, sect);
1028
1029       if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
1030           (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
1031         {
1032           matches = 1;
1033           offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1034           if (offset)
1035             printf_filtered ("%s + %u in ", 
1036                              SYMBOL_SOURCE_NAME (msymbol), offset);
1037           else
1038             printf_filtered ("%s in ", 
1039                              SYMBOL_SOURCE_NAME (msymbol));
1040           if (pc_in_unmapped_range (addr, sect))
1041             printf_filtered ("load address range of ");
1042           if (section_is_overlay (sect))
1043             printf_filtered ("%s overlay ", 
1044                              section_is_mapped (sect) ? "mapped" : "unmapped");
1045           printf_filtered ("section %s", sect->name);
1046           printf_filtered ("\n");
1047         }
1048     }
1049   if (matches == 0)
1050     printf_filtered ("No symbol matches %s.\n", arg);
1051 }
1052
1053 /* ARGSUSED */
1054 static void
1055 address_info (exp, from_tty)
1056      char *exp;
1057      int from_tty;
1058 {
1059   register struct symbol *sym;
1060   register struct minimal_symbol *msymbol;
1061   register long val;
1062   register long basereg;
1063   asection *section;
1064   CORE_ADDR load_addr;
1065   int is_a_field_of_this;       /* C++: lookup_symbol sets this to nonzero
1066                                    if exp is a field of `this'. */
1067
1068   if (exp == 0)
1069     error ("Argument required.");
1070
1071   sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE, 
1072                        &is_a_field_of_this, (struct symtab **)NULL);
1073   if (sym == NULL)
1074     {
1075       if (is_a_field_of_this)
1076         {
1077           printf_filtered ("Symbol \"");
1078           fprintf_symbol_filtered (gdb_stdout, exp,
1079                                    current_language->la_language, DMGL_ANSI);
1080           printf_filtered ("\" is a field of the local class variable `this'\n");
1081           return;
1082         }
1083
1084       msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1085
1086       if (msymbol != NULL)
1087         {
1088           load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1089
1090           printf_filtered ("Symbol \"");
1091           fprintf_symbol_filtered (gdb_stdout, exp,
1092                                    current_language->la_language, DMGL_ANSI);
1093           printf_filtered ("\" is at ");
1094           print_address_numeric (load_addr, 1, gdb_stdout);
1095           printf_filtered (" in a file compiled without debugging");
1096           section = SYMBOL_BFD_SECTION (msymbol);
1097           if (section_is_overlay (section))
1098             {
1099               load_addr = overlay_unmapped_address (load_addr, section);
1100               printf_filtered (",\n -- loaded at ");
1101               print_address_numeric (load_addr, 1, gdb_stdout);
1102               printf_filtered (" in overlay section %s", section->name);
1103             }
1104           printf_filtered (".\n");
1105         }
1106       else
1107         error ("No symbol \"%s\" in current context.", exp);
1108       return;
1109     }
1110
1111   printf_filtered ("Symbol \"");
1112   fprintf_symbol_filtered (gdb_stdout, SYMBOL_NAME (sym),
1113                            current_language->la_language, DMGL_ANSI);
1114   printf_filtered ("\" is ");
1115   val     = SYMBOL_VALUE (sym);
1116   basereg = SYMBOL_BASEREG (sym);
1117   section = SYMBOL_BFD_SECTION (sym);
1118
1119   switch (SYMBOL_CLASS (sym))
1120     {
1121     case LOC_CONST:
1122     case LOC_CONST_BYTES:
1123       printf_filtered ("constant");
1124       break;
1125
1126     case LOC_LABEL:
1127       printf_filtered ("a label at address ");
1128       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym), 
1129                              1, gdb_stdout);
1130       if (section_is_overlay (section))
1131         {
1132           load_addr = overlay_unmapped_address (load_addr, section);
1133           printf_filtered (",\n -- loaded at ");
1134           print_address_numeric (load_addr, 1, gdb_stdout);
1135           printf_filtered (" in overlay section %s", section->name);
1136         }
1137       break;
1138
1139     case LOC_REGISTER:
1140       printf_filtered ("a variable in register %s", REGISTER_NAME (val));
1141       break;
1142
1143     case LOC_STATIC:
1144       printf_filtered ("static storage at address ");
1145       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym), 
1146                              1, gdb_stdout);
1147       if (section_is_overlay (section))
1148         {
1149           load_addr = overlay_unmapped_address (load_addr, section);
1150           printf_filtered (",\n -- loaded at ");
1151           print_address_numeric (load_addr, 1, gdb_stdout);
1152           printf_filtered (" in overlay section %s", section->name);
1153         }
1154       break;
1155
1156     case LOC_INDIRECT:
1157       printf_filtered ("external global (indirect addressing), at address *(");
1158       print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1159                              1, gdb_stdout);
1160       printf_filtered (")");
1161       if (section_is_overlay (section))
1162         {
1163           load_addr = overlay_unmapped_address (load_addr, section);
1164           printf_filtered (",\n -- loaded at ");
1165           print_address_numeric (load_addr, 1, gdb_stdout);
1166           printf_filtered (" in overlay section %s", section->name);
1167         }
1168       break;
1169
1170     case LOC_REGPARM:
1171       printf_filtered ("an argument in register %s", REGISTER_NAME (val));
1172       break;
1173
1174     case LOC_REGPARM_ADDR:
1175       printf_filtered ("address of an argument in register %s", REGISTER_NAME (val));
1176       break;
1177
1178     case LOC_ARG:
1179       printf_filtered ("an argument at offset %ld", val);
1180       break;
1181
1182     case LOC_LOCAL_ARG:
1183       printf_filtered ("an argument at frame offset %ld", val);
1184       break;
1185
1186     case LOC_LOCAL:
1187       printf_filtered ("a local variable at frame offset %ld", val);
1188       break;
1189
1190     case LOC_REF_ARG:
1191       printf_filtered ("a reference argument at offset %ld", val);
1192       break;
1193
1194     case LOC_BASEREG:
1195       printf_filtered ("a variable at offset %ld from register %s",
1196               val, REGISTER_NAME (basereg));
1197       break;
1198
1199     case LOC_BASEREG_ARG:
1200       printf_filtered ("an argument at offset %ld from register %s",
1201               val, REGISTER_NAME (basereg));
1202       break;
1203
1204     case LOC_TYPEDEF:
1205       printf_filtered ("a typedef");
1206       break;
1207
1208     case LOC_BLOCK:
1209       printf_filtered ("a function at address ");
1210 #ifdef GDB_TARGET_MASK_DISAS_PC
1211       print_address_numeric
1212         (load_addr= GDB_TARGET_MASK_DISAS_PC (BLOCK_START (SYMBOL_BLOCK_VALUE (sym))),
1213          1, gdb_stdout);
1214 #else
1215       print_address_numeric (load_addr=BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
1216                              1, gdb_stdout);
1217 #endif
1218       if (section_is_overlay (section))
1219         {
1220           load_addr = overlay_unmapped_address (load_addr, section);
1221           printf_filtered (",\n -- loaded at ");
1222           print_address_numeric (load_addr, 1, gdb_stdout);
1223           printf_filtered (" in overlay section %s", section->name);
1224         }
1225       break;
1226
1227     case LOC_UNRESOLVED:
1228       {
1229         struct minimal_symbol *msym;
1230
1231         msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, NULL);
1232         if (msym == NULL)
1233           printf_filtered ("unresolved");
1234         else
1235           {
1236             section = SYMBOL_BFD_SECTION (msym);
1237             printf_filtered ("static storage at address ");
1238             print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (msym), 
1239                                    1, gdb_stdout);
1240             if (section_is_overlay (section))
1241               {
1242                 load_addr = overlay_unmapped_address (load_addr, section);
1243                 printf_filtered (",\n -- loaded at ");
1244                 print_address_numeric (load_addr, 1, gdb_stdout);
1245                 printf_filtered (" in overlay section %s", section->name);
1246               }
1247           }
1248       }
1249       break;
1250
1251     case LOC_THREAD_LOCAL_STATIC:
1252       printf_filtered (
1253               "a thread-local variable at offset %ld from the thread base register %s",
1254               val, REGISTER_NAME (basereg));
1255       break;
1256
1257     case LOC_OPTIMIZED_OUT:
1258       printf_filtered ("optimized out");
1259       break;
1260       
1261     default:
1262       printf_filtered ("of unknown (botched) type");
1263       break;
1264     }
1265   printf_filtered (".\n");
1266 }
1267 \f
1268 void
1269 x_command (exp, from_tty)
1270      char *exp;
1271      int from_tty;
1272 {
1273   struct expression *expr;
1274   struct format_data fmt;
1275   struct cleanup *old_chain;
1276   struct value *val;
1277
1278   fmt.format = last_format;
1279   fmt.size = last_size;
1280   fmt.count = 1;
1281
1282   if (exp && *exp == '/')
1283     {
1284       exp++;
1285       fmt = decode_format (&exp, last_format, last_size);
1286     }
1287
1288   /* If we have an expression, evaluate it and use it as the address.  */
1289
1290   if (exp != 0 && *exp != 0)
1291     {
1292       expr = parse_expression (exp);
1293       /* Cause expression not to be there any more
1294          if this command is repeated with Newline.
1295          But don't clobber a user-defined command's definition.  */
1296       if (from_tty)
1297         *exp = 0;
1298       old_chain = make_cleanup ((make_cleanup_func) free_current_contents, 
1299                                 &expr);
1300       val = evaluate_expression (expr);
1301       if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF)
1302         val = value_ind (val);
1303       /* In rvalue contexts, such as this, functions are coerced into
1304          pointers to functions.  This makes "x/i main" work.  */
1305       if (/* last_format == 'i'
1306           && */ TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
1307           && VALUE_LVAL (val) == lval_memory)
1308         next_address = VALUE_ADDRESS (val);
1309       else
1310         next_address = value_as_pointer (val);
1311       if (VALUE_BFD_SECTION (val))
1312         next_section = VALUE_BFD_SECTION (val);
1313       do_cleanups (old_chain);
1314     }
1315
1316   do_examine (fmt, next_address, next_section);
1317
1318   /* If the examine succeeds, we remember its size and format for next time.  */
1319   last_size = fmt.size;
1320   last_format = fmt.format;
1321
1322   /* Set a couple of internal variables if appropriate. */
1323   if (last_examine_value)
1324     {
1325       /* Make last address examined available to the user as $_.  Use
1326          the correct pointer type.  */
1327       set_internalvar (lookup_internalvar ("_"),
1328                value_from_longest (
1329                  lookup_pointer_type (VALUE_TYPE (last_examine_value)),
1330                                    (LONGEST) last_examine_address));
1331       
1332       /* Make contents of last address examined available to the user as $__.*/
1333       /* If the last value has not been fetched from memory then don't
1334          fetch it now - instead mark it by voiding the $__ variable. */
1335       if (VALUE_LAZY (last_examine_value))
1336         set_internalvar (lookup_internalvar ("__"),
1337                          allocate_value (builtin_type_void));
1338       else
1339         set_internalvar (lookup_internalvar ("__"), last_examine_value);
1340     }
1341 }
1342
1343 \f
1344 /* Add an expression to the auto-display chain.
1345    Specify the expression.  */
1346
1347 static void
1348 display_command (exp, from_tty)
1349      char *exp;
1350      int from_tty;
1351 {
1352   struct format_data fmt;
1353   register struct expression *expr;
1354   register struct display *new;
1355   int display_it = 1;
1356
1357 #if defined(TUI)
1358   if (tui_version && *exp == '$')
1359     display_it = ((TuiStatus)tuiDo(
1360                   (TuiOpaqueFuncPtr)tui_vSetLayoutTo, exp) == TUI_FAILURE);
1361 #endif
1362
1363   if (display_it)
1364     {
1365       if (exp == 0)
1366         {
1367           do_displays ();
1368           return;
1369         }
1370
1371       if (*exp == '/')
1372         {
1373           exp++;
1374           fmt = decode_format (&exp, 0, 0);
1375           if (fmt.size && fmt.format == 0)
1376             fmt.format = 'x';
1377           if (fmt.format == 'i' || fmt.format == 's')
1378             fmt.size = 'b';
1379         }
1380       else
1381         {
1382           fmt.format = 0;
1383           fmt.size = 0;
1384           fmt.count = 0;
1385         }
1386
1387       innermost_block = 0;
1388       expr = parse_expression (exp);
1389
1390       new = (struct display *) xmalloc (sizeof (struct display));
1391
1392       new->exp = expr;
1393       new->block = innermost_block;
1394       new->next = display_chain;
1395       new->number = ++display_number;
1396       new->format = fmt;
1397       new->status = enabled;
1398       display_chain = new;
1399
1400       if (from_tty && target_has_execution)
1401         do_one_display (new);
1402
1403       dont_repeat ();
1404     }
1405 }
1406
1407 static void
1408 free_display (d)
1409      struct display *d;
1410 {
1411   free ((PTR)d->exp);
1412   free ((PTR)d);
1413 }
1414
1415 /* Clear out the display_chain.
1416    Done when new symtabs are loaded, since this invalidates
1417    the types stored in many expressions.  */
1418
1419 void
1420 clear_displays ()
1421 {
1422   register struct display *d;
1423
1424   while ((d = display_chain) != NULL)
1425     {
1426       free ((PTR)d->exp);
1427       display_chain = d->next;
1428       free ((PTR)d);
1429     }
1430 }
1431
1432 /* Delete the auto-display number NUM.  */
1433
1434 static void
1435 delete_display (num)
1436      int num;
1437 {
1438   register struct display *d1, *d;
1439
1440   if (!display_chain)
1441     error ("No display number %d.", num);
1442
1443   if (display_chain->number == num)
1444     {
1445       d1 = display_chain;
1446       display_chain = d1->next;
1447       free_display (d1);
1448     }
1449   else
1450     for (d = display_chain; ; d = d->next)
1451       {
1452         if (d->next == 0)
1453           error ("No display number %d.", num);
1454         if (d->next->number == num)
1455           {
1456             d1 = d->next;
1457             d->next = d1->next;
1458             free_display (d1);
1459             break;
1460           }
1461       }
1462 }
1463
1464 /* Delete some values from the auto-display chain.
1465    Specify the element numbers.  */
1466
1467 static void
1468 undisplay_command (args, from_tty)
1469      char *args;
1470      int from_tty;
1471 {
1472   register char *p = args;
1473   register char *p1;
1474   register int num;
1475
1476   if (args == 0)
1477     {
1478       if (query ("Delete all auto-display expressions? "))
1479         clear_displays ();
1480       dont_repeat ();
1481       return;
1482     }
1483
1484   while (*p)
1485     {
1486       p1 = p;
1487       while (*p1 >= '0' && *p1 <= '9') p1++;
1488       if (*p1 && *p1 != ' ' && *p1 != '\t')
1489         error ("Arguments must be display numbers.");
1490
1491       num = atoi (p);
1492
1493       delete_display (num);
1494
1495       p = p1;
1496       while (*p == ' ' || *p == '\t') p++;
1497     }
1498   dont_repeat ();
1499 }
1500
1501 /* Display a single auto-display.  
1502    Do nothing if the display cannot be printed in the current context,
1503    or if the display is disabled. */
1504
1505 static void
1506 do_one_display (d)
1507      struct display *d;
1508 {
1509   int within_current_scope;
1510
1511   if (d->status == disabled)
1512     return;
1513
1514   if (d->block)
1515     within_current_scope = contained_in (get_selected_block (), d->block);
1516   else
1517     within_current_scope = 1;
1518   if (!within_current_scope)
1519     return;
1520
1521   current_display_number = d->number;
1522
1523   annotate_display_begin ();
1524   printf_filtered ("%d", d->number);
1525   annotate_display_number_end ();
1526   printf_filtered (": ");
1527   if (d->format.size)
1528     {
1529       CORE_ADDR addr;
1530       value_ptr val;
1531
1532       annotate_display_format ();
1533
1534       printf_filtered ("x/");
1535       if (d->format.count != 1)
1536         printf_filtered ("%d", d->format.count);
1537       printf_filtered ("%c", d->format.format);
1538       if (d->format.format != 'i' && d->format.format != 's')
1539         printf_filtered ("%c", d->format.size);
1540       printf_filtered (" ");
1541
1542       annotate_display_expression ();
1543
1544       print_expression (d->exp, gdb_stdout);
1545       annotate_display_expression_end ();
1546
1547       if (d->format.count != 1)
1548         printf_filtered ("\n");
1549       else
1550         printf_filtered ("  ");
1551       
1552       val = evaluate_expression (d->exp);
1553       addr = value_as_pointer (val);
1554       if (d->format.format == 'i')
1555         addr = ADDR_BITS_REMOVE (addr);
1556
1557       annotate_display_value ();
1558
1559       do_examine (d->format, addr, VALUE_BFD_SECTION (val));
1560     }
1561   else
1562     {
1563       annotate_display_format ();
1564
1565       if (d->format.format)
1566         printf_filtered ("/%c ", d->format.format);
1567
1568       annotate_display_expression ();
1569
1570       print_expression (d->exp, gdb_stdout);
1571       annotate_display_expression_end ();
1572
1573       printf_filtered (" = ");
1574
1575       annotate_display_expression ();
1576
1577       print_formatted (evaluate_expression (d->exp),
1578                        d->format.format, d->format.size);
1579       printf_filtered ("\n");
1580     }
1581
1582   annotate_display_end ();
1583
1584   gdb_flush (gdb_stdout);
1585   current_display_number = -1;
1586 }
1587
1588 /* Display all of the values on the auto-display chain which can be
1589    evaluated in the current scope.  */
1590
1591 void
1592 do_displays ()
1593 {
1594   register struct display *d;
1595
1596   for (d = display_chain; d; d = d->next)
1597     do_one_display (d);
1598 }
1599
1600 /* Delete the auto-display which we were in the process of displaying.
1601    This is done when there is an error or a signal.  */
1602
1603 void
1604 disable_display (num)
1605      int num;
1606 {
1607   register struct display *d;
1608
1609   for (d = display_chain; d; d = d->next)
1610     if (d->number == num)
1611       {
1612         d->status = disabled;
1613         return;
1614       }
1615   printf_unfiltered ("No display number %d.\n", num);
1616 }
1617   
1618 void
1619 disable_current_display ()
1620 {
1621   if (current_display_number >= 0)
1622     {
1623       disable_display (current_display_number);
1624       fprintf_unfiltered (gdb_stderr, "Disabling display %d to avoid infinite recursion.\n",
1625                current_display_number);
1626     }
1627   current_display_number = -1;
1628 }
1629
1630 static void
1631 display_info (ignore, from_tty)
1632      char *ignore;
1633      int from_tty;
1634 {
1635   register 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->status]);
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 (), 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 (args, from_tty)
1661      char *args;
1662      int from_tty;
1663 {
1664   register char *p = args;
1665   register char *p1;
1666   register int num;
1667   register struct display *d;
1668
1669   if (p == 0)
1670     {
1671       for (d = display_chain; d; d = d->next)
1672         d->status = enabled;
1673     }
1674   else
1675     while (*p)
1676       {
1677         p1 = p;
1678         while (*p1 >= '0' && *p1 <= '9')
1679           p1++;
1680         if (*p1 && *p1 != ' ' && *p1 != '\t')
1681           error ("Arguments must be display numbers.");
1682         
1683         num = atoi (p);
1684         
1685         for (d = display_chain; d; d = d->next)
1686           if (d->number == num)
1687             {
1688               d->status = enabled;
1689               goto win;
1690             }
1691         printf_unfiltered ("No display number %d.\n", num);
1692       win:
1693         p = p1;
1694         while (*p == ' ' || *p == '\t')
1695           p++;
1696       }
1697 }
1698
1699 /* ARGSUSED */
1700 static void
1701 disable_display_command (args, from_tty)
1702      char *args;
1703      int from_tty;
1704 {
1705   register char *p = args;
1706   register char *p1;
1707   register struct display *d;
1708
1709   if (p == 0)
1710     {
1711       for (d = display_chain; d; d = d->next)
1712         d->status = disabled;
1713     }
1714   else
1715     while (*p)
1716       {
1717         p1 = p;
1718         while (*p1 >= '0' && *p1 <= '9')
1719           p1++;
1720         if (*p1 && *p1 != ' ' && *p1 != '\t')
1721           error ("Arguments must be display numbers.");
1722         
1723         disable_display (atoi (p));
1724
1725         p = p1;
1726         while (*p == ' ' || *p == '\t')
1727           p++;
1728       }
1729 }
1730
1731 \f
1732 /* Print the value in stack frame FRAME of a variable
1733    specified by a struct symbol.  */
1734
1735 void
1736 print_variable_value (var, frame, stream)
1737      struct symbol *var;
1738      struct frame_info *frame;
1739      GDB_FILE *stream;
1740 {
1741   value_ptr val = read_var_value (var, frame);
1742
1743   value_print (val, stream, 0, Val_pretty_default);
1744 }
1745
1746 /* Print the arguments of a stack frame, given the function FUNC
1747    running in that frame (as a symbol), the info on the frame,
1748    and the number of args according to the stack frame (or -1 if unknown).  */
1749
1750 /* References here and elsewhere to "number of args according to the
1751    stack frame" appear in all cases to refer to "number of ints of args
1752    according to the stack frame".  At least for VAX, i386, isi.  */
1753
1754 void
1755 print_frame_args (func, fi, num, stream)
1756      struct symbol *func;
1757      struct frame_info *fi;
1758      int num;
1759      GDB_FILE *stream;
1760 {
1761   struct block *b = NULL;
1762   int nsyms = 0;
1763   int first = 1;
1764   register int i;
1765   register struct symbol *sym;
1766   register value_ptr val;
1767   /* Offset of next stack argument beyond the one we have seen that is
1768      at the highest offset.
1769      -1 if we haven't come to a stack argument yet.  */
1770   long highest_offset = -1;
1771   int arg_size;
1772   /* Number of ints of arguments that we have printed so far.  */
1773   int args_printed = 0;
1774
1775   if (func)
1776     {
1777       b = SYMBOL_BLOCK_VALUE (func);
1778       nsyms = BLOCK_NSYMS (b);
1779     }
1780
1781   for (i = 0; i < nsyms; i++)
1782     {
1783       QUIT;
1784       sym = BLOCK_SYM (b, i);
1785
1786       /* Keep track of the highest stack argument offset seen, and
1787          skip over any kinds of symbols we don't care about.  */
1788
1789       switch (SYMBOL_CLASS (sym)) {
1790       case LOC_ARG:
1791       case LOC_REF_ARG:
1792         {
1793           long current_offset = SYMBOL_VALUE (sym);
1794           arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
1795           
1796           /* Compute address of next argument by adding the size of
1797              this argument and rounding to an int boundary.  */
1798           current_offset
1799             = ((current_offset + arg_size + sizeof (int) - 1)
1800                & ~(sizeof (int) - 1));
1801
1802           /* If this is the highest offset seen yet, set highest_offset.  */
1803           if (highest_offset == -1
1804               || (current_offset > highest_offset))
1805             highest_offset = current_offset;
1806
1807           /* Add the number of ints we're about to print to args_printed.  */
1808           args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
1809         }
1810
1811       /* We care about types of symbols, but don't need to keep track of
1812          stack offsets in them.  */
1813       case LOC_REGPARM:
1814       case LOC_REGPARM_ADDR:
1815       case LOC_LOCAL_ARG:
1816       case LOC_BASEREG_ARG:
1817         break;
1818
1819       /* Other types of symbols we just skip over.  */
1820       default:
1821         continue;
1822       }
1823
1824       /* We have to look up the symbol because arguments can have
1825          two entries (one a parameter, one a local) and the one we
1826          want is the local, which lookup_symbol will find for us.
1827          This includes gcc1 (not gcc2) on the sparc when passing a
1828          small structure and gcc2 when the argument type is float
1829          and it is passed as a double and converted to float by
1830          the prologue (in the latter case the type of the LOC_ARG
1831          symbol is double and the type of the LOC_LOCAL symbol is
1832          float).  */
1833       /* But if the parameter name is null, don't try it.
1834          Null parameter names occur on the RS/6000, for traceback tables.
1835          FIXME, should we even print them?  */
1836
1837       if (*SYMBOL_NAME (sym))
1838         {
1839           struct symbol *nsym;
1840           nsym = lookup_symbol
1841             (SYMBOL_NAME (sym),
1842              b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
1843           if (SYMBOL_CLASS (nsym) == LOC_REGISTER)
1844             {
1845               /* There is a LOC_ARG/LOC_REGISTER pair.  This means that
1846                  it was passed on the stack and loaded into a register,
1847                  or passed in a register and stored in a stack slot.
1848                  GDB 3.x used the LOC_ARG; GDB 4.0-4.11 used the LOC_REGISTER.
1849
1850                  Reasons for using the LOC_ARG:
1851                  (1) because find_saved_registers may be slow for remote
1852                  debugging,
1853                  (2) because registers are often re-used and stack slots
1854                  rarely (never?) are.  Therefore using the stack slot is
1855                  much less likely to print garbage.
1856
1857                  Reasons why we might want to use the LOC_REGISTER:
1858                  (1) So that the backtrace prints the same value as
1859                  "print foo".  I see no compelling reason why this needs
1860                  to be the case; having the backtrace print the value which
1861                  was passed in, and "print foo" print the value as modified
1862                  within the called function, makes perfect sense to me.
1863
1864                  Additional note:  It might be nice if "info args" displayed
1865                  both values.
1866                  One more note:  There is a case with sparc structure passing
1867                  where we need to use the LOC_REGISTER, but this is dealt with
1868                  by creating a single LOC_REGPARM in symbol reading.  */
1869
1870               /* Leave sym (the LOC_ARG) alone.  */
1871               ;
1872             }
1873           else
1874             sym = nsym;
1875         }
1876
1877       /* Print the current arg.  */
1878       if (! first)
1879         fprintf_filtered (stream, ", ");
1880       wrap_here ("    ");
1881
1882       annotate_arg_begin ();
1883
1884       fprintf_symbol_filtered (stream, SYMBOL_SOURCE_NAME (sym),
1885                                SYMBOL_LANGUAGE (sym), DMGL_PARAMS | DMGL_ANSI);
1886       annotate_arg_name_end ();
1887       fputs_filtered ("=", stream);
1888
1889       /* Avoid value_print because it will deref ref parameters.  We just
1890          want to print their addresses.  Print ??? for args whose address
1891          we do not know.  We pass 2 as "recurse" to val_print because our
1892          standard indentation here is 4 spaces, and val_print indents
1893          2 for each recurse.  */
1894       val = read_var_value (sym, fi);
1895
1896       annotate_arg_value (val == NULL ? NULL : VALUE_TYPE (val));
1897
1898       if (val)
1899         {
1900 #ifdef GDB_TARGET_IS_D10V
1901           if (SYMBOL_CLASS(sym) == LOC_REGPARM && TYPE_CODE(VALUE_TYPE(val)) == TYPE_CODE_PTR)
1902             TYPE_LENGTH(VALUE_TYPE(val)) = 2;
1903 #endif
1904           val_print (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
1905                      VALUE_ADDRESS (val),
1906                    stream, 0, 0, 2, Val_no_prettyprint);
1907         }
1908       else
1909         fputs_filtered ("???", stream);
1910
1911       annotate_arg_end ();
1912
1913       first = 0;
1914     }
1915
1916   /* Don't print nameless args in situations where we don't know
1917      enough about the stack to find them.  */
1918   if (num != -1)
1919     {
1920       long start;
1921
1922       if (highest_offset == -1)
1923         start = FRAME_ARGS_SKIP;
1924       else
1925         start = highest_offset;
1926
1927       print_frame_nameless_args (fi, start, num - args_printed,
1928                                  first, stream);
1929     }
1930 }
1931
1932 /* Print nameless args on STREAM.
1933    FI is the frameinfo for this frame, START is the offset
1934    of the first nameless arg, and NUM is the number of nameless args to
1935    print.  FIRST is nonzero if this is the first argument (not just
1936    the first nameless arg).  */
1937
1938 static void
1939 print_frame_nameless_args (fi, start, num, first, stream)
1940      struct frame_info *fi;
1941      long start;
1942      int num;
1943      int first;
1944      GDB_FILE *stream;
1945 {
1946   int i;
1947   CORE_ADDR argsaddr;
1948   long arg_value;
1949
1950   for (i = 0; i < num; i++)
1951     {
1952       QUIT;
1953 #ifdef NAMELESS_ARG_VALUE
1954       NAMELESS_ARG_VALUE (fi, start, &arg_value);
1955 #else
1956       argsaddr = FRAME_ARGS_ADDRESS (fi);
1957       if (!argsaddr)
1958         return;
1959
1960       arg_value = read_memory_integer (argsaddr + start, sizeof (int));
1961 #endif
1962
1963       if (!first)
1964         fprintf_filtered (stream, ", ");
1965
1966 #ifdef  PRINT_NAMELESS_INTEGER
1967       PRINT_NAMELESS_INTEGER (stream, arg_value);
1968 #else
1969 #ifdef PRINT_TYPELESS_INTEGER
1970       PRINT_TYPELESS_INTEGER (stream, builtin_type_int, (LONGEST) arg_value);
1971 #else
1972       fprintf_filtered (stream, "%ld", arg_value);
1973 #endif /* PRINT_TYPELESS_INTEGER */
1974 #endif /* PRINT_NAMELESS_INTEGER */
1975       first = 0;
1976       start += sizeof (int);
1977     }
1978 }
1979 \f
1980 /* ARGSUSED */
1981 static void
1982 printf_command (arg, from_tty)
1983      char *arg;
1984      int from_tty;
1985 {
1986   register char *f = NULL;
1987   register char *s = arg;
1988   char *string = NULL;
1989   value_ptr *val_args;
1990   char *substrings;
1991   char *current_substring;
1992   int nargs = 0;
1993   int allocated_args = 20;
1994   struct cleanup *old_cleanups;
1995
1996   val_args = (value_ptr *) xmalloc (allocated_args * sizeof (value_ptr));
1997   old_cleanups = make_cleanup ((make_cleanup_func) free_current_contents, 
1998                                &val_args);
1999
2000   if (s == 0)
2001     error_no_arg ("format-control string and values to print");
2002
2003   /* Skip white space before format string */
2004   while (*s == ' ' || *s == '\t') s++;
2005
2006   /* A format string should follow, enveloped in double quotes */
2007   if (*s++ != '"')
2008     error ("Bad format string, missing '\"'.");
2009
2010   /* Parse the format-control string and copy it into the string STRING,
2011      processing some kinds of escape sequence.  */
2012
2013   f = string = (char *) alloca (strlen (s) + 1);
2014
2015   while (*s != '"')
2016     {
2017       int c = *s++;
2018       switch (c)
2019         {
2020         case '\0':
2021           error ("Bad format string, non-terminated '\"'.");
2022
2023         case '\\':
2024           switch (c = *s++)
2025             {
2026             case '\\':
2027               *f++ = '\\';
2028               break;
2029             case 'a':
2030 #ifdef __STDC__
2031               *f++ = '\a';
2032 #else
2033               *f++ = '\007';  /* Bell */
2034 #endif
2035               break;
2036             case 'b':
2037               *f++ = '\b';
2038               break;
2039             case 'f':
2040               *f++ = '\f';
2041               break;
2042             case 'n':
2043               *f++ = '\n';
2044               break;
2045             case 'r':
2046               *f++ = '\r';
2047               break;
2048             case 't':
2049               *f++ = '\t';
2050               break;
2051             case 'v':
2052               *f++ = '\v';
2053               break;
2054             case '"':
2055               *f++ = '"';
2056               break;
2057             default:
2058               /* ??? TODO: handle other escape sequences */
2059               error ("Unrecognized escape character \\%c in format string.",
2060                      c);
2061             }
2062           break;
2063
2064         default:
2065           *f++ = c;
2066         }
2067     }
2068
2069   /* Skip over " and following space and comma.  */
2070   s++;
2071   *f++ = '\0';
2072   while (*s == ' ' || *s == '\t') s++;
2073
2074   if (*s != ',' && *s != 0)
2075     error ("Invalid argument syntax");
2076
2077   if (*s == ',') s++;
2078   while (*s == ' ' || *s == '\t') s++;
2079
2080   /* Need extra space for the '\0's.  Doubling the size is sufficient.  */
2081   substrings = alloca (strlen (string) * 2);
2082   current_substring = substrings;
2083
2084   {
2085     /* Now scan the string for %-specs and see what kinds of args they want.
2086        argclass[I] classifies the %-specs so we can give printf_filtered
2087        something of the right size.  */
2088
2089     enum argclass {no_arg, int_arg, string_arg, double_arg, long_long_arg};
2090     enum argclass *argclass;
2091     enum argclass this_argclass;
2092     char *last_arg;
2093     int nargs_wanted;
2094     int lcount;
2095     int i;
2096
2097     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
2098     nargs_wanted = 0;
2099     f = string;
2100     last_arg = string;
2101     while (*f)
2102       if (*f++ == '%')
2103         {
2104           lcount = 0;
2105           while (strchr ("0123456789.hlL-+ #", *f)) 
2106             {
2107               if (*f == 'l' || *f == 'L')
2108                 lcount++;
2109               f++;
2110             }
2111           switch (*f)
2112             {
2113             case 's':
2114               this_argclass = string_arg;
2115               break;
2116
2117             case 'e':
2118             case 'f':
2119             case 'g':
2120               this_argclass = double_arg;
2121               break;
2122
2123             case '*':
2124               error ("`*' not supported for precision or width in printf");
2125
2126             case 'n':
2127               error ("Format specifier `n' not supported in printf");
2128
2129             case '%':
2130               this_argclass = no_arg;
2131               break;
2132
2133             default:
2134               if (lcount > 1)
2135                 this_argclass = long_long_arg;
2136               else
2137                 this_argclass = int_arg;
2138               break;
2139             }
2140           f++;
2141           if (this_argclass != no_arg)
2142             {
2143               strncpy (current_substring, last_arg, f - last_arg);
2144               current_substring += f - last_arg;
2145               *current_substring++ = '\0';
2146               last_arg = f;
2147               argclass[nargs_wanted++] = this_argclass;
2148             }
2149         }
2150
2151     /* Now, parse all arguments and evaluate them.
2152        Store the VALUEs in VAL_ARGS.  */
2153
2154     while (*s != '\0')
2155       {
2156         char *s1;
2157         if (nargs == allocated_args)
2158           val_args = (value_ptr *) xrealloc ((char *) val_args,
2159                                              (allocated_args *= 2)
2160                                              * sizeof (value_ptr));
2161         s1 = s;
2162         val_args[nargs] = parse_to_comma_and_eval (&s1);
2163  
2164         /* If format string wants a float, unchecked-convert the value to
2165            floating point of the same size */
2166  
2167         if (argclass[nargs] == double_arg)
2168           {
2169             struct type *type = VALUE_TYPE (val_args[nargs]);
2170             if (TYPE_LENGTH (type) == sizeof (float))
2171               VALUE_TYPE (val_args[nargs]) = builtin_type_float;
2172             if (TYPE_LENGTH (type) == sizeof (double))
2173               VALUE_TYPE (val_args[nargs]) = builtin_type_double;
2174           }
2175         nargs++;
2176         s = s1;
2177         if (*s == ',')
2178           s++;
2179       }
2180  
2181     if (nargs != nargs_wanted)
2182       error ("Wrong number of arguments for specified format-string");
2183
2184     /* Now actually print them.  */
2185     current_substring = substrings;
2186     for (i = 0; i < nargs; i++)
2187       {
2188         switch (argclass[i])
2189           {
2190           case string_arg:
2191             {
2192               char *str;
2193               CORE_ADDR tem;
2194               int j;
2195               tem = value_as_pointer (val_args[i]);
2196
2197               /* This is a %s argument.  Find the length of the string.  */
2198               for (j = 0; ; j++)
2199                 {
2200                   char c;
2201                   QUIT;
2202                   read_memory_section (tem + j, &c, 1,
2203                                        VALUE_BFD_SECTION (val_args[i]));
2204                   if (c == 0)
2205                     break;
2206                 }
2207
2208               /* Copy the string contents into a string inside GDB.  */
2209               str = (char *) alloca (j + 1);
2210               read_memory_section (tem, str, j, VALUE_BFD_SECTION (val_args[i]));
2211               str[j] = 0;
2212
2213               printf_filtered (current_substring, str);
2214             }
2215             break;
2216           case double_arg:
2217             {
2218               double val = value_as_double (val_args[i]);
2219               printf_filtered (current_substring, val);
2220               break;
2221             }
2222           case long_long_arg:
2223 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2224             {
2225               long long val = value_as_long (val_args[i]);
2226               printf_filtered (current_substring, val);
2227               break;
2228             }
2229 #else
2230             error ("long long not supported in printf");
2231 #endif
2232           case int_arg:
2233             {
2234               /* FIXME: there should be separate int_arg and long_arg.  */
2235               long val = value_as_long (val_args[i]);
2236               printf_filtered (current_substring, val);
2237               break;
2238             }
2239           default: /* purecov: deadcode */
2240             error ("internal error in printf_command"); /* purecov: deadcode */
2241           }
2242         /* Skip to the next substring.  */
2243         current_substring += strlen (current_substring) + 1;
2244       }
2245     /* Print the portion of the format string after the last argument.  */
2246     printf_filtered (last_arg);
2247   }
2248   do_cleanups (old_cleanups);
2249 }
2250 \f
2251 /* Dump a specified section of assembly code.  With no command line
2252    arguments, this command will dump the assembly code for the
2253    function surrounding the pc value in the selected frame.  With one
2254    argument, it will dump the assembly code surrounding that pc value.
2255    Two arguments are interpeted as bounds within which to dump
2256    assembly.  */
2257
2258 /* ARGSUSED */
2259 static void
2260 disassemble_command (arg, from_tty)
2261      char *arg;
2262      int from_tty;
2263 {
2264   CORE_ADDR low, high;
2265   char *name;
2266   CORE_ADDR pc, pc_masked;
2267   char *space_index;
2268 #if 0
2269   asection *section;
2270 #endif
2271
2272   name = NULL;
2273   if (!arg)
2274     {
2275       if (!selected_frame)
2276         error ("No frame selected.\n");
2277
2278       pc = get_frame_pc (selected_frame);
2279       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
2280         error ("No function contains program counter for selected frame.\n");
2281 #if defined(TUI)
2282       else if (tui_version)
2283         low = (CORE_ADDR)tuiDo((TuiOpaqueFuncPtr)tui_vGetLowDisassemblyAddress, 
2284                                (Opaque)low, 
2285                                (Opaque)pc);
2286 #endif
2287       low += FUNCTION_START_OFFSET;
2288     }
2289   else if (!(space_index = (char *) strchr (arg, ' ')))
2290     {
2291       /* One argument.  */
2292       pc = parse_and_eval_address (arg);
2293       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
2294         error ("No function contains specified address.\n");
2295 #if defined(TUI)
2296       else if (tui_version)
2297         low = (CORE_ADDR)tuiDo((TuiOpaqueFuncPtr)tui_vGetLowDisassemblyAddress, 
2298                                (Opaque)low, 
2299                                (Opaque)pc);
2300 #endif
2301 #if 0
2302       if (overlay_debugging)
2303         {
2304           section = find_pc_overlay (pc);
2305           if (pc_in_unmapped_range (pc, section))
2306             {
2307               /* find_pc_partial_function will have returned low and high
2308                  relative to the symbolic (mapped) address range.  Need to
2309                  translate them back to the unmapped range where PC is.  */
2310               low  = overlay_unmapped_address (low, section);
2311               high = overlay_unmapped_address (high, section);
2312             }
2313         }
2314 #endif
2315       low += FUNCTION_START_OFFSET;
2316     }
2317   else
2318     {
2319       /* Two arguments.  */
2320       *space_index = '\0';
2321       low = parse_and_eval_address (arg);
2322       high = parse_and_eval_address (space_index + 1);
2323     }
2324
2325 #if defined(TUI)
2326   if (!tui_version ||
2327       m_winPtrIsNull(disassemWin) || !disassemWin->generic.isVisible)
2328 #endif
2329     {
2330       printf_filtered ("Dump of assembler code ");
2331       if (name != NULL)
2332         {
2333           printf_filtered ("for function %s:\n", name);
2334         }
2335       else
2336         {
2337           printf_filtered ("from ");
2338           print_address_numeric (low, 1, gdb_stdout);
2339           printf_filtered (" to ");
2340           print_address_numeric (high, 1, gdb_stdout);
2341           printf_filtered (":\n");
2342         }
2343
2344       /* Dump the specified range.  */
2345       pc = low;
2346
2347 #ifdef GDB_TARGET_MASK_DISAS_PC
2348       pc_masked = GDB_TARGET_MASK_DISAS_PC (pc);
2349 #else
2350       pc_masked = pc;
2351 #endif
2352
2353       while (pc_masked < high)
2354         {
2355           QUIT;
2356           print_address (pc_masked, gdb_stdout);
2357           printf_filtered (":\t");
2358           /* We often wrap here if there are long symbolic names.  */
2359           wrap_here ("    ");
2360           pc += print_insn (pc, gdb_stdout);
2361           printf_filtered ("\n");
2362
2363 #ifdef GDB_TARGET_MASK_DISAS_PC
2364           pc_masked = GDB_TARGET_MASK_DISAS_PC (pc);
2365 #else
2366           pc_masked = pc;
2367 #endif
2368         }
2369       printf_filtered ("End of assembler dump.\n");
2370       gdb_flush (gdb_stdout);
2371     }
2372 #if defined(TUI)
2373   else
2374     {
2375       tuiDo((TuiOpaqueFuncPtr)tui_vAddWinToLayout, DISASSEM_WIN);
2376       tuiDo((TuiOpaqueFuncPtr)tui_vUpdateSourceWindowsWithAddr, low);
2377     }
2378 #endif
2379 }
2380
2381 /* Print the instruction at address MEMADDR in debugged memory,
2382    on STREAM.  Returns length of the instruction, in bytes.  */
2383
2384 static int
2385 print_insn (memaddr, stream)
2386      CORE_ADDR memaddr;
2387      GDB_FILE *stream;
2388 {
2389   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2390     TARGET_PRINT_INSN_INFO->endian = BFD_ENDIAN_BIG;
2391   else
2392     TARGET_PRINT_INSN_INFO->endian = BFD_ENDIAN_LITTLE;
2393
2394   if (TARGET_ARCHITECTURE != NULL)
2395     TARGET_PRINT_INSN_INFO->mach = TARGET_ARCHITECTURE->mach;
2396   /* else: should set .mach=0 but some disassemblers don't grok this */
2397
2398   return TARGET_PRINT_INSN (memaddr, TARGET_PRINT_INSN_INFO);
2399 }
2400
2401 \f
2402 void
2403 _initialize_printcmd ()
2404 {
2405   current_display_number = -1;
2406
2407   add_info ("address", address_info,
2408            "Describe where symbol SYM is stored.");
2409
2410   add_info ("symbol", sym_info, 
2411             "Describe what symbol is at location ADDR.\n\
2412 Only for symbols with fixed locations (global or static scope).");
2413
2414   add_com ("x", class_vars, x_command,
2415            concat ("Examine memory: x/FMT ADDRESS.\n\
2416 ADDRESS is an expression for the memory address to examine.\n\
2417 FMT is a repeat count followed by a format letter and a size letter.\n\
2418 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2419   t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n",
2420 "Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2421 The specified number of objects of the specified size are printed\n\
2422 according to the format.\n\n\
2423 Defaults for format and size letters are those previously used.\n\
2424 Default count is 1.  Default address is following last thing printed\n\
2425 with this command or \"print\".", NULL));
2426
2427   add_com ("disassemble", class_vars, disassemble_command,
2428            "Disassemble a specified section of memory.\n\
2429 Default is the function surrounding the pc of the selected frame.\n\
2430 With a single argument, the function surrounding that address is dumped.\n\
2431 Two arguments are taken as a range of memory to dump.");
2432   if (xdb_commands)
2433         add_com_alias ("va", "disassemble", class_xdb, 0);
2434
2435 #if 0
2436   add_com ("whereis", class_vars, whereis_command,
2437            "Print line number and file of definition of variable.");
2438 #endif
2439   
2440   add_info ("display", display_info,
2441             "Expressions to display when program stops, with code numbers.");
2442
2443   add_cmd ("undisplay", class_vars, undisplay_command,
2444            "Cancel some expressions to be displayed when program stops.\n\
2445 Arguments are the code numbers of the expressions to stop displaying.\n\
2446 No argument means cancel all automatic-display expressions.\n\
2447 \"delete display\" has the same effect as this command.\n\
2448 Do \"info display\" to see current list of code numbers.",
2449                   &cmdlist);
2450
2451   add_com ("display", class_vars, display_command,
2452            "Print value of expression EXP each time the program stops.\n\
2453 /FMT may be used before EXP as in the \"print\" command.\n\
2454 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2455 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2456 and examining is done as in the \"x\" command.\n\n\
2457 With no argument, display all currently requested auto-display expressions.\n\
2458 Use \"undisplay\" to cancel display requests previously made."
2459 );
2460
2461   add_cmd ("display", class_vars, enable_display, 
2462            "Enable some expressions to be displayed when program stops.\n\
2463 Arguments are the code numbers of the expressions to resume displaying.\n\
2464 No argument means enable all automatic-display expressions.\n\
2465 Do \"info display\" to see current list of code numbers.", &enablelist);
2466
2467   add_cmd ("display", class_vars, disable_display_command, 
2468            "Disable some expressions to be displayed when program stops.\n\
2469 Arguments are the code numbers of the expressions to stop displaying.\n\
2470 No argument means disable all automatic-display expressions.\n\
2471 Do \"info display\" to see current list of code numbers.", &disablelist);
2472
2473   add_cmd ("display", class_vars, undisplay_command, 
2474            "Cancel some expressions to be displayed when program stops.\n\
2475 Arguments are the code numbers of the expressions to stop displaying.\n\
2476 No argument means cancel all automatic-display expressions.\n\
2477 Do \"info display\" to see current list of code numbers.", &deletelist);
2478
2479   add_com ("printf", class_vars, printf_command,
2480         "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2481 This is useful for formatted output in user-defined commands.");
2482
2483   add_com ("output", class_vars, output_command,
2484            "Like \"print\" but don't put in value history and don't print newline.\n\
2485 This is useful in user-defined commands.");
2486
2487   add_prefix_cmd ("set", class_vars, set_command,
2488 concat ("Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2489 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2490 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2491 with $), a register (a few standard names starting with $), or an actual\n\
2492 variable in the program being debugged.  EXP is any valid expression.\n",
2493 "Use \"set variable\" for variables with names identical to set subcommands.\n\
2494 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2495 You can see these environment settings with the \"show\" command.", NULL),
2496                   &setlist, "set ", 1, &cmdlist);
2497   if (dbx_commands)
2498     add_com("assign", class_vars, set_command, concat ("Evaluate expression \
2499 EXP and assign result to variable VAR, using assignment\n\
2500 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2501 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2502 with $), a register (a few standard names starting with $), or an actual\n\
2503 variable in the program being debugged.  EXP is any valid expression.\n",
2504 "Use \"set variable\" for variables with names identical to set subcommands.\n\
2505 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2506 You can see these environment settings with the \"show\" command.", NULL));
2507
2508   /* "call" is the same as "set", but handy for dbx users to call fns. */
2509   add_com ("call", class_vars, call_command,
2510            "Call a function in the program.\n\
2511 The argument is the function name and arguments, in the notation of the\n\
2512 current working language.  The result is printed and saved in the value\n\
2513 history, if it is not void.");
2514
2515   add_cmd ("variable", class_vars, set_command,
2516 "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2517 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2518 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2519 with $), a register (a few standard names starting with $), or an actual\n\
2520 variable in the program being debugged.  EXP is any valid expression.\n\
2521 This may usually be abbreviated to simply \"set\".",
2522            &setlist);
2523
2524   add_com ("print", class_vars, print_command,
2525            concat ("Print value of expression EXP.\n\
2526 Variables accessible are those of the lexical environment of the selected\n\
2527 stack frame, plus all those whose scope is global or an entire file.\n\
2528 \n\
2529 $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
2530 $$NUM refers to NUM'th value back from the last one.\n\
2531 Names starting with $ refer to registers (with the values they would have\n",
2532 "if the program were to return to the stack frame now selected, restoring\n\
2533 all registers saved by frames farther in) or else to debugger\n\
2534 \"convenience\" variables (any such name not a known register).\n\
2535 Use assignment expressions to give values to convenience variables.\n",
2536                    "\n\
2537 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2538 @ is a binary operator for treating consecutive data objects\n\
2539 anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
2540 element is FOO, whose second element is stored in the space following\n\
2541 where FOO is stored, etc.  FOO must be an expression whose value\n\
2542 resides in memory.\n",
2543                    "\n\
2544 EXP may be preceded with /FMT, where FMT is a format letter\n\
2545 but no count or size letter (see \"x\" command).", NULL));
2546   add_com_alias ("p", "print", class_vars, 1);
2547
2548   add_com ("inspect", class_vars, inspect_command,
2549 "Same as \"print\" command, except that if you are running in the epoch\n\
2550 environment, the value is printed in its own window.");
2551
2552   add_show_from_set (
2553       add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,
2554                    (char *)&max_symbolic_offset,
2555         "Set the largest offset that will be printed in <symbol+1234> form.",
2556                    &setprintlist),
2557       &showprintlist);
2558   add_show_from_set (
2559       add_set_cmd ("symbol-filename", no_class, var_boolean,
2560                    (char *)&print_symbol_filename,
2561         "Set printing of source filename and line number with <symbol>.",
2562                    &setprintlist),
2563       &showprintlist);
2564
2565   /* For examine/instruction a single byte quantity is specified as
2566      the data.  This avoids problems with value_at_lazy() requiring a
2567      valid data type (and rejecting VOID). */
2568   examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2569
2570   examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2571   examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2572   examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2573   examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2574
2575 }