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/doc/ChangeLog | 5 +++++ gdb/doc/python.texi | 4 ++++ 2 files changed, 9 insertions(+) (limited to 'gdb/doc') diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index f04dca4..1c96508 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,8 @@ +2018-09-13 Simon Marchi + + * python.texi (Basic Python): Mention the string representation + of GDB Python objects. + 2018-09-12 Simon Marchi * gdb.texinfo (Skipping Over Functions and Files): Document diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 5e2ab76..6e2bf5c 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -207,6 +207,10 @@ methods and classes added by @value{GDBN} are placed in this module. @value{GDBN} automatically @code{import}s the @code{gdb} module for use in all scripts evaluated by the @code{python} command. +Some types of the @code{gdb} module come with a textual representation +(accessible through the @code{repr} or @code{str} functions). These are +offered for debugging purposes only, expect them to change over time. + @findex gdb.PYTHONDIR @defvar gdb.PYTHONDIR A string containing the python directory (@pxref{Python}). -- cgit v1.1