|  | 
|  | 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 | +/// Valid Swift symbols begin with the following prefixes: | 
|  | 35 | +///   ┌─────────────────────╥────────┐ | 
|  | 36 | +///   │ Swift Version       ║        │ | 
|  | 37 | +///   ╞═════════════════════╬════════╡ | 
|  | 38 | +///   │ Swift 3 and below   ║   _T   │ | 
|  | 39 | +///   ├─────────────────────╫────────┤ | 
|  | 40 | +///   │ Swift 4             ║  _T0   │ | 
|  | 41 | +///   ├─────────────────────╫────────┤ | 
|  | 42 | +///   │ Swift 4.x           ║   $S   │ | 
|  | 43 | +///   ├─────────────────────╫────────┤ | 
|  | 44 | +///   │ Swift 5+            ║   $s   │ | 
|  | 45 | +///   └─────────────────────╨────────┘ | 
|  | 46 | +/// | 
|  | 47 | +/// - Parameters: | 
|  | 48 | +///   - mangledName: A mangled Swift symbol. | 
|  | 49 | +/// - Returns: A human readable demangled Swift symbol. | 
|  | 50 | +/// - Warning: The demangled output is lossy is not not guaranteed to be stable across Swift versions. | 
|  | 51 | +///            Future versions of Swift may choose to print more (or less) information in the demangled format. | 
|  | 52 | +public func demangle(_ mangledName: String) -> String? { | 
|  | 53 | +  var length: size_t = 0 | 
|  | 54 | + | 
|  | 55 | +  let demangled = _swift_runtime_demangle( | 
|  | 56 | +    mangledName, mangledName.utf8.count, | 
|  | 57 | +    nil, &length, | 
|  | 58 | +    /*flags=*/0 | 
|  | 59 | +  ) | 
|  | 60 | + | 
|  | 61 | +  guard let demangled else { | 
|  | 62 | +    return nil | 
|  | 63 | +  } | 
|  | 64 | +  defer { free(demangled) } | 
|  | 65 | + | 
|  | 66 | +  // length is the size of the buffer that was allocated, *not* the | 
|  | 67 | +  // length of the string. | 
|  | 68 | +  let stringLen = strlen(demangled) | 
|  | 69 | +  guard stringLen > 0 else { | 
|  | 70 | +    return nil | 
|  | 71 | +  } | 
|  | 72 | + | 
|  | 73 | +  return demangled.withMemoryRebound(to: UInt8.self, capacity: stringLen) { | 
|  | 74 | +    let demangledBytes = UnsafeBufferPointer<UInt8>(start: $0, count: stringLen) | 
|  | 75 | +    return String(decoding: demangledBytes, as: UTF8.self) | 
|  | 76 | +  } | 
|  | 77 | +} | 
|  | 78 | + | 
|  | 79 | + | 
|  | 80 | +/// Given a mangled Swift symbol, demangle it into a human readable format. | 
|  | 81 | +/// | 
|  | 82 | +/// Valid Swift symbols begin with the following prefixes: | 
|  | 83 | +///   ┌─────────────────────╥────────┐ | 
|  | 84 | +///   │ Swift Version       ║        │ | 
|  | 85 | +///   ╞═════════════════════╬════════╡ | 
|  | 86 | +///   │ Swift 3 and below   ║   _T   │ | 
|  | 87 | +///   ├─────────────────────╫────────┤ | 
|  | 88 | +///   │ Swift 4             ║  _T0   │ | 
|  | 89 | +///   ├─────────────────────╫────────┤ | 
|  | 90 | +///   │ Swift 4.x           ║   $S   │ | 
|  | 91 | +///   ├─────────────────────╫────────┤ | 
|  | 92 | +///   │ Swift 5+            ║   $s   │ | 
|  | 93 | +///   └─────────────────────╨────────┘ | 
|  | 94 | +/// | 
|  | 95 | +/// - Parameters: | 
|  | 96 | +///   - mangledNameBuffer: A buffer pointer pointing to a null-terminated C | 
|  | 97 | +///                        string that contains the mangled Swift symbol. | 
|  | 98 | +///   - buffer: A pre-allocated buffer to demangle the Swift symbol as a c-string into. | 
|  | 99 | +/// - Returns: An enum, `DemanglingResult`, indicating the various result states | 
|  | 100 | +///            of demangling. | 
|  | 101 | +/// - Warning: The demangled output is lossy is not not guaranteed to be stable across Swift versions. | 
|  | 102 | +///            Future versions of Swift may choose to print more (or less) information in the demangled format. | 
|  | 103 | +public func demangle( | 
|  | 104 | +  _ mangledNameBuffer: UnsafeBufferPointer<Int8>, | 
|  | 105 | +  into buffer: UnsafeMutableBufferPointer<Int8> | 
|  | 106 | +) -> DemanglingResult { | 
|  | 107 | +  var bufferSize = buffer.count | 
|  | 108 | + | 
|  | 109 | +   let demangledPtr = _swift_runtime_demangle( | 
|  | 110 | +    /*mangledName=*/mangledNameBuffer.baseAddress, | 
|  | 111 | +    /*mangledNameLength=*/mangledNameBuffer.count - 1, | 
|  | 112 | +    /*outputBuffer=*/buffer.baseAddress, | 
|  | 113 | +    /*outputBufferSize=*/&bufferSize, | 
|  | 114 | +    /*flags=*/0 | 
|  | 115 | +  ) | 
|  | 116 | + | 
|  | 117 | +  guard let demangledPtr else { | 
|  | 118 | +    return .invalidSymbol | 
|  | 119 | +  } | 
|  | 120 | + | 
|  | 121 | +  // If the buffer size is still equal to the buffer count, the demangle was | 
|  | 122 | +  // successful. | 
|  | 123 | +  if bufferSize <= buffer.count { | 
|  | 124 | +    return .success | 
|  | 125 | +  } | 
|  | 126 | + | 
|  | 127 | +  // However if it's not equal, the result was truncated. Return the amount | 
|  | 128 | +  // needed to get a full demangle. | 
|  | 129 | +  return .truncated(bufferSize) | 
|  | 130 | +} | 
|  | 131 | + | 
|  | 132 | +/// Given a mangled Swift symbol, demangle it into a human readable format. | 
|  | 133 | +/// | 
|  | 134 | +/// Valid Swift symbols begin with the following prefixes: | 
|  | 135 | +///   ┌─────────────────────╥────────┐ | 
|  | 136 | +///   │ Swift Version       ║        │ | 
|  | 137 | +///   ╞═════════════════════╬════════╡ | 
|  | 138 | +///   │ Swift 3 and below   ║   _T   │ | 
|  | 139 | +///   ├─────────────────────╫────────┤ | 
|  | 140 | +///   │ Swift 4             ║  _T0   │ | 
|  | 141 | +///   ├─────────────────────╫────────┤ | 
|  | 142 | +///   │ Swift 4.x           ║   $S   │ | 
|  | 143 | +///   ├─────────────────────╫────────┤ | 
|  | 144 | +///   │ Swift 5+            ║   $s   │ | 
|  | 145 | +///   └─────────────────────╨────────┘ | 
|  | 146 | +/// | 
|  | 147 | +/// - Parameters: | 
|  | 148 | +///   - mangledName: A mangled Swift symbol. | 
|  | 149 | +///   - buffer: A pre-allocated buffer to demangle the Swift symbol as a c-string into. | 
|  | 150 | +/// - Returns: An enum, `DemanglingResult`, indicating the various result states | 
|  | 151 | +///            of demangling. | 
|  | 152 | +/// - Warning: The demangled output is lossy is not not guaranteed to be stable across Swift versions. | 
|  | 153 | +///            Future versions of Swift may choose to print more (or less) information in the demangled format. | 
|  | 154 | +public func demangle( | 
|  | 155 | +  _ mangledName: String, | 
|  | 156 | +  into buffer: UnsafeMutableBufferPointer<Int8> | 
|  | 157 | +) -> DemanglingResult { | 
|  | 158 | +  mangledName.utf8CString.withUnsafeBufferPointer { | 
|  | 159 | +    demangle($0, into: buffer) | 
|  | 160 | +  } | 
|  | 161 | +} | 
|  | 162 | + | 
|  | 163 | +/// Represents whether or not demangling of a symbol was successful. | 
|  | 164 | +public enum DemanglingResult: Equatable { | 
|  | 165 | +  /// Demangling completed successfully. | 
|  | 166 | +  case success | 
|  | 167 | + | 
|  | 168 | +  /// Demangling resulted in truncating the result. The payload value is the | 
|  | 169 | +  /// number of bytes necessary for a full demangle. | 
|  | 170 | +  case truncated(Int) | 
|  | 171 | + | 
|  | 172 | +  /// The passed Swift mangled symbol was invalid. | 
|  | 173 | +  case invalidSymbol | 
|  | 174 | +} | 
0 commit comments