gdb/go-valprint.c - gdb

Functions defined

Source code

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

  2.    Copyright (C) 2012-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.    NOTE: This currently only provides special support for printing gccgo
  15.    strings.  6g objects are handled in Python.
  16.    The remaining gccgo types may also be handled in Python.
  17.    Strings are handled specially here, at least for now, in case the Python
  18.    support is unavailable.  */

  19. #include "defs.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcore.h"
  22. #include "go-lang.h"
  23. #include "c-lang.h"
  24. #include "valprint.h"

  25. /* Print a Go string.

  26.    Note: We assume
  27.    gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING).  */

  28. static void
  29. print_go_string (struct type *type, const gdb_byte *valaddr,
  30.                  int embedded_offset, CORE_ADDR address,
  31.                  struct ui_file *stream, int recurse,
  32.                  const struct value *val,
  33.                  const struct value_print_options *options)
  34. {
  35.   struct gdbarch *gdbarch = get_type_arch (type);
  36.   struct type *elt_ptr_type = TYPE_FIELD_TYPE (type, 0);
  37.   struct type *elt_type = TYPE_TARGET_TYPE (elt_ptr_type);
  38.   LONGEST length;
  39.   /* TODO(dje): The encapsulation of what a pointer is belongs in value.c.
  40.      I.e. If there's going to be unpack_pointer, there should be
  41.      unpack_value_field_as_pointer.  Do this until we can get
  42.      unpack_value_field_as_pointer.  */
  43.   LONGEST addr;

  44.   gdb_assert (valaddr == value_contents_for_printing_const (val));

  45.   if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 0,
  46.                                     val, &addr))
  47.     error (_("Unable to read string address"));

  48.   if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 1,
  49.                                     val, &length))
  50.     error (_("Unable to read string length"));

  51.   /* TODO(dje): Print address of struct or actual string?  */
  52.   if (options->addressprint)
  53.     {
  54.       fputs_filtered (paddress (gdbarch, addr), stream);
  55.       fputs_filtered (" ", stream);
  56.     }

  57.   if (length < 0)
  58.     {
  59.       fputs_filtered (_("<invalid length: "), stream);
  60.       fputs_filtered (plongest (addr), stream);
  61.       fputs_filtered (">", stream);
  62.       return;
  63.     }

  64.   /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING.
  65.      The target encoding is a global switch.
  66.      Either choice is problematic.  */
  67.   val_print_string (elt_type, NULL, addr, length, stream, options);
  68. }

  69. /* Implements the la_val_print routine for language Go.  */

  70. void
  71. go_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
  72.               CORE_ADDR address, struct ui_file *stream, int recurse,
  73.               const struct value *val,
  74.               const struct value_print_options *options)
  75. {
  76.   CHECK_TYPEDEF (type);

  77.   switch (TYPE_CODE (type))
  78.     {
  79.       case TYPE_CODE_STRUCT:
  80.         {
  81.           enum go_type go_type = go_classify_struct_type (type);

  82.           switch (go_type)
  83.             {
  84.             case GO_TYPE_STRING:
  85.               if (! options->raw)
  86.                 {
  87.                   print_go_string (type, valaddr, embedded_offset, address,
  88.                                    stream, recurse, val, options);
  89.                   return;
  90.                 }
  91.               break;
  92.             default:
  93.               break;
  94.             }
  95.         }
  96.         /* Fall through.  */

  97.       default:
  98.         c_val_print (type, valaddr, embedded_offset, address, stream,
  99.                      recurse, val, options);
  100.         break;
  101.     }
  102. }