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