|
| 1 | +//===--- TargetLinux.swift - Represents a process we are inspecting -------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 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 `Target`, which represents the process we are inspecting. |
| 14 | +// This is the Linux version. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#if os(Linux) |
| 19 | + |
| 20 | +import Glibc |
| 21 | + |
| 22 | +import _Backtracing |
| 23 | +@_spi(Internal) import _Backtracing |
| 24 | +@_spi(Contexts) import _Backtracing |
| 25 | +@_spi(MemoryReaders) import _Backtracing |
| 26 | +@_spi(Utils) import _Backtracing |
| 27 | + |
| 28 | +@_implementationOnly import Runtime |
| 29 | + |
| 30 | +struct TargetThread { |
| 31 | + typealias ThreadID = pid_t |
| 32 | + |
| 33 | + var id: ThreadID |
| 34 | + var context: HostContext? |
| 35 | + var name: String |
| 36 | + var backtrace: SymbolicatedBacktrace |
| 37 | +} |
| 38 | + |
| 39 | +class Target { |
| 40 | + typealias Address = UInt64 |
| 41 | + |
| 42 | + var pid: pid_t |
| 43 | + var name: String |
| 44 | + var signal: UInt64 |
| 45 | + var faultAddress: Address |
| 46 | + var crashingThread: TargetThread.ThreadID |
| 47 | + |
| 48 | + var images: [Backtrace.Image] = [] |
| 49 | + |
| 50 | + var threads: [TargetThread] = [] |
| 51 | + var crashingThreadNdx: Int = -1 |
| 52 | + |
| 53 | + var signalName: String { |
| 54 | + switch signal { |
| 55 | + case UInt64(SIGQUIT): return "SIGQUIT" |
| 56 | + case UInt64(SIGABRT): return "SIGABRT" |
| 57 | + case UInt64(SIGBUS): return "SIGBUS" |
| 58 | + case UInt64(SIGFPE): return "SIGFPE" |
| 59 | + case UInt64(SIGILL): return "SIGILL" |
| 60 | + case UInt64(SIGSEGV): return "SIGSEGV" |
| 61 | + case UInt64(SIGTRAP): return "SIGTRAP" |
| 62 | + default: return "\(signal)" |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + var signalDescription: String { |
| 67 | + switch signal { |
| 68 | + case UInt64(SIGQUIT): return "Terminated" |
| 69 | + case UInt64(SIGABRT): return "Aborted" |
| 70 | + case UInt64(SIGBUS): return "Bus error" |
| 71 | + case UInt64(SIGFPE): return "Floating point exception" |
| 72 | + case UInt64(SIGILL): return "Illegal instruction" |
| 73 | + case UInt64(SIGSEGV): return "Bad pointer dereference" |
| 74 | + case UInt64(SIGTRAP): return "System trap" |
| 75 | + default: |
| 76 | + return "Signal \(signal)" |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var reader: MemserverMemoryReader |
| 81 | + |
| 82 | + // Get the name of a process |
| 83 | + private static func getProcessName(pid: pid_t) -> String { |
| 84 | + let path = "/proc/\(pid)/comm" |
| 85 | + guard let name = readString(from: path) else { |
| 86 | + return "" |
| 87 | + } |
| 88 | + return String(stripWhitespace(name)) |
| 89 | + } |
| 90 | + |
| 91 | + /// Get the name of a thread |
| 92 | + private func getThreadName(tid: Int64) -> String { |
| 93 | + let path = "/proc/\(pid)/task/\(tid)/comm" |
| 94 | + guard let name = readString(from: path) else { |
| 95 | + return "" |
| 96 | + } |
| 97 | + let trimmed = String(stripWhitespace(name)) |
| 98 | + |
| 99 | + // Allow the main thread to use the process' name, but other |
| 100 | + // threads will have an empty name unless they've set the name |
| 101 | + // explicitly |
| 102 | + if trimmed == self.name && pid != tid { |
| 103 | + return "" |
| 104 | + } |
| 105 | + return trimmed |
| 106 | + } |
| 107 | + |
| 108 | + init(crashInfoAddr: UInt64, limit: Int?, top: Int, cache: Bool) { |
| 109 | + // fd #4 is reserved for the memory server |
| 110 | + let memserverFd: CInt = 4 |
| 111 | + |
| 112 | + pid = getppid() |
| 113 | + reader = MemserverMemoryReader(fd: memserverFd) |
| 114 | + name = Self.getProcessName(pid: pid) |
| 115 | + |
| 116 | + let crashInfo: CrashInfo |
| 117 | + do { |
| 118 | + crashInfo = try reader.fetch(from: crashInfoAddr, as: CrashInfo.self) |
| 119 | + } catch { |
| 120 | + print("swift-backtrace: unable to fetch crash info.") |
| 121 | + exit(1) |
| 122 | + } |
| 123 | + |
| 124 | + crashingThread = TargetThread.ThreadID(crashInfo.crashing_thread) |
| 125 | + signal = crashInfo.signal |
| 126 | + faultAddress = crashInfo.fault_address |
| 127 | + |
| 128 | + images = Backtrace.captureImages(using: reader, |
| 129 | + forProcess: Int(pid)) |
| 130 | + |
| 131 | + do { |
| 132 | + try fetchThreads(threadListHead: Address(crashInfo.thread_list), |
| 133 | + limit: limit, top: top, cache: cache) |
| 134 | + } catch { |
| 135 | + print("swift-backtrace: failed to fetch thread information: \(error)") |
| 136 | + exit(1) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// Fetch information about all of the process's threads; the crash_info |
| 141 | + /// structure contains a linked list of thread ucontexts, which may not |
| 142 | + /// include every thread. In particular, if a thread was stuck in an |
| 143 | + /// uninterruptible wait, we won't have a ucontext for it. |
| 144 | + func fetchThreads( |
| 145 | + threadListHead: Address, |
| 146 | + limit: Int?, top: Int, cache: Bool |
| 147 | + ) throws { |
| 148 | + var next = threadListHead |
| 149 | + |
| 150 | + while next != 0 { |
| 151 | + let t = try reader.fetch(from: next, as: thread.self) |
| 152 | + |
| 153 | + next = t.next |
| 154 | + |
| 155 | + guard let ucontext |
| 156 | + = try? reader.fetch(from: t.uctx, as: ucontext_t.self) else { |
| 157 | + // This can happen if a thread is in an uninterruptible wait |
| 158 | + continue |
| 159 | + } |
| 160 | + |
| 161 | + let context = HostContext.fromHostMContext(ucontext.uc_mcontext) |
| 162 | + let backtrace = try Backtrace.capture(from: context, |
| 163 | + using: reader, |
| 164 | + images: images, |
| 165 | + limit: limit, |
| 166 | + top: top) |
| 167 | + guard let symbolicated |
| 168 | + = backtrace.symbolicated(with: images, |
| 169 | + sharedCacheInfo: nil, |
| 170 | + useSymbolCache: cache) else { |
| 171 | + print("unable to symbolicate backtrace for thread \(t.tid)") |
| 172 | + exit(1) |
| 173 | + } |
| 174 | + |
| 175 | + threads.append(TargetThread(id: TargetThread.ThreadID(t.tid), |
| 176 | + context: context, |
| 177 | + name: getThreadName(tid: t.tid), |
| 178 | + backtrace: symbolicated)) |
| 179 | + } |
| 180 | + |
| 181 | + // Sort the threads by thread ID; the main thread always sorts |
| 182 | + // lower than any other. |
| 183 | + threads.sort { |
| 184 | + return $0.id == pid || ($1.id != pid && $0.id < $1.id) |
| 185 | + } |
| 186 | + |
| 187 | + // Find the crashing thread index |
| 188 | + if let ndx = threads.firstIndex(where: { $0.id == crashingThread }) { |
| 189 | + crashingThreadNdx = ndx |
| 190 | + } else { |
| 191 | + print("unable to find the crashing thread") |
| 192 | + exit(1) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + func redoBacktraces(limit: Int?, top: Int, cache: Bool) { |
| 197 | + for (ndx, thread) in threads.enumerated() { |
| 198 | + guard let context = thread.context else { |
| 199 | + continue |
| 200 | + } |
| 201 | + |
| 202 | + guard let backtrace = try? Backtrace.capture(from: context, |
| 203 | + using: reader, |
| 204 | + images: images, |
| 205 | + limit: limit, |
| 206 | + top: top) else { |
| 207 | + print("unable to capture backtrace from context for thread \(ndx)") |
| 208 | + continue |
| 209 | + } |
| 210 | + |
| 211 | + guard let symbolicated = backtrace.symbolicated(with: images, |
| 212 | + sharedCacheInfo: nil, |
| 213 | + useSymbolCache: cache) else { |
| 214 | + print("unable to symbolicate backtrace from context for thread \(ndx)") |
| 215 | + continue |
| 216 | + } |
| 217 | + |
| 218 | + threads[ndx].backtrace = symbolicated |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + func withDebugger(_ body: () -> ()) throws { |
| 223 | + print(""" |
| 224 | + From another shell, please run |
| 225 | +
|
| 226 | + lldb --attach-pid \(pid) -o c |
| 227 | + """) |
| 228 | + body() |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +#endif // os(Linux) |
0 commit comments