15796c8dcSSimon Schubert /* Generic code for supporting multiple C++ ABI's 25796c8dcSSimon Schubert 3*ef5ccd6cSJohn Marino Copyright (C) 2001-2013 Free Software Foundation, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert #include "defs.h" 215796c8dcSSimon Schubert #include "value.h" 225796c8dcSSimon Schubert #include "cp-abi.h" 235796c8dcSSimon Schubert #include "command.h" 245796c8dcSSimon Schubert #include "exceptions.h" 255796c8dcSSimon Schubert #include "gdbcmd.h" 265796c8dcSSimon Schubert #include "ui-out.h" 27c50c785cSJohn Marino #include "gdb_assert.h" 285796c8dcSSimon Schubert #include "gdb_string.h" 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert static struct cp_abi_ops *find_cp_abi (const char *short_name); 315796c8dcSSimon Schubert 325796c8dcSSimon Schubert static struct cp_abi_ops current_cp_abi = { "", NULL }; 335796c8dcSSimon Schubert static struct cp_abi_ops auto_cp_abi = { "auto", NULL }; 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert #define CP_ABI_MAX 8 365796c8dcSSimon Schubert static struct cp_abi_ops *cp_abis[CP_ABI_MAX]; 375796c8dcSSimon Schubert static int num_cp_abis = 0; 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert enum ctor_kinds 405796c8dcSSimon Schubert is_constructor_name (const char *name) 415796c8dcSSimon Schubert { 425796c8dcSSimon Schubert if ((current_cp_abi.is_constructor_name) == NULL) 435796c8dcSSimon Schubert error (_("ABI doesn't define required function is_constructor_name")); 445796c8dcSSimon Schubert return (*current_cp_abi.is_constructor_name) (name); 455796c8dcSSimon Schubert } 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert enum dtor_kinds 485796c8dcSSimon Schubert is_destructor_name (const char *name) 495796c8dcSSimon Schubert { 505796c8dcSSimon Schubert if ((current_cp_abi.is_destructor_name) == NULL) 515796c8dcSSimon Schubert error (_("ABI doesn't define required function is_destructor_name")); 525796c8dcSSimon Schubert return (*current_cp_abi.is_destructor_name) (name); 535796c8dcSSimon Schubert } 545796c8dcSSimon Schubert 555796c8dcSSimon Schubert int 565796c8dcSSimon Schubert is_vtable_name (const char *name) 575796c8dcSSimon Schubert { 585796c8dcSSimon Schubert if ((current_cp_abi.is_vtable_name) == NULL) 595796c8dcSSimon Schubert error (_("ABI doesn't define required function is_vtable_name")); 605796c8dcSSimon Schubert return (*current_cp_abi.is_vtable_name) (name); 615796c8dcSSimon Schubert } 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert int 645796c8dcSSimon Schubert is_operator_name (const char *name) 655796c8dcSSimon Schubert { 665796c8dcSSimon Schubert if ((current_cp_abi.is_operator_name) == NULL) 675796c8dcSSimon Schubert error (_("ABI doesn't define required function is_operator_name")); 685796c8dcSSimon Schubert return (*current_cp_abi.is_operator_name) (name); 695796c8dcSSimon Schubert } 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert int 72c50c785cSJohn Marino baseclass_offset (struct type *type, int index, const gdb_byte *valaddr, 73c50c785cSJohn Marino int embedded_offset, CORE_ADDR address, 74c50c785cSJohn Marino const struct value *val) 755796c8dcSSimon Schubert { 76c50c785cSJohn Marino volatile struct gdb_exception ex; 77c50c785cSJohn Marino int res = 0; 78c50c785cSJohn Marino 79c50c785cSJohn Marino gdb_assert (current_cp_abi.baseclass_offset != NULL); 80c50c785cSJohn Marino 81c50c785cSJohn Marino TRY_CATCH (ex, RETURN_MASK_ERROR) 82c50c785cSJohn Marino { 83c50c785cSJohn Marino res = (*current_cp_abi.baseclass_offset) (type, index, valaddr, 84c50c785cSJohn Marino embedded_offset, 85c50c785cSJohn Marino address, val); 86c50c785cSJohn Marino } 87c50c785cSJohn Marino 88c50c785cSJohn Marino if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR) 89c50c785cSJohn Marino throw_error (NOT_AVAILABLE_ERROR, 90c50c785cSJohn Marino _("Cannot determine virtual baseclass offset " 91c50c785cSJohn Marino "of incomplete object")); 92c50c785cSJohn Marino else if (ex.reason < 0) 93c50c785cSJohn Marino throw_exception (ex); 94c50c785cSJohn Marino else 95c50c785cSJohn Marino return res; 965796c8dcSSimon Schubert } 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert struct value * 99c50c785cSJohn Marino value_virtual_fn_field (struct value **arg1p, 100c50c785cSJohn Marino struct fn_field *f, int j, 1015796c8dcSSimon Schubert struct type *type, int offset) 1025796c8dcSSimon Schubert { 1035796c8dcSSimon Schubert if ((current_cp_abi.virtual_fn_field) == NULL) 1045796c8dcSSimon Schubert return NULL; 105c50c785cSJohn Marino return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, 106c50c785cSJohn Marino type, offset); 1075796c8dcSSimon Schubert } 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert struct type * 110c50c785cSJohn Marino value_rtti_type (struct value *v, int *full, 111c50c785cSJohn Marino int *top, int *using_enc) 1125796c8dcSSimon Schubert { 1135796c8dcSSimon Schubert struct type *ret = NULL; 114*ef5ccd6cSJohn Marino volatile struct gdb_exception e; 115cf7f2e2dSJohn Marino 1165796c8dcSSimon Schubert if ((current_cp_abi.rtti_type) == NULL) 1175796c8dcSSimon Schubert return NULL; 1185796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ERROR) 1195796c8dcSSimon Schubert { 1205796c8dcSSimon Schubert ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc); 1215796c8dcSSimon Schubert } 1225796c8dcSSimon Schubert if (e.reason < 0) 1235796c8dcSSimon Schubert return NULL; 1245796c8dcSSimon Schubert return ret; 1255796c8dcSSimon Schubert } 1265796c8dcSSimon Schubert 1275796c8dcSSimon Schubert void 128c50c785cSJohn Marino cplus_print_method_ptr (const gdb_byte *contents, 129c50c785cSJohn Marino struct type *type, 1305796c8dcSSimon Schubert struct ui_file *stream) 1315796c8dcSSimon Schubert { 1325796c8dcSSimon Schubert if (current_cp_abi.print_method_ptr == NULL) 1335796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1345796c8dcSSimon Schubert (*current_cp_abi.print_method_ptr) (contents, type, stream); 1355796c8dcSSimon Schubert } 1365796c8dcSSimon Schubert 1375796c8dcSSimon Schubert int 1385796c8dcSSimon Schubert cplus_method_ptr_size (struct type *to_type) 1395796c8dcSSimon Schubert { 1405796c8dcSSimon Schubert if (current_cp_abi.method_ptr_size == NULL) 1415796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1425796c8dcSSimon Schubert return (*current_cp_abi.method_ptr_size) (to_type); 1435796c8dcSSimon Schubert } 1445796c8dcSSimon Schubert 1455796c8dcSSimon Schubert void 1465796c8dcSSimon Schubert cplus_make_method_ptr (struct type *type, gdb_byte *contents, 1475796c8dcSSimon Schubert CORE_ADDR value, int is_virtual) 1485796c8dcSSimon Schubert { 1495796c8dcSSimon Schubert if (current_cp_abi.make_method_ptr == NULL) 1505796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1515796c8dcSSimon Schubert (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual); 1525796c8dcSSimon Schubert } 1535796c8dcSSimon Schubert 1545796c8dcSSimon Schubert CORE_ADDR 155c50c785cSJohn Marino cplus_skip_trampoline (struct frame_info *frame, 156c50c785cSJohn Marino CORE_ADDR stop_pc) 1575796c8dcSSimon Schubert { 1585796c8dcSSimon Schubert if (current_cp_abi.skip_trampoline == NULL) 1595796c8dcSSimon Schubert return 0; 1605796c8dcSSimon Schubert return (*current_cp_abi.skip_trampoline) (frame, stop_pc); 1615796c8dcSSimon Schubert } 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert struct value * 164c50c785cSJohn Marino cplus_method_ptr_to_value (struct value **this_p, 165c50c785cSJohn Marino struct value *method_ptr) 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert if (current_cp_abi.method_ptr_to_value == NULL) 1685796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1695796c8dcSSimon Schubert return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr); 1705796c8dcSSimon Schubert } 1715796c8dcSSimon Schubert 172*ef5ccd6cSJohn Marino /* See cp-abi.h. */ 173*ef5ccd6cSJohn Marino 174*ef5ccd6cSJohn Marino void 175*ef5ccd6cSJohn Marino cplus_print_vtable (struct value *value) 176*ef5ccd6cSJohn Marino { 177*ef5ccd6cSJohn Marino if (current_cp_abi.print_vtable == NULL) 178*ef5ccd6cSJohn Marino error (_("GDB cannot print the vtable on this target")); 179*ef5ccd6cSJohn Marino (*current_cp_abi.print_vtable) (value); 180*ef5ccd6cSJohn Marino } 181*ef5ccd6cSJohn Marino 1825796c8dcSSimon Schubert int 1835796c8dcSSimon Schubert cp_pass_by_reference (struct type *type) 1845796c8dcSSimon Schubert { 1855796c8dcSSimon Schubert if ((current_cp_abi.pass_by_reference) == NULL) 1865796c8dcSSimon Schubert return 0; 1875796c8dcSSimon Schubert return (*current_cp_abi.pass_by_reference) (type); 1885796c8dcSSimon Schubert } 1895796c8dcSSimon Schubert 1905796c8dcSSimon Schubert /* Set the current C++ ABI to SHORT_NAME. */ 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert static int 1935796c8dcSSimon Schubert switch_to_cp_abi (const char *short_name) 1945796c8dcSSimon Schubert { 1955796c8dcSSimon Schubert struct cp_abi_ops *abi; 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert abi = find_cp_abi (short_name); 1985796c8dcSSimon Schubert if (abi == NULL) 1995796c8dcSSimon Schubert return 0; 2005796c8dcSSimon Schubert 2015796c8dcSSimon Schubert current_cp_abi = *abi; 2025796c8dcSSimon Schubert return 1; 2035796c8dcSSimon Schubert } 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert /* Add ABI to the list of supported C++ ABI's. */ 2065796c8dcSSimon Schubert 2075796c8dcSSimon Schubert int 2085796c8dcSSimon Schubert register_cp_abi (struct cp_abi_ops *abi) 2095796c8dcSSimon Schubert { 2105796c8dcSSimon Schubert if (num_cp_abis == CP_ABI_MAX) 2115796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, 212c50c785cSJohn Marino _("Too many C++ ABIs, please increase " 213c50c785cSJohn Marino "CP_ABI_MAX in cp-abi.c")); 2145796c8dcSSimon Schubert 2155796c8dcSSimon Schubert cp_abis[num_cp_abis++] = abi; 2165796c8dcSSimon Schubert 2175796c8dcSSimon Schubert return 1; 2185796c8dcSSimon Schubert } 2195796c8dcSSimon Schubert 2205796c8dcSSimon Schubert /* Set the ABI to use in "auto" mode to SHORT_NAME. */ 2215796c8dcSSimon Schubert 2225796c8dcSSimon Schubert void 2235796c8dcSSimon Schubert set_cp_abi_as_auto_default (const char *short_name) 2245796c8dcSSimon Schubert { 2255796c8dcSSimon Schubert char *new_longname, *new_doc; 2265796c8dcSSimon Schubert struct cp_abi_ops *abi = find_cp_abi (short_name); 2275796c8dcSSimon Schubert 2285796c8dcSSimon Schubert if (abi == NULL) 2295796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, 2305796c8dcSSimon Schubert _("Cannot find C++ ABI \"%s\" to set it as auto default."), 2315796c8dcSSimon Schubert short_name); 2325796c8dcSSimon Schubert 2335796c8dcSSimon Schubert if (auto_cp_abi.longname != NULL) 2345796c8dcSSimon Schubert xfree ((char *) auto_cp_abi.longname); 2355796c8dcSSimon Schubert if (auto_cp_abi.doc != NULL) 2365796c8dcSSimon Schubert xfree ((char *) auto_cp_abi.doc); 2375796c8dcSSimon Schubert 2385796c8dcSSimon Schubert auto_cp_abi = *abi; 2395796c8dcSSimon Schubert 2405796c8dcSSimon Schubert auto_cp_abi.shortname = "auto"; 2415796c8dcSSimon Schubert new_longname = xstrprintf ("currently \"%s\"", abi->shortname); 2425796c8dcSSimon Schubert auto_cp_abi.longname = new_longname; 2435796c8dcSSimon Schubert 2445796c8dcSSimon Schubert new_doc = xstrprintf ("Automatically selected; currently \"%s\"", 2455796c8dcSSimon Schubert abi->shortname); 2465796c8dcSSimon Schubert auto_cp_abi.doc = new_doc; 2475796c8dcSSimon Schubert 2485796c8dcSSimon Schubert /* Since we copy the current ABI into current_cp_abi instead of 2495796c8dcSSimon Schubert using a pointer, if auto is currently the default, we need to 2505796c8dcSSimon Schubert reset it. */ 2515796c8dcSSimon Schubert if (strcmp (current_cp_abi.shortname, "auto") == 0) 2525796c8dcSSimon Schubert switch_to_cp_abi ("auto"); 2535796c8dcSSimon Schubert } 2545796c8dcSSimon Schubert 2555796c8dcSSimon Schubert /* Return the ABI operations associated with SHORT_NAME. */ 2565796c8dcSSimon Schubert 2575796c8dcSSimon Schubert static struct cp_abi_ops * 2585796c8dcSSimon Schubert find_cp_abi (const char *short_name) 2595796c8dcSSimon Schubert { 2605796c8dcSSimon Schubert int i; 2615796c8dcSSimon Schubert 2625796c8dcSSimon Schubert for (i = 0; i < num_cp_abis; i++) 2635796c8dcSSimon Schubert if (strcmp (cp_abis[i]->shortname, short_name) == 0) 2645796c8dcSSimon Schubert return cp_abis[i]; 2655796c8dcSSimon Schubert 2665796c8dcSSimon Schubert return NULL; 2675796c8dcSSimon Schubert } 2685796c8dcSSimon Schubert 2695796c8dcSSimon Schubert /* Display the list of registered C++ ABIs. */ 2705796c8dcSSimon Schubert 2715796c8dcSSimon Schubert static void 2725796c8dcSSimon Schubert list_cp_abis (int from_tty) 2735796c8dcSSimon Schubert { 274a45ae5f8SJohn Marino struct ui_out *uiout = current_uiout; 2755796c8dcSSimon Schubert struct cleanup *cleanup_chain; 2765796c8dcSSimon Schubert int i; 2775796c8dcSSimon Schubert 278cf7f2e2dSJohn Marino ui_out_text (uiout, "The available C++ ABIs are:\n"); 279c50c785cSJohn Marino cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, 280c50c785cSJohn Marino "cp-abi-list"); 2815796c8dcSSimon Schubert for (i = 0; i < num_cp_abis; i++) 2825796c8dcSSimon Schubert { 2835796c8dcSSimon Schubert char pad[14]; 2845796c8dcSSimon Schubert int padcount; 2855796c8dcSSimon Schubert 2865796c8dcSSimon Schubert ui_out_text (uiout, " "); 2875796c8dcSSimon Schubert ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname); 2885796c8dcSSimon Schubert 2895796c8dcSSimon Schubert padcount = 16 - 2 - strlen (cp_abis[i]->shortname); 2905796c8dcSSimon Schubert pad[padcount] = 0; 2915796c8dcSSimon Schubert while (padcount > 0) 2925796c8dcSSimon Schubert pad[--padcount] = ' '; 2935796c8dcSSimon Schubert ui_out_text (uiout, pad); 2945796c8dcSSimon Schubert 2955796c8dcSSimon Schubert ui_out_field_string (uiout, "doc", cp_abis[i]->doc); 2965796c8dcSSimon Schubert ui_out_text (uiout, "\n"); 2975796c8dcSSimon Schubert } 2985796c8dcSSimon Schubert do_cleanups (cleanup_chain); 2995796c8dcSSimon Schubert } 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert /* Set the current C++ ABI, or display the list of options if no 3025796c8dcSSimon Schubert argument is given. */ 3035796c8dcSSimon Schubert 3045796c8dcSSimon Schubert static void 3055796c8dcSSimon Schubert set_cp_abi_cmd (char *args, int from_tty) 3065796c8dcSSimon Schubert { 3075796c8dcSSimon Schubert if (args == NULL) 3085796c8dcSSimon Schubert { 3095796c8dcSSimon Schubert list_cp_abis (from_tty); 3105796c8dcSSimon Schubert return; 3115796c8dcSSimon Schubert } 3125796c8dcSSimon Schubert 3135796c8dcSSimon Schubert if (!switch_to_cp_abi (args)) 3145796c8dcSSimon Schubert error (_("Could not find \"%s\" in ABI list"), args); 3155796c8dcSSimon Schubert } 3165796c8dcSSimon Schubert 317*ef5ccd6cSJohn Marino /* A completion function for "set cp-abi". */ 318*ef5ccd6cSJohn Marino 319*ef5ccd6cSJohn Marino static VEC (char_ptr) * 320*ef5ccd6cSJohn Marino cp_abi_completer (struct cmd_list_element *ignore, 321*ef5ccd6cSJohn Marino char *text, char *word) 322*ef5ccd6cSJohn Marino { 323*ef5ccd6cSJohn Marino static const char **cp_abi_names; 324*ef5ccd6cSJohn Marino 325*ef5ccd6cSJohn Marino if (cp_abi_names == NULL) 326*ef5ccd6cSJohn Marino { 327*ef5ccd6cSJohn Marino int i; 328*ef5ccd6cSJohn Marino 329*ef5ccd6cSJohn Marino cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1); 330*ef5ccd6cSJohn Marino for (i = 0; i < num_cp_abis; ++i) 331*ef5ccd6cSJohn Marino cp_abi_names[i] = cp_abis[i]->shortname; 332*ef5ccd6cSJohn Marino cp_abi_names[i] = NULL; 333*ef5ccd6cSJohn Marino } 334*ef5ccd6cSJohn Marino 335*ef5ccd6cSJohn Marino return complete_on_enum (cp_abi_names, text, word); 336*ef5ccd6cSJohn Marino } 337*ef5ccd6cSJohn Marino 3385796c8dcSSimon Schubert /* Show the currently selected C++ ABI. */ 3395796c8dcSSimon Schubert 3405796c8dcSSimon Schubert static void 3415796c8dcSSimon Schubert show_cp_abi_cmd (char *args, int from_tty) 3425796c8dcSSimon Schubert { 343a45ae5f8SJohn Marino struct ui_out *uiout = current_uiout; 344a45ae5f8SJohn Marino 3455796c8dcSSimon Schubert ui_out_text (uiout, "The currently selected C++ ABI is \""); 3465796c8dcSSimon Schubert 3475796c8dcSSimon Schubert ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname); 3485796c8dcSSimon Schubert ui_out_text (uiout, "\" ("); 3495796c8dcSSimon Schubert ui_out_field_string (uiout, "longname", current_cp_abi.longname); 3505796c8dcSSimon Schubert ui_out_text (uiout, ").\n"); 3515796c8dcSSimon Schubert } 3525796c8dcSSimon Schubert 3535796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */ 3545796c8dcSSimon Schubert 3555796c8dcSSimon Schubert void 3565796c8dcSSimon Schubert _initialize_cp_abi (void) 3575796c8dcSSimon Schubert { 358*ef5ccd6cSJohn Marino struct cmd_list_element *c; 359*ef5ccd6cSJohn Marino 3605796c8dcSSimon Schubert register_cp_abi (&auto_cp_abi); 3615796c8dcSSimon Schubert switch_to_cp_abi ("auto"); 3625796c8dcSSimon Schubert 363*ef5ccd6cSJohn Marino c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\ 3645796c8dcSSimon Schubert Set the ABI used for inspecting C++ objects.\n\ 3655796c8dcSSimon Schubert \"set cp-abi\" with no arguments will list the available ABIs."), 3665796c8dcSSimon Schubert &setlist); 367*ef5ccd6cSJohn Marino set_cmd_completer (c, cp_abi_completer); 3685796c8dcSSimon Schubert 3695796c8dcSSimon Schubert add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd, 370c50c785cSJohn Marino _("Show the ABI used for inspecting C++ objects."), 371c50c785cSJohn Marino &showlist); 3725796c8dcSSimon Schubert } 373