Axel Borja
Axel Borja

Reputation: 3974

How to print content of a list of pointers in gdb?

I have the following list of pointers:

(here is a simple example, but in reality, my list could be componed by hundreds of entries)

{0xae5c4e8, 0xa09d4e8, 0xa753458,  0xae554e8}

I successfully print pointers content, one by one, by using :

p *(const Point *) 0xae5c4e8

How can I print contents of the preceding list in one command ?

Upvotes: 3

Views: 6895

Answers (3)

Tom Tromey
Tom Tromey

Reputation: 22549

There is no canned way to do this. It would be cool if you could type print *(*p @ 23) -- using the @ extension inside another expression, resulting in an implicit loop -- but you can't.

However, there are two decent ways to do this.

One way to do it is to use Python. Something like:

(gdb) python x = gdb.parse_and_eval('my_array')
(gdb) python
    >   for i in range(nnn):
    >     print x[i].dereference()
    > end

You can wrap this in a new gdb command, written in Python, pretty easily.

Another way is to use define to make your own command using the gdb command language. This is a bit uglier, and has some (minor) limitations compared to the Python approach, but it is still doable.

Finally, once upon a time there was a gdb extension called "duel" that provided this feature. Unfortunately it was never merged in.

Upvotes: 5

Employed Russian
Employed Russian

Reputation: 213754

I have the following list of pointers:

You don't appear to have a list, you appear to have an array. Let's assume that it looks something like:

void *array[10] = {0xae5c4e8, 0xa09d4e8, 0xa753458,  0xae554e8};

You can print the first 4 dereferenced elements of that array like so:

(gdb) print ((Point**)array)[0]@4

Upvotes: 0

W.F.
W.F.

Reputation: 13988

I don't think there is a simple way to show all the list elements at once. You could try to iterate through the items using:

set $it=mylist.begin()._M_node
print *(*(std::_List_node<const Point *>*)$it)._M_data
set $it=(*$it)._M_next
...

In the batch way. The problem is that the list items do not need to be located near to each other in the memory. To have better preview option in debug you could switch to the std::vector.

Hope this will help.

Upvotes: 1

Related Questions