|
| 1 | +//===--- Backtrace.swift --------------------------------------*- swift -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Defines functions and types allowing to handle Swift's mangling scheme. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Swift |
| 18 | + |
| 19 | +#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) |
| 20 | +internal import Darwin |
| 21 | +#elseif os(Windows) |
| 22 | +internal import ucrt |
| 23 | +#elseif canImport(Glibc) |
| 24 | +internal import Glibc |
| 25 | +#elseif canImport(Musl) |
| 26 | +internal import Musl |
| 27 | +#endif |
| 28 | +internal import BacktracingImpl.Runtime |
| 29 | + |
| 30 | +// - MARK: Demangling |
| 31 | + |
| 32 | +/// Given a mangled Swift symbol, demangle it into a human readable format. |
| 33 | +/// |
| 34 | +/// If the provided bytes are not a valid mangled swift name, the output span will be initialized with zero elements. |
| 35 | +/// If mangling succeeds the output span will contain the resulting demangled string. |
| 36 | +/// A successfully demangled string is _not_ null terminated, and its length is communicated by the `initializedCount` |
| 37 | +/// of the output span. |
| 38 | +/// |
| 39 | +/// The demangled output may be _truncated_ if the output span's capacity is insufficient for the |
| 40 | +/// demangled output string! You can detect this situation by inspecting the returned ``DemanglingResult``, |
| 41 | +/// for the ``DemanglingResult/truncated`` case. |
| 42 | +/// |
| 43 | +/// Valid Swift symbols begin with the following prefixes: |
| 44 | +/// ┌─────────────────────╥────────┐ |
| 45 | +/// │ Swift Version ║ │ |
| 46 | +/// ╞═════════════════════╬════════╡ |
| 47 | +/// │ Swift 3 and below ║ _T │ |
| 48 | +/// ├─────────────────────╫────────┤ |
| 49 | +/// │ Swift 4 ║ _T0 │ |
| 50 | +/// ├─────────────────────╫────────┤ |
| 51 | +/// │ Swift 4.x ║ $S │ |
| 52 | +/// ├─────────────────────╫────────┤ |
| 53 | +/// │ Swift 5+ ║ $s │ |
| 54 | +/// └─────────────────────╨────────┘ |
| 55 | +/// |
| 56 | +/// - Parameters: |
| 57 | +/// - mangledName: A mangled Swift symbol. |
| 58 | +/// - Returns: A human readable demangled Swift symbol. |
| 59 | +/// - Warning: The demangled output is lossy is not not guaranteed to be stable across Swift versions. |
| 60 | +/// Future versions of Swift may choose to print more (or less) information in the demangled format. |
| 61 | +@available(StdlibDeploymentTarget 6.3, *) |
| 62 | +public func demangle(_ mangledName: String) -> String? { |
| 63 | + var length: size_t = 0 |
| 64 | + |
| 65 | + let demangled: UnsafeMutablePointer<Int8>? = _swift_runtime_demangle( |
| 66 | + mangledName, mangledName.utf8.count, |
| 67 | + nil, &length, |
| 68 | + /*flags=*/0 |
| 69 | + ) |
| 70 | + |
| 71 | + guard length > 0 else { |
| 72 | + assert(demangled == nil) |
| 73 | + return nil |
| 74 | + } |
| 75 | + defer { free(demangled) } |
| 76 | + |
| 77 | + return UnsafeBufferPointer(start: demangled, count: length).withMemoryRebound(to: UTF8.CodeUnit.self) { buffer in |
| 78 | + guard let demangledSpan = try? UTF8Span(validating: buffer.span) else { |
| 79 | + return nil |
| 80 | + } |
| 81 | + return String(copying: demangledSpan) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Given a mangled Swift symbol, demangle it into a human readable format. |
| 86 | +/// |
| 87 | +/// If the provided bytes are not a valid mangled swift name, the output span will be initialized with zero elements. |
| 88 | +/// If mangling succeeds the output span will contain the resulting demangled string. |
| 89 | +/// A successfully demangled string is _not_ null terminated, and its length is communicated by the `initializedCount` |
| 90 | +/// of the output span. |
| 91 | +/// |
| 92 | +/// The demangled output may be _truncated_ if the output span's capacity is insufficient for the |
| 93 | +/// demangled output string! You can detect this situation by inspecting the returned ``DemanglingResult``, |
| 94 | +/// for the ``DemanglingResult/truncated`` case. |
| 95 | +/// |
| 96 | +/// Valid Swift symbols begin with the following prefixes: |
| 97 | +/// ┌─────────────────────╥────────┐ |
| 98 | +/// │ Swift Version ║ │ |
| 99 | +/// ╞═════════════════════╬════════╡ |
| 100 | +/// │ Swift 3 and below ║ _T │ |
| 101 | +/// ├─────────────────────╫────────┤ |
| 102 | +/// │ Swift 4 ║ _T0 │ |
| 103 | +/// ├─────────────────────╫────────┤ |
| 104 | +/// │ Swift 4.x ║ $S │ |
| 105 | +/// ├─────────────────────╫────────┤ |
| 106 | +/// │ Swift 5+ ║ $s │ |
| 107 | +/// └─────────────────────╨────────┘ |
| 108 | +/// |
| 109 | +/// - Parameters: |
| 110 | +/// - mangledName: Mangled name to be demangled. |
| 111 | +/// - output: A pre-allocated span to demangle the Swift symbol into. |
| 112 | +/// - Returns: An enum, `DemanglingResult`, indicating the various result states |
| 113 | +/// of demangling. |
| 114 | +/// - Warning: The demangled output is lossy is not not guaranteed to be stable across Swift versions. |
| 115 | +/// Future versions of Swift may choose to print more (or less) information in the demangled format. |
| 116 | +@available(StdlibDeploymentTarget 6.3, *) |
| 117 | +public func demangle( |
| 118 | + _ mangledName: borrowing UTF8Span, |
| 119 | + into output: inout OutputSpan<UTF8.CodeUnit> |
| 120 | +) -> DemanglingResult { |
| 121 | + var demangledLength = output.capacity |
| 122 | + let outputCapacity = output.capacity |
| 123 | + |
| 124 | + let demangledPtr = output.withUnsafeMutableBufferPointer { outputBufferUInt8, outputLength in |
| 125 | + outputBufferUInt8.withMemoryRebound(to: Int8.self) { outputBuffer in |
| 126 | + mangledName.span.withUnsafeBytes { mangledNamePtr in |
| 127 | + let res = _swift_runtime_demangle( |
| 128 | + mangledNamePtr.baseAddress, mangledName.count, |
| 129 | + outputBuffer.baseAddress, &demangledLength, |
| 130 | + /*flags=*/0) |
| 131 | + |
| 132 | + // If the demangled string is longer than the Span capacity, we |
| 133 | + // demangled only up-to the capacity of the span, and therefore must |
| 134 | + // indicate this here. Such situation will return a `.truncated` result. |
| 135 | + outputLength = min(outputCapacity, demangledLength) |
| 136 | + |
| 137 | + return res |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + guard demangledPtr != nil else { |
| 143 | + return .invalidSymbol |
| 144 | + } |
| 145 | + |
| 146 | + // If the buffer size is still equal to the buffer count, the demangle was |
| 147 | + // successful. |
| 148 | + if demangledLength <= output.capacity { |
| 149 | + return .success |
| 150 | + } |
| 151 | + |
| 152 | + // The result was truncated. Return the amount needed to get a full demangle. |
| 153 | + return .truncated(demangledLength) |
| 154 | +} |
| 155 | + |
| 156 | +/// Represents whether or not demangling of a symbol was successful. |
| 157 | +@available(StdlibDeploymentTarget 6.3, *) |
| 158 | +public enum DemanglingResult: Equatable { |
| 159 | + /// Demangling completed successfully. |
| 160 | + case success |
| 161 | + |
| 162 | + /// Demangling resulted in truncating the result. The payload value is the |
| 163 | + /// number of bytes necessary for a full demangle. |
| 164 | + case truncated(Int) |
| 165 | + |
| 166 | + /// The passed Swift mangled symbol was invalid. |
| 167 | + case invalidSymbol |
| 168 | +} |
0 commit comments