LLDB mainline
ObjCLanguageRuntime.h
Go to the documentation of this file.
1//===-- 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 LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
10#define LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
11
12#include <functional>
13#include <map>
14#include <memory>
15#include <optional>
16#include <unordered_set>
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Support/Casting.h"
21
25#include "lldb/Symbol/Type.h"
30#include "lldb/lldb-private.h"
31
33
34namespace lldb_private {
35
36class TypeSystemClang;
37class UtilityFunction;
38
40public:
47
49
50 class ClassDescriptor;
51 typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
52
53 // the information that we want to support retrieving from an ObjC class this
54 // needs to be pure virtual since there are at least 2 different
55 // implementations of the runtime, and more might come
57 public:
59
60 virtual ~ClassDescriptor() = default;
61
63
65
66 virtual ClassDescriptorSP GetMetaclass() const = 0;
67
68 // virtual if any implementation has some other version-specific rules but
69 // for the known v1/v2 this is all that needs to be done
70 virtual bool IsKVO() {
72 const char *class_name = GetClassName().AsCString(nullptr);
73 if (class_name && *class_name)
74 m_is_kvo =
75 (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
76 }
77 return (m_is_kvo == eLazyBoolYes);
78 }
79
80 // virtual if any implementation has some other version-specific rules but
81 // for the known v1/v2 this is all that needs to be done
82 virtual bool IsCFType() {
84 const char *class_name = GetClassName().AsCString(nullptr);
85 if (class_name && *class_name)
86 m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
87 strcmp(class_name, "NSCFType") == 0);
88 }
89 return (m_is_cf == eLazyBoolYes);
90 }
91
92 /// Determine whether this class is implemented in Swift.
96
97 virtual bool IsValid() = 0;
98
99 /// There are two routines in the ObjC runtime that tagged pointer clients
100 /// can call to get the value from their tagged pointer, one that retrieves
101 /// it as an unsigned value and one a signed value. These two
102 /// GetTaggedPointerInfo methods mirror those two ObjC runtime calls.
103 /// @{
104 virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
105 uint64_t *value_bits = nullptr,
106 uint64_t *payload = nullptr) = 0;
107
108 virtual bool GetTaggedPointerInfoSigned(uint64_t *info_bits = nullptr,
109 int64_t *value_bits = nullptr,
110 uint64_t *payload = nullptr) = 0;
111 /// @}
112
113 virtual uint64_t GetInstanceSize() = 0;
114
115 // use to implement version-specific additional constraints on pointers
116 virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
117 return true;
118 }
119
120 virtual ObjCISA GetISA() = 0;
121
122 // This should return true iff the interface could be completed
123 virtual bool
124 Describe(std::function<void(ObjCISA)> const &superclass_func,
125 std::function<bool(const char *, const char *)> const
126 &instance_method_func,
127 std::function<bool(const char *, const char *)> const
128 &class_method_func,
129 std::function<bool(const char *, const char *, lldb::addr_t,
130 uint64_t)> const &ivar_func) const {
131 return false;
132 }
133
134 lldb::TypeSP GetType() { return m_type_wp.lock(); }
135
136 void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
137
144
145 virtual size_t GetNumIVars() { return 0; }
146
147 virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
148 return iVarDescriptor();
149 }
150
151 protected:
152 bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
153 bool allow_NULLs = false, bool allow_tagged = false,
154 bool check_version_specific = false) const;
155
156 private:
160 };
161
163 public:
165
166 virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name,
167 bool for_expression) = 0;
168 virtual CompilerType RealizeType(const char *name, bool for_expression);
169
170 protected:
171 std::shared_ptr<TypeSystemClang> m_scratch_ast_ctx_sp;
172 };
173
175 public:
177
178 ~ObjCExceptionPrecondition() override = default;
179
180 bool EvaluatePrecondition(StoppointCallbackContext &context) override;
181 void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
182 Status ConfigurePrecondition(Args &args) override;
183
184 protected:
185 void AddClassName(const char *class_name);
186
187 private:
188 std::unordered_set<std::string> m_class_names;
189 };
190
193 bool throw_bp);
194
196 public:
197 virtual ~TaggedPointerVendor() = default;
198
200
203
204 protected:
206
207 private:
210 };
211
213
214 static char ID;
215
216 bool isA(const void *ClassID) const override {
217 return ClassID == &ID || LanguageRuntime::isA(ClassID);
218 }
219
220 static bool classof(const LanguageRuntime *runtime) {
221 return runtime->isA(&ID);
222 }
223
224 static ObjCLanguageRuntime *Get(Process &process) {
225 return llvm::cast_or_null<ObjCLanguageRuntime>(
227 }
228
229 virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
230
231 typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
232
234
236
238
239 virtual ClassDescriptorSP
241
243
245
248 }
249
250 virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
251
252 virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
253
254 virtual bool HasReadObjCLibrary() = 0;
255
256 // These two methods actually use different caches. The only time we'll
257 // cache a sel_str is if we found a "selector specific stub" for the selector
258 // and conversely we only add to the SEL cache if we saw a regular dispatch.
261 llvm::StringRef sel_str);
262
263 void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
264 lldb::addr_t impl_addr);
265
266 void AddToMethodCache(lldb::addr_t class_addr, llvm::StringRef sel_str,
267 lldb::addr_t impl_addr);
268
270
271 void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
272 lldb::TypeSP type_sp);
273
275 const TypeAndOrName &class_or_type_name);
276
278
279 std::optional<CompilerType> GetRuntimeType(CompilerType base_type) override;
280
281 virtual llvm::Expected<std::unique_ptr<UtilityFunction>>
282 CreateObjectChecker(std::string name, ExecutionContext &exe_ctx) = 0;
283
287
288 bool IsValidISA(ObjCISA isa) {
290 return m_isa_to_descriptor.count(isa) > 0;
291 }
292
294
300
301 virtual ObjCISA GetISA(ConstString name);
302
303 virtual ObjCISA GetParentClass(ObjCISA isa);
304
305 // Finds the byte offset of the child_type ivar in parent_type. If it can't
306 // find the offset, returns LLDB_INVALID_IVAR_OFFSET.
307
308 virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
309 const char *ivar_name);
310
321
322 void SymbolsDidLoad(const ModuleList &module_list) override {
324 }
325
326 std::optional<uint64_t>
327 GetTypeBitSize(const CompilerType &compiler_type) override;
328
329 /// Check whether the name is "self" or "_cmd" and should show up in
330 /// "frame variable".
331 bool IsAllowedRuntimeValue(ConstString name) override;
332
333protected:
334 // Classes that inherit from ObjCLanguageRuntime can see and modify these
336
337 virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
338
339 bool ISAIsCached(ObjCISA isa) const {
340 return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
341 }
342
343 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
344 if (isa != 0) {
345 m_isa_to_descriptor.insert_or_assign(isa, descriptor_sp);
346 return true;
347 }
348 return false;
349 }
350
351 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
352 const char *class_name);
353
354 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
355 uint32_t class_name_hash) {
356 if (isa != 0) {
357 m_isa_to_descriptor.insert_or_assign(isa, descriptor_sp);
358 m_hash_to_isa_map[class_name_hash].push_back(isa);
359 return true;
360 }
361 return false;
362 }
363
364private:
365 // We keep two maps of <Class,Selector>->Implementation so we don't have
366 // to call the resolver function over and over.
367 // The first comes from regular obj_msgSend type dispatch, and maps the
368 // class + uniqued SEL value to an implementation.
369 // The second comes from the "selector-specific stubs", which are always
370 // of the form _objc_msgSend$SelectorName, so we don't know the uniqued
371 // selector, only the string name.
372
373 // FIXME: We need to watch for the loading of Protocols, and flush the cache
374 // for any
375 // class that we see so changed.
376
377 struct ClassAndSel {
378 ClassAndSel() = default;
379
380 ClassAndSel(lldb::addr_t in_class_addr, lldb::addr_t in_sel_addr)
381 : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
382
383 bool operator==(const ClassAndSel &rhs) {
384 if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
385 return true;
386 else
387 return false;
388 }
389
390 bool operator<(const ClassAndSel &rhs) const {
391 return std::tie(class_addr, sel_addr) <
392 std::tie(rhs.class_addr, rhs.sel_addr);
393 }
394
397 };
398
400 ClassAndSelStr() = default;
401
402 ClassAndSelStr(lldb::addr_t in_class_addr, llvm::StringRef in_sel_name)
403 : class_addr(in_class_addr), sel_name(in_sel_name) {}
404
405 bool operator==(const ClassAndSelStr &rhs) {
406 return class_addr == rhs.class_addr && sel_name == rhs.sel_name;
407 }
408
409 bool operator<(const ClassAndSelStr &rhs) const {
410 if (class_addr < rhs.class_addr)
411 return true;
412 else if (class_addr > rhs.class_addr)
413 return false;
414 else
416 }
417
420 };
421
422 typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
423 typedef std::map<ClassAndSelStr, lldb::addr_t> MsgImplStrMap;
424 typedef llvm::DenseMap<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
425
426 /// Keys are already djbHash values, so use identity as the hash function.
428 static constexpr uint32_t getEmptyKey() { return ~0U; }
429 static unsigned getHashValue(uint32_t Val) { return Val; }
430 static bool isEqual(uint32_t LHS, uint32_t RHS) { return LHS == RHS; }
431 };
432
433 typedef llvm::SmallVector<ObjCISA, 2> ISAVector;
434 typedef llvm::DenseMap<uint32_t, ISAVector, IdentityHashKeyInfo> HashToISAMap;
435 typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
437
444
445protected:
447
448 typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
450
452 size_t operator()(ConstString arg) const // for hashing
453 {
454 return (size_t)arg.GetCString();
455 }
457 ConstString arg2) const // for equality
458 {
459 return arg1.operator==(arg2);
460 }
461 };
462 typedef std::unordered_set<ConstString, ConstStringSetHelpers,
463 ConstStringSetHelpers>
466
468
469 friend class ::CommandObjectObjC_ClassTable_Dump;
470
471 std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
472 GetDescriptorIteratorPair(bool update_if_needed = true);
473
474 void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
475
478
479private:
482};
483
484} // namespace lldb_private
485
486#endif // LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
A command line argument class.
Definition Args.h:33
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
static int Compare(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Compare two string objects.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
virtual bool isA(const void *ClassID) const
A collection class for Module objects.
Definition ModuleList.h:125
virtual bool GetTaggedPointerInfoSigned(uint64_t *info_bits=nullptr, int64_t *value_bits=nullptr, uint64_t *payload=nullptr)=0
virtual bool GetTaggedPointerInfo(uint64_t *info_bits=nullptr, uint64_t *value_bits=nullptr, uint64_t *payload=nullptr)=0
There are two routines in the ObjC runtime that tagged pointer clients can call to get the value from...
virtual ClassDescriptorSP GetMetaclass() const =0
bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs=false, bool allow_tagged=false, bool check_version_specific=false) const
virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const
virtual bool Describe(std::function< void(ObjCISA)> const &superclass_func, std::function< bool(const char *, const char *)> const &instance_method_func, std::function< bool(const char *, const char *)> const &class_method_func, std::function< bool(const char *, const char *, lldb::addr_t, uint64_t)> const &ivar_func) const
virtual lldb::LanguageType GetImplementationLanguage() const
Determine whether this class is implemented in Swift.
virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name, bool for_expression)=0
std::shared_ptr< TypeSystemClang > m_scratch_ast_ctx_sp
void GetDescription(Stream &stream, lldb::DescriptionLevel level) override
bool EvaluatePrecondition(StoppointCallbackContext &context) override
virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr)=0
TaggedPointerVendor(const TaggedPointerVendor &)=delete
const TaggedPointerVendor & operator=(const TaggedPointerVendor &)=delete
virtual ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(lldb::addr_t ptr)=0
virtual ObjCISA GetISA(ConstString name)
TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr)
void AddToClassNameCache(lldb::addr_t class_addr, const char *name, lldb::TypeSP type_sp)
std::map< ConstString, lldb::TypeWP > CompleteClassMap
virtual EncodingToTypeSP GetEncodingToType()
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp)
virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp)=0
virtual ObjCRuntimeVersions GetRuntimeVersion() const
ISAToDescriptorMap::iterator ISAToDescriptorIterator
const ObjCLanguageRuntime & operator=(const ObjCLanguageRuntime &)=delete
ThreadSafeDenseMap< void *, uint64_t > TypeSizeCache
std::optional< uint64_t > GetTypeBitSize(const CompilerType &compiler_type) override
std::pair< ISAToDescriptorIterator, ISAToDescriptorIterator > GetDescriptorIteratorPair(bool update_if_needed=true)
virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type, const char *ivar_name)
ISAToDescriptorIterator GetDescriptorIterator(ConstString name)
std::unordered_set< ConstString, ConstStringSetHelpers, ConstStringSetHelpers > CompleteClassSet
bool isA(const void *ClassID) const override
void AddToClassNameCache(lldb::addr_t class_addr, const TypeAndOrName &class_or_type_name)
lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel)
virtual TaggedPointerVendor * GetTaggedPointerVendor()
std::map< ClassAndSelStr, lldb::addr_t > MsgImplStrMap
lldb::TypeSP LookupInCompleteClassCache(ConstString &name)
static bool classof(const LanguageRuntime *runtime)
virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp)=0
ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value)
virtual void UpdateISAToDescriptorMapIfNeeded()=0
std::optional< CompilerType > GetRuntimeType(CompilerType base_type) override
void ReadObjCLibraryIfNeeded(const ModuleList &module_list)
virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa)
CompilerType LookupInRuntime(ConstString class_name)
ObjCLanguageRuntime(const ObjCLanguageRuntime &)=delete
void SymbolsDidLoad(const ModuleList &module_list) override
bool IsAllowedRuntimeValue(ConstString name) override
Check whether the name is "self" or "_cmd" and should show up in "frame variable".
static ObjCLanguageRuntime * Get(Process &process)
bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp, uint32_t class_name_hash)
llvm::DenseMap< ObjCISA, ClassDescriptorSP > ISAToDescriptorMap
virtual llvm::Expected< std::unique_ptr< UtilityFunction > > CreateObjectChecker(std::string name, ExecutionContext &exe_ctx)=0
llvm::SmallVector< ObjCISA, 2 > ISAVector
std::map< ClassAndSel, lldb::addr_t > MsgImplMap
llvm::DenseMap< uint32_t, ISAVector, IdentityHashKeyInfo > HashToISAMap
CompilerType LookupInModulesVendor(ConstString class_name, Target &process)
virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value)
virtual ClassDescriptorSP GetClassDescriptorFromClassName(ConstString class_name)
void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel, lldb::addr_t impl_addr)
lldb::LanguageType GetLanguageType() const override
static lldb::BreakpointPreconditionSP GetBreakpointExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
virtual ObjCISA GetParentClass(ObjCISA isa)
std::shared_ptr< EncodingToType > EncodingToTypeSP
A plug-in interface definition class for debugging a process.
Definition Process.h:357
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition Process.cpp:1508
Process * m_process
Definition Runtime.h:29
An error handling class.
Definition Status.h:118
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
Definition Type.h:780
A TypeSystem implementation based on Clang.
"lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that provides a function that i...
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
LanguageType
Programming language type.
@ eLanguageTypeObjC
Objective-C.
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::BreakpointPrecondition > BreakpointPreconditionSP
std::weak_ptr< lldb_private::Type > TypeWP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
ClassAndSelStr(lldb::addr_t in_class_addr, llvm::StringRef in_sel_name)
bool operator<(const ClassAndSelStr &rhs) const
bool operator<(const ClassAndSel &rhs) const
ClassAndSel(lldb::addr_t in_class_addr, lldb::addr_t in_sel_addr)
bool operator()(ConstString arg1, ConstString arg2) const
Keys are already djbHash values, so use identity as the hash function.