py-progspace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* Python interface to program spaces.
  2. Copyright (C) 2010-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 "python-internal.h"
  16. #include "charset.h"
  17. #include "progspace.h"
  18. #include "objfiles.h"
  19. #include "language.h"
  20. #include "arch-utils.h"
  21. #include "solib.h"
  22. #include "block.h"
  23. struct pspace_object
  24. {
  25. PyObject_HEAD
  26. /* The corresponding pspace. */
  27. struct program_space *pspace;
  28. /* Dictionary holding user-added attributes.
  29. This is the __dict__ attribute of the object. */
  30. PyObject *dict;
  31. /* The pretty-printer list of functions. */
  32. PyObject *printers;
  33. /* The frame filter list of functions. */
  34. PyObject *frame_filters;
  35. /* The frame unwinder list. */
  36. PyObject *frame_unwinders;
  37. /* The type-printer list. */
  38. PyObject *type_printers;
  39. /* The debug method list. */
  40. PyObject *xmethods;
  41. };
  42. extern PyTypeObject pspace_object_type
  43. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
  44. static const struct program_space_data *pspy_pspace_data_key;
  45. /* Require that PSPACE_OBJ be a valid program space ID. */
  46. #define PSPY_REQUIRE_VALID(pspace_obj) \
  47. do { \
  48. if (pspace_obj->pspace == nullptr) \
  49. { \
  50. PyErr_SetString (PyExc_RuntimeError, \
  51. _("Program space no longer exists.")); \
  52. return NULL; \
  53. } \
  54. } while (0)
  55. /* An Objfile method which returns the objfile's file name, or None. */
  56. static PyObject *
  57. pspy_get_filename (PyObject *self, void *closure)
  58. {
  59. pspace_object *obj = (pspace_object *) self;
  60. if (obj->pspace)
  61. {
  62. struct objfile *objfile = obj->pspace->symfile_object_file;
  63. if (objfile)
  64. return (host_string_to_python_string (objfile_name (objfile))
  65. .release ());
  66. }
  67. Py_RETURN_NONE;
  68. }
  69. static void
  70. pspy_dealloc (PyObject *self)
  71. {
  72. pspace_object *ps_self = (pspace_object *) self;
  73. Py_XDECREF (ps_self->dict);
  74. Py_XDECREF (ps_self->printers);
  75. Py_XDECREF (ps_self->frame_filters);
  76. Py_XDECREF (ps_self->frame_unwinders);
  77. Py_XDECREF (ps_self->type_printers);
  78. Py_XDECREF (ps_self->xmethods);
  79. Py_TYPE (self)->tp_free (self);
  80. }
  81. /* Initialize a pspace_object.
  82. The result is a boolean indicating success. */
  83. static int
  84. pspy_initialize (pspace_object *self)
  85. {
  86. self->pspace = NULL;
  87. self->dict = PyDict_New ();
  88. if (self->dict == NULL)
  89. return 0;
  90. self->printers = PyList_New (0);
  91. if (self->printers == NULL)
  92. return 0;
  93. self->frame_filters = PyDict_New ();
  94. if (self->frame_filters == NULL)
  95. return 0;
  96. self->frame_unwinders = PyList_New (0);
  97. if (self->frame_unwinders == NULL)
  98. return 0;
  99. self->type_printers = PyList_New (0);
  100. if (self->type_printers == NULL)
  101. return 0;
  102. self->xmethods = PyList_New (0);
  103. if (self->xmethods == NULL)
  104. return 0;
  105. return 1;
  106. }
  107. static PyObject *
  108. pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
  109. {
  110. gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0));
  111. if (self != NULL)
  112. {
  113. if (!pspy_initialize (self.get ()))
  114. return NULL;
  115. }
  116. return (PyObject *) self.release ();
  117. }
  118. PyObject *
  119. pspy_get_printers (PyObject *o, void *ignore)
  120. {
  121. pspace_object *self = (pspace_object *) o;
  122. Py_INCREF (self->printers);
  123. return self->printers;
  124. }
  125. static int
  126. pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
  127. {
  128. pspace_object *self = (pspace_object *) o;
  129. if (! value)
  130. {
  131. PyErr_SetString (PyExc_TypeError,
  132. "cannot delete the pretty_printers attribute");
  133. return -1;
  134. }
  135. if (! PyList_Check (value))
  136. {
  137. PyErr_SetString (PyExc_TypeError,
  138. "the pretty_printers attribute must be a list");
  139. return -1;
  140. }
  141. /* Take care in case the LHS and RHS are related somehow. */
  142. gdbpy_ref<> tmp (self->printers);
  143. Py_INCREF (value);
  144. self->printers = value;
  145. return 0;
  146. }
  147. /* Return the Python dictionary attribute containing frame filters for
  148. this program space. */
  149. PyObject *
  150. pspy_get_frame_filters (PyObject *o, void *ignore)
  151. {
  152. pspace_object *self = (pspace_object *) o;
  153. Py_INCREF (self->frame_filters);
  154. return self->frame_filters;
  155. }
  156. /* Set this object file's frame filters dictionary to FILTERS. */
  157. static int
  158. pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
  159. {
  160. pspace_object *self = (pspace_object *) o;
  161. if (! frame)
  162. {
  163. PyErr_SetString (PyExc_TypeError,
  164. "cannot delete the frame filter attribute");
  165. return -1;
  166. }
  167. if (! PyDict_Check (frame))
  168. {
  169. PyErr_SetString (PyExc_TypeError,
  170. "the frame filter attribute must be a dictionary");
  171. return -1;
  172. }
  173. /* Take care in case the LHS and RHS are related somehow. */
  174. gdbpy_ref<> tmp (self->frame_filters);
  175. Py_INCREF (frame);
  176. self->frame_filters = frame;
  177. return 0;
  178. }
  179. /* Return the list of the frame unwinders for this program space. */
  180. PyObject *
  181. pspy_get_frame_unwinders (PyObject *o, void *ignore)
  182. {
  183. pspace_object *self = (pspace_object *) o;
  184. Py_INCREF (self->frame_unwinders);
  185. return self->frame_unwinders;
  186. }
  187. /* Set this program space's list of the unwinders to UNWINDERS. */
  188. static int
  189. pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
  190. {
  191. pspace_object *self = (pspace_object *) o;
  192. if (!unwinders)
  193. {
  194. PyErr_SetString (PyExc_TypeError,
  195. "cannot delete the frame unwinders list");
  196. return -1;
  197. }
  198. if (!PyList_Check (unwinders))
  199. {
  200. PyErr_SetString (PyExc_TypeError,
  201. "the frame unwinders attribute must be a list");
  202. return -1;
  203. }
  204. /* Take care in case the LHS and RHS are related somehow. */
  205. gdbpy_ref<> tmp (self->frame_unwinders);
  206. Py_INCREF (unwinders);
  207. self->frame_unwinders = unwinders;
  208. return 0;
  209. }
  210. /* Get the 'type_printers' attribute. */
  211. static PyObject *
  212. pspy_get_type_printers (PyObject *o, void *ignore)
  213. {
  214. pspace_object *self = (pspace_object *) o;
  215. Py_INCREF (self->type_printers);
  216. return self->type_printers;
  217. }
  218. /* Get the 'xmethods' attribute. */
  219. PyObject *
  220. pspy_get_xmethods (PyObject *o, void *ignore)
  221. {
  222. pspace_object *self = (pspace_object *) o;
  223. Py_INCREF (self->xmethods);
  224. return self->xmethods;
  225. }
  226. /* Set the 'type_printers' attribute. */
  227. static int
  228. pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
  229. {
  230. pspace_object *self = (pspace_object *) o;
  231. if (! value)
  232. {
  233. PyErr_SetString (PyExc_TypeError,
  234. "cannot delete the type_printers attribute");
  235. return -1;
  236. }
  237. if (! PyList_Check (value))
  238. {
  239. PyErr_SetString (PyExc_TypeError,
  240. "the type_printers attribute must be a list");
  241. return -1;
  242. }
  243. /* Take care in case the LHS and RHS are related somehow. */
  244. gdbpy_ref<> tmp (self->type_printers);
  245. Py_INCREF (value);
  246. self->type_printers = value;
  247. return 0;
  248. }
  249. /* Implement the objfiles method. */
  250. static PyObject *
  251. pspy_get_objfiles (PyObject *self_, PyObject *args)
  252. {
  253. pspace_object *self = (pspace_object *) self_;
  254. PSPY_REQUIRE_VALID (self);
  255. gdbpy_ref<> list (PyList_New (0));
  256. if (list == NULL)
  257. return NULL;
  258. if (self->pspace != NULL)
  259. {
  260. for (objfile *objf : self->pspace->objfiles ())
  261. {
  262. gdbpy_ref<> item = objfile_to_objfile_object (objf);
  263. if (item == nullptr
  264. || PyList_Append (list.get (), item.get ()) == -1)
  265. return NULL;
  266. }
  267. }
  268. return list.release ();
  269. }
  270. /* Implementation of solib_name (Long) -> String.
  271. Returns the name of the shared library holding a given address, or None. */
  272. static PyObject *
  273. pspy_solib_name (PyObject *o, PyObject *args)
  274. {
  275. char *soname;
  276. gdb_py_ulongest pc;
  277. pspace_object *self = (pspace_object *) o;
  278. PSPY_REQUIRE_VALID (self);
  279. if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
  280. return NULL;
  281. soname = solib_name_from_address (self->pspace, pc);
  282. if (soname == nullptr)
  283. Py_RETURN_NONE;
  284. return host_string_to_python_string (soname).release ();
  285. }
  286. /* Return the innermost lexical block containing the specified pc value,
  287. or 0 if there is none. */
  288. static PyObject *
  289. pspy_block_for_pc (PyObject *o, PyObject *args)
  290. {
  291. pspace_object *self = (pspace_object *) o;
  292. gdb_py_ulongest pc;
  293. const struct block *block = NULL;
  294. struct compunit_symtab *cust = NULL;
  295. PSPY_REQUIRE_VALID (self);
  296. if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
  297. return NULL;
  298. try
  299. {
  300. scoped_restore_current_program_space saver;
  301. set_current_program_space (self->pspace);
  302. cust = find_pc_compunit_symtab (pc);
  303. if (cust != NULL && cust->objfile () != NULL)
  304. block = block_for_pc (pc);
  305. }
  306. catch (const gdb_exception &except)
  307. {
  308. GDB_PY_HANDLE_EXCEPTION (except);
  309. }
  310. if (cust == NULL || cust->objfile () == NULL)
  311. Py_RETURN_NONE;
  312. if (block)
  313. return block_to_block_object (block, cust->objfile ());
  314. Py_RETURN_NONE;
  315. }
  316. /* Implementation of the find_pc_line function.
  317. Returns the gdb.Symtab_and_line object corresponding to a PC value. */
  318. static PyObject *
  319. pspy_find_pc_line (PyObject *o, PyObject *args)
  320. {
  321. gdb_py_ulongest pc_llu;
  322. PyObject *result = NULL; /* init for gcc -Wall */
  323. pspace_object *self = (pspace_object *) o;
  324. PSPY_REQUIRE_VALID (self);
  325. if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
  326. return NULL;
  327. try
  328. {
  329. struct symtab_and_line sal;
  330. CORE_ADDR pc;
  331. scoped_restore_current_program_space saver;
  332. set_current_program_space (self->pspace);
  333. pc = (CORE_ADDR) pc_llu;
  334. sal = find_pc_line (pc, 0);
  335. result = symtab_and_line_to_sal_object (sal);
  336. }
  337. catch (const gdb_exception &except)
  338. {
  339. GDB_PY_HANDLE_EXCEPTION (except);
  340. }
  341. return result;
  342. }
  343. /* Implementation of is_valid (self) -> Boolean.
  344. Returns True if this program space still exists in GDB. */
  345. static PyObject *
  346. pspy_is_valid (PyObject *o, PyObject *args)
  347. {
  348. pspace_object *self = (pspace_object *) o;
  349. if (self->pspace == NULL)
  350. Py_RETURN_FALSE;
  351. Py_RETURN_TRUE;
  352. }
  353. /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
  354. static void
  355. py_free_pspace (struct program_space *pspace, void *datum)
  356. {
  357. /* This is a fiction, but we're in a nasty spot: The pspace is in the
  358. process of being deleted, we can't rely on anything in it. Plus
  359. this is one time when the current program space and current inferior
  360. are not in sync: All inferiors that use PSPACE may no longer exist.
  361. We don't need to do much here, and since "there is always an inferior"
  362. using target_gdbarch suffices.
  363. Note: We cannot call get_current_arch because it may try to access
  364. the target, which may involve accessing data in the pspace currently
  365. being deleted. */
  366. struct gdbarch *arch = target_gdbarch ();
  367. gdbpy_enter enter_py (arch);
  368. gdbpy_ref<pspace_object> object ((pspace_object *) datum);
  369. object->pspace = NULL;
  370. }
  371. /* Return a new reference to the Python object of type Pspace
  372. representing PSPACE. If the object has already been created,
  373. return it. Otherwise, create it. Return NULL and set the Python
  374. error on failure. */
  375. gdbpy_ref<>
  376. pspace_to_pspace_object (struct program_space *pspace)
  377. {
  378. PyObject *result
  379. ((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
  380. if (result == NULL)
  381. {
  382. gdbpy_ref<pspace_object> object
  383. ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
  384. if (object == NULL)
  385. return NULL;
  386. if (!pspy_initialize (object.get ()))
  387. return NULL;
  388. object->pspace = pspace;
  389. set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
  390. result = (PyObject *) object.release ();
  391. }
  392. return gdbpy_ref<>::new_reference (result);
  393. }
  394. /* See python-internal.h. */
  395. struct program_space *
  396. progspace_object_to_program_space (PyObject *obj)
  397. {
  398. gdb_assert (gdbpy_is_progspace (obj));
  399. return ((pspace_object *) obj)->pspace;
  400. }
  401. /* See python-internal.h. */
  402. bool
  403. gdbpy_is_progspace (PyObject *obj)
  404. {
  405. return PyObject_TypeCheck (obj, &pspace_object_type);
  406. }
  407. void _initialize_py_progspace ();
  408. void
  409. _initialize_py_progspace ()
  410. {
  411. pspy_pspace_data_key
  412. = register_program_space_data_with_cleanup (NULL, py_free_pspace);
  413. }
  414. int
  415. gdbpy_initialize_pspace (void)
  416. {
  417. if (PyType_Ready (&pspace_object_type) < 0)
  418. return -1;
  419. return gdb_pymodule_addobject (gdb_module, "Progspace",
  420. (PyObject *) &pspace_object_type);
  421. }
  422. static gdb_PyGetSetDef pspace_getset[] =
  423. {
  424. { "__dict__", gdb_py_generic_dict, NULL,
  425. "The __dict__ for this progspace.", &pspace_object_type },
  426. { "filename", pspy_get_filename, NULL,
  427. "The progspace's main filename, or None.", NULL },
  428. { "pretty_printers", pspy_get_printers, pspy_set_printers,
  429. "Pretty printers.", NULL },
  430. { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
  431. "Frame filters.", NULL },
  432. { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
  433. "Frame unwinders.", NULL },
  434. { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
  435. "Type printers.", NULL },
  436. { "xmethods", pspy_get_xmethods, NULL,
  437. "Debug methods.", NULL },
  438. { NULL }
  439. };
  440. static PyMethodDef progspace_object_methods[] =
  441. {
  442. { "objfiles", pspy_get_objfiles, METH_NOARGS,
  443. "Return a sequence of objfiles associated to this program space." },
  444. { "solib_name", pspy_solib_name, METH_VARARGS,
  445. "solib_name (Long) -> String.\n\
  446. Return the name of the shared library holding a given address, or None." },
  447. { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
  448. "Return the block containing the given pc value, or None." },
  449. { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
  450. "find_pc_line (pc) -> Symtab_and_line.\n\
  451. Return the gdb.Symtab_and_line object corresponding to the pc value." },
  452. { "is_valid", pspy_is_valid, METH_NOARGS,
  453. "is_valid () -> Boolean.\n\
  454. Return true if this program space is valid, false if not." },
  455. { NULL }
  456. };
  457. PyTypeObject pspace_object_type =
  458. {
  459. PyVarObject_HEAD_INIT (NULL, 0)
  460. "gdb.Progspace", /*tp_name*/
  461. sizeof (pspace_object), /*tp_basicsize*/
  462. 0, /*tp_itemsize*/
  463. pspy_dealloc, /*tp_dealloc*/
  464. 0, /*tp_print*/
  465. 0, /*tp_getattr*/
  466. 0, /*tp_setattr*/
  467. 0, /*tp_compare*/
  468. 0, /*tp_repr*/
  469. 0, /*tp_as_number*/
  470. 0, /*tp_as_sequence*/
  471. 0, /*tp_as_mapping*/
  472. 0, /*tp_hash */
  473. 0, /*tp_call*/
  474. 0, /*tp_str*/
  475. 0, /*tp_getattro*/
  476. 0, /*tp_setattro*/
  477. 0, /*tp_as_buffer*/
  478. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  479. "GDB progspace object", /* tp_doc */
  480. 0, /* tp_traverse */
  481. 0, /* tp_clear */
  482. 0, /* tp_richcompare */
  483. 0, /* tp_weaklistoffset */
  484. 0, /* tp_iter */
  485. 0, /* tp_iternext */
  486. progspace_object_methods, /* tp_methods */
  487. 0, /* tp_members */
  488. pspace_getset, /* tp_getset */
  489. 0, /* tp_base */
  490. 0, /* tp_dict */
  491. 0, /* tp_descr_get */
  492. 0, /* tp_descr_set */
  493. offsetof (pspace_object, dict), /* tp_dictoffset */
  494. 0, /* tp_init */
  495. 0, /* tp_alloc */
  496. pspy_new, /* tp_new */
  497. };