home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / ch-valprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-04  |  15.5 KB  |  582 lines

  1. /* Support for printing Chill values for GDB, the GNU debugger.
  2.    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "obstack.h"
  23. #include "symtab.h"
  24. #include "gdbtypes.h"
  25. #include "valprint.h"
  26. #include "expression.h"
  27. #include "value.h"
  28. #include "language.h"
  29. #include "demangle.h"
  30. #include "c-lang.h" /* For c_val_print */
  31. #include "typeprint.h"
  32. #include "ch-lang.h"
  33.  
  34. static void
  35. chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
  36.                   enum val_prettyprint, struct type **));
  37.  
  38.  
  39. /* Print the elements of an array.
  40.    Similar to val_print_array_elements, but prints
  41.    element indexes (in Chill syntax). */
  42.  
  43. static void
  44. chill_val_print_array_elements (type, valaddr, address, stream,
  45.                 format, deref_ref, recurse, pretty)
  46.      struct type *type;
  47.      char *valaddr;
  48.      CORE_ADDR address;
  49.      GDB_FILE *stream;
  50.      int format;
  51.      int deref_ref;
  52.      int recurse;
  53.      enum val_prettyprint pretty;
  54. {
  55.   unsigned int i = 0;
  56.   unsigned int things_printed = 0;
  57.   unsigned len;
  58.   struct type *elttype;
  59.   struct type *range_type = TYPE_FIELD_TYPE (type, 0);
  60.   struct type *index_type = TYPE_TARGET_TYPE (range_type);
  61.   unsigned eltlen;
  62.   /* Position of the array element we are examining to see
  63.      whether it is repeated.  */
  64.   unsigned int rep1;
  65.   /* Number of repetitions we have detected so far.  */
  66.   unsigned int reps;
  67.   LONGEST low_bound =  TYPE_FIELD_BITPOS (range_type, 0);
  68.   LONGEST high_bound = TYPE_FIELD_BITPOS (range_type, 1);
  69.       
  70.   elttype = TYPE_TARGET_TYPE (type);
  71.   eltlen = TYPE_LENGTH (elttype);
  72.   len = TYPE_LENGTH (type) / eltlen;
  73.  
  74.   annotate_array_section_begin (i, elttype);
  75.  
  76.   for (; i < len && things_printed < print_max; i++)
  77.     {
  78.       if (i != 0)
  79.     {
  80.       if (prettyprint_arrays)
  81.         {
  82.           fprintf_filtered (stream, ",\n");
  83.           print_spaces_filtered (2 + 2 * recurse, stream);
  84.         }
  85.       else
  86.         {
  87.           fprintf_filtered (stream, ", ");
  88.         }
  89.     }
  90.       wrap_here (n_spaces (2 + 2 * recurse));
  91.  
  92.       rep1 = i + 1;
  93.       reps = 1;
  94.       while ((rep1 < len) && 
  95.          !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
  96.     {
  97.       ++reps;
  98.       ++rep1;
  99.     }
  100.  
  101.       fputs_filtered ("(", stream);
  102.       print_type_scalar (index_type, low_bound + i, stream);
  103.       if (reps > 1)
  104.     {
  105.       fputs_filtered (":", stream);
  106.       print_type_scalar (index_type, low_bound + i + reps - 1, stream);
  107.       fputs_filtered ("): ", stream);
  108.       val_print (elttype, valaddr + i * eltlen, 0, stream, format,
  109.              deref_ref, recurse + 1, pretty);
  110.  
  111.       i = rep1 - 1;
  112.       things_printed += 1;
  113.     }
  114.       else
  115.     {
  116.       fputs_filtered ("): ", stream);
  117.       val_print (elttype, valaddr + i * eltlen, 0, stream, format,
  118.              deref_ref, recurse + 1, pretty);
  119.       annotate_elt ();
  120.       things_printed++;
  121.     }
  122.     }
  123.   annotate_array_section_end ();
  124.   if (i < len)
  125.     {
  126.       fprintf_filtered (stream, "...");
  127.     }
  128. }
  129.  
  130. /* Print data of type TYPE located at VALADDR (within GDB), which came from
  131.    the inferior at address ADDRESS, onto stdio stream STREAM according to
  132.    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
  133.    target byte order.
  134.  
  135.    If the data are a string pointer, returns the number of string characters
  136.    printed.
  137.  
  138.    If DEREF_REF is nonzero, then dereference references, otherwise just print
  139.    them like pointers.
  140.  
  141.    The PRETTY parameter controls prettyprinting.  */
  142.  
  143. int
  144. chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
  145.          pretty)
  146.      struct type *type;
  147.      char *valaddr;
  148.      CORE_ADDR address;
  149.      GDB_FILE *stream;
  150.      int format;
  151.      int deref_ref;
  152.      int recurse;
  153.      enum val_prettyprint pretty;
  154. {
  155.   LONGEST val;
  156.   unsigned int i = 0;        /* Number of characters printed.  */
  157.   struct type *elttype;
  158.   CORE_ADDR addr;
  159.  
  160.   switch (TYPE_CODE (type))
  161.     {
  162.     case TYPE_CODE_ARRAY:
  163.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
  164.     {
  165.       if (prettyprint_arrays)
  166.         {
  167.           print_spaces_filtered (2 + 2 * recurse, stream);
  168.         }
  169.       fprintf_filtered (stream, "[");
  170.       chill_val_print_array_elements (type, valaddr, address, stream,
  171.                       format, deref_ref, recurse, pretty);
  172.       fprintf_filtered (stream, "]");
  173.     }
  174.       else
  175.     {
  176.       error ("unimplemented in chill_val_print; unspecified array length");
  177.     }
  178.       break;
  179.  
  180.     case TYPE_CODE_INT:
  181.       format = format ? format : output_format;
  182.       if (format)
  183.     {
  184.       print_scalar_formatted (valaddr, type, format, 0, stream);
  185.     }
  186.       else
  187.     {
  188.       val_print_type_code_int (type, valaddr, stream);
  189.     }
  190.       break;
  191.  
  192.     case TYPE_CODE_CHAR:
  193.       format = format ? format : output_format;
  194.       if (format)
  195.     {
  196.       print_scalar_formatted (valaddr, type, format, 0, stream);
  197.     }
  198.       else
  199.     {
  200.       LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
  201.              stream);
  202.     }
  203.       break;
  204.  
  205.     case TYPE_CODE_FLT:
  206.       if (format)
  207.     {
  208.       print_scalar_formatted (valaddr, type, format, 0, stream);
  209.     }
  210.       else
  211.     {
  212.       print_floating (valaddr, type, stream);
  213.     }
  214.       break;
  215.  
  216.     case TYPE_CODE_BOOL:
  217.       format = format ? format : output_format;
  218.       if (format)
  219.     {
  220.       print_scalar_formatted (valaddr, type, format, 0, stream);
  221.     }
  222.       else
  223.     {
  224.       /* FIXME: Why is this using builtin_type_chill_bool not type?  */
  225.       val = unpack_long (builtin_type_chill_bool, valaddr);
  226.       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
  227.     }
  228.       break;
  229.  
  230.     case TYPE_CODE_UNDEF:
  231.       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
  232.      dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
  233.      and no complete type for struct foo in that file.  */
  234.       fprintf_filtered (stream, "<incomplete type>");
  235.       break;
  236.  
  237.     case TYPE_CODE_PTR:
  238.       if (format && format != 's')
  239.     {
  240.       print_scalar_formatted (valaddr, type, format, 0, stream);
  241.       break;
  242.     }
  243.       addr = unpack_pointer (type, valaddr);
  244.       elttype = TYPE_TARGET_TYPE (type);
  245.  
  246.       /* We assume a NULL pointer is all zeros ... */
  247.       if (addr == 0)
  248.     {
  249.       fputs_filtered ("NULL", stream);
  250.       return 0;
  251.     }
  252.       
  253.       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  254.     {
  255.       /* Try to print what function it points to.  */
  256.       print_address_demangle (addr, stream, demangle);
  257.       /* Return value is irrelevant except for string pointers.  */
  258.       return (0);
  259.     }
  260.       if (addressprint && format != 's')
  261.     {
  262.       print_address_numeric (addr, 1, stream);
  263.     }
  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_CHAR
  269.       && (format == 0 || format == 's')
  270.       && addr != 0
  271.       && /* If print_max is UINT_MAX, the alloca below will fail.
  272.         In that case don't try to print the string.  */
  273.       print_max < UINT_MAX)
  274.       {
  275.         i = val_print_string (addr, 0, stream);
  276.       }
  277.       /* Return number of characters printed, plus one for the
  278.      terminating null if we have "reached the end".  */
  279.       return (i + (print_max && i != print_max));
  280.       break;
  281.  
  282.     case TYPE_CODE_STRING:
  283.       if (format && format != 's')
  284.     {
  285.       print_scalar_formatted (valaddr, type, format, 0, stream);
  286.       break;
  287.     }
  288.       i = TYPE_LENGTH (type);
  289.       LA_PRINT_STRING (stream, valaddr, i, 0);
  290.       /* Return number of characters printed, plus one for the terminating
  291.      null if we have "reached the end".  */
  292.       return (i + (print_max && i != print_max));
  293.       break;
  294.  
  295.     case TYPE_CODE_BITSTRING:
  296.     case TYPE_CODE_SET:
  297.       elttype = TYPE_FIELD_TYPE (type, 0);
  298.       check_stub_type (elttype);
  299.       if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
  300.     {
  301.       fprintf_filtered (stream, "<incomplete type>");
  302.       gdb_flush (stream);
  303.       break;
  304.     }
  305.       {
  306.     struct type *range = elttype;
  307.     int low_bound = TYPE_LOW_BOUND (range);
  308.     int high_bound = TYPE_HIGH_BOUND (range);
  309.     int i;
  310.     int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
  311.     int need_comma = 0;
  312.  
  313.     if (is_bitstring)
  314.       fputs_filtered ("B'", stream);
  315.     else
  316.       fputs_filtered ("[", stream);
  317.     for (i = low_bound; i <= high_bound; i++)
  318.       {
  319.         int element = value_bit_index (type, valaddr, i);
  320.         if (is_bitstring)
  321.           fprintf_filtered (stream, "%d", element);
  322.         else if (element)
  323.           {
  324.         if (need_comma)
  325.           fputs_filtered (", ", stream);
  326.         print_type_scalar (TYPE_TARGET_TYPE (range), i, stream);
  327.         need_comma = 1;
  328.  
  329.         /* Look for a continuous range of true elements. */
  330.         if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
  331.           {
  332.             int j = i; /* j is the upper bound so far of the range */
  333.             fputs_filtered (":", stream);
  334.             while (i+1 <= high_bound
  335.                && value_bit_index (type, valaddr, ++i))
  336.               j = i;
  337.             print_type_scalar (TYPE_TARGET_TYPE (range), j, stream);
  338.           }
  339.           }
  340.       }
  341.     if (is_bitstring)
  342.       fputs_filtered ("'", stream);
  343.     else
  344.       fputs_filtered ("]", stream);
  345.       }
  346.       break;
  347.  
  348.     case TYPE_CODE_STRUCT:
  349.       if (chill_is_varying_struct (type))
  350.     {
  351.       struct type *inner = TYPE_FIELD_TYPE (type, 1);
  352.       long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
  353.       char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
  354.       
  355.       switch (TYPE_CODE (inner))
  356.         {
  357.         case TYPE_CODE_STRING:
  358.           if (length > TYPE_LENGTH (type))
  359.         {
  360.           fprintf_filtered (stream,
  361.                     "<dynamic length %d > static length %d>",
  362.                     length, TYPE_LENGTH (type));
  363.         }
  364.           LA_PRINT_STRING (stream, data_addr, length, 0);
  365.           return length;
  366.         default:
  367.           break;
  368.         }
  369.     }
  370.       chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
  371.                 0);
  372.       break;
  373.  
  374.     case TYPE_CODE_REF:
  375.       if (addressprint)
  376.         {
  377.       fprintf_filtered (stream, "LOC(");
  378.       print_address_numeric
  379.         (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
  380.          1,
  381.          stream);
  382.       fprintf_filtered (stream, ")");
  383.       if (deref_ref)
  384.         fputs_filtered (": ", stream);
  385.         }
  386.       /* De-reference the reference.  */
  387.       if (deref_ref)
  388.     {
  389.       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
  390.         {
  391.           value_ptr deref_val =
  392.         value_at
  393.           (TYPE_TARGET_TYPE (type),
  394.            unpack_pointer (lookup_pointer_type (builtin_type_void),
  395.                    valaddr));
  396.           val_print (VALUE_TYPE (deref_val),
  397.              VALUE_CONTENTS (deref_val),
  398.              VALUE_ADDRESS (deref_val), stream, format,
  399.              deref_ref, recurse + 1, pretty);
  400.         }
  401.       else
  402.         fputs_filtered ("???", stream);
  403.     }
  404.       break;
  405.  
  406.     case TYPE_CODE_ENUM:
  407.       c_val_print (type, valaddr, address, stream, format,
  408.            deref_ref, recurse, pretty);
  409.       break;
  410.  
  411.     case TYPE_CODE_RANGE:
  412.       if (TYPE_TARGET_TYPE (type))
  413.     chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
  414.              format, deref_ref, recurse, pretty);
  415.       break;
  416.  
  417.     case TYPE_CODE_MEMBER:
  418.     case TYPE_CODE_UNION:
  419.     case TYPE_CODE_FUNC:
  420.     case TYPE_CODE_VOID:
  421.     case TYPE_CODE_ERROR:
  422.     default:
  423.       /* Let's defer printing to the C printer, rather than
  424.      print an error message.  FIXME! */
  425.       c_val_print (type, valaddr, address, stream, format,
  426.            deref_ref, recurse, pretty);
  427.     }
  428.   gdb_flush (stream);
  429.   return (0);
  430. }
  431.  
  432. /* Mutually recursive subroutines of cplus_print_value and c_val_print to
  433.    print out a structure's fields: cp_print_value_fields and cplus_print_value.
  434.  
  435.    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
  436.    same meanings as in cplus_print_value and c_val_print.
  437.  
  438.    DONT_PRINT is an array of baseclass types that we
  439.    should not print, or zero if called from top level.  */
  440.  
  441. static void
  442. chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
  443.               dont_print)
  444.      struct type *type;
  445.      char *valaddr;
  446.      GDB_FILE *stream;
  447.      int format;
  448.      int recurse;
  449.      enum val_prettyprint pretty;
  450.      struct type **dont_print;
  451. {
  452.   int i, len;
  453.   int fields_seen = 0;
  454.  
  455.   check_stub_type (type);
  456.  
  457.   fprintf_filtered (stream, "[");
  458.   len = TYPE_NFIELDS (type);
  459.   if (len == 0)
  460.     {
  461.       fprintf_filtered (stream, "<No data fields>");
  462.     }
  463.   else
  464.     {
  465.       for (i = 0; i < len; i++)
  466.     {
  467.       if (fields_seen)
  468.         {
  469.           fprintf_filtered (stream, ", ");
  470.         }
  471.       fields_seen = 1;
  472.       if (pretty)
  473.         {
  474.           fprintf_filtered (stream, "\n");
  475.           print_spaces_filtered (2 + 2 * recurse, stream);
  476.         }
  477.       else 
  478.         {
  479.           wrap_here (n_spaces (2 + 2 * recurse));
  480.         }
  481.       fputs_filtered (".", stream);
  482.       fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
  483.                    language_chill, DMGL_NO_OPTS);
  484.       fputs_filtered (": ", stream);
  485.       if (TYPE_FIELD_PACKED (type, i))
  486.         {
  487.           value_ptr v;
  488.  
  489.           /* Bitfields require special handling, especially due to byte
  490.          order problems.  */
  491.           v = value_from_longest (TYPE_FIELD_TYPE (type, i),
  492.                       unpack_field_as_long (type, valaddr, i));
  493.  
  494.           chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
  495.                    stream, format, 0, recurse + 1, pretty);
  496.         }
  497.       else
  498.         {
  499.           chill_val_print (TYPE_FIELD_TYPE (type, i), 
  500.                    valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
  501.                    0, stream, format, 0, recurse + 1, pretty);
  502.         }
  503.     }
  504.       if (pretty)
  505.     {
  506.       fprintf_filtered (stream, "\n");
  507.       print_spaces_filtered (2 * recurse, stream);
  508.     }
  509.     }
  510.   fprintf_filtered (stream, "]");
  511. }
  512.  
  513. int
  514. chill_value_print (val, stream, format, pretty)
  515.      value_ptr val;
  516.      GDB_FILE *stream;
  517.      int format;
  518.      enum val_prettyprint pretty;
  519. {
  520.   /* A "repeated" value really contains several values in a row.
  521.      They are made by the @ operator.
  522.      Print such values as if they were arrays.  */
  523.  
  524.   if (VALUE_REPEATED (val))
  525.     {
  526.       register unsigned int n = VALUE_REPETITIONS (val);
  527.       register unsigned int typelen = TYPE_LENGTH (VALUE_TYPE (val));
  528.       fprintf_filtered (stream, "[");
  529.       /* Print arrays of characters using string syntax.  */
  530.       if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
  531.       && format == 0)
  532.     LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
  533.       else
  534.     {
  535.       value_print_array_elements (val, stream, format, pretty);
  536.     }
  537.       fprintf_filtered (stream, "]");
  538.       return (n * typelen);
  539.     }
  540.   else
  541.     {
  542.       struct type *type = VALUE_TYPE (val);
  543.  
  544.       /* If it is a pointer, indicate what it points to.
  545.  
  546.      Print type also if it is a reference.
  547.  
  548.          C++: if it is a member pointer, we will take care
  549.      of that when we print it.  */
  550.       if (TYPE_CODE (type) == TYPE_CODE_PTR ||
  551.       TYPE_CODE (type) == TYPE_CODE_REF)
  552.     {
  553.       char *valaddr = VALUE_CONTENTS (val);
  554.       CORE_ADDR addr = unpack_pointer (type, valaddr);
  555.           if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
  556.         {
  557.           int i;
  558.           char *name = TYPE_NAME (type);
  559.           if (name)
  560.         fputs_filtered (name, stream);
  561.           else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
  562.         fputs_filtered ("PTR", stream);
  563.           else
  564.         {
  565.           fprintf_filtered (stream, "(");
  566.           type_print (type, "", stream, -1);
  567.           fprintf_filtered (stream, ")");
  568.         }
  569.           fprintf_filtered (stream, "(");
  570.           i = val_print (type, valaddr, VALUE_ADDRESS (val),
  571.                  stream, format, 1, 0, pretty);
  572.           fprintf_filtered (stream, ")");
  573.           return i;
  574.         }
  575.     }
  576.       return (val_print (type, VALUE_CONTENTS (val),
  577.              VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
  578.     }
  579. }
  580.  
  581.  
  582.