py-block.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* Python interface to blocks.
  2. Copyright (C) 2008-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 "block.h"
  16. #include "dictionary.h"
  17. #include "symtab.h"
  18. #include "python-internal.h"
  19. #include "objfiles.h"
  20. struct block_object {
  21. PyObject_HEAD
  22. /* The GDB block structure that represents a frame's code block. */
  23. const struct block *block;
  24. /* The backing object file. There is no direct relationship in GDB
  25. between a block and an object file. When a block is created also
  26. store a pointer to the object file for later use. */
  27. struct objfile *objfile;
  28. /* Keep track of all blocks with a doubly-linked list. Needed for
  29. block invalidation if the source object file has been freed. */
  30. block_object *prev;
  31. block_object *next;
  32. };
  33. struct block_syms_iterator_object {
  34. PyObject_HEAD
  35. /* The block. */
  36. const struct block *block;
  37. /* The iterator for that block. */
  38. struct block_iterator iter;
  39. /* Has the iterator been initialized flag. */
  40. int initialized_p;
  41. /* Pointer back to the original source block object. Needed to
  42. check if the block is still valid, and has not been invalidated
  43. when an object file has been freed. */
  44. block_object *source;
  45. };
  46. /* Require a valid block. All access to block_object->block should be
  47. gated by this call. */
  48. #define BLPY_REQUIRE_VALID(block_obj, block) \
  49. do { \
  50. block = block_object_to_block (block_obj); \
  51. if (block == NULL) \
  52. { \
  53. PyErr_SetString (PyExc_RuntimeError, \
  54. _("Block is invalid.")); \
  55. return NULL; \
  56. } \
  57. } while (0)
  58. /* Require a valid block. This macro is called during block iterator
  59. creation, and at each next call. */
  60. #define BLPY_ITER_REQUIRE_VALID(block_obj) \
  61. do { \
  62. if (block_obj->block == NULL) \
  63. { \
  64. PyErr_SetString (PyExc_RuntimeError, \
  65. _("Source block for iterator is invalid.")); \
  66. return NULL; \
  67. } \
  68. } while (0)
  69. extern PyTypeObject block_syms_iterator_object_type
  70. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("block_syms_iterator_object");
  71. static const struct objfile_data *blpy_objfile_data_key;
  72. static PyObject *
  73. blpy_iter (PyObject *self)
  74. {
  75. block_syms_iterator_object *block_iter_obj;
  76. const struct block *block = NULL;
  77. BLPY_REQUIRE_VALID (self, block);
  78. block_iter_obj = PyObject_New (block_syms_iterator_object,
  79. &block_syms_iterator_object_type);
  80. if (block_iter_obj == NULL)
  81. return NULL;
  82. block_iter_obj->block = block;
  83. block_iter_obj->initialized_p = 0;
  84. Py_INCREF (self);
  85. block_iter_obj->source = (block_object *) self;
  86. return (PyObject *) block_iter_obj;
  87. }
  88. static PyObject *
  89. blpy_get_start (PyObject *self, void *closure)
  90. {
  91. const struct block *block = NULL;
  92. BLPY_REQUIRE_VALID (self, block);
  93. return gdb_py_object_from_ulongest (BLOCK_START (block)).release ();
  94. }
  95. static PyObject *
  96. blpy_get_end (PyObject *self, void *closure)
  97. {
  98. const struct block *block = NULL;
  99. BLPY_REQUIRE_VALID (self, block);
  100. return gdb_py_object_from_ulongest (BLOCK_END (block)).release ();
  101. }
  102. static PyObject *
  103. blpy_get_function (PyObject *self, void *closure)
  104. {
  105. struct symbol *sym;
  106. const struct block *block;
  107. BLPY_REQUIRE_VALID (self, block);
  108. sym = BLOCK_FUNCTION (block);
  109. if (sym)
  110. return symbol_to_symbol_object (sym);
  111. Py_RETURN_NONE;
  112. }
  113. static PyObject *
  114. blpy_get_superblock (PyObject *self, void *closure)
  115. {
  116. const struct block *block;
  117. const struct block *super_block;
  118. block_object *self_obj = (block_object *) self;
  119. BLPY_REQUIRE_VALID (self, block);
  120. super_block = BLOCK_SUPERBLOCK (block);
  121. if (super_block)
  122. return block_to_block_object (super_block, self_obj->objfile);
  123. Py_RETURN_NONE;
  124. }
  125. /* Return the global block associated to this block. */
  126. static PyObject *
  127. blpy_get_global_block (PyObject *self, void *closure)
  128. {
  129. const struct block *block;
  130. const struct block *global_block;
  131. block_object *self_obj = (block_object *) self;
  132. BLPY_REQUIRE_VALID (self, block);
  133. global_block = block_global_block (block);
  134. return block_to_block_object (global_block,
  135. self_obj->objfile);
  136. }
  137. /* Return the static block associated to this block. Return None
  138. if we cannot get the static block (this is the global block). */
  139. static PyObject *
  140. blpy_get_static_block (PyObject *self, void *closure)
  141. {
  142. const struct block *block;
  143. const struct block *static_block;
  144. block_object *self_obj = (block_object *) self;
  145. BLPY_REQUIRE_VALID (self, block);
  146. if (BLOCK_SUPERBLOCK (block) == NULL)
  147. Py_RETURN_NONE;
  148. static_block = block_static_block (block);
  149. return block_to_block_object (static_block, self_obj->objfile);
  150. }
  151. /* Implementation of gdb.Block.is_global (self) -> Boolean.
  152. Returns True if this block object is a global block. */
  153. static PyObject *
  154. blpy_is_global (PyObject *self, void *closure)
  155. {
  156. const struct block *block;
  157. BLPY_REQUIRE_VALID (self, block);
  158. if (BLOCK_SUPERBLOCK (block))
  159. Py_RETURN_FALSE;
  160. Py_RETURN_TRUE;
  161. }
  162. /* Implementation of gdb.Block.is_static (self) -> Boolean.
  163. Returns True if this block object is a static block. */
  164. static PyObject *
  165. blpy_is_static (PyObject *self, void *closure)
  166. {
  167. const struct block *block;
  168. BLPY_REQUIRE_VALID (self, block);
  169. if (BLOCK_SUPERBLOCK (block) != NULL
  170. && BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
  171. Py_RETURN_TRUE;
  172. Py_RETURN_FALSE;
  173. }
  174. /* Given a string, returns the gdb.Symbol representing that symbol in this
  175. block. If such a symbol does not exist, returns NULL with a Python
  176. exception. */
  177. static PyObject *
  178. blpy_getitem (PyObject *self, PyObject *key)
  179. {
  180. const struct block *block;
  181. BLPY_REQUIRE_VALID (self, block);
  182. gdb::unique_xmalloc_ptr<char> name = python_string_to_host_string (key);
  183. if (name == nullptr)
  184. return nullptr;
  185. lookup_name_info lookup_name (name.get(), symbol_name_match_type::FULL);
  186. /* We use ALL_BLOCK_SYMBOLS_WITH_NAME instead of block_lookup_symbol so
  187. that we can look up symbols irrespective of the domain, matching the
  188. iterator. It would be confusing if the iterator returns symbols you
  189. can't find via getitem. */
  190. struct block_iterator iter;
  191. struct symbol *sym = nullptr;
  192. ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
  193. {
  194. /* Just stop at the first match */
  195. break;
  196. }
  197. if (sym == nullptr)
  198. {
  199. PyErr_SetObject (PyExc_KeyError, key);
  200. return nullptr;
  201. }
  202. return symbol_to_symbol_object (sym);
  203. }
  204. static void
  205. blpy_dealloc (PyObject *obj)
  206. {
  207. block_object *block = (block_object *) obj;
  208. if (block->prev)
  209. block->prev->next = block->next;
  210. else if (block->objfile)
  211. {
  212. set_objfile_data (block->objfile, blpy_objfile_data_key,
  213. block->next);
  214. }
  215. if (block->next)
  216. block->next->prev = block->prev;
  217. block->block = NULL;
  218. Py_TYPE (obj)->tp_free (obj);
  219. }
  220. /* Given a block, and a block_object that has previously been
  221. allocated and initialized, populate the block_object with the
  222. struct block data. Also, register the block_object life-cycle
  223. with the life-cycle of the object file associated with this
  224. block, if needed. */
  225. static void
  226. set_block (block_object *obj, const struct block *block,
  227. struct objfile *objfile)
  228. {
  229. obj->block = block;
  230. obj->prev = NULL;
  231. if (objfile)
  232. {
  233. obj->objfile = objfile;
  234. obj->next = ((block_object *)
  235. objfile_data (objfile, blpy_objfile_data_key));
  236. if (obj->next)
  237. obj->next->prev = obj;
  238. set_objfile_data (objfile, blpy_objfile_data_key, obj);
  239. }
  240. else
  241. obj->next = NULL;
  242. }
  243. /* Create a new block object (gdb.Block) that encapsulates the struct
  244. block object from GDB. */
  245. PyObject *
  246. block_to_block_object (const struct block *block, struct objfile *objfile)
  247. {
  248. block_object *block_obj;
  249. block_obj = PyObject_New (block_object, &block_object_type);
  250. if (block_obj)
  251. set_block (block_obj, block, objfile);
  252. return (PyObject *) block_obj;
  253. }
  254. /* Return struct block reference that is wrapped by this object. */
  255. const struct block *
  256. block_object_to_block (PyObject *obj)
  257. {
  258. if (! PyObject_TypeCheck (obj, &block_object_type))
  259. return NULL;
  260. return ((block_object *) obj)->block;
  261. }
  262. /* Return a reference to the block iterator. */
  263. static PyObject *
  264. blpy_block_syms_iter (PyObject *self)
  265. {
  266. block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
  267. BLPY_ITER_REQUIRE_VALID (iter_obj->source);
  268. Py_INCREF (self);
  269. return self;
  270. }
  271. /* Return the next symbol in the iteration through the block's
  272. dictionary. */
  273. static PyObject *
  274. blpy_block_syms_iternext (PyObject *self)
  275. {
  276. block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
  277. struct symbol *sym;
  278. BLPY_ITER_REQUIRE_VALID (iter_obj->source);
  279. if (!iter_obj->initialized_p)
  280. {
  281. sym = block_iterator_first (iter_obj->block, &(iter_obj->iter));
  282. iter_obj->initialized_p = 1;
  283. }
  284. else
  285. sym = block_iterator_next (&(iter_obj->iter));
  286. if (sym == NULL)
  287. {
  288. PyErr_SetString (PyExc_StopIteration, _("Symbol is null."));
  289. return NULL;
  290. }
  291. return symbol_to_symbol_object (sym);
  292. }
  293. static void
  294. blpy_block_syms_dealloc (PyObject *obj)
  295. {
  296. block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj;
  297. Py_XDECREF (iter_obj->source);
  298. Py_TYPE (obj)->tp_free (obj);
  299. }
  300. /* Implementation of gdb.Block.is_valid (self) -> Boolean.
  301. Returns True if this block object still exists in GDB. */
  302. static PyObject *
  303. blpy_is_valid (PyObject *self, PyObject *args)
  304. {
  305. const struct block *block;
  306. block = block_object_to_block (self);
  307. if (block == NULL)
  308. Py_RETURN_FALSE;
  309. Py_RETURN_TRUE;
  310. }
  311. /* Implementation of gdb.BlockIterator.is_valid (self) -> Boolean.
  312. Returns True if this block iterator object still exists in GDB */
  313. static PyObject *
  314. blpy_iter_is_valid (PyObject *self, PyObject *args)
  315. {
  316. block_syms_iterator_object *iter_obj =
  317. (block_syms_iterator_object *) self;
  318. if (iter_obj->source->block == NULL)
  319. Py_RETURN_FALSE;
  320. Py_RETURN_TRUE;
  321. }
  322. /* This function is called when an objfile is about to be freed.
  323. Invalidate the block as further actions on the block would result
  324. in bad data. All access to obj->symbol should be gated by
  325. BLPY_REQUIRE_VALID which will raise an exception on invalid
  326. blocks. */
  327. static void
  328. del_objfile_blocks (struct objfile *objfile, void *datum)
  329. {
  330. block_object *obj = (block_object *) datum;
  331. while (obj)
  332. {
  333. block_object *next = obj->next;
  334. obj->block = NULL;
  335. obj->objfile = NULL;
  336. obj->next = NULL;
  337. obj->prev = NULL;
  338. obj = next;
  339. }
  340. }
  341. void _initialize_py_block ();
  342. void
  343. _initialize_py_block ()
  344. {
  345. /* Register an objfile "free" callback so we can properly
  346. invalidate blocks when an object file is about to be
  347. deleted. */
  348. blpy_objfile_data_key
  349. = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
  350. }
  351. int
  352. gdbpy_initialize_blocks (void)
  353. {
  354. block_object_type.tp_new = PyType_GenericNew;
  355. if (PyType_Ready (&block_object_type) < 0)
  356. return -1;
  357. block_syms_iterator_object_type.tp_new = PyType_GenericNew;
  358. if (PyType_Ready (&block_syms_iterator_object_type) < 0)
  359. return -1;
  360. if (gdb_pymodule_addobject (gdb_module, "Block",
  361. (PyObject *) &block_object_type) < 0)
  362. return -1;
  363. return gdb_pymodule_addobject (gdb_module, "BlockIterator",
  364. (PyObject *) &block_syms_iterator_object_type);
  365. }
  366. static PyMethodDef block_object_methods[] = {
  367. { "is_valid", blpy_is_valid, METH_NOARGS,
  368. "is_valid () -> Boolean.\n\
  369. Return true if this block is valid, false if not." },
  370. {NULL} /* Sentinel */
  371. };
  372. static gdb_PyGetSetDef block_object_getset[] = {
  373. { "start", blpy_get_start, NULL, "Start address of the block.", NULL },
  374. { "end", blpy_get_end, NULL, "End address of the block.", NULL },
  375. { "function", blpy_get_function, NULL,
  376. "Symbol that names the block, or None.", NULL },
  377. { "superblock", blpy_get_superblock, NULL,
  378. "Block containing the block, or None.", NULL },
  379. { "global_block", blpy_get_global_block, NULL,
  380. "Block containing the global block.", NULL },
  381. { "static_block", blpy_get_static_block, NULL,
  382. "Block containing the static block.", NULL },
  383. { "is_static", blpy_is_static, NULL,
  384. "Whether this block is a static block.", NULL },
  385. { "is_global", blpy_is_global, NULL,
  386. "Whether this block is a global block.", NULL },
  387. { NULL } /* Sentinel */
  388. };
  389. static PyMappingMethods block_object_as_mapping = {
  390. NULL,
  391. blpy_getitem,
  392. NULL
  393. };
  394. PyTypeObject block_object_type = {
  395. PyVarObject_HEAD_INIT (NULL, 0)
  396. "gdb.Block", /*tp_name*/
  397. sizeof (block_object), /*tp_basicsize*/
  398. 0, /*tp_itemsize*/
  399. blpy_dealloc, /*tp_dealloc*/
  400. 0, /*tp_print*/
  401. 0, /*tp_getattr*/
  402. 0, /*tp_setattr*/
  403. 0, /*tp_compare*/
  404. 0, /*tp_repr*/
  405. 0, /*tp_as_number*/
  406. 0, /*tp_as_sequence*/
  407. &block_object_as_mapping, /*tp_as_mapping*/
  408. 0, /*tp_hash */
  409. 0, /*tp_call*/
  410. 0, /*tp_str*/
  411. 0, /*tp_getattro*/
  412. 0, /*tp_setattro*/
  413. 0, /*tp_as_buffer*/
  414. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  415. "GDB block object", /* tp_doc */
  416. 0, /* tp_traverse */
  417. 0, /* tp_clear */
  418. 0, /* tp_richcompare */
  419. 0, /* tp_weaklistoffset */
  420. blpy_iter, /* tp_iter */
  421. 0, /* tp_iternext */
  422. block_object_methods, /* tp_methods */
  423. 0, /* tp_members */
  424. block_object_getset /* tp_getset */
  425. };
  426. static PyMethodDef block_iterator_object_methods[] = {
  427. { "is_valid", blpy_iter_is_valid, METH_NOARGS,
  428. "is_valid () -> Boolean.\n\
  429. Return true if this block iterator is valid, false if not." },
  430. {NULL} /* Sentinel */
  431. };
  432. PyTypeObject block_syms_iterator_object_type = {
  433. PyVarObject_HEAD_INIT (NULL, 0)
  434. "gdb.BlockIterator", /*tp_name*/
  435. sizeof (block_syms_iterator_object), /*tp_basicsize*/
  436. 0, /*tp_itemsize*/
  437. blpy_block_syms_dealloc, /*tp_dealloc*/
  438. 0, /*tp_print*/
  439. 0, /*tp_getattr*/
  440. 0, /*tp_setattr*/
  441. 0, /*tp_compare*/
  442. 0, /*tp_repr*/
  443. 0, /*tp_as_number*/
  444. 0, /*tp_as_sequence*/
  445. 0, /*tp_as_mapping*/
  446. 0, /*tp_hash */
  447. 0, /*tp_call*/
  448. 0, /*tp_str*/
  449. 0, /*tp_getattro*/
  450. 0, /*tp_setattro*/
  451. 0, /*tp_as_buffer*/
  452. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  453. "GDB block syms iterator object", /*tp_doc */
  454. 0, /*tp_traverse */
  455. 0, /*tp_clear */
  456. 0, /*tp_richcompare */
  457. 0, /*tp_weaklistoffset */
  458. blpy_block_syms_iter, /*tp_iter */
  459. blpy_block_syms_iternext, /*tp_iternext */
  460. block_iterator_object_methods /*tp_methods */
  461. };