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