GDB (xrefs)

/tmp/gdb-8.1/gdb/go-valprint.c
Go to the documentation of this file.
1 /* Support for printing Go values for GDB, the GNU debugger.
2
3 Copyright (C) 2012-2018 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,
40 LONGEST embedded_offset, CORE_ADDR address,
41 struct ui_file *stream, int recurse,
42 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 const gdb_byte *valaddr = value_contents_for_printing (val);
55
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 {
68 fputs_filtered (paddress (gdbarch, addr), stream);
69 fputs_filtered (" ", stream);
70 }
71
72 if (length < 0)
73 {
74 fputs_filtered (_("<invalid length: "), stream);
75 fputs_filtered (plongest (addr), stream);
76 fputs_filtered (">", stream);
77 return;
78 }
79
80 /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING.
81 The target encoding is a global switch.
82 Either choice is problematic. */
83 val_print_string (elt_type, NULL, addr, length, stream, options);
84 }
85
86 /* Implements the la_val_print routine for language Go. */
87
88 void
89 go_val_print (struct type *type, int embedded_offset,
90 CORE_ADDR address, struct ui_file *stream, int recurse,
91 struct value *val,
92 const struct value_print_options *options)
93 {
94 type = check_typedef (type);
95
96 switch (TYPE_CODE (type))
97 {
98 case TYPE_CODE_STRUCT:
99 {
100 enum go_type go_type = go_classify_struct_type (type);
101
102 switch (go_type)
103 {
104 case GO_TYPE_STRING:
105 if (! options->raw)
106 {
107 print_go_string (type, embedded_offset, address,
108 stream, recurse, val, options);
109 return;
110 }
111 break;
112 default:
113 break;
114 }
115 }
116 /* Fall through. */
117
118 default:
119 c_val_print (type, embedded_offset, address, stream,
120 recurse, val, options);
121 break;
122 }
123 }
gdbarch
Definition: gdbarch.c:127
type::length
unsigned int length
Definition: gdbtypes.h:803
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
plongest
char * plongest(LONGEST l)
Definition: print-utils.c:137
_
#define _(String)
Definition: gdb_locale.h:35
value_contents_for_printing
const gdb_byte * value_contents_for_printing(struct value *value)
Definition: value.c:1250
ui_file
Definition: ui-file.h:26
defs.h
TYPE_FIELD_TYPE
#define TYPE_FIELD_TYPE(thistype, n)
Definition: gdbtypes.h:1371
paddress
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: utils.c:2745
check_typedef
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
value_print_options
Definition: valprint.h:25
fputs_filtered
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
type
Definition: gdbtypes.h:749
get_type_arch
struct gdbarch * get_type_arch(const struct type *type)
Definition: gdbtypes.c:234
unpack_value_field_as_long
int unpack_value_field_as_long(struct type *type, const gdb_byte *valaddr, LONGEST embedded_offset, int fieldno, const struct value *val, LONGEST *result)
Definition: value.c:3293
value_print_options::addressprint
int addressprint
Definition: valprint.h:43
go_classify_struct_type
enum go_type go_classify_struct_type(struct type *type)
Definition: go-lang.c:120
c_val_print
void c_val_print(struct type *, int, CORE_ADDR, struct ui_file *, int, struct value *, const struct value_print_options *)
Definition: c-valprint.c:500
TYPE_CODE_STRUCT
Definition: gdbtypes.h:106
value
Definition: value.c:169
go_val_print
void go_val_print(struct type *type, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *val, const struct value_print_options *options)
Definition: go-valprint.c:89
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
go_type
go_type
Definition: go-lang.h:53
TYPE_TARGET_TYPE
#define TYPE_TARGET_TYPE(thistype)
Definition: gdbtypes.h:1226
gdbcore.h
TYPE_CODE
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
c-lang.h
go-lang.h
gdbtypes.h
value_print_options::raw
int raw
Definition: valprint.h:85
val_print_string
int val_print_string(struct type *elttype, const char *encoding, CORE_ADDR addr, int len, struct ui_file *stream, const struct value_print_options *options)
Definition: valprint.c:2795
print_go_string
static void print_go_string(struct type *type, LONGEST embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *val, const struct value_print_options *options)
Definition: go-valprint.c:39
valprint.h
error
void error(const char *fmt,...)
Definition: errors.c:38
LONGEST
long long LONGEST
Definition: common-types.h:52
GO_TYPE_STRING
Definition: go-lang.h:56