aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/py-block.c
diff options
context:
space:
mode:
authorMatheus Branco Borella <dark.ryu.550@gmail.com>2023-05-18 00:33:57 -0300
committerAndrew Burgess <aburgess@redhat.com>2023-07-04 12:07:16 +0100
commitbb2bd584f31a25ba1cfe5bdac4d07d8cffe87c3d (patch)
tree8dac0966bc63e625f87af8e1a6a2243e15df3803 /gdb/python/py-block.c
parentb8c2de06bcbc13a1a6b93163cf675272d7eada9f (diff)
downloadgdb-bb2bd584f31a25ba1cfe5bdac4d07d8cffe87c3d.zip
gdb-bb2bd584f31a25ba1cfe5bdac4d07d8cffe87c3d.tar.gz
gdb-bb2bd584f31a25ba1cfe5bdac4d07d8cffe87c3d.tar.bz2
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).
Diffstat (limited to 'gdb/python/py-block.c')
-rw-r--r--gdb/python/py-block.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 09fa74d..dd6d6d2 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -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*/