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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 7 May 2022 at 02:24, Paul Smith <paul@xxxxxxxxxxxxxxxxx> wrote:
>
> Are there any docs or other information about how to use the GDB
> pretty-printers for C++ STL that come with GCC?
>
> I have them installed and they work for displaying data, but they don't
> work for accessing data.
>
> Just as one very simple example, is there a way to dereference a
> unique_ptr?  I see xmethods defined but nothing I've tried works.
> Suppose I have:
>
>   (gdb) p $mp->mgr
>   $6 = std::unique_ptr<class Mgr> = {
>     get() = 0x7f519a24e000
>   }
>
> (so you can see the pretty-printers are installed).  Now, how do I
> extract out this object pointer so I can see what's in it?  These don't
> work:
>
>   (gdb) p *$mp->mgr
>   One of the arguments you tried to pass to operator* could not be
>   converted to what the function wants.
>
>   (gdb) p $mp->mgr->initialized
>   One of the arguments you tried to pass to operator-> could not be
>   converted to what the function wants.
>
> It used to work in GCC 10.2 / GDB 10.1 to access the pointer directly
> if you knew, or deciphered, the internal structor of unique_ptr:
>
>   (gdb) p $mp->mgr._M_t._M_t._M_head_impl
>
> However, with GCC 11.3 / GDB 12.1 this no longer works: I get this
> error:
>
>   Request for member '_M_head_impl' is ambiguous in type
> 'std::tuple<Mgr*, std::default_delete<Mgr> >'.
>   Candidates are:
>     'std::default_delete<Mgr> std::_Head_base<1ul,
> std::default_delete<Mgr>, true>::_M_head_impl' (std::tuple<Mgr*,
> std::default_delete<Mgr> > -> std::_Tuple_impl<0ul, Mgr*,
> std::default_delete<Mgr> > -> std::_Tuple_impl<1ul,
> std::default_delete<Mgr> > -> std::_Head_base<1ul,
> std::default_delete<Mgr>, true>)
>     '<unnamed type> std::_Head_base<0ul, Mgr*, false>::_M_head_impl'
> (std::tuple<Mgr*, std::default_delete<Mgr> > -> std::_Tuple_impl<0ul,
> Mgr*, std::default_delete<Mgr> > -> std::_Head_base<0ul, Mgr*, false>)
>
> I have found no way to resolve this ambiguity to GDB's satisfaction.

The new GDB is correct, see
https://sourceware.org/bugzilla/show_bug.cgi?id=28480

To make it work you need to disambiguate the member access by
qualifying it with the class you want to access, but you shouldn't
need to so I'm not going to waste time figuring out the right voodoo.

>
> The only thing I've found that works is just to access the pointer
> value directly by cutting and pasting it with a cast:
>
>   (gdb) p *((Mgr*)0x7f519a24e000)
>   $8 = {
>     ...
>     initialized = true
>   }
>
>   (gdb) p ((Mgr*)0x7f519a24e000)->initialized
>   $9 = true
>
> Is that really what we have to do?

No, your Xmethods aren't working.

This works perfectly for me:

#include <memory>

struct Mgr
{
  bool initialized = true;
};

struct X
{
  std::unique_ptr<Mgr> mgr;
};

int main()
{
  X x;
  X* p = &x;
  x.mgr.reset(new Mgr);
  return 0; // line 18
}

(gdb) file a.out
Reading symbols from a.out...
(gdb) br 18
Breakpoint 1 at 0x401193: file up.C, line 18.
(gdb) r
Starting program: /tmp/a.out

Breakpoint 1, main () at up.C:18
18        return 0;
(gdb) set $mp = p
(gdb) p *$mp->mgr
$1 = {initialized = true}
(gdb) p $mp->mgr->initialized
$2 = true



> Secondly, is there some interface that is defined by the libstdcxx.v6
> Python macros for GDB that people who are doing their own python

(Aside: There are no macros involved in any of this)

> scripting for their own C++ programs can take advantage of to avoid too
> much groveling through the depths of the C++ STL implementation?

No. What sort of thing do you have in mind?



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux