* symtab.c (decode_line_1): Use end of block to figure out whether
[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 #ifdef ADDR_BITS_REMOVE
591   fprintf_filtered (stream, local_hex_format(), ADDR_BITS_REMOVE(addr));
592 #else
593   fprintf_filtered (stream, local_hex_format(), addr);
594 #endif
595   print_address_symbolic (addr, stream, asm_demangle, " ");
596 }
597
598 /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
599    controls whether to print the symbolic name "raw" or demangled.
600    Global setting "addressprint" controls whether to print hex address
601    or not.  */
602
603 void
604 print_address_demangle (addr, stream, do_demangle)
605      CORE_ADDR addr;
606      FILE *stream;
607      int do_demangle;
608 {
609   if (addr == 0) {
610     fprintf_filtered (stream, "0");
611   } else if (addressprint) {
612     fprintf_filtered (stream, local_hex_format(), addr);
613     print_address_symbolic (addr, stream, do_demangle, " ");
614   } else {
615     print_address_symbolic (addr, stream, do_demangle, "");
616   }
617 }
618 \f
619
620 /* Examine data at address ADDR in format FMT.
621    Fetch it from memory and print on stdout.  */
622
623 static void
624 do_examine (fmt, addr)
625      struct format_data fmt;
626      CORE_ADDR addr;
627 {
628   register char format = 0;
629   register char size;
630   register int count = 1;
631   struct type *val_type;
632   register int i;
633   register int maxelts;
634
635   format = fmt.format;
636   size = fmt.size;
637   count = fmt.count;
638   next_address = addr;
639
640   /* String or instruction format implies fetch single bytes
641      regardless of the specified size.  */
642   if (format == 's' || format == 'i')
643     size = 'b';
644
645   if (size == 'b')
646     val_type = builtin_type_char;
647   else if (size == 'h')
648     val_type = builtin_type_short;
649   else if (size == 'w')
650     val_type = builtin_type_long;
651   else if (size == 'g')
652 #ifndef CC_HAS_LONG_LONG
653     val_type = builtin_type_double;
654 #else
655     val_type = builtin_type_long_long;
656 #endif
657
658   maxelts = 8;
659   if (size == 'w')
660     maxelts = 4;
661   if (size == 'g')
662     maxelts = 2;
663   if (format == 's' || format == 'i')
664     maxelts = 1;
665
666   /* Print as many objects as specified in COUNT, at most maxelts per line,
667      with the address of the next one at the start of each line.  */
668
669   while (count > 0)
670     {
671       print_address (next_address, stdout);
672       printf_filtered (":");
673       for (i = maxelts;
674            i > 0 && count > 0;
675            i--, count--)
676         {
677           printf_filtered ("\t");
678           /* Note that print_formatted sets next_address for the next
679              object.  */
680           last_examine_address = next_address;
681           last_examine_value = value_at (val_type, next_address);
682           print_formatted (last_examine_value, format, size);
683         }
684       printf_filtered ("\n");
685       fflush (stdout);
686     }
687 }
688 \f
689 static void
690 validate_format (fmt, cmdname)
691      struct format_data fmt;
692      char *cmdname;
693 {
694   if (fmt.size != 0)
695     error ("Size letters are meaningless in \"%s\" command.", cmdname);
696   if (fmt.count != 1)
697     error ("Item count other than 1 is meaningless in \"%s\" command.",
698            cmdname);
699   if (fmt.format == 'i' || fmt.format == 's')
700     error ("Format letter \"%c\" is meaningless in \"%s\" command.",
701            fmt.format, cmdname);
702 }
703
704 /*  Evaluate string EXP as an expression in the current language and
705     print the resulting value.  EXP may contain a format specifier as the
706     first argument ("/x myvar" for example, to print myvar in hex).
707     */
708
709 static void
710 print_command_1 (exp, inspect, voidprint)
711      char *exp;
712      int inspect;
713      int voidprint;
714 {
715   struct expression *expr;
716   register struct cleanup *old_chain = 0;
717   register char format = 0;
718   register value val;
719   struct format_data fmt;
720   int cleanup = 0;
721
722   /* Pass inspect flag to the rest of the print routines in a global (sigh). */
723   inspect_it = inspect;
724
725   if (exp && *exp == '/')
726     {
727       exp++;
728       fmt = decode_format (&exp, last_format, 0);
729       validate_format (fmt, "print");
730       last_format = format = fmt.format;
731     }
732   else
733     {
734       fmt.count = 1;
735       fmt.format = 0;
736       fmt.size = 0;
737     }
738
739   if (exp && *exp)
740     {
741       extern int objectprint;
742       struct type *type;
743       expr = parse_expression (exp);
744       old_chain = make_cleanup (free_current_contents, &expr);
745       cleanup = 1;
746       val = evaluate_expression (expr);
747
748       /* C++: figure out what type we actually want to print it as.  */
749       type = VALUE_TYPE (val);
750
751       if (objectprint
752           && (   TYPE_CODE (type) == TYPE_CODE_PTR
753               || TYPE_CODE (type) == TYPE_CODE_REF)
754           && (   TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT
755               || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_UNION))
756         {
757           value v;
758
759           v = value_from_vtable_info (val, TYPE_TARGET_TYPE (type));
760           if (v != 0)
761             {
762               val = v;
763               type = VALUE_TYPE (val);
764             }
765         }
766     }
767   else
768     val = access_value_history (0);
769
770   if (voidprint || (val && VALUE_TYPE (val) &&
771                     TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_VOID))
772     {
773       int histindex = record_latest_value (val);
774
775       if (inspect)
776         printf ("\031(gdb-makebuffer \"%s\"  %d '(\"", exp, histindex);
777       else
778         if (histindex >= 0) printf_filtered ("$%d = ", histindex);
779
780       print_formatted (val, format, fmt.size);
781       printf_filtered ("\n");
782       if (inspect)
783         printf("\") )\030");
784     }
785
786   if (cleanup)
787     do_cleanups (old_chain);
788   inspect_it = 0;       /* Reset print routines to normal */
789 }
790
791 /* ARGSUSED */
792 static void
793 print_command (exp, from_tty)
794      char *exp;
795      int from_tty;
796 {
797   print_command_1 (exp, 0, 1);
798 }
799
800 /* Same as print, except in epoch, it gets its own window */
801 /* ARGSUSED */
802 static void
803 inspect_command (exp, from_tty)
804      char *exp;
805      int from_tty;
806 {
807   extern int epoch_interface;
808
809   print_command_1 (exp, epoch_interface, 1);
810 }
811
812 /* Same as print, except it doesn't print void results. */
813 /* ARGSUSED */
814 static void
815 call_command (exp, from_tty)
816      char *exp;
817      int from_tty;
818 {
819   print_command_1 (exp, 0, 0);
820 }
821
822 /* ARGSUSED */
823 static void
824 output_command (exp, from_tty)
825      char *exp;
826      int from_tty;
827 {
828   struct expression *expr;
829   register struct cleanup *old_chain;
830   register char format = 0;
831   register value val;
832   struct format_data fmt;
833
834   if (exp && *exp == '/')
835     {
836       exp++;
837       fmt = decode_format (&exp, 0, 0);
838       validate_format (fmt, "output");
839       format = fmt.format;
840     }
841
842   expr = parse_expression (exp);
843   old_chain = make_cleanup (free_current_contents, &expr);
844
845   val = evaluate_expression (expr);
846
847   print_formatted (val, format, fmt.size);
848
849   do_cleanups (old_chain);
850 }
851
852 /* ARGSUSED */
853 static void
854 set_command (exp, from_tty)
855      char *exp;
856      int from_tty;
857 {
858   struct expression *expr = parse_expression (exp);
859   register struct cleanup *old_chain
860     = make_cleanup (free_current_contents, &expr);
861   evaluate_expression (expr);
862   do_cleanups (old_chain);
863 }
864
865 /* ARGSUSED */
866 static void
867 address_info (exp, from_tty)
868      char *exp;
869      int from_tty;
870 {
871   register struct symbol *sym;
872   register struct minimal_symbol *msymbol;
873   register long val;
874   register long basereg;
875   int is_a_field_of_this;       /* C++: lookup_symbol sets this to nonzero
876                                    if exp is a field of `this'. */
877
878   if (exp == 0)
879     error ("Argument required.");
880
881   sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE, 
882                        &is_a_field_of_this, (struct symtab **)NULL);
883   if (sym == NULL)
884     {
885       if (is_a_field_of_this)
886         {
887           printf ("Symbol \"%s\" is a field of the local class variable `this'\n", exp);
888           return;
889         }
890
891       msymbol = lookup_minimal_symbol (exp, (struct objfile *) NULL);
892
893       if (msymbol != NULL)
894         printf ("Symbol \"%s\" is at %s in a file compiled without debugging.\n",
895                 exp, local_hex_string(SYMBOL_VALUE_ADDRESS (msymbol)));
896       else
897         error ("No symbol \"%s\" in current context.", exp);
898       return;
899     }
900
901   printf ("Symbol \"%s\" is ", SYMBOL_NAME (sym));
902   val = SYMBOL_VALUE (sym);
903   basereg = SYMBOL_BASEREG (sym);
904
905   switch (SYMBOL_CLASS (sym))
906     {
907     case LOC_CONST:
908     case LOC_CONST_BYTES:
909       printf ("constant");
910       break;
911
912     case LOC_LABEL:
913       printf ("a label at address %s", local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
914       break;
915
916     case LOC_REGISTER:
917       printf ("a variable in register %s", reg_names[val]);
918       break;
919
920     case LOC_STATIC:
921       printf ("static storage at address %s", local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
922       break;
923
924     case LOC_REGPARM:
925       printf ("an argument in register %s", reg_names[val]);
926       break;
927
928    case LOC_REGPARM_ADDR:
929      printf ("address of an argument in register %s", reg_names[val]);
930      break;
931       
932     case LOC_ARG:
933       if (SYMBOL_BASEREG_VALID (sym))
934         {
935           printf ("an argument at offset %ld from register %s",
936                   val, reg_names[basereg]);
937         }
938       else
939         {
940           printf ("an argument at offset %ld", val);
941         }
942       break;
943
944     case LOC_LOCAL_ARG:
945       if (SYMBOL_BASEREG_VALID (sym))
946         {
947           printf ("an argument at offset %ld from register %s",
948                   val, reg_names[basereg]);
949         }
950       else
951         {
952           printf ("an argument at frame offset %ld", val);
953         }
954       break;
955
956     case LOC_LOCAL:
957       if (SYMBOL_BASEREG_VALID (sym))
958         {
959           printf ("a local variable at offset %ld from register %s",
960                   val, reg_names[basereg]);
961         }
962       else
963         {
964           printf ("a local variable at frame offset %ld", val);
965         }
966       break;
967
968     case LOC_REF_ARG:
969       printf ("a reference argument at offset %ld", val);
970       break;
971
972     case LOC_TYPEDEF:
973       printf ("a typedef");
974       break;
975
976     case LOC_BLOCK:
977       printf ("a function at address %s",
978               local_hex_string(BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
979       break;
980
981     case LOC_OPTIMIZED_OUT:
982       printf_filtered ("optimized out");
983       break;
984       
985     default:
986       printf ("of unknown (botched) type");
987       break;
988     }
989   printf (".\n");
990 }
991 \f
992 static void
993 x_command (exp, from_tty)
994      char *exp;
995      int from_tty;
996 {
997   struct expression *expr;
998   struct format_data fmt;
999   struct cleanup *old_chain;
1000   struct value *val;
1001
1002   fmt.format = last_format;
1003   fmt.size = last_size;
1004   fmt.count = 1;
1005
1006   if (exp && *exp == '/')
1007     {
1008       exp++;
1009       fmt = decode_format (&exp, last_format, last_size);
1010     }
1011
1012   /* If we have an expression, evaluate it and use it as the address.  */
1013
1014   if (exp != 0 && *exp != 0)
1015     {
1016       expr = parse_expression (exp);
1017       /* Cause expression not to be there any more
1018          if this command is repeated with Newline.
1019          But don't clobber a user-defined command's definition.  */
1020       if (from_tty)
1021         *exp = 0;
1022       old_chain = make_cleanup (free_current_contents, &expr);
1023       val = evaluate_expression (expr);
1024       if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF)
1025         val = value_ind (val);
1026       /* In rvalue contexts, such as this, functions are coerced into
1027          pointers to functions.  This makes "x/i main" work.  */
1028       if (/* last_format == 'i'
1029           && */ TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
1030           && VALUE_LVAL (val) == lval_memory)
1031         next_address = VALUE_ADDRESS (val);
1032       else
1033         next_address = value_as_pointer (val);
1034       do_cleanups (old_chain);
1035     }
1036
1037   do_examine (fmt, next_address);
1038
1039   /* If the examine succeeds, we remember its size and format for next time.  */
1040   last_size = fmt.size;
1041   last_format = fmt.format;
1042
1043   /* Set a couple of internal variables if appropriate. */
1044   if (last_examine_value)
1045     {
1046       /* Make last address examined available to the user as $_.  Use
1047          the correct pointer type.  */
1048       set_internalvar (lookup_internalvar ("_"),
1049                value_from_longest (
1050                  lookup_pointer_type (VALUE_TYPE (last_examine_value)),
1051                                    (LONGEST) last_examine_address));
1052       
1053       /* Make contents of last address examined available to the user as $__.*/
1054       set_internalvar (lookup_internalvar ("__"), last_examine_value);
1055     }
1056 }
1057
1058 \f
1059 /* Add an expression to the auto-display chain.
1060    Specify the expression.  */
1061
1062 static void
1063 display_command (exp, from_tty)
1064      char *exp;
1065      int from_tty;
1066 {
1067   struct format_data fmt;
1068   register struct expression *expr;
1069   register struct display *new;
1070
1071   if (exp == 0)
1072     {
1073       do_displays ();
1074       return;
1075     }
1076
1077   if (*exp == '/')
1078     {
1079       exp++;
1080       fmt = decode_format (&exp, 0, 0);
1081       if (fmt.size && fmt.format == 0)
1082         fmt.format = 'x';
1083       if (fmt.format == 'i' || fmt.format == 's')
1084         fmt.size = 'b';
1085     }
1086   else
1087     {
1088       fmt.format = 0;
1089       fmt.size = 0;
1090       fmt.count = 0;
1091     }
1092
1093   innermost_block = 0;
1094   expr = parse_expression (exp);
1095
1096   new = (struct display *) xmalloc (sizeof (struct display));
1097
1098   new->exp = expr;
1099   new->block = innermost_block;
1100   new->next = display_chain;
1101   new->number = ++display_number;
1102   new->format = fmt;
1103   new->status = enabled;
1104   display_chain = new;
1105
1106   if (from_tty && target_has_execution)
1107     do_one_display (new);
1108
1109   dont_repeat ();
1110 }
1111
1112 static void
1113 free_display (d)
1114      struct display *d;
1115 {
1116   free ((PTR)d->exp);
1117   free ((PTR)d);
1118 }
1119
1120 /* Clear out the display_chain.
1121    Done when new symtabs are loaded, since this invalidates
1122    the types stored in many expressions.  */
1123
1124 void
1125 clear_displays ()
1126 {
1127   register struct display *d;
1128
1129   while ((d = display_chain) != NULL)
1130     {
1131       free ((PTR)d->exp);
1132       display_chain = d->next;
1133       free ((PTR)d);
1134     }
1135 }
1136
1137 /* Delete the auto-display number NUM.  */
1138
1139 static void
1140 delete_display (num)
1141      int num;
1142 {
1143   register struct display *d1, *d;
1144
1145   if (!display_chain)
1146     error ("No display number %d.", num);
1147
1148   if (display_chain->number == num)
1149     {
1150       d1 = display_chain;
1151       display_chain = d1->next;
1152       free_display (d1);
1153     }
1154   else
1155     for (d = display_chain; ; d = d->next)
1156       {
1157         if (d->next == 0)
1158           error ("No display number %d.", num);
1159         if (d->next->number == num)
1160           {
1161             d1 = d->next;
1162             d->next = d1->next;
1163             free_display (d1);
1164             break;
1165           }
1166       }
1167 }
1168
1169 /* Delete some values from the auto-display chain.
1170    Specify the element numbers.  */
1171
1172 static void
1173 undisplay_command (args, from_tty)
1174      char *args;
1175      int from_tty;
1176 {
1177   register char *p = args;
1178   register char *p1;
1179   register int num;
1180
1181   if (args == 0)
1182     {
1183       if (query ("Delete all auto-display expressions? "))
1184         clear_displays ();
1185       dont_repeat ();
1186       return;
1187     }
1188
1189   while (*p)
1190     {
1191       p1 = p;
1192       while (*p1 >= '0' && *p1 <= '9') p1++;
1193       if (*p1 && *p1 != ' ' && *p1 != '\t')
1194         error ("Arguments must be display numbers.");
1195
1196       num = atoi (p);
1197
1198       delete_display (num);
1199
1200       p = p1;
1201       while (*p == ' ' || *p == '\t') p++;
1202     }
1203   dont_repeat ();
1204 }
1205
1206 /* Display a single auto-display.  
1207    Do nothing if the display cannot be printed in the current context,
1208    or if the display is disabled. */
1209
1210 static void
1211 do_one_display (d)
1212      struct display *d;
1213 {
1214   int within_current_scope;
1215
1216   if (d->status == disabled)
1217     return;
1218
1219   if (d->block)
1220     within_current_scope = contained_in (get_selected_block (), d->block);
1221   else
1222     within_current_scope = 1;
1223   if (!within_current_scope)
1224     return;
1225
1226   current_display_number = d->number;
1227
1228   printf_filtered ("%d: ", d->number);
1229   if (d->format.size)
1230     {
1231       CORE_ADDR addr;
1232       
1233       printf_filtered ("x/");
1234       if (d->format.count != 1)
1235         printf_filtered ("%d", d->format.count);
1236       printf_filtered ("%c", d->format.format);
1237       if (d->format.format != 'i' && d->format.format != 's')
1238         printf_filtered ("%c", d->format.size);
1239       printf_filtered (" ");
1240       print_expression (d->exp, stdout);
1241       if (d->format.count != 1)
1242         printf_filtered ("\n");
1243       else
1244         printf_filtered ("  ");
1245       
1246       addr = value_as_pointer (evaluate_expression (d->exp));
1247       if (d->format.format == 'i')
1248         addr = ADDR_BITS_REMOVE (addr);
1249       
1250       do_examine (d->format, addr);
1251     }
1252   else
1253     {
1254       if (d->format.format)
1255         printf_filtered ("/%c ", d->format.format);
1256       print_expression (d->exp, stdout);
1257       printf_filtered (" = ");
1258       print_formatted (evaluate_expression (d->exp),
1259                        d->format.format, d->format.size);
1260       printf_filtered ("\n");
1261     }
1262
1263   fflush (stdout);
1264   current_display_number = -1;
1265 }
1266
1267 /* Display all of the values on the auto-display chain which can be
1268    evaluated in the current scope.  */
1269
1270 void
1271 do_displays ()
1272 {
1273   register struct display *d;
1274
1275   for (d = display_chain; d; d = d->next)
1276     do_one_display (d);
1277 }
1278
1279 /* Delete the auto-display which we were in the process of displaying.
1280    This is done when there is an error or a signal.  */
1281
1282 void
1283 disable_display (num)
1284      int num;
1285 {
1286   register struct display *d;
1287
1288   for (d = display_chain; d; d = d->next)
1289     if (d->number == num)
1290       {
1291         d->status = disabled;
1292         return;
1293       }
1294   printf ("No display number %d.\n", num);
1295 }
1296   
1297 void
1298 disable_current_display ()
1299 {
1300   if (current_display_number >= 0)
1301     {
1302       disable_display (current_display_number);
1303       fprintf (stderr, "Disabling display %d to avoid infinite recursion.\n",
1304                current_display_number);
1305     }
1306   current_display_number = -1;
1307 }
1308
1309 static void
1310 display_info (ignore, from_tty)
1311      char *ignore;
1312      int from_tty;
1313 {
1314   register struct display *d;
1315
1316   if (!display_chain)
1317     printf ("There are no auto-display expressions now.\n");
1318   else
1319       printf_filtered ("Auto-display expressions now in effect:\n\
1320 Num Enb Expression\n");
1321
1322   for (d = display_chain; d; d = d->next)
1323     {
1324       printf_filtered ("%d:   %c  ", d->number, "ny"[(int)d->status]);
1325       if (d->format.size)
1326         printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1327                 d->format.format);
1328       else if (d->format.format)
1329         printf_filtered ("/%c ", d->format.format);
1330       print_expression (d->exp, stdout);
1331       if (d->block && !contained_in (get_selected_block (), d->block))
1332         printf_filtered (" (cannot be evaluated in the current context)");
1333       printf_filtered ("\n");
1334       fflush (stdout);
1335     }
1336 }
1337
1338 static void
1339 enable_display (args, from_tty)
1340      char *args;
1341      int from_tty;
1342 {
1343   register char *p = args;
1344   register char *p1;
1345   register int num;
1346   register struct display *d;
1347
1348   if (p == 0)
1349     {
1350       for (d = display_chain; d; d = d->next)
1351         d->status = enabled;
1352     }
1353   else
1354     while (*p)
1355       {
1356         p1 = p;
1357         while (*p1 >= '0' && *p1 <= '9')
1358           p1++;
1359         if (*p1 && *p1 != ' ' && *p1 != '\t')
1360           error ("Arguments must be display numbers.");
1361         
1362         num = atoi (p);
1363         
1364         for (d = display_chain; d; d = d->next)
1365           if (d->number == num)
1366             {
1367               d->status = enabled;
1368               goto win;
1369             }
1370         printf ("No display number %d.\n", num);
1371       win:
1372         p = p1;
1373         while (*p == ' ' || *p == '\t')
1374           p++;
1375       }
1376 }
1377
1378 /* ARGSUSED */
1379 static void
1380 disable_display_command (args, from_tty)
1381      char *args;
1382      int from_tty;
1383 {
1384   register char *p = args;
1385   register char *p1;
1386   register struct display *d;
1387
1388   if (p == 0)
1389     {
1390       for (d = display_chain; d; d = d->next)
1391         d->status = disabled;
1392     }
1393   else
1394     while (*p)
1395       {
1396         p1 = p;
1397         while (*p1 >= '0' && *p1 <= '9')
1398           p1++;
1399         if (*p1 && *p1 != ' ' && *p1 != '\t')
1400           error ("Arguments must be display numbers.");
1401         
1402         disable_display (atoi (p));
1403
1404         p = p1;
1405         while (*p == ' ' || *p == '\t')
1406           p++;
1407       }
1408 }
1409
1410 \f
1411 /* Print the value in stack frame FRAME of a variable
1412    specified by a struct symbol.  */
1413
1414 void
1415 print_variable_value (var, frame, stream)
1416      struct symbol *var;
1417      FRAME frame;
1418      FILE *stream;
1419 {
1420   value val = read_var_value (var, frame);
1421   value_print (val, stream, 0, Val_pretty_default);
1422 }
1423
1424 /* Print the arguments of a stack frame, given the function FUNC
1425    running in that frame (as a symbol), the info on the frame,
1426    and the number of args according to the stack frame (or -1 if unknown).  */
1427
1428 /* References here and elsewhere to "number of args according to the
1429    stack frame" appear in all cases to refer to "number of ints of args
1430    according to the stack frame".  At least for VAX, i386, isi.  */
1431
1432 void
1433 print_frame_args (func, fi, num, stream)
1434      struct symbol *func;
1435      struct frame_info *fi;
1436      int num;
1437      FILE *stream;
1438 {
1439   struct block *b;
1440   int nsyms = 0;
1441   int first = 1;
1442   register int i;
1443   register struct symbol *sym;
1444   register value val;
1445   /* Offset of next stack argument beyond the one we have seen that is
1446      at the highest offset.
1447      -1 if we haven't come to a stack argument yet.  */
1448   long highest_offset = -1;
1449   int arg_size;
1450   /* Number of ints of arguments that we have printed so far.  */
1451   int args_printed = 0;
1452
1453   if (func)
1454     {
1455       b = SYMBOL_BLOCK_VALUE (func);
1456       nsyms = BLOCK_NSYMS (b);
1457     }
1458
1459   for (i = 0; i < nsyms; i++)
1460     {
1461       QUIT;
1462       sym = BLOCK_SYM (b, i);
1463
1464       /* Keep track of the highest stack argument offset seen, and
1465          skip over any kinds of symbols we don't care about.  */
1466
1467       switch (SYMBOL_CLASS (sym)) {
1468       case LOC_ARG:
1469       case LOC_REF_ARG:
1470         {
1471           long current_offset = SYMBOL_VALUE (sym);
1472
1473           arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
1474           
1475           /* Compute address of next argument by adding the size of
1476              this argument and rounding to an int boundary.  */
1477           current_offset
1478             = ((current_offset + arg_size + sizeof (int) - 1)
1479                & ~(sizeof (int) - 1));
1480
1481           /* If this is the highest offset seen yet, set highest_offset.  */
1482           if (highest_offset == -1
1483               || (current_offset > highest_offset))
1484             highest_offset = current_offset;
1485
1486           /* Add the number of ints we're about to print to args_printed.  */
1487           args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
1488         }
1489
1490       /* We care about types of symbols, but don't need to keep track of
1491          stack offsets in them.  */
1492       case LOC_REGPARM:
1493       case LOC_REGPARM_ADDR:
1494       case LOC_LOCAL_ARG:
1495         break;
1496
1497       /* Other types of symbols we just skip over.  */
1498       default:
1499         continue;
1500       }
1501
1502       /* We have to look up the symbol because arguments can have
1503          two entries (one a parameter, one a local) and the one we
1504          want is the local, which lookup_symbol will find for us.
1505          This includes gcc1 (not gcc2) on the sparc when passing a
1506          small structure and gcc2 when the argument type is float
1507          and it is passed as a double and converted to float by
1508          the prologue (in the latter case the type of the LOC_ARG
1509          symbol is double and the type of the LOC_LOCAL symbol is
1510          float).  There are also LOC_ARG/LOC_REGISTER pairs which
1511          are not combined in symbol-reading.  */
1512       /* But if the parameter name is null, don't try it.
1513          Null parameter names occur on the RS/6000, for traceback tables.
1514          FIXME, should we even print them?  */
1515
1516       if (*SYMBOL_NAME (sym))
1517         sym = lookup_symbol
1518           (SYMBOL_NAME (sym),
1519            b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
1520
1521       /* Print the current arg.  */
1522       if (! first)
1523         fprintf_filtered (stream, ", ");
1524       wrap_here ("    ");
1525       fprintf_symbol_filtered (stream, SYMBOL_SOURCE_NAME (sym),
1526                                SYMBOL_LANGUAGE (sym), DMGL_PARAMS | DMGL_ANSI);
1527       fputs_filtered ("=", stream);
1528
1529       /* Avoid value_print because it will deref ref parameters.  We just
1530          want to print their addresses.  Print ??? for args whose address
1531          we do not know.  We pass 2 as "recurse" to val_print because our
1532          standard indentation here is 4 spaces, and val_print indents
1533          2 for each recurse.  */
1534       val = read_var_value (sym, FRAME_INFO_ID (fi));
1535       if (val)
1536         val_print (VALUE_TYPE (val), VALUE_CONTENTS (val), VALUE_ADDRESS (val),
1537                    stream, 0, 0, 2, Val_no_prettyprint);
1538       else
1539         fputs_filtered ("???", stream);
1540       first = 0;
1541     }
1542
1543   /* Don't print nameless args in situations where we don't know
1544      enough about the stack to find them.  */
1545   if (num != -1)
1546     {
1547       long start;
1548
1549       if (highest_offset == -1)
1550         start = FRAME_ARGS_SKIP;
1551       else
1552         start = highest_offset;
1553
1554       print_frame_nameless_args (fi, start, num - args_printed,
1555                                  first, stream);
1556     }
1557 }
1558
1559 /* Print nameless args on STREAM.
1560    FI is the frameinfo for this frame, START is the offset
1561    of the first nameless arg, and NUM is the number of nameless args to
1562    print.  FIRST is nonzero if this is the first argument (not just
1563    the first nameless arg).  */
1564 static void
1565 print_frame_nameless_args (fi, start, num, first, stream)
1566      struct frame_info *fi;
1567      long start;
1568      int num;
1569      int first;
1570      FILE *stream;
1571 {
1572   int i;
1573   CORE_ADDR argsaddr;
1574   long arg_value;
1575
1576   for (i = 0; i < num; i++)
1577     {
1578       QUIT;
1579 #ifdef NAMELESS_ARG_VALUE
1580       NAMELESS_ARG_VALUE (fi, start, &arg_value);
1581 #else
1582       argsaddr = FRAME_ARGS_ADDRESS (fi);
1583       if (!argsaddr)
1584         return;
1585
1586       arg_value = read_memory_integer (argsaddr + start, sizeof (int));
1587 #endif
1588
1589       if (!first)
1590         fprintf_filtered (stream, ", ");
1591
1592 #ifdef  PRINT_NAMELESS_INTEGER
1593       PRINT_NAMELESS_INTEGER (stream, arg_value);
1594 #else
1595 #ifdef PRINT_TYPELESS_INTEGER
1596       PRINT_TYPELESS_INTEGER (stream, builtin_type_int, (LONGEST) arg_value);
1597 #else
1598       fprintf_filtered (stream, "%d", arg_value);
1599 #endif /* PRINT_TYPELESS_INTEGER */
1600 #endif /* PRINT_NAMELESS_INTEGER */
1601       first = 0;
1602       start += sizeof (int);
1603     }
1604 }
1605 \f
1606 /* Make makeva* work on an __INT_VARARGS_H machine.  */
1607
1608 #if defined (__INT_VARARGS_H)
1609 /* This is used on an 88k.  Not sure whether it is used by anything else.  */
1610 #define MAKEVA_END(list) \
1611   va_list retval; \
1612   retval.__va_arg = 0; \
1613   retval.__va_stk = (int *) (list)->arg_bytes; \
1614   retval.__va_reg = (int *) (list)->arg_bytes; \
1615   return retval;
1616 #endif
1617 \f
1618 /* This is an interface which allows to us make a va_list.  */
1619 typedef struct {
1620   unsigned int nargs;
1621   unsigned int max_arg_size;
1622
1623   /* Current position in bytes.  */
1624   unsigned int argindex;
1625
1626   char arg_bytes[1];
1627 } makeva_list;
1628
1629 /* Tell the caller how many bytes to allocate for a makeva_list with NARGS
1630    arguments and whose largest argument is MAX_ARG_SIZE bytes.  This
1631    way the caller can use alloca, malloc, or some other allocator.  */
1632 unsigned int
1633 makeva_size (nargs, max_arg_size)
1634      unsigned int nargs;
1635      unsigned int max_arg_size;
1636 {
1637   return sizeof (makeva_list) + nargs * max_arg_size;
1638 }
1639
1640 /* Start working on LIST with NARGS arguments and whose largest
1641    argument is MAX_ARG_SIZE bytes.  */
1642 void
1643 makeva_start (list, nargs, max_arg_size)
1644      makeva_list *list;
1645      unsigned int nargs;
1646      unsigned int max_arg_size;
1647 {
1648   list->nargs = nargs;
1649   list->max_arg_size = max_arg_size;
1650 #if defined (MAKEVA_START)
1651   MAKEVA_START (list);
1652 #else
1653   list->argindex = 0;
1654 #endif
1655 }
1656
1657 /* Add ARG to LIST.  */
1658 void
1659 makeva_arg (list, argaddr, argsize)
1660      makeva_list *list;
1661      PTR argaddr;
1662      unsigned int argsize;
1663 {
1664 #if defined (MAKEVA_ARG)
1665   MAKEVA_ARG (list, argaddr, argsize);
1666 #else
1667   memcpy (&list->arg_bytes[list->argindex], argaddr, argsize);
1668   list->argindex += argsize;
1669 #endif
1670 }
1671
1672 /* From LIST, for which makeva_arg has been called for each arg,
1673    return a va_list containing the args.  */
1674 va_list
1675 makeva_end (list)
1676      makeva_list *list;
1677 {
1678 #if defined (MAKEVA_END)
1679   MAKEVA_END (list);
1680 #else
1681   /* This works if a va_list is just a pointer to the arguments.  */
1682   return (va_list) list->arg_bytes;
1683 #endif
1684 }
1685 \f
1686 /* ARGSUSED */
1687 static void
1688 printf_command (arg, from_tty)
1689      char *arg;
1690      int from_tty;
1691 {
1692   register char *f;
1693   register char *s = arg;
1694   char *string;
1695   value *val_args;
1696   int nargs = 0;
1697   int allocated_args = 20;
1698   va_list args_to_vprintf;
1699
1700   val_args = (value *) xmalloc (allocated_args * sizeof (value));
1701
1702   if (s == 0)
1703     error_no_arg ("format-control string and values to print");
1704
1705   /* Skip white space before format string */
1706   while (*s == ' ' || *s == '\t') s++;
1707
1708   /* A format string should follow, enveloped in double quotes */
1709   if (*s++ != '"')
1710     error ("Bad format string, missing '\"'.");
1711
1712   /* Parse the format-control string and copy it into the string STRING,
1713      processing some kinds of escape sequence.  */
1714
1715   f = string = (char *) alloca (strlen (s) + 1);
1716   while (*s != '"')
1717     {
1718       int c = *s++;
1719       switch (c)
1720         {
1721         case '\0':
1722           error ("Bad format string, non-terminated '\"'.");
1723           /* doesn't return */
1724
1725         case '\\':
1726           switch (c = *s++)
1727             {
1728             case '\\':
1729               *f++ = '\\';
1730               break;
1731             case 'n':
1732               *f++ = '\n';
1733               break;
1734             case 't':
1735               *f++ = '\t';
1736               break;
1737             case 'r':
1738               *f++ = '\r';
1739               break;
1740             case '"':
1741               *f++ = '"';
1742               break;
1743             default:
1744               /* ??? TODO: handle other escape sequences */
1745               error ("Unrecognized \\ escape character in format string.");
1746             }
1747           break;
1748
1749         default:
1750           *f++ = c;
1751         }
1752     }
1753
1754   /* Skip over " and following space and comma.  */
1755   s++;
1756   *f++ = '\0';
1757   while (*s == ' ' || *s == '\t') s++;
1758
1759   if (*s != ',' && *s != 0)
1760     error ("Invalid argument syntax");
1761
1762   if (*s == ',') s++;
1763   while (*s == ' ' || *s == '\t') s++;
1764
1765   {
1766     /* Now scan the string for %-specs and see what kinds of args they want.
1767        argclass[I] classifies the %-specs so we can give vprintf something
1768        of the right size.  */
1769  
1770     enum argclass {int_arg, string_arg, double_arg, long_long_arg};
1771     enum argclass *argclass;
1772     int nargs_wanted;
1773     int lcount;
1774     int i;
1775     makeva_list *args_makeva;
1776
1777     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1778     nargs_wanted = 0;
1779     f = string;
1780     while (*f)
1781       if (*f++ == '%')
1782         {
1783           lcount = 0;
1784           while (strchr ("0123456789.hlL-+ #", *f)) 
1785             {
1786               if (*f == 'l' || *f == 'L')
1787                 lcount++;
1788               f++;
1789             }
1790           if (*f == 's')
1791             argclass[nargs_wanted++] = string_arg;
1792           else if (*f == 'e' || *f == 'f' || *f == 'g')
1793             argclass[nargs_wanted++] = double_arg;
1794           else if (lcount > 1)
1795             argclass[nargs_wanted++] = long_long_arg;
1796           else if (*f != '%')
1797             argclass[nargs_wanted++] = int_arg;
1798           f++;
1799         }
1800  
1801     /* Now, parse all arguments and evaluate them.
1802        Store the VALUEs in VAL_ARGS.  */
1803  
1804     while (*s != '\0')
1805       {
1806         char *s1;
1807         if (nargs == allocated_args)
1808           val_args = (value *) xrealloc ((char *) val_args,
1809                                          (allocated_args *= 2)
1810                                          * sizeof (value));
1811         s1 = s;
1812         val_args[nargs] = parse_to_comma_and_eval (&s1);
1813  
1814         /* If format string wants a float, unchecked-convert the value to
1815            floating point of the same size */
1816  
1817         if (argclass[nargs] == double_arg)
1818           {
1819             if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (float))
1820               VALUE_TYPE (val_args[nargs]) = builtin_type_float;
1821             if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (double))
1822               VALUE_TYPE (val_args[nargs]) = builtin_type_double;
1823           }
1824         nargs++;
1825         s = s1;
1826         if (*s == ',')
1827           s++;
1828       }
1829  
1830     if (nargs != nargs_wanted)
1831       error ("Wrong number of arguments for specified format-string");
1832
1833     /* Now lay out an argument-list containing the arguments
1834        as doubles, integers and C pointers.  */
1835
1836     args_makeva = (makeva_list *)
1837       alloca (makeva_size (nargs, sizeof (double)));
1838     makeva_start (args_makeva, nargs, sizeof (double));
1839     for (i = 0; i < nargs; i++)
1840       {
1841         if (argclass[i] == string_arg)
1842           {
1843             char *str;
1844             CORE_ADDR tem;
1845             int j;
1846             tem = value_as_pointer (val_args[i]);
1847  
1848             /* This is a %s argument.  Find the length of the string.  */
1849             for (j = 0; ; j++)
1850               {
1851                 char c;
1852                 QUIT;
1853                 read_memory (tem + j, &c, 1);
1854                 if (c == 0)
1855                   break;
1856               }
1857  
1858             /* Copy the string contents into a string inside GDB.  */
1859             str = (char *) alloca (j + 1);
1860             read_memory (tem, str, j);
1861             str[j] = 0;
1862  
1863             /* Pass address of internal copy as the arg to vprintf.  */
1864             makeva_arg (args_makeva, &str, sizeof (str));
1865           }
1866         else if (VALUE_TYPE (val_args[i])->code == TYPE_CODE_FLT)
1867           {
1868             double val = value_as_double (val_args[i]);
1869             makeva_arg (args_makeva, &val, sizeof (val));
1870           }
1871         else
1872 #ifdef CC_HAS_LONG_LONG
1873           if (argclass[i] == long_long_arg)
1874             {
1875               long long val = value_as_long (val_args[i]);
1876               makeva_arg (args_makeva, &val, sizeof (val));
1877             }
1878           else
1879 #endif
1880             {
1881               long val = value_as_long (val_args[i]);
1882               makeva_arg (args_makeva, &val, sizeof (val));
1883             }
1884       }
1885     args_to_vprintf = makeva_end (args_makeva);
1886   }
1887
1888   /* FIXME: We should be using vprintf_filtered, but as long as it has an
1889      arbitrary limit that is unacceptable.  Correct fix is for vprintf_filtered
1890      to scan down the format string so it knows how big a buffer it needs.
1891
1892      But for now, just force out any pending output, so at least the output
1893      appears in the correct order.  */
1894   wrap_here ((char *)NULL);
1895   vprintf (string, args_to_vprintf);
1896 }
1897 \f
1898 /* Dump a specified section of assembly code.  With no command line
1899    arguments, this command will dump the assembly code for the
1900    function surrounding the pc value in the selected frame.  With one
1901    argument, it will dump the assembly code surrounding that pc value.
1902    Two arguments are interpeted as bounds within which to dump
1903    assembly.  */
1904
1905 /* ARGSUSED */
1906 static void
1907 disassemble_command (arg, from_tty)
1908      char *arg;
1909      int from_tty;
1910 {
1911   CORE_ADDR low, high;
1912   char *name;
1913   CORE_ADDR pc;
1914   char *space_index;
1915
1916   name = NULL;
1917   if (!arg)
1918     {
1919       if (!selected_frame)
1920         error ("No frame selected.\n");
1921
1922       pc = get_frame_pc (selected_frame);
1923       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
1924         error ("No function contains program counter for selected frame.\n");
1925     }
1926   else if (!(space_index = (char *) strchr (arg, ' ')))
1927     {
1928       /* One argument.  */
1929       pc = parse_and_eval_address (arg);
1930       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
1931         error ("No function contains specified address.\n");
1932     }
1933   else
1934     {
1935       /* Two arguments.  */
1936       *space_index = '\0';
1937       low = parse_and_eval_address (arg);
1938       high = parse_and_eval_address (space_index + 1);
1939     }
1940
1941   printf_filtered ("Dump of assembler code ");
1942   if (name != NULL)
1943     {
1944       printf_filtered ("for function %s:\n", name);
1945     }
1946   else
1947     {
1948       printf_filtered ("from %s ", local_hex_string(low));
1949       printf_filtered ("to %s:\n", local_hex_string(high));
1950     }
1951
1952   /* Dump the specified range.  */
1953   for (pc = low; pc < high; )
1954     {
1955       QUIT;
1956       print_address (pc, stdout);
1957       printf_filtered (":\t");
1958       pc += print_insn (pc, stdout);
1959       printf_filtered ("\n");
1960     }
1961   printf_filtered ("End of assembler dump.\n");
1962   fflush (stdout);
1963 }
1964
1965 \f
1966 void
1967 _initialize_printcmd ()
1968 {
1969   current_display_number = -1;
1970
1971   add_info ("address", address_info,
1972            "Describe where variable VAR is stored.");
1973
1974   add_com ("x", class_vars, x_command,
1975            "Examine memory: x/FMT ADDRESS.\n\
1976 ADDRESS is an expression for the memory address to examine.\n\
1977 FMT is a repeat count followed by a format letter and a size letter.\n\
1978 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
1979   t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
1980 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
1981 The specified number of objects of the specified size are printed\n\
1982 according to the format.\n\n\
1983 Defaults for format and size letters are those previously used.\n\
1984 Default count is 1.  Default address is following last thing printed\n\
1985 with this command or \"print\".");
1986
1987   add_com ("disassemble", class_vars, disassemble_command,
1988            "Disassemble a specified section of memory.\n\
1989 Default is the function surrounding the pc of the selected frame.\n\
1990 With a single argument, the function surrounding that address is dumped.\n\
1991 Two arguments are taken as a range of memory to dump.");
1992
1993 #if 0
1994   add_com ("whereis", class_vars, whereis_command,
1995            "Print line number and file of definition of variable.");
1996 #endif
1997   
1998   add_info ("display", display_info,
1999             "Expressions to display when program stops, with code numbers.");
2000
2001   add_cmd ("undisplay", class_vars, undisplay_command,
2002            "Cancel some expressions to be displayed when program stops.\n\
2003 Arguments are the code numbers of the expressions to stop displaying.\n\
2004 No argument means cancel all automatic-display expressions.\n\
2005 \"delete display\" has the same effect as this command.\n\
2006 Do \"info display\" to see current list of code numbers.",
2007                   &cmdlist);
2008
2009   add_com ("display", class_vars, display_command,
2010            "Print value of expression EXP each time the program stops.\n\
2011 /FMT may be used before EXP as in the \"print\" command.\n\
2012 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2013 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2014 and examining is done as in the \"x\" command.\n\n\
2015 With no argument, display all currently requested auto-display expressions.\n\
2016 Use \"undisplay\" to cancel display requests previously made.");
2017
2018   add_cmd ("display", class_vars, enable_display, 
2019            "Enable some expressions to be displayed when program stops.\n\
2020 Arguments are the code numbers of the expressions to resume displaying.\n\
2021 No argument means enable all automatic-display expressions.\n\
2022 Do \"info display\" to see current list of code numbers.", &enablelist);
2023
2024   add_cmd ("display", class_vars, disable_display_command, 
2025            "Disable 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 disable all automatic-display expressions.\n\
2028 Do \"info display\" to see current list of code numbers.", &disablelist);
2029
2030   add_cmd ("display", class_vars, undisplay_command, 
2031            "Cancel some expressions to be displayed when program stops.\n\
2032 Arguments are the code numbers of the expressions to stop displaying.\n\
2033 No argument means cancel all automatic-display expressions.\n\
2034 Do \"info display\" to see current list of code numbers.", &deletelist);
2035
2036   add_com ("printf", class_vars, printf_command,
2037         "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2038 This is useful for formatted output in user-defined commands.");
2039   add_com ("output", class_vars, output_command,
2040            "Like \"print\" but don't put in value history and don't print newline.\n\
2041 This is useful in user-defined commands.");
2042
2043   add_prefix_cmd ("set", class_vars, set_command,
2044 "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2045 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2046 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2047 with $), a register (a few standard names starting with $), or an actual\n\
2048 variable in the program being debugged.  EXP is any valid expression.\n\
2049 Use \"set variable\" for variables with names identical to set subcommands.\n\
2050 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2051 You can see these environment settings with the \"show\" command.",
2052                   &setlist, "set ", 1, &cmdlist);
2053
2054   /* "call" is the same as "set", but handy for dbx users to call fns. */
2055   add_com ("call", class_vars, call_command,
2056            "Call a function in the program.\n\
2057 The argument is the function name and arguments, in the notation of the\n\
2058 current working language.  The result is printed and saved in the value\n\
2059 history, if it is not void.");
2060
2061   add_cmd ("variable", class_vars, set_command,
2062 "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2063 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2064 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
2065 with $), a register (a few standard names starting with $), or an actual\n\
2066 variable in the program being debugged.  EXP is any valid expression.\n\
2067 This may usually be abbreviated to simply \"set\".",
2068            &setlist);
2069
2070   add_com ("print", class_vars, print_command,
2071            concat ("Print value of expression EXP.\n\
2072 Variables accessible are those of the lexical environment of the selected\n\
2073 stack frame, plus all those whose scope is global or an entire file.\n\
2074 \n\
2075 $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
2076 $$NUM refers to NUM'th value back from the last one.\n\
2077 Names starting with $ refer to registers (with the values they would have\n\
2078 if the program were to return to the stack frame now selected, restoring\n\
2079 all registers saved by frames farther in) or else to debugger\n\
2080 \"convenience\" variables (any such name not a known register).\n\
2081 Use assignment expressions to give values to convenience variables.\n",
2082                    "\n\
2083 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2084 @ is a binary operator for treating consecutive data objects\n\
2085 anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
2086 element is FOO, whose second element is stored in the space following\n\
2087 where FOO is stored, etc.  FOO must be an expression whose value\n\
2088 resides in memory.\n",
2089                    "\n\
2090 EXP may be preceded with /FMT, where FMT is a format letter\n\
2091 but no count or size letter (see \"x\" command).", NULL));
2092   add_com_alias ("p", "print", class_vars, 1);
2093
2094   add_com ("inspect", class_vars, inspect_command,
2095 "Same as \"print\" command, except that if you are running in the epoch\n\
2096 environment, the value is printed in its own window.");
2097
2098   add_show_from_set (
2099       add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,
2100                    (char *)&max_symbolic_offset,
2101         "Set the largest offset that will be printed in <symbol+1234> form.",
2102                    &setprintlist),
2103       &showprintlist);
2104   add_show_from_set (
2105       add_set_cmd ("symbol-filename", no_class, var_boolean,
2106                    (char *)&print_symbol_filename,
2107         "Set printing of source filename and line number with <symbol>.",
2108                    &setprintlist),
2109       &showprintlist);
2110 }