Python script stuck when trying to continue process in lldb

I’m trying to do some research on iOS and it involves attaching lldb to a process. I’m able to do it with lldb console, however when I’m trying to convert it to a python script, it stuck at “process continue” for the first time and never reach the commands at the end. Can anyone helps? Thanks.

The code: http://pastebin.com/Yi380xFe

I tried to get the attached process by “process = debugger.GetSelectedTarget().GetProcess()” and call “process.Continue()” but I’m getting the same result.

Jason

I would try using the API a bit more instead of HandleCommand. Your original code:

import lldb
debugger = lldb.SBDebugger.Create()
debugger.SetAsync(False)
debugger.HandleCommand('platform select remote-ios')
debugger.HandleCommand('process connect connect://localhost:1234')
debugger.HandleCommand('process continue')

New code using the API is a much better way:

import lldb
debugger = lldb.SBDebugger.Create()
filename = None # fill this in if you know the local version of the executable file, else leave as None
triple = 'armv7s-apple-ios' # Modify the ARM architecture to match
platform = 'remote-ios'
error = lldb.SBError()
target = CreateTarget (filename, triple, platform, False, error)
if target.IsValid():
    process = target.ConnectRemote(debugger, "connect://localhost:1234", "gdb-remote", error)
    if process.IsValid():
        process.Continue()

Should it be:

target = debugger.CreateTarget (filename, triple, platform, False, error)

I tried that and get error on this line

process = target.ConnectRemote(debugger, “connect://localhost:1234”, “gdb-remote”, error)

File “lldbControl.py”, line 54, in
process = target.ConnectRemote(debugger, “connect://localhost:1234”, “gdb-remote”, error)
File “/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/Python/lldb/init.py”, line 7773, in ConnectRemote
return _lldb.SBTarget_ConnectRemote(self, *args)

This is an iPhone 4 running iOS 7 so I guess it’s armv7, right?

Should it be:

target = debugger.CreateTarget (filename, triple, platform, False, error)

Yes "debugger." is required before CreateTarget

I tried that and get error on this line
    process = target.ConnectRemote(debugger, "connect://localhost:1234", "gdb-remote", error)

This should have been:
    process = target.ConnectRemote(debugger.GetListener(), "connect://localhost:1234", "gdb-remote", error)

File "lldbControl.py", line 54, in <module>
    process = target.ConnectRemote(debugger, "connect://localhost:1234", "gdb-remote", error)
  File "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/Python/lldb/__init__.py", line 7773, in ConnectRemote
    return _lldb.SBTarget_ConnectRemote(self, *args)

This is an iPhone 4 running iOS 7 so I guess it's armv7, right?

I believe so.

It might be a good idea to handle the process events yourself. We have some great sample code:

svn cat http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/process_events.py

This sample code shows you how to run a debug session and actually respond to all the events required to run a session. You should be able to insert your target create + connect code straight into this code and make it your own.

Greg

I’m able to attach to the process now, however when I try to run a simple command and it crashes

if target.IsValid():
process = target.ConnectRemote(debugger.GetListener(), “connect://localhost:1235”, “gdb-remote”, error)
if process.IsValid():
debugger.HandleCommand(‘po [UIApplication sharedApplication]’)
process.Continue()

Crash log:

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000000000f8

VM Regions Near 0xf8:

__TEXT 0000000104926000-0000000104928000 [ 8K] r-x/rwx SM=COW /usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Application Specific Information:
HandleCommand(command = “po [UIApplication sharedApplication]”)

Regarding the stuck script in original post, it seems like debugger.SetAsync(False) make process.Continue() never return, is it a bug? Sorry if I’m asking a lot of simple questions, I just started with this.

No this isn't a bug. When you set async to false, you are requiring your process to run and stop before "process.Continue()" returns. This is why I pointed you to the process_events.py example which properly allows you to wait for events (like "process running", "process stopped") and also timeout waiting for an event and run some commands. Before you run an expression, you will need to make sure your process is up and running. If you have debugserver launching your process for you on the device, then it will be stopped at the entry point. This isn't a great time to be running an expression 'po [UIApplication sharedApplication]' since you haven't loaded _anything_ yet. You will want to let your application run for a while to get into its run loop, then interrupt it. This will ensure that the objective C runtime has loaded and your app is ready to evaluate expressions.

We shouldn't be crashing though no matter what you do.

Greg