* m32c/gdb-if.c (m32c_signal_to_host): Rename to
[gdb/SamB.git] / gdb / c-valprint.c
blobcfdf2c05cc19bb2f95ad918bb0eda2790c034690
1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "c-lang.h"
31 #include "cp-abi.h"
32 #include "target.h"
35 /* Print function pointer with inferior address ADDRESS onto stdio
36 stream STREAM. */
38 static void
39 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream,
40 int addressprint)
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
43 address,
44 &current_target);
46 /* If the function pointer is represented by a description, print the
47 address of the description. */
48 if (addressprint && func_addr != address)
50 fputs_filtered ("@", stream);
51 fputs_filtered (paddress (address), stream);
52 fputs_filtered (": ", stream);
54 print_address_demangle (func_addr, stream, demangle);
58 /* A helper for textual_element_type. This checks the name of the
59 typedef. This is bogus but it isn't apparent that the compiler
60 provides us the help we may need. */
62 static int
63 textual_name (const char *name)
65 return (!strcmp (name, "wchar_t")
66 || !strcmp (name, "char16_t")
67 || !strcmp (name, "char32_t"));
70 /* Apply a heuristic to decide whether an array of TYPE or a pointer
71 to TYPE should be printed as a textual string. Return non-zero if
72 it should, or zero if it should be treated as an array of integers
73 or pointer to integers. FORMAT is the current format letter,
74 or 0 if none.
76 We guess that "char" is a character. Explicitly signed and
77 unsigned character types are also characters. Integer data from
78 vector types is not. The user can override this by using the /s
79 format letter. */
81 static int
82 textual_element_type (struct type *type, char format)
84 struct type *true_type = check_typedef (type);
86 if (format != 0 && format != 's')
87 return 0;
89 /* TYPE_CODE_CHAR is always textual. */
90 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
91 return 1;
92 /* Any other character-like types must be integral. */
93 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
94 return 0;
96 /* Check the names of the type and the typedef. */
97 if (TYPE_NAME (type) && textual_name (TYPE_NAME (type)))
98 return 1;
99 if (TYPE_NAME (true_type) && textual_name (TYPE_NAME (true_type)))
100 return 1;
102 if (format == 's')
104 /* Print this as a string if we can manage it. For now, no
105 wide character support. */
106 if (TYPE_CODE (true_type) == TYPE_CODE_INT
107 && TYPE_LENGTH (true_type) == 1)
108 return 1;
110 else
112 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
113 flag, then we treat it as text; otherwise, we assume it's
114 being used as data. */
115 if (TYPE_CODE (true_type) == TYPE_CODE_INT
116 && TYPE_LENGTH (true_type) == 1
117 && !TYPE_NOTTEXT (true_type))
118 return 1;
121 return 0;
125 /* Print data of type TYPE located at VALADDR (within GDB), which came from
126 the inferior at address ADDRESS, onto stdio stream STREAM according to
127 OPTIONS. The data at VALADDR is in target byte order.
129 If the data are a string pointer, returns the number of string characters
130 printed. */
133 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
134 CORE_ADDR address, struct ui_file *stream, int recurse,
135 const struct value_print_options *options)
137 unsigned int i = 0; /* Number of characters printed */
138 unsigned len;
139 struct type *elttype, *unresolved_elttype;
140 struct type *unresolved_type = type;
141 unsigned eltlen;
142 LONGEST val;
143 CORE_ADDR addr;
145 CHECK_TYPEDEF (type);
146 switch (TYPE_CODE (type))
148 case TYPE_CODE_ARRAY:
149 unresolved_elttype = TYPE_TARGET_TYPE (type);
150 elttype = check_typedef (unresolved_elttype);
151 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
153 eltlen = TYPE_LENGTH (elttype);
154 len = TYPE_LENGTH (type) / eltlen;
155 if (options->prettyprint_arrays)
157 print_spaces_filtered (2 + 2 * recurse, stream);
160 /* Print arrays of textual chars with a string syntax. */
161 if (textual_element_type (unresolved_elttype, options->format))
163 /* If requested, look for the first null char and only print
164 elements up to it. */
165 if (options->stop_print_at_null)
167 unsigned int temp_len;
169 for (temp_len = 0;
170 (temp_len < len
171 && temp_len < options->print_max
172 && extract_unsigned_integer (valaddr + embedded_offset
173 + temp_len * eltlen,
174 eltlen) == 0);
175 ++temp_len)
177 len = temp_len;
180 LA_PRINT_STRING (stream, unresolved_elttype,
181 valaddr + embedded_offset, len, 0, options);
182 i = len;
184 else
186 fprintf_filtered (stream, "{");
187 /* If this is a virtual function table, print the 0th
188 entry specially, and the rest of the members normally. */
189 if (cp_is_vtbl_ptr_type (elttype))
191 i = 1;
192 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
194 else
196 i = 0;
198 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
199 recurse, options, i);
200 fprintf_filtered (stream, "}");
202 break;
204 /* Array of unspecified length: treat like pointer to first elt. */
205 addr = address;
206 goto print_unpacked_pointer;
208 case TYPE_CODE_MEMBERPTR:
209 if (options->format)
211 print_scalar_formatted (valaddr + embedded_offset, type,
212 options, 0, stream);
213 break;
215 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
216 break;
218 case TYPE_CODE_METHODPTR:
219 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
220 break;
222 case TYPE_CODE_PTR:
223 if (options->format && options->format != 's')
225 print_scalar_formatted (valaddr + embedded_offset, type,
226 options, 0, stream);
227 break;
229 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
231 /* Print the unmangled name if desired. */
232 /* Print vtable entry - we only get here if we ARE using
233 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
234 CORE_ADDR addr
235 = extract_typed_address (valaddr + embedded_offset, type);
236 print_function_pointer_address (addr, stream, options->addressprint);
237 break;
239 unresolved_elttype = TYPE_TARGET_TYPE (type);
240 elttype = check_typedef (unresolved_elttype);
242 addr = unpack_pointer (type, valaddr + embedded_offset);
243 print_unpacked_pointer:
245 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
247 /* Try to print what function it points to. */
248 print_function_pointer_address (addr, stream,
249 options->addressprint);
250 /* Return value is irrelevant except for string pointers. */
251 return (0);
254 if (options->addressprint)
255 fputs_filtered (paddress (addr), stream);
257 /* For a pointer to a textual type, also print the string
258 pointed to, unless pointer is null. */
260 if (textual_element_type (unresolved_elttype, options->format)
261 && addr != 0)
263 i = val_print_string (unresolved_elttype, addr, -1, stream,
264 options);
266 else if (cp_is_vtbl_member (type))
268 /* print vtbl's nicely */
269 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
271 struct minimal_symbol *msymbol =
272 lookup_minimal_symbol_by_pc (vt_address);
273 if ((msymbol != NULL) &&
274 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
276 fputs_filtered (" <", stream);
277 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
278 fputs_filtered (">", stream);
280 if (vt_address && options->vtblprint)
282 struct value *vt_val;
283 struct symbol *wsym = (struct symbol *) NULL;
284 struct type *wtype;
285 struct block *block = (struct block *) NULL;
286 int is_this_fld;
288 if (msymbol != NULL)
289 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
290 VAR_DOMAIN, &is_this_fld);
292 if (wsym)
294 wtype = SYMBOL_TYPE (wsym);
296 else
298 wtype = unresolved_elttype;
300 vt_val = value_at (wtype, vt_address);
301 common_val_print (vt_val, stream, recurse + 1, options,
302 current_language);
303 if (options->pretty)
305 fprintf_filtered (stream, "\n");
306 print_spaces_filtered (2 + 2 * recurse, stream);
311 /* Return number of characters printed, including the terminating
312 '\0' if we reached the end. val_print_string takes care including
313 the terminating '\0' if necessary. */
314 return i;
316 break;
318 case TYPE_CODE_REF:
319 elttype = check_typedef (TYPE_TARGET_TYPE (type));
320 if (options->addressprint)
322 CORE_ADDR addr
323 = extract_typed_address (valaddr + embedded_offset, type);
324 fprintf_filtered (stream, "@");
325 fputs_filtered (paddress (addr), stream);
326 if (options->deref_ref)
327 fputs_filtered (": ", stream);
329 /* De-reference the reference. */
330 if (options->deref_ref)
332 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
334 struct value *deref_val =
335 value_at
336 (TYPE_TARGET_TYPE (type),
337 unpack_pointer (type, valaddr + embedded_offset));
338 common_val_print (deref_val, stream, recurse, options,
339 current_language);
341 else
342 fputs_filtered ("???", stream);
344 break;
346 case TYPE_CODE_UNION:
347 if (recurse && !options->unionprint)
349 fprintf_filtered (stream, "{...}");
350 break;
352 /* Fall through. */
353 case TYPE_CODE_STRUCT:
354 /*FIXME: Abstract this away */
355 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
357 /* Print the unmangled name if desired. */
358 /* Print vtable entry - we only get here if NOT using
359 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
360 int offset = (embedded_offset +
361 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
362 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
363 CORE_ADDR addr
364 = extract_typed_address (valaddr + offset, field_type);
366 print_function_pointer_address (addr, stream, options->addressprint);
368 else
369 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream,
370 recurse, options, NULL, 0);
371 break;
373 case TYPE_CODE_ENUM:
374 if (options->format)
376 print_scalar_formatted (valaddr + embedded_offset, type,
377 options, 0, stream);
378 break;
380 len = TYPE_NFIELDS (type);
381 val = unpack_long (type, valaddr + embedded_offset);
382 for (i = 0; i < len; i++)
384 QUIT;
385 if (val == TYPE_FIELD_BITPOS (type, i))
387 break;
390 if (i < len)
392 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
394 else
396 print_longest (stream, 'd', 0, val);
398 break;
400 case TYPE_CODE_FLAGS:
401 if (options->format)
402 print_scalar_formatted (valaddr + embedded_offset, type,
403 options, 0, stream);
404 else
405 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
406 break;
408 case TYPE_CODE_FUNC:
409 case TYPE_CODE_METHOD:
410 if (options->format)
412 print_scalar_formatted (valaddr + embedded_offset, type,
413 options, 0, stream);
414 break;
416 /* FIXME, we should consider, at least for ANSI C language, eliminating
417 the distinction made between FUNCs and POINTERs to FUNCs. */
418 fprintf_filtered (stream, "{");
419 type_print (type, "", stream, -1);
420 fprintf_filtered (stream, "} ");
421 /* Try to print what function it points to, and its address. */
422 print_address_demangle (address, stream, demangle);
423 break;
425 case TYPE_CODE_BOOL:
426 if (options->format || options->output_format)
428 struct value_print_options opts = *options;
429 opts.format = (options->format ? options->format
430 : options->output_format);
431 print_scalar_formatted (valaddr + embedded_offset, type,
432 &opts, 0, stream);
434 else
436 val = unpack_long (type, valaddr + embedded_offset);
437 if (val == 0)
438 fputs_filtered ("false", stream);
439 else if (val == 1)
440 fputs_filtered ("true", stream);
441 else
442 print_longest (stream, 'd', 0, val);
444 break;
446 case TYPE_CODE_RANGE:
447 /* FIXME: create_range_type does not set the unsigned bit in a
448 range type (I think it probably should copy it from the target
449 type), so we won't print values which are too large to
450 fit in a signed integer correctly. */
451 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
452 print with the target type, though, because the size of our type
453 and the target type might differ). */
454 /* FALLTHROUGH */
456 case TYPE_CODE_INT:
457 if (options->format || options->output_format)
459 struct value_print_options opts = *options;
460 opts.format = (options->format ? options->format
461 : options->output_format);
462 print_scalar_formatted (valaddr + embedded_offset, type,
463 &opts, 0, stream);
465 else
467 val_print_type_code_int (type, valaddr + embedded_offset, stream);
468 /* C and C++ has no single byte int type, char is used instead.
469 Since we don't know whether the value is really intended to
470 be used as an integer or a character, print the character
471 equivalent as well. */
472 if (textual_element_type (unresolved_type, options->format))
474 fputs_filtered (" ", stream);
475 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
476 unresolved_type, stream);
479 break;
481 case TYPE_CODE_CHAR:
482 if (options->format || options->output_format)
484 struct value_print_options opts = *options;
485 opts.format = (options->format ? options->format
486 : options->output_format);
487 print_scalar_formatted (valaddr + embedded_offset, type,
488 &opts, 0, stream);
490 else
492 val = unpack_long (type, valaddr + embedded_offset);
493 if (TYPE_UNSIGNED (type))
494 fprintf_filtered (stream, "%u", (unsigned int) val);
495 else
496 fprintf_filtered (stream, "%d", (int) val);
497 fputs_filtered (" ", stream);
498 LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream);
500 break;
502 case TYPE_CODE_FLT:
503 if (options->format)
505 print_scalar_formatted (valaddr + embedded_offset, type,
506 options, 0, stream);
508 else
510 print_floating (valaddr + embedded_offset, type, stream);
512 break;
514 case TYPE_CODE_DECFLOAT:
515 if (options->format)
516 print_scalar_formatted (valaddr + embedded_offset, type,
517 options, 0, stream);
518 else
519 print_decimal_floating (valaddr + embedded_offset, type, stream);
520 break;
522 case TYPE_CODE_VOID:
523 fprintf_filtered (stream, "void");
524 break;
526 case TYPE_CODE_ERROR:
527 fprintf_filtered (stream, _("<error type>"));
528 break;
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>"));
535 break;
537 case TYPE_CODE_COMPLEX:
538 if (options->format)
539 print_scalar_formatted (valaddr + embedded_offset,
540 TYPE_TARGET_TYPE (type),
541 options, 0, stream);
542 else
543 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
544 stream);
545 fprintf_filtered (stream, " + ");
546 if (options->format)
547 print_scalar_formatted (valaddr + embedded_offset
548 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
549 TYPE_TARGET_TYPE (type),
550 options, 0, stream);
551 else
552 print_floating (valaddr + embedded_offset
553 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
554 TYPE_TARGET_TYPE (type),
555 stream);
556 fprintf_filtered (stream, " * I");
557 break;
559 default:
560 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
562 gdb_flush (stream);
563 return (0);
567 c_value_print (struct value *val, struct ui_file *stream,
568 const struct value_print_options *options)
570 struct type *type, *real_type, *val_type;
571 int full, top, using_enc;
572 struct value_print_options opts = *options;
574 opts.deref_ref = 1;
576 /* If it is a pointer, indicate what it points to.
578 Print type also if it is a reference.
580 C++: if it is a member pointer, we will take care
581 of that when we print it. */
583 /* Preserve the original type before stripping typedefs. We prefer
584 to pass down the original type when possible, but for local
585 checks it is better to look past the typedefs. */
586 val_type = value_type (val);
587 type = check_typedef (val_type);
589 if (TYPE_CODE (type) == TYPE_CODE_PTR
590 || TYPE_CODE (type) == TYPE_CODE_REF)
592 /* Hack: remove (char *) for char strings. Their
593 type is indicated by the quoted string anyway.
594 (Don't use textual_element_type here; quoted strings
595 are always exactly (char *), (wchar_t *), or the like. */
596 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
597 && TYPE_NAME (val_type) == NULL
598 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
599 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0
600 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
602 /* Print nothing */
604 else if (options->objectprint
605 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
608 if (TYPE_CODE(type) == TYPE_CODE_REF)
610 /* Copy value, change to pointer, so we don't get an
611 * error about a non-pointer type in value_rtti_target_type
613 struct value *temparg;
614 temparg=value_copy(val);
615 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
616 val=temparg;
618 /* Pointer to class, check real type of object */
619 fprintf_filtered (stream, "(");
620 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
621 if (real_type)
623 /* RTTI entry found */
624 if (TYPE_CODE (type) == TYPE_CODE_PTR)
626 /* create a pointer type pointing to the real type */
627 type = lookup_pointer_type (real_type);
629 else
631 /* create a reference type referencing the real type */
632 type = lookup_reference_type (real_type);
634 /* JYG: Need to adjust pointer value. */
635 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
636 value_contents_writeable (val)[0] -= top;
638 /* Note: When we look up RTTI entries, we don't get any
639 information on const or volatile attributes */
641 type_print (type, "", stream, -1);
642 fprintf_filtered (stream, ") ");
643 val_type = type;
645 else
647 /* normal case */
648 fprintf_filtered (stream, "(");
649 type_print (value_type (val), "", stream, -1);
650 fprintf_filtered (stream, ") ");
654 if (!value_initialized (val))
655 fprintf_filtered (stream, " [uninitialized] ");
657 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
659 /* Attempt to determine real type of object */
660 real_type = value_rtti_type (val, &full, &top, &using_enc);
661 if (real_type)
663 /* We have RTTI information, so use it */
664 val = value_full_object (val, real_type, full, top, using_enc);
665 fprintf_filtered (stream, "(%s%s) ",
666 TYPE_NAME (real_type),
667 full ? "" : _(" [incomplete object]"));
668 /* Print out object: enclosing type is same as real_type if full */
669 return val_print (value_enclosing_type (val),
670 value_contents_all (val), 0,
671 VALUE_ADDRESS (val), stream, 0,
672 &opts, current_language);
673 /* Note: When we look up RTTI entries, we don't get any information on
674 const or volatile attributes */
676 else if (type != check_typedef (value_enclosing_type (val)))
678 /* No RTTI information, so let's do our best */
679 fprintf_filtered (stream, "(%s ?) ",
680 TYPE_NAME (value_enclosing_type (val)));
681 return val_print (value_enclosing_type (val),
682 value_contents_all (val), 0,
683 VALUE_ADDRESS (val), stream, 0,
684 &opts, current_language);
686 /* Otherwise, we end up at the return outside this "if" */
689 return val_print (val_type, value_contents_all (val),
690 value_embedded_offset (val),
691 VALUE_ADDRESS (val) + value_offset (val),
692 stream, 0, &opts, current_language);