blob: 1fc8744407a77c01f783fe2dc934fbd394ddcb00 [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===//
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#ifndef liblldb_ObjCLanguageRuntime_h_
10#define liblldb_ObjCLanguageRuntime_h_
11
12#include <functional>
13#include <map>
14#include <memory>
15#include <unordered_set>
16
17#include "llvm/Support/Casting.h"
18
19#include "lldb/Breakpoint/BreakpointPrecondition.h"
20#include "lldb/Core/PluginInterface.h"
21#include "lldb/Core/ThreadSafeDenseMap.h"
22#include "lldb/Symbol/CompilerType.h"
23#include "lldb/Symbol/Type.h"
24#include "lldb/Target/LanguageRuntime.h"
25#include "lldb/lldb-private.h"
26
27class CommandObjectObjC_ClassTable_Dump;
28
29namespace lldb_private {
30
31class UtilityFunction;
32
33class ObjCLanguageRuntime : public LanguageRuntime {
34public:
35 enum class ObjCRuntimeVersions {
36 eObjC_VersionUnknown = 0,
37 eAppleObjC_V1 = 1,
38 eAppleObjC_V2 = 2
39 };
40
41 typedef lldb::addr_t ObjCISA;
42
43 class ClassDescriptor;
44 typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
45
46 // the information that we want to support retrieving from an ObjC class this
47 // needs to be pure virtual since there are at least 2 different
48 // implementations of the runtime, and more might come
49 class ClassDescriptor {
50 public:
51 ClassDescriptor()
52 : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate),
53 m_type_wp() {}
54
55 virtual ~ClassDescriptor() = default;
56
57 virtual ConstString GetClassName() = 0;
58
59 virtual ClassDescriptorSP GetSuperclass() = 0;
60
61 virtual ClassDescriptorSP GetMetaclass() const = 0;
62
63 // virtual if any implementation has some other version-specific rules but
64 // for the known v1/v2 this is all that needs to be done
65 virtual bool IsKVO() {
66 if (m_is_kvo == eLazyBoolCalculate) {
67 const char *class_name = GetClassName().AsCString();
68 if (class_name && *class_name)
69 m_is_kvo =
70 (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
71 }
72 return (m_is_kvo == eLazyBoolYes);
73 }
74
75 // virtual if any implementation has some other version-specific rules but
76 // for the known v1/v2 this is all that needs to be done
77 virtual bool IsCFType() {
78 if (m_is_cf == eLazyBoolCalculate) {
79 const char *class_name = GetClassName().AsCString();
80 if (class_name && *class_name)
81 m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
82 strcmp(class_name, "NSCFType") == 0);
83 }
84 return (m_is_cf == eLazyBoolYes);
85 }
86
87 virtual bool IsValid() = 0;
88
89 virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
90 uint64_t *value_bits = nullptr,
91 uint64_t *payload = nullptr) = 0;
92
93 virtual uint64_t GetInstanceSize() = 0;
94
95 // use to implement version-specific additional constraints on pointers
96 virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
97 return true;
98 }
99
100 virtual ObjCISA GetISA() = 0;
101
102 // This should return true iff the interface could be completed
103 virtual bool
104 Describe(std::function<void(ObjCISA)> const &superclass_func,
105 std::function<bool(const char *, const char *)> const
106 &instance_method_func,
107 std::function<bool(const char *, const char *)> const
108 &class_method_func,
109 std::function<bool(const char *, const char *, lldb::addr_t,
110 uint64_t)> const &ivar_func) const {
111 return false;
112 }
113
114 lldb::TypeSP GetType() { return m_type_wp.lock(); }
115
116 void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
117
118 struct iVarDescriptor {
119 ConstString m_name;
120 CompilerType m_type;
121 uint64_t m_size;
122 int32_t m_offset;
123 };
124
125 virtual size_t GetNumIVars() { return 0; }
126
127 virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
128 return iVarDescriptor();
129 }
130
131 protected:
132 bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
133 bool allow_NULLs = false, bool allow_tagged = false,
134 bool check_version_specific = false) const;
135
136 private:
137 LazyBool m_is_kvo;
138 LazyBool m_is_cf;
139 lldb::TypeWP m_type_wp;
140 };
141
142 class EncodingToType {
143 public:
144 virtual ~EncodingToType();
145
146 virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name,
147 bool for_expression);
148 virtual CompilerType RealizeType(const char *name, bool for_expression);
149
150 virtual CompilerType RealizeType(clang::ASTContext &ast_ctx,
151 const char *name, bool for_expression) = 0;
152
153 protected:
154 std::unique_ptr<ClangASTContext> m_scratch_ast_ctx_up;
155 };
156
157 class ObjCExceptionPrecondition : public BreakpointPrecondition {
158 public:
159 ObjCExceptionPrecondition();
160
161 ~ObjCExceptionPrecondition() override = default;
162
163 bool EvaluatePrecondition(StoppointCallbackContext &context) override;
164 void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
165 Status ConfigurePrecondition(Args &args) override;
166
167 protected:
168 void AddClassName(const char *class_name);
169
170 private:
171 std::unordered_set<std::string> m_class_names;
172 };
173
174 static lldb::BreakpointPreconditionSP
175 GetBreakpointExceptionPrecondition(lldb::LanguageType language,
176 bool throw_bp);
177
178 class TaggedPointerVendor {
179 public:
180 virtual ~TaggedPointerVendor() = default;
181
182 virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0;
183
184 virtual ObjCLanguageRuntime::ClassDescriptorSP
185 GetClassDescriptor(lldb::addr_t ptr) = 0;
186
187 protected:
188 TaggedPointerVendor() = default;
189
190 private:
191 DISALLOW_COPY_AND_ASSIGN(TaggedPointerVendor);
192 };
193
194 ~ObjCLanguageRuntime() override;
195
196 static char ID;
197
198 bool isA(const void *ClassID) const override {
199 return ClassID == &ID || LanguageRuntime::isA(ClassID);
200 }
201
202 static bool classof(const LanguageRuntime *runtime) {
203 return runtime->isA(&ID);
204 }
205
206 static ObjCLanguageRuntime *Get(Process &process) {
207 return llvm::cast_or_null<ObjCLanguageRuntime>(
208 process.GetLanguageRuntime(lldb::eLanguageTypeObjC));
209 }
210
211 virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
212
213 typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
214
215 virtual EncodingToTypeSP GetEncodingToType();
216
217 virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value);
218
219 ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value);
220
221 virtual ClassDescriptorSP
222 GetClassDescriptorFromClassName(ConstString class_name);
223
224 virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa);
225
226 ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa);
227
228 lldb::LanguageType GetLanguageType() const override {
229 return lldb::eLanguageTypeObjC;
230 }
231
232 virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
233
234 virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
235
236 virtual bool HasReadObjCLibrary() = 0;
237
238 lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel);
239
240 void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
241 lldb::addr_t impl_addr);
242
243 TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr);
244
245 void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
246 lldb::TypeSP type_sp);
247
248 void AddToClassNameCache(lldb::addr_t class_addr,
249 const TypeAndOrName &class_or_type_name);
250
251 lldb::TypeSP LookupInCompleteClassCache(ConstString &name);
252
253 virtual UtilityFunction *CreateObjectChecker(const char *) = 0;
254
255 virtual ObjCRuntimeVersions GetRuntimeVersion() const {
256 return ObjCRuntimeVersions::eObjC_VersionUnknown;
257 }
258
259 bool IsValidISA(ObjCISA isa) {
260 UpdateISAToDescriptorMap();
261 return m_isa_to_descriptor.count(isa) > 0;
262 }
263
264 virtual void UpdateISAToDescriptorMapIfNeeded() = 0;
265
266 void UpdateISAToDescriptorMap() {
267 if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) {
268 UpdateISAToDescriptorMapIfNeeded();
269 }
270 }
271
272 virtual ObjCISA GetISA(ConstString name);
273
274 virtual ConstString GetActualTypeName(ObjCISA isa);
275
276 virtual ObjCISA GetParentClass(ObjCISA isa);
277
278 // Finds the byte offset of the child_type ivar in parent_type. If it can't
279 // find the offset, returns LLDB_INVALID_IVAR_OFFSET.
280
281 virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
282 const char *ivar_name);
283
284 bool HasNewLiteralsAndIndexing() {
285 if (m_has_new_literals_and_indexing == eLazyBoolCalculate) {
286 if (CalculateHasNewLiteralsAndIndexing())
287 m_has_new_literals_and_indexing = eLazyBoolYes;
288 else
289 m_has_new_literals_and_indexing = eLazyBoolNo;
290 }
291
292 return (m_has_new_literals_and_indexing == eLazyBoolYes);
293 }
294
295 void SymbolsDidLoad(const ModuleList &module_list) override {
296 m_negative_complete_class_cache.clear();
297 }
298
299 bool GetTypeBitSize(const CompilerType &compiler_type,
300 uint64_t &size) override;
301
302 /// Check whether the name is "self" or "_cmd" and should show up in
303 /// "frame variable".
304 bool IsWhitelistedRuntimeValue(ConstString name) override;
305
306protected:
307 // Classes that inherit from ObjCLanguageRuntime can see and modify these
308 ObjCLanguageRuntime(Process *process);
309
310 virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
311
312 bool ISAIsCached(ObjCISA isa) const {
313 return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
314 }
315
316 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
317 if (isa != 0) {
318 m_isa_to_descriptor[isa] = descriptor_sp;
319 return true;
320 }
321 return false;
322 }
323
324 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
325 const char *class_name);
326
327 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
328 uint32_t class_name_hash) {
329 if (isa != 0) {
330 m_isa_to_descriptor[isa] = descriptor_sp;
331 m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));
332 return true;
333 }
334 return false;
335 }
336
337private:
338 // We keep a map of <Class,Selector>->Implementation so we don't have to call
339 // the resolver function over and over.
340
341 // FIXME: We need to watch for the loading of Protocols, and flush the cache
342 // for any
343 // class that we see so changed.
344
345 struct ClassAndSel {
346 ClassAndSel() {
347 sel_addr = LLDB_INVALID_ADDRESS;
348 class_addr = LLDB_INVALID_ADDRESS;
349 }
350
351 ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr)
352 : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
353
354 bool operator==(const ClassAndSel &rhs) {
355 if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
356 return true;
357 else
358 return false;
359 }
360
361 bool operator<(const ClassAndSel &rhs) const {
362 if (class_addr < rhs.class_addr)
363 return true;
364 else if (class_addr > rhs.class_addr)
365 return false;
366 else {
367 if (sel_addr < rhs.sel_addr)
368 return true;
369 else
370 return false;
371 }
372 }
373
374 lldb::addr_t class_addr;
375 lldb::addr_t sel_addr;
376 };
377
378 typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
379 typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
380 typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;
381 typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
382 typedef HashToISAMap::iterator HashToISAIterator;
383 typedef ThreadSafeDenseMap<void *, uint64_t> TypeSizeCache;
384
385 MsgImplMap m_impl_cache;
386 LazyBool m_has_new_literals_and_indexing;
387 ISAToDescriptorMap m_isa_to_descriptor;
388 HashToISAMap m_hash_to_isa_map;
389 TypeSizeCache m_type_size_cache;
390
391protected:
392 uint32_t m_isa_to_descriptor_stop_id;
393
394 typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
395 CompleteClassMap m_complete_class_cache;
396
397 struct ConstStringSetHelpers {
398 size_t operator()(ConstString arg) const // for hashing
399 {
400 return (size_t)arg.GetCString();
401 }
402 bool operator()(ConstString arg1,
403 ConstString arg2) const // for equality
404 {
405 return arg1.operator==(arg2);
406 }
407 };
408 typedef std::unordered_set<ConstString, ConstStringSetHelpers,
409 ConstStringSetHelpers>
410 CompleteClassSet;
411 CompleteClassSet m_negative_complete_class_cache;
412
413 ISAToDescriptorIterator GetDescriptorIterator(ConstString name);
414
415 friend class ::CommandObjectObjC_ClassTable_Dump;
416
417 std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
418 GetDescriptorIteratorPair(bool update_if_needed = true);
419
420 void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
421
422 DISALLOW_COPY_AND_ASSIGN(ObjCLanguageRuntime);
423};
424
425} // namespace lldb_private
426
427#endif // liblldb_ObjCLanguageRuntime_h_