Commit | Line | Data |
---|---|---|
a766d390 DE |
1 | /* Support for printing Go values for GDB, the GNU debugger. |
2 | ||
42a4f53d | 3 | Copyright (C) 2012-2019 Free Software Foundation, Inc. |
a766d390 DE |
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" | |
2dbc041e | 32 | #include "cli/cli-style.h" |
a766d390 DE |
33 | |
34 | /* Print a Go string. | |
35 | ||
36 | Note: We assume | |
37 | gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING). */ | |
38 | ||
39 | static void | |
e8b24d9f | 40 | print_go_string (struct type *type, |
6b850546 | 41 | LONGEST embedded_offset, CORE_ADDR address, |
a766d390 | 42 | struct ui_file *stream, int recurse, |
e8b24d9f | 43 | struct value *val, |
a766d390 DE |
44 | const struct value_print_options *options) |
45 | { | |
46 | struct gdbarch *gdbarch = get_type_arch (type); | |
47 | struct type *elt_ptr_type = TYPE_FIELD_TYPE (type, 0); | |
48 | struct type *elt_type = TYPE_TARGET_TYPE (elt_ptr_type); | |
49 | LONGEST length; | |
50 | /* TODO(dje): The encapsulation of what a pointer is belongs in value.c. | |
51 | I.e. If there's going to be unpack_pointer, there should be | |
52 | unpack_value_field_as_pointer. Do this until we can get | |
53 | unpack_value_field_as_pointer. */ | |
54 | LONGEST addr; | |
e8b24d9f | 55 | const gdb_byte *valaddr = value_contents_for_printing (val); |
a766d390 | 56 | |
a766d390 DE |
57 | |
58 | if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 0, | |
59 | val, &addr)) | |
60 | error (_("Unable to read string address")); | |
61 | ||
62 | if (! unpack_value_field_as_long (type, valaddr, embedded_offset, 1, | |
63 | val, &length)) | |
64 | error (_("Unable to read string length")); | |
65 | ||
66 | /* TODO(dje): Print address of struct or actual string? */ | |
67 | if (options->addressprint) | |
b012acdd TT |
68 | { |
69 | fputs_filtered (paddress (gdbarch, addr), stream); | |
70 | fputs_filtered (" ", stream); | |
71 | } | |
a766d390 DE |
72 | |
73 | if (length < 0) | |
74 | { | |
2dbc041e TT |
75 | printf_filtered (_("<invalid length: %ps>"), |
76 | styled_string (metadata_style.style (), | |
77 | plongest (addr))); | |
a766d390 DE |
78 | return; |
79 | } | |
80 | ||
81 | /* TODO(dje): Perhaps we should pass "UTF8" for ENCODING. | |
82 | The target encoding is a global switch. | |
83 | Either choice is problematic. */ | |
84 | val_print_string (elt_type, NULL, addr, length, stream, options); | |
85 | } | |
86 | ||
87 | /* Implements the la_val_print routine for language Go. */ | |
88 | ||
89 | void | |
e8b24d9f | 90 | go_val_print (struct type *type, int embedded_offset, |
a766d390 | 91 | CORE_ADDR address, struct ui_file *stream, int recurse, |
e8b24d9f | 92 | struct value *val, |
a766d390 DE |
93 | const struct value_print_options *options) |
94 | { | |
f168693b | 95 | type = check_typedef (type); |
a766d390 DE |
96 | |
97 | switch (TYPE_CODE (type)) | |
98 | { | |
99 | case TYPE_CODE_STRUCT: | |
100 | { | |
101 | enum go_type go_type = go_classify_struct_type (type); | |
102 | ||
103 | switch (go_type) | |
104 | { | |
105 | case GO_TYPE_STRING: | |
106 | if (! options->raw) | |
107 | { | |
e8b24d9f | 108 | print_go_string (type, embedded_offset, address, |
a766d390 DE |
109 | stream, recurse, val, options); |
110 | return; | |
111 | } | |
112 | break; | |
113 | default: | |
114 | break; | |
115 | } | |
116 | } | |
117 | /* Fall through. */ | |
118 | ||
119 | default: | |
e8b24d9f | 120 | c_val_print (type, embedded_offset, address, stream, |
a766d390 DE |
121 | recurse, val, options); |
122 | break; | |
123 | } | |
124 | } |