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