Skip to content

feat: implement the stdout and stderr redirect to file #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ If you are *not* using a node version manager like [nvm](https://github.com/crea
-e, --exists check if the app with given bundle_id is installed or not
-B, --list_bundle_id list bundle_id
-W, --no-wifi ignore wifi devices
-O, --output <file> write stdout and stderr to this file
--detect_deadlocks <sec> start printing backtraces for all threads periodically after specific amount of seconds

## Examples
Expand Down
31 changes: 30 additions & 1 deletion src/ios-deploy/ios-deploy.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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_output_path=\"{output_path}\"\n\
script fruitstrap_error_path=\"{error_path}\"\n\
target modules search-paths add {modules_search_paths_pairs}\n\
command script import \"{python_file_path}\"\n\
command script add -f {python_command}.connect_command connect\n\
Expand Down Expand Up @@ -66,6 +68,8 @@
#include "lldb.py.h"
;

const char* output_path = NULL;
const char* error_path = NULL;

typedef struct am_device * AMDeviceRef;
mach_error_t AMDeviceSecureStartService(AMDeviceRef device, CFStringRef service_name, unsigned int *unknown, ServiceConnRef * handle);
Expand Down Expand Up @@ -730,6 +734,21 @@ void write_lldb_prep_cmds(AMDeviceRef device, CFURLRef disk_app_url) {
CFStringFindAndReplace(cmds, CFSTR("{device_port}"), device_port, range, 0);
range.length = CFStringGetLength(cmds);

if (output_path) {
CFStringRef output_path_str = CFStringCreateWithFormat(NULL, NULL, CFSTR("%s"), output_path);
CFStringFindAndReplace(cmds, CFSTR("{output_path}"), output_path_str, range, 0);
} else {
CFStringFindAndReplace(cmds, CFSTR("{output_path}"), CFSTR(""), range, 0);
}
range.length = CFStringGetLength(cmds);
if (error_path) {
CFStringRef error_path_str = CFStringCreateWithFormat(NULL, NULL, CFSTR("%s"), error_path);
CFStringFindAndReplace(cmds, CFSTR("{error_path}"), error_path_str, range, 0);
} else {
CFStringFindAndReplace(cmds, CFSTR("{error_path}"), CFSTR(""), range, 0);
}
range.length = CFStringGetLength(cmds);

CFURLRef device_container_url = CFURLCreateCopyDeletingLastPathComponent(NULL, device_app_url);
CFStringRef device_container_path = CFURLCopyFileSystemPath(device_container_url, kCFURLPOSIXPathStyle);
CFMutableStringRef dcp_noprivate = CFStringCreateMutableCopy(NULL, 0, device_container_path);
Expand Down Expand Up @@ -1777,6 +1796,8 @@ void usage(const char* app) {
@" -e, --exists check if the app with given bundle_id is installed or not \n"
@" -B, --list_bundle_id list bundle_id \n"
@" -W, --no-wifi ignore wifi devices\n"
@" -O, --output <file> write stdout to this file\n"
@" -E, --error_output <file> write stderr to this file\n"
@" --detect_deadlocks <sec> start printing backtraces for all threads periodically after specific amount of seconds\n"
@" -j, --json format output as JSON\n",
[NSString stringWithUTF8String:app]);
Expand Down Expand Up @@ -1825,13 +1846,15 @@ int main(int argc, char *argv[]) {
{ "exists", no_argument, NULL, 'e'},
{ "list_bundle_id", no_argument, NULL, 'B'},
{ "no-wifi", no_argument, NULL, 'W'},
{ "output", required_argument, NULL, 'O' },
{ "error_output", required_argument, NULL, 'E' },
{ "detect_deadlocks", required_argument, NULL, 1000 },
{ "json", no_argument, NULL, 'j'},
{ NULL, 0, NULL, 0 },
};
int ch;

while ((ch = getopt_long(argc, argv, "VmcdvunrILeD:R:i:b:a:t:p:1:2:o:l:w:9BWjNs:", longopts, NULL)) != -1)
while ((ch = getopt_long(argc, argv, "VmcdvunrILeD:R:i:b:a:t:p:1:2:o:l:w:9BWjNs:OE:", longopts, NULL)) != -1)
{
switch (ch) {
case 'm':
Expand Down Expand Up @@ -1937,6 +1960,12 @@ int main(int argc, char *argv[]) {
case 'W':
no_wifi = true;
break;
case 'O':
output_path = optarg;
break;
case 'E':
error_path = optarg;
break;
case 1000:
_detectDeadlockTimeout = atoi(optarg);
break;
Expand Down
30 changes: 28 additions & 2 deletions src/scripts/lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def autoexit_command(debugger, command, result, internal_dict):
global listener
process = lldb.target.process

output_path = internal_dict['fruitstrap_output_path']
out = None
if output_path:
out = open(output_path, 'w')

error_path = internal_dict['fruitstrap_error_path']
err = None
if error_path:
err = open(error_path, 'w')

detectDeadlockTimeout = {detect_deadlock_timeout}
printBacktraceTime = time.time() + detectDeadlockTimeout if detectDeadlockTimeout > 0 else None

Expand All @@ -97,14 +107,26 @@ def autoexit_command(debugger, command, result, internal_dict):
def ProcessSTDOUT():
stdout = process.GetSTDOUT(1024)
while stdout:
sys.stdout.write(stdout)
if out:
out.write(stdout)
else:
sys.stdout.write(stdout)
stdout = process.GetSTDOUT(1024)

def ProcessSTDERR():
stderr = process.GetSTDERR(1024)
while stderr:
sys.stdout.write(stderr)
if err:
err.write(stderr)
else:
sys.stdout.write(stderr)
stderr = process.GetSTDERR(1024)

def CloseOut():
if (out):
out.close()
if (err):
err.close()

while True:
if listener.WaitForEvent(1, event) and lldb.SBProcess.EventIsProcessEvent(event):
Expand All @@ -127,17 +149,21 @@ def ProcessSTDERR():

if state == lldb.eStateExited:
sys.stdout.write( '\\nPROCESS_EXITED\\n' )
CloseOut()
os._exit(process.GetExitStatus())
elif printBacktraceTime is None and state == lldb.eStateStopped:
sys.stdout.write( '\\nPROCESS_STOPPED\\n' )
debugger.HandleCommand('bt')
CloseOut()
os._exit({exitcode_app_crash})
elif state == lldb.eStateCrashed:
sys.stdout.write( '\\nPROCESS_CRASHED\\n' )
debugger.HandleCommand('bt')
CloseOut()
os._exit({exitcode_app_crash})
elif state == lldb.eStateDetached:
sys.stdout.write( '\\nPROCESS_DETACHED\\n' )
CloseOut()
os._exit({exitcode_app_crash})
elif printBacktraceTime is not None and time.time() >= printBacktraceTime:
printBacktraceTime = None
Expand Down