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

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.


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);

Method call is not (yet?) supported by GDB. https://gdb.sourceware.narkive.com/KVTUIAvl/c-method-call-for-a-gdb-value-object-from-within-python

However function call is supported. If you have opportunity to make a function wrapper which will call your method:

void func(mystruct& arg) { arg.method(); }

Then you can find this func via global gdb search, and call:

sym = gdb.lookup_global_symbol('namespace::func(mystruct&)')
result = sym.value()(this.val)

This method does not require taking address of the variable, which fails 50% of the time due to compiler optimizations.