1 //===-- NSError.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Cocoa.h"
11 
12 #include "lldb/Core/DataBufferHeap.h"
13 #include "lldb/Core/Error.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/ValueObject.h"
16 #include "lldb/Core/ValueObjectConstResult.h"
17 #include "lldb/DataFormatters/FormattersHelpers.h"
18 #include "lldb/Host/Endian.h"
19 #include "lldb/Symbol/ClangASTContext.h"
20 #include "lldb/Target/ObjCLanguageRuntime.h"
21 #include "lldb/Target/Target.h"
22 
23 #include "lldb/Utility/ProcessStructReader.h"
24 
25 #include "clang/AST/DeclCXX.h"
26 
27 #include "Plugins/Language/ObjC/NSString.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 using namespace lldb_private::formatters;
32 
33 bool
34 lldb_private::formatters::NSError_SummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
35 {
36     ProcessSP process_sp(valobj.GetProcessSP());
37     if (!process_sp)
38         return false;
39 
40     lldb::addr_t ptr_value = LLDB_INVALID_ADDRESS;
41 
42     CompilerType valobj_type(valobj.GetCompilerType());
43     Flags type_flags(valobj_type.GetTypeInfo());
44     if (type_flags.AllClear(eTypeHasValue))
45     {
46         if (valobj.IsBaseClass() && valobj.GetParent())
47             ptr_value = valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
48     }
49     else
50         ptr_value = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
51 
52     if (ptr_value == LLDB_INVALID_ADDRESS)
53         return false;
54     size_t ptr_size = process_sp->GetAddressByteSize();
55     lldb::addr_t code_location = ptr_value + 2 * ptr_size;
56     lldb::addr_t domain_location = ptr_value + 3 * ptr_size;
57 
58     Error error;
59     uint64_t code = process_sp->ReadUnsignedIntegerFromMemory(code_location, ptr_size, 0, error);
60     if (error.Fail())
61         return false;
62 
63     lldb::addr_t domain_str_value = process_sp->ReadPointerFromMemory(domain_location, error);
64     if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
65         return false;
66 
67     if (!domain_str_value)
68     {
69         stream.Printf("domain: nil - code: %" PRIu64, code);
70         return true;
71     }
72 
73     InferiorSizedWord isw(domain_str_value, *process_sp);
74 
75     ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData("domain_str", isw.GetAsData(process_sp->GetByteOrder()), valobj.GetExecutionContextRef(), process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType(lldb::eBasicTypeVoid).GetPointerType());
76 
77     if (!domain_str_sp)
78         return false;
79 
80     StreamString domain_str_summary;
81     if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) && !domain_str_summary.Empty())
82     {
83         stream.Printf("domain: %s - code: %" PRIu64, domain_str_summary.GetData(), code);
84         return true;
85     }
86     else
87     {
88         stream.Printf("domain: nil - code: %" PRIu64, code);
89         return true;
90     }
91 }
92 
93 class NSErrorSyntheticFrontEnd : public SyntheticChildrenFrontEnd
94 {
95 public:
96     NSErrorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
97     SyntheticChildrenFrontEnd(*valobj_sp)
98     {}
99 
100     virtual size_t
101     CalculateNumChildren ()
102     {
103         if (m_child_ptr)
104             return 1;
105         if (m_child_sp)
106             return 1;
107         return 0;
108     }
109 
110     virtual lldb::ValueObjectSP
111     GetChildAtIndex (size_t idx)
112     {
113         if (idx != 0)
114             return lldb::ValueObjectSP();
115 
116         if (m_child_ptr)
117             return m_child_ptr->GetSP();
118         return m_child_sp;
119     }
120 
121     virtual bool
122     Update()
123     {
124         m_child_ptr = nullptr;
125         m_child_sp.reset();
126 
127         ProcessSP process_sp(m_backend.GetProcessSP());
128         if (!process_sp)
129             return false;
130 
131         lldb::addr_t userinfo_location = LLDB_INVALID_ADDRESS;
132 
133         CompilerType valobj_type(m_backend.GetCompilerType());
134         Flags type_flags(valobj_type.GetTypeInfo());
135         if (type_flags.AllClear(eTypeHasValue))
136         {
137             if (m_backend.IsBaseClass() && m_backend.GetParent())
138                 userinfo_location = m_backend.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
139         }
140         else
141             userinfo_location = m_backend.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
142 
143         if (userinfo_location == LLDB_INVALID_ADDRESS)
144             return false;
145 
146         size_t ptr_size = process_sp->GetAddressByteSize();
147 
148         userinfo_location += 4 * ptr_size;
149         Error error;
150         lldb::addr_t userinfo = process_sp->ReadPointerFromMemory(userinfo_location, error);
151         if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
152             return false;
153         InferiorSizedWord isw(userinfo,*process_sp);
154         m_child_sp = ValueObject::CreateValueObjectFromData("_userInfo",
155                                                             isw.GetAsData(process_sp->GetByteOrder()),
156                                                             m_backend.GetExecutionContextRef(),
157                                                             process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType(lldb::eBasicTypeObjCID));
158         return false;
159     }
160 
161     virtual bool
162     MightHaveChildren ()
163     {
164         return true;
165     }
166 
167     virtual size_t
168     GetIndexOfChildWithName (const ConstString &name)
169     {
170         static ConstString g___userInfo("_userInfo");
171         if (name == g___userInfo)
172             return 0;
173         return UINT32_MAX;
174     }
175 
176     virtual
177     ~NSErrorSyntheticFrontEnd ()
178     {
179         // no need to delete m_child_ptr - it's kept alive by the cluster manager on our behalf
180     }
181 
182 private:
183     // the child here can be "real" (i.e. an actual child of the root) or synthetized from raw memory
184     // if the former, I need to store a plain pointer to it - or else a loop of references will cause this entire hierarchy of values to leak
185     // if the latter, then I need to store a SharedPointer to it - so that it only goes away when everyone else in the cluster goes away
186     // oh joy!
187     ValueObject* m_child_ptr;
188     ValueObjectSP m_child_sp;
189 };
190 
191 SyntheticChildrenFrontEnd*
192 lldb_private::formatters::NSErrorSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
193 {
194     lldb::ProcessSP process_sp (valobj_sp->GetProcessSP());
195     if (!process_sp)
196         return nullptr;
197     ObjCLanguageRuntime *runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
198     if (!runtime)
199         return nullptr;
200 
201     ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(*valobj_sp.get()));
202 
203     if (!descriptor.get() || !descriptor->IsValid())
204         return nullptr;
205 
206     const char* class_name = descriptor->GetClassName().GetCString();
207 
208     if (!class_name || !*class_name)
209         return nullptr;
210 
211     if (!strcmp(class_name,"NSError"))
212         return (new NSErrorSyntheticFrontEnd(valobj_sp));
213     else if (!strcmp(class_name,"__NSCFError"))
214         return (new NSErrorSyntheticFrontEnd(valobj_sp));
215 
216     return nullptr;
217 }
218 
219