GDB (xrefs)
Loading...
Searching...
No Matches
/tmp/gdb-13.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-2023 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#include "cli/cli-style.h"
33
34/* Print a Go string.
35
36 Note: We assume
37 gdb_assert (go_classify_struct_type (type) == GO_TYPE_STRING). */
38
39static void
41 LONGEST embedded_offset, CORE_ADDR address,
42 struct ui_file *stream, int recurse,
43 struct value *val,
44 const struct value_print_options *options)
45{
46 struct gdbarch *gdbarch = type->arch ();
47 struct type *elt_ptr_type = type->field (0).type ();
48 struct type *elt_type = elt_ptr_type->target_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;
55 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
56
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)
68 {
69 gdb_puts (paddress (gdbarch, addr), stream);
70 gdb_puts (" ", stream);
71 }
72
73 if (length < 0)
74 {
75 gdb_printf (_("<invalid length: %ps>"),
77 plongest (addr)));
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/* See go-lang.h. */
88
89void
90go_language::value_print_inner (struct value *val, struct ui_file *stream,
91 int recurse,
92 const struct value_print_options *options) const
93{
94 struct type *type = check_typedef (value_type (val));
95
96 switch (type->code ())
97 {
98 case TYPE_CODE_STRUCT:
99 {
101
102 switch (go_type)
103 {
104 case GO_TYPE_STRING:
105 if (! options->raw)
106 {
108 value_address (val),
109 stream, recurse, val, options);
110 return;
111 }
112 break;
113 default:
114 break;
115 }
116 }
117 /* Fall through. */
118
119 default:
120 c_value_print_inner (val, stream, recurse, options);
121 break;
122 }
123}
void c_value_print_inner(struct value *, struct ui_file *, int, const struct value_print_options *)
Definition: c-valprint.c:421
ui_file_style style() const
Definition: cli-style.c:169
void value_print_inner(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const override
Definition: go-valprint.c:90
cli_style_option metadata_style
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:3010
enum go_type go_classify_struct_type(struct type *type)
Definition: go-lang.c:121
go_type
Definition: go-lang.h:54
@ GO_TYPE_STRING
Definition: go-lang.h:56
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:40
struct type * type() const
Definition: gdbtypes.h:559
Definition: gdbtypes.h:922
struct type * target_type() const
Definition: gdbtypes.h:1000
type_code code() const
Definition: gdbtypes.h:927
ULONGEST length() const
Definition: gdbtypes.h:954
struct field & field(int idx) const
Definition: gdbtypes.h:983
gdbarch * arch() const
Definition: gdbtypes.c:245
Definition: value.c:181
static styled_string_s * styled_string(const ui_file_style &style, const char *str, styled_string_s &&tmp={})
Definition: ui-out.h:151
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: utils.c:3114
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition: utils.c:1865
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1788
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:2598
struct type * value_type(const struct value *value)
Definition: value.c:1109
LONGEST value_embedded_offset(const struct value *value)
Definition: value.c:1555
CORE_ADDR value_address(const struct value *value)
Definition: value.c:1607
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:3356
gdb::array_view< const gdb_byte > value_contents_for_printing(struct value *value)
Definition: value.c:1265