f-valprint.c revision 1.9 1 1.1 christos /* Support for printing Fortran values for GDB, the GNU debugger.
2 1.1 christos
3 1.9 christos Copyright (C) 1993-2020 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.9 christos #include "cli/cli-style.h"
37 1.9 christos #include "gdbarch.h"
38 1.1 christos
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.9 christos LONGEST
47 1.1 christos f77_get_lowerbound (struct type *type)
48 1.1 christos {
49 1.9 christos if (type->bounds ()->low.kind () == PROP_UNDEFINED)
50 1.1 christos error (_("Lower bound may not be '*' in F77"));
51 1.1 christos
52 1.9 christos return type->bounds ()->low.const_val ();
53 1.1 christos }
54 1.1 christos
55 1.9 christos LONGEST
56 1.1 christos f77_get_upperbound (struct type *type)
57 1.1 christos {
58 1.9 christos if (type->bounds ()->high.kind () == PROP_UNDEFINED)
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.9 christos return type->bounds ()->high.const_val ();
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.9 christos if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
89 1.9 christos || TYPE_TARGET_TYPE (type)->code () == 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.9 christos struct type *range_type = check_typedef (type)->index_type ();
116 1.6 christos CORE_ADDR addr = address + embedded_offset;
117 1.6 christos LONGEST lowerbound, upperbound;
118 1.9 christos LONGEST 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.9 christos struct gdbarch *gdbarch = get_type_arch (type);
125 1.9 christos size_t dim_size = type_length_units (TYPE_TARGET_TYPE (type));
126 1.9 christos int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
127 1.9 christos size_t byte_stride = type->bit_stride () / (unit_size * 8);
128 1.9 christos if (byte_stride == 0)
129 1.9 christos byte_stride = dim_size;
130 1.6 christos size_t offs = 0;
131 1.6 christos
132 1.6 christos for (i = lowerbound;
133 1.6 christos (i < upperbound + 1 && (*elts) < options->print_max);
134 1.1 christos i++)
135 1.1 christos {
136 1.6 christos struct value *subarray = value_from_contents_and_address
137 1.6 christos (TYPE_TARGET_TYPE (type), value_contents_for_printing_const (val)
138 1.6 christos + offs, addr + offs);
139 1.6 christos
140 1.1 christos fprintf_filtered (stream, "( ");
141 1.6 christos f77_print_array_1 (nss + 1, ndimensions, value_type (subarray),
142 1.6 christos value_contents_for_printing (subarray),
143 1.6 christos value_embedded_offset (subarray),
144 1.6 christos value_address (subarray),
145 1.6 christos stream, recurse, subarray, options, elts);
146 1.9 christos offs += byte_stride;
147 1.1 christos fprintf_filtered (stream, ") ");
148 1.1 christos }
149 1.6 christos if (*elts >= options->print_max && i < upperbound)
150 1.1 christos fprintf_filtered (stream, "...");
151 1.1 christos }
152 1.1 christos else
153 1.1 christos {
154 1.6 christos for (i = lowerbound; i < upperbound + 1 && (*elts) < options->print_max;
155 1.1 christos i++, (*elts)++)
156 1.1 christos {
157 1.6 christos struct value *elt = value_subscript ((struct value *)val, i);
158 1.1 christos
159 1.9 christos common_val_print (elt, stream, recurse, options, current_language);
160 1.6 christos
161 1.6 christos if (i != upperbound)
162 1.1 christos fprintf_filtered (stream, ", ");
163 1.1 christos
164 1.1 christos if ((*elts == options->print_max - 1)
165 1.6 christos && (i != upperbound))
166 1.1 christos fprintf_filtered (stream, "...");
167 1.1 christos }
168 1.1 christos }
169 1.1 christos }
170 1.1 christos
171 1.1 christos /* This function gets called to print an F77 array, we set up some
172 1.1 christos stuff and then immediately call f77_print_array_1(). */
173 1.1 christos
174 1.1 christos static void
175 1.1 christos f77_print_array (struct type *type, const gdb_byte *valaddr,
176 1.1 christos int embedded_offset,
177 1.1 christos CORE_ADDR address, struct ui_file *stream,
178 1.1 christos int recurse,
179 1.1 christos const struct value *val,
180 1.1 christos const struct value_print_options *options)
181 1.1 christos {
182 1.1 christos int ndimensions;
183 1.1 christos int elts = 0;
184 1.1 christos
185 1.1 christos ndimensions = calc_f77_array_dims (type);
186 1.1 christos
187 1.1 christos if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
188 1.1 christos error (_("\
189 1.1 christos Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
190 1.1 christos ndimensions, MAX_FORTRAN_DIMS);
191 1.1 christos
192 1.1 christos f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
193 1.1 christos address, stream, recurse, val, options, &elts);
194 1.1 christos }
195 1.1 christos
196 1.1 christos
198 1.1 christos /* Decorations for Fortran. */
199 1.1 christos
200 1.1 christos static const struct generic_val_print_decorations f_decorations =
201 1.1 christos {
202 1.1 christos "(",
203 1.1 christos ",",
204 1.1 christos ")",
205 1.1 christos ".TRUE.",
206 1.9 christos ".FALSE.",
207 1.6 christos "void",
208 1.6 christos "{",
209 1.1 christos "}"
210 1.1 christos };
211 1.9 christos
212 1.1 christos /* See f-lang.h. */
213 1.1 christos
214 1.9 christos void
215 1.9 christos f_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
216 1.1 christos const struct value_print_options *options)
217 1.9 christos {
218 1.1 christos struct type *type = check_typedef (value_type (val));
219 1.6 christos struct gdbarch *gdbarch = get_type_arch (type);
220 1.1 christos int printed_field = 0; /* Number of fields printed. */
221 1.1 christos struct type *elttype;
222 1.1 christos CORE_ADDR addr;
223 1.9 christos int index;
224 1.9 christos const gdb_byte *valaddr = value_contents_for_printing (val);
225 1.1 christos const CORE_ADDR address = value_address (val);
226 1.9 christos
227 1.1 christos switch (type->code ())
228 1.1 christos {
229 1.1 christos case TYPE_CODE_STRING:
230 1.1 christos f77_get_dynamic_length_of_aggregate (type);
231 1.9 christos LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
232 1.1 christos valaddr, TYPE_LENGTH (type), NULL, 0, options);
233 1.1 christos break;
234 1.1 christos
235 1.9 christos case TYPE_CODE_ARRAY:
236 1.1 christos if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_CHAR)
237 1.1 christos {
238 1.9 christos fprintf_filtered (stream, "(");
239 1.9 christos f77_print_array (type, valaddr, 0,
240 1.1 christos address, stream, recurse, val, options);
241 1.1 christos fprintf_filtered (stream, ")");
242 1.1 christos }
243 1.1 christos else
244 1.1 christos {
245 1.1 christos struct type *ch_type = TYPE_TARGET_TYPE (type);
246 1.1 christos
247 1.9 christos f77_get_dynamic_length_of_aggregate (type);
248 1.1 christos LA_PRINT_STRING (stream, ch_type, valaddr,
249 1.1 christos TYPE_LENGTH (type) / TYPE_LENGTH (ch_type),
250 1.1 christos NULL, 0, options);
251 1.1 christos }
252 1.1 christos break;
253 1.1 christos
254 1.1 christos case TYPE_CODE_PTR:
255 1.1 christos if (options->format && options->format != 's')
256 1.9 christos {
257 1.1 christos value_print_scalar_formatted (val, options, 0, stream);
258 1.1 christos break;
259 1.1 christos }
260 1.1 christos else
261 1.1 christos {
262 1.1 christos int want_space = 0;
263 1.9 christos
264 1.1 christos addr = unpack_pointer (type, valaddr);
265 1.1 christos elttype = check_typedef (TYPE_TARGET_TYPE (type));
266 1.9 christos
267 1.1 christos if (elttype->code () == TYPE_CODE_FUNC)
268 1.1 christos {
269 1.1 christos /* Try to print what function it points to. */
270 1.1 christos print_function_pointer_address (options, gdbarch, addr, stream);
271 1.1 christos return;
272 1.1 christos }
273 1.1 christos
274 1.1 christos if (options->symbol_print)
275 1.1 christos want_space = print_address_demangle (options, gdbarch, addr,
276 1.1 christos stream, demangle);
277 1.1 christos else if (options->addressprint && options->format != 's')
278 1.1 christos {
279 1.1 christos fputs_filtered (paddress (gdbarch, addr), stream);
280 1.1 christos want_space = 1;
281 1.1 christos }
282 1.1 christos
283 1.1 christos /* For a pointer to char or unsigned char, also print the string
284 1.1 christos pointed to, unless pointer is null. */
285 1.9 christos if (TYPE_LENGTH (elttype) == 1
286 1.1 christos && elttype->code () == TYPE_CODE_INT
287 1.1 christos && (options->format == 0 || options->format == 's')
288 1.1 christos && addr != 0)
289 1.1 christos {
290 1.1 christos if (want_space)
291 1.6 christos fputs_filtered (" ", stream);
292 1.6 christos val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
293 1.1 christos stream, options);
294 1.1 christos }
295 1.1 christos return;
296 1.1 christos }
297 1.1 christos break;
298 1.1 christos
299 1.1 christos case TYPE_CODE_INT:
300 1.1 christos if (options->format || options->output_format)
301 1.1 christos {
302 1.1 christos struct value_print_options opts = *options;
303 1.1 christos
304 1.1 christos opts.format = (options->format ? options->format
305 1.9 christos : options->output_format);
306 1.1 christos value_print_scalar_formatted (val, &opts, 0, stream);
307 1.1 christos }
308 1.9 christos else
309 1.1 christos value_print_scalar_formatted (val, options, 0, stream);
310 1.1 christos break;
311 1.1 christos
312 1.1 christos case TYPE_CODE_STRUCT:
313 1.1 christos case TYPE_CODE_UNION:
314 1.1 christos /* Starting from the Fortran 90 standard, Fortran supports derived
315 1.1 christos types. */
316 1.9 christos fprintf_filtered (stream, "( ");
317 1.1 christos for (index = 0; index < type->num_fields (); index++)
318 1.9 christos {
319 1.6 christos struct value *field = value_field (val, index);
320 1.9 christos
321 1.6 christos struct type *field_type = check_typedef (type->field (index).type ());
322 1.6 christos
323 1.9 christos
324 1.6 christos if (field_type->code () != TYPE_CODE_FUNC)
325 1.6 christos {
326 1.6 christos const char *field_name;
327 1.6 christos
328 1.6 christos if (printed_field > 0)
329 1.1 christos fputs_filtered (", ", stream);
330 1.6 christos
331 1.6 christos field_name = TYPE_FIELD_NAME (type, index);
332 1.6 christos if (field_name != NULL)
333 1.9 christos {
334 1.9 christos fputs_styled (field_name, variable_name_style.style (),
335 1.6 christos stream);
336 1.6 christos fputs_filtered (" = ", stream);
337 1.6 christos }
338 1.9 christos
339 1.9 christos common_val_print (field, stream, recurse + 1,
340 1.6 christos options, current_language);
341 1.6 christos
342 1.6 christos ++printed_field;
343 1.6 christos }
344 1.1 christos }
345 1.1 christos fprintf_filtered (stream, " )");
346 1.1 christos break;
347 1.9 christos
348 1.9 christos case TYPE_CODE_BOOL:
349 1.9 christos if (options->format || options->output_format)
350 1.9 christos {
351 1.9 christos struct value_print_options opts = *options;
352 1.9 christos opts.format = (options->format ? options->format
353 1.9 christos : options->output_format);
354 1.9 christos value_print_scalar_formatted (val, &opts, 0, stream);
355 1.9 christos }
356 1.9 christos else
357 1.9 christos {
358 1.9 christos LONGEST longval = value_as_long (val);
359 1.9 christos /* The Fortran standard doesn't specify how logical types are
360 1.9 christos represented. Different compilers use different non zero
361 1.9 christos values to represent logical true. */
362 1.9 christos if (longval == 0)
363 1.9 christos fputs_filtered (f_decorations.false_name, stream);
364 1.9 christos else
365 1.9 christos fputs_filtered (f_decorations.true_name, stream);
366 1.9 christos }
367 1.9 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_CHAR:
379 1.9 christos default:
380 1.1 christos generic_value_print (val, stream, recurse, options, &f_decorations);
381 1.1 christos break;
382 1.1 christos }
383 1.1 christos }
384 1.1 christos
385 1.3 christos static void
386 1.1 christos info_common_command_for_block (const struct block *block, const char *comname,
387 1.1 christos int *any_printed)
388 1.1 christos {
389 1.1 christos struct block_iterator iter;
390 1.1 christos struct symbol *sym;
391 1.1 christos struct value_print_options opts;
392 1.1 christos
393 1.1 christos get_user_print_options (&opts);
394 1.1 christos
395 1.1 christos ALL_BLOCK_SYMBOLS (block, iter, sym)
396 1.1 christos if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
397 1.3 christos {
398 1.1 christos const struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
399 1.1 christos size_t index;
400 1.1 christos
401 1.1 christos gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);
402 1.9 christos
403 1.9 christos if (comname && (!sym->linkage_name ()
404 1.1 christos || strcmp (comname, sym->linkage_name ()) != 0))
405 1.1 christos continue;
406 1.1 christos
407 1.1 christos if (*any_printed)
408 1.1 christos putchar_filtered ('\n');
409 1.1 christos else
410 1.9 christos *any_printed = 1;
411 1.1 christos if (sym->print_name ())
412 1.9 christos printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
413 1.1 christos sym->print_name ());
414 1.1 christos else
415 1.1 christos printf_filtered (_("Contents of blank COMMON block:\n"));
416 1.1 christos
417 1.1 christos for (index = 0; index < common->n_entries; index++)
418 1.1 christos {
419 1.1 christos struct value *val = NULL;
420 1.1 christos
421 1.9 christos printf_filtered ("%s = ",
422 1.1 christos common->contents[index]->print_name ());
423 1.9 christos
424 1.1 christos try
425 1.1 christos {
426 1.1 christos val = value_of_variable (common->contents[index], block);
427 1.1 christos value_print (val, gdb_stdout, &opts);
428 1.1 christos }
429 1.9 christos
430 1.5 christos catch (const gdb_exception_error &except)
431 1.9 christos {
432 1.9 christos fprintf_styled (gdb_stdout, metadata_style.style (),
433 1.9 christos "<error reading variable: %s>",
434 1.5 christos except.what ());
435 1.5 christos }
436 1.1 christos
437 1.1 christos putchar_filtered ('\n');
438 1.1 christos }
439 1.1 christos }
440 1.1 christos }
441 1.1 christos
442 1.1 christos /* This function is used to print out the values in a given COMMON
443 1.1 christos block. It will always use the most local common block of the
444 1.1 christos given name. */
445 1.1 christos
446 1.8 christos static void
447 1.1 christos info_common_command (const char *comname, int from_tty)
448 1.1 christos {
449 1.3 christos struct frame_info *fi;
450 1.1 christos const struct block *block;
451 1.1 christos int values_printed = 0;
452 1.1 christos
453 1.1 christos /* We have been told to display the contents of F77 COMMON
454 1.1 christos block supposedly visible in this function. Let us
455 1.1 christos first make sure that it is visible and if so, let
456 1.1 christos us display its contents. */
457 1.1 christos
458 1.1 christos fi = get_selected_frame (_("No frame selected"));
459 1.1 christos
460 1.1 christos /* The following is generally ripped off from stack.c's routine
461 1.1 christos print_frame_info(). */
462 1.1 christos
463 1.1 christos block = get_frame_block (fi, 0);
464 1.1 christos if (block == NULL)
465 1.1 christos {
466 1.1 christos printf_filtered (_("No symbol table info available.\n"));
467 1.1 christos return;
468 1.1 christos }
469 1.1 christos
470 1.1 christos while (block)
471 1.1 christos {
472 1.1 christos info_common_command_for_block (block, comname, &values_printed);
473 1.1 christos /* After handling the function's top-level block, stop. Don't
474 1.1 christos continue to its superblock, the block of per-file symbols. */
475 1.1 christos if (BLOCK_FUNCTION (block))
476 1.1 christos break;
477 1.1 christos block = BLOCK_SUPERBLOCK (block);
478 1.1 christos }
479 1.1 christos
480 1.1 christos if (!values_printed)
481 1.1 christos {
482 1.1 christos if (comname)
483 1.1 christos printf_filtered (_("No common block '%s'.\n"), comname);
484 1.1 christos else
485 1.1 christos printf_filtered (_("No common blocks.\n"));
486 1.1 christos }
487 1.1 christos }
488 1.9 christos
489 1.1 christos void _initialize_f_valprint ();
490 1.9 christos void
491 1.1 christos _initialize_f_valprint ()
492 1.1 christos {
493 1.1 christos add_info ("common", info_common_command,
494 1.1 christos _("Print out the values contained in a Fortran COMMON block."));
495 }
496