gdb/jv-valprint.c - gdb

Functions defined

Source code

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

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "gdbcore.h"
  18. #include "expression.h"
  19. #include "value.h"
  20. #include "demangle.h"
  21. #include "valprint.h"
  22. #include "language.h"
  23. #include "jv-lang.h"
  24. #include "c-lang.h"
  25. #include "annotate.h"
  26. /* Local functions */

  27. void
  28. java_value_print (struct value *val, struct ui_file *stream,
  29.                   const struct value_print_options *options)
  30. {
  31.   struct gdbarch *gdbarch = get_type_arch (value_type (val));
  32.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  33.   struct type *type;
  34.   CORE_ADDR address;
  35.   int i;
  36.   const char *name;
  37.   struct value_print_options opts;

  38.   type = value_type (val);
  39.   address = value_address (val);

  40.   if (is_object_type (type))
  41.     {
  42.       CORE_ADDR obj_addr;
  43.       struct value *tem = val;

  44.       /* Get the run-time type, and cast the object into that.  */
  45.       while (TYPE_CODE (value_type (tem)) == TYPE_CODE_PTR)
  46.         tem = value_ind (tem);

  47.       obj_addr = value_address (tem);

  48.       if (obj_addr != 0)
  49.         {
  50.           type = type_from_class (gdbarch, java_class_from_object (val));
  51.           type = lookup_pointer_type (type);

  52.           val = value_at (type, address);
  53.           type = value_type (val);
  54.         }
  55.     }

  56.   if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
  57.     type_print (TYPE_TARGET_TYPE (type), "", stream, -1);

  58.   name = TYPE_TAG_NAME (type);
  59.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
  60.       && (i = strlen (name), name[i - 1] == ']'))
  61.     {
  62.       gdb_byte buf4[4];
  63.       long length;
  64.       unsigned int things_printed = 0;
  65.       int reps;
  66.       struct type *el_type
  67.         = java_primitive_type_from_name (gdbarch, name, i - 2);

  68.       i = 0;
  69.       read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);

  70.       length = (long) extract_signed_integer (buf4, 4, byte_order);
  71.       fprintf_filtered (stream, "{length: %ld", length);

  72.       if (el_type == NULL)
  73.         {
  74.           CORE_ADDR element;
  75.           CORE_ADDR next_element = -1; /* Dummy initial value.  */

  76.           /* Skip object header and length.  */
  77.           address += get_java_object_header_size (gdbarch) + 4;

  78.           while (i < length && things_printed < options->print_max)
  79.             {
  80.               gdb_byte *buf;

  81.               buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
  82.               fputs_filtered (", ", stream);
  83.               wrap_here (n_spaces (2));

  84.               if (i > 0)
  85.                 element = next_element;
  86.               else
  87.                 {
  88.                   read_memory (address, buf, sizeof (buf));
  89.                   address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
  90.                   /* FIXME: cagney/2003-05-24: Bogus or what.  It
  91.                      pulls a host sized pointer out of the target and
  92.                      then extracts that as an address (while assuming
  93.                      that the address is unsigned)!  */
  94.                   element = extract_unsigned_integer (buf, sizeof (buf),
  95.                                                       byte_order);
  96.                 }

  97.               for (reps = 1; i + reps < length; reps++)
  98.                 {
  99.                   read_memory (address, buf, sizeof (buf));
  100.                   address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
  101.                   /* FIXME: cagney/2003-05-24: Bogus or what.  It
  102.                      pulls a host sized pointer out of the target and
  103.                      then extracts that as an address (while assuming
  104.                      that the address is unsigned)!  */
  105.                   next_element = extract_unsigned_integer (buf, sizeof (buf),
  106.                                                            byte_order);
  107.                   if (next_element != element)
  108.                     break;
  109.                 }

  110.               if (reps == 1)
  111.                 fprintf_filtered (stream, "%d: ", i);
  112.               else
  113.                 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);

  114.               if (element == 0)
  115.                 fprintf_filtered (stream, "null");
  116.               else
  117.                 fprintf_filtered (stream, "@%s", paddress (gdbarch, element));

  118.               things_printed++;
  119.               i += reps;
  120.             }
  121.         }
  122.       else
  123.         {
  124.           struct value *v = allocate_value (el_type);
  125.           struct value *next_v = allocate_value (el_type);

  126.           set_value_address (v, (address
  127.                                  + get_java_object_header_size (gdbarch) + 4));
  128.           set_value_address (next_v, value_raw_address (v));

  129.           while (i < length && things_printed < options->print_max)
  130.             {
  131.               fputs_filtered (", ", stream);
  132.               wrap_here (n_spaces (2));

  133.               if (i > 0)
  134.                 {
  135.                   struct value *tmp;

  136.                   tmp = next_v;
  137.                   next_v = v;
  138.                   v = tmp;
  139.                 }
  140.               else
  141.                 {
  142.                   set_value_lazy (v, 1);
  143.                   set_value_offset (v, 0);
  144.                 }

  145.               set_value_offset (next_v, value_offset (v));

  146.               for (reps = 1; i + reps < length; reps++)
  147.                 {
  148.                   set_value_lazy (next_v, 1);
  149.                   set_value_offset (next_v, value_offset (next_v)
  150.                                     + TYPE_LENGTH (el_type));
  151.                   value_fetch_lazy (next_v);
  152.                   if (!value_contents_eq (v, value_embedded_offset (v),
  153.                                           next_v,
  154.                                           value_embedded_offset (next_v),
  155.                                           TYPE_LENGTH (el_type)))
  156.                     break;
  157.                 }

  158.               if (reps == 1)
  159.                 fprintf_filtered (stream, "%d: ", i);
  160.               else
  161.                 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);

  162.               opts = *options;
  163.               opts.deref_ref = 1;
  164.               common_val_print (v, stream, 1, &opts, current_language);

  165.               things_printed++;
  166.               i += reps;
  167.             }
  168.         }

  169.       if (i < length)
  170.         fprintf_filtered (stream, "...");

  171.       fprintf_filtered (stream, "}");

  172.       return;
  173.     }

  174.   /* If it's type String, print it.  */

  175.   if (TYPE_CODE (type) == TYPE_CODE_PTR
  176.       && TYPE_TARGET_TYPE (type)
  177.       && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
  178.       && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
  179.                  "java.lang.String") == 0
  180.       && (options->format == 0 || options->format == 's')
  181.       && address != 0
  182.       && value_as_address (val) != 0)
  183.     {
  184.       struct type *char_type;
  185.       struct value *data_val;
  186.       CORE_ADDR data;
  187.       struct value *boffset_val;
  188.       unsigned long boffset;
  189.       struct value *count_val;
  190.       unsigned long count;
  191.       struct value *mark;

  192.       fputs_filtered (" ", stream);

  193.       mark = value_mark ();        /* Remember start of new values.  */

  194.       data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
  195.       data = value_as_address (data_val);

  196.       boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
  197.       boffset = value_as_address (boffset_val);

  198.       count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
  199.       count = value_as_address (count_val);

  200.       value_free_to_mark (mark);        /* Release unnecessary values.  */

  201.       char_type = builtin_java_type (gdbarch)->builtin_char;
  202.       val_print_string (char_type, NULL, data + boffset, count, stream,
  203.                         options);

  204.       return;
  205.     }

  206.   opts = *options;
  207.   opts.deref_ref = 1;
  208.   common_val_print (val, stream, 0, &opts, current_language);
  209. }

  210. /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
  211.    same meanings as in cp_print_value and c_val_print.

  212.    DONT_PRINT is an array of baseclass types that we
  213.    should not print, or zero if called from top level.  */

  214. static void
  215. java_print_value_fields (struct type *type, const gdb_byte *valaddr,
  216.                          int offset,
  217.                          CORE_ADDR address, struct ui_file *stream,
  218.                          int recurse,
  219.                          const struct value *val,
  220.                          const struct value_print_options *options)
  221. {
  222.   int i, len, n_baseclasses;

  223.   CHECK_TYPEDEF (type);

  224.   fprintf_filtered (stream, "{");
  225.   len = TYPE_NFIELDS (type);
  226.   n_baseclasses = TYPE_N_BASECLASSES (type);

  227.   if (n_baseclasses > 0)
  228.     {
  229.       int i, n_baseclasses = TYPE_N_BASECLASSES (type);

  230.       for (i = 0; i < n_baseclasses; i++)
  231.         {
  232.           int boffset;
  233.           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  234.           const char *basename = TYPE_NAME (baseclass);
  235.           const gdb_byte *base_valaddr;

  236.           if (BASETYPE_VIA_VIRTUAL (type, i))
  237.             continue;

  238.           if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
  239.             continue;

  240.           boffset = 0;

  241.           if (options->prettyformat)
  242.             {
  243.               fprintf_filtered (stream, "\n");
  244.               print_spaces_filtered (2 * (recurse + 1), stream);
  245.             }
  246.           fputs_filtered ("<", stream);
  247.           /* Not sure what the best notation is in the case where there is no
  248.              baseclass name.  */
  249.           fputs_filtered (basename ? basename : "", stream);
  250.           fputs_filtered ("> = ", stream);

  251.           base_valaddr = valaddr;

  252.           java_print_value_fields (baseclass, base_valaddr,
  253.                                    offset + boffset, address,
  254.                                    stream, recurse + 1, val, options);
  255.           fputs_filtered (", ", stream);
  256.         }
  257.     }

  258.   if (!len && n_baseclasses == 1)
  259.     fprintf_filtered (stream, "<No data fields>");
  260.   else
  261.     {
  262.       int fields_seen = 0;

  263.       for (i = n_baseclasses; i < len; i++)
  264.         {
  265.           /* If requested, skip printing of static fields.  */
  266.           if (field_is_static (&TYPE_FIELD (type, i)))
  267.             {
  268.               const char *name = TYPE_FIELD_NAME (type, i);

  269.               if (!options->static_field_print)
  270.                 continue;
  271.               if (name != NULL && strcmp (name, "class") == 0)
  272.                 continue;
  273.             }
  274.           if (fields_seen)
  275.             fprintf_filtered (stream, ", ");
  276.           else if (n_baseclasses > 0)
  277.             {
  278.               if (options->prettyformat)
  279.                 {
  280.                   fprintf_filtered (stream, "\n");
  281.                   print_spaces_filtered (2 + 2 * recurse, stream);
  282.                   fputs_filtered ("members of ", stream);
  283.                   fputs_filtered (type_name_no_tag (type), stream);
  284.                   fputs_filtered (": ", stream);
  285.                 }
  286.             }
  287.           fields_seen = 1;

  288.           if (options->prettyformat)
  289.             {
  290.               fprintf_filtered (stream, "\n");
  291.               print_spaces_filtered (2 + 2 * recurse, stream);
  292.             }
  293.           else
  294.             {
  295.               wrap_here (n_spaces (2 + 2 * recurse));
  296.             }

  297.           annotate_field_begin (TYPE_FIELD_TYPE (type, i));

  298.           if (field_is_static (&TYPE_FIELD (type, i)))
  299.             fputs_filtered ("static ", stream);
  300.           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
  301.                                    language_cplus,
  302.                                    DMGL_PARAMS | DMGL_ANSI);
  303.           annotate_field_name_end ();
  304.           fputs_filtered (": ", stream);
  305.           annotate_field_value ();

  306.           if (!field_is_static (&TYPE_FIELD (type, i))
  307.               && TYPE_FIELD_PACKED (type, i))
  308.             {
  309.               struct value *v;

  310.               /* Bitfields require special handling, especially due to byte
  311.                  order problems.  */
  312.               if (TYPE_FIELD_IGNORE (type, i))
  313.                 {
  314.                   fputs_filtered ("<optimized out or zero length>", stream);
  315.                 }
  316.               else if (value_bits_synthetic_pointer (val,
  317.                                                      TYPE_FIELD_BITPOS (type,
  318.                                                                         i),
  319.                                                      TYPE_FIELD_BITSIZE (type,
  320.                                                                          i)))
  321.                 {
  322.                   fputs_filtered (_("<synthetic pointer>"), stream);
  323.                 }
  324.               else
  325.                 {
  326.                   struct value_print_options opts;

  327.                   v = value_field_bitfield (type, i, valaddr, offset, val);

  328.                   opts = *options;
  329.                   opts.deref_ref = 0;
  330.                   common_val_print (v, stream, recurse + 1,
  331.                                     &opts, current_language);
  332.                 }
  333.             }
  334.           else
  335.             {
  336.               if (TYPE_FIELD_IGNORE (type, i))
  337.                 {
  338.                   fputs_filtered ("<optimized out or zero length>", stream);
  339.                 }
  340.               else if (field_is_static (&TYPE_FIELD (type, i)))
  341.                 {
  342.                   struct value_print_options opts;
  343.                   struct value *v = value_static_field (type, i);
  344.                   struct type *t = check_typedef (value_type (v));

  345.                   if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
  346.                     v = value_addr (v);
  347.                   opts = *options;
  348.                   opts.deref_ref = 0;
  349.                   common_val_print (v, stream, recurse + 1,
  350.                                     &opts, current_language);
  351.                 }
  352.               else if (TYPE_FIELD_TYPE (type, i) == NULL)
  353.                 fputs_filtered ("<unknown type>", stream);
  354.               else
  355.                 {
  356.                   struct value_print_options opts = *options;

  357.                   opts.deref_ref = 0;
  358.                   val_print (TYPE_FIELD_TYPE (type, i),
  359.                              valaddr,
  360.                              offset + TYPE_FIELD_BITPOS (type, i) / 8,
  361.                              address, stream, recurse + 1, val, &opts,
  362.                              current_language);
  363.                 }
  364.             }
  365.           annotate_field_end ();
  366.         }

  367.       if (options->prettyformat)
  368.         {
  369.           fprintf_filtered (stream, "\n");
  370.           print_spaces_filtered (2 * recurse, stream);
  371.         }
  372.     }
  373.   fprintf_filtered (stream, "}");
  374. }

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

  377. void
  378. java_val_print (struct type *type, const gdb_byte *valaddr,
  379.                 int embedded_offset, CORE_ADDR address,
  380.                 struct ui_file *stream, int recurse,
  381.                 const struct value *val,
  382.                 const struct value_print_options *options)
  383. {
  384.   struct gdbarch *gdbarch = get_type_arch (type);
  385.   struct type *target_type;
  386.   CORE_ADDR addr;

  387.   CHECK_TYPEDEF (type);
  388.   switch (TYPE_CODE (type))
  389.     {
  390.     case TYPE_CODE_PTR:
  391.       if (options->format && options->format != 's')
  392.         {
  393.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  394.                                       val, options, 0, stream);
  395.           break;
  396.         }
  397.       addr = unpack_pointer (type, valaddr + embedded_offset);
  398.       if (addr == 0)
  399.         {
  400.           fputs_filtered ("null", stream);
  401.           return;
  402.         }
  403.       target_type = check_typedef (TYPE_TARGET_TYPE (type));

  404.       if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
  405.         {
  406.           /* Try to print what function it points to.  */
  407.           print_address_demangle (options, gdbarch, addr, stream, demangle);
  408.           return;
  409.         }

  410.       if (options->addressprint && options->format != 's')
  411.         {
  412.           fputs_filtered ("@", stream);
  413.           print_longest (stream, 'x', 0, (ULONGEST) addr);
  414.         }

  415.       return;

  416.     case TYPE_CODE_CHAR:
  417.     case TYPE_CODE_INT:
  418.       /* Can't just call c_val_print because that prints bytes as C
  419.          chars.  */
  420.       if (options->format || options->output_format)
  421.         {
  422.           struct value_print_options opts = *options;

  423.           opts.format = (options->format ? options->format
  424.                          : options->output_format);
  425.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  426.                                       val, &opts, 0, stream);
  427.         }
  428.       else if (TYPE_CODE (type) == TYPE_CODE_CHAR
  429.                || (TYPE_CODE (type) == TYPE_CODE_INT
  430.                    && TYPE_LENGTH (type) == 2
  431.                    && strcmp (TYPE_NAME (type), "char") == 0))
  432.         LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset),
  433.                        type, stream);
  434.       else
  435.         val_print_type_code_int (type, valaddr + embedded_offset, stream);
  436.       break;

  437.     case TYPE_CODE_STRUCT:
  438.       java_print_value_fields (type, valaddr, embedded_offset,
  439.                                address, stream, recurse, val, options);
  440.       break;

  441.     default:
  442.       c_val_print (type, valaddr, embedded_offset, address, stream,
  443.                    recurse, val, options);
  444.       break;
  445.     }
  446. }