1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1996, 1998, 2000, 2005, 2006,
4 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
28 #include "typeprint.h"
33 int print_unpacked_pointer (struct type *type,
34 CORE_ADDR address, CORE_ADDR addr,
35 int format, struct ui_file *stream);
37 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
38 int embedded_offset, CORE_ADDR address,
39 struct ui_file *stream, int format,
40 enum val_prettyprint pretty,
41 int deref_ref, int recurse, int len);
44 /* Print function pointer with inferior address ADDRESS onto stdio
48 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
50 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
54 /* If the function pointer is represented by a description, print the
55 address of the description. */
56 if (addressprint && func_addr != address)
58 fputs_filtered ("@", stream);
59 fputs_filtered (paddress (address), stream);
60 fputs_filtered (": ", stream);
62 print_address_demangle (func_addr, stream, demangle);
65 /* get_long_set_bounds - assigns the bounds of the long set to low and
69 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
73 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
75 len = TYPE_NFIELDS (type);
76 i = TYPE_N_BASECLASSES (type);
79 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
80 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
84 error (_("expecting long_set"));
89 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
90 int embedded_offset, CORE_ADDR address,
91 struct ui_file *stream, int format,
92 enum val_prettyprint pretty)
96 LONGEST previous_low = 0;
97 LONGEST previous_high= 0;
98 LONGEST i, low_bound, high_bound;
99 LONGEST field_low, field_high;
105 CHECK_TYPEDEF (type);
107 fprintf_filtered (stream, "{");
108 len = TYPE_NFIELDS (type);
109 if (get_long_set_bounds (type, &low_bound, &high_bound))
111 field = TYPE_N_BASECLASSES (type);
112 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
116 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
120 target = TYPE_TARGET_TYPE (range);
122 target = builtin_type_int32;
124 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
126 for (i = low_bound; i <= high_bound; i++)
128 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
129 (TYPE_FIELD_BITPOS (type, field) / 8) +
130 valaddr + embedded_offset, i);
132 error (_("bit test is out of range"));
139 fprintf_filtered (stream, ", ");
140 print_type_scalar (target, i, stream);
151 if (previous_low+1 < previous_high)
152 fprintf_filtered (stream, "..");
153 if (previous_low+1 < previous_high)
154 print_type_scalar (target, previous_high, stream);
163 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
164 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
166 target = TYPE_TARGET_TYPE (range);
168 target = builtin_type_int32;
173 if (previous_low+1 < previous_high)
175 fprintf_filtered (stream, "..");
176 print_type_scalar (target, previous_high, stream);
180 fprintf_filtered (stream, "}");
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 format,
188 int deref_ref, enum val_prettyprint pretty,
191 struct type *content_type;
196 CHECK_TYPEDEF (type);
197 content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
199 addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
200 (TYPE_FIELD_BITPOS (type, 0) / 8) +
201 valaddr + embedded_offset);
203 val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
205 len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
207 fprintf_filtered (stream, "{");
208 m2_print_array_contents (value_type (val), value_contents(val),
209 value_embedded_offset (val), addr, stream,
210 format, deref_ref, pretty, recurse, len);
211 fprintf_filtered (stream, ", HIGH = %d}", (int) len);
215 print_unpacked_pointer (struct type *type,
216 CORE_ADDR address, CORE_ADDR addr,
217 int format, struct ui_file *stream)
219 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
221 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
223 /* Try to print what function it points to. */
224 print_function_pointer_address (addr, stream);
225 /* Return value is irrelevant except for string pointers. */
229 if (addressprint && format != 's')
230 fputs_filtered (paddress (address), stream);
232 /* For a pointer to char or unsigned char, also print the string
233 pointed to, unless pointer is null. */
235 if (TYPE_LENGTH (elttype) == 1
236 && TYPE_CODE (elttype) == TYPE_CODE_INT
237 && (format == 0 || format == 's')
239 return val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
245 print_variable_at_address (struct type *type,
246 const gdb_byte *valaddr,
247 struct ui_file *stream, int format,
248 int deref_ref, int recurse,
249 enum val_prettyprint pretty)
251 CORE_ADDR addr = unpack_pointer (type, valaddr);
252 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
254 fprintf_filtered (stream, "[");
255 fputs_filtered (paddress (addr), stream);
256 fprintf_filtered (stream, "] : ");
258 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
260 struct value *deref_val =
261 value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
262 common_val_print (deref_val, stream, format, deref_ref,
263 recurse, pretty, current_language);
266 fputs_filtered ("???", stream);
270 /* m2_print_array_contents - prints out the contents of an
271 array up to a max_print values.
272 It prints arrays of char as a string
273 and all other data types as comma
277 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
278 int embedded_offset, CORE_ADDR address,
279 struct ui_file *stream, int format,
280 enum val_prettyprint pretty,
281 int deref_ref, int recurse, int len)
284 CHECK_TYPEDEF (type);
286 if (TYPE_LENGTH (type) > 0)
288 eltlen = TYPE_LENGTH (type);
289 if (prettyprint_arrays)
290 print_spaces_filtered (2 + 2 * recurse, stream);
291 /* For an array of chars, print with string syntax. */
293 ((TYPE_CODE (type) == TYPE_CODE_INT)
294 || ((current_language->la_language == language_m2)
295 && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
296 && (format == 0 || format == 's'))
297 val_print_string (address, len+1, eltlen, stream);
300 fprintf_filtered (stream, "{");
301 val_print_array_elements (type, valaddr + embedded_offset,
302 address, stream, format,
303 deref_ref, recurse, pretty, 0);
304 fprintf_filtered (stream, "}");
310 /* Print data of type TYPE located at VALADDR (within GDB), which came from
311 the inferior at address ADDRESS, onto stdio stream STREAM according to
312 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
315 If the data are a string pointer, returns the number of string characters
318 If DEREF_REF is nonzero, then dereference references, otherwise just print
321 The PRETTY parameter controls prettyprinting. */
324 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
325 CORE_ADDR address, struct ui_file *stream, int format,
326 int deref_ref, int recurse, enum val_prettyprint pretty)
328 unsigned int i = 0; /* Number of characters printed */
330 struct type *elttype;
332 int length_pos, length_size, string_pos;
337 CHECK_TYPEDEF (type);
338 switch (TYPE_CODE (type))
340 case TYPE_CODE_ARRAY:
341 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
343 elttype = check_typedef (TYPE_TARGET_TYPE (type));
344 eltlen = TYPE_LENGTH (elttype);
345 len = TYPE_LENGTH (type) / eltlen;
346 if (prettyprint_arrays)
347 print_spaces_filtered (2 + 2 * recurse, stream);
348 /* For an array of chars, print with string syntax. */
350 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
351 || ((current_language->la_language == language_m2)
352 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
353 && (format == 0 || format == 's'))
355 /* If requested, look for the first null char and only print
356 elements up to it. */
357 if (stop_print_at_null)
359 unsigned int temp_len;
361 /* Look for a NULL char. */
363 (valaddr + embedded_offset)[temp_len]
364 && temp_len < len && temp_len < print_max;
369 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, 1, 0);
374 fprintf_filtered (stream, "{");
375 val_print_array_elements (type, valaddr + embedded_offset,
376 address, stream, format, deref_ref,
378 fprintf_filtered (stream, "}");
382 /* Array of unspecified length: treat like pointer to first elt. */
383 print_unpacked_pointer (type, address, address, format, stream);
387 if (TYPE_CONST (type))
388 print_variable_at_address (type, valaddr + embedded_offset,
389 stream, format, deref_ref, recurse,
391 else if (format && format != 's')
392 print_scalar_formatted (valaddr + embedded_offset, type, format,
396 addr = unpack_pointer (type, valaddr + embedded_offset);
397 print_unpacked_pointer (type, addr, address, format, stream);
402 elttype = check_typedef (TYPE_TARGET_TYPE (type));
406 = extract_typed_address (valaddr + embedded_offset, type);
407 fprintf_filtered (stream, "@");
408 fputs_filtered (paddress (addr), stream);
410 fputs_filtered (": ", stream);
412 /* De-reference the reference. */
415 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
417 struct value *deref_val =
419 (TYPE_TARGET_TYPE (type),
420 unpack_pointer (type, valaddr + embedded_offset));
421 common_val_print (deref_val, stream, format, deref_ref,
422 recurse, pretty, current_language);
425 fputs_filtered ("???", stream);
429 case TYPE_CODE_UNION:
430 if (recurse && !unionprint)
432 fprintf_filtered (stream, "{...}");
436 case TYPE_CODE_STRUCT:
437 if (m2_is_long_set (type))
438 m2_print_long_set (type, valaddr, embedded_offset, address,
439 stream, format, pretty);
440 else if (m2_is_unbounded_array (type))
441 m2_print_unbounded_array (type, valaddr, embedded_offset,
442 address, stream, format, deref_ref,
445 cp_print_value_fields (type, type, valaddr, embedded_offset,
446 address, stream, format,
447 recurse, pretty, NULL, 0);
453 print_scalar_formatted (valaddr + embedded_offset, type,
457 len = TYPE_NFIELDS (type);
458 val = unpack_long (type, valaddr + embedded_offset);
459 for (i = 0; i < len; i++)
462 if (val == TYPE_FIELD_BITPOS (type, i))
469 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
473 print_longest (stream, 'd', 0, val);
480 print_scalar_formatted (valaddr + embedded_offset, type,
484 /* FIXME, we should consider, at least for ANSI C language, eliminating
485 the distinction made between FUNCs and POINTERs to FUNCs. */
486 fprintf_filtered (stream, "{");
487 type_print (type, "", stream, -1);
488 fprintf_filtered (stream, "} ");
489 /* Try to print what function it points to, and its address. */
490 print_address_demangle (address, stream, demangle);
494 format = format ? format : output_format;
496 print_scalar_formatted (valaddr + embedded_offset, type,
500 val = unpack_long (type, valaddr + embedded_offset);
502 fputs_filtered ("FALSE", stream);
504 fputs_filtered ("TRUE", stream);
506 fprintf_filtered (stream, "%ld)", (long int) val);
510 case TYPE_CODE_RANGE:
511 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
513 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
514 address, stream, format, deref_ref, recurse, pretty);
517 /* FIXME: create_range_type does not set the unsigned bit in a
518 range type (I think it probably should copy it from the target
519 type), so we won't print values which are too large to
520 fit in a signed integer correctly. */
521 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
522 print with the target type, though, because the size of our type
523 and the target type might differ). */
527 format = format ? format : output_format;
529 print_scalar_formatted (valaddr + embedded_offset, type, format,
532 val_print_type_code_int (type, valaddr + embedded_offset, stream);
536 format = format ? format : output_format;
538 print_scalar_formatted (valaddr + embedded_offset, type,
542 val = unpack_long (type, valaddr + embedded_offset);
543 if (TYPE_UNSIGNED (type))
544 fprintf_filtered (stream, "%u", (unsigned int) val);
546 fprintf_filtered (stream, "%d", (int) val);
547 fputs_filtered (" ", stream);
548 LA_PRINT_CHAR ((unsigned char) val, stream);
554 print_scalar_formatted (valaddr + embedded_offset, type,
557 print_floating (valaddr + embedded_offset, type, stream);
560 case TYPE_CODE_METHOD:
563 case TYPE_CODE_BITSTRING:
565 elttype = TYPE_INDEX_TYPE (type);
566 CHECK_TYPEDEF (elttype);
567 if (TYPE_STUB (elttype))
569 fprintf_filtered (stream, _("<incomplete type>"));
575 struct type *range = elttype;
576 LONGEST low_bound, high_bound;
578 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
582 fputs_filtered ("B'", stream);
584 fputs_filtered ("{", stream);
586 i = get_discrete_bounds (range, &low_bound, &high_bound);
590 fputs_filtered (_("<error value>"), stream);
594 for (i = low_bound; i <= high_bound; i++)
596 int element = value_bit_index (type, valaddr + embedded_offset,
601 goto maybe_bad_bstring;
604 fprintf_filtered (stream, "%d", element);
608 fputs_filtered (", ", stream);
609 print_type_scalar (range, i, stream);
612 if (i + 1 <= high_bound
613 && value_bit_index (type, valaddr + embedded_offset,
617 fputs_filtered ("..", stream);
618 while (i + 1 <= high_bound
619 && value_bit_index (type,
620 valaddr + embedded_offset,
623 print_type_scalar (range, j, stream);
629 fputs_filtered ("'", stream);
631 fputs_filtered ("}", stream);
636 fprintf_filtered (stream, "void");
639 case TYPE_CODE_ERROR:
640 fprintf_filtered (stream, _("<error type>"));
643 case TYPE_CODE_UNDEF:
644 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
645 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
646 and no complete type for struct foo in that file. */
647 fprintf_filtered (stream, _("<incomplete type>"));
651 error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));