gdb/python/py-block.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Python interface to blocks.

  2.    Copyright (C) 2008-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 "block.h"
  16. #include "dictionary.h"
  17. #include "symtab.h"
  18. #include "python-internal.h"
  19. #include "objfiles.h"
  20. #include "symtab.h"

  21. typedef struct blpy_block_object {
  22.   PyObject_HEAD
  23.   /* The GDB block structure that represents a frame's code block.  */
  24.   const struct block *block;
  25.   /* The backing object file.  There is no direct relationship in GDB
  26.      between a block and an object file.  When a block is created also
  27.      store a pointer to the object file for later use.  */
  28.   struct objfile *objfile;
  29.   /* Keep track of all blocks with a doubly-linked list.  Needed for
  30.      block invalidation if the source object file has been freed.  */
  31.   struct blpy_block_object *prev;
  32.   struct blpy_block_object *next;
  33. } block_object;

  34. typedef struct {
  35.   PyObject_HEAD
  36.   /* The block.  */
  37.   const struct block *block;
  38.   /* The iterator for that block.  */
  39.   struct block_iterator iter;
  40.   /* Has the iterator been initialized flag.  */
  41.   int initialized_p;
  42.   /* Pointer back to the original source block object.  Needed to
  43.      check if the block is still valid, and has not been invalidated
  44.      when an object file has been freed.  */
  45.   struct blpy_block_object *source;
  46. } block_syms_iterator_object;

  47. /* Require a valid block.  All access to block_object->block should be
  48.    gated by this call.  */
  49. #define BLPY_REQUIRE_VALID(block_obj, block)                \
  50.   do {                                                        \
  51.     block = block_object_to_block (block_obj);                \
  52.     if (block == NULL)                                        \
  53.       {                                                        \
  54.         PyErr_SetString (PyExc_RuntimeError,                \
  55.                          _("Block is invalid."));        \
  56.         return NULL;                                        \
  57.       }                                                        \
  58.   } while (0)

  59. /* Require a valid block.  This macro is called during block iterator
  60.    creation, and at each next call.  */
  61. #define BLPY_ITER_REQUIRE_VALID(block_obj)                                \
  62.   do {                                                                        \
  63.     if (block_obj->block == NULL)                                        \
  64.       {                                                                        \
  65.         PyErr_SetString (PyExc_RuntimeError,                                \
  66.                          _("Source block for iterator is invalid."));        \
  67.         return NULL;                                                        \
  68.       }                                                                        \
  69.   } while (0)

  70. static PyTypeObject block_syms_iterator_object_type
  71.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("block_syms_iterator_object");
  72. static const struct objfile_data *blpy_objfile_data_key;

  73. static PyObject *
  74. blpy_iter (PyObject *self)
  75. {
  76.   block_syms_iterator_object *block_iter_obj;
  77.   const struct block *block = NULL;

  78.   BLPY_REQUIRE_VALID (self, block);

  79.   block_iter_obj = PyObject_New (block_syms_iterator_object,
  80.                                  &block_syms_iterator_object_type);
  81.   if (block_iter_obj == NULL)
  82.       return NULL;

  83.   block_iter_obj->block = block;
  84.   block_iter_obj->initialized_p = 0;
  85.   Py_INCREF (self);
  86.   block_iter_obj->source = (block_object *) self;

  87.   return (PyObject *) block_iter_obj;
  88. }

  89. static PyObject *
  90. blpy_get_start (PyObject *self, void *closure)
  91. {
  92.   const struct block *block = NULL;

  93.   BLPY_REQUIRE_VALID (self, block);

  94.   return gdb_py_object_from_ulongest (BLOCK_START (block));
  95. }

  96. static PyObject *
  97. blpy_get_end (PyObject *self, void *closure)
  98. {
  99.   const struct block *block = NULL;

  100.   BLPY_REQUIRE_VALID (self, block);

  101.   return gdb_py_object_from_ulongest (BLOCK_END (block));
  102. }

  103. static PyObject *
  104. blpy_get_function (PyObject *self, void *closure)
  105. {
  106.   struct symbol *sym;
  107.   const struct block *block;

  108.   BLPY_REQUIRE_VALID (self, block);

  109.   sym = BLOCK_FUNCTION (block);
  110.   if (sym)
  111.     return symbol_to_symbol_object (sym);

  112.   Py_RETURN_NONE;
  113. }

  114. static PyObject *
  115. blpy_get_superblock (PyObject *self, void *closure)
  116. {
  117.   const struct block *block;
  118.   const struct block *super_block;
  119.   block_object *self_obj  = (block_object *) self;

  120.   BLPY_REQUIRE_VALID (self, block);

  121.   super_block = BLOCK_SUPERBLOCK (block);
  122.   if (super_block)
  123.     return block_to_block_object (super_block, self_obj->objfile);

  124.   Py_RETURN_NONE;
  125. }

  126. /* Return the global block associated to this block.  */

  127. static PyObject *
  128. blpy_get_global_block (PyObject *self, void *closure)
  129. {
  130.   const struct block *block;
  131.   const struct block *global_block;
  132.   block_object *self_obj  = (block_object *) self;

  133.   BLPY_REQUIRE_VALID (self, block);

  134.   global_block = block_global_block (block);

  135.   return block_to_block_object (global_block,
  136.                                 self_obj->objfile);

  137. }

  138. /* Return the static block associated to this block.  Return None
  139.    if we cannot get the static block (this is the global block).  */

  140. static PyObject *
  141. blpy_get_static_block (PyObject *self, void *closure)
  142. {
  143.   const struct block *block;
  144.   const struct block *static_block;
  145.   block_object *self_obj  = (block_object *) self;

  146.   BLPY_REQUIRE_VALID (self, block);

  147.   if (BLOCK_SUPERBLOCK (block) == NULL)
  148.     Py_RETURN_NONE;

  149.   static_block = block_static_block (block);

  150.   return block_to_block_object (static_block, self_obj->objfile);
  151. }

  152. /* Implementation of gdb.Block.is_global (self) -> Boolean.
  153.    Returns True if this block object is a global block.  */

  154. static PyObject *
  155. blpy_is_global (PyObject *self, void *closure)
  156. {
  157.   const struct block *block;

  158.   BLPY_REQUIRE_VALID (self, block);

  159.   if (BLOCK_SUPERBLOCK (block))
  160.     Py_RETURN_FALSE;

  161.   Py_RETURN_TRUE;
  162. }

  163. /* Implementation of gdb.Block.is_static (self) -> Boolean.
  164.    Returns True if this block object is a static block.  */

  165. static PyObject *
  166. blpy_is_static (PyObject *self, void *closure)
  167. {
  168.   const struct block *block;

  169.   BLPY_REQUIRE_VALID (self, block);

  170.   if (BLOCK_SUPERBLOCK (block) != NULL
  171.      && BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
  172.     Py_RETURN_TRUE;

  173.   Py_RETURN_FALSE;
  174. }

  175. static void
  176. blpy_dealloc (PyObject *obj)
  177. {
  178.   block_object *block = (block_object *) obj;

  179.   if (block->prev)
  180.     block->prev->next = block->next;
  181.   else if (block->objfile)
  182.     {
  183.       set_objfile_data (block->objfile, blpy_objfile_data_key,
  184.                         block->next);
  185.     }
  186.   if (block->next)
  187.     block->next->prev = block->prev;
  188.   block->block = NULL;
  189. }

  190. /* Given a block, and a block_object that has previously been
  191.    allocated and initialized, populate the block_object with the
  192.    struct block data.  Also, register the block_object life-cycle
  193.    with the life-cycle of the object file associated with this
  194.    block, if needed.  */
  195. static void
  196. set_block (block_object *obj, const struct block *block,
  197.            struct objfile *objfile)
  198. {
  199.   obj->block = block;
  200.   obj->prev = NULL;
  201.   if (objfile)
  202.     {
  203.       obj->objfile = objfile;
  204.       obj->next = objfile_data (objfile, blpy_objfile_data_key);
  205.       if (obj->next)
  206.         obj->next->prev = obj;
  207.       set_objfile_data (objfile, blpy_objfile_data_key, obj);
  208.     }
  209.   else
  210.     obj->next = NULL;
  211. }

  212. /* Create a new block object (gdb.Block) that encapsulates the struct
  213.    block object from GDB.  */
  214. PyObject *
  215. block_to_block_object (const struct block *block, struct objfile *objfile)
  216. {
  217.   block_object *block_obj;

  218.   block_obj = PyObject_New (block_object, &block_object_type);
  219.   if (block_obj)
  220.     set_block (block_obj, block, objfile);

  221.   return (PyObject *) block_obj;
  222. }

  223. /* Return struct block reference that is wrapped by this object.  */
  224. const struct block *
  225. block_object_to_block (PyObject *obj)
  226. {
  227.   if (! PyObject_TypeCheck (obj, &block_object_type))
  228.     return NULL;
  229.   return ((block_object *) obj)->block;
  230. }

  231. /* Return a reference to the block iterator.  */
  232. static PyObject *
  233. blpy_block_syms_iter (PyObject *self)
  234. {
  235.   block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;

  236.   BLPY_ITER_REQUIRE_VALID (iter_obj->source);

  237.   Py_INCREF (self);
  238.   return self;
  239. }

  240. /* Return the next symbol in the iteration through the block's
  241.    dictionary.  */
  242. static PyObject *
  243. blpy_block_syms_iternext (PyObject *self)
  244. {
  245.   block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
  246.   struct symbol *sym;

  247.   BLPY_ITER_REQUIRE_VALID (iter_obj->source);

  248.   if (!iter_obj->initialized_p)
  249.     {
  250.       sym = block_iterator_first (iter_obj->block,  &(iter_obj->iter));
  251.       iter_obj->initialized_p = 1;
  252.     }
  253.   else
  254.     sym = block_iterator_next (&(iter_obj->iter));

  255.   if (sym == NULL)
  256.     {
  257.       PyErr_SetString (PyExc_StopIteration, _("Symbol is null."));
  258.       return NULL;
  259.     }

  260.   return symbol_to_symbol_object (sym);
  261. }

  262. static void
  263. blpy_block_syms_dealloc (PyObject *obj)
  264. {
  265.   block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj;

  266.   Py_XDECREF (iter_obj->source);
  267. }

  268. /* Implementation of gdb.Block.is_valid (self) -> Boolean.
  269.    Returns True if this block object still exists in GDB.  */

  270. static PyObject *
  271. blpy_is_valid (PyObject *self, PyObject *args)
  272. {
  273.   const struct block *block;

  274.   block = block_object_to_block (self);
  275.   if (block == NULL)
  276.     Py_RETURN_FALSE;

  277.   Py_RETURN_TRUE;
  278. }

  279. /* Implementation of gdb.BlockIterator.is_valid (self) -> Boolean.
  280.    Returns True if this block iterator object still exists in GDB  */

  281. static PyObject *
  282. blpy_iter_is_valid (PyObject *self, PyObject *args)
  283. {
  284.   block_syms_iterator_object *iter_obj =
  285.     (block_syms_iterator_object *) self;

  286.   if (iter_obj->source->block == NULL)
  287.     Py_RETURN_FALSE;

  288.   Py_RETURN_TRUE;
  289. }

  290. /* Return the innermost lexical block containing the specified pc value,
  291.    or 0 if there is none.  */
  292. PyObject *
  293. gdbpy_block_for_pc (PyObject *self, PyObject *args)
  294. {
  295.   gdb_py_ulongest pc;
  296.   const struct block *block = NULL;
  297.   struct compunit_symtab *cust = NULL;
  298.   volatile struct gdb_exception except;

  299.   if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
  300.     return NULL;

  301.   TRY_CATCH (except, RETURN_MASK_ALL)
  302.     {
  303.       cust = find_pc_compunit_symtab (pc);

  304.       if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
  305.         block = block_for_pc (pc);
  306.     }
  307.   GDB_PY_HANDLE_EXCEPTION (except);

  308.   if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
  309.     {
  310.       PyErr_SetString (PyExc_RuntimeError,
  311.                        _("Cannot locate object file for block."));
  312.       return NULL;
  313.     }

  314.   if (block)
  315.     return block_to_block_object (block, COMPUNIT_OBJFILE (cust));

  316.   Py_RETURN_NONE;
  317. }

  318. /* This function is called when an objfile is about to be freed.
  319.    Invalidate the block as further actions on the block would result
  320.    in bad data.  All access to obj->symbol should be gated by
  321.    BLPY_REQUIRE_VALID which will raise an exception on invalid
  322.    blocks.  */
  323. static void
  324. del_objfile_blocks (struct objfile *objfile, void *datum)
  325. {
  326.   block_object *obj = datum;

  327.   while (obj)
  328.     {
  329.       block_object *next = obj->next;

  330.       obj->block = NULL;
  331.       obj->objfile = NULL;
  332.       obj->next = NULL;
  333.       obj->prev = NULL;

  334.       obj = next;
  335.     }
  336. }

  337. int
  338. gdbpy_initialize_blocks (void)
  339. {
  340.   block_object_type.tp_new = PyType_GenericNew;
  341.   if (PyType_Ready (&block_object_type) < 0)
  342.     return -1;

  343.   block_syms_iterator_object_type.tp_new = PyType_GenericNew;
  344.   if (PyType_Ready (&block_syms_iterator_object_type) < 0)
  345.     return -1;

  346.   /* Register an objfile "free" callback so we can properly
  347.      invalidate blocks when an object file is about to be
  348.      deleted.  */
  349.   blpy_objfile_data_key
  350.     = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);

  351.   if (gdb_pymodule_addobject (gdb_module, "Block",
  352.                               (PyObject *) &block_object_type) < 0)
  353.     return -1;

  354.   return gdb_pymodule_addobject (gdb_module, "BlockIterator",
  355.                                  (PyObject *) &block_syms_iterator_object_type);
  356. }



  357. static PyMethodDef block_object_methods[] = {
  358.   { "is_valid", blpy_is_valid, METH_NOARGS,
  359.     "is_valid () -> Boolean.\n\
  360. Return true if this block is valid, false if not." },
  361.   {NULL/* Sentinel */
  362. };

  363. static PyGetSetDef block_object_getset[] = {
  364.   { "start", blpy_get_start, NULL, "Start address of the block.", NULL },
  365.   { "end", blpy_get_end, NULL, "End address of the block.", NULL },
  366.   { "function", blpy_get_function, NULL,
  367.     "Symbol that names the block, or None.", NULL },
  368.   { "superblock", blpy_get_superblock, NULL,
  369.     "Block containing the block, or None.", NULL },
  370.   { "global_block", blpy_get_global_block, NULL,
  371.     "Block containing the global block.", NULL },
  372.   { "static_block", blpy_get_static_block, NULL,
  373.     "Block containing the static block.", NULL },
  374.   { "is_static", blpy_is_static, NULL,
  375.     "Whether this block is a static block.", NULL },
  376.   { "is_global", blpy_is_global, NULL,
  377.     "Whether this block is a global block.", NULL },
  378.   { NULL/* Sentinel */
  379. };

  380. PyTypeObject block_object_type = {
  381.   PyVarObject_HEAD_INIT (NULL, 0)
  382.   "gdb.Block",                          /*tp_name*/
  383.   sizeof (block_object),          /*tp_basicsize*/
  384.   0,                                  /*tp_itemsize*/
  385.   blpy_dealloc,                   /*tp_dealloc*/
  386.   0,                                  /*tp_print*/
  387.   0,                                  /*tp_getattr*/
  388.   0,                                  /*tp_setattr*/
  389.   0,                                  /*tp_compare*/
  390.   0,                                  /*tp_repr*/
  391.   0,                                  /*tp_as_number*/
  392.   0,                                  /*tp_as_sequence*/
  393.   0,                                  /*tp_as_mapping*/
  394.   0,                                  /*tp_hash */
  395.   0,                                  /*tp_call*/
  396.   0,                                  /*tp_str*/
  397.   0,                                  /*tp_getattro*/
  398.   0,                                  /*tp_setattro*/
  399.   0,                                  /*tp_as_buffer*/
  400.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER/*tp_flags*/
  401.   "GDB block object",                  /* tp_doc */
  402.   0,                                  /* tp_traverse */
  403.   0,                                  /* tp_clear */
  404.   0,                                  /* tp_richcompare */
  405.   0,                                  /* tp_weaklistoffset */
  406.   blpy_iter,                          /* tp_iter */
  407.   0,                                  /* tp_iternext */
  408.   block_object_methods,                  /* tp_methods */
  409.   0,                                  /* tp_members */
  410.   block_object_getset                  /* tp_getset */
  411. };

  412. static PyMethodDef block_iterator_object_methods[] = {
  413.   { "is_valid", blpy_iter_is_valid, METH_NOARGS,
  414.     "is_valid () -> Boolean.\n\
  415. Return true if this block iterator is valid, false if not." },
  416.   {NULL/* Sentinel */
  417. };

  418. static PyTypeObject block_syms_iterator_object_type = {
  419.   PyVarObject_HEAD_INIT (NULL, 0)
  420.   "gdb.BlockIterator",                  /*tp_name*/
  421.   sizeof (block_syms_iterator_object),              /*tp_basicsize*/
  422.   0,                                  /*tp_itemsize*/
  423.   blpy_block_syms_dealloc,          /*tp_dealloc*/
  424.   0,                                  /*tp_print*/
  425.   0,                                  /*tp_getattr*/
  426.   0,                                  /*tp_setattr*/
  427.   0,                                  /*tp_compare*/
  428.   0,                                  /*tp_repr*/
  429.   0,                                  /*tp_as_number*/
  430.   0,                                  /*tp_as_sequence*/
  431.   0,                                  /*tp_as_mapping*/
  432.   0,                                  /*tp_hash */
  433.   0,                                  /*tp_call*/
  434.   0,                                  /*tp_str*/
  435.   0,                                  /*tp_getattro*/
  436.   0,                                  /*tp_setattro*/
  437.   0,                                  /*tp_as_buffer*/
  438.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER/*tp_flags*/
  439.   "GDB block syms iterator object",              /*tp_doc */
  440.   0,                                  /*tp_traverse */
  441.   0,                                  /*tp_clear */
  442.   0,                                  /*tp_richcompare */
  443.   0,                                  /*tp_weaklistoffset */
  444.   blpy_block_syms_iter,           /*tp_iter */
  445.   blpy_block_syms_iternext,          /*tp_iternext */
  446.   block_iterator_object_methods   /*tp_methods */
  447. };