Skip to content

Commit 5977e12

Browse files
Merge pull request #7074 from apple/dl/lldb-add-foundation._nsswifttimezone-support
[lldb] Add Foundation._NSSwiftTimeZone support
2 parents 85562f7 + 666003e commit 5977e12

File tree

14 files changed

+248
-5
lines changed

14 files changed

+248
-5
lines changed

lldb/include/lldb/Target/LanguageRuntime.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class LanguageRuntime : public Runtime, public PluginInterface {
6767

6868
virtual lldb::LanguageType GetLanguageType() const = 0;
6969

70+
/// Return the preferred language runtime instance, which in most cases will
71+
/// be the current instance.
72+
virtual LanguageRuntime *GetPreferredLanguageRuntime(ValueObject &in_value) {
73+
return nullptr;
74+
}
75+
7076
virtual bool GetObjectDescription(Stream &str, ValueObject &object) = 0;
7177

7278
virtual bool GetObjectDescription(Stream &str, Value &value,

lldb/source/Core/ValueObjectDynamicValue.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,18 @@ bool ValueObjectDynamicValue::UpdateValue() {
181181
known_type != lldb::eLanguageTypeUnknown &&
182182
known_type != lldb::eLanguageTypeC) {
183183
runtime = process->GetLanguageRuntime(known_type);
184-
if (runtime)
184+
if (auto *preferred_runtime =
185+
runtime->GetPreferredLanguageRuntime(*m_parent)) {
186+
// Try the preferred runtime first.
187+
found_dynamic_type = preferred_runtime->GetDynamicTypeAndAddress(
188+
*m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
189+
value_type);
190+
if (found_dynamic_type)
191+
// Set the operative `runtime` for later use in this function.
192+
runtime = preferred_runtime;
193+
}
194+
if (!found_dynamic_type)
195+
// Fallback to the runtime for `known_type`.
185196
found_dynamic_type = runtime->GetDynamicTypeAndAddress(
186197
*m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
187198
value_type);

lldb/source/Plugins/Language/ObjC/Cocoa.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ bool lldb_private::formatters::NSTimeZoneSummaryProvider(
132132
stream.Printf("%s", summary_stream.GetData());
133133
return true;
134134
}
135+
} else if (class_name == "_NSSwiftTimeZone") {
136+
llvm::ArrayRef<ConstString> identifier_path = {
137+
ConstString("timeZone"),
138+
ConstString("_timeZone"),
139+
ConstString("some"),
140+
ConstString("identifier"),
141+
};
142+
if (auto identifier_sp = valobj.GetChildAtNamePath(identifier_path)) {
143+
std::string desc;
144+
if (identifier_sp->GetSummaryAsCString(desc, options)) {
145+
stream.PutCString(desc);
146+
return true;
147+
}
148+
}
135149
}
136150

137151
return false;

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ LoadFoundationValueTypesFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
636636
"Decimal summary provider", ConstString("Foundation.Decimal"),
637637
TypeSummaryImpl::Flags(summary_flags).SetDontShowChildren(true));
638638

639+
lldb_private::formatters::AddCXXSummary(
640+
swift_category_sp, lldb_private::formatters::NSTimeZoneSummaryProvider,
641+
"NSTimeZone summary provider", ConstString("Foundation._NSSwiftTimeZone"),
642+
TypeSummaryImpl::Flags(summary_flags).SetDontShowChildren(true));
643+
639644
lldb_private::formatters::AddCXXSynthetic(
640645
swift_category_sp,
641646
lldb_private::formatters::swift::URLComponentsSyntheticFrontEndCreator,

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#include "lldb/Expression/FunctionCaller.h"
1212
#include "lldb/Target/ABI.h"
13+
#include "lldb/Target/Language.h"
1314
#include "lldb/Utility/LLDBLog.h"
1415
#include "lldb/Utility/Log.h"
16+
#include "lldb/lldb-enumerations.h"
1517

1618
using namespace lldb;
1719
using namespace lldb_private;
@@ -668,6 +670,19 @@ uint64_t ClassDescriptorV2::GetInstanceSize() {
668670
return 0;
669671
}
670672

673+
// From the ObjC runtime.
674+
static uint8_t IS_SWIFT_STABLE = 1U << 1;
675+
676+
LanguageType ClassDescriptorV2::GetImplementationLanguage() const {
677+
std::unique_ptr<objc_class_t> objc_class;
678+
if (auto *process = m_runtime.GetProcess())
679+
if (Read_objc_class(process, objc_class))
680+
if (objc_class->m_flags & IS_SWIFT_STABLE)
681+
return lldb::eLanguageTypeSwift;
682+
683+
return lldb::eLanguageTypeObjC;
684+
}
685+
671686
ClassDescriptorV2::iVarsStorage::iVarsStorage() : m_ivars(), m_mutex() {}
672687

673688
size_t ClassDescriptorV2::iVarsStorage::size() { return m_ivars.size(); }

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <mutex>
1313

1414
#include "AppleObjCRuntimeV2.h"
15+
#include "lldb/lldb-enumerations.h"
1516
#include "lldb/lldb-private.h"
1617

1718
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
@@ -34,6 +35,8 @@ class ClassDescriptorV2 : public ObjCLanguageRuntime::ClassDescriptor {
3435
return true; // any Objective-C v2 runtime class descriptor we vend is valid
3536
}
3637

38+
lldb::LanguageType GetImplementationLanguage() const override;
39+
3740
// a custom descriptor is used for tagged pointers
3841
bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
3942
uint64_t *value_bits = nullptr,

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "lldb/Target/ABI.h"
3434
#include "lldb/Target/DynamicLoader.h"
3535
#include "lldb/Target/ExecutionContext.h"
36+
#include "lldb/Target/LanguageRuntime.h"
3637
#include "lldb/Target/Platform.h"
3738
#include "lldb/Target/Process.h"
3839
#include "lldb/Target/RegisterContext.h"
@@ -758,6 +759,19 @@ AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
758759
RegisterObjCExceptionRecognizer(process);
759760
}
760761

762+
LanguageRuntime *
763+
AppleObjCRuntimeV2::GetPreferredLanguageRuntime(ValueObject &in_value) {
764+
if (auto process_sp = in_value.GetProcessSP()) {
765+
assert(process_sp.get() == m_process);
766+
if (auto descriptor_sp = GetNonKVOClassDescriptor(in_value)) {
767+
LanguageType impl_lang = descriptor_sp->GetImplementationLanguage();
768+
if (impl_lang != eLanguageTypeUnknown)
769+
return process_sp->GetLanguageRuntime(impl_lang);
770+
}
771+
}
772+
return nullptr;
773+
}
774+
761775
bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress(
762776
ValueObject &in_value, lldb::DynamicValueType use_dynamic,
763777
TypeAndOrName &class_type_or_name, Address &address,

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class AppleObjCRuntimeV2 : public AppleObjCRuntime {
3737

3838
static llvm::StringRef GetPluginNameStatic() { return "apple-objc-v2"; }
3939

40+
LanguageRuntime *GetPreferredLanguageRuntime(ValueObject &in_value) override;
41+
4042
static char ID;
4143

4244
bool isA(const void *ClassID) const override {

lldb/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "lldb/Symbol/Type.h"
2424
#include "lldb/Target/LanguageRuntime.h"
2525
#include "lldb/Utility/ConstString.h"
26+
#include "lldb/lldb-enumerations.h"
2627
#include "lldb/lldb-private.h"
2728

2829
class CommandObjectObjC_ClassTable_Dump;
@@ -84,6 +85,11 @@ class ObjCLanguageRuntime : public LanguageRuntime {
8485
return (m_is_cf == eLazyBoolYes);
8586
}
8687

88+
/// Determine whether this class is implemented in Swift.
89+
virtual lldb::LanguageType GetImplementationLanguage() const {
90+
return lldb::eLanguageTypeObjC;
91+
}
92+
8793
virtual bool IsValid() = 0;
8894

8995
/// There are two routines in the ObjC runtime that tagged pointer clients

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "lldb/Symbol/Variable.h"
2323
#include "lldb/Symbol/VariableList.h"
2424
#include "lldb/Target/ProcessStructReader.h"
25+
#include "lldb/Target/SectionLoadList.h"
2526
#include "lldb/Target/Target.h"
2627
#include "lldb/Utility/LLDBLog.h"
2728
#include "lldb/Utility/Log.h"
@@ -39,6 +40,7 @@
3940
#include "swift/Remote/MemoryReader.h"
4041
#include "swift/RemoteAST/RemoteAST.h"
4142
#include "swift/Runtime/Metadata.h"
43+
#include "swift/Strings.h"
4244

4345
#include <sstream>
4446

@@ -2941,6 +2943,80 @@ SwiftLanguageRuntimeImpl::GetValueType(ValueObject &in_value,
29412943
return Value::ValueType::Scalar;
29422944
}
29432945

2946+
namespace {
2947+
struct SwiftNominalType {
2948+
std::string module;
2949+
std::string identifier;
2950+
};
2951+
2952+
// Find the Swift class that backs an ObjC type.
2953+
//
2954+
// A Swift class that uses the @objc(<ClassName>) attribute will emit ObjC
2955+
// metadata into the binary. Typically, ObjC classes have a symbol in the form
2956+
// of OBJC_CLASS_$_<ClassName>, however for Swift classes, there are two symbols
2957+
// that both point to the ObjC class metadata, where the second symbol is a
2958+
// Swift mangled name.
2959+
std::optional<SwiftNominalType> GetSwiftClass(ValueObject &valobj,
2960+
AppleObjCRuntime &objc_runtime) {
2961+
// To find the Swift symbol, the following preparation steps are taken:
2962+
// 1. Get the value's ISA pointer
2963+
// 2. Resolve the ISA load address into an Address instance
2964+
// 3. Get the Module that contains the Address
2965+
auto descriptor_sp = objc_runtime.GetClassDescriptor(valobj);
2966+
if (!descriptor_sp)
2967+
return {};
2968+
2969+
auto isa_load_addr = descriptor_sp->GetISA();
2970+
Address isa;
2971+
const auto &sections = objc_runtime.GetTargetRef().GetSectionLoadList();
2972+
if (!sections.ResolveLoadAddress(isa_load_addr, isa))
2973+
return {};
2974+
2975+
// Next, iterate over the Module's symbol table, looking for a symbol with
2976+
// following criteria:
2977+
// 1. The symbol address is the ISA address
2978+
// 2. The symbol name is a Swift mangled name
2979+
std::optional<StringRef> swift_symbol;
2980+
auto find_swift_symbol_for_isa = [&](Symbol *symbol) {
2981+
if (symbol->GetAddress() == isa) {
2982+
StringRef symbol_name =
2983+
symbol->GetMangled().GetMangledName().GetStringRef();
2984+
if (SwiftLanguageRuntime::IsSwiftMangledName(symbol_name)) {
2985+
swift_symbol = symbol_name;
2986+
return false;
2987+
}
2988+
}
2989+
return true;
2990+
};
2991+
2992+
isa.GetModule()->GetSymtab()->ForEachSymbolContainingFileAddress(
2993+
isa.GetFileAddress(), find_swift_symbol_for_isa);
2994+
if (!swift_symbol)
2995+
return {};
2996+
2997+
// Once the Swift symbol is found, demangle it into a node tree. The node tree
2998+
// provides the final data, the name of the class and the name of its module.
2999+
swift::Demangle::Context ctx;
3000+
auto *global = ctx.demangleSymbolAsNode(*swift_symbol);
3001+
using Kind = Node::Kind;
3002+
auto *class_node = swift_demangle::nodeAtPath(
3003+
global, {Kind::TypeMetadata, Kind::Type, Kind::Class});
3004+
if (class_node && class_node->getNumChildren() == 2) {
3005+
auto module_node = class_node->getFirstChild();
3006+
auto ident_node = class_node->getLastChild();
3007+
if (module_node->getKind() == Kind::Module && module_node->hasText() &&
3008+
ident_node->getKind() == Kind::Identifier && ident_node->hasText()) {
3009+
auto module_name = module_node->getText();
3010+
auto class_name = ident_node->getText();
3011+
return SwiftNominalType{module_name.str(), class_name.str()};
3012+
}
3013+
}
3014+
3015+
return {};
3016+
}
3017+
3018+
} // namespace
3019+
29443020
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(
29453021
ValueObject &in_value, lldb::DynamicValueType use_dynamic,
29463022
TypeAndOrName &class_type_or_name, Address &address,
@@ -2967,9 +3043,22 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(
29673043
dyn_name.startswith("__NS"))
29683044
return false;
29693045

3046+
SwiftNominalType swift_class;
3047+
3048+
if (auto maybe_swift_class = GetSwiftClass(in_value, *objc_runtime)) {
3049+
swift_class = *maybe_swift_class;
3050+
std::string type_name =
3051+
(llvm::Twine(swift_class.module) + "." + swift_class.identifier).str();
3052+
dyn_class_type_or_name.SetName(type_name.data());
3053+
address.SetRawAddress(in_value.GetPointerValue());
3054+
} else {
3055+
swift_class.module = swift::MANGLING_MODULE_OBJC;
3056+
swift_class.identifier = dyn_name;
3057+
}
3058+
29703059
std::string remangled;
29713060
{
2972-
// Create a mangle tree for __C.dyn_name?.
3061+
// Create a mangle tree for Swift.Optional<$module.$class>
29733062
using namespace swift::Demangle;
29743063
NodeFactory factory;
29753064
NodePointer global = factory.createNode(Node::Kind::Global);
@@ -2980,7 +3069,8 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(
29803069
NodePointer ety = factory.createNode(Node::Kind::Type);
29813070
bge->addChild(ety, factory);
29823071
NodePointer e = factory.createNode(Node::Kind::Enum);
2983-
e->addChild(factory.createNode(Node::Kind::Module, "Swift"), factory);
3072+
e->addChild(factory.createNode(Node::Kind::Module, swift::STDLIB_NAME),
3073+
factory);
29843074
e->addChild(factory.createNode(Node::Kind::Identifier, "Optional"),
29853075
factory);
29863076
ety->addChild(e, factory);
@@ -2989,8 +3079,11 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(
29893079
NodePointer cty = factory.createNode(Node::Kind::Type);
29903080
list->addChild(cty, factory);
29913081
NodePointer c = factory.createNode(Node::Kind::Class);
2992-
c->addChild(factory.createNode(Node::Kind::Module, "__C"), factory);
2993-
c->addChild(factory.createNode(Node::Kind::Identifier, dyn_name), factory);
3082+
c->addChild(factory.createNode(Node::Kind::Module, swift_class.module),
3083+
factory);
3084+
c->addChild(
3085+
factory.createNode(Node::Kind::Identifier, swift_class.identifier),
3086+
factory);
29943087
cty->addChild(c, factory);
29953088

29963089
auto mangling = mangleNode(global);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
3+
struct Document {
4+
var kind: Kind
5+
var path: String
6+
7+
enum Kind {
8+
case binary
9+
case text
10+
}
11+
}
12+
13+
@objc(App)
14+
class App : NSObject {
15+
var name: String = "Debugger"
16+
var version: (Int, Int) = (1, 0)
17+
var recentDocuments: [Document]? = [
18+
Document(kind: .binary, path: "/path/to/something"),
19+
]
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OBJC_SOURCES := main.m
2+
SWIFT_SOURCES := App.swift
3+
SWIFTFLAGS_EXTRAS := -parse-as-library
4+
5+
include Makefile.rules
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
@skipUnlessFoundation
9+
@swiftTest
10+
def test(self):
11+
"""Verify printing of Swift implemented ObjC objects."""
12+
self.build()
13+
lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.m"))
14+
15+
# For Swift implemented objects, it's assumed the ObjC runtime prints
16+
# only a pointer, and cannot generate any child values.
17+
self.expect("v -d no app", startstr="(App *) app = 0x")
18+
self.expect(
19+
"v -d no -P1 app",
20+
matching=False,
21+
substrs=["name", "version", "recentDocuments"],
22+
)
23+
24+
# With dynamic typing, the Swift runtime produces Swift child values.
25+
self.expect("v app", substrs=["App?) app = 0x"])
26+
self.expect("v app.name", startstr='(String) app.name = "Debugger"')
27+
self.expect(
28+
"v app.version", startstr="((Int, Int)) app.version = (0 = 1, 1 = 0)"
29+
)
30+
31+
documents = """\
32+
([a.Document]?) app.recentDocuments = 1 value {
33+
[0] = {
34+
kind = binary
35+
path = "/path/to/something"
36+
}
37+
}
38+
"""
39+
self.expect("v app.recentDocuments", startstr=documents)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@interface App : NSObject
4+
@end
5+
6+
int main() {
7+
App *app = [App new];
8+
printf("break here\n");
9+
return 0;
10+
}

0 commit comments

Comments
 (0)