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