Search code examples
pythongdbpretty-printgdb-python

gdb python api: is it possible to make a call to a class/struct method


In Python, I have a a variable var of type gdb.Value that corresponds to a C++ struct.

The struct has a method void foo().

I can evaluate this expression var['foo']. But var['foo']\() will complain saying

RuntimeError: Value is not callable (not TYPE_CODE_FUNC)

I believe the value type will be gdb.TYPE_CODE_METHOD (not sure, but var['foo'].type.code returns 16) in my case.

So I guess the question is:

Does python API support calls to class methods, and if not, is there a workaround?

Thanks!


Solution

  • OK, I think I was able to do what I want using the Tom's advice and another workaround.

    The problem I need an extra workaround was (as I mentioned in the comment above) that I didn't have the variable name in order to compose a string of form: myval.method() to pass to gdb.parse_and_eval.

    So the workaround for that one is to get the address of the variable and then cast it to the type and then add a method call to the string.

    Both type and address exist in python api for gdb.Value. So the solution looks like the following:

        eval_string = "(*("+str(self.val.type)+"*)("+str(self.val.address)+")).method()"
        return gdb.parse_and_eval(eval_string);