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!
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);
It's just a missing feature that nobody has implemented yet. You might see if it is in bugzilla, and, if not, file a bug.
A typical workaround is to substitute the "this" argument's value into a string and make the call via gdb.parse_and_eval. This usually works but is, of course, distinctly second-best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With