* monitor.c (#include "gdb_wait.h"): Removed.
[platform/upstream/binutils.git] / gdb / ch-valprint.c
1 /* Support for printing Chill values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991-1994, 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 "obstack.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "valprint.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "language.h"
30 #include "demangle.h"
31 #include "c-lang.h"             /* For c_val_print */
32 #include "typeprint.h"
33 #include "ch-lang.h"
34 #include "annotate.h"
35
36 static void chill_print_value_fields (struct type *, char *,
37                                       struct ui_file *, int, int,
38                                       enum val_prettyprint, struct type **);
39
40 static void chill_print_type_scalar (struct type *, LONGEST,
41                                      struct ui_file *);
42
43 static void chill_val_print_array_elements (struct type *, char *,
44                                             CORE_ADDR, struct ui_file *,
45                                             int, int, int,
46                                             enum val_prettyprint);
47 \f
48
49 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
50    Used to print data from type structures in a specified type.  For example,
51    array bounds may be characters or booleans in some languages, and this
52    allows the ranges to be printed in their "natural" form rather than as
53    decimal integer values. */
54
55 static void
56 chill_print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
57 {
58   switch (TYPE_CODE (type))
59     {
60     case TYPE_CODE_RANGE:
61       if (TYPE_TARGET_TYPE (type))
62         {
63           chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
64           return;
65         }
66       break;
67     case TYPE_CODE_UNDEF:
68     case TYPE_CODE_PTR:
69     case TYPE_CODE_ARRAY:
70     case TYPE_CODE_STRUCT:
71     case TYPE_CODE_UNION:
72     case TYPE_CODE_ENUM:
73     case TYPE_CODE_FUNC:
74     case TYPE_CODE_INT:
75     case TYPE_CODE_FLT:
76     case TYPE_CODE_VOID:
77     case TYPE_CODE_SET:
78     case TYPE_CODE_STRING:
79     case TYPE_CODE_BITSTRING:
80     case TYPE_CODE_ERROR:
81     case TYPE_CODE_MEMBER:
82     case TYPE_CODE_METHOD:
83     case TYPE_CODE_REF:
84     case TYPE_CODE_CHAR:
85     case TYPE_CODE_BOOL:
86     case TYPE_CODE_COMPLEX:
87     case TYPE_CODE_TYPEDEF:
88     default:
89       break;
90     }
91   print_type_scalar (type, val, stream);
92 }
93 \f
94 /* Print the elements of an array.
95    Similar to val_print_array_elements, but prints
96    element indexes (in Chill syntax). */
97
98 static void
99 chill_val_print_array_elements (struct type *type, char *valaddr,
100                                 CORE_ADDR address, struct ui_file *stream,
101                                 int format, int deref_ref, int recurse,
102                                 enum val_prettyprint pretty)
103 {
104   unsigned int i = 0;
105   unsigned int things_printed = 0;
106   unsigned len;
107   struct type *elttype;
108   struct type *range_type = TYPE_FIELD_TYPE (type, 0);
109   struct type *index_type = TYPE_TARGET_TYPE (range_type);
110   unsigned eltlen;
111   /* Position of the array element we are examining to see
112      whether it is repeated.  */
113   unsigned int rep1;
114   /* Number of repetitions we have detected so far.  */
115   unsigned int reps;
116   LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
117
118   elttype = check_typedef (TYPE_TARGET_TYPE (type));
119   eltlen = TYPE_LENGTH (elttype);
120   len = TYPE_LENGTH (type) / eltlen;
121
122   annotate_array_section_begin (i, elttype);
123
124   for (; i < len && things_printed < print_max; i++)
125     {
126       if (i != 0)
127         {
128           if (prettyprint_arrays)
129             {
130               fprintf_filtered (stream, ",\n");
131               print_spaces_filtered (2 + 2 * recurse, stream);
132             }
133           else
134             {
135               fprintf_filtered (stream, ", ");
136             }
137         }
138       wrap_here (n_spaces (2 + 2 * recurse));
139
140       rep1 = i + 1;
141       reps = 1;
142       while ((rep1 < len) &&
143              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
144         {
145           ++reps;
146           ++rep1;
147         }
148
149       fputs_filtered ("(", stream);
150       chill_print_type_scalar (index_type, low_bound + i, stream);
151       if (reps > 1)
152         {
153           fputs_filtered (":", stream);
154           chill_print_type_scalar (index_type, low_bound + i + reps - 1,
155                                    stream);
156           fputs_filtered ("): ", stream);
157           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
158                      deref_ref, recurse + 1, pretty);
159
160           i = rep1 - 1;
161           things_printed += 1;
162         }
163       else
164         {
165           fputs_filtered ("): ", stream);
166           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
167                      deref_ref, recurse + 1, pretty);
168           annotate_elt ();
169           things_printed++;
170         }
171     }
172   annotate_array_section_end ();
173   if (i < len)
174     {
175       fprintf_filtered (stream, "...");
176     }
177 }
178
179 /* Print data of type TYPE located at VALADDR (within GDB), which came from
180    the inferior at address ADDRESS, onto stdio stream STREAM according to
181    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
182    target byte order.
183
184    If the data are a string pointer, returns the number of string characters
185    printed.
186
187    If DEREF_REF is nonzero, then dereference references, otherwise just print
188    them like pointers.
189
190    The PRETTY parameter controls prettyprinting.  */
191
192 int
193 chill_val_print (struct type *type, char *valaddr, int embedded_offset,
194                  CORE_ADDR address, struct ui_file *stream, int format,
195                  int deref_ref, int recurse, enum val_prettyprint pretty)
196 {
197   LONGEST val;
198   unsigned int i = 0;           /* Number of characters printed.  */
199   struct type *elttype;
200   CORE_ADDR addr;
201
202   CHECK_TYPEDEF (type);
203
204   switch (TYPE_CODE (type))
205     {
206     case TYPE_CODE_ARRAY:
207       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
208         {
209           if (prettyprint_arrays)
210             {
211               print_spaces_filtered (2 + 2 * recurse, stream);
212             }
213           fprintf_filtered (stream, "[");
214           chill_val_print_array_elements (type, valaddr, address, stream,
215                                         format, deref_ref, recurse, pretty);
216           fprintf_filtered (stream, "]");
217         }
218       else
219         {
220           error ("unimplemented in chill_val_print; unspecified array length");
221         }
222       break;
223
224     case TYPE_CODE_INT:
225       format = format ? format : output_format;
226       if (format)
227         {
228           print_scalar_formatted (valaddr, type, format, 0, stream);
229         }
230       else
231         {
232           val_print_type_code_int (type, valaddr, stream);
233         }
234       break;
235
236     case TYPE_CODE_CHAR:
237       format = format ? format : output_format;
238       if (format)
239         {
240           print_scalar_formatted (valaddr, type, format, 0, stream);
241         }
242       else
243         {
244           LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
245                          stream);
246         }
247       break;
248
249     case TYPE_CODE_FLT:
250       if (format)
251         {
252           print_scalar_formatted (valaddr, type, format, 0, stream);
253         }
254       else
255         {
256           print_floating (valaddr, type, stream);
257         }
258       break;
259
260     case TYPE_CODE_BOOL:
261       format = format ? format : output_format;
262       if (format)
263         {
264           print_scalar_formatted (valaddr, type, format, 0, stream);
265         }
266       else
267         {
268           /* FIXME: Why is this using builtin_type_chill_bool not type?  */
269           val = unpack_long (builtin_type_chill_bool, valaddr);
270           fprintf_filtered (stream, val ? "TRUE" : "FALSE");
271         }
272       break;
273
274     case TYPE_CODE_UNDEF:
275       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
276          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
277          and no complete type for struct foo in that file.  */
278       fprintf_filtered (stream, "<incomplete type>");
279       break;
280
281     case TYPE_CODE_PTR:
282       if (format && format != 's')
283         {
284           print_scalar_formatted (valaddr, type, format, 0, stream);
285           break;
286         }
287       addr = unpack_pointer (type, valaddr);
288       elttype = check_typedef (TYPE_TARGET_TYPE (type));
289
290       /* We assume a NULL pointer is all zeros ... */
291       if (addr == 0)
292         {
293           fputs_filtered ("NULL", stream);
294           return 0;
295         }
296
297       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
298         {
299           /* Try to print what function it points to.  */
300           print_address_demangle (addr, stream, demangle);
301           /* Return value is irrelevant except for string pointers.  */
302           return (0);
303         }
304       if (addressprint && format != 's')
305         {
306           print_address_numeric (addr, 1, stream);
307         }
308
309       /* For a pointer to char or unsigned char, also print the string
310          pointed to, unless pointer is null.  */
311       if (TYPE_LENGTH (elttype) == 1
312           && TYPE_CODE (elttype) == TYPE_CODE_CHAR
313           && (format == 0 || format == 's')
314           && addr != 0
315           &&                    /* If print_max is UINT_MAX, the alloca below will fail.
316                                    In that case don't try to print the string.  */
317           print_max < UINT_MAX)
318         i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
319
320       /* Return number of characters printed, plus one for the
321          terminating null if we have "reached the end".  */
322       return (i + (print_max && i != print_max));
323       break;
324
325     case TYPE_CODE_STRING:
326       i = TYPE_LENGTH (type);
327       LA_PRINT_STRING (stream, valaddr, i, 1, 0);
328       /* Return number of characters printed, plus one for the terminating
329          null if we have "reached the end".  */
330       return (i + (print_max && i != print_max));
331       break;
332
333     case TYPE_CODE_BITSTRING:
334     case TYPE_CODE_SET:
335       elttype = TYPE_INDEX_TYPE (type);
336       CHECK_TYPEDEF (elttype);
337       if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
338         {
339           fprintf_filtered (stream, "<incomplete type>");
340           gdb_flush (stream);
341           break;
342         }
343       {
344         struct type *range = elttype;
345         LONGEST low_bound, high_bound;
346         int i;
347         int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
348         int need_comma = 0;
349
350         if (is_bitstring)
351           fputs_filtered ("B'", stream);
352         else
353           fputs_filtered ("[", stream);
354
355         i = get_discrete_bounds (range, &low_bound, &high_bound);
356       maybe_bad_bstring:
357         if (i < 0)
358           {
359             fputs_filtered ("<error value>", stream);
360             goto done;
361           }
362
363         for (i = low_bound; i <= high_bound; i++)
364           {
365             int element = value_bit_index (type, valaddr, i);
366             if (element < 0)
367               {
368                 i = element;
369                 goto maybe_bad_bstring;
370               }
371             if (is_bitstring)
372               fprintf_filtered (stream, "%d", element);
373             else if (element)
374               {
375                 if (need_comma)
376                   fputs_filtered (", ", stream);
377                 chill_print_type_scalar (range, (LONGEST) i, stream);
378                 need_comma = 1;
379
380                 /* Look for a continuous range of true elements. */
381                 if (i + 1 <= high_bound && value_bit_index (type, valaddr, ++i))
382                   {
383                     int j = i;  /* j is the upper bound so far of the range */
384                     fputs_filtered (":", stream);
385                     while (i + 1 <= high_bound
386                            && value_bit_index (type, valaddr, ++i))
387                       j = i;
388                     chill_print_type_scalar (range, (LONGEST) j, stream);
389                   }
390               }
391           }
392       done:
393         if (is_bitstring)
394           fputs_filtered ("'", stream);
395         else
396           fputs_filtered ("]", stream);
397       }
398       break;
399
400     case TYPE_CODE_STRUCT:
401       if (chill_varying_type (type))
402         {
403           struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
404           long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
405           char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
406
407           switch (TYPE_CODE (inner))
408             {
409             case TYPE_CODE_STRING:
410               if (length > TYPE_LENGTH (type) - 2)
411                 {
412                   fprintf_filtered (stream,
413                         "<dynamic length %ld > static length %d> *invalid*",
414                                     length, TYPE_LENGTH (type));
415
416                   /* Don't print the string; doing so might produce a
417                      segfault.  */
418                   return length;
419                 }
420               LA_PRINT_STRING (stream, data_addr, length, 1, 0);
421               return length;
422             default:
423               break;
424             }
425         }
426       chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
427                                 0);
428       break;
429
430     case TYPE_CODE_REF:
431       if (addressprint)
432         {
433           fprintf_filtered (stream, "LOC(");
434           print_address_numeric
435             (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
436              1,
437              stream);
438           fprintf_filtered (stream, ")");
439           if (deref_ref)
440             fputs_filtered (": ", stream);
441         }
442       /* De-reference the reference.  */
443       if (deref_ref)
444         {
445           if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
446             {
447               value_ptr deref_val =
448               value_at
449               (TYPE_TARGET_TYPE (type),
450                unpack_pointer (lookup_pointer_type (builtin_type_void),
451                                valaddr),
452                NULL);
453               val_print (VALUE_TYPE (deref_val),
454                          VALUE_CONTENTS (deref_val),
455                          0,
456                          VALUE_ADDRESS (deref_val), stream, format,
457                          deref_ref, recurse + 1, pretty);
458             }
459           else
460             fputs_filtered ("???", stream);
461         }
462       break;
463
464     case TYPE_CODE_ENUM:
465       c_val_print (type, valaddr, 0, address, stream, format,
466                    deref_ref, recurse, pretty);
467       break;
468
469     case TYPE_CODE_RANGE:
470       if (TYPE_TARGET_TYPE (type))
471         chill_val_print (TYPE_TARGET_TYPE (type), valaddr, 0, address, stream,
472                          format, deref_ref, recurse, pretty);
473       break;
474
475     case TYPE_CODE_MEMBER:
476     case TYPE_CODE_UNION:
477     case TYPE_CODE_FUNC:
478     case TYPE_CODE_VOID:
479     case TYPE_CODE_ERROR:
480     default:
481       /* Let's defer printing to the C printer, rather than
482          print an error message.  FIXME! */
483       c_val_print (type, valaddr, 0, address, stream, format,
484                    deref_ref, recurse, pretty);
485     }
486   gdb_flush (stream);
487   return (0);
488 }
489
490 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
491    print out a structure's fields: cp_print_value_fields and cplus_print_value.
492
493    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
494    same meanings as in cplus_print_value and c_val_print.
495
496    DONT_PRINT is an array of baseclass types that we
497    should not print, or zero if called from top level.  */
498
499 static void
500 chill_print_value_fields (struct type *type, char *valaddr,
501                           struct ui_file *stream, int format, int recurse,
502                           enum val_prettyprint pretty, struct type **dont_print)
503 {
504   int i, len;
505   int fields_seen = 0;
506
507   CHECK_TYPEDEF (type);
508
509   fprintf_filtered (stream, "[");
510   len = TYPE_NFIELDS (type);
511   if (len == 0)
512     {
513       fprintf_filtered (stream, "<No data fields>");
514     }
515   else
516     {
517       for (i = 0; i < len; i++)
518         {
519           if (fields_seen)
520             {
521               fprintf_filtered (stream, ", ");
522             }
523           fields_seen = 1;
524           if (pretty)
525             {
526               fprintf_filtered (stream, "\n");
527               print_spaces_filtered (2 + 2 * recurse, stream);
528             }
529           else
530             {
531               wrap_here (n_spaces (2 + 2 * recurse));
532             }
533           fputs_filtered (".", stream);
534           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
535                                    language_chill, DMGL_NO_OPTS);
536           fputs_filtered (": ", stream);
537           if (TYPE_FIELD_PACKED (type, i))
538             {
539               value_ptr v;
540
541               /* Bitfields require special handling, especially due to byte
542                  order problems.  */
543               v = value_from_longest (TYPE_FIELD_TYPE (type, i),
544                                    unpack_field_as_long (type, valaddr, i));
545
546               chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
547                                stream, format, 0, recurse + 1, pretty);
548             }
549           else
550             {
551               chill_val_print (TYPE_FIELD_TYPE (type, i),
552                                valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
553                                0, stream, format, 0, recurse + 1, pretty);
554             }
555         }
556       if (pretty)
557         {
558           fprintf_filtered (stream, "\n");
559           print_spaces_filtered (2 * recurse, stream);
560         }
561     }
562   fprintf_filtered (stream, "]");
563 }
564 \f
565 int
566 chill_value_print (value_ptr val, struct ui_file *stream, int format,
567                    enum val_prettyprint pretty)
568 {
569   struct type *type = VALUE_TYPE (val);
570   struct type *real_type = check_typedef (type);
571
572   /* If it is a pointer, indicate what it points to.
573
574      Print type also if it is a reference. */
575
576   if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
577       TYPE_CODE (real_type) == TYPE_CODE_REF)
578     {
579       char *valaddr = VALUE_CONTENTS (val);
580       CORE_ADDR addr = unpack_pointer (type, valaddr);
581       if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
582         {
583           int i;
584           char *name = TYPE_NAME (type);
585           if (name)
586             fputs_filtered (name, stream);
587           else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
588             fputs_filtered ("PTR", stream);
589           else
590             {
591               fprintf_filtered (stream, "(");
592               type_print (type, "", stream, -1);
593               fprintf_filtered (stream, ")");
594             }
595           fprintf_filtered (stream, "(");
596           i = val_print (type, valaddr, 0, VALUE_ADDRESS (val),
597                          stream, format, 1, 0, pretty);
598           fprintf_filtered (stream, ")");
599           return i;
600         }
601     }
602   return (val_print (type, VALUE_CONTENTS (val), 0,
603                      VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
604 }