1 /* Support for printing Fortran values for GDB, the GNU debugger.
3 Copyright (C) 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2003, 2005, 2006
4 Free Software Foundation, Inc.
6 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
7 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
27 #include "gdb_string.h"
30 #include "expression.h"
41 static int there_is_a_visible_common_named (char *);
44 extern void _initialize_f_valprint (void);
45 static void info_common_command (char *, int);
46 static void list_all_visible_commons (char *);
47 static void f77_create_arrayprint_offset_tbl (struct type
*,
49 static void f77_get_dynamic_length_of_aggregate (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 (struct type
*type
, int *lower_bound
)
68 CORE_ADDR current_frame_addr
;
69 CORE_ADDR ptr_to_lower_bound
;
71 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type
))
73 case BOUND_BY_VALUE_ON_STACK
:
74 current_frame_addr
= get_frame_base (deprecated_selected_frame
);
75 if (current_frame_addr
> 0)
78 read_memory_integer (current_frame_addr
+
79 TYPE_ARRAY_LOWER_BOUND_VALUE (type
),
84 *lower_bound
= DEFAULT_LOWER_BOUND
;
85 return BOUND_FETCH_ERROR
;
90 *lower_bound
= TYPE_ARRAY_LOWER_BOUND_VALUE (type
);
93 case BOUND_CANNOT_BE_DETERMINED
:
94 error (_("Lower bound may not be '*' in F77"));
97 case BOUND_BY_REF_ON_STACK
:
98 current_frame_addr
= get_frame_base (deprecated_selected_frame
);
99 if (current_frame_addr
> 0)
102 read_memory_typed_address (current_frame_addr
+
103 TYPE_ARRAY_LOWER_BOUND_VALUE (type
),
104 builtin_type_void_data_ptr
);
105 *lower_bound
= read_memory_integer (ptr_to_lower_bound
, 4);
109 *lower_bound
= DEFAULT_LOWER_BOUND
;
110 return BOUND_FETCH_ERROR
;
114 case BOUND_BY_REF_IN_REG
:
115 case BOUND_BY_VALUE_IN_REG
:
117 error (_("??? unhandled dynamic array bound type ???"));
120 return BOUND_FETCH_OK
;
124 f77_get_dynamic_upperbound (struct type
*type
, int *upper_bound
)
126 CORE_ADDR current_frame_addr
= 0;
127 CORE_ADDR ptr_to_upper_bound
;
129 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type
))
131 case BOUND_BY_VALUE_ON_STACK
:
132 current_frame_addr
= get_frame_base (deprecated_selected_frame
);
133 if (current_frame_addr
> 0)
136 read_memory_integer (current_frame_addr
+
137 TYPE_ARRAY_UPPER_BOUND_VALUE (type
),
142 *upper_bound
= DEFAULT_UPPER_BOUND
;
143 return BOUND_FETCH_ERROR
;
148 *upper_bound
= TYPE_ARRAY_UPPER_BOUND_VALUE (type
);
151 case BOUND_CANNOT_BE_DETERMINED
:
152 /* we have an assumed size array on our hands. Assume that
153 upper_bound == lower_bound so that we show at least
154 1 element.If the user wants to see more elements, let
155 him manually ask for 'em and we'll subscript the
156 array and show him */
157 f77_get_dynamic_lowerbound (type
, upper_bound
);
160 case BOUND_BY_REF_ON_STACK
:
161 current_frame_addr
= get_frame_base (deprecated_selected_frame
);
162 if (current_frame_addr
> 0)
165 read_memory_typed_address (current_frame_addr
+
166 TYPE_ARRAY_UPPER_BOUND_VALUE (type
),
167 builtin_type_void_data_ptr
);
168 *upper_bound
= read_memory_integer (ptr_to_upper_bound
, 4);
172 *upper_bound
= DEFAULT_UPPER_BOUND
;
173 return BOUND_FETCH_ERROR
;
177 case BOUND_BY_REF_IN_REG
:
178 case BOUND_BY_VALUE_IN_REG
:
180 error (_("??? unhandled dynamic array bound type ???"));
183 return BOUND_FETCH_OK
;
186 /* Obtain F77 adjustable array dimensions */
189 f77_get_dynamic_length_of_aggregate (struct type
*type
)
191 int upper_bound
= -1;
195 /* Recursively go all the way down into a possibly multi-dimensional
196 F77 array and get the bounds. For simple arrays, this is pretty
197 easy but when the bounds are dynamic, we must be very careful
198 to add up all the lengths correctly. Not doing this right
199 will lead to horrendous-looking arrays in parameter lists.
201 This function also works for strings which behave very
202 similarly to arrays. */
204 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
205 || TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRING
)
206 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type
));
208 /* Recursion ends here, start setting up lengths. */
209 retcode
= f77_get_dynamic_lowerbound (type
, &lower_bound
);
210 if (retcode
== BOUND_FETCH_ERROR
)
211 error (_("Cannot obtain valid array lower bound"));
213 retcode
= f77_get_dynamic_upperbound (type
, &upper_bound
);
214 if (retcode
== BOUND_FETCH_ERROR
)
215 error (_("Cannot obtain valid array upper bound"));
217 /* Patch in a valid length value. */
220 (upper_bound
- lower_bound
+ 1) * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type
)));
223 /* Function that sets up the array offset,size table for the array
227 f77_create_arrayprint_offset_tbl (struct type
*type
, struct ui_file
*stream
)
229 struct type
*tmp_type
;
232 int upper
, lower
, retcode
;
236 while ((TYPE_CODE (tmp_type
) == TYPE_CODE_ARRAY
))
238 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type
) == BOUND_CANNOT_BE_DETERMINED
)
239 fprintf_filtered (stream
, "<assumed size array> ");
241 retcode
= f77_get_dynamic_upperbound (tmp_type
, &upper
);
242 if (retcode
== BOUND_FETCH_ERROR
)
243 error (_("Cannot obtain dynamic upper bound"));
245 retcode
= f77_get_dynamic_lowerbound (tmp_type
, &lower
);
246 if (retcode
== BOUND_FETCH_ERROR
)
247 error (_("Cannot obtain dynamic lower bound"));
249 F77_DIM_SIZE (ndimen
) = upper
- lower
+ 1;
251 tmp_type
= TYPE_TARGET_TYPE (tmp_type
);
255 /* Now we multiply eltlen by all the offsets, so that later we
256 can print out array elements correctly. Up till now we
257 know an offset to apply to get the item but we also
258 have to know how much to add to get to the next item */
261 eltlen
= TYPE_LENGTH (tmp_type
);
262 F77_DIM_OFFSET (ndimen
) = eltlen
;
265 eltlen
*= F77_DIM_SIZE (ndimen
+ 1);
266 F77_DIM_OFFSET (ndimen
) = eltlen
;
272 /* Actual function which prints out F77 arrays, Valaddr == address in
273 the superior. Address == the address in the inferior. */
276 f77_print_array_1 (int nss
, int ndimensions
, struct type
*type
,
277 const gdb_byte
*valaddr
, CORE_ADDR address
,
278 struct ui_file
*stream
, int format
,
279 int deref_ref
, int recurse
, enum val_prettyprint pretty
,
284 if (nss
!= ndimensions
)
286 for (i
= 0; (i
< F77_DIM_SIZE (nss
) && (*elts
) < print_max
); i
++)
288 fprintf_filtered (stream
, "( ");
289 f77_print_array_1 (nss
+ 1, ndimensions
, TYPE_TARGET_TYPE (type
),
290 valaddr
+ i
* F77_DIM_OFFSET (nss
),
291 address
+ i
* F77_DIM_OFFSET (nss
),
292 stream
, format
, deref_ref
, recurse
, pretty
, elts
);
293 fprintf_filtered (stream
, ") ");
295 if (*elts
>= print_max
&& i
< F77_DIM_SIZE (nss
))
296 fprintf_filtered (stream
, "...");
300 for (i
= 0; i
< F77_DIM_SIZE (nss
) && (*elts
) < print_max
;
303 val_print (TYPE_TARGET_TYPE (type
),
304 valaddr
+ i
* F77_DIM_OFFSET (ndimensions
),
306 address
+ i
* F77_DIM_OFFSET (ndimensions
),
307 stream
, format
, deref_ref
, recurse
, pretty
);
309 if (i
!= (F77_DIM_SIZE (nss
) - 1))
310 fprintf_filtered (stream
, ", ");
312 if ((*elts
== print_max
- 1) && (i
!= (F77_DIM_SIZE (nss
) - 1)))
313 fprintf_filtered (stream
, "...");
318 /* This function gets called to print an F77 array, we set up some
319 stuff and then immediately call f77_print_array_1() */
322 f77_print_array (struct type
*type
, const gdb_byte
*valaddr
,
323 CORE_ADDR address
, struct ui_file
*stream
,
324 int format
, int deref_ref
, int recurse
,
325 enum val_prettyprint pretty
)
330 ndimensions
= calc_f77_array_dims (type
);
332 if (ndimensions
> MAX_FORTRAN_DIMS
|| ndimensions
< 0)
333 error (_("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
334 ndimensions
, MAX_FORTRAN_DIMS
);
336 /* Since F77 arrays are stored column-major, we set up an
337 offset table to get at the various row's elements. The
338 offset table contains entries for both offset and subarray size. */
340 f77_create_arrayprint_offset_tbl (type
, stream
);
342 f77_print_array_1 (1, ndimensions
, type
, valaddr
, address
, stream
, format
,
343 deref_ref
, recurse
, pretty
, &elts
);
347 /* Print data of type TYPE located at VALADDR (within GDB), which came from
348 the inferior at address ADDRESS, onto stdio stream STREAM according to
349 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
352 If the data are a string pointer, returns the number of string characters
355 If DEREF_REF is nonzero, then dereference references, otherwise just print
358 The PRETTY parameter controls prettyprinting. */
361 f_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
362 CORE_ADDR address
, struct ui_file
*stream
, int format
,
363 int deref_ref
, int recurse
, enum val_prettyprint pretty
)
365 unsigned int i
= 0; /* Number of characters printed */
366 struct type
*elttype
;
371 CHECK_TYPEDEF (type
);
372 switch (TYPE_CODE (type
))
374 case TYPE_CODE_STRING
:
375 f77_get_dynamic_length_of_aggregate (type
);
376 LA_PRINT_STRING (stream
, valaddr
, TYPE_LENGTH (type
), 1, 0);
379 case TYPE_CODE_ARRAY
:
380 fprintf_filtered (stream
, "(");
381 f77_print_array (type
, valaddr
, address
, stream
, format
,
382 deref_ref
, recurse
, pretty
);
383 fprintf_filtered (stream
, ")");
387 if (format
&& format
!= 's')
389 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
394 addr
= unpack_pointer (type
, valaddr
);
395 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
397 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
399 /* Try to print what function it points to. */
400 print_address_demangle (addr
, stream
, demangle
);
401 /* Return value is irrelevant except for string pointers. */
405 if (addressprint
&& format
!= 's')
406 deprecated_print_address_numeric (addr
, 1, stream
);
408 /* For a pointer to char or unsigned char, also print the string
409 pointed to, unless pointer is null. */
410 if (TYPE_LENGTH (elttype
) == 1
411 && TYPE_CODE (elttype
) == TYPE_CODE_INT
412 && (format
== 0 || format
== 's')
414 i
= val_print_string (addr
, -1, TYPE_LENGTH (elttype
), stream
);
416 /* Return number of characters printed, including the terminating
417 '\0' if we reached the end. val_print_string takes care including
418 the terminating '\0' if necessary. */
424 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
428 = extract_typed_address (valaddr
+ embedded_offset
, type
);
429 fprintf_filtered (stream
, "@");
430 deprecated_print_address_numeric (addr
, 1, stream
);
432 fputs_filtered (": ", stream
);
434 /* De-reference the reference. */
437 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
439 struct value
*deref_val
=
441 (TYPE_TARGET_TYPE (type
),
442 unpack_pointer (lookup_pointer_type (builtin_type_void
),
443 valaddr
+ embedded_offset
));
444 common_val_print (deref_val
, stream
, format
, deref_ref
, recurse
,
448 fputs_filtered ("???", stream
);
455 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
458 /* FIXME, we should consider, at least for ANSI C language, eliminating
459 the distinction made between FUNCs and POINTERs to FUNCs. */
460 fprintf_filtered (stream
, "{");
461 type_print (type
, "", stream
, -1);
462 fprintf_filtered (stream
, "} ");
463 /* Try to print what function it points to, and its address. */
464 print_address_demangle (address
, stream
, demangle
);
468 format
= format
? format
: output_format
;
470 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
473 val_print_type_code_int (type
, valaddr
, stream
);
474 /* C and C++ has no single byte int type, char is used instead.
475 Since we don't know whether the value is really intended to
476 be used as an integer or a character, print the character
477 equivalent as well. */
478 if (TYPE_LENGTH (type
) == 1)
480 fputs_filtered (" ", stream
);
481 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
),
487 case TYPE_CODE_FLAGS
:
489 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
491 val_print_type_code_flags (type
, valaddr
, stream
);
496 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
498 print_floating (valaddr
, type
, stream
);
502 fprintf_filtered (stream
, "VOID");
505 case TYPE_CODE_ERROR
:
506 fprintf_filtered (stream
, "<error type>");
509 case TYPE_CODE_RANGE
:
510 /* FIXME, we should not ever have to print one of these yet. */
511 fprintf_filtered (stream
, "<range type>");
515 format
= format
? format
: output_format
;
517 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
521 switch (TYPE_LENGTH (type
))
524 val
= unpack_long (builtin_type_f_logical_s1
, valaddr
);
528 val
= unpack_long (builtin_type_f_logical_s2
, valaddr
);
532 val
= unpack_long (builtin_type_f_logical
, valaddr
);
536 error (_("Logicals of length %d bytes not supported"),
542 fprintf_filtered (stream
, ".FALSE.");
544 fprintf_filtered (stream
, ".TRUE.");
546 /* Not a legitimate logical type, print as an integer. */
548 /* Bash the type code temporarily. */
549 TYPE_CODE (type
) = TYPE_CODE_INT
;
550 f_val_print (type
, valaddr
, 0, address
, stream
, format
,
551 deref_ref
, recurse
, pretty
);
552 /* Restore the type code so later uses work as intended. */
553 TYPE_CODE (type
) = TYPE_CODE_BOOL
;
558 case TYPE_CODE_COMPLEX
:
559 switch (TYPE_LENGTH (type
))
562 type
= builtin_type_f_real
;
565 type
= builtin_type_f_real_s8
;
568 type
= builtin_type_f_real_s16
;
571 error (_("Cannot print out complex*%d variables"), TYPE_LENGTH (type
));
573 fputs_filtered ("(", stream
);
574 print_floating (valaddr
, type
, stream
);
575 fputs_filtered (",", stream
);
576 print_floating (valaddr
+ TYPE_LENGTH (type
), type
, stream
);
577 fputs_filtered (")", stream
);
580 case TYPE_CODE_UNDEF
:
581 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
582 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
583 and no complete type for struct foo in that file. */
584 fprintf_filtered (stream
, "<incomplete type>");
587 case TYPE_CODE_STRUCT
:
588 /* Starting from the Fortran 90 standard, Fortran supports derived
590 fprintf_filtered (stream
, "{ ");
591 for (index
= 0; index
< TYPE_NFIELDS (type
); index
++)
593 int offset
= TYPE_FIELD_BITPOS (type
, index
) / 8;
594 f_val_print (TYPE_FIELD_TYPE (type
, index
), valaddr
+ offset
,
595 embedded_offset
, address
, stream
,
596 format
, deref_ref
, recurse
, pretty
);
597 if (index
!= TYPE_NFIELDS (type
) - 1)
598 fputs_filtered (", ", stream
);
600 fprintf_filtered (stream
, "}");
604 error (_("Invalid F77 type code %d in symbol table."), TYPE_CODE (type
));
611 list_all_visible_commons (char *funname
)
613 SAVED_F77_COMMON_PTR tmp
;
615 tmp
= head_common_list
;
617 printf_filtered (_("All COMMON blocks visible at this level:\n\n"));
621 if (strcmp (tmp
->owning_function
, funname
) == 0)
622 printf_filtered ("%s\n", tmp
->name
);
628 /* This function is used to print out the values in a given COMMON
629 block. It will always use the most local common block of the
633 info_common_command (char *comname
, int from_tty
)
635 SAVED_F77_COMMON_PTR the_common
;
636 COMMON_ENTRY_PTR entry
;
637 struct frame_info
*fi
;
641 /* We have been told to display the contents of F77 COMMON
642 block supposedly visible in this function. Let us
643 first make sure that it is visible and if so, let
644 us display its contents */
646 fi
= deprecated_selected_frame
;
649 error (_("No frame selected"));
651 /* The following is generally ripped off from stack.c's routine
652 print_frame_info() */
654 func
= find_pc_function (get_frame_pc (fi
));
657 /* In certain pathological cases, the symtabs give the wrong
658 function (when we are in the first function in a file which
659 is compiled without debugging symbols, the previous function
660 is compiled with debugging symbols, and the "foo.o" symbol
661 that is supposed to tell us where the file with debugging symbols
662 ends has been truncated by ar because it is longer than 15
665 So look in the minimal symbol tables as well, and if it comes
666 up with a larger address for the function use that instead.
667 I don't think this can ever cause any problems; there shouldn't
668 be any minimal symbols in the middle of a function.
669 FIXME: (Not necessarily true. What about text labels) */
671 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (get_frame_pc (fi
));
674 && (SYMBOL_VALUE_ADDRESS (msymbol
)
675 > BLOCK_START (SYMBOL_BLOCK_VALUE (func
))))
676 funname
= DEPRECATED_SYMBOL_NAME (msymbol
);
678 funname
= DEPRECATED_SYMBOL_NAME (func
);
682 struct minimal_symbol
*msymbol
=
683 lookup_minimal_symbol_by_pc (get_frame_pc (fi
));
686 funname
= DEPRECATED_SYMBOL_NAME (msymbol
);
689 /* If comname is NULL, we assume the user wishes to see the
690 which COMMON blocks are visible here and then return */
694 list_all_visible_commons (funname
);
698 the_common
= find_common_for_function (comname
, funname
);
702 if (strcmp (comname
, BLANK_COMMON_NAME_LOCAL
) == 0)
703 printf_filtered (_("Contents of blank COMMON block:\n"));
705 printf_filtered (_("Contents of F77 COMMON block '%s':\n"), comname
);
707 printf_filtered ("\n");
708 entry
= the_common
->entries
;
710 while (entry
!= NULL
)
712 printf_filtered ("%s = ", DEPRECATED_SYMBOL_NAME (entry
->symbol
));
713 print_variable_value (entry
->symbol
, fi
, gdb_stdout
);
714 printf_filtered ("\n");
719 printf_filtered (_("Cannot locate the common block %s in function '%s'\n"),
723 /* This function is used to determine whether there is a
724 F77 common block visible at the current scope called 'comname'. */
728 there_is_a_visible_common_named (char *comname
)
730 SAVED_F77_COMMON_PTR the_common
;
731 struct frame_info
*fi
;
736 error (_("Cannot deal with NULL common name!"));
738 fi
= deprecated_selected_frame
;
741 error (_("No frame selected"));
743 /* The following is generally ripped off from stack.c's routine
744 print_frame_info() */
746 func
= find_pc_function (fi
->pc
);
749 /* In certain pathological cases, the symtabs give the wrong
750 function (when we are in the first function in a file which
751 is compiled without debugging symbols, the previous function
752 is compiled with debugging symbols, and the "foo.o" symbol
753 that is supposed to tell us where the file with debugging symbols
754 ends has been truncated by ar because it is longer than 15
757 So look in the minimal symbol tables as well, and if it comes
758 up with a larger address for the function use that instead.
759 I don't think this can ever cause any problems; there shouldn't
760 be any minimal symbols in the middle of a function.
761 FIXME: (Not necessarily true. What about text labels) */
763 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (fi
->pc
);
766 && (SYMBOL_VALUE_ADDRESS (msymbol
)
767 > BLOCK_START (SYMBOL_BLOCK_VALUE (func
))))
768 funname
= DEPRECATED_SYMBOL_NAME (msymbol
);
770 funname
= DEPRECATED_SYMBOL_NAME (func
);
774 struct minimal_symbol
*msymbol
=
775 lookup_minimal_symbol_by_pc (fi
->pc
);
778 funname
= DEPRECATED_SYMBOL_NAME (msymbol
);
781 the_common
= find_common_for_function (comname
, funname
);
783 return (the_common
? 1 : 0);
788 _initialize_f_valprint (void)
790 add_info ("common", info_common_command
,
791 _("Print out the values contained in a Fortran COMMON block."));
793 add_com ("lc", class_info
, info_common_command
,
794 _("Print out the values contained in a Fortran COMMON block."));