Files
swift-mirror/stdlib/public/RuntimeModule/ElfImageCache.swift
Carl Peto 42ea2a5f7f [Backtracing] Allow symbolication across platforms (#86750)
Instead of the symbolication method being decided by what platform the symbolication code is running on, use a parameter passed to the symbolication function, defaulted to the current platform for backward compatibility.

Useful for the upcoming offline symbolicator.

rdar://168769519

<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->

---------

Co-authored-by: Carl Peto <carlpeto@Carls-MacBook-Pro.local>
2026-02-13 13:38:50 +00:00

115 lines
3.3 KiB
Swift

//===--- ElfImageCache.swift - ELF support for Swift ----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Provides a per-thread Elf image cache that improves efficiency when
// taking multiple backtraces by avoiding loading ELF images multiple times.
//
//===----------------------------------------------------------------------===//
import Swift
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
internal import Darwin
#elseif os(Windows)
internal import WinSDK
#elseif canImport(Glibc)
internal import Glibc
#elseif canImport(Musl)
internal import Musl
#endif
/// Provides a per-thread image cache for ELF image processing. This means
/// if you take multiple backtraces from a thread, you won't load the same
/// image multiple times.
@available(Backtracing 6.2, *)
final class ElfImageCache {
var elf32: [String: Elf32Image] = [:]
var elf64: [String: Elf64Image] = [:]
func purge() {
elf32 = [:]
elf64 = [:]
}
enum Result {
case elf32Image(Elf32Image)
case elf64Image(Elf64Image)
}
func lookup(path: String?, alternativePaths: [String] = []) -> Result? {
guard let path = path else {
return nil
}
if let image = elf32[path] {
return .elf32Image(image)
}
if let image = elf64[path] {
return .elf64Image(image)
}
if let source = try? ImageSource(path: path, alternativePaths: alternativePaths) {
if let elfImage = try? Elf32Image(source: source) {
elf32[path] = elfImage
return .elf32Image(elfImage)
}
if let elfImage = try? Elf64Image(source: source) {
elf64[path] = elfImage
return .elf64Image(elfImage)
}
}
return nil
}
#if os(Windows)
private static var dwTlsIndex: DWORD = {
let dwNdx = TlsAlloc()
if dwNdx == TLS_OUT_OF_INDEXES {
fatalError("Unable to allocate TSD for ElfImageCache")
}
return dwNdx
}()
#else
private static var key: pthread_key_t = {
var theKey = pthread_key_t()
let err = pthread_key_create(
&theKey,
{ rawPtr in
let ptr = Unmanaged<ElfImageCache>.fromOpaque(
notMutable(notOptional(rawPtr))
)
ptr.release()
}
)
if err != 0 {
fatalError("Unable to create TSD key for ElfImageCache")
}
return theKey
}()
#endif
static var threadLocal: ElfImageCache {
#if os(Windows)
guard let rawPtr = TlsGetValue(dwTlsIndex) else {
let cache = Unmanaged<ElfImageCache>.passRetained(ElfImageCache())
TlsSetValue(dwTlsIndex, cache.toOpaque())
return cache.takeUnretainedValue()
}
#else
guard let rawPtr = pthread_getspecific(key) else {
let cache = Unmanaged<ElfImageCache>.passRetained(ElfImageCache())
pthread_setspecific(key, cache.toOpaque())
return cache.takeUnretainedValue()
}
#endif
let cache = Unmanaged<ElfImageCache>.fromOpaque(rawPtr)
return cache.takeUnretainedValue()
}
}