Skip to content

Commit bfdb934

Browse files
committed
[Backtracing] macOS fixes.
A handful of fixes to get the build going again on macOS after the Linux work changed a few things. Most of this is due to rearranging the way we import the various bits of OS functionality (especially SPI) to not rely on SwiftShims.
1 parent 263f0f7 commit bfdb934

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

stdlib/public/Backtracing/Context.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,9 +898,9 @@ extension arm_gprs {
898898
private func thread_get_state<T>(_ thread: thread_t,
899899
_ flavor: CInt,
900900
_ result: inout T) -> kern_return_t {
901-
var count: msg_type_number_t
902-
= msg_type_number_t(MemoryLayout<T>.stride
903-
/ MemoryLayout<natural_t>.stride)
901+
var count: mach_msg_type_number_t
902+
= mach_msg_type_number_t(MemoryLayout<T>.stride
903+
/ MemoryLayout<natural_t>.stride)
904904

905905
return withUnsafeMutablePointer(to: &result) { ptr in
906906
ptr.withMemoryRebound(to: natural_t.self,

stdlib/public/Backtracing/CoreSymbolication.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private let coreFoundationHandle = dlopen(coreFoundationPath, RTLD_LAZY)!
3131

3232
private let coreSymbolicationPath =
3333
"/System/Library/PrivateFrameworks/CoreSymbolication.framework/CoreSymbolication"
34-
private let coreSymbolicationHandle = dlopen(coreSymbolicationPath, RTLDLAZY)!
34+
private let coreSymbolicationHandle = dlopen(coreSymbolicationPath, RTLD_LAZY)!
3535

3636
private let crashReporterSupportPath =
3737
"/System/Library/PrivateFrameworks/CrashReporterSupport.framework/CrashReporterSupport"
@@ -48,7 +48,7 @@ private func symbol<T>(_ handle: UnsafeMutableRawPointer, _ name: String) -> T {
4848

4949
private enum Sym {
5050
// CRCopySanitizedPath
51-
static let CRCopySanitizedPath: @convention(c) (CFStringRef, CFIndex) -> CFStringRef =
51+
static let CRCopySanitizedPath: @convention(c) (CFString, CFIndex) -> CFString =
5252
symbol(crashReporterSupportHandle, "CRCopySanitizedPath")
5353

5454
// Base functionality
@@ -116,17 +116,17 @@ private enum Sym {
116116

117117
// CFString
118118
static let CFStringCreateWithBytes:
119-
@convention(c) (CFAllocatorRef?, UnsafeRawPointer?, CFIndex,
120-
CFStringEncoding, Bool) -> CFStringRef? =
119+
@convention(c) (CFAllocator?, UnsafeRawPointer?, CFIndex,
120+
CFStringEncoding, Bool) -> CFString? =
121121
symbol(coreFoundationHandle, "CFStringCreateWithBytes")
122122
static let CFStringGetLength:
123-
@convention(c) (CFStringRef) -> CFIndex =
123+
@convention(c) (CFString) -> CFIndex =
124124
symbol(coreFoundationHandle, "CFStringGetLength")
125125
static let CFStringGetCStringPtr:
126-
@convention(c) (CFStringRef, CFStringEncoding) -> UnsafePointer<CChar>? =
126+
@convention(c) (CFString, CFStringEncoding) -> UnsafePointer<CChar>? =
127127
symbol(coreFoundationHandle, "CFStringGetCStringPtr")
128128
static let CFStringGetBytes:
129-
@convention(c) (CFStringRef, CFRange, CFStringEncoding, UInt8, Bool,
129+
@convention(c) (CFString, CFRange, CFStringEncoding, UInt8, Bool,
130130
UnsafeMutableRawPointer?, CFIndex,
131131
UnsafeMutablePointer<CFIndex>?) -> CFIndex =
132132
symbol(coreFoundationHandle, "CFStringGetBytes")
@@ -138,30 +138,30 @@ internal func CFRangeMake(_ location: CFIndex, _ length: CFIndex) -> CFRange {
138138
return CFRange(location: location, length: length)
139139
}
140140

141-
internal func CFStringCreateWithBytes(_ allocator: CFAllocatorRef?,
141+
internal func CFStringCreateWithBytes(_ allocator: CFAllocator?,
142142
_ bytes: UnsafeRawPointer?,
143143
_ length: CFIndex,
144144
_ encoding: CFStringEncoding,
145145
_ isExternalRepresentation: Bool)
146-
-> CFStringRef? {
146+
-> CFString? {
147147
return Sym.CFStringCreateWithBytes(allocator,
148148
bytes,
149149
length,
150150
encoding,
151151
isExternalRepresentation)
152152
}
153153

154-
internal func CFStringGetLength(_ s: CFStringRef) -> CFIndex {
154+
internal func CFStringGetLength(_ s: CFString) -> CFIndex {
155155
return Sym.CFStringGetLength(s)
156156
}
157157

158-
internal func CFStringGetCStringPtr(_ s: CFStringRef,
158+
internal func CFStringGetCStringPtr(_ s: CFString,
159159
_ encoding: CFStringEncoding)
160160
-> UnsafePointer<CChar>? {
161161
return Sym.CFStringGetCStringPtr(s, encoding)
162162
}
163163

164-
internal func CFStringGetBytes(_ s: CFStringRef,
164+
internal func CFStringGetBytes(_ s: CFString,
165165
_ range: CFRange,
166166
_ encoding: CFStringEncoding,
167167
_ lossByte: UInt8,
@@ -180,7 +180,7 @@ internal func CFStringGetBytes(_ s: CFStringRef,
180180
// We can't import swiftFoundation here, so there's no automatic bridging for
181181
// CFString. As a result, we need to do the dance manually.
182182

183-
private func toCFString(_ s: String) -> CFStringRef! {
183+
private func toCFString(_ s: String) -> CFString! {
184184
var s = s
185185
return s.withUTF8 {
186186
return CFStringCreateWithBytes(nil,
@@ -191,7 +191,7 @@ private func toCFString(_ s: String) -> CFStringRef! {
191191
}
192192
}
193193

194-
private func fromCFString(_ cf: CFStringRef) -> String {
194+
private func fromCFString(_ cf: CFString) -> String {
195195
let length = CFStringGetLength(cf)
196196
if length == 0 {
197197
return ""

stdlib/public/Backtracing/MemoryReader.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ extension MemoryReader {
128128
let result = mach_vm_read_overwrite(task,
129129
UInt64(address),
130130
UInt64(size),
131-
buffer.baseAddress,
131+
mach_vm_address_t(
132+
Int(bitPattern: buffer.baseAddress)
133+
),
132134
&sizeOut)
133135

134136
if result != KERN_SUCCESS {

stdlib/public/Backtracing/modules/OS/Darwin.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
#endif
2828

2929
#include <mach/mach.h>
30+
#include <mach/mach_vm.h>
3031

3132
#include <libproc.h>
3233
#include <stdint.h>
@@ -67,7 +68,7 @@ struct darwin_arm64_mcontext {
6768
// followed by NEON state (which we don't care about)
6869
};
6970

70-
#define x86_THREAD_STATE64 4
71+
#define X86_THREAD_STATE64 4
7172
struct darwin_x86_64_thread_state {
7273
uint64_t rax;
7374
uint64_t rbx;
@@ -105,23 +106,38 @@ struct darwin_x86_64_mcontext {
105106
// followed by FP/AVX/AVX512 state (which we don't care about)
106107
};
107108

109+
// .. libproc SPI ..............................................................
110+
111+
int proc_name(int pid, void * buffer, uint32_t buffersize);
112+
113+
// .. Mach SPI .................................................................
114+
115+
extern kern_return_t task_read_for_pid(task_t task, int pid, task_t *ptask);
116+
108117
// .. dyld SPI .................................................................
109118

110119
struct dyld_process_cache_info {
111-
__swift_uuid_t cacheUUID;
112-
__swift_uint64_t cacheBaseAddress;
113-
__swift_bool noCache;
114-
__swift_bool privateCache;
120+
uuid_t cacheUUID;
121+
uint64_t cacheBaseAddress;
122+
bool noCache;
123+
bool privateCache;
115124
};
116125
typedef struct dyld_process_cache_info dyld_process_cache_info;
117126
typedef const struct dyld_process_info_base* dyld_process_info;
118127

119-
extern dyld_process_info _dyld_process_info_create(__swift_task_t task, __swift_uint64_t timestamp, __swift_kern_return_t* kernelError);
128+
extern dyld_process_info _dyld_process_info_create(task_t task, uint64_t timestamp, kern_return_t* kernelError);
120129
extern void _dyld_process_info_release(dyld_process_info info);
121130
extern void _dyld_process_info_retain(dyld_process_info info);
122131
extern void _dyld_process_info_get_cache(dyld_process_info info, dyld_process_cache_info* cacheInfo);
123-
extern void _dyld_process_info_for_each_image(dyld_process_info info, void (^callback)(__swift_uint64_t machHeaderAddress, const __swift_uuid_t uuid, const char* path));
124-
extern void _dyld_process_info_for_each_segment(dyld_process_info info, __swift_uint64_t machHeaderAddress, void (^callback)(__swift_uint64_t segmentAddress, __swift_uint64_t segmentSize, const char* segmentName));
132+
extern void _dyld_process_info_for_each_image(dyld_process_info info, void (^callback)(uint64_t machHeaderAddress, const uuid_t uuid, const char* path));
133+
extern void _dyld_process_info_for_each_segment(dyld_process_info info, uint64_t machHeaderAddress, void (^callback)(uint64_t segmentAddress, uint64_t segmentSize, const char* segmentName));
134+
135+
// .. Code Signing SPI .........................................................
136+
137+
#define CS_OPS_STATUS 0
138+
#define CS_PLATFORM_BINARY 0x04000000
139+
#define CS_PLATFORM_PATH 0x08000000
140+
extern int csops(int, unsigned int, void *, size_t);
125141

126142
// .. CoreSymbolication SPI ....................................................
127143

@@ -164,7 +180,7 @@ typedef struct _CSBinaryRelocationInformation {
164180
typedef struct _CSBinaryImageInformation {
165181
vm_address_t base;
166182
vm_address_t extent;
167-
backtrace_CFUUIDBytes uuid;
183+
CFUUIDBytes uuid;
168184
CSArchitecture arch;
169185
const char *path;
170186
CSBinaryRelocationInformation *relocations;

stdlib/public/libexec/swift-backtrace/TargetMacOS.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import _Backtracing
2626
@_spi(MemoryReaders) import _Backtracing
2727

2828
@_implementationOnly import Runtime
29+
@_implementationOnly import OS.Darwin
2930

3031
#if arch(x86_64)
3132
typealias MContext = darwin_x86_64_mcontext
@@ -157,7 +158,7 @@ class Target {
157158

158159
task = parentTask
159160

160-
reader = RemoteMemoryReader(task: __swift_task_t(task))
161+
reader = RemoteMemoryReader(task: task_t(task))
161162

162163
name = Self.getProcessName(pid: pid)
163164

stdlib/public/libexec/swift-backtrace/main.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,13 @@ Generate a backtrace for the parent process.
524524
tcgetattr(0, &oldAttrs)
525525

526526
var newAttrs = oldAttrs
527-
newAttrs.c_lflag &= ~(UInt32(ICANON) | UInt32(ECHO))
527+
528+
#if os(Linux)
529+
newAttrs.c_lflag &= ~UInt32(ICANON | ECHO)
530+
#else
531+
newAttrs.c_lflag &= ~UInt(ICANON | ECHO)
532+
#endif
533+
528534
tcsetattr(0, TCSANOW, &newAttrs)
529535

530536
return oldAttrs

0 commit comments

Comments
 (0)