1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1996, 1998,
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
27 #include "expression.h"
31 #include "typeprint.h"
36 int print_unpacked_pointer (struct type *type,
37 CORE_ADDR address, CORE_ADDR addr,
38 int format, struct ui_file *stream);
41 /* Print function pointer with inferior address ADDRESS onto stdio
45 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
47 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
51 /* If the function pointer is represented by a description, print the
52 address of the description. */
53 if (addressprint && func_addr != address)
55 fputs_filtered ("@", stream);
56 fputs_filtered (paddress (address), stream);
57 fputs_filtered (": ", stream);
59 print_address_demangle (func_addr, stream, demangle);
63 * get_long_set_bounds - assigns the bounds of the long set to low and high.
67 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
71 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
73 len = TYPE_NFIELDS (type);
74 i = TYPE_N_BASECLASSES (type);
77 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
78 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
82 error (_("expecting long_set"));
87 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
88 int embedded_offset, CORE_ADDR address,
89 struct ui_file *stream, int format,
90 enum val_prettyprint pretty)
94 LONGEST previous_low = 0;
95 LONGEST previous_high= 0;
96 LONGEST i, low_bound, high_bound;
97 LONGEST field_low, field_high;
103 CHECK_TYPEDEF (type);
105 fprintf_filtered (stream, "{");
106 len = TYPE_NFIELDS (type);
107 if (get_long_set_bounds (type, &low_bound, &high_bound))
109 field = TYPE_N_BASECLASSES (type);
110 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
114 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
118 target = TYPE_TARGET_TYPE (range);
120 target = builtin_type_int;
122 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
124 for (i = low_bound; i <= high_bound; i++)
126 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
127 (TYPE_FIELD_BITPOS (type, field) / 8) +
128 valaddr + embedded_offset, i);
130 error (_("bit test is out of range"));
137 fprintf_filtered (stream, ", ");
138 print_type_scalar (target, i, stream);
149 if (previous_low+1 < previous_high)
150 fprintf_filtered (stream, "..");
151 if (previous_low+1 < previous_high)
152 print_type_scalar (target, previous_high, stream);
161 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
162 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
164 target = TYPE_TARGET_TYPE (range);
166 target = builtin_type_int;
171 if (previous_low+1 < previous_high)
173 fprintf_filtered (stream, "..");
174 print_type_scalar (target, previous_high, stream);
178 fprintf_filtered (stream, "}");
183 print_unpacked_pointer (struct type *type,
184 CORE_ADDR address, CORE_ADDR addr,
185 int format, struct ui_file *stream)
187 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
189 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
191 /* Try to print what function it points to. */
192 print_function_pointer_address (addr, stream);
193 /* Return value is irrelevant except for string pointers. */
197 if (addressprint && format != 's')
198 fputs_filtered (paddress (address), stream);
200 /* For a pointer to char or unsigned char, also print the string
201 pointed to, unless pointer is null. */
203 if (TYPE_LENGTH (elttype) == 1
204 && TYPE_CODE (elttype) == TYPE_CODE_INT
205 && (format == 0 || format == 's')
207 return val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
213 print_variable_at_address (struct type *type, const gdb_byte *valaddr,
214 struct ui_file *stream, int format,
215 int deref_ref, int recurse,
216 enum val_prettyprint pretty)
218 CORE_ADDR addr = unpack_pointer (type, valaddr);
219 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
221 fprintf_filtered (stream, "[");
222 fputs_filtered (paddress (addr), stream);
223 fprintf_filtered (stream, "] : ");
225 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
227 struct value *deref_val =
229 (TYPE_TARGET_TYPE (type),
230 unpack_pointer (lookup_pointer_type (builtin_type_void),
232 common_val_print (deref_val, stream, format, deref_ref,
236 fputs_filtered ("???", stream);
239 /* Print data of type TYPE located at VALADDR (within GDB), which came from
240 the inferior at address ADDRESS, onto stdio stream STREAM according to
241 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
244 If the data are a string pointer, returns the number of string characters
247 If DEREF_REF is nonzero, then dereference references, otherwise just print
250 The PRETTY parameter controls prettyprinting. */
253 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
254 CORE_ADDR address, struct ui_file *stream, int format,
255 int deref_ref, int recurse, enum val_prettyprint pretty)
257 unsigned int i = 0; /* Number of characters printed */
259 struct type *elttype;
261 int length_pos, length_size, string_pos;
266 CHECK_TYPEDEF (type);
267 switch (TYPE_CODE (type))
269 case TYPE_CODE_ARRAY:
270 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
272 elttype = check_typedef (TYPE_TARGET_TYPE (type));
273 eltlen = TYPE_LENGTH (elttype);
274 len = TYPE_LENGTH (type) / eltlen;
275 if (prettyprint_arrays)
276 print_spaces_filtered (2 + 2 * recurse, stream);
277 /* For an array of chars, print with string syntax. */
279 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
280 || ((current_language->la_language == language_m2)
281 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
282 && (format == 0 || format == 's'))
284 /* If requested, look for the first null char and only print
285 elements up to it. */
286 if (stop_print_at_null)
288 unsigned int temp_len;
290 /* Look for a NULL char. */
292 (valaddr + embedded_offset)[temp_len]
293 && temp_len < len && temp_len < print_max;
298 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, 1, 0);
303 fprintf_filtered (stream, "{");
304 val_print_array_elements (type, valaddr + embedded_offset,
305 address, stream, format, deref_ref,
307 fprintf_filtered (stream, "}");
311 /* Array of unspecified length: treat like pointer to first elt. */
312 print_unpacked_pointer (type, address, address, format, stream);
316 if (TYPE_CONST (type))
317 print_variable_at_address (type, valaddr + embedded_offset,
318 stream, format, deref_ref, recurse,
320 else if (format && format != 's')
321 print_scalar_formatted (valaddr + embedded_offset, type, format,
325 addr = unpack_pointer (type, valaddr + embedded_offset);
326 print_unpacked_pointer (type, addr, address, format, stream);
331 elttype = check_typedef (TYPE_TARGET_TYPE (type));
335 = extract_typed_address (valaddr + embedded_offset, type);
336 fprintf_filtered (stream, "@");
337 fputs_filtered (paddress (addr), stream);
339 fputs_filtered (": ", stream);
341 /* De-reference the reference. */
344 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
346 struct value *deref_val =
348 (TYPE_TARGET_TYPE (type),
349 unpack_pointer (lookup_pointer_type (builtin_type_void),
350 valaddr + embedded_offset));
351 common_val_print (deref_val, stream, format, deref_ref,
355 fputs_filtered ("???", stream);
359 case TYPE_CODE_UNION:
360 if (recurse && !unionprint)
362 fprintf_filtered (stream, "{...}");
366 case TYPE_CODE_STRUCT:
367 if (m2_is_long_set (type))
368 m2_print_long_set (type, valaddr, embedded_offset, address,
369 stream, format, pretty);
371 cp_print_value_fields (type, type, valaddr, embedded_offset,
372 address, stream, format,
373 recurse, pretty, NULL, 0);
379 print_scalar_formatted (valaddr + embedded_offset, type,
383 len = TYPE_NFIELDS (type);
384 val = unpack_long (type, valaddr + embedded_offset);
385 for (i = 0; i < len; i++)
388 if (val == TYPE_FIELD_BITPOS (type, i))
395 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
399 print_longest (stream, 'd', 0, val);
406 print_scalar_formatted (valaddr + embedded_offset, type,
410 /* FIXME, we should consider, at least for ANSI C language, eliminating
411 the distinction made between FUNCs and POINTERs to FUNCs. */
412 fprintf_filtered (stream, "{");
413 type_print (type, "", stream, -1);
414 fprintf_filtered (stream, "} ");
415 /* Try to print what function it points to, and its address. */
416 print_address_demangle (address, stream, demangle);
420 format = format ? format : output_format;
422 print_scalar_formatted (valaddr + embedded_offset, type,
426 val = unpack_long (type, valaddr + embedded_offset);
428 fputs_filtered ("FALSE", stream);
430 fputs_filtered ("TRUE", stream);
432 fprintf_filtered (stream, "%ld)", (long int) val);
436 case TYPE_CODE_RANGE:
437 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
439 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
440 address, stream, format, deref_ref, recurse, pretty);
443 /* FIXME: create_range_type does not set the unsigned bit in a
444 range type (I think it probably should copy it from the target
445 type), so we won't print values which are too large to
446 fit in a signed integer correctly. */
447 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
448 print with the target type, though, because the size of our type
449 and the target type might differ). */
453 format = format ? format : output_format;
455 print_scalar_formatted (valaddr + embedded_offset, type, format,
458 val_print_type_code_int (type, valaddr + embedded_offset, stream);
462 format = format ? format : output_format;
464 print_scalar_formatted (valaddr + embedded_offset, type,
468 val = unpack_long (type, valaddr + embedded_offset);
469 if (TYPE_UNSIGNED (type))
470 fprintf_filtered (stream, "%u", (unsigned int) val);
472 fprintf_filtered (stream, "%d", (int) val);
473 fputs_filtered (" ", stream);
474 LA_PRINT_CHAR ((unsigned char) val, stream);
480 print_scalar_formatted (valaddr + embedded_offset, type,
483 print_floating (valaddr + embedded_offset, type, stream);
486 case TYPE_CODE_METHOD:
489 case TYPE_CODE_BITSTRING:
491 elttype = TYPE_INDEX_TYPE (type);
492 CHECK_TYPEDEF (elttype);
493 if (TYPE_STUB (elttype))
495 fprintf_filtered (stream, _("<incomplete type>"));
501 struct type *range = elttype;
502 LONGEST low_bound, high_bound;
504 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
508 fputs_filtered ("B'", stream);
510 fputs_filtered ("{", stream);
512 i = get_discrete_bounds (range, &low_bound, &high_bound);
516 fputs_filtered (_("<error value>"), stream);
520 for (i = low_bound; i <= high_bound; i++)
522 int element = value_bit_index (type, valaddr + embedded_offset,
527 goto maybe_bad_bstring;
530 fprintf_filtered (stream, "%d", element);
534 fputs_filtered (", ", stream);
535 print_type_scalar (range, i, stream);
538 if (i + 1 <= high_bound
539 && value_bit_index (type, valaddr + embedded_offset,
543 fputs_filtered ("..", stream);
544 while (i + 1 <= high_bound
545 && value_bit_index (type,
546 valaddr + embedded_offset,
549 print_type_scalar (range, j, stream);
555 fputs_filtered ("'", stream);
557 fputs_filtered ("}", stream);
562 fprintf_filtered (stream, "void");
565 case TYPE_CODE_ERROR:
566 fprintf_filtered (stream, _("<error type>"));
569 case TYPE_CODE_UNDEF:
570 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
571 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
572 and no complete type for struct foo in that file. */
573 fprintf_filtered (stream, _("<incomplete type>"));
577 error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));