1*ef5ccd6cSJohn Marino /* Support for printing Go values for GDB, the GNU debugger.
2*ef5ccd6cSJohn Marino
3*ef5ccd6cSJohn Marino Copyright (C) 2012-2013 Free Software Foundation, Inc.
4*ef5ccd6cSJohn Marino
5*ef5ccd6cSJohn Marino This file is part of GDB.
6*ef5ccd6cSJohn Marino
7*ef5ccd6cSJohn Marino This program is free software; you can redistribute it and/or modify
8*ef5ccd6cSJohn Marino it under the terms of the GNU General Public License as published by
9*ef5ccd6cSJohn Marino the Free Software Foundation; either version 3 of the License, or
10*ef5ccd6cSJohn Marino (at your option) any later version.
11*ef5ccd6cSJohn Marino
12*ef5ccd6cSJohn Marino This program is distributed in the hope that it will be useful,
13*ef5ccd6cSJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*ef5ccd6cSJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*ef5ccd6cSJohn Marino GNU General Public License for more details.
16*ef5ccd6cSJohn Marino
17*ef5ccd6cSJohn Marino You should have received a copy of the GNU General Public License
18*ef5ccd6cSJohn Marino along with this program. If not, see <http://www.gnu.org/licenses/>.
19*ef5ccd6cSJohn Marino
20*ef5ccd6cSJohn Marino NOTE: This currently only provides special support for printing gccgo
21*ef5ccd6cSJohn Marino strings. 6g objects are handled in Python.
22*ef5ccd6cSJohn Marino The remaining gccgo types may also be handled in Python.
23*ef5ccd6cSJohn Marino Strings are handled specially here, at least for now, in case the Python
24*ef5ccd6cSJohn Marino support is unavailable. */
25*ef5ccd6cSJohn Marino
26*ef5ccd6cSJohn Marino #include "defs.h"
27*ef5ccd6cSJohn Marino #include "gdbtypes.h"
28*ef5ccd6cSJohn Marino #include "gdbcore.h"
29*ef5ccd6cSJohn Marino #include "go-lang.h"
30*ef5ccd6cSJohn Marino #include "c-lang.h"
31*ef5ccd6cSJohn Marino #include "valprint.h"
32*ef5ccd6cSJohn Marino
33*ef5ccd6cSJohn Marino /* Print a Go string.
34*ef5ccd6cSJohn Marino
35*ef5ccd6cSJohn Marino Note: We assume
36*ef5ccd6cSJohn Marino gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING). */
37*ef5ccd6cSJohn Marino
38*ef5ccd6cSJohn Marino static void
print_go_string(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options)39*ef5ccd6cSJohn Marino print_go_string (struct type *type, const gdb_byte *valaddr,
40*ef5ccd6cSJohn Marino int embedded_offset, CORE_ADDR address,
41*ef5ccd6cSJohn Marino struct ui_file *stream, int recurse,
42*ef5ccd6cSJohn Marino const struct value *val,
43*ef5ccd6cSJohn Marino const struct value_print_options *options)
44*ef5ccd6cSJohn Marino {
45*ef5ccd6cSJohn Marino struct gdbarch *gdbarch = get_type_arch (type);
46*ef5ccd6cSJohn Marino struct type *elt_ptr_type = TYPE_FIELD_TYPE (type, 0);
47*ef5ccd6cSJohn Marino struct type *elt_type = TYPE_TARGET_TYPE (elt_ptr_type);
48*ef5ccd6cSJohn Marino LONGEST length;
49*ef5ccd6cSJohn Marino /* TODO(dje): The encapsulation of what a pointer is belongs in value.c.
50*ef5ccd6cSJohn Marino I.e. If there's going to be unpack_pointer, there should be
51*ef5ccd6cSJohn Marino unpack_value_field_as_pointer. Do this until we can get
52*ef5ccd6cSJohn Marino unpack_value_field_as_pointer. */
53*ef5ccd6cSJohn Marino LONGEST addr;
54*ef5ccd6cSJohn Marino
55*ef5ccd6cSJohn Marino gdb_assert (valaddr == value_contents_for_printing_const (val));
56*ef5ccd6cSJohn Marino
57*ef5ccd6cSJohn Marino if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 0,
58*ef5ccd6cSJohn Marino val, &addr))
59*ef5ccd6cSJohn Marino error (_("Unable to read string address"));
60*ef5ccd6cSJohn Marino
61*ef5ccd6cSJohn Marino if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 1,
62*ef5ccd6cSJohn Marino val, &length))
63*ef5ccd6cSJohn Marino error (_("Unable to read string length"));
64*ef5ccd6cSJohn Marino
65*ef5ccd6cSJohn Marino /* TODO(dje): Print address of struct or actual string? */
66*ef5ccd6cSJohn Marino if (options->addressprint)
67*ef5ccd6cSJohn Marino {
68*ef5ccd6cSJohn Marino fputs_filtered (paddress (gdbarch, addr), stream);
69*ef5ccd6cSJohn Marino fputs_filtered (" ", stream);
70*ef5ccd6cSJohn Marino }
71*ef5ccd6cSJohn Marino
72*ef5ccd6cSJohn Marino if (length < 0)
73*ef5ccd6cSJohn Marino {
74*ef5ccd6cSJohn Marino fputs_filtered (_("<invalid length: "), stream);
75*ef5ccd6cSJohn Marino fputs_filtered (plongest (addr), stream);
76*ef5ccd6cSJohn Marino fputs_filtered (">", stream);
77*ef5ccd6cSJohn Marino return;
78*ef5ccd6cSJohn Marino }
79*ef5ccd6cSJohn Marino
80*ef5ccd6cSJohn Marino /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING.
81*ef5ccd6cSJohn Marino The target encoding is a global switch.
82*ef5ccd6cSJohn Marino Either choice is problematic. */
83*ef5ccd6cSJohn Marino val_print_string (elt_type, NULL, addr, length, stream, options);
84*ef5ccd6cSJohn Marino }
85*ef5ccd6cSJohn Marino
86*ef5ccd6cSJohn Marino /* Implements the la_val_print routine for language Go. */
87*ef5ccd6cSJohn Marino
88*ef5ccd6cSJohn Marino void
go_val_print(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options)89*ef5ccd6cSJohn Marino go_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
90*ef5ccd6cSJohn Marino CORE_ADDR address, struct ui_file *stream, int recurse,
91*ef5ccd6cSJohn Marino const struct value *val,
92*ef5ccd6cSJohn Marino const struct value_print_options *options)
93*ef5ccd6cSJohn Marino {
94*ef5ccd6cSJohn Marino CHECK_TYPEDEF (type);
95*ef5ccd6cSJohn Marino
96*ef5ccd6cSJohn Marino switch (TYPE_CODE (type))
97*ef5ccd6cSJohn Marino {
98*ef5ccd6cSJohn Marino case TYPE_CODE_STRUCT:
99*ef5ccd6cSJohn Marino {
100*ef5ccd6cSJohn Marino enum go_type go_type = go_classify_struct_type (type);
101*ef5ccd6cSJohn Marino
102*ef5ccd6cSJohn Marino switch (go_type)
103*ef5ccd6cSJohn Marino {
104*ef5ccd6cSJohn Marino case GO_TYPE_STRING:
105*ef5ccd6cSJohn Marino if (! options->raw)
106*ef5ccd6cSJohn Marino {
107*ef5ccd6cSJohn Marino print_go_string (type, valaddr, embedded_offset, address,
108*ef5ccd6cSJohn Marino stream, recurse, val, options);
109*ef5ccd6cSJohn Marino return;
110*ef5ccd6cSJohn Marino }
111*ef5ccd6cSJohn Marino break;
112*ef5ccd6cSJohn Marino default:
113*ef5ccd6cSJohn Marino break;
114*ef5ccd6cSJohn Marino }
115*ef5ccd6cSJohn Marino }
116*ef5ccd6cSJohn Marino /* Fall through. */
117*ef5ccd6cSJohn Marino
118*ef5ccd6cSJohn Marino default:
119*ef5ccd6cSJohn Marino c_val_print (type, valaddr, embedded_offset, address, stream,
120*ef5ccd6cSJohn Marino recurse, val, options);
121*ef5ccd6cSJohn Marino break;
122*ef5ccd6cSJohn Marino }
123*ef5ccd6cSJohn Marino }
124