gdb/f-valprint.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Support for printing Fortran values for GDB, the GNU debugger.

  2.    Copyright (C) 1993-2015 Free Software Foundation, Inc.

  3.    Contributed by Motorola.  Adapted from the C definitions by Farooq Butt
  4.    (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "value.h"
  21. #include "valprint.h"
  22. #include "language.h"
  23. #include "f-lang.h"
  24. #include "frame.h"
  25. #include "gdbcore.h"
  26. #include "command.h"
  27. #include "block.h"
  28. #include "dictionary.h"

  29. extern void _initialize_f_valprint (void);
  30. static void info_common_command (char *, int);
  31. static void f77_create_arrayprint_offset_tbl (struct type *,
  32.                                               struct ui_file *);
  33. static void f77_get_dynamic_length_of_aggregate (struct type *);

  34. int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];

  35. /* Array which holds offsets to be applied to get a row's elements
  36.    for a given array.  Array also holds the size of each subarray.  */

  37. /* The following macro gives us the size of the nth dimension, Where
  38.    n is 1 based.  */

  39. #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])

  40. /* The following gives us the offset for row n where n is 1-based.  */

  41. #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])

  42. int
  43. f77_get_lowerbound (struct type *type)
  44. {
  45.   if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
  46.     error (_("Lower bound may not be '*' in F77"));

  47.   return TYPE_ARRAY_LOWER_BOUND_VALUE (type);
  48. }

  49. int
  50. f77_get_upperbound (struct type *type)
  51. {
  52.   if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
  53.     {
  54.       /* We have an assumed size array on our hands.  Assume that
  55.          upper_bound == lower_bound so that we show at least 1 element.
  56.          If the user wants to see more elements, let him manually ask for 'em
  57.          and we'll subscript the array and show him.  */

  58.       return f77_get_lowerbound (type);
  59.     }

  60.   return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
  61. }

  62. /* Obtain F77 adjustable array dimensions.  */

  63. static void
  64. f77_get_dynamic_length_of_aggregate (struct type *type)
  65. {
  66.   int upper_bound = -1;
  67.   int lower_bound = 1;

  68.   /* Recursively go all the way down into a possibly multi-dimensional
  69.      F77 array and get the bounds.  For simple arrays, this is pretty
  70.      easy but when the bounds are dynamic, we must be very careful
  71.      to add up all the lengths correctly.  Not doing this right
  72.      will lead to horrendous-looking arrays in parameter lists.

  73.      This function also works for strings which behave very
  74.      similarly to arrays.  */

  75.   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
  76.       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
  77.     f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));

  78.   /* Recursion ends here, start setting up lengths.  */
  79.   lower_bound = f77_get_lowerbound (type);
  80.   upper_bound = f77_get_upperbound (type);

  81.   /* Patch in a valid length value.  */

  82.   TYPE_LENGTH (type) =
  83.     (upper_bound - lower_bound + 1)
  84.     * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
  85. }

  86. /* Function that sets up the array offset,size table for the array
  87.    type "type".  */

  88. static void
  89. f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
  90. {
  91.   struct type *tmp_type;
  92.   int eltlen;
  93.   int ndimen = 1;
  94.   int upper, lower;

  95.   tmp_type = type;

  96.   while (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
  97.     {
  98.       upper = f77_get_upperbound (tmp_type);
  99.       lower = f77_get_lowerbound (tmp_type);

  100.       F77_DIM_SIZE (ndimen) = upper - lower + 1;

  101.       tmp_type = TYPE_TARGET_TYPE (tmp_type);
  102.       ndimen++;
  103.     }

  104.   /* Now we multiply eltlen by all the offsets, so that later we
  105.      can print out array elements correctly.  Up till now we
  106.      know an offset to apply to get the item but we also
  107.      have to know how much to add to get to the next item.  */

  108.   ndimen--;
  109.   eltlen = TYPE_LENGTH (tmp_type);
  110.   F77_DIM_OFFSET (ndimen) = eltlen;
  111.   while (--ndimen > 0)
  112.     {
  113.       eltlen *= F77_DIM_SIZE (ndimen + 1);
  114.       F77_DIM_OFFSET (ndimen) = eltlen;
  115.     }
  116. }



  117. /* Actual function which prints out F77 arrays, Valaddr == address in
  118.    the superior.  Address == the address in the inferior.  */

  119. static void
  120. f77_print_array_1 (int nss, int ndimensions, struct type *type,
  121.                    const gdb_byte *valaddr,
  122.                    int embedded_offset, CORE_ADDR address,
  123.                    struct ui_file *stream, int recurse,
  124.                    const struct value *val,
  125.                    const struct value_print_options *options,
  126.                    int *elts)
  127. {
  128.   int i;

  129.   if (nss != ndimensions)
  130.     {
  131.       for (i = 0;
  132.            (i < F77_DIM_SIZE (nss) && (*elts) < options->print_max);
  133.            i++)
  134.         {
  135.           fprintf_filtered (stream, "( ");
  136.           f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
  137.                              valaddr,
  138.                              embedded_offset + i * F77_DIM_OFFSET (nss),
  139.                              address,
  140.                              stream, recurse, val, options, elts);
  141.           fprintf_filtered (stream, ") ");
  142.         }
  143.       if (*elts >= options->print_max && i < F77_DIM_SIZE (nss))
  144.         fprintf_filtered (stream, "...");
  145.     }
  146.   else
  147.     {
  148.       for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < options->print_max;
  149.            i++, (*elts)++)
  150.         {
  151.           val_print (TYPE_TARGET_TYPE (type),
  152.                      valaddr,
  153.                      embedded_offset + i * F77_DIM_OFFSET (ndimensions),
  154.                      address, stream, recurse,
  155.                      val, options, current_language);

  156.           if (i != (F77_DIM_SIZE (nss) - 1))
  157.             fprintf_filtered (stream, ", ");

  158.           if ((*elts == options->print_max - 1)
  159.               && (i != (F77_DIM_SIZE (nss) - 1)))
  160.             fprintf_filtered (stream, "...");
  161.         }
  162.     }
  163. }

  164. /* This function gets called to print an F77 array, we set up some
  165.    stuff and then immediately call f77_print_array_1().  */

  166. static void
  167. f77_print_array (struct type *type, const gdb_byte *valaddr,
  168.                  int embedded_offset,
  169.                  CORE_ADDR address, struct ui_file *stream,
  170.                  int recurse,
  171.                  const struct value *val,
  172.                  const struct value_print_options *options)
  173. {
  174.   int ndimensions;
  175.   int elts = 0;

  176.   ndimensions = calc_f77_array_dims (type);

  177.   if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
  178.     error (_("\
  179. Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
  180.            ndimensions, MAX_FORTRAN_DIMS);

  181.   /* Since F77 arrays are stored column-major, we set up an
  182.      offset table to get at the various row's elements.  The
  183.      offset table contains entries for both offset and subarray size.  */

  184.   f77_create_arrayprint_offset_tbl (type, stream);

  185.   f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
  186.                      address, stream, recurse, val, options, &elts);
  187. }


  188. /* Decorations for Fortran.  */

  189. static const struct generic_val_print_decorations f_decorations =
  190. {
  191.   "(",
  192.   ",",
  193.   ")",
  194.   ".TRUE.",
  195.   ".FALSE.",
  196.   "VOID",
  197. };

  198. /* See val_print for a description of the various parameters of this
  199.    function; they are identical.  */

  200. void
  201. f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
  202.              CORE_ADDR address, struct ui_file *stream, int recurse,
  203.              const struct value *original_value,
  204.              const struct value_print_options *options)
  205. {
  206.   struct gdbarch *gdbarch = get_type_arch (type);
  207.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  208.   unsigned int i = 0;        /* Number of characters printed.  */
  209.   struct type *elttype;
  210.   CORE_ADDR addr;
  211.   int index;

  212.   CHECK_TYPEDEF (type);
  213.   switch (TYPE_CODE (type))
  214.     {
  215.     case TYPE_CODE_STRING:
  216.       f77_get_dynamic_length_of_aggregate (type);
  217.       LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
  218.                        valaddr + embedded_offset,
  219.                        TYPE_LENGTH (type), NULL, 0, options);
  220.       break;

  221.     case TYPE_CODE_ARRAY:
  222.       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_CHAR)
  223.         {
  224.           fprintf_filtered (stream, "(");
  225.           f77_print_array (type, valaddr, embedded_offset,
  226.                            address, stream, recurse, original_value, options);
  227.           fprintf_filtered (stream, ")");
  228.         }
  229.       else
  230.         {
  231.           struct type *ch_type = TYPE_TARGET_TYPE (type);

  232.           f77_get_dynamic_length_of_aggregate (type);
  233.           LA_PRINT_STRING (stream, ch_type,
  234.                            valaddr + embedded_offset,
  235.                            TYPE_LENGTH (type) / TYPE_LENGTH (ch_type),
  236.                            NULL, 0, options);
  237.         }
  238.       break;

  239.     case TYPE_CODE_PTR:
  240.       if (options->format && options->format != 's')
  241.         {
  242.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  243.                                       original_value, options, 0, stream);
  244.           break;
  245.         }
  246.       else
  247.         {
  248.           int want_space = 0;

  249.           addr = unpack_pointer (type, valaddr + embedded_offset);
  250.           elttype = check_typedef (TYPE_TARGET_TYPE (type));

  251.           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  252.             {
  253.               /* Try to print what function it points to.  */
  254.               print_function_pointer_address (options, gdbarch, addr, stream);
  255.               return;
  256.             }

  257.           if (options->symbol_print)
  258.             want_space = print_address_demangle (options, gdbarch, addr,
  259.                                                  stream, demangle);
  260.           else if (options->addressprint && options->format != 's')
  261.             {
  262.               fputs_filtered (paddress (gdbarch, addr), stream);
  263.               want_space = 1;
  264.             }

  265.           /* For a pointer to char or unsigned char, also print the string
  266.              pointed to, unless pointer is null.  */
  267.           if (TYPE_LENGTH (elttype) == 1
  268.               && TYPE_CODE (elttype) == TYPE_CODE_INT
  269.               && (options->format == 0 || options->format == 's')
  270.               && addr != 0)
  271.             {
  272.               if (want_space)
  273.                 fputs_filtered (" ", stream);
  274.               i = val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
  275.                                     stream, options);
  276.             }
  277.           return;
  278.         }
  279.       break;

  280.     case TYPE_CODE_INT:
  281.       if (options->format || options->output_format)
  282.         {
  283.           struct value_print_options opts = *options;

  284.           opts.format = (options->format ? options->format
  285.                          : options->output_format);
  286.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  287.                                       original_value, &opts, 0, stream);
  288.         }
  289.       else
  290.         {
  291.           val_print_type_code_int (type, valaddr + embedded_offset, stream);
  292.           /* C and C++ has no single byte int type, char is used instead.
  293.              Since we don't know whether the value is really intended to
  294.              be used as an integer or a character, print the character
  295.              equivalent as well.  */
  296.           if (TYPE_LENGTH (type) == 1)
  297.             {
  298.               LONGEST c;

  299.               fputs_filtered (" ", stream);
  300.               c = unpack_long (type, valaddr + embedded_offset);
  301.               LA_PRINT_CHAR ((unsigned char) c, type, stream);
  302.             }
  303.         }
  304.       break;

  305.     case TYPE_CODE_STRUCT:
  306.     case TYPE_CODE_UNION:
  307.       /* Starting from the Fortran 90 standard, Fortran supports derived
  308.          types.  */
  309.       fprintf_filtered (stream, "( ");
  310.       for (index = 0; index < TYPE_NFIELDS (type); index++)
  311.         {
  312.           int offset = TYPE_FIELD_BITPOS (type, index) / 8;

  313.           val_print (TYPE_FIELD_TYPE (type, index), valaddr,
  314.                      embedded_offset + offset,
  315.                      address, stream, recurse + 1,
  316.                      original_value, options, current_language);
  317.           if (index != TYPE_NFIELDS (type) - 1)
  318.             fputs_filtered (", ", stream);
  319.         }
  320.       fprintf_filtered (stream, " )");
  321.       break;

  322.     case TYPE_CODE_REF:
  323.     case TYPE_CODE_FUNC:
  324.     case TYPE_CODE_FLAGS:
  325.     case TYPE_CODE_FLT:
  326.     case TYPE_CODE_VOID:
  327.     case TYPE_CODE_ERROR:
  328.     case TYPE_CODE_RANGE:
  329.     case TYPE_CODE_UNDEF:
  330.     case TYPE_CODE_COMPLEX:
  331.     case TYPE_CODE_BOOL:
  332.     case TYPE_CODE_CHAR:
  333.     default:
  334.       generic_val_print (type, valaddr, embedded_offset, address,
  335.                          stream, recurse, original_value, options,
  336.                          &f_decorations);
  337.       break;
  338.     }
  339.   gdb_flush (stream);
  340. }

  341. static void
  342. info_common_command_for_block (const struct block *block, const char *comname,
  343.                                int *any_printed)
  344. {
  345.   struct block_iterator iter;
  346.   struct symbol *sym;
  347.   const char *name;
  348.   struct value_print_options opts;

  349.   get_user_print_options (&opts);

  350.   ALL_BLOCK_SYMBOLS (block, iter, sym)
  351.     if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
  352.       {
  353.         const struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
  354.         size_t index;

  355.         gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);

  356.         if (comname && (!SYMBOL_LINKAGE_NAME (sym)
  357.                         || strcmp (comname, SYMBOL_LINKAGE_NAME (sym)) != 0))
  358.           continue;

  359.         if (*any_printed)
  360.           putchar_filtered ('\n');
  361.         else
  362.           *any_printed = 1;
  363.         if (SYMBOL_PRINT_NAME (sym))
  364.           printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
  365.                            SYMBOL_PRINT_NAME (sym));
  366.         else
  367.           printf_filtered (_("Contents of blank COMMON block:\n"));

  368.         for (index = 0; index < common->n_entries; index++)
  369.           {
  370.             struct value *val = NULL;
  371.             volatile struct gdb_exception except;

  372.             printf_filtered ("%s = ",
  373.                              SYMBOL_PRINT_NAME (common->contents[index]));

  374.             TRY_CATCH (except, RETURN_MASK_ERROR)
  375.               {
  376.                 val = value_of_variable (common->contents[index], block);
  377.                 value_print (val, gdb_stdout, &opts);
  378.               }

  379.             if (except.reason < 0)
  380.               printf_filtered ("<error reading variable: %s>", except.message);
  381.             putchar_filtered ('\n');
  382.           }
  383.       }
  384. }

  385. /* This function is used to print out the values in a given COMMON
  386.    block.  It will always use the most local common block of the
  387.    given name.  */

  388. static void
  389. info_common_command (char *comname, int from_tty)
  390. {
  391.   struct frame_info *fi;
  392.   const struct block *block;
  393.   int values_printed = 0;

  394.   /* We have been told to display the contents of F77 COMMON
  395.      block supposedly visible in this function.  Let us
  396.      first make sure that it is visible and if so, let
  397.      us display its contents.  */

  398.   fi = get_selected_frame (_("No frame selected"));

  399.   /* The following is generally ripped off from stack.c's routine
  400.      print_frame_info().  */

  401.   block = get_frame_block (fi, 0);
  402.   if (block == NULL)
  403.     {
  404.       printf_filtered (_("No symbol table info available.\n"));
  405.       return;
  406.     }

  407.   while (block)
  408.     {
  409.       info_common_command_for_block (block, comname, &values_printed);
  410.       /* After handling the function's top-level block, stop.  Don't
  411.          continue to its superblock, the block of per-file symbols.  */
  412.       if (BLOCK_FUNCTION (block))
  413.         break;
  414.       block = BLOCK_SUPERBLOCK (block);
  415.     }

  416.   if (!values_printed)
  417.     {
  418.       if (comname)
  419.         printf_filtered (_("No common block '%s'.\n"), comname);
  420.       else
  421.         printf_filtered (_("No common blocks.\n"));
  422.     }
  423. }

  424. void
  425. _initialize_f_valprint (void)
  426. {
  427.   add_info ("common", info_common_command,
  428.             _("Print out the values contained in a Fortran COMMON block."));
  429.   if (xdb_commands)
  430.     add_com ("lc", class_info, info_common_command,
  431.              _("Print out the values contained in a Fortran COMMON block."));
  432. }