OSDN Git Service

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