gdb:
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2
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.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
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"
33 \f
34
35 /* Print function pointer with inferior address ADDRESS onto stdio
36 stream STREAM. */
37
38 static void
39 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream,
40 int addressprint)
41 {
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
43 address,
44 &current_target);
45
46 /* If the function pointer is represented by a description, print the
47 address of the description. */
48 if (addressprint && func_addr != address)
49 {
50 fputs_filtered ("@", stream);
51 fputs_filtered (paddress (address), stream);
52 fputs_filtered (": ", stream);
53 }
54 print_address_demangle (func_addr, stream, demangle);
55 }
56
57
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. */
61
62 static int
63 textual_name (const char *name)
64 {
65 return (!strcmp (name, "wchar_t")
66 || !strcmp (name, "char16_t")
67 || !strcmp (name, "char32_t"));
68 }
69
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.
75
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. */
80
81 static int
82 textual_element_type (struct type *type, char format)
83 {
84 struct type *true_type = check_typedef (type);
85
86 if (format != 0 && format != 's')
87 return 0;
88
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;
95
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;
101
102 if (format == 's')
103 {
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;
109 }
110 else
111 {
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;
119 }
120
121 return 0;
122 }
123
124
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.
128
129 If the data are a string pointer, returns the number of string characters
130 printed. */
131
132 int
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)
136 {
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;
144
145 CHECK_TYPEDEF (type);
146 switch (TYPE_CODE (type))
147 {
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)
152 {
153 eltlen = TYPE_LENGTH (elttype);
154 len = TYPE_LENGTH (type) / eltlen;
155 if (options->prettyprint_arrays)
156 {
157 print_spaces_filtered (2 + 2 * recurse, stream);
158 }
159
160 /* Print arrays of textual chars with a string syntax. */
161 if (textual_element_type (unresolved_elttype, options->format))
162 {
163 /* If requested, look for the first null char and only print
164 elements up to it. */
165 if (options->stop_print_at_null)
166 {
167 unsigned int temp_len;
168
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)
176 ;
177 len = temp_len;
178 }
179
180 LA_PRINT_STRING (stream, unresolved_elttype,
181 valaddr + embedded_offset, len, 0, options);
182 i = len;
183 }
184 else
185 {
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))
190 {
191 i = 1;
192 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
193 }
194 else
195 {
196 i = 0;
197 }
198 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
199 recurse, options, i);
200 fprintf_filtered (stream, "}");
201 }
202 break;
203 }
204 /* Array of unspecified length: treat like pointer to first elt. */
205 addr = address;
206 goto print_unpacked_pointer;
207
208 case TYPE_CODE_MEMBERPTR:
209 if (options->format)
210 {
211 print_scalar_formatted (valaddr + embedded_offset, type,
212 options, 0, stream);
213 break;
214 }
215 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
216 break;
217
218 case TYPE_CODE_METHODPTR:
219 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
220 break;
221
222 case TYPE_CODE_PTR:
223 if (options->format && options->format != 's')
224 {
225 print_scalar_formatted (valaddr + embedded_offset, type,
226 options, 0, stream);
227 break;
228 }
229 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
230 {
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;
238 }
239 unresolved_elttype = TYPE_TARGET_TYPE (type);
240 elttype = check_typedef (unresolved_elttype);
241 {
242 addr = unpack_pointer (type, valaddr + embedded_offset);
243 print_unpacked_pointer:
244
245 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
246 {
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);
252 }
253
254 if (options->addressprint)
255 fputs_filtered (paddress (addr), stream);
256
257 /* For a pointer to a textual type, also print the string
258 pointed to, unless pointer is null. */
259
260 if (textual_element_type (unresolved_elttype, options->format)
261 && addr != 0)
262 {
263 i = val_print_string (unresolved_elttype, addr, -1, stream,
264 options);
265 }
266 else if (cp_is_vtbl_member (type))
267 {
268 /* print vtbl's nicely */
269 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
270
271 struct minimal_symbol *msymbol =
272 lookup_minimal_symbol_by_pc (vt_address);
273 if ((msymbol != NULL) &&
274 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
275 {
276 fputs_filtered (" <", stream);
277 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
278 fputs_filtered (">", stream);
279 }
280 if (vt_address && options->vtblprint)
281 {
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;
287
288 if (msymbol != NULL)
289 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
290 VAR_DOMAIN, &is_this_fld);
291
292 if (wsym)
293 {
294 wtype = SYMBOL_TYPE (wsym);
295 }
296 else
297 {
298 wtype = unresolved_elttype;
299 }
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)
304 {
305 fprintf_filtered (stream, "\n");
306 print_spaces_filtered (2 + 2 * recurse, stream);
307 }
308 }
309 }
310
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;
315 }
316 break;
317
318 case TYPE_CODE_REF:
319 elttype = check_typedef (TYPE_TARGET_TYPE (type));
320 if (options->addressprint)
321 {
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);
328 }
329 /* De-reference the reference. */
330 if (options->deref_ref)
331 {
332 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
333 {
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);
340 }
341 else
342 fputs_filtered ("???", stream);
343 }
344 break;
345
346 case TYPE_CODE_UNION:
347 if (recurse && !options->unionprint)
348 {
349 fprintf_filtered (stream, "{...}");
350 break;
351 }
352 /* Fall through. */
353 case TYPE_CODE_STRUCT:
354 /*FIXME: Abstract this away */
355 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
356 {
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);
365
366 print_function_pointer_address (addr, stream, options->addressprint);
367 }
368 else
369 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream,
370 recurse, options, NULL, 0);
371 break;
372
373 case TYPE_CODE_ENUM:
374 if (options->format)
375 {
376 print_scalar_formatted (valaddr + embedded_offset, type,
377 options, 0, stream);
378 break;
379 }
380 len = TYPE_NFIELDS (type);
381 val = unpack_long (type, valaddr + embedded_offset);
382 for (i = 0; i < len; i++)
383 {
384 QUIT;
385 if (val == TYPE_FIELD_BITPOS (type, i))
386 {
387 break;
388 }
389 }
390 if (i < len)
391 {
392 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
393 }
394 else
395 {
396 print_longest (stream, 'd', 0, val);
397 }
398 break;
399
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;
407
408 case TYPE_CODE_FUNC:
409 case TYPE_CODE_METHOD:
410 if (options->format)
411 {
412 print_scalar_formatted (valaddr + embedded_offset, type,
413 options, 0, stream);
414 break;
415 }
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;
424
425 case TYPE_CODE_BOOL:
426 if (options->format || options->output_format)
427 {
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);
433 }
434 else
435 {
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);
443 }
444 break;
445
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 */
455
456 case TYPE_CODE_INT:
457 if (options->format || options->output_format)
458 {
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);
464 }
465 else
466 {
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))
473 {
474 fputs_filtered (" ", stream);
475 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
476 unresolved_type, stream);
477 }
478 }
479 break;
480
481 case TYPE_CODE_CHAR:
482 if (options->format || options->output_format)
483 {
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);
489 }
490 else
491 {
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);
499 }
500 break;
501
502 case TYPE_CODE_FLT:
503 if (options->format)
504 {
505 print_scalar_formatted (valaddr + embedded_offset, type,
506 options, 0, stream);
507 }
508 else
509 {
510 print_floating (valaddr + embedded_offset, type, stream);
511 }
512 break;
513
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;
521
522 case TYPE_CODE_VOID:
523 fprintf_filtered (stream, "void");
524 break;
525
526 case TYPE_CODE_ERROR:
527 fprintf_filtered (stream, _("<error type>"));
528 break;
529
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;
536
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;
558
559 default:
560 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
561 }
562 gdb_flush (stream);
563 return (0);
564 }
565 \f
566 int
567 c_value_print (struct value *val, struct ui_file *stream,
568 const struct value_print_options *options)
569 {
570 struct type *type, *real_type, *val_type;
571 int full, top, using_enc;
572 struct value_print_options opts = *options;
573
574 opts.deref_ref = 1;
575
576 /* If it is a pointer, indicate what it points to.
577
578 Print type also if it is a reference.
579
580 C++: if it is a member pointer, we will take care
581 of that when we print it. */
582
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);
588
589 if (TYPE_CODE (type) == TYPE_CODE_PTR
590 || TYPE_CODE (type) == TYPE_CODE_REF)
591 {
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)))))
601 {
602 /* Print nothing */
603 }
604 else if (options->objectprint
605 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
606 {
607
608 if (TYPE_CODE(type) == TYPE_CODE_REF)
609 {
610 /* Copy value, change to pointer, so we don't get an
611 * error about a non-pointer type in value_rtti_target_type
612 */
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;
617 }
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)
622 {
623 /* RTTI entry found */
624 if (TYPE_CODE (type) == TYPE_CODE_PTR)
625 {
626 /* create a pointer type pointing to the real type */
627 type = lookup_pointer_type (real_type);
628 }
629 else
630 {
631 /* create a reference type referencing the real type */
632 type = lookup_reference_type (real_type);
633 }
634 /* JYG: Need to adjust pointer value. */
635 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
636 value_contents_writeable (val)[0] -= top;
637
638 /* Note: When we look up RTTI entries, we don't get any
639 information on const or volatile attributes */
640 }
641 type_print (type, "", stream, -1);
642 fprintf_filtered (stream, ") ");
643 val_type = type;
644 }
645 else
646 {
647 /* normal case */
648 fprintf_filtered (stream, "(");
649 type_print (value_type (val), "", stream, -1);
650 fprintf_filtered (stream, ") ");
651 }
652 }
653
654 if (!value_initialized (val))
655 fprintf_filtered (stream, " [uninitialized] ");
656
657 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
658 {
659 /* Attempt to determine real type of object */
660 real_type = value_rtti_type (val, &full, &top, &using_enc);
661 if (real_type)
662 {
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 */
675 }
676 else if (type != check_typedef (value_enclosing_type (val)))
677 {
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);
685 }
686 /* Otherwise, we end up at the return outside this "if" */
687 }
688
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);
693 }
This page took 0.04456 seconds and 5 git commands to generate.