diff --git a/.github/file-filters.yml b/.github/file-filters.yml index 6d8fca24d35..14c3b3058e8 100644 --- a/.github/file-filters.yml +++ b/.github/file-filters.yml @@ -464,7 +464,9 @@ run_thread_inspection_harness_for_prs: - ".github/workflows/run-full-ci-workflow.yml" - "Sources/Sentry/SentryThreadSnapshot.c" - "Sources/Sentry/SentryThreadSnapshotKSCrash.c" + - "Sources/SentryNativeUtils/SentryThreadSuspension.c" - "Sources/Sentry/include/SentryThreadSnapshot.h" + - "Sources/SentryNativeUtils/include/SentryThreadSuspension.h" - "Tests/ThreadInspectionHarness/**" # GH Actions diff --git a/Package.swift b/Package.swift index 4e4ec0b3b37..21db325af7a 100644 --- a/Package.swift +++ b/Package.swift @@ -167,10 +167,13 @@ let sentryPrivateDependencies: [Target.Dependency] = [ ] targets += [ - // Shared Objective-C++ utilities only. No Swift sources or Swift target dependencies. + // Shared native utilities. No Swift sources or Swift target dependencies. .target( name: "SentryNativeUtils", - publicHeadersPath: "include" + publicHeadersPath: "include", + cSettings: cSettings, + cxxSettings: cxxSettings, + linkerSettings: [.linkedLibrary("c++")] ), .target( name: "SentryProfiling", @@ -180,7 +183,7 @@ targets += [ // At least one source file is required, therefore we use a dummy class to satisfy the SPM build system .target( name: "SentryHeaders", - dependencies: envSelectsV10 ? ["_SentryV10Configuration"] : [], + dependencies: envSelectsV10 ? ["_SentryV10Configuration", "SentryNativeUtils"] : ["SentryNativeUtils"], path: "Sources/Sentry", sources: ["SentryDummyPublicEmptyClass.m"], publicHeadersPath: "Public", diff --git a/Package@swift-6.1.swift b/Package@swift-6.1.swift index d94dec5ac50..fb38b70e8a7 100644 --- a/Package@swift-6.1.swift +++ b/Package@swift-6.1.swift @@ -174,10 +174,13 @@ let sentryObjCInternalCSettings: [CSetting] = [ let sentryPrivateDependencies: [Target.Dependency] = ["SentryHeaders"] + kscrashDependencies targets += [ - // Shared Objective-C++ utilities only. No Swift sources or Swift target dependencies. + // Shared native utilities. No Swift sources or Swift target dependencies. .target( name: "SentryNativeUtils", - publicHeadersPath: "include" + publicHeadersPath: "include", + cSettings: cSettings, + cxxSettings: cxxSettings, + linkerSettings: [.linkedLibrary("c++")] ), .target( name: "SentryProfiling", @@ -187,7 +190,7 @@ targets += [ // At least one source file is required, therefore we use a dummy class to satisfy the SPM build system .target( name: "SentryHeaders", - dependencies: envSelectsV10 ? [.target(name: "_SentryV10Configuration")] : [], + dependencies: envSelectsV10 ? ["_SentryV10Configuration", "SentryNativeUtils"] : ["SentryNativeUtils"], path: "Sources/Sentry", sources: ["SentryDummyPublicEmptyClass.m"], publicHeadersPath: "Public", diff --git a/Package@swift-6.2.swift b/Package@swift-6.2.swift index bb145e365d9..b5a01c23ad7 100644 --- a/Package@swift-6.2.swift +++ b/Package@swift-6.2.swift @@ -170,10 +170,13 @@ let sentryObjCInternalCSettings: [CSetting] = [ let sentryPrivateDependencies: [Target.Dependency] = ["SentryHeaders"] + kscrashDependencies targets += [ - // Shared Objective-C++ utilities only. No Swift sources or Swift target dependencies. + // Shared native utilities. No Swift sources or Swift target dependencies. .target( name: "SentryNativeUtils", - publicHeadersPath: "include" + publicHeadersPath: "include", + cSettings: cSettings, + cxxSettings: cxxSettings, + linkerSettings: [.linkedLibrary("c++")] ), .target( name: "SentryProfiling", @@ -183,7 +186,7 @@ targets += [ // At least one source file is required, therefore we use a dummy class to satisfy the SPM build system .target( name: "SentryHeaders", - dependencies: envSelectsV10 ? [.target(name: "_SentryV10Configuration")] : [], + dependencies: envSelectsV10 ? ["_SentryV10Configuration", "SentryNativeUtils"] : ["SentryNativeUtils"], path: "Sources/Sentry", sources: ["SentryDummyPublicEmptyClass.m"], publicHeadersPath: "Public", diff --git a/Sentry.xcodeproj/project.pbxproj b/Sentry.xcodeproj/project.pbxproj index 829a8424654..8efffe44576 100644 --- a/Sentry.xcodeproj/project.pbxproj +++ b/Sentry.xcodeproj/project.pbxproj @@ -7,6 +7,12 @@ objects = { /* Begin PBXBuildFile section */ + 9412E63BADA1611A452CCA78 /* SentryProfilingMemory.h in Headers */ = {isa = PBXBuildFile; fileRef = 6CAE64549643A3AEC7DCB207 /* SentryProfilingMemory.h */; }; + EEE63B62D2D5581002A7D76D /* SentryProfilingMemory.h in Headers */ = {isa = PBXBuildFile; fileRef = 6CAE64549643A3AEC7DCB207 /* SentryProfilingMemory.h */; }; + FBE953000000000000000003 /* SentryProfilingRuntime.h in Headers */ = {isa = PBXBuildFile; fileRef = FBE953000000000000000001 /* SentryProfilingRuntime.h */; }; + FBE953000000000000000004 /* SentryProfilingRuntime.h in Headers */ = {isa = PBXBuildFile; fileRef = FBE953000000000000000001 /* SentryProfilingRuntime.h */; }; + FBE953000000000000000005 /* SentryProfilingRuntime.m in Sources */ = {isa = PBXBuildFile; fileRef = FBE953000000000000000002 /* SentryProfilingRuntime.m */; }; + FBE953000000000000000006 /* SentryProfilingRuntime.m in Sources */ = {isa = PBXBuildFile; fileRef = FBE953000000000000000002 /* SentryProfilingRuntime.m */; }; 01F8480438964E6B9718EC9F /* SentryAppStartMeasurementProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = A71411BCABB1428EB4403ADF /* SentryAppStartMeasurementProvider.m */; }; 03244A18FAB65DD14EC57511 /* SentryThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 639FCF9A1EBC7F9500778193 /* SentryThread.h */; settings = {ATTRIBUTES = (Public, ); }; }; 0354A22B2A134D9C003C3A04 /* SentryProfilerState.h in Headers */ = {isa = PBXBuildFile; fileRef = 0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */; }; @@ -22,7 +28,6 @@ 03F84D2727DD414C008FE43F /* SentryMachLogging.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 03F84D1B27DD414C008FE43F /* SentryMachLogging.hpp */; }; 03F84D2827DD414C008FE43F /* SentryCPU.h in Headers */ = {isa = PBXBuildFile; fileRef = 03F84D1C27DD414C008FE43F /* SentryCPU.h */; }; 03F84D3227DD4191008FE43F /* SentryProfiler.mm in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D2B27DD4191008FE43F /* SentryProfiler.mm */; }; - 03F84D3327DD4191008FE43F /* SentryMachLogging.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D2C27DD4191008FE43F /* SentryMachLogging.cpp */; }; 03F84D3427DD4191008FE43F /* SentryThreadMetadataCache.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D2D27DD4191008FE43F /* SentryThreadMetadataCache.cpp */; }; 03F84D3527DD4191008FE43F /* SentryThreadHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D2E27DD4191008FE43F /* SentryThreadHandle.cpp */; }; 03F84D3727DD4191008FE43F /* SentrySamplingProfiler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D3027DD4191008FE43F /* SentrySamplingProfiler.cpp */; }; @@ -192,7 +197,6 @@ 63B818FA1EC34639002FDF4C /* SentryDebugMeta.m in Sources */ = {isa = PBXBuildFile; fileRef = 63B818F81EC34639002FDF4C /* SentryDebugMeta.m */; }; 63BE85701ECEC6DE00DC44F5 /* SentryDateUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 63BE856E1ECEC6DE00DC44F5 /* SentryDateUtils.h */; settings = {ATTRIBUTES = (Private, ); }; }; 63BE85711ECEC6DE00DC44F5 /* SentryDateUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 63BE856F1ECEC6DE00DC44F5 /* SentryDateUtils.m */; }; - 63FE710520DA4C1000CDBAE8 /* SentryAsyncSafeLog.c in Sources */ = {isa = PBXBuildFile; fileRef = 63FE700920DA4C1000CDBAE8 /* SentryAsyncSafeLog.c */; }; 63FE713D20DA4C1100CDBAE8 /* SentryAsyncSafeLog.h in Headers */ = {isa = PBXBuildFile; fileRef = 63FE702520DA4C1000CDBAE8 /* SentryAsyncSafeLog.h */; }; 64B5DCF5460ADAFE0D2ED3B2 /* SentryProfilerTestHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 848A451C2BBF9504006AAAEC /* SentryProfilerTestHelpers.m */; }; 66CF4D940B1856F464BED4F8 /* SentryDateUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 63BE856F1ECEC6DE00DC44F5 /* SentryDateUtils.m */; }; @@ -296,9 +300,11 @@ 879900020000000000000001 /* SentryThreadSnapshot.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000001 /* SentryThreadSnapshot.c */; }; 879900020000000000000002 /* SentryThreadSnapshotKSCrash.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000002 /* SentryThreadSnapshotKSCrash.c */; }; 879900020000000000000003 /* SentryThreadSnapshot.h in Headers */ = {isa = PBXBuildFile; fileRef = 879900010000000000000003 /* SentryThreadSnapshot.h */; }; + FBE954000000000000000003 /* SentryThreadSuspension.h in Headers */ = {isa = PBXBuildFile; fileRef = FBE954000000000000000001 /* SentryThreadSuspension.h */; }; 879900020000000000000011 /* SentryThreadSnapshot.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000001 /* SentryThreadSnapshot.c */; }; 879900020000000000000012 /* SentryThreadSnapshotKSCrash.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000002 /* SentryThreadSnapshotKSCrash.c */; }; 879900020000000000000013 /* SentryThreadSnapshot.h in Headers */ = {isa = PBXBuildFile; fileRef = 879900010000000000000003 /* SentryThreadSnapshot.h */; }; + FBE954000000000000000004 /* SentryThreadSuspension.h in Headers */ = {isa = PBXBuildFile; fileRef = FBE954000000000000000001 /* SentryThreadSuspension.h */; }; 879900020000000000000021 /* SentryMemory.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000021 /* SentryMemory.c */; }; 879900020000000000000022 /* SentryMemory.h in Headers */ = {isa = PBXBuildFile; fileRef = 879900010000000000000022 /* SentryMemory.h */; }; 879900020000000000000031 /* SentryMemory.c in Sources */ = {isa = PBXBuildFile; fileRef = 879900010000000000000021 /* SentryMemory.c */; }; @@ -391,7 +397,6 @@ B46A2CB892B3F7767E1B812D /* SentryTraceProfiler.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A305552BC9EF8C00D84283 /* SentryTraceProfiler.h */; }; B598CC75AB060B3F067D3186 /* SentryProfilerState.h in Headers */ = {isa = PBXBuildFile; fileRef = 0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */; }; B65A70B8F4F25C74BEF63113 /* SentrySpanProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 8E4E7C7325DAAB49006AB9E2 /* SentrySpanProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; - B76E0470EE325394808025AA /* SentryAsyncSafeLog.c in Sources */ = {isa = PBXBuildFile; fileRef = 63FE700920DA4C1000CDBAE8 /* SentryAsyncSafeLog.c */; }; BCCA84D972241233E5C9F607 /* SentryMechanismContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BECF42726145CD900D9826E /* SentryMechanismContext.m */; }; BCE05CEB1000000000000001 /* SentryKSCrashManagedSignal.c in Sources */ = {isa = PBXBuildFile; fileRef = BCE05CEB1000000000000003 /* SentryKSCrashManagedSignal.c */; }; BCE05CEB1000000000000002 /* SentryKSCrashManagedSignal.c in Sources */ = {isa = PBXBuildFile; fileRef = BCE05CEB1000000000000003 /* SentryKSCrashManagedSignal.c */; }; @@ -522,7 +527,6 @@ F1ECC087136BB8874A85F663 /* SentryCrashReportConverter.m in Sources */ = {isa = PBXBuildFile; fileRef = 6344DDB81EC3115C00D9160D /* SentryCrashReportConverter.m */; }; F25183BF3053504D46EBF367 /* SentryScopeSyncC.c in Sources */ = {isa = PBXBuildFile; fileRef = 7B96572126830D2400C66E25 /* SentryScopeSyncC.c */; }; F292CC41B7B16003698B3536 /* SentryRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = A8AFFCCE2906C03700967CD7 /* SentryRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; - F34C54A34AE2CA522D49A2E9 /* SentryMachLogging.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D2C27DD4191008FE43F /* SentryMachLogging.cpp */; }; F38CA50DE7947ABC6A78A4A1 /* SentryMetricProfiler.h in Headers */ = {isa = PBXBuildFile; fileRef = 844EDD6B2949387000C86F34 /* SentryMetricProfiler.h */; }; F400C0CD2F368A8B0042712D /* SentryProfilerSampleCreation.m in Sources */ = {isa = PBXBuildFile; fileRef = F400C0CC2F368A8B0042712D /* SentryProfilerSampleCreation.m */; }; F400C0D42F368A980042712D /* SentryProfilerSampleCreation.h in Headers */ = {isa = PBXBuildFile; fileRef = F400C0D32F368A980042712D /* SentryProfilerSampleCreation.h */; }; @@ -744,20 +748,23 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 6CAE64549643A3AEC7DCB207 /* SentryProfilingMemory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryProfilingMemory.h; path = Sources/Sentry/include/SentryProfilingMemory.h; sourceTree = SOURCE_ROOT; }; + FBE953000000000000000001 /* SentryProfilingRuntime.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryProfilingRuntime.h; path = ../include/SentryProfilingRuntime.h; sourceTree = ""; }; + FBE953000000000000000002 /* SentryProfilingRuntime.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryProfilingRuntime.m; sourceTree = ""; }; 0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryProfilerState.h; path = Sources/Sentry/include/SentryProfilerState.h; sourceTree = SOURCE_ROOT; }; - 03BCC38D27E2A377003232C7 /* SentryProfilingConditionals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryProfilingConditionals.h; path = ../Public/SentryProfilingConditionals.h; sourceTree = ""; }; + 03BCC38D27E2A377003232C7 /* SentryProfilingConditionals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryProfilingConditionals.h; path = Sources/SentryNativeUtils/include/SentryProfilingConditionals.h; sourceTree = SOURCE_ROOT; }; 03F84D1227DD414C008FE43F /* SentryBacktrace.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryBacktrace.hpp; path = Sources/Sentry/include/SentryBacktrace.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1427DD414C008FE43F /* SentryStackBounds.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryStackBounds.hpp; path = Sources/Sentry/include/SentryStackBounds.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1527DD414C008FE43F /* SentrySamplingProfiler.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentrySamplingProfiler.hpp; path = Sources/Sentry/include/SentrySamplingProfiler.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1627DD414C008FE43F /* SentryStackFrame.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryStackFrame.hpp; path = Sources/Sentry/include/SentryStackFrame.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1727DD414C008FE43F /* SentryThreadHandle.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryThreadHandle.hpp; path = Sources/Sentry/include/SentryThreadHandle.hpp; sourceTree = SOURCE_ROOT; }; - 03F84D1827DD414C008FE43F /* SentryCompiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryCompiler.h; path = Sources/Sentry/include/SentryCompiler.h; sourceTree = SOURCE_ROOT; }; + 03F84D1827DD414C008FE43F /* SentryCompiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryCompiler.h; path = Sources/SentryNativeUtils/include/SentryCompiler.h; sourceTree = SOURCE_ROOT; }; 03F84D1927DD414C008FE43F /* SentryThreadState.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryThreadState.hpp; path = Sources/Sentry/include/SentryThreadState.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1A27DD414C008FE43F /* SentryThreadMetadataCache.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryThreadMetadataCache.hpp; path = Sources/Sentry/include/SentryThreadMetadataCache.hpp; sourceTree = SOURCE_ROOT; }; - 03F84D1B27DD414C008FE43F /* SentryMachLogging.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryMachLogging.hpp; path = Sources/Sentry/include/SentryMachLogging.hpp; sourceTree = SOURCE_ROOT; }; + 03F84D1B27DD414C008FE43F /* SentryMachLogging.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SentryMachLogging.hpp; path = Sources/SentryNativeUtils/include/SentryMachLogging.hpp; sourceTree = SOURCE_ROOT; }; 03F84D1C27DD414C008FE43F /* SentryCPU.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryCPU.h; path = Sources/Sentry/include/SentryCPU.h; sourceTree = SOURCE_ROOT; }; 03F84D2B27DD4191008FE43F /* SentryProfiler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = SentryProfiler.mm; path = Sources/Sentry/SentryProfiler.mm; sourceTree = SOURCE_ROOT; }; - 03F84D2C27DD4191008FE43F /* SentryMachLogging.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryMachLogging.cpp; path = Sources/Sentry/SentryMachLogging.cpp; sourceTree = SOURCE_ROOT; }; + 03F84D2C27DD4191008FE43F /* SentryMachLogging.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryMachLogging.cpp; path = Sources/SentryNativeUtils/SentryMachLogging.cpp; sourceTree = SOURCE_ROOT; }; 03F84D2D27DD4191008FE43F /* SentryThreadMetadataCache.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryThreadMetadataCache.cpp; path = Sources/Sentry/SentryThreadMetadataCache.cpp; sourceTree = SOURCE_ROOT; }; 03F84D2E27DD4191008FE43F /* SentryThreadHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryThreadHandle.cpp; path = Sources/Sentry/SentryThreadHandle.cpp; sourceTree = SOURCE_ROOT; }; 03F84D3027DD4191008FE43F /* SentrySamplingProfiler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentrySamplingProfiler.cpp; path = Sources/Sentry/SentrySamplingProfiler.cpp; sourceTree = SOURCE_ROOT; }; @@ -768,7 +775,7 @@ 0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProviderHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryViewHierarchyProviderHelper.h; path = include/SentryViewHierarchyProviderHelper.h; sourceTree = ""; }; 0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProviderHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryViewHierarchyProviderHelper.m; sourceTree = ""; }; D4E1A0012F11000100A00001 /* SentryCurrentThreadInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryCurrentThreadInfo.h; path = include/SentryCurrentThreadInfo.h; sourceTree = ""; }; - 0A9E917028DC7E7000FB4182 /* SentryInternalCDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryInternalCDefines.h; path = include/SentryInternalCDefines.h; sourceTree = ""; }; + 0A9E917028DC7E7000FB4182 /* SentryInternalCDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryInternalCDefines.h; path = Sources/SentryNativeUtils/include/SentryInternalCDefines.h; sourceTree = SOURCE_ROOT; }; 1275E5EA5712B13FE13C519A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 15E0A8E9240F2C8F00F044E3 /* SentrySerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentrySerialization.h; path = include/SentrySerialization.h; sourceTree = ""; }; 15E0A8EC240F2CB000F044E3 /* SentrySerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentrySerialization.m; sourceTree = ""; }; @@ -843,8 +850,8 @@ 63B818F81EC34639002FDF4C /* SentryDebugMeta.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryDebugMeta.m; sourceTree = ""; }; 63BE856E1ECEC6DE00DC44F5 /* SentryDateUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryDateUtils.h; path = include/SentryDateUtils.h; sourceTree = ""; }; 63BE856F1ECEC6DE00DC44F5 /* SentryDateUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryDateUtils.m; sourceTree = ""; }; - 63FE700920DA4C1000CDBAE8 /* SentryAsyncSafeLog.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SentryAsyncSafeLog.c; sourceTree = ""; }; - 63FE702520DA4C1000CDBAE8 /* SentryAsyncSafeLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SentryAsyncSafeLog.h; sourceTree = ""; }; + 63FE700920DA4C1000CDBAE8 /* SentryAsyncSafeLog.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = Sources/SentryNativeUtils/SentryAsyncSafeLog.c; sourceTree = SOURCE_ROOT; }; + 63FE702520DA4C1000CDBAE8 /* SentryAsyncSafeLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Sources/SentryNativeUtils/include/SentryAsyncSafeLog.h; sourceTree = SOURCE_ROOT; }; 6E190D66E13B263953A7FB3A /* SentryObjC.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SentryObjC.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7B0DC72D288698F70039995F /* NSMutableDictionary+Sentry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "NSMutableDictionary+Sentry.h"; path = "include/NSMutableDictionary+Sentry.h"; sourceTree = ""; }; 7B0DC72E288698F70039995F /* NSMutableDictionary+Sentry.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSMutableDictionary+Sentry.m"; sourceTree = ""; }; @@ -950,8 +957,10 @@ 84F994E72A6894BD00EC0190 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/SystemConfiguration.framework; sourceTree = DEVELOPER_DIR; }; 85AE23B0FFFD4830A3D05EBB /* SentryAppStartMeasurementProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryAppStartMeasurementProvider.h; path = include/SentryAppStartMeasurementProvider.h; sourceTree = ""; }; 879900010000000000000001 /* SentryThreadSnapshot.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SentryThreadSnapshot.c; sourceTree = ""; }; + FBE954000000000000000002 /* SentryThreadSuspension.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = Sources/SentryNativeUtils/SentryThreadSuspension.c; sourceTree = SOURCE_ROOT; }; 879900010000000000000002 /* SentryThreadSnapshotKSCrash.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SentryThreadSnapshotKSCrash.c; sourceTree = ""; }; 879900010000000000000003 /* SentryThreadSnapshot.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryThreadSnapshot.h; path = include/SentryThreadSnapshot.h; sourceTree = ""; }; + FBE954000000000000000001 /* SentryThreadSuspension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryThreadSuspension.h; path = Sources/SentryNativeUtils/include/SentryThreadSuspension.h; sourceTree = SOURCE_ROOT; }; 879900010000000000000021 /* SentryMemory.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SentryMemory.c; sourceTree = ""; }; 879900010000000000000022 /* SentryMemory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryMemory.h; path = include/SentryMemory.h; sourceTree = ""; }; 879900010000000000000041 /* SentryThreadCrumb.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SentryThreadCrumb.c; sourceTree = ""; }; @@ -1582,8 +1591,10 @@ 7BA61CC7247D125400C130A8 /* SentryDefaultThreadInspector.h */, 7BA61CC9247D128B00C130A8 /* SentryDefaultThreadInspector.m */, 879900010000000000000001 /* SentryThreadSnapshot.c */, + FBE954000000000000000002 /* SentryThreadSuspension.c */, 879900010000000000000002 /* SentryThreadSnapshotKSCrash.c */, 879900010000000000000003 /* SentryThreadSnapshot.h */, + FBE954000000000000000001 /* SentryThreadSuspension.h */, 879900010000000000000021 /* SentryMemory.c */, 879900010000000000000022 /* SentryMemory.h */, 879900010000000000000041 /* SentryThreadCrumb.c */, @@ -1910,10 +1921,12 @@ FBE950000000000000000001 /* SentryProfilingId.h */, FBE951000000000000000001 /* SentryProfilingSample.h */, FBE952000000000000000001 /* SentryProfilingLog.h */, + FBE953000000000000000001 /* SentryProfilingRuntime.h */, FBB100000000000000000006 /* SentryProfilerCancellation.m */, FBE950000000000000000002 /* SentryProfilingId.m */, FBE951000000000000000002 /* SentryProfilingSample.m */, FBE952000000000000000002 /* SentryProfilingLog.m */, + FBE953000000000000000002 /* SentryProfilingRuntime.m */, 84A305562BC9EF8C00D84283 /* SentryTraceProfiler.mm */, E12A00000000000000000002 /* SentryProfilerSDKAdapter.mm */, 848A451B2BBF9504006AAAEC /* SentryProfilerTestHelpers.h */, @@ -2198,6 +2211,8 @@ FBE950000000000000000003 /* SentryProfilingId.h in Headers */, FBE951000000000000000003 /* SentryProfilingSample.h in Headers */, FBE952000000000000000003 /* SentryProfilingLog.h in Headers */, + FBE953000000000000000003 /* SentryProfilingRuntime.h in Headers */, + 9412E63BADA1611A452CCA78 /* SentryProfilingMemory.h in Headers */, 62E59A532E8FB70000DB7A7B /* SentryTracePropagation.h in Headers */, 04EF504E4D2707F39AE66D71 /* SentryURLRequestWrapper.h in Headers */, FAA414542ED7616800B269CD /* SentrySessionReplayHybridSDK.h in Headers */, @@ -2226,6 +2241,7 @@ 63BE85701ECEC6DE00DC44F5 /* SentryDateUtils.h in Headers */, 7BA61CC8247D125400C130A8 /* SentryDefaultThreadInspector.h in Headers */, 879900020000000000000003 /* SentryThreadSnapshot.h in Headers */, + FBE954000000000000000003 /* SentryThreadSuspension.h in Headers */, 879900020000000000000022 /* SentryMemory.h in Headers */, 879900020000000000000042 /* SentryThreadCrumb.h in Headers */, 6271ADF32BA06D9B0098D2E9 /* SentryInternalSerializable.h in Headers */, @@ -2390,6 +2406,8 @@ FBE950000000000000000004 /* SentryProfilingId.h in Headers */, FBE951000000000000000004 /* SentryProfilingSample.h in Headers */, FBE952000000000000000004 /* SentryProfilingLog.h in Headers */, + FBE953000000000000000004 /* SentryProfilingRuntime.h in Headers */, + EEE63B62D2D5581002A7D76D /* SentryProfilingMemory.h in Headers */, 70AC34E4A05B026560C07440 /* SentryTracePropagation.h in Headers */, 0D6923E1F9784138F7C7609D /* SentryURLRequestWrapper.h in Headers */, A158CB14D141F4D5DEBED6EC /* SentrySessionReplayHybridSDK.h in Headers */, @@ -2418,6 +2436,7 @@ 985EE6687E2CC46D2D1B57CD /* SentryDateUtils.h in Headers */, 2F6D0BFEC4980798559B1A7D /* SentryDefaultThreadInspector.h in Headers */, 879900020000000000000013 /* SentryThreadSnapshot.h in Headers */, + FBE954000000000000000004 /* SentryThreadSuspension.h in Headers */, 879900020000000000000032 /* SentryMemory.h in Headers */, 879900020000000000000052 /* SentryThreadCrumb.h in Headers */, 5FD453C8920A9A949807E56E /* SentryInternalSerializable.h in Headers */, @@ -2852,6 +2871,7 @@ FBE950000000000000000005 /* SentryProfilingId.m in Sources */, FBE951000000000000000005 /* SentryProfilingSample.m in Sources */, FBE952000000000000000005 /* SentryProfilingLog.m in Sources */, + FBE953000000000000000005 /* SentryProfilingRuntime.m in Sources */, F40C5EC42F3BC7DE00CAEC02 /* SentryProfileDebugLoggerHelper.m in Sources */, 7B96572226830D2400C66E25 /* SentryScopeSyncC.c in Sources */, 8E4E7C8225DAB2A5006AB9E2 /* SentryTracer.m in Sources */, @@ -2916,11 +2936,9 @@ 8453421228BE855D00C22EEC /* SentrySampleDecision.m in Sources */, 7B7D872E2486482600D2ECFF /* SentryStacktraceBuilder.m in Sources */, D8603DD6284F8497000E1227 /* SentryBaggage.m in Sources */, - 03F84D3327DD4191008FE43F /* SentryMachLogging.cpp in Sources */, 7B4E375F258231FC00059C93 /* SentryAttachment.m in Sources */, 636085141ED47BE600E8599E /* SentryFileManagerHelper.m in Sources */, 7BC9A20628F41781001E7C4C /* SentryMeasurementUnit.m in Sources */, - 63FE710520DA4C1000CDBAE8 /* SentryAsyncSafeLog.c in Sources */, 62E59A5A2E8FB85300DB7A7B /* SentryTracePropagation.m in Sources */, 9DB20DA9E3AD77978360302A /* SentryURLRequestWrapper.m in Sources */, 639FCF991EBC7B9700778193 /* SentryEvent.m in Sources */, @@ -2994,6 +3012,7 @@ FBE950000000000000000006 /* SentryProfilingId.m in Sources */, FBE951000000000000000006 /* SentryProfilingSample.m in Sources */, FBE952000000000000000006 /* SentryProfilingLog.m in Sources */, + FBE953000000000000000006 /* SentryProfilingRuntime.m in Sources */, A144564591C80E9166947DC8 /* SentryProfileDebugLoggerHelper.m in Sources */, F25183BF3053504D46EBF367 /* SentryScopeSyncC.c in Sources */, 96202BB0106F0BD614D3BE26 /* SentryTracer.m in Sources */, @@ -3056,11 +3075,9 @@ FEFB44709CAEFFE4B48D7E39 /* SentrySampleDecision.m in Sources */, 74CE90FEA418FCA3FDE9557F /* SentryStacktraceBuilder.m in Sources */, D50E6D5995E11E5EC48C8135 /* SentryBaggage.m in Sources */, - F34C54A34AE2CA522D49A2E9 /* SentryMachLogging.cpp in Sources */, C6A1542736BB225D03408984 /* SentryAttachment.m in Sources */, 53D2D8C2C5ABACA602199259 /* SentryFileManagerHelper.m in Sources */, 53BA8355CF85458B7802D03C /* SentryMeasurementUnit.m in Sources */, - B76E0470EE325394808025AA /* SentryAsyncSafeLog.c in Sources */, 13CF17416E0877EEDD04634E /* SentryTracePropagation.m in Sources */, 14B2EA3EBB319D3A7BF7BB9C /* SentryURLRequestWrapper.m in Sources */, 839F97EA0A6D47C61F54F08D /* SentryEvent.m in Sources */, diff --git a/Sources/AGENTS.md b/Sources/AGENTS.md index 27a9d4d5e8c..d043841e7b9 100644 --- a/Sources/AGENTS.md +++ b/Sources/AGENTS.md @@ -65,9 +65,9 @@ - Use compile-time gates for code that cannot compile or link on a target, not for ordinary runtime behavior - In Swift, use `os(...)`, `targetEnvironment(...)`, and `canImport(...)` for platform and module capabilities - Gate UI code by both supported platforms and `!SENTRY_NO_UI_FRAMEWORK` -- In Objective-C and C/C++, prefer capability macros from [`SentryDefines.h`](Sentry/Public/SentryDefines.h) and [`SentryProfilingConditionals.h`](Sentry/Public/SentryProfilingConditionals.h), including `SENTRY_HAS_UIKIT`, `SENTRY_TARGET_REPLAY_SUPPORTED`, and `SENTRY_TARGET_PROFILING_SUPPORTED` +- In Objective-C and C/C++, prefer capability macros from [`SentryDefines.h`](Sentry/Public/SentryDefines.h) and [`SentryProfilingConditionals.h`](SentryNativeUtils/include/SentryProfilingConditionals.h), including `SENTRY_HAS_UIKIT`, `SENTRY_TARGET_REPLAY_SUPPORTED`, and `SENTRY_TARGET_PROFILING_SUPPORTED` - Keep Swift, Objective-C, and `SentryObjC` declaration and implementation gates aligned -- Preserve older Xcode compatibility with `#if swift(>=...)`, compiler feature probes from [`SentryCompiler.h`](Sentry/include/SentryCompiler.h), and `canImport` or `__has_include` checks +- Preserve older Xcode compatibility with `#if swift(>=...)`, compiler feature probes from [`SentryCompiler.h`](SentryNativeUtils/include/SentryCompiler.h), and `canImport` or `__has_include` checks - Use `#available` or `@available` for runtime OS availability only after the code can compile with the oldest supported Xcode - When newer SDK symbols are absent from older Xcode headers, combine runtime availability with a compile-time gate or dynamic lookup instead of referencing the symbol directly diff --git a/Sources/Configuration/Sentry.xcconfig b/Sources/Configuration/Sentry.xcconfig index 7ac7ed93f41..76c22676b9f 100644 --- a/Sources/Configuration/Sentry.xcconfig +++ b/Sources/Configuration/Sentry.xcconfig @@ -3,10 +3,6 @@ SENTRY_SDK_MACH_O_TYPE = mh_dylib MACH_O_TYPE = $(SENTRY_SDK_MACH_O_TYPE) -// Retain the existing C exports on platforms without internal callers, such as watchOS. -SENTRY_NATIVE_UTILS_EXPORTS_mh_dylib = -u _timeIntervalToNanoseconds -OTHER_LDFLAGS = $(inherited) $(SENTRY_NATIVE_UTILS_EXPORTS_$(MACH_O_TYPE)) - INFOPLIST_FILE = Sources/Resources/Info.plist SENTRY_MODULE_NAME = Sentry @@ -38,6 +34,11 @@ LD_DYLIB_INSTALL_NAME = $(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_ LD_RUNPATH_SEARCH_PATHS = $(inherited) @executable_path/Frameworks @loader_path/Frameworks USE_HEADERMAP = YES +// Keep shared C entry points exported even on platforms where the SDK has no internal caller. +// Do not force-load the archive: Xcode also places static package dependencies in it. +SENTRY_NATIVE_UTILS_EXPORTS_mh_dylib = -u _sentry_asyncLogC -u _sentryThreadSuspensionTryAcquire -u _timeIntervalToNanoseconds +OTHER_LDFLAGS = $(inherited) $(SENTRY_NATIVE_UTILS_EXPORTS_$(MACH_O_TYPE)) + CODE_SIGN_STYLE = Manual DEVELOPMENT_TEAM = CODE_SIGN_IDENTITY = diff --git a/Sources/Configuration/SentryNativeUtils.xcconfig b/Sources/Configuration/SentryNativeUtils.xcconfig index 21e4bf62540..650ae40b6fd 100644 --- a/Sources/Configuration/SentryNativeUtils.xcconfig +++ b/Sources/Configuration/SentryNativeUtils.xcconfig @@ -1,4 +1,4 @@ -// Native utilities are implemented in Objective-C++ (.mm), never Swift. +// Shared native utilities contain no Swift sources. // Keep this target independent of Swift targets and the SDK implementation. #include "DeploymentTargets.xcconfig" @@ -12,6 +12,7 @@ SUPPORTS_MACCATALYST = YES ARCHS = $(ARCHS_STANDARD) arm64e ARCHS[sdk=*simulator] = $(ARCHS_STANDARD) SKIP_INSTALL = YES +GCC_C_LANGUAGE_STANDARD = gnu11 HEADER_SEARCH_PATHS = $(SRCROOT)/Sources/SentryNativeUtils/include $(SRCROOT)/Sources/SentryNativeUtils/include MODULEMAP_FILE = $(SRCROOT)/Sources/SentryNativeUtils/include/module.modulemap OTHER_LDFLAGS = diff --git a/Sources/Sentry/Profiling/SentryProfilerSDKAdapter.mm b/Sources/Sentry/Profiling/SentryProfilerSDKAdapter.mm index b0db8c010b4..e67efe02936 100644 --- a/Sources/Sentry/Profiling/SentryProfilerSDKAdapter.mm +++ b/Sources/Sentry/Profiling/SentryProfilerSDKAdapter.mm @@ -76,6 +76,11 @@ - (SentryProfilingDebugImageSerialization _Nullable)debugImageSerialization return [self.target debugImageSerialization]; } +- (SentryProfilingMemoryReader)memoryReader +{ + return [self.target memoryReader]; +} + - (SentryProfilingSystemMetrics *_Nullable)systemMetrics { return [self.target systemMetrics]; diff --git a/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m b/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m index 42a0c51d265..3fbf4f8f08d 100644 --- a/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m +++ b/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m @@ -6,20 +6,6 @@ # import "SentryInternalDefines.h" # import "SentrySerialization.h" -BOOL -sentry_threadSanitizerIsPresent(void) -{ -# if defined(__has_feature) -# if __has_feature(thread_sanitizer) - return YES; -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wunreachable-code" -# endif // __has_feature(thread_sanitizer) -# endif // defined(__has_feature) - - return NO; -} - # if defined(DEBUG) || defined(SENTRY_UI_TEST_SUPPORT) // Test support. void diff --git a/Sources/Sentry/Profiling/SentryProfilingRuntime.m b/Sources/Sentry/Profiling/SentryProfilingRuntime.m new file mode 100644 index 00000000000..21cc3e45c00 --- /dev/null +++ b/Sources/Sentry/Profiling/SentryProfilingRuntime.m @@ -0,0 +1,17 @@ +#import "SentryProfilingRuntime.h" + +#if SENTRY_TARGET_PROFILING_SUPPORTED + +# import "SentryCompiler.h" + +BOOL +sentry_threadSanitizerIsPresent(void) +{ +# if COMPILER_HAS_CLANG_FEATURE(thread_sanitizer) + return YES; +# else + return NO; +# endif +} + +#endif // SENTRY_TARGET_PROFILING_SUPPORTED diff --git a/Sources/Sentry/SentryBacktrace.cpp b/Sources/Sentry/SentryBacktrace.cpp index 71740b874dc..dcfb7b6273e 100644 --- a/Sources/Sentry/SentryBacktrace.cpp +++ b/Sources/Sentry/SentryBacktrace.cpp @@ -5,13 +5,12 @@ # include "SentryAsyncSafeLog.h" # include "SentryCompiler.h" # include "SentryMachLogging.hpp" -# include "SentryMemory.h" # include "SentryStackBounds.hpp" # include "SentryStackFrame.hpp" # include "SentryThreadHandle.hpp" # include "SentryThreadMetadataCache.hpp" -# include "SentryThreadSnapshot.h" # include "SentryThreadState.hpp" +# include "SentryThreadSuspension.h" # include "SentryTime.h" # include # include @@ -63,7 +62,7 @@ namespace profiling { NOT_TAIL_CALLED NEVER_INLINE std::size_t backtrace(const ThreadHandle &targetThread, const ThreadHandle &callingThread, std::uintptr_t *addresses, const StackBounds &bounds, bool *reachedEndOfStackPtr, - std::size_t maxDepth, std::size_t skip) noexcept + std::size_t maxDepth, SentryProfilingMemoryReader memoryReader, std::size_t skip) noexcept { assert(addresses != nullptr); if (UNLIKELY(maxDepth == 0 || !bounds.isValid())) { @@ -109,7 +108,7 @@ namespace profiling { bool reachedEndOfStack = false; while (depth < maxDepth) { const auto frame = reinterpret_cast(current); - if (!sentryMemoryIsReadable(frame, sizeof(StackFrame))) { + if (!memoryReader(frame, sizeof(StackFrame))) { break; } if (LIKELY(skip == 0)) { @@ -133,8 +132,8 @@ namespace profiling { } void - enumerateBacktracesForAllThreads( - const std::function &f, ThreadMetadataCache *cache) + enumerateBacktracesForAllThreads(const std::function &f, + ThreadMetadataCache *cache, SentryProfilingMemoryReader memoryReader) { const auto pair = ThreadHandle::allExcludingCurrent(); for (const auto &thread : pair.first) { @@ -189,7 +188,7 @@ namespace profiling { } depth = backtrace(*thread, *pair.second, addresses, stackBounds, &reachedEndOfStack, - kMaxBacktraceDepth, 0); + kMaxBacktraceDepth, memoryReader, 0); thread->resume(); } diff --git a/Sources/Sentry/SentryDefaultProfilerSDKBackend.m b/Sources/Sentry/SentryDefaultProfilerSDKBackend.m index 8ea3316fe30..c756c3ad75c 100644 --- a/Sources/Sentry/SentryDefaultProfilerSDKBackend.m +++ b/Sources/Sentry/SentryDefaultProfilerSDKBackend.m @@ -4,6 +4,7 @@ #import "SentryInternalDefines.h" #import "SentryInternalSerializable.h" #import "SentryLogC.h" +#import "SentryMemory.h" #import "SentryProfilerCancellation.h" #import "SentryProfilerConfigurationBridge.h" #import "SentryProfilerDeliveryBridge.h" @@ -52,6 +53,11 @@ @implementation SentryDefaultProfilerSDKBackend { SentrySystemWrapper *_systemWrapper; SentrySDKProfilingLogger *_profilingLogger; } +- (SentryProfilingMemoryReader)memoryReader +{ + return sentryMemoryIsReadable; +} + - (void)activate { _profilingLogger = [[SentrySDKProfilingLogger alloc] init]; diff --git a/Sources/Sentry/SentryProfiler.mm b/Sources/Sentry/SentryProfiler.mm index f324b54edf7..ccd17b9edad 100644 --- a/Sources/Sentry/SentryProfiler.mm +++ b/Sources/Sentry/SentryProfiler.mm @@ -6,8 +6,8 @@ # import "SentryMetricProfiler.h" # import "SentryProfilerSDKAdapter.h" # import "SentryProfilerState+ObjCpp.h" -# import "SentryProfilerTestHelpers.h" # import "SentryProfilingLog.h" +# import "SentryProfilingRuntime.h" # import "SentrySamplingProfiler.hpp" # import "SentryTime.h" @@ -116,12 +116,11 @@ - (void)start return; } - // Pop the clang diagnostic to ignore unreachable code for TSAN runs -# if defined(__has_feature) -# if __has_feature(thread_sanitizer) -# pragma clang diagnostic pop -# endif // __has_feature(thread_sanitizer) -# endif // defined(__has_feature) + SentryProfilingMemoryReader memoryReader = [SentryProfilerSDKAdapter.shared memoryReader]; + if (memoryReader == nullptr) { + SENTRY_PROFILING_LOG_WARN(@"Cannot start profiling without a memory reader."); + return; + } SENTRY_PROFILING_LOG_DEBUG(@"Starting profiler."); @@ -135,7 +134,7 @@ - (void)start [state appendBacktrace:backtraceCopy]; } }, - kSentryProfilerFrequencyHz); + kSentryProfilerFrequencyHz, memoryReader); _samplingProfiler->startSampling(); } diff --git a/Sources/Sentry/SentrySamplingProfiler.cpp b/Sources/Sentry/SentrySamplingProfiler.cpp index 1f006e12f04..3371cdd8ad4 100644 --- a/Sources/Sentry/SentrySamplingProfiler.cpp +++ b/Sources/Sentry/SentrySamplingProfiler.cpp @@ -23,6 +23,7 @@ namespace profiling { mach_timespec_t delaySpec; ThreadMetadataCache *cache; std::function callback; + SentryProfilingMemoryReader memoryReader; std::atomic_uint64_t &numSamples; std::function onThreadStart; }; @@ -65,7 +66,8 @@ namespace profiling { } params->numSamples.fetch_add(1, std::memory_order_relaxed); - enumerateBacktracesForAllThreads(params->callback, params->cache); + enumerateBacktracesForAllThreads( + params->callback, params->cache, params->memoryReader); } pthread_cleanup_pop(1); pthread_cleanup_pop(1); @@ -74,9 +76,10 @@ namespace profiling { } // namespace - SamplingProfiler::SamplingProfiler( - std::function callback, std::uint32_t samplingRateHz) + SamplingProfiler::SamplingProfiler(std::function callback, + std::uint32_t samplingRateHz, SentryProfilingMemoryReader memoryReader) : callback_(std::move(callback)) + , memoryReader_(memoryReader) , cache_(std::make_unique()) , isInitialized_(false) , isSampling_(false) @@ -140,7 +143,7 @@ namespace profiling { } const auto params = new SamplingThreadParams { port_, clock_, delaySpec_, cache_.get(), - callback_, std::ref(numSamples_), std::move(onThreadStart) }; + callback_, memoryReader_, std::ref(numSamples_), std::move(onThreadStart) }; if (SENTRY_ASYNC_SAFE_LOG_ERRNO_RETURN( pthread_create(&thread_, &attr, samplingThreadMain, params)) != 0) { diff --git a/Sources/Sentry/SentryThreadSnapshot.c b/Sources/Sentry/SentryThreadSnapshot.c index c942aef56fd..4be51a3748c 100644 --- a/Sources/Sentry/SentryThreadSnapshot.c +++ b/Sources/Sentry/SentryThreadSnapshot.c @@ -1,23 +1,4 @@ #include "SentryThreadSnapshot.h" -#include - -// Coordinates the SDK's nonfatal bulk-inspection and profiler suspension paths. atomic_flag is -// guaranteed lock-free, so a contender can yield without waiting on an owner that may have -// suspended its thread. -static atomic_flag g_threadSuspensionAdmission = ATOMIC_FLAG_INIT; - -bool -sentryThreadSuspensionTryAcquire(void) -{ - return !atomic_flag_test_and_set_explicit(&g_threadSuspensionAdmission, memory_order_acquire); -} - -void -sentryThreadSuspensionRelease(void) -{ - atomic_flag_clear_explicit(&g_threadSuspensionAdmission, memory_order_release); -} - #include bool diff --git a/Sources/Sentry/include/SentryBacktrace.hpp b/Sources/Sentry/include/SentryBacktrace.hpp index 11fc51129f2..f66ca72b081 100644 --- a/Sources/Sentry/include/SentryBacktrace.hpp +++ b/Sources/Sentry/include/SentryBacktrace.hpp @@ -4,6 +4,7 @@ #if SENTRY_TARGET_PROFILING_SUPPORTED +# include "SentryProfilingMemory.h" # include "SentryStackBounds.hpp" # include "SentryThreadHandle.hpp" # include "SentryThreadMetadataCache.hpp" @@ -38,6 +39,7 @@ namespace profiling { * additional frames that were not recorded because the `maxDepth` was too small. * @param maxDepth The maximum number of addresses to collect, this should * be less than or equal to the size of the buffer. + * @param memoryReader The async-safe C function used to validate each frame before reading it. * @param skip An optional number of stack frames to skip at the beginning. * * @return The actual number of addresses collected. Returns 0 if a backtrace @@ -45,7 +47,8 @@ namespace profiling { */ NOT_TAIL_CALLED NEVER_INLINE std::size_t backtrace(const ThreadHandle &targetThread, const ThreadHandle &callingThread, std::uintptr_t *addresses, const StackBounds &bounds, - bool *reachedEndOfStackPtr, std::size_t maxDepth, std::size_t skip = 0) noexcept; + bool *reachedEndOfStackPtr, std::size_t maxDepth, SentryProfilingMemoryReader memoryReader, + std::size_t skip = 0) noexcept; /** * Attempts to collect backtraces for every thread in the process, except the @@ -56,9 +59,10 @@ namespace profiling { * * @param f The function to call for each entry. * @param cache The cache used to look up thread metadata. + * @param memoryReader The async-safe C function used to validate sampled memory. */ - void enumerateBacktracesForAllThreads( - const std::function &f, ThreadMetadataCache *cache); + void enumerateBacktracesForAllThreads(const std::function &f, + ThreadMetadataCache *cache, SentryProfilingMemoryReader memoryReader); } // namespace profiling } // namespace sentry diff --git a/Sources/Sentry/include/SentryProfilerSDKAdapter.h b/Sources/Sentry/include/SentryProfilerSDKAdapter.h index c2f381d65f5..106d12f208e 100644 --- a/Sources/Sentry/include/SentryProfilerSDKAdapter.h +++ b/Sources/Sentry/include/SentryProfilerSDKAdapter.h @@ -4,6 +4,7 @@ #import "SentryProfilingConfiguration.h" #import "SentryProfilingFrameMeasurements.h" #import "SentryProfilingId.h" +#import "SentryProfilingMemory.h" #import "SentryProfilingSystemMetrics.h" #import "SentryProfilingTimestamp.h" #import @@ -34,6 +35,8 @@ typedef NSDictionary *_Nonnull (^SentryProfilingMetricSerializat // Bind to the current provider without reading or serializing images until invoked. - (SentryProfilingDebugImageSerialization _Nullable)debugImageSerialization; - (SentryProfilingSystemMetrics *_Nullable)systemMetrics; +/** Snapshotted before sampling starts, never messaged while threads are suspended. */ +- (SentryProfilingMemoryReader _Nullable)memoryReader; - (id _Nullable) scheduleMetricSamplingWithInterval:(uint64_t)intervalNanoseconds leeway:(uint64_t)leewayNanoseconds diff --git a/Sources/Sentry/include/SentryProfilerTestHelpers.h b/Sources/Sentry/include/SentryProfilerTestHelpers.h index d56092f3751..caa892a86c7 100644 --- a/Sources/Sentry/include/SentryProfilerTestHelpers.h +++ b/Sources/Sentry/include/SentryProfilerTestHelpers.h @@ -7,16 +7,11 @@ #if SENTRY_TARGET_PROFILING_SUPPORTED # import "SentryDefines.h" +# import "SentryProfilingRuntime.h" # import NS_ASSUME_NONNULL_BEGIN -/** - * Disable profiling when running with TSAN because it produces a TSAN false positive, similar to - * the situation described here: https://github.com/envoyproxy/envoy/issues/2561 - */ -SENTRY_EXTERN BOOL sentry_threadSanitizerIsPresent(void); - # if defined(DEBUG) || defined(SENTRY_UI_TEST_SUPPORT) // Test support. /** diff --git a/Sources/Sentry/include/SentryProfilingMemory.h b/Sources/Sentry/include/SentryProfilingMemory.h new file mode 100644 index 00000000000..276537dce9f --- /dev/null +++ b/Sources/Sentry/include/SentryProfilingMemory.h @@ -0,0 +1,15 @@ +#ifndef SENTRY_PROFILING_MEMORY_H +#define SENTRY_PROFILING_MEMORY_H + +#include +#include + +/** + * Checks whether a complete memory range is readable before the profiler dereferences it. + * + * The function must remain valid for the sampling session's lifetime and be safe to call while + * other threads are suspended. It must not allocate, acquire locks, or call Objective-C or Swift. + */ +typedef bool (*SentryProfilingMemoryReader)(const void *memory, size_t byteCount); + +#endif diff --git a/Sources/Sentry/include/SentryProfilingRuntime.h b/Sources/Sentry/include/SentryProfilingRuntime.h new file mode 100644 index 00000000000..a977fd0c009 --- /dev/null +++ b/Sources/Sentry/include/SentryProfilingRuntime.h @@ -0,0 +1,17 @@ +#import "SentryDefines.h" +#import "SentryProfilingConditionals.h" +#import + +#if SENTRY_TARGET_PROFILING_SUPPORTED + +NS_ASSUME_NONNULL_BEGIN + +/** + * Disable profiling when running with TSAN because it produces a TSAN false positive, similar to + * the situation described here: https://github.com/envoyproxy/envoy/issues/2561 + */ +SENTRY_EXTERN BOOL sentry_threadSanitizerIsPresent(void); + +NS_ASSUME_NONNULL_END + +#endif // SENTRY_TARGET_PROFILING_SUPPORTED diff --git a/Sources/Sentry/include/SentrySamplingProfiler.hpp b/Sources/Sentry/include/SentrySamplingProfiler.hpp index 62a1f2f2509..4c019b09daf 100644 --- a/Sources/Sentry/include/SentrySamplingProfiler.hpp +++ b/Sources/Sentry/include/SentrySamplingProfiler.hpp @@ -4,6 +4,8 @@ #if SENTRY_TARGET_PROFILING_SUPPORTED +# include "SentryProfilingMemory.h" + # include # include # include @@ -29,9 +31,10 @@ namespace profiling { * backtrace for a particular thread. The timestamp of the entry will be set to the * timestamp that the sample was collected at. * @param samplingRateHz The sampling rate, in Hz, to sample at. + * @param memoryReader The async-safe C function used to validate sampled memory. */ - SamplingProfiler( - std::function callback, std::uint32_t samplingRateHz); + SamplingProfiler(std::function callback, + std::uint32_t samplingRateHz, SentryProfilingMemoryReader memoryReader); ~SamplingProfiler(); @@ -56,6 +59,7 @@ namespace profiling { private: mach_timespec_t delaySpec_; std::function callback_; + const SentryProfilingMemoryReader memoryReader_; std::unique_ptr cache_; bool isInitialized_; std::mutex isSamplingLock_; diff --git a/Sources/Sentry/include/SentryThreadSnapshot.h b/Sources/Sentry/include/SentryThreadSnapshot.h index da43f0dda2a..fd11ecb998e 100644 --- a/Sources/Sentry/include/SentryThreadSnapshot.h +++ b/Sources/Sentry/include/SentryThreadSnapshot.h @@ -1,6 +1,8 @@ #ifndef SENTRY_THREAD_SNAPSHOT_H #define SENTRY_THREAD_SNAPSHOT_H +#include "SentryThreadSuspension.h" + #include #include #include @@ -72,17 +74,6 @@ typedef struct { SentryThreadSnapshot *threads; } SentryThreadSnapshotBuffer; -/** - * Process-wide, nonblocking admission shared by nonfatal bulk inspection and profiling. - * - * Acquisition returns immediately. Remote-suspension paths acquire before their first suspension. - * A caller that acquires admission must release it exactly once, after its final balancing resume - * attempt. A contender must skip remote suspension rather than wait because the current owner may - * already have suspended the contending thread. - */ -bool sentryThreadSuspensionTryAcquire(void); -void sentryThreadSuspensionRelease(void); - /** * C-only backend interface; no backend-specific type crosses the snapshot boundary. * diff --git a/Sources/Sentry/SentryAsyncSafeLog.c b/Sources/SentryNativeUtils/SentryAsyncSafeLog.c similarity index 100% rename from Sources/Sentry/SentryAsyncSafeLog.c rename to Sources/SentryNativeUtils/SentryAsyncSafeLog.c diff --git a/Sources/Sentry/SentryMachLogging.cpp b/Sources/SentryNativeUtils/SentryMachLogging.cpp similarity index 100% rename from Sources/Sentry/SentryMachLogging.cpp rename to Sources/SentryNativeUtils/SentryMachLogging.cpp diff --git a/Sources/SentryNativeUtils/SentryThreadSuspension.c b/Sources/SentryNativeUtils/SentryThreadSuspension.c new file mode 100644 index 00000000000..b633d10be69 --- /dev/null +++ b/Sources/SentryNativeUtils/SentryThreadSuspension.c @@ -0,0 +1,19 @@ +#include "SentryThreadSuspension.h" +#include + +// Coordinates the SDK's nonfatal bulk-inspection and profiler suspension paths. atomic_flag is +// guaranteed lock-free, so a contender can yield without waiting on an owner that may have +// suspended its thread. +static atomic_flag g_threadSuspensionAdmission = ATOMIC_FLAG_INIT; + +bool +sentryThreadSuspensionTryAcquire(void) +{ + return !atomic_flag_test_and_set_explicit(&g_threadSuspensionAdmission, memory_order_acquire); +} + +void +sentryThreadSuspensionRelease(void) +{ + atomic_flag_clear_explicit(&g_threadSuspensionAdmission, memory_order_release); +} diff --git a/Sources/Sentry/SentryAsyncSafeLog.h b/Sources/SentryNativeUtils/include/SentryAsyncSafeLog.h similarity index 100% rename from Sources/Sentry/SentryAsyncSafeLog.h rename to Sources/SentryNativeUtils/include/SentryAsyncSafeLog.h diff --git a/Sources/Sentry/include/SentryCompiler.h b/Sources/SentryNativeUtils/include/SentryCompiler.h similarity index 100% rename from Sources/Sentry/include/SentryCompiler.h rename to Sources/SentryNativeUtils/include/SentryCompiler.h diff --git a/Sources/Sentry/include/SentryInternalCDefines.h b/Sources/SentryNativeUtils/include/SentryInternalCDefines.h similarity index 100% rename from Sources/Sentry/include/SentryInternalCDefines.h rename to Sources/SentryNativeUtils/include/SentryInternalCDefines.h diff --git a/Sources/Sentry/include/SentryMachLogging.hpp b/Sources/SentryNativeUtils/include/SentryMachLogging.hpp similarity index 100% rename from Sources/Sentry/include/SentryMachLogging.hpp rename to Sources/SentryNativeUtils/include/SentryMachLogging.hpp diff --git a/Sources/Sentry/Public/SentryProfilingConditionals.h b/Sources/SentryNativeUtils/include/SentryProfilingConditionals.h similarity index 100% rename from Sources/Sentry/Public/SentryProfilingConditionals.h rename to Sources/SentryNativeUtils/include/SentryProfilingConditionals.h diff --git a/Sources/SentryNativeUtils/include/SentryThreadSuspension.h b/Sources/SentryNativeUtils/include/SentryThreadSuspension.h new file mode 100644 index 00000000000..6ef08df1c71 --- /dev/null +++ b/Sources/SentryNativeUtils/include/SentryThreadSuspension.h @@ -0,0 +1,25 @@ +#ifndef SENTRY_THREAD_SUSPENSION_H +#define SENTRY_THREAD_SUSPENSION_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Process-wide, nonblocking admission shared by nonfatal bulk inspection and profiling. + * + * Acquisition returns immediately. Remote-suspension paths acquire before their first suspension. + * A caller that acquires admission must release it exactly once, after its final balancing resume + * attempt. A contender must skip remote suspension rather than wait because the current owner may + * already have suspended the contending thread. + */ +bool sentryThreadSuspensionTryAcquire(void); +void sentryThreadSuspensionRelease(void); + +#ifdef __cplusplus +} +#endif + +#endif // SENTRY_THREAD_SUSPENSION_H diff --git a/Sources/SentryNativeUtils/include/module.modulemap b/Sources/SentryNativeUtils/include/module.modulemap index be6b9280619..3dede766c3a 100644 --- a/Sources/SentryNativeUtils/include/module.modulemap +++ b/Sources/SentryNativeUtils/include/module.modulemap @@ -1,5 +1,17 @@ module SentryNativeUtils { textual header "SentryNativeUtilsDefines.h" - umbrella "." + header "SentryNativeUtils.h" + header "SentryAsyncSafeLog.h" + header "SentryCompiler.h" + header "SentryInternalCDefines.h" + header "SentryProfilingConditionals.h" + header "SentryThreadSuspension.h" + header "SentryTime.h" export * + + module Cxx { + requires cplusplus + header "SentryMachLogging.hpp" + export * + } } diff --git a/Sources/SentryTestUtilsObjC/SentryAsyncSafeLog.h b/Sources/SentryTestUtilsObjC/SentryAsyncSafeLog.h index 83c4fb830bc..2e5d9f09aeb 100644 --- a/Sources/SentryTestUtilsObjC/SentryAsyncSafeLog.h +++ b/Sources/SentryTestUtilsObjC/SentryAsyncSafeLog.h @@ -1,2 +1,2 @@ // Forward this internal dependency without adding its source directory to header search paths. -#import "../Sentry/SentryAsyncSafeLog.h" +#import "../SentryNativeUtils/include/SentryAsyncSafeLog.h" diff --git a/Sources/SentryTestUtilsObjC/include/SentryTestUtilsObjC-SDKHeaders.h b/Sources/SentryTestUtilsObjC/include/SentryTestUtilsObjC-SDKHeaders.h index e6d5c03304d..a6a758ec863 100644 --- a/Sources/SentryTestUtilsObjC/include/SentryTestUtilsObjC-SDKHeaders.h +++ b/Sources/SentryTestUtilsObjC/include/SentryTestUtilsObjC-SDKHeaders.h @@ -4,7 +4,6 @@ // Load the owning module before categories so Swift retains the base types' module identity. @import _SentryPrivate; -#import "../../Sentry/SentryAsyncSafeLog.h" #import "../../Sentry/SentryFileIO.h" #import "../../Sentry/SentryJSONStreamWriter.h" #import "../../Sentry/include/NSMutableDictionary+Sentry.h" @@ -16,5 +15,6 @@ #import "../../Sentry/include/SentryTracer+Private.h" #import "../../Sentry/include/SentryTransportFactory.h" #import "../../Sentry/include/SentryWeakMap.h" +#import "../../SentryNativeUtils/include/SentryAsyncSafeLog.h" // File-manager tests also need these declarations without the V9 profiler wrapper's guards. #import "SentryFileManager+Test.h" diff --git a/Tests/SentryProfilerTests/ObjC/SentryBacktraceTests.mm b/Tests/SentryProfilerTests/ObjC/SentryBacktraceTests.mm index 5be292a5129..45f0c2d30cb 100644 --- a/Tests/SentryProfilerTests/ObjC/SentryBacktraceTests.mm +++ b/Tests/SentryProfilerTests/ObjC/SentryBacktraceTests.mm @@ -5,8 +5,10 @@ # import # import "SentryBacktrace.hpp" +# import "SentryMemory.h" +# import "SentryStackFrame.hpp" # import "SentryThreadHandle.hpp" -# import "SentryThreadSnapshot.h" +# import "SentryThreadSuspension.h" # import # import @@ -25,7 +27,7 @@ { auto current = ThreadHandle::current(); return backtrace(*current, *current, addresses, current->stackBounds(), reachedEndOfStackPtr, - maxDepth, skip); + maxDepth, sentryMemoryIsReadable, skip); } NOT_TAIL_CALLED NEVER_INLINE std::size_t @@ -80,6 +82,17 @@ } namespace { +std::atomic_size_t readabilityChecks { 0 }; +std::atomic_size_t readabilityByteCount { 0 }; + +bool +rejectMemoryRead(const void *, std::size_t byteCount) +{ + readabilityChecks.fetch_add(1, std::memory_order_relaxed); + readabilityByteCount.store(byteCount, std::memory_order_relaxed); + return false; +} + std::string symbolicate(std::uintptr_t address) noexcept { @@ -165,6 +178,25 @@ @interface SentryBacktraceTests : XCTestCase @implementation SentryBacktraceTests +- (void)testBacktrace_whenMemoryReaderRejectsFrame_shouldStopBeforeReadingIt +{ + // -- Arrange -- + auto current = ThreadHandle::current(); + std::uintptr_t addresses[16] = { }; + bool reachedEndOfStack = false; + readabilityChecks.store(0, std::memory_order_relaxed); + readabilityByteCount.store(0, std::memory_order_relaxed); + + // -- Act -- + backtrace(*current, *current, addresses, current->stackBounds(), &reachedEndOfStack, + countof(addresses), rejectMemoryRead); + + // -- Assert -- + XCTAssertEqual(readabilityChecks.load(std::memory_order_relaxed), 1U); + XCTAssertEqual(readabilityByteCount.load(std::memory_order_relaxed), sizeof(StackFrame)); + XCTAssertFalse(reachedEndOfStack); +} + - (void)testBacktrace { std::uintptr_t addresses[128]; @@ -244,7 +276,7 @@ - (void)testEnumerateBacktraces_whenSuspensionAdmissionBusy_shouldSkipRemoteCapt [&](auto &backtrace) { foundThread |= backtrace.threadMetadata.threadID == pthread_mach_thread_np(thread); }, - &cache); + &cache, sentryMemoryIsReadable); } sentryThreadSuspensionRelease(); XCTAssertEqual(pthread_cancel(thread), 0); @@ -293,7 +325,7 @@ - (void)testCollectsMultiThreadBacktrace } } }, - &cache); + &cache, sentryMemoryIsReadable); if (foundThread1 && foundThread2) { break; } diff --git a/Tests/SentryProfilerTests/ObjC/SentryProfilerSDKAdapterTests.m b/Tests/SentryProfilerTests/ObjC/SentryProfilerSDKAdapterTests.m index 2cd94e02774..0d06514e2b5 100644 --- a/Tests/SentryProfilerTests/ObjC/SentryProfilerSDKAdapterTests.m +++ b/Tests/SentryProfilerTests/ObjC/SentryProfilerSDKAdapterTests.m @@ -1,6 +1,7 @@ @import XCTest; #import "SentryDefaultProfilerSDKBackend.h" #import "SentryFileManagerHelper.h" +#import "SentryMemory.h" #import "SentryProfiler+Private.h" #import "SentryProfilerDeliveryBridge.h" #import "SentryProfilerSDKAdapter.h" @@ -271,6 +272,31 @@ - (void)testContinuousProfileEnvelope_whenEnrichmentUnavailable_shouldKeepProfil XCTAssertEqualObjects(payload[@"measurements"], metrics); } +- (void)testProfilingAdapter_whenBackendReleased_shouldKeepSnapshottedMemoryReaderCallable +{ + // -- Arrange -- + SentryProfilerSDKAdapter *adapter = [[SentryProfilerSDKAdapter alloc] init]; + SentryProfilingMemoryReader reader = NULL; + __weak SentryDefaultProfilerSDKBackend *weakBackend; + @autoreleasepool { + SentryDefaultProfilerSDKBackend *backend = [[SentryDefaultProfilerSDKBackend alloc] init]; + weakBackend = backend; + [adapter setTarget:backend]; + reader = [adapter memoryReader]; + } + int value = 123; + + // -- Act -- + BOOL readable = reader != NULL && reader(&value, sizeof(value)); + + // -- Assert -- + XCTAssertNil(weakBackend); + XCTAssertNil(adapter.target); + XCTAssertTrue(reader == sentryMemoryIsReadable); + XCTAssertTrue(readable); + XCTAssertTrue([adapter memoryReader] == NULL); +} + - (void)testProfilingAdapter_whenNoTarget_shouldNotScheduleWork { // -- Arrange -- @@ -289,6 +315,7 @@ - (void)testProfilingAdapter_whenNoTarget_shouldNotScheduleWork XCTAssertNil(timer); XCTAssertNil([adapter date]); XCTAssertEqual([adapter systemTime], 0ULL); + XCTAssertTrue([adapter memoryReader] == NULL); } - (void)testProfilingAdapter_whenTargetReplaced_shouldIgnoreOldTargetRemoval diff --git a/Tests/SentryProfilerTests/ObjC/SentryProfilerTests.mm b/Tests/SentryProfilerTests/ObjC/SentryProfilerTests.mm index 28cf6ae63ff..576230d1738 100644 --- a/Tests/SentryProfilerTests/ObjC/SentryProfilerTests.mm +++ b/Tests/SentryProfilerTests/ObjC/SentryProfilerTests.mm @@ -2,6 +2,7 @@ #if SENTRY_PROFILER_TESTS_SUPPORTED +# import "SentryCompiler.h" # import "SentryContinuousProfiler+Test.h" # import "SentryEvent+Private.h" # import "SentryHub.h" @@ -12,6 +13,7 @@ # import "SentryProfilerSampleCreation.h" # import "SentryProfilerSerialization+Private.h" # import "SentryProfilerState+ObjCpp.h" +# import "SentryProfilingRuntime.h" # import "SentryProfilingSample.h" # import "SentrySwift.h" # import "SentryThread.h" @@ -27,6 +29,19 @@ @interface SentryProfilerTests : XCTestCase @implementation SentryProfilerTests +- (void)testRuntime_whenCheckingThreadSanitizer_shouldMatchCompilerInstrumentation +{ + // -- Act -- + BOOL sanitizerPresent = sentry_threadSanitizerIsPresent(); + + // -- Assert -- +# if COMPILER_HAS_CLANG_FEATURE(thread_sanitizer) + XCTAssertTrue(sanitizerPresent); +# else + XCTAssertFalse(sanitizerPresent); +# endif +} + - (void)testProfilingSample_whenInitialized_shouldPreserveSampleDefaults { // -- Act -- diff --git a/Tests/SentryProfilerTests/ObjC/SentrySamplingProfilerTests.mm b/Tests/SentryProfilerTests/ObjC/SentrySamplingProfilerTests.mm index e21a4bbbf3b..4e12d5f72cf 100644 --- a/Tests/SentryProfilerTests/ObjC/SentrySamplingProfilerTests.mm +++ b/Tests/SentryProfilerTests/ObjC/SentrySamplingProfilerTests.mm @@ -5,6 +5,7 @@ # import # import "SentryBacktrace.hpp" +# import "SentryMemory.h" # import "SentrySamplingProfiler.hpp" # import "SentryThreadMetadataCache.hpp" # import "SentryTime.h" @@ -38,7 +39,7 @@ - (void)testProfiling numIdleSamples++; } }, - samplingRateHz); + samplingRateHz, sentryMemoryIsReadable); XCTAssertFalse(profiler->isSampling()); std::uint64_t start = 0; diff --git a/Tests/SentryTests/SentryCrash/SentryDefaultThreadInspectorTests.swift b/Tests/SentryTests/SentryCrash/SentryDefaultThreadInspectorTests.swift index 02ac03e6ce6..c5067688f42 100644 --- a/Tests/SentryTests/SentryCrash/SentryDefaultThreadInspectorTests.swift +++ b/Tests/SentryTests/SentryCrash/SentryDefaultThreadInspectorTests.swift @@ -4,6 +4,7 @@ @_spi(Private) @testable import Sentry #endif import _SentryPrivate +import SentryNativeUtils import SentryTestUtils import XCTest diff --git a/Tests/ThreadInspectionHarness/README.md b/Tests/ThreadInspectionHarness/README.md index f286d5d1e24..44de2d2a27f 100644 --- a/Tests/ThreadInspectionHarness/README.md +++ b/Tests/ThreadInspectionHarness/README.md @@ -1,6 +1,6 @@ # Thread Inspection Harness -This macOS Swift package tests the C capture path used by the V10 `SentryDefaultThreadInspector`. It compiles the production sources through relative symlinks into `Sources/Sentry`; it does not contain a copy of the implementation and is not part of an SDK release target. +This macOS Swift package tests the C capture path used by the V10 `SentryDefaultThreadInspector`. It compiles the production sources through relative symlinks into `Sources/Sentry` and `Sources/SentryNativeUtils`; it does not contain a copy of the implementation and is not part of an SDK release target. This is a nonfatal inspection path. Fatal reports use KSCrash's own thread capture pipeline. @@ -89,10 +89,11 @@ Depth 512 reaches the 512-frame capacity and reports truncation. Serial unwindin ## Capture Contract -The implementation is split into four layers: +The implementation is split into five layers: - `SentryThreadSnapshot.h` defines backend-neutral records and callback contracts. -- `SentryThreadSnapshot.c` owns allocation, process-wide admission, and capture phases. +- `SentryThreadSuspension.c` owns the process-wide admission flag shared with profiling. +- `SentryThreadSnapshot.c` owns allocation and capture phases and acquires shared admission. - `SentryThreadSnapshotKSCrash.c` provides task enumeration, Mach suspension, KSCrash unwind, names, and right cleanup. - `SentryDefaultThreadInspector+V10.swift` maps completed records to event models after all resume attempts. diff --git a/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/SentryThreadSuspension.c b/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/SentryThreadSuspension.c new file mode 120000 index 00000000000..ca9192e5b1d --- /dev/null +++ b/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/SentryThreadSuspension.c @@ -0,0 +1 @@ +../../../../Sources/SentryNativeUtils/SentryThreadSuspension.c \ No newline at end of file diff --git a/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/include/SentryThreadSuspension.h b/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/include/SentryThreadSuspension.h new file mode 120000 index 00000000000..2d1f3f31e5b --- /dev/null +++ b/Tests/ThreadInspectionHarness/Sources/ThreadSnapshot/include/SentryThreadSuspension.h @@ -0,0 +1 @@ +../../../../../Sources/SentryNativeUtils/include/SentryThreadSuspension.h \ No newline at end of file diff --git a/develop-docs/ARCHITECTURE.md b/develop-docs/ARCHITECTURE.md index d8e5d32f02e..35c78fc6df9 100644 --- a/develop-docs/ARCHITECTURE.md +++ b/develop-docs/ARCHITECTURE.md @@ -40,6 +40,10 @@ flowchart TD HTTP --> Files["SentryFileManager: on-disk envelope storage"] ``` +### Shared Native Utilities + +The SDK and profiling code consume [`SentryNativeUtils`](../Sources/SentryNativeUtils) for shared native utilities. This library owns process-wide suspension admission and async-safe logging state so consumers coordinate through one implementation. It has no dependency on SDK implementation, `_SentryPrivate`, or Swift. See [native utility builds](BUILD.md#native-utility-library) and [decision 42](DECISIONS.md#42-share-native-utilities-below-sdk-features). + ## Feature Areas - **Performance Tracking**: `SentryTracer`, `SentrySpan`, `SentryPerformanceTracker` for transaction monitoring diff --git a/develop-docs/BUILD.md b/develop-docs/BUILD.md index 9a11e4254ed..07df6f52bc9 100644 --- a/develop-docs/BUILD.md +++ b/develop-docs/BUILD.md @@ -8,7 +8,7 @@ This document covers the build system and configuration details for the Sentry C ## Native Library Targets -`SentryNativeUtils` contains Objective-C++ utilities only, with no Swift sources or Swift target dependencies. Its C, Objective-C, and C++ headers allow native and Swift consumers to call the utilities without requiring Swift in the library itself. +`SentryNativeUtils` contains shared C, C++, and Objective-C++ utilities, with no Swift sources or Swift target dependencies. Its C, Objective-C, and C++ headers allow native and Swift consumers to call the utilities without requiring Swift in the library itself. `SentryNativeUtils` and `SentryProfiling` have shared Xcode build schemes and target settings in `Sources/Configuration/`. SDK builds and package tests build both libraries as dependencies. For standalone builds, use the `--scheme` option of `scripts/sentry-xcodebuild.sh` to select either library. diff --git a/develop-docs/DECISIONS.md b/develop-docs/DECISIONS.md index c894943edbf..6d89ea0033f 100644 --- a/develop-docs/DECISIONS.md +++ b/develop-docs/DECISIONS.md @@ -46,6 +46,8 @@ - [42. Crash handler and MetricKit as parallel event sources](#42-crash-handler-and-metrickit-as-parallel-event-sources) - [43. Compile SDK test support under `DEBUG`](#43-compile-sdk-test-support-under-debug) +- [44. Share native utilities below SDK features](#44-share-native-utilities-below-sdk-features) + --- ## 1. Not capturing screenshots for crashes @@ -1090,3 +1092,17 @@ Related links: - [Support package tests in Debug (#9627)](https://github.com/getsentry/sentry-cocoa/pull/9627) - [Remove SDK project test targets (#9613)](https://github.com/getsentry/sentry-cocoa/pull/9613) - [Use Debug for SDK test support (#9624)](https://github.com/getsentry/sentry-cocoa/pull/9624) + +## 44. Share native utilities below SDK features + +Date: November 13, 2026 +Contributors: @philprime + +Use `SentryNativeUtils` for native utilities shared by the SDK and profiling. The SDK and the extracted profiling target depend on this library, which must not depend on SDK implementation, `_SentryPrivate`, or Swift. Shared mutable state, including suspension admission and async-safe logging output, has one implementation rather than a copy in each consumer. + +The library supports C, C++, and Objective-C. A future shared Swift utility layer can be named `SentryUtils` and depend on `SentryNativeUtils`, never the reverse. Add that layer only when a concrete shared Swift consumer requires it. + +Related links: + +- [Native boundary extraction](https://github.com/getsentry/sentry-cocoa/pull/9608) +- [Native utility builds](BUILD.md#native-utility-library) diff --git a/develop-docs/SWIFT.md b/develop-docs/SWIFT.md index 63eff8f1348..1980649daa0 100644 --- a/develop-docs/SWIFT.md +++ b/develop-docs/SWIFT.md @@ -6,7 +6,8 @@ Rules for the mixed-language SDK, and for converting a public type in `Sources/S - **`Sentry`** — The framework users import. The Xcode target `Sentry` in `Sentry.xcodeproj` builds `Sentry.framework` from source. - **`SentrySwift`** — SwiftPM target for `Sources/Swift`. Compiled into `Sentry`. -- **`SentryObjCInternal`** — SwiftPM target for the Objective-C and C implementation under `Sources/Sentry`. +- **`SentryObjCInternal`** — SwiftPM target for the Objective-C and C SDK implementation under `Sources/Sentry`. +- **`SentryNativeUtils`** — Shared C, C++, and Objective-C utilities under `Sources/SentryNativeUtils`. Both SDK generations consume it. It has no SDK or `_SentryPrivate` dependency. - **`SentryHeaders`** — Public Objective-C headers in `Sources/Sentry/Public`. Umbrella: `Sentry.h` in `Sources/Resources/Sentry.modulemap`. - **`_SentryPrivate`** — Private Objective-C headers for SDK Swift. List: `Sources/Sentry/include/SentryPrivate.h`. Users do not import this module. - **`SentrySwift.h`** — Shim at `Sources/Sentry/include/SentrySwift.h`. Objective-C imports this to call Swift. diff --git a/scripts/prepare-sentry-framework-inputs.sh b/scripts/prepare-sentry-framework-inputs.sh index cab824e51fe..3c3b76d1046 100755 --- a/scripts/prepare-sentry-framework-inputs.sh +++ b/scripts/prepare-sentry-framework-inputs.sh @@ -61,6 +61,7 @@ prepare_headers() { local header # Reuse Xcode's umbrella so Foundation and the public submodules have identical exports. cp "$SOURCE_HEADERS_DIR"/*.h "$headers_dir/" + cp "$SCRIPT_DIR/../Sources/SentryNativeUtils/include/SentryProfilingConditionals.h" "$headers_dir/" if [ "${SDK_V10:-0}" = 1 ]; then # Match SentryV10.xcconfig's COPY_HEADERS_RUN_UNIFDEF. Consumers must not need build # flags, and defining SDK_V10 in the umbrella would export it as a Swift constant.