4dc9778622664ef43fdc1ca46f53204548eff882
[deliverable/binutils-gdb.git] / gdb / valprint.c
1 /* Print values for GNU debugger gdb.
2 Copyright (C) 1986, 1988, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "defs.h"
23 #include "param.h"
24 #include "symtab.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "gdbcmd.h"
28 #include "target.h"
29 #include "obstack.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) ? "%.6g" : "%.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(type);
410 static const char vtbl_ptr_name[] =
411 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e' };
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 fprintf_filtered (stream, "{");
455 len = TYPE_NFIELDS (type);
456 n_baseclasses = TYPE_N_BASECLASSES (type);
457
458 /* Print out baseclasses such that we don't print
459 duplicates of virtual baseclasses. */
460 if (n_baseclasses > 0)
461 cplus_val_print (type, valaddr, stream, format, recurse+1, pretty, dont_print);
462
463 if (!len && n_baseclasses == 1)
464 fprintf_filtered (stream, "<No data fields>");
465 else
466 {
467 extern int inspect_it;
468 int fields_seen = 0;
469
470 for (i = n_baseclasses; i < len; i++)
471 {
472 /* Check if static field */
473 if (TYPE_FIELD_STATIC (type, i))
474 continue;
475 if (fields_seen)
476 fprintf_filtered (stream, ", ");
477 else if (n_baseclasses > 0)
478 {
479 fprintf_filtered (stream, "\n");
480 print_spaces_filtered (2 + 2 * recurse, stream);
481 fputs_filtered ("members of ", stream);
482 fputs_filtered (type_name_no_tag (type), stream);
483 fputs_filtered (": ", stream);
484 }
485 fields_seen = 1;
486
487 if (pretty)
488 {
489 fprintf_filtered (stream, "\n");
490 print_spaces_filtered (2 + 2 * recurse, stream);
491 }
492 else
493 {
494 wrap_here (n_spaces (2 + 2 * recurse));
495 }
496 if (inspect_it)
497 {
498 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
499 fputs_filtered ("\"( ptr \"", stream);
500 else
501 fputs_filtered ("\"( nodef \"", stream);
502 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
503 fputs_filtered ("\" \"", stream);
504 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
505 fputs_filtered ("\") \"", stream);
506 }
507 else
508 {
509 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
510 fputs_filtered (" = ", stream);
511 }
512 if (TYPE_FIELD_PACKED (type, i))
513 {
514 value v;
515
516 /* Bitfields require special handling, especially due to byte
517 order problems. */
518 v = value_from_long (TYPE_FIELD_TYPE (type, i),
519 unpack_field_as_long (type, valaddr, i));
520
521 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
522 stream, format, 0, recurse + 1, pretty);
523 }
524 else
525 {
526 val_print (TYPE_FIELD_TYPE (type, i),
527 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
528 0, stream, format, 0, recurse + 1, pretty);
529 }
530 }
531 if (pretty)
532 {
533 fprintf_filtered (stream, "\n");
534 print_spaces_filtered (2 * recurse, stream);
535 }
536 }
537 fprintf_filtered (stream, "}");
538 }
539
540 /* Special val_print routine to avoid printing multiple copies of virtual
541 baseclasses. */
542
543 static void
544 cplus_val_print (type, valaddr, stream, format, recurse, pretty, dont_print)
545 struct type *type;
546 char *valaddr;
547 FILE *stream;
548 char format;
549 int recurse;
550 enum val_prettyprint pretty;
551 struct type **dont_print;
552 {
553 struct obstack tmp_obstack;
554 struct type **last_dont_print
555 = (struct type **)obstack_next_free (&dont_print_obstack);
556 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
557
558 if (dont_print == 0)
559 {
560 /* If we're at top level, carve out a completely fresh
561 chunk of the obstack and use that until this particular
562 invocation returns. */
563 tmp_obstack = dont_print_obstack;
564 /* Bump up the high-water mark. Now alpha is omega. */
565 obstack_finish (&dont_print_obstack);
566 }
567
568 for (i = 0; i < n_baseclasses; i++)
569 {
570 char *baddr;
571 int err;
572
573 if (BASETYPE_VIA_VIRTUAL (type, i))
574 {
575 struct type **first_dont_print
576 = (struct type **)obstack_base (&dont_print_obstack);
577
578 int j = (struct type **)obstack_next_free (&dont_print_obstack)
579 - first_dont_print;
580
581 while (--j >= 0)
582 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
583 goto flush_it;
584
585 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
586 }
587
588 baddr = baseclass_addr (type, i, valaddr, 0, &err);
589 if (err == 0 && baddr == 0)
590 error ("could not find virtual baseclass `%s'\n",
591 type_name_no_tag (TYPE_BASECLASS (type, i)));
592
593 fprintf_filtered (stream, "\n");
594 if (pretty)
595 print_spaces_filtered (2 + 2 * recurse, stream);
596 fputs_filtered ("<", stream);
597 fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream);
598 fputs_filtered ("> = ", stream);
599 if (err != 0)
600 fprintf_filtered (stream, "<invalid address 0x%x>", baddr);
601 else
602 val_print_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
603 recurse, pretty,
604 (struct type **)obstack_base (&dont_print_obstack));
605 flush_it:
606 ;
607 }
608
609 if (dont_print == 0)
610 {
611 /* Free the space used to deal with the printing
612 of this type from top level. */
613 obstack_free (&dont_print_obstack, last_dont_print);
614 /* Reset watermark so that we can continue protecting
615 ourselves from whatever we were protecting ourselves. */
616 dont_print_obstack = tmp_obstack;
617 }
618 }
619
620 /* Print data of type TYPE located at VALADDR (within GDB),
621 which came from the inferior at address ADDRESS,
622 onto stdio stream STREAM according to FORMAT
623 (a letter or 0 for natural format). The data at VALADDR
624 is in target byte order.
625
626 If the data are a string pointer, returns the number of
627 sting characters printed.
628
629 if DEREF_REF is nonzero, then dereference references,
630 otherwise just print them like pointers.
631
632 The PRETTY parameter controls prettyprinting. */
633
634 int
635 val_print (type, valaddr, address, stream, format,
636 deref_ref, recurse, pretty)
637 struct type *type;
638 char *valaddr;
639 CORE_ADDR address;
640 FILE *stream;
641 char format;
642 int deref_ref;
643 int recurse;
644 enum val_prettyprint pretty;
645 {
646 register unsigned int i;
647 unsigned len;
648 struct type *elttype;
649 unsigned eltlen;
650 LONGEST val;
651 unsigned char c;
652
653 if (pretty == Val_pretty_default)
654 {
655 pretty = prettyprint ? Val_prettyprint : Val_no_prettyprint;
656 }
657
658 QUIT;
659
660 check_stub_type (type);
661
662 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
663 {
664 fprintf_filtered (stream, "<unknown struct>");
665 fflush (stream);
666 return 0;
667 }
668
669 switch (TYPE_CODE (type))
670 {
671 case TYPE_CODE_ARRAY:
672 if (TYPE_LENGTH (type) >= 0
673 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
674 {
675 elttype = TYPE_TARGET_TYPE (type);
676 eltlen = TYPE_LENGTH (elttype);
677 len = TYPE_LENGTH (type) / eltlen;
678 if (arrayprint)
679 print_spaces_filtered (2 + 2 * recurse, stream);
680 fprintf_filtered (stream, "{");
681 /* For an array of chars, print with string syntax. */
682 if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
683 && (format == 0 || format == 's') )
684 print_string (stream, valaddr, len, 0);
685 else
686 {
687 unsigned int things_printed = 0;
688
689 /* If this is a virtual function table, print the 0th
690 entry specially, and the rest of the members normally. */
691 if (is_vtbl_ptr_type (elttype))
692 {
693 fprintf_filtered (stream, "%d vtable entries", len-1);
694 i = 1;
695 }
696 else
697 i = 0;
698
699 for (; i < len && things_printed < print_max; i++)
700 {
701 /* Position of the array element we are examining to see
702 whether it is repeated. */
703 unsigned int rep1;
704 /* Number of repetitions we have detected so far. */
705 unsigned int reps;
706
707 if (i != 0)
708 if (arrayprint)
709 {
710 fprintf_filtered (stream, ",\n");
711 print_spaces_filtered (2 + 2 * recurse, stream);
712 }
713 else
714 fprintf_filtered (stream, ", ");
715 wrap_here (n_spaces (2 + 2 * recurse));
716
717 rep1 = i + 1;
718 reps = 1;
719 while (rep1 < len
720 && !bcmp (valaddr + i * eltlen,
721 valaddr + rep1 * eltlen, eltlen))
722 {
723 ++reps;
724 ++rep1;
725 }
726
727 if (reps > REPEAT_COUNT_THRESHOLD)
728 {
729 val_print (elttype, valaddr + i * eltlen,
730 0, stream, format, deref_ref,
731 recurse + 1, pretty);
732 fprintf_filtered (stream, " <repeats %u times>", reps);
733 i = rep1 - 1;
734 things_printed += REPEAT_COUNT_THRESHOLD;
735 }
736 else
737 {
738 val_print (elttype, valaddr + i * eltlen,
739 0, stream, format, deref_ref,
740 recurse + 1, pretty);
741 things_printed++;
742 }
743 }
744 if (i < len)
745 fprintf_filtered (stream, "...");
746 }
747 fprintf_filtered (stream, "}");
748 break;
749 }
750 /* Array of unspecified length: treat like pointer to first elt. */
751 valaddr = (char *) &address;
752
753 case TYPE_CODE_PTR:
754 if (format && format != 's')
755 {
756 print_scalar_formatted (valaddr, type, format, 0, stream);
757 break;
758 }
759 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
760 {
761 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
762 struct fn_field *f;
763 int j, len2;
764 char *kind = "";
765
766 val = unpack_long (builtin_type_int, valaddr);
767 if (val < 128)
768 {
769 len = TYPE_NFN_FIELDS (domain);
770 for (i = 0; i < len; i++)
771 {
772 f = TYPE_FN_FIELDLIST1 (domain, i);
773 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
774
775 for (j = 0; j < len2; j++)
776 {
777 QUIT;
778 if (TYPE_FN_FIELD_VOFFSET (f, j) == val)
779 {
780 kind = "virtual";
781 goto common;
782 }
783 }
784 }
785 }
786 else
787 {
788 struct symbol *sym = find_pc_function ((CORE_ADDR) val);
789 if (sym == 0)
790 error ("invalid pointer to member function");
791 len = TYPE_NFN_FIELDS (domain);
792 for (i = 0; i < len; i++)
793 {
794 f = TYPE_FN_FIELDLIST1 (domain, i);
795 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
796
797 for (j = 0; j < len2; j++)
798 {
799 QUIT;
800 if (!strcmp (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
801 goto common;
802 }
803 }
804 }
805 common:
806 if (i < len)
807 {
808 fprintf_filtered (stream, "&");
809 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
810 fprintf (stream, kind);
811 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
812 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
813 type_print_method_args
814 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
815 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
816 else
817 type_print_method_args
818 (TYPE_FN_FIELD_ARGS (f, j), "",
819 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
820 break;
821 }
822 fprintf_filtered (stream, "(");
823 type_print (type, "", stream, -1);
824 fprintf_filtered (stream, ") %d", (int) val >> 3);
825 }
826 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
827 {
828 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
829
830 /* VAL is a byte offset into the structure type DOMAIN.
831 Find the name of the field for that offset and
832 print it. */
833 int extra = 0;
834 int bits = 0;
835 len = TYPE_NFIELDS (domain);
836 /* @@ Make VAL into bit offset */
837 val = unpack_long (builtin_type_int, valaddr) << 3;
838 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
839 {
840 int bitpos = TYPE_FIELD_BITPOS (domain, i);
841 QUIT;
842 if (val == bitpos)
843 break;
844 if (val < bitpos && i != 0)
845 {
846 /* Somehow pointing into a field. */
847 i -= 1;
848 extra = (val - TYPE_FIELD_BITPOS (domain, i));
849 if (extra & 0x3)
850 bits = 1;
851 else
852 extra >>= 3;
853 break;
854 }
855 }
856 if (i < len)
857 {
858 fprintf_filtered (stream, "&");
859 type_print_base (domain, stream, 0, 0);
860 fprintf_filtered (stream, "::");
861 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
862 if (extra)
863 fprintf_filtered (stream, " + %d bytes", extra);
864 if (bits)
865 fprintf_filtered (stream, " (offset in bits)");
866 break;
867 }
868 fprintf_filtered (stream, "%d", val >> 3);
869 }
870 else
871 {
872 CORE_ADDR addr = (CORE_ADDR) unpack_long (type, valaddr);
873 elttype = TYPE_TARGET_TYPE (type);
874
875 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
876 {
877 /* Try to print what function it points to. */
878 print_address_demangle (addr, stream, demangle);
879 /* Return value is irrelevant except for string pointers. */
880 return 0;
881 }
882
883 if (addressprint && format != 's')
884 fprintf_filtered (stream, "0x%x", addr);
885
886 /* For a pointer to char or unsigned char,
887 also print the string pointed to, unless pointer is null. */
888 i = 0; /* Number of characters printed. */
889 if (TYPE_LENGTH (elttype) == 1
890 && TYPE_CODE (elttype) == TYPE_CODE_INT
891 && (format == 0 || format == 's')
892 && addr != 0
893 /* If print_max is UINT_MAX, the alloca below will fail.
894 In that case don't try to print the string. */
895 && print_max < UINT_MAX)
896 {
897 int first_addr_err = 0;
898 int errcode = 0;
899
900 /* Get first character. */
901 errcode = target_read_memory (addr, (char *)&c, 1);
902 if (errcode != 0)
903 {
904 /* First address out of bounds. */
905 first_addr_err = 1;
906 }
907 else
908 {
909 /* A real string. */
910 char *string = (char *) alloca (print_max);
911
912 /* If the loop ends by us hitting print_max characters,
913 we need to have elipses at the end. */
914 int force_ellipses = 1;
915
916 /* This loop always fetches print_max characters, even
917 though print_string might want to print more or fewer
918 (with repeated characters). This is so that
919 we don't spend forever fetching if we print
920 a long string consisting of the same character
921 repeated. Also so we can do it all in one memory
922 operation, which is faster. However, this will be
923 slower if print_max is set high, e.g. if you set
924 print_max to 1000, not only will it take a long
925 time to fetch short strings, but if you are near
926 the end of the address space, it might not work.
927 FIXME. */
928 QUIT;
929 errcode = target_read_memory (addr, string, print_max);
930 if (errcode != 0)
931 force_ellipses = 0;
932 else
933 for (i = 0; i < print_max; i++)
934 if (string[i] == '\0')
935 {
936 force_ellipses = 0;
937 break;
938 }
939 QUIT;
940
941 if (addressprint)
942 fputs_filtered (" ", stream);
943 print_string (stream, string, i, force_ellipses);
944 }
945
946 if (errcode != 0)
947 {
948 if (errcode == EIO)
949 {
950 fprintf_filtered (stream,
951 (" <Address 0x%x out of bounds>"
952 + first_addr_err),
953 addr + i);
954 }
955 else
956 {
957 if (errcode >= sys_nerr || errcode < 0)
958 error ("Error reading memory address 0x%x: unknown error (%d).",
959 addr + i, errcode);
960 else
961 error ("Error reading memory address 0x%x: %s.",
962 addr + i, sys_errlist[errcode]);
963 }
964 }
965
966 fflush (stream);
967 }
968 else /* print vtbl's nicely */
969 if (is_vtbl_member(type))
970 {
971 CORE_ADDR vt_address = (CORE_ADDR) unpack_long (type, valaddr);
972
973 int vt_index = find_pc_misc_function (vt_address);
974 if (vt_index >= 0
975 && vt_address == misc_function_vector[vt_index].address)
976 {
977 fputs_filtered (" <", stream);
978 fputs_demangled (misc_function_vector[vt_index].name,
979 stream, 1);
980 fputs_filtered (">", stream);
981 }
982 if (vtblprint)
983 {
984 value val;
985
986 val = value_at (TYPE_TARGET_TYPE (type), vt_address);
987 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
988 VALUE_ADDRESS (val), stream, format,
989 deref_ref, recurse + 1, pretty);
990 if (pretty)
991 {
992 fprintf_filtered (stream, "\n");
993 print_spaces_filtered (2 + 2 * recurse, stream);
994 }
995 }
996 }
997
998 /* Return number of characters printed, plus one for the
999 terminating null if we have "reached the end". */
1000 return i + (print_max && i != print_max);
1001 }
1002 break;
1003
1004 case TYPE_CODE_MEMBER:
1005 error ("not implemented: member type in val_print");
1006 break;
1007
1008 case TYPE_CODE_REF:
1009 if (addressprint)
1010 {
1011 fprintf_filtered (stream, "@0x%lx",
1012 unpack_long (builtin_type_int, valaddr));
1013 if (deref_ref)
1014 fputs_filtered (": ", stream);
1015 }
1016 /* De-reference the reference. */
1017 if (deref_ref)
1018 {
1019 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
1020 {
1021 value val = value_at (TYPE_TARGET_TYPE (type),
1022 (CORE_ADDR) unpack_long (builtin_type_int,
1023 valaddr));
1024 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
1025 VALUE_ADDRESS (val), stream, format,
1026 deref_ref, recurse + 1, pretty);
1027 }
1028 else
1029 fputs_filtered ("???", stream);
1030 }
1031 break;
1032
1033 case TYPE_CODE_UNION:
1034 if (recurse && !unionprint)
1035 {
1036 fprintf_filtered (stream, "{...}");
1037 break;
1038 }
1039 /* Fall through. */
1040 case TYPE_CODE_STRUCT:
1041 if (vtblprint && is_vtbl_ptr_type(type))
1042 {
1043 /* Print the unmangled name if desired. */
1044 print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */
1045 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
1046 stream, demangle);
1047 break;
1048 }
1049 val_print_fields (type, valaddr, stream, format, recurse, pretty, 0);
1050 break;
1051
1052 case TYPE_CODE_ENUM:
1053 if (format)
1054 {
1055 print_scalar_formatted (valaddr, type, format, 0, stream);
1056 break;
1057 }
1058 len = TYPE_NFIELDS (type);
1059 val = unpack_long (builtin_type_int, valaddr);
1060 for (i = 0; i < len; i++)
1061 {
1062 QUIT;
1063 if (val == TYPE_FIELD_BITPOS (type, i))
1064 break;
1065 }
1066 if (i < len)
1067 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1068 else
1069 fprintf_filtered (stream, "%d", (int) val);
1070 break;
1071
1072 case TYPE_CODE_FUNC:
1073 if (format)
1074 {
1075 print_scalar_formatted (valaddr, type, format, 0, stream);
1076 break;
1077 }
1078 fprintf_filtered (stream, "{");
1079 type_print (type, "", stream, -1);
1080 fprintf_filtered (stream, "} ");
1081 if (addressprint)
1082 fprintf_filtered (stream, "0x%x", address);
1083 break;
1084
1085 case TYPE_CODE_INT:
1086 if (format || output_format)
1087 {
1088 print_scalar_formatted (valaddr, type,
1089 format? format: output_format,
1090 0, stream);
1091 break;
1092 }
1093 if (TYPE_LENGTH (type) > sizeof (LONGEST))
1094 {
1095 if (TYPE_UNSIGNED (type))
1096 {
1097 /* First figure out whether the number in fact has zeros
1098 in all its bytes more significant than least significant
1099 sizeof (LONGEST) ones. */
1100 char *p;
1101 /* Pointer to first (i.e. lowest address) nonzero character. */
1102 char *first_addr;
1103 unsigned len = TYPE_LENGTH (type);
1104
1105 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1106 for (p = valaddr;
1107 len > sizeof (LONGEST)
1108 && p < valaddr + TYPE_LENGTH (type);
1109 p++)
1110 #else /* Little endian. */
1111 first_addr = valaddr;
1112 for (p = valaddr + TYPE_LENGTH (type);
1113 len > sizeof (LONGEST) && p >= valaddr;
1114 p--)
1115 #endif /* Little endian. */
1116 {
1117 if (*p == 0)
1118 len--;
1119 else
1120 break;
1121 }
1122 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1123 first_addr = p;
1124 #endif
1125
1126 if (len <= sizeof (LONGEST))
1127 {
1128 /* We can print it in decimal. */
1129 fprintf_filtered
1130 (stream,
1131 #if defined (LONG_LONG)
1132 "%llu",
1133 #else
1134 "%lu",
1135 #endif
1136 unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
1137 }
1138 else
1139 {
1140 /* It is big, so print it in hex. */
1141 print_hex_chars (stream, (unsigned char *)first_addr, len);
1142 }
1143 }
1144 else
1145 {
1146 /* Signed. One could assume two's complement (a reasonable
1147 assumption, I think) and do better than this. */
1148 print_hex_chars (stream, (unsigned char *)valaddr,
1149 TYPE_LENGTH (type));
1150 }
1151 break;
1152 }
1153 #ifdef PRINT_TYPELESS_INTEGER
1154 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
1155 #else
1156 #ifndef LONG_LONG
1157 fprintf_filtered (stream,
1158 TYPE_UNSIGNED (type) ? "%u" : "%d",
1159 unpack_long (type, valaddr));
1160 #else
1161 fprintf_filtered (stream,
1162 TYPE_UNSIGNED (type) ? "%llu" : "%lld",
1163 unpack_long (type, valaddr));
1164 #endif
1165 #endif
1166
1167 if (TYPE_LENGTH (type) == 1)
1168 {
1169 fprintf_filtered (stream, " '");
1170 printchar ((unsigned char) unpack_long (type, valaddr),
1171 stream, '\'');
1172 fprintf_filtered (stream, "'");
1173 }
1174 break;
1175
1176 case TYPE_CODE_FLT:
1177 if (format)
1178 print_scalar_formatted (valaddr, type, format, 0, stream);
1179 else
1180 print_floating (valaddr, type, stream);
1181 break;
1182
1183 case TYPE_CODE_VOID:
1184 fprintf_filtered (stream, "void");
1185 break;
1186
1187 case TYPE_CODE_UNDEF:
1188 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
1189 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1190 and no complete type for struct foo in that file. */
1191 fprintf_filtered (stream, "<unknown struct>");
1192 break;
1193
1194 case TYPE_CODE_ERROR:
1195 fprintf_filtered (stream, "?");
1196 break;
1197
1198 default:
1199 error ("Invalid type code in symbol table.");
1200 }
1201 fflush (stream);
1202 return 0;
1203 }
1204 \f
1205 /* Print a description of a type TYPE
1206 in the form of a declaration of a variable named VARSTRING.
1207 (VARSTRING is demangled if necessary.)
1208 Output goes to STREAM (via stdio).
1209 If SHOW is positive, we show the contents of the outermost level
1210 of structure even if there is a type name that could be used instead.
1211 If SHOW is negative, we never show the details of elements' types. */
1212
1213 void
1214 type_print (type, varstring, stream, show)
1215 struct type *type;
1216 char *varstring;
1217 FILE *stream;
1218 int show;
1219 {
1220 type_print_1 (type, varstring, stream, show, 0);
1221 }
1222
1223 /* LEVEL is the depth to indent lines by. */
1224
1225 void
1226 type_print_1 (type, varstring, stream, show, level)
1227 struct type *type;
1228 char *varstring;
1229 FILE *stream;
1230 int show;
1231 int level;
1232 {
1233 register enum type_code code;
1234 type_print_base (type, stream, show, level);
1235 code = TYPE_CODE (type);
1236 if ((varstring && *varstring)
1237 ||
1238 /* Need a space if going to print stars or brackets;
1239 but not if we will print just a type name. */
1240 ((show > 0 || TYPE_NAME (type) == 0)
1241 &&
1242 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
1243 || code == TYPE_CODE_METHOD
1244 || code == TYPE_CODE_ARRAY
1245 || code == TYPE_CODE_MEMBER
1246 || code == TYPE_CODE_REF)))
1247 fprintf_filtered (stream, " ");
1248 type_print_varspec_prefix (type, stream, show, 0);
1249 fputs_demangled (varstring, stream, -1); /* Print demangled name
1250 without arguments */
1251 type_print_varspec_suffix (type, stream, show, 0);
1252 }
1253
1254 /* Print the method arguments ARGS to the file STREAM. */
1255 static void
1256 type_print_method_args (args, prefix, varstring, staticp, stream)
1257 struct type **args;
1258 char *prefix, *varstring;
1259 int staticp;
1260 FILE *stream;
1261 {
1262 int i;
1263
1264 fputs_filtered (" ", stream);
1265 fputs_demangled (prefix, stream, 1);
1266 fputs_demangled (varstring, stream, 1);
1267 fputs_filtered (" (", stream);
1268 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
1269 {
1270 i = !staticp; /* skip the class variable */
1271 while (1)
1272 {
1273 type_print (args[i++], "", stream, 0);
1274 if (!args[i])
1275 {
1276 fprintf_filtered (stream, " ...");
1277 break;
1278 }
1279 else if (args[i]->code != TYPE_CODE_VOID)
1280 {
1281 fprintf_filtered (stream, ", ");
1282 }
1283 else break;
1284 }
1285 }
1286 fprintf_filtered (stream, ")");
1287 }
1288
1289 /* If TYPE is a derived type, then print out derivation
1290 information. Print out all layers of the type heirarchy
1291 until we encounter one with multiple inheritance.
1292 At that point, print out that ply, and return. */
1293 static void
1294 type_print_derivation_info (stream, type)
1295 FILE *stream;
1296 struct type *type;
1297 {
1298 char *name;
1299 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
1300 struct type *basetype = 0;
1301
1302 while (type && n_baseclasses > 0)
1303 {
1304 /* Not actually sure about this one -- Bryan. */
1305 check_stub_type (type);
1306
1307 fprintf_filtered (stream, ": ");
1308 for (i = 0; ;)
1309 {
1310 basetype = TYPE_BASECLASS (type, i);
1311 if (name = type_name_no_tag (basetype))
1312 {
1313 fprintf_filtered (stream, "%s%s ",
1314 BASETYPE_VIA_PUBLIC(type, i) ? "public" : "private",
1315 BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
1316 fputs_filtered (name, stream);
1317 }
1318 i++;
1319 if (i >= n_baseclasses)
1320 break;
1321 fprintf_filtered (stream, ", ");
1322 }
1323
1324 fprintf_filtered (stream, " ");
1325 if (n_baseclasses != 1)
1326 break;
1327 n_baseclasses = TYPE_N_BASECLASSES (basetype);
1328 type = basetype;
1329 }
1330 }
1331
1332 /* Print any asterisks or open-parentheses needed before the
1333 variable name (to describe its type).
1334
1335 On outermost call, pass 0 for PASSED_A_PTR.
1336 On outermost call, SHOW > 0 means should ignore
1337 any typename for TYPE and show its details.
1338 SHOW is always zero on recursive calls. */
1339
1340 static void
1341 type_print_varspec_prefix (type, stream, show, passed_a_ptr)
1342 struct type *type;
1343 FILE *stream;
1344 int show;
1345 int passed_a_ptr;
1346 {
1347 if (type == 0)
1348 return;
1349
1350 if (TYPE_NAME (type) && show <= 0)
1351 return;
1352
1353 QUIT;
1354
1355 switch (TYPE_CODE (type))
1356 {
1357 case TYPE_CODE_PTR:
1358 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1359 fprintf_filtered (stream, "*");
1360 break;
1361
1362 case TYPE_CODE_MEMBER:
1363 if (passed_a_ptr)
1364 fprintf_filtered (stream, "(");
1365 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1366 0);
1367 fprintf_filtered (stream, " ");
1368 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1369 passed_a_ptr);
1370 fprintf_filtered (stream, "::");
1371 break;
1372
1373 case TYPE_CODE_METHOD:
1374 if (passed_a_ptr)
1375 fprintf (stream, "(");
1376 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1377 0);
1378 if (passed_a_ptr)
1379 {
1380 fprintf_filtered (stream, " ");
1381 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1382 passed_a_ptr);
1383 fprintf_filtered (stream, "::");
1384 }
1385 break;
1386
1387 case TYPE_CODE_REF:
1388 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1389 fprintf_filtered (stream, "&");
1390 break;
1391
1392 case TYPE_CODE_FUNC:
1393 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1394 0);
1395 if (passed_a_ptr)
1396 fprintf_filtered (stream, "(");
1397 break;
1398
1399 case TYPE_CODE_ARRAY:
1400 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1401 0);
1402 if (passed_a_ptr)
1403 fprintf_filtered (stream, "(");
1404
1405 case TYPE_CODE_UNDEF:
1406 case TYPE_CODE_STRUCT:
1407 case TYPE_CODE_UNION:
1408 case TYPE_CODE_ENUM:
1409 case TYPE_CODE_INT:
1410 case TYPE_CODE_FLT:
1411 case TYPE_CODE_VOID:
1412 case TYPE_CODE_ERROR:
1413 /* These types need no prefix. They are listed here so that
1414 gcc -Wall will reveal any types that haven't been handled. */
1415 break;
1416 }
1417 }
1418
1419 /* Print any array sizes, function arguments or close parentheses
1420 needed after the variable name (to describe its type).
1421 Args work like type_print_varspec_prefix. */
1422
1423 static void
1424 type_print_varspec_suffix (type, stream, show, passed_a_ptr)
1425 struct type *type;
1426 FILE *stream;
1427 int show;
1428 int passed_a_ptr;
1429 {
1430 if (type == 0)
1431 return;
1432
1433 if (TYPE_NAME (type) && show <= 0)
1434 return;
1435
1436 QUIT;
1437
1438 switch (TYPE_CODE (type))
1439 {
1440 case TYPE_CODE_ARRAY:
1441 if (passed_a_ptr)
1442 fprintf_filtered (stream, ")");
1443
1444 fprintf_filtered (stream, "[");
1445 if (TYPE_LENGTH (type) >= 0
1446 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
1447 fprintf_filtered (stream, "%d",
1448 (TYPE_LENGTH (type)
1449 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
1450 fprintf_filtered (stream, "]");
1451
1452 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1453 0);
1454 break;
1455
1456 case TYPE_CODE_MEMBER:
1457 if (passed_a_ptr)
1458 fprintf_filtered (stream, ")");
1459 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1460 break;
1461
1462 case TYPE_CODE_METHOD:
1463 if (passed_a_ptr)
1464 fprintf_filtered (stream, ")");
1465 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1466 if (passed_a_ptr)
1467 {
1468 int i;
1469 struct type **args = TYPE_ARG_TYPES (type);
1470
1471 fprintf_filtered (stream, "(");
1472 if (args[1] == 0)
1473 fprintf_filtered (stream, "...");
1474 else for (i = 1; args[i] != 0 && args[i]->code != TYPE_CODE_VOID; i++)
1475 {
1476 type_print_1 (args[i], "", stream, -1, 0);
1477 if (args[i+1] == 0)
1478 fprintf_filtered (stream, "...");
1479 else if (args[i+1]->code != TYPE_CODE_VOID)
1480 fprintf_filtered (stream, ",");
1481 }
1482 fprintf_filtered (stream, ")");
1483 }
1484 break;
1485
1486 case TYPE_CODE_PTR:
1487 case TYPE_CODE_REF:
1488 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1489 break;
1490
1491 case TYPE_CODE_FUNC:
1492 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1493 passed_a_ptr);
1494 if (passed_a_ptr)
1495 fprintf_filtered (stream, ")");
1496 fprintf_filtered (stream, "()");
1497 break;
1498
1499 case TYPE_CODE_UNDEF:
1500 case TYPE_CODE_STRUCT:
1501 case TYPE_CODE_UNION:
1502 case TYPE_CODE_ENUM:
1503 case TYPE_CODE_INT:
1504 case TYPE_CODE_FLT:
1505 case TYPE_CODE_VOID:
1506 case TYPE_CODE_ERROR:
1507 /* These types do not need a suffix. They are listed so that
1508 gcc -Wall will report types that may not have been considered. */
1509 break;
1510 }
1511 }
1512
1513 /* Print the name of the type (or the ultimate pointer target,
1514 function value or array element), or the description of a
1515 structure or union.
1516
1517 SHOW nonzero means don't print this type as just its name;
1518 show its real definition even if it has a name.
1519 SHOW zero means print just typename or struct tag if there is one
1520 SHOW negative means abbreviate structure elements.
1521 SHOW is decremented for printing of structure elements.
1522
1523 LEVEL is the depth to indent by.
1524 We increase it for some recursive calls. */
1525
1526 static void
1527 type_print_base (type, stream, show, level)
1528 struct type *type;
1529 FILE *stream;
1530 int show;
1531 int level;
1532 {
1533 char *name;
1534 register int i;
1535 register int len;
1536 register int lastval;
1537
1538 QUIT;
1539
1540 if (type == 0)
1541 {
1542 fprintf_filtered (stream, "type unknown");
1543 return;
1544 }
1545
1546 if (TYPE_NAME (type) && show <= 0)
1547 {
1548 fputs_filtered (TYPE_NAME (type), stream);
1549 return;
1550 }
1551
1552 switch (TYPE_CODE (type))
1553 {
1554 case TYPE_CODE_ARRAY:
1555 case TYPE_CODE_PTR:
1556 case TYPE_CODE_MEMBER:
1557 case TYPE_CODE_REF:
1558 case TYPE_CODE_FUNC:
1559 case TYPE_CODE_METHOD:
1560 type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
1561 break;
1562
1563 case TYPE_CODE_STRUCT:
1564 fprintf_filtered (stream, "struct ");
1565 goto struct_union;
1566
1567 case TYPE_CODE_UNION:
1568 fprintf_filtered (stream, "union ");
1569 struct_union:
1570 if (name = type_name_no_tag (type))
1571 {
1572 fputs_filtered (name, stream);
1573 fputs_filtered (" ", stream);
1574 }
1575 if (show < 0)
1576 fprintf_filtered (stream, "{...}");
1577 else
1578 {
1579 check_stub_type (type);
1580
1581 type_print_derivation_info (stream, type);
1582
1583 fprintf_filtered (stream, "{");
1584 len = TYPE_NFIELDS (type);
1585 if (len)
1586 fprintf_filtered (stream, "\n");
1587 else
1588 {
1589 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1590 fprintf_filtered (stream, "<incomplete type>\n");
1591 else
1592 fprintf_filtered (stream, "<no data fields>\n");
1593 }
1594
1595 /* If there is a base class for this type,
1596 do not print the field that it occupies. */
1597 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1598 {
1599 QUIT;
1600 /* Don't print out virtual function table. */
1601 if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER &&
1602 !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5))
1603 continue;
1604
1605 print_spaces_filtered (level + 4, stream);
1606 if (TYPE_FIELD_STATIC (type, i))
1607 {
1608 fprintf_filtered (stream, "static ");
1609 }
1610 type_print_1 (TYPE_FIELD_TYPE (type, i),
1611 TYPE_FIELD_NAME (type, i),
1612 stream, show - 1, level + 4);
1613 if (!TYPE_FIELD_STATIC (type, i)
1614 && TYPE_FIELD_PACKED (type, i))
1615 {
1616 /* It is a bitfield. This code does not attempt
1617 to look at the bitpos and reconstruct filler,
1618 unnamed fields. This would lead to misleading
1619 results if the compiler does not put out fields
1620 for such things (I don't know what it does). */
1621 fprintf_filtered (stream, " : %d",
1622 TYPE_FIELD_BITSIZE (type, i));
1623 }
1624 fprintf_filtered (stream, ";\n");
1625 }
1626
1627 /* C++: print out the methods */
1628 len = TYPE_NFN_FIELDS (type);
1629 if (len) fprintf_filtered (stream, "\n");
1630 for (i = 0; i < len; i++)
1631 {
1632 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1633 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1634
1635 for (j = 0; j < len2; j++)
1636 {
1637 QUIT;
1638 print_spaces_filtered (level + 4, stream);
1639 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1640 fprintf_filtered (stream, "virtual ");
1641 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1642 fprintf_filtered (stream, "static ");
1643 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1644 {
1645 /* Keep GDB from crashing here. */
1646 fprintf (stream, "<undefined type> %s;\n",
1647 TYPE_FN_FIELD_PHYSNAME (f, j));
1648 break;
1649 }
1650 else
1651 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), "", stream, 0);
1652 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1653 {
1654 /* Build something we can demangle. */
1655 char *strchr (), *gdb_mangle_typename ();
1656 char *inner_name = gdb_mangle_typename (type);
1657 char *mangled_name
1658 = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type, i))
1659 + strlen (inner_name)
1660 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
1661 + 1);
1662 char *demangled_name, *cplus_demangle ();
1663 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
1664 strcat (mangled_name, inner_name);
1665 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
1666 demangled_name = cplus_demangle (mangled_name, 1);
1667 if (demangled_name == 0)
1668 fprintf_filtered (stream, " <badly mangled name %s>",
1669 mangled_name);
1670 else
1671 {
1672 fprintf_filtered (stream, " %s",
1673 strchr (demangled_name, ':') + 2);
1674 free (demangled_name);
1675 }
1676 free (mangled_name);
1677 }
1678 else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
1679 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
1680 type_print_method_args
1681 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
1682 TYPE_FN_FIELDLIST_NAME (type, i), 0, stream);
1683 else
1684 type_print_method_args
1685 (TYPE_FN_FIELD_ARGS (f, j), "",
1686 TYPE_FN_FIELDLIST_NAME (type, i),
1687 TYPE_FN_FIELD_STATIC_P (f, j), stream);
1688
1689 fprintf_filtered (stream, ";\n");
1690 }
1691 }
1692
1693 print_spaces_filtered (level, stream);
1694 fprintf_filtered (stream, "}");
1695 }
1696 break;
1697
1698 case TYPE_CODE_ENUM:
1699 fprintf_filtered (stream, "enum ");
1700 if (name = type_name_no_tag (type))
1701 {
1702 fputs_filtered (name, stream);
1703 fputs_filtered (" ", stream);
1704 }
1705 if (show < 0)
1706 fprintf_filtered (stream, "{...}");
1707 else
1708 {
1709 fprintf_filtered (stream, "{");
1710 len = TYPE_NFIELDS (type);
1711 lastval = 0;
1712 for (i = 0; i < len; i++)
1713 {
1714 QUIT;
1715 if (i) fprintf_filtered (stream, ", ");
1716 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1717 if (lastval != TYPE_FIELD_BITPOS (type, i))
1718 {
1719 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1720 lastval = TYPE_FIELD_BITPOS (type, i);
1721 }
1722 lastval++;
1723 }
1724 fprintf_filtered (stream, "}");
1725 }
1726 break;
1727
1728 case TYPE_CODE_INT:
1729 if (TYPE_LENGTH (type) > sizeof (LONGEST))
1730 {
1731 fprintf_filtered (stream, "<%d bit integer>",
1732 TYPE_LENGTH (type) * TARGET_CHAR_BIT);
1733 }
1734 else
1735 {
1736 if (TYPE_UNSIGNED (type))
1737 name = unsigned_type_table[TYPE_LENGTH (type)];
1738 else
1739 name = signed_type_table[TYPE_LENGTH (type)];
1740 }
1741 fputs_filtered (name, stream);
1742 break;
1743
1744 case TYPE_CODE_FLT:
1745 name = float_type_table[TYPE_LENGTH (type)];
1746 fputs_filtered (name, stream);
1747 break;
1748
1749 case TYPE_CODE_VOID:
1750 fprintf_filtered (stream, "void");
1751 break;
1752
1753 case 0:
1754 fprintf_filtered (stream, "struct unknown");
1755 break;
1756
1757 case TYPE_CODE_ERROR:
1758 fprintf_filtered (stream, "<unknown type>");
1759 break;
1760
1761 default:
1762 error ("Invalid type code in symbol table.");
1763 }
1764 }
1765 \f
1766 /* Validate an input or output radix setting, and make sure the user
1767 knows what they really did here. Radix setting is confusing, e.g.
1768 setting the input radix to "10" never changes it! */
1769
1770 static void
1771 set_input_radix (args, from_tty, c)
1772 char *args;
1773 int from_tty;
1774 struct cmd_list_element *c;
1775 {
1776 unsigned radix = *(unsigned *)c->var;
1777
1778 if (from_tty)
1779 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
1780 radix, radix, radix);
1781 }
1782
1783 static void
1784 set_output_radix (args, from_tty, c)
1785 char *args;
1786 int from_tty;
1787 struct cmd_list_element *c;
1788 {
1789 unsigned radix = *(unsigned *)c->var;
1790
1791 if (from_tty)
1792 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
1793 radix, radix, radix);
1794
1795 /* FIXME, we really should be able to validate the setting BEFORE
1796 it takes effect. */
1797 switch (radix)
1798 {
1799 case 16:
1800 output_format = 'x';
1801 break;
1802 case 10:
1803 output_format = 0;
1804 break;
1805 case 8:
1806 output_format = 'o'; /* octal */
1807 break;
1808 default:
1809 output_format = 0;
1810 error ("Unsupported radix ``decimal %d''; using decimal output",
1811 radix);
1812 }
1813 }
1814
1815 /* Both at once */
1816 static void
1817 set_radix (arg, from_tty, c)
1818 char *arg;
1819 int from_tty;
1820 struct cmd_list_element *c;
1821 {
1822 unsigned radix = *(unsigned *)c->var;
1823
1824 if (from_tty)
1825 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
1826 radix, radix, radix);
1827
1828 input_radix = radix;
1829 output_radix = radix;
1830
1831 set_output_radix (arg, 0, c);
1832 }
1833 \f
1834 struct cmd_list_element *setprintlist = NULL;
1835 struct cmd_list_element *showprintlist = NULL;
1836
1837 /*ARGSUSED*/
1838 static void
1839 set_print (arg, from_tty)
1840 char *arg;
1841 int from_tty;
1842 {
1843 printf (
1844 "\"set print\" must be followed by the name of a print subcommand.\n");
1845 help_list (setprintlist, "set print ", -1, stdout);
1846 }
1847
1848 /*ARGSUSED*/
1849 static void
1850 show_print (args, from_tty)
1851 char *args;
1852 int from_tty;
1853 {
1854 cmd_show_list (showprintlist, from_tty, "");
1855 }
1856 \f
1857 void
1858 _initialize_valprint ()
1859 {
1860 struct cmd_list_element *c;
1861
1862 add_prefix_cmd ("print", no_class, set_print,
1863 "Generic command for setting how things print.",
1864 &setprintlist, "set print ", 0, &setlist);
1865 add_prefix_cmd ("print", no_class, show_print,
1866 "Generic command for showing print settings.",
1867 &showprintlist, "show print ", 0, &showlist);
1868
1869 add_show_from_set
1870 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
1871 "Set limit on string chars or array elements to print.\n\
1872 \"set print elements 0\" causes there to be no limit.",
1873 &setprintlist),
1874 &showprintlist);
1875
1876 add_show_from_set
1877 (add_set_cmd ("pretty", class_support, var_boolean, (char *)&prettyprint,
1878 "Set prettyprinting of structures.",
1879 &setprintlist),
1880 &showprintlist);
1881
1882 add_show_from_set
1883 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
1884 "Set printing of unions interior to structures.",
1885 &setprintlist),
1886 &showprintlist);
1887
1888 add_show_from_set
1889 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
1890 "Set printing of C++ virtual function tables.",
1891 &setprintlist),
1892 &showprintlist);
1893
1894 add_show_from_set
1895 (add_set_cmd ("array", class_support, var_boolean, (char *)&arrayprint,
1896 "Set prettyprinting of arrays.",
1897 &setprintlist),
1898 &showprintlist);
1899
1900 add_show_from_set
1901 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
1902 "Set printing of object's derived type based on vtable info.",
1903 &setprintlist),
1904 &showprintlist);
1905
1906 add_show_from_set
1907 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
1908 "Set printing of addresses.",
1909 &setprintlist),
1910 &showprintlist);
1911
1912 #if 0
1913 /* The "show radix" cmd isn't good enough to show two separate values.
1914 The rest of the code works, but the show part is confusing, so don't
1915 let them be set separately 'til we work out "show". */
1916 c = add_set_cmd ("input-radix", class_support, var_uinteger,
1917 (char *)&input_radix,
1918 "Set default input radix for entering numbers.",
1919 &setlist);
1920 add_show_from_set (c, &showlist);
1921 c->function = set_input_radix;
1922
1923 c = add_set_cmd ("output-radix", class_support, var_uinteger,
1924 (char *)&output_radix,
1925 "Set default output radix for printing of values.",
1926 &setlist);
1927 add_show_from_set (c, &showlist);
1928 c->function = set_output_radix;
1929 #endif
1930
1931 c = add_set_cmd ("radix", class_support, var_uinteger,
1932 (char *)&output_radix,
1933 "Set default input and output number radix.",
1934 &setlist);
1935 add_show_from_set (c, &showlist);
1936 c->function = set_radix;
1937
1938 /* Give people the defaults which they are used to. */
1939 prettyprint = 0;
1940 unionprint = 1;
1941 vtblprint = 0;
1942 arrayprint = 0;
1943 addressprint = 1;
1944 objectprint = 0;
1945
1946 print_max = 200;
1947
1948 unsigned_type_table
1949 = (char **) xmalloc ((1 + sizeof (unsigned LONGEST)) * sizeof (char *));
1950 bzero (unsigned_type_table, (1 + sizeof (unsigned LONGEST)));
1951 unsigned_type_table[sizeof (unsigned char)] = "unsigned char";
1952 unsigned_type_table[sizeof (unsigned short)] = "unsigned short";
1953 unsigned_type_table[sizeof (unsigned long)] = "unsigned long";
1954 unsigned_type_table[sizeof (unsigned int)] = "unsigned int";
1955 #ifdef LONG_LONG
1956 unsigned_type_table[sizeof (unsigned long long)] = "unsigned long long";
1957 #endif
1958
1959 signed_type_table
1960 = (char **) xmalloc ((1 + sizeof (LONGEST)) * sizeof (char *));
1961 bzero (signed_type_table, (1 + sizeof (LONGEST)));
1962 signed_type_table[sizeof (char)] = "char";
1963 signed_type_table[sizeof (short)] = "short";
1964 signed_type_table[sizeof (long)] = "long";
1965 signed_type_table[sizeof (int)] = "int";
1966 #ifdef LONG_LONG
1967 signed_type_table[sizeof (long long)] = "long long";
1968 #endif
1969
1970 float_type_table
1971 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
1972 bzero (float_type_table, (1 + sizeof (double)));
1973 float_type_table[sizeof (float)] = "float";
1974 float_type_table[sizeof (double)] = "double";
1975 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
1976 }
This page took 0.071341 seconds and 3 git commands to generate.