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