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