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 "Cocoa.h"
10
15#include "lldb/Target/Target.h"
17#include "lldb/Utility/Endian.h"
18#include "lldb/Utility/Status.h"
19#include "lldb/Utility/Stream.h"
22#include "llvm/Support/ErrorExtras.h"
23
24using namespace lldb;
25using namespace lldb_private;
26using namespace lldb_private::formatters;
27
29 CompilerType valobj_type(valobj.GetCompilerType());
30 Flags type_flags(valobj_type.GetTypeInfo());
31 if (type_flags.AllClear(eTypeHasValue)) {
32 if (valobj.IsBaseClass() && valobj.GetParent())
34 } else {
36 if (type_flags.AllSet(eTypeIsPointer)) {
37 CompilerType pointee_type(valobj_type.GetPointeeType());
38 Flags pointee_flags(pointee_type.GetTypeInfo());
39 if (pointee_flags.AllSet(eTypeIsPointer)) {
40 if (ProcessSP process_sp = valobj.GetProcessSP()) {
42 ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
43 }
44 }
45 }
46 return ptr_value;
47 }
48
50}
51
53 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
54 ProcessSP process_sp(valobj.GetProcessSP());
55 if (!process_sp)
56 return false;
57
58 lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj);
59 if (ptr_value == LLDB_INVALID_ADDRESS)
60 return false;
61
62 size_t ptr_size = process_sp->GetAddressByteSize();
63 lldb::addr_t code_location = ptr_value + 2 * ptr_size;
64 lldb::addr_t domain_location = ptr_value + 3 * ptr_size;
65
67 int64_t code = process_sp->ReadSignedIntegerFromMemory(code_location,
68 ptr_size, 0, error);
69 if (error.Fail())
70 return false;
71
72 lldb::addr_t domain_str_value =
73 process_sp->ReadPointerFromMemory(domain_location, error);
74 if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
75 return false;
76
77 if (!domain_str_value) {
78 stream.Printf("domain: nil - code: %" PRIi64, code);
79 return true;
80 }
81
82 InferiorSizedWord isw(domain_str_value, *process_sp);
83 TypeSystemClangSP scratch_ts_sp =
84 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
85
86 if (!scratch_ts_sp)
87 return false;
89 "domain_str", isw.GetAsData(process_sp->GetByteOrder()),
91 scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType());
92
93 if (!domain_str_sp)
94 return false;
95
96 StreamString domain_str_summary;
97 if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) &&
98 !domain_str_summary.Empty()) {
99 stream.Printf("domain: %s - code: %" PRIi64, domain_str_summary.GetData(),
100 code);
101 return true;
102 } else {
103 stream.Printf("domain: nil - code: %" PRIi64, code);
104 return true;
105 }
106}
107
109public:
112
113 ~NSErrorSyntheticFrontEnd() override = default;
114 // no need to delete m_child_ptr - it's kept alive by the cluster manager on
115 // our behalf
116
117 llvm::Expected<uint32_t> CalculateNumChildren() override {
118 if (m_child_ptr)
119 return 1;
120 if (m_child_sp)
121 return 1;
122 return 0;
123 }
124
125 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
126 if (idx != 0)
127 return lldb::ValueObjectSP();
128
129 if (m_child_ptr)
130 return m_child_ptr->GetSP();
131 return m_child_sp;
132 }
133
135 m_child_ptr = nullptr;
136 m_child_sp.reset();
137
138 ProcessSP process_sp(m_backend.GetProcessSP());
139 if (!process_sp)
141
142 lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
143 if (userinfo_location == LLDB_INVALID_ADDRESS)
145
146 size_t ptr_size = process_sp->GetAddressByteSize();
147
148 userinfo_location += 4 * ptr_size;
150 lldb::addr_t userinfo =
151 process_sp->ReadPointerFromMemory(userinfo_location, error);
152 if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
154 InferiorSizedWord isw(userinfo, *process_sp);
155 TypeSystemClangSP scratch_ts_sp =
156 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
157 if (!scratch_ts_sp)
160 "_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
161 m_backend.GetExecutionContextRef(),
162 scratch_ts_sp->GetBasicType(lldb::eBasicTypeObjCID));
164 }
165
166 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
167 static ConstString g_userInfo("_userInfo");
168 if (name == g_userInfo)
169 return 0;
170 return llvm::createStringErrorV("type has no child named '{0}'", name);
171 }
172
173private:
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!
182};
183
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
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}
static llvm::raw_ostream & error(Stream &strm)
static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj)
Definition NSError.cpp:28
llvm::Expected< uint32_t > CalculateNumChildren() override
Definition NSError.cpp:117
ValueObjectSP m_child_sp
Definition NSError.cpp:181
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
Definition NSError.cpp:134
ValueObject * m_child_ptr
Definition NSError.cpp:180
~NSErrorSyntheticFrontEnd() override=default
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
Definition NSError.cpp:125
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
Definition NSError.cpp:166
NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition NSError.cpp:110
Generic representation of a type in a programming language.
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:118
const char * GetData() const
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:132
lldb::ValueObjectSP CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ProcessSP GetProcessSP() const
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()
CompilerType GetCompilerType()
virtual ValueObject * GetParent()
const ExecutionContextRef & GetExecutionContextRef() const
#define LLDB_INVALID_ADDRESS
bool NSError_SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSError.cpp:52
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:33
SyntheticChildrenFrontEnd * NSErrorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
A class that represents a running process on the host machine.
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
uint64_t addr_t
Definition lldb-types.h:80
DataExtractor GetAsData(lldb::ByteOrder byte_order=lldb::eByteOrderInvalid) const