0

I'm using ios-deploy to launch ios app automatically, it works fine but only a probem: it won't restart the app if it's already running.

I have studied its source code and learned it's using the lldb command to launch the app. the lldb script is (part):

def run_command(debugger, command, result, internal_dict):
    device_app = internal_dict['fruitstrap_device_app']
    args = command.split('--',1)
    error = lldb.SBError()
    lldb.target.modules[0].SetPlatformFileSpec(lldb.SBFileSpec(device_app))
    args_arr = []
    if len(args) > 1:
        args_arr = shlex.split(args[1])
    args_arr = args_arr + shlex.split('{args}')

    launchInfo = lldb.SBLaunchInfo(args_arr)
    global listener
    launchInfo.SetListener(listener)

    #This env variable makes NSLog, CFLog and os_log messages get mirrored to stderr
    #https://stackoverflow.com/a/39581193 
    launchInfo.SetEnvironmentEntries(['OS_ACTIVITY_DT_MODE=enable'], True)

    lldb.target.Launch(launchInfo, error)
    lockedstr = ': Locked'
    if lockedstr in str(error):
       print('\\nDevice Locked\\n')
       os._exit(254)
    else:
       print(str(error))

the start command:

(lldb) command source -s 0 '/tmp/BB1ED2A3-3A3E-413A-935D-323D7A7533D1/fruitstrap-lldb-prep-cmds-6a050aabefc708cb7fc6024c4dd1743080d6e20b' Executing commands in '/tmp/BB1ED2A3-3A3E-413A-935D-323D7A7533D1/fruitstrap-lldb-prep-cmds-6a050aabefc708cb7fc6024c4dd1743080d6e20b'. (lldb) platform select remote-ios --sysroot '/Users/wellbye/Library/Developer/Xcode/iOS DeviceSupport/12.0 (16A366)/Symbols' Platform: remote-ios Connected: no SDK Path: "/Users/wellbye/Library/Developer/Xcode/iOS DeviceSupport/12.0 (16A366)/Symbols" (lldb) target create "/Users/wellbye/git-repo/j/mj3d/Product/build/ios/Build/Products/Release-iphoneos/mj.app" Current executable set to '/Users/wellbye/git-repo/j/mj3d/Product/build/ios/Build/Products/Release-iphoneos/mj.app' (arm64). (lldb) script fruitstrap_device_app="/private/var/containers/Bundle/Application/1FB0E7E3-6616-4789-8E6F-598C4F5AAC35/mj.app" (lldb) script fruitstrap_connect_url="connect://127.0.0.1:62276"
(lldb) target modules search-paths add /usr "/Users/wellbye/Library/Developer/Xcode/iOS DeviceSupport/12.0 (16A366)/Symbols/usr" /System "/Users/wellbye/Library/Developer/Xcode/iOS DeviceSupport/12.0 (16A366)/Symbols/System" "/private/var/containers/Bundle/Application/1FB0E7E3-6616-4789-8E6F-598C4F5AAC35" "/Users/wellbye/git-repo/j/mj3d/Product/build/ios/Build/Products/Release-iphoneos" "/var/containers/Bundle/Application/1FB0E7E3-6616-4789-8E6F-598C4F5AAC35" "/Users/wellbye/git-repo/j/mj3d/Product/build/ios/Build/Products/Release-iphoneos" /Developer "/Users/wellbye/Library/Developer/Xcode/iOS DeviceSupport/12.0 (16A366)/Symbols/Developer" (lldb) command script import "/tmp/BB1ED2A3-3A3E-413A-935D-323D7A7533D1/fruitstrap_6a050aabefc708cb7fc6024c4dd1743080d6e20b.py" (lldb) command script add -f fruitstrap_6a050aabefc708cb7fc6024c4dd1743080d6e20b.connect_command connect (lldb) command script add -s asynchronous -f fruitstrap_6a050aabefc708cb7fc6024c4dd1743080d6e20b.run_command run
(lldb) command script add -s asynchronous -f fruitstrap_6a050aabefc708cb7fc6024c4dd1743080d6e20b.autoexit_command autoexit (lldb) command script add -s asynchronous -f fruitstrap_6a050aabefc708cb7fc6024c4dd1743080d6e20b.safequit_command safequit (lldb) connect (lldb) run

I have searched the lldb's python api reference, but haven't seen anything (args or flags) I could use for my purpose.

so how could we let the lldb server know it should kill the exist process and start a new one?

fatfatson
  • 796
  • 10
  • 24

1 Answers1

2

It depends on whether you are trying to support rerun behavior (i.e. you make a target, launch the process, then use the same target to re-run) or if you just want to kill off some instance of the app that was running - maybe because it was finger-launched on the device or whatever.

In the first case, since you are reusing the SBTarget, you can just check whether your target has a process (call target.process.IsValid()) and if does kill it with target.process.Kill() before launching.

But if lldb is not responsible for launching the extant copy of the app, then it won't know anything about it, and doesn't really have a way to kill it off.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • I have add the checking and killing code before launching, but after killing, the lldb says:`Process 0 exited with status = 9 (0x00000009) error: the platform is not currently connected` and doesn't launch the new process. – fatfatson Nov 16 '18 at 19:54
  • even the app is not running, `lldb.target.process.IsValid` return true, print the process shows `SBProcess: pid = 0, state = connected, threads = 0, executable = mj` – fatfatson Nov 16 '18 at 20:03
  • An exited process stays around (so you could check the exit status for instance.) But process.state will be lldb.eStateExited. You don't need to kill those... – Jim Ingham Nov 16 '18 at 20:07
  • I don't know anything about how ios-deploy works, however, so I can't help with how you would make a new connection to the device if you really did have to kill off the current process. – Jim Ingham Nov 16 '18 at 20:08