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 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_string.h"
28 #include "expression.h"
39 static int there_is_a_visible_common_named (char *);
42 extern void _initialize_f_valprint (void);
43 static void info_common_command (char *, int);
44 static void list_all_visible_commons (char *);
45 static void f77_create_arrayprint_offset_tbl (struct type *,
47 static void f77_get_dynamic_length_of_aggregate (struct type *);
49 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
51 /* Array which holds offsets to be applied to get a row's elements
52 for a given array. Array also holds the size of each subarray. */
54 /* The following macro gives us the size of the nth dimension, Where
57 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
59 /* The following gives us the offset for row n where n is 1-based. */
61 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
64 f77_get_dynamic_lowerbound (struct type *type, int *lower_bound)
66 struct frame_info *frame;
67 CORE_ADDR current_frame_addr;
68 CORE_ADDR ptr_to_lower_bound;
70 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
72 case BOUND_BY_VALUE_ON_STACK:
73 frame = deprecated_safe_get_selected_frame ();
74 current_frame_addr = get_frame_base (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 frame = deprecated_safe_get_selected_frame ();
99 current_frame_addr = get_frame_base (frame);
100 if (current_frame_addr > 0)
103 read_memory_typed_address (current_frame_addr +
104 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
105 builtin_type_void_data_ptr);
106 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
110 *lower_bound = DEFAULT_LOWER_BOUND;
111 return BOUND_FETCH_ERROR;
115 case BOUND_BY_REF_IN_REG:
116 case BOUND_BY_VALUE_IN_REG:
118 error (_("??? unhandled dynamic array bound type ???"));
121 return BOUND_FETCH_OK;
125 f77_get_dynamic_upperbound (struct type *type, int *upper_bound)
127 struct frame_info *frame;
128 CORE_ADDR current_frame_addr = 0;
129 CORE_ADDR ptr_to_upper_bound;
131 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
133 case BOUND_BY_VALUE_ON_STACK:
134 frame = deprecated_safe_get_selected_frame ();
135 current_frame_addr = get_frame_base (frame);
136 if (current_frame_addr > 0)
139 read_memory_integer (current_frame_addr +
140 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
145 *upper_bound = DEFAULT_UPPER_BOUND;
146 return BOUND_FETCH_ERROR;
151 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
154 case BOUND_CANNOT_BE_DETERMINED:
155 /* we have an assumed size array on our hands. Assume that
156 upper_bound == lower_bound so that we show at least
157 1 element.If the user wants to see more elements, let
158 him manually ask for 'em and we'll subscript the
159 array and show him */
160 f77_get_dynamic_lowerbound (type, upper_bound);
163 case BOUND_BY_REF_ON_STACK:
164 frame = deprecated_safe_get_selected_frame ();
165 current_frame_addr = get_frame_base (frame);
166 if (current_frame_addr > 0)
169 read_memory_typed_address (current_frame_addr +
170 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
171 builtin_type_void_data_ptr);
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 (struct type *type)
195 int upper_bound = -1;
199 /* Recursively go all the way down into a possibly multi-dimensional
200 F77 array and get the bounds. For simple arrays, this is pretty
201 easy but when the bounds are dynamic, we must be very careful
202 to add up all the lengths correctly. Not doing this right
203 will lead to horrendous-looking arrays in parameter lists.
205 This function also works for strings which behave very
206 similarly to arrays. */
208 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
209 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
210 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
212 /* Recursion ends here, start setting up lengths. */
213 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
214 if (retcode == BOUND_FETCH_ERROR)
215 error (_("Cannot obtain valid array lower bound"));
217 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
218 if (retcode == BOUND_FETCH_ERROR)
219 error (_("Cannot obtain valid array upper bound"));
221 /* Patch in a valid length value. */
224 (upper_bound - lower_bound + 1) * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
227 /* Function that sets up the array offset,size table for the array
231 f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
233 struct type *tmp_type;
236 int upper, lower, retcode;
240 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
242 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
243 fprintf_filtered (stream, "<assumed size array> ");
245 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
246 if (retcode == BOUND_FETCH_ERROR)
247 error (_("Cannot obtain dynamic upper bound"));
249 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
250 if (retcode == BOUND_FETCH_ERROR)
251 error (_("Cannot obtain dynamic lower bound"));
253 F77_DIM_SIZE (ndimen) = upper - lower + 1;
255 tmp_type = TYPE_TARGET_TYPE (tmp_type);
259 /* Now we multiply eltlen by all the offsets, so that later we
260 can print out array elements correctly. Up till now we
261 know an offset to apply to get the item but we also
262 have to know how much to add to get to the next item */
265 eltlen = TYPE_LENGTH (tmp_type);
266 F77_DIM_OFFSET (ndimen) = eltlen;
269 eltlen *= F77_DIM_SIZE (ndimen + 1);
270 F77_DIM_OFFSET (ndimen) = eltlen;
276 /* Actual function which prints out F77 arrays, Valaddr == address in
277 the superior. Address == the address in the inferior. */
280 f77_print_array_1 (int nss, int ndimensions, struct type *type,
281 const gdb_byte *valaddr, CORE_ADDR address,
282 struct ui_file *stream, int format,
283 int deref_ref, int recurse, enum val_prettyprint pretty,
288 if (nss != ndimensions)
290 for (i = 0; (i < F77_DIM_SIZE (nss) && (*elts) < print_max); i++)
292 fprintf_filtered (stream, "( ");
293 f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
294 valaddr + i * F77_DIM_OFFSET (nss),
295 address + i * F77_DIM_OFFSET (nss),
296 stream, format, deref_ref, recurse, pretty, elts);
297 fprintf_filtered (stream, ") ");
299 if (*elts >= print_max && i < F77_DIM_SIZE (nss))
300 fprintf_filtered (stream, "...");
304 for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < print_max;
307 val_print (TYPE_TARGET_TYPE (type),
308 valaddr + i * F77_DIM_OFFSET (ndimensions),
310 address + i * F77_DIM_OFFSET (ndimensions),
311 stream, format, deref_ref, recurse, pretty,
314 if (i != (F77_DIM_SIZE (nss) - 1))
315 fprintf_filtered (stream, ", ");
317 if ((*elts == print_max - 1) && (i != (F77_DIM_SIZE (nss) - 1)))
318 fprintf_filtered (stream, "...");
323 /* This function gets called to print an F77 array, we set up some
324 stuff and then immediately call f77_print_array_1() */
327 f77_print_array (struct type *type, const gdb_byte *valaddr,
328 CORE_ADDR address, struct ui_file *stream,
329 int format, int deref_ref, int recurse,
330 enum val_prettyprint pretty)
335 ndimensions = calc_f77_array_dims (type);
337 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
338 error (_("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
339 ndimensions, MAX_FORTRAN_DIMS);
341 /* Since F77 arrays are stored column-major, we set up an
342 offset table to get at the various row's elements. The
343 offset table contains entries for both offset and subarray size. */
345 f77_create_arrayprint_offset_tbl (type, stream);
347 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
348 deref_ref, recurse, pretty, &elts);
352 /* Print data of type TYPE located at VALADDR (within GDB), which came from
353 the inferior at address ADDRESS, onto stdio stream STREAM according to
354 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
357 If the data are a string pointer, returns the number of string characters
360 If DEREF_REF is nonzero, then dereference references, otherwise just print
363 The PRETTY parameter controls prettyprinting. */
366 f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
367 CORE_ADDR address, struct ui_file *stream, int format,
368 int deref_ref, int recurse, enum val_prettyprint pretty)
370 unsigned int i = 0; /* Number of characters printed */
371 struct type *elttype;
376 CHECK_TYPEDEF (type);
377 switch (TYPE_CODE (type))
379 case TYPE_CODE_STRING:
380 f77_get_dynamic_length_of_aggregate (type);
381 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 1, 0);
384 case TYPE_CODE_ARRAY:
385 fprintf_filtered (stream, "(");
386 f77_print_array (type, valaddr, address, stream, format,
387 deref_ref, recurse, pretty);
388 fprintf_filtered (stream, ")");
392 if (format && format != 's')
394 print_scalar_formatted (valaddr, type, format, 0, stream);
399 addr = unpack_pointer (type, valaddr);
400 elttype = check_typedef (TYPE_TARGET_TYPE (type));
402 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
404 /* Try to print what function it points to. */
405 print_address_demangle (addr, stream, demangle);
406 /* Return value is irrelevant except for string pointers. */
410 if (addressprint && format != 's')
411 fputs_filtered (paddress (addr), stream);
413 /* For a pointer to char or unsigned char, also print the string
414 pointed to, unless pointer is null. */
415 if (TYPE_LENGTH (elttype) == 1
416 && TYPE_CODE (elttype) == TYPE_CODE_INT
417 && (format == 0 || format == 's')
419 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
421 /* Return number of characters printed, including the terminating
422 '\0' if we reached the end. val_print_string takes care including
423 the terminating '\0' if necessary. */
429 elttype = check_typedef (TYPE_TARGET_TYPE (type));
433 = extract_typed_address (valaddr + embedded_offset, type);
434 fprintf_filtered (stream, "@");
435 fputs_filtered (paddress (addr), stream);
437 fputs_filtered (": ", stream);
439 /* De-reference the reference. */
442 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
444 struct value *deref_val =
446 (TYPE_TARGET_TYPE (type),
447 unpack_pointer (lookup_pointer_type (builtin_type_void),
448 valaddr + embedded_offset));
449 common_val_print (deref_val, stream, format, deref_ref, recurse,
450 pretty, current_language);
453 fputs_filtered ("???", stream);
460 print_scalar_formatted (valaddr, type, format, 0, stream);
463 /* FIXME, we should consider, at least for ANSI C language, eliminating
464 the distinction made between FUNCs and POINTERs to FUNCs. */
465 fprintf_filtered (stream, "{");
466 type_print (type, "", stream, -1);
467 fprintf_filtered (stream, "} ");
468 /* Try to print what function it points to, and its address. */
469 print_address_demangle (address, stream, demangle);
473 format = format ? format : output_format;
475 print_scalar_formatted (valaddr, type, format, 0, stream);
478 val_print_type_code_int (type, valaddr, stream);
479 /* C and C++ has no single byte int type, char is used instead.
480 Since we don't know whether the value is really intended to
481 be used as an integer or a character, print the character
482 equivalent as well. */
483 if (TYPE_LENGTH (type) == 1)
485 fputs_filtered (" ", stream);
486 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
492 case TYPE_CODE_FLAGS:
494 print_scalar_formatted (valaddr, type, format, 0, stream);
496 val_print_type_code_flags (type, valaddr, stream);
501 print_scalar_formatted (valaddr, type, format, 0, stream);
503 print_floating (valaddr, type, stream);
507 fprintf_filtered (stream, "VOID");
510 case TYPE_CODE_ERROR:
511 fprintf_filtered (stream, "<error type>");
514 case TYPE_CODE_RANGE:
515 /* FIXME, we should not ever have to print one of these yet. */
516 fprintf_filtered (stream, "<range type>");
520 format = format ? format : output_format;
522 print_scalar_formatted (valaddr, type, format, 0, stream);
526 switch (TYPE_LENGTH (type))
529 val = unpack_long (builtin_type_f_logical_s1, valaddr);
533 val = unpack_long (builtin_type_f_logical_s2, valaddr);
537 val = unpack_long (builtin_type_f_logical, valaddr);
541 error (_("Logicals of length %d bytes not supported"),
547 fprintf_filtered (stream, ".FALSE.");
549 fprintf_filtered (stream, ".TRUE.");
551 /* Not a legitimate logical type, print as an integer. */
553 /* Bash the type code temporarily. */
554 TYPE_CODE (type) = TYPE_CODE_INT;
555 f_val_print (type, valaddr, 0, address, stream, format,
556 deref_ref, recurse, pretty);
557 /* Restore the type code so later uses work as intended. */
558 TYPE_CODE (type) = TYPE_CODE_BOOL;
563 case TYPE_CODE_COMPLEX:
564 switch (TYPE_LENGTH (type))
567 type = builtin_type_f_real;
570 type = builtin_type_f_real_s8;
573 type = builtin_type_f_real_s16;
576 error (_("Cannot print out complex*%d variables"), TYPE_LENGTH (type));
578 fputs_filtered ("(", stream);
579 print_floating (valaddr, type, stream);
580 fputs_filtered (",", stream);
581 print_floating (valaddr + TYPE_LENGTH (type), type, stream);
582 fputs_filtered (")", stream);
585 case TYPE_CODE_UNDEF:
586 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
587 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
588 and no complete type for struct foo in that file. */
589 fprintf_filtered (stream, "<incomplete type>");
592 case TYPE_CODE_STRUCT:
593 case TYPE_CODE_UNION:
594 /* Starting from the Fortran 90 standard, Fortran supports derived
596 fprintf_filtered (stream, "( ");
597 for (index = 0; index < TYPE_NFIELDS (type); index++)
599 int offset = TYPE_FIELD_BITPOS (type, index) / 8;
600 f_val_print (TYPE_FIELD_TYPE (type, index), valaddr + offset,
601 embedded_offset, address, stream,
602 format, deref_ref, recurse, pretty);
603 if (index != TYPE_NFIELDS (type) - 1)
604 fputs_filtered (", ", stream);
606 fprintf_filtered (stream, " )");
610 error (_("Invalid F77 type code %d in symbol table."), TYPE_CODE (type));
617 list_all_visible_commons (char *funname)
619 SAVED_F77_COMMON_PTR tmp;
621 tmp = head_common_list;
623 printf_filtered (_("All COMMON blocks visible at this level:\n\n"));
627 if (strcmp (tmp->owning_function, funname) == 0)
628 printf_filtered ("%s\n", tmp->name);
634 /* This function is used to print out the values in a given COMMON
635 block. It will always use the most local common block of the
639 info_common_command (char *comname, int from_tty)
641 SAVED_F77_COMMON_PTR the_common;
642 COMMON_ENTRY_PTR entry;
643 struct frame_info *fi;
647 /* We have been told to display the contents of F77 COMMON
648 block supposedly visible in this function. Let us
649 first make sure that it is visible and if so, let
650 us display its contents */
652 fi = get_selected_frame (_("No frame selected"));
654 /* The following is generally ripped off from stack.c's routine
655 print_frame_info() */
657 func = find_pc_function (get_frame_pc (fi));
660 /* In certain pathological cases, the symtabs give the wrong
661 function (when we are in the first function in a file which
662 is compiled without debugging symbols, the previous function
663 is compiled with debugging symbols, and the "foo.o" symbol
664 that is supposed to tell us where the file with debugging symbols
665 ends has been truncated by ar because it is longer than 15
668 So look in the minimal symbol tables as well, and if it comes
669 up with a larger address for the function use that instead.
670 I don't think this can ever cause any problems; there shouldn't
671 be any minimal symbols in the middle of a function.
672 FIXME: (Not necessarily true. What about text labels) */
674 struct minimal_symbol *msymbol =
675 lookup_minimal_symbol_by_pc (get_frame_pc (fi));
678 && (SYMBOL_VALUE_ADDRESS (msymbol)
679 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
680 funname = DEPRECATED_SYMBOL_NAME (msymbol);
682 funname = DEPRECATED_SYMBOL_NAME (func);
686 struct minimal_symbol *msymbol =
687 lookup_minimal_symbol_by_pc (get_frame_pc (fi));
690 funname = DEPRECATED_SYMBOL_NAME (msymbol);
691 else /* Got no 'funname', code below will fail. */
692 error (_("No function found for frame."));
695 /* If comname is NULL, we assume the user wishes to see the
696 which COMMON blocks are visible here and then return */
700 list_all_visible_commons (funname);
704 the_common = find_common_for_function (comname, funname);
708 if (strcmp (comname, BLANK_COMMON_NAME_LOCAL) == 0)
709 printf_filtered (_("Contents of blank COMMON block:\n"));
711 printf_filtered (_("Contents of F77 COMMON block '%s':\n"), comname);
713 printf_filtered ("\n");
714 entry = the_common->entries;
716 while (entry != NULL)
718 printf_filtered ("%s = ", DEPRECATED_SYMBOL_NAME (entry->symbol));
719 print_variable_value (entry->symbol, fi, gdb_stdout);
720 printf_filtered ("\n");
725 printf_filtered (_("Cannot locate the common block %s in function '%s'\n"),
729 /* This function is used to determine whether there is a
730 F77 common block visible at the current scope called 'comname'. */
734 there_is_a_visible_common_named (char *comname)
736 SAVED_F77_COMMON_PTR the_common;
737 struct frame_info *fi;
742 error (_("Cannot deal with NULL common name!"));
744 fi = get_selected_frame (_("No frame selected"));
746 /* The following is generally ripped off from stack.c's routine
747 print_frame_info() */
749 func = find_pc_function (fi->pc);
752 /* In certain pathological cases, the symtabs give the wrong
753 function (when we are in the first function in a file which
754 is compiled without debugging symbols, the previous function
755 is compiled with debugging symbols, and the "foo.o" symbol
756 that is supposed to tell us where the file with debugging symbols
757 ends has been truncated by ar because it is longer than 15
760 So look in the minimal symbol tables as well, and if it comes
761 up with a larger address for the function use that instead.
762 I don't think this can ever cause any problems; there shouldn't
763 be any minimal symbols in the middle of a function.
764 FIXME: (Not necessarily true. What about text labels) */
766 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
769 && (SYMBOL_VALUE_ADDRESS (msymbol)
770 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
771 funname = DEPRECATED_SYMBOL_NAME (msymbol);
773 funname = DEPRECATED_SYMBOL_NAME (func);
777 struct minimal_symbol *msymbol =
778 lookup_minimal_symbol_by_pc (fi->pc);
781 funname = DEPRECATED_SYMBOL_NAME (msymbol);
784 the_common = find_common_for_function (comname, funname);
786 return (the_common ? 1 : 0);
791 _initialize_f_valprint (void)
793 add_info ("common", info_common_command,
794 _("Print out the values contained in a Fortran COMMON block."));
796 add_com ("lc", class_info, info_common_command,
797 _("Print out the values contained in a Fortran COMMON block."));