问题
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!
回答1:
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);
回答2:
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.
来源:https://stackoverflow.com/questions/22774067/gdb-python-api-is-it-possible-to-make-a-call-to-a-class-struct-method