Aurélien Gâteau

Gdb trick: the poor man loop

written on Thursday, April 17, 2008

Suppose you are in the middle of a gdb session, and need to print the content of an array or a class implementing operator[]. Here is a simple way to do that which I just discovered:

(gdb) set $pos=0

(gdb) print array[$pos++]

content of array[0]

(gdb)

content of array[1]

(gdb)

content of array[2]

...

The blank "(gdb)" lines mean i just pressed "return". In this case, gdb executes the previous command again, printing array and incrementing $pos one more time.

Hope this helps, and feel free to comment on any better way to achieve this!

This post was tagged gdb and tips