Initial pass at Go language support.
[deliverable/binutils-gdb.git] / gdb / go-valprint.c
1 /* Support for printing Go values for GDB, the GNU debugger.
2
3 Copyright (C) 2012 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 3 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, see <http://www.gnu.org/licenses/>.
19
20 NOTE: This currently only provides special support for printing gccgo
21 strings. 6g objects are handled in Python.
22 The remaining gccgo types may also be handled in Python.
23 Strings are handled specially here, at least for now, in case the Python
24 support is unavailable. */
25
26 #include "defs.h"
27 #include "gdbtypes.h"
28 #include "gdbcore.h"
29 #include "go-lang.h"
30 #include "c-lang.h"
31 #include "valprint.h"
32
33 /* Print a Go string.
34
35 Note: We assume
36 gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING). */
37
38 static void
39 print_go_string (struct type *type, const gdb_byte *valaddr,
40 int embedded_offset, CORE_ADDR address,
41 struct ui_file *stream, int recurse,
42 const struct value *val,
43 const struct value_print_options *options)
44 {
45 struct gdbarch *gdbarch = get_type_arch (type);
46 struct type *elt_ptr_type = TYPE_FIELD_TYPE (type, 0);
47 struct type *elt_type = TYPE_TARGET_TYPE (elt_ptr_type);
48 LONGEST length;
49 /* TODO(dje): The encapsulation of what a pointer is belongs in value.c.
50 I.e. If there's going to be unpack_pointer, there should be
51 unpack_value_field_as_pointer. Do this until we can get
52 unpack_value_field_as_pointer. */
53 LONGEST addr;
54
55 gdb_assert (valaddr == value_contents_for_printing_const (val));
56
57 if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 0,
58 val, &addr))
59 error (_("Unable to read string address"));
60
61 if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 1,
62 val, &length))
63 error (_("Unable to read string length"));
64
65 /* TODO(dje): Print address of struct or actual string? */
66 if (options->addressprint)
67 fputs_filtered (paddress (gdbarch, addr), stream);
68
69 if (length < 0)
70 {
71 fputs_filtered (_("<invalid length: "), stream);
72 fputs_filtered (plongest (addr), stream);
73 fputs_filtered (">", stream);
74 return;
75 }
76
77 /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING.
78 The target encoding is a global switch.
79 Either choice is problematic. */
80 val_print_string (elt_type, NULL, addr, length, stream, options);
81 }
82
83 /* Implements the la_val_print routine for language Go. */
84
85 void
86 go_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
87 CORE_ADDR address, struct ui_file *stream, int recurse,
88 const struct value *val,
89 const struct value_print_options *options)
90 {
91 CHECK_TYPEDEF (type);
92
93 switch (TYPE_CODE (type))
94 {
95 case TYPE_CODE_STRUCT:
96 {
97 enum go_type go_type = go_classify_struct_type (type);
98
99 switch (go_type)
100 {
101 case GO_TYPE_STRING:
102 if (! options->raw)
103 {
104 print_go_string (type, valaddr, embedded_offset, address,
105 stream, recurse, val, options);
106 return;
107 }
108 break;
109 default:
110 break;
111 }
112 }
113 /* Fall through. */
114
115 default:
116 c_val_print (type, valaddr, embedded_offset, address, stream,
117 recurse, val, options);
118 break;
119 }
120 }
This page took 0.031991 seconds and 4 git commands to generate.