Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the whole linked list in gdb?

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?

like image 498
Timothy Leung Avatar asked May 10 '13 10:05

Timothy Leung


2 Answers

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

like image 126
Employed Russian Avatar answered Sep 20 '22 18:09

Employed Russian


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.

like image 26
n. 1.8e9-where's-my-share m. Avatar answered Sep 21 '22 18:09

n. 1.8e9-where's-my-share m.