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