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