Given the address of an object, how would I call O

2019-08-13 06:40发布

问题:

In pseudo-code, I'd like to create a reusable LLDB Python script that works like this:

get_super_class(int address) {
    return ((id)address).getSuperClass();
}

Called like this:

(lldb) get_super_class(0x123abc)
UIViewController

Any ideas? I'm a python novice. I would think there is something in the documentation for SBValue that would get me close to what I want, but I don't see it if there is.

回答1:

I am not at my computer (actually at a restaurant in Reno :) so what follows is mostly cursory but it should point you in the right direction

What you want to do is compose the expression as a string. Say you are looking to do foo(X) where foo is defined as

id foo(id arg)

What you can do is make yourself a string

"(id)foo((id)0xaddr"

Then you want to evaluate that as an expression.

I believe either SBTarget or SBFrame have an EvaluateExpression method To that you pass your dutifully crafted string, and remember to enable dynamic types since you are dealing with ObjC and most likely want them to be resolved.

What that API will return is yet another SBValue. That you can query for value, summary, description, or you can fetch its children value etc etc..

Hope this helps. If you need more details feel free to ask and I will try and fill in the gaps as I get a chance!



回答2:

I don't know how to use SBValue for that, but you can use SBCommandInterpreter to run a command with lldb and capture it's output:

# First, get the command interpreter:
ci = lldb.debugger.GetCommandInterpreter()
# Then create an `SBCommandReturnObject` to capture the output
output = lldb.SBCommandReturnObject()
# run an lldb command to get the superclass
ci.HandleCommand("po [" + address + " superclass]", output)
# get the output and strip the trailing line break
superclass = output.GetOutput().rstrip()

There might be an easier way (one would hope), but I couldn't find any.