* configure.tgt (*-*-freebsd*): Set gdb_osabi to
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2
3 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation,
5 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "value.h"
30 #include "valprint.h"
31 #include "language.h"
32 #include "c-lang.h"
33 #include "cp-abi.h"
34 #include "target.h"
35 \f
36
37 /* Print function pointer with inferior address ADDRESS onto stdio
38 stream STREAM. */
39
40 static void
41 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
42 {
43 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
44 address,
45 &current_target);
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);
52 deprecated_print_address_numeric (address, 1, stream);
53 fputs_filtered (": ", stream);
54 }
55 print_address_demangle (func_addr, stream, demangle);
56 }
57
58
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
72 int
73 c_val_print (struct type *type, const bfd_byte *valaddr, int embedded_offset,
74 CORE_ADDR address, struct ui_file *stream, int format,
75 int deref_ref, int recurse, enum val_prettyprint pretty)
76 {
77 unsigned int i = 0; /* Number of characters printed */
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
105 elements up to it. */
106 if (stop_print_at_null)
107 {
108 unsigned int temp_len;
109
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 }
117
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
125 entry specially, and the rest of the members normally. */
126 if (cp_is_vtbl_ptr_type (elttype))
127 {
128 i = 1;
129 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
130 }
131 else
132 {
133 i = 0;
134 }
135 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
136 format, deref_ref, recurse, pretty, i);
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 }
151 if (vtblprint && cp_is_vtbl_ptr_type (type))
152 {
153 /* Print the unmangled name if desired. */
154 /* Print vtable entry - we only get here if we ARE using
155 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
156 CORE_ADDR addr
157 = extract_typed_address (valaddr + embedded_offset, type);
158 print_function_pointer_address (addr, stream);
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:
176
177 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
178 {
179 /* Try to print what function it points to. */
180 print_function_pointer_address (addr, stream);
181 /* Return value is irrelevant except for string pointers. */
182 return (0);
183 }
184
185 if (addressprint && format != 's')
186 {
187 deprecated_print_address_numeric (addr, 1, stream);
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 }
201 else if (cp_is_vtbl_member (type))
202 {
203 /* print vtbl's nicely */
204 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
205
206 struct minimal_symbol *msymbol =
207 lookup_minimal_symbol_by_pc (vt_address);
208 if ((msymbol != NULL) &&
209 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
210 {
211 fputs_filtered (" <", stream);
212 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
213 fputs_filtered (">", stream);
214 }
215 if (vt_address && vtblprint)
216 {
217 struct value *vt_val;
218 struct symbol *wsym = (struct symbol *) NULL;
219 struct type *wtype;
220 struct block *block = (struct block *) NULL;
221 int is_this_fld;
222
223 if (msymbol != NULL)
224 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
225 VAR_DOMAIN, &is_this_fld, NULL);
226
227 if (wsym)
228 {
229 wtype = SYMBOL_TYPE (wsym);
230 }
231 else
232 {
233 wtype = TYPE_TARGET_TYPE (type);
234 }
235 vt_val = value_at (wtype, vt_address);
236 common_val_print (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 }
243 }
244 }
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)
260 {
261 cp_print_class_member (valaddr + embedded_offset,
262 TYPE_DOMAIN_TYPE (elttype),
263 stream, "");
264 break;
265 }
266 if (addressprint)
267 {
268 CORE_ADDR addr
269 = extract_typed_address (valaddr + embedded_offset, type);
270 fprintf_filtered (stream, "@");
271 deprecated_print_address_numeric (addr, 1, stream);
272 if (deref_ref)
273 fputs_filtered (": ", stream);
274 }
275 /* De-reference the reference. */
276 if (deref_ref)
277 {
278 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
279 {
280 struct value *deref_val =
281 value_at
282 (TYPE_TARGET_TYPE (type),
283 unpack_pointer (lookup_pointer_type (builtin_type_void),
284 valaddr + embedded_offset));
285 common_val_print (deref_val, stream, format, deref_ref,
286 recurse, pretty);
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:
301 /*FIXME: Abstract this away */
302 if (vtblprint && cp_is_vtbl_ptr_type (type))
303 {
304 /* Print the unmangled name if desired. */
305 /* Print vtable entry - we only get here if NOT using
306 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
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
313 print_function_pointer_address (addr, stream);
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
346 case TYPE_CODE_FUNC:
347 if (format)
348 {
349 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
350 break;
351 }
352 /* FIXME, we should consider, at least for ANSI C language, eliminating
353 the distinction made between FUNCs and POINTERs to FUNCs. */
354 fprintf_filtered (stream, "{");
355 type_print (type, "", stream, -1);
356 fprintf_filtered (stream, "} ");
357 /* Try to print what function it points to, and its address. */
358 print_address_demangle (address, stream, demangle);
359 break;
360
361 case TYPE_CODE_BOOL:
362 format = format ? format : output_format;
363 if (format)
364 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
365 else
366 {
367 val = unpack_long (type, valaddr + embedded_offset);
368 if (val == 0)
369 fputs_filtered ("false", stream);
370 else if (val == 1)
371 fputs_filtered ("true", stream);
372 else
373 print_longest (stream, 'd', 0, val);
374 }
375 break;
376
377 case TYPE_CODE_RANGE:
378 /* FIXME: create_range_type does not set the unsigned bit in a
379 range type (I think it probably should copy it from the target
380 type), so we won't print values which are too large to
381 fit in a signed integer correctly. */
382 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
383 print with the target type, though, because the size of our type
384 and the target type might differ). */
385 /* FALLTHROUGH */
386
387 case TYPE_CODE_INT:
388 format = format ? format : output_format;
389 if (format)
390 {
391 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
392 }
393 else
394 {
395 val_print_type_code_int (type, valaddr + embedded_offset, stream);
396 /* C and C++ has no single byte int type, char is used instead.
397 Since we don't know whether the value is really intended to
398 be used as an integer or a character, print the character
399 equivalent as well. */
400 if (TYPE_LENGTH (type) == 1)
401 {
402 fputs_filtered (" ", stream);
403 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
404 stream);
405 }
406 }
407 break;
408
409 case TYPE_CODE_CHAR:
410 format = format ? format : output_format;
411 if (format)
412 {
413 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
414 }
415 else
416 {
417 val = unpack_long (type, valaddr + embedded_offset);
418 if (TYPE_UNSIGNED (type))
419 fprintf_filtered (stream, "%u", (unsigned int) val);
420 else
421 fprintf_filtered (stream, "%d", (int) val);
422 fputs_filtered (" ", stream);
423 LA_PRINT_CHAR ((unsigned char) val, stream);
424 }
425 break;
426
427 case TYPE_CODE_FLT:
428 if (format)
429 {
430 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
431 }
432 else
433 {
434 print_floating (valaddr + embedded_offset, type, stream);
435 }
436 break;
437
438 case TYPE_CODE_METHOD:
439 {
440 struct value *v = value_at (type, address);
441 cp_print_class_method (value_contents (value_addr (v)),
442 lookup_pointer_type (type), stream);
443 break;
444 }
445
446 case TYPE_CODE_VOID:
447 fprintf_filtered (stream, "void");
448 break;
449
450 case TYPE_CODE_ERROR:
451 fprintf_filtered (stream, _("<error type>"));
452 break;
453
454 case TYPE_CODE_UNDEF:
455 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
456 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
457 and no complete type for struct foo in that file. */
458 fprintf_filtered (stream, _("<incomplete type>"));
459 break;
460
461 case TYPE_CODE_COMPLEX:
462 if (format)
463 print_scalar_formatted (valaddr + embedded_offset,
464 TYPE_TARGET_TYPE (type),
465 format, 0, stream);
466 else
467 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
468 stream);
469 fprintf_filtered (stream, " + ");
470 if (format)
471 print_scalar_formatted (valaddr + embedded_offset
472 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
473 TYPE_TARGET_TYPE (type),
474 format, 0, stream);
475 else
476 print_floating (valaddr + embedded_offset
477 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
478 TYPE_TARGET_TYPE (type),
479 stream);
480 fprintf_filtered (stream, " * I");
481 break;
482
483 default:
484 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
485 }
486 gdb_flush (stream);
487 return (0);
488 }
489 \f
490 int
491 c_value_print (struct value *val, struct ui_file *stream, int format,
492 enum val_prettyprint pretty)
493 {
494 struct type *type, *real_type;
495 int full, top, using_enc;
496
497 /* If it is a pointer, indicate what it points to.
498
499 Print type also if it is a reference.
500
501 C++: if it is a member pointer, we will take care
502 of that when we print it. */
503
504 type = check_typedef (value_type (val));
505
506 if (TYPE_CODE (type) == TYPE_CODE_PTR
507 || TYPE_CODE (type) == TYPE_CODE_REF)
508 {
509 /* Hack: remove (char *) for char strings. Their
510 type is indicated by the quoted string anyway. */
511 if (TYPE_CODE (type) == TYPE_CODE_PTR
512 && TYPE_NAME (type) == NULL
513 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
514 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
515 {
516 /* Print nothing */
517 }
518 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
519 {
520
521 if (TYPE_CODE(type) == TYPE_CODE_REF)
522 {
523 /* Copy value, change to pointer, so we don't get an
524 * error about a non-pointer type in value_rtti_target_type
525 */
526 struct value *temparg;
527 temparg=value_copy(val);
528 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
529 val=temparg;
530 }
531 /* Pointer to class, check real type of object */
532 fprintf_filtered (stream, "(");
533 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
534 if (real_type)
535 {
536 /* RTTI entry found */
537 if (TYPE_CODE (type) == TYPE_CODE_PTR)
538 {
539 /* create a pointer type pointing to the real type */
540 type = lookup_pointer_type (real_type);
541 }
542 else
543 {
544 /* create a reference type referencing the real type */
545 type = lookup_reference_type (real_type);
546 }
547 /* JYG: Need to adjust pointer value. */
548 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
549 value_contents_writeable (val)[0] -= top;
550
551 /* Note: When we look up RTTI entries, we don't get any
552 information on const or volatile attributes */
553 }
554 type_print (type, "", stream, -1);
555 fprintf_filtered (stream, ") ");
556 }
557 else
558 {
559 /* normal case */
560 fprintf_filtered (stream, "(");
561 type_print (value_type (val), "", stream, -1);
562 fprintf_filtered (stream, ") ");
563 }
564 }
565
566 if (objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
567 {
568 /* Attempt to determine real type of object */
569 real_type = value_rtti_type (val, &full, &top, &using_enc);
570 if (real_type)
571 {
572 /* We have RTTI information, so use it */
573 val = value_full_object (val, real_type, full, top, using_enc);
574 fprintf_filtered (stream, "(%s%s) ",
575 TYPE_NAME (real_type),
576 full ? "" : _(" [incomplete object]"));
577 /* Print out object: enclosing type is same as real_type if full */
578 return val_print (value_enclosing_type (val),
579 value_contents_all (val), 0,
580 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
581 /* Note: When we look up RTTI entries, we don't get any information on
582 const or volatile attributes */
583 }
584 else if (type != check_typedef (value_enclosing_type (val)))
585 {
586 /* No RTTI information, so let's do our best */
587 fprintf_filtered (stream, "(%s ?) ",
588 TYPE_NAME (value_enclosing_type (val)));
589 return val_print (value_enclosing_type (val),
590 value_contents_all (val), 0,
591 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
592 }
593 /* Otherwise, we end up at the return outside this "if" */
594 }
595
596 return val_print (type, value_contents_all (val),
597 value_embedded_offset (val),
598 VALUE_ADDRESS (val) + value_offset (val),
599 stream, format, 1, 0, pretty);
600 }
This page took 0.046332 seconds and 4 git commands to generate.