Bug 17136 - 'info type-printers' causes an exception when there are per-objfile printers
Summary: 'info type-printers' causes an exception when there are per-objfile printers
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.7
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-09 12:08 UTC by Jonathan Wakely
Modified: 2023-03-13 13:41 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2014-07-09 12:08:25 UTC
Given this type printer defined somewhere like .gdbinit

class Printer(object):
  def __init__ (self):
    self.name = 'Printer'
    self.enabled = True

  class _recognizer(object):
    def recognize(self, type_obj):
      if type_obj.tag == "Foo":
        return "Bar"
      return None

  def instantiate(self):
    return self._recognizer()

if you debug any program then register the type printer with the current objfile, running 'info type-printers' results in a python exception:

(gdb) start
Temporary breakpoint 1 at 0x4005b4: file bug.cc, line 4.
Starting program: /tmp/a.out 

Temporary breakpoint 1, main () at bug.cc:4
4         return 0;
(gdb) python gdb.types.register_type_printer(gdb.current_objfile(), Printer())
(gdb) info type-printers
Python Exception <type 'exceptions.AttributeError'> 'gdb.Objfile' object has no attribute 'name': 
Error occurred in Python command: 'gdb.Objfile' object has no attribute 'name'

The exception doesn't happen if the type printer is registered globally, by passing None instead of gdb.current_objfile().
Comment 1 Keith Seitz 2014-07-24 17:17:25 UTC
I am unable to reproduce this with either 7.7.1.20140611-cvs or git HEAD. What does your gdb report as its version?

Example of my attempt to reproduce:

$ ./gdb -q -data-directory data-directory gdb
Reading symbols from gdb...done.
Setting up the environment for debugging gdb.
Breakpoint 1 at 0x6f6390: file ../../src/gdb/utils.c, line 829.
Breakpoint 2 at 0x4ead77: file ../../src/gdb/cli/cli-cmds.c, line 219.
(top-gdb) start
Temporary breakpoint 3 at 0x45bdff: file ../../src/gdb/gdb.c, line 29.
Starting program: /home/keiths/sources/gdb/git/releases/7.7/linux/gdb/gdb 
During symbol reading, cannot get low and high bounds for subprogram DIE at 8088.
During symbol reading, Child DIE 0x31a3 and its abstract origin 0x684d have different tags.
During symbol reading, Child DIE 0x31a3 and its abstract origin 0x684d have different parents.
During symbol reading, DW_AT_GNU_call_site_target target DIE has invalid low pc, for referencing DIE 0x4961 [in module /usr/lib/debug/lib64/ld-2.18.so.debug].
During symbol reading, Multiple children of DIE 0x6253 refer to DIE 0x5df3 as their abstract origin.
During symbol reading, incomplete CFI data; unspecified registers (e.g., rax) at 0x3878e0397a.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
During symbol reading, incomplete CFI data; unspecified registers (e.g., rax) at 0x3878e0397a.

Temporary breakpoint 3, main (argc=1, argv=0x7fffffffdce8)
    at ../../src/gdb/gdb.c:29
29	  memset (&args, 0, sizeof args);
(top-gdb) py execfile('/home/keiths/tmp/17136.py')
(top-gdb) py gdb.types.register_type_printer (gdb.current_objfile(), Printer())
(top-gdb) info type-printers 
Global type printers:
  Printer
(top-gdb) 

Do you have a different procedure which tickles the bug?
Comment 2 Jonathan Wakely 2015-05-29 12:06:08 UTC
Keith, sorry for not replying to your question, not sure why I didn't see it.

I still see this in Fedora 22 with: GNU gdb (GDB) Fedora 7.9.1-13.fc22

tmp$ cat > p.cc
int main() {
return 0;                                                                    
}
tmp$ 
tmp$ 
tmp$ g++ -g p.cc
tmp$ gdb a.out
GNU gdb (GDB) Fedora 7.9.1-13.fc22
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) python
>class Printer(object):
>  def __init__ (self):
>    self.name = 'Printer'
>    self.enabled = True
>
>  class _recognizer(object):
>    def recognize(self, type_obj):
>      if type_obj.tag == "Foo":
>        return "Bar"
>      return None
>
>  def instantiate(self):
>    return self._recognizer()
>end
(gdb) start
Temporary breakpoint 1 at 0x4005ba: file p.cc, line 2.
Starting program: /tmp/a.out 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-5.fc22.x86_64

Temporary breakpoint 1, main () at p.cc:2
2       return 0;
Missing separate debuginfos, use: dnf debuginfo-install libgcc-5.1.1-1.fc22.x86_64 libstdc++-5.1.1-1.fc22.x86_64
(gdb) python gdb.types.register_type_printer(gdb.current_objfile(), Printer())
(gdb) info type-printers
Python Exception <class 'AttributeError'> 'gdb.Objfile' object has no attribute 'name': 
Error occurred in Python command: 'gdb.Objfile' object has no attribute 'name'
(gdb)
Comment 3 Jonathan Wakely 2015-05-29 12:11:02 UTC
The fix at https://bugzilla.redhat.com/show_bug.cgi?id=1085576 works for me.
Comment 4 dje 2015-06-01 19:02:34 UTC
Hmmm, I would have expected to see objfile.name, or the correct objfile.filename, in more places in type_printers.py.
Alas instead of being consistent with pretty-printers,
type-printers have invented a new syntax for the info/enable/disable trio.

All the more reason to put all of this under one piece of common code
used by everyone (pretty-printers, type-printers, xmethods, and so on).
See, e.g., https://sourceware.org/ml/gdb-patches/2015-04/msg00006.html
Comment 5 dje 2015-06-01 19:13:53 UTC
(In reply to dje from comment #4)
Filed PR 18476 to cover the "move to common framework" issue.
Comment 6 dje 2015-06-01 19:22:55 UTC
(In reply to Jonathan Wakely from comment #3)
> The fix at https://bugzilla.redhat.com/show_bug.cgi?id=1085576 works for me.

The patch can be checked in IMO.
Given that I don't have the time to write a testcase myself
I'm not going to force someone else to.
Another reviewer may require one though.
Comment 7 Jan Kratochvil 2015-06-26 13:49:33 UTC
[patch] Fix 'info type-printers' Python error - PR 17136
https://sourceware.org/ml/gdb-patches/2015-06/msg00572.html
Comment 8 Sourceware Commits 2015-08-06 16:26:11 UTC
The master branch has been updated by Doug Evans <devans@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ca0a5f0bd33d0aa17a5cf518e41e47ddfde486ad

commit ca0a5f0bd33d0aa17a5cf518e41e47ddfde486ad
Author: Clem Dickey <clemd@acm.org>
Date:   Thu Aug 6 09:24:58 2015 -0700

    PR python/17136
    
    gdb/ChangeLog:
    
    	* python/lib/gdb/command/type_printers.py (InfoTypePrinter): Fix typo.
Comment 9 Sourceware Commits 2015-08-06 16:30:50 UTC
The gdb-7.10-branch branch has been updated by Doug Evans <devans@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=17d1595ac5371d06537bc57df86a9f7359e62127

commit 17d1595ac5371d06537bc57df86a9f7359e62127
Author: Clem Dickey <clemd@acm.org>
Date:   Thu Aug 6 09:29:03 2015 -0700

    PR python/17136
    
    gdb/ChangeLog:
    
    	* python/lib/gdb/command/type_printers.py (InfoTypePrinter): Fix typo.
Comment 10 dje 2015-08-06 16:35:15 UTC
patch checked into trunk and 7.10 branch
Comment 11 Sourceware Commits 2023-03-13 13:41:08 UTC
The master branch has been updated by Bruno Larsen <blarsen@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=83b755117d7a13e75877c3f166dfef1d8b69ec75

commit 83b755117d7a13e75877c3f166dfef1d8b69ec75
Author: Bruno Larsen <blarsen@redhat.com>
Date:   Thu Feb 23 13:56:32 2023 +0100

    gdb/testsuite: add regression test for per-objfile typeprinters
    
    PR python/17136 reported an unhandled exception when using typeprinters
    only valid on some objfiles, rather than being a global typeprinter. The
    fix was accepted without a regression test, and we've been carrying one
    out-of-tree for a while but I think it's worth upstreaming. The code
    itself was developed by Jan Kratochvil.
    
    Co-Authored-By: Jan Kratochvil <jkratochvil@azul.com>
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17136
    Reviewed-By: Andrew Burgess <aburgess@redhat.com>
    Approved-By: Tom Tromey <tom@tromey.com>