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