MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / c-valprint.c
blobfd42ae0b908d6fdea2793da908fa2c86a3b5e10b
1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "valprint.h"
30 #include "language.h"
31 #include "c-lang.h"
32 #include "cp-abi.h"
33 #include "target.h"
36 /* Print function pointer with inferior address ADDRESS onto stdio
37 stream STREAM. */
39 static void
40 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
43 address,
44 &current_target);
46 /* If the function pointer is represented by a description, print the
47 address of the description. */
48 if (addressprint && func_addr != address)
50 fputs_filtered ("@", stream);
51 print_address_numeric (address, 1, stream);
52 fputs_filtered (": ", stream);
54 print_address_demangle (func_addr, stream, demangle);
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.
63 If the data are a string pointer, returns the number of string characters
64 printed.
66 If DEREF_REF is nonzero, then dereference references, otherwise just print
67 them like pointers.
69 The PRETTY parameter controls prettyprinting. */
71 int
72 c_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)
76 unsigned int i = 0; /* Number of characters printed */
77 unsigned len;
78 struct type *elttype;
79 unsigned eltlen;
80 LONGEST val;
81 CORE_ADDR addr;
83 CHECK_TYPEDEF (type);
84 switch (TYPE_CODE (type))
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)
90 eltlen = TYPE_LENGTH (elttype);
91 len = TYPE_LENGTH (type) / eltlen;
92 if (prettyprint_arrays)
94 print_spaces_filtered (2 + 2 * recurse, stream);
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'))
103 /* If requested, look for the first null char and only print
104 elements up to it. */
105 if (stop_print_at_null)
107 unsigned int temp_len;
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;
117 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
118 i = len;
120 else
122 fprintf_filtered (stream, "{");
123 /* If this is a virtual function table, print the 0th
124 entry specially, and the rest of the members normally. */
125 if (cp_is_vtbl_ptr_type (elttype))
127 i = 1;
128 fprintf_filtered (stream, "%d vtable entries", len - 1);
130 else
132 i = 0;
134 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
135 format, deref_ref, recurse, pretty, i);
136 fprintf_filtered (stream, "}");
138 break;
140 /* Array of unspecified length: treat like pointer to first elt. */
141 addr = address;
142 goto print_unpacked_pointer;
144 case TYPE_CODE_PTR:
145 if (format && format != 's')
147 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
148 break;
150 if (vtblprint && cp_is_vtbl_ptr_type (type))
152 /* Print the unmangled name if desired. */
153 /* Print vtable entry - we only get here if we ARE using
154 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
155 CORE_ADDR addr
156 = extract_typed_address (valaddr + embedded_offset, type);
157 print_function_pointer_address (addr, stream);
158 break;
160 elttype = check_typedef (TYPE_TARGET_TYPE (type));
161 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
163 cp_print_class_method (valaddr + embedded_offset, type, stream);
165 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
167 cp_print_class_member (valaddr + embedded_offset,
168 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
169 stream, "&");
171 else
173 addr = unpack_pointer (type, valaddr + embedded_offset);
174 print_unpacked_pointer:
176 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
178 /* Try to print what function it points to. */
179 print_function_pointer_address (addr, stream);
180 /* Return value is irrelevant except for string pointers. */
181 return (0);
184 if (addressprint && format != 's')
186 print_address_numeric (addr, 1, stream);
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... */
193 if (TYPE_LENGTH (elttype) == 1
194 && TYPE_CODE (elttype) == TYPE_CODE_INT
195 && (format == 0 || format == 's')
196 && addr != 0)
198 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
200 else if (cp_is_vtbl_member (type))
202 /* print vtbl's nicely */
203 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
205 struct minimal_symbol *msymbol =
206 lookup_minimal_symbol_by_pc (vt_address);
207 if ((msymbol != NULL) &&
208 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
210 fputs_filtered (" <", stream);
211 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
212 fputs_filtered (">", stream);
214 if (vt_address && vtblprint)
216 struct value *vt_val;
217 struct symbol *wsym = (struct symbol *) NULL;
218 struct type *wtype;
219 struct block *block = (struct block *) NULL;
220 int is_this_fld;
222 if (msymbol != NULL)
223 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
224 VAR_DOMAIN, &is_this_fld, NULL);
226 if (wsym)
228 wtype = SYMBOL_TYPE (wsym);
230 else
232 wtype = TYPE_TARGET_TYPE (type);
234 vt_val = value_at (wtype, vt_address, NULL);
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)
240 fprintf_filtered (stream, "\n");
241 print_spaces_filtered (2 + 2 * recurse, stream);
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;
251 break;
253 case TYPE_CODE_MEMBER:
254 error ("not implemented: member type in c_val_print");
255 break;
257 case TYPE_CODE_REF:
258 elttype = check_typedef (TYPE_TARGET_TYPE (type));
259 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
261 cp_print_class_member (valaddr + embedded_offset,
262 TYPE_DOMAIN_TYPE (elttype),
263 stream, "");
264 break;
266 if (addressprint)
268 CORE_ADDR addr
269 = extract_typed_address (valaddr + embedded_offset, type);
270 fprintf_filtered (stream, "@");
271 print_address_numeric (addr, 1, stream);
272 if (deref_ref)
273 fputs_filtered (": ", stream);
275 /* De-reference the reference. */
276 if (deref_ref)
278 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
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 NULL);
286 val_print (VALUE_TYPE (deref_val),
287 VALUE_CONTENTS (deref_val),
289 VALUE_ADDRESS (deref_val),
290 stream,
291 format,
292 deref_ref,
293 recurse,
294 pretty);
296 else
297 fputs_filtered ("???", stream);
299 break;
301 case TYPE_CODE_UNION:
302 if (recurse && !unionprint)
304 fprintf_filtered (stream, "{...}");
305 break;
307 /* Fall through. */
308 case TYPE_CODE_STRUCT:
309 /*FIXME: Abstract this away */
310 if (vtblprint && cp_is_vtbl_ptr_type (type))
312 /* Print the unmangled name if desired. */
313 /* Print vtable entry - we only get here if NOT using
314 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
315 int offset = (embedded_offset +
316 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
317 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
318 CORE_ADDR addr
319 = extract_typed_address (valaddr + offset, field_type);
321 print_function_pointer_address (addr, stream);
323 else
324 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
325 recurse, pretty, NULL, 0);
326 break;
328 case TYPE_CODE_ENUM:
329 if (format)
331 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
332 break;
334 len = TYPE_NFIELDS (type);
335 val = unpack_long (type, valaddr + embedded_offset);
336 for (i = 0; i < len; i++)
338 QUIT;
339 if (val == TYPE_FIELD_BITPOS (type, i))
341 break;
344 if (i < len)
346 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
348 else
350 print_longest (stream, 'd', 0, val);
352 break;
354 case TYPE_CODE_FUNC:
355 if (format)
357 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
358 break;
360 /* FIXME, we should consider, at least for ANSI C language, eliminating
361 the distinction made between FUNCs and POINTERs to FUNCs. */
362 fprintf_filtered (stream, "{");
363 type_print (type, "", stream, -1);
364 fprintf_filtered (stream, "} ");
365 /* Try to print what function it points to, and its address. */
366 print_address_demangle (address, stream, demangle);
367 break;
369 case TYPE_CODE_BOOL:
370 format = format ? format : output_format;
371 if (format)
372 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
373 else
375 val = unpack_long (type, valaddr + embedded_offset);
376 if (val == 0)
377 fputs_filtered ("false", stream);
378 else if (val == 1)
379 fputs_filtered ("true", stream);
380 else
381 print_longest (stream, 'd', 0, val);
383 break;
385 case TYPE_CODE_RANGE:
386 /* FIXME: create_range_type does not set the unsigned bit in a
387 range type (I think it probably should copy it from the target
388 type), so we won't print values which are too large to
389 fit in a signed integer correctly. */
390 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
391 print with the target type, though, because the size of our type
392 and the target type might differ). */
393 /* FALLTHROUGH */
395 case TYPE_CODE_INT:
396 format = format ? format : output_format;
397 if (format)
399 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
401 else
403 val_print_type_code_int (type, valaddr + embedded_offset, stream);
404 /* C and C++ has no single byte int type, char is used instead.
405 Since we don't know whether the value is really intended to
406 be used as an integer or a character, print the character
407 equivalent as well. */
408 if (TYPE_LENGTH (type) == 1)
410 fputs_filtered (" ", stream);
411 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
412 stream);
415 break;
417 case TYPE_CODE_CHAR:
418 format = format ? format : output_format;
419 if (format)
421 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
423 else
425 val = unpack_long (type, valaddr + embedded_offset);
426 if (TYPE_UNSIGNED (type))
427 fprintf_filtered (stream, "%u", (unsigned int) val);
428 else
429 fprintf_filtered (stream, "%d", (int) val);
430 fputs_filtered (" ", stream);
431 LA_PRINT_CHAR ((unsigned char) val, stream);
433 break;
435 case TYPE_CODE_FLT:
436 if (format)
438 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
440 else
442 print_floating (valaddr + embedded_offset, type, stream);
444 break;
446 case TYPE_CODE_METHOD:
448 struct value *v = value_at (type, address, NULL);
449 cp_print_class_method (VALUE_CONTENTS (value_addr (v)),
450 lookup_pointer_type (type), stream);
451 break;
454 case TYPE_CODE_VOID:
455 fprintf_filtered (stream, "void");
456 break;
458 case TYPE_CODE_ERROR:
459 fprintf_filtered (stream, "<error type>");
460 break;
462 case TYPE_CODE_UNDEF:
463 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
464 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
465 and no complete type for struct foo in that file. */
466 fprintf_filtered (stream, "<incomplete type>");
467 break;
469 case TYPE_CODE_COMPLEX:
470 if (format)
471 print_scalar_formatted (valaddr + embedded_offset,
472 TYPE_TARGET_TYPE (type),
473 format, 0, stream);
474 else
475 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
476 stream);
477 fprintf_filtered (stream, " + ");
478 if (format)
479 print_scalar_formatted (valaddr + embedded_offset
480 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
481 TYPE_TARGET_TYPE (type),
482 format, 0, stream);
483 else
484 print_floating (valaddr + embedded_offset
485 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
486 TYPE_TARGET_TYPE (type),
487 stream);
488 fprintf_filtered (stream, " * I");
489 break;
491 default:
492 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
494 gdb_flush (stream);
495 return (0);
499 c_value_print (struct value *val, struct ui_file *stream, int format,
500 enum val_prettyprint pretty)
502 struct type *type = VALUE_TYPE (val);
503 struct type *real_type;
504 int full, top, using_enc;
506 /* If it is a pointer, indicate what it points to.
508 Print type also if it is a reference.
510 C++: if it is a member pointer, we will take care
511 of that when we print it. */
512 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
513 TYPE_CODE (type) == TYPE_CODE_REF)
515 /* Hack: remove (char *) for char strings. Their
516 type is indicated by the quoted string anyway. */
517 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
518 TYPE_NAME (type) == NULL &&
519 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
520 strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
522 /* Print nothing */
524 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
527 if (TYPE_CODE(type) == TYPE_CODE_REF)
529 /* Copy value, change to pointer, so we don't get an
530 * error about a non-pointer type in value_rtti_target_type
532 struct value *temparg;
533 temparg=value_copy(val);
534 VALUE_TYPE (temparg) = lookup_pointer_type(TYPE_TARGET_TYPE(type));
535 val=temparg;
537 /* Pointer to class, check real type of object */
538 fprintf_filtered (stream, "(");
539 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
540 if (real_type)
542 /* RTTI entry found */
543 if (TYPE_CODE (type) == TYPE_CODE_PTR)
545 /* create a pointer type pointing to the real type */
546 type = lookup_pointer_type (real_type);
548 else
550 /* create a reference type referencing the real type */
551 type = lookup_reference_type (real_type);
553 /* JYG: Need to adjust pointer value. */
554 val->aligner.contents[0] -= top;
556 /* Note: When we look up RTTI entries, we don't get any
557 information on const or volatile attributes */
559 type_print (type, "", stream, -1);
560 fprintf_filtered (stream, ") ");
562 else
564 /* normal case */
565 fprintf_filtered (stream, "(");
566 type_print (type, "", stream, -1);
567 fprintf_filtered (stream, ") ");
570 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
572 /* Attempt to determine real type of object */
573 real_type = value_rtti_type (val, &full, &top, &using_enc);
574 if (real_type)
576 /* We have RTTI information, so use it */
577 val = value_full_object (val, real_type, full, top, using_enc);
578 fprintf_filtered (stream, "(%s%s) ",
579 TYPE_NAME (real_type),
580 full ? "" : " [incomplete object]");
581 /* Print out object: enclosing type is same as real_type if full */
582 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
583 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
584 /* Note: When we look up RTTI entries, we don't get any information on
585 const or volatile attributes */
587 else if (type != VALUE_ENCLOSING_TYPE (val))
589 /* No RTTI information, so let's do our best */
590 fprintf_filtered (stream, "(%s ?) ",
591 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
592 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
593 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
595 /* Otherwise, we end up at the return outside this "if" */
598 return val_print (type, VALUE_CONTENTS_ALL (val),
599 VALUE_EMBEDDED_OFFSET (val),
600 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
601 stream, format, 1, 0, pretty);