1 /* Support for printing Fortran values for GDB, the GNU debugger.
2 Copyright 1993, 1994, 1995 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 unsigned int print_max; /* No of array elements to print */
38 extern int calc_f77_array_dims PARAMS ((struct type *));
40 int f77_array_offset_tbl[MAX_FORTRAN_DIMS+1][2];
42 /* Array which holds offsets to be applied to get a row's elements
43 for a given array. Array also holds the size of each subarray. */
45 /* The following macro gives us the size of the nth dimension, Where
48 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
50 /* The following gives us the offset for row n where n is 1-based. */
52 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
55 f77_get_dynamic_lowerbound (type, lower_bound)
59 CORE_ADDR current_frame_addr;
60 CORE_ADDR ptr_to_lower_bound;
62 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
64 case BOUND_BY_VALUE_ON_STACK:
65 current_frame_addr = selected_frame->frame;
66 if (current_frame_addr > 0)
69 read_memory_integer (current_frame_addr +
70 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
75 *lower_bound = DEFAULT_LOWER_BOUND;
76 return BOUND_FETCH_ERROR;
81 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
84 case BOUND_CANNOT_BE_DETERMINED:
85 error ("Lower bound may not be '*' in F77");
88 case BOUND_BY_REF_ON_STACK:
89 current_frame_addr = selected_frame->frame;
90 if (current_frame_addr > 0)
93 read_memory_integer (current_frame_addr +
94 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
96 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
100 *lower_bound = DEFAULT_LOWER_BOUND;
101 return BOUND_FETCH_ERROR;
105 case BOUND_BY_REF_IN_REG:
106 case BOUND_BY_VALUE_IN_REG:
108 error ("??? unhandled dynamic array bound type ???");
111 return BOUND_FETCH_OK;
115 f77_get_dynamic_upperbound (type, upper_bound)
119 CORE_ADDR current_frame_addr = 0;
120 CORE_ADDR ptr_to_upper_bound;
122 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
124 case BOUND_BY_VALUE_ON_STACK:
125 current_frame_addr = selected_frame->frame;
126 if (current_frame_addr > 0)
129 read_memory_integer (current_frame_addr +
130 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
135 *upper_bound = DEFAULT_UPPER_BOUND;
136 return BOUND_FETCH_ERROR;
141 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
144 case BOUND_CANNOT_BE_DETERMINED:
145 /* we have an assumed size array on our hands. Assume that
146 upper_bound == lower_bound so that we show at least
147 1 element.If the user wants to see more elements, let
148 him manually ask for 'em and we'll subscript the
149 array and show him */
150 f77_get_dynamic_lowerbound (type, upper_bound);
153 case BOUND_BY_REF_ON_STACK:
154 current_frame_addr = selected_frame->frame;
155 if (current_frame_addr > 0)
158 read_memory_integer (current_frame_addr +
159 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
161 *upper_bound = read_memory_integer(ptr_to_upper_bound, 4);
165 *upper_bound = DEFAULT_UPPER_BOUND;
166 return BOUND_FETCH_ERROR;
170 case BOUND_BY_REF_IN_REG:
171 case BOUND_BY_VALUE_IN_REG:
173 error ("??? unhandled dynamic array bound type ???");
176 return BOUND_FETCH_OK;
179 /* Obtain F77 adjustable array dimensions */
182 f77_get_dynamic_length_of_aggregate (type)
185 int upper_bound = -1;
189 /* Recursively go all the way down into a possibly multi-dimensional
190 F77 array and get the bounds. For simple arrays, this is pretty
191 easy but when the bounds are dynamic, we must be very careful
192 to add up all the lengths correctly. Not doing this right
193 will lead to horrendous-looking arrays in parameter lists.
195 This function also works for strings which behave very
196 similarly to arrays. */
198 if (TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
199 || TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
200 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
202 /* Recursion ends here, start setting up lengths. */
203 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
204 if (retcode == BOUND_FETCH_ERROR)
205 error ("Cannot obtain valid array lower bound");
207 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
208 if (retcode == BOUND_FETCH_ERROR)
209 error ("Cannot obtain valid array upper bound");
211 /* Patch in a valid length value. */
214 (upper_bound - lower_bound + 1) * TYPE_LENGTH (TYPE_TARGET_TYPE (type));
217 /* Function that sets up the array offset,size table for the array
221 f77_create_arrayprint_offset_tbl (type, stream)
225 struct type *tmp_type;
228 int upper, lower, retcode;
232 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
234 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
235 fprintf_filtered (stream, "<assumed size array> ");
237 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
238 if (retcode == BOUND_FETCH_ERROR)
239 error ("Cannot obtain dynamic upper bound");
241 retcode = f77_get_dynamic_lowerbound(tmp_type,&lower);
242 if (retcode == BOUND_FETCH_ERROR)
243 error("Cannot obtain dynamic lower bound");
245 F77_DIM_SIZE (ndimen) = upper - lower + 1;
247 tmp_type = TYPE_TARGET_TYPE (tmp_type);
251 /* Now we multiply eltlen by all the offsets, so that later we
252 can print out array elements correctly. Up till now we
253 know an offset to apply to get the item but we also
254 have to know how much to add to get to the next item */
257 eltlen = TYPE_LENGTH (tmp_type);
258 F77_DIM_OFFSET (ndimen) = eltlen;
261 eltlen *= F77_DIM_SIZE (ndimen + 1);
262 F77_DIM_OFFSET (ndimen) = eltlen;
266 /* Actual function which prints out F77 arrays, Valaddr == address in
267 the superior. Address == the address in the inferior. */
270 f77_print_array_1 (nss, ndimensions, type, valaddr, address,
271 stream, format, deref_ref, recurse, pretty)
281 enum val_prettyprint pretty;
285 if (nss != ndimensions)
287 for (i = 0; i< F77_DIM_SIZE(nss); i++)
289 fprintf_filtered (stream, "( ");
290 f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
291 valaddr + i * F77_DIM_OFFSET (nss),
292 address + i * F77_DIM_OFFSET (nss),
293 stream, format, deref_ref, recurse, pretty, i);
294 fprintf_filtered (stream, ") ");
299 for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
301 val_print (TYPE_TARGET_TYPE (type),
302 valaddr + i * F77_DIM_OFFSET (ndimensions),
303 address + i * F77_DIM_OFFSET (ndimensions),
304 stream, format, deref_ref, recurse, pretty);
306 if (i != (F77_DIM_SIZE (nss) - 1))
307 fprintf_filtered (stream, ", ");
309 if (i == print_max - 1)
310 fprintf_filtered (stream, "...");
315 /* This function gets called to print an F77 array, we set up some
316 stuff and then immediately call f77_print_array_1() */
319 f77_print_array (type, valaddr, address, stream, format, deref_ref, recurse,
328 enum val_prettyprint pretty;
332 ndimensions = calc_f77_array_dims (type);
334 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
335 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
336 ndimensions, MAX_FORTRAN_DIMS);
338 /* Since F77 arrays are stored column-major, we set up an
339 offset table to get at the various row's elements. The
340 offset table contains entries for both offset and subarray size. */
342 f77_create_arrayprint_offset_tbl (type, stream);
344 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
345 deref_ref, recurse, pretty);
349 /* Print data of type TYPE located at VALADDR (within GDB), which came from
350 the inferior at address ADDRESS, onto stdio stream STREAM according to
351 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
354 If the data are a string pointer, returns the number of string characters
357 If DEREF_REF is nonzero, then dereference references, otherwise just print
360 The PRETTY parameter controls prettyprinting. */
363 f_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
372 enum val_prettyprint pretty;
374 register unsigned int i = 0; /* Number of characters printed */
376 struct type *elttype;
382 switch (TYPE_CODE (type))
384 case TYPE_CODE_STRING:
385 f77_get_dynamic_length_of_aggregate (type);
386 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 0);
389 case TYPE_CODE_ARRAY:
390 fprintf_filtered (stream, "(");
391 f77_print_array (type, valaddr, address, stream, format,
392 deref_ref, recurse, pretty);
393 fprintf_filtered (stream, ")");
396 /* Array of unspecified length: treat like pointer to first elt. */
397 valaddr = (char *) &address;
401 if (format && format != 's')
403 print_scalar_formatted (valaddr, type, format, 0, stream);
408 addr = unpack_pointer (type, valaddr);
409 elttype = TYPE_TARGET_TYPE (type);
411 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
413 /* Try to print what function it points to. */
414 print_address_demangle (addr, stream, demangle);
415 /* Return value is irrelevant except for string pointers. */
419 if (addressprint && format != 's')
420 fprintf_filtered (stream, "0x%x", addr);
422 /* For a pointer to char or unsigned char, also print the string
423 pointed to, unless pointer is null. */
424 if (TYPE_LENGTH (elttype) == 1
425 && TYPE_CODE (elttype) == TYPE_CODE_INT
426 && (format == 0 || format == 's')
428 i = val_print_string (addr, 0, stream);
430 /* Return number of characters printed, plus one for the
431 terminating null if we have "reached the end". */
432 return (i + (print_max && i != print_max));
439 print_scalar_formatted (valaddr, type, format, 0, stream);
442 /* FIXME, we should consider, at least for ANSI C language, eliminating
443 the distinction made between FUNCs and POINTERs to FUNCs. */
444 fprintf_filtered (stream, "{");
445 type_print (type, "", stream, -1);
446 fprintf_filtered (stream, "} ");
447 /* Try to print what function it points to, and its address. */
448 print_address_demangle (address, stream, demangle);
452 format = format ? format : output_format;
454 print_scalar_formatted (valaddr, type, format, 0, stream);
457 val_print_type_code_int (type, valaddr, stream);
458 /* C and C++ has no single byte int type, char is used instead.
459 Since we don't know whether the value is really intended to
460 be used as an integer or a character, print the character
461 equivalent as well. */
462 if (TYPE_LENGTH (type) == 1)
464 fputs_filtered (" ", stream);
465 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
473 print_scalar_formatted (valaddr, type, format, 0, stream);
475 print_floating (valaddr, type, stream);
479 fprintf_filtered (stream, "VOID");
482 case TYPE_CODE_ERROR:
483 fprintf_filtered (stream, "<error type>");
486 case TYPE_CODE_RANGE:
487 /* FIXME, we should not ever have to print one of these yet. */
488 fprintf_filtered (stream, "<range type>");
492 format = format ? format : output_format;
494 print_scalar_formatted (valaddr, type, format, 0, stream);
498 switch (TYPE_LENGTH(type))
501 val = unpack_long (builtin_type_f_logical_s1, valaddr);
505 val = unpack_long (builtin_type_f_logical_s2, valaddr);
509 val = unpack_long (builtin_type_f_logical, valaddr);
513 error ("Logicals of length %d bytes not supported",
519 fprintf_filtered (stream, ".FALSE.");
522 fprintf_filtered (stream, ".TRUE.");
524 /* Not a legitimate logical type, print as an integer. */
526 /* Bash the type code temporarily. */
527 TYPE_CODE (type) = TYPE_CODE_INT;
528 f_val_print (type, valaddr, address, stream, format,
529 deref_ref, recurse, pretty);
530 /* Restore the type code so later uses work as intended. */
531 TYPE_CODE (type) = TYPE_CODE_BOOL;
536 case TYPE_CODE_COMPLEX:
537 switch (TYPE_LENGTH (type))
539 case 8: type = builtin_type_f_real; break;
540 case 16: type = builtin_type_f_real_s8; break;
541 case 32: type = builtin_type_f_real_s16; break;
543 error ("Cannot print out complex*%d variables", TYPE_LENGTH(type));
545 fputs_filtered ("(", stream);
546 print_floating (valaddr, type, stream);
547 fputs_filtered (",", stream);
548 print_floating (valaddr, type, stream);
549 fputs_filtered (")", stream);
552 case TYPE_CODE_UNDEF:
553 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
554 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
555 and no complete type for struct foo in that file. */
556 fprintf_filtered (stream, "<incomplete type>");
560 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
567 list_all_visible_commons (funname)
570 SAVED_F77_COMMON_PTR tmp;
572 tmp = head_common_list;
574 printf_filtered ("All COMMON blocks visible at this level:\n\n");
578 if (STREQ(tmp->owning_function,funname))
579 printf_filtered ("%s\n", tmp->name);
585 /* This function is used to print out the values in a given COMMON
586 block. It will always use the most local common block of the
590 info_common_command (comname, from_tty)
594 SAVED_F77_COMMON_PTR the_common;
595 COMMON_ENTRY_PTR entry;
596 struct frame_info *fi;
597 register char *funname = 0;
600 /* We have been told to display the contents of F77 COMMON
601 block supposedly visible in this function. Let us
602 first make sure that it is visible and if so, let
603 us display its contents */
608 error ("No frame selected");
610 /* The following is generally ripped off from stack.c's routine
611 print_frame_info() */
613 func = find_pc_function (fi->pc);
616 /* In certain pathological cases, the symtabs give the wrong
617 function (when we are in the first function in a file which
618 is compiled without debugging symbols, the previous function
619 is compiled with debugging symbols, and the "foo.o" symbol
620 that is supposed to tell us where the file with debugging symbols
621 ends has been truncated by ar because it is longer than 15
624 So look in the minimal symbol tables as well, and if it comes
625 up with a larger address for the function use that instead.
626 I don't think this can ever cause any problems; there shouldn't
627 be any minimal symbols in the middle of a function.
628 FIXME: (Not necessarily true. What about text labels) */
630 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
633 && (SYMBOL_VALUE_ADDRESS (msymbol)
634 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
635 funname = SYMBOL_NAME (msymbol);
637 funname = SYMBOL_NAME (func);
641 register struct minimal_symbol *msymbol =
642 lookup_minimal_symbol_by_pc (fi->pc);
645 funname = SYMBOL_NAME (msymbol);
648 /* If comname is NULL, we assume the user wishes to see the
649 which COMMON blocks are visible here and then return */
653 list_all_visible_commons (funname);
657 the_common = find_common_for_function (comname,funname);
661 if (STREQ(comname,BLANK_COMMON_NAME_LOCAL))
662 printf_filtered ("Contents of blank COMMON block:\n");
664 printf_filtered ("Contents of F77 COMMON block '%s':\n",comname);
666 printf_filtered ("\n");
667 entry = the_common->entries;
669 while (entry != NULL)
671 printf_filtered ("%s = ",SYMBOL_NAME(entry->symbol));
672 print_variable_value (entry->symbol,fi,stdout);
673 printf_filtered ("\n");
678 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
682 /* This function is used to determine whether there is a
683 F77 common block visible at the current scope called 'comname'. */
686 there_is_a_visible_common_named (comname)
689 SAVED_F77_COMMON_PTR the_common;
690 struct frame_info *fi;
691 register char *funname = 0;
695 error ("Cannot deal with NULL common name!");
700 error ("No frame selected");
702 /* The following is generally ripped off from stack.c's routine
703 print_frame_info() */
705 func = find_pc_function (fi->pc);
708 /* In certain pathological cases, the symtabs give the wrong
709 function (when we are in the first function in a file which
710 is compiled without debugging symbols, the previous function
711 is compiled with debugging symbols, and the "foo.o" symbol
712 that is supposed to tell us where the file with debugging symbols
713 ends has been truncated by ar because it is longer than 15
716 So look in the minimal symbol tables as well, and if it comes
717 up with a larger address for the function use that instead.
718 I don't think this can ever cause any problems; there shouldn't
719 be any minimal symbols in the middle of a function.
720 FIXME: (Not necessarily true. What about text labels) */
722 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
725 && (SYMBOL_VALUE_ADDRESS (msymbol)
726 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
727 funname = SYMBOL_NAME (msymbol);
729 funname = SYMBOL_NAME (func);
733 register struct minimal_symbol *msymbol =
734 lookup_minimal_symbol_by_pc (fi->pc);
737 funname = SYMBOL_NAME (msymbol);
740 the_common = find_common_for_function (comname, funname);
742 return (the_common ? 1 : 0);
746 _initialize_f_valprint ()
748 add_info ("common", info_common_command,
749 "Print out the values contained in a Fortran COMMON block.");