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/Support/Casting.h"
19
23#include "lldb/Symbol/Type.h"
28#include "lldb/lldb-private.h"
29
31
32namespace lldb_private {
33
34class TypeSystemClang;
35class UtilityFunction;
36
38public:
41 eAppleObjC_V1 = 1,
42 eAppleObjC_V2 = 2,
44 };
45
47
48 class ClassDescriptor;
49 typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
50
51 // the information that we want to support retrieving from an ObjC class this
52 // needs to be pure virtual since there are at least 2 different
53 // implementations of the runtime, and more might come
55 public:
57
58 virtual ~ClassDescriptor() = default;
59
61
63
64 virtual ClassDescriptorSP GetMetaclass() const = 0;
65
66 // virtual if any implementation has some other version-specific rules but
67 // for the known v1/v2 this is all that needs to be done
68 virtual bool IsKVO() {
70 const char *class_name = GetClassName().AsCString();
71 if (class_name && *class_name)
72 m_is_kvo =
73 (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
74 }
75 return (m_is_kvo == eLazyBoolYes);
76 }
77
78 // virtual if any implementation has some other version-specific rules but
79 // for the known v1/v2 this is all that needs to be done
80 virtual bool IsCFType() {
82 const char *class_name = GetClassName().AsCString();
83 if (class_name && *class_name)
84 m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
85 strcmp(class_name, "NSCFType") == 0);
86 }
87 return (m_is_cf == eLazyBoolYes);
88 }
89
90 /// Determine whether this class is implemented in Swift.
93 }
94
95 virtual bool IsValid() = 0;
96
97 /// There are two routines in the ObjC runtime that tagged pointer clients
98 /// can call to get the value from their tagged pointer, one that retrieves
99 /// it as an unsigned value and one a signed value. These two
100 /// GetTaggedPointerInfo methods mirror those two ObjC runtime calls.
101 /// @{
102 virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
103 uint64_t *value_bits = nullptr,
104 uint64_t *payload = nullptr) = 0;
105
106 virtual bool GetTaggedPointerInfoSigned(uint64_t *info_bits = nullptr,
107 int64_t *value_bits = nullptr,
108 uint64_t *payload = nullptr) = 0;
109 /// @}
110
111 virtual uint64_t GetInstanceSize() = 0;
112
113 // use to implement version-specific additional constraints on pointers
114 virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
115 return true;
116 }
117
118 virtual ObjCISA GetISA() = 0;
119
120 // This should return true iff the interface could be completed
121 virtual bool
122 Describe(std::function<void(ObjCISA)> const &superclass_func,
123 std::function<bool(const char *, const char *)> const
124 &instance_method_func,
125 std::function<bool(const char *, const char *)> const
126 &class_method_func,
127 std::function<bool(const char *, const char *, lldb::addr_t,
128 uint64_t)> const &ivar_func) const {
129 return false;
130 }
131
132 lldb::TypeSP GetType() { return m_type_wp.lock(); }
133
134 void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
135
139 uint64_t m_size;
140 int32_t m_offset;
141 };
142
143 virtual size_t GetNumIVars() { return 0; }
144
145 virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
146 return iVarDescriptor();
147 }
148
149 protected:
150 bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
151 bool allow_NULLs = false, bool allow_tagged = false,
152 bool check_version_specific = false) const;
153
154 private:
158 };
159
161 public:
163
164 virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name,
165 bool for_expression) = 0;
166 virtual CompilerType RealizeType(const char *name, bool for_expression);
167
168 protected:
169 std::shared_ptr<TypeSystemClang> m_scratch_ast_ctx_sp;
170 };
171
173 public:
175
176 ~ObjCExceptionPrecondition() override = default;
177
178 bool EvaluatePrecondition(StoppointCallbackContext &context) override;
179 void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
180 Status ConfigurePrecondition(Args &args) override;
181
182 protected:
183 void AddClassName(const char *class_name);
184
185 private:
186 std::unordered_set<std::string> m_class_names;
187 };
188
191 bool throw_bp);
192
194 public:
195 virtual ~TaggedPointerVendor() = default;
196
198
201
202 protected:
204
205 private:
208 };
209
211
212 static char ID;
213
214 bool isA(const void *ClassID) const override {
215 return ClassID == &ID || LanguageRuntime::isA(ClassID);
216 }
217
218 static bool classof(const LanguageRuntime *runtime) {
219 return runtime->isA(&ID);
220 }
221
222 static ObjCLanguageRuntime *Get(Process &process) {
223 return llvm::cast_or_null<ObjCLanguageRuntime>(
225 }
226
227 virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
228
229 typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
230
232
234
236
237 virtual ClassDescriptorSP
239
241
243
246 }
247
248 virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
249
250 virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
251
252 virtual bool HasReadObjCLibrary() = 0;
253
254 // These two methods actually use different caches. The only time we'll
255 // cache a sel_str is if we found a "selector specific stub" for the selector
256 // and conversely we only add to the SEL cache if we saw a regular dispatch.
259 llvm::StringRef sel_str);
260
261 void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
262 lldb::addr_t impl_addr);
263
264 void AddToMethodCache(lldb::addr_t class_addr, llvm::StringRef sel_str,
265 lldb::addr_t impl_addr);
266
268
269 void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
270 lldb::TypeSP type_sp);
271
273 const TypeAndOrName &class_or_type_name);
274
276
277 std::optional<CompilerType> GetRuntimeType(CompilerType base_type) override;
278
279 virtual llvm::Expected<std::unique_ptr<UtilityFunction>>
280 CreateObjectChecker(std::string name, ExecutionContext &exe_ctx) = 0;
281
284 }
285
286 bool IsValidISA(ObjCISA isa) {
288 return m_isa_to_descriptor.count(isa) > 0;
289 }
290
292
296 }
297 }
298
299 virtual ObjCISA GetISA(ConstString name);
300
301 virtual ObjCISA GetParentClass(ObjCISA isa);
302
303 // Finds the byte offset of the child_type ivar in parent_type. If it can't
304 // find the offset, returns LLDB_INVALID_IVAR_OFFSET.
305
306 virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
307 const char *ivar_name);
308
313 else
315 }
316
318 }
319
320 void SymbolsDidLoad(const ModuleList &module_list) override {
322 }
323
324 bool GetTypeBitSize(const CompilerType &compiler_type,
325 uint64_t &size) override;
326
327 /// Check whether the name is "self" or "_cmd" and should show up in
328 /// "frame variable".
329 bool IsAllowedRuntimeValue(ConstString name) override;
330
331protected:
332 // Classes that inherit from ObjCLanguageRuntime can see and modify these
334
335 virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
336
337 bool ISAIsCached(ObjCISA isa) const {
338 return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
339 }
340
341 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
342 if (isa != 0) {
343 m_isa_to_descriptor[isa] = descriptor_sp;
344 return true;
345 }
346 return false;
347 }
348
349 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
350 const char *class_name);
351
352 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
353 uint32_t class_name_hash) {
354 if (isa != 0) {
355 m_isa_to_descriptor[isa] = descriptor_sp;
356 m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));
357 return true;
358 }
359 return false;
360 }
361
362private:
363 // We keep two maps of <Class,Selector>->Implementation so we don't have
364 // to call the resolver function over and over.
365 // The first comes from regular obj_msgSend type dispatch, and maps the
366 // class + uniqued SEL value to an implementation.
367 // The second comes from the "selector-specific stubs", which are always
368 // of the form _objc_msgSend$SelectorName, so we don't know the uniqued
369 // selector, only the string name.
370
371 // FIXME: We need to watch for the loading of Protocols, and flush the cache
372 // for any
373 // class that we see so changed.
374
375 struct ClassAndSel {
376 ClassAndSel() = default;
377
378 ClassAndSel(lldb::addr_t in_class_addr, lldb::addr_t in_sel_addr)
379 : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
380
381 bool operator==(const ClassAndSel &rhs) {
382 if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
383 return true;
384 else
385 return false;
386 }
387
388 bool operator<(const ClassAndSel &rhs) const {
389 if (class_addr < rhs.class_addr)
390 return true;
391 else if (class_addr > rhs.class_addr)
392 return false;
393 else {
394 if (sel_addr < rhs.sel_addr)
395 return true;
396 else
397 return false;
398 }
399 }
400
403 };
404
406 ClassAndSelStr() = default;
407
408 ClassAndSelStr(lldb::addr_t in_class_addr, llvm::StringRef in_sel_name)
409 : class_addr(in_class_addr), sel_name(in_sel_name) {}
410
411 bool operator==(const ClassAndSelStr &rhs) {
412 return class_addr == rhs.class_addr && sel_name == rhs.sel_name;
413 }
414
415 bool operator<(const ClassAndSelStr &rhs) const {
416 if (class_addr < rhs.class_addr)
417 return true;
418 else if (class_addr > rhs.class_addr)
419 return false;
420 else
422 }
423
426 };
427
428 typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
429 typedef std::map<ClassAndSelStr, lldb::addr_t> MsgImplStrMap;
430 typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
431 typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;
432 typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
433 typedef HashToISAMap::iterator HashToISAIterator;
435
442
443protected:
445
446 typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
448
450 size_t operator()(ConstString arg) const // for hashing
451 {
452 return (size_t)arg.GetCString();
453 }
455 ConstString arg2) const // for equality
456 {
457 return arg1.operator==(arg2);
458 }
459 };
460 typedef std::unordered_set<ConstString, ConstStringSetHelpers,
461 ConstStringSetHelpers>
464
466
467 friend class ::CommandObjectObjC_ClassTable_Dump;
468
469 std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
470 GetDescriptorIteratorPair(bool update_if_needed = true);
471
472 void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
473
476};
477
478} // namespace lldb_private
479
480#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.
Definition: CompilerType.h:36
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=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
"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:103
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 iVarDescriptor GetIVarAtIndex(size_t idx)
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
HashToISAMap::iterator HashToISAIterator
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::multimap< uint32_t, ObjCISA > HashToISAMap
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::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
bool GetTypeBitSize(const CompilerType &compiler_type, uint64_t &size) 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< ObjCISA, ClassDescriptorSP > ISAToDescriptorMap
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)
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)
virtual llvm::Expected< std::unique_ptr< UtilityFunction > > CreateObjectChecker(std::string name, ExecutionContext &exe_ctx)=0
std::map< ClassAndSel, lldb::addr_t > MsgImplMap
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:340
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition: Process.cpp:1522
uint32_t GetStopID() const
Definition: Process.h:1475
Process * m_process
Definition: Runtime.h:29
An error handling class.
Definition: Status.h:44
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:712
A TypeSystem implementation based on Clang.
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
LanguageType
Programming language type.
@ eLanguageTypeObjC
Objective-C.
std::shared_ptr< lldb_private::Type > TypeSP
Definition: lldb-forward.h:449
std::shared_ptr< lldb_private::BreakpointPrecondition > BreakpointPreconditionSP
Definition: lldb-forward.h:319
std::weak_ptr< lldb_private::Type > TypeWP
Definition: lldb-forward.h:450
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365
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