py-xmethods.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* Support for debug methods in Python.
  2. Copyright (C) 2013-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 "arch-utils.h"
  16. #include "extension-priv.h"
  17. #include "objfiles.h"
  18. #include "value.h"
  19. #include "language.h"
  20. #include "python.h"
  21. #include "python-internal.h"
  22. static const char enabled_field_name[] = "enabled";
  23. static const char match_method_name[] = "match";
  24. static const char get_arg_types_method_name[] = "get_arg_types";
  25. static const char get_result_type_method_name[] = "get_result_type";
  26. static const char matchers_attr_str[] = "xmethods";
  27. static PyObject *py_match_method_name = NULL;
  28. static PyObject *py_get_arg_types_method_name = NULL;
  29. struct python_xmethod_worker : xmethod_worker
  30. {
  31. python_xmethod_worker (PyObject *worker, PyObject *this_type);
  32. ~python_xmethod_worker ();
  33. DISABLE_COPY_AND_ASSIGN (python_xmethod_worker);
  34. /* Implementation of xmethod_worker::invoke for Python. */
  35. value *invoke (value *obj, gdb::array_view<value *> args) override;
  36. /* Implementation of xmethod_worker::do_get_arg_types for Python. */
  37. ext_lang_rc do_get_arg_types (std::vector<type *> *type_args) override;
  38. /* Implementation of xmethod_worker::do_get_result_type for Python.
  39. For backward compatibility with 7.9, which did not support getting the
  40. result type, if the get_result_type operation is not provided by WORKER
  41. then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE. */
  42. ext_lang_rc do_get_result_type (value *obj, gdb::array_view<value *> args,
  43. type **result_type_ptr) override;
  44. private:
  45. PyObject *m_py_worker;
  46. PyObject *m_this_type;
  47. };
  48. python_xmethod_worker::~python_xmethod_worker ()
  49. {
  50. /* We don't do much here, but we still need the GIL. */
  51. gdbpy_enter enter_py;
  52. Py_DECREF (m_py_worker);
  53. Py_DECREF (m_this_type);
  54. }
  55. /* Invoke the "match" method of the MATCHER and return a new reference
  56. to the result. Returns NULL on error. */
  57. static PyObject *
  58. invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
  59. const char *xmethod_name)
  60. {
  61. int enabled;
  62. gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
  63. enabled_field_name));
  64. if (enabled_field == NULL)
  65. return NULL;
  66. enabled = PyObject_IsTrue (enabled_field.get ());
  67. if (enabled == -1)
  68. return NULL;
  69. if (enabled == 0)
  70. {
  71. /* Return 'None' if the matcher is not enabled. */
  72. Py_RETURN_NONE;
  73. }
  74. gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
  75. match_method_name));
  76. if (match_method == NULL)
  77. return NULL;
  78. gdbpy_ref<> py_xmethod_name (PyUnicode_FromString (xmethod_name));
  79. if (py_xmethod_name == NULL)
  80. return NULL;
  81. return PyObject_CallMethodObjArgs (matcher, py_match_method_name,
  82. py_obj_type, py_xmethod_name.get (),
  83. NULL);
  84. }
  85. /* Implementation of get_matching_xmethod_workers for Python. */
  86. enum ext_lang_rc
  87. gdbpy_get_matching_xmethod_workers
  88. (const struct extension_language_defn *extlang,
  89. struct type *obj_type, const char *method_name,
  90. std::vector<xmethod_worker_up> *dm_vec)
  91. {
  92. gdb_assert (obj_type != NULL && method_name != NULL);
  93. gdbpy_enter enter_py;
  94. gdbpy_ref<> py_type (type_to_type_object (obj_type));
  95. if (py_type == NULL)
  96. {
  97. gdbpy_print_stack ();
  98. return EXT_LANG_RC_ERROR;
  99. }
  100. /* Create an empty list of debug methods. */
  101. gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
  102. if (py_xmethod_matcher_list == NULL)
  103. {
  104. gdbpy_print_stack ();
  105. return EXT_LANG_RC_ERROR;
  106. }
  107. /* Gather debug method matchers registered with the object files.
  108. This could be done differently by iterating over each objfile's matcher
  109. list individually, but there's no data yet to show it's needed. */
  110. for (objfile *objfile : current_program_space->objfiles ())
  111. {
  112. gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
  113. if (py_objfile == NULL)
  114. {
  115. gdbpy_print_stack ();
  116. return EXT_LANG_RC_ERROR;
  117. }
  118. gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile.get (),
  119. NULL));
  120. gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
  121. objfile_matchers.get ()));
  122. if (temp == NULL)
  123. {
  124. gdbpy_print_stack ();
  125. return EXT_LANG_RC_ERROR;
  126. }
  127. py_xmethod_matcher_list = std::move (temp);
  128. }
  129. /* Gather debug methods matchers registered with the current program
  130. space. */
  131. gdbpy_ref<> py_progspace = pspace_to_pspace_object (current_program_space);
  132. if (py_progspace != NULL)
  133. {
  134. gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace.get (),
  135. NULL));
  136. gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
  137. pspace_matchers.get ()));
  138. if (temp == NULL)
  139. {
  140. gdbpy_print_stack ();
  141. return EXT_LANG_RC_ERROR;
  142. }
  143. py_xmethod_matcher_list = std::move (temp);
  144. }
  145. else
  146. {
  147. gdbpy_print_stack ();
  148. return EXT_LANG_RC_ERROR;
  149. }
  150. /* Gather debug method matchers registered globally. */
  151. if (gdb_python_module != NULL
  152. && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
  153. {
  154. gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
  155. matchers_attr_str));
  156. if (gdb_matchers != NULL)
  157. {
  158. gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
  159. gdb_matchers.get ()));
  160. if (temp == NULL)
  161. {
  162. gdbpy_print_stack ();
  163. return EXT_LANG_RC_ERROR;
  164. }
  165. py_xmethod_matcher_list = std::move (temp);
  166. }
  167. else
  168. {
  169. gdbpy_print_stack ();
  170. return EXT_LANG_RC_ERROR;
  171. }
  172. }
  173. gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
  174. if (list_iter == NULL)
  175. {
  176. gdbpy_print_stack ();
  177. return EXT_LANG_RC_ERROR;
  178. }
  179. while (true)
  180. {
  181. gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
  182. if (matcher == NULL)
  183. {
  184. if (PyErr_Occurred ())
  185. {
  186. gdbpy_print_stack ();
  187. return EXT_LANG_RC_ERROR;
  188. }
  189. break;
  190. }
  191. gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
  192. py_type.get (),
  193. method_name));
  194. if (match_result == NULL)
  195. {
  196. gdbpy_print_stack ();
  197. return EXT_LANG_RC_ERROR;
  198. }
  199. if (match_result == Py_None)
  200. ; /* This means there was no match. */
  201. else if (PySequence_Check (match_result.get ()))
  202. {
  203. gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
  204. if (iter == NULL)
  205. {
  206. gdbpy_print_stack ();
  207. return EXT_LANG_RC_ERROR;
  208. }
  209. while (true)
  210. {
  211. struct xmethod_worker *worker;
  212. gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
  213. if (py_worker == NULL)
  214. {
  215. if (PyErr_Occurred ())
  216. {
  217. gdbpy_print_stack ();
  218. return EXT_LANG_RC_ERROR;
  219. }
  220. break;
  221. }
  222. worker = new python_xmethod_worker (py_worker.get (),
  223. py_type.get ());
  224. dm_vec->emplace_back (worker);
  225. }
  226. }
  227. else
  228. {
  229. struct xmethod_worker *worker;
  230. worker = new python_xmethod_worker (match_result.get (),
  231. py_type.get ());
  232. dm_vec->emplace_back (worker);
  233. }
  234. }
  235. return EXT_LANG_RC_OK;
  236. }
  237. /* See declaration. */
  238. ext_lang_rc
  239. python_xmethod_worker::do_get_arg_types (std::vector<type *> *arg_types)
  240. {
  241. /* The gdbpy_enter object needs to be placed first, so that it's the last to
  242. be destroyed. */
  243. gdbpy_enter enter_py;
  244. struct type *obj_type;
  245. int i = 1, arg_count;
  246. gdbpy_ref<> list_iter;
  247. gdbpy_ref<> get_arg_types_method
  248. (PyObject_GetAttrString (m_py_worker, get_arg_types_method_name));
  249. if (get_arg_types_method == NULL)
  250. {
  251. gdbpy_print_stack ();
  252. return EXT_LANG_RC_ERROR;
  253. }
  254. gdbpy_ref<> py_argtype_list
  255. (PyObject_CallMethodObjArgs (m_py_worker, py_get_arg_types_method_name,
  256. NULL));
  257. if (py_argtype_list == NULL)
  258. {
  259. gdbpy_print_stack ();
  260. return EXT_LANG_RC_ERROR;
  261. }
  262. if (py_argtype_list == Py_None)
  263. arg_count = 0;
  264. else if (PySequence_Check (py_argtype_list.get ()))
  265. {
  266. arg_count = PySequence_Size (py_argtype_list.get ());
  267. if (arg_count == -1)
  268. {
  269. gdbpy_print_stack ();
  270. return EXT_LANG_RC_ERROR;
  271. }
  272. list_iter.reset (PyObject_GetIter (py_argtype_list.get ()));
  273. if (list_iter == NULL)
  274. {
  275. gdbpy_print_stack ();
  276. return EXT_LANG_RC_ERROR;
  277. }
  278. }
  279. else
  280. arg_count = 1;
  281. /* Include the 'this' argument in the size. */
  282. arg_types->resize (arg_count + 1);
  283. i = 1;
  284. if (list_iter != NULL)
  285. {
  286. while (true)
  287. {
  288. gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
  289. if (item == NULL)
  290. {
  291. if (PyErr_Occurred ())
  292. {
  293. gdbpy_print_stack ();
  294. return EXT_LANG_RC_ERROR;
  295. }
  296. break;
  297. }
  298. struct type *arg_type = type_object_to_type (item.get ());
  299. if (arg_type == NULL)
  300. {
  301. PyErr_SetString (PyExc_TypeError,
  302. _("Arg type returned by the get_arg_types "
  303. "method of a debug method worker object is "
  304. "not a gdb.Type object."));
  305. return EXT_LANG_RC_ERROR;
  306. }
  307. (*arg_types)[i] = arg_type;
  308. i++;
  309. }
  310. }
  311. else if (arg_count == 1)
  312. {
  313. /* py_argtype_list is not actually a list but a single gdb.Type
  314. object. */
  315. struct type *arg_type = type_object_to_type (py_argtype_list.get ());
  316. if (arg_type == NULL)
  317. {
  318. PyErr_SetString (PyExc_TypeError,
  319. _("Arg type returned by the get_arg_types method "
  320. "of an xmethod worker object is not a gdb.Type "
  321. "object."));
  322. return EXT_LANG_RC_ERROR;
  323. }
  324. else
  325. {
  326. (*arg_types)[i] = arg_type;
  327. i++;
  328. }
  329. }
  330. /* Add the type of 'this' as the first argument. The 'this' pointer should
  331. be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
  332. type. */
  333. obj_type = type_object_to_type (m_this_type);
  334. (*arg_types)[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type),
  335. NULL);
  336. return EXT_LANG_RC_OK;
  337. }
  338. /* See declaration. */
  339. ext_lang_rc
  340. python_xmethod_worker::do_get_result_type (value *obj,
  341. gdb::array_view<value *> args,
  342. type **result_type_ptr)
  343. {
  344. struct type *obj_type, *this_type;
  345. int i;
  346. gdbpy_enter enter_py;
  347. /* First see if there is a get_result_type method.
  348. If not this could be an old xmethod (pre 7.9.1). */
  349. gdbpy_ref<> get_result_type_method
  350. (PyObject_GetAttrString (m_py_worker, get_result_type_method_name));
  351. if (get_result_type_method == NULL)
  352. {
  353. PyErr_Clear ();
  354. *result_type_ptr = NULL;
  355. return EXT_LANG_RC_OK;
  356. }
  357. obj_type = check_typedef (value_type (obj));
  358. this_type = check_typedef (type_object_to_type (m_this_type));
  359. if (obj_type->code () == TYPE_CODE_PTR)
  360. {
  361. struct type *this_ptr = lookup_pointer_type (this_type);
  362. if (!types_equal (obj_type, this_ptr))
  363. obj = value_cast (this_ptr, obj);
  364. }
  365. else if (TYPE_IS_REFERENCE (obj_type))
  366. {
  367. struct type *this_ref
  368. = lookup_reference_type (this_type, obj_type->code ());
  369. if (!types_equal (obj_type, this_ref))
  370. obj = value_cast (this_ref, obj);
  371. }
  372. else
  373. {
  374. if (!types_equal (obj_type, this_type))
  375. obj = value_cast (this_type, obj);
  376. }
  377. gdbpy_ref<> py_value_obj (value_to_value_object (obj));
  378. if (py_value_obj == NULL)
  379. {
  380. gdbpy_print_stack ();
  381. return EXT_LANG_RC_ERROR;
  382. }
  383. gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
  384. if (py_arg_tuple == NULL)
  385. {
  386. gdbpy_print_stack ();
  387. return EXT_LANG_RC_ERROR;
  388. }
  389. /* PyTuple_SET_ITEM steals the reference of the element, hence the
  390. release. */
  391. PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
  392. for (i = 0; i < args.size (); i++)
  393. {
  394. PyObject *py_value_arg = value_to_value_object (args[i]);
  395. if (py_value_arg == NULL)
  396. {
  397. gdbpy_print_stack ();
  398. return EXT_LANG_RC_ERROR;
  399. }
  400. PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
  401. }
  402. gdbpy_ref<> py_result_type
  403. (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
  404. if (py_result_type == NULL)
  405. {
  406. gdbpy_print_stack ();
  407. return EXT_LANG_RC_ERROR;
  408. }
  409. *result_type_ptr = type_object_to_type (py_result_type.get ());
  410. if (*result_type_ptr == NULL)
  411. {
  412. PyErr_SetString (PyExc_TypeError,
  413. _("Type returned by the get_result_type method of an"
  414. " xmethod worker object is not a gdb.Type object."));
  415. gdbpy_print_stack ();
  416. return EXT_LANG_RC_ERROR;
  417. }
  418. return EXT_LANG_RC_OK;
  419. }
  420. /* See declaration. */
  421. struct value *
  422. python_xmethod_worker::invoke (struct value *obj,
  423. gdb::array_view<value *> args)
  424. {
  425. gdbpy_enter enter_py;
  426. int i;
  427. struct type *obj_type, *this_type;
  428. struct value *res = NULL;
  429. obj_type = check_typedef (value_type (obj));
  430. this_type = check_typedef (type_object_to_type (m_this_type));
  431. if (obj_type->code () == TYPE_CODE_PTR)
  432. {
  433. struct type *this_ptr = lookup_pointer_type (this_type);
  434. if (!types_equal (obj_type, this_ptr))
  435. obj = value_cast (this_ptr, obj);
  436. }
  437. else if (TYPE_IS_REFERENCE (obj_type))
  438. {
  439. struct type *this_ref
  440. = lookup_reference_type (this_type, obj_type->code ());
  441. if (!types_equal (obj_type, this_ref))
  442. obj = value_cast (this_ref, obj);
  443. }
  444. else
  445. {
  446. if (!types_equal (obj_type, this_type))
  447. obj = value_cast (this_type, obj);
  448. }
  449. gdbpy_ref<> py_value_obj (value_to_value_object (obj));
  450. if (py_value_obj == NULL)
  451. {
  452. gdbpy_print_stack ();
  453. error (_("Error while executing Python code."));
  454. }
  455. gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
  456. if (py_arg_tuple == NULL)
  457. {
  458. gdbpy_print_stack ();
  459. error (_("Error while executing Python code."));
  460. }
  461. /* PyTuple_SET_ITEM steals the reference of the element, hence the
  462. release. */
  463. PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
  464. for (i = 0; i < args.size (); i++)
  465. {
  466. PyObject *py_value_arg = value_to_value_object (args[i]);
  467. if (py_value_arg == NULL)
  468. {
  469. gdbpy_print_stack ();
  470. error (_("Error while executing Python code."));
  471. }
  472. PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
  473. }
  474. gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
  475. py_arg_tuple.get ()));
  476. if (py_result == NULL)
  477. {
  478. gdbpy_print_stack ();
  479. error (_("Error while executing Python code."));
  480. }
  481. if (py_result != Py_None)
  482. {
  483. res = convert_value_from_python (py_result.get ());
  484. if (res == NULL)
  485. {
  486. gdbpy_print_stack ();
  487. error (_("Error while executing Python code."));
  488. }
  489. }
  490. else
  491. {
  492. res = allocate_value (lookup_typename (current_language,
  493. "void", NULL, 0));
  494. }
  495. return res;
  496. }
  497. python_xmethod_worker::python_xmethod_worker (PyObject *py_worker,
  498. PyObject *this_type)
  499. : xmethod_worker (&extension_language_python),
  500. m_py_worker (py_worker), m_this_type (this_type)
  501. {
  502. gdb_assert (m_py_worker != NULL && m_this_type != NULL);
  503. Py_INCREF (py_worker);
  504. Py_INCREF (this_type);
  505. }
  506. int
  507. gdbpy_initialize_xmethods (void)
  508. {
  509. py_match_method_name = PyUnicode_FromString (match_method_name);
  510. if (py_match_method_name == NULL)
  511. return -1;
  512. py_get_arg_types_method_name
  513. = PyUnicode_FromString (get_arg_types_method_name);
  514. if (py_get_arg_types_method_name == NULL)
  515. return -1;
  516. return 1;
  517. }