* breakpoint.h (enum enable): New enum shlib_disabled for
[deliverable/binutils-gdb.git] / gdb / ch-valprint.c
1 /* Support for printing Chill values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
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, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "obstack.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "valprint.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "language.h"
29 #include "demangle.h"
30 #include "c-lang.h" /* For c_val_print */
31 #include "typeprint.h"
32 #include "ch-lang.h"
33 #include "annotate.h"
34
35 static void
36 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
37 enum val_prettyprint, struct type **));
38
39 \f
40 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
41 Used to print data from type structures in a specified type. For example,
42 array bounds may be characters or booleans in some languages, and this
43 allows the ranges to be printed in their "natural" form rather than as
44 decimal integer values. */
45
46 void
47 chill_print_type_scalar (type, val, stream)
48 struct type *type;
49 LONGEST val;
50 GDB_FILE *stream;
51 {
52 switch (TYPE_CODE (type))
53 {
54 case TYPE_CODE_RANGE:
55 if (TYPE_TARGET_TYPE (type))
56 {
57 chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
58 return;
59 }
60 break;
61 case TYPE_CODE_UNDEF:
62 case TYPE_CODE_PTR:
63 case TYPE_CODE_ARRAY:
64 case TYPE_CODE_STRUCT:
65 case TYPE_CODE_UNION:
66 case TYPE_CODE_ENUM:
67 case TYPE_CODE_FUNC:
68 case TYPE_CODE_INT:
69 case TYPE_CODE_FLT:
70 case TYPE_CODE_VOID:
71 case TYPE_CODE_SET:
72 case TYPE_CODE_STRING:
73 case TYPE_CODE_BITSTRING:
74 case TYPE_CODE_ERROR:
75 case TYPE_CODE_MEMBER:
76 case TYPE_CODE_METHOD:
77 case TYPE_CODE_REF:
78 case TYPE_CODE_CHAR:
79 case TYPE_CODE_BOOL:
80 case TYPE_CODE_COMPLEX:
81 case TYPE_CODE_TYPEDEF:
82 default:
83 break;
84 }
85 print_type_scalar (type, val, stream);
86 }
87 \f
88 /* Print the elements of an array.
89 Similar to val_print_array_elements, but prints
90 element indexes (in Chill syntax). */
91
92 static void
93 chill_val_print_array_elements (type, valaddr, address, stream,
94 format, deref_ref, recurse, pretty)
95 struct type *type;
96 char *valaddr;
97 CORE_ADDR address;
98 GDB_FILE *stream;
99 int format;
100 int deref_ref;
101 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, 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, 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 /* In certain cases it could happen, that an array type doesn't
180 have a length (this have to do with seizing). The reason is
181 shown in the following stabs:
182
183 .stabs "m_x:Tt81=s36i:1,0,32;ar:82=ar80;0;1;83=xsm_struct:,32,256;;",128,0,25,0
184
185 .stabs "m_struct:Tt83=s16f1:9,0,16;f2:85=*84,32,32;f3:84,64,64;;",128,0,10,0
186
187 When processing t81, the array ar80 doesn't have a length, cause
188 struct m_struct is specified extern at thse moment. Afterwards m_struct
189 gets specified and updated, but not the surrounding type.
190
191 So we walk through array's till we find a type with a length and
192 calculate the array length.
193
194 FIXME: Where may this happen too ?
195 */
196
197 static void
198 calculate_array_length (type)
199 struct type *type;
200 {
201 struct type *target_type;
202 struct type *range_type;
203 LONGEST lower_bound, upper_bound;
204
205 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
206 /* not an array, stop processing */
207 return;
208
209 target_type = TYPE_TARGET_TYPE (type);
210 range_type = TYPE_FIELD_TYPE (type, 0);
211 lower_bound = TYPE_FIELD_BITPOS (range_type, 0);
212 upper_bound = TYPE_FIELD_BITPOS (range_type, 1);
213
214 if (TYPE_LENGTH (target_type) == 0 &&
215 TYPE_CODE (target_type) == TYPE_CODE_ARRAY)
216 /* we've got another array */
217 calculate_array_length (target_type);
218
219 TYPE_LENGTH (type) = (upper_bound - lower_bound + 1) * TYPE_LENGTH (target_type);
220 }
221
222 /* Print data of type TYPE located at VALADDR (within GDB), which came from
223 the inferior at address ADDRESS, onto stdio stream STREAM according to
224 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
225 target byte order.
226
227 If the data are a string pointer, returns the number of string characters
228 printed.
229
230 If DEREF_REF is nonzero, then dereference references, otherwise just print
231 them like pointers.
232
233 The PRETTY parameter controls prettyprinting. */
234
235 int
236 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
237 pretty)
238 struct type *type;
239 char *valaddr;
240 CORE_ADDR address;
241 GDB_FILE *stream;
242 int format;
243 int deref_ref;
244 int recurse;
245 enum val_prettyprint pretty;
246 {
247 LONGEST val;
248 unsigned int i = 0; /* Number of characters printed. */
249 struct type *elttype;
250 CORE_ADDR addr;
251
252 CHECK_TYPEDEF (type);
253
254 switch (TYPE_CODE (type))
255 {
256 case TYPE_CODE_ARRAY:
257 if (TYPE_LENGTH (type) == 0)
258 /* see comment function calculate_array_length */
259 calculate_array_length (type);
260
261 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
262 {
263 if (prettyprint_arrays)
264 {
265 print_spaces_filtered (2 + 2 * recurse, stream);
266 }
267 fprintf_filtered (stream, "[");
268 chill_val_print_array_elements (type, valaddr, address, stream,
269 format, deref_ref, recurse, pretty);
270 fprintf_filtered (stream, "]");
271 }
272 else
273 {
274 error ("unimplemented in chill_val_print; unspecified array length");
275 }
276 break;
277
278 case TYPE_CODE_INT:
279 format = format ? format : output_format;
280 if (format)
281 {
282 print_scalar_formatted (valaddr, type, format, 0, stream);
283 }
284 else
285 {
286 val_print_type_code_int (type, valaddr, stream);
287 }
288 break;
289
290 case TYPE_CODE_CHAR:
291 format = format ? format : output_format;
292 if (format)
293 {
294 print_scalar_formatted (valaddr, type, format, 0, stream);
295 }
296 else
297 {
298 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
299 stream);
300 }
301 break;
302
303 case TYPE_CODE_FLT:
304 if (format)
305 {
306 print_scalar_formatted (valaddr, type, format, 0, stream);
307 }
308 else
309 {
310 print_floating (valaddr, type, stream);
311 }
312 break;
313
314 case TYPE_CODE_BOOL:
315 format = format ? format : output_format;
316 if (format)
317 {
318 print_scalar_formatted (valaddr, type, format, 0, stream);
319 }
320 else
321 {
322 /* FIXME: Why is this using builtin_type_chill_bool not type? */
323 val = unpack_long (builtin_type_chill_bool, valaddr);
324 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
325 }
326 break;
327
328 case TYPE_CODE_UNDEF:
329 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
330 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
331 and no complete type for struct foo in that file. */
332 fprintf_filtered (stream, "<incomplete type>");
333 break;
334
335 case TYPE_CODE_PTR:
336 if (format && format != 's')
337 {
338 print_scalar_formatted (valaddr, type, format, 0, stream);
339 break;
340 }
341 addr = unpack_pointer (type, valaddr);
342 elttype = check_typedef (TYPE_TARGET_TYPE (type));
343
344 /* We assume a NULL pointer is all zeros ... */
345 if (addr == 0)
346 {
347 fputs_filtered ("NULL", stream);
348 return 0;
349 }
350
351 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
352 {
353 /* Try to print what function it points to. */
354 print_address_demangle (addr, stream, demangle);
355 /* Return value is irrelevant except for string pointers. */
356 return (0);
357 }
358 if (addressprint && format != 's')
359 {
360 print_address_numeric (addr, 1, stream);
361 }
362
363 /* For a pointer to char or unsigned char, also print the string
364 pointed to, unless pointer is null. */
365 if (TYPE_LENGTH (elttype) == 1
366 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
367 && (format == 0 || format == 's')
368 && addr != 0
369 && /* If print_max is UINT_MAX, the alloca below will fail.
370 In that case don't try to print the string. */
371 print_max < UINT_MAX)
372 {
373 i = val_print_string (addr, 0, stream);
374 }
375 /* Return number of characters printed, plus one for the
376 terminating null if we have "reached the end". */
377 return (i + (print_max && i != print_max));
378 break;
379
380 case TYPE_CODE_STRING:
381 i = TYPE_LENGTH (type);
382 LA_PRINT_STRING (stream, valaddr, i, 0);
383 /* Return number of characters printed, plus one for the terminating
384 null if we have "reached the end". */
385 return (i + (print_max && i != print_max));
386 break;
387
388 case TYPE_CODE_BITSTRING:
389 case TYPE_CODE_SET:
390 elttype = TYPE_INDEX_TYPE (type);
391 CHECK_TYPEDEF (elttype);
392 if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
393 {
394 fprintf_filtered (stream, "<incomplete type>");
395 gdb_flush (stream);
396 break;
397 }
398 {
399 struct type *range = elttype;
400 LONGEST low_bound, high_bound;
401 int i;
402 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
403 int need_comma = 0;
404
405 if (is_bitstring)
406 fputs_filtered ("B'", stream);
407 else
408 fputs_filtered ("[", stream);
409
410 i = get_discrete_bounds (range, &low_bound, &high_bound);
411 maybe_bad_bstring:
412 if (i < 0)
413 {
414 fputs_filtered ("<error value>", stream);
415 goto done;
416 }
417
418 for (i = low_bound; i <= high_bound; i++)
419 {
420 int element = value_bit_index (type, valaddr, i);
421 if (element < 0)
422 {
423 i = element;
424 goto maybe_bad_bstring;
425 }
426 if (is_bitstring)
427 fprintf_filtered (stream, "%d", element);
428 else if (element)
429 {
430 if (need_comma)
431 fputs_filtered (", ", stream);
432 chill_print_type_scalar (range, i, stream);
433 need_comma = 1;
434
435 /* Look for a continuous range of true elements. */
436 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
437 {
438 int j = i; /* j is the upper bound so far of the range */
439 fputs_filtered (":", stream);
440 while (i+1 <= high_bound
441 && value_bit_index (type, valaddr, ++i))
442 j = i;
443 chill_print_type_scalar (range, j, stream);
444 }
445 }
446 }
447 done:
448 if (is_bitstring)
449 fputs_filtered ("'", stream);
450 else
451 fputs_filtered ("]", stream);
452 }
453 break;
454
455 case TYPE_CODE_STRUCT:
456 if (chill_varying_type (type))
457 {
458 struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
459 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
460 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
461
462 switch (TYPE_CODE (inner))
463 {
464 case TYPE_CODE_STRING:
465 if (length > TYPE_LENGTH (type))
466 {
467 fprintf_filtered (stream,
468 "<dynamic length %ld > static length %d>",
469 length, TYPE_LENGTH (type));
470 }
471 LA_PRINT_STRING (stream, data_addr, length, 0);
472 return length;
473 default:
474 break;
475 }
476 }
477 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
478 0);
479 break;
480
481 case TYPE_CODE_REF:
482 if (addressprint)
483 {
484 fprintf_filtered (stream, "LOC(");
485 print_address_numeric
486 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
487 1,
488 stream);
489 fprintf_filtered (stream, ")");
490 if (deref_ref)
491 fputs_filtered (": ", stream);
492 }
493 /* De-reference the reference. */
494 if (deref_ref)
495 {
496 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
497 {
498 value_ptr deref_val =
499 value_at
500 (TYPE_TARGET_TYPE (type),
501 unpack_pointer (lookup_pointer_type (builtin_type_void),
502 valaddr));
503 val_print (VALUE_TYPE (deref_val),
504 VALUE_CONTENTS (deref_val),
505 VALUE_ADDRESS (deref_val), stream, format,
506 deref_ref, recurse + 1, pretty);
507 }
508 else
509 fputs_filtered ("???", stream);
510 }
511 break;
512
513 case TYPE_CODE_ENUM:
514 c_val_print (type, valaddr, address, stream, format,
515 deref_ref, recurse, pretty);
516 break;
517
518 case TYPE_CODE_RANGE:
519 if (TYPE_TARGET_TYPE (type))
520 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
521 format, deref_ref, recurse, pretty);
522 break;
523
524 case TYPE_CODE_MEMBER:
525 case TYPE_CODE_UNION:
526 case TYPE_CODE_FUNC:
527 case TYPE_CODE_VOID:
528 case TYPE_CODE_ERROR:
529 default:
530 /* Let's defer printing to the C printer, rather than
531 print an error message. FIXME! */
532 c_val_print (type, valaddr, address, stream, format,
533 deref_ref, recurse, pretty);
534 }
535 gdb_flush (stream);
536 return (0);
537 }
538
539 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
540 print out a structure's fields: cp_print_value_fields and cplus_print_value.
541
542 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
543 same meanings as in cplus_print_value and c_val_print.
544
545 DONT_PRINT is an array of baseclass types that we
546 should not print, or zero if called from top level. */
547
548 static void
549 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
550 dont_print)
551 struct type *type;
552 char *valaddr;
553 GDB_FILE *stream;
554 int format;
555 int recurse;
556 enum val_prettyprint pretty;
557 struct type **dont_print;
558 {
559 int i, len;
560 int fields_seen = 0;
561
562 CHECK_TYPEDEF (type);
563
564 fprintf_filtered (stream, "[");
565 len = TYPE_NFIELDS (type);
566 if (len == 0)
567 {
568 fprintf_filtered (stream, "<No data fields>");
569 }
570 else
571 {
572 for (i = 0; i < len; i++)
573 {
574 if (fields_seen)
575 {
576 fprintf_filtered (stream, ", ");
577 }
578 fields_seen = 1;
579 if (pretty)
580 {
581 fprintf_filtered (stream, "\n");
582 print_spaces_filtered (2 + 2 * recurse, stream);
583 }
584 else
585 {
586 wrap_here (n_spaces (2 + 2 * recurse));
587 }
588 fputs_filtered (".", stream);
589 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
590 language_chill, DMGL_NO_OPTS);
591 fputs_filtered (": ", stream);
592 if (TYPE_FIELD_PACKED (type, i))
593 {
594 value_ptr v;
595
596 /* Bitfields require special handling, especially due to byte
597 order problems. */
598 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
599 unpack_field_as_long (type, valaddr, i));
600
601 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
602 stream, format, 0, recurse + 1, pretty);
603 }
604 else
605 {
606 chill_val_print (TYPE_FIELD_TYPE (type, i),
607 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
608 0, stream, format, 0, recurse + 1, pretty);
609 }
610 }
611 if (pretty)
612 {
613 fprintf_filtered (stream, "\n");
614 print_spaces_filtered (2 * recurse, stream);
615 }
616 }
617 fprintf_filtered (stream, "]");
618 }
619 \f
620 int
621 chill_value_print (val, stream, format, pretty)
622 value_ptr val;
623 GDB_FILE *stream;
624 int format;
625 enum val_prettyprint pretty;
626 {
627 struct type *type = VALUE_TYPE (val);
628 struct type *real_type = check_typedef (type);
629
630 /* If it is a pointer, indicate what it points to.
631
632 Print type also if it is a reference. */
633
634 if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
635 TYPE_CODE (real_type) == TYPE_CODE_REF)
636 {
637 char *valaddr = VALUE_CONTENTS (val);
638 CORE_ADDR addr = unpack_pointer (type, valaddr);
639 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
640 {
641 int i;
642 char *name = TYPE_NAME (type);
643 if (name)
644 fputs_filtered (name, stream);
645 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
646 fputs_filtered ("PTR", stream);
647 else
648 {
649 fprintf_filtered (stream, "(");
650 type_print (type, "", stream, -1);
651 fprintf_filtered (stream, ")");
652 }
653 fprintf_filtered (stream, "(");
654 i = val_print (type, valaddr, VALUE_ADDRESS (val),
655 stream, format, 1, 0, pretty);
656 fprintf_filtered (stream, ")");
657 return i;
658 }
659 }
660 return (val_print (type, VALUE_CONTENTS (val),
661 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
662 }
663
664
This page took 0.043381 seconds and 4 git commands to generate.