Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adds better option handling and debug support

adds gdb launching, attached to session pid. It now uses NSUserDefaults
to handle the options.
  • Loading branch information...
commit 1a6cc0f8c3ce4f4f104632eb2f67596efc2a1768 1 parent 6a1f65f
Rolando Abarca authored
Showing with 128 additions and 110 deletions.
  1. +2 −1  Source/iPhoneSimulator.h
  2. +126 −109 Source/iPhoneSimulator.m
View
3  Source/iPhoneSimulator.h
@@ -35,7 +35,8 @@
@interface iPhoneSimulator : NSObject <DTiPhoneSimulatorSessionDelegate> {
@private
- DTiPhoneSimulatorSystemRoot *sdkRoot;
+ DTiPhoneSimulatorSystemRoot *sdkRoot;
+ BOOL debug;
}
- (void) runWithArgc: (int) argc argv: (char **) argv;
View
235 Source/iPhoneSimulator.m
@@ -42,10 +42,17 @@ @implementation iPhoneSimulator
* Print usage.
*/
- (void) printUsage {
- fprintf(stderr, "Usage: iphonesim <options> <command> ...\n");
- fprintf(stderr, "Commands:\n");
- fprintf(stderr, " showsdks\n");
- fprintf(stderr, " launch <application path> [sdkversion] [family] [uuid]\n");
+ fprintf(stderr, "Usage: iphonesim <options> <command> ...\n");
+ fprintf(stderr, "Commands:\n");
+ fprintf(stderr, " showsdks\n");
+ fprintf(stderr, " launch <application path>\n");
+ fprintf(stderr, "Options:\n");
+ fprintf(stderr, " -sdkVersion <sdk version>\n");
+ fprintf(stderr, " -family <family>\n");
+ fprintf(stderr, " -uuid <uuid>\n");
+ fprintf(stderr, " -stderr <stderr redirection>\n");
+ fprintf(stderr, " -stdout <stdout redirection>\n");
+ fprintf(stderr, " -debug <YES|NO>\n");
}
@@ -53,68 +60,72 @@ - (void) printUsage {
* List available SDK roots.
*/
- (int) showSDKs {
- NSArray *roots = [DTiPhoneSimulatorSystemRoot knownRoots];
+ NSArray *roots = [DTiPhoneSimulatorSystemRoot knownRoots];
- nsprintf(@"Simulator SDK Roots:");
- for (DTiPhoneSimulatorSystemRoot *root in roots) {
- nsfprintf(stderr, @"'%@' (%@)\n\t%@", [root sdkDisplayName], [root sdkVersion], [root sdkRootPath]);
- }
+ nsprintf(@"Simulator SDK Roots:");
+ for (DTiPhoneSimulatorSystemRoot *root in roots) {
+ nsfprintf(stderr, @"'%@' (%@)\n\t%@", [root sdkDisplayName], [root sdkVersion], [root sdkRootPath]);
+ }
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
-- (void) session: (DTiPhoneSimulatorSession *) session didEndWithError: (NSError *) error {
- nsprintf(@"Session did end with error %@", error);
-
- if (error != nil)
- exit(EXIT_FAILURE);
+- (void)session:(DTiPhoneSimulatorSession *)session didEndWithError:(NSError *)error {
+ nsprintf(@"Session did end with error %@", error);
+
+ if (error != nil)
+ exit(EXIT_FAILURE);
- exit(EXIT_SUCCESS);
+ exit(EXIT_SUCCESS);
}
-- (void) session: (DTiPhoneSimulatorSession *) session didStart: (BOOL) started withError: (NSError *) error {
- if (started) {
- nsprintf(@"Session started");
- } else {
- nsprintf(@"Session could not be started: %@", error);
- exit(EXIT_FAILURE);
- }
+- (void)session:(DTiPhoneSimulatorSession *)session didStart:(BOOL)started withError:(NSError *)error {
+ if (started) {
+ nsprintf(@"Session started with pid %@", [session simulatedApplicationPID]);
+ if (debug) {
+ // launch gdb (and continue execution) maybe we should fork and exec?
+ system([[NSString stringWithFormat:@"echo c | /usr/bin/gdb --pid %@", [session simulatedApplicationPID]] UTF8String]);
+ }
+ } else {
+ nsprintf(@"Session could not be started: %@", error);
+ exit(EXIT_FAILURE);
+ }
}
/**
* Launch the given Simulator binary.
*/
-- (int) launchApp: (NSString *) path withFamily:(NSString*)family uuid:(NSString*)uuid{
- DTiPhoneSimulatorApplicationSpecifier *appSpec;
- DTiPhoneSimulatorSessionConfig *config;
- DTiPhoneSimulatorSession *session;
- NSError *error;
+- (int)launchApp:(NSString *)path withFamily:(NSString*)family uuid:(NSString*)uuid{
+ DTiPhoneSimulatorApplicationSpecifier *appSpec;
+ DTiPhoneSimulatorSessionConfig *config;
+ DTiPhoneSimulatorSession *session;
+ NSError *error;
char *tmp;
- /* Create the app specifier */
- appSpec = [DTiPhoneSimulatorApplicationSpecifier specifierWithApplicationPath: path];
- if (appSpec == nil) {
- nsprintf(@"Could not load application specification for %s", path);
- return EXIT_FAILURE;
- }
- nsprintf(@"App Spec: %@", appSpec);
+ /* Create the app specifier */
+ appSpec = [DTiPhoneSimulatorApplicationSpecifier specifierWithApplicationPath:path];
+ if (appSpec == nil) {
+ nsprintf(@"Could not load application specification for %@", path);
+ return EXIT_FAILURE;
+ }
+ nsprintf(@"App Spec: %@", appSpec);
- /* Load the default SDK root */
-
- nsprintf(@"SDK Root: %@", sdkRoot);
+ /* Load the default SDK root */
+
+ nsprintf(@"SDK Root: %@", sdkRoot);
- /* Set up the session configuration */
- config = [[[DTiPhoneSimulatorSessionConfig alloc] init] autorelease];
- [config setApplicationToSimulateOnStart: appSpec];
- [config setSimulatedSystemRoot: sdkRoot];
- [config setSimulatedApplicationShouldWaitForDebugger: NO];
+ /* Set up the session configuration */
+ config = [[[DTiPhoneSimulatorSessionConfig alloc] init] autorelease];
+ [config setApplicationToSimulateOnStart:appSpec];
+ [config setSimulatedSystemRoot:sdkRoot];
+ [config setSimulatedApplicationShouldWaitForDebugger:debug];
- [config setSimulatedApplicationLaunchArgs: [NSArray array]];
- [config setSimulatedApplicationLaunchEnvironment: [NSDictionary dictionary]];
+ [config setSimulatedApplicationLaunchArgs:[NSArray array]];
+ [config setSimulatedApplicationLaunchEnvironment:[NSDictionary dictionary]];
- [config setLocalizedClientName: @"TitaniumDeveloper"];
+ [config setLocalizedClientName:@"TitaniumDeveloper"];
/* redirect stderr, maybe this could be in a cli option? */
tmp = getenv("STDERR");
@@ -146,83 +157,89 @@ - (int) launchApp: (NSString *) path withFamily:(NSString*)family uuid:(NSString
}
}
- /* Start the session */
- session = [[[DTiPhoneSimulatorSession alloc] init] autorelease];
- [session setDelegate: self];
- [session setSimulatedApplicationPID: [NSNumber numberWithInt: 35]];
+ /* Start the session */
+ session = [[[DTiPhoneSimulatorSession alloc] init] autorelease];
+ [session setDelegate: self];
+ NSNumber *pid = [NSNumber numberWithInt: 35];
+ [session setSimulatedApplicationPID:pid];
if (uuid!=nil)
{
[session setUuid:uuid];
}
- if (![session requestStartWithConfig: config timeout: 30 error: &error]) {
- nsprintf(@"Could not start simulator session: %@", error);
- return EXIT_FAILURE;
- }
+ if (![session requestStartWithConfig: config timeout: 30 error: &error]) {
+ nsprintf(@"Could not start simulator session: %@", error);
+ return EXIT_FAILURE;
+ }
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
/**
* Execute 'main'
*/
-- (void) runWithArgc: (int) argc argv: (char **) argv {
- /* Read the command */
- if (argc < 2) {
- [self printUsage];
- exit(EXIT_FAILURE);
- }
-
- if (strcmp(argv[1], "showsdks") == 0) {
- exit([self showSDKs]);
- }
- else if (strcmp(argv[1], "launch") == 0) {
- /* Requires an additional argument */
- if (argc < 3) {
- fprintf(stderr, "Missing application path argument\n");
- [self printUsage];
- exit(EXIT_FAILURE);
- }
- if (argc > 3) {
- NSString* ver = [NSString stringWithCString:argv[3] encoding:NSUTF8StringEncoding];
- NSArray *roots = [DTiPhoneSimulatorSystemRoot knownRoots];
- for (DTiPhoneSimulatorSystemRoot *root in roots) {
- NSString *v = [root sdkVersion];
- if ([v isEqualToString:ver])
- {
- sdkRoot = root;
- break;
- }
- }
- if (sdkRoot == nil)
- {
- fprintf(stderr,"Unknown or unsupported SDK version: %s\n",argv[3]);
- [self showSDKs];
- exit(EXIT_FAILURE);
- }
- }
- else {
- sdkRoot = [DTiPhoneSimulatorSystemRoot defaultRoot];
- }
-
- /* Don't exit, adds to runloop */
- NSString *family = nil;
- NSString *uuid = nil;
- if (argc > 4)
- {
- family = [NSString stringWithUTF8String:argv[4]];
+- (void)runWithArgc:(int)argc argv:(char **)argv {
+ // Foundation way to parse args
+ NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
+
+ /* Read the command */
+ char *cmd = nil;
+ int cmdIdx = -1;
+
+ for (int i=1; i < argc; i++) {
+ // first non option argument is the command
+ if (argv[i][0] != '-') {
+ cmd = argv[i];
+ cmdIdx = i;
+ break;
}
- if (argc > 5)
- {
- uuid = [NSString stringWithUTF8String:argv[5]];
+ }
+ if (!cmd) {
+ [self printUsage];
+ exit(EXIT_FAILURE);
+ }
+
+ // debug option (launch gdb)
+ debug = [args boolForKey:@"debug"];
+
+ if (strcmp(cmd, "showsdks") == 0) {
+ exit([self showSDKs]);
+ } else if (strcmp(cmd, "launch") == 0) {
+ /* Requires an additional argument */
+ if (cmdIdx + 1 >= argc) {
+ fprintf(stderr, "Missing application path argument\n");
+ [self printUsage];
+ exit(EXIT_FAILURE);
+ }
+ NSString* ver = [args stringForKey:@"sdkVersion"];
+ if (ver) {
+ NSArray *roots = [DTiPhoneSimulatorSystemRoot knownRoots];
+ for (DTiPhoneSimulatorSystemRoot *root in roots) {
+ NSString *v = [root sdkVersion];
+ if ([v isEqualToString:ver])
+ {
+ sdkRoot = root;
+ break;
+ }
+ }
+ if (sdkRoot == nil)
+ {
+ fprintf(stderr,"Unknown or unsupported SDK version: %s\n",ver);
+ [self showSDKs];
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ sdkRoot = [DTiPhoneSimulatorSystemRoot defaultRoot];
}
- [self launchApp: [NSString stringWithUTF8String: argv[2]] withFamily:family uuid:uuid];
- } else {
- fprintf(stderr, "Unknown command\n");
- [self printUsage];
- exit(EXIT_FAILURE);
- }
+
+ /* Don't exit, adds to runloop */
+ [self launchApp:[NSString stringWithUTF8String:argv[cmdIdx+1]] withFamily:[args stringForKey:@"family"] uuid:[args stringForKey:@"uuid"]];
+ } else {
+ fprintf(stderr, "Unknown command: '%s'\n", cmd);
+ [self printUsage];
+ exit(EXIT_FAILURE);
+ }
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.