Handle EM_L1OM.
[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, 2010
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 (struct gdbarch *gdbarch, CORE_ADDR address,
40 struct ui_file *stream, int addressprint)
41 {
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
43 &current_target);
44
45 /* If the function pointer is represented by a description, print the
46 address of the description. */
47 if (addressprint && func_addr != address)
48 {
49 fputs_filtered ("@", stream);
50 fputs_filtered (paddress (gdbarch, address), stream);
51 fputs_filtered (": ", stream);
52 }
53 print_address_demangle (gdbarch, func_addr, stream, demangle);
54 }
55
56
57 /* A helper for c_textual_element_type. This checks the name of the
58 typedef. This is bogus but it isn't apparent that the compiler
59 provides us the help we may need. */
60
61 static int
62 textual_name (const char *name)
63 {
64 return (!strcmp (name, "wchar_t")
65 || !strcmp (name, "char16_t")
66 || !strcmp (name, "char32_t"));
67 }
68
69 /* Apply a heuristic to decide whether an array of TYPE or a pointer
70 to TYPE should be printed as a textual string. Return non-zero if
71 it should, or zero if it should be treated as an array of integers
72 or pointer to integers. FORMAT is the current format letter,
73 or 0 if none.
74
75 We guess that "char" is a character. Explicitly signed and
76 unsigned character types are also characters. Integer data from
77 vector types is not. The user can override this by using the /s
78 format letter. */
79
80 int
81 c_textual_element_type (struct type *type, char format)
82 {
83 struct type *true_type, *iter_type;
84
85 if (format != 0 && format != 's')
86 return 0;
87
88 /* We also rely on this for its side effect of setting up all the
89 typedef pointers. */
90 true_type = check_typedef (type);
91
92 /* TYPE_CODE_CHAR is always textual. */
93 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
94 return 1;
95
96 /* Any other character-like types must be integral. */
97 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
98 return 0;
99
100 /* We peel typedefs one by one, looking for a match. */
101 iter_type = type;
102 while (iter_type)
103 {
104 /* Check the name of the type. */
105 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
106 return 1;
107
108 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
109 break;
110
111 /* Peel a single typedef. If the typedef doesn't have a target
112 type, we use check_typedef and hope the result is ok -- it
113 might be for C++, where wchar_t is a built-in type. */
114 if (TYPE_TARGET_TYPE (iter_type))
115 iter_type = TYPE_TARGET_TYPE (iter_type);
116 else
117 iter_type = check_typedef (iter_type);
118 }
119
120 if (format == 's')
121 {
122 /* Print this as a string if we can manage it. For now, no
123 wide character support. */
124 if (TYPE_CODE (true_type) == TYPE_CODE_INT
125 && TYPE_LENGTH (true_type) == 1)
126 return 1;
127 }
128 else
129 {
130 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
131 flag, then we treat it as text; otherwise, we assume it's
132 being used as data. */
133 if (TYPE_CODE (true_type) == TYPE_CODE_INT
134 && TYPE_LENGTH (true_type) == 1
135 && !TYPE_NOTTEXT (true_type))
136 return 1;
137 }
138
139 return 0;
140 }
141
142
143 /* Print data of type TYPE located at VALADDR (within GDB), which came from
144 the inferior at address ADDRESS, onto stdio stream STREAM according to
145 OPTIONS. The data at VALADDR is in target byte order.
146
147 If the data are a string pointer, returns the number of string characters
148 printed. */
149
150 int
151 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
152 CORE_ADDR address, struct ui_file *stream, int recurse,
153 const struct value_print_options *options)
154 {
155 struct gdbarch *gdbarch = get_type_arch (type);
156 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
157 unsigned int i = 0; /* Number of characters printed */
158 unsigned len;
159 struct type *elttype, *unresolved_elttype;
160 struct type *unresolved_type = type;
161 unsigned eltlen;
162 LONGEST val;
163 CORE_ADDR addr;
164
165 CHECK_TYPEDEF (type);
166 switch (TYPE_CODE (type))
167 {
168 case TYPE_CODE_ARRAY:
169 unresolved_elttype = TYPE_TARGET_TYPE (type);
170 elttype = check_typedef (unresolved_elttype);
171 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
172 {
173 eltlen = TYPE_LENGTH (elttype);
174 len = TYPE_LENGTH (type) / eltlen;
175 if (options->prettyprint_arrays)
176 {
177 print_spaces_filtered (2 + 2 * recurse, stream);
178 }
179
180 /* Print arrays of textual chars with a string syntax. */
181 if (c_textual_element_type (unresolved_elttype, options->format))
182 {
183 /* If requested, look for the first null char and only print
184 elements up to it. */
185 if (options->stop_print_at_null)
186 {
187 unsigned int temp_len;
188
189 for (temp_len = 0;
190 (temp_len < len
191 && temp_len < options->print_max
192 && extract_unsigned_integer (valaddr + embedded_offset
193 + temp_len * eltlen,
194 eltlen, byte_order) != 0);
195 ++temp_len)
196 ;
197 len = temp_len;
198 }
199
200 LA_PRINT_STRING (stream, unresolved_elttype,
201 valaddr + embedded_offset, len,
202 NULL, 0, options);
203 i = len;
204 }
205 else
206 {
207 fprintf_filtered (stream, "{");
208 /* If this is a virtual function table, print the 0th
209 entry specially, and the rest of the members normally. */
210 if (cp_is_vtbl_ptr_type (elttype))
211 {
212 i = 1;
213 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
214 }
215 else
216 {
217 i = 0;
218 }
219 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
220 recurse, options, i);
221 fprintf_filtered (stream, "}");
222 }
223 break;
224 }
225 /* Array of unspecified length: treat like pointer to first elt. */
226 addr = address;
227 goto print_unpacked_pointer;
228
229 case TYPE_CODE_MEMBERPTR:
230 if (options->format)
231 {
232 print_scalar_formatted (valaddr + embedded_offset, type,
233 options, 0, stream);
234 break;
235 }
236 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
237 break;
238
239 case TYPE_CODE_METHODPTR:
240 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
241 break;
242
243 case TYPE_CODE_PTR:
244 if (options->format && options->format != 's')
245 {
246 print_scalar_formatted (valaddr + embedded_offset, type,
247 options, 0, stream);
248 break;
249 }
250 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
251 {
252 /* Print the unmangled name if desired. */
253 /* Print vtable entry - we only get here if we ARE using
254 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
255 CORE_ADDR addr
256 = extract_typed_address (valaddr + embedded_offset, type);
257 print_function_pointer_address (gdbarch, addr, stream,
258 options->addressprint);
259 break;
260 }
261 unresolved_elttype = TYPE_TARGET_TYPE (type);
262 elttype = check_typedef (unresolved_elttype);
263 {
264 addr = unpack_pointer (type, valaddr + embedded_offset);
265 print_unpacked_pointer:
266
267 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
268 {
269 /* Try to print what function it points to. */
270 print_function_pointer_address (gdbarch, addr, stream,
271 options->addressprint);
272 /* Return value is irrelevant except for string pointers. */
273 return (0);
274 }
275
276 if (options->addressprint)
277 fputs_filtered (paddress (gdbarch, addr), stream);
278
279 /* For a pointer to a textual type, also print the string
280 pointed to, unless pointer is null. */
281
282 if (c_textual_element_type (unresolved_elttype, options->format)
283 && addr != 0)
284 {
285 i = val_print_string (unresolved_elttype, addr, -1, stream,
286 options);
287 }
288 else if (cp_is_vtbl_member (type))
289 {
290 /* print vtbl's nicely */
291 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
292
293 struct minimal_symbol *msymbol =
294 lookup_minimal_symbol_by_pc (vt_address);
295 if ((msymbol != NULL)
296 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
297 {
298 fputs_filtered (" <", stream);
299 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
300 fputs_filtered (">", stream);
301 }
302 if (vt_address && options->vtblprint)
303 {
304 struct value *vt_val;
305 struct symbol *wsym = (struct symbol *) NULL;
306 struct type *wtype;
307 struct block *block = (struct block *) NULL;
308 int is_this_fld;
309
310 if (msymbol != NULL)
311 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
312 VAR_DOMAIN, &is_this_fld);
313
314 if (wsym)
315 {
316 wtype = SYMBOL_TYPE (wsym);
317 }
318 else
319 {
320 wtype = unresolved_elttype;
321 }
322 vt_val = value_at (wtype, vt_address);
323 common_val_print (vt_val, stream, recurse + 1, options,
324 current_language);
325 if (options->pretty)
326 {
327 fprintf_filtered (stream, "\n");
328 print_spaces_filtered (2 + 2 * recurse, stream);
329 }
330 }
331 }
332
333 /* Return number of characters printed, including the terminating
334 '\0' if we reached the end. val_print_string takes care including
335 the terminating '\0' if necessary. */
336 return i;
337 }
338 break;
339
340 case TYPE_CODE_REF:
341 elttype = check_typedef (TYPE_TARGET_TYPE (type));
342 if (options->addressprint)
343 {
344 CORE_ADDR addr
345 = extract_typed_address (valaddr + embedded_offset, type);
346 fprintf_filtered (stream, "@");
347 fputs_filtered (paddress (gdbarch, addr), stream);
348 if (options->deref_ref)
349 fputs_filtered (": ", stream);
350 }
351 /* De-reference the reference. */
352 if (options->deref_ref)
353 {
354 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
355 {
356 struct value *deref_val =
357 value_at
358 (TYPE_TARGET_TYPE (type),
359 unpack_pointer (type, valaddr + embedded_offset));
360 common_val_print (deref_val, stream, recurse, options,
361 current_language);
362 }
363 else
364 fputs_filtered ("???", stream);
365 }
366 break;
367
368 case TYPE_CODE_UNION:
369 if (recurse && !options->unionprint)
370 {
371 fprintf_filtered (stream, "{...}");
372 break;
373 }
374 /* Fall through. */
375 case TYPE_CODE_STRUCT:
376 /*FIXME: Abstract this away */
377 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
378 {
379 /* Print the unmangled name if desired. */
380 /* Print vtable entry - we only get here if NOT using
381 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
382 int offset = (embedded_offset +
383 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
384 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
385 CORE_ADDR addr
386 = extract_typed_address (valaddr + offset, field_type);
387
388 print_function_pointer_address (gdbarch, addr, stream,
389 options->addressprint);
390 }
391 else
392 cp_print_value_fields_rtti (type, valaddr,
393 embedded_offset, address, stream,
394 recurse, options, NULL, 0);
395 break;
396
397 case TYPE_CODE_ENUM:
398 if (options->format)
399 {
400 print_scalar_formatted (valaddr + embedded_offset, type,
401 options, 0, stream);
402 break;
403 }
404 len = TYPE_NFIELDS (type);
405 val = unpack_long (type, valaddr + embedded_offset);
406 for (i = 0; i < len; i++)
407 {
408 QUIT;
409 if (val == TYPE_FIELD_BITPOS (type, i))
410 {
411 break;
412 }
413 }
414 if (i < len)
415 {
416 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
417 }
418 else
419 {
420 print_longest (stream, 'd', 0, val);
421 }
422 break;
423
424 case TYPE_CODE_FLAGS:
425 if (options->format)
426 print_scalar_formatted (valaddr + embedded_offset, type,
427 options, 0, stream);
428 else
429 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
430 break;
431
432 case TYPE_CODE_FUNC:
433 case TYPE_CODE_METHOD:
434 if (options->format)
435 {
436 print_scalar_formatted (valaddr + embedded_offset, type,
437 options, 0, stream);
438 break;
439 }
440 /* FIXME, we should consider, at least for ANSI C language, eliminating
441 the distinction made between FUNCs and POINTERs to FUNCs. */
442 fprintf_filtered (stream, "{");
443 type_print (type, "", stream, -1);
444 fprintf_filtered (stream, "} ");
445 /* Try to print what function it points to, and its address. */
446 print_address_demangle (gdbarch, address, stream, demangle);
447 break;
448
449 case TYPE_CODE_BOOL:
450 if (options->format || options->output_format)
451 {
452 struct value_print_options opts = *options;
453 opts.format = (options->format ? options->format
454 : options->output_format);
455 print_scalar_formatted (valaddr + embedded_offset, type,
456 &opts, 0, stream);
457 }
458 else
459 {
460 val = unpack_long (type, valaddr + embedded_offset);
461 if (val == 0)
462 fputs_filtered ("false", stream);
463 else if (val == 1)
464 fputs_filtered ("true", stream);
465 else
466 print_longest (stream, 'd', 0, val);
467 }
468 break;
469
470 case TYPE_CODE_RANGE:
471 /* FIXME: create_range_type does not set the unsigned bit in a
472 range type (I think it probably should copy it from the target
473 type), so we won't print values which are too large to
474 fit in a signed integer correctly. */
475 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
476 print with the target type, though, because the size of our type
477 and the target type might differ). */
478 /* FALLTHROUGH */
479
480 case TYPE_CODE_INT:
481 if (options->format || options->output_format)
482 {
483 struct value_print_options opts = *options;
484 opts.format = (options->format ? options->format
485 : options->output_format);
486 print_scalar_formatted (valaddr + embedded_offset, type,
487 &opts, 0, stream);
488 }
489 else
490 {
491 val_print_type_code_int (type, valaddr + embedded_offset, stream);
492 /* C and C++ has no single byte int type, char is used instead.
493 Since we don't know whether the value is really intended to
494 be used as an integer or a character, print the character
495 equivalent as well. */
496 if (c_textual_element_type (unresolved_type, options->format))
497 {
498 fputs_filtered (" ", stream);
499 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
500 unresolved_type, stream);
501 }
502 }
503 break;
504
505 case TYPE_CODE_CHAR:
506 if (options->format || options->output_format)
507 {
508 struct value_print_options opts = *options;
509 opts.format = (options->format ? options->format
510 : options->output_format);
511 print_scalar_formatted (valaddr + embedded_offset, type,
512 &opts, 0, stream);
513 }
514 else
515 {
516 val = unpack_long (type, valaddr + embedded_offset);
517 if (TYPE_UNSIGNED (type))
518 fprintf_filtered (stream, "%u", (unsigned int) val);
519 else
520 fprintf_filtered (stream, "%d", (int) val);
521 fputs_filtered (" ", stream);
522 LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream);
523 }
524 break;
525
526 case TYPE_CODE_FLT:
527 if (options->format)
528 {
529 print_scalar_formatted (valaddr + embedded_offset, type,
530 options, 0, stream);
531 }
532 else
533 {
534 print_floating (valaddr + embedded_offset, type, stream);
535 }
536 break;
537
538 case TYPE_CODE_DECFLOAT:
539 if (options->format)
540 print_scalar_formatted (valaddr + embedded_offset, type,
541 options, 0, stream);
542 else
543 print_decimal_floating (valaddr + embedded_offset, type, stream);
544 break;
545
546 case TYPE_CODE_VOID:
547 fprintf_filtered (stream, "void");
548 break;
549
550 case TYPE_CODE_ERROR:
551 fprintf_filtered (stream, _("<error type>"));
552 break;
553
554 case TYPE_CODE_UNDEF:
555 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
556 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
557 and no complete type for struct foo in that file. */
558 fprintf_filtered (stream, _("<incomplete type>"));
559 break;
560
561 case TYPE_CODE_COMPLEX:
562 if (options->format)
563 print_scalar_formatted (valaddr + embedded_offset,
564 TYPE_TARGET_TYPE (type),
565 options, 0, stream);
566 else
567 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
568 stream);
569 fprintf_filtered (stream, " + ");
570 if (options->format)
571 print_scalar_formatted (valaddr + embedded_offset
572 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
573 TYPE_TARGET_TYPE (type),
574 options, 0, stream);
575 else
576 print_floating (valaddr + embedded_offset
577 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
578 TYPE_TARGET_TYPE (type),
579 stream);
580 fprintf_filtered (stream, " * I");
581 break;
582
583 default:
584 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
585 }
586 gdb_flush (stream);
587 return (0);
588 }
589 \f
590 int
591 c_value_print (struct value *val, struct ui_file *stream,
592 const struct value_print_options *options)
593 {
594 struct type *type, *real_type, *val_type;
595 int full, top, using_enc;
596 struct value_print_options opts = *options;
597
598 opts.deref_ref = 1;
599
600 /* If it is a pointer, indicate what it points to.
601
602 Print type also if it is a reference.
603
604 C++: if it is a member pointer, we will take care
605 of that when we print it. */
606
607 /* Preserve the original type before stripping typedefs. We prefer
608 to pass down the original type when possible, but for local
609 checks it is better to look past the typedefs. */
610 val_type = value_type (val);
611 type = check_typedef (val_type);
612
613 if (TYPE_CODE (type) == TYPE_CODE_PTR
614 || TYPE_CODE (type) == TYPE_CODE_REF)
615 {
616 /* Hack: remove (char *) for char strings. Their
617 type is indicated by the quoted string anyway.
618 (Don't use c_textual_element_type here; quoted strings
619 are always exactly (char *), (wchar_t *), or the like. */
620 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
621 && TYPE_NAME (val_type) == NULL
622 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
623 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0
624 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
625 {
626 /* Print nothing */
627 }
628 else if (options->objectprint
629 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
630 {
631
632 if (TYPE_CODE(type) == TYPE_CODE_REF)
633 {
634 /* Copy value, change to pointer, so we don't get an
635 * error about a non-pointer type in value_rtti_target_type
636 */
637 struct value *temparg;
638 temparg=value_copy(val);
639 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
640 val=temparg;
641 }
642 /* Pointer to class, check real type of object */
643 fprintf_filtered (stream, "(");
644 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
645 if (real_type)
646 {
647 /* RTTI entry found */
648 if (TYPE_CODE (type) == TYPE_CODE_PTR)
649 {
650 /* create a pointer type pointing to the real type */
651 type = lookup_pointer_type (real_type);
652 }
653 else
654 {
655 /* create a reference type referencing the real type */
656 type = lookup_reference_type (real_type);
657 }
658 /* JYG: Need to adjust pointer value. */
659 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
660 value_contents_writeable (val)[0] -= top;
661
662 /* Note: When we look up RTTI entries, we don't get any
663 information on const or volatile attributes */
664 }
665 type_print (type, "", stream, -1);
666 fprintf_filtered (stream, ") ");
667 val_type = type;
668 }
669 else
670 {
671 /* normal case */
672 fprintf_filtered (stream, "(");
673 type_print (value_type (val), "", stream, -1);
674 fprintf_filtered (stream, ") ");
675 }
676 }
677
678 if (!value_initialized (val))
679 fprintf_filtered (stream, " [uninitialized] ");
680
681 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
682 {
683 /* Attempt to determine real type of object */
684 real_type = value_rtti_type (val, &full, &top, &using_enc);
685 if (real_type)
686 {
687 /* We have RTTI information, so use it */
688 val = value_full_object (val, real_type, full, top, using_enc);
689 fprintf_filtered (stream, "(%s%s) ",
690 TYPE_NAME (real_type),
691 full ? "" : _(" [incomplete object]"));
692 /* Print out object: enclosing type is same as real_type if full */
693 return val_print (value_enclosing_type (val),
694 value_contents_all (val), 0,
695 value_address (val), stream, 0,
696 &opts, current_language);
697 /* Note: When we look up RTTI entries, we don't get any information on
698 const or volatile attributes */
699 }
700 else if (type != check_typedef (value_enclosing_type (val)))
701 {
702 /* No RTTI information, so let's do our best */
703 fprintf_filtered (stream, "(%s ?) ",
704 TYPE_NAME (value_enclosing_type (val)));
705 return val_print (value_enclosing_type (val),
706 value_contents_all (val), 0,
707 value_address (val), stream, 0,
708 &opts, current_language);
709 }
710 /* Otherwise, we end up at the return outside this "if" */
711 }
712
713 return val_print (val_type, value_contents_all (val),
714 value_embedded_offset (val),
715 value_address (val),
716 stream, 0, &opts, current_language);
717 }
This page took 0.043719 seconds and 4 git commands to generate.