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