]> Git Repo - binutils.git/blob - gdb/valprint.c
More filename renaming.
[binutils.git] / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 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 <stdio.h>
21 #include <string.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "target.h"
28 #include "obstack.h"
29 #include "language.h"
30
31 #include <errno.h>
32 extern int sys_nerr;
33 extern char *sys_errlist[];
34
35 extern void print_scalar_formatted();   /* printcmd.c */
36 extern void print_address_demangle();   /* printcmd.c */
37 extern int demangle;    /* whether to print C++ syms raw or source-form */
38
39 /* Maximum number of chars to print for a string pointer value
40    or vector contents, or UINT_MAX for no limit.  */
41
42 static unsigned int print_max;
43
44 static void type_print_varspec_suffix ();
45 static void type_print_varspec_prefix ();
46 static void type_print_base ();
47 static void type_print_method_args ();
48
49 /* Default input and output radixes, and output format letter.  */
50
51 unsigned input_radix = 10;
52 unsigned output_radix = 10;
53 int output_format = 0;
54
55
56 char **unsigned_type_table;
57 char **signed_type_table;
58 char **float_type_table;
59
60
61 /* Print repeat counts if there are more than this
62    many repetitions of an element in an array.  */
63 #define REPEAT_COUNT_THRESHOLD  10
64
65 /* Define a mess of print controls.  */
66
67 int prettyprint;        /* Controls pretty printing of structures */
68 int vtblprint;          /* Controls printing of vtbl's */
69 int unionprint;         /* Controls printing of nested unions.  */
70 int arrayprint;         /* Controls pretty printing of arrays.  */
71 int addressprint;       /* Controls pretty printing of addresses.  */
72 int objectprint;        /* Controls looking up an object's derived type
73                            using what we find in its vtables.  */
74
75 struct obstack dont_print_obstack;
76
77 static void cplus_val_print ();
78 \f
79 /* Print the character string STRING, printing at most LENGTH characters.
80    Printing stops early if the number hits print_max; repeat counts
81    are printed as appropriate.  Print ellipses at the end if we
82    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.  */
83
84 void
85 print_string (stream, string, length, force_ellipses)
86      FILE *stream;
87      char *string;
88      unsigned int length;
89      int force_ellipses;
90 {
91   register unsigned int i;
92   unsigned int things_printed = 0;
93   int in_quotes = 0;
94   int need_comma = 0;
95   extern int inspect_it;
96
97   if (length == 0)
98     {
99       fputs_filtered ("\"\"", stdout);
100       return;
101     }
102
103   for (i = 0; i < length && things_printed < print_max; ++i)
104     {
105       /* Position of the character we are examining
106          to see whether it is repeated.  */
107       unsigned int rep1;
108       /* Number of repetitions we have detected so far.  */
109       unsigned int reps;
110
111       QUIT;
112
113       if (need_comma)
114         {
115           fputs_filtered (", ", stream);
116           need_comma = 0;
117         }
118
119       rep1 = i + 1;
120       reps = 1;
121       while (rep1 < length && string[rep1] == string[i])
122         {
123           ++rep1;
124           ++reps;
125         }
126
127       if (reps > REPEAT_COUNT_THRESHOLD)
128         {
129           if (in_quotes)
130             {
131               if (inspect_it)
132                 fputs_filtered ("\\\", ", stream);
133               else
134                 fputs_filtered ("\", ", stream);
135               in_quotes = 0;
136             }
137           fputs_filtered ("'", stream);
138           printchar (string[i], stream, '\'');
139           fprintf_filtered (stream, "' <repeats %u times>", reps);
140           i = rep1 - 1;
141           things_printed += REPEAT_COUNT_THRESHOLD;
142           need_comma = 1;
143         }
144       else
145         {
146           if (!in_quotes)
147             {
148               if (inspect_it)
149                 fputs_filtered ("\\\"", stream);
150               else
151                 fputs_filtered ("\"", stream);
152               in_quotes = 1;
153             }
154           printchar (string[i], stream, '"');
155           ++things_printed;
156         }
157     }
158
159   /* Terminate the quotes if necessary.  */
160   if (in_quotes)
161     {
162       if (inspect_it)
163         fputs_filtered ("\\\"", stream);
164       else
165         fputs_filtered ("\"", stream);
166     }
167
168   if (force_ellipses || i < length)
169     fputs_filtered ("...", stream);
170 }
171
172 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
173    on STREAM.  */
174
175 void
176 print_floating (valaddr, type, stream)
177      char *valaddr;
178      struct type *type;
179      FILE *stream;
180 {
181   double doub;
182   int inv;
183   unsigned len = TYPE_LENGTH (type);
184   
185 #if defined (IEEE_FLOAT)
186
187   /* Check for NaN's.  Note that this code does not depend on us being
188      on an IEEE conforming system.  It only depends on the target
189      machine using IEEE representation.  This means (a)
190      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
191      be defined for systems like the 68881, which uses IEEE
192      representation, but is not IEEE conforming.  */
193
194   {
195     long low, high;
196     /* Is the sign bit 0?  */
197     int nonnegative;
198     /* Is it is a NaN (i.e. the exponent is all ones and
199        the fraction is nonzero)?  */
200     int is_nan;
201
202     if (len == sizeof (float))
203       {
204         /* It's single precision. */
205         bcopy (valaddr, &low, sizeof (low));
206         /* target -> host.  */
207         SWAP_TARGET_AND_HOST (&low, sizeof (float));
208         nonnegative = low >= 0;
209         is_nan = ((((low >> 23) & 0xFF) == 0xFF) 
210                   && 0 != (low & 0x7FFFFF));
211         low &= 0x7fffff;
212         high = 0;
213       }
214     else
215       {
216         /* It's double precision.  Get the high and low words.  */
217
218 #if TARGET_BYTE_ORDER == BIG_ENDIAN
219           bcopy (valaddr+4, &low,  sizeof (low));
220           bcopy (valaddr+0, &high, sizeof (high));
221 #else
222           bcopy (valaddr+0, &low,  sizeof (low));
223           bcopy (valaddr+4, &high, sizeof (high));
224 #endif
225           SWAP_TARGET_AND_HOST (&low, sizeof (low));
226           SWAP_TARGET_AND_HOST (&high, sizeof (high));
227           nonnegative = high >= 0;
228           is_nan = (((high >> 20) & 0x7ff) == 0x7ff
229                     && ! ((((high & 0xfffff) == 0)) && (low == 0)));
230           high &= 0xfffff;
231         }
232
233     if (is_nan)
234       {
235         /* The meaning of the sign and fraction is not defined by IEEE.
236            But the user might know what they mean.  For example, they
237            (in an implementation-defined manner) distinguish between
238            signaling and quiet NaN's.  */
239         if (high)
240           fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
241                             high, low);
242         else
243           fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
244         return;
245       }
246   }
247 #endif /* IEEE_FLOAT.  */
248
249   doub = unpack_double (type, valaddr, &inv);
250   if (inv)
251     fprintf_filtered (stream, "<invalid float value>");
252   else
253     fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
254 }
255
256 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
257 static void
258 print_hex_chars (stream, valaddr, len)
259      FILE *stream;
260      unsigned char *valaddr;
261      unsigned len;
262 {
263   unsigned char *p;
264   
265   fprintf_filtered (stream, "0x");
266 #if TARGET_BYTE_ORDER == BIG_ENDIAN
267   for (p = valaddr;
268        p < valaddr + len;
269        p++)
270 #else /* Little endian.  */
271   for (p = valaddr + len - 1;
272        p >= valaddr;
273        p--)
274 #endif
275     {
276       fprintf_filtered (stream, "%02x", *p);
277     }
278 }
279 \f
280 /* Print the value VAL in C-ish syntax on stream STREAM.
281    FORMAT is a format-letter, or 0 for print in natural format of data type.
282    If the object printed is a string pointer, returns
283    the number of string bytes printed.  */
284
285 int
286 value_print (val, stream, format, pretty)
287      value val;
288      FILE *stream;
289      char format;
290      enum val_prettyprint pretty;
291 {
292   register unsigned int i, n, typelen;
293
294   if (val == 0)
295     {
296       printf_filtered ("<address of value unknown>");
297       return 0;
298     }
299   if (VALUE_OPTIMIZED_OUT (val))
300     {
301       printf_filtered ("<value optimized out>");
302       return 0;
303     }
304
305   /* A "repeated" value really contains several values in a row.
306      They are made by the @ operator.
307      Print such values as if they were arrays.  */
308
309   else if (VALUE_REPEATED (val))
310     {
311       n = VALUE_REPETITIONS (val);
312       typelen = TYPE_LENGTH (VALUE_TYPE (val));
313       fprintf_filtered (stream, "{");
314       /* Print arrays of characters using string syntax.  */
315       if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
316           && format == 0)
317         print_string (stream, VALUE_CONTENTS (val), n, 0);
318       else
319         {
320           unsigned int things_printed = 0;
321           
322           for (i = 0; i < n && things_printed < print_max; i++)
323             {
324               /* Position of the array element we are examining to see
325                  whether it is repeated.  */
326               unsigned int rep1;
327               /* Number of repetitions we have detected so far.  */
328               unsigned int reps;
329
330               if (i != 0)
331                 fprintf_filtered (stream, ", ");
332               wrap_here ("");
333
334               rep1 = i + 1;
335               reps = 1;
336               while (rep1 < n
337                      && !bcmp (VALUE_CONTENTS (val) + typelen * i,
338                                VALUE_CONTENTS (val) + typelen * rep1, typelen))
339                 {
340                   ++reps;
341                   ++rep1;
342                 }
343
344               if (reps > REPEAT_COUNT_THRESHOLD)
345                 {
346                   val_print (VALUE_TYPE (val),
347                              VALUE_CONTENTS (val) + typelen * i,
348                              VALUE_ADDRESS (val) + typelen * i,
349                              stream, format, 1, 0, pretty);
350                   fprintf (stream, " <repeats %u times>", reps);
351                   i = rep1 - 1;
352                   things_printed += REPEAT_COUNT_THRESHOLD;
353                 }
354               else
355                 {
356                   val_print (VALUE_TYPE (val),
357                              VALUE_CONTENTS (val) + typelen * i,
358                              VALUE_ADDRESS (val) + typelen * i,
359                              stream, format, 1, 0, pretty);
360                   things_printed++;
361                 }
362             }
363           if (i < n)
364             fprintf_filtered (stream, "...");
365         }
366       fprintf_filtered (stream, "}");
367       return n * typelen;
368     }
369   else
370     {
371       struct type *type = VALUE_TYPE (val);
372
373       /* If it is a pointer, indicate what it points to.
374
375          Print type also if it is a reference.
376
377          C++: if it is a member pointer, we will take care
378          of that when we print it.  */
379       if (TYPE_CODE (type) == TYPE_CODE_PTR
380           || TYPE_CODE (type) == TYPE_CODE_REF)
381         {
382           /* Hack:  remove (char *) for char strings.  Their
383              type is indicated by the quoted string anyway. */
384           if (TYPE_CODE (type) == TYPE_CODE_PTR
385               && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char)
386               && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT
387               && !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
388             {
389                 /* Print nothing */
390             }
391           else
392             {
393               fprintf_filtered (stream, "(");
394               type_print (type, "", stream, -1);
395               fprintf_filtered (stream, ") ");
396             }
397         }
398       return val_print (type, VALUE_CONTENTS (val),
399                         VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
400     }
401 }
402
403 /* Return truth value for assertion that TYPE is of the type
404    "pointer to virtual function".  */
405 static int
406 is_vtbl_ptr_type(type)
407      struct type *type;
408 {
409   char *typename = type_name_no_tag (type);
410   static const char vtbl_ptr_name[] =
411     { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
412
413   return (typename != NULL && !strcmp(typename, vtbl_ptr_name));
414 }
415
416 /* Return truth value for the assertion that TYPE is of the type
417    "pointer to virtual function table".  */
418 static int
419 is_vtbl_member(type)
420      struct type *type;
421 {
422   if (TYPE_CODE (type) == TYPE_CODE_PTR)
423     type = TYPE_TARGET_TYPE (type);
424   else
425     return 0;
426
427   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
428       && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
429     /* Virtual functions tables are full of pointers to virtual functions.  */
430     return is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
431   return 0;
432 }
433 \f
434 /* Mutually recursive subroutines of cplus_val_print and val_print to print out
435    a structure's fields: val_print_fields and cplus_val_print.
436
437    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
438    same meanings as in cplus_val_print and val_print.
439
440    DONT_PRINT is an array of baseclass types that we
441    should not print, or zero if called from top level.  */
442 static void
443 val_print_fields (type, valaddr, stream, format, recurse, pretty, dont_print)
444      struct type *type;
445      char *valaddr;
446      FILE *stream;
447      char format;
448      int recurse;
449      enum val_prettyprint pretty;
450      struct type **dont_print;
451 {
452   int i, len, n_baseclasses;
453
454   check_stub_type (type);
455
456   fprintf_filtered (stream, "{");
457   len = TYPE_NFIELDS (type);
458   n_baseclasses = TYPE_N_BASECLASSES (type);
459
460   /* Print out baseclasses such that we don't print
461      duplicates of virtual baseclasses.  */
462   if (n_baseclasses > 0)
463     cplus_val_print (type, valaddr, stream, format, recurse+1, pretty, dont_print);
464
465   if (!len && n_baseclasses == 1)
466     fprintf_filtered (stream, "<No data fields>");
467   else
468     {
469       extern int inspect_it;
470       int fields_seen = 0;
471
472       for (i = n_baseclasses; i < len; i++)
473         {
474           /* Check if static field */
475           if (TYPE_FIELD_STATIC (type, i))
476             continue;
477           if (fields_seen)
478             fprintf_filtered (stream, ", ");
479           else if (n_baseclasses > 0)
480             {
481               fprintf_filtered (stream, "\n");
482               print_spaces_filtered (2 + 2 * recurse, stream);
483               fputs_filtered ("members of ", stream);
484               fputs_filtered (type_name_no_tag (type), stream);
485               fputs_filtered (": ", stream);
486             }
487           fields_seen = 1;
488
489           if (pretty)
490             {
491               fprintf_filtered (stream, "\n");
492               print_spaces_filtered (2 + 2 * recurse, stream);
493             }
494           else 
495             {
496               wrap_here (n_spaces (2 + 2 * recurse));
497             }
498           if (inspect_it)
499             {
500               if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
501                 fputs_filtered ("\"( ptr \"", stream);
502               else
503                 fputs_filtered ("\"( nodef \"", stream);
504               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
505               fputs_filtered ("\" \"", stream);
506               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
507               fputs_filtered ("\") \"", stream);
508             }
509           else
510             {
511               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
512               fputs_filtered (" = ", stream);
513             }
514           if (TYPE_FIELD_PACKED (type, i))
515             {
516               value v;
517
518               /* Bitfields require special handling, especially due to byte
519                  order problems.  */
520               v = value_from_longest (TYPE_FIELD_TYPE (type, i),
521                                    unpack_field_as_long (type, valaddr, i));
522
523               val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
524                          stream, format, 0, recurse + 1, pretty);
525             }
526           else
527             {
528               val_print (TYPE_FIELD_TYPE (type, i), 
529                          valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
530                          0, stream, format, 0, recurse + 1, pretty);
531             }
532         }
533       if (pretty)
534         {
535           fprintf_filtered (stream, "\n");
536           print_spaces_filtered (2 * recurse, stream);
537         }
538     }
539   fprintf_filtered (stream, "}");
540 }
541
542 /* Special val_print routine to avoid printing multiple copies of virtual
543    baseclasses.  */
544
545 static void
546 cplus_val_print (type, valaddr, stream, format, recurse, pretty, dont_print)
547      struct type *type;
548      char *valaddr;
549      FILE *stream;
550      char format;
551      int recurse;
552      enum val_prettyprint pretty;
553      struct type **dont_print;
554 {
555   struct obstack tmp_obstack;
556   struct type **last_dont_print
557     = (struct type **)obstack_next_free (&dont_print_obstack);
558   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
559
560   if (dont_print == 0)
561     {
562       /* If we're at top level, carve out a completely fresh
563          chunk of the obstack and use that until this particular
564          invocation returns.  */
565       tmp_obstack = dont_print_obstack;
566       /* Bump up the high-water mark.  Now alpha is omega.  */
567       obstack_finish (&dont_print_obstack);
568     }
569
570   for (i = 0; i < n_baseclasses; i++)
571     {
572       char *baddr;
573       int err;
574
575       if (BASETYPE_VIA_VIRTUAL (type, i))
576         {
577           struct type **first_dont_print
578             = (struct type **)obstack_base (&dont_print_obstack);
579
580           int j = (struct type **)obstack_next_free (&dont_print_obstack)
581             - first_dont_print;
582
583           while (--j >= 0)
584             if (TYPE_BASECLASS (type, i) == first_dont_print[j])
585               goto flush_it;
586
587           obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
588         }
589
590       baddr = baseclass_addr (type, i, valaddr, 0, &err);
591       if (err == 0 && baddr == 0)
592         error ("could not find virtual baseclass `%s'\n",
593                type_name_no_tag (TYPE_BASECLASS (type, i)));
594
595       fprintf_filtered (stream, "\n");
596       if (pretty)
597         print_spaces_filtered (2 + 2 * recurse, stream);
598       fputs_filtered ("<", stream);
599       fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream);
600       fputs_filtered ("> = ", stream);
601       if (err != 0)
602         fprintf_filtered (stream, "<invalid address 0x%x>", baddr);
603       else
604         val_print_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
605                           recurse, pretty,
606                           (struct type **)obstack_base (&dont_print_obstack));
607     flush_it:
608       ;
609     }
610
611   if (dont_print == 0)
612     {
613       /* Free the space used to deal with the printing
614          of this type from top level.  */
615       obstack_free (&dont_print_obstack, last_dont_print);
616       /* Reset watermark so that we can continue protecting
617          ourselves from whatever we were protecting ourselves.  */
618       dont_print_obstack = tmp_obstack;
619     }
620 }
621
622 /* Print data of type TYPE located at VALADDR (within GDB),
623    which came from the inferior at address ADDRESS,
624    onto stdio stream STREAM according to FORMAT
625    (a letter or 0 for natural format).  The data at VALADDR
626    is in target byte order.
627
628    If the data are a string pointer, returns the number of
629    sting characters printed.
630
631    if DEREF_REF is nonzero, then dereference references,
632    otherwise just print them like pointers.
633
634    The PRETTY parameter controls prettyprinting.  */
635
636 int
637 val_print (type, valaddr, address, stream, format,
638            deref_ref, recurse, pretty)
639      struct type *type;
640      char *valaddr;
641      CORE_ADDR address;
642      FILE *stream;
643      char format;
644      int deref_ref;
645      int recurse;
646      enum val_prettyprint pretty;
647 {
648   register unsigned int i;
649   unsigned len;
650   struct type *elttype;
651   unsigned eltlen;
652   LONGEST val;
653   unsigned char c;
654
655   if (pretty == Val_pretty_default)
656     {
657       pretty = prettyprint ? Val_prettyprint : Val_no_prettyprint;
658     }
659   
660   QUIT;
661
662   check_stub_type (type);
663   
664   if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
665     {
666       fprintf_filtered (stream, "<unknown struct>");
667       fflush (stream);
668       return 0;
669     }
670   
671   switch (TYPE_CODE (type))
672     {
673     case TYPE_CODE_ARRAY:
674       /* FIXME: TYPE_LENGTH (type) is unsigned and therefore always
675          >= 0.  Is "> 0" meant? I'm not sure what an "array of
676          unspecified length" (mentioned in the comment for the else-part
677          of this if) is.  */
678       if (TYPE_LENGTH (type) >= 0
679           && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
680         {
681           elttype = TYPE_TARGET_TYPE (type);
682           eltlen = TYPE_LENGTH (elttype);
683           len = TYPE_LENGTH (type) / eltlen;
684           if (arrayprint)
685             print_spaces_filtered (2 + 2 * recurse, stream);
686           fprintf_filtered (stream, "{");
687           /* For an array of chars, print with string syntax.  */
688           if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
689               && (format == 0 || format == 's') )
690             print_string (stream, valaddr, len, 0);
691           else
692             {
693               unsigned int things_printed = 0;
694               
695               /* If this is a virtual function table, print the 0th
696                  entry specially, and the rest of the members normally.  */
697               if (is_vtbl_ptr_type (elttype))
698                 {
699                   fprintf_filtered (stream, "%d vtable entries", len-1);
700                   i = 1;
701                 }
702               else
703                 i = 0;
704
705               for (; i < len && things_printed < print_max; i++)
706                 {
707                   /* Position of the array element we are examining to see
708                      whether it is repeated.  */
709                   unsigned int rep1;
710                   /* Number of repetitions we have detected so far.  */
711                   unsigned int reps;
712                   
713                   if (i != 0)
714                     if (arrayprint)
715                       {
716                         fprintf_filtered (stream, ",\n");
717                         print_spaces_filtered (2 + 2 * recurse, stream);
718                       }
719                     else
720                       fprintf_filtered (stream, ", ");
721                     wrap_here (n_spaces (2 + 2 * recurse));
722                   
723                   rep1 = i + 1;
724                   reps = 1;
725                   while (rep1 < len
726                          && !bcmp (valaddr + i * eltlen,
727                                    valaddr + rep1 * eltlen, eltlen))
728                     {
729                       ++reps;
730                       ++rep1;
731                     }
732
733                   if (reps > REPEAT_COUNT_THRESHOLD)
734                     {
735                       val_print (elttype, valaddr + i * eltlen,
736                                  0, stream, format, deref_ref,
737                                  recurse + 1, pretty);
738                       fprintf_filtered (stream, " <repeats %u times>", reps);
739                       i = rep1 - 1;
740                       things_printed += REPEAT_COUNT_THRESHOLD;
741                     }
742                   else
743                     {
744                       val_print (elttype, valaddr + i * eltlen,
745                                  0, stream, format, deref_ref,
746                                  recurse + 1, pretty);
747                       things_printed++;
748                     }
749                 }
750               if (i < len)
751                 fprintf_filtered (stream, "...");
752             }
753           fprintf_filtered (stream, "}");
754           break;
755         }
756       /* Array of unspecified length: treat like pointer to first elt.  */
757       valaddr = (char *) &address;
758
759     case TYPE_CODE_PTR:
760       if (format && format != 's')
761         {
762           print_scalar_formatted (valaddr, type, format, 0, stream);
763           break;
764         }
765       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
766         {
767           struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
768           struct fn_field *f;
769           int j, len2;
770           char *kind = "";
771           CORE_ADDR addr;
772
773           addr = unpack_pointer (lookup_pointer_type (builtin_type_void),
774                                 valaddr);
775           if (addr < 128)
776             {
777               len = TYPE_NFN_FIELDS (domain);
778               for (i = 0; i < len; i++)
779                 {
780                   f = TYPE_FN_FIELDLIST1 (domain, i);
781                   len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
782
783                   for (j = 0; j < len2; j++)
784                     {
785                       QUIT;
786                       if (TYPE_FN_FIELD_VOFFSET (f, j) == addr)
787                         {
788                           kind = "virtual";
789                           goto common;
790                         }
791                     }
792                 }
793             }
794           else
795             {
796               struct symbol *sym = find_pc_function (addr);
797               if (sym == 0)
798                 error ("invalid pointer to member function");
799               len = TYPE_NFN_FIELDS (domain);
800               for (i = 0; i < len; i++)
801                 {
802                   f = TYPE_FN_FIELDLIST1 (domain, i);
803                   len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
804
805                   for (j = 0; j < len2; j++)
806                     {
807                       QUIT;
808                       if (!strcmp (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
809                         goto common;
810                     }
811                 }
812             }
813         common:
814           if (i < len)
815             {
816               fprintf_filtered (stream, "&");
817               type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
818               fprintf (stream, kind);
819               if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
820                   && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
821                 type_print_method_args
822                   (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
823                    TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
824               else
825                 type_print_method_args
826                   (TYPE_FN_FIELD_ARGS (f, j), "",
827                    TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
828               break;
829             }
830           fprintf_filtered (stream, "(");
831           type_print (type, "", stream, -1);
832           fprintf_filtered (stream, ") %d", (int) addr >> 3);
833         }
834       else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
835         {
836           struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
837
838           /* VAL is a byte offset into the structure type DOMAIN.
839              Find the name of the field for that offset and
840              print it.  */
841           int extra = 0;
842           int bits = 0;
843           len = TYPE_NFIELDS (domain);
844           /* @@ Make VAL into bit offset */
845           val = unpack_long (builtin_type_int, valaddr) << 3;
846           for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
847             {
848               int bitpos = TYPE_FIELD_BITPOS (domain, i);
849               QUIT;
850               if (val == bitpos)
851                 break;
852               if (val < bitpos && i != 0)
853                 {
854                   /* Somehow pointing into a field.  */
855                   i -= 1;
856                   extra = (val - TYPE_FIELD_BITPOS (domain, i));
857                   if (extra & 0x3)
858                     bits = 1;
859                   else
860                     extra >>= 3;
861                   break;
862                 }
863             }
864           if (i < len)
865             {
866               fprintf_filtered (stream, "&");
867               type_print_base (domain, stream, 0, 0);
868               fprintf_filtered (stream, "::");
869               fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
870               if (extra)
871                 fprintf_filtered (stream, " + %d bytes", extra);
872               if (bits)
873                 fprintf_filtered (stream, " (offset in bits)");
874               break;
875             }
876           fprintf_filtered (stream, "%d", val >> 3);
877         }
878       else
879         {
880           CORE_ADDR addr = unpack_pointer (type, valaddr);
881           elttype = TYPE_TARGET_TYPE (type);
882
883           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
884             {
885               /* Try to print what function it points to.  */
886               print_address_demangle (addr, stream, demangle);
887               /* Return value is irrelevant except for string pointers.  */
888               return 0;
889             }
890
891           if (addressprint && format != 's')
892             fprintf_filtered (stream, "0x%x", addr);
893
894           /* For a pointer to char or unsigned char,
895              also print the string pointed to, unless pointer is null.  */
896           i = 0;                /* Number of characters printed.  */
897           if (TYPE_LENGTH (elttype) == 1 
898               && TYPE_CODE (elttype) == TYPE_CODE_INT
899               && (format == 0 || format == 's')
900               && addr != 0
901               /* If print_max is UINT_MAX, the alloca below will fail.
902                  In that case don't try to print the string.  */
903               && print_max < UINT_MAX)
904             {
905               int first_addr_err = 0;
906               int errcode = 0;
907               
908               /* Get first character.  */
909               errcode = target_read_memory (addr, (char *)&c, 1);
910               if (errcode != 0)
911                 {
912                   /* First address out of bounds.  */
913                   first_addr_err = 1;
914                 }
915               else
916                 {
917                   /* A real string.  */
918                   char *string = (char *) alloca (print_max);
919
920                   /* If the loop ends by us hitting print_max characters,
921                      we need to have elipses at the end.  */
922                   int force_ellipses = 1;
923
924                   /* This loop always fetches print_max characters, even
925                      though print_string might want to print more or fewer
926                      (with repeated characters).  This is so that
927                      we don't spend forever fetching if we print
928                      a long string consisting of the same character
929                      repeated.  Also so we can do it all in one memory
930                      operation, which is faster.  However, this will be
931                      slower if print_max is set high, e.g. if you set
932                      print_max to 1000, not only will it take a long
933                      time to fetch short strings, but if you are near
934                      the end of the address space, it might not work.
935                      FIXME.  */
936                   QUIT;
937                   errcode = target_read_memory (addr, string, print_max);
938                   if (errcode != 0)
939                       force_ellipses = 0;
940                   else 
941                     for (i = 0; i < print_max; i++)
942                       if (string[i] == '\0')
943                         {
944                           force_ellipses = 0;
945                           break;
946                         }
947                   QUIT;
948
949                   if (addressprint)
950                     fputs_filtered (" ", stream);
951                   print_string (stream, string, i, force_ellipses);
952                 }
953
954               if (errcode != 0)
955                 {
956                   if (errcode == EIO)
957                     {
958                       fprintf_filtered (stream,
959                                         (" <Address 0x%x out of bounds>"
960                                          + first_addr_err),
961                                         addr + i);
962                     }
963                   else
964                     {
965                       if (errcode >= sys_nerr || errcode < 0)
966                         error ("Error reading memory address 0x%x: unknown error (%d).",
967                                addr + i, errcode);
968                       else
969                         error ("Error reading memory address 0x%x: %s.",
970                                addr + i, sys_errlist[errcode]);
971                     }
972                 }
973
974               fflush (stream);
975             }
976           else /* print vtbl's nicely */
977           if (is_vtbl_member(type))
978             {
979               CORE_ADDR vt_address = unpack_pointer (type, valaddr);
980
981               int vt_index = find_pc_misc_function (vt_address);
982               if (vt_index >= 0
983                   && vt_address == misc_function_vector[vt_index].address)
984                 {
985                   fputs_filtered (" <", stream);
986                   fputs_demangled (misc_function_vector[vt_index].name,
987                                    stream, 1);
988                   fputs_filtered (">", stream);
989                 }
990               if (vtblprint)
991                 {
992                   value vt_val;
993
994                   vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address);
995                   val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
996                              VALUE_ADDRESS (vt_val), stream, format,
997                              deref_ref, recurse + 1, pretty);
998                   if (pretty)
999                     {
1000                       fprintf_filtered (stream, "\n");
1001                       print_spaces_filtered (2 + 2 * recurse, stream);
1002                     }
1003                 }
1004               }
1005
1006           /* Return number of characters printed, plus one for the
1007              terminating null if we have "reached the end".  */
1008           return i + (print_max && i != print_max);
1009         }
1010       break;
1011
1012     case TYPE_CODE_MEMBER:
1013       error ("not implemented: member type in val_print");
1014       break;
1015
1016     case TYPE_CODE_REF:
1017       if (addressprint)
1018         {
1019           fprintf_filtered (stream, "@0x%lx",
1020                             unpack_long (builtin_type_int, valaddr));
1021           if (deref_ref)
1022             fputs_filtered (": ", stream);
1023         }
1024       /* De-reference the reference.  */
1025       if (deref_ref)
1026         {
1027           if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
1028             {
1029               value deref_val =
1030                 value_at
1031                   (TYPE_TARGET_TYPE (type),
1032                    unpack_pointer (lookup_pointer_type (builtin_type_void),
1033                                    valaddr));
1034               val_print (VALUE_TYPE (deref_val), VALUE_CONTENTS (deref_val),
1035                          VALUE_ADDRESS (deref_val), stream, format,
1036                          deref_ref, recurse + 1, pretty);
1037             }
1038           else
1039             fputs_filtered ("???", stream);
1040         }
1041       break;
1042
1043     case TYPE_CODE_UNION:
1044       if (recurse && !unionprint)
1045         {
1046           fprintf_filtered (stream, "{...}");
1047           break;
1048         }
1049       /* Fall through.  */
1050     case TYPE_CODE_STRUCT:
1051       if (vtblprint && is_vtbl_ptr_type(type))
1052         {
1053           /* Print the unmangled name if desired.  */
1054           print_address_demangle(*((int *) (valaddr +   /* FIXME bytesex */
1055               TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
1056               stream, demangle);
1057           break;
1058         }
1059       val_print_fields (type, valaddr, stream, format, recurse, pretty, 0);
1060       break;
1061
1062     case TYPE_CODE_ENUM:
1063       if (format)
1064         {
1065           print_scalar_formatted (valaddr, type, format, 0, stream);
1066           break;
1067         }
1068       len = TYPE_NFIELDS (type);
1069       val = unpack_long (builtin_type_int, valaddr);
1070       for (i = 0; i < len; i++)
1071         {
1072           QUIT;
1073           if (val == TYPE_FIELD_BITPOS (type, i))
1074             break;
1075         }
1076       if (i < len)
1077         fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1078       else
1079 #ifdef LONG_LONG
1080         fprintf_filtered (stream, "%lld", val);
1081 #else
1082         fprintf_filtered (stream, "%ld", val);
1083 #endif
1084       break;
1085
1086     case TYPE_CODE_FUNC:
1087       if (format)
1088         {
1089           print_scalar_formatted (valaddr, type, format, 0, stream);
1090           break;
1091         }
1092       /* FIXME, we should consider, at least for ANSI C language, eliminating
1093          the distinction made between FUNCs and POINTERs to FUNCs.  */
1094       fprintf_filtered (stream, "{");
1095       type_print (type, "", stream, -1);
1096       fprintf_filtered (stream, "} ");
1097       /* Try to print what function it points to, and its address.  */
1098       print_address_demangle (address, stream, demangle);
1099       break;
1100
1101     case TYPE_CODE_INT:
1102       if (format || output_format)
1103         {
1104           print_scalar_formatted (valaddr, type,
1105                                   format? format: output_format,
1106                                   0, stream);
1107           break;
1108         }
1109       if (TYPE_LENGTH (type) > sizeof (LONGEST))
1110         {
1111           if (TYPE_UNSIGNED (type))
1112             {
1113               /* First figure out whether the number in fact has zeros
1114                  in all its bytes more significant than least significant
1115                  sizeof (LONGEST) ones.  */
1116               char *p;
1117               /* Pointer to first (i.e. lowest address) nonzero character.  */
1118               char *first_addr;
1119               len = TYPE_LENGTH (type);
1120
1121 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1122               for (p = valaddr;
1123                    len > sizeof (LONGEST)
1124                    && p < valaddr + TYPE_LENGTH (type);
1125                    p++)
1126 #else /* Little endian.  */
1127               first_addr = valaddr;
1128               for (p = valaddr + TYPE_LENGTH (type);
1129                    len > sizeof (LONGEST) && p >= valaddr;
1130                    p--)
1131 #endif /* Little endian.  */
1132                 {
1133                   if (*p == 0)
1134                     len--;
1135                   else
1136                     break;
1137                 }
1138 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1139               first_addr = p;
1140 #endif
1141               
1142               if (len <= sizeof (LONGEST))
1143                 {
1144                   /* We can print it in decimal.  */
1145                   fprintf_filtered
1146                     (stream, 
1147 #if defined (LONG_LONG)
1148                      "%llu",
1149 #else
1150                      "%lu",
1151 #endif
1152                      unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
1153                 }
1154               else
1155                 {
1156                   /* It is big, so print it in hex.  */
1157                   print_hex_chars (stream, (unsigned char *)first_addr, len);
1158                 }
1159             }
1160           else
1161             {
1162               /* Signed.  One could assume two's complement (a reasonable
1163                  assumption, I think) and do better than this.  */
1164               print_hex_chars (stream, (unsigned char *)valaddr,
1165                                TYPE_LENGTH (type));
1166             }
1167           break;
1168         }
1169 #ifdef PRINT_TYPELESS_INTEGER
1170       PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
1171 #else
1172 #ifndef LONG_LONG
1173       fprintf_filtered (stream,
1174                         TYPE_UNSIGNED (type) ? "%u" : "%d",
1175                         unpack_long (type, valaddr));
1176 #else
1177       fprintf_filtered (stream,
1178                         TYPE_UNSIGNED (type) ? "%llu" : "%lld",
1179                         unpack_long (type, valaddr));
1180 #endif
1181 #endif
1182                         
1183       if (TYPE_LENGTH (type) == 1)
1184         {
1185           fprintf_filtered (stream, " '");
1186           printchar ((unsigned char) unpack_long (type, valaddr), 
1187                      stream, '\'');
1188           fprintf_filtered (stream, "'");
1189         }
1190       break;
1191
1192     case TYPE_CODE_FLT:
1193       if (format)
1194         print_scalar_formatted (valaddr, type, format, 0, stream);
1195       else
1196         print_floating (valaddr, type, stream);
1197       break;
1198
1199     case TYPE_CODE_VOID:
1200       fprintf_filtered (stream, "void");
1201       break;
1202
1203     case TYPE_CODE_UNDEF:
1204       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
1205          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1206          and no complete type for struct foo in that file.  */
1207       fprintf_filtered (stream, "<unknown struct>");
1208       break;
1209
1210     case TYPE_CODE_ERROR:
1211       fprintf_filtered (stream, "?");
1212       break;
1213
1214     case TYPE_CODE_RANGE:
1215       /* FIXME, we should not ever have to print one of these yet.  */
1216       fprintf_filtered (stream, "<range type>");
1217       break;
1218
1219     default:
1220       error ("Invalid type code in symbol table.");
1221     }
1222   fflush (stream);
1223   return 0;
1224 }
1225 \f
1226 /* Print a description of a type in the format of a 
1227    typedef for the current language.
1228    NEW is the new name for a type TYPE. */
1229 void
1230 typedef_print (type, new, stream)
1231    struct type *type;
1232    struct symbol *new;
1233    FILE *stream;
1234 {
1235    switch (current_language->la_language)
1236    {
1237 #ifdef _LANG_c
1238    case language_c:
1239    case language_cplus:
1240       fprintf_filtered(stream, "typedef ");
1241       type_print(type,"",stream,0);
1242       if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
1243          || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (new))),
1244                          SYMBOL_NAME (new)))
1245          fprintf_filtered(stream,  " %s", SYMBOL_NAME(new));
1246       break;
1247 #endif
1248 #ifdef _LANG_m2
1249    case language_m2:
1250       fprintf_filtered(stream, "TYPE ");
1251       if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
1252          strcmp (TYPE_NAME(SYMBOL_TYPE(new)),
1253                     SYMBOL_NAME(new)))
1254          fprintf_filtered(stream, "%s = ", SYMBOL_NAME(new));
1255       else
1256          fprintf_filtered(stream, "<builtin> = ");
1257       type_print(type,"",stream,0);
1258       break;
1259 #endif
1260    default:
1261       error("Language not supported.");
1262    }
1263    fprintf_filtered(stream, ";\n");
1264 }
1265
1266
1267 /* Print a description of a type TYPE
1268    in the form of a declaration of a variable named VARSTRING.
1269    (VARSTRING is demangled if necessary.)
1270    Output goes to STREAM (via stdio).
1271    If SHOW is positive, we show the contents of the outermost level
1272    of structure even if there is a type name that could be used instead.
1273    If SHOW is negative, we never show the details of elements' types.  */
1274
1275 void
1276 type_print (type, varstring, stream, show)
1277      struct type *type;
1278      char *varstring;
1279      FILE *stream;
1280      int show;
1281 {
1282   type_print_1 (type, varstring, stream, show, 0);
1283 }
1284
1285 /* LEVEL is the depth to indent lines by.  */
1286
1287 void
1288 type_print_1 (type, varstring, stream, show, level)
1289      struct type *type;
1290      char *varstring;
1291      FILE *stream;
1292      int show;
1293      int level;
1294 {
1295   register enum type_code code;
1296   type_print_base (type, stream, show, level);
1297   code = TYPE_CODE (type);
1298   if ((varstring && *varstring)
1299       ||
1300       /* Need a space if going to print stars or brackets;
1301          but not if we will print just a type name.  */
1302       ((show > 0 || TYPE_NAME (type) == 0)
1303        &&
1304        (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
1305         || code == TYPE_CODE_METHOD
1306         || code == TYPE_CODE_ARRAY
1307         || code == TYPE_CODE_MEMBER
1308         || code == TYPE_CODE_REF)))
1309     fprintf_filtered (stream, " ");
1310   type_print_varspec_prefix (type, stream, show, 0);
1311   fputs_demangled (varstring, stream, -1);      /* Print demangled name
1312                                                    without arguments */
1313   type_print_varspec_suffix (type, stream, show, 0);
1314 }
1315
1316 /* Print the method arguments ARGS to the file STREAM.  */
1317 static void
1318 type_print_method_args (args, prefix, varstring, staticp, stream)
1319      struct type **args;
1320      char *prefix, *varstring;
1321      int staticp;
1322      FILE *stream;
1323 {
1324   int i;
1325
1326   fputs_filtered (" ", stream);
1327   fputs_demangled (prefix, stream, 1);
1328   fputs_demangled (varstring, stream, 1);
1329   fputs_filtered (" (", stream);
1330   if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
1331     {
1332       i = !staticp;             /* skip the class variable */
1333       while (1)
1334         {
1335           type_print (args[i++], "", stream, 0);
1336           if (!args[i]) 
1337             {
1338               fprintf_filtered (stream, " ...");
1339               break;
1340             }
1341           else if (args[i]->code != TYPE_CODE_VOID)
1342             {
1343               fprintf_filtered (stream, ", ");
1344             }
1345           else break;
1346         }
1347     }
1348   fprintf_filtered (stream, ")");
1349 }
1350   
1351 /* If TYPE is a derived type, then print out derivation
1352    information.  Print out all layers of the type heirarchy
1353    until we encounter one with multiple inheritance.
1354    At that point, print out that ply, and return.  */
1355 static void
1356 type_print_derivation_info (stream, type)
1357      FILE *stream;
1358      struct type *type;
1359 {
1360   char *name;
1361   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
1362   struct type *basetype = 0;
1363
1364   while (type && n_baseclasses > 0)
1365     {
1366       /* Not actually sure about this one -- Bryan. */
1367       check_stub_type (type);
1368       
1369       fprintf_filtered (stream, ": ");
1370       for (i = 0; ;)
1371         {
1372           basetype = TYPE_BASECLASS (type, i);
1373           if (name = type_name_no_tag (basetype))
1374             {
1375               fprintf_filtered (stream, "%s%s ",
1376                        BASETYPE_VIA_PUBLIC(type, i) ? "public" : "private",
1377                        BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
1378               fputs_filtered (name, stream);
1379             }
1380           i++;
1381           if (i >= n_baseclasses)
1382               break;
1383           fprintf_filtered (stream, ", ");
1384         }
1385
1386       fprintf_filtered (stream, " ");
1387       if (n_baseclasses != 1)
1388         break;
1389       n_baseclasses = TYPE_N_BASECLASSES (basetype);
1390       type = basetype;
1391     }
1392 }
1393
1394 /* Print any asterisks or open-parentheses needed before the
1395    variable name (to describe its type).
1396
1397    On outermost call, pass 0 for PASSED_A_PTR.
1398    On outermost call, SHOW > 0 means should ignore
1399    any typename for TYPE and show its details.
1400    SHOW is always zero on recursive calls.  */
1401
1402 static void
1403 type_print_varspec_prefix (type, stream, show, passed_a_ptr)
1404      struct type *type;
1405      FILE *stream;
1406      int show;
1407      int passed_a_ptr;
1408 {
1409   if (type == 0)
1410     return;
1411
1412   if (TYPE_NAME (type) && show <= 0)
1413     return;
1414
1415   QUIT;
1416
1417   switch (TYPE_CODE (type))
1418     {
1419     case TYPE_CODE_PTR:
1420       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1421       fprintf_filtered (stream, "*");
1422       break;
1423
1424     case TYPE_CODE_MEMBER:
1425       if (passed_a_ptr)
1426         fprintf_filtered (stream, "(");
1427       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1428                                  0);
1429       fprintf_filtered (stream, " ");
1430       type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1431                        passed_a_ptr);
1432       fprintf_filtered (stream, "::");
1433       break;
1434
1435     case TYPE_CODE_METHOD:
1436       if (passed_a_ptr)
1437         fprintf (stream, "(");
1438       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1439                                  0);
1440       if (passed_a_ptr)
1441         {
1442           fprintf_filtered (stream, " ");
1443           type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1444                            passed_a_ptr);
1445           fprintf_filtered (stream, "::");
1446         }
1447       break;
1448
1449     case TYPE_CODE_REF:
1450       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1451       fprintf_filtered (stream, "&");
1452       break;
1453
1454     case TYPE_CODE_FUNC:
1455       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1456                                  0);
1457       if (passed_a_ptr)
1458         fprintf_filtered (stream, "(");
1459       break;
1460
1461     case TYPE_CODE_ARRAY:
1462       type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1463                                  0);
1464       if (passed_a_ptr)
1465         fprintf_filtered (stream, "(");
1466
1467     case TYPE_CODE_UNDEF:
1468     case TYPE_CODE_STRUCT:
1469     case TYPE_CODE_UNION:
1470     case TYPE_CODE_ENUM:
1471     case TYPE_CODE_INT:
1472     case TYPE_CODE_FLT:
1473     case TYPE_CODE_VOID:
1474     case TYPE_CODE_ERROR:
1475     case TYPE_CODE_CHAR:
1476     case TYPE_CODE_BOOL:
1477       /* These types need no prefix.  They are listed here so that
1478          gcc -Wall will reveal any types that haven't been handled.  */
1479       break;
1480     }
1481 }
1482
1483 /* Print any array sizes, function arguments or close parentheses
1484    needed after the variable name (to describe its type).
1485    Args work like type_print_varspec_prefix.  */
1486
1487 static void
1488 type_print_varspec_suffix (type, stream, show, passed_a_ptr)
1489      struct type *type;
1490      FILE *stream;
1491      int show;
1492      int passed_a_ptr;
1493 {
1494   if (type == 0)
1495     return;
1496
1497   if (TYPE_NAME (type) && show <= 0)
1498     return;
1499
1500   QUIT;
1501
1502   switch (TYPE_CODE (type))
1503     {
1504     case TYPE_CODE_ARRAY:
1505       if (passed_a_ptr)
1506         fprintf_filtered (stream, ")");
1507       
1508       fprintf_filtered (stream, "[");
1509       if (TYPE_LENGTH (type) > 0
1510           && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
1511         fprintf_filtered (stream, "%d",
1512                           (TYPE_LENGTH (type)
1513                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
1514       fprintf_filtered (stream, "]");
1515       
1516       type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1517                                  0);
1518       break;
1519
1520     case TYPE_CODE_MEMBER:
1521       if (passed_a_ptr)
1522         fprintf_filtered (stream, ")");
1523       type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1524       break;
1525
1526     case TYPE_CODE_METHOD:
1527       if (passed_a_ptr)
1528         fprintf_filtered (stream, ")");
1529       type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1530       if (passed_a_ptr)
1531         {
1532           int i;
1533           struct type **args = TYPE_ARG_TYPES (type);
1534
1535           fprintf_filtered (stream, "(");
1536           if (args[1] == 0)
1537             fprintf_filtered (stream, "...");
1538           else for (i = 1; args[i] != 0 && args[i]->code != TYPE_CODE_VOID; i++)
1539             {
1540               type_print_1 (args[i], "", stream, -1, 0);
1541               if (args[i+1] == 0)
1542                 fprintf_filtered (stream, "...");
1543               else if (args[i+1]->code != TYPE_CODE_VOID) {
1544                 fprintf_filtered (stream, ",");
1545                 wrap_here ("    ");
1546               }
1547             }
1548           fprintf_filtered (stream, ")");
1549         }
1550       break;
1551
1552     case TYPE_CODE_PTR:
1553     case TYPE_CODE_REF:
1554       type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1555       break;
1556
1557     case TYPE_CODE_FUNC:
1558       type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1559                                  passed_a_ptr);
1560       if (passed_a_ptr)
1561         fprintf_filtered (stream, ")");
1562       fprintf_filtered (stream, "()");
1563       break;
1564
1565     case TYPE_CODE_UNDEF:
1566     case TYPE_CODE_STRUCT:
1567     case TYPE_CODE_UNION:
1568     case TYPE_CODE_ENUM:
1569     case TYPE_CODE_INT:
1570     case TYPE_CODE_FLT:
1571     case TYPE_CODE_VOID:
1572     case TYPE_CODE_ERROR:
1573     case TYPE_CODE_CHAR:
1574     case TYPE_CODE_BOOL:
1575       /* These types do not need a suffix.  They are listed so that
1576          gcc -Wall will report types that may not have been considered.  */
1577       break;
1578     }
1579 }
1580
1581 /* Print the name of the type (or the ultimate pointer target,
1582    function value or array element), or the description of a
1583    structure or union.
1584
1585    SHOW nonzero means don't print this type as just its name;
1586    show its real definition even if it has a name.
1587    SHOW zero means print just typename or struct tag if there is one
1588    SHOW negative means abbreviate structure elements.
1589    SHOW is decremented for printing of structure elements.
1590
1591    LEVEL is the depth to indent by.
1592    We increase it for some recursive calls.  */
1593
1594 static void
1595 type_print_base (type, stream, show, level)
1596      struct type *type;
1597      FILE *stream;
1598      int show;
1599      int level;
1600 {
1601   char *name;
1602   register int i;
1603   register int len;
1604   register int lastval;
1605
1606   QUIT;
1607
1608   wrap_here ("    ");
1609   if (type == 0)
1610     {
1611       fprintf_filtered (stream, "<type unknown>");
1612       return;
1613     }
1614
1615   if (TYPE_NAME (type) && show <= 0)
1616     {
1617       fputs_filtered (TYPE_NAME (type), stream);
1618       return;
1619     }
1620
1621   switch (TYPE_CODE (type))
1622     {
1623     case TYPE_CODE_ARRAY:
1624     case TYPE_CODE_PTR:
1625     case TYPE_CODE_MEMBER:
1626     case TYPE_CODE_REF:
1627     case TYPE_CODE_FUNC:
1628     case TYPE_CODE_METHOD:
1629       type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
1630       break;
1631
1632     case TYPE_CODE_STRUCT:
1633       fprintf_filtered (stream, "struct ");
1634       goto struct_union;
1635
1636     case TYPE_CODE_UNION:
1637       fprintf_filtered (stream, "union ");
1638     struct_union:
1639       if (name = type_name_no_tag (type))
1640         {
1641           fputs_filtered (name, stream);
1642           fputs_filtered (" ", stream);
1643           wrap_here ("    ");
1644         }
1645       if (show < 0)
1646         fprintf_filtered (stream, "{...}");
1647       else
1648         {
1649           check_stub_type (type);
1650           
1651           type_print_derivation_info (stream, type);
1652           
1653           fprintf_filtered (stream, "{");
1654           len = TYPE_NFIELDS (type);
1655           if (len)
1656             fprintf_filtered (stream, "\n");
1657           else
1658             {
1659               if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1660                 fprintf_filtered (stream, "<incomplete type>\n");
1661               else
1662                 fprintf_filtered (stream, "<no data fields>\n");
1663             }
1664
1665           /* If there is a base class for this type,
1666              do not print the field that it occupies.  */
1667           for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1668             {
1669               QUIT;
1670               /* Don't print out virtual function table.  */
1671               if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER &&
1672                   !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5))
1673                 continue;
1674
1675               print_spaces_filtered (level + 4, stream);
1676               if (TYPE_FIELD_STATIC (type, i))
1677                 {
1678                   fprintf_filtered (stream, "static ");
1679                 }
1680               type_print_1 (TYPE_FIELD_TYPE (type, i),
1681                             TYPE_FIELD_NAME (type, i),
1682                             stream, show - 1, level + 4);
1683               if (!TYPE_FIELD_STATIC (type, i)
1684                   && TYPE_FIELD_PACKED (type, i))
1685                 {
1686                   /* It is a bitfield.  This code does not attempt
1687                      to look at the bitpos and reconstruct filler,
1688                      unnamed fields.  This would lead to misleading
1689                      results if the compiler does not put out fields
1690                      for such things (I don't know what it does).  */
1691                   fprintf_filtered (stream, " : %d",
1692                                     TYPE_FIELD_BITSIZE (type, i));
1693                 }
1694               fprintf_filtered (stream, ";\n");
1695             }
1696
1697           /* C++: print out the methods */
1698           len = TYPE_NFN_FIELDS (type);
1699           if (len) fprintf_filtered (stream, "\n");
1700           for (i = 0; i < len; i++)
1701             {
1702               struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1703               int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1704
1705               for (j = 0; j < len2; j++)
1706                 {
1707                   QUIT;
1708                   print_spaces_filtered (level + 4, stream);
1709                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1710                     fprintf_filtered (stream, "virtual ");
1711                   else if (TYPE_FN_FIELD_STATIC_P (f, j))
1712                     fprintf_filtered (stream, "static ");
1713                   if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1714                     {
1715                       /* Keep GDB from crashing here.  */
1716                       fprintf (stream, "<undefined type> %s;\n",
1717                                TYPE_FN_FIELD_PHYSNAME (f, j));
1718                       break;
1719                     }
1720                   else
1721                     type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), "", stream, 0);
1722                   if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1723                     {
1724                       /* Build something we can demangle.  */
1725                       char *strchr (), *gdb_mangle_name (), *cplus_demangle ();
1726                       char *mangled_name = gdb_mangle_name (type, i, j);
1727                       char *demangled_name = cplus_demangle (mangled_name, 1);
1728                       if (demangled_name == 0)
1729                         fprintf_filtered (stream, " <badly mangled name %s>",
1730                             mangled_name);
1731                       else 
1732                         {
1733                           fprintf_filtered (stream, " %s",
1734                               strchr (demangled_name, ':') + 2);
1735                           free (demangled_name);
1736                         }
1737                       free (mangled_name);
1738                     }
1739                   else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
1740                         && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
1741                     type_print_method_args
1742                       (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
1743                        TYPE_FN_FIELDLIST_NAME (type, i), 0, stream);
1744                   else
1745                     type_print_method_args
1746                       (TYPE_FN_FIELD_ARGS (f, j), "",
1747                        TYPE_FN_FIELDLIST_NAME (type, i),
1748                        TYPE_FN_FIELD_STATIC_P (f, j), stream);
1749
1750                   fprintf_filtered (stream, ";\n");
1751                 }
1752             }
1753
1754           print_spaces_filtered (level, stream);
1755           fprintf_filtered (stream, "}");
1756         }
1757       break;
1758
1759     case TYPE_CODE_ENUM:
1760       fprintf_filtered (stream, "enum ");
1761       if (name = type_name_no_tag (type))
1762         {
1763           fputs_filtered (name, stream);
1764           fputs_filtered (" ", stream);
1765         }
1766       wrap_here ("    ");
1767       if (show < 0)
1768         fprintf_filtered (stream, "{...}");
1769       else
1770         {
1771           fprintf_filtered (stream, "{");
1772           len = TYPE_NFIELDS (type);
1773           lastval = 0;
1774           for (i = 0; i < len; i++)
1775             {
1776               QUIT;
1777               if (i) fprintf_filtered (stream, ", ");
1778               wrap_here ("    ");
1779               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1780               if (lastval != TYPE_FIELD_BITPOS (type, i))
1781                 {
1782                   fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1783                   lastval = TYPE_FIELD_BITPOS (type, i);
1784                 }
1785               lastval++;
1786             }
1787           fprintf_filtered (stream, "}");
1788         }
1789       break;
1790
1791     case TYPE_CODE_INT:
1792       name = 0;
1793       if (TYPE_LENGTH (type) <= sizeof (LONGEST))
1794         {
1795           if (TYPE_UNSIGNED (type))
1796             name = unsigned_type_table[TYPE_LENGTH (type)];
1797           else
1798             name = signed_type_table[TYPE_LENGTH (type)];
1799         }
1800       if (name)
1801         fputs_filtered (name, stream);
1802       else
1803         fprintf_filtered (stream, "<%d bit integer>",
1804                           TYPE_LENGTH (type) * TARGET_CHAR_BIT);
1805       break;
1806
1807     case TYPE_CODE_FLT:
1808       name = float_type_table[TYPE_LENGTH (type)];
1809       fputs_filtered (name, stream);
1810       break;
1811
1812     case TYPE_CODE_VOID:
1813       fprintf_filtered (stream, "void");
1814       break;
1815
1816     case TYPE_CODE_UNDEF:
1817       fprintf_filtered (stream, "struct <unknown>");
1818       break;
1819
1820     case TYPE_CODE_ERROR:
1821       fprintf_filtered (stream, "<unknown type>");
1822       break;
1823
1824     case TYPE_CODE_RANGE:
1825       /* This should not occur */
1826       fprintf_filtered (stream, "<range type>");
1827       break;
1828
1829     default:
1830       error ("Invalid type code in symbol table.");
1831     }
1832 }
1833 \f
1834 #if 0
1835 /* Validate an input or output radix setting, and make sure the user
1836    knows what they really did here.  Radix setting is confusing, e.g.
1837    setting the input radix to "10" never changes it!  */
1838
1839 /* ARGSUSED */
1840 static void
1841 set_input_radix (args, from_tty, c)
1842      char *args;
1843      int from_tty;
1844      struct cmd_list_element *c;
1845 {
1846   unsigned radix = *(unsigned *)c->var;
1847
1848   if (from_tty)
1849     printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
1850         radix, radix, radix);
1851 }
1852 #endif
1853
1854 /* ARGSUSED */
1855 static void
1856 set_output_radix (args, from_tty, c)
1857      char *args;
1858      int from_tty;
1859      struct cmd_list_element *c;
1860 {
1861   unsigned radix = *(unsigned *)c->var;
1862
1863   if (from_tty)
1864     printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
1865         radix, radix, radix);
1866
1867   /* FIXME, we really should be able to validate the setting BEFORE
1868      it takes effect.  */
1869   switch (radix)
1870     {
1871     case 16:
1872       output_format = 'x';
1873       break;
1874     case 10:
1875       output_format = 0;
1876       break;
1877     case 8:
1878       output_format = 'o';              /* octal */
1879       break;
1880     default:
1881       output_format = 0;
1882       error ("Unsupported radix ``decimal %d''; using decimal output",
1883               radix);
1884     }
1885 }
1886
1887 /* Both at once */
1888 static void
1889 set_radix (arg, from_tty, c)
1890      char *arg;
1891      int from_tty;
1892      struct cmd_list_element *c;
1893 {
1894   unsigned radix = *(unsigned *)c->var;
1895
1896   if (from_tty)
1897     printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
1898         radix, radix, radix);
1899
1900   input_radix = radix;
1901   output_radix = radix;
1902
1903   set_output_radix (arg, 0, c);
1904 }
1905 \f
1906 struct cmd_list_element *setprintlist = NULL;
1907 struct cmd_list_element *showprintlist = NULL;
1908
1909 /*ARGSUSED*/
1910 static void
1911 set_print (arg, from_tty)
1912      char *arg;
1913      int from_tty;
1914 {
1915   printf (
1916 "\"set print\" must be followed by the name of a print subcommand.\n");
1917   help_list (setprintlist, "set print ", -1, stdout);
1918 }
1919
1920 /*ARGSUSED*/
1921 static void
1922 show_print (args, from_tty)
1923      char *args;
1924      int from_tty;
1925 {
1926   cmd_show_list (showprintlist, from_tty, "");
1927 }
1928 \f
1929 void
1930 _initialize_valprint ()
1931 {
1932   struct cmd_list_element *c;
1933
1934   add_prefix_cmd ("print", no_class, set_print,
1935                   "Generic command for setting how things print.",
1936                   &setprintlist, "set print ", 0, &setlist);
1937   add_alias_cmd ("p", "print", no_class, 1, &setlist); 
1938   add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
1939                                                                                                                    to     set prompt */
1940   add_prefix_cmd ("print", no_class, show_print,
1941                   "Generic command for showing print settings.",
1942                   &showprintlist, "show print ", 0, &showlist);
1943   add_alias_cmd ("p", "print", no_class, 1, &showlist); 
1944   add_alias_cmd ("pr", "print", no_class, 1, &showlist); 
1945
1946   add_show_from_set
1947     (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
1948                   "Set limit on string chars or array elements to print.\n\
1949 \"set print elements 0\" causes there to be no limit.",
1950                   &setprintlist),
1951      &showprintlist);
1952
1953   add_show_from_set
1954     (add_set_cmd ("pretty", class_support, var_boolean, (char *)&prettyprint,
1955                   "Set prettyprinting of structures.",
1956                   &setprintlist),
1957      &showprintlist);
1958
1959   add_show_from_set
1960     (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
1961                   "Set printing of unions interior to structures.",
1962                   &setprintlist),
1963      &showprintlist);
1964   
1965   add_show_from_set
1966     (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
1967                   "Set printing of C++ virtual function tables.",
1968                   &setprintlist),
1969      &showprintlist);
1970
1971   add_show_from_set
1972     (add_set_cmd ("array", class_support, var_boolean, (char *)&arrayprint,
1973                   "Set prettyprinting of arrays.",
1974                   &setprintlist),
1975      &showprintlist);
1976
1977   add_show_from_set
1978     (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
1979           "Set printing of object's derived type based on vtable info.",
1980                   &setprintlist),
1981      &showprintlist);
1982
1983   add_show_from_set
1984     (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
1985                   "Set printing of addresses.",
1986                   &setprintlist),
1987      &showprintlist);
1988
1989 #if 0
1990   /* The "show radix" cmd isn't good enough to show two separate values.
1991      The rest of the code works, but the show part is confusing, so don't
1992      let them be set separately 'til we work out "show".  */
1993   c = add_set_cmd ("input-radix", class_support, var_uinteger,
1994                    (char *)&input_radix,
1995                   "Set default input radix for entering numbers.",
1996                   &setlist);
1997   add_show_from_set (c, &showlist);
1998   c->function = set_input_radix;
1999
2000   c = add_set_cmd ("output-radix", class_support, var_uinteger,
2001                    (char *)&output_radix,
2002                   "Set default output radix for printing of values.",
2003                   &setlist);
2004   add_show_from_set (c, &showlist);
2005   c->function = set_output_radix;
2006 #endif 
2007
2008   c = add_set_cmd ("radix", class_support, var_uinteger,
2009                    (char *)&output_radix,
2010                   "Set default input and output number radix.",
2011                   &setlist);
2012   add_show_from_set (c, &showlist);
2013   c->function = set_radix;
2014
2015   /* Give people the defaults which they are used to.  */
2016   prettyprint = 0;
2017   unionprint = 1;
2018   vtblprint = 0;
2019   arrayprint = 0;
2020   addressprint = 1;
2021   objectprint = 0;
2022
2023   print_max = 200;
2024
2025   /* Initialize the names of the various types based on their lengths on
2026      the target, in bits.  Note that ordering is important, so that for example,
2027      if ints and longs are the same size, that size will default to "int". */
2028
2029   unsigned_type_table = (char **)
2030     xmalloc ((1 + (TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT)) * sizeof (char *));
2031   bzero (unsigned_type_table, (1 + (TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT)));
2032   unsigned_type_table[TARGET_CHAR_BIT/TARGET_CHAR_BIT] = "unsigned char";
2033   unsigned_type_table[TARGET_SHORT_BIT/TARGET_CHAR_BIT] = "unsigned short";
2034   unsigned_type_table[TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT] = "unsigned long long";
2035   unsigned_type_table[TARGET_LONG_BIT/TARGET_CHAR_BIT] = "unsigned long";
2036   unsigned_type_table[TARGET_INT_BIT/TARGET_CHAR_BIT] = "unsigned int";
2037
2038   signed_type_table = (char **)
2039     xmalloc ((1 + (TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT)) * sizeof (char *));
2040   bzero (signed_type_table, (1 + (TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT)));
2041   signed_type_table[TARGET_CHAR_BIT/TARGET_CHAR_BIT] = "char";
2042   signed_type_table[TARGET_SHORT_BIT/TARGET_CHAR_BIT] = "short";
2043   signed_type_table[TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT] = "long long";
2044   signed_type_table[TARGET_LONG_BIT/TARGET_CHAR_BIT] = "long";
2045   signed_type_table[TARGET_INT_BIT/TARGET_CHAR_BIT] = "int";
2046
2047   float_type_table = (char **)
2048     xmalloc ((1 + (TARGET_LONG_DOUBLE_BIT/TARGET_CHAR_BIT)) * sizeof (char *));
2049   bzero (float_type_table, (1 + (TARGET_LONG_DOUBLE_BIT/TARGET_CHAR_BIT)));
2050   float_type_table[TARGET_FLOAT_BIT/TARGET_CHAR_BIT] = "float";
2051   float_type_table[TARGET_DOUBLE_COMPLEX_BIT/TARGET_CHAR_BIT] = "double complex";
2052   float_type_table[TARGET_COMPLEX_BIT/TARGET_CHAR_BIT] = "complex";
2053   float_type_table[TARGET_LONG_DOUBLE_BIT/TARGET_CHAR_BIT] = "long double";
2054   float_type_table[TARGET_DOUBLE_BIT/TARGET_CHAR_BIT] = "double";
2055
2056   obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
2057 }
This page took 0.143536 seconds and 4 git commands to generate.