gdb/cp-abi.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Generic code for supporting multiple C++ ABI's

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "value.h"
  16. #include "cp-abi.h"
  17. #include "command.h"
  18. #include "gdbcmd.h"
  19. #include "ui-out.h"
  20. static struct cp_abi_ops *find_cp_abi (const char *short_name);

  21. static struct cp_abi_ops current_cp_abi = { "", NULL };
  22. static struct cp_abi_ops auto_cp_abi = { "auto", NULL };

  23. #define CP_ABI_MAX 8
  24. static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
  25. static int num_cp_abis = 0;

  26. enum ctor_kinds
  27. is_constructor_name (const char *name)
  28. {
  29.   if ((current_cp_abi.is_constructor_name) == NULL)
  30.     error (_("ABI doesn't define required function is_constructor_name"));
  31.   return (*current_cp_abi.is_constructor_name) (name);
  32. }

  33. enum dtor_kinds
  34. is_destructor_name (const char *name)
  35. {
  36.   if ((current_cp_abi.is_destructor_name) == NULL)
  37.     error (_("ABI doesn't define required function is_destructor_name"));
  38.   return (*current_cp_abi.is_destructor_name) (name);
  39. }

  40. int
  41. is_vtable_name (const char *name)
  42. {
  43.   if ((current_cp_abi.is_vtable_name) == NULL)
  44.     error (_("ABI doesn't define required function is_vtable_name"));
  45.   return (*current_cp_abi.is_vtable_name) (name);
  46. }

  47. int
  48. is_operator_name (const char *name)
  49. {
  50.   if ((current_cp_abi.is_operator_name) == NULL)
  51.     error (_("ABI doesn't define required function is_operator_name"));
  52.   return (*current_cp_abi.is_operator_name) (name);
  53. }

  54. int
  55. baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
  56.                   int embedded_offset, CORE_ADDR address,
  57.                   const struct value *val)
  58. {
  59.   volatile struct gdb_exception ex;
  60.   int res = 0;

  61.   gdb_assert (current_cp_abi.baseclass_offset != NULL);

  62.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  63.     {
  64.       res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
  65.                                                 embedded_offset,
  66.                                                 address, val);
  67.     }

  68.   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
  69.     throw_error (NOT_AVAILABLE_ERROR,
  70.                  _("Cannot determine virtual baseclass offset "
  71.                    "of incomplete object"));
  72.   else if (ex.reason < 0)
  73.     throw_exception (ex);
  74.   else
  75.     return res;
  76. }

  77. struct value *
  78. value_virtual_fn_field (struct value **arg1p,
  79.                         struct fn_field *f, int j,
  80.                         struct type *type, int offset)
  81. {
  82.   if ((current_cp_abi.virtual_fn_field) == NULL)
  83.     return NULL;
  84.   return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
  85.                                              type, offset);
  86. }

  87. struct type *
  88. value_rtti_type (struct value *v, int *full,
  89.                  int *top, int *using_enc)
  90. {
  91.   struct type *ret = NULL;
  92.   volatile struct gdb_exception e;

  93.   if ((current_cp_abi.rtti_type) == NULL)
  94.     return NULL;
  95.   TRY_CATCH (e, RETURN_MASK_ERROR)
  96.     {
  97.       ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
  98.     }
  99.   if (e.reason < 0)
  100.     return NULL;
  101.   return ret;
  102. }

  103. void
  104. cplus_print_method_ptr (const gdb_byte *contents,
  105.                         struct type *type,
  106.                         struct ui_file *stream)
  107. {
  108.   if (current_cp_abi.print_method_ptr == NULL)
  109.     error (_("GDB does not support pointers to methods on this target"));
  110.   (*current_cp_abi.print_method_ptr) (contents, type, stream);
  111. }

  112. int
  113. cplus_method_ptr_size (struct type *to_type)
  114. {
  115.   if (current_cp_abi.method_ptr_size == NULL)
  116.     error (_("GDB does not support pointers to methods on this target"));
  117.   return (*current_cp_abi.method_ptr_size) (to_type);
  118. }

  119. void
  120. cplus_make_method_ptr (struct type *type, gdb_byte *contents,
  121.                        CORE_ADDR value, int is_virtual)
  122. {
  123.   if (current_cp_abi.make_method_ptr == NULL)
  124.     error (_("GDB does not support pointers to methods on this target"));
  125.   (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
  126. }

  127. CORE_ADDR
  128. cplus_skip_trampoline (struct frame_info *frame,
  129.                        CORE_ADDR stop_pc)
  130. {
  131.   if (current_cp_abi.skip_trampoline == NULL)
  132.     return 0;
  133.   return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
  134. }

  135. struct value *
  136. cplus_method_ptr_to_value (struct value **this_p,
  137.                            struct value *method_ptr)
  138. {
  139.   if (current_cp_abi.method_ptr_to_value == NULL)
  140.     error (_("GDB does not support pointers to methods on this target"));
  141.   return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
  142. }

  143. /* See cp-abi.h.  */

  144. void
  145. cplus_print_vtable (struct value *value)
  146. {
  147.   if (current_cp_abi.print_vtable == NULL)
  148.     error (_("GDB cannot print the vtable on this target"));
  149.   (*current_cp_abi.print_vtable) (value);
  150. }

  151. /* See cp-abi.h.  */

  152. struct value *
  153. cplus_typeid (struct value *value)
  154. {
  155.   if (current_cp_abi.get_typeid == NULL)
  156.     error (_("GDB cannot find the typeid on this target"));
  157.   return (*current_cp_abi.get_typeid) (value);
  158. }

  159. /* See cp-abi.h.  */

  160. struct type *
  161. cplus_typeid_type (struct gdbarch *gdbarch)
  162. {
  163.   if (current_cp_abi.get_typeid_type == NULL)
  164.     error (_("GDB cannot find the type for 'typeid' on this target"));
  165.   return (*current_cp_abi.get_typeid_type) (gdbarch);
  166. }

  167. /* See cp-abi.h.  */

  168. struct type *
  169. cplus_type_from_type_info (struct value *value)
  170. {
  171.   if (current_cp_abi.get_type_from_type_info == NULL)
  172.     error (_("GDB cannot find the type from a std::type_info on this target"));
  173.   return (*current_cp_abi.get_type_from_type_info) (value);
  174. }

  175. /* See cp-abi.h.  */

  176. char *
  177. cplus_typename_from_type_info (struct value *value)
  178. {
  179.   if (current_cp_abi.get_typename_from_type_info == NULL)
  180.     error (_("GDB cannot find the type name "
  181.              "from a std::type_info on this target"));
  182.   return (*current_cp_abi.get_typename_from_type_info) (value);
  183. }

  184. int
  185. cp_pass_by_reference (struct type *type)
  186. {
  187.   if ((current_cp_abi.pass_by_reference) == NULL)
  188.     return 0;
  189.   return (*current_cp_abi.pass_by_reference) (type);
  190. }

  191. /* Set the current C++ ABI to SHORT_NAME.  */

  192. static int
  193. switch_to_cp_abi (const char *short_name)
  194. {
  195.   struct cp_abi_ops *abi;

  196.   abi = find_cp_abi (short_name);
  197.   if (abi == NULL)
  198.     return 0;

  199.   current_cp_abi = *abi;
  200.   return 1;
  201. }

  202. /* Add ABI to the list of supported C++ ABI's.  */

  203. int
  204. register_cp_abi (struct cp_abi_ops *abi)
  205. {
  206.   if (num_cp_abis == CP_ABI_MAX)
  207.     internal_error (__FILE__, __LINE__,
  208.                     _("Too many C++ ABIs, please increase "
  209.                       "CP_ABI_MAX in cp-abi.c"));

  210.   cp_abis[num_cp_abis++] = abi;

  211.   return 1;
  212. }

  213. /* Set the ABI to use in "auto" mode to SHORT_NAME.  */

  214. void
  215. set_cp_abi_as_auto_default (const char *short_name)
  216. {
  217.   char *new_longname, *new_doc;
  218.   struct cp_abi_ops *abi = find_cp_abi (short_name);

  219.   if (abi == NULL)
  220.     internal_error (__FILE__, __LINE__,
  221.                     _("Cannot find C++ ABI \"%s\" to set it as auto default."),
  222.                     short_name);

  223.   if (auto_cp_abi.longname != NULL)
  224.     xfree ((char *) auto_cp_abi.longname);
  225.   if (auto_cp_abi.doc != NULL)
  226.     xfree ((char *) auto_cp_abi.doc);

  227.   auto_cp_abi = *abi;

  228.   auto_cp_abi.shortname = "auto";
  229.   new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
  230.   auto_cp_abi.longname = new_longname;

  231.   new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
  232.              abi->shortname);
  233.   auto_cp_abi.doc = new_doc;

  234.   /* Since we copy the current ABI into current_cp_abi instead of
  235.      using a pointer, if auto is currently the default, we need to
  236.      reset it.  */
  237.   if (strcmp (current_cp_abi.shortname, "auto") == 0)
  238.     switch_to_cp_abi ("auto");
  239. }

  240. /* Return the ABI operations associated with SHORT_NAME.  */

  241. static struct cp_abi_ops *
  242. find_cp_abi (const char *short_name)
  243. {
  244.   int i;

  245.   for (i = 0; i < num_cp_abis; i++)
  246.     if (strcmp (cp_abis[i]->shortname, short_name) == 0)
  247.       return cp_abis[i];

  248.   return NULL;
  249. }

  250. /* Display the list of registered C++ ABIs.  */

  251. static void
  252. list_cp_abis (int from_tty)
  253. {
  254.   struct ui_out *uiout = current_uiout;
  255.   struct cleanup *cleanup_chain;
  256.   int i;

  257.   ui_out_text (uiout, "The available C++ ABIs are:\n");
  258.   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout,
  259.                                                        "cp-abi-list");
  260.   for (i = 0; i < num_cp_abis; i++)
  261.     {
  262.       char pad[14];
  263.       int padcount;

  264.       ui_out_text (uiout, "  ");
  265.       ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname);

  266.       padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
  267.       pad[padcount] = 0;
  268.       while (padcount > 0)
  269.         pad[--padcount] = ' ';
  270.       ui_out_text (uiout, pad);

  271.       ui_out_field_string (uiout, "doc", cp_abis[i]->doc);
  272.       ui_out_text (uiout, "\n");
  273.     }
  274.   do_cleanups (cleanup_chain);
  275. }

  276. /* Set the current C++ ABI, or display the list of options if no
  277.    argument is given.  */

  278. static void
  279. set_cp_abi_cmd (char *args, int from_tty)
  280. {
  281.   if (args == NULL)
  282.     {
  283.       list_cp_abis (from_tty);
  284.       return;
  285.     }

  286.   if (!switch_to_cp_abi (args))
  287.     error (_("Could not find \"%s\" in ABI list"), args);
  288. }

  289. /* A completion function for "set cp-abi".  */

  290. static VEC (char_ptr) *
  291. cp_abi_completer (struct cmd_list_element *ignore,
  292.                   const char *text, const char *word)
  293. {
  294.   static const char **cp_abi_names;

  295.   if (cp_abi_names == NULL)
  296.     {
  297.       int i;

  298.       cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
  299.       for (i = 0; i < num_cp_abis; ++i)
  300.         cp_abi_names[i] = cp_abis[i]->shortname;
  301.       cp_abi_names[i] = NULL;
  302.     }

  303.   return complete_on_enum (cp_abi_names, text, word);
  304. }

  305. /* Show the currently selected C++ ABI.  */

  306. static void
  307. show_cp_abi_cmd (char *args, int from_tty)
  308. {
  309.   struct ui_out *uiout = current_uiout;

  310.   ui_out_text (uiout, "The currently selected C++ ABI is \"");

  311.   ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname);
  312.   ui_out_text (uiout, "\" (");
  313.   ui_out_field_string (uiout, "longname", current_cp_abi.longname);
  314.   ui_out_text (uiout, ").\n");
  315. }

  316. extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */

  317. void
  318. _initialize_cp_abi (void)
  319. {
  320.   struct cmd_list_element *c;

  321.   register_cp_abi (&auto_cp_abi);
  322.   switch_to_cp_abi ("auto");

  323.   c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
  324. Set the ABI used for inspecting C++ objects.\n\
  325. \"set cp-abi\" with no arguments will list the available ABIs."),
  326.                &setlist);
  327.   set_cmd_completer (c, cp_abi_completer);

  328.   add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
  329.            _("Show the ABI used for inspecting C++ objects."),
  330.            &showlist);
  331. }