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