1 /* Support for printing C values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "expression.h"
32 extern int vtblprint
; /* Controls printing of vtbl's */
33 extern int demangle
; /* whether to print C++ syms raw or src-form */
36 cp_print_class_member
PARAMS ((char *, struct type
*, GDB_FILE
*, char *));
39 cp_print_class_method
PARAMS ((char *, struct type
*, GDB_FILE
*));
42 cp_print_value_fields
PARAMS ((struct type
*, char *, GDB_FILE
*, int, int,
43 enum val_prettyprint
, struct type
**));
46 cp_is_vtbl_ptr_type
PARAMS ((struct type
*));
49 cp_is_vtbl_member
PARAMS ((struct type
*));
54 /* BEGIN-FIXME: Hooks into c-typeprint.c */
57 c_type_print_varspec_prefix
PARAMS ((struct type
*, GDB_FILE
*, int, int));
60 cp_type_print_method_args
PARAMS ((struct type
**, char *, char *, int,
65 extern struct obstack dont_print_obstack
;
68 /* Print data of type TYPE located at VALADDR (within GDB), which came from
69 the inferior at address ADDRESS, onto stdio stream STREAM according to
70 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
73 If the data are a string pointer, returns the number of string characters
76 If DEREF_REF is nonzero, then dereference references, otherwise just print
79 The PRETTY parameter controls prettyprinting. */
82 c_val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
,
91 enum val_prettyprint pretty
;
93 register unsigned int i
= 0; /* Number of characters printed */
100 switch (TYPE_CODE (type
))
102 case TYPE_CODE_ARRAY
:
103 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
105 elttype
= TYPE_TARGET_TYPE (type
);
106 eltlen
= TYPE_LENGTH (elttype
);
107 len
= TYPE_LENGTH (type
) / eltlen
;
108 if (prettyprint_arrays
)
110 print_spaces_filtered (2 + 2 * recurse
, stream
);
112 /* For an array of chars, print with string syntax. */
113 if (eltlen
== 1 && TYPE_CODE (elttype
) == TYPE_CODE_INT
114 && (format
== 0 || format
== 's'))
116 LA_PRINT_STRING (stream
, valaddr
, len
, 0);
121 fprintf_filtered (stream
, "{");
122 /* If this is a virtual function table, print the 0th
123 entry specially, and the rest of the members normally. */
124 if (cp_is_vtbl_ptr_type (elttype
))
127 fprintf_filtered (stream
, "%d vtable entries", len
- 1);
133 val_print_array_elements (type
, valaddr
, address
, stream
,
134 format
, deref_ref
, recurse
, pretty
, i
);
135 fprintf_filtered (stream
, "}");
139 /* Array of unspecified length: treat like pointer to first elt. */
141 goto print_unpacked_pointer
;
144 if (format
&& format
!= 's')
146 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
149 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_METHOD
)
151 cp_print_class_method (valaddr
, type
, stream
);
153 else if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
155 cp_print_class_member (valaddr
,
156 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
161 addr
= unpack_pointer (type
, valaddr
);
162 print_unpacked_pointer
:
163 elttype
= TYPE_TARGET_TYPE (type
);
165 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
167 /* Try to print what function it points to. */
168 print_address_demangle (addr
, stream
, demangle
);
169 /* Return value is irrelevant except for string pointers. */
173 if (addressprint
&& format
!= 's')
175 print_address_numeric (addr
, stream
);
178 /* For a pointer to char or unsigned char, also print the string
179 pointed to, unless pointer is null. */
180 if (TYPE_LENGTH (elttype
) == 1
181 && TYPE_CODE (elttype
) == TYPE_CODE_INT
182 && (format
== 0 || format
== 's')
185 i
= val_print_string (addr
, 0, stream
);
187 else if (cp_is_vtbl_member(type
))
189 /* print vtbl's nicely */
190 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
);
192 struct minimal_symbol
*msymbol
=
193 lookup_minimal_symbol_by_pc (vt_address
);
194 if ((msymbol
!= NULL
) &&
195 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
197 fputs_filtered (" <", stream
);
198 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol
), stream
);
199 fputs_filtered (">", stream
);
201 if (vt_address
&& vtblprint
)
204 struct symbol
*wsym
= (struct symbol
*)NULL
;
207 struct block
*block
= (struct block
*)NULL
;
211 wsym
= lookup_symbol (SYMBOL_NAME(msymbol
), block
,
212 VAR_NAMESPACE
, &is_this_fld
, &s
);
216 wtype
= SYMBOL_TYPE(wsym
);
220 wtype
= TYPE_TARGET_TYPE(type
);
222 vt_val
= value_at (wtype
, vt_address
);
223 val_print (VALUE_TYPE (vt_val
), VALUE_CONTENTS (vt_val
),
224 VALUE_ADDRESS (vt_val
), stream
, format
,
225 deref_ref
, recurse
+ 1, pretty
);
228 fprintf_filtered (stream
, "\n");
229 print_spaces_filtered (2 + 2 * recurse
, stream
);
234 /* Return number of characters printed, plus one for the
235 terminating null if we have "reached the end". */
236 return (i
+ (print_max
&& i
!= print_max
));
240 case TYPE_CODE_MEMBER
:
241 error ("not implemented: member type in c_val_print");
245 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
247 cp_print_class_member (valaddr
,
248 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
254 fprintf_filtered (stream
, "@");
255 print_address_numeric
256 (extract_address (valaddr
,
257 TARGET_PTR_BIT
/ HOST_CHAR_BIT
), stream
);
259 fputs_filtered (": ", stream
);
261 /* De-reference the reference. */
264 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_UNDEF
)
268 (TYPE_TARGET_TYPE (type
),
269 unpack_pointer (lookup_pointer_type (builtin_type_void
),
271 val_print (VALUE_TYPE (deref_val
),
272 VALUE_CONTENTS (deref_val
),
273 VALUE_ADDRESS (deref_val
), stream
, format
,
274 deref_ref
, recurse
+ 1, pretty
);
277 fputs_filtered ("???", stream
);
281 case TYPE_CODE_UNION
:
282 if (recurse
&& !unionprint
)
284 fprintf_filtered (stream
, "{...}");
288 case TYPE_CODE_STRUCT
:
289 if (vtblprint
&& cp_is_vtbl_ptr_type(type
))
291 /* Print the unmangled name if desired. */
292 print_address_demangle(*((int *) (valaddr
+ /* FIXME bytesex */
293 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8)),
297 cp_print_value_fields (type
, valaddr
, stream
, format
, recurse
, pretty
,
304 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
307 len
= TYPE_NFIELDS (type
);
308 val
= unpack_long (type
, valaddr
);
309 for (i
= 0; i
< len
; i
++)
312 if (val
== TYPE_FIELD_BITPOS (type
, i
))
319 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
323 print_longest (stream
, 'd', 0, val
);
330 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
333 /* FIXME, we should consider, at least for ANSI C language, eliminating
334 the distinction made between FUNCs and POINTERs to FUNCs. */
335 fprintf_filtered (stream
, "{");
336 type_print (type
, "", stream
, -1);
337 fprintf_filtered (stream
, "} ");
338 /* Try to print what function it points to, and its address. */
339 print_address_demangle (address
, stream
, demangle
);
343 /* Do something at least vaguely reasonable, for example if the
344 language is set wrong. */
346 case TYPE_CODE_RANGE
:
347 /* FIXME: create_range_type does not set the unsigned bit in a
348 range type (I think it probably should copy it from the target
349 type), so we won't print values which are too large to
350 fit in a signed integer correctly. */
351 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
352 print with the target type, though, because the size of our type
353 and the target type might differ). */
357 format
= format
? format
: output_format
;
360 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
364 val_print_type_code_int (type
, valaddr
, stream
);
365 /* C and C++ has no single byte int type, char is used instead.
366 Since we don't know whether the value is really intended to
367 be used as an integer or a character, print the character
368 equivalent as well. */
369 if (TYPE_LENGTH (type
) == 1)
371 fputs_filtered (" ", stream
);
372 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
),
379 format
= format
? format
: output_format
;
382 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
386 fprintf_filtered (stream
, TYPE_UNSIGNED (type
) ? "%u" : "%d",
387 unpack_long (type
, valaddr
));
388 fputs_filtered (" ", stream
);
389 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
), stream
);
396 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
400 print_floating (valaddr
, type
, stream
);
405 fprintf_filtered (stream
, "void");
408 case TYPE_CODE_ERROR
:
409 fprintf_filtered (stream
, "<error type>");
412 case TYPE_CODE_UNDEF
:
413 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
414 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
415 and no complete type for struct foo in that file. */
416 fprintf_filtered (stream
, "<incomplete type>");
420 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type
));