Skip to content

Commit

Permalink
fix: View hierarchy not sent for crashes (#2781)
Browse files Browse the repository at this point in the history
View hierarchy was not being sent for crashes because of the lack of a filename.

Co-authored-by: Andrew McKnight <[email protected]>
  • Loading branch information
brustolin and armcknight authored Mar 22, 2023
1 parent ea73af6 commit 66922ca
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- View hierarchy not sent for crashes (#2781)

## 8.3.2

### Features
Expand All @@ -8,6 +14,7 @@

### Fixes


- Updating AppHang state on main thread (#2793)
- App Hang report crashes with too many threads (#2811)

Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryScreenshot.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @implementation SentryScreenshot
return result;
}

- (void)saveScreenShots:(NSString *)path
- (void)saveScreenShots:(NSString *)imagesDirectoryPath
{
// This function does not dispatch the screenshot to the main thread.
// The caller should be aware of that.
Expand All @@ -31,7 +31,7 @@ - (void)saveScreenShots:(NSString *)path
NSString *name = idx == 0
? @"screenshot.png"
: [NSString stringWithFormat:@"screenshot-%li.png", (unsigned long)idx + 1];
NSString *fileName = [path stringByAppendingPathComponent:name];
NSString *fileName = [imagesDirectoryPath stringByAppendingPathComponent:name];
[obj writeToFile:fileName atomically:YES];
}];
}
Expand Down
11 changes: 9 additions & 2 deletions Sources/Sentry/SentryViewHierarchyIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@

#if SENTRY_HAS_UIKIT

/**
* Function to call through to the ObjC method to save a view hierarchy, which can be passed around
* as a function pointer in the C crash reporting code.
* @param reportDirectoryPath The path to the directory containing crash reporting files, in which a
* new file will be created to store the view hierarchy description.
*/
void
saveViewHierarchy(const char *path)
saveViewHierarchy(const char *reportDirectoryPath)
{
NSString *reportPath = [NSString stringWithUTF8String:path];
NSString *reportPath = [[NSString stringWithUTF8String:reportDirectoryPath]
stringByAppendingPathComponent:@"view-hierarchy.json"];
[SentryDependencyContainer.sharedInstance.viewHierarchy saveViewHierarchy:reportPath];
}

Expand Down
8 changes: 7 additions & 1 deletion Sources/Sentry/include/SentryScreenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (nullable NSArray<NSData *> *)appScreenshots;

- (void)saveScreenShots:(NSString *)path;
/**
* Save the current app screen shots in the given directory.
* If an app has more than one screen, one image for each screen will be saved.
*
* @param imagesDirectoryPath The path where the images should be saved.
*/
- (void)saveScreenShots:(NSString *)imagesDirectoryPath;

- (NSArray<NSData *> *)takeScreenshots;
@end
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/include/SentryViewHierarchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable NSData *)fetchViewHierarchy;

/**
* Save the current app view hierarchy in the given file path.
*
* @param filePath The full path where the view hierarchy should be saved.
*/
- (BOOL)saveViewHierarchy:(NSString *)filePath;
@end

Expand Down
20 changes: 16 additions & 4 deletions Sources/SentryCrash/Recording/SentryCrashC.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,32 @@ void sentrycrash_setCrashNotifyCallback(const SentryCrashReportWriteCallback onC
*/
void sentrycrash_setMaxReportCount(int maxReportCount);

/**
* @typedef SaveAttachmentCallback
*
* This typedef defines a function pointer to a callback that will be called during crashes
* to request extra attachments to be saved.
*
* @param directoryPath The path to a directory where the view hierarchy should be saved.
*/
typedef void (*SaveAttachmentCallback)(const char *directoryPath);

/**
* Set the callback to be called at the end of a crash to make the app save a screenshot;
*
* @param callback function pointer that will be called with a give path.
* @param callback function pointer that will be called with a path to a directory where the screen
* shot should be saved.
*/
void sentrycrash_setSaveScreenshots(void (*callback)(const char *));
void sentrycrash_setSaveScreenshots(SaveAttachmentCallback callback);

/**
* Set the callback to be called at the end of a crash to make the app save the view hierarchy
* descriptions;
*
* @param callback function pointer that will be called with a give path.
* @param callback function pointer that will be called with a path to a directory where the view
* hierarchy should be saved.
*/
void sentrycrash_setSaveViewHierarchy(void (*callback)(const char *));
void sentrycrash_setSaveViewHierarchy(SaveAttachmentCallback callback);

/** Report a custom, user defined exception.
* This can be useful when dealing with scripting languages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class SentryViewHierarchyIntegrationTests: XCTestCase {
XCTAssertFalse(sentrycrash_hasSaveViewHierarchyCallback())
}

func test_integrationAddFileName() {
SentrySDK.start { $0.attachViewHierarchy = true }
saveViewHierarchy("/test/path")
XCTAssertEqual("/test/path/view-hierarchy.json", fixture.viewHierarchy.saveFilePathUsed)
}

func test_processAttachments() {
let sut = fixture.getSut()
let event = Event(error: NSError(domain: "", code: -1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#import "SentryViewHierarchy.h"

#if SENTRY_HAS_UIKIT

void saveViewHierarchy(const char *path);

@interface
SentryViewHierarchy (Test)
- (int)viewHierarchyFromView:(UIView *)view intoContext:(SentryCrashJSONEncodeContext *)context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestSentryViewHierarchy: SentryViewHierarchy {
var result: Data?
var viewHierarchyResult: Int32 = 0
var processViewHierarchyCallback: (() -> Void)?
var saveFilePathUsed: String?

override func fetch() -> Data? {
guard let result = self.result
Expand All @@ -15,6 +16,11 @@ class TestSentryViewHierarchy: SentryViewHierarchy {
return result
}

override func save(_ filePath: String) -> Bool {
saveFilePathUsed = filePath
return true
}

override func viewHierarchy(from view: UIView!, into context: UnsafeMutablePointer<SentryCrashJSONEncodeContext>!) -> Int32 {
return viewHierarchyResult != 0 ? viewHierarchyResult : super.viewHierarchy(from: view, into: context)
}
Expand Down

0 comments on commit 66922ca

Please sign in to comment.