1 /* Python interface to blocks.
3 Copyright (C) 2008-2023 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"
29 /* The GDB block structure that represents a frame's code block. */
30 const struct block
*block
;
31 /* The backing object file. There is no direct relationship in GDB
32 between a block and an object file. When a block is created also
33 store a pointer to the object file for later use. */
34 struct objfile
*objfile
;
35 /* Keep track of all blocks with a doubly-linked list. Needed for
36 block invalidation if the source object file has been freed. */
41 struct block_syms_iterator_object
{
44 const struct block
*block
;
45 /* The iterator for that block. */
46 struct block_iterator iter
;
47 /* Has the iterator been initialized flag. */
49 /* Pointer back to the original source block object. Needed to
50 check if the block is still valid, and has not been invalidated
51 when an object file has been freed. */
55 /* Require a valid block. All access to block_object->block should be
56 gated by this call. */
57 #define BLPY_REQUIRE_VALID(block_obj, block) \
59 block = block_object_to_block (block_obj); \
62 PyErr_SetString (PyExc_RuntimeError, \
63 _("Block is invalid.")); \
68 /* Require a valid block. This macro is called during block iterator
69 creation, and at each next call. */
70 #define BLPY_ITER_REQUIRE_VALID(block_obj) \
72 if (block_obj->block == NULL) \
74 PyErr_SetString (PyExc_RuntimeError, \
75 _("Source block for iterator is invalid.")); \
80 /* This is called when an objfile is about to be freed.
81 Invalidate the block as further actions on the block would result
82 in bad data. All access to obj->symbol should be gated by
83 BLPY_REQUIRE_VALID which will raise an exception on invalid
87 void operator() (block_object
*obj
)
91 block_object
*next
= obj
->next
;
103 extern PyTypeObject block_syms_iterator_object_type
104 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("block_syms_iterator_object");
105 static const registry
<objfile
>::key
<block_object
, blpy_deleter
>
106 blpy_objfile_data_key
;
109 blpy_iter (PyObject
*self
)
111 block_syms_iterator_object
*block_iter_obj
;
112 const struct block
*block
= NULL
;
114 BLPY_REQUIRE_VALID (self
, block
);
116 block_iter_obj
= PyObject_New (block_syms_iterator_object
,
117 &block_syms_iterator_object_type
);
118 if (block_iter_obj
== NULL
)
121 block_iter_obj
->block
= block
;
122 block_iter_obj
->initialized_p
= 0;
124 block_iter_obj
->source
= (block_object
*) self
;
126 return (PyObject
*) block_iter_obj
;
130 blpy_get_start (PyObject
*self
, void *closure
)
132 const struct block
*block
= NULL
;
134 BLPY_REQUIRE_VALID (self
, block
);
136 return gdb_py_object_from_ulongest (block
->start ()).release ();
140 blpy_get_end (PyObject
*self
, void *closure
)
142 const struct block
*block
= NULL
;
144 BLPY_REQUIRE_VALID (self
, block
);
146 return gdb_py_object_from_ulongest (block
->end ()).release ();
150 blpy_get_function (PyObject
*self
, void *closure
)
153 const struct block
*block
;
155 BLPY_REQUIRE_VALID (self
, block
);
157 sym
= block
->function ();
159 return symbol_to_symbol_object (sym
);
165 blpy_get_superblock (PyObject
*self
, void *closure
)
167 const struct block
*block
;
168 const struct block
*super_block
;
169 block_object
*self_obj
= (block_object
*) self
;
171 BLPY_REQUIRE_VALID (self
, block
);
173 super_block
= block
->superblock ();
175 return block_to_block_object (super_block
, self_obj
->objfile
);
180 /* Return the global block associated to this block. */
183 blpy_get_global_block (PyObject
*self
, void *closure
)
185 const struct block
*block
;
186 const struct block
*global_block
;
187 block_object
*self_obj
= (block_object
*) self
;
189 BLPY_REQUIRE_VALID (self
, block
);
191 global_block
= block_global_block (block
);
193 return block_to_block_object (global_block
,
198 /* Return the static block associated to this block. Return None
199 if we cannot get the static block (this is the global block). */
202 blpy_get_static_block (PyObject
*self
, void *closure
)
204 const struct block
*block
;
205 const struct block
*static_block
;
206 block_object
*self_obj
= (block_object
*) self
;
208 BLPY_REQUIRE_VALID (self
, block
);
210 if (block
->superblock () == NULL
)
213 static_block
= block_static_block (block
);
215 return block_to_block_object (static_block
, self_obj
->objfile
);
218 /* Implementation of gdb.Block.is_global (self) -> Boolean.
219 Returns True if this block object is a global block. */
222 blpy_is_global (PyObject
*self
, void *closure
)
224 const struct block
*block
;
226 BLPY_REQUIRE_VALID (self
, block
);
228 if (block
->superblock ())
234 /* Implementation of gdb.Block.is_static (self) -> Boolean.
235 Returns True if this block object is a static block. */
238 blpy_is_static (PyObject
*self
, void *closure
)
240 const struct block
*block
;
242 BLPY_REQUIRE_VALID (self
, block
);
244 if (block
->superblock () != NULL
245 && block
->superblock ()->superblock () == NULL
)
251 /* Given a string, returns the gdb.Symbol representing that symbol in this
252 block. If such a symbol does not exist, returns NULL with a Python
256 blpy_getitem (PyObject
*self
, PyObject
*key
)
258 const struct block
*block
;
260 BLPY_REQUIRE_VALID (self
, block
);
262 gdb::unique_xmalloc_ptr
<char> name
= python_string_to_host_string (key
);
266 lookup_name_info
lookup_name (name
.get(), symbol_name_match_type::FULL
);
268 /* We use ALL_BLOCK_SYMBOLS_WITH_NAME instead of block_lookup_symbol so
269 that we can look up symbols irrespective of the domain, matching the
270 iterator. It would be confusing if the iterator returns symbols you
271 can't find via getitem. */
272 struct block_iterator iter
;
273 struct symbol
*sym
= nullptr;
274 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
276 /* Just stop at the first match */
282 PyErr_SetObject (PyExc_KeyError
, key
);
285 return symbol_to_symbol_object (sym
);
289 blpy_dealloc (PyObject
*obj
)
291 block_object
*block
= (block_object
*) obj
;
294 block
->prev
->next
= block
->next
;
295 else if (block
->objfile
)
296 blpy_objfile_data_key
.set (block
->objfile
, block
->next
);
298 block
->next
->prev
= block
->prev
;
300 Py_TYPE (obj
)->tp_free (obj
);
303 /* Given a block, and a block_object that has previously been
304 allocated and initialized, populate the block_object with the
305 struct block data. Also, register the block_object life-cycle
306 with the life-cycle of the object file associated with this
309 set_block (block_object
*obj
, const struct block
*block
,
310 struct objfile
*objfile
)
316 obj
->objfile
= objfile
;
317 obj
->next
= blpy_objfile_data_key
.get (objfile
);
319 obj
->next
->prev
= obj
;
320 blpy_objfile_data_key
.set (objfile
, obj
);
326 /* Create a new block object (gdb.Block) that encapsulates the struct
327 block object from GDB. */
329 block_to_block_object (const struct block
*block
, struct objfile
*objfile
)
331 block_object
*block_obj
;
333 block_obj
= PyObject_New (block_object
, &block_object_type
);
335 set_block (block_obj
, block
, objfile
);
337 return (PyObject
*) block_obj
;
340 /* Return struct block reference that is wrapped by this object. */
342 block_object_to_block (PyObject
*obj
)
344 if (! PyObject_TypeCheck (obj
, &block_object_type
))
346 return ((block_object
*) obj
)->block
;
349 /* Return a reference to the block iterator. */
351 blpy_block_syms_iter (PyObject
*self
)
353 block_syms_iterator_object
*iter_obj
= (block_syms_iterator_object
*) self
;
355 BLPY_ITER_REQUIRE_VALID (iter_obj
->source
);
361 /* Return the next symbol in the iteration through the block's
364 blpy_block_syms_iternext (PyObject
*self
)
366 block_syms_iterator_object
*iter_obj
= (block_syms_iterator_object
*) self
;
369 BLPY_ITER_REQUIRE_VALID (iter_obj
->source
);
371 if (!iter_obj
->initialized_p
)
373 sym
= block_iterator_first (iter_obj
->block
, &(iter_obj
->iter
));
374 iter_obj
->initialized_p
= 1;
377 sym
= block_iterator_next (&(iter_obj
->iter
));
381 PyErr_SetString (PyExc_StopIteration
, _("Symbol is null."));
385 return symbol_to_symbol_object (sym
);
389 blpy_block_syms_dealloc (PyObject
*obj
)
391 block_syms_iterator_object
*iter_obj
= (block_syms_iterator_object
*) obj
;
393 Py_XDECREF (iter_obj
->source
);
394 Py_TYPE (obj
)->tp_free (obj
);
397 /* Implementation of gdb.Block.is_valid (self) -> Boolean.
398 Returns True if this block object still exists in GDB. */
401 blpy_is_valid (PyObject
*self
, PyObject
*args
)
403 const struct block
*block
;
405 block
= block_object_to_block (self
);
412 /* Implementation of gdb.BlockIterator.is_valid (self) -> Boolean.
413 Returns True if this block iterator object still exists in GDB */
416 blpy_iter_is_valid (PyObject
*self
, PyObject
*args
)
418 block_syms_iterator_object
*iter_obj
=
419 (block_syms_iterator_object
*) self
;
421 if (iter_obj
->source
->block
== NULL
)
428 gdbpy_initialize_blocks (void)
430 block_object_type
.tp_new
= PyType_GenericNew
;
431 if (PyType_Ready (&block_object_type
) < 0)
434 block_syms_iterator_object_type
.tp_new
= PyType_GenericNew
;
435 if (PyType_Ready (&block_syms_iterator_object_type
) < 0)
438 if (gdb_pymodule_addobject (gdb_module
, "Block",
439 (PyObject
*) &block_object_type
) < 0)
442 return gdb_pymodule_addobject (gdb_module
, "BlockIterator",
443 (PyObject
*) &block_syms_iterator_object_type
);
448 static PyMethodDef block_object_methods
[] = {
449 { "is_valid", blpy_is_valid
, METH_NOARGS
,
450 "is_valid () -> Boolean.\n\
451 Return true if this block is valid, false if not." },
452 {NULL
} /* Sentinel */
455 static gdb_PyGetSetDef block_object_getset
[] = {
456 { "start", blpy_get_start
, NULL
, "Start address of the block.", NULL
},
457 { "end", blpy_get_end
, NULL
, "End address of the block.", NULL
},
458 { "function", blpy_get_function
, NULL
,
459 "Symbol that names the block, or None.", NULL
},
460 { "superblock", blpy_get_superblock
, NULL
,
461 "Block containing the block, or None.", NULL
},
462 { "global_block", blpy_get_global_block
, NULL
,
463 "Block containing the global block.", NULL
},
464 { "static_block", blpy_get_static_block
, NULL
,
465 "Block containing the static block.", NULL
},
466 { "is_static", blpy_is_static
, NULL
,
467 "Whether this block is a static block.", NULL
},
468 { "is_global", blpy_is_global
, NULL
,
469 "Whether this block is a global block.", NULL
},
470 { NULL
} /* Sentinel */
473 static PyMappingMethods block_object_as_mapping
= {
479 PyTypeObject block_object_type
= {
480 PyVarObject_HEAD_INIT (NULL
, 0)
481 "gdb.Block", /*tp_name*/
482 sizeof (block_object
), /*tp_basicsize*/
484 blpy_dealloc
, /*tp_dealloc*/
491 0, /*tp_as_sequence*/
492 &block_object_as_mapping
, /*tp_as_mapping*/
499 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
500 "GDB block object", /* tp_doc */
503 0, /* tp_richcompare */
504 0, /* tp_weaklistoffset */
505 blpy_iter
, /* tp_iter */
507 block_object_methods
, /* tp_methods */
509 block_object_getset
/* tp_getset */
512 static PyMethodDef block_iterator_object_methods
[] = {
513 { "is_valid", blpy_iter_is_valid
, METH_NOARGS
,
514 "is_valid () -> Boolean.\n\
515 Return true if this block iterator is valid, false if not." },
516 {NULL
} /* Sentinel */
519 PyTypeObject block_syms_iterator_object_type
= {
520 PyVarObject_HEAD_INIT (NULL
, 0)
521 "gdb.BlockIterator", /*tp_name*/
522 sizeof (block_syms_iterator_object
), /*tp_basicsize*/
524 blpy_block_syms_dealloc
, /*tp_dealloc*/
531 0, /*tp_as_sequence*/
539 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
540 "GDB block syms iterator object", /*tp_doc */
543 0, /*tp_richcompare */
544 0, /*tp_weaklistoffset */
545 blpy_block_syms_iter
, /*tp_iter */
546 blpy_block_syms_iternext
, /*tp_iternext */
547 block_iterator_object_methods
/*tp_methods */