GDB (xrefs)
/tmp/gdb-8.1/gdb/m2-valprint.c
Go to the documentation of this file.
1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
2 
3  Copyright (C) 1986-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
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.
11 
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.
16 
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/>. */
19 
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "valprint.h"
26 #include "language.h"
27 #include "typeprint.h"
28 #include "c-lang.h"
29 #include "m2-lang.h"
30 #include "target.h"
31 
32 static int print_unpacked_pointer (struct type *type,
33  CORE_ADDR address, CORE_ADDR addr,
34  const struct value_print_options *options,
35  struct ui_file *stream);
36 static void
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 recurse,
40  struct value *val,
41  const struct value_print_options *options,
42  int len);
43 
44 
45 /* get_long_set_bounds - assigns the bounds of the long set to low and
46  high. */
47 
48 int
50 {
51  int len, i;
52 
54  {
55  len = TYPE_NFIELDS (type);
57  if (len == 0)
58  return 0;
61  len-1)));
62  return 1;
63  }
64  error (_("expecting long_set"));
65  return 0;
66 }
67 
68 static void
69 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
70  int embedded_offset, CORE_ADDR address,
71  struct ui_file *stream)
72 {
73  int empty_set = 1;
74  int element_seen = 0;
75  LONGEST previous_low = 0;
76  LONGEST previous_high= 0;
77  LONGEST i, low_bound, high_bound;
78  LONGEST field_low, field_high;
79  struct type *range;
80  int len, field;
81  struct type *target;
82  int bitval;
83 
85 
86  fprintf_filtered (stream, "{");
87  len = TYPE_NFIELDS (type);
88  if (get_long_set_bounds (type, &low_bound, &high_bound))
89  {
92  }
93  else
94  {
95  fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
96  return;
97  }
98 
99  target = TYPE_TARGET_TYPE (range);
100 
101  if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
102  {
103  for (i = low_bound; i <= high_bound; i++)
104  {
106  (TYPE_FIELD_BITPOS (type, field) / 8) +
107  valaddr + embedded_offset, i);
108  if (bitval < 0)
109  error (_("bit test is out of range"));
110  else if (bitval > 0)
111  {
112  previous_high = i;
113  if (! element_seen)
114  {
115  if (! empty_set)
116  fprintf_filtered (stream, ", ");
117  print_type_scalar (target, i, stream);
118  empty_set = 0;
119  element_seen = 1;
120  previous_low = i;
121  }
122  }
123  else
124  {
125  /* bit is not set */
126  if (element_seen)
127  {
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);
132  element_seen = 0;
133  }
134  }
135  if (i == field_high)
136  {
137  field++;
138  if (field == len)
139  break;
141  if (get_discrete_bounds (range, &field_low, &field_high) < 0)
142  break;
143  target = TYPE_TARGET_TYPE (range);
144  }
145  }
146  if (element_seen)
147  {
148  if (previous_low+1 < previous_high)
149  {
150  fprintf_filtered (stream, "..");
151  print_type_scalar (target, previous_high, stream);
152  }
153  element_seen = 0;
154  }
155  fprintf_filtered (stream, "}");
156  }
157 }
158 
159 static void
160 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
161  int embedded_offset, CORE_ADDR address,
162  struct ui_file *stream, int recurse,
163  const struct value_print_options *options)
164 {
165  CORE_ADDR addr;
166  LONGEST len;
167  struct value *val;
168 
169  type = check_typedef (type);
170 
171  addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
172  (TYPE_FIELD_BITPOS (type, 0) / 8) +
173  valaddr + embedded_offset);
174 
176  addr);
177  len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
178 
179  fprintf_filtered (stream, "{");
182  value_embedded_offset (val), addr, stream,
183  recurse, val, options, len);
184  fprintf_filtered (stream, ", HIGH = %d}", (int) len);
185 }
186 
187 static int
190  const struct value_print_options *options,
191  struct ui_file *stream)
192 {
193  struct gdbarch *gdbarch = get_type_arch (type);
194  struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
195  int want_space = 0;
196 
197  if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
198  {
199  /* Try to print what function it points to. */
200  print_function_pointer_address (options, gdbarch, addr, stream);
201  /* Return value is irrelevant except for string pointers. */
202  return 0;
203  }
204 
205  if (options->addressprint && options->format != 's')
206  {
207  fputs_filtered (paddress (gdbarch, address), stream);
208  want_space = 1;
209  }
210 
211  /* For a pointer to char or unsigned char, also print the string
212  pointed to, unless pointer is null. */
213 
214  if (TYPE_LENGTH (elttype) == 1
215  && TYPE_CODE (elttype) == TYPE_CODE_INT
216  && (options->format == 0 || options->format == 's')
217  && addr != 0)
218  {
219  if (want_space)
220  fputs_filtered (" ", stream);
221  return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
222  stream, options);
223  }
224 
225  return 0;
226 }
227 
228 static void
230  const gdb_byte *valaddr,
231  struct ui_file *stream,
232  int recurse,
233  const struct value_print_options *options)
234 {
235  struct gdbarch *gdbarch = get_type_arch (type);
236  CORE_ADDR addr = unpack_pointer (type, valaddr);
237  struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
238 
239  fprintf_filtered (stream, "[");
240  fputs_filtered (paddress (gdbarch, addr), stream);
241  fprintf_filtered (stream, "] : ");
242 
243  if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
244  {
245  struct value *deref_val =
247 
248  common_val_print (deref_val, stream, recurse, options, current_language);
249  }
250  else
251  fputs_filtered ("???", stream);
252 }
253 
254 
255 /* m2_print_array_contents - prints out the contents of an
256  array up to a max_print values.
257  It prints arrays of char as a string
258  and all other data types as comma
259  separated values. */
260 
261 static void
262 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
264  struct ui_file *stream, int recurse,
265  struct value *val,
266  const struct value_print_options *options,
267  int len)
268 {
269  type = check_typedef (type);
270 
271  if (TYPE_LENGTH (type) > 0)
272  {
273  if (options->prettyformat_arrays)
274  print_spaces_filtered (2 + 2 * recurse, stream);
275  /* For an array of chars, print with string syntax. */
276  if (TYPE_LENGTH (type) == 1 &&
279  && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
280  && (options->format == 0 || options->format == 's'))
281  val_print_string (type, NULL, address, len+1, stream, options);
282  else
283  {
284  fprintf_filtered (stream, "{");
286  address, stream, recurse, val,
287  options, 0);
288  fprintf_filtered (stream, "}");
289  }
290  }
291 }
292 
293 /* Decorations for Modula 2. */
294 
296 {
297  "",
298  " + ",
299  " * I",
300  "TRUE",
301  "FALSE",
302  "void",
303  "{",
304  "}"
305 };
306 
307 /* See val_print for a description of the various parameters of this
308  function; they are identical. */
309 
310 void
311 m2_val_print (struct type *type, int embedded_offset,
312  CORE_ADDR address, struct ui_file *stream, int recurse,
313  struct value *original_value,
314  const struct value_print_options *options)
315 {
316  unsigned len;
317  struct type *elttype;
318  CORE_ADDR addr;
319  const gdb_byte *valaddr = value_contents_for_printing (original_value);
320 
321  type = check_typedef (type);
322  switch (TYPE_CODE (type))
323  {
324  case TYPE_CODE_ARRAY:
325  if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
326  {
327  elttype = check_typedef (TYPE_TARGET_TYPE (type));
328  len = TYPE_LENGTH (type) / TYPE_LENGTH (elttype);
329  if (options->prettyformat_arrays)
330  print_spaces_filtered (2 + 2 * recurse, stream);
331  /* For an array of chars, print with string syntax. */
332  if (TYPE_LENGTH (elttype) == 1 &&
333  ((TYPE_CODE (elttype) == TYPE_CODE_INT)
335  && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
336  && (options->format == 0 || options->format == 's'))
337  {
338  /* If requested, look for the first null char and only print
339  elements up to it. */
340  if (options->stop_print_at_null)
341  {
342  unsigned int temp_len;
343 
344  /* Look for a NULL char. */
345  for (temp_len = 0;
346  (valaddr + embedded_offset)[temp_len]
347  && temp_len < len && temp_len < options->print_max;
348  temp_len++);
349  len = temp_len;
350  }
351 
353  valaddr + embedded_offset, len, NULL,
354  0, options);
355  }
356  else
357  {
358  fprintf_filtered (stream, "{");
359  val_print_array_elements (type, embedded_offset,
360  address, stream,
361  recurse, original_value,
362  options, 0);
363  fprintf_filtered (stream, "}");
364  }
365  break;
366  }
367  /* Array of unspecified length: treat like pointer to first elt. */
368  print_unpacked_pointer (type, address, address, options, stream);
369  break;
370 
371  case TYPE_CODE_PTR:
372  if (TYPE_CONST (type))
373  print_variable_at_address (type, valaddr + embedded_offset,
374  stream, recurse, options);
375  else if (options->format && options->format != 's')
376  val_print_scalar_formatted (type, embedded_offset,
377  original_value, options, 0, stream);
378  else
379  {
380  addr = unpack_pointer (type, valaddr + embedded_offset);
381  print_unpacked_pointer (type, addr, address, options, stream);
382  }
383  break;
384 
385  case TYPE_CODE_UNION:
386  if (recurse && !options->unionprint)
387  {
388  fprintf_filtered (stream, "{...}");
389  break;
390  }
391  /* Fall through. */
392  case TYPE_CODE_STRUCT:
393  if (m2_is_long_set (type))
394  m2_print_long_set (type, valaddr, embedded_offset, address,
395  stream);
396  else if (m2_is_unbounded_array (type))
397  m2_print_unbounded_array (type, valaddr, embedded_offset,
398  address, stream, recurse, options);
399  else
400  cp_print_value_fields (type, type, embedded_offset,
401  address, stream, recurse, original_value,
402  options, NULL, 0);
403  break;
404 
405  case TYPE_CODE_SET:
406  elttype = TYPE_INDEX_TYPE (type);
407  elttype = check_typedef (elttype);
408  if (TYPE_STUB (elttype))
409  {
410  fprintf_filtered (stream, _("<incomplete type>"));
411  gdb_flush (stream);
412  break;
413  }
414  else
415  {
416  struct type *range = elttype;
417  LONGEST low_bound, high_bound;
418  int i;
419  int need_comma = 0;
420 
421  fputs_filtered ("{", stream);
422 
423  i = get_discrete_bounds (range, &low_bound, &high_bound);
424  maybe_bad_bstring:
425  if (i < 0)
426  {
427  fputs_filtered (_("<error value>"), stream);
428  goto done;
429  }
430 
431  for (i = low_bound; i <= high_bound; i++)
432  {
433  int element = value_bit_index (type, valaddr + embedded_offset,
434  i);
435 
436  if (element < 0)
437  {
438  i = element;
439  goto maybe_bad_bstring;
440  }
441  if (element)
442  {
443  if (need_comma)
444  fputs_filtered (", ", stream);
445  print_type_scalar (range, i, stream);
446  need_comma = 1;
447 
448  if (i + 1 <= high_bound
449  && value_bit_index (type, valaddr + embedded_offset,
450  ++i))
451  {
452  int j = i;
453 
454  fputs_filtered ("..", stream);
455  while (i + 1 <= high_bound
456  && value_bit_index (type,
457  valaddr + embedded_offset,
458  ++i))
459  j = i;
460  print_type_scalar (range, j, stream);
461  }
462  }
463  }
464  done:
465  fputs_filtered ("}", stream);
466  }
467  break;
468 
469  case TYPE_CODE_RANGE:
471  {
472  m2_val_print (TYPE_TARGET_TYPE (type), embedded_offset,
473  address, stream, recurse, original_value, options);
474  break;
475  }
476  /* FIXME: create_static_range_type does not set the unsigned bit in a
477  range type (I think it probably should copy it from the target
478  type), so we won't print values which are too large to
479  fit in a signed integer correctly. */
480  /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
481  print with the target type, though, because the size of our type
482  and the target type might differ). */
483  /* FALLTHROUGH */
484 
485  case TYPE_CODE_REF:
486  case TYPE_CODE_ENUM:
487  case TYPE_CODE_FUNC:
488  case TYPE_CODE_INT:
489  case TYPE_CODE_FLT:
490  case TYPE_CODE_METHOD:
491  case TYPE_CODE_VOID:
492  case TYPE_CODE_ERROR:
493  case TYPE_CODE_UNDEF:
494  case TYPE_CODE_BOOL:
495  case TYPE_CODE_CHAR:
496  default:
497  generic_val_print (type, embedded_offset, address,
498  stream, recurse, original_value, options,
499  &m2_decorations);
500  break;
501  }
502  gdb_flush (stream);
503 }
static const char * range
Definition: language.c:114
bfd_vma CORE_ADDR
Definition: common-types.h:41
#define TYPE_N_BASECLASSES(thistype)
Definition: gdbtypes.h:1331
#define TYPE_LOW_BOUND(range_type)
Definition: gdbtypes.h:1244
static void print_variable_at_address(struct type *type, const gdb_byte *valaddr, struct ui_file *stream, int recurse, const struct value_print_options *options)
Definition: m2-valprint.c:229
LONGEST embedded_offset
Definition: value.c:309
CORE_ADDR unpack_pointer(struct type *type, const gdb_byte *valaddr)
Definition: value.c:2934
LONGEST value_embedded_offset(const struct value *value)
Definition: value.c:1477
struct value * value_at(struct type *type, CORE_ADDR addr)
Definition: valops.c:933
#define TYPE_HIGH_BOUND(range_type)
Definition: gdbtypes.h:1246
void common_val_print(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language)
Definition: valprint.c:1136
int get_long_set_bounds(struct type *type, LONGEST *low, LONGEST *high)
Definition: m2-valprint.c:49
static int print_unpacked_pointer(struct type *type, CORE_ADDR address, CORE_ADDR addr, const struct value_print_options *options, struct ui_file *stream)
Definition: m2-valprint.c:188
enum language la_language
Definition: language.h:144
#define _(String)
Definition: gdb_locale.h:35
const gdb_byte * value_contents_for_printing(struct value *value)
Definition: value.c:1250
int m2_is_long_set(struct type *type)
Definition: m2-typeprint.c:336
#define TYPE_FIELD_TYPE(thistype, n)
Definition: gdbtypes.h:1371
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: utils.c:2745
static void m2_print_unbounded_array(struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, const struct value_print_options *options)
Definition: m2-valprint.c:160
void val_print_scalar_formatted(struct type *type, LONGEST embedded_offset, struct value *val, const struct value_print_options *options, int size, struct ui_file *stream)
Definition: valprint.c:1240
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
LONGEST unpack_field_as_long(struct type *type, const gdb_byte *valaddr, int fieldno)
Definition: value.c:3318
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
static void m2_print_array_contents(struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *val, const struct value_print_options *options, int len)
Definition: m2-valprint.c:262
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
Definition: gdbtypes.h:749
void m2_val_print(struct type *type, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *original_value, const struct value_print_options *options)
Definition: m2-valprint.c:311
struct gdbarch * get_type_arch(const struct type *type)
Definition: gdbtypes.c:234
static const struct generic_val_print_decorations m2_decorations
Definition: m2-valprint.c:295
struct value * value_at_lazy(struct type *type, CORE_ADDR addr)
Definition: valops.c:944
void print_type_scalar(struct type *type, LONGEST val, struct ui_file *stream)
Definition: typeprint.c:577
Definition: value.c:62
void cp_print_value_fields(struct type *, struct type *, LONGEST, CORE_ADDR, struct ui_file *, int, struct value *, const struct value_print_options *, struct type **, int)
Definition: cp-valprint.c:155
int m2_is_unbounded_array(struct type *type)
Definition: m2-typeprint.c:490
static void m2_print_long_set(struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_file *stream)
Definition: m2-valprint.c:69
int value_bit_index(struct type *type, const gdb_byte *valaddr, int index)
Definition: valarith.c:1714
#define TYPE_FIELD_BITPOS(thistype, n)
Definition: gdbtypes.h:1374
Definition: value.c:169
void print_spaces_filtered(int n, struct ui_file *stream)
Definition: utils.c:2121
bfd_byte gdb_byte
Definition: common-types.h:38
const struct language_defn * current_language
Definition: language.c:81
#define TYPE_TARGET_TYPE(thistype)
Definition: gdbtypes.h:1226
void generic_val_print(struct type *type, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *original_value, const struct value_print_options *options, const struct generic_val_print_decorations *decorations)
Definition: valprint.c:884
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
#define TYPE_INDEX_TYPE(type)
Definition: gdbtypes.h:1242
#define TYPE_NFIELDS(thistype)
Definition: gdbtypes.h:1239
#define TYPE_STUB(t)
Definition: gdbtypes.h:217
struct type * value_type(const struct value *value)
Definition: value.c:1095
#define LA_PRINT_STRING(stream, elttype, string, length, encoding, force_ellipses, options)
Definition: language.h:529
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
int val_print_string(struct type *elttype, const char *encoding, CORE_ADDR addr, int len, struct ui_file *stream, const struct value_print_options *options)
Definition: valprint.c:2795
CORE_ADDR address
Definition: value.c:208
void print_function_pointer_address(const struct value_print_options *options, struct gdbarch *gdbarch, CORE_ADDR address, struct ui_file *stream)
Definition: valprint.c:1884
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:93
void val_print_array_elements(struct type *type, LONGEST embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *val, const struct value_print_options *options, unsigned int i)
Definition: valprint.c:1932
#define TYPE_CONST(t)
Definition: gdbtypes.h:310
int get_discrete_bounds(struct type *type, LONGEST *lowp, LONGEST *highp)
Definition: gdbtypes.c:977
void error(const char *fmt,...)
Definition: errors.c:38
long long LONGEST
Definition: common-types.h:52