1 /* Support for printing Fortran values for GDB, the GNU debugger.
2 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2003
3 Free Software Foundation, Inc.
4 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
5 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
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_print_array (struct type *, char *, CORE_ADDR,
46 struct ui_file *, int, int, int,
47 enum val_prettyprint);
48 static void f77_print_array_1 (int, int, struct type *, char *,
49 CORE_ADDR, struct ui_file *, int, int, int,
52 static void f77_create_arrayprint_offset_tbl (struct type *,
54 static void f77_get_dynamic_length_of_aggregate (struct type *);
56 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
58 /* Array which holds offsets to be applied to get a row's elements
59 for a given array. Array also holds the size of each subarray. */
61 /* The following macro gives us the size of the nth dimension, Where
64 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
66 /* The following gives us the offset for row n where n is 1-based. */
68 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
71 f77_get_dynamic_lowerbound (struct type *type, int *lower_bound)
73 CORE_ADDR current_frame_addr;
74 CORE_ADDR ptr_to_lower_bound;
76 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
78 case BOUND_BY_VALUE_ON_STACK:
79 current_frame_addr = get_frame_base (deprecated_selected_frame);
80 if (current_frame_addr > 0)
83 read_memory_integer (current_frame_addr +
84 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
89 *lower_bound = DEFAULT_LOWER_BOUND;
90 return BOUND_FETCH_ERROR;
95 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
98 case BOUND_CANNOT_BE_DETERMINED:
99 error ("Lower bound may not be '*' in F77");
102 case BOUND_BY_REF_ON_STACK:
103 current_frame_addr = get_frame_base (deprecated_selected_frame);
104 if (current_frame_addr > 0)
107 read_memory_typed_address (current_frame_addr +
108 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
109 builtin_type_void_data_ptr);
110 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
114 *lower_bound = DEFAULT_LOWER_BOUND;
115 return BOUND_FETCH_ERROR;
119 case BOUND_BY_REF_IN_REG:
120 case BOUND_BY_VALUE_IN_REG:
122 error ("??? unhandled dynamic array bound type ???");
125 return BOUND_FETCH_OK;
129 f77_get_dynamic_upperbound (struct type *type, int *upper_bound)
131 CORE_ADDR current_frame_addr = 0;
132 CORE_ADDR ptr_to_upper_bound;
134 switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
136 case BOUND_BY_VALUE_ON_STACK:
137 current_frame_addr = get_frame_base (deprecated_selected_frame);
138 if (current_frame_addr > 0)
141 read_memory_integer (current_frame_addr +
142 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
147 *upper_bound = DEFAULT_UPPER_BOUND;
148 return BOUND_FETCH_ERROR;
153 *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
156 case BOUND_CANNOT_BE_DETERMINED:
157 /* we have an assumed size array on our hands. Assume that
158 upper_bound == lower_bound so that we show at least
159 1 element.If the user wants to see more elements, let
160 him manually ask for 'em and we'll subscript the
161 array and show him */
162 f77_get_dynamic_lowerbound (type, upper_bound);
165 case BOUND_BY_REF_ON_STACK:
166 current_frame_addr = get_frame_base (deprecated_selected_frame);
167 if (current_frame_addr > 0)
170 read_memory_typed_address (current_frame_addr +
171 TYPE_ARRAY_UPPER_BOUND_VALUE (type),
172 builtin_type_void_data_ptr);
173 *upper_bound = read_memory_integer (ptr_to_upper_bound, 4);
177 *upper_bound = DEFAULT_UPPER_BOUND;
178 return BOUND_FETCH_ERROR;
182 case BOUND_BY_REF_IN_REG:
183 case BOUND_BY_VALUE_IN_REG:
185 error ("??? unhandled dynamic array bound type ???");
188 return BOUND_FETCH_OK;
191 /* Obtain F77 adjustable array dimensions */
194 f77_get_dynamic_length_of_aggregate (struct type *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 (struct type *type, struct ui_file *stream)
234 struct type *tmp_type;
237 int upper, lower, retcode;
241 while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
243 if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
244 fprintf_filtered (stream, "<assumed size array> ");
246 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
247 if (retcode == BOUND_FETCH_ERROR)
248 error ("Cannot obtain dynamic upper bound");
250 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
251 if (retcode == BOUND_FETCH_ERROR)
252 error ("Cannot obtain dynamic lower bound");
254 F77_DIM_SIZE (ndimen) = upper - lower + 1;
256 tmp_type = TYPE_TARGET_TYPE (tmp_type);
260 /* Now we multiply eltlen by all the offsets, so that later we
261 can print out array elements correctly. Up till now we
262 know an offset to apply to get the item but we also
263 have to know how much to add to get to the next item */
266 eltlen = TYPE_LENGTH (tmp_type);
267 F77_DIM_OFFSET (ndimen) = eltlen;
270 eltlen *= F77_DIM_SIZE (ndimen + 1);
271 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 (int nss, int ndimensions, struct type *type, char *valaddr,
282 CORE_ADDR address, 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);
313 if (i != (F77_DIM_SIZE (nss) - 1))
314 fprintf_filtered (stream, ", ");
316 if ((*elts == print_max - 1) && (i != (F77_DIM_SIZE (nss) - 1)))
317 fprintf_filtered (stream, "...");
322 /* This function gets called to print an F77 array, we set up some
323 stuff and then immediately call f77_print_array_1() */
326 f77_print_array (struct type *type, char *valaddr, CORE_ADDR address,
327 struct ui_file *stream, int format, int deref_ref, int recurse,
328 enum val_prettyprint pretty)
333 ndimensions = calc_f77_array_dims (type);
335 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
336 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
337 ndimensions, MAX_FORTRAN_DIMS);
339 /* Since F77 arrays are stored column-major, we set up an
340 offset table to get at the various row's elements. The
341 offset table contains entries for both offset and subarray size. */
343 f77_create_arrayprint_offset_tbl (type, stream);
345 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
346 deref_ref, recurse, pretty, &elts);
350 /* Print data of type TYPE located at VALADDR (within GDB), which came from
351 the inferior at address ADDRESS, onto stdio stream STREAM according to
352 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
355 If the data are a string pointer, returns the number of string characters
358 If DEREF_REF is nonzero, then dereference references, otherwise just print
361 The PRETTY parameter controls prettyprinting. */
364 f_val_print (struct type *type, char *valaddr, int embedded_offset,
365 CORE_ADDR address, struct ui_file *stream, int format,
366 int deref_ref, int recurse, enum val_prettyprint pretty)
368 unsigned int i = 0; /* Number of characters printed */
369 struct type *elttype;
373 CHECK_TYPEDEF (type);
374 switch (TYPE_CODE (type))
376 case TYPE_CODE_STRING:
377 f77_get_dynamic_length_of_aggregate (type);
378 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 1, 0);
381 case TYPE_CODE_ARRAY:
382 fprintf_filtered (stream, "(");
383 f77_print_array (type, valaddr, address, stream, format,
384 deref_ref, recurse, pretty);
385 fprintf_filtered (stream, ")");
388 /* Array of unspecified length: treat like pointer to first elt. */
389 valaddr = (char *) &address;
393 if (format && format != 's')
395 print_scalar_formatted (valaddr, type, format, 0, stream);
400 addr = unpack_pointer (type, valaddr);
401 elttype = check_typedef (TYPE_TARGET_TYPE (type));
403 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
405 /* Try to print what function it points to. */
406 print_address_demangle (addr, stream, demangle);
407 /* Return value is irrelevant except for string pointers. */
411 if (addressprint && format != 's')
412 fprintf_filtered (stream, "0x%s", paddr_nz (addr));
414 /* For a pointer to char or unsigned char, also print the string
415 pointed to, unless pointer is null. */
416 if (TYPE_LENGTH (elttype) == 1
417 && TYPE_CODE (elttype) == TYPE_CODE_INT
418 && (format == 0 || format == 's')
420 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
422 /* Return number of characters printed, plus one for the
423 terminating null if we have "reached the end". */
424 return (i + (print_max && i != print_max));
431 print_scalar_formatted (valaddr, type, format, 0, stream);
434 /* FIXME, we should consider, at least for ANSI C language, eliminating
435 the distinction made between FUNCs and POINTERs to FUNCs. */
436 fprintf_filtered (stream, "{");
437 type_print (type, "", stream, -1);
438 fprintf_filtered (stream, "} ");
439 /* Try to print what function it points to, and its address. */
440 print_address_demangle (address, stream, demangle);
444 format = format ? format : output_format;
446 print_scalar_formatted (valaddr, type, format, 0, stream);
449 val_print_type_code_int (type, valaddr, stream);
450 /* C and C++ has no single byte int type, char is used instead.
451 Since we don't know whether the value is really intended to
452 be used as an integer or a character, print the character
453 equivalent as well. */
454 if (TYPE_LENGTH (type) == 1)
456 fputs_filtered (" ", stream);
457 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
465 print_scalar_formatted (valaddr, type, format, 0, stream);
467 print_floating (valaddr, type, stream);
471 fprintf_filtered (stream, "VOID");
474 case TYPE_CODE_ERROR:
475 fprintf_filtered (stream, "<error type>");
478 case TYPE_CODE_RANGE:
479 /* FIXME, we should not ever have to print one of these yet. */
480 fprintf_filtered (stream, "<range type>");
484 format = format ? format : output_format;
486 print_scalar_formatted (valaddr, type, format, 0, stream);
490 switch (TYPE_LENGTH (type))
493 val = unpack_long (builtin_type_f_logical_s1, valaddr);
497 val = unpack_long (builtin_type_f_logical_s2, valaddr);
501 val = unpack_long (builtin_type_f_logical, valaddr);
505 error ("Logicals of length %d bytes not supported",
511 fprintf_filtered (stream, ".FALSE.");
513 fprintf_filtered (stream, ".TRUE.");
515 /* Not a legitimate logical type, print as an integer. */
517 /* Bash the type code temporarily. */
518 TYPE_CODE (type) = TYPE_CODE_INT;
519 f_val_print (type, valaddr, 0, address, stream, format,
520 deref_ref, recurse, pretty);
521 /* Restore the type code so later uses work as intended. */
522 TYPE_CODE (type) = TYPE_CODE_BOOL;
527 case TYPE_CODE_COMPLEX:
528 switch (TYPE_LENGTH (type))
531 type = builtin_type_f_real;
534 type = builtin_type_f_real_s8;
537 type = builtin_type_f_real_s16;
540 error ("Cannot print out complex*%d variables", TYPE_LENGTH (type));
542 fputs_filtered ("(", stream);
543 print_floating (valaddr, type, stream);
544 fputs_filtered (",", stream);
545 print_floating (valaddr + TYPE_LENGTH (type), type, stream);
546 fputs_filtered (")", stream);
549 case TYPE_CODE_UNDEF:
550 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
551 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
552 and no complete type for struct foo in that file. */
553 fprintf_filtered (stream, "<incomplete type>");
557 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
564 list_all_visible_commons (char *funname)
566 SAVED_F77_COMMON_PTR tmp;
568 tmp = head_common_list;
570 printf_filtered ("All COMMON blocks visible at this level:\n\n");
574 if (strcmp (tmp->owning_function, funname) == 0)
575 printf_filtered ("%s\n", tmp->name);
581 /* This function is used to print out the values in a given COMMON
582 block. It will always use the most local common block of the
586 info_common_command (char *comname, int from_tty)
588 SAVED_F77_COMMON_PTR the_common;
589 COMMON_ENTRY_PTR entry;
590 struct frame_info *fi;
594 /* We have been told to display the contents of F77 COMMON
595 block supposedly visible in this function. Let us
596 first make sure that it is visible and if so, let
597 us display its contents */
599 fi = deprecated_selected_frame;
602 error ("No frame selected");
604 /* The following is generally ripped off from stack.c's routine
605 print_frame_info() */
607 func = find_pc_function (get_frame_pc (fi));
610 /* In certain pathological cases, the symtabs give the wrong
611 function (when we are in the first function in a file which
612 is compiled without debugging symbols, the previous function
613 is compiled with debugging symbols, and the "foo.o" symbol
614 that is supposed to tell us where the file with debugging symbols
615 ends has been truncated by ar because it is longer than 15
618 So look in the minimal symbol tables as well, and if it comes
619 up with a larger address for the function use that instead.
620 I don't think this can ever cause any problems; there shouldn't
621 be any minimal symbols in the middle of a function.
622 FIXME: (Not necessarily true. What about text labels) */
624 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
627 && (SYMBOL_VALUE_ADDRESS (msymbol)
628 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
629 funname = DEPRECATED_SYMBOL_NAME (msymbol);
631 funname = DEPRECATED_SYMBOL_NAME (func);
635 struct minimal_symbol *msymbol =
636 lookup_minimal_symbol_by_pc (get_frame_pc (fi));
639 funname = DEPRECATED_SYMBOL_NAME (msymbol);
642 /* If comname is NULL, we assume the user wishes to see the
643 which COMMON blocks are visible here and then return */
647 list_all_visible_commons (funname);
651 the_common = find_common_for_function (comname, funname);
655 if (strcmp (comname, BLANK_COMMON_NAME_LOCAL) == 0)
656 printf_filtered ("Contents of blank COMMON block:\n");
658 printf_filtered ("Contents of F77 COMMON block '%s':\n", comname);
660 printf_filtered ("\n");
661 entry = the_common->entries;
663 while (entry != NULL)
665 printf_filtered ("%s = ", DEPRECATED_SYMBOL_NAME (entry->symbol));
666 print_variable_value (entry->symbol, fi, gdb_stdout);
667 printf_filtered ("\n");
672 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
676 /* This function is used to determine whether there is a
677 F77 common block visible at the current scope called 'comname'. */
681 there_is_a_visible_common_named (char *comname)
683 SAVED_F77_COMMON_PTR the_common;
684 struct frame_info *fi;
689 error ("Cannot deal with NULL common name!");
691 fi = deprecated_selected_frame;
694 error ("No frame selected");
696 /* The following is generally ripped off from stack.c's routine
697 print_frame_info() */
699 func = find_pc_function (fi->pc);
702 /* In certain pathological cases, the symtabs give the wrong
703 function (when we are in the first function in a file which
704 is compiled without debugging symbols, the previous function
705 is compiled with debugging symbols, and the "foo.o" symbol
706 that is supposed to tell us where the file with debugging symbols
707 ends has been truncated by ar because it is longer than 15
710 So look in the minimal symbol tables as well, and if it comes
711 up with a larger address for the function use that instead.
712 I don't think this can ever cause any problems; there shouldn't
713 be any minimal symbols in the middle of a function.
714 FIXME: (Not necessarily true. What about text labels) */
716 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
719 && (SYMBOL_VALUE_ADDRESS (msymbol)
720 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
721 funname = DEPRECATED_SYMBOL_NAME (msymbol);
723 funname = DEPRECATED_SYMBOL_NAME (func);
727 struct minimal_symbol *msymbol =
728 lookup_minimal_symbol_by_pc (fi->pc);
731 funname = DEPRECATED_SYMBOL_NAME (msymbol);
734 the_common = find_common_for_function (comname, funname);
736 return (the_common ? 1 : 0);
741 _initialize_f_valprint (void)
743 add_info ("common", info_common_command,
744 "Print out the values contained in a Fortran COMMON block.");
746 add_com ("lc", class_info, info_common_command,
747 "Print out the values contained in a Fortran COMMON block.");