mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-08-06 14:49:38 +08:00
gdb: add __repr__() implementation to a few Python types
Only a few types in the Python API currently have __repr__() implementations. This patch adds a few more of them. specifically: it adds __repr__() implementations to gdb.Symbol, gdb.Architecture, gdb.Block, gdb.Breakpoint, gdb.BreakpointLocation, and gdb.Type. This makes it easier to play around the GDB Python API in the Python interpreter session invoked with the 'pi' command in GDB, giving more easily accessible tipe information to users. An example of how this would look like: (gdb) pi >> gdb.lookup_type("char") <gdb.Type code=TYPE_CODE_INT name=char> >> gdb.lookup_global_symbol("main") <gdb.Symbol print_name=main> The gdb.Block.__repr__() method shows the first 5 symbols from the block, and then a message to show how many more were elided (if any).
This commit is contained in:

committed by
Andrew Burgess

parent
b8c2de06bc
commit
bb2bd584f3
@ -418,6 +418,41 @@ blpy_iter_is_valid (PyObject *self, PyObject *args)
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
/* __repr__ implementation for gdb.Block. */
|
||||
|
||||
static PyObject *
|
||||
blpy_repr (PyObject *self)
|
||||
{
|
||||
const auto block = block_object_to_block (self);
|
||||
if (block == nullptr)
|
||||
return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
|
||||
|
||||
const auto name = block->function () ?
|
||||
block->function ()->print_name () : "<anonymous>";
|
||||
|
||||
std::string str;
|
||||
unsigned int written_symbols = 0;
|
||||
const int len = mdict_size (block->multidict ());
|
||||
static constexpr int SYMBOLS_TO_SHOW = 5;
|
||||
for (struct symbol *symbol : block_iterator_range (block))
|
||||
{
|
||||
if (written_symbols == SYMBOLS_TO_SHOW)
|
||||
{
|
||||
const int remaining = len - SYMBOLS_TO_SHOW;
|
||||
if (remaining == 1)
|
||||
str += string_printf ("... (%d more symbol)", remaining);
|
||||
else
|
||||
str += string_printf ("... (%d more symbols)", remaining);
|
||||
break;
|
||||
}
|
||||
str += symbol->print_name ();
|
||||
if (++written_symbols < len)
|
||||
str += ", ";
|
||||
}
|
||||
return PyUnicode_FromFormat ("<%s %s {%s}>", Py_TYPE (self)->tp_name,
|
||||
name, str.c_str ());
|
||||
}
|
||||
|
||||
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
|
||||
gdbpy_initialize_blocks (void)
|
||||
{
|
||||
@ -482,7 +517,7 @@ PyTypeObject block_object_type = {
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare*/
|
||||
0, /*tp_repr*/
|
||||
blpy_repr, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
&block_object_as_mapping, /*tp_as_mapping*/
|
||||
|
Reference in New Issue
Block a user