From 1256af7d1afb123c15ae3935de4470becdf7c512 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 13 Sep 2018 11:53:22 -0400 Subject: python: Provide textual representation for Inferior and Objfile Printing a GDB Python object is notoriously not helpful: >>> print(gdb.selected_inferior()) >>> print(gdb.objfiles()) [] This makes printing debug traces more difficult than it should be. This patch provides some repr() implementation for these two types (more to come if people agree with the idea, but I want to test the water first). Here's the same example as above, but with this patch: >>> print(gdb.selected_inferior()) >>> print(gdb.objfiles()) [] I implemented repr rather than str, because when printing a list (or another container I suppose), Python calls the repr method of the elements. This is useful when printing a list of inferiors or objfiles. The print(gdb.objfiles()) above would not have worked if I had implemented str. I found this post useful to understand the difference between repr and str: https://stackoverflow.com/questions/1436703/difference-between-str-and-repr gdb/ChangeLog: * python/py-inferior.c (infpy_repr): New. (inferior_object_type): Register infpy_repr. * python/py-objfile.c (objfpy_repr): New. (objfile_object_type): Register objfpy_repr. gdb/testsuite/ChangeLog: * gdb.python/py-inferior.exp: Test repr() of gdb.Inferior. * gdb.python/py-objfile.exp: Test repr() of gdb.Objfile. * gdb.python/py-symtab.exp: Update test printing an objfile. gdb/doc/ChangeLog: * python.texi (Basic Python): Mention the string representation of GDB Python objects. --- gdb/python/py-objfile.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'gdb/python/py-objfile.c') diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index c2b40ff5352..61d3a158198 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -456,6 +456,21 @@ objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } +/* Implement repr() for gdb.Objfile. */ + +static PyObject * +objfpy_repr (PyObject *self_) +{ + objfile_object *self = (objfile_object *) self_; + objfile *obj = self->objfile; + + if (obj == nullptr) + return PyString_FromString (""); + + return PyString_FromFormat ("", + objfile_filename (obj)); +} + /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. Return non-zero if STRING is a potentially valid build id. */ @@ -709,7 +724,7 @@ PyTypeObject objfile_object_type = 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ - 0, /*tp_repr*/ + objfpy_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ -- cgit v1.2.1