GDB (API)
/home/stan/gdb/src/gdb/go-valprint.c
Go to the documentation of this file.
00001 /* Support for printing Go values for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020    NOTE: This currently only provides special support for printing gccgo
00021    strings.  6g objects are handled in Python.
00022    The remaining gccgo types may also be handled in Python.
00023    Strings are handled specially here, at least for now, in case the Python
00024    support is unavailable.  */
00025 
00026 #include "defs.h"
00027 #include "gdbtypes.h"
00028 #include "gdbcore.h"
00029 #include "go-lang.h"
00030 #include "c-lang.h"
00031 #include "valprint.h"
00032 
00033 /* Print a Go string.
00034 
00035    Note: We assume
00036    gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING).  */
00037 
00038 static void
00039 print_go_string (struct type *type, const gdb_byte *valaddr,
00040                  int embedded_offset, CORE_ADDR address,
00041                  struct ui_file *stream, int recurse,
00042                  const struct value *val,
00043                  const struct value_print_options *options)
00044 {
00045   struct gdbarch *gdbarch = get_type_arch (type);
00046   struct type *elt_ptr_type = TYPE_FIELD_TYPE (type, 0);
00047   struct type *elt_type = TYPE_TARGET_TYPE (elt_ptr_type);
00048   LONGEST length;
00049   /* TODO(dje): The encapsulation of what a pointer is belongs in value.c.
00050      I.e. If there's going to be unpack_pointer, there should be
00051      unpack_value_field_as_pointer.  Do this until we can get
00052      unpack_value_field_as_pointer.  */
00053   LONGEST addr;
00054 
00055   gdb_assert (valaddr == value_contents_for_printing_const (val));
00056 
00057   if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 0,
00058                                     val, &addr))
00059     error (_("Unable to read string address"));
00060 
00061   if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 1,
00062                                     val, &length))
00063     error (_("Unable to read string length"));
00064 
00065   /* TODO(dje): Print address of struct or actual string?  */
00066   if (options->addressprint)
00067     {
00068       fputs_filtered (paddress (gdbarch, addr), stream);
00069       fputs_filtered (" ", stream);
00070     }
00071 
00072   if (length < 0)
00073     {
00074       fputs_filtered (_("<invalid length: "), stream);
00075       fputs_filtered (plongest (addr), stream);
00076       fputs_filtered (">", stream);
00077       return;
00078     }
00079 
00080   /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING.
00081      The target encoding is a global switch.
00082      Either choice is problematic.  */
00083   val_print_string (elt_type, NULL, addr, length, stream, options);
00084 }
00085 
00086 /* Implements the la_val_print routine for language Go.  */
00087 
00088 void
00089 go_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
00090               CORE_ADDR address, struct ui_file *stream, int recurse,
00091               const struct value *val,
00092               const struct value_print_options *options)
00093 {
00094   CHECK_TYPEDEF (type);
00095 
00096   switch (TYPE_CODE (type))
00097     {
00098       case TYPE_CODE_STRUCT:
00099         {
00100           enum go_type go_type = go_classify_struct_type (type);
00101 
00102           switch (go_type)
00103             {
00104             case GO_TYPE_STRING:
00105               if (! options->raw)
00106                 {
00107                   print_go_string (type, valaddr, embedded_offset, address,
00108                                    stream, recurse, val, options);
00109                   return;
00110                 }
00111               break;
00112             default:
00113               break;
00114             }
00115         }
00116         /* Fall through.  */
00117 
00118       default:
00119         c_val_print (type, valaddr, embedded_offset, address, stream,
00120                      recurse, val, options);
00121         break;
00122     }
00123 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines