From bb2bd584f31a25ba1cfe5bdac4d07d8cffe87c3d Mon Sep 17 00:00:00 2001 From: Matheus Branco Borella Date: Thu, 18 May 2023 00:33:57 -0300 Subject: 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.lookup_global_symbol("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). --- gdb/python/py-symbol.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'gdb/python/py-symbol.c') diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index ff3d185..ee863aa 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -378,6 +378,19 @@ sympy_dealloc (PyObject *obj) Py_TYPE (obj)->tp_free (obj); } +/* __repr__ implementation for gdb.Symbol. */ + +static PyObject * +sympy_repr (PyObject *self) +{ + const auto symbol = symbol_object_to_symbol (self); + if (symbol == nullptr) + return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + + return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name, + symbol->print_name ()); +} + /* Implementation of gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this) A tuple with 2 elements is always returned. The first is the symbol @@ -741,7 +754,7 @@ PyTypeObject symbol_object_type = { 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ - 0, /*tp_repr*/ + sympy_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ -- cgit v1.1