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

add setNumberOfStackFramesToSkipForNotFatalErrors #11476

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ void FIRCLSUserLoggingRecordUserKeysAndValues(NSDictionary* keysAndValues);
void FIRCLSUserLoggingRecordInternalKeyValue(NSString* key, id value);
void FIRCLSUserLoggingWriteInternalKeyValue(NSString* key, NSString* value);

void FIRCLSUserLoggingRecordError(NSError* error, NSDictionary<NSString*, id>* additionalUserInfo);
void FIRCLSUserLoggingRecordError(NSError* error,
NSDictionary<NSString*, id>* additionalUserInfo,
NSUInteger skipStacks);

NSDictionary* FIRCLSUserLoggingGetCompactedKVEntries(FIRCLSUserLoggingKVStorage* storage,
bool decodeHex);
Expand Down
7 changes: 6 additions & 1 deletion Crashlytics/Crashlytics/Components/FIRCLSUserLogging.m
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ static void FIRCLSUserLoggingWriteError(FIRCLSFile *file,
}

void FIRCLSUserLoggingRecordError(NSError *error,
NSDictionary<NSString *, id> *additionalUserInfo) {
NSDictionary<NSString *, id> *additionalUserInfo,
NSUInteger numberOfStacksToSkip) {
if (!error) {
return;
}
Expand All @@ -391,6 +392,10 @@ void FIRCLSUserLoggingRecordError(NSError *error,
// record the stacktrace and timestamp here, so we
// are as close as possible to the user's log statement
NSArray *addresses = [NSThread callStackReturnAddresses];
if (numberOfStacksToSkip > 0 && [addresses count] > numberOfStacksToSkip) {
NSRange range = NSMakeRange(numberOfStacksToSkip, [addresses count] - numberOfStacksToSkip);
addresses = [addresses subarrayWithRange:range];
}
uint64_t timestamp = time(NULL);

FIRCLSUserLoggingWriteAndCheckABFiles(
Expand Down
8 changes: 7 additions & 1 deletion Crashlytics/Crashlytics/FIRCrashlytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ @interface FIRCrashlytics () <FIRLibrary, FIRCrashlyticsInstanceProvider, FIRSes
// Dependencies common to each of the Controllers
@property(nonatomic, strong) FIRCLSManagerData *managerData;

@property(nonatomic, assign) NSUInteger stackFramesToSkip;
@end

@implementation FIRCrashlytics
Expand Down Expand Up @@ -129,6 +130,7 @@ - (instancetype)initWithApp:(FIRApp *)app
_fileManager = [[FIRCLSFileManager alloc] init];
_googleAppID = app.options.googleAppID;
_dataArbiter = [[FIRCLSDataCollectionArbiter alloc] initWithApp:app withAppInfo:appInfo];
_stackFramesToSkip = 0;

FIRCLSApplicationIdentifierModel *appModel = [[FIRCLSApplicationIdentifierModel alloc] init];
FIRCLSSettings *settings = [[FIRCLSSettings alloc] initWithFileManager:_fileManager
Expand Down Expand Up @@ -377,7 +379,7 @@ - (void)recordError:(NSError *)error {
}

- (void)recordError:(NSError *)error userInfo:(NSDictionary<NSString *, id> *)userInfo {
FIRCLSUserLoggingRecordError(error, userInfo);
FIRCLSUserLoggingRecordError(error, userInfo, self.stackFramesToSkip);
}

- (void)recordExceptionModel:(FIRExceptionModel *)exceptionModel {
Expand All @@ -391,6 +393,10 @@ - (void)recordOnDemandExceptionModel:(FIRExceptionModel *)exceptionModel {
usingExistingReportManager:self.existingReportManager];
}

- (void)setNumberOfStackFramesToSkipForNotFatalErrors:(NSUInteger)frames {
self.stackFramesToSkip = frames;
}

#pragma mark - FIRSessionsSubscriber

- (void)onSessionChanged:(FIRSessionDetails *_Nonnull)session {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ NS_SWIFT_NAME(Crashlytics)
*/
- (void)deleteUnsentReports;

- (void)setNumberOfStackFramesToSkipForNotFatalErrors:(NSUInteger)frames;

@end

NS_ASSUME_NONNULL_END
31 changes: 26 additions & 5 deletions Crashlytics/UnitTests/FIRCLSLoggingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ - (void)testLoggedError {
code:-1
userInfo:@{@"key1" : @"value", @"key2" : @"value2"}];

FIRCLSUserLoggingRecordError(error, @{@"additional" : @"key"});
FIRCLSUserLoggingRecordError(error, @{@"additional" : @"key"}, 0);

NSArray* errors = [self errorAContents];

Expand Down Expand Up @@ -405,7 +405,7 @@ - (void)testWritingMaximumNumberOfLoggedErrors {
userInfo:@{@"key1" : @"value", @"key2" : @"value2"}];

for (size_t i = 0; i < _firclsContext.readonly->logging.errorStorage.maxEntries; ++i) {
FIRCLSUserLoggingRecordError(error, nil);
FIRCLSUserLoggingRecordError(error, nil, 0);
}

NSArray* errors = [self errorAContents];
Expand All @@ -414,15 +414,15 @@ - (void)testWritingMaximumNumberOfLoggedErrors {

// at this point, if we log one more, we should expect a roll over to the next file

FIRCLSUserLoggingRecordError(error, nil);
FIRCLSUserLoggingRecordError(error, nil, 0);

XCTAssertEqual([[self errorAContents] count], 8, @"");
XCTAssertEqual([[self errorBContents] count], 1, @"");
XCTAssertEqual(*_firclsContext.readonly->logging.errorStorage.entryCount, 1);

// and our next entry should continue into the B file

FIRCLSUserLoggingRecordError(error, nil);
FIRCLSUserLoggingRecordError(error, nil, 0);

XCTAssertEqual([[self errorAContents] count], 8, @"");
XCTAssertEqual([[self errorBContents] count], 2, @"");
Expand All @@ -432,7 +432,7 @@ - (void)testWritingMaximumNumberOfLoggedErrors {
- (void)testLoggedErrorWithNullsInAdditionalInfo {
NSError* error = [NSError errorWithDomain:@"Domain" code:-1 userInfo:nil];

FIRCLSUserLoggingRecordError(error, @{@"null-key" : [NSNull null]});
FIRCLSUserLoggingRecordError(error, @{@"null-key" : [NSNull null]}, 0);

NSArray* errors = [self errorAContents];

Expand All @@ -455,4 +455,25 @@ - (void)testLoggedErrorWithNullsInAdditionalInfo {
XCTAssertEqualObjects(additionalEntries[0], entryOne, @"");
}

- (void)testSkippingFramesInStackTrace {
NSError* error = [NSError errorWithDomain:@"Domain" code:-1 userInfo:nil];
FIRCLSUserLoggingRecordError(error, @{}, 0);

NSUInteger numberOfStacksToSkip = 2;
FIRCLSUserLoggingRecordError(error, @{}, numberOfStacksToSkip);

NSArray* errors = [self errorAContents];

NSArray* firstTrace = [errors[0] valueForKeyPath:@"error.stacktrace"];
NSArray* secondTrace = [errors[1] valueForKeyPath:@"error.stacktrace"];

XCTAssertEqual([secondTrace count], [firstTrace count] - 2,
@"second error should have have 2 frames less");
XCTAssertEqual(secondTrace[0], firstTrace[2], @"should have same stack traces starting with 2");
XCTAssertEqual(secondTrace[1], firstTrace[3], @"should have same stack traces starting with 2");
XCTAssertEqualObjects(secondTrace,
[firstTrace subarrayWithRange:NSMakeRange(2, [secondTrace count])],
@"should have same stack traces with first 2 removed");
}

@end