xref: /dflybsd-src/contrib/gdb-7/gdb/f-valprint.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Support for printing Fortran values for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2003, 2005, 2006,
4*cf7f2e2dSJohn Marino    2007, 2008, 2009, 2010 Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Contributed by Motorola.  Adapted from the C definitions by Farooq Butt
75796c8dcSSimon Schubert    (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This file is part of GDB.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
125796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
135796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
145796c8dcSSimon Schubert    (at your option) any later version.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
175796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
185796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195796c8dcSSimon Schubert    GNU General Public License for more details.
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
225796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "defs.h"
255796c8dcSSimon Schubert #include "gdb_string.h"
265796c8dcSSimon Schubert #include "symtab.h"
275796c8dcSSimon Schubert #include "gdbtypes.h"
285796c8dcSSimon Schubert #include "expression.h"
295796c8dcSSimon Schubert #include "value.h"
305796c8dcSSimon Schubert #include "valprint.h"
315796c8dcSSimon Schubert #include "language.h"
325796c8dcSSimon Schubert #include "f-lang.h"
335796c8dcSSimon Schubert #include "frame.h"
345796c8dcSSimon Schubert #include "gdbcore.h"
355796c8dcSSimon Schubert #include "command.h"
365796c8dcSSimon Schubert #include "block.h"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #if 0
395796c8dcSSimon Schubert static int there_is_a_visible_common_named (char *);
405796c8dcSSimon Schubert #endif
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert extern void _initialize_f_valprint (void);
435796c8dcSSimon Schubert static void info_common_command (char *, int);
445796c8dcSSimon Schubert static void list_all_visible_commons (char *);
455796c8dcSSimon Schubert static void f77_create_arrayprint_offset_tbl (struct type *,
465796c8dcSSimon Schubert 					      struct ui_file *);
475796c8dcSSimon Schubert static void f77_get_dynamic_length_of_aggregate (struct type *);
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Array which holds offsets to be applied to get a row's elements
525796c8dcSSimon Schubert    for a given array. Array also holds the size of each subarray.  */
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert /* The following macro gives us the size of the nth dimension, Where
555796c8dcSSimon Schubert    n is 1 based. */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert /* The following gives us the offset for row n where n is 1-based. */
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert int
645796c8dcSSimon Schubert f77_get_lowerbound (struct type *type)
655796c8dcSSimon Schubert {
665796c8dcSSimon Schubert   if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
675796c8dcSSimon Schubert     error (_("Lower bound may not be '*' in F77"));
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert   return TYPE_ARRAY_LOWER_BOUND_VALUE (type);
705796c8dcSSimon Schubert }
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert int
735796c8dcSSimon Schubert f77_get_upperbound (struct type *type)
745796c8dcSSimon Schubert {
755796c8dcSSimon Schubert   if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
765796c8dcSSimon Schubert     {
775796c8dcSSimon Schubert       /* We have an assumed size array on our hands.  Assume that
785796c8dcSSimon Schubert 	 upper_bound == lower_bound so that we show at least 1 element.
795796c8dcSSimon Schubert 	 If the user wants to see more elements, let him manually ask for 'em
805796c8dcSSimon Schubert 	 and we'll subscript the array and show him.  */
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert       return f77_get_lowerbound (type);
835796c8dcSSimon Schubert     }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert   return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
865796c8dcSSimon Schubert }
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* Obtain F77 adjustable array dimensions */
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static void
915796c8dcSSimon Schubert f77_get_dynamic_length_of_aggregate (struct type *type)
925796c8dcSSimon Schubert {
935796c8dcSSimon Schubert   int upper_bound = -1;
945796c8dcSSimon Schubert   int lower_bound = 1;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert   /* Recursively go all the way down into a possibly multi-dimensional
975796c8dcSSimon Schubert      F77 array and get the bounds.  For simple arrays, this is pretty
985796c8dcSSimon Schubert      easy but when the bounds are dynamic, we must be very careful
995796c8dcSSimon Schubert      to add up all the lengths correctly.  Not doing this right
1005796c8dcSSimon Schubert      will lead to horrendous-looking arrays in parameter lists.
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert      This function also works for strings which behave very
1035796c8dcSSimon Schubert      similarly to arrays.  */
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
1065796c8dcSSimon Schubert       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
1075796c8dcSSimon Schubert     f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert   /* Recursion ends here, start setting up lengths.  */
1105796c8dcSSimon Schubert   lower_bound = f77_get_lowerbound (type);
1115796c8dcSSimon Schubert   upper_bound = f77_get_upperbound (type);
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert   /* Patch in a valid length value. */
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   TYPE_LENGTH (type) =
1165796c8dcSSimon Schubert     (upper_bound - lower_bound + 1) * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
1175796c8dcSSimon Schubert }
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert /* Function that sets up the array offset,size table for the array
1205796c8dcSSimon Schubert    type "type".  */
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert static void
1235796c8dcSSimon Schubert f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
1245796c8dcSSimon Schubert {
1255796c8dcSSimon Schubert   struct type *tmp_type;
1265796c8dcSSimon Schubert   int eltlen;
1275796c8dcSSimon Schubert   int ndimen = 1;
128*cf7f2e2dSJohn Marino   int upper, lower;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   tmp_type = type;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert   while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
1335796c8dcSSimon Schubert     {
1345796c8dcSSimon Schubert       upper = f77_get_upperbound (tmp_type);
1355796c8dcSSimon Schubert       lower = f77_get_lowerbound (tmp_type);
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert       F77_DIM_SIZE (ndimen) = upper - lower + 1;
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert       tmp_type = TYPE_TARGET_TYPE (tmp_type);
1405796c8dcSSimon Schubert       ndimen++;
1415796c8dcSSimon Schubert     }
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert   /* Now we multiply eltlen by all the offsets, so that later we
1445796c8dcSSimon Schubert      can print out array elements correctly.  Up till now we
1455796c8dcSSimon Schubert      know an offset to apply to get the item but we also
1465796c8dcSSimon Schubert      have to know how much to add to get to the next item */
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert   ndimen--;
1495796c8dcSSimon Schubert   eltlen = TYPE_LENGTH (tmp_type);
1505796c8dcSSimon Schubert   F77_DIM_OFFSET (ndimen) = eltlen;
1515796c8dcSSimon Schubert   while (--ndimen > 0)
1525796c8dcSSimon Schubert     {
1535796c8dcSSimon Schubert       eltlen *= F77_DIM_SIZE (ndimen + 1);
1545796c8dcSSimon Schubert       F77_DIM_OFFSET (ndimen) = eltlen;
1555796c8dcSSimon Schubert     }
1565796c8dcSSimon Schubert }
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert /* Actual function which prints out F77 arrays, Valaddr == address in
1615796c8dcSSimon Schubert    the superior.  Address == the address in the inferior.  */
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert static void
1645796c8dcSSimon Schubert f77_print_array_1 (int nss, int ndimensions, struct type *type,
1655796c8dcSSimon Schubert 		   const gdb_byte *valaddr, CORE_ADDR address,
1665796c8dcSSimon Schubert 		   struct ui_file *stream, int recurse,
167*cf7f2e2dSJohn Marino 		   const struct value *val,
1685796c8dcSSimon Schubert 		   const struct value_print_options *options,
1695796c8dcSSimon Schubert 		   int *elts)
1705796c8dcSSimon Schubert {
1715796c8dcSSimon Schubert   int i;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert   if (nss != ndimensions)
1745796c8dcSSimon Schubert     {
1755796c8dcSSimon Schubert       for (i = 0; (i < F77_DIM_SIZE (nss) && (*elts) < options->print_max); i++)
1765796c8dcSSimon Schubert 	{
1775796c8dcSSimon Schubert 	  fprintf_filtered (stream, "( ");
1785796c8dcSSimon Schubert 	  f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
1795796c8dcSSimon Schubert 			     valaddr + i * F77_DIM_OFFSET (nss),
1805796c8dcSSimon Schubert 			     address + i * F77_DIM_OFFSET (nss),
181*cf7f2e2dSJohn Marino 			     stream, recurse, val, options, elts);
1825796c8dcSSimon Schubert 	  fprintf_filtered (stream, ") ");
1835796c8dcSSimon Schubert 	}
1845796c8dcSSimon Schubert       if (*elts >= options->print_max && i < F77_DIM_SIZE (nss))
1855796c8dcSSimon Schubert 	fprintf_filtered (stream, "...");
1865796c8dcSSimon Schubert     }
1875796c8dcSSimon Schubert   else
1885796c8dcSSimon Schubert     {
1895796c8dcSSimon Schubert       for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < options->print_max;
1905796c8dcSSimon Schubert 	   i++, (*elts)++)
1915796c8dcSSimon Schubert 	{
1925796c8dcSSimon Schubert 	  val_print (TYPE_TARGET_TYPE (type),
1935796c8dcSSimon Schubert 		     valaddr + i * F77_DIM_OFFSET (ndimensions),
1945796c8dcSSimon Schubert 		     0,
1955796c8dcSSimon Schubert 		     address + i * F77_DIM_OFFSET (ndimensions),
196*cf7f2e2dSJohn Marino 		     stream, recurse, val, options, current_language);
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert 	  if (i != (F77_DIM_SIZE (nss) - 1))
1995796c8dcSSimon Schubert 	    fprintf_filtered (stream, ", ");
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert 	  if ((*elts == options->print_max - 1)
2025796c8dcSSimon Schubert 	      && (i != (F77_DIM_SIZE (nss) - 1)))
2035796c8dcSSimon Schubert 	    fprintf_filtered (stream, "...");
2045796c8dcSSimon Schubert 	}
2055796c8dcSSimon Schubert     }
2065796c8dcSSimon Schubert }
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert /* This function gets called to print an F77 array, we set up some
2095796c8dcSSimon Schubert    stuff and then immediately call f77_print_array_1() */
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert static void
2125796c8dcSSimon Schubert f77_print_array (struct type *type, const gdb_byte *valaddr,
2135796c8dcSSimon Schubert 		 CORE_ADDR address, struct ui_file *stream,
214*cf7f2e2dSJohn Marino 		 int recurse,
215*cf7f2e2dSJohn Marino 		 const struct value *val,
216*cf7f2e2dSJohn Marino 		 const struct value_print_options *options)
2175796c8dcSSimon Schubert {
2185796c8dcSSimon Schubert   int ndimensions;
2195796c8dcSSimon Schubert   int elts = 0;
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert   ndimensions = calc_f77_array_dims (type);
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert   if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
2245796c8dcSSimon Schubert     error (_("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
2255796c8dcSSimon Schubert 	   ndimensions, MAX_FORTRAN_DIMS);
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert   /* Since F77 arrays are stored column-major, we set up an
2285796c8dcSSimon Schubert      offset table to get at the various row's elements. The
2295796c8dcSSimon Schubert      offset table contains entries for both offset and subarray size. */
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   f77_create_arrayprint_offset_tbl (type, stream);
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert   f77_print_array_1 (1, ndimensions, type, valaddr, address, stream,
234*cf7f2e2dSJohn Marino 		     recurse, val, options, &elts);
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert /* Print data of type TYPE located at VALADDR (within GDB), which came from
2395796c8dcSSimon Schubert    the inferior at address ADDRESS, onto stdio stream STREAM according to
2405796c8dcSSimon Schubert    OPTIONS.  The data at VALADDR is in target byte order.
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert    If the data are a string pointer, returns the number of string characters
2435796c8dcSSimon Schubert    printed.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert int
2465796c8dcSSimon Schubert f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
2475796c8dcSSimon Schubert 	     CORE_ADDR address, struct ui_file *stream, int recurse,
248*cf7f2e2dSJohn Marino 	     const struct value *original_value,
2495796c8dcSSimon Schubert 	     const struct value_print_options *options)
2505796c8dcSSimon Schubert {
2515796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
2525796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2535796c8dcSSimon Schubert   unsigned int i = 0;	/* Number of characters printed */
2545796c8dcSSimon Schubert   struct type *elttype;
2555796c8dcSSimon Schubert   LONGEST val;
2565796c8dcSSimon Schubert   CORE_ADDR addr;
2575796c8dcSSimon Schubert   int index;
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
2605796c8dcSSimon Schubert   switch (TYPE_CODE (type))
2615796c8dcSSimon Schubert     {
2625796c8dcSSimon Schubert     case TYPE_CODE_STRING:
2635796c8dcSSimon Schubert       f77_get_dynamic_length_of_aggregate (type);
2645796c8dcSSimon Schubert       LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
265*cf7f2e2dSJohn Marino 		       valaddr, TYPE_LENGTH (type), NULL, 0, options);
2665796c8dcSSimon Schubert       break;
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
2695796c8dcSSimon Schubert       fprintf_filtered (stream, "(");
270*cf7f2e2dSJohn Marino       f77_print_array (type, valaddr, address, stream, recurse, original_value, options);
2715796c8dcSSimon Schubert       fprintf_filtered (stream, ")");
2725796c8dcSSimon Schubert       break;
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert     case TYPE_CODE_PTR:
2755796c8dcSSimon Schubert       if (options->format && options->format != 's')
2765796c8dcSSimon Schubert 	{
2775796c8dcSSimon Schubert 	  print_scalar_formatted (valaddr, type, options, 0, stream);
2785796c8dcSSimon Schubert 	  break;
2795796c8dcSSimon Schubert 	}
2805796c8dcSSimon Schubert       else
2815796c8dcSSimon Schubert 	{
2825796c8dcSSimon Schubert 	  addr = unpack_pointer (type, valaddr);
2835796c8dcSSimon Schubert 	  elttype = check_typedef (TYPE_TARGET_TYPE (type));
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert 	  if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
2865796c8dcSSimon Schubert 	    {
2875796c8dcSSimon Schubert 	      /* Try to print what function it points to.  */
2885796c8dcSSimon Schubert 	      print_address_demangle (gdbarch, addr, stream, demangle);
2895796c8dcSSimon Schubert 	      /* Return value is irrelevant except for string pointers.  */
2905796c8dcSSimon Schubert 	      return 0;
2915796c8dcSSimon Schubert 	    }
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert 	  if (options->addressprint && options->format != 's')
2945796c8dcSSimon Schubert 	    fputs_filtered (paddress (gdbarch, addr), stream);
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert 	  /* For a pointer to char or unsigned char, also print the string
2975796c8dcSSimon Schubert 	     pointed to, unless pointer is null.  */
2985796c8dcSSimon Schubert 	  if (TYPE_LENGTH (elttype) == 1
2995796c8dcSSimon Schubert 	      && TYPE_CODE (elttype) == TYPE_CODE_INT
3005796c8dcSSimon Schubert 	      && (options->format == 0 || options->format == 's')
3015796c8dcSSimon Schubert 	      && addr != 0)
3025796c8dcSSimon Schubert 	    i = val_print_string (TYPE_TARGET_TYPE (type), addr, -1, stream,
3035796c8dcSSimon Schubert 				  options);
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert 	  /* Return number of characters printed, including the terminating
3065796c8dcSSimon Schubert 	     '\0' if we reached the end.  val_print_string takes care including
3075796c8dcSSimon Schubert 	     the terminating '\0' if necessary.  */
3085796c8dcSSimon Schubert 	  return i;
3095796c8dcSSimon Schubert 	}
3105796c8dcSSimon Schubert       break;
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert     case TYPE_CODE_REF:
3135796c8dcSSimon Schubert       elttype = check_typedef (TYPE_TARGET_TYPE (type));
3145796c8dcSSimon Schubert       if (options->addressprint)
3155796c8dcSSimon Schubert 	{
3165796c8dcSSimon Schubert 	  CORE_ADDR addr
3175796c8dcSSimon Schubert 	    = extract_typed_address (valaddr + embedded_offset, type);
318*cf7f2e2dSJohn Marino 
3195796c8dcSSimon Schubert 	  fprintf_filtered (stream, "@");
3205796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, addr), stream);
3215796c8dcSSimon Schubert 	  if (options->deref_ref)
3225796c8dcSSimon Schubert 	    fputs_filtered (": ", stream);
3235796c8dcSSimon Schubert 	}
3245796c8dcSSimon Schubert       /* De-reference the reference.  */
3255796c8dcSSimon Schubert       if (options->deref_ref)
3265796c8dcSSimon Schubert 	{
3275796c8dcSSimon Schubert 	  if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
3285796c8dcSSimon Schubert 	    {
3295796c8dcSSimon Schubert 	      struct value *deref_val =
3305796c8dcSSimon Schubert 		value_at
3315796c8dcSSimon Schubert 		(TYPE_TARGET_TYPE (type),
3325796c8dcSSimon Schubert 		 unpack_pointer (type, valaddr + embedded_offset));
333*cf7f2e2dSJohn Marino 
3345796c8dcSSimon Schubert 	      common_val_print (deref_val, stream, recurse,
3355796c8dcSSimon Schubert 				options, current_language);
3365796c8dcSSimon Schubert 	    }
3375796c8dcSSimon Schubert 	  else
3385796c8dcSSimon Schubert 	    fputs_filtered ("???", stream);
3395796c8dcSSimon Schubert 	}
3405796c8dcSSimon Schubert       break;
3415796c8dcSSimon Schubert 
3425796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
3435796c8dcSSimon Schubert       if (options->format)
3445796c8dcSSimon Schubert 	{
3455796c8dcSSimon Schubert 	  print_scalar_formatted (valaddr, type, options, 0, stream);
3465796c8dcSSimon Schubert 	  break;
3475796c8dcSSimon Schubert 	}
3485796c8dcSSimon Schubert       /* FIXME, we should consider, at least for ANSI C language, eliminating
3495796c8dcSSimon Schubert          the distinction made between FUNCs and POINTERs to FUNCs.  */
3505796c8dcSSimon Schubert       fprintf_filtered (stream, "{");
3515796c8dcSSimon Schubert       type_print (type, "", stream, -1);
3525796c8dcSSimon Schubert       fprintf_filtered (stream, "} ");
3535796c8dcSSimon Schubert       /* Try to print what function it points to, and its address.  */
3545796c8dcSSimon Schubert       print_address_demangle (gdbarch, address, stream, demangle);
3555796c8dcSSimon Schubert       break;
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert     case TYPE_CODE_INT:
3585796c8dcSSimon Schubert       if (options->format || options->output_format)
3595796c8dcSSimon Schubert 	{
3605796c8dcSSimon Schubert 	  struct value_print_options opts = *options;
361*cf7f2e2dSJohn Marino 
3625796c8dcSSimon Schubert 	  opts.format = (options->format ? options->format
3635796c8dcSSimon Schubert 			 : options->output_format);
3645796c8dcSSimon Schubert 	  print_scalar_formatted (valaddr, type, &opts, 0, stream);
3655796c8dcSSimon Schubert 	}
3665796c8dcSSimon Schubert       else
3675796c8dcSSimon Schubert 	{
3685796c8dcSSimon Schubert 	  val_print_type_code_int (type, valaddr, stream);
3695796c8dcSSimon Schubert 	  /* C and C++ has no single byte int type, char is used instead.
3705796c8dcSSimon Schubert 	     Since we don't know whether the value is really intended to
3715796c8dcSSimon Schubert 	     be used as an integer or a character, print the character
3725796c8dcSSimon Schubert 	     equivalent as well. */
3735796c8dcSSimon Schubert 	  if (TYPE_LENGTH (type) == 1)
3745796c8dcSSimon Schubert 	    {
3755796c8dcSSimon Schubert 	      fputs_filtered (" ", stream);
3765796c8dcSSimon Schubert 	      LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
3775796c8dcSSimon Schubert 			     type, stream);
3785796c8dcSSimon Schubert 	    }
3795796c8dcSSimon Schubert 	}
3805796c8dcSSimon Schubert       break;
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert     case TYPE_CODE_FLAGS:
3835796c8dcSSimon Schubert       if (options->format)
3845796c8dcSSimon Schubert 	  print_scalar_formatted (valaddr, type, options, 0, stream);
3855796c8dcSSimon Schubert       else
3865796c8dcSSimon Schubert 	val_print_type_code_flags (type, valaddr, stream);
3875796c8dcSSimon Schubert       break;
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert     case TYPE_CODE_FLT:
3905796c8dcSSimon Schubert       if (options->format)
3915796c8dcSSimon Schubert 	print_scalar_formatted (valaddr, type, options, 0, stream);
3925796c8dcSSimon Schubert       else
3935796c8dcSSimon Schubert 	print_floating (valaddr, type, stream);
3945796c8dcSSimon Schubert       break;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert     case TYPE_CODE_VOID:
3975796c8dcSSimon Schubert       fprintf_filtered (stream, "VOID");
3985796c8dcSSimon Schubert       break;
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert     case TYPE_CODE_ERROR:
401*cf7f2e2dSJohn Marino       fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
4025796c8dcSSimon Schubert       break;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
4055796c8dcSSimon Schubert       /* FIXME, we should not ever have to print one of these yet.  */
4065796c8dcSSimon Schubert       fprintf_filtered (stream, "<range type>");
4075796c8dcSSimon Schubert       break;
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
4105796c8dcSSimon Schubert       if (options->format || options->output_format)
4115796c8dcSSimon Schubert 	{
4125796c8dcSSimon Schubert 	  struct value_print_options opts = *options;
413*cf7f2e2dSJohn Marino 
4145796c8dcSSimon Schubert 	  opts.format = (options->format ? options->format
4155796c8dcSSimon Schubert 			 : options->output_format);
4165796c8dcSSimon Schubert 	  print_scalar_formatted (valaddr, type, &opts, 0, stream);
4175796c8dcSSimon Schubert 	}
4185796c8dcSSimon Schubert       else
4195796c8dcSSimon Schubert 	{
4205796c8dcSSimon Schubert 	  val = extract_unsigned_integer (valaddr,
4215796c8dcSSimon Schubert 					  TYPE_LENGTH (type), byte_order);
4225796c8dcSSimon Schubert 	  if (val == 0)
4235796c8dcSSimon Schubert 	    fprintf_filtered (stream, ".FALSE.");
4245796c8dcSSimon Schubert 	  else if (val == 1)
4255796c8dcSSimon Schubert 	    fprintf_filtered (stream, ".TRUE.");
4265796c8dcSSimon Schubert 	  else
4275796c8dcSSimon Schubert 	    /* Not a legitimate logical type, print as an integer.  */
4285796c8dcSSimon Schubert 	    {
4295796c8dcSSimon Schubert 	      /* Bash the type code temporarily.  */
4305796c8dcSSimon Schubert 	      TYPE_CODE (type) = TYPE_CODE_INT;
431*cf7f2e2dSJohn Marino 	      val_print (type, valaddr, 0, address, stream, recurse,
432*cf7f2e2dSJohn Marino 			 original_value, options, current_language);
4335796c8dcSSimon Schubert 	      /* Restore the type code so later uses work as intended. */
4345796c8dcSSimon Schubert 	      TYPE_CODE (type) = TYPE_CODE_BOOL;
4355796c8dcSSimon Schubert 	    }
4365796c8dcSSimon Schubert 	}
4375796c8dcSSimon Schubert       break;
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert     case TYPE_CODE_COMPLEX:
4405796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
4415796c8dcSSimon Schubert       fputs_filtered ("(", stream);
4425796c8dcSSimon Schubert       print_floating (valaddr, type, stream);
4435796c8dcSSimon Schubert       fputs_filtered (",", stream);
4445796c8dcSSimon Schubert       print_floating (valaddr + TYPE_LENGTH (type), type, stream);
4455796c8dcSSimon Schubert       fputs_filtered (")", stream);
4465796c8dcSSimon Schubert       break;
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert     case TYPE_CODE_UNDEF:
4495796c8dcSSimon Schubert       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
4505796c8dcSSimon Schubert          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
4515796c8dcSSimon Schubert          and no complete type for struct foo in that file.  */
4525796c8dcSSimon Schubert       fprintf_filtered (stream, "<incomplete type>");
4535796c8dcSSimon Schubert       break;
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
4565796c8dcSSimon Schubert     case TYPE_CODE_UNION:
4575796c8dcSSimon Schubert       /* Starting from the Fortran 90 standard, Fortran supports derived
4585796c8dcSSimon Schubert          types.  */
4595796c8dcSSimon Schubert       fprintf_filtered (stream, "( ");
4605796c8dcSSimon Schubert       for (index = 0; index < TYPE_NFIELDS (type); index++)
4615796c8dcSSimon Schubert         {
4625796c8dcSSimon Schubert           int offset = TYPE_FIELD_BITPOS (type, index) / 8;
463*cf7f2e2dSJohn Marino 
464*cf7f2e2dSJohn Marino           val_print (TYPE_FIELD_TYPE (type, index), valaddr + offset,
465*cf7f2e2dSJohn Marino 		     embedded_offset, address, stream, recurse + 1,
466*cf7f2e2dSJohn Marino 		     original_value, options, current_language);
4675796c8dcSSimon Schubert           if (index != TYPE_NFIELDS (type) - 1)
4685796c8dcSSimon Schubert             fputs_filtered (", ", stream);
4695796c8dcSSimon Schubert         }
4705796c8dcSSimon Schubert       fprintf_filtered (stream, " )");
4715796c8dcSSimon Schubert       break;
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert     default:
4745796c8dcSSimon Schubert       error (_("Invalid F77 type code %d in symbol table."), TYPE_CODE (type));
4755796c8dcSSimon Schubert     }
4765796c8dcSSimon Schubert   gdb_flush (stream);
4775796c8dcSSimon Schubert   return 0;
4785796c8dcSSimon Schubert }
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert static void
4815796c8dcSSimon Schubert list_all_visible_commons (char *funname)
4825796c8dcSSimon Schubert {
4835796c8dcSSimon Schubert   SAVED_F77_COMMON_PTR tmp;
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert   tmp = head_common_list;
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert   printf_filtered (_("All COMMON blocks visible at this level:\n\n"));
4885796c8dcSSimon Schubert 
4895796c8dcSSimon Schubert   while (tmp != NULL)
4905796c8dcSSimon Schubert     {
4915796c8dcSSimon Schubert       if (strcmp (tmp->owning_function, funname) == 0)
4925796c8dcSSimon Schubert 	printf_filtered ("%s\n", tmp->name);
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert       tmp = tmp->next;
4955796c8dcSSimon Schubert     }
4965796c8dcSSimon Schubert }
4975796c8dcSSimon Schubert 
4985796c8dcSSimon Schubert /* This function is used to print out the values in a given COMMON
4995796c8dcSSimon Schubert    block. It will always use the most local common block of the
5005796c8dcSSimon Schubert    given name */
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert static void
5035796c8dcSSimon Schubert info_common_command (char *comname, int from_tty)
5045796c8dcSSimon Schubert {
5055796c8dcSSimon Schubert   SAVED_F77_COMMON_PTR the_common;
5065796c8dcSSimon Schubert   COMMON_ENTRY_PTR entry;
5075796c8dcSSimon Schubert   struct frame_info *fi;
5085796c8dcSSimon Schubert   char *funname = 0;
5095796c8dcSSimon Schubert   struct symbol *func;
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert   /* We have been told to display the contents of F77 COMMON
5125796c8dcSSimon Schubert      block supposedly visible in this function.  Let us
5135796c8dcSSimon Schubert      first make sure that it is visible and if so, let
5145796c8dcSSimon Schubert      us display its contents */
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert   fi = get_selected_frame (_("No frame selected"));
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert   /* The following is generally ripped off from stack.c's routine
5195796c8dcSSimon Schubert      print_frame_info() */
5205796c8dcSSimon Schubert 
5215796c8dcSSimon Schubert   func = find_pc_function (get_frame_pc (fi));
5225796c8dcSSimon Schubert   if (func)
5235796c8dcSSimon Schubert     {
5245796c8dcSSimon Schubert       /* In certain pathological cases, the symtabs give the wrong
5255796c8dcSSimon Schubert          function (when we are in the first function in a file which
5265796c8dcSSimon Schubert          is compiled without debugging symbols, the previous function
5275796c8dcSSimon Schubert          is compiled with debugging symbols, and the "foo.o" symbol
5285796c8dcSSimon Schubert          that is supposed to tell us where the file with debugging symbols
5295796c8dcSSimon Schubert          ends has been truncated by ar because it is longer than 15
5305796c8dcSSimon Schubert          characters).
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert          So look in the minimal symbol tables as well, and if it comes
5335796c8dcSSimon Schubert          up with a larger address for the function use that instead.
5345796c8dcSSimon Schubert          I don't think this can ever cause any problems; there shouldn't
5355796c8dcSSimon Schubert          be any minimal symbols in the middle of a function.
5365796c8dcSSimon Schubert          FIXME:  (Not necessarily true.  What about text labels) */
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
5395796c8dcSSimon Schubert 	lookup_minimal_symbol_by_pc (get_frame_pc (fi));
5405796c8dcSSimon Schubert 
5415796c8dcSSimon Schubert       if (msymbol != NULL
5425796c8dcSSimon Schubert 	  && (SYMBOL_VALUE_ADDRESS (msymbol)
5435796c8dcSSimon Schubert 	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
5445796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (msymbol);
5455796c8dcSSimon Schubert       else
5465796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (func);
5475796c8dcSSimon Schubert     }
5485796c8dcSSimon Schubert   else
5495796c8dcSSimon Schubert     {
5505796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
5515796c8dcSSimon Schubert 	lookup_minimal_symbol_by_pc (get_frame_pc (fi));
5525796c8dcSSimon Schubert 
5535796c8dcSSimon Schubert       if (msymbol != NULL)
5545796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (msymbol);
5555796c8dcSSimon Schubert       else /* Got no 'funname', code below will fail.  */
5565796c8dcSSimon Schubert 	error (_("No function found for frame."));
5575796c8dcSSimon Schubert     }
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert   /* If comname is NULL, we assume the user wishes to see the
5605796c8dcSSimon Schubert      which COMMON blocks are visible here and then return */
5615796c8dcSSimon Schubert 
5625796c8dcSSimon Schubert   if (comname == 0)
5635796c8dcSSimon Schubert     {
5645796c8dcSSimon Schubert       list_all_visible_commons (funname);
5655796c8dcSSimon Schubert       return;
5665796c8dcSSimon Schubert     }
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   the_common = find_common_for_function (comname, funname);
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert   if (the_common)
5715796c8dcSSimon Schubert     {
5725796c8dcSSimon Schubert       if (strcmp (comname, BLANK_COMMON_NAME_LOCAL) == 0)
5735796c8dcSSimon Schubert 	printf_filtered (_("Contents of blank COMMON block:\n"));
5745796c8dcSSimon Schubert       else
5755796c8dcSSimon Schubert 	printf_filtered (_("Contents of F77 COMMON block '%s':\n"), comname);
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert       printf_filtered ("\n");
5785796c8dcSSimon Schubert       entry = the_common->entries;
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert       while (entry != NULL)
5815796c8dcSSimon Schubert 	{
5825796c8dcSSimon Schubert 	  print_variable_and_value (NULL, entry->symbol, fi, gdb_stdout, 0);
5835796c8dcSSimon Schubert 	  entry = entry->next;
5845796c8dcSSimon Schubert 	}
5855796c8dcSSimon Schubert     }
5865796c8dcSSimon Schubert   else
5875796c8dcSSimon Schubert     printf_filtered (_("Cannot locate the common block %s in function '%s'\n"),
5885796c8dcSSimon Schubert 		     comname, funname);
5895796c8dcSSimon Schubert }
5905796c8dcSSimon Schubert 
5915796c8dcSSimon Schubert /* This function is used to determine whether there is a
5925796c8dcSSimon Schubert    F77 common block visible at the current scope called 'comname'. */
5935796c8dcSSimon Schubert 
5945796c8dcSSimon Schubert #if 0
5955796c8dcSSimon Schubert static int
5965796c8dcSSimon Schubert there_is_a_visible_common_named (char *comname)
5975796c8dcSSimon Schubert {
5985796c8dcSSimon Schubert   SAVED_F77_COMMON_PTR the_common;
5995796c8dcSSimon Schubert   struct frame_info *fi;
6005796c8dcSSimon Schubert   char *funname = 0;
6015796c8dcSSimon Schubert   struct symbol *func;
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert   if (comname == NULL)
6045796c8dcSSimon Schubert     error (_("Cannot deal with NULL common name!"));
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert   fi = get_selected_frame (_("No frame selected"));
6075796c8dcSSimon Schubert 
6085796c8dcSSimon Schubert   /* The following is generally ripped off from stack.c's routine
6095796c8dcSSimon Schubert      print_frame_info() */
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert   func = find_pc_function (fi->pc);
6125796c8dcSSimon Schubert   if (func)
6135796c8dcSSimon Schubert     {
6145796c8dcSSimon Schubert       /* In certain pathological cases, the symtabs give the wrong
6155796c8dcSSimon Schubert          function (when we are in the first function in a file which
6165796c8dcSSimon Schubert          is compiled without debugging symbols, the previous function
6175796c8dcSSimon Schubert          is compiled with debugging symbols, and the "foo.o" symbol
6185796c8dcSSimon Schubert          that is supposed to tell us where the file with debugging symbols
6195796c8dcSSimon Schubert          ends has been truncated by ar because it is longer than 15
6205796c8dcSSimon Schubert          characters).
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert          So look in the minimal symbol tables as well, and if it comes
6235796c8dcSSimon Schubert          up with a larger address for the function use that instead.
6245796c8dcSSimon Schubert          I don't think this can ever cause any problems; there shouldn't
6255796c8dcSSimon Schubert          be any minimal symbols in the middle of a function.
6265796c8dcSSimon Schubert          FIXME:  (Not necessarily true.  What about text labels) */
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
6295796c8dcSSimon Schubert 
6305796c8dcSSimon Schubert       if (msymbol != NULL
6315796c8dcSSimon Schubert 	  && (SYMBOL_VALUE_ADDRESS (msymbol)
6325796c8dcSSimon Schubert 	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
6335796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (msymbol);
6345796c8dcSSimon Schubert       else
6355796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (func);
6365796c8dcSSimon Schubert     }
6375796c8dcSSimon Schubert   else
6385796c8dcSSimon Schubert     {
6395796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
6405796c8dcSSimon Schubert 	lookup_minimal_symbol_by_pc (fi->pc);
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert       if (msymbol != NULL)
6435796c8dcSSimon Schubert 	funname = SYMBOL_LINKAGE_NAME (msymbol);
6445796c8dcSSimon Schubert     }
6455796c8dcSSimon Schubert 
6465796c8dcSSimon Schubert   the_common = find_common_for_function (comname, funname);
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert   return (the_common ? 1 : 0);
6495796c8dcSSimon Schubert }
6505796c8dcSSimon Schubert #endif
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert void
6535796c8dcSSimon Schubert _initialize_f_valprint (void)
6545796c8dcSSimon Schubert {
6555796c8dcSSimon Schubert   add_info ("common", info_common_command,
6565796c8dcSSimon Schubert 	    _("Print out the values contained in a Fortran COMMON block."));
6575796c8dcSSimon Schubert   if (xdb_commands)
6585796c8dcSSimon Schubert     add_com ("lc", class_info, info_common_command,
6595796c8dcSSimon Schubert 	     _("Print out the values contained in a Fortran COMMON block."));
6605796c8dcSSimon Schubert }
661