public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Paul Smith <paul@mad-scientist.net>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Help using the GDB C++ STL pretty-printers / xmethods
Date: Sat, 7 May 2022 16:25:43 +0100	[thread overview]
Message-ID: <CAH6eHdTw0vJ_RNjysQ2bUr8bj9-2MHtwZ0mq11miNu4z8WX-jw@mail.gmail.com> (raw)
In-Reply-To: <5568db74d0acb198a3e8121ee75e3cfa02ea0c6f.camel@mad-scientist.net>

On Sat, 7 May 2022 at 02:24, Paul Smith <paul@mad-scientist.net> 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?

      parent reply	other threads:[~2022-05-07 15:25 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-07  1:23 Paul Smith
2022-05-07 11:19 ` Hannes Domani
2022-05-07 15:07   ` Paul Smith
2022-05-07 15:35     ` Jonathan Wakely
2022-05-07 19:07       ` Paul Smith
2022-05-07 19:51         ` Jonathan Wakely
2022-05-07 23:08           ` Paul Smith
2022-05-08  8:13             ` Jonathan Wakely
2022-05-08  8:16               ` Jonathan Wakely
2022-05-08 14:09                 ` Paul Smith
2022-05-08 14:36                   ` Jonathan Wakely
2022-05-08 19:44                 ` Paul Smith
2022-05-08 20:26                   ` Paul Smith
2022-05-09 10:47                     ` Hannes Domani
2022-05-09 10:52                       ` Hannes Domani
2022-05-09  9:32                   ` Jonathan Wakely
2022-05-09 11:23                     ` Jonathan Wakely
2022-05-09 14:05                       ` Paul Smith
2022-05-09 14:40                         ` Paul Smith
2022-05-07 15:44     ` Hannes Domani
2022-05-07 15:25 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH6eHdTw0vJ_RNjysQ2bUr8bj9-2MHtwZ0mq11miNu4z8WX-jw@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=paul@mad-scientist.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).