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