[RFC 0/2] Using pretty-printers for [] indexing

Simon Marchi simon.marchi@polymtl.ca
Thu Jun 17 18:37:38 GMT 2021


On 2021-05-13 6:35 a.m., Andrew Burgess wrote:
> Ping!
> 
> Any thoughts?
> 
> thanks,
> Andrew

Hi Andrew,

I am writing a pretty-printer for a C++ intrusive list type that I plan
on contributing to GDB in an upcoming series (initially written by
Pedro).  I think that this patch of yours would be very helpful, let me
explain why.

To use the list, you put an intrusive_list member in the type you want
to link:

    struct item
    {
      item (int x) : x (x) {}

      int x;
      intrusive_list_node<item> node;
    };

That intrusive_list_node simply contains next and prev pointers:

    template<typename T>
    struct intrusive_list_node
    {
      intrusive_list_node<T> *next = nullptr;
      intrusive_list_node<T> *prev = nullptr;
    };

The problem is that inspecting this list in GDB, you can easily get
pointers to intrusive_list_node objects, but not to the enclosing object
(the struct item in my example):

    (gdb) p list.m_head.next 
    $7 = (intrusive_list_node<item> *) 0x7fffffffdd48
    (gdb) p list.m_head.next.next
    $8 = (intrusive_list_node<item> *) 0x7fffffffdd30
    (gdb) p list.m_head.next.next.next
    $9 = (intrusive_list_node<item> *) 0x7fffffffdd18

I wrote a pretty printer for the list type:

    (gdb) p list
    $11 = intrusive list of item, linked through &item::node = {{
        x = 1,
        node = {
          next = 0x7fffffffdd30,
          prev = 0x7fffffffdd58
        }
      }, {
        x = 2,
        node = {
          next = 0x7fffffffdd18,
          prev = 0x7fffffffdd48
        }
      }, {
        x = 3,
        node = {
          next = 0x7fffffffdd58,
          prev = 0x7fffffffdd30
        }
      }}

Your patch makes it possible to get list items by indexing, which yields
a struct item directly:

    (gdb) p list[1]
    $12 = {
      x = 2,
      node = {
        next = 0x7fffffffdd18,
        prev = 0x7fffffffdd48
      }
    }
    (gdb) p list[1].x
    $13 = 2

Sooo yeah, I'd like to see this patch merged eventually.

The only problem that I don't think I'll be able to solve (but that's
not related to your patch) is if you just have a pointer to a struct
item and would like to find the next item:

    (gdb) p $14
    $16 = (item *) 0x7fffffffdd28
    (gdb) p $14->node.next
    $17 = (intrusive_list_node<item> *) 0x7fffffffdd18

I don't think there's an easy way to get back to the enclosing item, as
the type intrusive_list_node<item> by itself doesn't have a clue of
what it is embedded into.

Simon


More information about the Gdb-patches mailing list