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_is_vtbl_ptr_type
PARAMS ((struct type
*));
41 cp_print_value_fields
PARAMS ((struct type
*, char *, FILE *, int, int,
42 enum val_prettyprint
, struct type
**));
45 cp_is_vtbl_member
PARAMS ((struct type
*));
50 /* BEGIN-FIXME: Hooks into c-typeprint.c */
53 c_type_print_varspec_prefix
PARAMS ((struct type
*, FILE *, int, int));
56 cp_type_print_method_args
PARAMS ((struct type
**, char *, char *, int,
61 extern struct obstack dont_print_obstack
;
64 /* Print data of type TYPE located at VALADDR (within GDB), which came from
65 the inferior at address ADDRESS, onto stdio stream STREAM according to
66 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
69 If the data are a string pointer, returns the number of string characters
72 If DEREF_REF is nonzero, then dereference references, otherwise just print
75 The PRETTY parameter controls prettyprinting. */
78 c_val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
,
87 enum val_prettyprint pretty
;
89 register unsigned int i
;
96 switch (TYPE_CODE (type
))
99 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
101 elttype
= TYPE_TARGET_TYPE (type
);
102 eltlen
= TYPE_LENGTH (elttype
);
103 len
= TYPE_LENGTH (type
) / eltlen
;
104 if (prettyprint_arrays
)
106 print_spaces_filtered (2 + 2 * recurse
, stream
);
108 fprintf_filtered (stream
, "{");
109 /* For an array of chars, print with string syntax. */
110 if (eltlen
== 1 && TYPE_CODE (elttype
) == TYPE_CODE_INT
111 && (format
== 0 || format
== 's') )
113 LA_PRINT_STRING (stream
, valaddr
, len
, 0);
117 /* If this is a virtual function table, print the 0th
118 entry specially, and the rest of the members normally. */
119 if (cp_is_vtbl_ptr_type (elttype
))
122 fprintf_filtered (stream
, "%d vtable entries", len
- 1);
128 val_print_array_elements (type
, valaddr
, address
, stream
,
129 format
, deref_ref
, recurse
, pretty
, i
);
131 fprintf_filtered (stream
, "}");
134 /* Array of unspecified length: treat like pointer to first elt. */
135 valaddr
= (char *) &address
;
138 if (format
&& format
!= 's')
140 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
143 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_METHOD
)
145 struct type
*domain
= TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
));
151 addr
= unpack_pointer (lookup_pointer_type (builtin_type_void
),
153 if (METHOD_PTR_IS_VIRTUAL(addr
))
155 int offset
= METHOD_PTR_TO_VOFFSET(addr
);
156 len
= TYPE_NFN_FIELDS (domain
);
157 for (i
= 0; i
< len
; i
++)
159 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
160 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
162 for (j
= 0; j
< len2
; j
++)
165 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == offset
)
175 struct symbol
*sym
= find_pc_function (addr
);
178 error ("invalid pointer to member function");
180 len
= TYPE_NFN_FIELDS (domain
);
181 for (i
= 0; i
< len
; i
++)
183 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
184 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
186 for (j
= 0; j
< len2
; j
++)
189 if (STREQ (SYMBOL_NAME (sym
),
190 TYPE_FN_FIELD_PHYSNAME (f
, j
)))
200 fprintf_filtered (stream
, "&");
201 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, 0);
202 fprintf (stream
, kind
);
203 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
204 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
205 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
206 TYPE_FN_FIELDLIST_NAME (domain
, i
),
209 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
210 TYPE_FN_FIELDLIST_NAME (domain
, i
),
214 fprintf_filtered (stream
, "(");
215 type_print (type
, "", stream
, -1);
216 fprintf_filtered (stream
, ") %d", (int) addr
>> 3);
218 else if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
220 cp_print_class_member (valaddr
,
221 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
226 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
227 elttype
= TYPE_TARGET_TYPE (type
);
229 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
231 /* Try to print what function it points to. */
232 print_address_demangle (addr
, stream
, demangle
);
233 /* Return value is irrelevant except for string pointers. */
237 if (addressprint
&& format
!= 's')
239 fprintf_filtered (stream
, "0x%x", addr
);
242 /* For a pointer to char or unsigned char, also print the string
243 pointed to, unless pointer is null. */
244 i
= 0; /* Number of characters printed. */
245 if (TYPE_LENGTH (elttype
) == 1 &&
246 TYPE_CODE (elttype
) == TYPE_CODE_INT
&&
247 (format
== 0 || format
== 's') &&
249 /* If print_max is UINT_MAX, the alloca below will fail.
250 In that case don't try to print the string. */
251 print_max
< UINT_MAX
)
253 int first_addr_err
= 0;
256 /* Get first character. */
257 errcode
= target_read_memory (addr
, (char *)&c
, 1);
260 /* First address out of bounds. */
266 char *string
= (char *) alloca (print_max
);
268 /* If the loop ends by us hitting print_max characters,
269 we need to have elipses at the end. */
270 int force_ellipses
= 1;
272 /* This loop always fetches print_max characters, even
273 though LA_PRINT_STRING might want to print more or fewer
274 (with repeated characters). This is so that
275 we don't spend forever fetching if we print
276 a long string consisting of the same character
277 repeated. Also so we can do it all in one memory
278 operation, which is faster. However, this will be
279 slower if print_max is set high, e.g. if you set
280 print_max to 1000, not only will it take a long
281 time to fetch short strings, but if you are near
282 the end of the address space, it might not work. */
284 errcode
= target_read_memory (addr
, string
, print_max
);
287 /* Try reading just one character. If that succeeds,
288 assume we hit the end of the address space, but
289 the initial part of the string is probably safe. */
291 errcode
= target_read_memory (addr
, x
, 1);
296 for (i
= 0; i
< print_max
; i
++)
297 if (string
[i
] == '\0')
306 fputs_filtered (" ", stream
);
308 LA_PRINT_STRING (stream
, string
, i
, force_ellipses
);
315 fprintf_filtered (stream
,
316 (" <Address 0x%x out of bounds>" +
322 error ("Error reading memory address 0x%x: %s.",
323 addr
+ i
, safe_strerror (errcode
));
329 else if (cp_is_vtbl_member(type
))
331 /* print vtbl's nicely */
332 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
);
334 struct minimal_symbol
*msymbol
=
335 lookup_minimal_symbol_by_pc (vt_address
);
336 if ((msymbol
!= NULL
) &&
337 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
339 fputs_filtered (" <", stream
);
340 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol
), stream
);
341 fputs_filtered (">", stream
);
347 vt_val
= value_at (TYPE_TARGET_TYPE (type
), vt_address
);
348 val_print (VALUE_TYPE (vt_val
), VALUE_CONTENTS (vt_val
),
349 VALUE_ADDRESS (vt_val
), stream
, format
,
350 deref_ref
, recurse
+ 1, pretty
);
353 fprintf_filtered (stream
, "\n");
354 print_spaces_filtered (2 + 2 * recurse
, stream
);
359 /* Return number of characters printed, plus one for the
360 terminating null if we have "reached the end". */
361 return (i
+ (print_max
&& i
!= print_max
));
365 case TYPE_CODE_MEMBER
:
366 error ("not implemented: member type in c_val_print");
370 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
372 cp_print_class_member (valaddr
,
373 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
)),
379 fprintf_filtered (stream
, "@0x%lx",
380 unpack_long (builtin_type_int
, valaddr
));
382 fputs_filtered (": ", stream
);
384 /* De-reference the reference. */
387 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_UNDEF
)
391 (TYPE_TARGET_TYPE (type
),
392 unpack_pointer (lookup_pointer_type (builtin_type_void
),
394 val_print (VALUE_TYPE (deref_val
),
395 VALUE_CONTENTS (deref_val
),
396 VALUE_ADDRESS (deref_val
), stream
, format
,
397 deref_ref
, recurse
+ 1, pretty
);
400 fputs_filtered ("???", stream
);
404 case TYPE_CODE_UNION
:
405 if (recurse
&& !unionprint
)
407 fprintf_filtered (stream
, "{...}");
411 case TYPE_CODE_STRUCT
:
412 if (vtblprint
&& cp_is_vtbl_ptr_type(type
))
414 /* Print the unmangled name if desired. */
415 print_address_demangle(*((int *) (valaddr
+ /* FIXME bytesex */
416 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8)),
420 cp_print_value_fields (type
, valaddr
, stream
, format
, recurse
, pretty
,
427 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
430 len
= TYPE_NFIELDS (type
);
431 val
= unpack_long (builtin_type_int
, valaddr
);
432 for (i
= 0; i
< len
; i
++)
435 if (val
== TYPE_FIELD_BITPOS (type
, i
))
442 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
447 fprintf_filtered (stream
, "%lld", val
);
449 fprintf_filtered (stream
, "%ld", val
);
457 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
460 /* FIXME, we should consider, at least for ANSI C language, eliminating
461 the distinction made between FUNCs and POINTERs to FUNCs. */
462 fprintf_filtered (stream
, "{");
463 type_print (type
, "", stream
, -1);
464 fprintf_filtered (stream
, "} ");
465 /* Try to print what function it points to, and its address. */
466 print_address_demangle (address
, stream
, demangle
);
470 format
= format
? format
: output_format
;
473 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
477 val_print_type_code_int (type
, valaddr
, stream
);
478 /* C and C++ has no single byte int type, char is used instead.
479 Since we don't know whether the value is really intended to
480 be used as an integer or a character, print the character
481 equivalent as well. */
482 if (TYPE_LENGTH (type
) == 1)
484 fputs_filtered (" ", stream
);
485 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
),
492 format
= format
? format
: output_format
;
495 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
499 fprintf_filtered (stream
, TYPE_UNSIGNED (type
) ? "%u" : "%d",
500 unpack_long (type
, valaddr
));
501 fputs_filtered (" ", stream
);
502 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
), stream
);
509 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
513 print_floating (valaddr
, type
, stream
);
518 fprintf_filtered (stream
, "void");
521 case TYPE_CODE_ERROR
:
522 fprintf_filtered (stream
, "<error type>");
525 case TYPE_CODE_RANGE
:
526 /* FIXME, we should not ever have to print one of these yet. */
527 fprintf_filtered (stream
, "<range type>");
530 case TYPE_CODE_UNDEF
:
531 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
532 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
533 and no complete type for struct foo in that file. */
534 fprintf_filtered (stream
, "<incomplete type>");
538 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type
));