2004-11-09 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
CommitLineData
c906108c 1/* Support for printing C values for GDB, the GNU debugger.
1bac305b
AC
2
3 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
309367d4 24#include "gdb_string.h"
c906108c
SS
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "value.h"
c906108c
SS
29#include "valprint.h"
30#include "language.h"
31#include "c-lang.h"
015a42b4 32#include "cp-abi.h"
e2d0e7eb 33#include "target.h"
c906108c 34\f
c5aa993b 35
6e778545
PS
36/* Print function pointer with inferior address ADDRESS onto stdio
37 stream STREAM. */
38
39static void
40print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
41{
e2d0e7eb
AC
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
43 address,
44 &current_target);
6e778545
PS
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 print_address_numeric (address, 1, stream);
52 fputs_filtered (": ", stream);
53 }
54 print_address_demangle (func_addr, stream, demangle);
55}
56
57
c906108c
SS
58/* Print data of type TYPE located at VALADDR (within GDB), which came from
59 the inferior at address ADDRESS, onto stdio stream STREAM according to
60 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
61 target byte order.
62
63 If the data are a string pointer, returns the number of string characters
64 printed.
65
66 If DEREF_REF is nonzero, then dereference references, otherwise just print
67 them like pointers.
68
69 The PRETTY parameter controls prettyprinting. */
70
71int
fba45db2
KB
72c_val_print (struct type *type, char *valaddr, int embedded_offset,
73 CORE_ADDR address, struct ui_file *stream, int format,
74 int deref_ref, int recurse, enum val_prettyprint pretty)
c906108c 75{
52f0bd74 76 unsigned int i = 0; /* Number of characters printed */
c906108c
SS
77 unsigned len;
78 struct type *elttype;
79 unsigned eltlen;
80 LONGEST val;
81 CORE_ADDR addr;
82
83 CHECK_TYPEDEF (type);
84 switch (TYPE_CODE (type))
85 {
86 case TYPE_CODE_ARRAY:
87 elttype = check_typedef (TYPE_TARGET_TYPE (type));
88 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
89 {
90 eltlen = TYPE_LENGTH (elttype);
91 len = TYPE_LENGTH (type) / eltlen;
92 if (prettyprint_arrays)
93 {
94 print_spaces_filtered (2 + 2 * recurse, stream);
95 }
96 /* For an array of chars, print with string syntax. */
97 if (eltlen == 1 &&
98 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
99 || ((current_language->la_language == language_m2)
100 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
101 && (format == 0 || format == 's'))
102 {
103 /* If requested, look for the first null char and only print
c5aa993b 104 elements up to it. */
c906108c
SS
105 if (stop_print_at_null)
106 {
745b8ca0 107 unsigned int temp_len;
c5aa993b 108
c906108c
SS
109 /* Look for a NULL char. */
110 for (temp_len = 0;
111 (valaddr + embedded_offset)[temp_len]
112 && temp_len < len && temp_len < print_max;
113 temp_len++);
114 len = temp_len;
115 }
c5aa993b 116
c906108c
SS
117 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
118 i = len;
119 }
120 else
121 {
122 fprintf_filtered (stream, "{");
123 /* If this is a virtual function table, print the 0th
c5aa993b 124 entry specially, and the rest of the members normally. */
c906108c
SS
125 if (cp_is_vtbl_ptr_type (elttype))
126 {
127 i = 1;
128 fprintf_filtered (stream, "%d vtable entries", len - 1);
129 }
130 else
131 {
132 i = 0;
133 }
134 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
c5aa993b 135 format, deref_ref, recurse, pretty, i);
c906108c
SS
136 fprintf_filtered (stream, "}");
137 }
138 break;
139 }
140 /* Array of unspecified length: treat like pointer to first elt. */
141 addr = address;
142 goto print_unpacked_pointer;
143
144 case TYPE_CODE_PTR:
145 if (format && format != 's')
146 {
147 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
148 break;
149 }
c5aa993b 150 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 151 {
c5aa993b 152 /* Print the unmangled name if desired. */
c906108c
SS
153 /* Print vtable entry - we only get here if we ARE using
154 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
4478b372
JB
155 CORE_ADDR addr
156 = extract_typed_address (valaddr + embedded_offset, type);
6e778545 157 print_function_pointer_address (addr, stream);
c906108c
SS
158 break;
159 }
160 elttype = check_typedef (TYPE_TARGET_TYPE (type));
161 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
162 {
163 cp_print_class_method (valaddr + embedded_offset, type, stream);
164 }
165 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
166 {
167 cp_print_class_member (valaddr + embedded_offset,
168 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
169 stream, "&");
170 }
171 else
172 {
173 addr = unpack_pointer (type, valaddr + embedded_offset);
174 print_unpacked_pointer:
c906108c
SS
175
176 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
177 {
178 /* Try to print what function it points to. */
6e778545 179 print_function_pointer_address (addr, stream);
c906108c
SS
180 /* Return value is irrelevant except for string pointers. */
181 return (0);
182 }
183
184 if (addressprint && format != 's')
185 {
186 print_address_numeric (addr, 1, stream);
187 }
188
189 /* For a pointer to char or unsigned char, also print the string
190 pointed to, unless pointer is null. */
191 /* FIXME: need to handle wchar_t here... */
192
193 if (TYPE_LENGTH (elttype) == 1
194 && TYPE_CODE (elttype) == TYPE_CODE_INT
195 && (format == 0 || format == 's')
196 && addr != 0)
197 {
198 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
199 }
c5aa993b
JM
200 else if (cp_is_vtbl_member (type))
201 {
c906108c
SS
202 /* print vtbl's nicely */
203 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
204
205 struct minimal_symbol *msymbol =
c5aa993b 206 lookup_minimal_symbol_by_pc (vt_address);
c906108c
SS
207 if ((msymbol != NULL) &&
208 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
209 {
210 fputs_filtered (" <", stream);
de5ad195 211 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
c906108c
SS
212 fputs_filtered (">", stream);
213 }
214 if (vt_address && vtblprint)
c5aa993b 215 {
6943961c 216 struct value *vt_val;
c5aa993b
JM
217 struct symbol *wsym = (struct symbol *) NULL;
218 struct type *wtype;
c5aa993b 219 struct block *block = (struct block *) NULL;
c906108c
SS
220 int is_this_fld;
221
222 if (msymbol != NULL)
22abf04a 223 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
cdef89d0 224 VAR_DOMAIN, &is_this_fld, NULL);
c5aa993b 225
c906108c
SS
226 if (wsym)
227 {
c5aa993b 228 wtype = SYMBOL_TYPE (wsym);
c906108c
SS
229 }
230 else
231 {
c5aa993b 232 wtype = TYPE_TARGET_TYPE (type);
c906108c 233 }
00a4c844 234 vt_val = value_at (wtype, vt_address);
c906108c
SS
235 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
236 VALUE_ADDRESS (vt_val), stream, format,
237 deref_ref, recurse + 1, pretty);
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:
254 error ("not implemented: member type in c_val_print");
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, "@");
4478b372 271 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));
c906108c 285 val_print (VALUE_TYPE (deref_val),
c5aa993b
JM
286 VALUE_CONTENTS (deref_val),
287 0,
288 VALUE_ADDRESS (deref_val),
289 stream,
290 format,
291 deref_ref,
292 recurse,
293 pretty);
c906108c
SS
294 }
295 else
296 fputs_filtered ("???", stream);
297 }
298 break;
299
300 case TYPE_CODE_UNION:
301 if (recurse && !unionprint)
302 {
303 fprintf_filtered (stream, "{...}");
304 break;
305 }
306 /* Fall through. */
307 case TYPE_CODE_STRUCT:
015a42b4 308 /*FIXME: Abstract this away */
c5aa993b 309 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 310 {
c5aa993b 311 /* Print the unmangled name if desired. */
c906108c
SS
312 /* Print vtable entry - we only get here if NOT using
313 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
4478b372
JB
314 int offset = (embedded_offset +
315 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
316 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
317 CORE_ADDR addr
318 = extract_typed_address (valaddr + offset, field_type);
319
6e778545 320 print_function_pointer_address (addr, stream);
c906108c
SS
321 }
322 else
323 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
324 recurse, pretty, NULL, 0);
325 break;
326
327 case TYPE_CODE_ENUM:
328 if (format)
329 {
330 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
331 break;
332 }
333 len = TYPE_NFIELDS (type);
334 val = unpack_long (type, valaddr + embedded_offset);
335 for (i = 0; i < len; i++)
336 {
337 QUIT;
338 if (val == TYPE_FIELD_BITPOS (type, i))
339 {
340 break;
341 }
342 }
343 if (i < len)
344 {
345 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
346 }
347 else
348 {
349 print_longest (stream, 'd', 0, val);
350 }
351 break;
352
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);
acf5ed49
DJ
448 cp_print_class_method (VALUE_CONTENTS (value_addr (v)),
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:
458 fprintf_filtered (stream, "<error type>");
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. */
c906108c
SS
465 fprintf_filtered (stream, "<incomplete type>");
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
SS
490 default:
491 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
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
SS
500{
501 struct type *type = VALUE_TYPE (val);
c5aa993b 502 struct type *real_type;
c906108c 503 int full, top, using_enc;
c5aa993b 504
c906108c
SS
505 /* If it is a pointer, indicate what it points to.
506
507 Print type also if it is a reference.
508
509 C++: if it is a member pointer, we will take care
510 of that when we print it. */
511 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
512 TYPE_CODE (type) == TYPE_CODE_REF)
513 {
514 /* Hack: remove (char *) for char strings. Their
c5aa993b 515 type is indicated by the quoted string anyway. */
c906108c
SS
516 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
517 TYPE_NAME (type) == NULL &&
518 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
bde58177 519 strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
c906108c
SS
520 {
521 /* Print nothing */
522 }
523 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
c5aa993b 524 {
070ad9f0
DB
525
526 if (TYPE_CODE(type) == TYPE_CODE_REF)
527 {
528 /* Copy value, change to pointer, so we don't get an
529 * error about a non-pointer type in value_rtti_target_type
530 */
6943961c 531 struct value *temparg;
070ad9f0
DB
532 temparg=value_copy(val);
533 VALUE_TYPE (temparg) = lookup_pointer_type(TYPE_TARGET_TYPE(type));
534 val=temparg;
535 }
c5aa993b 536 /* Pointer to class, check real type of object */
c906108c 537 fprintf_filtered (stream, "(");
c4093a6a
JM
538 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
539 if (real_type)
c5aa993b
JM
540 {
541 /* RTTI entry found */
c4093a6a
JM
542 if (TYPE_CODE (type) == TYPE_CODE_PTR)
543 {
544 /* create a pointer type pointing to the real type */
545 type = lookup_pointer_type (real_type);
546 }
547 else
548 {
549 /* create a reference type referencing the real type */
550 type = lookup_reference_type (real_type);
551 }
070ad9f0
DB
552 /* JYG: Need to adjust pointer value. */
553 val->aligner.contents[0] -= top;
554
c4093a6a
JM
555 /* Note: When we look up RTTI entries, we don't get any
556 information on const or volatile attributes */
557 }
558 type_print (type, "", stream, -1);
c906108c 559 fprintf_filtered (stream, ") ");
c5aa993b 560 }
c906108c
SS
561 else
562 {
c5aa993b 563 /* normal case */
c906108c
SS
564 fprintf_filtered (stream, "(");
565 type_print (type, "", stream, -1);
566 fprintf_filtered (stream, ") ");
567 }
568 }
569 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
570 {
571 /* Attempt to determine real type of object */
572 real_type = value_rtti_type (val, &full, &top, &using_enc);
c5aa993b
JM
573 if (real_type)
574 {
575 /* We have RTTI information, so use it */
576 val = value_full_object (val, real_type, full, top, using_enc);
577 fprintf_filtered (stream, "(%s%s) ",
578 TYPE_NAME (real_type),
579 full ? "" : " [incomplete object]");
580 /* Print out object: enclosing type is same as real_type if full */
581 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
582 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
c4093a6a
JM
583 /* Note: When we look up RTTI entries, we don't get any information on
584 const or volatile attributes */
c5aa993b 585 }
c906108c 586 else if (type != VALUE_ENCLOSING_TYPE (val))
c5aa993b
JM
587 {
588 /* No RTTI information, so let's do our best */
589 fprintf_filtered (stream, "(%s ?) ",
590 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
591 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
592 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
593 }
c906108c
SS
594 /* Otherwise, we end up at the return outside this "if" */
595 }
c5aa993b 596
9e14d721
DJ
597 return val_print (type, VALUE_CONTENTS_ALL (val),
598 VALUE_EMBEDDED_OFFSET (val),
599 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
c906108c
SS
600 stream, format, 1, 0, pretty);
601}
This page took 0.444584 seconds and 4 git commands to generate.