GDB (xrefs)
Loading...
Searching...
No Matches
cp-abi.c
Go to the documentation of this file.
1/* Generic code for supporting multiple C++ ABI's
2
3 Copyright (C) 2001-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#include "defs.h"
21#include "language.h"
22#include "value.h"
23#include "cp-abi.h"
24#include "command.h"
25#include "gdbcmd.h"
26#include "ui-out.h"
27static struct cp_abi_ops *find_cp_abi (const char *short_name);
28
29static struct cp_abi_ops current_cp_abi = { "", NULL };
30static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
31
32#define CP_ABI_MAX 8
34static int num_cp_abis = 0;
35
36enum ctor_kinds
38{
40 error (_("ABI doesn't define required function is_constructor_name"));
42}
43
44enum dtor_kinds
46{
48 error (_("ABI doesn't define required function is_destructor_name"));
50}
51
52int
53is_vtable_name (const char *name)
54{
55 if ((current_cp_abi.is_vtable_name) == NULL)
56 error (_("ABI doesn't define required function is_vtable_name"));
58}
59
60int
62{
63 if ((current_cp_abi.is_operator_name) == NULL)
64 error (_("ABI doesn't define required function is_operator_name"));
66}
67
68int
69baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
70 LONGEST embedded_offset, CORE_ADDR address,
71 const struct value *val)
72{
73 int res = 0;
74
75 gdb_assert (current_cp_abi.baseclass_offset != NULL);
76
77 try
78 {
79 res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
80 embedded_offset,
81 address, val);
82 }
83 catch (const gdb_exception_error &ex)
84 {
85 if (ex.error != NOT_AVAILABLE_ERROR)
86 throw;
87
88 throw_error (NOT_AVAILABLE_ERROR,
89 _("Cannot determine virtual baseclass offset "
90 "of incomplete object"));
91 }
92
93 return res;
94}
95
96struct value *
98 struct fn_field *f, int j,
99 struct type *type, int offset)
100{
101 if ((current_cp_abi.virtual_fn_field) == NULL)
102 return NULL;
103 return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
104 type, offset);
105}
106
107struct type *
108value_rtti_type (struct value *v, int *full,
109 LONGEST *top, int *using_enc)
110{
111 struct type *ret = NULL;
112
113 if ((current_cp_abi.rtti_type) == NULL
115 return NULL;
116 try
117 {
118 ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
119 }
120 catch (const gdb_exception_error &e)
121 {
122 return NULL;
123 }
124
125 return ret;
126}
127
128void
129cplus_print_method_ptr (const gdb_byte *contents,
130 struct type *type,
131 struct ui_file *stream)
132{
134 error (_("GDB does not support pointers to methods on this target"));
135 (*current_cp_abi.print_method_ptr) (contents, type, stream);
136}
137
138int
140{
142 error (_("GDB does not support pointers to methods on this target"));
143 return (*current_cp_abi.method_ptr_size) (to_type);
144}
145
146void
147cplus_make_method_ptr (struct type *type, gdb_byte *contents,
148 CORE_ADDR value, int is_virtual)
149{
151 error (_("GDB does not support pointers to methods on this target"));
152 (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
153}
154
155CORE_ADDR
157 CORE_ADDR stop_pc)
158{
160 return 0;
161 return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
162}
163
164struct value *
166 struct value *method_ptr)
167{
169 error (_("GDB does not support pointers to methods on this target"));
170 return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
171}
172
173/* See cp-abi.h. */
174
175void
177{
178 if (current_cp_abi.print_vtable == NULL)
179 error (_("GDB cannot print the vtable on this target"));
181}
182
183/* See cp-abi.h. */
184
185struct value *
187{
188 if (current_cp_abi.get_typeid == NULL)
189 error (_("GDB cannot find the typeid on this target"));
190 return (*current_cp_abi.get_typeid) (value);
191}
192
193/* See cp-abi.h. */
194
195struct type *
197{
199 error (_("GDB cannot find the type for 'typeid' on this target"));
201}
202
203/* See cp-abi.h. */
204
205struct type *
207{
209 error (_("GDB cannot find the type from a std::type_info on this target"));
211}
212
213/* See cp-abi.h. */
214
215std::string
217{
219 error (_("GDB cannot find the type name "
220 "from a std::type_info on this target"));
222}
223
224/* See cp-abi.h. */
225
228{
229 if ((current_cp_abi.pass_by_reference) == NULL)
230 return {};
232}
233
234/* Set the current C++ ABI to SHORT_NAME. */
235
236static int
237switch_to_cp_abi (const char *short_name)
238{
239 struct cp_abi_ops *abi;
240
241 abi = find_cp_abi (short_name);
242 if (abi == NULL)
243 return 0;
244
245 current_cp_abi = *abi;
246 return 1;
247}
248
249/* Add ABI to the list of supported C++ ABI's. */
250
251int
253{
254 if (num_cp_abis == CP_ABI_MAX)
255 internal_error (_("Too many C++ ABIs, please increase "
256 "CP_ABI_MAX in cp-abi.c"));
257
258 cp_abis[num_cp_abis++] = abi;
259
260 return 1;
261}
262
263/* Set the ABI to use in "auto" mode to SHORT_NAME. */
264
265void
266set_cp_abi_as_auto_default (const char *short_name)
267{
268 struct cp_abi_ops *abi = find_cp_abi (short_name);
269
270 if (abi == NULL)
271 internal_error (_("Cannot find C++ ABI \"%s\" to set it as auto default."),
272 short_name);
273
274 xfree ((char *) auto_cp_abi.longname);
275 xfree ((char *) auto_cp_abi.doc);
276
277 auto_cp_abi = *abi;
278
279 auto_cp_abi.shortname = "auto";
280 auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
281 abi->shortname).release ();
282 auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
283 abi->shortname).release ();
284
285 /* Since we copy the current ABI into current_cp_abi instead of
286 using a pointer, if auto is currently the default, we need to
287 reset it. */
288 if (strcmp (current_cp_abi.shortname, "auto") == 0)
289 switch_to_cp_abi ("auto");
290}
291
292/* Return the ABI operations associated with SHORT_NAME. */
293
294static struct cp_abi_ops *
295find_cp_abi (const char *short_name)
296{
297 int i;
298
299 for (i = 0; i < num_cp_abis; i++)
300 if (strcmp (cp_abis[i]->shortname, short_name) == 0)
301 return cp_abis[i];
302
303 return NULL;
304}
305
306/* Display the list of registered C++ ABIs. */
307
308static void
309list_cp_abis (int from_tty)
310{
311 struct ui_out *uiout = current_uiout;
312 int i;
313
314 uiout->text ("The available C++ ABIs are:\n");
315 ui_out_emit_tuple tuple_emitter (uiout, "cp-abi-list");
316 for (i = 0; i < num_cp_abis; i++)
317 {
318 char pad[14];
319 int padcount;
320
321 uiout->text (" ");
322 uiout->field_string ("cp-abi", cp_abis[i]->shortname);
323
324 padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
325 pad[padcount] = 0;
326 while (padcount > 0)
327 pad[--padcount] = ' ';
328 uiout->text (pad);
329
330 uiout->field_string ("doc", cp_abis[i]->doc);
331 uiout->text ("\n");
332 }
333}
334
335/* Set the current C++ ABI, or display the list of options if no
336 argument is given. */
337
338static void
339set_cp_abi_cmd (const char *args, int from_tty)
340{
341 if (args == NULL)
342 {
343 list_cp_abis (from_tty);
344 return;
345 }
346
347 if (!switch_to_cp_abi (args))
348 error (_("Could not find \"%s\" in ABI list"), args);
349}
350
351/* A completion function for "set cp-abi". */
352
353static void
355 completion_tracker &tracker,
356 const char *text, const char *word)
357{
358 static const char **cp_abi_names;
359
360 if (cp_abi_names == NULL)
361 {
362 int i;
363
364 cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
365 for (i = 0; i < num_cp_abis; ++i)
366 cp_abi_names[i] = cp_abis[i]->shortname;
367 cp_abi_names[i] = NULL;
368 }
369
370 complete_on_enum (tracker, cp_abi_names, text, word);
371}
372
373/* Show the currently selected C++ ABI. */
374
375static void
376show_cp_abi_cmd (const char *args, int from_tty)
377{
378 struct ui_out *uiout = current_uiout;
379
380 uiout->text ("The currently selected C++ ABI is \"");
381
382 uiout->field_string ("cp-abi", current_cp_abi.shortname);
383 uiout->text ("\" (");
384 uiout->field_string ("longname", current_cp_abi.longname);
385 uiout->text (").\n");
386}
387
388void _initialize_cp_abi ();
389void
391{
392 struct cmd_list_element *c;
393
395 switch_to_cp_abi ("auto");
396
397 c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
398Set the ABI used for inspecting C++ objects.\n\
399\"set cp-abi\" with no arguments will list the available ABIs."),
400 &setlist);
402
404 _("Show the ABI used for inspecting C++ objects."),
405 &showlist);
406}
const char *const name
void xfree(void *)
void f()
Definition 1.cc:36
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void text(const char *string)
Definition ui-out.c:566
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * setlist
Definition cli-cmds.c:119
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition cli-decode.c:117
void complete_on_enum(completion_tracker &tracker, const char *const *enumlist, const char *text, const char *word)
@ class_obscure
Definition command.h:64
static struct cp_abi_ops auto_cp_abi
Definition cp-abi.c:30
std::string cplus_typename_from_type_info(struct value *value)
Definition cp-abi.c:216
static void cp_abi_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition cp-abi.c:354
int is_operator_name(const char *name)
Definition cp-abi.c:61
struct value * value_virtual_fn_field(struct value **arg1p, struct fn_field *f, int j, struct type *type, int offset)
Definition cp-abi.c:97
void cplus_print_vtable(struct value *value)
Definition cp-abi.c:176
void _initialize_cp_abi()
Definition cp-abi.c:390
struct value * cplus_method_ptr_to_value(struct value **this_p, struct value *method_ptr)
Definition cp-abi.c:165
#define CP_ABI_MAX
Definition cp-abi.c:32
static void set_cp_abi_cmd(const char *args, int from_tty)
Definition cp-abi.c:339
static void show_cp_abi_cmd(const char *args, int from_tty)
Definition cp-abi.c:376
int register_cp_abi(struct cp_abi_ops *abi)
Definition cp-abi.c:252
struct type * cplus_typeid_type(struct gdbarch *gdbarch)
Definition cp-abi.c:196
int cplus_method_ptr_size(struct type *to_type)
Definition cp-abi.c:139
struct language_pass_by_ref_info cp_pass_by_reference(struct type *type)
Definition cp-abi.c:227
enum dtor_kinds is_destructor_name(const char *name)
Definition cp-abi.c:45
static struct cp_abi_ops current_cp_abi
Definition cp-abi.c:29
void cplus_print_method_ptr(const gdb_byte *contents, struct type *type, struct ui_file *stream)
Definition cp-abi.c:129
static int switch_to_cp_abi(const char *short_name)
Definition cp-abi.c:237
enum ctor_kinds is_constructor_name(const char *name)
Definition cp-abi.c:37
void cplus_make_method_ptr(struct type *type, gdb_byte *contents, CORE_ADDR value, int is_virtual)
Definition cp-abi.c:147
int baseclass_offset(struct type *type, int index, const gdb_byte *valaddr, LONGEST embedded_offset, CORE_ADDR address, const struct value *val)
Definition cp-abi.c:69
void set_cp_abi_as_auto_default(const char *short_name)
Definition cp-abi.c:266
struct type * value_rtti_type(struct value *v, int *full, LONGEST *top, int *using_enc)
Definition cp-abi.c:108
struct type * cplus_type_from_type_info(struct value *value)
Definition cp-abi.c:206
static void list_cp_abis(int from_tty)
Definition cp-abi.c:309
static int num_cp_abis
Definition cp-abi.c:34
int is_vtable_name(const char *name)
Definition cp-abi.c:53
CORE_ADDR cplus_skip_trampoline(frame_info_ptr frame, CORE_ADDR stop_pc)
Definition cp-abi.c:156
static struct cp_abi_ops * find_cp_abi(const char *short_name)
Definition cp-abi.c:295
struct value * cplus_typeid(struct value *value)
Definition cp-abi.c:186
static struct cp_abi_ops * cp_abis[CP_ABI_MAX]
Definition cp-abi.c:33
dtor_kinds
Definition cp-abi.h:61
ctor_kinds
Definition cp-abi.h:40
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
#define HAVE_CPLUS_STRUCT(type)
Definition gdbtypes.h:1826
int value
Definition py-param.c:79
enum var_types type
Definition scm-param.c:142
const char * shortname
Definition cp-abi.h:218
int(* method_ptr_size)(struct type *)
Definition cp-abi.h:240
CORE_ADDR(* skip_trampoline)(frame_info_ptr, CORE_ADDR)
Definition cp-abi.h:250
struct type *(* get_typeid_type)(struct gdbarch *gdbarch)
Definition cp-abi.h:247
struct type *(* get_type_from_type_info)(struct value *value)
Definition cp-abi.h:248
void(* make_method_ptr)(struct type *, gdb_byte *, CORE_ADDR, int)
Definition cp-abi.h:241
int(* baseclass_offset)(struct type *type, int index, const bfd_byte *valaddr, LONGEST embedded_offset, CORE_ADDR address, const struct value *val)
Definition cp-abi.h:234
struct value *(* method_ptr_to_value)(struct value **, struct value *)
Definition cp-abi.h:243
int(* is_vtable_name)(const char *name)
Definition cp-abi.h:226
enum ctor_kinds(* is_constructor_name)(const char *name)
Definition cp-abi.h:224
struct value *(* virtual_fn_field)(struct value **arg1p, struct fn_field *f, int j, struct type *type, int offset)
Definition cp-abi.h:228
enum dtor_kinds(* is_destructor_name)(const char *name)
Definition cp-abi.h:225
void(* print_vtable)(struct value *)
Definition cp-abi.h:245
int(* is_operator_name)(const char *name)
Definition cp-abi.h:227
const char * longname
Definition cp-abi.h:219
const char * doc
Definition cp-abi.h:220
struct language_pass_by_ref_info(* pass_by_reference)(struct type *type)
Definition cp-abi.h:251
std::string(* get_typename_from_type_info)(struct value *value)
Definition cp-abi.h:249
struct value *(* get_typeid)(struct value *value)
Definition cp-abi.h:246
void(* print_method_ptr)(const gdb_byte *contents, struct type *type, struct ui_file *stream)
Definition cp-abi.h:237
struct type *(* rtti_type)(struct value *v, int *full, LONGEST *top, int *using_enc)
Definition cp-abi.h:232
Definition value.h:130
struct type * type() const
Definition value.h:180
LONGEST offset() const
Definition value.h:222
#define current_uiout
Definition ui-out.h:40