|
| 1 | +#if (TARGET_OS_TV || TARGET_OS_IPHONE) |
| 2 | +#define BSGOOMAvailable 1 |
| 3 | +#else |
| 4 | +#define BSGOOMAvailable 0 |
| 5 | +#endif |
| 6 | + |
| 7 | +#if BSGOOMAvailable |
| 8 | +#import <UIKit/UIKit.h> |
| 9 | +#endif |
| 10 | +#import "BSGOutOfMemoryWatchdog.h" |
| 11 | +#import "BSG_KSSystemInfo.h" |
| 12 | +#import "BugsnagLogger.h" |
| 13 | +#import "Bugsnag.h" |
| 14 | +#import "BugsnagKSCrashSysInfoParser.h" |
| 15 | +#import "BugsnagSessionTracker.h" |
| 16 | + |
| 17 | +@interface BSGOutOfMemoryWatchdog () |
| 18 | +@property(nonatomic, getter=isWatching) BOOL watching; |
| 19 | +@property(nonatomic, strong) NSString *sentinelFilePath; |
| 20 | +@property(nonatomic, getter=didOOMLastLaunch) BOOL oomLastLaunch; |
| 21 | +@property(nonatomic, strong, readwrite) NSMutableDictionary *cachedFileInfo; |
| 22 | +@property(nonatomic, strong, readwrite) NSDictionary *lastBootCachedFileInfo; |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation BSGOutOfMemoryWatchdog |
| 26 | + |
| 27 | +- (instancetype)init { |
| 28 | + self = [self initWithSentinelPath:nil configuration:nil]; |
| 29 | + return self; |
| 30 | +} |
| 31 | + |
| 32 | +- (instancetype)initWithSentinelPath:(NSString *)sentinelFilePath |
| 33 | + configuration:(BugsnagConfiguration *)config { |
| 34 | + if (sentinelFilePath.length == 0) { |
| 35 | + return nil; // disallow enabling a watcher without a file path |
| 36 | + } |
| 37 | + if (self = [super init]) { |
| 38 | + _sentinelFilePath = sentinelFilePath; |
| 39 | +#ifdef BSGOOMAvailable |
| 40 | + _oomLastLaunch = [self computeDidOOMLastLaunchWithConfig:config]; |
| 41 | + _cachedFileInfo = [self generateCacheInfoWithConfig:config]; |
| 42 | +#endif |
| 43 | + } |
| 44 | + return self; |
| 45 | +} |
| 46 | + |
| 47 | +- (void)enable { |
| 48 | +#if BSGOOMAvailable |
| 49 | + if ([self isWatching]) { |
| 50 | + return; |
| 51 | + } |
| 52 | + [self writeSentinelFile]; |
| 53 | + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 54 | + [center addObserver:self |
| 55 | + selector:@selector(disable:) |
| 56 | + name:UIApplicationWillTerminateNotification |
| 57 | + object:nil]; |
| 58 | + [center addObserver:self |
| 59 | + selector:@selector(handleTransitionToBackground:) |
| 60 | + name:UIApplicationDidEnterBackgroundNotification |
| 61 | + object:nil]; |
| 62 | + [center addObserver:self |
| 63 | + selector:@selector(handleTransitionToForeground:) |
| 64 | + name:UIApplicationWillEnterForegroundNotification |
| 65 | + object:nil]; |
| 66 | + [center addObserver:self |
| 67 | + selector:@selector(handleLowMemoryChange:) |
| 68 | + name:UIApplicationDidReceiveMemoryWarningNotification |
| 69 | + object:nil]; |
| 70 | + [center addObserver:self |
| 71 | + selector:@selector(handleUpdateSession:) |
| 72 | + name:BSGSessionUpdateNotification |
| 73 | + object:nil]; |
| 74 | + [[Bugsnag configuration] |
| 75 | + addObserver:self |
| 76 | + forKeyPath:NSStringFromSelector(@selector(releaseStage)) |
| 77 | + options:NSKeyValueObservingOptionNew |
| 78 | + context:nil]; |
| 79 | + self.watching = YES; |
| 80 | +#endif |
| 81 | +} |
| 82 | + |
| 83 | +- (void)disable:(NSNotification *)note { |
| 84 | + [self disable]; |
| 85 | +} |
| 86 | + |
| 87 | +- (void)disable { |
| 88 | + if (![self isWatching]) { |
| 89 | + // Avoid unsubscribing from KVO when not observing |
| 90 | + // From the docs: |
| 91 | + // > Asking to be removed as an observer if not already registered as |
| 92 | + // > one results in an NSRangeException. You either call |
| 93 | + // > `removeObserver:forKeyPath:context: exactly once for the |
| 94 | + // > corresponding call to `addObserver:forKeyPath:options:context:` |
| 95 | + return; |
| 96 | + } |
| 97 | + self.watching = NO; |
| 98 | + [self deleteSentinelFile]; |
| 99 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 100 | + @try { |
| 101 | + [[Bugsnag configuration] |
| 102 | + removeObserver:self |
| 103 | + forKeyPath:NSStringFromSelector(@selector(releaseStage))]; |
| 104 | + } @catch (NSException *exception) { |
| 105 | + // Shouldn't happen, but if for some reason, unregistration happens |
| 106 | + // without registration, catch the resulting exception. |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +- (void)observeValueForKeyPath:(NSString *)keyPath |
| 111 | + ofObject:(id)object |
| 112 | + change:(NSDictionary<NSString *, id> *)change |
| 113 | + context:(void *)context { |
| 114 | + self.cachedFileInfo[@"app"][@"releaseStage"] = change[NSKeyValueChangeNewKey]; |
| 115 | + [self writeSentinelFile]; |
| 116 | +} |
| 117 | +- (void)handleTransitionToForeground:(NSNotification *)note { |
| 118 | + self.cachedFileInfo[@"app"][@"inForeground"] = @YES; |
| 119 | + [self writeSentinelFile]; |
| 120 | +} |
| 121 | + |
| 122 | +- (void)handleTransitionToBackground:(NSNotification *)note { |
| 123 | + self.cachedFileInfo[@"app"][@"inForeground"] = @NO; |
| 124 | + [self writeSentinelFile]; |
| 125 | +} |
| 126 | + |
| 127 | +- (void)handleLowMemoryChange:(NSNotification *)note { |
| 128 | + self.cachedFileInfo[@"device"][@"lowMemory"] = [[Bugsnag payloadDateFormatter] |
| 129 | + stringFromDate:[NSDate date]]; |
| 130 | + [self writeSentinelFile]; |
| 131 | +} |
| 132 | + |
| 133 | +- (void)handleUpdateSession:(NSNotification *)note { |
| 134 | + id session = [note object]; |
| 135 | + NSMutableDictionary *cache = (id)self.cachedFileInfo; |
| 136 | + if (session) { |
| 137 | + cache[@"session"] = session; |
| 138 | + } else { |
| 139 | + [cache removeObjectForKey:@"session"]; |
| 140 | + } |
| 141 | + [self writeSentinelFile]; |
| 142 | +} |
| 143 | + |
| 144 | +- (BOOL)computeDidOOMLastLaunchWithConfig:(BugsnagConfiguration *)config { |
| 145 | + if ([[NSFileManager defaultManager] fileExistsAtPath:self.sentinelFilePath]) { |
| 146 | + NSDictionary *lastBootInfo = [self readSentinelFile]; |
| 147 | + if (lastBootInfo != nil) { |
| 148 | + self.lastBootCachedFileInfo = lastBootInfo; |
| 149 | + NSString *lastBootAppVersion = |
| 150 | + [lastBootInfo valueForKeyPath:@"app.version"]; |
| 151 | + NSString *lastBootOSVersion = |
| 152 | + [lastBootInfo valueForKeyPath:@"device.osBuild"]; |
| 153 | + BOOL lastBootInForeground = |
| 154 | + [[lastBootInfo valueForKeyPath:@"app.inForeground"] boolValue]; |
| 155 | + NSString *osVersion = [BSG_KSSystemInfo osBuildVersion]; |
| 156 | + NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary]; |
| 157 | + NSString *appVersion = |
| 158 | + [appInfo valueForKey:(__bridge NSString *)kCFBundleVersionKey]; |
| 159 | + BOOL sameVersions = [lastBootOSVersion isEqualToString:osVersion] && |
| 160 | + [lastBootAppVersion isEqualToString:appVersion]; |
| 161 | + BOOL shouldReport = config.reportBackgroundOOMs || lastBootInForeground; |
| 162 | + [self deleteSentinelFile]; |
| 163 | + return sameVersions && shouldReport; |
| 164 | + } |
| 165 | + } |
| 166 | + return NO; |
| 167 | +} |
| 168 | + |
| 169 | +- (void)deleteSentinelFile { |
| 170 | + NSError *error = nil; |
| 171 | + [[NSFileManager defaultManager] removeItemAtPath:self.sentinelFilePath |
| 172 | + error:&error]; |
| 173 | + if (error) { |
| 174 | + bsg_log_err(@"Failed to delete oom watchdog file: %@", error); |
| 175 | + unlink([self.sentinelFilePath UTF8String]); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +- (NSDictionary *)readSentinelFile { |
| 180 | + NSError *error = nil; |
| 181 | + NSData *data = [NSData dataWithContentsOfFile:self.sentinelFilePath options:0 error:&error]; |
| 182 | + if (error) { |
| 183 | + bsg_log_err(@"Failed to read oom watchdog file: %@", error); |
| 184 | + return nil; |
| 185 | + } |
| 186 | + NSDictionary *contents = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; |
| 187 | + if (error) { |
| 188 | + bsg_log_err(@"Failed to read oom watchdog file: %@", error); |
| 189 | + return nil; |
| 190 | + } |
| 191 | + return contents; |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +- (void)writeSentinelFile { |
| 196 | + NSError *error = nil; |
| 197 | + if (![NSJSONSerialization isValidJSONObject:self.cachedFileInfo]) { |
| 198 | + bsg_log_err(@"Cached oom watchdog data cannot be written as JSON"); |
| 199 | + return; |
| 200 | + } |
| 201 | + NSData *data = [NSJSONSerialization dataWithJSONObject:self.cachedFileInfo options:0 error:&error]; |
| 202 | + if (error) { |
| 203 | + bsg_log_err(@"Cached oom watchdog data cannot be written as JSON: %@", error); |
| 204 | + return; |
| 205 | + } |
| 206 | + [data writeToFile:self.sentinelFilePath atomically:YES]; |
| 207 | +} |
| 208 | + |
| 209 | +- (NSMutableDictionary *)generateCacheInfoWithConfig:(BugsnagConfiguration *)config { |
| 210 | + NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo]; |
| 211 | + NSMutableDictionary *cache = [NSMutableDictionary new]; |
| 212 | + NSMutableDictionary *app = [NSMutableDictionary new]; |
| 213 | + |
| 214 | + app[@"id"] = systemInfo[@BSG_KSSystemField_BundleID] ?: @""; |
| 215 | + app[@"name"] = systemInfo[@BSG_KSSystemField_BundleName] ?: @""; |
| 216 | + app[@"releaseStage"] = config.releaseStage; |
| 217 | + app[@"version"] = systemInfo[@BSG_KSSystemField_BundleVersion] ?: @""; |
| 218 | + app[@"inForeground"] = @YES; |
| 219 | +#if TARGET_OS_TV |
| 220 | + app[@"type"] = @"tvOS"; |
| 221 | +#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE |
| 222 | + app[@"type"] = @"iOS"; |
| 223 | +#endif |
| 224 | + cache[@"app"] = app; |
| 225 | + |
| 226 | + NSMutableDictionary *device = [NSMutableDictionary new]; |
| 227 | + device[@"id"] = systemInfo[@BSG_KSSystemField_DeviceAppHash]; |
| 228 | + // device[@"lowMemory"] is initially unset |
| 229 | + device[@"osBuild"] = systemInfo[@BSG_KSSystemField_OSVersion]; |
| 230 | + device[@"osVersion"] = systemInfo[@BSG_KSSystemField_SystemVersion]; |
| 231 | + device[@"model"] = systemInfo[@BSG_KSSystemField_Machine]; |
| 232 | + device[@"wordSize"] = @(PLATFORM_WORD_SIZE); |
| 233 | +#if TARGET_OS_SIMULATOR |
| 234 | + device[@"simulator"] = @YES; |
| 235 | +#else |
| 236 | + device[@"simulator"] = @NO; |
| 237 | +#endif |
| 238 | + cache[@"device"] = device; |
| 239 | + |
| 240 | + return cache; |
| 241 | +} |
| 242 | + |
| 243 | +@end |
0 commit comments