1 /* Support for printing Fortran values for GDB, the GNU debugger.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
4 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "expression.h"
36 extern struct obstack dont_print_obstack;
38 extern unsigned int print_max; /* No of array elements to print */
40 extern int calc_f77_array_dims PARAMS ((struct type *));
42 int f77_array_offset_tbl[MAX_FORTRAN_DIMS+1][2];
44 /* Array which holds offsets to be applied to get a row's elements
45 for a given array. Array also holds the size of each subarray. */
47 /* The following macro gives us the size of the nth dimension, Where
50 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
52 /* The following gives us the offset for row n where n is 1-based. */
54 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
57 f77_get_dynamic_lowerbound (type, lower_bound)
61 CORE_ADDR current_frame_addr;
62 CORE_ADDR ptr_to_lower_bound;
64 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
66 case BOUND_BY_VALUE_ON_STACK:
67 current_frame_addr = selected_frame->frame;
68 if (current_frame_addr > 0)
71 read_memory_integer (current_frame_addr +
72 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
77 *lower_bound = DEFAULT_LOWER_BOUND;
78 return BOUND_FETCH_ERROR;
83 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
86 case BOUND_CANNOT_BE_DETERMINED:
87 error ("Lower bound may not be '*' in F77");
90 case BOUND_BY_REF_ON_STACK:
91 current_frame_addr = selected_frame->frame;
92 if (current_frame_addr > 0)
95 read_memory_integer (current_frame_addr +
96 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
98 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
102 *lower_bound = DEFAULT_LOWER_BOUND;
103 return BOUND_FETCH_ERROR;
107 case BOUND_BY_REF_IN_REG:
108 case BOUND_BY_VALUE_IN_REG:
110 error ("??? unhandled dynamic array bound type ???");
113 return BOUND_FETCH_OK;
117 f77_get_dynamic_upperbound (type, upper_bound)
121 CORE_ADDR current_frame_addr = 0;
122 CORE_ADDR ptr_to_upper_bound;
124 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
126 case BOUND_BY_VALUE_ON_STACK:
127 current_frame_addr = selected_frame->frame;
128 if (current_frame_addr > 0)
131 read_memory_integer (current_frame_addr +
132 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
137 *upper_bound = DEFAULT_UPPER_BOUND;
138 return BOUND_FETCH_ERROR;
143 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
146 case BOUND_CANNOT_BE_DETERMINED:
147 /* we have an assumed size array on our hands. Assume that
148 upper_bound == lower_bound so that we show at least
149 1 element.If the user wants to see more elements, let
150 him manually ask for 'em and we'll subscript the
151 array and show him */
152 f77_get_dynamic_lowerbound (type, upper_bound);
155 case BOUND_BY_REF_ON_STACK:
156 current_frame_addr = selected_frame->frame;
157 if (current_frame_addr > 0)
160 read_memory_integer (current_frame_addr +
161 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
163 *upper_bound = read_memory_integer(ptr_to_upper_bound, 4);
167 *upper_bound = DEFAULT_UPPER_BOUND;
168 return BOUND_FETCH_ERROR;
172 case BOUND_BY_REF_IN_REG:
173 case BOUND_BY_VALUE_IN_REG:
175 error ("??? unhandled dynamic array bound type ???");
178 return BOUND_FETCH_OK;
181 /* Obtain F77 adjustable array dimensions */
184 f77_get_dynamic_length_of_aggregate (type)
187 int upper_bound = -1;
191 /* Recursively go all the way down into a possibly multi-dimensional
192 F77 array and get the bounds. For simple arrays, this is pretty
193 easy but when the bounds are dynamic, we must be very careful
194 to add up all the lengths correctly. Not doing this right
195 will lead to horrendous-looking arrays in parameter lists.
197 This function also works for strings which behave very
198 similarly to arrays. */
200 if (TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
201 || TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
202 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
204 /* Recursion ends here, start setting up lengths. */
205 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
206 if (retcode == BOUND_FETCH_ERROR)
207 error ("Cannot obtain valid array lower bound");
209 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
210 if (retcode == BOUND_FETCH_ERROR)
211 error ("Cannot obtain valid array upper bound");
213 /* Patch in a valid length value. */
216 (upper_bound - lower_bound + 1) * TYPE_LENGTH (TYPE_TARGET_TYPE (type));
219 /* Function that sets up the array offset,size table for the array
223 f77_create_arrayprint_offset_tbl (type, stream)
227 struct type *tmp_type;
230 int upper, lower, retcode;
234 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
236 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
237 fprintf_filtered (stream, "<assumed size array> ");
239 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
240 if (retcode == BOUND_FETCH_ERROR)
241 error ("Cannot obtain dynamic upper bound");
243 retcode = f77_get_dynamic_lowerbound(tmp_type,&lower);
244 if (retcode == BOUND_FETCH_ERROR)
245 error("Cannot obtain dynamic lower bound");
247 F77_DIM_SIZE (ndimen) = upper - lower + 1;
249 tmp_type = TYPE_TARGET_TYPE (tmp_type);
253 /* Now we multiply eltlen by all the offsets, so that later we
254 can print out array elements correctly. Up till now we
255 know an offset to apply to get the item but we also
256 have to know how much to add to get to the next item */
259 eltlen = TYPE_LENGTH (tmp_type);
260 F77_DIM_OFFSET (ndimen) = eltlen;
263 eltlen *= F77_DIM_SIZE (ndimen + 1);
264 F77_DIM_OFFSET (ndimen) = eltlen;
268 /* Actual function which prints out F77 arrays, Valaddr == address in
269 the superior. Address == the address in the inferior. */
272 f77_print_array_1 (nss, ndimensions, type, valaddr, address,
273 stream, format, deref_ref, recurse, pretty)
283 enum val_prettyprint pretty;
287 if (nss != ndimensions)
289 for (i = 0; i< F77_DIM_SIZE(nss); i++)
291 fprintf_filtered (stream, "( ");
292 f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
293 valaddr + i * F77_DIM_OFFSET (nss),
294 address + i * F77_DIM_OFFSET (nss),
295 stream, format, deref_ref, recurse, pretty, i);
296 fprintf_filtered (stream, ") ");
301 for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
303 val_print (TYPE_TARGET_TYPE (type),
304 valaddr + i * F77_DIM_OFFSET (ndimensions),
305 address + i * F77_DIM_OFFSET (ndimensions),
306 stream, format, deref_ref, recurse, pretty);
308 if (i != (F77_DIM_SIZE (nss) - 1))
309 fprintf_filtered (stream, ", ");
311 if (i == print_max - 1)
312 fprintf_filtered (stream, "...");
317 /* This function gets called to print an F77 array, we set up some
318 stuff and then immediately call f77_print_array_1() */
321 f77_print_array (type, valaddr, address, stream, format, deref_ref, recurse,
330 enum val_prettyprint pretty;
334 ndimensions = calc_f77_array_dims (type);
336 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
337 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
338 ndimensions, MAX_FORTRAN_DIMS);
340 /* Since F77 arrays are stored column-major, we set up an
341 offset table to get at the various row's elements. The
342 offset table contains entries for both offset and subarray size. */
344 f77_create_arrayprint_offset_tbl (type, stream);
346 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
347 deref_ref, recurse, pretty);
351 /* Print data of type TYPE located at VALADDR (within GDB), which came from
352 the inferior at address ADDRESS, onto stdio stream STREAM according to
353 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
356 If the data are a string pointer, returns the number of string characters
359 If DEREF_REF is nonzero, then dereference references, otherwise just print
362 The PRETTY parameter controls prettyprinting. */
365 f_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
374 enum val_prettyprint pretty;
376 register unsigned int i = 0; /* Number of characters printed */
378 struct type *elttype;
384 switch (TYPE_CODE (type))
386 case TYPE_CODE_STRING:
387 f77_get_dynamic_length_of_aggregate (type);
388 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 0);
391 case TYPE_CODE_ARRAY:
392 fprintf_filtered (stream, "(");
393 f77_print_array (type, valaddr, address, stream, format,
394 deref_ref, recurse, pretty);
395 fprintf_filtered (stream, ")");
398 /* Array of unspecified length: treat like pointer to first elt. */
399 valaddr = (char *) &address;
403 if (format && format != 's')
405 print_scalar_formatted (valaddr, type, format, 0, stream);
410 addr = unpack_pointer (type, valaddr);
411 elttype = TYPE_TARGET_TYPE (type);
413 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
415 /* Try to print what function it points to. */
416 print_address_demangle (addr, stream, demangle);
417 /* Return value is irrelevant except for string pointers. */
421 if (addressprint && format != 's')
422 fprintf_filtered (stream, "0x%x", addr);
424 /* For a pointer to char or unsigned char, also print the string
425 pointed to, unless pointer is null. */
426 if (TYPE_LENGTH (elttype) == 1
427 && TYPE_CODE (elttype) == TYPE_CODE_INT
428 && (format == 0 || format == 's')
430 i = val_print_string (addr, 0, stream);
432 /* Return number of characters printed, plus one for the
433 terminating null if we have "reached the end". */
434 return (i + (print_max && i != print_max));
441 print_scalar_formatted (valaddr, type, format, 0, stream);
444 /* FIXME, we should consider, at least for ANSI C language, eliminating
445 the distinction made between FUNCs and POINTERs to FUNCs. */
446 fprintf_filtered (stream, "{");
447 type_print (type, "", stream, -1);
448 fprintf_filtered (stream, "} ");
449 /* Try to print what function it points to, and its address. */
450 print_address_demangle (address, stream, demangle);
454 format = format ? format : output_format;
456 print_scalar_formatted (valaddr, type, format, 0, stream);
459 val_print_type_code_int (type, valaddr, stream);
460 /* C and C++ has no single byte int type, char is used instead.
461 Since we don't know whether the value is really intended to
462 be used as an integer or a character, print the character
463 equivalent as well. */
464 if (TYPE_LENGTH (type) == 1)
466 fputs_filtered (" ", stream);
467 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
475 print_scalar_formatted (valaddr, type, format, 0, stream);
477 print_floating (valaddr, type, stream);
481 fprintf_filtered (stream, "VOID");
484 case TYPE_CODE_ERROR:
485 fprintf_filtered (stream, "<error type>");
488 case TYPE_CODE_RANGE:
489 /* FIXME, we should not ever have to print one of these yet. */
490 fprintf_filtered (stream, "<range type>");
494 format = format ? format : output_format;
496 print_scalar_formatted (valaddr, type, format, 0, stream);
500 switch (TYPE_LENGTH(type))
503 val = unpack_long (builtin_type_f_logical_s1, valaddr);
507 val = unpack_long (builtin_type_f_logical_s2, valaddr);
511 val = unpack_long (builtin_type_f_logical, valaddr);
515 error ("Logicals of length %d bytes not supported",
521 fprintf_filtered (stream, ".FALSE.");
524 fprintf_filtered (stream, ".TRUE.");
526 /* Not a legitimate logical type, print as an integer. */
528 /* Bash the type code temporarily. */
529 TYPE_CODE (type) = TYPE_CODE_INT;
530 f_val_print (type, valaddr, address, stream, format,
531 deref_ref, recurse, pretty);
532 /* Restore the type code so later uses work as intended. */
533 TYPE_CODE (type) = TYPE_CODE_BOOL;
538 case TYPE_CODE_COMPLEX:
539 switch (TYPE_LENGTH (type))
541 case 8: type = builtin_type_f_real; break;
542 case 16: type = builtin_type_f_real_s8; break;
543 case 32: type = builtin_type_f_real_s16; break;
545 error ("Cannot print out complex*%d variables", TYPE_LENGTH(type));
547 fputs_filtered ("(", stream);
548 print_floating (valaddr, type, stream);
549 fputs_filtered (",", stream);
550 print_floating (valaddr, type, stream);
551 fputs_filtered (")", stream);
554 case TYPE_CODE_UNDEF:
555 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
556 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
557 and no complete type for struct foo in that file. */
558 fprintf_filtered (stream, "<incomplete type>");
562 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
569 list_all_visible_commons (funname)
572 SAVED_F77_COMMON_PTR tmp;
574 tmp = head_common_list;
576 printf_filtered ("All COMMON blocks visible at this level:\n\n");
580 if (STREQ(tmp->owning_function,funname))
581 printf_filtered ("%s\n", tmp->name);
587 /* This function is used to print out the values in a given COMMON
588 block. It will always use the most local common block of the
592 info_common_command (comname, from_tty)
596 SAVED_F77_COMMON_PTR the_common;
597 COMMON_ENTRY_PTR entry;
598 struct frame_info *fi;
599 register char *funname = 0;
602 /* We have been told to display the contents of F77 COMMON
603 block supposedly visible in this function. Let us
604 first make sure that it is visible and if so, let
605 us display its contents */
610 error ("No frame selected");
612 /* The following is generally ripped off from stack.c's routine
613 print_frame_info() */
615 func = find_pc_function (fi->pc);
618 /* In certain pathological cases, the symtabs give the wrong
619 function (when we are in the first function in a file which
620 is compiled without debugging symbols, the previous function
621 is compiled with debugging symbols, and the "foo.o" symbol
622 that is supposed to tell us where the file with debugging symbols
623 ends has been truncated by ar because it is longer than 15
626 So look in the minimal symbol tables as well, and if it comes
627 up with a larger address for the function use that instead.
628 I don't think this can ever cause any problems; there shouldn't
629 be any minimal symbols in the middle of a function.
630 FIXME: (Not necessarily true. What about text labels) */
632 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
635 && (SYMBOL_VALUE_ADDRESS (msymbol)
636 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
637 funname = SYMBOL_NAME (msymbol);
639 funname = SYMBOL_NAME (func);
643 register struct minimal_symbol *msymbol =
644 lookup_minimal_symbol_by_pc (fi->pc);
647 funname = SYMBOL_NAME (msymbol);
650 /* If comname is NULL, we assume the user wishes to see the
651 which COMMON blocks are visible here and then return */
655 list_all_visible_commons (funname);
659 the_common = find_common_for_function (comname,funname);
663 if (STREQ(comname,BLANK_COMMON_NAME_LOCAL))
664 printf_filtered ("Contents of blank COMMON block:\n");
666 printf_filtered ("Contents of F77 COMMON block '%s':\n",comname);
668 printf_filtered ("\n");
669 entry = the_common->entries;
671 while (entry != NULL)
673 printf_filtered ("%s = ",SYMBOL_NAME(entry->symbol));
674 print_variable_value (entry->symbol,fi,stdout);
675 printf_filtered ("\n");
680 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
684 /* This function is used to determine whether there is a
685 F77 common block visible at the current scope called 'comname'. */
688 there_is_a_visible_common_named (comname)
691 SAVED_F77_COMMON_PTR the_common;
692 struct frame_info *fi;
693 register char *funname = 0;
697 error ("Cannot deal with NULL common name!");
702 error ("No frame selected");
704 /* The following is generally ripped off from stack.c's routine
705 print_frame_info() */
707 func = find_pc_function (fi->pc);
710 /* In certain pathological cases, the symtabs give the wrong
711 function (when we are in the first function in a file which
712 is compiled without debugging symbols, the previous function
713 is compiled with debugging symbols, and the "foo.o" symbol
714 that is supposed to tell us where the file with debugging symbols
715 ends has been truncated by ar because it is longer than 15
718 So look in the minimal symbol tables as well, and if it comes
719 up with a larger address for the function use that instead.
720 I don't think this can ever cause any problems; there shouldn't
721 be any minimal symbols in the middle of a function.
722 FIXME: (Not necessarily true. What about text labels) */
724 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
727 && (SYMBOL_VALUE_ADDRESS (msymbol)
728 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
729 funname = SYMBOL_NAME (msymbol);
731 funname = SYMBOL_NAME (func);
735 register struct minimal_symbol *msymbol =
736 lookup_minimal_symbol_by_pc (fi->pc);
739 funname = SYMBOL_NAME (msymbol);
742 the_common = find_common_for_function (comname, funname);
744 return (the_common ? 1 : 0);
748 _initialize_f_valprint ()
750 add_info ("common", info_common_command,
751 "Print out the values contained in a Fortran COMMON block.");