/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/ObjC/NSException.cpp
Line | Count | Source (jump to first uncovered line) |
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 "lldb/Core/ValueObject.h" |
14 | | #include "lldb/Core/ValueObjectConstResult.h" |
15 | | #include "lldb/DataFormatters/FormattersHelpers.h" |
16 | | #include "lldb/Target/Target.h" |
17 | | #include "lldb/Utility/DataBufferHeap.h" |
18 | | #include "lldb/Utility/Endian.h" |
19 | | #include "lldb/Utility/Status.h" |
20 | | #include "lldb/Utility/Stream.h" |
21 | | |
22 | | #include "Plugins/Language/ObjC/NSString.h" |
23 | | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
24 | | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
25 | | |
26 | | using namespace lldb; |
27 | | using namespace lldb_private; |
28 | | using namespace lldb_private::formatters; |
29 | | |
30 | | static bool ExtractFields(ValueObject &valobj, ValueObjectSP *name_sp, |
31 | | ValueObjectSP *reason_sp, ValueObjectSP *userinfo_sp, |
32 | 108 | ValueObjectSP *reserved_sp) { |
33 | 108 | ProcessSP process_sp(valobj.GetProcessSP()); |
34 | 108 | if (!process_sp) |
35 | 0 | return false; |
36 | | |
37 | 108 | lldb::addr_t ptr = LLDB_INVALID_ADDRESS; |
38 | | |
39 | 108 | CompilerType valobj_type(valobj.GetCompilerType()); |
40 | 108 | Flags type_flags(valobj_type.GetTypeInfo()); |
41 | 108 | if (type_flags.AllClear(eTypeHasValue)) { |
42 | 10 | if (valobj.IsBaseClass() && valobj.GetParent()2 ) |
43 | 2 | ptr = valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
44 | 98 | } else { |
45 | 98 | ptr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
46 | 98 | } |
47 | | |
48 | 108 | if (ptr == LLDB_INVALID_ADDRESS) |
49 | 10 | return false; |
50 | 98 | size_t ptr_size = process_sp->GetAddressByteSize(); |
51 | | |
52 | 98 | Status error; |
53 | 98 | auto name = process_sp->ReadPointerFromMemory(ptr + 1 * ptr_size, error); |
54 | 98 | if (error.Fail() || name == LLDB_INVALID_ADDRESS) |
55 | 0 | return false; |
56 | 98 | auto reason = process_sp->ReadPointerFromMemory(ptr + 2 * ptr_size, error); |
57 | 98 | if (error.Fail() || reason == LLDB_INVALID_ADDRESS) |
58 | 0 | return false; |
59 | 98 | auto userinfo = process_sp->ReadPointerFromMemory(ptr + 3 * ptr_size, error); |
60 | 98 | if (error.Fail() || userinfo == LLDB_INVALID_ADDRESS) |
61 | 0 | return false; |
62 | 98 | auto reserved = process_sp->ReadPointerFromMemory(ptr + 4 * ptr_size, error); |
63 | 98 | if (error.Fail() || reserved == LLDB_INVALID_ADDRESS) |
64 | 0 | return false; |
65 | | |
66 | 98 | InferiorSizedWord name_isw(name, *process_sp); |
67 | 98 | InferiorSizedWord reason_isw(reason, *process_sp); |
68 | 98 | InferiorSizedWord userinfo_isw(userinfo, *process_sp); |
69 | 98 | InferiorSizedWord reserved_isw(reserved, *process_sp); |
70 | | |
71 | 98 | TypeSystemClangSP scratch_ts_sp = |
72 | 98 | ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget()); |
73 | 98 | if (!scratch_ts_sp) |
74 | 0 | return false; |
75 | | |
76 | 98 | CompilerType voidstar = |
77 | 98 | scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType(); |
78 | | |
79 | 98 | if (name_sp) |
80 | 53 | *name_sp = ValueObject::CreateValueObjectFromData( |
81 | 53 | "name", name_isw.GetAsData(process_sp->GetByteOrder()), |
82 | 53 | valobj.GetExecutionContextRef(), voidstar); |
83 | 98 | if (reason_sp) |
84 | 98 | *reason_sp = ValueObject::CreateValueObjectFromData( |
85 | 98 | "reason", reason_isw.GetAsData(process_sp->GetByteOrder()), |
86 | 98 | valobj.GetExecutionContextRef(), voidstar); |
87 | 98 | if (userinfo_sp) |
88 | 53 | *userinfo_sp = ValueObject::CreateValueObjectFromData( |
89 | 53 | "userInfo", userinfo_isw.GetAsData(process_sp->GetByteOrder()), |
90 | 53 | valobj.GetExecutionContextRef(), voidstar); |
91 | 98 | if (reserved_sp) |
92 | 53 | *reserved_sp = ValueObject::CreateValueObjectFromData( |
93 | 53 | "reserved", reserved_isw.GetAsData(process_sp->GetByteOrder()), |
94 | 53 | valobj.GetExecutionContextRef(), voidstar); |
95 | | |
96 | 98 | return true; |
97 | 98 | } |
98 | | |
99 | | bool lldb_private::formatters::NSException_SummaryProvider( |
100 | 55 | ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { |
101 | 55 | lldb::ValueObjectSP reason_sp; |
102 | 55 | if (!ExtractFields(valobj, nullptr, &reason_sp, nullptr, nullptr)) |
103 | 10 | return false; |
104 | | |
105 | 45 | if (!reason_sp) { |
106 | 0 | stream.Printf("No reason"); |
107 | 0 | return false; |
108 | 0 | } |
109 | | |
110 | 45 | StreamString reason_str_summary; |
111 | 45 | if (NSStringSummaryProvider(*reason_sp, reason_str_summary, options) && |
112 | 45 | !reason_str_summary.Empty()) { |
113 | 45 | stream.Printf("%s", reason_str_summary.GetData()); |
114 | 45 | return true; |
115 | 45 | } else |
116 | 0 | return false; |
117 | 45 | } |
118 | | |
119 | | class NSExceptionSyntheticFrontEnd : public SyntheticChildrenFrontEnd { |
120 | | public: |
121 | | NSExceptionSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) |
122 | 78 | : SyntheticChildrenFrontEnd(*valobj_sp) {} |
123 | | |
124 | 78 | ~NSExceptionSyntheticFrontEnd() override = default; |
125 | | |
126 | 0 | size_t CalculateNumChildren() override { |
127 | 0 | return 4; |
128 | 0 | } |
129 | | |
130 | 14 | lldb::ValueObjectSP GetChildAtIndex(size_t idx) override { |
131 | 14 | switch (idx) { |
132 | 4 | case 0: return m_name_sp; |
133 | 4 | case 1: return m_reason_sp; |
134 | 4 | case 2: return m_userinfo_sp; |
135 | 2 | case 3: return m_reserved_sp; |
136 | 14 | } |
137 | 0 | return lldb::ValueObjectSP(); |
138 | 14 | } |
139 | | |
140 | 53 | bool Update() override { |
141 | 53 | m_name_sp.reset(); |
142 | 53 | m_reason_sp.reset(); |
143 | 53 | m_userinfo_sp.reset(); |
144 | 53 | m_reserved_sp.reset(); |
145 | | |
146 | 53 | return ExtractFields(m_backend, &m_name_sp, &m_reason_sp, &m_userinfo_sp, |
147 | 53 | &m_reserved_sp); |
148 | 53 | } |
149 | | |
150 | 3 | bool MightHaveChildren() override { return true; } |
151 | | |
152 | 14 | size_t GetIndexOfChildWithName(ConstString name) override { |
153 | | // NSException has 4 members: |
154 | | // NSString *name; |
155 | | // NSString *reason; |
156 | | // NSDictionary *userInfo; |
157 | | // id reserved; |
158 | 14 | static ConstString g_name("name"); |
159 | 14 | static ConstString g_reason("reason"); |
160 | 14 | static ConstString g_userInfo("userInfo"); |
161 | 14 | static ConstString g_reserved("reserved"); |
162 | 14 | if (name == g_name) return 04 ; |
163 | 10 | if (name == g_reason) return 14 ; |
164 | 6 | if (name == g_userInfo) return 24 ; |
165 | 2 | if (name == g_reserved) return 3; |
166 | 0 | return UINT32_MAX; |
167 | 2 | } |
168 | | |
169 | | private: |
170 | | ValueObjectSP m_name_sp; |
171 | | ValueObjectSP m_reason_sp; |
172 | | ValueObjectSP m_userinfo_sp; |
173 | | ValueObjectSP m_reserved_sp; |
174 | | }; |
175 | | |
176 | | SyntheticChildrenFrontEnd * |
177 | | lldb_private::formatters::NSExceptionSyntheticFrontEndCreator( |
178 | 84 | CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { |
179 | 84 | lldb::ProcessSP process_sp(valobj_sp->GetProcessSP()); |
180 | 84 | if (!process_sp) |
181 | 0 | return nullptr; |
182 | 84 | ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp); |
183 | 84 | if (!runtime) |
184 | 0 | return nullptr; |
185 | | |
186 | 84 | ObjCLanguageRuntime::ClassDescriptorSP descriptor( |
187 | 84 | runtime->GetClassDescriptor(*valobj_sp.get())); |
188 | | |
189 | 84 | if (!descriptor.get() || !descriptor->IsValid()78 ) |
190 | 6 | return nullptr; |
191 | | |
192 | 78 | const char *class_name = descriptor->GetClassName().GetCString(); |
193 | | |
194 | 78 | if (!class_name || !*class_name) |
195 | 0 | return nullptr; |
196 | | |
197 | 78 | if (!strcmp(class_name, "NSException")) |
198 | 78 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
199 | 0 | else if (!strcmp(class_name, "NSCFException")) |
200 | 0 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
201 | 0 | else if (!strcmp(class_name, "__NSCFException")) |
202 | 0 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
203 | | |
204 | 0 | return nullptr; |
205 | 78 | } |