Given the address of an object, how would I call Obj-C methods on it in an LLDB Python script?

烈酒焚心 提交于 2019-12-11 07:24:17

问题


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.



来源:https://stackoverflow.com/questions/20275139/given-the-address-of-an-object-how-would-i-call-obj-c-methods-on-it-in-an-lldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!