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