Using LLDB Commands in Python Script
Question: I'm writing a Python script to use in Xcode's LLDB. I have this simple script
up and running:

    
    
    import lldb
    
    def say_hello(debugger, command, result, dict):
      print command
    
    def __lldb_init_module (debugger, dict):
      debugger.HandleCommand('command script add -f sayhello.say_hello hello')
    

What I'd like to do is be able to use the output of LLDB's
XCUIApplication().debugDescription function in the Python script. So is there
a way to either:

a) Access XCUIApplication() within the python script.

b) Pass the XCUIApplication().debugDescription as an input to the say_hello
function in the Python script.


Answer: IIRC XCUIApplication is a function provided by the XCTest framework, so it is
a function in the program you are debugging. So you would call it the same way
you call any other function, using the "EvaluateExpression" API either on
SBTarget or on SBFrame. The result of evaluating the expression will be
returned to you in an SBValue, and you can print that or whatever you need
with it.

Note, unless you need to support a very old Xcode (6.x) it is more convenient
to use the new form of the python command:

    
    
    def command_function(debugger, command, exe_ctx, result, internal_dict):
    

The exe_ctx is the SBExecutionContext in which the command is running. If you
do it this way, then you can just do:

    
    
    def command_function(debugger, command, exe_ctx, result, internal_dict):
        options = lldb.SBExpressionOptions()
        thread = exe_ctx.GetThread()
        if thread.IsValid():
            value = thread.GetFrameAtIndex(0).EvaluateExpression("XCUIApplication().debugDescription", options)
            if value.GetError().Success():
                # Do whatever you want with the result of the expression
    

