GDB (API)
/home/stan/gdb/src/gdb/f-valprint.c
Go to the documentation of this file.
00001 /* Support for printing Fortran values for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1993-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Motorola.  Adapted from the C definitions by Farooq Butt
00006    (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 #include "gdb_string.h"
00025 #include "symtab.h"
00026 #include "gdbtypes.h"
00027 #include "expression.h"
00028 #include "value.h"
00029 #include "valprint.h"
00030 #include "language.h"
00031 #include "f-lang.h"
00032 #include "frame.h"
00033 #include "gdbcore.h"
00034 #include "command.h"
00035 #include "block.h"
00036 #include "dictionary.h"
00037 #include "gdb_assert.h"
00038 #include "exceptions.h"
00039 
00040 extern void _initialize_f_valprint (void);
00041 static void info_common_command (char *, int);
00042 static void f77_create_arrayprint_offset_tbl (struct type *,
00043                                               struct ui_file *);
00044 static void f77_get_dynamic_length_of_aggregate (struct type *);
00045 
00046 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
00047 
00048 /* Array which holds offsets to be applied to get a row's elements
00049    for a given array.  Array also holds the size of each subarray.  */
00050 
00051 /* The following macro gives us the size of the nth dimension, Where 
00052    n is 1 based.  */
00053 
00054 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
00055 
00056 /* The following gives us the offset for row n where n is 1-based.  */
00057 
00058 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
00059 
00060 int
00061 f77_get_lowerbound (struct type *type)
00062 {
00063   if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
00064     error (_("Lower bound may not be '*' in F77"));
00065 
00066   return TYPE_ARRAY_LOWER_BOUND_VALUE (type);
00067 }
00068 
00069 int
00070 f77_get_upperbound (struct type *type)
00071 {
00072   if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
00073     {
00074       /* We have an assumed size array on our hands.  Assume that
00075          upper_bound == lower_bound so that we show at least 1 element.
00076          If the user wants to see more elements, let him manually ask for 'em
00077          and we'll subscript the array and show him.  */
00078 
00079       return f77_get_lowerbound (type);
00080     }
00081 
00082   return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
00083 }
00084 
00085 /* Obtain F77 adjustable array dimensions.  */
00086 
00087 static void
00088 f77_get_dynamic_length_of_aggregate (struct type *type)
00089 {
00090   int upper_bound = -1;
00091   int lower_bound = 1;
00092 
00093   /* Recursively go all the way down into a possibly multi-dimensional
00094      F77 array and get the bounds.  For simple arrays, this is pretty
00095      easy but when the bounds are dynamic, we must be very careful 
00096      to add up all the lengths correctly.  Not doing this right 
00097      will lead to horrendous-looking arrays in parameter lists.
00098 
00099      This function also works for strings which behave very 
00100      similarly to arrays.  */
00101 
00102   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
00103       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
00104     f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
00105 
00106   /* Recursion ends here, start setting up lengths.  */
00107   lower_bound = f77_get_lowerbound (type);
00108   upper_bound = f77_get_upperbound (type);
00109 
00110   /* Patch in a valid length value.  */
00111 
00112   TYPE_LENGTH (type) =
00113     (upper_bound - lower_bound + 1)
00114     * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
00115 }
00116 
00117 /* Function that sets up the array offset,size table for the array 
00118    type "type".  */
00119 
00120 static void
00121 f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
00122 {
00123   struct type *tmp_type;
00124   int eltlen;
00125   int ndimen = 1;
00126   int upper, lower;
00127 
00128   tmp_type = type;
00129 
00130   while (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
00131     {
00132       upper = f77_get_upperbound (tmp_type);
00133       lower = f77_get_lowerbound (tmp_type);
00134 
00135       F77_DIM_SIZE (ndimen) = upper - lower + 1;
00136 
00137       tmp_type = TYPE_TARGET_TYPE (tmp_type);
00138       ndimen++;
00139     }
00140 
00141   /* Now we multiply eltlen by all the offsets, so that later we 
00142      can print out array elements correctly.  Up till now we 
00143      know an offset to apply to get the item but we also 
00144      have to know how much to add to get to the next item.  */
00145 
00146   ndimen--;
00147   eltlen = TYPE_LENGTH (tmp_type);
00148   F77_DIM_OFFSET (ndimen) = eltlen;
00149   while (--ndimen > 0)
00150     {
00151       eltlen *= F77_DIM_SIZE (ndimen + 1);
00152       F77_DIM_OFFSET (ndimen) = eltlen;
00153     }
00154 }
00155 
00156 
00157 
00158 /* Actual function which prints out F77 arrays, Valaddr == address in 
00159    the superior.  Address == the address in the inferior.  */
00160 
00161 static void
00162 f77_print_array_1 (int nss, int ndimensions, struct type *type,
00163                    const gdb_byte *valaddr,
00164                    int embedded_offset, CORE_ADDR address,
00165                    struct ui_file *stream, int recurse,
00166                    const struct value *val,
00167                    const struct value_print_options *options,
00168                    int *elts)
00169 {
00170   int i;
00171 
00172   if (nss != ndimensions)
00173     {
00174       for (i = 0;
00175            (i < F77_DIM_SIZE (nss) && (*elts) < options->print_max);
00176            i++)
00177         {
00178           fprintf_filtered (stream, "( ");
00179           f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
00180                              valaddr,
00181                              embedded_offset + i * F77_DIM_OFFSET (nss),
00182                              address,
00183                              stream, recurse, val, options, elts);
00184           fprintf_filtered (stream, ") ");
00185         }
00186       if (*elts >= options->print_max && i < F77_DIM_SIZE (nss)) 
00187         fprintf_filtered (stream, "...");
00188     }
00189   else
00190     {
00191       for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < options->print_max;
00192            i++, (*elts)++)
00193         {
00194           val_print (TYPE_TARGET_TYPE (type),
00195                      valaddr,
00196                      embedded_offset + i * F77_DIM_OFFSET (ndimensions),
00197                      address, stream, recurse,
00198                      val, options, current_language);
00199 
00200           if (i != (F77_DIM_SIZE (nss) - 1))
00201             fprintf_filtered (stream, ", ");
00202 
00203           if ((*elts == options->print_max - 1)
00204               && (i != (F77_DIM_SIZE (nss) - 1)))
00205             fprintf_filtered (stream, "...");
00206         }
00207     }
00208 }
00209 
00210 /* This function gets called to print an F77 array, we set up some 
00211    stuff and then immediately call f77_print_array_1().  */
00212 
00213 static void
00214 f77_print_array (struct type *type, const gdb_byte *valaddr,
00215                  int embedded_offset,
00216                  CORE_ADDR address, struct ui_file *stream,
00217                  int recurse,
00218                  const struct value *val,
00219                  const struct value_print_options *options)
00220 {
00221   int ndimensions;
00222   int elts = 0;
00223 
00224   ndimensions = calc_f77_array_dims (type);
00225 
00226   if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
00227     error (_("\
00228 Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
00229            ndimensions, MAX_FORTRAN_DIMS);
00230 
00231   /* Since F77 arrays are stored column-major, we set up an 
00232      offset table to get at the various row's elements.  The 
00233      offset table contains entries for both offset and subarray size.  */
00234 
00235   f77_create_arrayprint_offset_tbl (type, stream);
00236 
00237   f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
00238                      address, stream, recurse, val, options, &elts);
00239 }
00240 
00241 
00242 /* Decorations for Fortran.  */
00243 
00244 static const struct generic_val_print_decorations f_decorations =
00245 {
00246   "(",
00247   ",",
00248   ")",
00249   ".TRUE.",
00250   ".FALSE.",
00251   "VOID",
00252 };
00253 
00254 /* See val_print for a description of the various parameters of this
00255    function; they are identical.  */
00256 
00257 void
00258 f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
00259              CORE_ADDR address, struct ui_file *stream, int recurse,
00260              const struct value *original_value,
00261              const struct value_print_options *options)
00262 {
00263   struct gdbarch *gdbarch = get_type_arch (type);
00264   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00265   unsigned int i = 0;   /* Number of characters printed.  */
00266   struct type *elttype;
00267   CORE_ADDR addr;
00268   int index;
00269 
00270   CHECK_TYPEDEF (type);
00271   switch (TYPE_CODE (type))
00272     {
00273     case TYPE_CODE_STRING:
00274       f77_get_dynamic_length_of_aggregate (type);
00275       LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
00276                        valaddr + embedded_offset,
00277                        TYPE_LENGTH (type), NULL, 0, options);
00278       break;
00279 
00280     case TYPE_CODE_ARRAY:
00281       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_CHAR)
00282         {
00283           fprintf_filtered (stream, "(");
00284           f77_print_array (type, valaddr, embedded_offset,
00285                            address, stream, recurse, original_value, options);
00286           fprintf_filtered (stream, ")");
00287         }
00288       else
00289         {
00290           struct type *ch_type = TYPE_TARGET_TYPE (type);
00291 
00292           f77_get_dynamic_length_of_aggregate (type);
00293           LA_PRINT_STRING (stream, ch_type,
00294                            valaddr + embedded_offset,
00295                            TYPE_LENGTH (type) / TYPE_LENGTH (ch_type),
00296                            NULL, 0, options);
00297         }
00298       break;
00299 
00300     case TYPE_CODE_PTR:
00301       if (options->format && options->format != 's')
00302         {
00303           val_print_scalar_formatted (type, valaddr, embedded_offset,
00304                                       original_value, options, 0, stream);
00305           break;
00306         }
00307       else
00308         {
00309           int want_space = 0;
00310 
00311           addr = unpack_pointer (type, valaddr + embedded_offset);
00312           elttype = check_typedef (TYPE_TARGET_TYPE (type));
00313 
00314           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
00315             {
00316               /* Try to print what function it points to.  */
00317               print_function_pointer_address (options, gdbarch, addr, stream);
00318               return;
00319             }
00320 
00321           if (options->symbol_print)
00322             want_space = print_address_demangle (options, gdbarch, addr,
00323                                                  stream, demangle);
00324           else if (options->addressprint && options->format != 's')
00325             {
00326               fputs_filtered (paddress (gdbarch, addr), stream);
00327               want_space = 1;
00328             }
00329 
00330           /* For a pointer to char or unsigned char, also print the string
00331              pointed to, unless pointer is null.  */
00332           if (TYPE_LENGTH (elttype) == 1
00333               && TYPE_CODE (elttype) == TYPE_CODE_INT
00334               && (options->format == 0 || options->format == 's')
00335               && addr != 0)
00336             {
00337               if (want_space)
00338                 fputs_filtered (" ", stream);
00339               i = val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
00340                                     stream, options);
00341             }
00342           return;
00343         }
00344       break;
00345 
00346     case TYPE_CODE_INT:
00347       if (options->format || options->output_format)
00348         {
00349           struct value_print_options opts = *options;
00350 
00351           opts.format = (options->format ? options->format
00352                          : options->output_format);
00353           val_print_scalar_formatted (type, valaddr, embedded_offset,
00354                                       original_value, options, 0, stream);
00355         }
00356       else
00357         {
00358           val_print_type_code_int (type, valaddr + embedded_offset, stream);
00359           /* C and C++ has no single byte int type, char is used instead.
00360              Since we don't know whether the value is really intended to
00361              be used as an integer or a character, print the character
00362              equivalent as well.  */
00363           if (TYPE_LENGTH (type) == 1)
00364             {
00365               LONGEST c;
00366 
00367               fputs_filtered (" ", stream);
00368               c = unpack_long (type, valaddr + embedded_offset);
00369               LA_PRINT_CHAR ((unsigned char) c, type, stream);
00370             }
00371         }
00372       break;
00373 
00374     case TYPE_CODE_STRUCT:
00375     case TYPE_CODE_UNION:
00376       /* Starting from the Fortran 90 standard, Fortran supports derived
00377          types.  */
00378       fprintf_filtered (stream, "( ");
00379       for (index = 0; index < TYPE_NFIELDS (type); index++)
00380         {
00381           int offset = TYPE_FIELD_BITPOS (type, index) / 8;
00382 
00383           val_print (TYPE_FIELD_TYPE (type, index), valaddr,
00384                      embedded_offset + offset,
00385                      address, stream, recurse + 1,
00386                      original_value, options, current_language);
00387           if (index != TYPE_NFIELDS (type) - 1)
00388             fputs_filtered (", ", stream);
00389         }
00390       fprintf_filtered (stream, " )");
00391       break;     
00392 
00393     case TYPE_CODE_REF:
00394     case TYPE_CODE_FUNC:
00395     case TYPE_CODE_FLAGS:
00396     case TYPE_CODE_FLT:
00397     case TYPE_CODE_VOID:
00398     case TYPE_CODE_ERROR:
00399     case TYPE_CODE_RANGE:
00400     case TYPE_CODE_UNDEF:
00401     case TYPE_CODE_COMPLEX:
00402     case TYPE_CODE_BOOL:
00403     case TYPE_CODE_CHAR:
00404     default:
00405       generic_val_print (type, valaddr, embedded_offset, address,
00406                          stream, recurse, original_value, options,
00407                          &f_decorations);
00408       break;
00409     }
00410   gdb_flush (stream);
00411 }
00412 
00413 static void
00414 info_common_command_for_block (struct block *block, const char *comname,
00415                                int *any_printed)
00416 {
00417   struct block_iterator iter;
00418   struct symbol *sym;
00419   const char *name;
00420   struct value_print_options opts;
00421 
00422   get_user_print_options (&opts);
00423 
00424   ALL_BLOCK_SYMBOLS (block, iter, sym)
00425     if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
00426       {
00427         struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
00428         size_t index;
00429 
00430         gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);
00431 
00432         if (comname && (!SYMBOL_LINKAGE_NAME (sym)
00433                         || strcmp (comname, SYMBOL_LINKAGE_NAME (sym)) != 0))
00434           continue;
00435 
00436         if (*any_printed)
00437           putchar_filtered ('\n');
00438         else
00439           *any_printed = 1;
00440         if (SYMBOL_PRINT_NAME (sym))
00441           printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
00442                            SYMBOL_PRINT_NAME (sym));
00443         else
00444           printf_filtered (_("Contents of blank COMMON block:\n"));
00445         
00446         for (index = 0; index < common->n_entries; index++)
00447           {
00448             struct value *val = NULL;
00449             volatile struct gdb_exception except;
00450 
00451             printf_filtered ("%s = ",
00452                              SYMBOL_PRINT_NAME (common->contents[index]));
00453 
00454             TRY_CATCH (except, RETURN_MASK_ERROR)
00455               {
00456                 val = value_of_variable (common->contents[index], block);
00457                 value_print (val, gdb_stdout, &opts);
00458               }
00459 
00460             if (except.reason < 0)
00461               printf_filtered ("<error reading variable: %s>", except.message);
00462             putchar_filtered ('\n');
00463           }
00464       }
00465 }
00466 
00467 /* This function is used to print out the values in a given COMMON 
00468    block.  It will always use the most local common block of the 
00469    given name.  */
00470 
00471 static void
00472 info_common_command (char *comname, int from_tty)
00473 {
00474   struct frame_info *fi;
00475   struct block *block;
00476   int values_printed = 0;
00477 
00478   /* We have been told to display the contents of F77 COMMON 
00479      block supposedly visible in this function.  Let us 
00480      first make sure that it is visible and if so, let 
00481      us display its contents.  */
00482 
00483   fi = get_selected_frame (_("No frame selected"));
00484 
00485   /* The following is generally ripped off from stack.c's routine 
00486      print_frame_info().  */
00487 
00488   block = get_frame_block (fi, 0);
00489   if (block == NULL)
00490     {
00491       printf_filtered (_("No symbol table info available.\n"));
00492       return;
00493     }
00494 
00495   while (block)
00496     {
00497       info_common_command_for_block (block, comname, &values_printed);
00498       /* After handling the function's top-level block, stop.  Don't
00499          continue to its superblock, the block of per-file symbols.  */
00500       if (BLOCK_FUNCTION (block))
00501         break;
00502       block = BLOCK_SUPERBLOCK (block);
00503     }
00504 
00505   if (!values_printed)
00506     {
00507       if (comname)
00508         printf_filtered (_("No common block '%s'.\n"), comname);
00509       else
00510         printf_filtered (_("No common blocks.\n"));
00511     }
00512 }
00513 
00514 void
00515 _initialize_f_valprint (void)
00516 {
00517   add_info ("common", info_common_command,
00518             _("Print out the values contained in a Fortran COMMON block."));
00519   if (xdb_commands)
00520     add_com ("lc", class_info, info_common_command,
00521              _("Print out the values contained in a Fortran COMMON block."));
00522 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines