import gdb-1999-08-09 snapshot
[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, 1992, 1993, 1994, 1995, 1996, 1997
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 GDB_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 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 print_address_demangle (extract_address (valaddr + embedded_offset, TYPE_LENGTH (type)),
139 stream, demangle);
140 break;
141 }
142 elttype = check_typedef (TYPE_TARGET_TYPE (type));
143 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
144 {
145 cp_print_class_method (valaddr + embedded_offset, type, stream);
146 }
147 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
148 {
149 cp_print_class_member (valaddr + embedded_offset,
150 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
151 stream, "&");
152 }
153 else
154 {
155 addr = unpack_pointer (type, valaddr + embedded_offset);
156 print_unpacked_pointer:
157 elttype = check_typedef (TYPE_TARGET_TYPE (type));
158
159 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
160 {
161 /* Try to print what function it points to. */
162 print_address_demangle (addr, stream, demangle);
163 /* Return value is irrelevant except for string pointers. */
164 return (0);
165 }
166
167 if (addressprint && format != 's')
168 {
169 print_address_numeric (addr, 1, stream);
170 }
171
172 /* For a pointer to char or unsigned char, also print the string
173 pointed to, unless pointer is null. */
174 /* FIXME: need to handle wchar_t here... */
175
176 if (TYPE_LENGTH (elttype) == 1
177 && TYPE_CODE (elttype) == TYPE_CODE_INT
178 && (format == 0 || format == 's')
179 && addr != 0)
180 {
181 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
182 }
183 else if (cp_is_vtbl_member (type))
184 {
185 /* print vtbl's nicely */
186 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
187
188 struct minimal_symbol *msymbol =
189 lookup_minimal_symbol_by_pc (vt_address);
190 if ((msymbol != NULL) &&
191 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
192 {
193 fputs_filtered (" <", stream);
194 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
195 fputs_filtered (">", stream);
196 }
197 if (vt_address && vtblprint)
198 {
199 value_ptr vt_val;
200 struct symbol *wsym = (struct symbol *) NULL;
201 struct type *wtype;
202 struct symtab *s;
203 struct block *block = (struct block *) NULL;
204 int is_this_fld;
205
206 if (msymbol != NULL)
207 wsym = lookup_symbol (SYMBOL_NAME (msymbol), block,
208 VAR_NAMESPACE, &is_this_fld, &s);
209
210 if (wsym)
211 {
212 wtype = SYMBOL_TYPE (wsym);
213 }
214 else
215 {
216 wtype = TYPE_TARGET_TYPE (type);
217 }
218 vt_val = value_at (wtype, vt_address, NULL);
219 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
220 VALUE_ADDRESS (vt_val), stream, format,
221 deref_ref, recurse + 1, pretty);
222 if (pretty)
223 {
224 fprintf_filtered (stream, "\n");
225 print_spaces_filtered (2 + 2 * recurse, stream);
226 }
227 }
228 }
229
230 /* Return number of characters printed, including the terminating
231 '\0' if we reached the end. val_print_string takes care including
232 the terminating '\0' if necessary. */
233 return i;
234 }
235 break;
236
237 case TYPE_CODE_MEMBER:
238 error ("not implemented: member type in c_val_print");
239 break;
240
241 case TYPE_CODE_REF:
242 elttype = check_typedef (TYPE_TARGET_TYPE (type));
243 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
244 {
245 cp_print_class_member (valaddr + embedded_offset,
246 TYPE_DOMAIN_TYPE (elttype),
247 stream, "");
248 break;
249 }
250 if (addressprint)
251 {
252 fprintf_filtered (stream, "@");
253 print_address_numeric
254 (extract_address (valaddr + embedded_offset,
255 TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
256 if (deref_ref)
257 fputs_filtered (": ", stream);
258 }
259 /* De-reference the reference. */
260 if (deref_ref)
261 {
262 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
263 {
264 value_ptr deref_val =
265 value_at
266 (TYPE_TARGET_TYPE (type),
267 unpack_pointer (lookup_pointer_type (builtin_type_void),
268 valaddr + embedded_offset),
269 NULL);
270 val_print (VALUE_TYPE (deref_val),
271 VALUE_CONTENTS (deref_val),
272 0,
273 VALUE_ADDRESS (deref_val),
274 stream,
275 format,
276 deref_ref,
277 recurse,
278 pretty);
279 }
280 else
281 fputs_filtered ("???", stream);
282 }
283 break;
284
285 case TYPE_CODE_UNION:
286 if (recurse && !unionprint)
287 {
288 fprintf_filtered (stream, "{...}");
289 break;
290 }
291 /* Fall through. */
292 case TYPE_CODE_STRUCT:
293 if (vtblprint && cp_is_vtbl_ptr_type (type))
294 {
295 /* Print the unmangled name if desired. */
296 /* Print vtable entry - we only get here if NOT using
297 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
298 print_address_demangle (extract_address (
299 valaddr + embedded_offset +
300 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8,
301 TYPE_LENGTH (TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET))),
302 stream, demangle);
303 }
304 else
305 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
306 recurse, pretty, NULL, 0);
307 break;
308
309 case TYPE_CODE_ENUM:
310 if (format)
311 {
312 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
313 break;
314 }
315 len = TYPE_NFIELDS (type);
316 val = unpack_long (type, valaddr + embedded_offset);
317 for (i = 0; i < len; i++)
318 {
319 QUIT;
320 if (val == TYPE_FIELD_BITPOS (type, i))
321 {
322 break;
323 }
324 }
325 if (i < len)
326 {
327 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
328 }
329 else
330 {
331 print_longest (stream, 'd', 0, val);
332 }
333 break;
334
335 case TYPE_CODE_FUNC:
336 if (format)
337 {
338 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
339 break;
340 }
341 /* FIXME, we should consider, at least for ANSI C language, eliminating
342 the distinction made between FUNCs and POINTERs to FUNCs. */
343 fprintf_filtered (stream, "{");
344 type_print (type, "", stream, -1);
345 fprintf_filtered (stream, "} ");
346 /* Try to print what function it points to, and its address. */
347 print_address_demangle (address, stream, demangle);
348 break;
349
350 case TYPE_CODE_BOOL:
351 format = format ? format : output_format;
352 if (format)
353 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
354 else
355 {
356 val = unpack_long (type, valaddr + embedded_offset);
357 if (val == 0)
358 fputs_filtered ("false", stream);
359 else if (val == 1)
360 fputs_filtered ("true", stream);
361 else
362 print_longest (stream, 'd', 0, val);
363 }
364 break;
365
366 case TYPE_CODE_RANGE:
367 /* FIXME: create_range_type does not set the unsigned bit in a
368 range type (I think it probably should copy it from the target
369 type), so we won't print values which are too large to
370 fit in a signed integer correctly. */
371 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
372 print with the target type, though, because the size of our type
373 and the target type might differ). */
374 /* FALLTHROUGH */
375
376 case TYPE_CODE_INT:
377 format = format ? format : output_format;
378 if (format)
379 {
380 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
381 }
382 else
383 {
384 val_print_type_code_int (type, valaddr + embedded_offset, stream);
385 /* C and C++ has no single byte int type, char is used instead.
386 Since we don't know whether the value is really intended to
387 be used as an integer or a character, print the character
388 equivalent as well. */
389 if (TYPE_LENGTH (type) == 1)
390 {
391 fputs_filtered (" ", stream);
392 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
393 stream);
394 }
395 }
396 break;
397
398 case TYPE_CODE_CHAR:
399 format = format ? format : output_format;
400 if (format)
401 {
402 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
403 }
404 else
405 {
406 val = unpack_long (type, valaddr + embedded_offset);
407 if (TYPE_UNSIGNED (type))
408 fprintf_filtered (stream, "%u", (unsigned int) val);
409 else
410 fprintf_filtered (stream, "%d", (int) val);
411 fputs_filtered (" ", stream);
412 LA_PRINT_CHAR ((unsigned char) val, stream);
413 }
414 break;
415
416 case TYPE_CODE_FLT:
417 if (format)
418 {
419 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
420 }
421 else
422 {
423 print_floating (valaddr + embedded_offset, type, stream);
424 }
425 break;
426
427 case TYPE_CODE_METHOD:
428 cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream);
429 break;
430
431 case TYPE_CODE_VOID:
432 fprintf_filtered (stream, "void");
433 break;
434
435 case TYPE_CODE_ERROR:
436 fprintf_filtered (stream, "<error type>");
437 break;
438
439 case TYPE_CODE_UNDEF:
440 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
441 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
442 and no complete type for struct foo in that file. */
443 fprintf_filtered (stream, "<incomplete type>");
444 break;
445
446 default:
447 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
448 }
449 gdb_flush (stream);
450 return (0);
451 }
452 \f
453 int
454 c_value_print (val, stream, format, pretty)
455 value_ptr val;
456 GDB_FILE *stream;
457 int format;
458 enum val_prettyprint pretty;
459 {
460 struct type *type = VALUE_TYPE (val);
461 struct type *real_type;
462 int full, top, using_enc;
463
464 /* If it is a pointer, indicate what it points to.
465
466 Print type also if it is a reference.
467
468 C++: if it is a member pointer, we will take care
469 of that when we print it. */
470 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
471 TYPE_CODE (type) == TYPE_CODE_REF)
472 {
473 /* Hack: remove (char *) for char strings. Their
474 type is indicated by the quoted string anyway. */
475 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
476 TYPE_NAME (type) == NULL &&
477 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
478 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
479 {
480 /* Print nothing */
481 }
482 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
483 {
484 /* Pointer to class, check real type of object */
485 fprintf_filtered (stream, "(");
486 type = value_rtti_target_type (val, &full, &top, &using_enc);
487 if (type)
488 {
489 /* RTTI entry found */
490 type = lookup_pointer_type (type);
491 type_print (type, "", stream, -1);
492 }
493 else
494 {
495 /* No RTTI fields, do whatever we can */
496 type = VALUE_ENCLOSING_TYPE (val);
497 type_print (type, "", stream, -1);
498 fprintf_filtered (stream, " ?");
499 }
500 fprintf_filtered (stream, ") ");
501 }
502 else
503 {
504 /* normal case */
505 fprintf_filtered (stream, "(");
506 type_print (type, "", stream, -1);
507 fprintf_filtered (stream, ") ");
508 }
509 }
510 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
511 {
512 /* Attempt to determine real type of object */
513 real_type = value_rtti_type (val, &full, &top, &using_enc);
514 if (real_type)
515 {
516 /* We have RTTI information, so use it */
517 val = value_full_object (val, real_type, full, top, using_enc);
518 fprintf_filtered (stream, "(%s%s) ",
519 TYPE_NAME (real_type),
520 full ? "" : " [incomplete object]");
521 /* Print out object: enclosing type is same as real_type if full */
522 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
523 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
524 }
525 else if (type != VALUE_ENCLOSING_TYPE (val))
526 {
527 /* No RTTI information, so let's do our best */
528 fprintf_filtered (stream, "(%s ?) ",
529 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
530 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
531 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
532 }
533 /* Otherwise, we end up at the return outside this "if" */
534 }
535
536 return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
537 VALUE_ADDRESS (val),
538 stream, format, 1, 0, pretty);
539 }
This page took 0.041111 seconds and 4 git commands to generate.