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