Skip to content

Commit

Permalink
update headers
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Sep 6, 2024
1 parent e88e2d0 commit 99a8bef
Show file tree
Hide file tree
Showing 40 changed files with 112 additions and 534 deletions.
10 changes: 2 additions & 8 deletions sentry-kotlin-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,8 @@ kotlin {
macosArm64()
).forEach {
it.compilations.getByName("main") {
cinterops.create("Sentry.NSException") {
includeDirs("$projectDir/src/nativeInterop/cinterop/SentryNSException")
}
cinterops.create("Sentry.Scope") {
includeDirs("$projectDir/src/nativeInterop/cinterop/SentryScope")
}
cinterops.create("Sentry.PrivateSentrySDKOnly") {
includeDirs("$projectDir/src/nativeInterop/cinterop/SentryPrivateSentrySDKOnly")
cinterops.create("Sentry.Internal") {
includeDirs("$projectDir/src/nativeInterop/cinterop/SentryInternal")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import io.sentry.kotlin.multiplatform.extensions.toKmpUser
import io.sentry.kotlin.multiplatform.extensions.toMutableMap
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.protocol.User
import Scope.Sentry.SentryScope as PrivateCocoaScope

internal class CocoaScopeProvider(private val scope: CocoaScope) : Scope {

/*
This bridge exposes private Cocoa SDK API to fetch internal properties such as user, level, etc.
We need this in order to return properties because the Cocoa SDK doesn't implement getters.
This is only used for get methods.
*/
private val privateScope = scope as? PrivateCocoaScope
private val privateScope = scope as? Internal.Sentry.SentryScope

override var level: SentryLevel?
set(value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.sentry.kotlin.multiplatform

import NSException.Sentry.SentryEvent
import PrivateSentrySDKOnly.Sentry.PrivateSentrySDKOnly
import Internal.Sentry.PrivateSentrySDKOnly
import cocoapods.Sentry.SentrySDK
import io.sentry.kotlin.multiplatform.extensions.toCocoaBreadcrumb
import io.sentry.kotlin.multiplatform.extensions.toCocoaUser
Expand Down Expand Up @@ -38,7 +37,7 @@ internal actual fun SentryPlatformOptions.prepareForInit() {
sdk["packages"] = packages
event?.sdk = sdk

dropKotlinCrashEvent(event as SentryEvent?) as CocoaSentryEvent?
dropKotlinCrashEvent(event)
}

cocoa?.setBeforeSend(modifiedBeforeSend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,37 @@

package io.sentry.kotlin.multiplatform.nsexception

import NSException.Sentry.NSExceptionKt_SentryCrashStackCursorFromNSException
import NSException.Sentry.NSExceptionKt_SentryMechanismSetNotHandled
import NSException.Sentry.NSExceptionKt_SentryThreadSetCrashed
import NSException.Sentry.SentryDependencyContainer
import NSException.Sentry.SentryEnvelope
import NSException.Sentry.SentryEnvelopeHeader
import NSException.Sentry.SentryEnvelopeItem
import NSException.Sentry.SentryEvent
import NSException.Sentry.SentryException
import NSException.Sentry.SentryMechanism
import NSException.Sentry.SentrySDK
import NSException.Sentry.SentryThread
import NSException.Sentry.SentryThreadInspector
import NSException.Sentry.currentHub
import NSException.Sentry.isCrashEvent
import NSException.Sentry.kSentryLevelFatal
import NSException.Sentry.prepareEvent
import NSException.Sentry.storeEnvelope
import NSException.Sentry.threadInspector
import Internal.Sentry.NSExceptionKt_SentryCrashStackCursorFromNSException
import Internal.Sentry.kSentryLevelFatal
import platform.Foundation.NSException
import platform.Foundation.NSNumber

private typealias InternalSentryEvent = Internal.Sentry.SentryEvent
private typealias InternalSentrySDK = Internal.Sentry.SentrySDK
private typealias InternalSentryEnvelope = Internal.Sentry.SentryEnvelope
private typealias InternalSentryDependencyContainer = Internal.Sentry.SentryDependencyContainer
private typealias InternalSentryEnvelopeHeader = Internal.Sentry.SentryEnvelopeHeader
private typealias InternalSentryEnvelopeItem = Internal.Sentry.SentryEnvelopeItem
private typealias InternalSentryThreadInspector = Internal.Sentry.SentryThreadInspector

private typealias CocoapodsSentryEvent = cocoapods.Sentry.SentryEvent
private typealias CocoapodsSentrySDK = cocoapods.Sentry.SentrySDK
private typealias CocoapodsSentryStacktrace = cocoapods.Sentry.SentryStacktrace
private typealias CocoapodsSentryException = cocoapods.Sentry.SentryException
private typealias CocoapodsSentryMechanism = cocoapods.Sentry.SentryMechanism
private typealias CocoapodsSentryThread = cocoapods.Sentry.SentryThread

/**
* Drops the Kotlin crash that follows an unhandled Kotlin exception except our custom SentryEvent.
*/
internal fun dropKotlinCrashEvent(event: SentryEvent?): SentryEvent? {
return event?.takeUnless { it.isCrashEvent && (it.tags?.containsKey(KOTLIN_CRASH_TAG) ?: false) }
internal fun dropKotlinCrashEvent(event: CocoapodsSentryEvent?): CocoapodsSentryEvent? {
return event?.takeUnless {
(it as InternalSentryEvent).isCrashEvent && (
it.tags?.containsKey(
KOTLIN_CRASH_TAG
) ?: false
)
}
}

/**
Expand All @@ -53,8 +57,8 @@ public fun setSentryUnhandledExceptionHook(): Unit = wrapUnhandledExceptionHook
val envelope = throwable.asSentryEnvelope()
// The envelope will be persisted, so we can safely terminate afterwards.
// https://github.com/getsentry/sentry-cocoa/blob/678172142ac1d10f5ed7978f69d16ab03e801057/Sources/Sentry/SentryClient.m#L409
SentrySDK.storeEnvelope(envelope)
SentrySDK.configureScope { scope ->
InternalSentrySDK.storeEnvelope(envelope)
CocoapodsSentrySDK.configureScope { scope ->
scope?.setTagValue(KOTLIN_CRASH_TAG, KOTLIN_CRASH_TAG)
}
}
Expand All @@ -67,56 +71,60 @@ internal const val KOTLIN_CRASH_TAG = "nsexceptionkt.kotlin_crashed"
/**
* Converts `this` [Throwable] to a [SentryEnvelope].
*/
internal fun Throwable.asSentryEnvelope(): SentryEnvelope {
val event = asSentryEvent()
val preparedEvent = SentrySDK.currentHub().let { hub ->
hub.getClient()?.prepareEvent(event, hub.scope, alwaysAttachStacktrace = false, isCrashEvent = true)
internal fun Throwable.asSentryEnvelope(): InternalSentryEnvelope {
val event = asSentryEvent() as InternalSentryEvent
val preparedEvent = InternalSentrySDK.currentHub().let { hub ->
hub.getClient()
?.prepareEvent(event, hub.scope, alwaysAttachStacktrace = false, isCrashEvent = true)
} ?: event
val item = SentryEnvelopeItem(preparedEvent)
val item = InternalSentryEnvelopeItem(preparedEvent)
// TODO: pass traceState when enabling performance monitoring for KMP SDK
val header = SentryEnvelopeHeader(preparedEvent.eventId, null)
return SentryEnvelope(header, listOf(item))
val header = InternalSentryEnvelopeHeader(preparedEvent.eventId, null)
return InternalSentryEnvelope(header, listOf(item))
}

/**
* Converts `this` [Throwable] to a [SentryEvent].
*/
@Suppress("UnnecessaryOptInAnnotation")
private fun Throwable.asSentryEvent(): SentryEvent = SentryEvent(kSentryLevelFatal).apply {
isCrashEvent = true
@Suppress("UNCHECKED_CAST")
val threads = threadInspector?.getCurrentThreadsWithStackTrace() as List<SentryThread>?
this.threads = threads
val currentThread = threads?.firstOrNull { it.current?.boolValue ?: false }?.apply {
NSExceptionKt_SentryThreadSetCrashed(this)
// Crashed threats shouldn't have a stacktrace, the thread_id should be set on the exception instead
// https://develop.sentry.dev/sdk/event-payloads/threads/
stacktrace = null
private fun Throwable.asSentryEvent(): CocoapodsSentryEvent =
CocoapodsSentryEvent(kSentryLevelFatal).apply {
@Suppress("UNCHECKED_CAST")
val threads =
threadInspector?.getCurrentThreadsWithStackTrace() as List<CocoapodsSentryThread>?
this.threads = threads
val currentThread = threads?.firstOrNull { it.current?.boolValue ?: false }?.apply {
setCrashed(NSNumber(true))
// Crashed threats shouldn't have a stacktrace, the thread_id should be set on the exception instead
// https://develop.sentry.dev/sdk/event-payloads/threads/
stacktrace = null
}
debugMeta = threads?.let {
InternalSentryDependencyContainer.sharedInstance().debugImageProvider.getDebugImagesForThreads(
it
)
}
exceptions = this@asSentryEvent
.let { throwable -> throwable.causes.asReversed() + throwable }
.map { it.asNSException().asSentryException(currentThread?.threadId) }
}
debugMeta = threads?.let {
SentryDependencyContainer.sharedInstance().debugImageProvider.getDebugImagesForThreads(it)
}
exceptions = this@asSentryEvent
.let { throwable -> throwable.causes.asReversed() + throwable }
.map { it.asNSException().asSentryException(currentThread?.threadId) }
}

/**
* Converts `this` [NSException] to a [SentryException].
*/
private fun NSException.asSentryException(
threadId: NSNumber?
): SentryException = SentryException(reason ?: "", name ?: "Throwable").apply {
): CocoapodsSentryException = CocoapodsSentryException(reason ?: "", name ?: "Throwable").apply {
this.threadId = threadId
mechanism = SentryMechanism("generic").apply {
NSExceptionKt_SentryMechanismSetNotHandled(this)
mechanism = CocoapodsSentryMechanism("generic").apply {
setHandled(NSNumber(false))
}
stacktrace = threadInspector?.stacktraceBuilder?.let { stacktraceBuilder ->
val cursor = NSExceptionKt_SentryCrashStackCursorFromNSException(this@asSentryException)
val stacktrace = stacktraceBuilder.retrieveStacktraceFromCursor(cursor)
stacktrace
stacktrace as CocoapodsSentryStacktrace
}
}

private val threadInspector: SentryThreadInspector?
get() = SentrySDK.currentHub().getClient()?.threadInspector
private val threadInspector: InternalSentryThreadInspector?
get() = InternalSentrySDK.currentHub().getClient()?.threadInspector
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language = Objective-C
headers = SentryClient.h SentryDebugImageProvider.h SentryEnvelope.h \ SentryHub.h SentryScope.h \
SentryCrashMonitor_NSException.h SentryCrashMonitor_NSException+NSExceptionKt.h \
SentryCrashStackCursor.h SentryDependencyContainer.h SentryHook.h SentrySDK.h \
SentryStacktraceBuilder.h SentryThreadInspector.h PrivateSentrySDKOnly.h \

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The following are snippets from the Sentry Cocoa SDK used to generate Kotlin stubs.
//
// https://github.com/getsentry/sentry-cocoa/blob/64a7645a598b90e217be42333594c981a3ddc642/Sources/Sentry/SentryClient.m
// https://github.com/getsentry/sentry-cocoa/blob/64a7645a598b90e217be42333594c981a3ddc642/Sources/Sentry/Public/SentryClient.h
//
// The MIT License (MIT)
//
Expand All @@ -16,12 +16,12 @@
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

#import <Private/SentryThreadInspector.h>
#import <SentryClient.h>
#import <SentryEvent.h>
#import <SentryScope.h>
#import <Foundation/Foundation.h>
#import "SentryThreadInspector.h"

@interface SentryClient ()
@class SentryEvent, SentryScope, SentryClient;

@interface SentryClient : NSObject

@property (nonatomic, strong, nonnull) SentryThreadInspector *threadInspector;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/rickclephas/NSExceptionKt/blob/master/nsexception-kt-sentry/src/nativeInterop/cinterop/Sentry/Private/SentryCrashMonitor_NSException%2BNSExceptionKt.h
// https://github.com/rickclephas/NSExceptionKt/blob/master/nsexception-kt-sentry/src/nativeInterop/cinterop/Sentry/SentryCrashMonitor_NSException%2BNSExceptionKt.h
//
// Copyright (c) 2022 Rick Clephas
//
Expand All @@ -13,8 +13,8 @@
// copies or substantial portions of the Software.

#import <Foundation/Foundation.h>
#import <Private/SentryCrashMonitor_NSException.h>
#import <Private/SentryCrashStackCursor.h>
#import <SentryCrashMonitor_NSException.h>
#import <SentryCrashStackCursor.h>

// Similar to how Sentry converts stacktraces from NSExceptions
// https://github.com/getsentry/sentry-cocoa/blob/167de8bea5a0effef3aaa5c99c540088de30b361/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_NSException.m#L60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
// in this source code.

#import <Foundation/Foundation.h>
#import <Private/SentryCrashStackCursor.h>
#import <SentryCrashStackCursor.h>

extern void sentrycrashsc_initWithBacktrace(SentryCrashStackCursor *cursor, const uintptr_t *backtrace, int backtraceLength, int skipEntries);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// The above copyright notice and this permission notice shall remain in place
// in this source code.

#import <Private/SentryHook.h>
#import <SentryHook.h>

#define SentryCrashSC_CONTEXT_SIZE 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// copies or substantial portions of the Software.

#import <Foundation/Foundation.h>
#import <SentryDebugMeta.h>
#import <SentryThread.h>

@class SentryDebugMeta, SentryThread;

@interface SentryDebugImageProvider : NSObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
// copies or substantial portions of the Software.

#import <Foundation/Foundation.h>
#import <Private/SentryTraceContext.h>
#import <SentryEvent.h>
#import <SentryId.h>

@class SentryId, SentryTraceContext;

@interface SentryEnvelopeHeader : NSObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
// copies or substantial portions of the Software.

#import <Foundation/Foundation.h>
#import <SentryEvent.h>

@interface SentryEvent ()
@class SentryId;

@property (nonatomic) BOOL isCrashEvent;
@interface SentryEvent : NSObject

@property (nonatomic, assign) BOOL isCrashEvent;

@property (nonatomic, strong) SentryId *eventId;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

#import <Foundation/Foundation.h>
#import <SentryClient.h>
#import <SentryScope.h>

@class SentryScope;

@interface SentryHub : NSObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@

#import <SentryEnvelope.h>
#import <SentryHub.h>
#import <SentryOptions.h>
#import <SentrySDK.h>

@interface SentrySDK ()
@interface SentrySDK : NSObject

+ (void)storeEnvelope:(SentryEnvelope *_Nonnull)envelope;
+ (SentryHub *_Nonnull)currentHub;

@property (nonatomic, nullable, readonly, class) SentryOptions *options;

@end
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#import <Foundation/Foundation.h>
#import "SentryUser.h"
#import "SentryDefines.h"

@class SentryUser;

typedef NS_ENUM(NSUInteger, SentryLevel) {
kSentryLevelNone = 0,
kSentryLevelDebug = 1,
kSentryLevelInfo = 2,
kSentryLevelWarning = 3,
kSentryLevelError = 4,
kSentryLevelFatal = 5
};

@interface SentryScope : NSObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
// copies or substantial portions of the Software.

#import <Foundation/Foundation.h>
#import <Private/SentryCrashStackCursor.h>
#import <SentryStacktrace.h>
#import <SentryCrashStackCursor.h>

@class SentryStacktrace;

@interface SentryStacktraceBuilder : NSObject

Expand Down
Loading

0 comments on commit 99a8bef

Please sign in to comment.