Skip to content

Commit

Permalink
Merge pull request #1021 from bugsnag/nickdowell/fix-free-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdowell committed Mar 8, 2021
2 parents 6894ac1 + 0f70af1 commit 73596d7
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Bugsnag/BugsnagSessionTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ - (void)startNewSessionWithAutoCaptureValue:(BOOL)isAutoCaptured {
BugsnagApp *app = [BugsnagApp appWithDictionary:@{@"system": systemInfo}
config:self.config
codeBundleId:self.codeBundleId];
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:@{@"system": systemInfo}];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:@{@"system": systemInfo}];
[device appendRuntimeInfo:self.extraRuntimeInfo];

BugsnagSession *newSession = [[BugsnagSession alloc] initWithId:[[NSUUID UUID] UUIDString]
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ - (BugsnagAppWithState *)generateAppWithState:(NSDictionary *)systemInfo {
}

- (BugsnagDeviceWithState *)generateDeviceWithState:(NSDictionary *)systemInfo {
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithDictionary:@{@"system": systemInfo}];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithKSCrashReport:@{@"system": systemInfo}];
device.time = [NSDate date]; // default to current time for handled errors
[device appendRuntimeInfo:self.extraRuntimeInfo];
device.orientation = _lastOrientation;
Expand Down
8 changes: 4 additions & 4 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ + (NSDictionary *)systemInfo {
sysInfo[@BSG_KSSystemField_DeviceAppHash] = [self deviceAndAppHash];
sysInfo[@BSG_KSSystemField_BuildType] = [BSG_KSSystemInfo buildType];

NSDictionary *memory = @{
@BSG_KSSystemField_Size: [self int64Sysctl:@"hw.memsize"],
@BSG_KSCrashField_Usable: @(bsg_ksmachusableMemory())
sysInfo[@(BSG_KSSystemField_Memory)] = @{
@(BSG_KSCrashField_Free): @(bsg_ksmachfreeMemory()),
@(BSG_KSCrashField_Usable): @(bsg_ksmachusableMemory()),
@(BSG_KSSystemField_Size): [self int64Sysctl:@"hw.memsize"]
};
sysInfo[@BSG_KSSystemField_Memory] = memory;

NSDictionary *statsInfo = [[BSG_KSCrash sharedInstance] captureAppStats];
sysInfo[@BSG_KSCrashField_AppStats] = statsInfo;
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Payload/BugsnagDevice+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface BugsnagDevice ()

+ (instancetype)deviceWithDictionary:(NSDictionary *)event;
+ (instancetype)deviceWithKSCrashReport:(NSDictionary *)event;

+ (instancetype)deserializeFromJson:(NSDictionary *)json;

Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Payload/BugsnagDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ + (BugsnagDevice *)deserializeFromJson:(NSDictionary *)json {
return device;
}

+ (BugsnagDevice *)deviceWithDictionary:(NSDictionary *)event {
+ (BugsnagDevice *)deviceWithKSCrashReport:(NSDictionary *)event {
BugsnagDevice *device = [BugsnagDevice new];
[self populateFields:device dictionary:event];
return device;
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Payload/BugsnagDeviceWithState+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)deviceFromJson:(NSDictionary *)json;

+ (instancetype)deviceWithDictionary:(NSDictionary *)event;
+ (instancetype)deviceWithKSCrashReport:(NSDictionary *)event;

+ (instancetype)deviceWithOomData:(NSDictionary *)data;

Expand Down
4 changes: 2 additions & 2 deletions Bugsnag/Payload/BugsnagDeviceWithState.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ + (BugsnagDeviceWithState *)deviceWithOomData:(NSDictionary *)data {
return device;
}

+ (BugsnagDeviceWithState *)deviceWithDictionary:(NSDictionary *)event {
+ (BugsnagDeviceWithState *)deviceWithKSCrashReport:(NSDictionary *)event {
BugsnagDeviceWithState *device = [BugsnagDeviceWithState new];
[self populateFields:device dictionary:event];
device.orientation = [event valueForKeyPath:@"user.state.deviceState.orientation"];
device.freeMemory = [event valueForKeyPath:@"system.memory.free"] ?: [event valueForKeyPath:@"system.memory.usable"];
device.freeMemory = [event valueForKeyPath:@"system.memory.free"];
device.freeDisk = BSGDeviceFreeSpace(NSCachesDirectory);

NSString *val = [event valueForKeyPath:@"report.timestamp"];
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Payload/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ - (instancetype)initWithKSCrashData:(NSDictionary *)event {
}
}
NSString *deviceAppHash = [event valueForKeyPath:@"system.device_app_hash"];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithDictionary:event];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithKSCrashReport:event];
BugsnagUser *user = [self parseUser:event deviceAppHash:deviceAppHash deviceId:device.id];
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithDictionaryRepresentation:[event valueForKeyPath:@"user.config"]];
BugsnagAppWithState *app = [BugsnagAppWithState appWithDictionary:event config:config codeBundleId:self.codeBundleId];
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Changelog
* Fix missing `osName` and `osVersion` for errors reported from app extensions that do not link against UIKit.
[#1022](https://github.com/bugsnag/bugsnag-cocoa/pull/1022)

* Fix incorrect `freeMemory` for errors reported via `notify()`
[#1021](https://github.com/bugsnag/bugsnag-cocoa/pull/1021)

## 6.7.0 (2021-03-03)

### Enhancements
Expand Down
22 changes: 14 additions & 8 deletions Tests/BugsnagDeviceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//

#import <XCTest/XCTest.h>
#import "BugsnagConfiguration.h"
#import "BugsnagDeviceWithState+Private.h"

#import "BSG_KSSystemInfo.h"
#import "BugsnagDevice+Private.h"
#import "BugsnagTestConstants.h"
#import "BugsnagDeviceWithState+Private.h"

@interface BugsnagDeviceTest : XCTestCase
@property NSDictionary *data;
Expand Down Expand Up @@ -49,7 +49,7 @@ - (void)setUp {
}

- (void)testDevice {
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:self.data];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:self.data];

// verify stateless fields
XCTAssertTrue(device.jailbroken);
Expand All @@ -69,7 +69,7 @@ - (void)testDevice {
}

- (void)testDeviceWithState {
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithDictionary:self.data];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithKSCrashReport:self.data];

// verify stateless fields
XCTAssertTrue(device.jailbroken);
Expand Down Expand Up @@ -98,8 +98,14 @@ - (void)testDeviceWithState {
XCTAssertEqualObjects([formatter dateFromString:@"2014-12-02T01:56:13Z"], device.time);
}

- (void)testDeviceWithRealSystemInfo {
NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithKSCrashReport:@{@"system": systemInfo}];
XCTAssertLessThan(device.freeMemory.unsignedLongLongValue, device.totalMemory.unsignedLongLongValue);
}

- (void)testDeviceToDict {
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:self.data];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:self.data];
device.locale = @"en-US";
NSDictionary *dict = [device toDictionary];

Expand All @@ -122,7 +128,7 @@ - (void)testDeviceToDict {
}

- (void)testDeviceWithStateToDict {
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithDictionary:self.data];
BugsnagDeviceWithState *device = [BugsnagDeviceWithState deviceWithKSCrashReport:self.data];
device.locale = @"en-US";
NSDictionary *dict = [device toDictionary];

Expand Down Expand Up @@ -185,7 +191,7 @@ - (void)testDeviceFreeSpaceShouldBeNilWhenFailsToRetrieveIt {
}

- (void)testDeviceRuntimeInfoAppended {
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:self.data];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:self.data];
XCTAssertEqual(2, [device.runtimeVersions count]);
XCTAssertEqualObjects(@"14B25", device.runtimeVersions[@"osBuild"]);
XCTAssertEqualObjects(@"10.0.0 (clang-1000.11.45.5)", device.runtimeVersions[@"clangVersion"]);
Expand Down
2 changes: 1 addition & 1 deletion Tests/BugsnagSessionTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ - (BugsnagDevice *)generateDevice {
}
}
};
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:deviceData];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:deviceData];
device.locale = @"en-US";
return device;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/BugsnagSessionTrackingPayloadTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (BugsnagDevice *)generateDevice {
}
}
};
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:deviceData];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:deviceData];
device.locale = @"en-US";
return device;
}
Expand Down
1 change: 1 addition & 0 deletions features/app_and_device_attributes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Feature: App and Device attributes present
| ios | @not_null |
| macos | @null |
And the error payload field "events.0.device.time" is a date
And the event "device.freeMemory" is less than the event "device.totalMemory"

# App

Expand Down
2 changes: 2 additions & 0 deletions features/barebone_tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Feature: Barebone tests
And the event "app.version" equals "12.3"
And the event "breadcrumbs.0.name" equals "Running BareboneTestHandledScenario"
And the event "breadcrumbs.1.name" equals "This is super <redacted>"
And the event "device.freeMemory" is less than the event "device.totalMemory"
And the event "device.id" is not null
And the event "device.jailbroken" is false
And the event "device.locale" is not null
Expand Down Expand Up @@ -119,6 +120,7 @@ Feature: Barebone tests
And the event "app.version" equals "12.3"
And the event "breadcrumbs.0.name" equals "Bugsnag loaded"
And the event "breadcrumbs.1.name" is null
And the event "device.freeMemory" is less than the event "device.totalMemory"
And the event "device.id" is not null
And the event "device.jailbroken" is false
And the event "device.locale" is not null
Expand Down
8 changes: 8 additions & 0 deletions features/steps/ios_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ def request_fields_are_equal(key, index_a, index_b)
assert_true(lower <= value && value <= upper, "Expected a value between #{lower} and #{upper}, but received #{value}")
end

Then('the event {string} is less than the event {string}') do |field1, field2|
value1 = Maze::Helper.read_key_path(Maze::Server.errors.current[:body], "events.0.#{field1}")
assert_not_nil(value1, 'Expected a value')
value2 = Maze::Helper.read_key_path(Maze::Server.errors.current[:body], "events.0.#{field2}")
assert_not_nil(value2, 'Expected a value')
assert_true(value1 < value2, "Expected value to be less than #{value2}, but received #{value1}")
end

Then('the event breadcrumbs contain {string} with type {string}') do |string, type|
crumbs = Maze::Helper.read_key_path(find_request(0)[:body], 'events.0.breadcrumbs')
assert_not_equal(0, crumbs.length, 'There are no breadcrumbs on this event')
Expand Down

0 comments on commit 73596d7

Please sign in to comment.