Sharath
Sharath

Reputation: 33

Traversing the linked list using pretty printers in GDB

I have a linked list pretty printer which takes the input from command prompt. E.g., print xyz

My code is something like below:

class Randomcalss:
    def __init__(self, val):
        self.val = int(val)

    def to_string(self):
        return "printing linked list:"

    def children(self):
        for field in self.val.type.fields():
            key = field.name
            val = self.val[key]
            yield key,val.dereference()

It does work as expected, and prints:

 printing linked list:= {head = {next = 0x625590, prev = 0x623c70}}

But if I want to traverse the linked list and proceed further what do I do. Because every time I try to access head['next'] it says head is a string and string indices must be integers.

Cant I do something like self.val[key] to access next node of head too?

Upvotes: 1

Views: 1240

Answers (1)

Anamika Nupur
Anamika Nupur

Reputation: 46

You can do val.dereference()['next'] and this will give you address of next member of the list. You can cast the value obtained(if required) and traverse further.

Upvotes: 2

Related Questions