Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
aa8c7d9
Adding option
brustolin May 13, 2022
a031821
Moving Tracker
brustolin May 17, 2022
8a30e05
Capturing threads and stacktraces
brustolin May 19, 2022
6f531c9
tests
brustolin Jun 3, 2022
71fa2a9
Merge branch 'master' into feat/anr-report
brustolin Jun 3, 2022
bb55710
Format code
getsentry-bot Jun 3, 2022
8bb2744
Update CHANGELOG.md
brustolin Jun 3, 2022
4ceb2a0
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 3, 2022
bdbb43f
Update SentryANRTrackingIntegration.m
brustolin Jun 3, 2022
ac91664
Format code
getsentry-bot Jun 3, 2022
9329348
Update Sources/Sentry/Public/SentryOptions.h
brustolin Jun 7, 2022
f6cac32
Update SentryOutOfMemoryIntegrationTests.swift
brustolin Jun 7, 2022
40ad695
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 7, 2022
560a4da
Update SentryANRTracker.m
brustolin Jun 7, 2022
e92ec71
Update SentryANRTracker.m
brustolin Jun 7, 2022
1451d9c
Apply suggestions from code review
brustolin Jun 8, 2022
342d409
Review Comments
brustolin Jun 8, 2022
7ca6f9a
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 8, 2022
8668e7f
Format code
getsentry-bot Jun 8, 2022
7d59aaa
Update SentryANRTracker.m
brustolin Jun 8, 2022
9e35381
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 8, 2022
4aa37e2
fixes
brustolin Jun 8, 2022
e071ea6
Merge branch 'master' into feat/anr-report
brustolin Jun 8, 2022
ba4d310
Update SentryThreadInspectorTests.swift
brustolin Jun 8, 2022
15035ef
fix tests
brustolin Jun 10, 2022
c22dfd0
Renaming
brustolin Jun 10, 2022
b65589c
Format code
getsentry-bot Jun 10, 2022
c9ed569
Apply suggestions from code review
brustolin Jun 10, 2022
8398ee9
refactoring
brustolin Jun 10, 2022
2d70f8c
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 10, 2022
14284ac
Remove tests
brustolin Jun 10, 2022
53da818
Format code
getsentry-bot Jun 10, 2022
567049e
ANRTracker
brustolin Jun 13, 2022
3ac5510
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 13, 2022
4e0e708
Update SentrySDKTests.swift
brustolin Jun 13, 2022
521ed3a
Apply suggestions from code review
brustolin Jun 13, 2022
410cafa
Format code
getsentry-bot Jun 13, 2022
40b363d
improviments
brustolin Jun 13, 2022
243b550
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 13, 2022
4b49448
Update SentryANRTrackingIntegrationTests.swift
brustolin Jun 13, 2022
8a9264e
Update SentryANRTrackingIntegrationTests.swift
brustolin Jun 14, 2022
ae0b168
Merge branch 'master' into feat/anr-report
brustolin Jun 14, 2022
495a1d7
Update SentryANRTrackingIntegrationTests.swift
brustolin Jun 14, 2022
00659ac
remove default timeout
brustolin Jun 14, 2022
b5105a7
improving tests
brustolin Jun 14, 2022
109ed7f
Format code
getsentry-bot Jun 14, 2022
920226f
Update SentryANRTrackingIntegration.m
brustolin Jun 14, 2022
f336638
Merge branch 'feat/anr-report' of https://github.com/getsentry/sentry…
brustolin Jun 14, 2022
97b483a
Format code
getsentry-bot Jun 14, 2022
2737889
Main Thread
brustolin Jun 15, 2022
b49f671
Update SentryThreadInspector.h
brustolin Jun 20, 2022
47bf7a9
Disabled by default
brustolin Jun 20, 2022
3c6bcad
Merge branch 'master' into feat/anr-report
brustolin Jun 20, 2022
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 Sources/Sentry/Public/SentryThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ SENTRY_NO_INIT
*/
@property (nonatomic, copy) NSNumber *_Nullable current;

/**
* The thread from which this SentryThread extracted its information
*/
@property (nonatomic) uintptr_t thread;

/**
* Initializes a SentryThread with its id
* @param threadId NSNumber
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryANRTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval

- (void)detectANRs
{
NSThread.currentThread.name = @"io.sentry.anr-tracker";
NSThread.currentThread.name = @"io.sentry.app-hang-tracker";

self.thread = NSThread.currentThread;

Expand Down
20 changes: 17 additions & 3 deletions Sources/Sentry/SentryANRTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,31 @@ - (void)uninstall

- (void)anrDetected
{
SentryThreadInspector *threadInspector = SentrySDK.currentHub.getClient.threadInspector;

NSString *message = [NSString stringWithFormat:@"App hanging for at least %li ms.",
(long)(self.options.appHangTimeoutInterval * 1000)];

NSArray<SentryThread *> *threads =
[SentrySDK.currentHub.getClient.threadInspector getCurrentThreadsWithStackTrace:YES];
NSArray<SentryThread *> *threads = [threadInspector getCurrentThreadsWithStackTrace:YES];

SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError];
SentryException *sentryException = [[SentryException alloc] initWithValue:message
type:@"App Hanging"];
sentryException.mechanism = [[SentryMechanism alloc] initWithType:@"AppHang"];
sentryException.stacktrace = threads[0].stacktrace;

SentryThread *mainThread;

for (SentryThread *sentryThread in threads) {
if ([threadInspector isMainThread:sentryThread.thread]) {
sentryThread.current = @YES;
mainThread = sentryThread;
} else {
sentryThread.current = @NO;
}
}

sentryException.stacktrace = [mainThread stacktrace];

event.exceptions = @[ sentryException ];
event.threads = threads;

Expand Down
15 changes: 14 additions & 1 deletion Sources/Sentry/SentryThreadInspector.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#import "SentryStacktrace.h"
#import "SentryStacktraceBuilder.h"
#import "SentryThread.h"
#include <pthread.h>

SentryCrashThread mainThreadID;

@interface
SentryThreadInspector ()
Expand All @@ -14,6 +17,11 @@

@implementation SentryThreadInspector

+ (void)initialize
{
mainThreadID = pthread_mach_thread_np(pthread_self());
}

- (id)initWithStacktraceBuilder:(SentryStacktraceBuilder *)stacktraceBuilder
andMachineContextWrapper:(id<SentryCrashMachineContextWrapper>)machineContextWrapper
{
Expand Down Expand Up @@ -47,14 +55,14 @@ - (id)initWithStacktraceBuilder:(SentryStacktraceBuilder *)stacktraceBuilder
for (int i = 0; i < threadCount; i++) {
SentryCrashThread thread = [self.machineContextWrapper getThread:context withIndex:i];
SentryThread *sentryThread = [[SentryThread alloc] initWithThreadId:@(i)];
sentryThread.thread = thread;

sentryThread.name = [self getThreadName:thread];

sentryThread.crashed = @NO;
bool isCurrent = thread == sentrycrashthread_self();
sentryThread.current = @(isCurrent);

// For now we can only retrieve the stack trace of the current thread.
if (isCurrent) {
sentryThread.stacktrace = [self.stacktraceBuilder buildStacktraceForCurrentThread];
} else if (getAllStacktraces) {
Expand Down Expand Up @@ -84,4 +92,9 @@ - (NSString *)getThreadName:(SentryCrashThread)thread
return threadName;
}

- (BOOL)isMainThread:(SentryCrashThread)thread
{
return thread == mainThreadID;
}

@end
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryThreadInspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ SENTRY_NO_INIT
- (NSArray<SentryThread *> *)getCurrentThreads;

- (NSArray<SentryThread *> *)getCurrentThreadsWithStackTrace:(BOOL)getAllStacktraces;

- (BOOL)isMainThread:(SentryCrashThread)thread;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {

override func tearDown() {
sut.uninstall()
clearTestState()
super.tearDown()
}

Expand Down Expand Up @@ -66,6 +67,7 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {

func testANRDetected_EventCaptured() {
givenInitializedTracker()
setUpThreadInspector()

Dynamic(sut).anrDetected()

Expand All @@ -80,6 +82,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
XCTAssertEqual(ex.type, "App Hanging")
XCTAssertEqual(ex.value, "App hanging for at least 4500 ms.")
XCTAssertNotNil(ex.stacktrace)
XCTAssertEqual(ex.stacktrace?.frames.first?.function, "main")
XCTAssertTrue(event?.threads?[1].current?.boolValue ?? false)

guard let threads = event?.threads else {
XCTFail("ANR Exception not found")
Expand All @@ -102,4 +106,32 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
sut = SentryANRTrackingIntegration()
sut.install(with: self.options)
}

private func setUpThreadInspector() {
let threadInspector = TestThreadInspector.instance
threadInspector.mainThread = 256

let frame1 = Sentry.Frame()
frame1.function = "Second_frame_function"

let thread1 = Sentry.Thread(threadId: 0)
thread1.thread = 245_432
thread1.stacktrace = Stacktrace(frames: [frame1], registers: [:])
thread1.current = true

let frame2 = Sentry.Frame()
frame2.function = "main"

let thread2 = Sentry.Thread(threadId: 1)
thread2.thread = 256
thread2.stacktrace = Stacktrace(frames: [frame2], registers: [:])
thread2.current = false

threadInspector.allThreds = [
thread1,
thread2
]

SentrySDK.currentHub().getClient()?.threadInspector = threadInspector
}
}
13 changes: 12 additions & 1 deletion Tests/SentryTests/SentryCrash/TestThreadInspector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Foundation

class TestThreadInspector: SentryThreadInspector {

var mainThread: SentryCrashThread?
var allThreds: [Sentry.Thread]?

static var instance: TestThreadInspector {
// We need something to pass to the super initializer, because the empty initializer has been marked unavailable.
let inAppLogic = SentryInAppLogic(inAppIncludes: [], inAppExcludes: [])
Expand All @@ -11,7 +14,15 @@ class TestThreadInspector: SentryThreadInspector {
}

override func getCurrentThreads() -> [Sentry.Thread] {
return [TestData.thread]
return allThreds ?? [TestData.thread]
}

override func getCurrentThreads(withStackTrace getAllStacktraces: Bool) -> [Sentry.Thread] {
return allThreds ?? [TestData.thread]
}

override func isMainThread(_ thread: SentryCrashThread) -> Bool {
return thread == mainThread
}

}