ExceptionUtils.m
changeset 27 256370e8935a
parent 16 ce9c83f7ec14
child 33 d52f6b0d94be
     1.1 --- a/ExceptionUtils.m	Sun Jul 13 10:45:42 2008 -0700
     1.2 +++ b/ExceptionUtils.m	Sun May 03 10:13:31 2009 -0700
     1.3 @@ -43,7 +43,7 @@
     1.4  }
     1.5  
     1.6  
     1.7 -@implementation NSException (MooseyardUtil)
     1.8 +@implementation NSException (MYUtilities)
     1.9  
    1.10  
    1.11  - (NSArray*) my_callStackReturnAddresses
    1.12 @@ -70,10 +70,12 @@
    1.13  
    1.14  - (NSString*) my_callStack
    1.15  {
    1.16 -    NSArray *addresses = [self my_callStackReturnAddressesSkipping: 2 limit: 15];
    1.17 +    NSArray *addresses = [self my_callStackReturnAddressesSkipping: 1 limit: 15];
    1.18      if (!addresses)
    1.19          return nil;
    1.20      
    1.21 +    FILE *file = NULL;
    1.22 +#if !TARGET_OS_IPHONE
    1.23      // We pipe the hex return addresses through the 'atos' tool to get symbolic names:
    1.24      // Adapted from <http://paste.lisp.org/display/47196>:
    1.25      NSMutableString *cmd = [NSMutableString stringWithFormat: @"/usr/bin/atos -p %d", getpid()];
    1.26 @@ -81,32 +83,41 @@
    1.27      foreach(addr,addresses) {
    1.28          [cmd appendFormat: @" %p", [addr pointerValue]];
    1.29      }
    1.30 -    FILE *file = popen( [cmd UTF8String], "r" );
    1.31 -    if( ! file )
    1.32 -        return nil;
    1.33 -    
    1.34 -    NSMutableData *output = [NSMutableData data];
    1.35 -    char buffer[512];
    1.36 -    size_t length;
    1.37 -    while ((length = fread( buffer, 1, sizeof( buffer ), file ) ))
    1.38 -        [output appendBytes: buffer length: length];
    1.39 -    pclose( file );
    1.40 -    NSString *outStr = [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease];
    1.41 +    file = popen( [cmd UTF8String], "r" );
    1.42 +#endif
    1.43      
    1.44      NSMutableString *result = [NSMutableString string];
    1.45 -    NSString *line;
    1.46 -    foreach( line, [outStr componentsSeparatedByString: @"\n"] ) {
    1.47 -        // Skip  frames that are part of the exception/assertion handling itself:
    1.48 -        if( [line hasPrefix: @"-[NSAssertionHandler"] || [line hasPrefix: @"+[NSException"] 
    1.49 -                || [line hasPrefix: @"-[NSException"] || [line hasPrefix: @"_AssertFailed"] )
    1.50 -            continue;
    1.51 -        if( result.length )
    1.52 -            [result appendString: @"\n"];
    1.53 -        [result appendString: @"\t"];
    1.54 -        [result appendString: line];
    1.55 -        // Don't show the "__start" frame below "main":
    1.56 -        if( [line hasPrefix: @"main "] )
    1.57 -            break;
    1.58 +    if( file ) {
    1.59 +        NSMutableData *output = [NSMutableData data];
    1.60 +        char buffer[512];
    1.61 +        size_t length;
    1.62 +        while ((length = fread( buffer, 1, sizeof( buffer ), file ) ))
    1.63 +            [output appendBytes: buffer length: length];
    1.64 +        pclose( file );
    1.65 +        NSString *outStr = [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease];
    1.66 +        
    1.67 +        NSString *line;
    1.68 +        foreach( line, [outStr componentsSeparatedByString: @"\n"] ) {
    1.69 +            // Skip  frames that are part of the exception/assertion handling itself:
    1.70 +            if( [line hasPrefix: @"-[NSAssertionHandler"] || [line hasPrefix: @"+[NSException"] 
    1.71 +                    || [line hasPrefix: @"-[NSException"] || [line hasPrefix: @"_AssertFailed"]
    1.72 +                    || [line hasPrefix: @"objc_"] )
    1.73 +                continue;
    1.74 +            if( result.length )
    1.75 +                [result appendString: @"\n"];
    1.76 +            [result appendString: @"\t"];
    1.77 +            [result appendString: line];
    1.78 +            // Don't show the "__start" frame below "main":
    1.79 +            if( [line hasPrefix: @"main "] )
    1.80 +                break;
    1.81 +        }
    1.82 +    } else {
    1.83 +        NSValue *addr;
    1.84 +        foreach(addr,addresses) {
    1.85 +            if( result.length )
    1.86 +                [result appendString: @" <- "];
    1.87 +            [result appendFormat: @"%p", [addr pointerValue]];
    1.88 +        }
    1.89      }
    1.90      return result;
    1.91  }