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