Skip to content

Commit 42a4537

Browse files
authored
Merge pull request #4204 from apple/runtime-expose-SwiftError-layout
runtime: expose the layout of SwiftError to LLDB
2 parents 59fd678 + 3709e1f commit 42a4537

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(swift_runtime_sources
3939
CygwinPort.cpp
4040
Demangle.cpp
4141
Enum.cpp
42+
ErrorObjectConstants.cpp
4243
ErrorObjectNative.cpp
4344
Errors.cpp
4445
Heap.cpp

stdlib/public/runtime/ErrorObject.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct SwiftError : SwiftErrorHeader {
7878
/// This member is only available for native Swift errors.
7979
const WitnessTable *errorConformance;
8080

81+
#if SWIFT_OBJC_INTEROP
8182
/// The base type that introduces the `Hashable` conformance.
8283
/// This member is only available for native Swift errors.
8384
/// This member is lazily-initialized.
@@ -89,6 +90,7 @@ struct SwiftError : SwiftErrorHeader {
8990
/// This member is lazily-initialized.
9091
/// Instead of using it directly, call `getHashableConformance()`.
9192
mutable std::atomic<const hashable_support::HashableWitnessTable *> hashableConformance;
93+
#endif
9294

9395
/// Get a pointer to the value contained inside the indirectly-referenced
9496
/// box reference.
@@ -143,13 +145,15 @@ struct SwiftError : SwiftErrorHeader {
143145
const WitnessTable *getErrorConformance() const { return errorConformance; }
144146
#endif
145147

148+
#if SWIFT_OBJC_INTEROP
146149
/// Get the base type that conforms to `Hashable`.
147150
/// Returns NULL if the type does not conform.
148151
const Metadata *getHashableBaseType() const;
149152

150153
/// Get the `Hashable` protocol witness table for the contained type.
151154
/// Returns NULL if the type does not conform.
152155
const hashable_support::HashableWitnessTable *getHashableConformance() const;
156+
#endif
153157

154158
// Don't copy or move, please.
155159
SwiftError(const SwiftError &) = delete;
@@ -229,6 +233,14 @@ const Metadata *getNSErrorMetadata();
229233

230234
#endif
231235

236+
SWIFT_RUNTIME_EXPORT
237+
extern "C"
238+
const size_t _swift_lldb_offsetof_SwiftError_typeMetadata;
239+
240+
SWIFT_RUNTIME_EXPORT
241+
extern "C"
242+
const size_t _swift_lldb_sizeof_SwiftError;
243+
232244
} // namespace swift
233245

234246
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "ErrorObject.h"
14+
15+
using namespace swift;
16+
17+
#pragma clang diagnostic push
18+
#pragma clang diagnostic ignored "-Winvalid-offsetof"
19+
const size_t swift::_swift_lldb_offsetof_SwiftError_typeMetadata =
20+
offsetof(SwiftError, type);
21+
#pragma clang diagnostic pop
22+
23+
const size_t swift::_swift_lldb_sizeof_SwiftError = sizeof(SwiftError);
24+

test/1_stdlib/Runtime.swift.gyb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import Swift
99
import StdlibUnittest
1010
import SwiftShims
1111

12+
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
13+
import Darwin
14+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
15+
import Glibc
16+
#endif
1217

1318
@_silgen_name("swift_demangle")
1419
public
@@ -496,6 +501,35 @@ Runtime.test("Struct layout with reference storage types") {
496501
print(malkovich)
497502
}
498503

504+
Runtime.test("SwiftError layout constants for LLDB") {
505+
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
506+
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
507+
#elseif os(Linux)
508+
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
509+
#else
510+
_UnimplementedError()
511+
#endif
512+
513+
let offsetof_SwiftError_typeMetadata =
514+
dlsym(RTLD_DEFAULT, "_swift_lldb_offsetof_SwiftError_typeMetadata")!
515+
let sizeof_SwiftError =
516+
dlsym(RTLD_DEFAULT, "_swift_lldb_sizeof_SwiftError")!
517+
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
518+
#if arch(i386) || arch(arm)
519+
expectEqual(20, offsetof_SwiftError_typeMetadata.load(as: UInt.self))
520+
expectEqual(36, sizeof_SwiftError.load(as: UInt.self))
521+
#else
522+
expectEqual(40, offsetof_SwiftError_typeMetadata.load(as: UInt.self))
523+
expectEqual(72, sizeof_SwiftError.load(as: UInt.self))
524+
#endif
525+
#elseif os(Linux)
526+
expectEqual(16, offsetof_SwiftError_typeMetadata.load(as: UInt.self))
527+
expectEqual(32, sizeof_SwiftError.load(as: UInt.self))
528+
#else
529+
_UnimplementedError()
530+
#endif
531+
}
532+
499533
var Reflection = TestSuite("Reflection")
500534

501535
func wrap1 (_ x: Any) -> Any { return x }

0 commit comments

Comments
 (0)