cp-abi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Generic code for supporting multiple C++ ABI's
  2. Copyright (C) 2001-2022 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. LONGEST embedded_offset, CORE_ADDR address,
  57. const struct value *val)
  58. {
  59. int res = 0;
  60. gdb_assert (current_cp_abi.baseclass_offset != NULL);
  61. try
  62. {
  63. res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
  64. embedded_offset,
  65. address, val);
  66. }
  67. catch (const gdb_exception_error &ex)
  68. {
  69. if (ex.error != NOT_AVAILABLE_ERROR)
  70. throw;
  71. throw_error (NOT_AVAILABLE_ERROR,
  72. _("Cannot determine virtual baseclass offset "
  73. "of incomplete object"));
  74. }
  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. LONGEST *top, int *using_enc)
  90. {
  91. struct type *ret = NULL;
  92. if ((current_cp_abi.rtti_type) == NULL
  93. || !HAVE_CPLUS_STRUCT (check_typedef (value_type (v))))
  94. return NULL;
  95. try
  96. {
  97. ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
  98. }
  99. catch (const gdb_exception_error &e)
  100. {
  101. return NULL;
  102. }
  103. return ret;
  104. }
  105. void
  106. cplus_print_method_ptr (const gdb_byte *contents,
  107. struct type *type,
  108. struct ui_file *stream)
  109. {
  110. if (current_cp_abi.print_method_ptr == NULL)
  111. error (_("GDB does not support pointers to methods on this target"));
  112. (*current_cp_abi.print_method_ptr) (contents, type, stream);
  113. }
  114. int
  115. cplus_method_ptr_size (struct type *to_type)
  116. {
  117. if (current_cp_abi.method_ptr_size == NULL)
  118. error (_("GDB does not support pointers to methods on this target"));
  119. return (*current_cp_abi.method_ptr_size) (to_type);
  120. }
  121. void
  122. cplus_make_method_ptr (struct type *type, gdb_byte *contents,
  123. CORE_ADDR value, int is_virtual)
  124. {
  125. if (current_cp_abi.make_method_ptr == NULL)
  126. error (_("GDB does not support pointers to methods on this target"));
  127. (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
  128. }
  129. CORE_ADDR
  130. cplus_skip_trampoline (struct frame_info *frame,
  131. CORE_ADDR stop_pc)
  132. {
  133. if (current_cp_abi.skip_trampoline == NULL)
  134. return 0;
  135. return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
  136. }
  137. struct value *
  138. cplus_method_ptr_to_value (struct value **this_p,
  139. struct value *method_ptr)
  140. {
  141. if (current_cp_abi.method_ptr_to_value == NULL)
  142. error (_("GDB does not support pointers to methods on this target"));
  143. return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
  144. }
  145. /* See cp-abi.h. */
  146. void
  147. cplus_print_vtable (struct value *value)
  148. {
  149. if (current_cp_abi.print_vtable == NULL)
  150. error (_("GDB cannot print the vtable on this target"));
  151. (*current_cp_abi.print_vtable) (value);
  152. }
  153. /* See cp-abi.h. */
  154. struct value *
  155. cplus_typeid (struct value *value)
  156. {
  157. if (current_cp_abi.get_typeid == NULL)
  158. error (_("GDB cannot find the typeid on this target"));
  159. return (*current_cp_abi.get_typeid) (value);
  160. }
  161. /* See cp-abi.h. */
  162. struct type *
  163. cplus_typeid_type (struct gdbarch *gdbarch)
  164. {
  165. if (current_cp_abi.get_typeid_type == NULL)
  166. error (_("GDB cannot find the type for 'typeid' on this target"));
  167. return (*current_cp_abi.get_typeid_type) (gdbarch);
  168. }
  169. /* See cp-abi.h. */
  170. struct type *
  171. cplus_type_from_type_info (struct value *value)
  172. {
  173. if (current_cp_abi.get_type_from_type_info == NULL)
  174. error (_("GDB cannot find the type from a std::type_info on this target"));
  175. return (*current_cp_abi.get_type_from_type_info) (value);
  176. }
  177. /* See cp-abi.h. */
  178. std::string
  179. cplus_typename_from_type_info (struct value *value)
  180. {
  181. if (current_cp_abi.get_typename_from_type_info == NULL)
  182. error (_("GDB cannot find the type name "
  183. "from a std::type_info on this target"));
  184. return (*current_cp_abi.get_typename_from_type_info) (value);
  185. }
  186. /* See cp-abi.h. */
  187. struct language_pass_by_ref_info
  188. cp_pass_by_reference (struct type *type)
  189. {
  190. if ((current_cp_abi.pass_by_reference) == NULL)
  191. return {};
  192. return (*current_cp_abi.pass_by_reference) (type);
  193. }
  194. /* Set the current C++ ABI to SHORT_NAME. */
  195. static int
  196. switch_to_cp_abi (const char *short_name)
  197. {
  198. struct cp_abi_ops *abi;
  199. abi = find_cp_abi (short_name);
  200. if (abi == NULL)
  201. return 0;
  202. current_cp_abi = *abi;
  203. return 1;
  204. }
  205. /* Add ABI to the list of supported C++ ABI's. */
  206. int
  207. register_cp_abi (struct cp_abi_ops *abi)
  208. {
  209. if (num_cp_abis == CP_ABI_MAX)
  210. internal_error (__FILE__, __LINE__,
  211. _("Too many C++ ABIs, please increase "
  212. "CP_ABI_MAX in cp-abi.c"));
  213. cp_abis[num_cp_abis++] = abi;
  214. return 1;
  215. }
  216. /* Set the ABI to use in "auto" mode to SHORT_NAME. */
  217. void
  218. set_cp_abi_as_auto_default (const char *short_name)
  219. {
  220. struct cp_abi_ops *abi = find_cp_abi (short_name);
  221. if (abi == NULL)
  222. internal_error (__FILE__, __LINE__,
  223. _("Cannot find C++ ABI \"%s\" to set it as auto default."),
  224. short_name);
  225. xfree ((char *) auto_cp_abi.longname);
  226. xfree ((char *) auto_cp_abi.doc);
  227. auto_cp_abi = *abi;
  228. auto_cp_abi.shortname = "auto";
  229. auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
  230. abi->shortname).release ();
  231. auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
  232. abi->shortname).release ();
  233. /* Since we copy the current ABI into current_cp_abi instead of
  234. using a pointer, if auto is currently the default, we need to
  235. reset it. */
  236. if (strcmp (current_cp_abi.shortname, "auto") == 0)
  237. switch_to_cp_abi ("auto");
  238. }
  239. /* Return the ABI operations associated with SHORT_NAME. */
  240. static struct cp_abi_ops *
  241. find_cp_abi (const char *short_name)
  242. {
  243. int i;
  244. for (i = 0; i < num_cp_abis; i++)
  245. if (strcmp (cp_abis[i]->shortname, short_name) == 0)
  246. return cp_abis[i];
  247. return NULL;
  248. }
  249. /* Display the list of registered C++ ABIs. */
  250. static void
  251. list_cp_abis (int from_tty)
  252. {
  253. struct ui_out *uiout = current_uiout;
  254. int i;
  255. uiout->text ("The available C++ ABIs are:\n");
  256. ui_out_emit_tuple tuple_emitter (uiout, "cp-abi-list");
  257. for (i = 0; i < num_cp_abis; i++)
  258. {
  259. char pad[14];
  260. int padcount;
  261. uiout->text (" ");
  262. uiout->field_string ("cp-abi", cp_abis[i]->shortname);
  263. padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
  264. pad[padcount] = 0;
  265. while (padcount > 0)
  266. pad[--padcount] = ' ';
  267. uiout->text (pad);
  268. uiout->field_string ("doc", cp_abis[i]->doc);
  269. uiout->text ("\n");
  270. }
  271. }
  272. /* Set the current C++ ABI, or display the list of options if no
  273. argument is given. */
  274. static void
  275. set_cp_abi_cmd (const char *args, int from_tty)
  276. {
  277. if (args == NULL)
  278. {
  279. list_cp_abis (from_tty);
  280. return;
  281. }
  282. if (!switch_to_cp_abi (args))
  283. error (_("Could not find \"%s\" in ABI list"), args);
  284. }
  285. /* A completion function for "set cp-abi". */
  286. static void
  287. cp_abi_completer (struct cmd_list_element *ignore,
  288. completion_tracker &tracker,
  289. const char *text, const char *word)
  290. {
  291. static const char **cp_abi_names;
  292. if (cp_abi_names == NULL)
  293. {
  294. int i;
  295. cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
  296. for (i = 0; i < num_cp_abis; ++i)
  297. cp_abi_names[i] = cp_abis[i]->shortname;
  298. cp_abi_names[i] = NULL;
  299. }
  300. complete_on_enum (tracker, cp_abi_names, text, word);
  301. }
  302. /* Show the currently selected C++ ABI. */
  303. static void
  304. show_cp_abi_cmd (const char *args, int from_tty)
  305. {
  306. struct ui_out *uiout = current_uiout;
  307. uiout->text ("The currently selected C++ ABI is \"");
  308. uiout->field_string ("cp-abi", current_cp_abi.shortname);
  309. uiout->text ("\" (");
  310. uiout->field_string ("longname", current_cp_abi.longname);
  311. uiout->text (").\n");
  312. }
  313. void _initialize_cp_abi ();
  314. void
  315. _initialize_cp_abi ()
  316. {
  317. struct cmd_list_element *c;
  318. register_cp_abi (&auto_cp_abi);
  319. switch_to_cp_abi ("auto");
  320. c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
  321. Set the ABI used for inspecting C++ objects.\n\
  322. \"set cp-abi\" with no arguments will list the available ABIs."),
  323. &setlist);
  324. set_cmd_completer (c, cp_abi_completer);
  325. add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
  326. _("Show the ABI used for inspecting C++ objects."),
  327. &showlist);
  328. }