From a3a99e6553fda5608cd92cd904c073c64bce11c7 Mon Sep 17 00:00:00 2001 From: Julius Trinkunas Date: Wed, 26 Mar 2014 18:59:01 +0200 Subject: [PATCH 1/3] Remove python wrapper shell. --- ios-deploy.c | 60 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/ios-deploy.c b/ios-deploy.c index 7604a09..8341ac5 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -18,7 +18,7 @@ #define APP_VERSION "1.0.5" #define PREP_CMDS_PATH "/tmp/fruitstrap-lldb-prep-cmds-" -#define LLDB_SHELL "python -u -c $'import time\ntime.sleep(1.0)\n%swhile True: time.sleep(0.1); cmd = raw_input(); print (cmd)' | lldb -s " PREP_CMDS_PATH +#define LLDB_SHELL "lldb -s " PREP_CMDS_PATH /* * Startup script passed to lldb. @@ -31,10 +31,18 @@ target create \"{disk_app}\"\n\ script fruitstrap_device_app=\"{device_app}\"\n\ script fruitstrap_connect_url=\"connect://127.0.0.1:{device_port}\"\n\ - script fruitstrap_handle_command=\"command script add -s asynchronous -f {python_command}.fsrun_command run\"\n\ command script import \"{python_file_path}\"\n\ + command script add -f {python_command}.connect_command connect\n\ + command script add -s asynchronous -f {python_command}.run_command run\n\ + connect\n\ ") +const char* lldb_prep_no_cmds = ""; + +const char* lldb_prep_interactive_cmds = "\ + run\n\ +"; + /* * Some things do not seem to work when using the normal commands like process connect/launch, so we invoke them * through the python interface. Also, Launch () doesn't seem to work when ran from init_module (), so we add @@ -43,19 +51,35 @@ #define LLDB_FRUITSTRAP_MODULE CFSTR("\ import lldb\n\ \n\ -def __lldb_init_module(debugger, internal_dict):\n\ +def connect_command(debugger, command, result, internal_dict):\n\ # These two are passed in by the script which loads us\n\ - device_app=internal_dict['fruitstrap_device_app']\n\ - connect_url=internal_dict['fruitstrap_connect_url']\n\ - handle_command = internal_dict['fruitstrap_handle_command']\n\ - lldb.target.modules[0].SetPlatformFileSpec(lldb.SBFileSpec(device_app))\n\ - lldb.debugger.HandleCommand(handle_command)\n\ - error=lldb.SBError()\n\ - lldb.target.ConnectRemote(lldb.target.GetDebugger().GetListener(),connect_url,None,error)\n\ + connect_url = internal_dict['fruitstrap_connect_url']\n\ + error = lldb.SBError()\n\ \n\ -def fsrun_command(debugger, command, result, internal_dict):\n\ - error=lldb.SBError()\n\ - lldb.target.Launch(lldb.SBLaunchInfo(['{args}']),error)\n\ + process = lldb.target.ConnectRemote(lldb.target.GetDebugger().GetListener(), connect_url, None, error)\n\ +\n\ + # Wait for connection to succeed\n\ + listener = lldb.target.GetDebugger().GetListener()\n\ + listener.StartListeningForEvents(process.GetBroadcaster(), lldb.SBProcess.eBroadcastBitStateChanged)\n\ + events = []\n\ + state = lldb.eStateInvalid\n\ + while state != lldb.eStateConnected:\n\ + event = lldb.SBEvent()\n\ + if listener.WaitForEvent(1, event):\n\ + state = process.GetStateFromEvent(event)\n\ + events.append(event)\n\ + else:\n\ + state = lldb.eStateInvalid\n\ +\n\ + # Add events back to queue, otherwise lldb freezes\n\ + for event in events:\n\ + listener.AddEvent(event)\n\ +\n\ +def run_command(debugger, command, result, internal_dict):\n\ + device_app = internal_dict['fruitstrap_device_app']\n\ + error = lldb.SBError()\n\ + lldb.target.modules[0].SetPlatformFileSpec(lldb.SBFileSpec(device_app))\n\ + lldb.target.Launch(lldb.SBLaunchInfo(['{args}']), error)\n\ print str(error)\n\ ") @@ -488,6 +512,13 @@ void write_lldb_prep_cmds(AMDeviceRef device, CFURLRef disk_app_url) { strcat(prep_cmds_path, device_id); FILE *out = fopen(prep_cmds_path, "w"); fwrite(CFDataGetBytePtr(cmds_data), CFDataGetLength(cmds_data), 1, out); + // Write additional commands based on mode we're running in + const char* extra_cmds; + if (nostart) + extra_cmds = lldb_prep_no_cmds; + else + extra_cmds = lldb_prep_interactive_cmds; + fwrite(extra_cmds, strlen(extra_cmds), 1, out); fclose(out); CFDataRef pmodule_data = CFStringCreateExternalRepresentation(NULL, pmodule, kCFStringEncodingASCII, 0); @@ -668,9 +699,8 @@ void launch_debugger(AMDeviceRef device, CFURLRef url) { parent = getpid(); int pid = fork(); if (pid == 0) { - char *runOption = nostart ? "" : "print \\\'run\\\'\n"; char lldb_shell[400]; - sprintf(lldb_shell, LLDB_SHELL, runOption); + sprintf(lldb_shell, LLDB_SHELL); if(device_id != NULL) strcat(lldb_shell, device_id); From a4f3c2c84be6c573fb1bbbc515de53d3e9b6d5f5 Mon Sep 17 00:00:00 2001 From: Julius Trinkunas Date: Wed, 26 Mar 2014 19:13:48 +0200 Subject: [PATCH 2/3] Add non interactive mode. --- ios-deploy.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ios-deploy.c b/ios-deploy.c index 8341ac5..791c2f6 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -43,6 +43,13 @@ const char* lldb_prep_interactive_cmds = "\ run\n\ "; +const char* lldb_prep_noninteractive_cmds = "\ + settings set -- auto-confirm true\n\ + target stop-hook add --one-liner \"bt\"\n\ + target stop-hook add --one-liner \"quit\"\n\ + run\n\ +"; + /* * Some things do not seem to work when using the normal commands like process connect/launch, so we invoke them * through the python interface. Also, Launch () doesn't seem to work when ran from init_module (), so we add @@ -90,6 +97,7 @@ int AMDeviceMountImage(AMDeviceRef device, CFStringRef image, CFDictionaryRef op mach_error_t AMDeviceLookupApplications(AMDeviceRef device, CFDictionaryRef options, CFDictionaryRef *result); bool found_device = false, debug = false, verbose = false, unbuffered = false, nostart = false, detect_only = false, install = true; +bool interactive = true; char *app_path = NULL; char *device_id = NULL; char *args = NULL; @@ -514,7 +522,9 @@ void write_lldb_prep_cmds(AMDeviceRef device, CFURLRef disk_app_url) { fwrite(CFDataGetBytePtr(cmds_data), CFDataGetLength(cmds_data), 1, out); // Write additional commands based on mode we're running in const char* extra_cmds; - if (nostart) + if (!interactive) + extra_cmds = lldb_prep_noninteractive_cmds; + else if (nostart) extra_cmds = lldb_prep_no_cmds; else extra_cmds = lldb_prep_interactive_cmds; @@ -820,8 +830,9 @@ void usage(const char* app) { " -g, --gdbargs extra arguments to pass to GDB when starting the debugger\n" " -x, --gdbexec GDB commands script file\n" " -n, --nostart do not start the app when debugging\n" + " -I, --noninteractive start in non interactive mode (quit when app crashes or exits)\n" " -v, --verbose enable verbose output\n" - " -m, --noinstall directly start debugging without app install (-d not required) \n" + " -m, --noinstall directly start debugging without app install (-d not required)\n" " -p, --port port used for device, default: 12345 \n" " -V, --version print the executable version \n", app); @@ -842,6 +853,7 @@ int main(int argc, char *argv[]) { { "gdbexec", no_argument, NULL, 'x' }, { "unbuffered", no_argument, NULL, 'u' }, { "nostart", no_argument, NULL, 'n' }, + { "noninteractive", no_argument, NULL, 'I' }, { "detect", no_argument, NULL, 'c' }, { "version", no_argument, NULL, 'V' }, { "noinstall", no_argument, NULL, 'm' }, @@ -850,7 +862,7 @@ int main(int argc, char *argv[]) { }; char ch; - while ((ch = getopt_long(argc, argv, "Vmcdvuni:b:a:t:g:x:p:", longopts, NULL)) != -1) + while ((ch = getopt_long(argc, argv, "VmcdvunIi:b:a:t:g:x:p:", longopts, NULL)) != -1) { switch (ch) { case 'm': @@ -881,6 +893,9 @@ int main(int argc, char *argv[]) { case 'n': nostart = 1; break; + case 'I': + interactive = false; + break; case 'c': detect_only = true; break; From 08f3b92306bdb11cc5cb104c6f9bc9885efd35c6 Mon Sep 17 00:00:00 2001 From: Julius Trinkunas Date: Thu, 27 Mar 2014 16:14:14 +0200 Subject: [PATCH 3/3] Fix hangup when launching from scripts. --- ios-deploy.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ios-deploy.c b/ios-deploy.c index 791c2f6..239c265 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -684,6 +684,16 @@ void lldb_finished_handler(int signum) _exit(0); } +void bring_process_to_foreground() { + if (setpgid(0, 0) == -1) + perror("setpgid failed"); + + signal(SIGTTOU, SIG_IGN); + if (tcsetpgrp(STDIN_FILENO, getpid()) == -1) + perror("tcsetpgrp failed"); + signal(SIGTTOU, SIG_DFL); +} + void launch_debugger(AMDeviceRef device, CFURLRef url) { AMDeviceConnect(device); assert(AMDeviceIsPaired(device)); @@ -709,6 +719,8 @@ void launch_debugger(AMDeviceRef device, CFURLRef url) { parent = getpid(); int pid = fork(); if (pid == 0) { + bring_process_to_foreground(); + char lldb_shell[400]; sprintf(lldb_shell, LLDB_SHELL);