1 /* Python interface to blocks.
3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "dictionary.h"
24 #include "python-internal.h"
28 typedef struct blpy_block_object {
30 /* The GDB block structure that represents a frame's code block. */
32 /* The backing object file. There is no direct relationship in GDB
33 between a block and an object file. When a block is created also
34 store a pointer to the object file for later use. */
35 struct objfile *objfile;
36 /* Keep track of all blocks with a doubly-linked list. Needed for
37 block invalidation if the source object file has been freed. */
38 struct blpy_block_object *prev;
39 struct blpy_block_object *next;
44 /* The block dictionary of symbols. */
45 struct dictionary *dict;
46 /* The iterator for that dictionary. */
47 struct dict_iterator iter;
48 /* Has the iterator been initialized flag. */
50 /* Pointer back to the original source block object. Needed to
51 check if the block is still valid, and has not been invalidated
52 when an object file has been freed. */
53 struct blpy_block_object *source;
54 } block_syms_iterator_object;
56 /* Require a valid block. All access to block_object->block should be
57 gated by this call. */
58 #define BLPY_REQUIRE_VALID(block_obj, block) \
60 block = block_object_to_block (block_obj); \
63 PyErr_SetString (PyExc_RuntimeError, \
64 _("Block is invalid.")); \
69 /* Require a valid block. This macro is called during block iterator
70 creation, and at each next call. */
71 #define BLPY_ITER_REQUIRE_VALID(block_obj) \
73 if (block_obj->block == NULL) \
75 PyErr_SetString (PyExc_RuntimeError, \
76 _("Source block for iterator is invalid.")); \
81 static PyTypeObject block_syms_iterator_object_type;
82 static const struct objfile_data *blpy_objfile_data_key;
85 blpy_iter (PyObject *self)
87 block_syms_iterator_object *block_iter_obj;
88 struct block *block = NULL;
90 BLPY_REQUIRE_VALID (self, block);
92 block_iter_obj = PyObject_New (block_syms_iterator_object,
93 &block_syms_iterator_object_type);
94 if (block_iter_obj == NULL)
97 block_iter_obj->dict = BLOCK_DICT (block);
98 block_iter_obj->initialized_p = 0;
100 block_iter_obj->source = (block_object *) self;
102 return (PyObject *) block_iter_obj;
106 blpy_get_start (PyObject *self, void *closure)
108 struct block *block = NULL;
110 BLPY_REQUIRE_VALID (self, block);
112 return PyLong_FromUnsignedLongLong (BLOCK_START (block));
116 blpy_get_end (PyObject *self, void *closure)
118 struct block *block = NULL;
120 BLPY_REQUIRE_VALID (self, block);
122 return PyLong_FromUnsignedLongLong (BLOCK_END (block));
126 blpy_get_function (PyObject *self, void *closure)
129 struct block *block = NULL;
131 BLPY_REQUIRE_VALID (self, block);
133 sym = BLOCK_FUNCTION (block);
135 return symbol_to_symbol_object (sym);
141 blpy_get_superblock (PyObject *self, void *closure)
143 struct block *block = NULL;
144 struct block *super_block = NULL;
145 block_object *self_obj = (block_object *) self;
147 BLPY_REQUIRE_VALID (self, block);
149 super_block = BLOCK_SUPERBLOCK (block);
151 return block_to_block_object (super_block, self_obj->objfile);
157 blpy_dealloc (PyObject *obj)
159 block_object *block = (block_object *) obj;
162 block->prev->next = block->next;
163 else if (block->objfile)
165 set_objfile_data (block->objfile, blpy_objfile_data_key,
169 block->next->prev = block->prev;
173 /* Given a block, and a block_object that has previously been
174 allocated and initialized, populate the block_object with the
175 struct block data. Also, register the block_object life-cycle
176 with the life-cycle of the the object file associated with this
179 set_block (block_object *obj, struct block *block,
180 struct objfile *objfile)
186 obj->objfile = objfile;
187 obj->next = objfile_data (objfile, blpy_objfile_data_key);
189 obj->next->prev = obj;
190 set_objfile_data (objfile, blpy_objfile_data_key, obj);
196 /* Create a new block object (gdb.Block) that encapsulates the struct
197 block object from GDB. */
199 block_to_block_object (struct block *block, struct objfile *objfile)
201 block_object *block_obj;
203 block_obj = PyObject_New (block_object, &block_object_type);
205 set_block (block_obj, block, objfile);
207 return (PyObject *) block_obj;
210 /* Return struct block reference that is wrapped by this object. */
212 block_object_to_block (PyObject *obj)
214 if (! PyObject_TypeCheck (obj, &block_object_type))
216 return ((block_object *) obj)->block;
219 /* Return a reference to the block iterator. */
221 blpy_block_syms_iter (PyObject *self)
223 block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
225 BLPY_ITER_REQUIRE_VALID (iter_obj->source);
231 /* Return the next symbol in the iteration through the block's
234 blpy_block_syms_iternext (PyObject *self)
236 block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
239 BLPY_ITER_REQUIRE_VALID (iter_obj->source);
241 if (!iter_obj->initialized_p)
243 sym = dict_iterator_first (iter_obj->dict, &(iter_obj->iter));
244 iter_obj->initialized_p = 1;
247 sym = dict_iterator_next (&(iter_obj->iter));
251 PyErr_SetString (PyExc_StopIteration, _("Symbol is null."));
255 return symbol_to_symbol_object (sym);
259 blpy_block_syms_dealloc (PyObject *obj)
261 block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj;
262 Py_XDECREF (iter_obj->source);
265 /* Return the innermost lexical block containing the specified pc value,
266 or 0 if there is none. */
268 gdbpy_block_for_pc (PyObject *self, PyObject *args)
270 unsigned PY_LONG_LONG pc;
272 struct obj_section *section;
273 struct symtab *symtab;
275 if (!PyArg_ParseTuple (args, "K", &pc))
278 section = find_pc_mapped_section (pc);
279 symtab = find_pc_sect_symtab (pc, section);
280 if (!symtab || symtab->objfile == NULL)
282 PyErr_SetString (PyExc_RuntimeError,
283 _("Cannot locate object file for block."));
287 block = block_for_pc (pc);
289 return block_to_block_object (block, symtab->objfile);
294 /* This function is called when an objfile is about to be freed.
295 Invalidate the block as further actions on the block would result
296 in bad data. All access to obj->symbol should be gated by
297 BLPY_REQUIRE_VALID which will raise an exception on invalid
300 del_objfile_blocks (struct objfile *objfile, void *datum)
302 block_object *obj = datum;
305 block_object *next = obj->next;
317 gdbpy_initialize_blocks (void)
319 block_object_type.tp_new = PyType_GenericNew;
320 if (PyType_Ready (&block_object_type) < 0)
323 block_syms_iterator_object_type.tp_new = PyType_GenericNew;
324 if (PyType_Ready (&block_syms_iterator_object_type) < 0)
327 /* Register an objfile "free" callback so we can properly
328 invalidate blocks when an object file is about to be
330 blpy_objfile_data_key
331 = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
333 Py_INCREF (&block_object_type);
334 PyModule_AddObject (gdb_module, "Block", (PyObject *) &block_object_type);
336 Py_INCREF (&block_syms_iterator_object_type);
337 PyModule_AddObject (gdb_module, "BlockIterator",
338 (PyObject *) &block_syms_iterator_object_type);
343 static PyGetSetDef block_object_getset[] = {
344 { "start", blpy_get_start, NULL, "Start address of the block.", NULL },
345 { "end", blpy_get_end, NULL, "End address of the block.", NULL },
346 { "function", blpy_get_function, NULL,
347 "Symbol that names the block, or None.", NULL },
348 { "superblock", blpy_get_superblock, NULL,
349 "Block containing the block, or None.", NULL },
350 { NULL } /* Sentinel */
353 PyTypeObject block_object_type = {
354 PyObject_HEAD_INIT (NULL)
356 "gdb.Block", /*tp_name*/
357 sizeof (block_object), /*tp_basicsize*/
359 blpy_dealloc, /*tp_dealloc*/
366 0, /*tp_as_sequence*/
374 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
375 "GDB block object", /* tp_doc */
378 0, /* tp_richcompare */
379 0, /* tp_weaklistoffset */
380 blpy_iter, /* tp_iter */
384 block_object_getset /* tp_getset */
387 static PyTypeObject block_syms_iterator_object_type = {
388 PyObject_HEAD_INIT (NULL)
390 "gdb.BlockIterator", /*tp_name*/
391 sizeof (block_syms_iterator_object), /*tp_basicsize*/
393 blpy_block_syms_dealloc, /*tp_dealloc*/
400 0, /*tp_as_sequence*/
408 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
409 "GDB block syms iterator object", /*tp_doc */
412 0, /*tp_richcompare */
413 0, /*tp_weaklistoffset */
414 blpy_block_syms_iter, /*tp_iter */
415 blpy_block_syms_iternext, /*tp_iternext */