From 7e55575fbf6323f4c608ba7f575483098c1a0cbb Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Thu, 17 Jan 2019 11:10:07 +0300 Subject: [PATCH] Use dlopen/dlsym for CoreSymbolication (#2542) Linking it directly seems to be inconvenient when producing static lib --- .../kotlin/backend/konan/KonanConfig.kt | 10 +---- .../targets/ios_x64/tbd/CoreSymbolication.tbd | 16 ------- .../macos_x64/tbd/CoreSymbolication.tbd | 16 ------- runtime/src/debug/cpp/SourceInfo.cpp | 45 ++++++++++++++----- .../kotlin/konan/target/Distribution.kt | 1 - 5 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 konan/targets/ios_x64/tbd/CoreSymbolication.tbd delete mode 100644 konan/targets/macos_x64/tbd/CoreSymbolication.tbd diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index 94b1161b5cc..69b93b83669 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -123,15 +123,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration internal val includeBinaries: List = configuration.getList(KonanConfigKeys.INCLUDED_BINARY_FILES) - internal val defaultSystemLibraries: List = when (target) { - KonanTarget.MACOS_X64, KonanTarget.IOS_X64 -> if (debug) { - listOf(File(distribution.tbdDirectory(target)).child("CoreSymbolication.tbd").absolutePath) - } else { - emptyList() - } - - else -> emptyList() - } + internal val defaultSystemLibraries: List = emptyList() internal val languageVersionSettings = configuration.get(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS)!! diff --git a/konan/targets/ios_x64/tbd/CoreSymbolication.tbd b/konan/targets/ios_x64/tbd/CoreSymbolication.tbd deleted file mode 100644 index 3bfe53b40a7..00000000000 --- a/konan/targets/ios_x64/tbd/CoreSymbolication.tbd +++ /dev/null @@ -1,16 +0,0 @@ ---- !tapi-tbd-v3 -archs: [ x86_64 ] -uuids: [ 'x86_64: BA456C79-974B-499E-9013-BEDB414FE7B9' ] -platform: ios -install-name: /System/Library/PrivateFrameworks/CoreSymbolication.framework/CoreSymbolication -exports: - - archs: [ x86_64 ] - symbols: [ _CSIsNull, - _CSSourceInfoGetFilename, - _CSSourceInfoGetPath, - _CSSourceInfoGetLineNumber, - _CSSourceInfoGetColumn, - _CSSymbolOwnerGetSourceInfoWithAddress, - _CSSymbolicatorCreateWithPid, - _CSSymbolicatorGetSymbolOwnerWithAddressAtTime ] -... diff --git a/konan/targets/macos_x64/tbd/CoreSymbolication.tbd b/konan/targets/macos_x64/tbd/CoreSymbolication.tbd deleted file mode 100644 index ba1c6c9820d..00000000000 --- a/konan/targets/macos_x64/tbd/CoreSymbolication.tbd +++ /dev/null @@ -1,16 +0,0 @@ ---- !tapi-tbd-v3 -archs: [ x86_64 ] -uuids: [ 'x86_64: E996B8F6-929B-4111-A859-EB054C062FE8' ] -platform: macosx -install-name: /System/Library/PrivateFrameworks/CoreSymbolication.framework/Versions/A/CoreSymbolication -exports: - - archs: [ x86_64 ] - symbols: [ _CSIsNull, - _CSSourceInfoGetFilename, - _CSSourceInfoGetPath, - _CSSourceInfoGetLineNumber, - _CSSourceInfoGetColumn, - _CSSymbolOwnerGetSourceInfoWithAddress, - _CSSymbolicatorCreateWithPid, - _CSSymbolicatorGetSymbolOwnerWithAddressAtTime ] -... diff --git a/runtime/src/debug/cpp/SourceInfo.cpp b/runtime/src/debug/cpp/SourceInfo.cpp index fafa8c4d3d4..dc7fa0f6d92 100644 --- a/runtime/src/debug/cpp/SourceInfo.cpp +++ b/runtime/src/debug/cpp/SourceInfo.cpp @@ -18,6 +18,7 @@ #ifdef KONAN_CORE_SYMBOLICATION +#include #include #include #include @@ -41,39 +42,61 @@ typedef unsigned long long CSArchitecture; #define kCSNow LONG_MAX -extern "C" { +namespace { -CSSymbolicatorRef CSSymbolicatorCreateWithPid(pid_t pid); +CSSymbolicatorRef (*CSSymbolicatorCreateWithPid)(pid_t pid); -CSSymbolOwnerRef CSSymbolicatorGetSymbolOwnerWithAddressAtTime( +CSSymbolOwnerRef (*CSSymbolicatorGetSymbolOwnerWithAddressAtTime)( CSSymbolicatorRef symbolicator, unsigned long long address, long time ); -CSSourceInfoRef CSSymbolOwnerGetSourceInfoWithAddress( +CSSourceInfoRef (*CSSymbolOwnerGetSourceInfoWithAddress)( CSSymbolOwnerRef owner, unsigned long long address ); -const char* CSSourceInfoGetPath(CSSourceInfoRef info); +const char* (*CSSourceInfoGetPath)(CSSourceInfoRef info); -uint32_t CSSourceInfoGetLineNumber(CSSourceInfoRef info); +uint32_t (*CSSourceInfoGetLineNumber)(CSSourceInfoRef info); -uint32_t CSSourceInfoGetColumn(CSSourceInfoRef info); +uint32_t (*CSSourceInfoGetColumn)(CSSourceInfoRef info); -bool CSIsNull(CSTypeRef); +bool (*CSIsNull)(CSTypeRef); -} // extern "C" +CSSymbolicatorRef symbolicator; + +bool TryInitializeCoreSymbolication() { + void* cs = dlopen("/System/Library/PrivateFrameworks/CoreSymbolication.framework/CoreSymbolication", RTLD_LAZY); + if (!cs) return false; + +#define KONAN_CS_LOOKUP(name) name = (decltype(name)) dlsym(cs, #name); if (!name) return false; + + KONAN_CS_LOOKUP(CSSymbolicatorCreateWithPid) + KONAN_CS_LOOKUP(CSSymbolicatorGetSymbolOwnerWithAddressAtTime) + KONAN_CS_LOOKUP(CSSymbolOwnerGetSourceInfoWithAddress) + KONAN_CS_LOOKUP(CSSourceInfoGetPath) + KONAN_CS_LOOKUP(CSSourceInfoGetLineNumber) + KONAN_CS_LOOKUP(CSSourceInfoGetColumn) + KONAN_CS_LOOKUP(CSIsNull) + +#undef KONAN_CS_LOOKUP + + symbolicator = CSSymbolicatorCreateWithPid(getpid()); + return !CSIsNull(symbolicator); +} + +} // namespace extern "C" struct SourceInfo Kotlin_getSourceInfo(void* addr) { SourceInfo result = { .fileName = nullptr, .lineNumber = -1, .column = -1 }; - static CSSymbolicatorRef symbolicator = CSSymbolicatorCreateWithPid(getpid()); + static bool csIsAvailable = TryInitializeCoreSymbolication(); - if (!CSIsNull(symbolicator)) { + if (csIsAvailable) { unsigned long long address = static_cast((uintptr_t)addr); CSSymbolOwnerRef symbolOwner = CSSymbolicatorGetSymbolOwnerWithAddressAtTime(symbolicator, address, kCSNow); diff --git a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Distribution.kt b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Distribution.kt index 82dabc936ea..ac6ab7a651b 100644 --- a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Distribution.kt +++ b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Distribution.kt @@ -75,7 +75,6 @@ class Distribution( val stdlib = "$klib/common/stdlib" fun defaultNatives(target: KonanTarget) = "$konanHome/konan/targets/${target.visibleName}/native" - fun tbdDirectory(target: KonanTarget) = "$konanHome/konan/targets/${target.visibleName}/tbd" fun runtime(target: KonanTarget) = runtimeFileOverride ?: "$stdlib/targets/${target.visibleName}/native/runtime.bc"