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