f-valprint.c revision 1.7 1 1.1 christos /* Support for printing Fortran values for GDB, the GNU debugger.
2 1.1 christos
3 1.7 christos Copyright (C) 1993-2017 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Contributed by Motorola. Adapted from the C definitions by Farooq Butt
6 1.1 christos (fmbutt (at) engage.sps.mot.com), additionally worked over by Stan Shebs.
7 1.1 christos
8 1.1 christos This file is part of GDB.
9 1.1 christos
10 1.1 christos This program is free software; you can redistribute it and/or modify
11 1.1 christos it under the terms of the GNU General Public License as published by
12 1.1 christos the Free Software Foundation; either version 3 of the License, or
13 1.1 christos (at your option) any later version.
14 1.1 christos
15 1.1 christos This program is distributed in the hope that it will be useful,
16 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
17 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 1.1 christos GNU General Public License for more details.
19 1.1 christos
20 1.1 christos You should have received a copy of the GNU General Public License
21 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 1.1 christos
23 1.1 christos #include "defs.h"
24 1.1 christos #include "symtab.h"
25 1.1 christos #include "gdbtypes.h"
26 1.1 christos #include "expression.h"
27 1.1 christos #include "value.h"
28 1.1 christos #include "valprint.h"
29 1.1 christos #include "language.h"
30 1.1 christos #include "f-lang.h"
31 1.1 christos #include "frame.h"
32 1.1 christos #include "gdbcore.h"
33 1.1 christos #include "command.h"
34 1.1 christos #include "block.h"
35 1.1 christos #include "dictionary.h"
36 1.1 christos
37 1.1 christos extern void _initialize_f_valprint (void);
38 1.1 christos static void info_common_command (char *, int);
39 1.1 christos static void f77_get_dynamic_length_of_aggregate (struct type *);
40 1.1 christos
41 1.1 christos int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
42 1.1 christos
43 1.1 christos /* Array which holds offsets to be applied to get a row's elements
44 1.1 christos for a given array. Array also holds the size of each subarray. */
45 1.1 christos
46 1.1 christos int
47 1.1 christos f77_get_lowerbound (struct type *type)
48 1.1 christos {
49 1.1 christos if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
50 1.1 christos error (_("Lower bound may not be '*' in F77"));
51 1.1 christos
52 1.1 christos return TYPE_ARRAY_LOWER_BOUND_VALUE (type);
53 1.1 christos }
54 1.1 christos
55 1.1 christos int
56 1.1 christos f77_get_upperbound (struct type *type)
57 1.1 christos {
58 1.1 christos if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
59 1.1 christos {
60 1.1 christos /* We have an assumed size array on our hands. Assume that
61 1.1 christos upper_bound == lower_bound so that we show at least 1 element.
62 1.1 christos If the user wants to see more elements, let him manually ask for 'em
63 1.1 christos and we'll subscript the array and show him. */
64 1.1 christos
65 1.1 christos return f77_get_lowerbound (type);
66 1.1 christos }
67 1.1 christos
68 1.1 christos return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
69 1.1 christos }
70 1.1 christos
71 1.1 christos /* Obtain F77 adjustable array dimensions. */
72 1.1 christos
73 1.1 christos static void
74 1.1 christos f77_get_dynamic_length_of_aggregate (struct type *type)
75 1.1 christos {
76 1.1 christos int upper_bound = -1;
77 1.1 christos int lower_bound = 1;
78 1.1 christos
79 1.1 christos /* Recursively go all the way down into a possibly multi-dimensional
80 1.1 christos F77 array and get the bounds. For simple arrays, this is pretty
81 1.1 christos easy but when the bounds are dynamic, we must be very careful
82 1.1 christos to add up all the lengths correctly. Not doing this right
83 1.1 christos will lead to horrendous-looking arrays in parameter lists.
84 1.1 christos
85 1.1 christos This function also works for strings which behave very
86 1.1 christos similarly to arrays. */
87 1.1 christos
88 1.1 christos if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
89 1.1 christos || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
90 1.1 christos f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
91 1.1 christos
92 1.1 christos /* Recursion ends here, start setting up lengths. */
93 1.1 christos lower_bound = f77_get_lowerbound (type);
94 1.1 christos upper_bound = f77_get_upperbound (type);
95 1.1 christos
96 1.1 christos /* Patch in a valid length value. */
97 1.1 christos
98 1.1 christos TYPE_LENGTH (type) =
99 1.1 christos (upper_bound - lower_bound + 1)
100 1.1 christos * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
101 1.1 christos }
102 1.1 christos
103 1.1 christos /* Actual function which prints out F77 arrays, Valaddr == address in
104 1.1 christos the superior. Address == the address in the inferior. */
105 1.1 christos
106 1.1 christos static void
107 1.1 christos f77_print_array_1 (int nss, int ndimensions, struct type *type,
108 1.1 christos const gdb_byte *valaddr,
109 1.1 christos int embedded_offset, CORE_ADDR address,
110 1.1 christos struct ui_file *stream, int recurse,
111 1.1 christos const struct value *val,
112 1.1 christos const struct value_print_options *options,
113 1.1 christos int *elts)
114 1.1 christos {
115 1.6 christos struct type *range_type = TYPE_INDEX_TYPE (check_typedef (type));
116 1.6 christos CORE_ADDR addr = address + embedded_offset;
117 1.6 christos LONGEST lowerbound, upperbound;
118 1.1 christos int i;
119 1.1 christos
120 1.6 christos get_discrete_bounds (range_type, &lowerbound, &upperbound);
121 1.6 christos
122 1.1 christos if (nss != ndimensions)
123 1.1 christos {
124 1.6 christos size_t dim_size = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
125 1.6 christos size_t offs = 0;
126 1.6 christos
127 1.6 christos for (i = lowerbound;
128 1.6 christos (i < upperbound + 1 && (*elts) < options->print_max);
129 1.1 christos i++)
130 1.1 christos {
131 1.6 christos struct value *subarray = value_from_contents_and_address
132 1.6 christos (TYPE_TARGET_TYPE (type), value_contents_for_printing_const (val)
133 1.6 christos + offs, addr + offs);
134 1.6 christos
135 1.1 christos fprintf_filtered (stream, "( ");
136 1.6 christos f77_print_array_1 (nss + 1, ndimensions, value_type (subarray),
137 1.6 christos value_contents_for_printing (subarray),
138 1.6 christos value_embedded_offset (subarray),
139 1.6 christos value_address (subarray),
140 1.6 christos stream, recurse, subarray, options, elts);
141 1.6 christos offs += dim_size;
142 1.1 christos fprintf_filtered (stream, ") ");
143 1.1 christos }
144 1.6 christos if (*elts >= options->print_max && i < upperbound)
145 1.1 christos fprintf_filtered (stream, "...");
146 1.1 christos }
147 1.1 christos else
148 1.1 christos {
149 1.6 christos for (i = lowerbound; i < upperbound + 1 && (*elts) < options->print_max;
150 1.1 christos i++, (*elts)++)
151 1.1 christos {
152 1.6 christos struct value *elt = value_subscript ((struct value *)val, i);
153 1.1 christos
154 1.6 christos val_print (value_type (elt),
155 1.6 christos value_embedded_offset (elt),
156 1.6 christos value_address (elt), stream, recurse,
157 1.6 christos elt, options, current_language);
158 1.6 christos
159 1.6 christos if (i != upperbound)
160 1.1 christos fprintf_filtered (stream, ", ");
161 1.1 christos
162 1.1 christos if ((*elts == options->print_max - 1)
163 1.6 christos && (i != upperbound))
164 1.1 christos fprintf_filtered (stream, "...");
165 1.1 christos }
166 1.1 christos }
167 1.1 christos }
168 1.1 christos
169 1.1 christos /* This function gets called to print an F77 array, we set up some
170 1.1 christos stuff and then immediately call f77_print_array_1(). */
171 1.1 christos
172 1.1 christos static void
173 1.1 christos f77_print_array (struct type *type, const gdb_byte *valaddr,
174 1.1 christos int embedded_offset,
175 1.1 christos CORE_ADDR address, struct ui_file *stream,
176 1.1 christos int recurse,
177 1.1 christos const struct value *val,
178 1.1 christos const struct value_print_options *options)
179 1.1 christos {
180 1.1 christos int ndimensions;
181 1.1 christos int elts = 0;
182 1.1 christos
183 1.1 christos ndimensions = calc_f77_array_dims (type);
184 1.1 christos
185 1.1 christos if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
186 1.1 christos error (_("\
187 1.1 christos Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
188 1.1 christos ndimensions, MAX_FORTRAN_DIMS);
189 1.1 christos
190 1.1 christos f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
191 1.1 christos address, stream, recurse, val, options, &elts);
192 1.1 christos }
193 1.1 christos
194 1.1 christos
196 1.1 christos /* Decorations for Fortran. */
197 1.1 christos
198 1.1 christos static const struct generic_val_print_decorations f_decorations =
199 1.1 christos {
200 1.1 christos "(",
201 1.1 christos ",",
202 1.1 christos ")",
203 1.1 christos ".TRUE.",
204 1.1 christos ".FALSE.",
205 1.6 christos "VOID",
206 1.6 christos "{",
207 1.1 christos "}"
208 1.1 christos };
209 1.1 christos
210 1.1 christos /* See val_print for a description of the various parameters of this
211 1.1 christos function; they are identical. */
212 1.1 christos
213 1.7 christos void
214 1.1 christos f_val_print (struct type *type, int embedded_offset,
215 1.7 christos CORE_ADDR address, struct ui_file *stream, int recurse,
216 1.1 christos struct value *original_value,
217 1.1 christos const struct value_print_options *options)
218 1.1 christos {
219 1.1 christos struct gdbarch *gdbarch = get_type_arch (type);
220 1.6 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
221 1.1 christos int printed_field = 0; /* Number of fields printed. */
222 1.1 christos struct type *elttype;
223 1.1 christos CORE_ADDR addr;
224 1.7 christos int index;
225 1.1 christos const gdb_byte *valaddr =value_contents_for_printing (original_value);
226 1.6 christos
227 1.1 christos type = check_typedef (type);
228 1.1 christos switch (TYPE_CODE (type))
229 1.1 christos {
230 1.1 christos case TYPE_CODE_STRING:
231 1.1 christos f77_get_dynamic_length_of_aggregate (type);
232 1.1 christos LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
233 1.1 christos valaddr + embedded_offset,
234 1.1 christos TYPE_LENGTH (type), NULL, 0, options);
235 1.1 christos break;
236 1.1 christos
237 1.1 christos case TYPE_CODE_ARRAY:
238 1.1 christos if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_CHAR)
239 1.1 christos {
240 1.1 christos fprintf_filtered (stream, "(");
241 1.1 christos f77_print_array (type, valaddr, embedded_offset,
242 1.1 christos address, stream, recurse, original_value, options);
243 1.1 christos fprintf_filtered (stream, ")");
244 1.1 christos }
245 1.1 christos else
246 1.1 christos {
247 1.1 christos struct type *ch_type = TYPE_TARGET_TYPE (type);
248 1.1 christos
249 1.1 christos f77_get_dynamic_length_of_aggregate (type);
250 1.1 christos LA_PRINT_STRING (stream, ch_type,
251 1.1 christos valaddr + embedded_offset,
252 1.1 christos TYPE_LENGTH (type) / TYPE_LENGTH (ch_type),
253 1.1 christos NULL, 0, options);
254 1.1 christos }
255 1.1 christos break;
256 1.1 christos
257 1.1 christos case TYPE_CODE_PTR:
258 1.1 christos if (options->format && options->format != 's')
259 1.7 christos {
260 1.1 christos val_print_scalar_formatted (type, embedded_offset,
261 1.1 christos original_value, options, 0, stream);
262 1.1 christos break;
263 1.1 christos }
264 1.1 christos else
265 1.1 christos {
266 1.1 christos int want_space = 0;
267 1.1 christos
268 1.1 christos addr = unpack_pointer (type, valaddr + embedded_offset);
269 1.1 christos elttype = check_typedef (TYPE_TARGET_TYPE (type));
270 1.1 christos
271 1.1 christos if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
272 1.1 christos {
273 1.1 christos /* Try to print what function it points to. */
274 1.1 christos print_function_pointer_address (options, gdbarch, addr, stream);
275 1.1 christos return;
276 1.1 christos }
277 1.1 christos
278 1.1 christos if (options->symbol_print)
279 1.1 christos want_space = print_address_demangle (options, gdbarch, addr,
280 1.1 christos stream, demangle);
281 1.1 christos else if (options->addressprint && options->format != 's')
282 1.1 christos {
283 1.1 christos fputs_filtered (paddress (gdbarch, addr), stream);
284 1.1 christos want_space = 1;
285 1.1 christos }
286 1.1 christos
287 1.1 christos /* For a pointer to char or unsigned char, also print the string
288 1.1 christos pointed to, unless pointer is null. */
289 1.1 christos if (TYPE_LENGTH (elttype) == 1
290 1.1 christos && TYPE_CODE (elttype) == TYPE_CODE_INT
291 1.1 christos && (options->format == 0 || options->format == 's')
292 1.1 christos && addr != 0)
293 1.1 christos {
294 1.1 christos if (want_space)
295 1.6 christos fputs_filtered (" ", stream);
296 1.6 christos val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
297 1.1 christos stream, options);
298 1.1 christos }
299 1.1 christos return;
300 1.1 christos }
301 1.1 christos break;
302 1.1 christos
303 1.1 christos case TYPE_CODE_INT:
304 1.1 christos if (options->format || options->output_format)
305 1.1 christos {
306 1.1 christos struct value_print_options opts = *options;
307 1.1 christos
308 1.1 christos opts.format = (options->format ? options->format
309 1.7 christos : options->output_format);
310 1.3 christos val_print_scalar_formatted (type, embedded_offset,
311 1.1 christos original_value, &opts, 0, stream);
312 1.1 christos }
313 1.1 christos else
314 1.1 christos {
315 1.1 christos val_print_type_code_int (type, valaddr + embedded_offset, stream);
316 1.1 christos /* C and C++ has no single byte int type, char is used instead.
317 1.1 christos Since we don't know whether the value is really intended to
318 1.1 christos be used as an integer or a character, print the character
319 1.1 christos equivalent as well. */
320 1.1 christos if (TYPE_LENGTH (type) == 1)
321 1.1 christos {
322 1.1 christos LONGEST c;
323 1.1 christos
324 1.1 christos fputs_filtered (" ", stream);
325 1.1 christos c = unpack_long (type, valaddr + embedded_offset);
326 1.1 christos LA_PRINT_CHAR ((unsigned char) c, type, stream);
327 1.1 christos }
328 1.1 christos }
329 1.1 christos break;
330 1.1 christos
331 1.1 christos case TYPE_CODE_STRUCT:
332 1.1 christos case TYPE_CODE_UNION:
333 1.1 christos /* Starting from the Fortran 90 standard, Fortran supports derived
334 1.1 christos types. */
335 1.1 christos fprintf_filtered (stream, "( ");
336 1.1 christos for (index = 0; index < TYPE_NFIELDS (type); index++)
337 1.6 christos {
338 1.6 christos struct value *field = value_field
339 1.6 christos ((struct value *)original_value, index);
340 1.6 christos
341 1.6 christos struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, index));
342 1.6 christos
343 1.6 christos
344 1.6 christos if (TYPE_CODE (field_type) != TYPE_CODE_FUNC)
345 1.6 christos {
346 1.6 christos const char *field_name;
347 1.6 christos
348 1.6 christos if (printed_field > 0)
349 1.1 christos fputs_filtered (", ", stream);
350 1.6 christos
351 1.6 christos field_name = TYPE_FIELD_NAME (type, index);
352 1.6 christos if (field_name != NULL)
353 1.6 christos {
354 1.6 christos fputs_filtered (field_name, stream);
355 1.6 christos fputs_filtered (" = ", stream);
356 1.6 christos }
357 1.6 christos
358 1.6 christos val_print (value_type (field),
359 1.6 christos value_embedded_offset (field),
360 1.6 christos value_address (field), stream, recurse + 1,
361 1.6 christos field, options, current_language);
362 1.6 christos
363 1.6 christos ++printed_field;
364 1.6 christos }
365 1.1 christos }
366 1.1 christos fprintf_filtered (stream, " )");
367 1.1 christos break;
368 1.1 christos
369 1.1 christos case TYPE_CODE_REF:
370 1.1 christos case TYPE_CODE_FUNC:
371 1.1 christos case TYPE_CODE_FLAGS:
372 1.1 christos case TYPE_CODE_FLT:
373 1.1 christos case TYPE_CODE_VOID:
374 1.1 christos case TYPE_CODE_ERROR:
375 1.1 christos case TYPE_CODE_RANGE:
376 1.1 christos case TYPE_CODE_UNDEF:
377 1.1 christos case TYPE_CODE_COMPLEX:
378 1.1 christos case TYPE_CODE_BOOL:
379 1.1 christos case TYPE_CODE_CHAR:
380 1.7 christos default:
381 1.1 christos generic_val_print (type, embedded_offset, address,
382 1.1 christos stream, recurse, original_value, options,
383 1.1 christos &f_decorations);
384 1.1 christos break;
385 1.1 christos }
386 1.1 christos gdb_flush (stream);
387 1.1 christos }
388 1.1 christos
389 1.3 christos static void
390 1.1 christos info_common_command_for_block (const struct block *block, const char *comname,
391 1.1 christos int *any_printed)
392 1.1 christos {
393 1.1 christos struct block_iterator iter;
394 1.1 christos struct symbol *sym;
395 1.1 christos const char *name;
396 1.1 christos struct value_print_options opts;
397 1.1 christos
398 1.1 christos get_user_print_options (&opts);
399 1.1 christos
400 1.1 christos ALL_BLOCK_SYMBOLS (block, iter, sym)
401 1.1 christos if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
402 1.3 christos {
403 1.1 christos const struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
404 1.1 christos size_t index;
405 1.1 christos
406 1.1 christos gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);
407 1.1 christos
408 1.1 christos if (comname && (!SYMBOL_LINKAGE_NAME (sym)
409 1.1 christos || strcmp (comname, SYMBOL_LINKAGE_NAME (sym)) != 0))
410 1.1 christos continue;
411 1.1 christos
412 1.1 christos if (*any_printed)
413 1.1 christos putchar_filtered ('\n');
414 1.1 christos else
415 1.1 christos *any_printed = 1;
416 1.1 christos if (SYMBOL_PRINT_NAME (sym))
417 1.1 christos printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
418 1.1 christos SYMBOL_PRINT_NAME (sym));
419 1.1 christos else
420 1.1 christos printf_filtered (_("Contents of blank COMMON block:\n"));
421 1.1 christos
422 1.1 christos for (index = 0; index < common->n_entries; index++)
423 1.1 christos {
424 1.1 christos struct value *val = NULL;
425 1.1 christos
426 1.1 christos printf_filtered ("%s = ",
427 1.1 christos SYMBOL_PRINT_NAME (common->contents[index]));
428 1.5 christos
429 1.1 christos TRY
430 1.1 christos {
431 1.1 christos val = value_of_variable (common->contents[index], block);
432 1.1 christos value_print (val, gdb_stdout, &opts);
433 1.1 christos }
434 1.5 christos
435 1.5 christos CATCH (except, RETURN_MASK_ERROR)
436 1.5 christos {
437 1.5 christos printf_filtered ("<error reading variable: %s>", except.message);
438 1.5 christos }
439 1.5 christos END_CATCH
440 1.1 christos
441 1.1 christos putchar_filtered ('\n');
442 1.1 christos }
443 1.1 christos }
444 1.1 christos }
445 1.1 christos
446 1.1 christos /* This function is used to print out the values in a given COMMON
447 1.1 christos block. It will always use the most local common block of the
448 1.1 christos given name. */
449 1.1 christos
450 1.1 christos static void
451 1.1 christos info_common_command (char *comname, int from_tty)
452 1.1 christos {
453 1.3 christos struct frame_info *fi;
454 1.1 christos const struct block *block;
455 1.1 christos int values_printed = 0;
456 1.1 christos
457 1.1 christos /* We have been told to display the contents of F77 COMMON
458 1.1 christos block supposedly visible in this function. Let us
459 1.1 christos first make sure that it is visible and if so, let
460 1.1 christos us display its contents. */
461 1.1 christos
462 1.1 christos fi = get_selected_frame (_("No frame selected"));
463 1.1 christos
464 1.1 christos /* The following is generally ripped off from stack.c's routine
465 1.1 christos print_frame_info(). */
466 1.1 christos
467 1.1 christos block = get_frame_block (fi, 0);
468 1.1 christos if (block == NULL)
469 1.1 christos {
470 1.1 christos printf_filtered (_("No symbol table info available.\n"));
471 1.1 christos return;
472 1.1 christos }
473 1.1 christos
474 1.1 christos while (block)
475 1.1 christos {
476 1.1 christos info_common_command_for_block (block, comname, &values_printed);
477 1.1 christos /* After handling the function's top-level block, stop. Don't
478 1.1 christos continue to its superblock, the block of per-file symbols. */
479 1.1 christos if (BLOCK_FUNCTION (block))
480 1.1 christos break;
481 1.1 christos block = BLOCK_SUPERBLOCK (block);
482 1.1 christos }
483 1.1 christos
484 1.1 christos if (!values_printed)
485 1.1 christos {
486 1.1 christos if (comname)
487 1.1 christos printf_filtered (_("No common block '%s'.\n"), comname);
488 1.1 christos else
489 1.1 christos printf_filtered (_("No common blocks.\n"));
490 1.1 christos }
491 1.1 christos }
492 1.1 christos
493 1.1 christos void
494 1.1 christos _initialize_f_valprint (void)
495 1.1 christos {
496 1.1 christos add_info ("common", info_common_command,
497 1.1 christos _("Print out the values contained in a Fortran COMMON block."));
498 }
499