在 gdb 中打印整个链表?

2022-01-20 00:00:00 debugging data-structures ubuntu gdb c++

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?

解决方案

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

相关文章