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