|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +private import _TestingInternals |
| 12 | + |
| 13 | +/// A type representing a backtrace or stack trace. |
| 14 | +@_spi(Experimental) @_spi(ForToolsIntegrationOnly) |
| 15 | +extension Backtrace { |
| 16 | + /// An enumeration describing the symbolication mode to use when handling |
| 17 | + /// events containing backtraces. |
| 18 | + public enum SymbolicationMode: Sendable { |
| 19 | + /// The backtrace should be symbolicated, but no demangling should be |
| 20 | + /// performed. |
| 21 | + case mangled |
| 22 | + |
| 23 | + /// The backtrace should be symbolicated and Swift symbols should be |
| 24 | + /// demangled if possible. |
| 25 | + /// |
| 26 | + /// "Foreign" symbol names such as those produced by C++ are not demangled. |
| 27 | + case demangled |
| 28 | + } |
| 29 | + |
| 30 | + /// A type representing an instance of ``Backtrace/Address`` that has been |
| 31 | + /// symbolicated by a call to ``Backtrace/symbolicate(_:)``. |
| 32 | + public struct SymbolicatedAddress: Sendable { |
| 33 | + /// The (unsymbolicated) address from the backtrace. |
| 34 | + public var address: Address |
| 35 | + |
| 36 | + /// The offset of ``address`` from the start of the corresponding function, |
| 37 | + /// if available. |
| 38 | + /// |
| 39 | + /// If ``address`` could not be resolved to a symbol, the value of this |
| 40 | + /// property is `nil`. |
| 41 | + public var offset: UInt64? |
| 42 | + |
| 43 | + /// The name of the symbol at ``address``, if available. |
| 44 | + /// |
| 45 | + /// If ``address`` could not be resolved to a symbol, the value of this |
| 46 | + /// property is `nil`. |
| 47 | + public var symbolName: String? |
| 48 | + } |
| 49 | + |
| 50 | + /// Symbolicate the addresses in this backtrace. |
| 51 | + /// |
| 52 | + /// - Parameters: |
| 53 | + /// - mode: How to symbolicate the addresses in the backtrace. |
| 54 | + /// |
| 55 | + /// - Returns: An array of strings representing the names of symbols in |
| 56 | + /// `addresses`. |
| 57 | + /// |
| 58 | + /// If an address in `addresses` cannot be symbolicated, the corresponding |
| 59 | + /// instance of ``SymbolicatedAddress`` in the resulting array has a `nil` |
| 60 | + /// value for its ``Backtrace/SymbolicatedAddress/symbolName`` property. |
| 61 | + public func symbolicate(_ mode: SymbolicationMode) -> [SymbolicatedAddress] { |
| 62 | + var result = addresses.map { SymbolicatedAddress(address: $0) } |
| 63 | + |
| 64 | +#if SWT_TARGET_OS_APPLE |
| 65 | + for (i, address) in addresses.enumerated() { |
| 66 | + var info = Dl_info() |
| 67 | + if 0 != dladdr(UnsafePointer(bitPattern: UInt(clamping: address)), &info) { |
| 68 | + let offset = address - Address(clamping: UInt(bitPattern: info.dli_saddr)) |
| 69 | + let symbolName = info.dli_sname.flatMap(String.init(validatingCString:)) |
| 70 | + result[i] = SymbolicatedAddress(address: address, offset: offset, symbolName: symbolName) |
| 71 | + } |
| 72 | + } |
| 73 | +#elseif os(Linux) |
| 74 | + // Although Linux has dladdr(), it does not have symbol names from ELF |
| 75 | + // binaries by default. The standard library's backtracing functionality has |
| 76 | + // implemented sufficient ELF/DWARF parsing to be able to symbolicate Linux |
| 77 | + // backtraces. TODO: adopt the standard library's Backtrace on Linux |
| 78 | + // Note that this means on Linux we don't have demangling capability (since |
| 79 | + // we don't have the mangled symbol names in the first place) so this code |
| 80 | + // does not check the mode argument. |
| 81 | +#elseif os(Windows) |
| 82 | + _withDbgHelpLibrary { hProcess in |
| 83 | + guard let hProcess else { |
| 84 | + return |
| 85 | + } |
| 86 | + for (i, address) in addresses.enumerated() { |
| 87 | + withUnsafeTemporaryAllocation(of: SYMBOL_INFO_PACKAGEW.self, capacity: 1) { symbolInfo in |
| 88 | + let symbolInfo = symbolInfo.baseAddress! |
| 89 | + symbolInfo.pointee.si.SizeOfStruct = ULONG(MemoryLayout<SYMBOL_INFOW>.stride) |
| 90 | + symbolInfo.pointee.si.MaxNameLen = ULONG(MAX_SYM_NAME) |
| 91 | + var displacement = DWORD64(0) |
| 92 | + if SymFromAddrW(hProcess, DWORD64(clamping: address), &displacement, symbolInfo.pointer(to: \.si)!) { |
| 93 | + let symbolName = String.decodeCString(symbolInfo.pointer(to: \.si.Name)!, as: UTF16.self)?.result |
| 94 | + result[i] = SymbolicatedAddress(address: address, offset: displacement, symbolName: symbolName) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +#elseif os(WASI) |
| 100 | + // WASI does not currently support backtracing let alone symbolication. |
| 101 | +#else |
| 102 | +#warning("Platform-specific implementation missing: backtrace symbolication unavailable") |
| 103 | +#endif |
| 104 | + |
| 105 | + if mode != .mangled { |
| 106 | + result = result.map { symbolicatedAddress in |
| 107 | + var symbolicatedAddress = symbolicatedAddress |
| 108 | + if let demangledName = symbolicatedAddress.symbolName.flatMap(_demangle) { |
| 109 | + symbolicatedAddress.symbolName = demangledName |
| 110 | + } |
| 111 | + return symbolicatedAddress |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return result |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// MARK: - Codable |
| 120 | + |
| 121 | +extension Backtrace.SymbolicatedAddress: Codable {} |
| 122 | + |
| 123 | +// MARK: - Swift runtime wrappers |
| 124 | + |
| 125 | +/// Demangle a symbol name. |
| 126 | +/// |
| 127 | +/// - Parameters: |
| 128 | +/// - mangledSymbolName: The symbol name to demangle. |
| 129 | +/// |
| 130 | +/// - Returns: The demangled form of `mangledSymbolName` according to the |
| 131 | +/// Swift standard library or the platform's C++ standard library, or `nil` |
| 132 | +/// if the symbol name could not be demangled. |
| 133 | +private func _demangle(_ mangledSymbolName: String) -> String? { |
| 134 | + mangledSymbolName.withCString { mangledSymbolName in |
| 135 | + guard let demangledSymbolName = swift_demangle(mangledSymbolName, strlen(mangledSymbolName), nil, nil, 0) else { |
| 136 | + return nil |
| 137 | + } |
| 138 | + defer { |
| 139 | + free(demangledSymbolName) |
| 140 | + } |
| 141 | + return String(validatingCString: demangledSymbolName) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#if os(Windows) |
| 146 | +/// Configure the environment to allow calling into the Debug Help library. |
| 147 | +/// |
| 148 | +/// - Parameters: |
| 149 | +/// - body: A function to invoke. A process handle valid for use with Debug |
| 150 | +/// Help functions is passed in, or `nullptr` if the Debug Help library |
| 151 | +/// could not be initialized. |
| 152 | +/// - context: An arbitrary pointer to pass to `body`. |
| 153 | +/// |
| 154 | +/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All |
| 155 | +/// calls into it from the Swift runtime and stdlib should route through this |
| 156 | +/// function. |
| 157 | +private func _withDbgHelpLibrary(_ body: (HANDLE?) -> Void) { |
| 158 | + withoutActuallyEscaping(body) { body in |
| 159 | + withUnsafePointer(to: body) { context in |
| 160 | + _swift_win32_withDbgHelpLibrary({ hProcess, context in |
| 161 | + let body = context!.load(as: ((HANDLE?) -> Void).self) |
| 162 | + body(hProcess) |
| 163 | + }, .init(mutating: context)) |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | +#endif |
0 commit comments