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,
50 enum val_prettyprint);
51 static void f77_create_arrayprint_offset_tbl (struct type *,
53 static void f77_get_dynamic_length_of_aggregate (struct type *);
55 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
57 /* Array which holds offsets to be applied to get a row's elements
58 for a given array. Array also holds the size of each subarray. */
60 /* The following macro gives us the size of the nth dimension, Where
63 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
65 /* The following gives us the offset for row n where n is 1-based. */
67 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
70 f77_get_dynamic_lowerbound (struct type *type, int *lower_bound)
72 CORE_ADDR current_frame_addr;
73 CORE_ADDR ptr_to_lower_bound;
75 switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
77 case BOUND_BY_VALUE_ON_STACK:
78 current_frame_addr = get_frame_base (deprecated_selected_frame);
79 if (current_frame_addr > 0)
82 read_memory_integer (current_frame_addr +
83 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
88 *lower_bound = DEFAULT_LOWER_BOUND;
89 return BOUND_FETCH_ERROR;
94 *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
97 case BOUND_CANNOT_BE_DETERMINED:
98 error ("Lower bound may not be '*' in F77");
101 case BOUND_BY_REF_ON_STACK:
102 current_frame_addr = get_frame_base (deprecated_selected_frame);
103 if (current_frame_addr > 0)
106 read_memory_typed_address (current_frame_addr +
107 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
108 builtin_type_void_data_ptr);
109 *lower_bound = read_memory_integer (ptr_to_lower_bound, 4);
113 *lower_bound = DEFAULT_LOWER_BOUND;
114 return BOUND_FETCH_ERROR;
118 case BOUND_BY_REF_IN_REG:
119 case BOUND_BY_VALUE_IN_REG:
121 error ("??? unhandled dynamic array bound type ???");
124 return BOUND_FETCH_OK;
128 f77_get_dynamic_upperbound (struct type *type, int *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 = get_frame_base (deprecated_selected_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 = get_frame_base (deprecated_selected_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;
274 /* Actual function which prints out F77 arrays, Valaddr == address in
275 the superior. Address == the address in the inferior. */
278 f77_print_array_1 (int nss, int ndimensions, struct type *type, char *valaddr,
279 CORE_ADDR address, struct ui_file *stream, int format,
280 int deref_ref, int recurse, enum val_prettyprint pretty)
284 if (nss != ndimensions)
286 for (i = 0; i < F77_DIM_SIZE (nss); 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);
293 fprintf_filtered (stream, ") ");
298 for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
300 val_print (TYPE_TARGET_TYPE (type),
301 valaddr + i * F77_DIM_OFFSET (ndimensions),
303 address + i * F77_DIM_OFFSET (ndimensions),
304 stream, format, deref_ref, recurse, pretty);
306 if (i != (F77_DIM_SIZE (nss) - 1))
307 fprintf_filtered (stream, ", ");
309 if (i == print_max - 1)
310 fprintf_filtered (stream, "...");
315 /* This function gets called to print an F77 array, we set up some
316 stuff and then immediately call f77_print_array_1() */
319 f77_print_array (struct type *type, char *valaddr, CORE_ADDR address,
320 struct ui_file *stream, int format, int deref_ref, int recurse,
321 enum val_prettyprint pretty)
325 ndimensions = calc_f77_array_dims (type);
327 if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
328 error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
329 ndimensions, MAX_FORTRAN_DIMS);
331 /* Since F77 arrays are stored column-major, we set up an
332 offset table to get at the various row's elements. The
333 offset table contains entries for both offset and subarray size. */
335 f77_create_arrayprint_offset_tbl (type, stream);
337 f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format,
338 deref_ref, recurse, pretty);
342 /* Print data of type TYPE located at VALADDR (within GDB), which came from
343 the inferior at address ADDRESS, onto stdio stream STREAM according to
344 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
347 If the data are a string pointer, returns the number of string characters
350 If DEREF_REF is nonzero, then dereference references, otherwise just print
353 The PRETTY parameter controls prettyprinting. */
356 f_val_print (struct type *type, char *valaddr, int embedded_offset,
357 CORE_ADDR address, struct ui_file *stream, int format,
358 int deref_ref, int recurse, enum val_prettyprint pretty)
360 register unsigned int i = 0; /* Number of characters printed */
361 struct type *elttype;
365 CHECK_TYPEDEF (type);
366 switch (TYPE_CODE (type))
368 case TYPE_CODE_STRING:
369 f77_get_dynamic_length_of_aggregate (type);
370 LA_PRINT_STRING (stream, valaddr, TYPE_LENGTH (type), 1, 0);
373 case TYPE_CODE_ARRAY:
374 fprintf_filtered (stream, "(");
375 f77_print_array (type, valaddr, address, stream, format,
376 deref_ref, recurse, pretty);
377 fprintf_filtered (stream, ")");
380 /* Array of unspecified length: treat like pointer to first elt. */
381 valaddr = (char *) &address;
385 if (format && format != 's')
387 print_scalar_formatted (valaddr, type, format, 0, stream);
392 addr = unpack_pointer (type, valaddr);
393 elttype = check_typedef (TYPE_TARGET_TYPE (type));
395 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
397 /* Try to print what function it points to. */
398 print_address_demangle (addr, stream, demangle);
399 /* Return value is irrelevant except for string pointers. */
403 if (addressprint && format != 's')
404 fprintf_filtered (stream, "0x%s", paddr_nz (addr));
406 /* For a pointer to char or unsigned char, also print the string
407 pointed to, unless pointer is null. */
408 if (TYPE_LENGTH (elttype) == 1
409 && TYPE_CODE (elttype) == TYPE_CODE_INT
410 && (format == 0 || format == 's')
412 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
414 /* Return number of characters printed, plus one for the
415 terminating null if we have "reached the end". */
416 return (i + (print_max && i != print_max));
423 print_scalar_formatted (valaddr, type, format, 0, stream);
426 /* FIXME, we should consider, at least for ANSI C language, eliminating
427 the distinction made between FUNCs and POINTERs to FUNCs. */
428 fprintf_filtered (stream, "{");
429 type_print (type, "", stream, -1);
430 fprintf_filtered (stream, "} ");
431 /* Try to print what function it points to, and its address. */
432 print_address_demangle (address, stream, demangle);
436 format = format ? format : output_format;
438 print_scalar_formatted (valaddr, type, format, 0, stream);
441 val_print_type_code_int (type, valaddr, stream);
442 /* C and C++ has no single byte int type, char is used instead.
443 Since we don't know whether the value is really intended to
444 be used as an integer or a character, print the character
445 equivalent as well. */
446 if (TYPE_LENGTH (type) == 1)
448 fputs_filtered (" ", stream);
449 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
457 print_scalar_formatted (valaddr, type, format, 0, stream);
459 print_floating (valaddr, type, stream);
463 fprintf_filtered (stream, "VOID");
466 case TYPE_CODE_ERROR:
467 fprintf_filtered (stream, "<error type>");
470 case TYPE_CODE_RANGE:
471 /* FIXME, we should not ever have to print one of these yet. */
472 fprintf_filtered (stream, "<range type>");
476 format = format ? format : output_format;
478 print_scalar_formatted (valaddr, type, format, 0, stream);
482 switch (TYPE_LENGTH (type))
485 val = unpack_long (builtin_type_f_logical_s1, valaddr);
489 val = unpack_long (builtin_type_f_logical_s2, valaddr);
493 val = unpack_long (builtin_type_f_logical, valaddr);
497 error ("Logicals of length %d bytes not supported",
503 fprintf_filtered (stream, ".FALSE.");
505 fprintf_filtered (stream, ".TRUE.");
507 /* Not a legitimate logical type, print as an integer. */
509 /* Bash the type code temporarily. */
510 TYPE_CODE (type) = TYPE_CODE_INT;
511 f_val_print (type, valaddr, 0, address, stream, format,
512 deref_ref, recurse, pretty);
513 /* Restore the type code so later uses work as intended. */
514 TYPE_CODE (type) = TYPE_CODE_BOOL;
519 case TYPE_CODE_COMPLEX:
520 switch (TYPE_LENGTH (type))
523 type = builtin_type_f_real;
526 type = builtin_type_f_real_s8;
529 type = builtin_type_f_real_s16;
532 error ("Cannot print out complex*%d variables", TYPE_LENGTH (type));
534 fputs_filtered ("(", stream);
535 print_floating (valaddr, type, stream);
536 fputs_filtered (",", stream);
537 print_floating (valaddr + TYPE_LENGTH (type), type, stream);
538 fputs_filtered (")", stream);
541 case TYPE_CODE_UNDEF:
542 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
543 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
544 and no complete type for struct foo in that file. */
545 fprintf_filtered (stream, "<incomplete type>");
549 error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
556 list_all_visible_commons (char *funname)
558 SAVED_F77_COMMON_PTR tmp;
560 tmp = head_common_list;
562 printf_filtered ("All COMMON blocks visible at this level:\n\n");
566 if (strcmp (tmp->owning_function, funname) == 0)
567 printf_filtered ("%s\n", tmp->name);
573 /* This function is used to print out the values in a given COMMON
574 block. It will always use the most local common block of the
578 info_common_command (char *comname, int from_tty)
580 SAVED_F77_COMMON_PTR the_common;
581 COMMON_ENTRY_PTR entry;
582 struct frame_info *fi;
583 register char *funname = 0;
586 /* We have been told to display the contents of F77 COMMON
587 block supposedly visible in this function. Let us
588 first make sure that it is visible and if so, let
589 us display its contents */
591 fi = deprecated_selected_frame;
594 error ("No frame selected");
596 /* The following is generally ripped off from stack.c's routine
597 print_frame_info() */
599 func = find_pc_function (get_frame_pc (fi));
602 /* In certain pathological cases, the symtabs give the wrong
603 function (when we are in the first function in a file which
604 is compiled without debugging symbols, the previous function
605 is compiled with debugging symbols, and the "foo.o" symbol
606 that is supposed to tell us where the file with debugging symbols
607 ends has been truncated by ar because it is longer than 15
610 So look in the minimal symbol tables as well, and if it comes
611 up with a larger address for the function use that instead.
612 I don't think this can ever cause any problems; there shouldn't
613 be any minimal symbols in the middle of a function.
614 FIXME: (Not necessarily true. What about text labels) */
616 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
619 && (SYMBOL_VALUE_ADDRESS (msymbol)
620 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
621 funname = DEPRECATED_SYMBOL_NAME (msymbol);
623 funname = DEPRECATED_SYMBOL_NAME (func);
627 register struct minimal_symbol *msymbol =
628 lookup_minimal_symbol_by_pc (get_frame_pc (fi));
631 funname = DEPRECATED_SYMBOL_NAME (msymbol);
634 /* If comname is NULL, we assume the user wishes to see the
635 which COMMON blocks are visible here and then return */
639 list_all_visible_commons (funname);
643 the_common = find_common_for_function (comname, funname);
647 if (strcmp (comname, BLANK_COMMON_NAME_LOCAL) == 0)
648 printf_filtered ("Contents of blank COMMON block:\n");
650 printf_filtered ("Contents of F77 COMMON block '%s':\n", comname);
652 printf_filtered ("\n");
653 entry = the_common->entries;
655 while (entry != NULL)
657 printf_filtered ("%s = ", DEPRECATED_SYMBOL_NAME (entry->symbol));
658 print_variable_value (entry->symbol, fi, gdb_stdout);
659 printf_filtered ("\n");
664 printf_filtered ("Cannot locate the common block %s in function '%s'\n",
668 /* This function is used to determine whether there is a
669 F77 common block visible at the current scope called 'comname'. */
673 there_is_a_visible_common_named (char *comname)
675 SAVED_F77_COMMON_PTR the_common;
676 struct frame_info *fi;
677 register char *funname = 0;
681 error ("Cannot deal with NULL common name!");
683 fi = deprecated_selected_frame;
686 error ("No frame selected");
688 /* The following is generally ripped off from stack.c's routine
689 print_frame_info() */
691 func = find_pc_function (fi->pc);
694 /* In certain pathological cases, the symtabs give the wrong
695 function (when we are in the first function in a file which
696 is compiled without debugging symbols, the previous function
697 is compiled with debugging symbols, and the "foo.o" symbol
698 that is supposed to tell us where the file with debugging symbols
699 ends has been truncated by ar because it is longer than 15
702 So look in the minimal symbol tables as well, and if it comes
703 up with a larger address for the function use that instead.
704 I don't think this can ever cause any problems; there shouldn't
705 be any minimal symbols in the middle of a function.
706 FIXME: (Not necessarily true. What about text labels) */
708 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
711 && (SYMBOL_VALUE_ADDRESS (msymbol)
712 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
713 funname = DEPRECATED_SYMBOL_NAME (msymbol);
715 funname = DEPRECATED_SYMBOL_NAME (func);
719 register struct minimal_symbol *msymbol =
720 lookup_minimal_symbol_by_pc (fi->pc);
723 funname = DEPRECATED_SYMBOL_NAME (msymbol);
726 the_common = find_common_for_function (comname, funname);
728 return (the_common ? 1 : 0);
733 _initialize_f_valprint (void)
735 add_info ("common", info_common_command,
736 "Print out the values contained in a Fortran COMMON block.");
738 add_com ("lc", class_info, info_common_command,
739 "Print out the values contained in a Fortran COMMON block.");