]>
Commit | Line | Data |
---|---|---|
a766d390 DE |
1 | /* Support for printing Go values for GDB, the GNU debugger. |
2 | ||
1d506c26 | 3 | Copyright (C) 2012-2024 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 | ||
a766d390 DE |
26 | #include "gdbtypes.h" |
27 | #include "gdbcore.h" | |
28 | #include "go-lang.h" | |
29 | #include "c-lang.h" | |
30 | #include "valprint.h" | |
2dbc041e | 31 | #include "cli/cli-style.h" |
a766d390 DE |
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 | |
e8b24d9f | 39 | print_go_string (struct type *type, |
6b850546 | 40 | LONGEST embedded_offset, CORE_ADDR address, |
a766d390 | 41 | struct ui_file *stream, int recurse, |
e8b24d9f | 42 | struct value *val, |
a766d390 DE |
43 | const struct value_print_options *options) |
44 | { | |
8ee511af | 45 | struct gdbarch *gdbarch = type->arch (); |
940da03e | 46 | struct type *elt_ptr_type = type->field (0).type (); |
27710edb | 47 | struct type *elt_type = elt_ptr_type->target_type (); |
a766d390 DE |
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; | |
efaf1ae0 | 54 | const gdb_byte *valaddr = val->contents_for_printing ().data (); |
a766d390 | 55 | |
a766d390 DE |
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) | |
b012acdd | 67 | { |
0426ad51 TT |
68 | gdb_puts (paddress (gdbarch, addr), stream); |
69 | gdb_puts (" ", stream); | |
b012acdd | 70 | } |
a766d390 DE |
71 | |
72 | if (length < 0) | |
73 | { | |
6cb06a8c TT |
74 | gdb_printf (_("<invalid length: %ps>"), |
75 | styled_string (metadata_style.style (), | |
76 | plongest (addr))); | |
a766d390 DE |
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 | ||
23b0f06b TT |
86 | /* See go-lang.h. */ |
87 | ||
88 | void | |
82fc57fd AB |
89 | go_language::value_print_inner (struct value *val, struct ui_file *stream, |
90 | int recurse, | |
91 | const struct value_print_options *options) const | |
23b0f06b | 92 | { |
d0c97917 | 93 | struct type *type = check_typedef (val->type ()); |
23b0f06b | 94 | |
78134374 | 95 | switch (type->code ()) |
23b0f06b TT |
96 | { |
97 | case TYPE_CODE_STRUCT: | |
98 | { | |
99 | enum go_type go_type = go_classify_struct_type (type); | |
100 | ||
101 | switch (go_type) | |
102 | { | |
103 | case GO_TYPE_STRING: | |
104 | if (! options->raw) | |
105 | { | |
391f8628 | 106 | print_go_string (type, val->embedded_offset (), |
9feb2d07 | 107 | val->address (), |
23b0f06b TT |
108 | stream, recurse, val, options); |
109 | return; | |
110 | } | |
111 | break; | |
112 | default: | |
113 | break; | |
114 | } | |
115 | } | |
d182e398 | 116 | [[fallthrough]]; |
23b0f06b TT |
117 | |
118 | default: | |
119 | c_value_print_inner (val, stream, recurse, options); | |
120 | break; | |
121 | } | |
122 | } |