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, 2009, 2010 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 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 type *type, const gdb_byte *valaddr,
39 int embedded_offset, CORE_ADDR address,
40 struct ui_file *stream, int recurse,
41 const struct value_print_options *options,
45 /* Print function pointer with inferior address ADDRESS onto stdio
49 print_function_pointer_address (struct gdbarch *gdbarch, CORE_ADDR address,
50 struct ui_file *stream, int addressprint)
52 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
55 /* If the function pointer is represented by a description, print the
56 address of the description. */
57 if (addressprint && func_addr != address)
59 fputs_filtered ("@", stream);
60 fputs_filtered (paddress (gdbarch, address), stream);
61 fputs_filtered (": ", stream);
63 print_address_demangle (gdbarch, func_addr, stream, demangle);
66 /* get_long_set_bounds - assigns the bounds of the long set to low and
70 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
74 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
76 len = TYPE_NFIELDS (type);
77 i = TYPE_N_BASECLASSES (type);
80 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
81 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
85 error (_("expecting long_set"));
90 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
91 int embedded_offset, CORE_ADDR address,
92 struct ui_file *stream)
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 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);
169 if (previous_low+1 < previous_high)
171 fprintf_filtered (stream, "..");
172 print_type_scalar (target, previous_high, stream);
176 fprintf_filtered (stream, "}");
181 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
182 int embedded_offset, CORE_ADDR address,
183 struct ui_file *stream, int recurse,
184 const struct value_print_options *options)
186 struct type *content_type;
191 CHECK_TYPEDEF (type);
192 content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
194 addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
195 (TYPE_FIELD_BITPOS (type, 0) / 8) +
196 valaddr + embedded_offset);
198 val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
200 len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
202 fprintf_filtered (stream, "{");
203 m2_print_array_contents (value_type (val), value_contents(val),
204 value_embedded_offset (val), addr, stream,
205 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));
218 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
220 /* Try to print what function it points to. */
221 print_function_pointer_address (gdbarch, addr, stream,
222 options->addressprint);
223 /* Return value is irrelevant except for string pointers. */
227 if (options->addressprint && options->format != 's')
228 fputs_filtered (paddress (gdbarch, address), stream);
230 /* For a pointer to char or unsigned char, also print the string
231 pointed to, unless pointer is null. */
233 if (TYPE_LENGTH (elttype) == 1
234 && TYPE_CODE (elttype) == TYPE_CODE_INT
235 && (options->format == 0 || options->format == 's')
237 return val_print_string (TYPE_TARGET_TYPE (type), addr, -1,
244 print_variable_at_address (struct type *type,
245 const gdb_byte *valaddr,
246 struct ui_file *stream,
248 const struct value_print_options *options)
250 struct gdbarch *gdbarch = get_type_arch (type);
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 (gdbarch, 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, recurse, options, current_language);
265 fputs_filtered ("???", stream);
269 /* m2_print_array_contents - prints out the contents of an
270 array up to a max_print values.
271 It prints arrays of char as a string
272 and all other data types as comma
276 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
277 int embedded_offset, CORE_ADDR address,
278 struct ui_file *stream, int recurse,
279 const struct value_print_options *options,
283 CHECK_TYPEDEF (type);
285 if (TYPE_LENGTH (type) > 0)
287 eltlen = TYPE_LENGTH (type);
288 if (options->prettyprint_arrays)
289 print_spaces_filtered (2 + 2 * recurse, stream);
290 /* For an array of chars, print with string syntax. */
292 ((TYPE_CODE (type) == TYPE_CODE_INT)
293 || ((current_language->la_language == language_m2)
294 && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
295 && (options->format == 0 || options->format == 's'))
296 val_print_string (type, address, len+1, stream, options);
299 fprintf_filtered (stream, "{");
300 val_print_array_elements (type, valaddr + embedded_offset,
301 address, stream, recurse, options, 0);
302 fprintf_filtered (stream, "}");
308 /* Print data of type TYPE located at VALADDR (within GDB), which came from
309 the inferior at address ADDRESS, onto stdio stream STREAM according to
310 OPTIONS. The data at VALADDR is in target byte order.
312 If the data are a string pointer, returns the number of string characters
316 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
317 CORE_ADDR address, struct ui_file *stream, int recurse,
318 const struct value_print_options *options)
320 struct gdbarch *gdbarch = get_type_arch (type);
321 unsigned int i = 0; /* Number of characters printed */
323 struct type *elttype;
328 CHECK_TYPEDEF (type);
329 switch (TYPE_CODE (type))
331 case TYPE_CODE_ARRAY:
332 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
334 elttype = check_typedef (TYPE_TARGET_TYPE (type));
335 eltlen = TYPE_LENGTH (elttype);
336 len = TYPE_LENGTH (type) / eltlen;
337 if (options->prettyprint_arrays)
338 print_spaces_filtered (2 + 2 * recurse, stream);
339 /* For an array of chars, print with string syntax. */
341 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
342 || ((current_language->la_language == language_m2)
343 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
344 && (options->format == 0 || options->format == 's'))
346 /* If requested, look for the first null char and only print
347 elements up to it. */
348 if (options->stop_print_at_null)
350 unsigned int temp_len;
352 /* Look for a NULL char. */
354 (valaddr + embedded_offset)[temp_len]
355 && temp_len < len && temp_len < options->print_max;
360 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
361 valaddr + embedded_offset, len, NULL,
367 fprintf_filtered (stream, "{");
368 val_print_array_elements (type, valaddr + embedded_offset,
369 address, stream, recurse, options, 0);
370 fprintf_filtered (stream, "}");
374 /* Array of unspecified length: treat like pointer to first elt. */
375 print_unpacked_pointer (type, address, address, options, stream);
379 if (TYPE_CONST (type))
380 print_variable_at_address (type, valaddr + embedded_offset,
381 stream, recurse, options);
382 else if (options->format && options->format != 's')
383 print_scalar_formatted (valaddr + embedded_offset, type,
387 addr = unpack_pointer (type, valaddr + embedded_offset);
388 print_unpacked_pointer (type, addr, address, options, stream);
393 elttype = check_typedef (TYPE_TARGET_TYPE (type));
394 if (options->addressprint)
397 = extract_typed_address (valaddr + embedded_offset, type);
398 fprintf_filtered (stream, "@");
399 fputs_filtered (paddress (gdbarch, addr), stream);
400 if (options->deref_ref)
401 fputs_filtered (": ", stream);
403 /* De-reference the reference. */
404 if (options->deref_ref)
406 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
408 struct value *deref_val =
410 (TYPE_TARGET_TYPE (type),
411 unpack_pointer (type, valaddr + embedded_offset));
412 common_val_print (deref_val, stream, recurse, options,
416 fputs_filtered ("???", stream);
420 case TYPE_CODE_UNION:
421 if (recurse && !options->unionprint)
423 fprintf_filtered (stream, "{...}");
427 case TYPE_CODE_STRUCT:
428 if (m2_is_long_set (type))
429 m2_print_long_set (type, valaddr, embedded_offset, address,
431 else if (m2_is_unbounded_array (type))
432 m2_print_unbounded_array (type, valaddr, embedded_offset,
433 address, stream, recurse, options);
435 cp_print_value_fields (type, type, valaddr, embedded_offset,
436 address, stream, recurse, options, NULL, 0);
442 print_scalar_formatted (valaddr + embedded_offset, type,
446 len = TYPE_NFIELDS (type);
447 val = unpack_long (type, valaddr + embedded_offset);
448 for (i = 0; i < len; i++)
451 if (val == TYPE_FIELD_BITPOS (type, i))
458 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
462 print_longest (stream, 'd', 0, val);
469 print_scalar_formatted (valaddr + embedded_offset, type,
473 /* FIXME, we should consider, at least for ANSI C language, eliminating
474 the distinction made between FUNCs and POINTERs to FUNCs. */
475 fprintf_filtered (stream, "{");
476 type_print (type, "", stream, -1);
477 fprintf_filtered (stream, "} ");
478 /* Try to print what function it points to, and its address. */
479 print_address_demangle (gdbarch, address, stream, demangle);
483 if (options->format || options->output_format)
485 struct value_print_options opts = *options;
486 opts.format = (options->format ? options->format
487 : options->output_format);
488 print_scalar_formatted (valaddr + embedded_offset, type,
493 val = unpack_long (type, valaddr + embedded_offset);
495 fputs_filtered ("FALSE", stream);
497 fputs_filtered ("TRUE", stream);
499 fprintf_filtered (stream, "%ld)", (long int) val);
503 case TYPE_CODE_RANGE:
504 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
506 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
507 address, stream, recurse, options);
510 /* FIXME: create_range_type does not set the unsigned bit in a
511 range type (I think it probably should copy it from the target
512 type), so we won't print values which are too large to
513 fit in a signed integer correctly. */
514 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
515 print with the target type, though, because the size of our type
516 and the target type might differ). */
520 if (options->format || options->output_format)
522 struct value_print_options opts = *options;
523 opts.format = (options->format ? options->format
524 : options->output_format);
525 print_scalar_formatted (valaddr + embedded_offset, type,
529 val_print_type_code_int (type, valaddr + embedded_offset, stream);
533 if (options->format || options->output_format)
535 struct value_print_options opts = *options;
536 opts.format = (options->format ? options->format
537 : options->output_format);
538 print_scalar_formatted (valaddr + embedded_offset, type,
543 val = unpack_long (type, valaddr + embedded_offset);
544 if (TYPE_UNSIGNED (type))
545 fprintf_filtered (stream, "%u", (unsigned int) val);
547 fprintf_filtered (stream, "%d", (int) val);
548 fputs_filtered (" ", stream);
549 LA_PRINT_CHAR ((unsigned char) val, type, stream);
555 print_scalar_formatted (valaddr + embedded_offset, type,
558 print_floating (valaddr + embedded_offset, type, stream);
561 case TYPE_CODE_METHOD:
564 case TYPE_CODE_BITSTRING:
566 elttype = TYPE_INDEX_TYPE (type);
567 CHECK_TYPEDEF (elttype);
568 if (TYPE_STUB (elttype))
570 fprintf_filtered (stream, _("<incomplete type>"));
576 struct type *range = elttype;
577 LONGEST low_bound, high_bound;
579 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
583 fputs_filtered ("B'", stream);
585 fputs_filtered ("{", stream);
587 i = get_discrete_bounds (range, &low_bound, &high_bound);
591 fputs_filtered (_("<error value>"), stream);
595 for (i = low_bound; i <= high_bound; i++)
597 int element = value_bit_index (type, valaddr + embedded_offset,
602 goto maybe_bad_bstring;
605 fprintf_filtered (stream, "%d", element);
609 fputs_filtered (", ", stream);
610 print_type_scalar (range, i, stream);
613 if (i + 1 <= high_bound
614 && value_bit_index (type, valaddr + embedded_offset,
618 fputs_filtered ("..", stream);
619 while (i + 1 <= high_bound
620 && value_bit_index (type,
621 valaddr + embedded_offset,
624 print_type_scalar (range, j, stream);
630 fputs_filtered ("'", stream);
632 fputs_filtered ("}", stream);
637 fprintf_filtered (stream, "void");
640 case TYPE_CODE_ERROR:
641 fprintf_filtered (stream, _("<error type>"));
644 case TYPE_CODE_UNDEF:
645 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
646 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
647 and no complete type for struct foo in that file. */
648 fprintf_filtered (stream, _("<incomplete type>"));
652 error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));