]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-valprint.c
* configure.ac: Switch license to GPLv3.
[thirdparty/binutils-gdb.git] / gdb / c-valprint.c
CommitLineData
c906108c 1/* Support for printing C values for GDB, the GNU debugger.
1bac305b 2
6aba47ca
DJ
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007
4f2aea11 5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
197e01b6
EZ
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
c906108c
SS
23
24#include "defs.h"
309367d4 25#include "gdb_string.h"
c906108c
SS
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "expression.h"
29#include "value.h"
c906108c
SS
30#include "valprint.h"
31#include "language.h"
32#include "c-lang.h"
015a42b4 33#include "cp-abi.h"
e2d0e7eb 34#include "target.h"
c906108c 35\f
c5aa993b 36
6e778545
PS
37/* Print function pointer with inferior address ADDRESS onto stdio
38 stream STREAM. */
39
40static void
41print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
42{
e2d0e7eb
AC
43 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
44 address,
45 &current_target);
6e778545
PS
46
47 /* If the function pointer is represented by a description, print the
48 address of the description. */
49 if (addressprint && func_addr != address)
50 {
51 fputs_filtered ("@", stream);
66bf4b3a 52 deprecated_print_address_numeric (address, 1, stream);
6e778545
PS
53 fputs_filtered (": ", stream);
54 }
55 print_address_demangle (func_addr, stream, demangle);
56}
57
58
c906108c
SS
59/* Print data of type TYPE located at VALADDR (within GDB), which came from
60 the inferior at address ADDRESS, onto stdio stream STREAM according to
61 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
62 target byte order.
63
64 If the data are a string pointer, returns the number of string characters
65 printed.
66
67 If DEREF_REF is nonzero, then dereference references, otherwise just print
68 them like pointers.
69
70 The PRETTY parameter controls prettyprinting. */
71
72int
fc1a4b47 73c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
fba45db2
KB
74 CORE_ADDR address, struct ui_file *stream, int format,
75 int deref_ref, int recurse, enum val_prettyprint pretty)
c906108c 76{
52f0bd74 77 unsigned int i = 0; /* Number of characters printed */
c906108c
SS
78 unsigned len;
79 struct type *elttype;
80 unsigned eltlen;
81 LONGEST val;
82 CORE_ADDR addr;
83
84 CHECK_TYPEDEF (type);
85 switch (TYPE_CODE (type))
86 {
87 case TYPE_CODE_ARRAY:
88 elttype = check_typedef (TYPE_TARGET_TYPE (type));
89 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
90 {
91 eltlen = TYPE_LENGTH (elttype);
92 len = TYPE_LENGTH (type) / eltlen;
93 if (prettyprint_arrays)
94 {
95 print_spaces_filtered (2 + 2 * recurse, stream);
96 }
97 /* For an array of chars, print with string syntax. */
98 if (eltlen == 1 &&
e1f48ead 99 ((TYPE_CODE (elttype) == TYPE_CODE_INT && TYPE_NOSIGN (elttype))
c906108c
SS
100 || ((current_language->la_language == language_m2)
101 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
102 && (format == 0 || format == 's'))
103 {
104 /* If requested, look for the first null char and only print
c5aa993b 105 elements up to it. */
c906108c
SS
106 if (stop_print_at_null)
107 {
745b8ca0 108 unsigned int temp_len;
c5aa993b 109
c906108c
SS
110 /* Look for a NULL char. */
111 for (temp_len = 0;
112 (valaddr + embedded_offset)[temp_len]
113 && temp_len < len && temp_len < print_max;
114 temp_len++);
115 len = temp_len;
116 }
c5aa993b 117
c906108c
SS
118 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
119 i = len;
120 }
121 else
122 {
123 fprintf_filtered (stream, "{");
124 /* If this is a virtual function table, print the 0th
c5aa993b 125 entry specially, and the rest of the members normally. */
c906108c
SS
126 if (cp_is_vtbl_ptr_type (elttype))
127 {
128 i = 1;
3d263c1d 129 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
c906108c
SS
130 }
131 else
132 {
133 i = 0;
134 }
135 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
c5aa993b 136 format, deref_ref, recurse, pretty, i);
c906108c
SS
137 fprintf_filtered (stream, "}");
138 }
139 break;
140 }
141 /* Array of unspecified length: treat like pointer to first elt. */
142 addr = address;
143 goto print_unpacked_pointer;
144
0d5de010
DJ
145 case TYPE_CODE_MEMBERPTR:
146 if (format)
147 {
148 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
149 break;
150 }
151 cp_print_class_member (valaddr + embedded_offset,
152 TYPE_DOMAIN_TYPE (type),
153 stream, "&");
154 break;
155
156 case TYPE_CODE_METHODPTR:
157 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
158 break;
159
c906108c
SS
160 case TYPE_CODE_PTR:
161 if (format && format != 's')
162 {
163 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
164 break;
165 }
c5aa993b 166 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 167 {
c5aa993b 168 /* Print the unmangled name if desired. */
c906108c
SS
169 /* Print vtable entry - we only get here if we ARE using
170 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
4478b372
JB
171 CORE_ADDR addr
172 = extract_typed_address (valaddr + embedded_offset, type);
6e778545 173 print_function_pointer_address (addr, stream);
c906108c
SS
174 break;
175 }
176 elttype = check_typedef (TYPE_TARGET_TYPE (type));
c906108c
SS
177 {
178 addr = unpack_pointer (type, valaddr + embedded_offset);
179 print_unpacked_pointer:
c906108c
SS
180
181 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
182 {
183 /* Try to print what function it points to. */
6e778545 184 print_function_pointer_address (addr, stream);
c906108c
SS
185 /* Return value is irrelevant except for string pointers. */
186 return (0);
187 }
188
189 if (addressprint && format != 's')
190 {
66bf4b3a 191 deprecated_print_address_numeric (addr, 1, stream);
c906108c
SS
192 }
193
194 /* For a pointer to char or unsigned char, also print the string
195 pointed to, unless pointer is null. */
196 /* FIXME: need to handle wchar_t here... */
197
198 if (TYPE_LENGTH (elttype) == 1
199 && TYPE_CODE (elttype) == TYPE_CODE_INT
200 && (format == 0 || format == 's')
201 && addr != 0)
202 {
203 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
204 }
c5aa993b
JM
205 else if (cp_is_vtbl_member (type))
206 {
c906108c
SS
207 /* print vtbl's nicely */
208 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
209
210 struct minimal_symbol *msymbol =
c5aa993b 211 lookup_minimal_symbol_by_pc (vt_address);
c906108c
SS
212 if ((msymbol != NULL) &&
213 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
214 {
215 fputs_filtered (" <", stream);
de5ad195 216 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
c906108c
SS
217 fputs_filtered (">", stream);
218 }
219 if (vt_address && vtblprint)
c5aa993b 220 {
6943961c 221 struct value *vt_val;
c5aa993b
JM
222 struct symbol *wsym = (struct symbol *) NULL;
223 struct type *wtype;
c5aa993b 224 struct block *block = (struct block *) NULL;
c906108c
SS
225 int is_this_fld;
226
227 if (msymbol != NULL)
22abf04a 228 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
cdef89d0 229 VAR_DOMAIN, &is_this_fld, NULL);
c5aa993b 230
c906108c
SS
231 if (wsym)
232 {
c5aa993b 233 wtype = SYMBOL_TYPE (wsym);
c906108c
SS
234 }
235 else
236 {
c5aa993b 237 wtype = TYPE_TARGET_TYPE (type);
c906108c 238 }
00a4c844 239 vt_val = value_at (wtype, vt_address);
806048c6
DJ
240 common_val_print (vt_val, stream, format,
241 deref_ref, recurse + 1, pretty);
c906108c
SS
242 if (pretty)
243 {
244 fprintf_filtered (stream, "\n");
245 print_spaces_filtered (2 + 2 * recurse, stream);
246 }
c5aa993b
JM
247 }
248 }
c906108c
SS
249
250 /* Return number of characters printed, including the terminating
251 '\0' if we reached the end. val_print_string takes care including
252 the terminating '\0' if necessary. */
253 return i;
254 }
255 break;
256
c906108c
SS
257 case TYPE_CODE_REF:
258 elttype = check_typedef (TYPE_TARGET_TYPE (type));
c906108c 259 if (addressprint)
c5aa993b 260 {
4478b372
JB
261 CORE_ADDR addr
262 = extract_typed_address (valaddr + embedded_offset, type);
c906108c 263 fprintf_filtered (stream, "@");
66bf4b3a 264 deprecated_print_address_numeric (addr, 1, stream);
c906108c
SS
265 if (deref_ref)
266 fputs_filtered (": ", stream);
c5aa993b 267 }
c906108c
SS
268 /* De-reference the reference. */
269 if (deref_ref)
270 {
271 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
272 {
6943961c 273 struct value *deref_val =
c5aa993b
JM
274 value_at
275 (TYPE_TARGET_TYPE (type),
276 unpack_pointer (lookup_pointer_type (builtin_type_void),
00a4c844 277 valaddr + embedded_offset));
806048c6
DJ
278 common_val_print (deref_val, stream, format, deref_ref,
279 recurse, pretty);
c906108c
SS
280 }
281 else
282 fputs_filtered ("???", stream);
283 }
284 break;
285
286 case TYPE_CODE_UNION:
287 if (recurse && !unionprint)
288 {
289 fprintf_filtered (stream, "{...}");
290 break;
291 }
292 /* Fall through. */
293 case TYPE_CODE_STRUCT:
015a42b4 294 /*FIXME: Abstract this away */
c5aa993b 295 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 296 {
c5aa993b 297 /* Print the unmangled name if desired. */
c906108c
SS
298 /* Print vtable entry - we only get here if NOT using
299 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
4478b372
JB
300 int offset = (embedded_offset +
301 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
302 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
303 CORE_ADDR addr
304 = extract_typed_address (valaddr + offset, field_type);
305
6e778545 306 print_function_pointer_address (addr, stream);
c906108c
SS
307 }
308 else
309 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
310 recurse, pretty, NULL, 0);
311 break;
312
313 case TYPE_CODE_ENUM:
314 if (format)
315 {
316 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
317 break;
318 }
319 len = TYPE_NFIELDS (type);
320 val = unpack_long (type, valaddr + embedded_offset);
321 for (i = 0; i < len; i++)
322 {
323 QUIT;
324 if (val == TYPE_FIELD_BITPOS (type, i))
325 {
326 break;
327 }
328 }
329 if (i < len)
330 {
331 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
332 }
333 else
334 {
335 print_longest (stream, 'd', 0, val);
336 }
337 break;
338
4f2aea11
MK
339 case TYPE_CODE_FLAGS:
340 if (format)
341 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
342 else
343 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
344 break;
345
c906108c 346 case TYPE_CODE_FUNC:
0d5de010 347 case TYPE_CODE_METHOD:
c906108c
SS
348 if (format)
349 {
350 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
351 break;
352 }
353 /* FIXME, we should consider, at least for ANSI C language, eliminating
c5aa993b 354 the distinction made between FUNCs and POINTERs to FUNCs. */
c906108c
SS
355 fprintf_filtered (stream, "{");
356 type_print (type, "", stream, -1);
357 fprintf_filtered (stream, "} ");
358 /* Try to print what function it points to, and its address. */
359 print_address_demangle (address, stream, demangle);
360 break;
361
362 case TYPE_CODE_BOOL:
363 format = format ? format : output_format;
364 if (format)
365 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
366 else
367 {
368 val = unpack_long (type, valaddr + embedded_offset);
369 if (val == 0)
370 fputs_filtered ("false", stream);
371 else if (val == 1)
372 fputs_filtered ("true", stream);
373 else
374 print_longest (stream, 'd', 0, val);
375 }
376 break;
377
378 case TYPE_CODE_RANGE:
379 /* FIXME: create_range_type does not set the unsigned bit in a
c5aa993b
JM
380 range type (I think it probably should copy it from the target
381 type), so we won't print values which are too large to
382 fit in a signed integer correctly. */
c906108c 383 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
c5aa993b
JM
384 print with the target type, though, because the size of our type
385 and the target type might differ). */
c906108c
SS
386 /* FALLTHROUGH */
387
388 case TYPE_CODE_INT:
389 format = format ? format : output_format;
390 if (format)
391 {
392 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
393 }
394 else
395 {
396 val_print_type_code_int (type, valaddr + embedded_offset, stream);
397 /* C and C++ has no single byte int type, char is used instead.
398 Since we don't know whether the value is really intended to
399 be used as an integer or a character, print the character
400 equivalent as well. */
401 if (TYPE_LENGTH (type) == 1)
402 {
403 fputs_filtered (" ", stream);
404 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
405 stream);
406 }
407 }
408 break;
409
410 case TYPE_CODE_CHAR:
411 format = format ? format : output_format;
412 if (format)
413 {
414 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
415 }
416 else
417 {
96baa820
JM
418 val = unpack_long (type, valaddr + embedded_offset);
419 if (TYPE_UNSIGNED (type))
420 fprintf_filtered (stream, "%u", (unsigned int) val);
421 else
422 fprintf_filtered (stream, "%d", (int) val);
c906108c 423 fputs_filtered (" ", stream);
96baa820 424 LA_PRINT_CHAR ((unsigned char) val, stream);
c906108c
SS
425 }
426 break;
427
428 case TYPE_CODE_FLT:
429 if (format)
430 {
431 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
432 }
433 else
434 {
435 print_floating (valaddr + embedded_offset, type, stream);
436 }
437 break;
438
c906108c
SS
439 case TYPE_CODE_VOID:
440 fprintf_filtered (stream, "void");
441 break;
442
443 case TYPE_CODE_ERROR:
3d263c1d 444 fprintf_filtered (stream, _("<error type>"));
c906108c
SS
445 break;
446
447 case TYPE_CODE_UNDEF:
448 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
c5aa993b
JM
449 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
450 and no complete type for struct foo in that file. */
3d263c1d 451 fprintf_filtered (stream, _("<incomplete type>"));
c906108c
SS
452 break;
453
fca9e603
DJ
454 case TYPE_CODE_COMPLEX:
455 if (format)
456 print_scalar_formatted (valaddr + embedded_offset,
457 TYPE_TARGET_TYPE (type),
458 format, 0, stream);
459 else
460 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
461 stream);
462 fprintf_filtered (stream, " + ");
463 if (format)
464 print_scalar_formatted (valaddr + embedded_offset
465 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
466 TYPE_TARGET_TYPE (type),
467 format, 0, stream);
468 else
469 print_floating (valaddr + embedded_offset
470 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
471 TYPE_TARGET_TYPE (type),
472 stream);
473 fprintf_filtered (stream, " * I");
474 break;
475
c906108c 476 default:
3d263c1d 477 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
c906108c
SS
478 }
479 gdb_flush (stream);
480 return (0);
481}
482\f
483int
6943961c 484c_value_print (struct value *val, struct ui_file *stream, int format,
fba45db2 485 enum val_prettyprint pretty)
c906108c 486{
88750304 487 struct type *type, *real_type;
c906108c 488 int full, top, using_enc;
c5aa993b 489
c906108c
SS
490 /* If it is a pointer, indicate what it points to.
491
492 Print type also if it is a reference.
493
494 C++: if it is a member pointer, we will take care
495 of that when we print it. */
88750304
DJ
496
497 type = check_typedef (value_type (val));
498
499 if (TYPE_CODE (type) == TYPE_CODE_PTR
500 || TYPE_CODE (type) == TYPE_CODE_REF)
c906108c
SS
501 {
502 /* Hack: remove (char *) for char strings. Their
c5aa993b 503 type is indicated by the quoted string anyway. */
88750304
DJ
504 if (TYPE_CODE (type) == TYPE_CODE_PTR
505 && TYPE_NAME (type) == NULL
506 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
507 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
c906108c
SS
508 {
509 /* Print nothing */
510 }
511 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
c5aa993b 512 {
070ad9f0
DB
513
514 if (TYPE_CODE(type) == TYPE_CODE_REF)
515 {
516 /* Copy value, change to pointer, so we don't get an
517 * error about a non-pointer type in value_rtti_target_type
518 */
6943961c 519 struct value *temparg;
070ad9f0 520 temparg=value_copy(val);
04624583 521 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
070ad9f0
DB
522 val=temparg;
523 }
c5aa993b 524 /* Pointer to class, check real type of object */
c906108c 525 fprintf_filtered (stream, "(");
c4093a6a
JM
526 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
527 if (real_type)
c5aa993b
JM
528 {
529 /* RTTI entry found */
c4093a6a
JM
530 if (TYPE_CODE (type) == TYPE_CODE_PTR)
531 {
532 /* create a pointer type pointing to the real type */
533 type = lookup_pointer_type (real_type);
534 }
535 else
536 {
537 /* create a reference type referencing the real type */
538 type = lookup_reference_type (real_type);
539 }
070ad9f0 540 /* JYG: Need to adjust pointer value. */
5086187c
AC
541 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
542 value_contents_writeable (val)[0] -= top;
070ad9f0 543
c4093a6a
JM
544 /* Note: When we look up RTTI entries, we don't get any
545 information on const or volatile attributes */
546 }
547 type_print (type, "", stream, -1);
c906108c 548 fprintf_filtered (stream, ") ");
c5aa993b 549 }
c906108c
SS
550 else
551 {
c5aa993b 552 /* normal case */
c906108c 553 fprintf_filtered (stream, "(");
88750304 554 type_print (value_type (val), "", stream, -1);
c906108c
SS
555 fprintf_filtered (stream, ") ");
556 }
557 }
88750304 558
42be36b3
CT
559 if (!value_initialized (val))
560 fprintf_filtered (stream, " [uninitialized] ");
561
88750304 562 if (objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
c906108c
SS
563 {
564 /* Attempt to determine real type of object */
565 real_type = value_rtti_type (val, &full, &top, &using_enc);
c5aa993b
JM
566 if (real_type)
567 {
568 /* We have RTTI information, so use it */
569 val = value_full_object (val, real_type, full, top, using_enc);
570 fprintf_filtered (stream, "(%s%s) ",
571 TYPE_NAME (real_type),
3d263c1d 572 full ? "" : _(" [incomplete object]"));
c5aa993b 573 /* Print out object: enclosing type is same as real_type if full */
46615f07
AC
574 return val_print (value_enclosing_type (val),
575 value_contents_all (val), 0,
576 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
c4093a6a
JM
577 /* Note: When we look up RTTI entries, we don't get any information on
578 const or volatile attributes */
c5aa993b 579 }
88750304 580 else if (type != check_typedef (value_enclosing_type (val)))
c5aa993b
JM
581 {
582 /* No RTTI information, so let's do our best */
583 fprintf_filtered (stream, "(%s ?) ",
4754a64e 584 TYPE_NAME (value_enclosing_type (val)));
46615f07
AC
585 return val_print (value_enclosing_type (val),
586 value_contents_all (val), 0,
587 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
c5aa993b 588 }
c906108c
SS
589 /* Otherwise, we end up at the return outside this "if" */
590 }
c5aa993b 591
46615f07 592 return val_print (type, value_contents_all (val),
13c3b5f5 593 value_embedded_offset (val),
df407dfe 594 VALUE_ADDRESS (val) + value_offset (val),
c906108c
SS
595 stream, format, 1, 0, pretty);
596}