gdb python : How to iterate through a kernel linke

2019-08-17 00:13发布

问题:

using gdb-python script, i am trying to print data structure which includes kernel data structures and lists(e.g. struct list_head), the structure is

struct my_struct {
  struct my_hardware_context ahw;
  struct net_device *netdev;
  struct pci_dev *pdev;
  struct list_head mac_list;
  struct list_head wait_list;
  ....
  ....
};

So while iterating this struct my_struct, how to identify there is a linked list inside this structure as There is no any TYPE_CODE_ constant for Linked list in gdb manual and if identified, after identification how to print the dereferenced struct while iterating the list. i am using code of scottt in this link gdb-python : Parsing structure's each field and print them with proper value, if exists

回答1:

Only you, the programmer, know that this is a linked list. Even the C compiler does not, so therefore there is no way for gdb to know.

You can write a pretty-printer that treats these fields as linked lists. The simplest way is to just code this knowledge into your printer. That is, have the printer's "children" method iterate over the linked list.

There may be other ways, for example creating a separate pretty-printer for the list_head type.