Help using the GDB C++ STL pretty-printers / xmethods

Paul Smith
Sun May 8 20:26:20 GMT 2022

On Sun, 2022-05-08 at 15:44 -0400, Paul Smith wrote:
> On Sun, 2022-05-08 at 09:16 +0100, Jonathan Wakely wrote:
> > > gdb.set_convenience_variable('mgr', val['mgr'])
> > > init = gdb.parse_and_eval('$mgr->initialized')
> > > 
> > > This will use the xmethod to evaluate the expression.
> > 
> > And then:
> > 
> > if init:
> >     return gdb.parse_and_eval('*$mgr')
> Unfortunately, this doesn't work :(.  I can't do it from the GDB
> command line or python (I have tried both with the same results). 
> Something about convenience variables doesn't play well with xmethods
> (or maybe this xmethod implementation specifically?)

Hm.  I have done a fair amount of work writing GDB Python convenience
functions and commands, but I've only done a small amount of pretty-
printer work and no xmethod implementations.

But I don't understand this from the C++ STL

  class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
      def __call__(self, obj):
          impl_type = obj.dereference().type.fields()[0].type.tag

Why are we using dereference() here?  Aren't we expecting to receive a
gdb.Value of type std::unique_ptr here, not _pointer to_

But, it definitely doesn't work to remove the dereference(), and also
the value we get normally IS a pointer; adding debugging above I see:

  type: 'std::unique_ptr<Foo, std::default_delete<Foo> > *'

I don't really grok xmethods so I'm not sure how calling "foo->val"
when "foo" is std::unique_ptr<> results in the __call__ method being
invoked with a gdb.Value of type std::unique_ptr<>*.

My suspicion (not based on anything) is that whatever the reason is, is
why I can't use these xmethods with a convenience variable.

More information about the Gcc-help mailing list