1 //===-- NSError.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
13 #include "clang/AST/DeclCXX.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Core/ValueObjectConstResult.h"
20 #include "lldb/DataFormatters/FormattersHelpers.h"
21 #include "lldb/Symbol/ClangASTContext.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/ProcessStructReader.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/DataBufferHeap.h"
26 #include "lldb/Utility/Endian.h"
27 #include "lldb/Utility/Status.h"
28 #include "lldb/Utility/Stream.h"
30 #include "Plugins/Language/ObjC/NSString.h"
33 using namespace lldb_private;
34 using namespace lldb_private::formatters;
36 static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj) {
37 CompilerType valobj_type(valobj.GetCompilerType());
38 Flags type_flags(valobj_type.GetTypeInfo());
39 if (type_flags.AllClear(eTypeHasValue)) {
40 if (valobj.IsBaseClass() && valobj.GetParent())
41 return valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
43 lldb::addr_t ptr_value = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
44 if (type_flags.AllSet(eTypeIsPointer)) {
45 CompilerType pointee_type(valobj_type.GetPointeeType());
46 Flags pointee_flags(pointee_type.GetTypeInfo());
47 if (pointee_flags.AllSet(eTypeIsPointer)) {
48 if (ProcessSP process_sp = valobj.GetProcessSP()) {
50 ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
57 return LLDB_INVALID_ADDRESS;
60 bool lldb_private::formatters::NSError_SummaryProvider(
61 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
62 ProcessSP process_sp(valobj.GetProcessSP());
66 lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj);
67 if (ptr_value == LLDB_INVALID_ADDRESS)
70 size_t ptr_size = process_sp->GetAddressByteSize();
71 lldb::addr_t code_location = ptr_value + 2 * ptr_size;
72 lldb::addr_t domain_location = ptr_value + 3 * ptr_size;
75 uint64_t code = process_sp->ReadUnsignedIntegerFromMemory(code_location,
80 lldb::addr_t domain_str_value =
81 process_sp->ReadPointerFromMemory(domain_location, error);
82 if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
85 if (!domain_str_value) {
86 stream.Printf("domain: nil - code: %" PRIu64, code);
90 InferiorSizedWord isw(domain_str_value, *process_sp);
92 ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData(
93 "domain_str", isw.GetAsData(process_sp->GetByteOrder()),
94 valobj.GetExecutionContextRef(), process_sp->GetTarget()
95 .GetScratchClangASTContext()
96 ->GetBasicType(lldb::eBasicTypeVoid)
102 StreamString domain_str_summary;
103 if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) &&
104 !domain_str_summary.Empty()) {
105 stream.Printf("domain: %s - code: %" PRIu64, domain_str_summary.GetData(),
109 stream.Printf("domain: nil - code: %" PRIu64, code);
114 class NSErrorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
116 NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
117 : SyntheticChildrenFrontEnd(*valobj_sp) {}
119 ~NSErrorSyntheticFrontEnd() override = default;
120 // no need to delete m_child_ptr - it's kept alive by the cluster manager on
123 size_t CalculateNumChildren() override {
131 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
133 return lldb::ValueObjectSP();
136 return m_child_ptr->GetSP();
140 bool Update() override {
141 m_child_ptr = nullptr;
144 ProcessSP process_sp(m_backend.GetProcessSP());
148 lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
149 if (userinfo_location == LLDB_INVALID_ADDRESS)
152 size_t ptr_size = process_sp->GetAddressByteSize();
154 userinfo_location += 4 * ptr_size;
156 lldb::addr_t userinfo =
157 process_sp->ReadPointerFromMemory(userinfo_location, error);
158 if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
160 InferiorSizedWord isw(userinfo, *process_sp);
161 m_child_sp = CreateValueObjectFromData(
162 "_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
163 m_backend.GetExecutionContextRef(),
164 process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType(
165 lldb::eBasicTypeObjCID));
169 bool MightHaveChildren() override { return true; }
171 size_t GetIndexOfChildWithName(const ConstString &name) override {
172 static ConstString g___userInfo("_userInfo");
173 if (name == g___userInfo)
179 // the child here can be "real" (i.e. an actual child of the root) or
180 // synthetized from raw memory if the former, I need to store a plain pointer
181 // to it - or else a loop of references will cause this entire hierarchy of
182 // values to leak if the latter, then I need to store a SharedPointer to it -
183 // so that it only goes away when everyone else in the cluster goes away oh
185 ValueObject *m_child_ptr;
186 ValueObjectSP m_child_sp;
189 SyntheticChildrenFrontEnd *
190 lldb_private::formatters::NSErrorSyntheticFrontEndCreator(
191 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
192 lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
195 ObjCLanguageRuntime *runtime =
196 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
197 lldb::eLanguageTypeObjC);
201 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
202 runtime->GetClassDescriptor(*valobj_sp.get()));
204 if (!descriptor.get() || !descriptor->IsValid())
207 const char *class_name = descriptor->GetClassName().GetCString();
209 if (!class_name || !*class_name)
212 if (!strcmp(class_name, "NSError"))
213 return (new NSErrorSyntheticFrontEnd(valobj_sp));
214 else if (!strcmp(class_name, "__NSCFError"))
215 return (new NSErrorSyntheticFrontEnd(valobj_sp));