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:
Matheus Branco Borella
2023-05-18 00:33:57 -03:00
committed by Andrew Burgess
parent b8c2de06bc
commit bb2bd584f3
12 changed files with 329 additions and 19 deletions

View File

@@ -1028,6 +1028,34 @@ typy_template_argument (PyObject *self, PyObject *args)
return result;
}
/* __repr__ implementation for gdb.Type. */
static PyObject *
typy_repr (PyObject *self)
{
const auto type = type_object_to_type (self);
if (type == nullptr)
return PyUnicode_FromFormat ("<%s (invalid)>",
Py_TYPE (self)->tp_name);
const char *code = pyty_codes[type->code ()].name;
string_file type_name;
try
{
current_language->print_type (type, "", &type_name, -1, 0,
&type_print_raw_options);
}
catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
auto py_typename = PyUnicode_Decode (type_name.c_str (), type_name.size (),
host_charset (), NULL);
return PyUnicode_FromFormat ("<%s code=%s name=%U>", Py_TYPE (self)->tp_name,
code, py_typename);
}
static PyObject *
typy_str (PyObject *self)
{
@@ -1617,7 +1645,7 @@ PyTypeObject type_object_type =
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
typy_repr, /*tp_repr*/
&type_object_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
&typy_mapping, /*tp_as_mapping*/