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