]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Language/ObjC/NSError.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Language / ObjC / NSError.cpp
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/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"
29
30 #include "Plugins/Language/ObjC/NSString.h"
31
32 using namespace lldb;
33 using namespace lldb_private;
34 using namespace lldb_private::formatters;
35
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);
42   } else {
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()) {
49           Status error;
50           ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
51         }
52       }
53     }
54     return ptr_value;
55   }
56
57   return LLDB_INVALID_ADDRESS;
58 }
59
60 bool lldb_private::formatters::NSError_SummaryProvider(
61     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
62   ProcessSP process_sp(valobj.GetProcessSP());
63   if (!process_sp)
64     return false;
65
66   lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj);
67   if (ptr_value == LLDB_INVALID_ADDRESS)
68     return false;
69
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;
73
74   Status error;
75   uint64_t code = process_sp->ReadUnsignedIntegerFromMemory(code_location,
76                                                             ptr_size, 0, error);
77   if (error.Fail())
78     return false;
79
80   lldb::addr_t domain_str_value =
81       process_sp->ReadPointerFromMemory(domain_location, error);
82   if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
83     return false;
84
85   if (!domain_str_value) {
86     stream.Printf("domain: nil - code: %" PRIu64, code);
87     return true;
88   }
89
90   InferiorSizedWord isw(domain_str_value, *process_sp);
91
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)
97                                            .GetPointerType());
98
99   if (!domain_str_sp)
100     return false;
101
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(),
106                   code);
107     return true;
108   } else {
109     stream.Printf("domain: nil - code: %" PRIu64, code);
110     return true;
111   }
112 }
113
114 class NSErrorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
115 public:
116   NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
117       : SyntheticChildrenFrontEnd(*valobj_sp) {}
118
119   ~NSErrorSyntheticFrontEnd() override = default;
120   // no need to delete m_child_ptr - it's kept alive by the cluster manager on
121   // our behalf
122
123   size_t CalculateNumChildren() override {
124     if (m_child_ptr)
125       return 1;
126     if (m_child_sp)
127       return 1;
128     return 0;
129   }
130
131   lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
132     if (idx != 0)
133       return lldb::ValueObjectSP();
134
135     if (m_child_ptr)
136       return m_child_ptr->GetSP();
137     return m_child_sp;
138   }
139
140   bool Update() override {
141     m_child_ptr = nullptr;
142     m_child_sp.reset();
143
144     ProcessSP process_sp(m_backend.GetProcessSP());
145     if (!process_sp)
146       return false;
147
148     lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
149     if (userinfo_location == LLDB_INVALID_ADDRESS)
150       return false;
151
152     size_t ptr_size = process_sp->GetAddressByteSize();
153
154     userinfo_location += 4 * ptr_size;
155     Status error;
156     lldb::addr_t userinfo =
157         process_sp->ReadPointerFromMemory(userinfo_location, error);
158     if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
159       return false;
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));
166     return false;
167   }
168
169   bool MightHaveChildren() override { return true; }
170
171   size_t GetIndexOfChildWithName(const ConstString &name) override {
172     static ConstString g___userInfo("_userInfo");
173     if (name == g___userInfo)
174       return 0;
175     return UINT32_MAX;
176   }
177
178 private:
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
184   // joy!
185   ValueObject *m_child_ptr;
186   ValueObjectSP m_child_sp;
187 };
188
189 SyntheticChildrenFrontEnd *
190 lldb_private::formatters::NSErrorSyntheticFrontEndCreator(
191     CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
192   lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
193   if (!process_sp)
194     return nullptr;
195   ObjCLanguageRuntime *runtime =
196       (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
197           lldb::eLanguageTypeObjC);
198   if (!runtime)
199     return nullptr;
200
201   ObjCLanguageRuntime::ClassDescriptorSP descriptor(
202       runtime->GetClassDescriptor(*valobj_sp.get()));
203
204   if (!descriptor.get() || !descriptor->IsValid())
205     return nullptr;
206
207   const char *class_name = descriptor->GetClassName().GetCString();
208
209   if (!class_name || !*class_name)
210     return nullptr;
211
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));
216
217   return nullptr;
218 }