diff options
author | Simon Marchi <simon.marchi@ericsson.com> | 2018-09-13 11:53:22 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@ericsson.com> | 2018-09-13 11:54:38 -0400 |
commit | 1256af7d1afb123c15ae3935de4470becdf7c512 (patch) | |
tree | 8dcd53a790cf09f061522ecb2ee4298214c8580d /gdb/testsuite/gdb.python/py-objfile.exp | |
parent | 508d0c9b5945d30bcf163b9b88213d277949e9a8 (diff) | |
download | gdb-1256af7d1afb123c15ae3935de4470becdf7c512.zip gdb-1256af7d1afb123c15ae3935de4470becdf7c512.tar.gz gdb-1256af7d1afb123c15ae3935de4470becdf7c512.tar.bz2 |
python: Provide textual representation for Inferior and Objfile
Printing a GDB Python object is notoriously not helpful:
>>> print(gdb.selected_inferior())
<gdb.Inferior object at 0x7fea59aed198>
>>> print(gdb.objfiles())
[<gdb.Objfile object at 0x7fea59b57c90>]
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())
<gdb.Inferior num=1>
>>> print(gdb.objfiles())
[<gdb.Objfile filename=/home/emaisin/build/binutils-gdb-gcc-git/gdb/test>]
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.
Diffstat (limited to 'gdb/testsuite/gdb.python/py-objfile.exp')
-rw-r--r-- | gdb/testsuite/gdb.python/py-objfile.exp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.python/py-objfile.exp b/gdb/testsuite/gdb.python/py-objfile.exp index 6e81750..ce24b56 100644 --- a/gdb/testsuite/gdb.python/py-objfile.exp +++ b/gdb/testsuite/gdb.python/py-objfile.exp @@ -45,6 +45,9 @@ gdb_test "python print (objfile.filename)" "${testfile}" \ gdb_test "python print (objfile.username)" "${testfile}" \ "Get objfile user name" +gdb_test "python print (objfile)" \ + "<gdb.Objfile filename=[string_to_regexp ${binfile}]>" + gdb_test_no_output "python dir(objfile)" gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \ @@ -149,3 +152,9 @@ if [remote_file host exists "${symlink_binary}"] { gdb_test "python print (gdb.lookup_objfile (\"${symlink_binary}\").filename)" \ "${testfile}" "gdb.lookup_objfile of symlinked binary" } + +# Test printing an Objfile object that is no longer valid. +gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" \ + "get first objfile" 1 +gdb_file_cmd ${binfile} +gdb_test "python print(objfile)" "<gdb.Objfile \\\(invalid\\\)>"
\ No newline at end of file |