gdb-3.3
[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 "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "value.h"
25
26 /* Maximum number of chars to print for a string pointer value
27 or vector contents. */
28
29 static int print_max;
30
31 static void type_print_varspec_suffix ();
32 static void type_print_varspec_prefix ();
33 static void type_print_base ();
34 static void type_print_method_args ();
35
36
37 char **unsigned_type_table;
38 char **signed_type_table;
39 char **float_type_table;
40
41
42 /* Print repeat counts if there are more than this
43 many repetitions of an element in an array. */
44 #define REPEAT_COUNT_THRESHOLD 10
45 \f
46 /* Print the character string STRING, printing at most LENGTH characters.
47 Printing stops early if the number hits print_max; repeat counts
48 are printed as appropriate. */
49
50 void
51 print_string (stream, string, length)
52 FILE *stream;
53 char *string;
54 unsigned int length;
55 {
56 register unsigned int i;
57 unsigned int things_printed = 0;
58 int in_quotes = 0;
59 int need_comma = 0;
60
61 if (length == 0)
62 {
63 fputs_filtered ("\"\"", stdout);
64 return;
65 }
66
67 for (i = 0; i < length && things_printed < print_max; ++i)
68 {
69 /* Position of the character we are examining
70 to see whether it is repeated. */
71 unsigned int rep1;
72 /* Number of repititions we have detected so far. */
73 unsigned int reps;
74
75 QUIT;
76
77 if (need_comma)
78 {
79 fputs_filtered (", ", stream);
80 need_comma = 0;
81 }
82
83 rep1 = i + 1;
84 reps = 1;
85 while (rep1 < length && string[rep1] == string[i])
86 {
87 ++rep1;
88 ++reps;
89 }
90
91 if (reps > REPEAT_COUNT_THRESHOLD)
92 {
93 if (in_quotes)
94 {
95 fputs_filtered ("\", ", stream);
96 in_quotes = 0;
97 }
98 fputs_filtered ("'", stream);
99 printchar (string[i], stream, '\'');
100 fprintf_filtered (stream, "' <repeats %u times>", reps);
101 i = rep1 - 1;
102 things_printed += REPEAT_COUNT_THRESHOLD;
103 need_comma = 1;
104 }
105 else
106 {
107 if (!in_quotes)
108 {
109 fputs_filtered ("\"", stream);
110 in_quotes = 1;
111 }
112 printchar (string[i], stream, '"');
113 ++things_printed;
114 }
115 }
116
117 /* Terminate the quotes if necessary. */
118 if (in_quotes)
119 fputs_filtered ("\"", stream);
120
121 /* Print ellipses if we stopped before printing LENGTH characters. */
122 if (i < length)
123 fputs_filtered ("...", stream);
124 }
125 \f
126 /* Print the value VAL in C-ish syntax on stream STREAM.
127 FORMAT is a format-letter, or 0 for print in natural format of data type.
128 If the object printed is a string pointer, returns
129 the number of string bytes printed. */
130
131 int
132 value_print (val, stream, format, pretty)
133 value val;
134 FILE *stream;
135 char format;
136 enum val_prettyprint pretty;
137 {
138 register unsigned int i, n, typelen;
139
140 /* A "repeated" value really contains several values in a row.
141 They are made by the @ operator.
142 Print such values as if they were arrays. */
143
144 if (VALUE_REPEATED (val))
145 {
146 n = VALUE_REPETITIONS (val);
147 typelen = TYPE_LENGTH (VALUE_TYPE (val));
148 fprintf_filtered (stream, "{");
149 /* Print arrays of characters using string syntax. */
150 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
151 && format == 0)
152 print_string (stream, VALUE_CONTENTS (val), n);
153 else
154 {
155 unsigned int things_printed = 0;
156
157 for (i = 0; i < n && things_printed < print_max; i++)
158 {
159 /* Position of the array element we are examining to see
160 whether it is repeated. */
161 unsigned int rep1;
162 /* Number of repititions we have detected so far. */
163 unsigned int reps;
164
165 if (i != 0)
166 fprintf_filtered (stream, ", ");
167
168 rep1 = i + 1;
169 reps = 1;
170 while (rep1 < n
171 && !bcmp (VALUE_CONTENTS (val) + typelen * i,
172 VALUE_CONTENTS (val) + typelen * rep1, typelen))
173 {
174 ++reps;
175 ++rep1;
176 }
177
178 if (reps > REPEAT_COUNT_THRESHOLD)
179 {
180 val_print (VALUE_TYPE (val),
181 VALUE_CONTENTS (val) + typelen * i,
182 VALUE_ADDRESS (val) + typelen * i,
183 stream, format, 1, 0, pretty);
184 fprintf (stream, " <repeats %u times>", reps);
185 i = rep1 - 1;
186 things_printed += REPEAT_COUNT_THRESHOLD;
187 }
188 else
189 {
190 val_print (VALUE_TYPE (val),
191 VALUE_CONTENTS (val) + typelen * i,
192 VALUE_ADDRESS (val) + typelen * i,
193 stream, format, 1, 0, pretty);
194 things_printed++;
195 }
196 }
197 if (i < n)
198 fprintf_filtered (stream, "...");
199 }
200 fprintf_filtered (stream, "}");
201 return n * typelen;
202 }
203 else
204 {
205 /* If it is a pointer, indicate what it points to.
206
207 Print type also if it is a reference.
208
209 C++: if it is a member pointer, we will take care
210 of that when we print it. */
211 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_PTR
212 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF)
213 {
214 fprintf_filtered (stream, "(");
215 type_print (VALUE_TYPE (val), "", stream, -1);
216 fprintf_filtered (stream, ") ");
217
218 /* If this is a function pointer, try to print what
219 function it is pointing to by name. */
220 if (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (val)))
221 == TYPE_CODE_FUNC)
222 {
223 print_address (((int *) VALUE_CONTENTS (val))[0], stream);
224 /* Return value is irrelevant except for string pointers. */
225 return 0;
226 }
227 }
228 return val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
229 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
230 }
231 }
232 \f
233 static int prettyprint; /* Controls prettyprinting of structures. */
234 int unionprint; /* Controls printing of nested unions. */
235
236 /* Print data of type TYPE located at VALADDR (within GDB),
237 which came from the inferior at address ADDRESS,
238 onto stdio stream STREAM according to FORMAT
239 (a letter or 0 for natural format).
240
241 If the data are a string pointer, returns the number of
242 sting characters printed.
243
244 if DEREF_REF is nonzero, then dereference references,
245 otherwise just print them like pointers.
246
247 The PRETTY parameter controls prettyprinting. */
248
249 int
250 val_print (type, valaddr, address, stream, format,
251 deref_ref, recurse, pretty)
252 struct type *type;
253 char *valaddr;
254 CORE_ADDR address;
255 FILE *stream;
256 char format;
257 int deref_ref;
258 int recurse;
259 enum val_prettyprint pretty;
260 {
261 register unsigned int i;
262 int len, n_baseclasses;
263 struct type *elttype;
264 int eltlen;
265 LONGEST val;
266 unsigned char c;
267
268 if (pretty == Val_pretty_default)
269 {
270 pretty = prettyprint ? Val_prettyprint : Val_no_prettyprint;
271 }
272
273 QUIT;
274
275 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
276 {
277 fprintf_filtered (stream, "<Type not defined in this context>");
278 fflush (stream);
279 return 0;
280 }
281
282 switch (TYPE_CODE (type))
283 {
284 case TYPE_CODE_ARRAY:
285 if (TYPE_LENGTH (type) >= 0
286 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
287 {
288 elttype = TYPE_TARGET_TYPE (type);
289 eltlen = TYPE_LENGTH (elttype);
290 len = TYPE_LENGTH (type) / eltlen;
291 fprintf_filtered (stream, "{");
292 /* For an array of chars, print with string syntax. */
293 if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
294 && format == 0)
295 print_string (stream, valaddr, len);
296 else
297 {
298 unsigned int things_printed = 0;
299
300 for (i = 0; i < len && things_printed < print_max; i++)
301 {
302 /* Position of the array element we are examining to see
303 whether it is repeated. */
304 unsigned int rep1;
305 /* Number of repititions we have detected so far. */
306 unsigned int reps;
307
308 if (i > 0)
309 fprintf_filtered (stream, ", ");
310
311 rep1 = i + 1;
312 reps = 1;
313 while (rep1 < len
314 && !bcmp (valaddr + i * eltlen,
315 valaddr + rep1 * eltlen, eltlen))
316 {
317 ++reps;
318 ++rep1;
319 }
320
321 if (reps > REPEAT_COUNT_THRESHOLD)
322 {
323 val_print (elttype, valaddr + i * eltlen,
324 0, stream, format, deref_ref,
325 recurse + 1, pretty);
326 fprintf_filtered (stream, " <repeats %u times>", reps);
327 i = rep1 - 1;
328 things_printed += REPEAT_COUNT_THRESHOLD;
329 }
330 else
331 {
332 val_print (elttype, valaddr + i * eltlen,
333 0, stream, format, deref_ref,
334 recurse + 1, pretty);
335 things_printed++;
336 }
337 }
338 if (i < len)
339 fprintf_filtered (stream, "...");
340 }
341 fprintf_filtered (stream, "}");
342 break;
343 }
344 /* Array of unspecified length: treat like pointer to first elt. */
345 valaddr = (char *) &address;
346
347 case TYPE_CODE_PTR:
348 if (format)
349 {
350 print_scalar_formatted (valaddr, type, format, 0, stream);
351 break;
352 }
353 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
354 {
355 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
356 struct type *target = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type));
357 struct fn_field *f;
358 int j, len2;
359 char *kind = "";
360
361 val = unpack_long (builtin_type_int, valaddr);
362 if (val < 128)
363 {
364 len = TYPE_NFN_FIELDS (domain);
365 for (i = 0; i < len; i++)
366 {
367 f = TYPE_FN_FIELDLIST1 (domain, i);
368 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
369
370 for (j = 0; j < len2; j++)
371 {
372 QUIT;
373 if (TYPE_FN_FIELD_VOFFSET (f, j) == val)
374 {
375 kind = "virtual";
376 goto common;
377 }
378 }
379 }
380 }
381 else
382 {
383 struct symbol *sym = find_pc_function ((CORE_ADDR) val);
384 if (sym == 0)
385 error ("invalid pointer to member function");
386 len = TYPE_NFN_FIELDS (domain);
387 for (i = 0; i < len; i++)
388 {
389 f = TYPE_FN_FIELDLIST1 (domain, i);
390 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
391
392 for (j = 0; j < len2; j++)
393 {
394 QUIT;
395 if (!strcmp (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
396 goto common;
397 }
398 }
399 }
400 common:
401 if (i < len)
402 {
403 fprintf_filtered (stream, "&");
404 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
405 fprintf (stream, kind);
406 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
407 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == '$')
408 type_print_method_args
409 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
410 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
411 else
412 type_print_method_args
413 (TYPE_FN_FIELD_ARGS (f, j), "",
414 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
415 break;
416 }
417 fprintf_filtered (stream, "(");
418 type_print (type, "", stream, -1);
419 fprintf_filtered (stream, ") %d", (int) val >> 3);
420 }
421 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
422 {
423 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
424 struct type *target = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type));
425 char *kind = "";
426
427 /* VAL is a byte offset into the structure type DOMAIN.
428 Find the name of the field for that offset and
429 print it. */
430 int extra = 0;
431 int bits = 0;
432 len = TYPE_NFIELDS (domain);
433 /* @@ Make VAL into bit offset */
434 val = unpack_long (builtin_type_int, valaddr) << 3;
435 for (i = 0; i < len; i++)
436 {
437 int bitpos = TYPE_FIELD_BITPOS (domain, i);
438 QUIT;
439 if (val == bitpos)
440 break;
441 if (val < bitpos && i > 0)
442 {
443 int ptrsize = (TYPE_LENGTH (builtin_type_char) * TYPE_LENGTH (target));
444 /* Somehow pointing into a field. */
445 i -= 1;
446 extra = (val - TYPE_FIELD_BITPOS (domain, i));
447 if (extra & 0x3)
448 bits = 1;
449 else
450 extra >>= 3;
451 break;
452 }
453 }
454 if (i < len)
455 {
456 fprintf_filtered (stream, "&");
457 type_print_base (domain, stream, 0, 0);
458 fprintf_filtered (stream, "::");
459 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
460 if (extra)
461 fprintf_filtered (stream, " + %d bytes", extra);
462 if (bits)
463 fprintf_filtered (stream, " (offset in bits)");
464 break;
465 }
466 fprintf_filtered (stream, "%d", val >> 3);
467 }
468 else
469 {
470 fprintf_filtered (stream, "0x%x", * (int *) valaddr);
471 /* For a pointer to char or unsigned char,
472 also print the string pointed to, unless pointer is null. */
473
474 /* For an array of chars, print with string syntax. */
475 elttype = TYPE_TARGET_TYPE (type);
476 i = 0; /* Number of characters printed. */
477 if (TYPE_LENGTH (elttype) == 1
478 && TYPE_CODE (elttype) == TYPE_CODE_INT
479 && format == 0
480 && unpack_long (type, valaddr) != 0
481 && print_max)
482 {
483 fprintf_filtered (stream, " ");
484
485 /* Get first character. */
486 if (read_memory ( (CORE_ADDR) unpack_long (type, valaddr),
487 &c, 1))
488 {
489 /* First address out of bounds. */
490 fprintf_filtered (stream, "<Address 0x%x out of bounds>",
491 (* (int *) valaddr));
492 break;
493 }
494 else
495 {
496 /* A real string. */
497 int out_of_bounds = 0;
498 char *string = (char *) alloca (print_max);
499
500 while (i < print_max)
501 {
502 QUIT;
503 if (read_memory ((CORE_ADDR) unpack_long (type, valaddr)
504 + i, &c, 1))
505 {
506 out_of_bounds = 1;
507 break;
508 }
509 else if (c == '\0')
510 break;
511 else
512 string[i++] = c;
513 }
514
515 if (i != 0)
516 print_string (stream, string, i);
517 if (out_of_bounds)
518 fprintf_filtered (stream,
519 "*** <Address 0x%x out of bounds>",
520 (*(int *) valaddr) + i);
521 }
522
523 fflush (stream);
524 }
525 /* Return number of characters printed, plus one for the
526 terminating null if we have "reached the end". */
527 return i + (print_max && i != print_max);
528 }
529 break;
530
531 case TYPE_CODE_MEMBER:
532 error ("not implemented: member type in val_print");
533 break;
534
535 case TYPE_CODE_REF:
536 fprintf_filtered (stream, "(0x%x &) = ", * (int *) valaddr);
537 /* De-reference the reference. */
538 if (deref_ref)
539 {
540 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
541 {
542 value val = value_at (TYPE_TARGET_TYPE (type), * (int *) valaddr);
543 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
544 VALUE_ADDRESS (val), stream, format,
545 deref_ref, recurse + 1, pretty);
546 }
547 else
548 fprintf_filtered (stream, "???");
549 }
550 break;
551
552 case TYPE_CODE_UNION:
553 if (recurse && !unionprint)
554 {
555 fprintf_filtered (stream, "{...}");
556 break;
557 }
558 /* Fall through. */
559 case TYPE_CODE_STRUCT:
560 fprintf_filtered (stream, "{");
561 len = TYPE_NFIELDS (type);
562 n_baseclasses = TYPE_N_BASECLASSES (type);
563 for (i = 1; i <= n_baseclasses; i++)
564 {
565 fprintf_filtered (stream, "\n");
566 if (pretty)
567 print_spaces_filtered (2 + 2 * recurse, stream);
568 fputs_filtered ("<", stream);
569 fputs_filtered (TYPE_NAME (TYPE_BASECLASS (type, i)), stream);
570 fputs_filtered ("> = ", stream);
571 val_print (TYPE_FIELD_TYPE (type, 0),
572 valaddr + TYPE_FIELD_BITPOS (type, i-1) / 8,
573 0, stream, 0, 0, recurse + 1, pretty);
574 }
575 if (i > 1) {
576 fprintf_filtered (stream, "\n");
577 print_spaces_filtered (2 + 2 * recurse, stream);
578 fputs_filtered ("members of ", stream);
579 fputs_filtered (TYPE_NAME (type), stream);
580 fputs_filtered (": ", stream);
581 }
582 if (!len && i == 1)
583 fprintf_filtered (stream, "<No data fields>");
584 else
585 {
586 for (i -= 1; i < len; i++)
587 {
588 if (i > n_baseclasses) fprintf_filtered (stream, ", ");
589 if (pretty)
590 {
591 fprintf_filtered (stream, "\n");
592 print_spaces_filtered (2 + 2 * recurse, stream);
593 }
594 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
595 fputs_filtered (" = ", stream);
596 /* check if static field */
597 if (TYPE_FIELD_STATIC (type, i))
598 {
599 value v;
600
601 v = value_static_field (type, TYPE_FIELD_NAME (type, i), i);
602 val_print (TYPE_FIELD_TYPE (type, i),
603 VALUE_CONTENTS (v), 0, stream, format,
604 deref_ref, recurse + 1, pretty);
605 }
606 else if (TYPE_FIELD_PACKED (type, i))
607 {
608 char *valp = (char *) & val;
609 union {int i; char c;} test;
610 test.i = 1;
611 if (test.c != 1)
612 valp += sizeof val - TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
613 val = unpack_field_as_long (type, valaddr, i);
614 val_print (TYPE_FIELD_TYPE (type, i), valp, 0,
615 stream, format, deref_ref, recurse + 1, pretty);
616 }
617 else
618 {
619 val_print (TYPE_FIELD_TYPE (type, i),
620 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
621 0, stream, format, deref_ref,
622 recurse + 1, pretty);
623 }
624 }
625 if (pretty)
626 {
627 fprintf_filtered (stream, "\n");
628 print_spaces_filtered (2 * recurse, stream);
629 }
630 }
631 fprintf_filtered (stream, "}");
632 break;
633
634 case TYPE_CODE_ENUM:
635 if (format)
636 {
637 print_scalar_formatted (valaddr, type, format, 0, stream);
638 break;
639 }
640 len = TYPE_NFIELDS (type);
641 val = unpack_long (builtin_type_int, valaddr);
642 for (i = 0; i < len; i++)
643 {
644 QUIT;
645 if (val == TYPE_FIELD_BITPOS (type, i))
646 break;
647 }
648 if (i < len)
649 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
650 else
651 fprintf_filtered (stream, "%d", (int) val);
652 break;
653
654 case TYPE_CODE_FUNC:
655 if (format)
656 {
657 print_scalar_formatted (valaddr, type, format, 0, stream);
658 break;
659 }
660 fprintf_filtered (stream, "{");
661 type_print (type, "", stream, -1);
662 fprintf_filtered (stream, "} ");
663 fprintf_filtered (stream, "0x%x", address);
664 break;
665
666 case TYPE_CODE_INT:
667 if (format)
668 {
669 print_scalar_formatted (valaddr, type, format, 0, stream);
670 break;
671 }
672 #ifdef PRINT_TYPELESS_INTEGER
673 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
674 #else
675 #ifndef LONG_LONG
676 fprintf_filtered (stream,
677 TYPE_UNSIGNED (type) ? "%u" : "%d",
678 unpack_long (type, valaddr));
679 #else
680 fprintf_filtered (stream,
681 TYPE_UNSIGNED (type) ? "%llu" : "%lld",
682 unpack_long (type, valaddr));
683 #endif
684 #endif
685
686 if (TYPE_LENGTH (type) == 1)
687 {
688 fprintf_filtered (stream, " '");
689 printchar ((unsigned char) unpack_long (type, valaddr),
690 stream, '\'');
691 fprintf_filtered (stream, "'");
692 }
693 break;
694
695 case TYPE_CODE_FLT:
696 if (format)
697 {
698 print_scalar_formatted (valaddr, type, format, 0, stream);
699 break;
700 }
701 #ifdef IEEE_FLOAT
702 if (is_nan ((void *) valaddr, TYPE_LENGTH (type)))
703 {
704 fprintf_filtered (stream, "NaN");
705 break;
706 }
707 #endif
708 {
709 double doub;
710 int inv;
711
712 doub = unpack_double (type, valaddr, &inv);
713 if (inv)
714 fprintf_filtered (stream, "Invalid float value");
715 else
716 fprintf_filtered (stream,
717 TYPE_LENGTH (type) <= 4? "%.6g": "%.16g", doub);
718 }
719 break;
720
721 case TYPE_CODE_VOID:
722 fprintf_filtered (stream, "void");
723 break;
724
725 default:
726 error ("Invalid type code in symbol table.");
727 }
728 fflush (stream);
729 return 0;
730 }
731 \f
732 #ifdef IEEE_FLOAT
733
734 /* Nonzero if ARG (a double) is a NAN. */
735
736 int
737 is_nan (fp, len)
738 void *fp;
739 int len;
740 {
741 int lowhalf, highhalf;
742 union ieee
743 {
744 long i[2]; /* ASSUMED 32 BITS */
745 float f; /* ASSUMED 32 BITS */
746 double d; /* ASSUMED 64 BITS */
747 } *arg;
748
749 arg = (union ieee *)fp;
750
751 /*
752 * Single precision float.
753 */
754 if (len == sizeof(long))
755 {
756 highhalf = arg->i[0];
757 return ((((highhalf >> 23) & 0xFF) == 0xFF)
758 && 0 != (highhalf & 0x7FFFFF));
759 }
760
761 /* Separate the high and low words of the double.
762 Distinguish big and little-endian machines. */
763 #ifdef WORDS_BIG_ENDIAN
764 lowhalf = arg->i[1], highhalf = arg->i[0];
765 #else
766 lowhalf = arg->i[0], highhalf = arg->i[1];
767 #endif
768
769 /* Nan: exponent is the maximum possible, and fraction is nonzero. */
770 return (((highhalf>>20) & 0x7ff) == 0x7ff
771 && ! ((highhalf & 0xfffff == 0) && (lowhalf == 0)));
772 }
773 #endif
774 \f
775 /* Print a description of a type TYPE
776 in the form of a declaration of a variable named VARSTRING.
777 Output goes to STREAM (via stdio).
778 If SHOW is positive, we show the contents of the outermost level
779 of structure even if there is a type name that could be used instead.
780 If SHOW is negative, we never show the details of elements' types. */
781
782 void
783 type_print (type, varstring, stream, show)
784 struct type *type;
785 char *varstring;
786 FILE *stream;
787 int show;
788 {
789 type_print_1 (type, varstring, stream, show, 0);
790 }
791
792 /* LEVEL is the depth to indent lines by. */
793
794 void
795 type_print_1 (type, varstring, stream, show, level)
796 struct type *type;
797 char *varstring;
798 FILE *stream;
799 int show;
800 int level;
801 {
802 register enum type_code code;
803 type_print_base (type, stream, show, level);
804 code = TYPE_CODE (type);
805 if ((varstring && *varstring)
806 ||
807 /* Need a space if going to print stars or brackets;
808 but not if we will print just a type name. */
809 ((show > 0 || TYPE_NAME (type) == 0)
810 &&
811 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
812 || code == TYPE_CODE_METHOD
813 || code == TYPE_CODE_ARRAY
814 || code == TYPE_CODE_MEMBER
815 || code == TYPE_CODE_REF)))
816 fprintf_filtered (stream, " ");
817 type_print_varspec_prefix (type, stream, show, 0);
818 fputs_filtered (varstring, stream);
819 type_print_varspec_suffix (type, stream, show, 0);
820 }
821
822 /* Print the method arguments ARGS to the file STREAM. */
823 static void
824 type_print_method_args (args, prefix, varstring, staticp, stream)
825 struct type **args;
826 char *prefix, *varstring;
827 int staticp;
828 FILE *stream;
829 {
830 int i;
831
832 fputs_filtered (" ", stream);
833 fputs_filtered (prefix, stream);
834 fputs_filtered (varstring, stream);
835 fputs_filtered (" (", stream);
836 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
837 {
838 i = !staticp; /* skip the class variable */
839 while (1)
840 {
841 type_print (args[i++], "", stream, 0);
842 if (!args[i])
843 {
844 fprintf_filtered (stream, " ...");
845 break;
846 }
847 else if (args[i]->code != TYPE_CODE_VOID)
848 {
849 fprintf_filtered (stream, ", ");
850 }
851 else break;
852 }
853 }
854 fprintf_filtered (stream, ")");
855 }
856
857 /* If TYPE is a derived type, then print out derivation
858 information. Print out all layers of the type heirarchy
859 until we encounter one with multiple inheritance.
860 At that point, print out that ply, and return. */
861 static void
862 type_print_derivation_info (stream, type)
863 FILE *stream;
864 struct type *type;
865 {
866 char *name;
867 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
868 struct type *basetype = 0;
869
870 while (type && n_baseclasses == 1)
871 {
872 basetype = TYPE_BASECLASS (type, 1);
873 if (TYPE_NAME (basetype) && (name = TYPE_NAME (basetype)))
874 {
875 while (*name != ' ') name++;
876 fprintf_filtered (stream, ": %s%s ",
877 TYPE_VIA_PUBLIC (basetype) ? "public" : "private",
878 TYPE_VIA_VIRTUAL (basetype) ? " virtual" : "");
879 fputs_filtered (name + 1, stream);
880 fputs_filtered (" ", stream);
881 }
882 n_baseclasses = TYPE_N_BASECLASSES (basetype);
883 type = basetype;
884 }
885
886 if (type)
887 {
888 if (n_baseclasses != 0)
889 fprintf_filtered (stream, ": ");
890 for (i = 1; i <= n_baseclasses; i++)
891 {
892 basetype = TYPE_BASECLASS (type, i);
893 if (TYPE_NAME (basetype) && (name = TYPE_NAME (basetype)))
894 {
895 while (*name != ' ') name++;
896 fprintf_filtered (stream, "%s%s ",
897 TYPE_VIA_PUBLIC (basetype) ? "public" : "private",
898 TYPE_VIA_VIRTUAL (basetype) ? " virtual" : "");
899 fputs_filtered (name + 1, stream);
900 }
901 if (i < n_baseclasses)
902 fprintf_filtered (stream, ", ");
903 }
904 fprintf_filtered (stream, " ");
905 }
906 }
907
908 /* Print any asterisks or open-parentheses needed before the
909 variable name (to describe its type).
910
911 On outermost call, pass 0 for PASSED_A_PTR.
912 On outermost call, SHOW > 0 means should ignore
913 any typename for TYPE and show its details.
914 SHOW is always zero on recursive calls. */
915
916 static void
917 type_print_varspec_prefix (type, stream, show, passed_a_ptr)
918 struct type *type;
919 FILE *stream;
920 int show;
921 int passed_a_ptr;
922 {
923 if (type == 0)
924 return;
925
926 if (TYPE_NAME (type) && show <= 0)
927 return;
928
929 QUIT;
930
931 switch (TYPE_CODE (type))
932 {
933 case TYPE_CODE_PTR:
934 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
935 fprintf_filtered (stream, "*");
936 break;
937
938 case TYPE_CODE_MEMBER:
939 if (passed_a_ptr)
940 fprintf_filtered (stream, "(");
941 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
942 0);
943 fprintf_filtered (stream, " ");
944 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
945 passed_a_ptr);
946 fprintf_filtered (stream, "::");
947 break;
948
949 case TYPE_CODE_METHOD:
950 if (passed_a_ptr)
951 fprintf (stream, "(");
952 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
953 0);
954 fprintf_filtered (stream, " ");
955 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
956 passed_a_ptr);
957 fprintf_filtered (stream, "::");
958 break;
959
960 case TYPE_CODE_REF:
961 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
962 fprintf_filtered (stream, "&");
963 break;
964
965 case TYPE_CODE_FUNC:
966 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
967 0);
968 if (passed_a_ptr)
969 fprintf_filtered (stream, "(");
970 break;
971
972 case TYPE_CODE_ARRAY:
973 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
974 0);
975 if (passed_a_ptr)
976 fprintf_filtered (stream, "(");
977 }
978 }
979
980 /* Print any array sizes, function arguments or close parentheses
981 needed after the variable name (to describe its type).
982 Args work like type_print_varspec_prefix. */
983
984 static void
985 type_print_varspec_suffix (type, stream, show, passed_a_ptr)
986 struct type *type;
987 FILE *stream;
988 int show;
989 int passed_a_ptr;
990 {
991 if (type == 0)
992 return;
993
994 if (TYPE_NAME (type) && show <= 0)
995 return;
996
997 QUIT;
998
999 switch (TYPE_CODE (type))
1000 {
1001 case TYPE_CODE_ARRAY:
1002 if (passed_a_ptr)
1003 fprintf_filtered (stream, ")");
1004
1005 fprintf_filtered (stream, "[");
1006 if (TYPE_LENGTH (type) >= 0
1007 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
1008 fprintf_filtered (stream, "%d",
1009 (TYPE_LENGTH (type)
1010 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
1011 fprintf_filtered (stream, "]");
1012
1013 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1014 0);
1015 break;
1016
1017 case TYPE_CODE_MEMBER:
1018 if (passed_a_ptr)
1019 fprintf_filtered (stream, ")");
1020 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1021 break;
1022
1023 case TYPE_CODE_METHOD:
1024 if (passed_a_ptr)
1025 fprintf_filtered (stream, ")");
1026 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1027 if (passed_a_ptr)
1028 {
1029 int i;
1030 struct type **args = TYPE_ARG_TYPES (type);
1031
1032 fprintf_filtered (stream, "(");
1033 if (args[1] == 0)
1034 fprintf_filtered (stream, "...");
1035 else for (i = 1; args[i] != 0 && args[i]->code != TYPE_CODE_VOID; i++)
1036 {
1037 type_print_1 (args[i], "", stream, -1, 0);
1038 if (args[i+1] == 0)
1039 fprintf_filtered (stream, "...");
1040 else if (args[i+1]->code != TYPE_CODE_VOID)
1041 fprintf_filtered (stream, ",");
1042 }
1043 fprintf_filtered (stream, ")");
1044 }
1045 break;
1046
1047 case TYPE_CODE_PTR:
1048 case TYPE_CODE_REF:
1049 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1050 break;
1051
1052 case TYPE_CODE_FUNC:
1053 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1054 passed_a_ptr);
1055 if (passed_a_ptr)
1056 fprintf_filtered (stream, ")");
1057 fprintf_filtered (stream, "()");
1058 break;
1059 }
1060 }
1061
1062 /* Print the name of the type (or the ultimate pointer target,
1063 function value or array element), or the description of a
1064 structure or union.
1065
1066 SHOW nonzero means don't print this type as just its name;
1067 show its real definition even if it has a name.
1068 SHOW zero means print just typename or struct tag if there is one
1069 SHOW negative means abbreviate structure elements.
1070 SHOW is decremented for printing of structure elements.
1071
1072 LEVEL is the depth to indent by.
1073 We increase it for some recursive calls. */
1074
1075 static void
1076 type_print_base (type, stream, show, level)
1077 struct type *type;
1078 FILE *stream;
1079 int show;
1080 int level;
1081 {
1082 char *name;
1083 register int i;
1084 register int len;
1085 register int lastval;
1086
1087 QUIT;
1088
1089 if (type == 0)
1090 {
1091 fprintf_filtered (stream, "type unknown");
1092 return;
1093 }
1094
1095 if (TYPE_NAME (type) && show <= 0)
1096 {
1097 fputs_filtered (TYPE_NAME (type), stream);
1098 return;
1099 }
1100
1101 switch (TYPE_CODE (type))
1102 {
1103 case TYPE_CODE_ARRAY:
1104 case TYPE_CODE_PTR:
1105 case TYPE_CODE_MEMBER:
1106 case TYPE_CODE_REF:
1107 case TYPE_CODE_FUNC:
1108 case TYPE_CODE_METHOD:
1109 type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
1110 break;
1111
1112 case TYPE_CODE_STRUCT:
1113 fprintf_filtered (stream, "struct ");
1114 goto struct_union;
1115
1116 case TYPE_CODE_UNION:
1117 fprintf_filtered (stream, "union ");
1118 struct_union:
1119 if (TYPE_NAME (type) && (name = TYPE_NAME (type)))
1120 {
1121 while (*name != ' ') name++;
1122 fputs_filtered (name + 1, stream);
1123 fputs_filtered (" ", stream);
1124 }
1125 if (show < 0)
1126 fprintf_filtered (stream, "{...}");
1127 else
1128 {
1129 int i;
1130
1131 type_print_derivation_info (stream, type);
1132
1133 fprintf_filtered (stream, "{");
1134 len = TYPE_NFIELDS (type);
1135 if (len)
1136 fprintf_filtered (stream, "\n");
1137 else
1138 {
1139 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1140 fprintf_filtered (stream, "<incomplete type>\n");
1141 else
1142 fprintf_filtered (stream, "<no data fields>\n");
1143 }
1144
1145 /* If there is a base class for this type,
1146 do not print the field that it occupies. */
1147 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1148 {
1149 QUIT;
1150 /* Don't print out virtual function table. */
1151 if (! strncmp (TYPE_FIELD_NAME (type, i),
1152 "_vptr$", 6))
1153 continue;
1154
1155 print_spaces_filtered (level + 4, stream);
1156 if (TYPE_FIELD_STATIC (type, i))
1157 {
1158 fprintf_filtered (stream, "static ");
1159 }
1160 type_print_1 (TYPE_FIELD_TYPE (type, i),
1161 TYPE_FIELD_NAME (type, i),
1162 stream, show - 1, level + 4);
1163 if (!TYPE_FIELD_STATIC (type, i)
1164 && TYPE_FIELD_PACKED (type, i))
1165 {
1166 /* It is a bitfield. This code does not attempt
1167 to look at the bitpos and reconstruct filler,
1168 unnamed fields. This would lead to misleading
1169 results if the compiler does not put out fields
1170 for such things (I don't know what it does). */
1171 fprintf_filtered (stream, " : %d",
1172 TYPE_FIELD_BITSIZE (type, i));
1173 }
1174 fprintf_filtered (stream, ";\n");
1175 }
1176
1177 /* C++: print out the methods */
1178 len = TYPE_NFN_FIELDS (type);
1179 if (len) fprintf_filtered (stream, "\n");
1180 for (i = 0; i < len; i++)
1181 {
1182 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1183 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1184
1185 for (j = 0; j < len2; j++)
1186 {
1187 QUIT;
1188 print_spaces_filtered (level + 4, stream);
1189 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1190 fprintf_filtered (stream, "virtual ");
1191 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1192 fprintf_filtered (stream, "static ");
1193 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), "", stream, 0);
1194 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
1195 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == '$')
1196 type_print_method_args
1197 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
1198 TYPE_FN_FIELDLIST_NAME (type, i), 0, stream);
1199 else
1200 type_print_method_args
1201 (TYPE_FN_FIELD_ARGS (f, j), "",
1202 TYPE_FN_FIELDLIST_NAME (type, i),
1203 TYPE_FN_FIELD_STATIC_P (f, j), stream);
1204
1205 fprintf_filtered (stream, ";\n");
1206 }
1207 if (len2) fprintf_filtered (stream, "\n");
1208 }
1209
1210 print_spaces_filtered (level, stream);
1211 fprintf_filtered (stream, "}");
1212 }
1213 break;
1214
1215 case TYPE_CODE_ENUM:
1216 fprintf_filtered (stream, "enum ");
1217 if (TYPE_NAME (type))
1218 {
1219 name = TYPE_NAME (type);
1220 while (*name != ' ') name++;
1221 fputs_filtered (name + 1, stream);
1222 fputs_filtered (" ", stream);
1223 }
1224 if (show < 0)
1225 fprintf_filtered (stream, "{...}");
1226 else
1227 {
1228 fprintf_filtered (stream, "{");
1229 len = TYPE_NFIELDS (type);
1230 lastval = 0;
1231 for (i = 0; i < len; i++)
1232 {
1233 QUIT;
1234 if (i) fprintf_filtered (stream, ", ");
1235 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1236 if (lastval != TYPE_FIELD_BITPOS (type, i))
1237 {
1238 fprintf_filtered (stream, " : %d", TYPE_FIELD_BITPOS (type, i));
1239 lastval = TYPE_FIELD_BITPOS (type, i);
1240 }
1241 lastval++;
1242 }
1243 fprintf_filtered (stream, "}");
1244 }
1245 break;
1246
1247 case TYPE_CODE_INT:
1248 if (TYPE_UNSIGNED (type))
1249 name = unsigned_type_table[TYPE_LENGTH (type)];
1250 else
1251 name = signed_type_table[TYPE_LENGTH (type)];
1252 fputs_filtered (name, stream);
1253 break;
1254
1255 case TYPE_CODE_FLT:
1256 name = float_type_table[TYPE_LENGTH (type)];
1257 fputs_filtered (name, stream);
1258 break;
1259
1260 case TYPE_CODE_VOID:
1261 fprintf_filtered (stream, "void");
1262 break;
1263
1264 case 0:
1265 fprintf_filtered (stream, "struct unknown");
1266 break;
1267
1268 default:
1269 error ("Invalid type code in symbol table.");
1270 }
1271 }
1272 \f
1273 static void
1274 set_maximum_command (arg)
1275 char *arg;
1276 {
1277 if (!arg) error_no_arg ("value for maximum elements to print");
1278 print_max = parse_and_eval_address (arg);
1279 }
1280
1281 static void
1282 set_prettyprint_command (arg, from_tty)
1283 char *arg;
1284 int from_tty;
1285 {
1286 prettyprint = parse_binary_operation ("set prettyprint", arg);
1287 }
1288
1289 static void
1290 set_unionprint_command (arg, from_tty)
1291 char *arg;
1292 int from_tty;
1293 {
1294 unionprint = parse_binary_operation ("set unionprint", arg);
1295 }
1296
1297 format_info (arg, from_tty)
1298 char *arg;
1299 int from_tty;
1300 {
1301 if (arg)
1302 error ("\"info format\" does not take any arguments.");
1303 printf ("Prettyprinting of structures is %s.\n",
1304 prettyprint ? "on" : "off");
1305 printf ("Printing of unions interior to structures is %s.\n",
1306 unionprint ? "on" : "off");
1307 printf ("The maximum number of array elements printed is %d.\n",
1308 print_max);
1309 }
1310
1311 extern struct cmd_list_element *setlist;
1312
1313 void
1314 _initialize_valprint ()
1315 {
1316 add_cmd ("array-max", class_vars, set_maximum_command,
1317 "Set NUMBER as limit on string chars or array elements to print.",
1318 &setlist);
1319
1320 add_cmd ("prettyprint", class_support, set_prettyprint_command,
1321 "Turn prettyprinting of structures on and off.",
1322 &setlist);
1323 add_alias_cmd ("pp", "prettyprint", class_support, 1, &setlist);
1324
1325 add_cmd ("unionprint", class_support, set_unionprint_command,
1326 "Turn printing of unions interior to structures on and off.",
1327 &setlist);
1328
1329 add_info ("format", format_info,
1330 "Show current settings of data formatting options.");
1331
1332 /* Give people the defaults which they are used to. */
1333 prettyprint = 0;
1334 unionprint = 1;
1335
1336 print_max = 200;
1337
1338 unsigned_type_table
1339 = (char **) xmalloc ((1 + sizeof (unsigned LONGEST)) * sizeof (char *));
1340 bzero (unsigned_type_table, (1 + sizeof (unsigned LONGEST)));
1341 unsigned_type_table[sizeof (unsigned char)] = "unsigned char";
1342 unsigned_type_table[sizeof (unsigned short)] = "unsigned short";
1343 unsigned_type_table[sizeof (unsigned long)] = "unsigned long";
1344 unsigned_type_table[sizeof (unsigned int)] = "unsigned int";
1345 #ifdef LONG_LONG
1346 unsigned_type_table[sizeof (unsigned long long)] = "unsigned long long";
1347 #endif
1348
1349 signed_type_table
1350 = (char **) xmalloc ((1 + sizeof (LONGEST)) * sizeof (char *));
1351 bzero (signed_type_table, (1 + sizeof (LONGEST)));
1352 signed_type_table[sizeof (char)] = "char";
1353 signed_type_table[sizeof (short)] = "short";
1354 signed_type_table[sizeof (long)] = "long";
1355 signed_type_table[sizeof (int)] = "int";
1356 #ifdef LONG_LONG
1357 signed_type_table[sizeof (long long)] = "long long";
1358 #endif
1359
1360 float_type_table
1361 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
1362 bzero (float_type_table, (1 + sizeof (double)));
1363 float_type_table[sizeof (float)] = "float";
1364 float_type_table[sizeof (double)] = "double";
1365 }
1366
This page took 0.057274 seconds and 5 git commands to generate.