LLDB mainline
NSError.cpp
Go to the documentation of this file.
1//===-- NSError.cpp -------------------------------------------------------===//
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
18#include "lldb/Target/Target.h"
20#include "lldb/Utility/Endian.h"
21#include "lldb/Utility/Status.h"
22#include "lldb/Utility/Stream.h"
23
26
27using namespace lldb;
28using namespace lldb_private;
29using namespace lldb_private::formatters;
30
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())
37 } else {
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()) {
45 ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
46 }
47 }
48 }
49 return ptr_value;
50 }
51
53}
54
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
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 TypeSystemClangSP scratch_ts_sp =
87 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
88
89 if (!scratch_ts_sp)
90 return false;
91 ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData(
92 "domain_str", isw.GetAsData(process_sp->GetByteOrder()),
94 scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType());
95
96 if (!domain_str_sp)
97 return false;
98
99 StreamString domain_str_summary;
100 if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) &&
101 !domain_str_summary.Empty()) {
102 stream.Printf("domain: %s - code: %" PRIu64, domain_str_summary.GetData(),
103 code);
104 return true;
105 } else {
106 stream.Printf("domain: nil - code: %" PRIu64, code);
107 return true;
108 }
109}
110
112public:
113 NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
114 : SyntheticChildrenFrontEnd(*valobj_sp) {}
115
116 ~NSErrorSyntheticFrontEnd() override = default;
117 // no need to delete m_child_ptr - it's kept alive by the cluster manager on
118 // our behalf
119
120 size_t CalculateNumChildren() override {
121 if (m_child_ptr)
122 return 1;
123 if (m_child_sp)
124 return 1;
125 return 0;
126 }
127
128 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
129 if (idx != 0)
130 return lldb::ValueObjectSP();
131
132 if (m_child_ptr)
133 return m_child_ptr->GetSP();
134 return m_child_sp;
135 }
136
137 bool Update() override {
138 m_child_ptr = nullptr;
139 m_child_sp.reset();
140
141 ProcessSP process_sp(m_backend.GetProcessSP());
142 if (!process_sp)
143 return false;
144
145 lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
146 if (userinfo_location == LLDB_INVALID_ADDRESS)
147 return false;
148
149 size_t ptr_size = process_sp->GetAddressByteSize();
150
151 userinfo_location += 4 * ptr_size;
153 lldb::addr_t userinfo =
154 process_sp->ReadPointerFromMemory(userinfo_location, error);
155 if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
156 return false;
157 InferiorSizedWord isw(userinfo, *process_sp);
158 TypeSystemClangSP scratch_ts_sp =
159 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
160 if (!scratch_ts_sp)
161 return false;
163 "_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
165 scratch_ts_sp->GetBasicType(lldb::eBasicTypeObjCID));
166 return false;
167 }
168
169 bool MightHaveChildren() override { return true; }
170
171 size_t GetIndexOfChildWithName(ConstString name) override {
172 static ConstString g_userInfo("_userInfo");
173 if (name == g_userInfo)
174 return 0;
175 return UINT32_MAX;
176 }
177
178private:
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!
186 ValueObjectSP m_child_sp;
187};
188
191 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
192 lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
193 if (!process_sp)
194 return nullptr;
195 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
196 if (!runtime)
197 return nullptr;
198
200 runtime->GetClassDescriptor(*valobj_sp.get()));
201
202 if (!descriptor.get() || !descriptor->IsValid())
203 return nullptr;
204
205 const char *class_name = descriptor->GetClassName().GetCString();
206
207 if (!class_name || !*class_name)
208 return nullptr;
209
210 if (!strcmp(class_name, "NSError"))
211 return (new NSErrorSyntheticFrontEnd(valobj_sp));
212 else if (!strcmp(class_name, "__NSCFError"))
213 return (new NSErrorSyntheticFrontEnd(valobj_sp));
214
215 return nullptr;
216}
static llvm::raw_ostream & error(Stream &strm)
static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj)
Definition: NSError.cpp:31
size_t CalculateNumChildren() override
Definition: NSError.cpp:120
ValueObjectSP m_child_sp
Definition: NSError.cpp:186
ValueObject * m_child_ptr
Definition: NSError.cpp:185
bool MightHaveChildren() override
Definition: NSError.cpp:169
~NSErrorSyntheticFrontEnd() override=default
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: NSError.cpp:128
size_t GetIndexOfChildWithName(ConstString name) override
Definition: NSError.cpp:171
NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: NSError.cpp:113
bool Update() override
Definition: NSError.cpp:137
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
A class to manage flags.
Definition: Flags.h:22
bool AllClear(ValueType mask) const
Test if all bits in mask are clear.
Definition: Flags.h:103
bool AllSet(ValueType mask) const
Test if all bits in mask are 1 in the current flags.
Definition: Flags.h:83
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
static ObjCLanguageRuntime * Get(Process &process)
virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value)
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
An error handling class.
Definition: Status.h:44
const char * GetData() const
Definition: StreamString.h:43
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb::ValueObjectSP CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:554
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
static lldb::ValueObjectSP CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
virtual bool IsBaseClass()
Definition: ValueObject.h:398
virtual ValueObject * GetParent()
Definition: ValueObject.h:752
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
#define UINT32_MAX
Definition: lldb-defines.h:19
bool NSError_SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: NSError.cpp:55
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: NSString.cpp:33
SyntheticChildrenFrontEnd * NSErrorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
Definition: NSError.cpp:190
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
@ eBasicTypeObjCID
uint64_t addr_t
Definition: lldb-types.h:79
DataExtractor GetAsData(lldb::ByteOrder byte_order=lldb::eByteOrderInvalid) const