Skip to content

Commit 1258d45

Browse files
committed
[Backtracing] Build work.
Additional shimming required for some builds, as well as a few other build related tweaks. rdar://106234311
1 parent eac93f9 commit 1258d45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+792
-236
lines changed

CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
11441144
message(STATUS "Concurrency Support: ${SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY}")
11451145
message(STATUS "Distributed Support: ${SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED}")
11461146
message(STATUS "String Processing Support: ${SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING}")
1147+
message(STATUS "Backtracing Support: ${SWIFT_ENABLE_BACKTRACING}")
11471148
message(STATUS "Unicode Support: ${SWIFT_STDLIB_ENABLE_UNICODE_DATA}")
11481149
message(STATUS "Reflection Support: ${SWIFT_ENABLE_EXPERIMENTAL_REFLECTION}")
11491150
message(STATUS "Observation Support: ${SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION}")
@@ -1153,6 +1154,16 @@ else()
11531154
message(STATUS "")
11541155
endif()
11551156

1157+
if(SWIFT_BUILD_LIBEXEC)
1158+
message(STATUS "Building Swift auxiliary executables for SDKs: ${SWIFT_SDKS}")
1159+
message(STATUS "")
1160+
endif()
1161+
1162+
if(SWIFT_BUILD_REMOTE_MIRROR)
1163+
message(STATUS "Building Swift Remote Mirror for SDKs: ${SWIFT_SDKS}")
1164+
message(STATUS "")
1165+
endif()
1166+
11561167
#
11571168
# Find required dependencies.
11581169
#
@@ -1238,6 +1249,19 @@ else()
12381249
if(SWIFT_INCLUDE_TOOLS)
12391250
add_subdirectory(stdlib/public/SwiftShims/swift/shims)
12401251
endif()
1252+
1253+
# We might want to build Remote Mirror separately
1254+
if(SWIFT_BUILD_REMOTE_MIRROR)
1255+
add_subdirectory(stdlib/public/LLVMSupport)
1256+
add_subdirectory(stdlib/public/Demangling)
1257+
add_subdirectory(stdlib/public/Reflection)
1258+
add_subdirectory(stdlib/public/SwiftRemoteMirror)
1259+
endif()
1260+
1261+
# We might also want to build the things in libexec seaprately
1262+
if(SWIFT_BUILD_LIBEXEC)
1263+
add_subdirectory(stdlib/public/libexec)
1264+
endif()
12411265
endif()
12421266

12431267
if(SWIFT_INCLUDE_APINOTES)

cmake/modules/SwiftComponents.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# * llvm-toolchain-dev-tools -- install LLVM development tools useful in a shared toolchain
7070
# * dev -- headers and libraries required to use Swift compiler as a library.
7171
set(_SWIFT_DEFINED_COMPONENTS
72-
"autolink-driver;back-deployment;compiler;clang-builtin-headers;clang-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;stdlib;stdlib-experimental;sdk-overlay;static-mirror-lib;editor-integration;tools;testsuite-tools;toolchain-tools;toolchain-dev-tools;llvm-toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers")
72+
"autolink-driver;back-deployment;compiler;clang-builtin-headers;clang-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;libexec;stdlib;stdlib-experimental;sdk-overlay;static-mirror-lib;editor-integration;tools;testsuite-tools;toolchain-tools;toolchain-dev-tools;llvm-toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers")
7373

7474
# The default install components include all of the defined components, except
7575
# for the following exceptions.

include/swift/Runtime/Backtrace.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "swift/Runtime/Config.h"
2121

2222
#include "swift/shims/Visibility.h"
23-
#include "swift/shims/_SwiftBacktracing.h"
23+
#include "swift/shims/CrashInfo.h"
2424

2525
#include <inttypes.h>
2626

stdlib/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ if(SWIFT_STDLIB_ENABLE_SIB_TARGETS)
224224
endif()
225225
swift_create_stdlib_targets("swift-test-stdlib" "" FALSE)
226226

227+
# Create the libexec targets
228+
swift_create_stdlib_targets("swift-libexec" "" TRUE)
229+
swift_create_stdlib_targets("swift-test-libexec" "" FALSE)
230+
231+
# Check whether the Swift compiler we're using supports
232+
# -disable-implicit-backtracing-module-import
233+
include(SwiftImplicitImport)
234+
235+
swift_supports_implicit_module("backtracing" SWIFT_COMPILER_SUPPORTS_BACKTRACING)
236+
227237
# FIXME: Include the toolchain directory before the public directory. Otherwise
228238
# the clang resource directory symlink stops installing correctly.
229239
add_subdirectory(toolchain)

stdlib/cmake/modules/AddSwiftStdlib.cmake

+31-11
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ function(add_swift_target_library name)
18251825
endif()
18261826

18271827
# Turn off implicit import of _Backtracing when building libraries
1828-
if(SWIFT_IMPLICIT_BACKTRACING_IMPORT)
1828+
if(SWIFT_COMPILER_SUPPORTS_BACKTRACING)
18291829
list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-Xfrontend;-disable-implicit-backtracing-module-import")
18301830
endif()
18311831

@@ -2658,7 +2658,8 @@ endfunction()
26582658
function(add_swift_target_executable name)
26592659
set(SWIFTEXE_options
26602660
EXCLUDE_FROM_ALL
2661-
BUILD_WITH_STDLIB)
2661+
BUILD_WITH_STDLIB
2662+
BUILD_WITH_LIBEXEC)
26622663
set(SWIFTEXE_single_parameter_options
26632664
INSTALL_IN_COMPONENT)
26642665
set(SWIFTEXE_multiple_parameter_options
@@ -2715,10 +2716,12 @@ function(add_swift_target_executable name)
27152716
list(APPEND SWIFTEXE_TARGET_COMPILE_FLAGS "-Xfrontend;-disable-implicit-backtracing-module-import")
27162717
endif()
27172718

2718-
# All Swift executables depend on the standard library.
2719-
list(APPEND SWIFTEXE_TARGET_SWIFT_MODULE_DEPENDS Core)
2720-
# All Swift executables depend on the swiftSwiftOnoneSupport library.
2721-
list(APPEND SWIFTEXE_TARGET_SWIFT_MODULE_DEPENDS SwiftOnoneSupport)
2719+
if(SWIFT_BUILD_STDLIB)
2720+
# All Swift executables depend on the standard library.
2721+
list(APPEND SWIFTEXE_TARGET_SWIFT_MODULE_DEPENDS Core)
2722+
# All Swift executables depend on the swiftSwiftOnoneSupport library.
2723+
list(APPEND SWIFTEXE_TARGET_SWIFT_MODULE_DEPENDS SwiftOnoneSupport)
2724+
endif()
27222725

27232726
# If target SDKs are not specified, build for all known SDKs.
27242727
if("${SWIFTEXE_TARGET_TARGET_SDKS}" STREQUAL "")
@@ -2897,15 +2900,23 @@ function(add_swift_target_executable name)
28972900
list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME})
28982901
endforeach()
28992902

2900-
set(library_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
2901-
if(maccatalyst_build_flavor STREQUAL "ios-like")
2902-
set(library_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}")
2903+
if(SWIFTEXE_TARGET_BUILD_WITH_LIBEXEC)
2904+
set(library_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
2905+
if(maccatalyst_build_flavor STREQUAL "ios-like")
2906+
set(library_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}")
2907+
endif()
2908+
2909+
set(lipo_target_dir "${SWIFTLIBEXEC_DIR}/${library_subdir}")
2910+
set(lipo_suffix "")
2911+
else()
2912+
set(lipo_target_dir "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
2913+
set(lipo_suffix "-${sdk}")
29032914
endif()
29042915

29052916
if("${sdk}" STREQUAL "WINDOWS")
2906-
set(UNIVERSAL_NAME "${SWIFTLIBEXEC_DIR}/${library_subdir}/${name}.exe")
2917+
set(UNIVERSAL_NAME "${lipo_target_dir}/${name}${lipo_suffix}.exe")
29072918
else()
2908-
set(UNIVERSAL_NAME "${SWIFTLIBEXEC_DIR}/${library_subdir}/${name}")
2919+
set(UNIVERSAL_NAME "${lipo_target_dir}/${name}${lipo_suffix}")
29092920
endif()
29102921

29112922
set(lipo_target "${name}-${sdk}")
@@ -2990,5 +3001,14 @@ function(add_swift_target_executable name)
29903001
endforeach()
29913002
endif()
29923003

3004+
if(SWIFTEXE_TARGET_BUILD_WITH_LIBEXEC)
3005+
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
3006+
set(variant "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
3007+
if(TARGET "swift-libexec${variant}")
3008+
add_dependencies("swift-libexec${variant}" ${lipo_target})
3009+
endif()
3010+
endforeach()
3011+
endif()
3012+
29933013
endforeach()
29943014
endfunction()

stdlib/public/Backtracing/Backtrace.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Swift
1818

1919
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
2020

21-
@_implementationOnly import Darwin.Mach
2221
@_implementationOnly import _SwiftBacktracingShims
2322

2423
#endif
@@ -327,20 +326,20 @@ public struct Backtrace: CustomStringConvertible, Sendable {
327326
/// @returns A list of `Image`s.
328327
public static func captureImages() -> [Image] {
329328
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
330-
return captureImages(for: mach_task_self_)
329+
return captureImages(for: _swift_backtrace_task_self())
331330
#else
332331
return []
333332
#endif
334333
}
335334

336335
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
337-
private static func withDyldProcessInfo<T>(for task: task_t,
336+
private static func withDyldProcessInfo<T>(for task: __swift_task_t,
338337
fn: (OpaquePointer?) throws -> T)
339338
rethrows -> T {
340-
var kret: kern_return_t = KERN_SUCCESS
339+
var kret = __swift_kern_return_t(_SWIFT_KERN_SUCCESS)
341340
let dyldInfo = _dyld_process_info_create(task, 0, &kret)
342341

343-
if kret != KERN_SUCCESS {
342+
if kret != _SWIFT_KERN_SUCCESS {
344343
fatalError("error: cannot create dyld process info")
345344
}
346345

@@ -357,7 +356,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
357356
var images: [Image] = []
358357

359358
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
360-
let task = process as! task_t
359+
let task = process as! __swift_task_t
361360

362361
withDyldProcessInfo(for: task) { dyldInfo in
363362
_dyld_process_info_for_each_image(dyldInfo) {
@@ -366,7 +365,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
366365
if let path = path, let uuid = uuid {
367366
let pathString = String(cString: path)
368367
let theUUID = Array(UnsafeBufferPointer(start: uuid,
369-
count: MemoryLayout<uuid_t>.size))
368+
count: MemoryLayout<__swift_uuid_t>.size))
370369
let name: String
371370
if let slashIndex = pathString.lastIndex(of: "/") {
372371
name = String(pathString.suffix(from:
@@ -404,7 +403,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
404403
/// @returns A `SharedCacheInfo`.
405404
public static func captureSharedCacheInfo() -> SharedCacheInfo? {
406405
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
407-
return captureSharedCacheInfo(for: mach_task_self_)
406+
return captureSharedCacheInfo(for: _swift_backtrace_task_self())
408407
#else
409408
return nil
410409
#endif
@@ -413,13 +412,13 @@ public struct Backtrace: CustomStringConvertible, Sendable {
413412
@_spi(Internal)
414413
public static func captureSharedCacheInfo(for t: Any) -> SharedCacheInfo? {
415414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
416-
let task = t as! task_t
415+
let task = t as! __swift_task_t
417416
return withDyldProcessInfo(for: task) { dyldInfo in
418417
var cacheInfo = dyld_process_cache_info()
419418
_dyld_process_info_get_cache(dyldInfo, &cacheInfo)
420419
let theUUID = withUnsafePointer(to: cacheInfo.cacheUUID) {
421420
Array(UnsafeRawBufferPointer(start: $0,
422-
count: MemoryLayout<uuid_t>.size))
421+
count: MemoryLayout<__swift_uuid_t>.size))
423422
}
424423
return SharedCacheInfo(uuid: theUUID,
425424
baseAddress: Address(cacheInfo.cacheBaseAddress),

stdlib/public/Backtracing/BacktraceFormatter.swift

+6-12
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717

1818
import Swift
1919

20-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
21-
@_implementationOnly import Darwin
22-
#elseif os(Windows)
23-
@_implementationOnly import CRT
24-
#elseif os(Linux)
25-
@_implementationOnly import Glibc
26-
#endif
27-
2820
@_implementationOnly import _SwiftBacktracingShims
2921

3022
/// A backtrace formatting theme.
@@ -621,9 +613,11 @@ public struct BacktraceFormatter {
621613
/// with the point at which the program crashed highlighted.
622614
private func formattedSourceLines(from sourceLocation: SymbolicatedBacktrace.SourceLocation,
623615
indent theIndent: Int = 2) -> String? {
624-
guard let fp = fopen(sourceLocation.path, "rt") else { return nil }
616+
guard let fp = _swift_backtrace_fopen(sourceLocation.path, "rt") else {
617+
return nil
618+
}
625619
defer {
626-
fclose(fp)
620+
_swift_backtrace_fclose(fp)
627621
}
628622

629623
let indent = String(repeating: " ", count: theIndent)
@@ -690,8 +684,8 @@ public struct BacktraceFormatter {
690684
}
691685
}
692686

693-
while feof(fp) == 0 && ferror(fp) == 0 {
694-
guard let result = fgets(buffer.baseAddress,
687+
while _swift_backtrace_feof(fp) == 0 && _swift_backtrace_ferror(fp) == 0 {
688+
guard let result = _swift_backtrace_fgets(buffer.baseAddress,
695689
CInt(buffer.count), fp) else {
696690
break
697691
}

stdlib/public/Backtracing/CMakeLists.txt

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212

1313
set(swift_backtracing_link_libraries
1414
swiftCore
15+
swift_Concurrency
1516
)
1617

18+
set(concurrency)
19+
if(SWIFT_BUILD_STDLIB AND SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY)
20+
set(concurrency _Concurrency)
21+
endif()
22+
1723
set(BACKTRACING_SOURCES
1824
Backtrace.swift
1925
BacktraceFormatter.swift
@@ -24,6 +30,7 @@ set(BACKTRACING_SOURCES
2430
Registers.swift
2531
SymbolicatedBacktrace.swift
2632
Utils.swift
33+
Shims.cpp
2734

2835
get-cpu-context.${SWIFT_ASM_EXT}
2936
)
@@ -36,13 +43,7 @@ set(LLVM_OPTIONAL_SOURCES
3643
add_swift_target_library(swift_Backtracing ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
3744
${BACKTRACING_SOURCES}
3845

39-
SWIFT_MODULE_DEPENDS_IOS Darwin _Concurrency
40-
SWIFT_MODULE_DEPENDS_OSX Darwin _Concurrency
41-
SWIFT_MODULE_DEPENDS_TVOS Darwin _Concurrency
42-
SWIFT_MODULE_DEPENDS_WATCHOS Darwin _Concurrency
43-
SWIFT_MODULE_DEPENDS_MACCATALYST Darwin _Concurrency
44-
SWIFT_MODULE_DEPENDS_LINUX Glibc _Concurrency
45-
SWIFT_MODULE_DEPENDS_WINDOWS CRT _Concurrency
46+
SWIFT_MODULE_DEPENDS ${concurrency}
4647

4748
LINK_LIBRARIES ${swift_backtracing_link_libraries}
4849

0 commit comments

Comments
 (0)