Print the whole linked list in gdb?

2020-06-12 03:07发布

I have a linked list

struct node {
    data_t data;
    node_t *next;
};

typedef struct {
    node_t *head;
    node_t *foot;
    node_t *curr;   // for iterator
    unsigned int size;
} list_t;

with this structure, let say I defined a list

list_t* myList;

How can I use GDB to print the whole linked list?

2条回答
SAY GOODBYE
2楼-- · 2020-06-12 03:40

GDB is scriptable in Python. You can define your own pretty-printers and do other useful things.

Better yet, use a standard container, GDB now supports printing those natively.

查看更多
时光不老,我们不散
3楼-- · 2020-06-12 03:52

This should work (but untested):

define plist
  set var $n = $arg0->head
  while $n
    printf "%d ", $n->data
    set var $n = $n->next
  end
end

(gdb) plist myList

You could put plist into ~/.gdbinit

查看更多
登录 后发表回答