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. */
25 #include "expression.h"
33 extern struct obstack dont_print_obstack;
35 extern unsigned int print_max; /* No of array elements to print */
37 int f77_array_offset_tbl[MAX_FORTRAN_DIMS+1][2];
39 /* Array which holds offsets to be applied to get a row's elements
40 for a given array. Array also holds the size of each subarray. */
42 /* The following macro gives us the size of the nth dimension, Where
45 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
47 /* The following gives us the offset for row n where n is 1-based. */
49 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
52 f77_get_dynamic_lowerbound (type, lower_bound)
56 CORE_ADDR current_frame_addr;
57 CORE_ADDR ptr_to_lower_bound;
59 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
61 case BOUND_BY_VALUE_ON_STACK:
62 current_frame_addr = selected_frame->frame;
63 if (current_frame_addr > 0)
66 read_memory_integer (current_frame_addr +
67 TYPE_ARRAY_LOWER_BOUND_VALUE (type),4);
71 *lower_bound = DEFAULT_LOWER_BOUND;
72 return BOUND_FETCH_ERROR;
77 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
80 case BOUND_CANNOT_BE_DETERMINED:
81 error("Lower bound may not be '*' in F77");
84 case BOUND_BY_REF_ON_STACK:
85 current_frame_addr = selected_frame->frame;
86 if (current_frame_addr > 0)
89 read_memory_integer (current_frame_addr +
90 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
92 *lower_bound = read_memory_integer(ptr_to_lower_bound);
96 *lower_bound = DEFAULT_LOWER_BOUND;
97 return BOUND_FETCH_ERROR;
101 case BOUND_BY_REF_IN_REG:
102 case BOUND_BY_VALUE_IN_REG:
104 error ("??? unhandled dynamic array bound type ???");
107 return BOUND_FETCH_OK;
111 f77_get_dynamic_upperbound (type, upper_bound)
115 CORE_ADDR current_frame_addr = 0;
116 CORE_ADDR ptr_to_upper_bound;
118 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
120 case BOUND_BY_VALUE_ON_STACK:
121 current_frame_addr = selected_frame->frame;
122 if (current_frame_addr > 0)
125 read_memory_integer (current_frame_addr +
126 TYPE_ARRAY_UPPER_BOUND_VALUE (type),4);
130 *upper_bound = DEFAULT_UPPER_BOUND;
131 return BOUND_FETCH_ERROR;
136 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
139 case BOUND_CANNOT_BE_DETERMINED:
140 /* we have an assumed size array on our hands. Assume that
141 upper_bound == lower_bound so that we show at least
142 1 element.If the user wants to see more elements, let
143 him manually ask for 'em and we'll subscript the
144 array and show him */
145 f77_get_dynamic_lowerbound (type, &upper_bound);
148 case BOUND_BY_REF_ON_STACK:
149 current_frame_addr = selected_frame->frame;
150 if (current_frame_addr > 0)
153 read_memory_integer (current_frame_addr +
154 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
156 *upper_bound = read_memory_integer(ptr_to_upper_bound);
160 *upper_bound = DEFAULT_UPPER_BOUND;
161 return BOUND_FETCH_ERROR;
165 case BOUND_BY_REF_IN_REG:
166 case BOUND_BY_VALUE_IN_REG:
168 error ("??? unhandled dynamic array bound type ???");
171 return BOUND_FETCH_OK;
174 /* Obtain F77 adjustable array dimensions */
177 f77_get_dynamic_length_of_aggregate (type)
180 int upper_bound = -1;
182 unsigned int current_total = 1;
185 /* Recursively go all the way down into a possibly
186 multi-dimensional F77 array
187 and get the bounds. For simple arrays, this is pretty easy
188 but when the bounds are dynamic, we must be very careful
189 to add up all the lengths correctly. Not doing this right
190 will lead to horrendous-looking arrays in parameter lists.
192 This function also works for strings which behave very
193 similarly to arrays. */
195 if (TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
196 || TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
197 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
199 /* Recursion ends here, start setting up lengths. */
200 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
201 if (retcode == BOUND_FETCH_ERROR)
202 error ("Cannot obtain valid array lower bound");
204 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
205 if (retcode == BOUND_FETCH_ERROR)
206 error ("Cannot obtain valid array upper bound");
208 /* Patch in a valid length value. */
211 (upper_bound - lower_bound + 1) * TYPE_LENGTH (TYPE_TARGET_TYPE (type));
214 /* Print a FORTRAN COMPLEX value of type TYPE, pointed to in GDB by VALADDR,
215 on STREAM. which_complex indicates precision, which may be regular,
219 f77_print_cmplx (valaddr, type, stream, which_complex)
229 switch (which_complex)
231 case TARGET_COMPLEX_BIT:
232 f1 = (float *) valaddr;
233 f2 = (float *) (valaddr + sizeof(float));
234 fprintf_filtered (stream, "(%.7e,%.7e)", *f1, *f2);
237 case TARGET_DOUBLE_COMPLEX_BIT:
238 d1 = (double *) valaddr;
239 d2 = (double *) (valaddr + sizeof(double));
240 fprintf_filtered (stream, "(%.16e,%.16e)", *d1, *d2);
243 case TARGET_EXT_COMPLEX_BIT:
244 fprintf_filtered (stream, "<complex*32 format unavailable, "
245 "printing raw data>\n");
247 fprintf_filtered (stream, "( [ ");
250 fprintf_filtered (stream, "0x%x ",
251 * ( (unsigned int *) valaddr+i));
253 fprintf_filtered (stream, "],\n [ ");
256 fprintf_filtered (stream, "0x%x ",
257 * ((unsigned int *) valaddr+i));
259 fprintf_filtered (stream, "] )");
264 fprintf_filtered (stream, "<cannot handle complex of this type>");
269 /* Function that sets up the array offset,size table for the array
273 f77_create_arrayprint_offset_tbl (type, stream)
277 struct type *tmp_type;
280 int upper, lower, retcode;
284 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
286 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
287 fprintf_filtered (stream, "<assumed size array> ");
289 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
290 if (retcode == BOUND_FETCH_ERROR)
291 error ("Cannot obtain dynamic upper bound");
293 retcode = f77_get_dynamic_lowerbound(tmp_type,&lower);
294 if (retcode == BOUND_FETCH_ERROR)
295 error("Cannot obtain dynamic lower bound");
297 F77_DIM_SIZE (ndimen) = upper - lower + 1;
300 F77_DIM_OFFSET (ndimen) = 1;
302 F77_DIM_OFFSET (ndimen) =
303 F77_DIM_OFFSET (ndimen - 1) * F77_DIM_SIZE(ndimen - 1);
305 tmp_type = TYPE_TARGET_TYPE (tmp_type);
309 eltlen = TYPE_LENGTH (tmp_type);
311 /* Now we multiply eltlen by all the offsets, so that later we
312 can print out array elements correctly. Up till now we
313 know an offset to apply to get the item but we also
314 have to know how much to add to get to the next item */
319 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
321 F77_DIM_OFFSET (ndimen) *= eltlen;
323 tmp_type = TYPE_TARGET_TYPE (tmp_type);
327 /* Actual function which prints out F77 arrays, Valaddr == address in
328 the superior. Address == the address in the inferior. */
331 f77_print_array_1 (nss, ndimensions, type, valaddr, address,
332 stream, format, deref_ref, recurse, pretty)
342 enum val_prettyprint pretty;
346 if (nss != ndimensions)
348 for (i = 0; i< F77_DIM_SIZE(nss); i++)
350 fprintf_filtered (stream, "( ");
351 f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
352 valaddr + i * F77_DIM_OFFSET (nss),
353 address + i * F77_DIM_OFFSET (nss),
354 stream, format, deref_ref, recurse, pretty, i);
355 fprintf_filtered (stream, ") ");
360 for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
362 val_print (TYPE_TARGET_TYPE (type),
363 valaddr + i * F77_DIM_OFFSET (ndimensions),
364 address + i * F77_DIM_OFFSET (ndimensions),
365 stream, format, deref_ref, recurse, pretty);
367 if (i != (F77_DIM_SIZE (nss) - 1))
368 fprintf_filtered (stream, ", ");
370 if (i == print_max - 1)
371 fprintf_filtered (stream, "...");
376 /* This function gets called to print an F77 array, we set up some
377 stuff and then immediately call f77_print_array_1() */
380 f77_print_array (type, valaddr, address, stream, format, deref_ref, recurse,
389 enum val_prettyprint pretty;
391 int array_size_array[MAX_FORTRAN_DIMS+1];
394 ndimensions = calc_f77_array_dims (type);
396 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
397 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
398 ndimensions, MAX_FORTRAN_DIMS);
400 /* Since F77 arrays are stored column-major, we set up an
401 offset table to get at the various row's elements. The
402 offset table contains entries for both offset and subarray size. */
404 f77_create_arrayprint_offset_tbl (type, stream);
406 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
407 deref_ref, recurse, pretty);
411 /* Print data of type TYPE located at VALADDR (within GDB), which came from
412 the inferior at address ADDRESS, onto stdio stream STREAM according to
413 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
416 If the data are a string pointer, returns the number of string characters
419 If DEREF_REF is nonzero, then dereference references, otherwise just print
422 The PRETTY parameter controls prettyprinting. */
425 f_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
434 enum val_prettyprint pretty;
436 register unsigned int i = 0; /* Number of characters printed */
438 struct type *elttype;
441 struct internalvar *ivar;
446 switch (TYPE_CODE (type))
448 case TYPE_CODE_LITERAL_STRING:
449 /* It is trivial to print out F77 strings allocated in the
450 superior process. The address field is actually a
451 pointer to the bytes of the literal. For an internalvar,
452 valaddr points to a ptr. which points to
453 VALUE_LITERAL_DATA(value->internalvar->value)
454 and for straight literals (i.e. of the form 'hello world'),
455 valaddr points a ptr to VALUE_LITERAL_DATA(value). */
457 /* First deref. valaddr */
459 addr = * (CORE_ADDR *) valaddr;
463 len = TYPE_LENGTH (type);
464 localstr = alloca (len + 1);
465 strncpy (localstr, addr, len);
466 localstr[len] = '\0';
467 fprintf_filtered (stream, "'%s'", localstr);
470 fprintf_filtered (stream, "Unable to print literal F77 string");
473 /* Strings are a little bit funny. They can be viewed as
474 monolithic arrays that are dealt with as atomic data
475 items. As such they are the only atomic data items whose
476 contents are not located in the superior process. Instead
477 instead of having the actual data, they contain pointers
478 to addresses in the inferior where data is located. Thus
479 instead of using valaddr, we use address. */
481 case TYPE_CODE_STRING:
482 f77_get_dynamic_length_of_aggregate (type);
483 val_print_string (address, TYPE_LENGTH (type), stream);
486 case TYPE_CODE_ARRAY:
487 fprintf_filtered (stream, "(");
488 f77_print_array (type, valaddr, address, stream, format,
489 deref_ref, recurse, pretty);
490 fprintf_filtered (stream, ")");
493 /* Array of unspecified length: treat like pointer to first elt. */
494 valaddr = (char *) &address;
498 if (format && format != 's')
500 print_scalar_formatted (valaddr, type, format, 0, stream);
505 addr = unpack_pointer (type, valaddr);
506 elttype = TYPE_TARGET_TYPE (type);
508 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
510 /* Try to print what function it points to. */
511 print_address_demangle (addr, stream, demangle);
512 /* Return value is irrelevant except for string pointers. */
516 if (addressprint && format != 's')
517 fprintf_filtered (stream, "0x%x", addr);
519 /* For a pointer to char or unsigned char, also print the string
520 pointed to, unless pointer is null. */
521 if (TYPE_LENGTH (elttype) == 1
522 && TYPE_CODE (elttype) == TYPE_CODE_INT
523 && (format == 0 || format == 's')
525 i = val_print_string (addr, 0, stream);
527 /* Return number of characters printed, plus one for the
528 terminating null if we have "reached the end". */
529 return (i + (print_max && i != print_max));
536 print_scalar_formatted (valaddr, type, format, 0, stream);
539 /* FIXME, we should consider, at least for ANSI C language, eliminating
540 the distinction made between FUNCs and POINTERs to FUNCs. */
541 fprintf_filtered (stream, "{");
542 type_print (type, "", stream, -1);
543 fprintf_filtered (stream, "} ");
544 /* Try to print what function it points to, and its address. */
545 print_address_demangle (address, stream, demangle);
549 format = format ? format : output_format;
551 print_scalar_formatted (valaddr, type, format, 0, stream);
554 val_print_type_code_int (type, valaddr, stream);
555 /* C and C++ has no single byte int type, char is used instead.
556 Since we don't know whether the value is really intended to
557 be used as an integer or a character, print the character
558 equivalent as well. */
559 if (TYPE_LENGTH (type) == 1)
561 fputs_filtered (" ", stream);
562 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
570 print_scalar_formatted (valaddr, type, format, 0, stream);
572 print_floating (valaddr, type, stream);
576 fprintf_filtered (stream, "VOID");
579 case TYPE_CODE_ERROR:
580 fprintf_filtered (stream, "<error type>");
583 case TYPE_CODE_RANGE:
584 /* FIXME, we should not ever have to print one of these yet. */
585 fprintf_filtered (stream, "<range type>");
589 format = format ? format : output_format;
591 print_scalar_formatted (valaddr, type, format, 0, stream);
595 switch (TYPE_LENGTH(type))
598 val = unpack_long (builtin_type_f_logical_s1, valaddr);
602 val = unpack_long (builtin_type_f_logical_s2, valaddr);
606 val = unpack_long (builtin_type_f_logical, valaddr);
610 error ("Logicals of length %d bytes not supported",
616 fprintf_filtered (stream, ".FALSE.");
619 fprintf_filtered (stream, ".TRUE.");
621 /* Not a legitimate logical type, print as an integer. */
623 /* Bash the type code temporarily. */
624 TYPE_CODE (type) = TYPE_CODE_INT;
625 f_val_print (type, valaddr, address, stream, format,
626 deref_ref, recurse, pretty);
627 /* Restore the type code so later uses work as intended. */
628 TYPE_CODE (type) = TYPE_CODE_BOOL;
633 case TYPE_CODE_LITERAL_COMPLEX:
634 /* We know that the literal complex is stored in the superior
635 process not the inferior and that it is 16 bytes long.
636 Just like the case above with a literal array, the
637 bytes for the the literal complex number are stored
638 at the address pointed to by valaddr */
640 if (TYPE_LENGTH(type) == 32)
641 error("Cannot currently print out complex*32 literals");
643 /* First deref. valaddr */
645 addr = * (CORE_ADDR *) valaddr;
649 fprintf_filtered (stream, "(");
651 if (TYPE_LENGTH(type) == 16)
653 fprintf_filtered (stream, "%.16f", * (double *) addr);
654 fprintf_filtered (stream, ", %.16f", * (double *)
655 (addr + sizeof(double)));
659 fprintf_filtered (stream, "%.8f", * (float *) addr);
660 fprintf_filtered (stream, ", %.8f", * (float *)
661 (addr + sizeof(float)));
663 fprintf_filtered (stream, ") ");
666 fprintf_filtered (stream, "Unable to print literal F77 array");
669 case TYPE_CODE_COMPLEX:
670 switch (TYPE_LENGTH (type))
673 f77_print_cmplx (valaddr, type, stream, TARGET_COMPLEX_BIT);
677 f77_print_cmplx(valaddr, type, stream, TARGET_DOUBLE_COMPLEX_BIT);
681 f77_print_cmplx(valaddr, type, stream, TARGET_EXT_COMPLEX_BIT);
685 error ("Cannot print out complex*%d variables", TYPE_LENGTH(type));
689 case TYPE_CODE_UNDEF:
690 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
691 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
692 and no complete type for struct foo in that file. */
693 fprintf_filtered (stream, "<incomplete type>");
697 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
704 list_all_visible_commons (funname)
707 SAVED_F77_COMMON_PTR tmp;
709 tmp = head_common_list;
711 printf_filtered ("All COMMON blocks visible at this level:\n\n");
715 if (STREQ(tmp->owning_function,funname))
716 printf_filtered ("%s\n", tmp->name);
722 /* This function is used to print out the values in a given COMMON
723 block. It will always use the most local common block of the
727 info_common_command (comname, from_tty)
731 SAVED_F77_COMMON_PTR the_common;
732 COMMON_ENTRY_PTR entry;
733 struct frame_info *fi;
734 register char *funname = 0;
738 /* We have been told to display the contents of F77 COMMON
739 block supposedly visible in this function. Let us
740 first make sure that it is visible and if so, let
741 us display its contents */
746 error ("No frame selected");
748 /* The following is generally ripped off from stack.c's routine
749 print_frame_info() */
751 func = find_pc_function (fi->pc);
754 /* In certain pathological cases, the symtabs give the wrong
755 function (when we are in the first function in a file which
756 is compiled without debugging symbols, the previous function
757 is compiled with debugging symbols, and the "foo.o" symbol
758 that is supposed to tell us where the file with debugging symbols
759 ends has been truncated by ar because it is longer than 15
762 So look in the minimal symbol tables as well, and if it comes
763 up with a larger address for the function use that instead.
764 I don't think this can ever cause any problems; there shouldn't
765 be any minimal symbols in the middle of a function.
766 FIXME: (Not necessarily true. What about text labels) */
768 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
771 && (SYMBOL_VALUE_ADDRESS (msymbol)
772 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
773 funname = SYMBOL_NAME (msymbol);
775 funname = SYMBOL_NAME (func);
779 register struct minimal_symbol *msymbol =
780 lookup_minimal_symbol_by_pc (fi->pc);
783 funname = SYMBOL_NAME (msymbol);
786 /* If comnname is NULL, we assume the user wishes to see the
787 which COMMON blocks are visible here and then return */
789 if (strlen (comname) == 0)
791 list_all_visible_commons (funname);
795 the_common = find_common_for_function (comname,funname);
799 if (STREQ(comname,BLANK_COMMON_NAME_LOCAL))
800 printf_filtered ("Contents of blank COMMON block:\n");
802 printf_filtered ("Contents of F77 COMMON block '%s':\n",comname);
804 printf_filtered ("\n");
805 entry = the_common->entries;
807 while (entry != NULL)
809 printf_filtered ("%s = ",SYMBOL_NAME(entry->symbol));
810 print_variable_value (entry->symbol,fi,stdout);
811 printf_filtered ("\n");
816 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
820 /* This function is used to determine whether there is a
821 F77 common block visible at the current scope called 'comname'. */
824 there_is_a_visible_common_named (comname)
827 SAVED_F77_COMMON_PTR the_common;
828 COMMON_ENTRY_PTR entry;
829 struct frame_info *fi;
830 register char *funname = 0;
834 error ("Cannot deal with NULL common name!");
839 error ("No frame selected");
841 /* The following is generally ripped off from stack.c's routine
842 print_frame_info() */
844 func = find_pc_function (fi->pc);
847 /* In certain pathological cases, the symtabs give the wrong
848 function (when we are in the first function in a file which
849 is compiled without debugging symbols, the previous function
850 is compiled with debugging symbols, and the "foo.o" symbol
851 that is supposed to tell us where the file with debugging symbols
852 ends has been truncated by ar because it is longer than 15
855 So look in the minimal symbol tables as well, and if it comes
856 up with a larger address for the function use that instead.
857 I don't think this can ever cause any problems; there shouldn't
858 be any minimal symbols in the middle of a function.
859 FIXME: (Not necessarily true. What about text labels) */
861 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
864 && (SYMBOL_VALUE_ADDRESS (msymbol)
865 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
866 funname = SYMBOL_NAME (msymbol);
868 funname = SYMBOL_NAME (func);
872 register struct minimal_symbol *msymbol =
873 lookup_minimal_symbol_by_pc (fi->pc);
876 funname = SYMBOL_NAME (msymbol);
879 the_common = find_common_for_function (comname, funname);
881 return (the_common ? 1 : 0);
885 _initialize_f_valprint ()
887 add_info ("common", info_common_command,
888 "Print out the values contained in a Fortran COMMON block.");