Skip to content
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

Ensure foreground stats are recorded for handled errors #755

Merged
merged 6 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
*/
- (NSArray<BugsnagThread *> *)captureThreads:(NSException *)exc depth:(int)depth;

/**
* Collects information about the application's foreground state (duration in foreground/background)
*/
- (NSDictionary *)captureAppStats;

/** If YES, reports will be sent even if a debugger is attached
*
* Default: NO
Expand Down
12 changes: 12 additions & 0 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#import "BugsnagThread.h"
#import "BSGSerialization.h"
#import "BugsnagErrorReportSink.h"
#import "BugsnagCollections.h"
#import "BSG_KSCrashReportFields.h"

#if BSG_HAS_UIKIT
#import <UIKit/UIKit.h>
Expand Down Expand Up @@ -320,6 +322,16 @@ - (void)sendAllReports {
return @[];
}

- (NSDictionary *)captureAppStats {
BSG_KSCrash_State state = crashContext()->state;
bsg_kscrashstate_updateForegroundStats(&state);
NSMutableDictionary *dict = [NSMutableDictionary new];
BSGDictSetSafeObject(dict, @(state.activeDurationSinceLaunch), @BSG_KSCrashField_ActiveTimeSinceLaunch);
BSGDictSetSafeObject(dict, @(state.backgroundDurationSinceLaunch), @BSG_KSCrashField_BGTimeSinceLaunch);
BSGDictSetSafeObject(dict, @(state.applicationIsInForeground), @BSG_KSCrashField_AppInFG);
return dict;
}

- (void)reportUserException:(NSString *)name
reason:(NSString *)reason
handledState:(NSDictionary *)handledState
Expand Down
1 change: 0 additions & 1 deletion Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,4 @@ char *bsg_kscrash_captureThreadTrace(int discardDepth, int frameCount, uintptr_t
bsg_kscrashsentry_resume_threads_user(false);
}
return trace;

}
8 changes: 8 additions & 0 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ void bsg_kscrashstate_notifyAppCrash(BSG_KSCrashType type);
*/
const BSG_KSCrash_State *bsg_kscrashstate_currentState(void);

/**
* Updates the stats for duration in foreground/background. This needs to
* be updated whenever an error report is captured.
*
* @param state the kscrash state
*/
void bsg_kscrashstate_updateForegroundStats(BSG_KSCrash_State *const state);
fractalwrench marked this conversation as resolved.
Show resolved Hide resolved

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 10 additions & 5 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
// Avoiding static functions due to linker issues.

// ============================================================================
void bsg_kscrashstate_updateForegroundStats(BSG_KSCrash_State *const state);
fractalwrench marked this conversation as resolved.
Show resolved Hide resolved

#pragma mark - JSON Encoding -
// ============================================================================

Expand Down Expand Up @@ -356,7 +358,15 @@ void bsg_kscrashstate_notifyAppTerminate(void) {
void bsg_kscrashstate_notifyAppCrash(BSG_KSCrashType type) {
BSG_KSCrash_State *const state = bsg_g_state;
const char *const stateFilePath = bsg_g_stateFilePath;
bsg_kscrashstate_updateForegroundStats(state);
BOOL didCrash = type != BSG_KSCrashTypeUserReported;
state->crashedThisLaunch |= didCrash;
if (didCrash) {
bsg_kscrashstate_i_saveState(state, stateFilePath);
}
}

void bsg_kscrashstate_updateForegroundStats(BSG_KSCrash_State *const state) {
const double duration = bsg_ksmachtimeDifferenceInSeconds(
mach_absolute_time(), state->appStateTransitionTime);
if (state->applicationIsActive) {
Expand All @@ -366,11 +376,6 @@ void bsg_kscrashstate_notifyAppCrash(BSG_KSCrashType type) {
state->backgroundDurationSinceLaunch += duration;
state->backgroundDurationSinceLastCrash += duration;
}
BOOL didCrash = type != BSG_KSCrashTypeUserReported;
state->crashedThisLaunch |= didCrash;
if (didCrash) {
bsg_kscrashstate_i_saveState(state, stateFilePath);
}
}

const BSG_KSCrash_State *bsg_kscrashstate_currentState(void) {
Expand Down
3 changes: 3 additions & 0 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#import "BSG_KSLogger.h"
#import "BSG_KSCrashReportFields.h"
#import "BSG_KSMach.h"
#import "BSG_KSCrash.h"

#import <CommonCrypto/CommonDigest.h>
#if BSG_PLATFORM_IOS || BSG_PLATFORM_TVOS
Expand Down Expand Up @@ -416,6 +417,8 @@ + (NSDictionary *)systemInfo {
};
BSGDictSetSafeObject(sysInfo, memory, @BSG_KSSystemField_Memory);

NSDictionary *statsInfo = [[BSG_KSCrash sharedInstance] captureAppStats];
BSGDictSetSafeObject(sysInfo, statsInfo, @BSG_KSCrashField_AppStats);
return sysInfo;
}

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

## TBD

* Ensure foreground stats are recorded for handled errors
[#755](https://github.com/bugsnag/bugsnag-cocoa/pull/755)

## 6.1.1 (2020-07-16)

### Bug fixes
Expand Down