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