15796c8dcSSimon Schubert /* Generic code for supporting multiple C++ ABI's 25796c8dcSSimon Schubert 3*c50c785cSJohn Marino Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011 45796c8dcSSimon Schubert Free Software Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of GDB. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 115796c8dcSSimon Schubert (at your option) any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165796c8dcSSimon Schubert GNU General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "value.h" 235796c8dcSSimon Schubert #include "cp-abi.h" 245796c8dcSSimon Schubert #include "command.h" 255796c8dcSSimon Schubert #include "exceptions.h" 265796c8dcSSimon Schubert #include "gdbcmd.h" 275796c8dcSSimon Schubert #include "ui-out.h" 28*c50c785cSJohn Marino #include "gdb_assert.h" 295796c8dcSSimon Schubert #include "gdb_string.h" 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert static struct cp_abi_ops *find_cp_abi (const char *short_name); 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert static struct cp_abi_ops current_cp_abi = { "", NULL }; 345796c8dcSSimon Schubert static struct cp_abi_ops auto_cp_abi = { "auto", NULL }; 355796c8dcSSimon Schubert 365796c8dcSSimon Schubert #define CP_ABI_MAX 8 375796c8dcSSimon Schubert static struct cp_abi_ops *cp_abis[CP_ABI_MAX]; 385796c8dcSSimon Schubert static int num_cp_abis = 0; 395796c8dcSSimon Schubert 405796c8dcSSimon Schubert enum ctor_kinds 415796c8dcSSimon Schubert is_constructor_name (const char *name) 425796c8dcSSimon Schubert { 435796c8dcSSimon Schubert if ((current_cp_abi.is_constructor_name) == NULL) 445796c8dcSSimon Schubert error (_("ABI doesn't define required function is_constructor_name")); 455796c8dcSSimon Schubert return (*current_cp_abi.is_constructor_name) (name); 465796c8dcSSimon Schubert } 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert enum dtor_kinds 495796c8dcSSimon Schubert is_destructor_name (const char *name) 505796c8dcSSimon Schubert { 515796c8dcSSimon Schubert if ((current_cp_abi.is_destructor_name) == NULL) 525796c8dcSSimon Schubert error (_("ABI doesn't define required function is_destructor_name")); 535796c8dcSSimon Schubert return (*current_cp_abi.is_destructor_name) (name); 545796c8dcSSimon Schubert } 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert int 575796c8dcSSimon Schubert is_vtable_name (const char *name) 585796c8dcSSimon Schubert { 595796c8dcSSimon Schubert if ((current_cp_abi.is_vtable_name) == NULL) 605796c8dcSSimon Schubert error (_("ABI doesn't define required function is_vtable_name")); 615796c8dcSSimon Schubert return (*current_cp_abi.is_vtable_name) (name); 625796c8dcSSimon Schubert } 635796c8dcSSimon Schubert 645796c8dcSSimon Schubert int 655796c8dcSSimon Schubert is_operator_name (const char *name) 665796c8dcSSimon Schubert { 675796c8dcSSimon Schubert if ((current_cp_abi.is_operator_name) == NULL) 685796c8dcSSimon Schubert error (_("ABI doesn't define required function is_operator_name")); 695796c8dcSSimon Schubert return (*current_cp_abi.is_operator_name) (name); 705796c8dcSSimon Schubert } 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert int 73*c50c785cSJohn Marino baseclass_offset (struct type *type, int index, const gdb_byte *valaddr, 74*c50c785cSJohn Marino int embedded_offset, CORE_ADDR address, 75*c50c785cSJohn Marino const struct value *val) 765796c8dcSSimon Schubert { 77*c50c785cSJohn Marino volatile struct gdb_exception ex; 78*c50c785cSJohn Marino int res = 0; 79*c50c785cSJohn Marino 80*c50c785cSJohn Marino gdb_assert (current_cp_abi.baseclass_offset != NULL); 81*c50c785cSJohn Marino 82*c50c785cSJohn Marino TRY_CATCH (ex, RETURN_MASK_ERROR) 83*c50c785cSJohn Marino { 84*c50c785cSJohn Marino res = (*current_cp_abi.baseclass_offset) (type, index, valaddr, 85*c50c785cSJohn Marino embedded_offset, 86*c50c785cSJohn Marino address, val); 87*c50c785cSJohn Marino } 88*c50c785cSJohn Marino 89*c50c785cSJohn Marino if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR) 90*c50c785cSJohn Marino throw_error (NOT_AVAILABLE_ERROR, 91*c50c785cSJohn Marino _("Cannot determine virtual baseclass offset " 92*c50c785cSJohn Marino "of incomplete object")); 93*c50c785cSJohn Marino else if (ex.reason < 0) 94*c50c785cSJohn Marino throw_exception (ex); 95*c50c785cSJohn Marino else 96*c50c785cSJohn Marino return res; 975796c8dcSSimon Schubert } 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert struct value * 100*c50c785cSJohn Marino value_virtual_fn_field (struct value **arg1p, 101*c50c785cSJohn Marino struct fn_field *f, int j, 1025796c8dcSSimon Schubert struct type *type, int offset) 1035796c8dcSSimon Schubert { 1045796c8dcSSimon Schubert if ((current_cp_abi.virtual_fn_field) == NULL) 1055796c8dcSSimon Schubert return NULL; 106*c50c785cSJohn Marino return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, 107*c50c785cSJohn Marino type, offset); 1085796c8dcSSimon Schubert } 1095796c8dcSSimon Schubert 1105796c8dcSSimon Schubert struct type * 111*c50c785cSJohn Marino value_rtti_type (struct value *v, int *full, 112*c50c785cSJohn Marino int *top, int *using_enc) 1135796c8dcSSimon Schubert { 1145796c8dcSSimon Schubert struct type *ret = NULL; 1155796c8dcSSimon Schubert struct gdb_exception e; 116cf7f2e2dSJohn Marino 1175796c8dcSSimon Schubert if ((current_cp_abi.rtti_type) == NULL) 1185796c8dcSSimon Schubert return NULL; 1195796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ERROR) 1205796c8dcSSimon Schubert { 1215796c8dcSSimon Schubert ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc); 1225796c8dcSSimon Schubert } 1235796c8dcSSimon Schubert if (e.reason < 0) 1245796c8dcSSimon Schubert return NULL; 1255796c8dcSSimon Schubert return ret; 1265796c8dcSSimon Schubert } 1275796c8dcSSimon Schubert 1285796c8dcSSimon Schubert void 129*c50c785cSJohn Marino cplus_print_method_ptr (const gdb_byte *contents, 130*c50c785cSJohn Marino struct type *type, 1315796c8dcSSimon Schubert struct ui_file *stream) 1325796c8dcSSimon Schubert { 1335796c8dcSSimon Schubert if (current_cp_abi.print_method_ptr == NULL) 1345796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1355796c8dcSSimon Schubert (*current_cp_abi.print_method_ptr) (contents, type, stream); 1365796c8dcSSimon Schubert } 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert int 1395796c8dcSSimon Schubert cplus_method_ptr_size (struct type *to_type) 1405796c8dcSSimon Schubert { 1415796c8dcSSimon Schubert if (current_cp_abi.method_ptr_size == NULL) 1425796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1435796c8dcSSimon Schubert return (*current_cp_abi.method_ptr_size) (to_type); 1445796c8dcSSimon Schubert } 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert void 1475796c8dcSSimon Schubert cplus_make_method_ptr (struct type *type, gdb_byte *contents, 1485796c8dcSSimon Schubert CORE_ADDR value, int is_virtual) 1495796c8dcSSimon Schubert { 1505796c8dcSSimon Schubert if (current_cp_abi.make_method_ptr == NULL) 1515796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1525796c8dcSSimon Schubert (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual); 1535796c8dcSSimon Schubert } 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert CORE_ADDR 156*c50c785cSJohn Marino cplus_skip_trampoline (struct frame_info *frame, 157*c50c785cSJohn Marino CORE_ADDR stop_pc) 1585796c8dcSSimon Schubert { 1595796c8dcSSimon Schubert if (current_cp_abi.skip_trampoline == NULL) 1605796c8dcSSimon Schubert return 0; 1615796c8dcSSimon Schubert return (*current_cp_abi.skip_trampoline) (frame, stop_pc); 1625796c8dcSSimon Schubert } 1635796c8dcSSimon Schubert 1645796c8dcSSimon Schubert struct value * 165*c50c785cSJohn Marino cplus_method_ptr_to_value (struct value **this_p, 166*c50c785cSJohn Marino struct value *method_ptr) 1675796c8dcSSimon Schubert { 1685796c8dcSSimon Schubert if (current_cp_abi.method_ptr_to_value == NULL) 1695796c8dcSSimon Schubert error (_("GDB does not support pointers to methods on this target")); 1705796c8dcSSimon Schubert return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr); 1715796c8dcSSimon Schubert } 1725796c8dcSSimon Schubert 1735796c8dcSSimon Schubert int 1745796c8dcSSimon Schubert cp_pass_by_reference (struct type *type) 1755796c8dcSSimon Schubert { 1765796c8dcSSimon Schubert if ((current_cp_abi.pass_by_reference) == NULL) 1775796c8dcSSimon Schubert return 0; 1785796c8dcSSimon Schubert return (*current_cp_abi.pass_by_reference) (type); 1795796c8dcSSimon Schubert } 1805796c8dcSSimon Schubert 1815796c8dcSSimon Schubert /* Set the current C++ ABI to SHORT_NAME. */ 1825796c8dcSSimon Schubert 1835796c8dcSSimon Schubert static int 1845796c8dcSSimon Schubert switch_to_cp_abi (const char *short_name) 1855796c8dcSSimon Schubert { 1865796c8dcSSimon Schubert struct cp_abi_ops *abi; 1875796c8dcSSimon Schubert 1885796c8dcSSimon Schubert abi = find_cp_abi (short_name); 1895796c8dcSSimon Schubert if (abi == NULL) 1905796c8dcSSimon Schubert return 0; 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert current_cp_abi = *abi; 1935796c8dcSSimon Schubert return 1; 1945796c8dcSSimon Schubert } 1955796c8dcSSimon Schubert 1965796c8dcSSimon Schubert /* Add ABI to the list of supported C++ ABI's. */ 1975796c8dcSSimon Schubert 1985796c8dcSSimon Schubert int 1995796c8dcSSimon Schubert register_cp_abi (struct cp_abi_ops *abi) 2005796c8dcSSimon Schubert { 2015796c8dcSSimon Schubert if (num_cp_abis == CP_ABI_MAX) 2025796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, 203*c50c785cSJohn Marino _("Too many C++ ABIs, please increase " 204*c50c785cSJohn Marino "CP_ABI_MAX in cp-abi.c")); 2055796c8dcSSimon Schubert 2065796c8dcSSimon Schubert cp_abis[num_cp_abis++] = abi; 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert return 1; 2095796c8dcSSimon Schubert } 2105796c8dcSSimon Schubert 2115796c8dcSSimon Schubert /* Set the ABI to use in "auto" mode to SHORT_NAME. */ 2125796c8dcSSimon Schubert 2135796c8dcSSimon Schubert void 2145796c8dcSSimon Schubert set_cp_abi_as_auto_default (const char *short_name) 2155796c8dcSSimon Schubert { 2165796c8dcSSimon Schubert char *new_longname, *new_doc; 2175796c8dcSSimon Schubert struct cp_abi_ops *abi = find_cp_abi (short_name); 2185796c8dcSSimon Schubert 2195796c8dcSSimon Schubert if (abi == NULL) 2205796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, 2215796c8dcSSimon Schubert _("Cannot find C++ ABI \"%s\" to set it as auto default."), 2225796c8dcSSimon Schubert short_name); 2235796c8dcSSimon Schubert 2245796c8dcSSimon Schubert if (auto_cp_abi.longname != NULL) 2255796c8dcSSimon Schubert xfree ((char *) auto_cp_abi.longname); 2265796c8dcSSimon Schubert if (auto_cp_abi.doc != NULL) 2275796c8dcSSimon Schubert xfree ((char *) auto_cp_abi.doc); 2285796c8dcSSimon Schubert 2295796c8dcSSimon Schubert auto_cp_abi = *abi; 2305796c8dcSSimon Schubert 2315796c8dcSSimon Schubert auto_cp_abi.shortname = "auto"; 2325796c8dcSSimon Schubert new_longname = xstrprintf ("currently \"%s\"", abi->shortname); 2335796c8dcSSimon Schubert auto_cp_abi.longname = new_longname; 2345796c8dcSSimon Schubert 2355796c8dcSSimon Schubert new_doc = xstrprintf ("Automatically selected; currently \"%s\"", 2365796c8dcSSimon Schubert abi->shortname); 2375796c8dcSSimon Schubert auto_cp_abi.doc = new_doc; 2385796c8dcSSimon Schubert 2395796c8dcSSimon Schubert /* Since we copy the current ABI into current_cp_abi instead of 2405796c8dcSSimon Schubert using a pointer, if auto is currently the default, we need to 2415796c8dcSSimon Schubert reset it. */ 2425796c8dcSSimon Schubert if (strcmp (current_cp_abi.shortname, "auto") == 0) 2435796c8dcSSimon Schubert switch_to_cp_abi ("auto"); 2445796c8dcSSimon Schubert } 2455796c8dcSSimon Schubert 2465796c8dcSSimon Schubert /* Return the ABI operations associated with SHORT_NAME. */ 2475796c8dcSSimon Schubert 2485796c8dcSSimon Schubert static struct cp_abi_ops * 2495796c8dcSSimon Schubert find_cp_abi (const char *short_name) 2505796c8dcSSimon Schubert { 2515796c8dcSSimon Schubert int i; 2525796c8dcSSimon Schubert 2535796c8dcSSimon Schubert for (i = 0; i < num_cp_abis; i++) 2545796c8dcSSimon Schubert if (strcmp (cp_abis[i]->shortname, short_name) == 0) 2555796c8dcSSimon Schubert return cp_abis[i]; 2565796c8dcSSimon Schubert 2575796c8dcSSimon Schubert return NULL; 2585796c8dcSSimon Schubert } 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert /* Display the list of registered C++ ABIs. */ 2615796c8dcSSimon Schubert 2625796c8dcSSimon Schubert static void 2635796c8dcSSimon Schubert list_cp_abis (int from_tty) 2645796c8dcSSimon Schubert { 2655796c8dcSSimon Schubert struct cleanup *cleanup_chain; 2665796c8dcSSimon Schubert int i; 2675796c8dcSSimon Schubert 268cf7f2e2dSJohn Marino ui_out_text (uiout, "The available C++ ABIs are:\n"); 269*c50c785cSJohn Marino cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, 270*c50c785cSJohn Marino "cp-abi-list"); 2715796c8dcSSimon Schubert for (i = 0; i < num_cp_abis; i++) 2725796c8dcSSimon Schubert { 2735796c8dcSSimon Schubert char pad[14]; 2745796c8dcSSimon Schubert int padcount; 2755796c8dcSSimon Schubert 2765796c8dcSSimon Schubert ui_out_text (uiout, " "); 2775796c8dcSSimon Schubert ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname); 2785796c8dcSSimon Schubert 2795796c8dcSSimon Schubert padcount = 16 - 2 - strlen (cp_abis[i]->shortname); 2805796c8dcSSimon Schubert pad[padcount] = 0; 2815796c8dcSSimon Schubert while (padcount > 0) 2825796c8dcSSimon Schubert pad[--padcount] = ' '; 2835796c8dcSSimon Schubert ui_out_text (uiout, pad); 2845796c8dcSSimon Schubert 2855796c8dcSSimon Schubert ui_out_field_string (uiout, "doc", cp_abis[i]->doc); 2865796c8dcSSimon Schubert ui_out_text (uiout, "\n"); 2875796c8dcSSimon Schubert } 2885796c8dcSSimon Schubert do_cleanups (cleanup_chain); 2895796c8dcSSimon Schubert } 2905796c8dcSSimon Schubert 2915796c8dcSSimon Schubert /* Set the current C++ ABI, or display the list of options if no 2925796c8dcSSimon Schubert argument is given. */ 2935796c8dcSSimon Schubert 2945796c8dcSSimon Schubert static void 2955796c8dcSSimon Schubert set_cp_abi_cmd (char *args, int from_tty) 2965796c8dcSSimon Schubert { 2975796c8dcSSimon Schubert if (args == NULL) 2985796c8dcSSimon Schubert { 2995796c8dcSSimon Schubert list_cp_abis (from_tty); 3005796c8dcSSimon Schubert return; 3015796c8dcSSimon Schubert } 3025796c8dcSSimon Schubert 3035796c8dcSSimon Schubert if (!switch_to_cp_abi (args)) 3045796c8dcSSimon Schubert error (_("Could not find \"%s\" in ABI list"), args); 3055796c8dcSSimon Schubert } 3065796c8dcSSimon Schubert 3075796c8dcSSimon Schubert /* Show the currently selected C++ ABI. */ 3085796c8dcSSimon Schubert 3095796c8dcSSimon Schubert static void 3105796c8dcSSimon Schubert show_cp_abi_cmd (char *args, int from_tty) 3115796c8dcSSimon Schubert { 3125796c8dcSSimon Schubert ui_out_text (uiout, "The currently selected C++ ABI is \""); 3135796c8dcSSimon Schubert 3145796c8dcSSimon Schubert ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname); 3155796c8dcSSimon Schubert ui_out_text (uiout, "\" ("); 3165796c8dcSSimon Schubert ui_out_field_string (uiout, "longname", current_cp_abi.longname); 3175796c8dcSSimon Schubert ui_out_text (uiout, ").\n"); 3185796c8dcSSimon Schubert } 3195796c8dcSSimon Schubert 3205796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */ 3215796c8dcSSimon Schubert 3225796c8dcSSimon Schubert void 3235796c8dcSSimon Schubert _initialize_cp_abi (void) 3245796c8dcSSimon Schubert { 3255796c8dcSSimon Schubert register_cp_abi (&auto_cp_abi); 3265796c8dcSSimon Schubert switch_to_cp_abi ("auto"); 3275796c8dcSSimon Schubert 3285796c8dcSSimon Schubert add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\ 3295796c8dcSSimon Schubert Set the ABI used for inspecting C++ objects.\n\ 3305796c8dcSSimon Schubert \"set cp-abi\" with no arguments will list the available ABIs."), 3315796c8dcSSimon Schubert &setlist); 3325796c8dcSSimon Schubert 3335796c8dcSSimon Schubert add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd, 334*c50c785cSJohn Marino _("Show the ABI used for inspecting C++ objects."), 335*c50c785cSJohn Marino &showlist); 3365796c8dcSSimon Schubert } 337