LLDB mainline
NSException.cpp
Go to the documentation of this file.
1//===-- NSException.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
13#include "llvm/Support/ErrorExtras.h"
14
16#include "lldb/Target/Target.h"
18#include "lldb/Utility/Endian.h"
19#include "lldb/Utility/Status.h"
20#include "lldb/Utility/Stream.h"
23
27
28using namespace lldb;
29using namespace lldb_private;
30using namespace lldb_private::formatters;
31
32static bool ExtractFields(ValueObject &valobj, ValueObjectSP *name_sp,
33 ValueObjectSP *reason_sp, ValueObjectSP *userinfo_sp,
34 ValueObjectSP *reserved_sp) {
35 ProcessSP process_sp(valobj.GetProcessSP());
36 if (!process_sp)
37 return false;
38
40
41 CompilerType valobj_type(valobj.GetCompilerType());
42 Flags type_flags(valobj_type.GetTypeInfo());
43 if (type_flags.AllClear(eTypeHasValue)) {
44 if (valobj.IsBaseClass() && valobj.GetParent())
46 } else {
48 }
49
50 if (ptr == LLDB_INVALID_ADDRESS)
51 return false;
52 size_t ptr_size = process_sp->GetAddressByteSize();
53
55 auto name = process_sp->ReadPointerFromMemory(ptr + 1 * ptr_size, error);
56 if (error.Fail() || name == LLDB_INVALID_ADDRESS)
57 return false;
58 auto reason = process_sp->ReadPointerFromMemory(ptr + 2 * ptr_size, error);
59 if (error.Fail() || reason == LLDB_INVALID_ADDRESS)
60 return false;
61 auto userinfo = process_sp->ReadPointerFromMemory(ptr + 3 * ptr_size, error);
62 if (error.Fail() || userinfo == LLDB_INVALID_ADDRESS)
63 return false;
64 auto reserved = process_sp->ReadPointerFromMemory(ptr + 4 * ptr_size, error);
65 if (error.Fail() || reserved == LLDB_INVALID_ADDRESS)
66 return false;
67
68 InferiorSizedWord name_isw(name, *process_sp);
69 InferiorSizedWord reason_isw(reason, *process_sp);
70 InferiorSizedWord userinfo_isw(userinfo, *process_sp);
71 InferiorSizedWord reserved_isw(reserved, *process_sp);
72
73 TypeSystemClangSP scratch_ts_sp =
74 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
75 if (!scratch_ts_sp)
76 return false;
77
78 CompilerType voidstar =
79 scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
80
81 if (name_sp)
83 "name", name_isw.GetAsData(process_sp->GetByteOrder()),
84 valobj.GetExecutionContextRef(), voidstar);
85 if (reason_sp)
87 "reason", reason_isw.GetAsData(process_sp->GetByteOrder()),
88 valobj.GetExecutionContextRef(), voidstar);
89 if (userinfo_sp)
91 "userInfo", userinfo_isw.GetAsData(process_sp->GetByteOrder()),
92 valobj.GetExecutionContextRef(), voidstar);
93 if (reserved_sp)
95 "reserved", reserved_isw.GetAsData(process_sp->GetByteOrder()),
96 valobj.GetExecutionContextRef(), voidstar);
97
98 return true;
99}
100
102 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
103 lldb::ValueObjectSP reason_sp;
104 if (!ExtractFields(valobj, nullptr, &reason_sp, nullptr, nullptr))
105 return false;
106
107 if (!reason_sp) {
108 stream.Printf("No reason");
109 return false;
110 }
111
112 StreamString reason_str_summary;
113 if (NSStringSummaryProvider(*reason_sp, reason_str_summary, options) &&
114 !reason_str_summary.Empty()) {
115 stream.Printf("%s", reason_str_summary.GetData());
116 return true;
117 } else
118 return false;
119}
120
122public:
125
126 ~NSExceptionSyntheticFrontEnd() override = default;
127
128 llvm::Expected<uint32_t> CalculateNumChildren() override { return 4; }
129
130 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
131 switch (idx) {
132 case 0: return m_name_sp;
133 case 1: return m_reason_sp;
134 case 2: return m_userinfo_sp;
135 case 3: return m_reserved_sp;
136 }
137 return lldb::ValueObjectSP();
138 }
139
141 m_name_sp.reset();
142 m_reason_sp.reset();
143 m_userinfo_sp.reset();
144 m_reserved_sp.reset();
145
146 const auto ret = ExtractFields(m_backend, &m_name_sp, &m_reason_sp,
148
151 }
152
153 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
154 // NSException has 4 members:
155 // NSString *name;
156 // NSString *reason;
157 // NSDictionary *userInfo;
158 // id reserved;
159 static ConstString g_name("name");
160 static ConstString g_reason("reason");
161 static ConstString g_userInfo("userInfo");
162 static ConstString g_reserved("reserved");
163 if (name == g_name) return 0;
164 if (name == g_reason) return 1;
165 if (name == g_userInfo) return 2;
166 if (name == g_reserved) return 3;
167 return llvm::createStringErrorV("type has no child named '{0}'", name);
168 }
169
170private:
175};
176
180 lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
181 if (!process_sp)
182 return nullptr;
183 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
184 if (!runtime)
185 return nullptr;
186
188 runtime->GetClassDescriptor(*valobj_sp.get()));
189
190 if (!descriptor.get() || !descriptor->IsValid())
191 return nullptr;
192
193 const char *class_name = descriptor->GetClassName().GetCString();
194
195 if (!class_name || !*class_name)
196 return nullptr;
197
198 if (!strcmp(class_name, "NSException"))
199 return (new NSExceptionSyntheticFrontEnd(valobj_sp));
200 else if (!strcmp(class_name, "NSCFException"))
201 return (new NSExceptionSyntheticFrontEnd(valobj_sp));
202 else if (!strcmp(class_name, "__NSCFException"))
203 return (new NSExceptionSyntheticFrontEnd(valobj_sp));
204
205 return nullptr;
206}
static llvm::raw_ostream & error(Stream &strm)
static bool ExtractFields(ValueObject &valobj, ValueObjectSP *name_sp, ValueObjectSP *reason_sp, ValueObjectSP *userinfo_sp, ValueObjectSP *reserved_sp)
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< uint32_t > CalculateNumChildren() override
~NSExceptionSyntheticFrontEnd() override=default
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
NSExceptionSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
Generic representation of a type in a programming language.
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
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
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 NSException_SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
SyntheticChildrenFrontEnd * NSExceptionSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:33
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.
@ eReuse
Children did not change and don't need to be recomputed; re-use what we computed the last time we cal...
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