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
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
26 #include "expression.h"
37 static int there_is_a_visible_common_named PARAMS ((char *));
40 static void info_common_command PARAMS ((char *, int));
41 static void list_all_visible_commons PARAMS ((char *));
42 static void f77_print_array PARAMS ((struct type *, char *, CORE_ADDR,
43 FILE *, int, int, int,
44 enum val_prettyprint));
45 static void f77_print_array_1 PARAMS ((int, int, struct type *, char *,
46 CORE_ADDR, FILE *, int, int, int,
47 enum val_prettyprint));
48 static void f77_create_arrayprint_offset_tbl PARAMS ((struct type *, FILE *));
49 static void f77_get_dynamic_length_of_aggregate PARAMS ((struct type *));
51 int f77_array_offset_tbl[MAX_FORTRAN_DIMS+1][2];
53 /* Array which holds offsets to be applied to get a row's elements
54 for a given array. Array also holds the size of each subarray. */
56 /* The following macro gives us the size of the nth dimension, Where
59 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
61 /* The following gives us the offset for row n where n is 1-based. */
63 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
66 f77_get_dynamic_lowerbound (type, lower_bound)
70 CORE_ADDR current_frame_addr;
71 CORE_ADDR ptr_to_lower_bound;
73 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
75 case BOUND_BY_VALUE_ON_STACK:
76 current_frame_addr = selected_frame->frame;
77 if (current_frame_addr > 0)
80 read_memory_integer (current_frame_addr +
81 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
86 *lower_bound = DEFAULT_LOWER_BOUND;
87 return BOUND_FETCH_ERROR;
92 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
95 case BOUND_CANNOT_BE_DETERMINED:
96 error ("Lower bound may not be '*' in F77");
99 case BOUND_BY_REF_ON_STACK:
100 current_frame_addr = selected_frame->frame;
101 if (current_frame_addr > 0)
104 read_memory_integer (current_frame_addr +
105 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
107 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
111 *lower_bound = DEFAULT_LOWER_BOUND;
112 return BOUND_FETCH_ERROR;
116 case BOUND_BY_REF_IN_REG:
117 case BOUND_BY_VALUE_IN_REG:
119 error ("??? unhandled dynamic array bound type ???");
122 return BOUND_FETCH_OK;
126 f77_get_dynamic_upperbound (type, upper_bound)
130 CORE_ADDR current_frame_addr = 0;
131 CORE_ADDR ptr_to_upper_bound;
133 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
135 case BOUND_BY_VALUE_ON_STACK:
136 current_frame_addr = selected_frame->frame;
137 if (current_frame_addr > 0)
140 read_memory_integer (current_frame_addr +
141 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
146 *upper_bound = DEFAULT_UPPER_BOUND;
147 return BOUND_FETCH_ERROR;
152 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
155 case BOUND_CANNOT_BE_DETERMINED:
156 /* we have an assumed size array on our hands. Assume that
157 upper_bound == lower_bound so that we show at least
158 1 element.If the user wants to see more elements, let
159 him manually ask for 'em and we'll subscript the
160 array and show him */
161 f77_get_dynamic_lowerbound (type, upper_bound);
164 case BOUND_BY_REF_ON_STACK:
165 current_frame_addr = selected_frame->frame;
166 if (current_frame_addr > 0)
169 read_memory_integer (current_frame_addr +
170 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
172 *upper_bound = read_memory_integer(ptr_to_upper_bound, 4);
176 *upper_bound = DEFAULT_UPPER_BOUND;
177 return BOUND_FETCH_ERROR;
181 case BOUND_BY_REF_IN_REG:
182 case BOUND_BY_VALUE_IN_REG:
184 error ("??? unhandled dynamic array bound type ???");
187 return BOUND_FETCH_OK;
190 /* Obtain F77 adjustable array dimensions */
193 f77_get_dynamic_length_of_aggregate (type)
196 int upper_bound = -1;
200 /* Recursively go all the way down into a possibly multi-dimensional
201 F77 array and get the bounds. For simple arrays, this is pretty
202 easy but when the bounds are dynamic, we must be very careful
203 to add up all the lengths correctly. Not doing this right
204 will lead to horrendous-looking arrays in parameter lists.
206 This function also works for strings which behave very
207 similarly to arrays. */
209 if (TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
210 || TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
211 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
213 /* Recursion ends here, start setting up lengths. */
214 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
215 if (retcode == BOUND_FETCH_ERROR)
216 error ("Cannot obtain valid array lower bound");
218 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
219 if (retcode == BOUND_FETCH_ERROR)
220 error ("Cannot obtain valid array upper bound");
222 /* Patch in a valid length value. */
225 (upper_bound - lower_bound + 1) * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
228 /* Function that sets up the array offset,size table for the array
232 f77_create_arrayprint_offset_tbl (type, stream)
236 struct type *tmp_type;
239 int upper, lower, retcode;
243 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
245 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
246 fprintf_filtered (stream, "<assumed size array> ");
248 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
249 if (retcode == BOUND_FETCH_ERROR)
250 error ("Cannot obtain dynamic upper bound");
252 retcode = f77_get_dynamic_lowerbound(tmp_type,&lower);
253 if (retcode == BOUND_FETCH_ERROR)
254 error("Cannot obtain dynamic lower bound");
256 F77_DIM_SIZE (ndimen) = upper - lower + 1;
258 tmp_type = TYPE_TARGET_TYPE (tmp_type);
262 /* Now we multiply eltlen by all the offsets, so that later we
263 can print out array elements correctly. Up till now we
264 know an offset to apply to get the item but we also
265 have to know how much to add to get to the next item */
268 eltlen = TYPE_LENGTH (tmp_type);
269 F77_DIM_OFFSET (ndimen) = eltlen;
272 eltlen *= F77_DIM_SIZE (ndimen + 1);
273 F77_DIM_OFFSET (ndimen) = eltlen;
277 /* Actual function which prints out F77 arrays, Valaddr == address in
278 the superior. Address == the address in the inferior. */
281 f77_print_array_1 (nss, ndimensions, type, valaddr, address,
282 stream, format, deref_ref, recurse, pretty)
292 enum val_prettyprint pretty;
296 if (nss != ndimensions)
298 for (i = 0; i< F77_DIM_SIZE(nss); i++)
300 fprintf_filtered (stream, "( ");
301 f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
302 valaddr + i * F77_DIM_OFFSET (nss),
303 address + i * F77_DIM_OFFSET (nss),
304 stream, format, deref_ref, recurse, pretty);
305 fprintf_filtered (stream, ") ");
310 for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
312 val_print (TYPE_TARGET_TYPE (type),
313 valaddr + i * F77_DIM_OFFSET (ndimensions),
314 address + i * F77_DIM_OFFSET (ndimensions),
315 stream, format, deref_ref, recurse, pretty);
317 if (i != (F77_DIM_SIZE (nss) - 1))
318 fprintf_filtered (stream, ", ");
320 if (i == print_max - 1)
321 fprintf_filtered (stream, "...");
326 /* This function gets called to print an F77 array, we set up some
327 stuff and then immediately call f77_print_array_1() */
330 f77_print_array (type, valaddr, address, stream, format, deref_ref, recurse,
339 enum val_prettyprint pretty;
343 ndimensions = calc_f77_array_dims (type);
345 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
346 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
347 ndimensions, MAX_FORTRAN_DIMS);
349 /* Since F77 arrays are stored column-major, we set up an
350 offset table to get at the various row's elements. The
351 offset table contains entries for both offset and subarray size. */
353 f77_create_arrayprint_offset_tbl (type, stream);
355 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
356 deref_ref, recurse, pretty);
360 /* Print data of type TYPE located at VALADDR (within GDB), which came from
361 the inferior at address ADDRESS, onto stdio stream STREAM according to
362 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
365 If the data are a string pointer, returns the number of string characters
368 If DEREF_REF is nonzero, then dereference references, otherwise just print
371 The PRETTY parameter controls prettyprinting. */
374 f_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
383 enum val_prettyprint pretty;
385 register unsigned int i = 0; /* Number of characters printed */
386 struct type *elttype;
390 CHECK_TYPEDEF (type);
391 switch (TYPE_CODE (type))
393 case TYPE_CODE_STRING:
394 f77_get_dynamic_length_of_aggregate (type);
395 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 0);
398 case TYPE_CODE_ARRAY:
399 fprintf_filtered (stream, "(");
400 f77_print_array (type, valaddr, address, stream, format,
401 deref_ref, recurse, pretty);
402 fprintf_filtered (stream, ")");
405 /* Array of unspecified length: treat like pointer to first elt. */
406 valaddr = (char *) &address;
410 if (format && format != 's')
412 print_scalar_formatted (valaddr, type, format, 0, stream);
417 addr = unpack_pointer (type, valaddr);
418 elttype = check_typedef (TYPE_TARGET_TYPE (type));
420 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
422 /* Try to print what function it points to. */
423 print_address_demangle (addr, stream, demangle);
424 /* Return value is irrelevant except for string pointers. */
428 if (addressprint && format != 's')
429 fprintf_filtered (stream, "0x%x", addr);
431 /* For a pointer to char or unsigned char, also print the string
432 pointed to, unless pointer is null. */
433 if (TYPE_LENGTH (elttype) == 1
434 && TYPE_CODE (elttype) == TYPE_CODE_INT
435 && (format == 0 || format == 's')
437 i = val_print_string (addr, 0, stream);
439 /* Return number of characters printed, plus one for the
440 terminating null if we have "reached the end". */
441 return (i + (print_max && i != print_max));
448 print_scalar_formatted (valaddr, type, format, 0, stream);
451 /* FIXME, we should consider, at least for ANSI C language, eliminating
452 the distinction made between FUNCs and POINTERs to FUNCs. */
453 fprintf_filtered (stream, "{");
454 type_print (type, "", stream, -1);
455 fprintf_filtered (stream, "} ");
456 /* Try to print what function it points to, and its address. */
457 print_address_demangle (address, stream, demangle);
461 format = format ? format : output_format;
463 print_scalar_formatted (valaddr, type, format, 0, stream);
466 val_print_type_code_int (type, valaddr, stream);
467 /* C and C++ has no single byte int type, char is used instead.
468 Since we don't know whether the value is really intended to
469 be used as an integer or a character, print the character
470 equivalent as well. */
471 if (TYPE_LENGTH (type) == 1)
473 fputs_filtered (" ", stream);
474 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
482 print_scalar_formatted (valaddr, type, format, 0, stream);
484 print_floating (valaddr, type, stream);
488 fprintf_filtered (stream, "VOID");
491 case TYPE_CODE_ERROR:
492 fprintf_filtered (stream, "<error type>");
495 case TYPE_CODE_RANGE:
496 /* FIXME, we should not ever have to print one of these yet. */
497 fprintf_filtered (stream, "<range type>");
501 format = format ? format : output_format;
503 print_scalar_formatted (valaddr, type, format, 0, stream);
507 switch (TYPE_LENGTH(type))
510 val = unpack_long (builtin_type_f_logical_s1, valaddr);
514 val = unpack_long (builtin_type_f_logical_s2, valaddr);
518 val = unpack_long (builtin_type_f_logical, valaddr);
522 error ("Logicals of length %d bytes not supported",
528 fprintf_filtered (stream, ".FALSE.");
531 fprintf_filtered (stream, ".TRUE.");
533 /* Not a legitimate logical type, print as an integer. */
535 /* Bash the type code temporarily. */
536 TYPE_CODE (type) = TYPE_CODE_INT;
537 f_val_print (type, valaddr, address, stream, format,
538 deref_ref, recurse, pretty);
539 /* Restore the type code so later uses work as intended. */
540 TYPE_CODE (type) = TYPE_CODE_BOOL;
545 case TYPE_CODE_COMPLEX:
546 switch (TYPE_LENGTH (type))
548 case 8: type = builtin_type_f_real; break;
549 case 16: type = builtin_type_f_real_s8; break;
550 case 32: type = builtin_type_f_real_s16; break;
552 error ("Cannot print out complex*%d variables", TYPE_LENGTH(type));
554 fputs_filtered ("(", stream);
555 print_floating (valaddr, type, stream);
556 fputs_filtered (",", stream);
557 print_floating (valaddr, type, stream);
558 fputs_filtered (")", stream);
561 case TYPE_CODE_UNDEF:
562 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
563 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
564 and no complete type for struct foo in that file. */
565 fprintf_filtered (stream, "<incomplete type>");
569 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
576 list_all_visible_commons (funname)
579 SAVED_F77_COMMON_PTR tmp;
581 tmp = head_common_list;
583 printf_filtered ("All COMMON blocks visible at this level:\n\n");
587 if (STREQ(tmp->owning_function,funname))
588 printf_filtered ("%s\n", tmp->name);
594 /* This function is used to print out the values in a given COMMON
595 block. It will always use the most local common block of the
599 info_common_command (comname, from_tty)
603 SAVED_F77_COMMON_PTR the_common;
604 COMMON_ENTRY_PTR entry;
605 struct frame_info *fi;
606 register char *funname = 0;
609 /* We have been told to display the contents of F77 COMMON
610 block supposedly visible in this function. Let us
611 first make sure that it is visible and if so, let
612 us display its contents */
617 error ("No frame selected");
619 /* The following is generally ripped off from stack.c's routine
620 print_frame_info() */
622 func = find_pc_function (fi->pc);
625 /* In certain pathological cases, the symtabs give the wrong
626 function (when we are in the first function in a file which
627 is compiled without debugging symbols, the previous function
628 is compiled with debugging symbols, and the "foo.o" symbol
629 that is supposed to tell us where the file with debugging symbols
630 ends has been truncated by ar because it is longer than 15
633 So look in the minimal symbol tables as well, and if it comes
634 up with a larger address for the function use that instead.
635 I don't think this can ever cause any problems; there shouldn't
636 be any minimal symbols in the middle of a function.
637 FIXME: (Not necessarily true. What about text labels) */
639 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
642 && (SYMBOL_VALUE_ADDRESS (msymbol)
643 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
644 funname = SYMBOL_NAME (msymbol);
646 funname = SYMBOL_NAME (func);
650 register struct minimal_symbol *msymbol =
651 lookup_minimal_symbol_by_pc (fi->pc);
654 funname = SYMBOL_NAME (msymbol);
657 /* If comname is NULL, we assume the user wishes to see the
658 which COMMON blocks are visible here and then return */
662 list_all_visible_commons (funname);
666 the_common = find_common_for_function (comname,funname);
670 if (STREQ(comname,BLANK_COMMON_NAME_LOCAL))
671 printf_filtered ("Contents of blank COMMON block:\n");
673 printf_filtered ("Contents of F77 COMMON block '%s':\n",comname);
675 printf_filtered ("\n");
676 entry = the_common->entries;
678 while (entry != NULL)
680 printf_filtered ("%s = ",SYMBOL_NAME(entry->symbol));
681 print_variable_value (entry->symbol,fi,stdout);
682 printf_filtered ("\n");
687 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
691 /* This function is used to determine whether there is a
692 F77 common block visible at the current scope called 'comname'. */
696 there_is_a_visible_common_named (comname)
699 SAVED_F77_COMMON_PTR the_common;
700 struct frame_info *fi;
701 register char *funname = 0;
705 error ("Cannot deal with NULL common name!");
710 error ("No frame selected");
712 /* The following is generally ripped off from stack.c's routine
713 print_frame_info() */
715 func = find_pc_function (fi->pc);
718 /* In certain pathological cases, the symtabs give the wrong
719 function (when we are in the first function in a file which
720 is compiled without debugging symbols, the previous function
721 is compiled with debugging symbols, and the "foo.o" symbol
722 that is supposed to tell us where the file with debugging symbols
723 ends has been truncated by ar because it is longer than 15
726 So look in the minimal symbol tables as well, and if it comes
727 up with a larger address for the function use that instead.
728 I don't think this can ever cause any problems; there shouldn't
729 be any minimal symbols in the middle of a function.
730 FIXME: (Not necessarily true. What about text labels) */
732 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
735 && (SYMBOL_VALUE_ADDRESS (msymbol)
736 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
737 funname = SYMBOL_NAME (msymbol);
739 funname = SYMBOL_NAME (func);
743 register struct minimal_symbol *msymbol =
744 lookup_minimal_symbol_by_pc (fi->pc);
747 funname = SYMBOL_NAME (msymbol);
750 the_common = find_common_for_function (comname, funname);
752 return (the_common ? 1 : 0);
757 _initialize_f_valprint ()
759 add_info ("common", info_common_command,
760 "Print out the values contained in a Fortran COMMON block.");