1 /* Support for printing C values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
31 extern int vtblprint
; /* Controls printing of vtbl's */
32 extern int demangle
; /* whether to print C++ syms raw or src-form */
35 cp_print_class_member
PARAMS ((char *, struct type
*, FILE *, char *));
38 cp_print_class_method
PARAMS ((char *, struct type
*, FILE *));
41 cp_print_value_fields
PARAMS ((struct type
*, char *, FILE *, int, int,
42 enum val_prettyprint
, struct type
**));
45 cp_is_vtbl_ptr_type
PARAMS ((struct type
*));
48 cp_is_vtbl_member
PARAMS ((struct type
*));
53 /* BEGIN-FIXME: Hooks into c-typeprint.c */
56 c_type_print_varspec_prefix
PARAMS ((struct type
*, FILE *, int, int));
59 cp_type_print_method_args
PARAMS ((struct type
**, char *, char *, int,
64 extern struct obstack dont_print_obstack
;
67 /* Print data of type TYPE located at VALADDR (within GDB), which came from
68 the inferior at address ADDRESS, onto stdio stream STREAM according to
69 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
72 If the data are a string pointer, returns the number of string characters
75 If DEREF_REF is nonzero, then dereference references, otherwise just print
78 The PRETTY parameter controls prettyprinting. */
81 c_val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
,
90 enum val_prettyprint pretty
;
92 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 if (addressprint
&& format
!= 's')
118 fprintf_filtered (stream
, "0x%x ", address
);
120 LA_PRINT_STRING (stream
, valaddr
, len
, 0);
125 fprintf_filtered (stream
, "{");
126 /* If this is a virtual function table, print the 0th
127 entry specially, and the rest of the members normally. */
128 if (cp_is_vtbl_ptr_type (elttype
))
131 fprintf_filtered (stream
, "%d vtable entries", len
- 1);
137 val_print_array_elements (type
, valaddr
, address
, stream
,
138 format
, deref_ref
, recurse
, pretty
, i
);
139 fprintf_filtered (stream
, "}");
143 /* Array of unspecified length: treat like pointer to first elt. */
144 valaddr
= (char *) &address
;
148 if (format
&& format
!= 's')
150 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
153 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_METHOD
)
155 cp_print_class_method (valaddr
, type
, stream
);
157 else if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
159 cp_print_class_member (valaddr
,
160 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
165 addr
= unpack_pointer (type
, valaddr
);
166 elttype
= TYPE_TARGET_TYPE (type
);
168 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
170 /* Try to print what function it points to. */
171 print_address_demangle (addr
, stream
, demangle
);
172 /* Return value is irrelevant except for string pointers. */
176 if (addressprint
&& format
!= 's')
178 fprintf_filtered (stream
, "0x%x", addr
);
181 /* For a pointer to char or unsigned char, also print the string
182 pointed to, unless pointer is null. */
183 if (TYPE_LENGTH (elttype
) == 1
184 && TYPE_CODE (elttype
) == TYPE_CODE_INT
185 && (format
== 0 || format
== 's')
188 i
= val_print_string (addr
, 0, stream
);
190 else if (cp_is_vtbl_member(type
))
192 /* print vtbl's nicely */
193 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
);
195 struct minimal_symbol
*msymbol
=
196 lookup_minimal_symbol_by_pc (vt_address
);
197 if ((msymbol
!= NULL
) &&
198 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
200 fputs_filtered (" <", stream
);
201 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol
), stream
);
202 fputs_filtered (">", stream
);
208 vt_val
= value_at (TYPE_TARGET_TYPE (type
), vt_address
);
209 val_print (VALUE_TYPE (vt_val
), VALUE_CONTENTS (vt_val
),
210 VALUE_ADDRESS (vt_val
), stream
, format
,
211 deref_ref
, recurse
+ 1, pretty
);
214 fprintf_filtered (stream
, "\n");
215 print_spaces_filtered (2 + 2 * recurse
, stream
);
220 /* Return number of characters printed, plus one for the
221 terminating null if we have "reached the end". */
222 return (i
+ (print_max
&& i
!= print_max
));
226 case TYPE_CODE_MEMBER
:
227 error ("not implemented: member type in c_val_print");
231 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
233 cp_print_class_member (valaddr
,
234 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
240 fprintf_filtered (stream
, "@0x%lx",
241 unpack_long (builtin_type_int
, valaddr
));
243 fputs_filtered (": ", stream
);
245 /* De-reference the reference. */
248 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_UNDEF
)
252 (TYPE_TARGET_TYPE (type
),
253 unpack_pointer (lookup_pointer_type (builtin_type_void
),
255 val_print (VALUE_TYPE (deref_val
),
256 VALUE_CONTENTS (deref_val
),
257 VALUE_ADDRESS (deref_val
), stream
, format
,
258 deref_ref
, recurse
+ 1, pretty
);
261 fputs_filtered ("???", stream
);
265 case TYPE_CODE_UNION
:
266 if (recurse
&& !unionprint
)
268 fprintf_filtered (stream
, "{...}");
272 case TYPE_CODE_STRUCT
:
273 if (vtblprint
&& cp_is_vtbl_ptr_type(type
))
275 /* Print the unmangled name if desired. */
276 print_address_demangle(*((int *) (valaddr
+ /* FIXME bytesex */
277 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8)),
281 cp_print_value_fields (type
, valaddr
, stream
, format
, recurse
, pretty
,
288 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
291 len
= TYPE_NFIELDS (type
);
292 val
= unpack_long (builtin_type_int
, valaddr
);
293 for (i
= 0; i
< len
; i
++)
296 if (val
== TYPE_FIELD_BITPOS (type
, i
))
303 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
308 fprintf_filtered (stream
, "%lld", val
);
310 fprintf_filtered (stream
, "%ld", val
);
318 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
321 /* FIXME, we should consider, at least for ANSI C language, eliminating
322 the distinction made between FUNCs and POINTERs to FUNCs. */
323 fprintf_filtered (stream
, "{");
324 type_print (type
, "", stream
, -1);
325 fprintf_filtered (stream
, "} ");
326 /* Try to print what function it points to, and its address. */
327 print_address_demangle (address
, stream
, demangle
);
331 format
= format
? format
: output_format
;
334 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
338 val_print_type_code_int (type
, valaddr
, stream
);
339 /* C and C++ has no single byte int type, char is used instead.
340 Since we don't know whether the value is really intended to
341 be used as an integer or a character, print the character
342 equivalent as well. */
343 if (TYPE_LENGTH (type
) == 1)
345 fputs_filtered (" ", stream
);
346 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
),
353 format
= format
? format
: output_format
;
356 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
360 fprintf_filtered (stream
, TYPE_UNSIGNED (type
) ? "%u" : "%d",
361 unpack_long (type
, valaddr
));
362 fputs_filtered (" ", stream
);
363 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
), stream
);
370 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
374 print_floating (valaddr
, type
, stream
);
379 fprintf_filtered (stream
, "void");
382 case TYPE_CODE_ERROR
:
383 fprintf_filtered (stream
, "<error type>");
386 case TYPE_CODE_RANGE
:
387 /* FIXME, we should not ever have to print one of these yet. */
388 fprintf_filtered (stream
, "<range type>");
391 case TYPE_CODE_UNDEF
:
392 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
393 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
394 and no complete type for struct foo in that file. */
395 fprintf_filtered (stream
, "<incomplete type>");
399 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type
));