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
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
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace lldb_private::formatters;
29
31 CompilerType valobj_type(valobj.GetCompilerType());
32 Flags type_flags(valobj_type.GetTypeInfo());
33 if (type_flags.AllClear(eTypeHasValue)) {
34 if (valobj.IsBaseClass() && valobj.GetParent())
36 } else {
38 if (type_flags.AllSet(eTypeIsPointer)) {
39 CompilerType pointee_type(valobj_type.GetPointeeType());
40 Flags pointee_flags(pointee_type.GetTypeInfo());
41 if (pointee_flags.AllSet(eTypeIsPointer)) {
42 if (ProcessSP process_sp = valobj.GetProcessSP()) {
44 ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
45 }
46 }
47 }
48 return ptr_value;
49 }
50
52}
53
55 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
56 ProcessSP process_sp(valobj.GetProcessSP());
57 if (!process_sp)
58 return false;
59
60 lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj);
61 if (ptr_value == LLDB_INVALID_ADDRESS)
62 return false;
63
64 size_t ptr_size = process_sp->GetAddressByteSize();
65 lldb::addr_t code_location = ptr_value + 2 * ptr_size;
66 lldb::addr_t domain_location = ptr_value + 3 * ptr_size;
67
69 int64_t code = process_sp->ReadSignedIntegerFromMemory(code_location,
70 ptr_size, 0, error);
71 if (error.Fail())
72 return false;
73
74 lldb::addr_t domain_str_value =
75 process_sp->ReadPointerFromMemory(domain_location, error);
76 if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
77 return false;
78
79 if (!domain_str_value) {
80 stream.Printf("domain: nil - code: %" PRIi64, code);
81 return true;
82 }
83
84 InferiorSizedWord isw(domain_str_value, *process_sp);
85 TypeSystemClangSP scratch_ts_sp =
86 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
87
88 if (!scratch_ts_sp)
89 return false;
91 "domain_str", isw.GetAsData(process_sp->GetByteOrder()),
93 scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType());
94
95 if (!domain_str_sp)
96 return false;
97
98 StreamString domain_str_summary;
99 if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) &&
100 !domain_str_summary.Empty()) {
101 stream.Printf("domain: %s - code: %" PRIi64, domain_str_summary.GetData(),
102 code);
103 return true;
104 } else {
105 stream.Printf("domain: nil - code: %" PRIi64, code);
106 return true;
107 }
108}
109
111public:
114
115 ~NSErrorSyntheticFrontEnd() override = default;
116 // no need to delete m_child_ptr - it's kept alive by the cluster manager on
117 // our behalf
118
119 llvm::Expected<uint32_t> CalculateNumChildren() override {
120 if (m_child_ptr)
121 return 1;
122 if (m_child_sp)
123 return 1;
124 return 0;
125 }
126
127 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
128 if (idx != 0)
129 return lldb::ValueObjectSP();
130
131 if (m_child_ptr)
132 return m_child_ptr->GetSP();
133 return m_child_sp;
134 }
135
137 m_child_ptr = nullptr;
138 m_child_sp.reset();
139
140 ProcessSP process_sp(m_backend.GetProcessSP());
141 if (!process_sp)
143
144 lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
145 if (userinfo_location == LLDB_INVALID_ADDRESS)
147
148 size_t ptr_size = process_sp->GetAddressByteSize();
149
150 userinfo_location += 4 * ptr_size;
152 lldb::addr_t userinfo =
153 process_sp->ReadPointerFromMemory(userinfo_location, error);
154 if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
156 InferiorSizedWord isw(userinfo, *process_sp);
157 TypeSystemClangSP scratch_ts_sp =
158 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
159 if (!scratch_ts_sp)
162 "_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
163 m_backend.GetExecutionContextRef(),
164 scratch_ts_sp->GetBasicType(lldb::eBasicTypeObjCID));
166 }
167
168 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
169 static ConstString g_userInfo("_userInfo");
170 if (name == g_userInfo)
171 return 0;
172 return llvm::createStringError("Type has no child named '%s'",
173 name.AsCString());
174 }
175
176private:
177 // the child here can be "real" (i.e. an actual child of the root) or
178 // synthetized from raw memory if the former, I need to store a plain pointer
179 // to it - or else a loop of references will cause this entire hierarchy of
180 // values to leak if the latter, then I need to store a SharedPointer to it -
181 // so that it only goes away when everyone else in the cluster goes away oh
182 // joy!
185};
186
190 lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
191 if (!process_sp)
192 return nullptr;
193 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
194 if (!runtime)
195 return nullptr;
196
198 runtime->GetClassDescriptor(*valobj_sp.get()));
199
200 if (!descriptor.get() || !descriptor->IsValid())
201 return nullptr;
202
203 const char *class_name = descriptor->GetClassName().GetCString();
204
205 if (!class_name || !*class_name)
206 return nullptr;
207
208 if (!strcmp(class_name, "NSError"))
209 return (new NSErrorSyntheticFrontEnd(valobj_sp));
210 else if (!strcmp(class_name, "__NSCFError"))
211 return (new NSErrorSyntheticFrontEnd(valobj_sp));
212
213 return nullptr;
214}
static llvm::raw_ostream & error(Stream &strm)
static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj)
Definition NSError.cpp:30
llvm::Expected< uint32_t > CalculateNumChildren() override
Definition NSError.cpp:119
ValueObjectSP m_child_sp
Definition NSError.cpp:184
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:136
ValueObject * m_child_ptr
Definition NSError.cpp:183
~NSErrorSyntheticFrontEnd() override=default
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
Definition NSError.cpp:127
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Definition NSError.cpp:168
NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition NSError.cpp:112
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
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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:134
lldb::ValueObjectSP CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
SyntheticChildrenFrontEnd(ValueObject &backend)
CompilerType GetCompilerType()
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()
virtual ValueObject * GetParent()
const ExecutionContextRef & GetExecutionContextRef() const
#define LLDB_INVALID_ADDRESS
bool NSError_SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSError.cpp:54
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:33
SyntheticChildrenFrontEnd * NSErrorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
Definition NSError.cpp:188
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