1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "expression.h"
27 #include "typeprint.h"
31 #include "cli/cli-style.h"
33 static int print_unpacked_pointer (struct type
*type
,
34 CORE_ADDR address
, CORE_ADDR addr
,
35 const struct value_print_options
*options
,
36 struct ui_file
*stream
);
38 m2_print_array_contents (struct value
*val
,
39 struct ui_file
*stream
, int recurse
,
40 const struct value_print_options
*options
,
44 /* get_long_set_bounds - assigns the bounds of the long set to low and
48 get_long_set_bounds (struct type
*type
, LONGEST
*low
, LONGEST
*high
)
52 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
54 len
= TYPE_NFIELDS (type
);
55 i
= TYPE_N_BASECLASSES (type
);
58 *low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)));
59 *high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
,
63 error (_("expecting long_set"));
68 m2_print_long_set (struct type
*type
, const gdb_byte
*valaddr
,
69 int embedded_offset
, CORE_ADDR address
,
70 struct ui_file
*stream
)
74 LONGEST previous_low
= 0;
75 LONGEST previous_high
= 0;
76 LONGEST i
, low_bound
, high_bound
;
77 LONGEST field_low
, field_high
;
83 type
= check_typedef (type
);
85 fprintf_filtered (stream
, "{");
86 len
= TYPE_NFIELDS (type
);
87 if (get_long_set_bounds (type
, &low_bound
, &high_bound
))
89 field
= TYPE_N_BASECLASSES (type
);
90 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, field
));
94 fprintf_styled (stream
, metadata_style
.style (),
95 " %s }", _("<unknown bounds of set>"));
99 target
= TYPE_TARGET_TYPE (range
);
101 if (get_discrete_bounds (range
, &field_low
, &field_high
) >= 0)
103 for (i
= low_bound
; i
<= high_bound
; i
++)
105 bitval
= value_bit_index (TYPE_FIELD_TYPE (type
, field
),
106 (TYPE_FIELD_BITPOS (type
, field
) / 8) +
107 valaddr
+ embedded_offset
, i
);
109 error (_("bit test is out of range"));
116 fprintf_filtered (stream
, ", ");
117 print_type_scalar (target
, i
, stream
);
128 if (previous_low
+1 < previous_high
)
129 fprintf_filtered (stream
, "..");
130 if (previous_low
+1 < previous_high
)
131 print_type_scalar (target
, previous_high
, stream
);
140 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, field
));
141 if (get_discrete_bounds (range
, &field_low
, &field_high
) < 0)
143 target
= TYPE_TARGET_TYPE (range
);
148 if (previous_low
+1 < previous_high
)
150 fprintf_filtered (stream
, "..");
151 print_type_scalar (target
, previous_high
, stream
);
155 fprintf_filtered (stream
, "}");
160 m2_print_unbounded_array (struct value
*value
,
161 struct ui_file
*stream
, int recurse
,
162 const struct value_print_options
*options
)
168 struct type
*type
= check_typedef (value_type (value
));
169 const gdb_byte
*valaddr
= value_contents_for_printing (value
);
171 addr
= unpack_pointer (TYPE_FIELD_TYPE (type
, 0),
172 (TYPE_FIELD_BITPOS (type
, 0) / 8) +
175 val
= value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0)),
177 len
= unpack_field_as_long (type
, valaddr
, 1);
179 fprintf_filtered (stream
, "{");
180 m2_print_array_contents (val
, stream
, recurse
, options
, len
);
181 fprintf_filtered (stream
, ", HIGH = %d}", (int) len
);
185 m2_print_unbounded_array (struct type
*type
, const gdb_byte
*valaddr
,
186 int embedded_offset
, CORE_ADDR address
,
187 struct ui_file
*stream
, int recurse
,
188 const struct value_print_options
*options
)
194 type
= check_typedef (type
);
196 addr
= unpack_pointer (TYPE_FIELD_TYPE (type
, 0),
197 (TYPE_FIELD_BITPOS (type
, 0) / 8) +
198 valaddr
+ embedded_offset
);
200 val
= value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0)),
202 len
= unpack_field_as_long (type
, valaddr
+ embedded_offset
, 1);
204 fprintf_filtered (stream
, "{");
205 m2_print_array_contents (val
, stream
, recurse
, options
, len
);
206 fprintf_filtered (stream
, ", HIGH = %d}", (int) len
);
210 print_unpacked_pointer (struct type
*type
,
211 CORE_ADDR address
, CORE_ADDR addr
,
212 const struct value_print_options
*options
,
213 struct ui_file
*stream
)
215 struct gdbarch
*gdbarch
= get_type_arch (type
);
216 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
219 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
221 /* Try to print what function it points to. */
222 print_function_pointer_address (options
, gdbarch
, addr
, stream
);
223 /* Return value is irrelevant except for string pointers. */
227 if (options
->addressprint
&& options
->format
!= 's')
229 fputs_filtered (paddress (gdbarch
, address
), stream
);
233 /* For a pointer to char or unsigned char, also print the string
234 pointed to, unless pointer is null. */
236 if (TYPE_LENGTH (elttype
) == 1
237 && TYPE_CODE (elttype
) == TYPE_CODE_INT
238 && (options
->format
== 0 || options
->format
== 's')
242 fputs_filtered (" ", stream
);
243 return val_print_string (TYPE_TARGET_TYPE (type
), NULL
, addr
, -1,
251 print_variable_at_address (struct type
*type
,
252 const gdb_byte
*valaddr
,
253 struct ui_file
*stream
,
255 const struct value_print_options
*options
)
257 struct gdbarch
*gdbarch
= get_type_arch (type
);
258 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
259 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
261 fprintf_filtered (stream
, "[");
262 fputs_filtered (paddress (gdbarch
, addr
), stream
);
263 fprintf_filtered (stream
, "] : ");
265 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
267 struct value
*deref_val
=
268 value_at (TYPE_TARGET_TYPE (type
), unpack_pointer (type
, valaddr
));
270 common_val_print (deref_val
, stream
, recurse
, options
, current_language
);
273 fputs_filtered ("???", stream
);
277 /* m2_print_array_contents - prints out the contents of an
278 array up to a max_print values.
279 It prints arrays of char as a string
280 and all other data types as comma
284 m2_print_array_contents (struct value
*val
,
285 struct ui_file
*stream
, int recurse
,
286 const struct value_print_options
*options
,
289 struct type
*type
= check_typedef (value_type (val
));
291 if (TYPE_LENGTH (type
) > 0)
293 if (options
->prettyformat_arrays
)
294 print_spaces_filtered (2 + 2 * recurse
, stream
);
295 /* For an array of chars, print with string syntax. */
296 if (TYPE_LENGTH (type
) == 1 &&
297 ((TYPE_CODE (type
) == TYPE_CODE_INT
)
298 || ((current_language
->la_language
== language_m2
)
299 && (TYPE_CODE (type
) == TYPE_CODE_CHAR
)))
300 && (options
->format
== 0 || options
->format
== 's'))
301 val_print_string (type
, NULL
, value_address (val
), len
+1, stream
,
305 fprintf_filtered (stream
, "{");
306 value_print_array_elements (val
, stream
, recurse
, options
, 0);
307 fprintf_filtered (stream
, "}");
312 /* Decorations for Modula 2. */
314 static const struct generic_val_print_decorations m2_decorations
=
326 /* See val_print for a description of the various parameters of this
327 function; they are identical. */
330 m2_val_print (struct type
*type
, int embedded_offset
,
331 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
332 struct value
*original_value
,
333 const struct value_print_options
*options
)
336 struct type
*elttype
;
338 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
340 type
= check_typedef (type
);
341 switch (TYPE_CODE (type
))
343 case TYPE_CODE_ARRAY
:
344 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
346 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
347 len
= TYPE_LENGTH (type
) / TYPE_LENGTH (elttype
);
348 if (options
->prettyformat_arrays
)
349 print_spaces_filtered (2 + 2 * recurse
, stream
);
350 /* For an array of chars, print with string syntax. */
351 if (TYPE_LENGTH (elttype
) == 1 &&
352 ((TYPE_CODE (elttype
) == TYPE_CODE_INT
)
353 || ((current_language
->la_language
== language_m2
)
354 && (TYPE_CODE (elttype
) == TYPE_CODE_CHAR
)))
355 && (options
->format
== 0 || options
->format
== 's'))
357 /* If requested, look for the first null char and only print
358 elements up to it. */
359 if (options
->stop_print_at_null
)
361 unsigned int temp_len
;
363 /* Look for a NULL char. */
365 (valaddr
+ embedded_offset
)[temp_len
]
366 && temp_len
< len
&& temp_len
< options
->print_max
;
371 LA_PRINT_STRING (stream
, TYPE_TARGET_TYPE (type
),
372 valaddr
+ embedded_offset
, len
, NULL
,
377 fprintf_filtered (stream
, "{");
378 val_print_array_elements (type
, embedded_offset
,
380 recurse
, original_value
,
382 fprintf_filtered (stream
, "}");
386 /* Array of unspecified length: treat like pointer to first elt. */
387 print_unpacked_pointer (type
, address
, address
, options
, stream
);
391 if (TYPE_CONST (type
))
392 print_variable_at_address (type
, valaddr
+ embedded_offset
,
393 stream
, recurse
, options
);
394 else if (options
->format
&& options
->format
!= 's')
395 val_print_scalar_formatted (type
, embedded_offset
,
396 original_value
, options
, 0, stream
);
399 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
400 print_unpacked_pointer (type
, addr
, address
, options
, stream
);
404 case TYPE_CODE_UNION
:
405 if (recurse
&& !options
->unionprint
)
407 fprintf_filtered (stream
, "{...}");
411 case TYPE_CODE_STRUCT
:
412 if (m2_is_long_set (type
))
413 m2_print_long_set (type
, valaddr
, embedded_offset
, address
,
415 else if (m2_is_unbounded_array (type
))
416 m2_print_unbounded_array (type
, valaddr
, embedded_offset
,
417 address
, stream
, recurse
, options
);
419 cp_print_value_fields (type
, type
, embedded_offset
,
420 address
, stream
, recurse
, original_value
,
425 elttype
= TYPE_INDEX_TYPE (type
);
426 elttype
= check_typedef (elttype
);
427 if (TYPE_STUB (elttype
))
429 fprintf_styled (stream
, metadata_style
.style (),
430 _("<incomplete type>"));
435 struct type
*range
= elttype
;
436 LONGEST low_bound
, high_bound
;
440 fputs_filtered ("{", stream
);
442 i
= get_discrete_bounds (range
, &low_bound
, &high_bound
);
446 fputs_styled (_("<error value>"), metadata_style
.style (),
451 for (i
= low_bound
; i
<= high_bound
; i
++)
453 int element
= value_bit_index (type
, valaddr
+ embedded_offset
,
459 goto maybe_bad_bstring
;
464 fputs_filtered (", ", stream
);
465 print_type_scalar (range
, i
, stream
);
468 if (i
+ 1 <= high_bound
469 && value_bit_index (type
, valaddr
+ embedded_offset
,
474 fputs_filtered ("..", stream
);
475 while (i
+ 1 <= high_bound
476 && value_bit_index (type
,
477 valaddr
+ embedded_offset
,
480 print_type_scalar (range
, j
, stream
);
485 fputs_filtered ("}", stream
);
489 case TYPE_CODE_RANGE
:
490 if (TYPE_LENGTH (type
) == TYPE_LENGTH (TYPE_TARGET_TYPE (type
)))
492 m2_val_print (TYPE_TARGET_TYPE (type
), embedded_offset
,
493 address
, stream
, recurse
, original_value
, options
);
496 /* FIXME: create_static_range_type does not set the unsigned bit in a
497 range type (I think it probably should copy it from the target
498 type), so we won't print values which are too large to
499 fit in a signed integer correctly. */
500 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
501 print with the target type, though, because the size of our type
502 and the target type might differ). */
510 case TYPE_CODE_METHOD
:
512 case TYPE_CODE_ERROR
:
513 case TYPE_CODE_UNDEF
:
517 generic_val_print (type
, embedded_offset
, address
,
518 stream
, recurse
, original_value
, options
,
527 m2_value_print_inner (struct value
*val
, struct ui_file
*stream
, int recurse
,
528 const struct value_print_options
*options
)
531 struct type
*elttype
;
533 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
534 const CORE_ADDR address
= value_address (val
);
536 struct type
*type
= check_typedef (value_type (val
));
537 switch (TYPE_CODE (type
))
539 case TYPE_CODE_ARRAY
:
540 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
542 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
543 len
= TYPE_LENGTH (type
) / TYPE_LENGTH (elttype
);
544 if (options
->prettyformat_arrays
)
545 print_spaces_filtered (2 + 2 * recurse
, stream
);
546 /* For an array of chars, print with string syntax. */
547 if (TYPE_LENGTH (elttype
) == 1 &&
548 ((TYPE_CODE (elttype
) == TYPE_CODE_INT
)
549 || ((current_language
->la_language
== language_m2
)
550 && (TYPE_CODE (elttype
) == TYPE_CODE_CHAR
)))
551 && (options
->format
== 0 || options
->format
== 's'))
553 /* If requested, look for the first null char and only print
554 elements up to it. */
555 if (options
->stop_print_at_null
)
557 unsigned int temp_len
;
559 /* Look for a NULL char. */
562 && temp_len
< len
&& temp_len
< options
->print_max
);
567 LA_PRINT_STRING (stream
, TYPE_TARGET_TYPE (type
),
568 valaddr
, len
, NULL
, 0, options
);
572 fprintf_filtered (stream
, "{");
573 value_print_array_elements (val
, stream
, recurse
,
575 fprintf_filtered (stream
, "}");
579 /* Array of unspecified length: treat like pointer to first elt. */
580 print_unpacked_pointer (type
, address
, address
, options
, stream
);
584 if (TYPE_CONST (type
))
585 print_variable_at_address (type
, valaddr
, stream
, recurse
, options
);
586 else if (options
->format
&& options
->format
!= 's')
587 value_print_scalar_formatted (val
, options
, 0, stream
);
590 addr
= unpack_pointer (type
, valaddr
);
591 print_unpacked_pointer (type
, addr
, address
, options
, stream
);
595 case TYPE_CODE_UNION
:
596 if (recurse
&& !options
->unionprint
)
598 fprintf_filtered (stream
, "{...}");
602 case TYPE_CODE_STRUCT
:
603 if (m2_is_long_set (type
))
604 m2_print_long_set (type
, valaddr
, 0, address
, stream
);
605 else if (m2_is_unbounded_array (type
))
606 m2_print_unbounded_array (val
, stream
, recurse
, options
);
608 cp_print_value_fields (type
, type
, 0,
609 address
, stream
, recurse
, val
,
614 elttype
= TYPE_INDEX_TYPE (type
);
615 elttype
= check_typedef (elttype
);
616 if (TYPE_STUB (elttype
))
618 fprintf_styled (stream
, metadata_style
.style (),
619 _("<incomplete type>"));
624 struct type
*range
= elttype
;
625 LONGEST low_bound
, high_bound
;
629 fputs_filtered ("{", stream
);
631 i
= get_discrete_bounds (range
, &low_bound
, &high_bound
);
635 fputs_styled (_("<error value>"), metadata_style
.style (),
640 for (i
= low_bound
; i
<= high_bound
; i
++)
642 int element
= value_bit_index (type
, valaddr
, i
);
647 goto maybe_bad_bstring
;
652 fputs_filtered (", ", stream
);
653 print_type_scalar (range
, i
, stream
);
656 if (i
+ 1 <= high_bound
657 && value_bit_index (type
, valaddr
, ++i
))
661 fputs_filtered ("..", stream
);
662 while (i
+ 1 <= high_bound
663 && value_bit_index (type
, valaddr
, ++i
))
665 print_type_scalar (range
, j
, stream
);
670 fputs_filtered ("}", stream
);
674 case TYPE_CODE_RANGE
:
675 if (TYPE_LENGTH (type
) == TYPE_LENGTH (TYPE_TARGET_TYPE (type
)))
677 struct value
*v
= value_cast (TYPE_TARGET_TYPE (type
), val
);
678 m2_value_print_inner (v
, stream
, recurse
, options
);
681 /* FIXME: create_static_range_type does not set the unsigned bit in a
682 range type (I think it probably should copy it from the target
683 type), so we won't print values which are too large to
684 fit in a signed integer correctly. */
685 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
686 print with the target type, though, because the size of our type
687 and the target type might differ). */
695 case TYPE_CODE_METHOD
:
697 case TYPE_CODE_ERROR
:
698 case TYPE_CODE_UNDEF
:
702 generic_value_print (val
, stream
, recurse
, options
, &m2_decorations
);