gdb/python/py-block.c - gdb
Global variables defined
Data types defined
Functions defined
Macros defined
Source code
- #include "defs.h"
- #include "block.h"
- #include "dictionary.h"
- #include "symtab.h"
- #include "python-internal.h"
- #include "objfiles.h"
- #include "symtab.h"
- typedef struct blpy_block_object {
- PyObject_HEAD
-
- const struct block *block;
-
- struct objfile *objfile;
-
- struct blpy_block_object *prev;
- struct blpy_block_object *next;
- } block_object;
- typedef struct {
- PyObject_HEAD
-
- const struct block *block;
-
- struct block_iterator iter;
-
- int initialized_p;
-
- struct blpy_block_object *source;
- } block_syms_iterator_object;
- #define BLPY_REQUIRE_VALID(block_obj, block) \
- do { \
- block = block_object_to_block (block_obj); \
- if (block == NULL) \
- { \
- PyErr_SetString (PyExc_RuntimeError, \
- _("Block is invalid.")); \
- return NULL; \
- } \
- } while (0)
- #define BLPY_ITER_REQUIRE_VALID(block_obj) \
- do { \
- if (block_obj->block == NULL) \
- { \
- PyErr_SetString (PyExc_RuntimeError, \
- _("Source block for iterator is invalid.")); \
- return NULL; \
- } \
- } while (0)
- static PyTypeObject block_syms_iterator_object_type
- CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("block_syms_iterator_object");
- static const struct objfile_data *blpy_objfile_data_key;
- static PyObject *
- blpy_iter (PyObject *self)
- {
- block_syms_iterator_object *block_iter_obj;
- const struct block *block = NULL;
- BLPY_REQUIRE_VALID (self, block);
- block_iter_obj = PyObject_New (block_syms_iterator_object,
- &block_syms_iterator_object_type);
- if (block_iter_obj == NULL)
- return NULL;
- block_iter_obj->block = block;
- block_iter_obj->initialized_p = 0;
- Py_INCREF (self);
- block_iter_obj->source = (block_object *) self;
- return (PyObject *) block_iter_obj;
- }
- static PyObject *
- blpy_get_start (PyObject *self, void *closure)
- {
- const struct block *block = NULL;
- BLPY_REQUIRE_VALID (self, block);
- return gdb_py_object_from_ulongest (BLOCK_START (block));
- }
- static PyObject *
- blpy_get_end (PyObject *self, void *closure)
- {
- const struct block *block = NULL;
- BLPY_REQUIRE_VALID (self, block);
- return gdb_py_object_from_ulongest (BLOCK_END (block));
- }
- static PyObject *
- blpy_get_function (PyObject *self, void *closure)
- {
- struct symbol *sym;
- const struct block *block;
- BLPY_REQUIRE_VALID (self, block);
- sym = BLOCK_FUNCTION (block);
- if (sym)
- return symbol_to_symbol_object (sym);
- Py_RETURN_NONE;
- }
- static PyObject *
- blpy_get_superblock (PyObject *self, void *closure)
- {
- const struct block *block;
- const struct block *super_block;
- block_object *self_obj = (block_object *) self;
- BLPY_REQUIRE_VALID (self, block);
- super_block = BLOCK_SUPERBLOCK (block);
- if (super_block)
- return block_to_block_object (super_block, self_obj->objfile);
- Py_RETURN_NONE;
- }
- static PyObject *
- blpy_get_global_block (PyObject *self, void *closure)
- {
- const struct block *block;
- const struct block *global_block;
- block_object *self_obj = (block_object *) self;
- BLPY_REQUIRE_VALID (self, block);
- global_block = block_global_block (block);
- return block_to_block_object (global_block,
- self_obj->objfile);
- }
- static PyObject *
- blpy_get_static_block (PyObject *self, void *closure)
- {
- const struct block *block;
- const struct block *static_block;
- block_object *self_obj = (block_object *) self;
- BLPY_REQUIRE_VALID (self, block);
- if (BLOCK_SUPERBLOCK (block) == NULL)
- Py_RETURN_NONE;
- static_block = block_static_block (block);
- return block_to_block_object (static_block, self_obj->objfile);
- }
- static PyObject *
- blpy_is_global (PyObject *self, void *closure)
- {
- const struct block *block;
- BLPY_REQUIRE_VALID (self, block);
- if (BLOCK_SUPERBLOCK (block))
- Py_RETURN_FALSE;
- Py_RETURN_TRUE;
- }
- static PyObject *
- blpy_is_static (PyObject *self, void *closure)
- {
- const struct block *block;
- BLPY_REQUIRE_VALID (self, block);
- if (BLOCK_SUPERBLOCK (block) != NULL
- && BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
- Py_RETURN_TRUE;
- Py_RETURN_FALSE;
- }
- static void
- blpy_dealloc (PyObject *obj)
- {
- block_object *block = (block_object *) obj;
- if (block->prev)
- block->prev->next = block->next;
- else if (block->objfile)
- {
- set_objfile_data (block->objfile, blpy_objfile_data_key,
- block->next);
- }
- if (block->next)
- block->next->prev = block->prev;
- block->block = NULL;
- }
- static void
- set_block (block_object *obj, const struct block *block,
- struct objfile *objfile)
- {
- obj->block = block;
- obj->prev = NULL;
- if (objfile)
- {
- obj->objfile = objfile;
- obj->next = objfile_data (objfile, blpy_objfile_data_key);
- if (obj->next)
- obj->next->prev = obj;
- set_objfile_data (objfile, blpy_objfile_data_key, obj);
- }
- else
- obj->next = NULL;
- }
- PyObject *
- block_to_block_object (const struct block *block, struct objfile *objfile)
- {
- block_object *block_obj;
- block_obj = PyObject_New (block_object, &block_object_type);
- if (block_obj)
- set_block (block_obj, block, objfile);
- return (PyObject *) block_obj;
- }
- const struct block *
- block_object_to_block (PyObject *obj)
- {
- if (! PyObject_TypeCheck (obj, &block_object_type))
- return NULL;
- return ((block_object *) obj)->block;
- }
- static PyObject *
- blpy_block_syms_iter (PyObject *self)
- {
- block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
- BLPY_ITER_REQUIRE_VALID (iter_obj->source);
- Py_INCREF (self);
- return self;
- }
- static PyObject *
- blpy_block_syms_iternext (PyObject *self)
- {
- block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
- struct symbol *sym;
- BLPY_ITER_REQUIRE_VALID (iter_obj->source);
- if (!iter_obj->initialized_p)
- {
- sym = block_iterator_first (iter_obj->block, &(iter_obj->iter));
- iter_obj->initialized_p = 1;
- }
- else
- sym = block_iterator_next (&(iter_obj->iter));
- if (sym == NULL)
- {
- PyErr_SetString (PyExc_StopIteration, _("Symbol is null."));
- return NULL;
- }
- return symbol_to_symbol_object (sym);
- }
- static void
- blpy_block_syms_dealloc (PyObject *obj)
- {
- block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj;
- Py_XDECREF (iter_obj->source);
- }
- static PyObject *
- blpy_is_valid (PyObject *self, PyObject *args)
- {
- const struct block *block;
- block = block_object_to_block (self);
- if (block == NULL)
- Py_RETURN_FALSE;
- Py_RETURN_TRUE;
- }
- static PyObject *
- blpy_iter_is_valid (PyObject *self, PyObject *args)
- {
- block_syms_iterator_object *iter_obj =
- (block_syms_iterator_object *) self;
- if (iter_obj->source->block == NULL)
- Py_RETURN_FALSE;
- Py_RETURN_TRUE;
- }
- PyObject *
- gdbpy_block_for_pc (PyObject *self, PyObject *args)
- {
- gdb_py_ulongest pc;
- const struct block *block = NULL;
- struct compunit_symtab *cust = NULL;
- volatile struct gdb_exception except;
- if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
- return NULL;
- TRY_CATCH (except, RETURN_MASK_ALL)
- {
- cust = find_pc_compunit_symtab (pc);
- if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
- block = block_for_pc (pc);
- }
- GDB_PY_HANDLE_EXCEPTION (except);
- if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
- {
- PyErr_SetString (PyExc_RuntimeError,
- _("Cannot locate object file for block."));
- return NULL;
- }
- if (block)
- return block_to_block_object (block, COMPUNIT_OBJFILE (cust));
- Py_RETURN_NONE;
- }
- static void
- del_objfile_blocks (struct objfile *objfile, void *datum)
- {
- block_object *obj = datum;
- while (obj)
- {
- block_object *next = obj->next;
- obj->block = NULL;
- obj->objfile = NULL;
- obj->next = NULL;
- obj->prev = NULL;
- obj = next;
- }
- }
- int
- gdbpy_initialize_blocks (void)
- {
- block_object_type.tp_new = PyType_GenericNew;
- if (PyType_Ready (&block_object_type) < 0)
- return -1;
- block_syms_iterator_object_type.tp_new = PyType_GenericNew;
- if (PyType_Ready (&block_syms_iterator_object_type) < 0)
- return -1;
-
- blpy_objfile_data_key
- = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
- if (gdb_pymodule_addobject (gdb_module, "Block",
- (PyObject *) &block_object_type) < 0)
- return -1;
- return gdb_pymodule_addobject (gdb_module, "BlockIterator",
- (PyObject *) &block_syms_iterator_object_type);
- }
- static PyMethodDef block_object_methods[] = {
- { "is_valid", blpy_is_valid, METH_NOARGS,
- "is_valid () -> Boolean.\n\
- Return true if this block is valid, false if not." },
- {NULL}
- };
- static PyGetSetDef block_object_getset[] = {
- { "start", blpy_get_start, NULL, "Start address of the block.", NULL },
- { "end", blpy_get_end, NULL, "End address of the block.", NULL },
- { "function", blpy_get_function, NULL,
- "Symbol that names the block, or None.", NULL },
- { "superblock", blpy_get_superblock, NULL,
- "Block containing the block, or None.", NULL },
- { "global_block", blpy_get_global_block, NULL,
- "Block containing the global block.", NULL },
- { "static_block", blpy_get_static_block, NULL,
- "Block containing the static block.", NULL },
- { "is_static", blpy_is_static, NULL,
- "Whether this block is a static block.", NULL },
- { "is_global", blpy_is_global, NULL,
- "Whether this block is a global block.", NULL },
- { NULL }
- };
- PyTypeObject block_object_type = {
- PyVarObject_HEAD_INIT (NULL, 0)
- "gdb.Block",
- sizeof (block_object),
- 0,
- blpy_dealloc,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
- "GDB block object",
- 0,
- 0,
- 0,
- 0,
- blpy_iter,
- 0,
- block_object_methods,
- 0,
- block_object_getset
- };
- static PyMethodDef block_iterator_object_methods[] = {
- { "is_valid", blpy_iter_is_valid, METH_NOARGS,
- "is_valid () -> Boolean.\n\
- Return true if this block iterator is valid, false if not." },
- {NULL}
- };
- static PyTypeObject block_syms_iterator_object_type = {
- PyVarObject_HEAD_INIT (NULL, 0)
- "gdb.BlockIterator",
- sizeof (block_syms_iterator_object),
- 0,
- blpy_block_syms_dealloc,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
- "GDB block syms iterator object",
- 0,
- 0,
- 0,
- 0,
- blpy_block_syms_iter,
- blpy_block_syms_iternext,
- block_iterator_object_methods
- };