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